blob: 88c613eab142962dc44f2075378fce0b94349e8e [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>
Willem de Bruijnb245be12015-01-30 13:29:32 -050077#include <linux/capability.h>
78#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079
Eric Dumazetd7e88832012-04-30 08:10:34 +000080struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/**
Jean Sacrenf05de732013-02-11 13:30:38 +000084 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000088 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Jean Sacrenf05de732013-02-11 13:30:38 +000090 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Jean Sacrenf05de732013-02-11 13:30:38 +000095static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000096 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Joe Perchese005d192012-05-16 19:58:40 +000098 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 +000099 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 BUG();
103}
104
Jean Sacrenf05de732013-02-11 13:30:38 +0000105static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jean Sacrenf05de732013-02-11 13:30:38 +0000107 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Jean Sacrenf05de732013-02-11 13:30:38 +0000110static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111{
112 skb_panic(skb, sz, addr, __func__);
113}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700114
115/*
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
121 */
122#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000124
125static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700127{
128 void *obj;
129 bool ret_pfmemalloc = false;
130
131 /*
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
134 */
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
140
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
144
145out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
148
149 return obj;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
155 *
156 */
157
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000158struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
159{
160 struct sk_buff *skb;
161
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
167
168 /*
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
172 */
173 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000174 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
177
Cong Wang35d04612013-05-29 15:16:05 +0800178 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000179out:
180 return skb;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
David S. Millerd179cd12005-08-17 14:57:30 -0700184 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * @size: size to allocate
186 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800191 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
199 */
Al Virodd0fc662005-10-07 07:46:04 +0100200struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700201 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Christoph Lametere18b8902006-12-06 20:33:20 -0800203 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800204 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct sk_buff *skb;
206 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
211
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!skb)
218 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700219 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
225 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000226 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!data)
230 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
234 */
235 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700236 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300238 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300242 */
243 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700246 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700250 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700251 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000254
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000259 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800260
Mel Gormanc93bdd02012-07-31 16:44:19 -0700261 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
265
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700268 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700269
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700271 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273out:
274 return skb;
275nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800276 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb = NULL;
278 goto out;
279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800280EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283 * build_skb - build a network buffer
284 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000285 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100288 * skb_shared_info. @data must have been allocated by kmalloc() only if
289 * @frag_size is 0, otherwise data should come from the page allocator.
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000290 * The return is the new skb buffer.
291 * On a failure the return is %NULL, and @data is not freed.
292 * Notes :
293 * Before IO, driver allocates only data buffer where NIC put incoming frame
294 * Driver should add room at head (NET_SKB_PAD) and
295 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
296 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
297 * before giving packet to stack.
298 * RX rings only contains data buffers, not full skbs.
299 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000300struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000301{
302 struct skb_shared_info *shinfo;
303 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000304 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000305
306 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
307 if (!skb)
308 return NULL;
309
Eric Dumazetd3836f22012-04-27 00:33:38 +0000310 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000311
312 memset(skb, 0, offsetof(struct sk_buff, tail));
313 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000314 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000322
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
328
329 return skb;
330}
331EXPORT_SYMBOL(build_skb);
332
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000333struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000334 struct page_frag frag;
335 /* we maintain a pagecount bias, so that we dont dirty cache line
336 * containing page->_count every time we allocate a fragment.
337 */
338 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000339};
340static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800341static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000342
Alexander Duyckffde7322014-12-09 19:40:42 -0800343static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
344 gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000345{
Alexander Duyckffde7322014-12-09 19:40:42 -0800346 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
347 struct page *page = NULL;
348 gfp_t gfp = gfp_mask;
Eric Dumazet6f532612012-05-18 05:12:12 +0000349
Alexander Duyckffde7322014-12-09 19:40:42 -0800350 if (order) {
351 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
352 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
353 nc->frag.size = PAGE_SIZE << (page ? order : 0);
354 }
355
356 if (unlikely(!page))
357 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
358
359 nc->frag.page = page;
360
361 return page;
362}
363
364static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
365 unsigned int fragsz, gfp_t gfp_mask)
366{
367 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
368 struct page *page = nc->frag.page;
369 unsigned int size;
370 int offset;
371
372 if (unlikely(!page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000373refill:
Alexander Duyckffde7322014-12-09 19:40:42 -0800374 page = __page_frag_refill(nc, gfp_mask);
375 if (!page)
376 return NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000377
Alexander Duyckffde7322014-12-09 19:40:42 -0800378 /* if size can vary use frag.size else just use PAGE_SIZE */
379 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
380
Eric Dumazet4c450582014-10-10 04:48:18 -0700381 /* Even if we own the page, we do not use atomic_set().
382 * This would break get_page_unless_zero() users.
383 */
Alexander Duyckffde7322014-12-09 19:40:42 -0800384 atomic_add(size - 1, &page->_count);
385
386 /* reset page count bias and offset to start of new frag */
387 nc->pagecnt_bias = size;
388 nc->frag.offset = size;
Eric Dumazet6f532612012-05-18 05:12:12 +0000389 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000390
Alexander Duyckffde7322014-12-09 19:40:42 -0800391 offset = nc->frag.offset - fragsz;
392 if (unlikely(offset < 0)) {
393 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
394 goto refill;
395
396 /* if size can vary use frag.size else just use PAGE_SIZE */
397 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
398
399 /* OK, page count is 0, we can safely set it */
400 atomic_set(&page->_count, size);
401
402 /* reset page count bias and offset to start of new frag */
403 nc->pagecnt_bias = size;
404 offset = size - fragsz;
Eric Dumazet6f532612012-05-18 05:12:12 +0000405 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000406
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000407 nc->pagecnt_bias--;
Alexander Duyckffde7322014-12-09 19:40:42 -0800408 nc->frag.offset = offset;
409
410 return page_address(page) + offset;
411}
412
413static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
414{
415 unsigned long flags;
416 void *data;
417
418 local_irq_save(flags);
419 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000420 local_irq_restore(flags);
421 return data;
422}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700423
424/**
425 * netdev_alloc_frag - allocate a page fragment
426 * @fragsz: fragment size
427 *
428 * Allocates a frag from a page for receive buffer.
429 * Uses GFP_ATOMIC allocations.
430 */
431void *netdev_alloc_frag(unsigned int fragsz)
432{
433 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
434}
Eric Dumazet6f532612012-05-18 05:12:12 +0000435EXPORT_SYMBOL(netdev_alloc_frag);
436
Alexander Duyckffde7322014-12-09 19:40:42 -0800437static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
438{
439 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
440}
441
442void *napi_alloc_frag(unsigned int fragsz)
443{
444 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
445}
446EXPORT_SYMBOL(napi_alloc_frag);
447
Eric Dumazet6f532612012-05-18 05:12:12 +0000448/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800449 * __alloc_rx_skb - allocate an skbuff for rx
Christoph Hellwig8af27452006-07-31 22:35:23 -0700450 * @length: length to allocate
451 * @gfp_mask: get_free_pages mask, passed to alloc_skb
Alexander Duyckfd11a832014-12-09 19:40:49 -0800452 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
453 * allocations in case we have to fallback to __alloc_skb()
454 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
455 * from napi_cache instead of netdev_cache.
Christoph Hellwig8af27452006-07-31 22:35:23 -0700456 *
457 * Allocate a new &sk_buff and assign it a usage count of one. The
458 * buffer has unspecified headroom built in. Users should allocate
459 * the headroom they think they need without accounting for the
460 * built in space. The built in space is used for optimisations.
461 *
462 * %NULL is returned if there is no free memory.
463 */
Alexander Duyckfd11a832014-12-09 19:40:49 -0800464static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
465 int flags)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700466{
Eric Dumazet6f532612012-05-18 05:12:12 +0000467 struct sk_buff *skb = NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800468 unsigned int fragsz = SKB_DATA_ALIGN(length) +
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000469 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700470
Eric Dumazet310e1582012-07-16 13:15:52 +0200471 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700472 void *data;
473
474 if (sk_memalloc_socks())
475 gfp_mask |= __GFP_MEMALLOC;
476
Alexander Duyckfd11a832014-12-09 19:40:49 -0800477 data = (flags & SKB_ALLOC_NAPI) ?
478 __napi_alloc_frag(fragsz, gfp_mask) :
479 __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000480
Eric Dumazet6f532612012-05-18 05:12:12 +0000481 if (likely(data)) {
482 skb = build_skb(data, fragsz);
483 if (unlikely(!skb))
484 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000485 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000486 } else {
Alexander Duyckfd11a832014-12-09 19:40:49 -0800487 skb = __alloc_skb(length, gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700488 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000489 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490 return skb;
491}
492
493/**
494 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
495 * @dev: network device to receive on
496 * @length: length to allocate
497 * @gfp_mask: get_free_pages mask, passed to alloc_skb
498 *
499 * Allocate a new &sk_buff and assign it a usage count of one. The
500 * buffer has NET_SKB_PAD headroom built in. Users should allocate
501 * the headroom they think they need without accounting for the
502 * built in space. The built in space is used for optimisations.
503 *
504 * %NULL is returned if there is no free memory.
505 */
506struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
507 unsigned int length, gfp_t gfp_mask)
508{
509 struct sk_buff *skb;
510
511 length += NET_SKB_PAD;
512 skb = __alloc_rx_skb(length, gfp_mask, 0);
513
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700514 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700515 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700516 skb->dev = dev;
517 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800518
Christoph Hellwig8af27452006-07-31 22:35:23 -0700519 return skb;
520}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800521EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alexander Duyckfd11a832014-12-09 19:40:49 -0800523/**
524 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
525 * @napi: napi instance this buffer was allocated for
526 * @length: length to allocate
527 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
528 *
529 * Allocate a new sk_buff for use in NAPI receive. This buffer will
530 * attempt to allocate the head from a special reserved region used
531 * only for NAPI Rx allocation. By doing this we can save several
532 * CPU cycles by avoiding having to disable and re-enable IRQs.
533 *
534 * %NULL is returned if there is no free memory.
535 */
536struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
537 unsigned int length, gfp_t gfp_mask)
538{
539 struct sk_buff *skb;
540
541 length += NET_SKB_PAD + NET_IP_ALIGN;
542 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
543
544 if (likely(skb)) {
545 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
546 skb->dev = napi->dev;
547 }
548
549 return skb;
550}
551EXPORT_SYMBOL(__napi_alloc_skb);
552
Peter Zijlstra654bed12008-10-07 14:22:33 -0700553void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000554 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700555{
556 skb_fill_page_desc(skb, i, page, off, size);
557 skb->len += size;
558 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000559 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700560}
561EXPORT_SYMBOL(skb_add_rx_frag);
562
Jason Wangf8e617e2013-11-01 14:07:47 +0800563void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
564 unsigned int truesize)
565{
566 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
567
568 skb_frag_size_add(frag, size);
569 skb->len += size;
570 skb->data_len += size;
571 skb->truesize += truesize;
572}
573EXPORT_SYMBOL(skb_coalesce_rx_frag);
574
Herbert Xu27b437c2006-07-13 19:26:39 -0700575static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700577 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700578 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Herbert Xu27b437c2006-07-13 19:26:39 -0700581static inline void skb_drop_fraglist(struct sk_buff *skb)
582{
583 skb_drop_list(&skb_shinfo(skb)->frag_list);
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static void skb_clone_fraglist(struct sk_buff *skb)
587{
588 struct sk_buff *list;
589
David S. Millerfbb398a2009-06-09 00:18:59 -0700590 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 skb_get(list);
592}
593
Eric Dumazetd3836f22012-04-27 00:33:38 +0000594static void skb_free_head(struct sk_buff *skb)
595{
596 if (skb->head_frag)
597 put_page(virt_to_head_page(skb->head));
598 else
599 kfree(skb->head);
600}
601
Adrian Bunk5bba1712006-06-29 13:02:35 -0700602static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Eric Dumazetff04a772014-09-23 18:39:30 -0700604 struct skb_shared_info *shinfo = skb_shinfo(skb);
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Eric Dumazetff04a772014-09-23 18:39:30 -0700607 if (skb->cloned &&
608 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
609 &shinfo->dataref))
610 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000611
Eric Dumazetff04a772014-09-23 18:39:30 -0700612 for (i = 0; i < shinfo->nr_frags; i++)
613 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000614
Eric Dumazetff04a772014-09-23 18:39:30 -0700615 /*
616 * If skb buf is from userspace, we need to notify the caller
617 * the lower device DMA has done;
618 */
619 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
620 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Eric Dumazetff04a772014-09-23 18:39:30 -0700622 uarg = shinfo->destructor_arg;
623 if (uarg->callback)
624 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700626
627 if (shinfo->frag_list)
628 kfree_skb_list(shinfo->frag_list);
629
630 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
634 * Free an skbuff by memory without cleaning the state.
635 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800636static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700638 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700639
David S. Millerd179cd12005-08-17 14:57:30 -0700640 switch (skb->fclone) {
641 case SKB_FCLONE_UNAVAILABLE:
642 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800643 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700644
645 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700646 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800647
648 /* We usually free the clone (TX completion) before original skb
649 * This test would have no chance to be true for the clone,
650 * while here, branch prediction will be good.
651 */
652 if (atomic_read(&fclones->fclone_ref) == 1)
653 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700654 break;
655
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800656 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700657 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700658 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700659 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800660 if (!atomic_dec_and_test(&fclones->fclone_ref))
661 return;
662fastpath:
663 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700666static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Eric Dumazetadf30902009-06-02 05:19:30 +0000668 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#ifdef CONFIG_XFRM
670 secpath_put(skb->sp);
671#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700672 if (skb->destructor) {
673 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 skb->destructor(skb);
675 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000676#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700677 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100678#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200679#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 nf_bridge_put(skb->nf_bridge);
681#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700682}
683
684/* Free everything but the sk_buff shell. */
685static void skb_release_all(struct sk_buff *skb)
686{
687 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000688 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000689 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Herbert Xu2d4baff2007-11-26 23:11:19 +0800692/**
693 * __kfree_skb - private function
694 * @skb: buffer
695 *
696 * Free an sk_buff. Release anything attached to the buffer.
697 * Clean the state. This is an internal helper function. Users should
698 * always call kfree_skb
699 */
700
701void __kfree_skb(struct sk_buff *skb)
702{
703 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 kfree_skbmem(skb);
705}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800706EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800709 * kfree_skb - free an sk_buff
710 * @skb: buffer to free
711 *
712 * Drop a reference to the buffer and free it if the usage count has
713 * hit zero.
714 */
715void kfree_skb(struct sk_buff *skb)
716{
717 if (unlikely(!skb))
718 return;
719 if (likely(atomic_read(&skb->users) == 1))
720 smp_rmb();
721 else if (likely(!atomic_dec_and_test(&skb->users)))
722 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000723 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800724 __kfree_skb(skb);
725}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800726EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800727
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700728void kfree_skb_list(struct sk_buff *segs)
729{
730 while (segs) {
731 struct sk_buff *next = segs->next;
732
733 kfree_skb(segs);
734 segs = next;
735 }
736}
737EXPORT_SYMBOL(kfree_skb_list);
738
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700739/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000740 * skb_tx_error - report an sk_buff xmit error
741 * @skb: buffer that triggered an error
742 *
743 * Report xmit error if a device callback is tracking this skb.
744 * skb must be freed afterwards.
745 */
746void skb_tx_error(struct sk_buff *skb)
747{
748 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
749 struct ubuf_info *uarg;
750
751 uarg = skb_shinfo(skb)->destructor_arg;
752 if (uarg->callback)
753 uarg->callback(uarg, false);
754 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
755 }
756}
757EXPORT_SYMBOL(skb_tx_error);
758
759/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000760 * consume_skb - free an skbuff
761 * @skb: buffer to free
762 *
763 * Drop a ref to the buffer and free it if the usage count has hit zero
764 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
765 * is being dropped after a failure and notes that
766 */
767void consume_skb(struct sk_buff *skb)
768{
769 if (unlikely(!skb))
770 return;
771 if (likely(atomic_read(&skb->users) == 1))
772 smp_rmb();
773 else if (likely(!atomic_dec_and_test(&skb->users)))
774 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900775 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000776 __kfree_skb(skb);
777}
778EXPORT_SYMBOL(consume_skb);
779
Eric Dumazetb1937222014-09-28 22:18:47 -0700780/* Make sure a field is enclosed inside headers_start/headers_end section */
781#define CHECK_SKB_FIELD(field) \
782 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
783 offsetof(struct sk_buff, headers_start)); \
784 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
785 offsetof(struct sk_buff, headers_end)); \
786
Herbert Xudec18812007-10-14 00:37:30 -0700787static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
788{
789 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700790 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700791 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700792 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000793 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700794#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700795 new->sp = secpath_get(old->sp);
796#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700797 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700798
Eric Dumazetb1937222014-09-28 22:18:47 -0700799 /* Note : this field could be in headers_start/headers_end section
800 * It is not yet because we do not want to have a 16 bit hole
801 */
802 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300803
Eric Dumazetb1937222014-09-28 22:18:47 -0700804 memcpy(&new->headers_start, &old->headers_start,
805 offsetof(struct sk_buff, headers_end) -
806 offsetof(struct sk_buff, headers_start));
807 CHECK_SKB_FIELD(protocol);
808 CHECK_SKB_FIELD(csum);
809 CHECK_SKB_FIELD(hash);
810 CHECK_SKB_FIELD(priority);
811 CHECK_SKB_FIELD(skb_iif);
812 CHECK_SKB_FIELD(vlan_proto);
813 CHECK_SKB_FIELD(vlan_tci);
814 CHECK_SKB_FIELD(transport_header);
815 CHECK_SKB_FIELD(network_header);
816 CHECK_SKB_FIELD(mac_header);
817 CHECK_SKB_FIELD(inner_protocol);
818 CHECK_SKB_FIELD(inner_transport_header);
819 CHECK_SKB_FIELD(inner_network_header);
820 CHECK_SKB_FIELD(inner_mac_header);
821 CHECK_SKB_FIELD(mark);
822#ifdef CONFIG_NETWORK_SECMARK
823 CHECK_SKB_FIELD(secmark);
824#endif
Cong Wange0d10952013-08-01 11:10:25 +0800825#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700826 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300827#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800828#ifdef CONFIG_XPS
829 CHECK_SKB_FIELD(sender_cpu);
830#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700831#ifdef CONFIG_NET_SCHED
832 CHECK_SKB_FIELD(tc_index);
833#ifdef CONFIG_NET_CLS_ACT
834 CHECK_SKB_FIELD(tc_verd);
835#endif
836#endif
837
Herbert Xudec18812007-10-14 00:37:30 -0700838}
839
Herbert Xu82c49a32009-05-22 22:11:37 +0000840/*
841 * You should not add any new code to this function. Add it to
842 * __copy_skb_header above instead.
843 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700844static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846#define C(x) n->x = skb->x
847
848 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700850 __copy_skb_header(n, skb);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 C(len);
853 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700854 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700855 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800856 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 C(tail);
860 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800861 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000862 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800863 C(data);
864 C(truesize);
865 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 atomic_inc(&(skb_shinfo(skb)->dataref));
868 skb->cloned = 1;
869
870 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700871#undef C
872}
873
874/**
875 * skb_morph - morph one skb into another
876 * @dst: the skb to receive the contents
877 * @src: the skb to supply the contents
878 *
879 * This is identical to skb_clone except that the target skb is
880 * supplied by the user.
881 *
882 * The target skb is returned upon exit.
883 */
884struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
885{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800886 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700887 return __skb_clone(dst, src);
888}
889EXPORT_SYMBOL_GPL(skb_morph);
890
Ben Hutchings2c530402012-07-10 10:55:09 +0000891/**
892 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000893 * @skb: the skb to modify
894 * @gfp_mask: allocation priority
895 *
896 * This must be called on SKBTX_DEV_ZEROCOPY skb.
897 * It will copy all frags into kernel and drop the reference
898 * to userspace pages.
899 *
900 * If this function is called from an interrupt gfp_mask() must be
901 * %GFP_ATOMIC.
902 *
903 * Returns 0 on success or a negative error code on failure
904 * to allocate kernel memory to copy to.
905 */
906int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000907{
908 int i;
909 int num_frags = skb_shinfo(skb)->nr_frags;
910 struct page *page, *head = NULL;
911 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
912
913 for (i = 0; i < num_frags; i++) {
914 u8 *vaddr;
915 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
916
Krishna Kumar02756ed2012-07-17 02:05:29 +0000917 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000918 if (!page) {
919 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900920 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000921 put_page(head);
922 head = next;
923 }
924 return -ENOMEM;
925 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200926 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000927 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000928 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200929 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900930 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000931 head = page;
932 }
933
934 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000935 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000936 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000937
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000938 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000939
940 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000941 for (i = num_frags - 1; i >= 0; i--) {
942 __skb_fill_page_desc(skb, i, head, 0,
943 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900944 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000945 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000946
947 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000948 return 0;
949}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000950EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000951
Herbert Xue0053ec2007-10-14 00:37:52 -0700952/**
953 * skb_clone - duplicate an sk_buff
954 * @skb: buffer to clone
955 * @gfp_mask: allocation priority
956 *
957 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
958 * copies share the same packet data but not structure. The new
959 * buffer has a reference count of 1. If the allocation fails the
960 * function returns %NULL otherwise the new buffer is returned.
961 *
962 * If this function is called from an interrupt gfp_mask() must be
963 * %GFP_ATOMIC.
964 */
965
966struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
967{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700968 struct sk_buff_fclones *fclones = container_of(skb,
969 struct sk_buff_fclones,
970 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800971 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700972
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000973 if (skb_orphan_frags(skb, gfp_mask))
974 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000975
Herbert Xue0053ec2007-10-14 00:37:52 -0700976 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800977 atomic_read(&fclones->fclone_ref) == 1) {
978 n = &fclones->skb2;
979 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700980 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700981 if (skb_pfmemalloc(skb))
982 gfp_mask |= __GFP_MEMALLOC;
983
Herbert Xue0053ec2007-10-14 00:37:52 -0700984 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
985 if (!n)
986 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200987
988 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700989 n->fclone = SKB_FCLONE_UNAVAILABLE;
990 }
991
992 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800994EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000996static void skb_headers_offset_update(struct sk_buff *skb, int off)
997{
Eric Dumazet030737b2013-10-19 11:42:54 -0700998 /* Only adjust this if it actually is csum_start rather than csum */
999 if (skb->ip_summed == CHECKSUM_PARTIAL)
1000 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001001 /* {transport,network,mac}_header and tail are relative to skb->head */
1002 skb->transport_header += off;
1003 skb->network_header += off;
1004 if (skb_mac_header_was_set(skb))
1005 skb->mac_header += off;
1006 skb->inner_transport_header += off;
1007 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001008 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1012{
Herbert Xudec18812007-10-14 00:37:30 -07001013 __copy_skb_header(new, old);
1014
Herbert Xu79671682006-06-22 02:40:14 -07001015 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1016 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1017 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Mel Gormanc93bdd02012-07-31 16:44:19 -07001020static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1021{
1022 if (skb_pfmemalloc(skb))
1023 return SKB_ALLOC_RX;
1024 return 0;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
1028 * skb_copy - create private copy of an sk_buff
1029 * @skb: buffer to copy
1030 * @gfp_mask: allocation priority
1031 *
1032 * Make a copy of both an &sk_buff and its data. This is used when the
1033 * caller wishes to modify the data and needs a private copy of the
1034 * data to alter. Returns %NULL on failure or the pointer to the buffer
1035 * on success. The returned buffer has a reference count of 1.
1036 *
1037 * As by-product this function converts non-linear &sk_buff to linear
1038 * one, so that &sk_buff becomes completely private and caller is allowed
1039 * to modify all the data of returned buffer. This means that this
1040 * function is not recommended for use in circumstances when only
1041 * header is going to be modified. Use pskb_copy() instead.
1042 */
1043
Al Virodd0fc662005-10-07 07:46:04 +01001044struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001046 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001047 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001048 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1049 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!n)
1052 return NULL;
1053
1054 /* Set the data pointer */
1055 skb_reserve(n, headerlen);
1056 /* Set the tail pointer and length */
1057 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1060 BUG();
1061
1062 copy_skb_header(n, skb);
1063 return n;
1064}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001065EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001068 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001070 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001072 * @fclone: if true allocate the copy of the skb from the fclone
1073 * cache instead of the head cache; it is recommended to set this
1074 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
1076 * Make a copy of both an &sk_buff and part of its data, located
1077 * in header. Fragmented data remain shared. This is used when
1078 * the caller wishes to modify only header of &sk_buff and needs
1079 * private copy of the header to alter. Returns %NULL on failure
1080 * or the pointer to the buffer on success.
1081 * The returned buffer has a reference count of 1.
1082 */
1083
Octavian Purdilabad93e92014-06-12 01:36:26 +03001084struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1085 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Eric Dumazet117632e2011-12-03 21:39:53 +00001087 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001088 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1089 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!n)
1092 goto out;
1093
1094 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001095 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Set the tail pointer and length */
1097 skb_put(n, skb_headlen(skb));
1098 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001099 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Herbert Xu25f484a2006-11-07 14:57:15 -08001101 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 n->data_len = skb->data_len;
1103 n->len = skb->len;
1104
1105 if (skb_shinfo(skb)->nr_frags) {
1106 int i;
1107
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001108 if (skb_orphan_frags(skb, gfp_mask)) {
1109 kfree_skb(n);
1110 n = NULL;
1111 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1114 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001115 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117 skb_shinfo(n)->nr_frags = i;
1118 }
1119
David S. Miller21dc3302010-08-23 00:13:46 -07001120 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1122 skb_clone_fraglist(n);
1123 }
1124
1125 copy_skb_header(n, skb);
1126out:
1127 return n;
1128}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001129EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131/**
1132 * pskb_expand_head - reallocate header of &sk_buff
1133 * @skb: buffer to reallocate
1134 * @nhead: room to add at head
1135 * @ntail: room to add at tail
1136 * @gfp_mask: allocation priority
1137 *
Mathias Krausebc323832013-11-07 14:18:26 +01001138 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1139 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * reference count of 1. Returns zero in the case of success or error,
1141 * if expansion failed. In the last case, &sk_buff is not changed.
1142 *
1143 * All the pointers pointing into skb header may change and must be
1144 * reloaded after call to this function.
1145 */
1146
Victor Fusco86a76ca2005-07-08 14:57:47 -07001147int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001148 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 int i;
1151 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001152 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 long off;
1154
Herbert Xu4edd87a2008-10-01 07:09:38 -07001155 BUG_ON(nhead < 0);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (skb_shared(skb))
1158 BUG();
1159
1160 size = SKB_DATA_ALIGN(size);
1161
Mel Gormanc93bdd02012-07-31 16:44:19 -07001162 if (skb_pfmemalloc(skb))
1163 gfp_mask |= __GFP_MEMALLOC;
1164 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1165 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!data)
1167 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001168 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001171 * optimized for the cases when header is void.
1172 */
1173 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1174
1175 memcpy((struct skb_shared_info *)(data + size),
1176 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001177 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Alexander Duyck3e245912012-05-04 14:26:51 +00001179 /*
1180 * if shinfo is shared we must drop the old head gracefully, but if it
1181 * is not we can just drop the old head and let the existing refcount
1182 * be since all we did is relocate the values
1183 */
1184 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001185 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001186 if (skb_orphan_frags(skb, gfp_mask))
1187 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001188 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001189 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Eric Dumazet1fd63042010-09-02 23:09:32 +00001191 if (skb_has_frag_list(skb))
1192 skb_clone_fraglist(skb);
1193
1194 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001195 } else {
1196 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 off = (data + nhead) - skb->head;
1199
1200 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001201 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001203#ifdef NET_SKBUFF_DATA_USES_OFFSET
1204 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001205 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001206#else
1207 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001208#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001209 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001210 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001212 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 skb->nohdr = 0;
1214 atomic_set(&skb_shinfo(skb)->dataref, 1);
1215 return 0;
1216
Shirley Maa6686f22011-07-06 12:22:12 +00001217nofrags:
1218 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219nodata:
1220 return -ENOMEM;
1221}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001222EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224/* Make private copy of skb with writable head and some headroom */
1225
1226struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1227{
1228 struct sk_buff *skb2;
1229 int delta = headroom - skb_headroom(skb);
1230
1231 if (delta <= 0)
1232 skb2 = pskb_copy(skb, GFP_ATOMIC);
1233 else {
1234 skb2 = skb_clone(skb, GFP_ATOMIC);
1235 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1236 GFP_ATOMIC)) {
1237 kfree_skb(skb2);
1238 skb2 = NULL;
1239 }
1240 }
1241 return skb2;
1242}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001243EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245/**
1246 * skb_copy_expand - copy and expand sk_buff
1247 * @skb: buffer to copy
1248 * @newheadroom: new free bytes at head
1249 * @newtailroom: new free bytes at tail
1250 * @gfp_mask: allocation priority
1251 *
1252 * Make a copy of both an &sk_buff and its data and while doing so
1253 * allocate additional space.
1254 *
1255 * This is used when the caller wishes to modify the data and needs a
1256 * private copy of the data to alter as well as more space for new fields.
1257 * Returns %NULL on failure or the pointer to the buffer
1258 * on success. The returned buffer has a reference count of 1.
1259 *
1260 * You must pass %GFP_ATOMIC as the allocation priority if this function
1261 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
1263struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001264 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001265 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 /*
1268 * Allocate the copy buffer
1269 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001270 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1271 gfp_mask, skb_alloc_rx_flag(skb),
1272 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001273 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int head_copy_len, head_copy_off;
1275
1276 if (!n)
1277 return NULL;
1278
1279 skb_reserve(n, newheadroom);
1280
1281 /* Set the tail pointer and length */
1282 skb_put(n, skb->len);
1283
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001284 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 head_copy_off = 0;
1286 if (newheadroom <= head_copy_len)
1287 head_copy_len = newheadroom;
1288 else
1289 head_copy_off = newheadroom - head_copy_len;
1290
1291 /* Copy the linear header and data. */
1292 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1293 skb->len + head_copy_len))
1294 BUG();
1295
1296 copy_skb_header(n, skb);
1297
Eric Dumazet030737b2013-10-19 11:42:54 -07001298 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return n;
1301}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001302EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304/**
1305 * skb_pad - zero pad the tail of an skb
1306 * @skb: buffer to pad
1307 * @pad: space to pad
1308 *
1309 * Ensure that a buffer is followed by a padding area that is zero
1310 * filled. Used by network drivers which may DMA or transfer data
1311 * beyond the buffer end onto the wire.
1312 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001313 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001315
Herbert Xu5b057c62006-06-23 02:06:41 -07001316int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Herbert Xu5b057c62006-06-23 02:06:41 -07001318 int err;
1319 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001322 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001326
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001327 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001328 if (likely(skb_cloned(skb) || ntail > 0)) {
1329 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1330 if (unlikely(err))
1331 goto free_skb;
1332 }
1333
1334 /* FIXME: The use of this function with non-linear skb's really needs
1335 * to be audited.
1336 */
1337 err = skb_linearize(skb);
1338 if (unlikely(err))
1339 goto free_skb;
1340
1341 memset(skb->data + skb->len, 0, pad);
1342 return 0;
1343
1344free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001346 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001347}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001348EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001349
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001350/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001351 * pskb_put - add data to the tail of a potentially fragmented buffer
1352 * @skb: start of the buffer to use
1353 * @tail: tail fragment of the buffer to use
1354 * @len: amount of data to add
1355 *
1356 * This function extends the used data area of the potentially
1357 * fragmented buffer. @tail must be the last fragment of @skb -- or
1358 * @skb itself. If this would exceed the total buffer size the kernel
1359 * will panic. A pointer to the first byte of the extra data is
1360 * returned.
1361 */
1362
1363unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1364{
1365 if (tail != skb) {
1366 skb->data_len += len;
1367 skb->len += len;
1368 }
1369 return skb_put(tail, len);
1370}
1371EXPORT_SYMBOL_GPL(pskb_put);
1372
1373/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001374 * skb_put - add data to a buffer
1375 * @skb: buffer to use
1376 * @len: amount of data to add
1377 *
1378 * This function extends the used data area of the buffer. If this would
1379 * exceed the total buffer size the kernel will panic. A pointer to the
1380 * first byte of the extra data is returned.
1381 */
1382unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1383{
1384 unsigned char *tmp = skb_tail_pointer(skb);
1385 SKB_LINEAR_ASSERT(skb);
1386 skb->tail += len;
1387 skb->len += len;
1388 if (unlikely(skb->tail > skb->end))
1389 skb_over_panic(skb, len, __builtin_return_address(0));
1390 return tmp;
1391}
1392EXPORT_SYMBOL(skb_put);
1393
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001394/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001395 * skb_push - add data to the start of a buffer
1396 * @skb: buffer to use
1397 * @len: amount of data to add
1398 *
1399 * This function extends the used data area of the buffer at the buffer
1400 * start. If this would exceed the total buffer headroom the kernel will
1401 * panic. A pointer to the first byte of the extra data is returned.
1402 */
1403unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1404{
1405 skb->data -= len;
1406 skb->len += len;
1407 if (unlikely(skb->data<skb->head))
1408 skb_under_panic(skb, len, __builtin_return_address(0));
1409 return skb->data;
1410}
1411EXPORT_SYMBOL(skb_push);
1412
1413/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001414 * skb_pull - remove data from the start of a buffer
1415 * @skb: buffer to use
1416 * @len: amount of data to remove
1417 *
1418 * This function removes data from the start of a buffer, returning
1419 * the memory to the headroom. A pointer to the next data in the buffer
1420 * is returned. Once the data has been pulled future pushes will overwrite
1421 * the old data.
1422 */
1423unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1424{
David S. Miller47d29642010-05-02 02:21:44 -07001425 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001426}
1427EXPORT_SYMBOL(skb_pull);
1428
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001429/**
1430 * skb_trim - remove end from a buffer
1431 * @skb: buffer to alter
1432 * @len: new length
1433 *
1434 * Cut the length of a buffer down by removing data from the tail. If
1435 * the buffer is already under the length specified it is not modified.
1436 * The skb must be linear.
1437 */
1438void skb_trim(struct sk_buff *skb, unsigned int len)
1439{
1440 if (skb->len > len)
1441 __skb_trim(skb, len);
1442}
1443EXPORT_SYMBOL(skb_trim);
1444
Herbert Xu3cc0e872006-06-09 16:13:38 -07001445/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
1447
Herbert Xu3cc0e872006-06-09 16:13:38 -07001448int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Herbert Xu27b437c2006-07-13 19:26:39 -07001450 struct sk_buff **fragp;
1451 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int offset = skb_headlen(skb);
1453 int nfrags = skb_shinfo(skb)->nr_frags;
1454 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001455 int err;
1456
1457 if (skb_cloned(skb) &&
1458 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001461 i = 0;
1462 if (offset >= len)
1463 goto drop_pages;
1464
1465 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001466 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001467
1468 if (end < len) {
1469 offset = end;
1470 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001472
Eric Dumazet9e903e02011-10-18 21:00:24 +00001473 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001474
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001475drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001476 skb_shinfo(skb)->nr_frags = i;
1477
1478 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001479 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001480
David S. Miller21dc3302010-08-23 00:13:46 -07001481 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001482 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001483 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Herbert Xu27b437c2006-07-13 19:26:39 -07001486 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1487 fragp = &frag->next) {
1488 int end = offset + frag->len;
1489
1490 if (skb_shared(frag)) {
1491 struct sk_buff *nfrag;
1492
1493 nfrag = skb_clone(frag, GFP_ATOMIC);
1494 if (unlikely(!nfrag))
1495 return -ENOMEM;
1496
1497 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001498 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001499 frag = nfrag;
1500 *fragp = frag;
1501 }
1502
1503 if (end < len) {
1504 offset = end;
1505 continue;
1506 }
1507
1508 if (end > len &&
1509 unlikely((err = pskb_trim(frag, len - offset))))
1510 return err;
1511
1512 if (frag->next)
1513 skb_drop_list(&frag->next);
1514 break;
1515 }
1516
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001517done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001518 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 skb->data_len -= skb->len - len;
1520 skb->len = len;
1521 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001522 skb->len = len;
1523 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001524 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
1527 return 0;
1528}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001529EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/**
1532 * __pskb_pull_tail - advance tail of skb header
1533 * @skb: buffer to reallocate
1534 * @delta: number of bytes to advance tail
1535 *
1536 * The function makes a sense only on a fragmented &sk_buff,
1537 * it expands header moving its tail forward and copying necessary
1538 * data from fragmented part.
1539 *
1540 * &sk_buff MUST have reference count of 1.
1541 *
1542 * Returns %NULL (and &sk_buff does not change) if pull failed
1543 * or value of new tail of skb in the case of success.
1544 *
1545 * All the pointers pointing into skb header may change and must be
1546 * reloaded after call to this function.
1547 */
1548
1549/* Moves tail of skb head forward, copying data from fragmented part,
1550 * when it is necessary.
1551 * 1. It may fail due to malloc failure.
1552 * 2. It may change skb pointers.
1553 *
1554 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1555 */
1556unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1557{
1558 /* If skb has not enough free space at tail, get new one
1559 * plus 128 bytes for future expansions. If we have enough
1560 * room at tail, reallocate without expansion only if skb is cloned.
1561 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001562 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (eat > 0 || skb_cloned(skb)) {
1565 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1566 GFP_ATOMIC))
1567 return NULL;
1568 }
1569
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001570 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 BUG();
1572
1573 /* Optimization: no fragments, no reasons to preestimate
1574 * size of pulled pages. Superb.
1575 */
David S. Miller21dc3302010-08-23 00:13:46 -07001576 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto pull_pages;
1578
1579 /* Estimate size of pulled pages. */
1580 eat = delta;
1581 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001582 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1583
1584 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001586 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588
1589 /* If we need update frag list, we are in troubles.
1590 * Certainly, it possible to add an offset to skb data,
1591 * but taking into account that pulling is expected to
1592 * be very rare operation, it is worth to fight against
1593 * further bloating skb head and crucify ourselves here instead.
1594 * Pure masohism, indeed. 8)8)
1595 */
1596 if (eat) {
1597 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1598 struct sk_buff *clone = NULL;
1599 struct sk_buff *insp = NULL;
1600
1601 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001602 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (list->len <= eat) {
1605 /* Eaten as whole. */
1606 eat -= list->len;
1607 list = list->next;
1608 insp = list;
1609 } else {
1610 /* Eaten partially. */
1611
1612 if (skb_shared(list)) {
1613 /* Sucks! We need to fork list. :-( */
1614 clone = skb_clone(list, GFP_ATOMIC);
1615 if (!clone)
1616 return NULL;
1617 insp = list->next;
1618 list = clone;
1619 } else {
1620 /* This may be pulled without
1621 * problems. */
1622 insp = list;
1623 }
1624 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001625 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return NULL;
1627 }
1628 break;
1629 }
1630 } while (eat);
1631
1632 /* Free pulled out fragments. */
1633 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1634 skb_shinfo(skb)->frag_list = list->next;
1635 kfree_skb(list);
1636 }
1637 /* And insert new clone at head. */
1638 if (clone) {
1639 clone->next = list;
1640 skb_shinfo(skb)->frag_list = clone;
1641 }
1642 }
1643 /* Success! Now we may commit changes to skb data. */
1644
1645pull_pages:
1646 eat = delta;
1647 k = 0;
1648 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001649 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1650
1651 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001652 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001653 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 } else {
1655 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1656 if (eat) {
1657 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001658 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 eat = 0;
1660 }
1661 k++;
1662 }
1663 }
1664 skb_shinfo(skb)->nr_frags = k;
1665
1666 skb->tail += delta;
1667 skb->data_len -= delta;
1668
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001669 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001671EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Eric Dumazet22019b12011-07-29 18:37:31 +00001673/**
1674 * skb_copy_bits - copy bits from skb to kernel buffer
1675 * @skb: source skb
1676 * @offset: offset in source
1677 * @to: destination buffer
1678 * @len: number of bytes to copy
1679 *
1680 * Copy the specified number of bytes from the source skb to the
1681 * destination buffer.
1682 *
1683 * CAUTION ! :
1684 * If its prototype is ever changed,
1685 * check arch/{*}/net/{*}.S files,
1686 * since it is called from BPF assembly code.
1687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1689{
David S. Miller1a028e52007-04-27 15:21:23 -07001690 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001691 struct sk_buff *frag_iter;
1692 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (offset > (int)skb->len - len)
1695 goto fault;
1696
1697 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001698 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (copy > len)
1700 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001701 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if ((len -= copy) == 0)
1703 return 0;
1704 offset += copy;
1705 to += copy;
1706 }
1707
1708 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001709 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001710 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001712 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001713
Eric Dumazet51c56b02012-04-05 11:35:15 +02001714 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if ((copy = end - offset) > 0) {
1716 u8 *vaddr;
1717
1718 if (copy > len)
1719 copy = len;
1720
Eric Dumazet51c56b02012-04-05 11:35:15 +02001721 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001723 vaddr + f->page_offset + offset - start,
1724 copy);
1725 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if ((len -= copy) == 0)
1728 return 0;
1729 offset += copy;
1730 to += copy;
1731 }
David S. Miller1a028e52007-04-27 15:21:23 -07001732 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
David S. Millerfbb398a2009-06-09 00:18:59 -07001735 skb_walk_frags(skb, frag_iter) {
1736 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
David S. Millerfbb398a2009-06-09 00:18:59 -07001738 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David S. Millerfbb398a2009-06-09 00:18:59 -07001740 end = start + frag_iter->len;
1741 if ((copy = end - offset) > 0) {
1742 if (copy > len)
1743 copy = len;
1744 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1745 goto fault;
1746 if ((len -= copy) == 0)
1747 return 0;
1748 offset += copy;
1749 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001751 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Shirley Maa6686f22011-07-06 12:22:12 +00001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!len)
1755 return 0;
1756
1757fault:
1758 return -EFAULT;
1759}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001760EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jens Axboe9c55e012007-11-06 23:30:13 -08001762/*
1763 * Callback from splice_to_pipe(), if we need to release some pages
1764 * at the end of the spd in case we error'ed out in filling the pipe.
1765 */
1766static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1767{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001768 put_page(spd->pages[i]);
1769}
Jens Axboe9c55e012007-11-06 23:30:13 -08001770
David S. Millera108d5f2012-04-23 23:06:11 -04001771static struct page *linear_to_page(struct page *page, unsigned int *len,
1772 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001773 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001774{
Eric Dumazet5640f762012-09-23 23:04:42 +00001775 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001776
Eric Dumazet5640f762012-09-23 23:04:42 +00001777 if (!sk_page_frag_refill(sk, pfrag))
1778 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001779
Eric Dumazet5640f762012-09-23 23:04:42 +00001780 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001781
Eric Dumazet5640f762012-09-23 23:04:42 +00001782 memcpy(page_address(pfrag->page) + pfrag->offset,
1783 page_address(page) + *offset, *len);
1784 *offset = pfrag->offset;
1785 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001786
Eric Dumazet5640f762012-09-23 23:04:42 +00001787 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001788}
1789
Eric Dumazet41c73a02012-04-22 12:26:16 +00001790static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1791 struct page *page,
1792 unsigned int offset)
1793{
1794 return spd->nr_pages &&
1795 spd->pages[spd->nr_pages - 1] == page &&
1796 (spd->partial[spd->nr_pages - 1].offset +
1797 spd->partial[spd->nr_pages - 1].len == offset);
1798}
1799
Jens Axboe9c55e012007-11-06 23:30:13 -08001800/*
1801 * Fill page/offset/length into spd, if it can hold more pages.
1802 */
David S. Millera108d5f2012-04-23 23:06:11 -04001803static bool spd_fill_page(struct splice_pipe_desc *spd,
1804 struct pipe_inode_info *pipe, struct page *page,
1805 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001806 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001807 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001808{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001809 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001810 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001811
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001812 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001813 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001814 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001815 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001816 }
1817 if (spd_can_coalesce(spd, page, offset)) {
1818 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001819 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001820 }
1821 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001822 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001823 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001824 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001826
David S. Millera108d5f2012-04-23 23:06:11 -04001827 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001828}
1829
David S. Millera108d5f2012-04-23 23:06:11 -04001830static bool __splice_segment(struct page *page, unsigned int poff,
1831 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001832 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001833 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001834 struct sock *sk,
1835 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001836{
1837 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001838 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001839
1840 /* skip this segment if already processed */
1841 if (*off >= plen) {
1842 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001843 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001844 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001845
Octavian Purdila2870c432008-07-15 00:49:11 -07001846 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001847 poff += *off;
1848 plen -= *off;
1849 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001850
Eric Dumazet18aafc62013-01-11 14:46:37 +00001851 do {
1852 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001853
Eric Dumazet18aafc62013-01-11 14:46:37 +00001854 if (spd_fill_page(spd, pipe, page, &flen, poff,
1855 linear, sk))
1856 return true;
1857 poff += flen;
1858 plen -= flen;
1859 *len -= flen;
1860 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001861
David S. Millera108d5f2012-04-23 23:06:11 -04001862 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001863}
1864
1865/*
David S. Millera108d5f2012-04-23 23:06:11 -04001866 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001867 * pipe is full or if we already spliced the requested length.
1868 */
David S. Millera108d5f2012-04-23 23:06:11 -04001869static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1870 unsigned int *offset, unsigned int *len,
1871 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001872{
1873 int seg;
1874
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001875 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001876 * If skb->head_frag is set, this 'linear' part is backed by a
1877 * fragment, and if the head is not shared with any clones then
1878 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001879 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001880 if (__splice_segment(virt_to_page(skb->data),
1881 (unsigned long) skb->data & (PAGE_SIZE - 1),
1882 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001883 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001884 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001885 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001886 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001887
1888 /*
1889 * then map the fragments
1890 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001891 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1892 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1893
Ian Campbellea2ab692011-08-22 23:44:58 +00001894 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001895 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001896 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001897 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001898 }
1899
David S. Millera108d5f2012-04-23 23:06:11 -04001900 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001901}
1902
1903/*
1904 * Map data from the skb to a pipe. Should handle both the linear part,
1905 * the fragments, and the frag list. It does NOT handle frag lists within
1906 * the frag list, if such a thing exists. We'd probably need to recurse to
1907 * handle that cleanly.
1908 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001909int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001910 struct pipe_inode_info *pipe, unsigned int tlen,
1911 unsigned int flags)
1912{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001913 struct partial_page partial[MAX_SKB_FRAGS];
1914 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001915 struct splice_pipe_desc spd = {
1916 .pages = pages,
1917 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001918 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001919 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001920 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001921 .spd_release = sock_spd_release,
1922 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001923 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001924 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001925 int ret = 0;
1926
Jens Axboe9c55e012007-11-06 23:30:13 -08001927 /*
1928 * __skb_splice_bits() only fails if the output has no room left,
1929 * so no point in going over the frag_list for the error case.
1930 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001931 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001932 goto done;
1933 else if (!tlen)
1934 goto done;
1935
1936 /*
1937 * now see if we have a frag_list to map
1938 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001939 skb_walk_frags(skb, frag_iter) {
1940 if (!tlen)
1941 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001942 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001943 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001944 }
1945
1946done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001947 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001948 /*
1949 * Drop the socket lock, otherwise we have reverse
1950 * locking dependencies between sk_lock and i_mutex
1951 * here as compared to sendfile(). We enter here
1952 * with the socket lock held, and splice_to_pipe() will
1953 * grab the pipe inode lock. For sendfile() emulation,
1954 * we call into ->sendpage() with the i_mutex lock held
1955 * and networking will grab the socket lock.
1956 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001957 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001958 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001959 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001960 }
1961
Jens Axboe35f3d142010-05-20 10:43:18 +02001962 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001963}
1964
Herbert Xu357b40a2005-04-19 22:30:14 -07001965/**
1966 * skb_store_bits - store bits from kernel buffer to skb
1967 * @skb: destination buffer
1968 * @offset: offset in destination
1969 * @from: source buffer
1970 * @len: number of bytes to copy
1971 *
1972 * Copy the specified number of bytes from the source buffer to the
1973 * destination skb. This function handles all the messy bits of
1974 * traversing fragment lists and such.
1975 */
1976
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001977int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001978{
David S. Miller1a028e52007-04-27 15:21:23 -07001979 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001980 struct sk_buff *frag_iter;
1981 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001982
1983 if (offset > (int)skb->len - len)
1984 goto fault;
1985
David S. Miller1a028e52007-04-27 15:21:23 -07001986 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001987 if (copy > len)
1988 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001989 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001990 if ((len -= copy) == 0)
1991 return 0;
1992 offset += copy;
1993 from += copy;
1994 }
1995
1996 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1997 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001998 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001999
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002000 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002001
Eric Dumazet9e903e02011-10-18 21:00:24 +00002002 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002003 if ((copy = end - offset) > 0) {
2004 u8 *vaddr;
2005
2006 if (copy > len)
2007 copy = len;
2008
Eric Dumazet51c56b02012-04-05 11:35:15 +02002009 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002010 memcpy(vaddr + frag->page_offset + offset - start,
2011 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002012 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002013
2014 if ((len -= copy) == 0)
2015 return 0;
2016 offset += copy;
2017 from += copy;
2018 }
David S. Miller1a028e52007-04-27 15:21:23 -07002019 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002020 }
2021
David S. Millerfbb398a2009-06-09 00:18:59 -07002022 skb_walk_frags(skb, frag_iter) {
2023 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002024
David S. Millerfbb398a2009-06-09 00:18:59 -07002025 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002026
David S. Millerfbb398a2009-06-09 00:18:59 -07002027 end = start + frag_iter->len;
2028 if ((copy = end - offset) > 0) {
2029 if (copy > len)
2030 copy = len;
2031 if (skb_store_bits(frag_iter, offset - start,
2032 from, copy))
2033 goto fault;
2034 if ((len -= copy) == 0)
2035 return 0;
2036 offset += copy;
2037 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002038 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002039 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002040 }
2041 if (!len)
2042 return 0;
2043
2044fault:
2045 return -EFAULT;
2046}
Herbert Xu357b40a2005-04-19 22:30:14 -07002047EXPORT_SYMBOL(skb_store_bits);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002050__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2051 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
David S. Miller1a028e52007-04-27 15:21:23 -07002053 int start = skb_headlen(skb);
2054 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002055 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int pos = 0;
2057
2058 /* Checksum header. */
2059 if (copy > 0) {
2060 if (copy > len)
2061 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002062 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if ((len -= copy) == 0)
2064 return csum;
2065 offset += copy;
2066 pos = copy;
2067 }
2068
2069 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002070 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002071 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002073 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002074
Eric Dumazet51c56b02012-04-05 11:35:15 +02002075 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002077 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (copy > len)
2081 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002082 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002083 csum2 = ops->update(vaddr + frag->page_offset +
2084 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002085 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002086 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!(len -= copy))
2088 return csum;
2089 offset += copy;
2090 pos += copy;
2091 }
David S. Miller1a028e52007-04-27 15:21:23 -07002092 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 skb_walk_frags(skb, frag_iter) {
2096 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
David S. Millerfbb398a2009-06-09 00:18:59 -07002098 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
David S. Millerfbb398a2009-06-09 00:18:59 -07002100 end = start + frag_iter->len;
2101 if ((copy = end - offset) > 0) {
2102 __wsum csum2;
2103 if (copy > len)
2104 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002105 csum2 = __skb_checksum(frag_iter, offset - start,
2106 copy, 0, ops);
2107 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002108 if ((len -= copy) == 0)
2109 return csum;
2110 offset += copy;
2111 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002113 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002115 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 return csum;
2118}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002119EXPORT_SYMBOL(__skb_checksum);
2120
2121__wsum skb_checksum(const struct sk_buff *skb, int offset,
2122 int len, __wsum csum)
2123{
2124 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002125 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002126 .combine = csum_block_add_ext,
2127 };
2128
2129 return __skb_checksum(skb, offset, len, csum, &ops);
2130}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002131EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133/* Both of above in one bottle. */
2134
Al Viro81d77662006-11-14 21:37:33 -08002135__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2136 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
David S. Miller1a028e52007-04-27 15:21:23 -07002138 int start = skb_headlen(skb);
2139 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002140 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int pos = 0;
2142
2143 /* Copy header. */
2144 if (copy > 0) {
2145 if (copy > len)
2146 copy = len;
2147 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2148 copy, csum);
2149 if ((len -= copy) == 0)
2150 return csum;
2151 offset += copy;
2152 to += copy;
2153 pos = copy;
2154 }
2155
2156 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002157 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002159 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002160
Eric Dumazet9e903e02011-10-18 21:00:24 +00002161 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002163 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 u8 *vaddr;
2165 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2166
2167 if (copy > len)
2168 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002169 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002171 frag->page_offset +
2172 offset - start, to,
2173 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002174 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 csum = csum_block_add(csum, csum2, pos);
2176 if (!(len -= copy))
2177 return csum;
2178 offset += copy;
2179 to += copy;
2180 pos += copy;
2181 }
David S. Miller1a028e52007-04-27 15:21:23 -07002182 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
David S. Millerfbb398a2009-06-09 00:18:59 -07002185 skb_walk_frags(skb, frag_iter) {
2186 __wsum csum2;
2187 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
David S. Millerfbb398a2009-06-09 00:18:59 -07002189 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
David S. Millerfbb398a2009-06-09 00:18:59 -07002191 end = start + frag_iter->len;
2192 if ((copy = end - offset) > 0) {
2193 if (copy > len)
2194 copy = len;
2195 csum2 = skb_copy_and_csum_bits(frag_iter,
2196 offset - start,
2197 to, copy, 0);
2198 csum = csum_block_add(csum, csum2, pos);
2199 if ((len -= copy) == 0)
2200 return csum;
2201 offset += copy;
2202 to += copy;
2203 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002205 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002207 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return csum;
2209}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002210EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Thomas Grafaf2806f2013-12-13 15:22:17 +01002212 /**
2213 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2214 * @from: source buffer
2215 *
2216 * Calculates the amount of linear headroom needed in the 'to' skb passed
2217 * into skb_zerocopy().
2218 */
2219unsigned int
2220skb_zerocopy_headlen(const struct sk_buff *from)
2221{
2222 unsigned int hlen = 0;
2223
2224 if (!from->head_frag ||
2225 skb_headlen(from) < L1_CACHE_BYTES ||
2226 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2227 hlen = skb_headlen(from);
2228
2229 if (skb_has_frag_list(from))
2230 hlen = from->len;
2231
2232 return hlen;
2233}
2234EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2235
2236/**
2237 * skb_zerocopy - Zero copy skb to skb
2238 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002239 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002240 * @len: number of bytes to copy from source buffer
2241 * @hlen: size of linear headroom in destination buffer
2242 *
2243 * Copies up to `len` bytes from `from` to `to` by creating references
2244 * to the frags in the source buffer.
2245 *
2246 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2247 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002248 *
2249 * Return value:
2250 * 0: everything is OK
2251 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2252 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002253 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002254int
2255skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002256{
2257 int i, j = 0;
2258 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002259 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002260 struct page *page;
2261 unsigned int offset;
2262
2263 BUG_ON(!from->head_frag && !hlen);
2264
2265 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002266 if (len <= skb_tailroom(to))
2267 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002268
2269 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002270 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2271 if (unlikely(ret))
2272 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002273 len -= hlen;
2274 } else {
2275 plen = min_t(int, skb_headlen(from), len);
2276 if (plen) {
2277 page = virt_to_head_page(from->head);
2278 offset = from->data - (unsigned char *)page_address(page);
2279 __skb_fill_page_desc(to, 0, page, offset, plen);
2280 get_page(page);
2281 j = 1;
2282 len -= plen;
2283 }
2284 }
2285
2286 to->truesize += len + plen;
2287 to->len += len + plen;
2288 to->data_len += len + plen;
2289
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002290 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2291 skb_tx_error(from);
2292 return -ENOMEM;
2293 }
2294
Thomas Grafaf2806f2013-12-13 15:22:17 +01002295 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2296 if (!len)
2297 break;
2298 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2299 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2300 len -= skb_shinfo(to)->frags[j].size;
2301 skb_frag_ref(to, j);
2302 j++;
2303 }
2304 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002305
2306 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002307}
2308EXPORT_SYMBOL_GPL(skb_zerocopy);
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2311{
Al Virod3bc23e2006-11-14 21:24:49 -08002312 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 long csstart;
2314
Patrick McHardy84fa7932006-08-29 16:44:56 -07002315 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002316 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 else
2318 csstart = skb_headlen(skb);
2319
Kris Katterjohn09a62662006-01-08 22:24:28 -08002320 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002322 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 csum = 0;
2325 if (csstart != skb->len)
2326 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2327 skb->len - csstart, 0);
2328
Patrick McHardy84fa7932006-08-29 16:44:56 -07002329 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002330 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Al Virod3bc23e2006-11-14 21:24:49 -08002332 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002335EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337/**
2338 * skb_dequeue - remove from the head of the queue
2339 * @list: list to dequeue from
2340 *
2341 * Remove the head of the list. The list lock is taken so the function
2342 * may be used safely with other locking list functions. The head item is
2343 * returned or %NULL if the list is empty.
2344 */
2345
2346struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2347{
2348 unsigned long flags;
2349 struct sk_buff *result;
2350
2351 spin_lock_irqsave(&list->lock, flags);
2352 result = __skb_dequeue(list);
2353 spin_unlock_irqrestore(&list->lock, flags);
2354 return result;
2355}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002356EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358/**
2359 * skb_dequeue_tail - remove from the tail of the queue
2360 * @list: list to dequeue from
2361 *
2362 * Remove the tail of the list. The list lock is taken so the function
2363 * may be used safely with other locking list functions. The tail item is
2364 * returned or %NULL if the list is empty.
2365 */
2366struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2367{
2368 unsigned long flags;
2369 struct sk_buff *result;
2370
2371 spin_lock_irqsave(&list->lock, flags);
2372 result = __skb_dequeue_tail(list);
2373 spin_unlock_irqrestore(&list->lock, flags);
2374 return result;
2375}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002376EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378/**
2379 * skb_queue_purge - empty a list
2380 * @list: list to empty
2381 *
2382 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2383 * the list and one reference dropped. This function takes the list
2384 * lock and is atomic with respect to other list locking functions.
2385 */
2386void skb_queue_purge(struct sk_buff_head *list)
2387{
2388 struct sk_buff *skb;
2389 while ((skb = skb_dequeue(list)) != NULL)
2390 kfree_skb(skb);
2391}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002392EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394/**
2395 * skb_queue_head - queue a buffer at the list head
2396 * @list: list to use
2397 * @newsk: buffer to queue
2398 *
2399 * Queue a buffer at the start of the list. This function takes the
2400 * list lock and can be used safely with other locking &sk_buff functions
2401 * safely.
2402 *
2403 * A buffer cannot be placed on two lists at the same time.
2404 */
2405void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2406{
2407 unsigned long flags;
2408
2409 spin_lock_irqsave(&list->lock, flags);
2410 __skb_queue_head(list, newsk);
2411 spin_unlock_irqrestore(&list->lock, flags);
2412}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002413EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415/**
2416 * skb_queue_tail - queue a buffer at the list tail
2417 * @list: list to use
2418 * @newsk: buffer to queue
2419 *
2420 * Queue a buffer at the tail of the list. This function takes the
2421 * list lock and can be used safely with other locking &sk_buff functions
2422 * safely.
2423 *
2424 * A buffer cannot be placed on two lists at the same time.
2425 */
2426void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2427{
2428 unsigned long flags;
2429
2430 spin_lock_irqsave(&list->lock, flags);
2431 __skb_queue_tail(list, newsk);
2432 spin_unlock_irqrestore(&list->lock, flags);
2433}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002434EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436/**
2437 * skb_unlink - remove a buffer from a list
2438 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002439 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 *
David S. Miller8728b832005-08-09 19:25:21 -07002441 * Remove a packet from a list. The list locks are taken and this
2442 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 *
David S. Miller8728b832005-08-09 19:25:21 -07002444 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 */
David S. Miller8728b832005-08-09 19:25:21 -07002446void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
David S. Miller8728b832005-08-09 19:25:21 -07002448 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
David S. Miller8728b832005-08-09 19:25:21 -07002450 spin_lock_irqsave(&list->lock, flags);
2451 __skb_unlink(skb, list);
2452 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002454EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/**
2457 * skb_append - append a buffer
2458 * @old: buffer to insert after
2459 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002460 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 *
2462 * Place a packet after a given packet in a list. The list locks are taken
2463 * and this function is atomic with respect to other list locked calls.
2464 * A buffer cannot be placed on two lists at the same time.
2465 */
David S. Miller8728b832005-08-09 19:25:21 -07002466void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 unsigned long flags;
2469
David S. Miller8728b832005-08-09 19:25:21 -07002470 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002471 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002472 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002474EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476/**
2477 * skb_insert - insert a buffer
2478 * @old: buffer to insert before
2479 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002480 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 *
David S. Miller8728b832005-08-09 19:25:21 -07002482 * Place a packet before a given packet in a list. The list locks are
2483 * taken and this function is atomic with respect to other list locked
2484 * calls.
2485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * A buffer cannot be placed on two lists at the same time.
2487 */
David S. Miller8728b832005-08-09 19:25:21 -07002488void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 unsigned long flags;
2491
David S. Miller8728b832005-08-09 19:25:21 -07002492 spin_lock_irqsave(&list->lock, flags);
2493 __skb_insert(newsk, old->prev, old, list);
2494 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002496EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498static inline void skb_split_inside_header(struct sk_buff *skb,
2499 struct sk_buff* skb1,
2500 const u32 len, const int pos)
2501{
2502 int i;
2503
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002504 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2505 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /* And move data appendix as is. */
2507 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2508 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2509
2510 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2511 skb_shinfo(skb)->nr_frags = 0;
2512 skb1->data_len = skb->data_len;
2513 skb1->len += skb1->data_len;
2514 skb->data_len = 0;
2515 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002516 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519static inline void skb_split_no_header(struct sk_buff *skb,
2520 struct sk_buff* skb1,
2521 const u32 len, int pos)
2522{
2523 int i, k = 0;
2524 const int nfrags = skb_shinfo(skb)->nr_frags;
2525
2526 skb_shinfo(skb)->nr_frags = 0;
2527 skb1->len = skb1->data_len = skb->len - len;
2528 skb->len = len;
2529 skb->data_len = len - pos;
2530
2531 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002532 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 if (pos + size > len) {
2535 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2536
2537 if (pos < len) {
2538 /* Split frag.
2539 * We have two variants in this case:
2540 * 1. Move all the frag to the second
2541 * part, if it is possible. F.e.
2542 * this approach is mandatory for TUX,
2543 * where splitting is expensive.
2544 * 2. Split is accurately. We make this.
2545 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002546 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002548 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2549 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 skb_shinfo(skb)->nr_frags++;
2551 }
2552 k++;
2553 } else
2554 skb_shinfo(skb)->nr_frags++;
2555 pos += size;
2556 }
2557 skb_shinfo(skb1)->nr_frags = k;
2558}
2559
2560/**
2561 * skb_split - Split fragmented skb to two parts at length len.
2562 * @skb: the buffer to split
2563 * @skb1: the buffer to receive the second part
2564 * @len: new length for skb
2565 */
2566void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2567{
2568 int pos = skb_headlen(skb);
2569
Amerigo Wang68534c62013-02-19 22:51:30 +00002570 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (len < pos) /* Split line is inside header. */
2572 skb_split_inside_header(skb, skb1, len, pos);
2573 else /* Second chunk has no header, nothing to copy. */
2574 skb_split_no_header(skb, skb1, len, pos);
2575}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002576EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002578/* Shifting from/to a cloned skb is a no-go.
2579 *
2580 * Caller cannot keep skb_shinfo related pointers past calling here!
2581 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002582static int skb_prepare_for_shift(struct sk_buff *skb)
2583{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002584 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002585}
2586
2587/**
2588 * skb_shift - Shifts paged data partially from skb to another
2589 * @tgt: buffer into which tail data gets added
2590 * @skb: buffer from which the paged data comes from
2591 * @shiftlen: shift up to this many bytes
2592 *
2593 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002594 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002595 * It's up to caller to free skb if everything was shifted.
2596 *
2597 * If @tgt runs out of frags, the whole operation is aborted.
2598 *
2599 * Skb cannot include anything else but paged data while tgt is allowed
2600 * to have non-paged data as well.
2601 *
2602 * TODO: full sized shift could be optimized but that would need
2603 * specialized skb free'er to handle frags without up-to-date nr_frags.
2604 */
2605int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2606{
2607 int from, to, merge, todo;
2608 struct skb_frag_struct *fragfrom, *fragto;
2609
2610 BUG_ON(shiftlen > skb->len);
2611 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2612
2613 todo = shiftlen;
2614 from = 0;
2615 to = skb_shinfo(tgt)->nr_frags;
2616 fragfrom = &skb_shinfo(skb)->frags[from];
2617
2618 /* Actual merge is delayed until the point when we know we can
2619 * commit all, so that we don't have to undo partial changes
2620 */
2621 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002622 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2623 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002624 merge = -1;
2625 } else {
2626 merge = to - 1;
2627
Eric Dumazet9e903e02011-10-18 21:00:24 +00002628 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002629 if (todo < 0) {
2630 if (skb_prepare_for_shift(skb) ||
2631 skb_prepare_for_shift(tgt))
2632 return 0;
2633
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002634 /* All previous frag pointers might be stale! */
2635 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002636 fragto = &skb_shinfo(tgt)->frags[merge];
2637
Eric Dumazet9e903e02011-10-18 21:00:24 +00002638 skb_frag_size_add(fragto, shiftlen);
2639 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002640 fragfrom->page_offset += shiftlen;
2641
2642 goto onlymerged;
2643 }
2644
2645 from++;
2646 }
2647
2648 /* Skip full, not-fitting skb to avoid expensive operations */
2649 if ((shiftlen == skb->len) &&
2650 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2651 return 0;
2652
2653 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2654 return 0;
2655
2656 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2657 if (to == MAX_SKB_FRAGS)
2658 return 0;
2659
2660 fragfrom = &skb_shinfo(skb)->frags[from];
2661 fragto = &skb_shinfo(tgt)->frags[to];
2662
Eric Dumazet9e903e02011-10-18 21:00:24 +00002663 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002664 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002665 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002666 from++;
2667 to++;
2668
2669 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002670 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002671 fragto->page = fragfrom->page;
2672 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002673 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002674
2675 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002676 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002677 todo = 0;
2678
2679 to++;
2680 break;
2681 }
2682 }
2683
2684 /* Ready to "commit" this state change to tgt */
2685 skb_shinfo(tgt)->nr_frags = to;
2686
2687 if (merge >= 0) {
2688 fragfrom = &skb_shinfo(skb)->frags[0];
2689 fragto = &skb_shinfo(tgt)->frags[merge];
2690
Eric Dumazet9e903e02011-10-18 21:00:24 +00002691 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002692 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002693 }
2694
2695 /* Reposition in the original skb */
2696 to = 0;
2697 while (from < skb_shinfo(skb)->nr_frags)
2698 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2699 skb_shinfo(skb)->nr_frags = to;
2700
2701 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2702
2703onlymerged:
2704 /* Most likely the tgt won't ever need its checksum anymore, skb on
2705 * the other hand might need it if it needs to be resent
2706 */
2707 tgt->ip_summed = CHECKSUM_PARTIAL;
2708 skb->ip_summed = CHECKSUM_PARTIAL;
2709
2710 /* Yak, is it really working this way? Some helper please? */
2711 skb->len -= shiftlen;
2712 skb->data_len -= shiftlen;
2713 skb->truesize -= shiftlen;
2714 tgt->len += shiftlen;
2715 tgt->data_len += shiftlen;
2716 tgt->truesize += shiftlen;
2717
2718 return shiftlen;
2719}
2720
Thomas Graf677e90e2005-06-23 20:59:51 -07002721/**
2722 * skb_prepare_seq_read - Prepare a sequential read of skb data
2723 * @skb: the buffer to read
2724 * @from: lower offset of data to be read
2725 * @to: upper offset of data to be read
2726 * @st: state variable
2727 *
2728 * Initializes the specified state variable. Must be called before
2729 * invoking skb_seq_read() for the first time.
2730 */
2731void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2732 unsigned int to, struct skb_seq_state *st)
2733{
2734 st->lower_offset = from;
2735 st->upper_offset = to;
2736 st->root_skb = st->cur_skb = skb;
2737 st->frag_idx = st->stepped_offset = 0;
2738 st->frag_data = NULL;
2739}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002740EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002741
2742/**
2743 * skb_seq_read - Sequentially read skb data
2744 * @consumed: number of bytes consumed by the caller so far
2745 * @data: destination pointer for data to be returned
2746 * @st: state variable
2747 *
Mathias Krausebc323832013-11-07 14:18:26 +01002748 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002749 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002750 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002751 * of the block or 0 if the end of the skb data or the upper
2752 * offset has been reached.
2753 *
2754 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002755 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002756 * of bytes already consumed and the next call to
2757 * skb_seq_read() will return the remaining part of the block.
2758 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002759 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002760 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002761 * reads of potentially non linear data.
2762 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002763 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002764 * at the moment, state->root_skb could be replaced with
2765 * a stack for this purpose.
2766 */
2767unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2768 struct skb_seq_state *st)
2769{
2770 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2771 skb_frag_t *frag;
2772
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002773 if (unlikely(abs_offset >= st->upper_offset)) {
2774 if (st->frag_data) {
2775 kunmap_atomic(st->frag_data);
2776 st->frag_data = NULL;
2777 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002778 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002779 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002780
2781next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002782 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002783
Thomas Chenault995b3372009-05-18 21:43:27 -07002784 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002785 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002786 return block_limit - abs_offset;
2787 }
2788
2789 if (st->frag_idx == 0 && !st->frag_data)
2790 st->stepped_offset += skb_headlen(st->cur_skb);
2791
2792 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2793 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002794 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002795
2796 if (abs_offset < block_limit) {
2797 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002798 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002799
2800 *data = (u8 *) st->frag_data + frag->page_offset +
2801 (abs_offset - st->stepped_offset);
2802
2803 return block_limit - abs_offset;
2804 }
2805
2806 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002807 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002808 st->frag_data = NULL;
2809 }
2810
2811 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002812 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002813 }
2814
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002815 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002816 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002817 st->frag_data = NULL;
2818 }
2819
David S. Miller21dc3302010-08-23 00:13:46 -07002820 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002821 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002822 st->frag_idx = 0;
2823 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002824 } else if (st->cur_skb->next) {
2825 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002826 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002827 goto next_skb;
2828 }
2829
2830 return 0;
2831}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002832EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002833
2834/**
2835 * skb_abort_seq_read - Abort a sequential read of skb data
2836 * @st: state variable
2837 *
2838 * Must be called if skb_seq_read() was not called until it
2839 * returned 0.
2840 */
2841void skb_abort_seq_read(struct skb_seq_state *st)
2842{
2843 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002844 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002845}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002846EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002847
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002848#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2849
2850static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2851 struct ts_config *conf,
2852 struct ts_state *state)
2853{
2854 return skb_seq_read(offset, text, TS_SKB_CB(state));
2855}
2856
2857static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2858{
2859 skb_abort_seq_read(TS_SKB_CB(state));
2860}
2861
2862/**
2863 * skb_find_text - Find a text pattern in skb data
2864 * @skb: the buffer to look in
2865 * @from: search offset
2866 * @to: search limit
2867 * @config: textsearch configuration
2868 * @state: uninitialized textsearch state variable
2869 *
2870 * Finds a pattern in the skb data according to the specified
2871 * textsearch configuration. Use textsearch_next() to retrieve
2872 * subsequent occurrences of the pattern. Returns the offset
2873 * to the first occurrence or UINT_MAX if no match was found.
2874 */
2875unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2876 unsigned int to, struct ts_config *config,
2877 struct ts_state *state)
2878{
Phil Oesterf72b9482006-06-26 00:00:57 -07002879 unsigned int ret;
2880
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002881 config->get_next_block = skb_ts_get_next_block;
2882 config->finish = skb_ts_finish;
2883
2884 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2885
Phil Oesterf72b9482006-06-26 00:00:57 -07002886 ret = textsearch_find(config, state);
2887 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002888}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002889EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002890
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002891/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002892 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002893 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002894 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002895 * @getfrag: call back function to be used for getting the user data
2896 * @from: pointer to user message iov
2897 * @length: length of the iov message
2898 *
2899 * Description: This procedure append the user data in the fragment part
2900 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2901 */
2902int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002903 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002904 int len, int odd, struct sk_buff *skb),
2905 void *from, int length)
2906{
Eric Dumazetb2111722012-12-28 06:06:37 +00002907 int frg_cnt = skb_shinfo(skb)->nr_frags;
2908 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002909 int offset = 0;
2910 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002911 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002912
2913 do {
2914 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002915 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002916 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002917
Eric Dumazetb2111722012-12-28 06:06:37 +00002918 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002919 return -ENOMEM;
2920
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002921 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002922 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002923
Eric Dumazetb2111722012-12-28 06:06:37 +00002924 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2925 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002926 if (ret < 0)
2927 return -EFAULT;
2928
2929 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002930 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2931 copy);
2932 frg_cnt++;
2933 pfrag->offset += copy;
2934 get_page(pfrag->page);
2935
2936 skb->truesize += copy;
2937 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002938 skb->len += copy;
2939 skb->data_len += copy;
2940 offset += copy;
2941 length -= copy;
2942
2943 } while (length > 0);
2944
2945 return 0;
2946}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002947EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002948
Herbert Xucbb042f2006-03-20 22:43:56 -08002949/**
2950 * skb_pull_rcsum - pull skb and update receive checksum
2951 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002952 * @len: length of data pulled
2953 *
2954 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002955 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002956 * receive path processing instead of skb_pull unless you know
2957 * that the checksum difference is zero (e.g., a valid IP header)
2958 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002959 */
2960unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2961{
2962 BUG_ON(len > skb->len);
2963 skb->len -= len;
2964 BUG_ON(skb->len < skb->data_len);
2965 skb_postpull_rcsum(skb, skb->data, len);
2966 return skb->data += len;
2967}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002968EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2969
Herbert Xuf4c50d92006-06-22 03:02:40 -07002970/**
2971 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002972 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002973 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002974 *
2975 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002976 * a pointer to the first in a list of new skbs for the segments.
2977 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002978 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002979struct sk_buff *skb_segment(struct sk_buff *head_skb,
2980 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002981{
2982 struct sk_buff *segs = NULL;
2983 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002984 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002985 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2986 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2987 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002988 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002989 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002990 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002991 unsigned int headroom;
2992 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002993 __be16 proto;
2994 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002995 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002996 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002997 int err = -ENOMEM;
2998 int i = 0;
2999 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003000 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003001
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003002 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003003 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003004 if (unlikely(!proto))
3005 return ERR_PTR(-EINVAL);
3006
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003007 csum = !head_skb->encap_hdr_csum &&
3008 !!can_checksum_protocol(features, proto);
3009
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003010 headroom = skb_headroom(head_skb);
3011 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003012
3013 do {
3014 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003015 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003016 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003017 int size;
3018
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003019 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003020 if (len > mss)
3021 len = mss;
3022
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003023 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003024 if (hsize < 0)
3025 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003026 if (hsize > len || !sg)
3027 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003028
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003029 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3030 (skb_headlen(list_skb) == len || sg)) {
3031 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003032
Herbert Xu9d8506c2013-11-21 11:10:04 -08003033 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003034 nfrags = skb_shinfo(list_skb)->nr_frags;
3035 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003036 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003037 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003038
3039 while (pos < offset + len) {
3040 BUG_ON(i >= nfrags);
3041
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003042 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003043 if (pos + size > offset + len)
3044 break;
3045
3046 i++;
3047 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003048 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003049 }
3050
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003051 nskb = skb_clone(list_skb, GFP_ATOMIC);
3052 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003053
3054 if (unlikely(!nskb))
3055 goto err;
3056
Herbert Xu9d8506c2013-11-21 11:10:04 -08003057 if (unlikely(pskb_trim(nskb, len))) {
3058 kfree_skb(nskb);
3059 goto err;
3060 }
3061
Alexander Duyckec47ea82012-05-04 14:26:56 +00003062 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003063 if (skb_cow_head(nskb, doffset + headroom)) {
3064 kfree_skb(nskb);
3065 goto err;
3066 }
3067
Alexander Duyckec47ea82012-05-04 14:26:56 +00003068 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003069 skb_release_head_state(nskb);
3070 __skb_push(nskb, doffset);
3071 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003072 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003073 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003074 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003075
3076 if (unlikely(!nskb))
3077 goto err;
3078
3079 skb_reserve(nskb, headroom);
3080 __skb_put(nskb, doffset);
3081 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003082
3083 if (segs)
3084 tail->next = nskb;
3085 else
3086 segs = nskb;
3087 tail = nskb;
3088
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003089 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003090
Eric Dumazet030737b2013-10-19 11:42:54 -07003091 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003092 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003093
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003094 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003095 nskb->data - tnl_hlen,
3096 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003097
Herbert Xu9d8506c2013-11-21 11:10:04 -08003098 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003099 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003100
Tom Herberte585f232014-11-04 09:06:54 -08003101 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003102 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003103 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003104 skb_put(nskb, len),
3105 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003106 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003107 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003108 continue;
3109 }
3110
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003111 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003112
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003113 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003114 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003115
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003116 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3117 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003118
Herbert Xu9d8506c2013-11-21 11:10:04 -08003119 while (pos < offset + len) {
3120 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003121 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003122
3123 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003124 nfrags = skb_shinfo(list_skb)->nr_frags;
3125 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003126 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003127
3128 BUG_ON(!nfrags);
3129
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003130 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003131 }
3132
3133 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3134 MAX_SKB_FRAGS)) {
3135 net_warn_ratelimited(
3136 "skb_segment: too many frags: %u %u\n",
3137 pos, mss);
3138 goto err;
3139 }
3140
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003141 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3142 goto err;
3143
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003144 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003145 __skb_frag_ref(nskb_frag);
3146 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003147
3148 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003149 nskb_frag->page_offset += offset - pos;
3150 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003151 }
3152
Herbert Xu89319d382008-12-15 23:26:06 -08003153 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003154
3155 if (pos + size <= offset + len) {
3156 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003157 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003158 pos += size;
3159 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003160 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003161 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003162 }
3163
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003164 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003165 }
3166
Herbert Xu89319d382008-12-15 23:26:06 -08003167skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003168 nskb->data_len = len - hsize;
3169 nskb->len += nskb->data_len;
3170 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003171
Simon Horman1cdbcb72013-05-19 15:46:49 +00003172perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003173 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003174 nskb->csum = skb_checksum(nskb, doffset,
3175 nskb->len - doffset, 0);
3176 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003177 SKB_GSO_CB(nskb)->csum_start =
3178 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003179 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003180 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003181
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003182 /* Some callers want to get the end of the list.
3183 * Put it in segs->prev to avoid walking the list.
3184 * (see validate_xmit_skb_list() for example)
3185 */
3186 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003187
3188 /* Following permits correct backpressure, for protocols
3189 * using skb_set_owner_w().
3190 * Idea is to tranfert ownership from head_skb to last segment.
3191 */
3192 if (head_skb->destructor == sock_wfree) {
3193 swap(tail->truesize, head_skb->truesize);
3194 swap(tail->destructor, head_skb->destructor);
3195 swap(tail->sk, head_skb->sk);
3196 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003197 return segs;
3198
3199err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003200 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003201 return ERR_PTR(err);
3202}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003203EXPORT_SYMBOL_GPL(skb_segment);
3204
Herbert Xu71d93b32008-12-15 23:42:33 -08003205int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3206{
Eric Dumazet8a291112013-10-08 09:02:23 -07003207 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003208 unsigned int offset = skb_gro_offset(skb);
3209 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003210 struct sk_buff *nskb, *lp, *p = *head;
3211 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003212 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003213 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003214
Eric Dumazet8a291112013-10-08 09:02:23 -07003215 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003216 return -E2BIG;
3217
Eric Dumazet29e98242014-05-16 11:34:37 -07003218 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003219 pinfo = skb_shinfo(lp);
3220
3221 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003222 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003223 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003224 int i = skbinfo->nr_frags;
3225 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003226
Herbert Xu66e92fc2009-05-26 18:50:32 +00003227 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003228 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003229
Eric Dumazet8a291112013-10-08 09:02:23 -07003230 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003231 pinfo->nr_frags = nr_frags;
3232 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003233
Herbert Xu9aaa1562009-05-26 18:50:33 +00003234 frag = pinfo->frags + nr_frags;
3235 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003236 do {
3237 *--frag = *--frag2;
3238 } while (--i);
3239
3240 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003241 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003242
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003243 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003244 delta_truesize = skb->truesize -
3245 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003246
Herbert Xuf5572062009-01-14 20:40:03 -08003247 skb->truesize -= skb->data_len;
3248 skb->len -= skb->data_len;
3249 skb->data_len = 0;
3250
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003251 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003252 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003253 } else if (skb->head_frag) {
3254 int nr_frags = pinfo->nr_frags;
3255 skb_frag_t *frag = pinfo->frags + nr_frags;
3256 struct page *page = virt_to_head_page(skb->head);
3257 unsigned int first_size = headlen - offset;
3258 unsigned int first_offset;
3259
3260 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003261 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003262
3263 first_offset = skb->data -
3264 (unsigned char *)page_address(page) +
3265 offset;
3266
3267 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3268
3269 frag->page.p = page;
3270 frag->page_offset = first_offset;
3271 skb_frag_size_set(frag, first_size);
3272
3273 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3274 /* We dont need to clear skbinfo->nr_frags here */
3275
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003276 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003277 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3278 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003279 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003280 /* switch back to head shinfo */
3281 pinfo = skb_shinfo(p);
3282
Eric Dumazet8a291112013-10-08 09:02:23 -07003283 if (pinfo->frag_list)
3284 goto merge;
3285 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003286 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003287
3288 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003289 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003290 if (unlikely(!nskb))
3291 return -ENOMEM;
3292
3293 __copy_skb_header(nskb, p);
3294 nskb->mac_len = p->mac_len;
3295
3296 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003297 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003298
Herbert Xu86911732009-01-29 14:19:50 +00003299 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003300 skb_set_network_header(nskb, skb_network_offset(p));
3301 skb_set_transport_header(nskb, skb_transport_offset(p));
3302
Herbert Xu86911732009-01-29 14:19:50 +00003303 __skb_pull(p, skb_gro_offset(p));
3304 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3305 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003306
Herbert Xu71d93b32008-12-15 23:42:33 -08003307 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003308 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003309 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003310 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003311 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003312
3313 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003314 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003315 nskb->len += p->len;
3316
3317 *head = nskb;
3318 nskb->next = p->next;
3319 p->next = NULL;
3320
3321 p = nskb;
3322
3323merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003324 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003325 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003326 unsigned int eat = offset - headlen;
3327
3328 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003329 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003330 skb->data_len -= eat;
3331 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003332 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003333 }
3334
Herbert Xu67147ba2009-05-26 18:50:22 +00003335 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003336
Eric Dumazet29e98242014-05-16 11:34:37 -07003337 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003338 skb_shinfo(p)->frag_list = skb;
3339 else
3340 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003341 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003342 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003343 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003344
Herbert Xu5d38a072009-01-04 16:13:40 -08003345done:
3346 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003347 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003348 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003349 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003350 if (lp != p) {
3351 lp->data_len += len;
3352 lp->truesize += delta_truesize;
3353 lp->len += len;
3354 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003355 NAPI_GRO_CB(skb)->same_flow = 1;
3356 return 0;
3357}
Herbert Xu71d93b32008-12-15 23:42:33 -08003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359void __init skb_init(void)
3360{
3361 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3362 sizeof(struct sk_buff),
3363 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003364 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003365 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003366 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003367 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003368 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003369 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003370 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
David Howells716ea3a2007-04-02 20:19:53 -07003373/**
3374 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3375 * @skb: Socket buffer containing the buffers to be mapped
3376 * @sg: The scatter-gather list to map into
3377 * @offset: The offset into the buffer's contents to start mapping
3378 * @len: Length of buffer space to be mapped
3379 *
3380 * Fill the specified scatter-gather list with mappings/pointers into a
3381 * region of the buffer space attached to a socket buffer.
3382 */
David S. Miller51c739d2007-10-30 21:29:29 -07003383static int
3384__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003385{
David S. Miller1a028e52007-04-27 15:21:23 -07003386 int start = skb_headlen(skb);
3387 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003388 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003389 int elt = 0;
3390
3391 if (copy > 0) {
3392 if (copy > len)
3393 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003394 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003395 elt++;
3396 if ((len -= copy) == 0)
3397 return elt;
3398 offset += copy;
3399 }
3400
3401 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003402 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003403
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003404 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003405
Eric Dumazet9e903e02011-10-18 21:00:24 +00003406 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003407 if ((copy = end - offset) > 0) {
3408 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3409
3410 if (copy > len)
3411 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003412 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003413 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003414 elt++;
3415 if (!(len -= copy))
3416 return elt;
3417 offset += copy;
3418 }
David S. Miller1a028e52007-04-27 15:21:23 -07003419 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003420 }
3421
David S. Millerfbb398a2009-06-09 00:18:59 -07003422 skb_walk_frags(skb, frag_iter) {
3423 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003424
David S. Millerfbb398a2009-06-09 00:18:59 -07003425 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003426
David S. Millerfbb398a2009-06-09 00:18:59 -07003427 end = start + frag_iter->len;
3428 if ((copy = end - offset) > 0) {
3429 if (copy > len)
3430 copy = len;
3431 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3432 copy);
3433 if ((len -= copy) == 0)
3434 return elt;
3435 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003436 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003437 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003438 }
3439 BUG_ON(len);
3440 return elt;
3441}
3442
Fan Du25a91d82014-01-18 09:54:23 +08003443/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3444 * sglist without mark the sg which contain last skb data as the end.
3445 * So the caller can mannipulate sg list as will when padding new data after
3446 * the first call without calling sg_unmark_end to expend sg list.
3447 *
3448 * Scenario to use skb_to_sgvec_nomark:
3449 * 1. sg_init_table
3450 * 2. skb_to_sgvec_nomark(payload1)
3451 * 3. skb_to_sgvec_nomark(payload2)
3452 *
3453 * This is equivalent to:
3454 * 1. sg_init_table
3455 * 2. skb_to_sgvec(payload1)
3456 * 3. sg_unmark_end
3457 * 4. skb_to_sgvec(payload2)
3458 *
3459 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3460 * is more preferable.
3461 */
3462int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3463 int offset, int len)
3464{
3465 return __skb_to_sgvec(skb, sg, offset, len);
3466}
3467EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3468
David S. Miller51c739d2007-10-30 21:29:29 -07003469int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3470{
3471 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3472
Jens Axboec46f2332007-10-31 12:06:37 +01003473 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003474
3475 return nsg;
3476}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003477EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003478
David Howells716ea3a2007-04-02 20:19:53 -07003479/**
3480 * skb_cow_data - Check that a socket buffer's data buffers are writable
3481 * @skb: The socket buffer to check.
3482 * @tailbits: Amount of trailing space to be added
3483 * @trailer: Returned pointer to the skb where the @tailbits space begins
3484 *
3485 * Make sure that the data buffers attached to a socket buffer are
3486 * writable. If they are not, private copies are made of the data buffers
3487 * and the socket buffer is set to use these instead.
3488 *
3489 * If @tailbits is given, make sure that there is space to write @tailbits
3490 * bytes of data beyond current end of socket buffer. @trailer will be
3491 * set to point to the skb in which this space begins.
3492 *
3493 * The number of scatterlist elements required to completely map the
3494 * COW'd and extended socket buffer will be returned.
3495 */
3496int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3497{
3498 int copyflag;
3499 int elt;
3500 struct sk_buff *skb1, **skb_p;
3501
3502 /* If skb is cloned or its head is paged, reallocate
3503 * head pulling out all the pages (pages are considered not writable
3504 * at the moment even if they are anonymous).
3505 */
3506 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3507 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3508 return -ENOMEM;
3509
3510 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003511 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003512 /* A little of trouble, not enough of space for trailer.
3513 * This should not happen, when stack is tuned to generate
3514 * good frames. OK, on miss we reallocate and reserve even more
3515 * space, 128 bytes is fair. */
3516
3517 if (skb_tailroom(skb) < tailbits &&
3518 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3519 return -ENOMEM;
3520
3521 /* Voila! */
3522 *trailer = skb;
3523 return 1;
3524 }
3525
3526 /* Misery. We are in troubles, going to mincer fragments... */
3527
3528 elt = 1;
3529 skb_p = &skb_shinfo(skb)->frag_list;
3530 copyflag = 0;
3531
3532 while ((skb1 = *skb_p) != NULL) {
3533 int ntail = 0;
3534
3535 /* The fragment is partially pulled by someone,
3536 * this can happen on input. Copy it and everything
3537 * after it. */
3538
3539 if (skb_shared(skb1))
3540 copyflag = 1;
3541
3542 /* If the skb is the last, worry about trailer. */
3543
3544 if (skb1->next == NULL && tailbits) {
3545 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003546 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003547 skb_tailroom(skb1) < tailbits)
3548 ntail = tailbits + 128;
3549 }
3550
3551 if (copyflag ||
3552 skb_cloned(skb1) ||
3553 ntail ||
3554 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003555 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003556 struct sk_buff *skb2;
3557
3558 /* Fuck, we are miserable poor guys... */
3559 if (ntail == 0)
3560 skb2 = skb_copy(skb1, GFP_ATOMIC);
3561 else
3562 skb2 = skb_copy_expand(skb1,
3563 skb_headroom(skb1),
3564 ntail,
3565 GFP_ATOMIC);
3566 if (unlikely(skb2 == NULL))
3567 return -ENOMEM;
3568
3569 if (skb1->sk)
3570 skb_set_owner_w(skb2, skb1->sk);
3571
3572 /* Looking around. Are we still alive?
3573 * OK, link new skb, drop old one */
3574
3575 skb2->next = skb1->next;
3576 *skb_p = skb2;
3577 kfree_skb(skb1);
3578 skb1 = skb2;
3579 }
3580 elt++;
3581 *trailer = skb1;
3582 skb_p = &skb1->next;
3583 }
3584
3585 return elt;
3586}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003587EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003588
Eric Dumazetb1faf562010-05-31 23:44:05 -07003589static void sock_rmem_free(struct sk_buff *skb)
3590{
3591 struct sock *sk = skb->sk;
3592
3593 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3594}
3595
3596/*
3597 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3598 */
3599int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3600{
3601 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003602 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003603 return -ENOMEM;
3604
3605 skb_orphan(skb);
3606 skb->sk = sk;
3607 skb->destructor = sock_rmem_free;
3608 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3609
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003610 /* before exiting rcu section, make sure dst is refcounted */
3611 skb_dst_force(skb);
3612
Eric Dumazetb1faf562010-05-31 23:44:05 -07003613 skb_queue_tail(&sk->sk_error_queue, skb);
3614 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003615 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003616 return 0;
3617}
3618EXPORT_SYMBOL(sock_queue_err_skb);
3619
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003620struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3621{
3622 struct sk_buff_head *q = &sk->sk_error_queue;
3623 struct sk_buff *skb, *skb_next;
3624 int err = 0;
3625
3626 spin_lock_bh(&q->lock);
3627 skb = __skb_dequeue(q);
3628 if (skb && (skb_next = skb_peek(q)))
3629 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3630 spin_unlock_bh(&q->lock);
3631
3632 sk->sk_err = err;
3633 if (err)
3634 sk->sk_error_report(sk);
3635
3636 return skb;
3637}
3638EXPORT_SYMBOL(sock_dequeue_err_skb);
3639
Alexander Duyckcab41c42014-09-10 18:05:26 -04003640/**
3641 * skb_clone_sk - create clone of skb, and take reference to socket
3642 * @skb: the skb to clone
3643 *
3644 * This function creates a clone of a buffer that holds a reference on
3645 * sk_refcnt. Buffers created via this function are meant to be
3646 * returned using sock_queue_err_skb, or free via kfree_skb.
3647 *
3648 * When passing buffers allocated with this function to sock_queue_err_skb
3649 * it is necessary to wrap the call with sock_hold/sock_put in order to
3650 * prevent the socket from being released prior to being enqueued on
3651 * the sk_error_queue.
3652 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003653struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3654{
3655 struct sock *sk = skb->sk;
3656 struct sk_buff *clone;
3657
3658 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3659 return NULL;
3660
3661 clone = skb_clone(skb, GFP_ATOMIC);
3662 if (!clone) {
3663 sock_put(sk);
3664 return NULL;
3665 }
3666
3667 clone->sk = sk;
3668 clone->destructor = sock_efree;
3669
3670 return clone;
3671}
3672EXPORT_SYMBOL(skb_clone_sk);
3673
Alexander Duyck37846ef2014-09-04 13:31:10 -04003674static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3675 struct sock *sk,
3676 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003677{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003678 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003679 int err;
3680
Patrick Ohlyac45f602009-02-12 05:03:37 +00003681 serr = SKB_EXT_ERR(skb);
3682 memset(serr, 0, sizeof(*serr));
3683 serr->ee.ee_errno = ENOMSG;
3684 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003685 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003686 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003687 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003688 if (sk->sk_protocol == IPPROTO_TCP)
3689 serr->ee.ee_data -= sk->sk_tskey;
3690 }
Eric Dumazet29030372010-05-29 00:20:48 -07003691
Patrick Ohlyac45f602009-02-12 05:03:37 +00003692 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003693
Patrick Ohlyac45f602009-02-12 05:03:37 +00003694 if (err)
3695 kfree_skb(skb);
3696}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003697
Willem de Bruijnb245be12015-01-30 13:29:32 -05003698static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3699{
3700 bool ret;
3701
3702 if (likely(sysctl_tstamp_allow_data || tsonly))
3703 return true;
3704
3705 read_lock_bh(&sk->sk_callback_lock);
3706 ret = sk->sk_socket && sk->sk_socket->file &&
3707 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3708 read_unlock_bh(&sk->sk_callback_lock);
3709 return ret;
3710}
3711
Alexander Duyck37846ef2014-09-04 13:31:10 -04003712void skb_complete_tx_timestamp(struct sk_buff *skb,
3713 struct skb_shared_hwtstamps *hwtstamps)
3714{
3715 struct sock *sk = skb->sk;
3716
Willem de Bruijnb245be12015-01-30 13:29:32 -05003717 if (!skb_may_tx_timestamp(sk, false))
3718 return;
3719
Alexander Duyck62bccb82014-09-04 13:31:35 -04003720 /* take a reference to prevent skb_orphan() from freeing the socket */
3721 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003722
Alexander Duyck62bccb82014-09-04 13:31:35 -04003723 *skb_hwtstamps(skb) = *hwtstamps;
3724 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003725
3726 sock_put(sk);
3727}
3728EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3729
3730void __skb_tstamp_tx(struct sk_buff *orig_skb,
3731 struct skb_shared_hwtstamps *hwtstamps,
3732 struct sock *sk, int tstype)
3733{
3734 struct sk_buff *skb;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003735 bool tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003736
Willem de Bruijnb245be12015-01-30 13:29:32 -05003737 if (!sk || !skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003738 return;
3739
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003740 if (tsonly)
3741 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003742 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003743 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003744 if (!skb)
3745 return;
3746
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003747 if (tsonly) {
3748 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3749 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3750 }
3751
3752 if (hwtstamps)
3753 *skb_hwtstamps(skb) = *hwtstamps;
3754 else
3755 skb->tstamp = ktime_get_real();
3756
Alexander Duyck37846ef2014-09-04 13:31:10 -04003757 __skb_complete_tx_timestamp(skb, sk, tstype);
3758}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003759EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3760
3761void skb_tstamp_tx(struct sk_buff *orig_skb,
3762 struct skb_shared_hwtstamps *hwtstamps)
3763{
3764 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3765 SCM_TSTAMP_SND);
3766}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003767EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3768
Johannes Berg6e3e9392011-11-09 10:15:42 +01003769void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3770{
3771 struct sock *sk = skb->sk;
3772 struct sock_exterr_skb *serr;
3773 int err;
3774
3775 skb->wifi_acked_valid = 1;
3776 skb->wifi_acked = acked;
3777
3778 serr = SKB_EXT_ERR(skb);
3779 memset(serr, 0, sizeof(*serr));
3780 serr->ee.ee_errno = ENOMSG;
3781 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3782
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003783 /* take a reference to prevent skb_orphan() from freeing the socket */
3784 sock_hold(sk);
3785
Johannes Berg6e3e9392011-11-09 10:15:42 +01003786 err = sock_queue_err_skb(sk, skb);
3787 if (err)
3788 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003789
3790 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003791}
3792EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3793
Patrick Ohlyac45f602009-02-12 05:03:37 +00003794
Rusty Russellf35d9d82008-02-04 23:49:54 -05003795/**
3796 * skb_partial_csum_set - set up and verify partial csum values for packet
3797 * @skb: the skb to set
3798 * @start: the number of bytes after skb->data to start checksumming.
3799 * @off: the offset from start to place the checksum.
3800 *
3801 * For untrusted partially-checksummed packets, we need to make sure the values
3802 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3803 *
3804 * This function checks and sets those values and skb->ip_summed: if this
3805 * returns false you should drop the packet.
3806 */
3807bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3808{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003809 if (unlikely(start > skb_headlen(skb)) ||
3810 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003811 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3812 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003813 return false;
3814 }
3815 skb->ip_summed = CHECKSUM_PARTIAL;
3816 skb->csum_start = skb_headroom(skb) + start;
3817 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003818 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003819 return true;
3820}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003821EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003822
Paul Durranted1f50c2014-01-09 10:02:46 +00003823static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3824 unsigned int max)
3825{
3826 if (skb_headlen(skb) >= len)
3827 return 0;
3828
3829 /* If we need to pullup then pullup to the max, so we
3830 * won't need to do it again.
3831 */
3832 if (max > skb->len)
3833 max = skb->len;
3834
3835 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3836 return -ENOMEM;
3837
3838 if (skb_headlen(skb) < len)
3839 return -EPROTO;
3840
3841 return 0;
3842}
3843
Jan Beulichf9708b42014-03-11 13:56:05 +00003844#define MAX_TCP_HDR_LEN (15 * 4)
3845
3846static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3847 typeof(IPPROTO_IP) proto,
3848 unsigned int off)
3849{
3850 switch (proto) {
3851 int err;
3852
3853 case IPPROTO_TCP:
3854 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3855 off + MAX_TCP_HDR_LEN);
3856 if (!err && !skb_partial_csum_set(skb, off,
3857 offsetof(struct tcphdr,
3858 check)))
3859 err = -EPROTO;
3860 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3861
3862 case IPPROTO_UDP:
3863 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3864 off + sizeof(struct udphdr));
3865 if (!err && !skb_partial_csum_set(skb, off,
3866 offsetof(struct udphdr,
3867 check)))
3868 err = -EPROTO;
3869 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3870 }
3871
3872 return ERR_PTR(-EPROTO);
3873}
3874
Paul Durranted1f50c2014-01-09 10:02:46 +00003875/* This value should be large enough to cover a tagged ethernet header plus
3876 * maximally sized IP and TCP or UDP headers.
3877 */
3878#define MAX_IP_HDR_LEN 128
3879
Jan Beulichf9708b42014-03-11 13:56:05 +00003880static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003881{
3882 unsigned int off;
3883 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003884 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003885 int err;
3886
3887 fragment = false;
3888
3889 err = skb_maybe_pull_tail(skb,
3890 sizeof(struct iphdr),
3891 MAX_IP_HDR_LEN);
3892 if (err < 0)
3893 goto out;
3894
3895 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3896 fragment = true;
3897
3898 off = ip_hdrlen(skb);
3899
3900 err = -EPROTO;
3901
3902 if (fragment)
3903 goto out;
3904
Jan Beulichf9708b42014-03-11 13:56:05 +00003905 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3906 if (IS_ERR(csum))
3907 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003908
Jan Beulichf9708b42014-03-11 13:56:05 +00003909 if (recalculate)
3910 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3911 ip_hdr(skb)->daddr,
3912 skb->len - off,
3913 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003914 err = 0;
3915
3916out:
3917 return err;
3918}
3919
3920/* This value should be large enough to cover a tagged ethernet header plus
3921 * an IPv6 header, all options, and a maximal TCP or UDP header.
3922 */
3923#define MAX_IPV6_HDR_LEN 256
3924
3925#define OPT_HDR(type, skb, off) \
3926 (type *)(skb_network_header(skb) + (off))
3927
3928static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3929{
3930 int err;
3931 u8 nexthdr;
3932 unsigned int off;
3933 unsigned int len;
3934 bool fragment;
3935 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003936 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003937
3938 fragment = false;
3939 done = false;
3940
3941 off = sizeof(struct ipv6hdr);
3942
3943 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3944 if (err < 0)
3945 goto out;
3946
3947 nexthdr = ipv6_hdr(skb)->nexthdr;
3948
3949 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3950 while (off <= len && !done) {
3951 switch (nexthdr) {
3952 case IPPROTO_DSTOPTS:
3953 case IPPROTO_HOPOPTS:
3954 case IPPROTO_ROUTING: {
3955 struct ipv6_opt_hdr *hp;
3956
3957 err = skb_maybe_pull_tail(skb,
3958 off +
3959 sizeof(struct ipv6_opt_hdr),
3960 MAX_IPV6_HDR_LEN);
3961 if (err < 0)
3962 goto out;
3963
3964 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3965 nexthdr = hp->nexthdr;
3966 off += ipv6_optlen(hp);
3967 break;
3968 }
3969 case IPPROTO_AH: {
3970 struct ip_auth_hdr *hp;
3971
3972 err = skb_maybe_pull_tail(skb,
3973 off +
3974 sizeof(struct ip_auth_hdr),
3975 MAX_IPV6_HDR_LEN);
3976 if (err < 0)
3977 goto out;
3978
3979 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3980 nexthdr = hp->nexthdr;
3981 off += ipv6_authlen(hp);
3982 break;
3983 }
3984 case IPPROTO_FRAGMENT: {
3985 struct frag_hdr *hp;
3986
3987 err = skb_maybe_pull_tail(skb,
3988 off +
3989 sizeof(struct frag_hdr),
3990 MAX_IPV6_HDR_LEN);
3991 if (err < 0)
3992 goto out;
3993
3994 hp = OPT_HDR(struct frag_hdr, skb, off);
3995
3996 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3997 fragment = true;
3998
3999 nexthdr = hp->nexthdr;
4000 off += sizeof(struct frag_hdr);
4001 break;
4002 }
4003 default:
4004 done = true;
4005 break;
4006 }
4007 }
4008
4009 err = -EPROTO;
4010
4011 if (!done || fragment)
4012 goto out;
4013
Jan Beulichf9708b42014-03-11 13:56:05 +00004014 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4015 if (IS_ERR(csum))
4016 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004017
Jan Beulichf9708b42014-03-11 13:56:05 +00004018 if (recalculate)
4019 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4020 &ipv6_hdr(skb)->daddr,
4021 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004022 err = 0;
4023
4024out:
4025 return err;
4026}
4027
4028/**
4029 * skb_checksum_setup - set up partial checksum offset
4030 * @skb: the skb to set up
4031 * @recalculate: if true the pseudo-header checksum will be recalculated
4032 */
4033int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4034{
4035 int err;
4036
4037 switch (skb->protocol) {
4038 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004039 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004040 break;
4041
4042 case htons(ETH_P_IPV6):
4043 err = skb_checksum_setup_ipv6(skb, recalculate);
4044 break;
4045
4046 default:
4047 err = -EPROTO;
4048 break;
4049 }
4050
4051 return err;
4052}
4053EXPORT_SYMBOL(skb_checksum_setup);
4054
Ben Hutchings4497b072008-06-19 16:22:28 -07004055void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4056{
Joe Perchese87cc472012-05-13 21:56:26 +00004057 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4058 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004059}
Ben Hutchings4497b072008-06-19 16:22:28 -07004060EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004061
4062void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4063{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004064 if (head_stolen) {
4065 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004066 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004067 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004068 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004069 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004070}
4071EXPORT_SYMBOL(kfree_skb_partial);
4072
4073/**
4074 * skb_try_coalesce - try to merge skb to prior one
4075 * @to: prior buffer
4076 * @from: buffer to add
4077 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004078 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004079 */
4080bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4081 bool *fragstolen, int *delta_truesize)
4082{
4083 int i, delta, len = from->len;
4084
4085 *fragstolen = false;
4086
4087 if (skb_cloned(to))
4088 return false;
4089
4090 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004091 if (len)
4092 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004093 *delta_truesize = 0;
4094 return true;
4095 }
4096
4097 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4098 return false;
4099
4100 if (skb_headlen(from) != 0) {
4101 struct page *page;
4102 unsigned int offset;
4103
4104 if (skb_shinfo(to)->nr_frags +
4105 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4106 return false;
4107
4108 if (skb_head_is_locked(from))
4109 return false;
4110
4111 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4112
4113 page = virt_to_head_page(from->head);
4114 offset = from->data - (unsigned char *)page_address(page);
4115
4116 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4117 page, offset, skb_headlen(from));
4118 *fragstolen = true;
4119 } else {
4120 if (skb_shinfo(to)->nr_frags +
4121 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4122 return false;
4123
Weiping Panf4b549a2012-09-28 20:15:30 +00004124 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004125 }
4126
4127 WARN_ON_ONCE(delta < len);
4128
4129 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4130 skb_shinfo(from)->frags,
4131 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4132 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4133
4134 if (!skb_cloned(from))
4135 skb_shinfo(from)->nr_frags = 0;
4136
Li RongQing8ea853f2012-09-18 16:53:21 +00004137 /* if the skb is not cloned this does nothing
4138 * since we set nr_frags to 0.
4139 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004140 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4141 skb_frag_ref(from, i);
4142
4143 to->truesize += delta;
4144 to->len += len;
4145 to->data_len += len;
4146
4147 *delta_truesize = delta;
4148 return true;
4149}
4150EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004151
4152/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004153 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004154 *
4155 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004156 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004157 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004158 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4159 * into/from a tunnel. Some information have to be cleared during these
4160 * operations.
4161 * skb_scrub_packet can also be used to clean a skb before injecting it in
4162 * another namespace (@xnet == true). We have to clear all information in the
4163 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004164 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004165void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004166{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004167 if (xnet)
4168 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004169 skb->tstamp.tv64 = 0;
4170 skb->pkt_type = PACKET_HOST;
4171 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004172 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004173 skb_dst_drop(skb);
4174 skb->mark = 0;
Eric Dumazet2bd82482015-02-03 23:48:24 -08004175 skb->sender_cpu = 0;
Thomas Grafb8fb4e02014-12-23 01:13:18 +01004176 skb_init_secmark(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004177 secpath_reset(skb);
4178 nf_reset(skb);
4179 nf_reset_trace(skb);
4180}
4181EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004182
4183/**
4184 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4185 *
4186 * @skb: GSO skb
4187 *
4188 * skb_gso_transport_seglen is used to determine the real size of the
4189 * individual segments, including Layer4 headers (TCP/UDP).
4190 *
4191 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4192 */
4193unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4194{
4195 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004196 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004197
Florian Westphalf993bc22014-10-20 13:49:18 +02004198 if (skb->encapsulation) {
4199 thlen = skb_inner_transport_header(skb) -
4200 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004201
Florian Westphalf993bc22014-10-20 13:49:18 +02004202 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4203 thlen += inner_tcp_hdrlen(skb);
4204 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4205 thlen = tcp_hdrlen(skb);
4206 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004207 /* UFO sets gso_size to the size of the fragmentation
4208 * payload, i.e. the size of the L4 (UDP) header is already
4209 * accounted for.
4210 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004211 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004212}
4213EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004214
4215static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4216{
4217 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4218 kfree_skb(skb);
4219 return NULL;
4220 }
4221
4222 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4223 skb->mac_header += VLAN_HLEN;
4224 return skb;
4225}
4226
4227struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4228{
4229 struct vlan_hdr *vhdr;
4230 u16 vlan_tci;
4231
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004232 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004233 /* vlan_tci is already set-up so leave this for another time */
4234 return skb;
4235 }
4236
4237 skb = skb_share_check(skb, GFP_ATOMIC);
4238 if (unlikely(!skb))
4239 goto err_free;
4240
4241 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4242 goto err_free;
4243
4244 vhdr = (struct vlan_hdr *)skb->data;
4245 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4246 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4247
4248 skb_pull_rcsum(skb, VLAN_HLEN);
4249 vlan_set_encap_proto(skb, vhdr);
4250
4251 skb = skb_reorder_vlan_header(skb);
4252 if (unlikely(!skb))
4253 goto err_free;
4254
4255 skb_reset_network_header(skb);
4256 skb_reset_transport_header(skb);
4257 skb_reset_mac_len(skb);
4258
4259 return skb;
4260
4261err_free:
4262 kfree_skb(skb);
4263 return NULL;
4264}
4265EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004266
Jiri Pirkoe2195122014-11-19 14:05:01 +01004267int skb_ensure_writable(struct sk_buff *skb, int write_len)
4268{
4269 if (!pskb_may_pull(skb, write_len))
4270 return -ENOMEM;
4271
4272 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4273 return 0;
4274
4275 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4276}
4277EXPORT_SYMBOL(skb_ensure_writable);
4278
Jiri Pirko93515d52014-11-19 14:05:02 +01004279/* remove VLAN header from packet and update csum accordingly. */
4280static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4281{
4282 struct vlan_hdr *vhdr;
4283 unsigned int offset = skb->data - skb_mac_header(skb);
4284 int err;
4285
4286 __skb_push(skb, offset);
4287 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4288 if (unlikely(err))
4289 goto pull;
4290
4291 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4292
4293 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4294 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4295
4296 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4297 __skb_pull(skb, VLAN_HLEN);
4298
4299 vlan_set_encap_proto(skb, vhdr);
4300 skb->mac_header += VLAN_HLEN;
4301
4302 if (skb_network_offset(skb) < ETH_HLEN)
4303 skb_set_network_header(skb, ETH_HLEN);
4304
4305 skb_reset_mac_len(skb);
4306pull:
4307 __skb_pull(skb, offset);
4308
4309 return err;
4310}
4311
4312int skb_vlan_pop(struct sk_buff *skb)
4313{
4314 u16 vlan_tci;
4315 __be16 vlan_proto;
4316 int err;
4317
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004318 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004319 skb->vlan_tci = 0;
4320 } else {
4321 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4322 skb->protocol != htons(ETH_P_8021AD)) ||
4323 skb->len < VLAN_ETH_HLEN))
4324 return 0;
4325
4326 err = __skb_vlan_pop(skb, &vlan_tci);
4327 if (err)
4328 return err;
4329 }
4330 /* move next vlan tag to hw accel tag */
4331 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4332 skb->protocol != htons(ETH_P_8021AD)) ||
4333 skb->len < VLAN_ETH_HLEN))
4334 return 0;
4335
4336 vlan_proto = skb->protocol;
4337 err = __skb_vlan_pop(skb, &vlan_tci);
4338 if (unlikely(err))
4339 return err;
4340
4341 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4342 return 0;
4343}
4344EXPORT_SYMBOL(skb_vlan_pop);
4345
4346int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4347{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004348 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004349 unsigned int offset = skb->data - skb_mac_header(skb);
4350 int err;
4351
4352 /* __vlan_insert_tag expect skb->data pointing to mac header.
4353 * So change skb->data before calling it and change back to
4354 * original position later
4355 */
4356 __skb_push(skb, offset);
4357 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004358 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004359 if (err)
4360 return err;
4361 skb->protocol = skb->vlan_proto;
4362 skb->mac_len += VLAN_HLEN;
4363 __skb_pull(skb, offset);
4364
4365 if (skb->ip_summed == CHECKSUM_COMPLETE)
4366 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4367 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4368 }
4369 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4370 return 0;
4371}
4372EXPORT_SYMBOL(skb_vlan_push);
4373
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004374/**
4375 * alloc_skb_with_frags - allocate skb with page frags
4376 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004377 * @header_len: size of linear part
4378 * @data_len: needed length in frags
4379 * @max_page_order: max page order desired.
4380 * @errcode: pointer to error code if any
4381 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004382 *
4383 * This can be used to allocate a paged skb, given a maximal order for frags.
4384 */
4385struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4386 unsigned long data_len,
4387 int max_page_order,
4388 int *errcode,
4389 gfp_t gfp_mask)
4390{
4391 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4392 unsigned long chunk;
4393 struct sk_buff *skb;
4394 struct page *page;
4395 gfp_t gfp_head;
4396 int i;
4397
4398 *errcode = -EMSGSIZE;
4399 /* Note this test could be relaxed, if we succeed to allocate
4400 * high order pages...
4401 */
4402 if (npages > MAX_SKB_FRAGS)
4403 return NULL;
4404
4405 gfp_head = gfp_mask;
4406 if (gfp_head & __GFP_WAIT)
4407 gfp_head |= __GFP_REPEAT;
4408
4409 *errcode = -ENOBUFS;
4410 skb = alloc_skb(header_len, gfp_head);
4411 if (!skb)
4412 return NULL;
4413
4414 skb->truesize += npages << PAGE_SHIFT;
4415
4416 for (i = 0; npages > 0; i++) {
4417 int order = max_page_order;
4418
4419 while (order) {
4420 if (npages >= 1 << order) {
4421 page = alloc_pages(gfp_mask |
4422 __GFP_COMP |
4423 __GFP_NOWARN |
4424 __GFP_NORETRY,
4425 order);
4426 if (page)
4427 goto fill_page;
4428 /* Do not retry other high order allocations */
4429 order = 1;
4430 max_page_order = 0;
4431 }
4432 order--;
4433 }
4434 page = alloc_page(gfp_mask);
4435 if (!page)
4436 goto failure;
4437fill_page:
4438 chunk = min_t(unsigned long, data_len,
4439 PAGE_SIZE << order);
4440 skb_fill_page_desc(skb, i, page, 0, chunk);
4441 data_len -= chunk;
4442 npages -= 1 << order;
4443 }
4444 return skb;
4445
4446failure:
4447 kfree_skb(skb);
4448 return NULL;
4449}
4450EXPORT_SYMBOL(alloc_skb_with_frags);