blob: 3cfff2a3d651fb7d7cd2baaa3698c123eb7fc00f [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 Dumazet2ea2f622015-04-24 16:05:01 -0700283 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700285 * @frag_size: size of data, 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
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
300 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700301struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000302{
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000305 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
310
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 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}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700331
332/* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
336 */
337struct sk_buff *build_skb(void *data, unsigned int frag_size)
338{
339 struct sk_buff *skb = __build_skb(data, frag_size);
340
341 if (skb && frag_size) {
342 skb->head_frag = 1;
343 if (virt_to_head_page(data)->pfmemalloc)
344 skb->pfmemalloc = 1;
345 }
346 return skb;
347}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000348EXPORT_SYMBOL(build_skb);
349
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000350struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351 struct page_frag frag;
352 /* we maintain a pagecount bias, so that we dont dirty cache line
353 * containing page->_count every time we allocate a fragment.
354 */
355 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000356};
357static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800358static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000359
Alexander Duyckffde7322014-12-09 19:40:42 -0800360static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
361 gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000362{
Alexander Duyckffde7322014-12-09 19:40:42 -0800363 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
364 struct page *page = NULL;
365 gfp_t gfp = gfp_mask;
Eric Dumazet6f532612012-05-18 05:12:12 +0000366
Alexander Duyckffde7322014-12-09 19:40:42 -0800367 if (order) {
Eric Dumazet79930f52015-04-22 07:33:36 -0700368 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
369 __GFP_NOMEMALLOC;
Alexander Duyckffde7322014-12-09 19:40:42 -0800370 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
371 nc->frag.size = PAGE_SIZE << (page ? order : 0);
372 }
373
374 if (unlikely(!page))
375 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
376
377 nc->frag.page = page;
378
379 return page;
380}
381
382static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
383 unsigned int fragsz, gfp_t gfp_mask)
384{
385 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
386 struct page *page = nc->frag.page;
387 unsigned int size;
388 int offset;
389
390 if (unlikely(!page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000391refill:
Alexander Duyckffde7322014-12-09 19:40:42 -0800392 page = __page_frag_refill(nc, gfp_mask);
393 if (!page)
394 return NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000395
Alexander Duyckffde7322014-12-09 19:40:42 -0800396 /* 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
Eric Dumazet4c450582014-10-10 04:48:18 -0700399 /* Even if we own the page, we do not use atomic_set().
400 * This would break get_page_unless_zero() users.
401 */
Alexander Duyckffde7322014-12-09 19:40:42 -0800402 atomic_add(size - 1, &page->_count);
403
404 /* reset page count bias and offset to start of new frag */
405 nc->pagecnt_bias = size;
406 nc->frag.offset = size;
Eric Dumazet6f532612012-05-18 05:12:12 +0000407 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000408
Alexander Duyckffde7322014-12-09 19:40:42 -0800409 offset = nc->frag.offset - fragsz;
410 if (unlikely(offset < 0)) {
411 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
412 goto refill;
413
414 /* if size can vary use frag.size else just use PAGE_SIZE */
415 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
416
417 /* OK, page count is 0, we can safely set it */
418 atomic_set(&page->_count, size);
419
420 /* reset page count bias and offset to start of new frag */
421 nc->pagecnt_bias = size;
422 offset = size - fragsz;
Eric Dumazet6f532612012-05-18 05:12:12 +0000423 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000424
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000425 nc->pagecnt_bias--;
Alexander Duyckffde7322014-12-09 19:40:42 -0800426 nc->frag.offset = offset;
427
428 return page_address(page) + offset;
429}
430
431static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
432{
433 unsigned long flags;
434 void *data;
435
436 local_irq_save(flags);
437 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000438 local_irq_restore(flags);
439 return data;
440}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700441
442/**
443 * netdev_alloc_frag - allocate a page fragment
444 * @fragsz: fragment size
445 *
446 * Allocates a frag from a page for receive buffer.
447 * Uses GFP_ATOMIC allocations.
448 */
449void *netdev_alloc_frag(unsigned int fragsz)
450{
451 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
452}
Eric Dumazet6f532612012-05-18 05:12:12 +0000453EXPORT_SYMBOL(netdev_alloc_frag);
454
Alexander Duyckffde7322014-12-09 19:40:42 -0800455static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
456{
457 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
458}
459
460void *napi_alloc_frag(unsigned int fragsz)
461{
462 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
463}
464EXPORT_SYMBOL(napi_alloc_frag);
465
Eric Dumazet6f532612012-05-18 05:12:12 +0000466/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467 * __alloc_rx_skb - allocate an skbuff for rx
Christoph Hellwig8af27452006-07-31 22:35:23 -0700468 * @length: length to allocate
469 * @gfp_mask: get_free_pages mask, passed to alloc_skb
Alexander Duyckfd11a832014-12-09 19:40:49 -0800470 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
471 * allocations in case we have to fallback to __alloc_skb()
472 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
473 * from napi_cache instead of netdev_cache.
Christoph Hellwig8af27452006-07-31 22:35:23 -0700474 *
475 * Allocate a new &sk_buff and assign it a usage count of one. The
476 * buffer has unspecified headroom built in. Users should allocate
477 * the headroom they think they need without accounting for the
478 * built in space. The built in space is used for optimisations.
479 *
480 * %NULL is returned if there is no free memory.
481 */
Alexander Duyckfd11a832014-12-09 19:40:49 -0800482static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
483 int flags)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700484{
Eric Dumazet6f532612012-05-18 05:12:12 +0000485 struct sk_buff *skb = NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800486 unsigned int fragsz = SKB_DATA_ALIGN(length) +
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000487 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700488
Eric Dumazet310e1582012-07-16 13:15:52 +0200489 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700490 void *data;
491
492 if (sk_memalloc_socks())
493 gfp_mask |= __GFP_MEMALLOC;
494
Alexander Duyckfd11a832014-12-09 19:40:49 -0800495 data = (flags & SKB_ALLOC_NAPI) ?
496 __napi_alloc_frag(fragsz, gfp_mask) :
497 __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000498
Eric Dumazet6f532612012-05-18 05:12:12 +0000499 if (likely(data)) {
500 skb = build_skb(data, fragsz);
501 if (unlikely(!skb))
502 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000503 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000504 } else {
Alexander Duyckfd11a832014-12-09 19:40:49 -0800505 skb = __alloc_skb(length, gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700506 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000507 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800508 return skb;
509}
510
511/**
512 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
513 * @dev: network device to receive on
514 * @length: length to allocate
515 * @gfp_mask: get_free_pages mask, passed to alloc_skb
516 *
517 * Allocate a new &sk_buff and assign it a usage count of one. The
518 * buffer has NET_SKB_PAD headroom built in. Users should allocate
519 * the headroom they think they need without accounting for the
520 * built in space. The built in space is used for optimisations.
521 *
522 * %NULL is returned if there is no free memory.
523 */
524struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
525 unsigned int length, gfp_t gfp_mask)
526{
527 struct sk_buff *skb;
528
529 length += NET_SKB_PAD;
530 skb = __alloc_rx_skb(length, gfp_mask, 0);
531
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700532 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700533 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700534 skb->dev = dev;
535 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800536
Christoph Hellwig8af27452006-07-31 22:35:23 -0700537 return skb;
538}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800539EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Alexander Duyckfd11a832014-12-09 19:40:49 -0800541/**
542 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
543 * @napi: napi instance this buffer was allocated for
544 * @length: length to allocate
545 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
546 *
547 * Allocate a new sk_buff for use in NAPI receive. This buffer will
548 * attempt to allocate the head from a special reserved region used
549 * only for NAPI Rx allocation. By doing this we can save several
550 * CPU cycles by avoiding having to disable and re-enable IRQs.
551 *
552 * %NULL is returned if there is no free memory.
553 */
554struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
555 unsigned int length, gfp_t gfp_mask)
556{
557 struct sk_buff *skb;
558
559 length += NET_SKB_PAD + NET_IP_ALIGN;
560 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
561
562 if (likely(skb)) {
563 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
564 skb->dev = napi->dev;
565 }
566
567 return skb;
568}
569EXPORT_SYMBOL(__napi_alloc_skb);
570
Peter Zijlstra654bed12008-10-07 14:22:33 -0700571void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000572 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700573{
574 skb_fill_page_desc(skb, i, page, off, size);
575 skb->len += size;
576 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000577 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700578}
579EXPORT_SYMBOL(skb_add_rx_frag);
580
Jason Wangf8e617e2013-11-01 14:07:47 +0800581void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
582 unsigned int truesize)
583{
584 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
585
586 skb_frag_size_add(frag, size);
587 skb->len += size;
588 skb->data_len += size;
589 skb->truesize += truesize;
590}
591EXPORT_SYMBOL(skb_coalesce_rx_frag);
592
Herbert Xu27b437c2006-07-13 19:26:39 -0700593static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700595 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700596 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Herbert Xu27b437c2006-07-13 19:26:39 -0700599static inline void skb_drop_fraglist(struct sk_buff *skb)
600{
601 skb_drop_list(&skb_shinfo(skb)->frag_list);
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static void skb_clone_fraglist(struct sk_buff *skb)
605{
606 struct sk_buff *list;
607
David S. Millerfbb398a2009-06-09 00:18:59 -0700608 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 skb_get(list);
610}
611
Eric Dumazetd3836f22012-04-27 00:33:38 +0000612static void skb_free_head(struct sk_buff *skb)
613{
614 if (skb->head_frag)
615 put_page(virt_to_head_page(skb->head));
616 else
617 kfree(skb->head);
618}
619
Adrian Bunk5bba1712006-06-29 13:02:35 -0700620static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Eric Dumazetff04a772014-09-23 18:39:30 -0700622 struct skb_shared_info *shinfo = skb_shinfo(skb);
623 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Eric Dumazetff04a772014-09-23 18:39:30 -0700625 if (skb->cloned &&
626 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
627 &shinfo->dataref))
628 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000629
Eric Dumazetff04a772014-09-23 18:39:30 -0700630 for (i = 0; i < shinfo->nr_frags; i++)
631 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000632
Eric Dumazetff04a772014-09-23 18:39:30 -0700633 /*
634 * If skb buf is from userspace, we need to notify the caller
635 * the lower device DMA has done;
636 */
637 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
638 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Eric Dumazetff04a772014-09-23 18:39:30 -0700640 uarg = shinfo->destructor_arg;
641 if (uarg->callback)
642 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700644
645 if (shinfo->frag_list)
646 kfree_skb_list(shinfo->frag_list);
647
648 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Free an skbuff by memory without cleaning the state.
653 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800654static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700656 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700657
David S. Millerd179cd12005-08-17 14:57:30 -0700658 switch (skb->fclone) {
659 case SKB_FCLONE_UNAVAILABLE:
660 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800661 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700662
663 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700664 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800665
666 /* We usually free the clone (TX completion) before original skb
667 * This test would have no chance to be true for the clone,
668 * while here, branch prediction will be good.
669 */
670 if (atomic_read(&fclones->fclone_ref) == 1)
671 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700672 break;
673
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800674 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700675 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700676 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700677 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800678 if (!atomic_dec_and_test(&fclones->fclone_ref))
679 return;
680fastpath:
681 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700684static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Eric Dumazetadf30902009-06-02 05:19:30 +0000686 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687#ifdef CONFIG_XFRM
688 secpath_put(skb->sp);
689#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700690 if (skb->destructor) {
691 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 skb->destructor(skb);
693 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000694#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700695 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100696#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200697#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 nf_bridge_put(skb->nf_bridge);
699#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700700}
701
702/* Free everything but the sk_buff shell. */
703static void skb_release_all(struct sk_buff *skb)
704{
705 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000706 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000707 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Herbert Xu2d4baff2007-11-26 23:11:19 +0800710/**
711 * __kfree_skb - private function
712 * @skb: buffer
713 *
714 * Free an sk_buff. Release anything attached to the buffer.
715 * Clean the state. This is an internal helper function. Users should
716 * always call kfree_skb
717 */
718
719void __kfree_skb(struct sk_buff *skb)
720{
721 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 kfree_skbmem(skb);
723}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800724EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800727 * kfree_skb - free an sk_buff
728 * @skb: buffer to free
729 *
730 * Drop a reference to the buffer and free it if the usage count has
731 * hit zero.
732 */
733void kfree_skb(struct sk_buff *skb)
734{
735 if (unlikely(!skb))
736 return;
737 if (likely(atomic_read(&skb->users) == 1))
738 smp_rmb();
739 else if (likely(!atomic_dec_and_test(&skb->users)))
740 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000741 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800742 __kfree_skb(skb);
743}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800744EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800745
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700746void kfree_skb_list(struct sk_buff *segs)
747{
748 while (segs) {
749 struct sk_buff *next = segs->next;
750
751 kfree_skb(segs);
752 segs = next;
753 }
754}
755EXPORT_SYMBOL(kfree_skb_list);
756
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700757/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000758 * skb_tx_error - report an sk_buff xmit error
759 * @skb: buffer that triggered an error
760 *
761 * Report xmit error if a device callback is tracking this skb.
762 * skb must be freed afterwards.
763 */
764void skb_tx_error(struct sk_buff *skb)
765{
766 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
767 struct ubuf_info *uarg;
768
769 uarg = skb_shinfo(skb)->destructor_arg;
770 if (uarg->callback)
771 uarg->callback(uarg, false);
772 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
773 }
774}
775EXPORT_SYMBOL(skb_tx_error);
776
777/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000778 * consume_skb - free an skbuff
779 * @skb: buffer to free
780 *
781 * Drop a ref to the buffer and free it if the usage count has hit zero
782 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
783 * is being dropped after a failure and notes that
784 */
785void consume_skb(struct sk_buff *skb)
786{
787 if (unlikely(!skb))
788 return;
789 if (likely(atomic_read(&skb->users) == 1))
790 smp_rmb();
791 else if (likely(!atomic_dec_and_test(&skb->users)))
792 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900793 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000794 __kfree_skb(skb);
795}
796EXPORT_SYMBOL(consume_skb);
797
Eric Dumazetb1937222014-09-28 22:18:47 -0700798/* Make sure a field is enclosed inside headers_start/headers_end section */
799#define CHECK_SKB_FIELD(field) \
800 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
801 offsetof(struct sk_buff, headers_start)); \
802 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
803 offsetof(struct sk_buff, headers_end)); \
804
Herbert Xudec18812007-10-14 00:37:30 -0700805static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
806{
807 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700808 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700809 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700810 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000811 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700812#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700813 new->sp = secpath_get(old->sp);
814#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700815 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700816
Eric Dumazetb1937222014-09-28 22:18:47 -0700817 /* Note : this field could be in headers_start/headers_end section
818 * It is not yet because we do not want to have a 16 bit hole
819 */
820 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300821
Eric Dumazetb1937222014-09-28 22:18:47 -0700822 memcpy(&new->headers_start, &old->headers_start,
823 offsetof(struct sk_buff, headers_end) -
824 offsetof(struct sk_buff, headers_start));
825 CHECK_SKB_FIELD(protocol);
826 CHECK_SKB_FIELD(csum);
827 CHECK_SKB_FIELD(hash);
828 CHECK_SKB_FIELD(priority);
829 CHECK_SKB_FIELD(skb_iif);
830 CHECK_SKB_FIELD(vlan_proto);
831 CHECK_SKB_FIELD(vlan_tci);
832 CHECK_SKB_FIELD(transport_header);
833 CHECK_SKB_FIELD(network_header);
834 CHECK_SKB_FIELD(mac_header);
835 CHECK_SKB_FIELD(inner_protocol);
836 CHECK_SKB_FIELD(inner_transport_header);
837 CHECK_SKB_FIELD(inner_network_header);
838 CHECK_SKB_FIELD(inner_mac_header);
839 CHECK_SKB_FIELD(mark);
840#ifdef CONFIG_NETWORK_SECMARK
841 CHECK_SKB_FIELD(secmark);
842#endif
Cong Wange0d10952013-08-01 11:10:25 +0800843#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700844 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300845#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800846#ifdef CONFIG_XPS
847 CHECK_SKB_FIELD(sender_cpu);
848#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700849#ifdef CONFIG_NET_SCHED
850 CHECK_SKB_FIELD(tc_index);
851#ifdef CONFIG_NET_CLS_ACT
852 CHECK_SKB_FIELD(tc_verd);
853#endif
854#endif
855
Herbert Xudec18812007-10-14 00:37:30 -0700856}
857
Herbert Xu82c49a32009-05-22 22:11:37 +0000858/*
859 * You should not add any new code to this function. Add it to
860 * __copy_skb_header above instead.
861 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700862static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#define C(x) n->x = skb->x
865
866 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700868 __copy_skb_header(n, skb);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 C(len);
871 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700872 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700873 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800874 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 C(tail);
878 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800879 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000880 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800881 C(data);
882 C(truesize);
883 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 atomic_inc(&(skb_shinfo(skb)->dataref));
886 skb->cloned = 1;
887
888 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700889#undef C
890}
891
892/**
893 * skb_morph - morph one skb into another
894 * @dst: the skb to receive the contents
895 * @src: the skb to supply the contents
896 *
897 * This is identical to skb_clone except that the target skb is
898 * supplied by the user.
899 *
900 * The target skb is returned upon exit.
901 */
902struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
903{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800904 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700905 return __skb_clone(dst, src);
906}
907EXPORT_SYMBOL_GPL(skb_morph);
908
Ben Hutchings2c530402012-07-10 10:55:09 +0000909/**
910 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000911 * @skb: the skb to modify
912 * @gfp_mask: allocation priority
913 *
914 * This must be called on SKBTX_DEV_ZEROCOPY skb.
915 * It will copy all frags into kernel and drop the reference
916 * to userspace pages.
917 *
918 * If this function is called from an interrupt gfp_mask() must be
919 * %GFP_ATOMIC.
920 *
921 * Returns 0 on success or a negative error code on failure
922 * to allocate kernel memory to copy to.
923 */
924int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000925{
926 int i;
927 int num_frags = skb_shinfo(skb)->nr_frags;
928 struct page *page, *head = NULL;
929 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
930
931 for (i = 0; i < num_frags; i++) {
932 u8 *vaddr;
933 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
934
Krishna Kumar02756ed2012-07-17 02:05:29 +0000935 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000936 if (!page) {
937 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900938 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000939 put_page(head);
940 head = next;
941 }
942 return -ENOMEM;
943 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200944 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000945 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000946 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200947 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900948 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000949 head = page;
950 }
951
952 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000953 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000954 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000955
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000956 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000957
958 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000959 for (i = num_frags - 1; i >= 0; i--) {
960 __skb_fill_page_desc(skb, i, head, 0,
961 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900962 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000963 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000964
965 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000966 return 0;
967}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000968EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000969
Herbert Xue0053ec2007-10-14 00:37:52 -0700970/**
971 * skb_clone - duplicate an sk_buff
972 * @skb: buffer to clone
973 * @gfp_mask: allocation priority
974 *
975 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
976 * copies share the same packet data but not structure. The new
977 * buffer has a reference count of 1. If the allocation fails the
978 * function returns %NULL otherwise the new buffer is returned.
979 *
980 * If this function is called from an interrupt gfp_mask() must be
981 * %GFP_ATOMIC.
982 */
983
984struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
985{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700986 struct sk_buff_fclones *fclones = container_of(skb,
987 struct sk_buff_fclones,
988 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800989 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700990
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000991 if (skb_orphan_frags(skb, gfp_mask))
992 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000993
Herbert Xue0053ec2007-10-14 00:37:52 -0700994 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800995 atomic_read(&fclones->fclone_ref) == 1) {
996 n = &fclones->skb2;
997 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700998 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700999 if (skb_pfmemalloc(skb))
1000 gfp_mask |= __GFP_MEMALLOC;
1001
Herbert Xue0053ec2007-10-14 00:37:52 -07001002 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1003 if (!n)
1004 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001005
1006 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001007 n->fclone = SKB_FCLONE_UNAVAILABLE;
1008 }
1009
1010 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001012EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001014static void skb_headers_offset_update(struct sk_buff *skb, int off)
1015{
Eric Dumazet030737b2013-10-19 11:42:54 -07001016 /* Only adjust this if it actually is csum_start rather than csum */
1017 if (skb->ip_summed == CHECKSUM_PARTIAL)
1018 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001019 /* {transport,network,mac}_header and tail are relative to skb->head */
1020 skb->transport_header += off;
1021 skb->network_header += off;
1022 if (skb_mac_header_was_set(skb))
1023 skb->mac_header += off;
1024 skb->inner_transport_header += off;
1025 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001026 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1030{
Herbert Xudec18812007-10-14 00:37:30 -07001031 __copy_skb_header(new, old);
1032
Herbert Xu79671682006-06-22 02:40:14 -07001033 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1034 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1035 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Mel Gormanc93bdd02012-07-31 16:44:19 -07001038static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1039{
1040 if (skb_pfmemalloc(skb))
1041 return SKB_ALLOC_RX;
1042 return 0;
1043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/**
1046 * skb_copy - create private copy of an sk_buff
1047 * @skb: buffer to copy
1048 * @gfp_mask: allocation priority
1049 *
1050 * Make a copy of both an &sk_buff and its data. This is used when the
1051 * caller wishes to modify the data and needs a private copy of the
1052 * data to alter. Returns %NULL on failure or the pointer to the buffer
1053 * on success. The returned buffer has a reference count of 1.
1054 *
1055 * As by-product this function converts non-linear &sk_buff to linear
1056 * one, so that &sk_buff becomes completely private and caller is allowed
1057 * to modify all the data of returned buffer. This means that this
1058 * function is not recommended for use in circumstances when only
1059 * header is going to be modified. Use pskb_copy() instead.
1060 */
1061
Al Virodd0fc662005-10-07 07:46:04 +01001062struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001064 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001065 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001066 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1067 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!n)
1070 return NULL;
1071
1072 /* Set the data pointer */
1073 skb_reserve(n, headerlen);
1074 /* Set the tail pointer and length */
1075 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1078 BUG();
1079
1080 copy_skb_header(n, skb);
1081 return n;
1082}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001083EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001086 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001088 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001090 * @fclone: if true allocate the copy of the skb from the fclone
1091 * cache instead of the head cache; it is recommended to set this
1092 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
1094 * Make a copy of both an &sk_buff and part of its data, located
1095 * in header. Fragmented data remain shared. This is used when
1096 * the caller wishes to modify only header of &sk_buff and needs
1097 * private copy of the header to alter. Returns %NULL on failure
1098 * or the pointer to the buffer on success.
1099 * The returned buffer has a reference count of 1.
1100 */
1101
Octavian Purdilabad93e92014-06-12 01:36:26 +03001102struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1103 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Eric Dumazet117632e2011-12-03 21:39:53 +00001105 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001106 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1107 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!n)
1110 goto out;
1111
1112 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001113 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /* Set the tail pointer and length */
1115 skb_put(n, skb_headlen(skb));
1116 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001117 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Herbert Xu25f484a2006-11-07 14:57:15 -08001119 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 n->data_len = skb->data_len;
1121 n->len = skb->len;
1122
1123 if (skb_shinfo(skb)->nr_frags) {
1124 int i;
1125
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001126 if (skb_orphan_frags(skb, gfp_mask)) {
1127 kfree_skb(n);
1128 n = NULL;
1129 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1132 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001133 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 skb_shinfo(n)->nr_frags = i;
1136 }
1137
David S. Miller21dc3302010-08-23 00:13:46 -07001138 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1140 skb_clone_fraglist(n);
1141 }
1142
1143 copy_skb_header(n, skb);
1144out:
1145 return n;
1146}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001147EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149/**
1150 * pskb_expand_head - reallocate header of &sk_buff
1151 * @skb: buffer to reallocate
1152 * @nhead: room to add at head
1153 * @ntail: room to add at tail
1154 * @gfp_mask: allocation priority
1155 *
Mathias Krausebc323832013-11-07 14:18:26 +01001156 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1157 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * reference count of 1. Returns zero in the case of success or error,
1159 * if expansion failed. In the last case, &sk_buff is not changed.
1160 *
1161 * All the pointers pointing into skb header may change and must be
1162 * reloaded after call to this function.
1163 */
1164
Victor Fusco86a76ca2005-07-08 14:57:47 -07001165int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001166 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 int i;
1169 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001170 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 long off;
1172
Herbert Xu4edd87a2008-10-01 07:09:38 -07001173 BUG_ON(nhead < 0);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (skb_shared(skb))
1176 BUG();
1177
1178 size = SKB_DATA_ALIGN(size);
1179
Mel Gormanc93bdd02012-07-31 16:44:19 -07001180 if (skb_pfmemalloc(skb))
1181 gfp_mask |= __GFP_MEMALLOC;
1182 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1183 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!data)
1185 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001186 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001189 * optimized for the cases when header is void.
1190 */
1191 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1192
1193 memcpy((struct skb_shared_info *)(data + size),
1194 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001195 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Alexander Duyck3e245912012-05-04 14:26:51 +00001197 /*
1198 * if shinfo is shared we must drop the old head gracefully, but if it
1199 * is not we can just drop the old head and let the existing refcount
1200 * be since all we did is relocate the values
1201 */
1202 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001203 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001204 if (skb_orphan_frags(skb, gfp_mask))
1205 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001206 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001207 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Eric Dumazet1fd63042010-09-02 23:09:32 +00001209 if (skb_has_frag_list(skb))
1210 skb_clone_fraglist(skb);
1211
1212 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001213 } else {
1214 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 off = (data + nhead) - skb->head;
1217
1218 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001219 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001221#ifdef NET_SKBUFF_DATA_USES_OFFSET
1222 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001223 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001224#else
1225 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001226#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001227 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001228 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001230 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 skb->nohdr = 0;
1232 atomic_set(&skb_shinfo(skb)->dataref, 1);
1233 return 0;
1234
Shirley Maa6686f22011-07-06 12:22:12 +00001235nofrags:
1236 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237nodata:
1238 return -ENOMEM;
1239}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001240EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242/* Make private copy of skb with writable head and some headroom */
1243
1244struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1245{
1246 struct sk_buff *skb2;
1247 int delta = headroom - skb_headroom(skb);
1248
1249 if (delta <= 0)
1250 skb2 = pskb_copy(skb, GFP_ATOMIC);
1251 else {
1252 skb2 = skb_clone(skb, GFP_ATOMIC);
1253 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1254 GFP_ATOMIC)) {
1255 kfree_skb(skb2);
1256 skb2 = NULL;
1257 }
1258 }
1259 return skb2;
1260}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001261EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263/**
1264 * skb_copy_expand - copy and expand sk_buff
1265 * @skb: buffer to copy
1266 * @newheadroom: new free bytes at head
1267 * @newtailroom: new free bytes at tail
1268 * @gfp_mask: allocation priority
1269 *
1270 * Make a copy of both an &sk_buff and its data and while doing so
1271 * allocate additional space.
1272 *
1273 * This is used when the caller wishes to modify the data and needs a
1274 * private copy of the data to alter as well as more space for new fields.
1275 * Returns %NULL on failure or the pointer to the buffer
1276 * on success. The returned buffer has a reference count of 1.
1277 *
1278 * You must pass %GFP_ATOMIC as the allocation priority if this function
1279 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
1281struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001282 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001283 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 /*
1286 * Allocate the copy buffer
1287 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001288 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1289 gfp_mask, skb_alloc_rx_flag(skb),
1290 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001291 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int head_copy_len, head_copy_off;
1293
1294 if (!n)
1295 return NULL;
1296
1297 skb_reserve(n, newheadroom);
1298
1299 /* Set the tail pointer and length */
1300 skb_put(n, skb->len);
1301
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001302 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 head_copy_off = 0;
1304 if (newheadroom <= head_copy_len)
1305 head_copy_len = newheadroom;
1306 else
1307 head_copy_off = newheadroom - head_copy_len;
1308
1309 /* Copy the linear header and data. */
1310 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1311 skb->len + head_copy_len))
1312 BUG();
1313
1314 copy_skb_header(n, skb);
1315
Eric Dumazet030737b2013-10-19 11:42:54 -07001316 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return n;
1319}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001320EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322/**
1323 * skb_pad - zero pad the tail of an skb
1324 * @skb: buffer to pad
1325 * @pad: space to pad
1326 *
1327 * Ensure that a buffer is followed by a padding area that is zero
1328 * filled. Used by network drivers which may DMA or transfer data
1329 * beyond the buffer end onto the wire.
1330 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001331 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001333
Herbert Xu5b057c62006-06-23 02:06:41 -07001334int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Herbert Xu5b057c62006-06-23 02:06:41 -07001336 int err;
1337 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001340 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001344
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001345 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001346 if (likely(skb_cloned(skb) || ntail > 0)) {
1347 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1348 if (unlikely(err))
1349 goto free_skb;
1350 }
1351
1352 /* FIXME: The use of this function with non-linear skb's really needs
1353 * to be audited.
1354 */
1355 err = skb_linearize(skb);
1356 if (unlikely(err))
1357 goto free_skb;
1358
1359 memset(skb->data + skb->len, 0, pad);
1360 return 0;
1361
1362free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001364 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001365}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001366EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001367
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001368/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001369 * pskb_put - add data to the tail of a potentially fragmented buffer
1370 * @skb: start of the buffer to use
1371 * @tail: tail fragment of the buffer to use
1372 * @len: amount of data to add
1373 *
1374 * This function extends the used data area of the potentially
1375 * fragmented buffer. @tail must be the last fragment of @skb -- or
1376 * @skb itself. If this would exceed the total buffer size the kernel
1377 * will panic. A pointer to the first byte of the extra data is
1378 * returned.
1379 */
1380
1381unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1382{
1383 if (tail != skb) {
1384 skb->data_len += len;
1385 skb->len += len;
1386 }
1387 return skb_put(tail, len);
1388}
1389EXPORT_SYMBOL_GPL(pskb_put);
1390
1391/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001392 * skb_put - add data to a buffer
1393 * @skb: buffer to use
1394 * @len: amount of data to add
1395 *
1396 * This function extends the used data area of the buffer. If this would
1397 * exceed the total buffer size the kernel will panic. A pointer to the
1398 * first byte of the extra data is returned.
1399 */
1400unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1401{
1402 unsigned char *tmp = skb_tail_pointer(skb);
1403 SKB_LINEAR_ASSERT(skb);
1404 skb->tail += len;
1405 skb->len += len;
1406 if (unlikely(skb->tail > skb->end))
1407 skb_over_panic(skb, len, __builtin_return_address(0));
1408 return tmp;
1409}
1410EXPORT_SYMBOL(skb_put);
1411
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001412/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001413 * skb_push - add data to the start of a buffer
1414 * @skb: buffer to use
1415 * @len: amount of data to add
1416 *
1417 * This function extends the used data area of the buffer at the buffer
1418 * start. If this would exceed the total buffer headroom the kernel will
1419 * panic. A pointer to the first byte of the extra data is returned.
1420 */
1421unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1422{
1423 skb->data -= len;
1424 skb->len += len;
1425 if (unlikely(skb->data<skb->head))
1426 skb_under_panic(skb, len, __builtin_return_address(0));
1427 return skb->data;
1428}
1429EXPORT_SYMBOL(skb_push);
1430
1431/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001432 * skb_pull - remove data from the start of a buffer
1433 * @skb: buffer to use
1434 * @len: amount of data to remove
1435 *
1436 * This function removes data from the start of a buffer, returning
1437 * the memory to the headroom. A pointer to the next data in the buffer
1438 * is returned. Once the data has been pulled future pushes will overwrite
1439 * the old data.
1440 */
1441unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1442{
David S. Miller47d29642010-05-02 02:21:44 -07001443 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001444}
1445EXPORT_SYMBOL(skb_pull);
1446
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001447/**
1448 * skb_trim - remove end from a buffer
1449 * @skb: buffer to alter
1450 * @len: new length
1451 *
1452 * Cut the length of a buffer down by removing data from the tail. If
1453 * the buffer is already under the length specified it is not modified.
1454 * The skb must be linear.
1455 */
1456void skb_trim(struct sk_buff *skb, unsigned int len)
1457{
1458 if (skb->len > len)
1459 __skb_trim(skb, len);
1460}
1461EXPORT_SYMBOL(skb_trim);
1462
Herbert Xu3cc0e872006-06-09 16:13:38 -07001463/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465
Herbert Xu3cc0e872006-06-09 16:13:38 -07001466int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Herbert Xu27b437c2006-07-13 19:26:39 -07001468 struct sk_buff **fragp;
1469 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int offset = skb_headlen(skb);
1471 int nfrags = skb_shinfo(skb)->nr_frags;
1472 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001473 int err;
1474
1475 if (skb_cloned(skb) &&
1476 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001479 i = 0;
1480 if (offset >= len)
1481 goto drop_pages;
1482
1483 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001484 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001485
1486 if (end < len) {
1487 offset = end;
1488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001490
Eric Dumazet9e903e02011-10-18 21:00:24 +00001491 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001492
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001493drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001494 skb_shinfo(skb)->nr_frags = i;
1495
1496 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001497 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001498
David S. Miller21dc3302010-08-23 00:13:46 -07001499 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001500 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001501 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
Herbert Xu27b437c2006-07-13 19:26:39 -07001504 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1505 fragp = &frag->next) {
1506 int end = offset + frag->len;
1507
1508 if (skb_shared(frag)) {
1509 struct sk_buff *nfrag;
1510
1511 nfrag = skb_clone(frag, GFP_ATOMIC);
1512 if (unlikely(!nfrag))
1513 return -ENOMEM;
1514
1515 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001516 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001517 frag = nfrag;
1518 *fragp = frag;
1519 }
1520
1521 if (end < len) {
1522 offset = end;
1523 continue;
1524 }
1525
1526 if (end > len &&
1527 unlikely((err = pskb_trim(frag, len - offset))))
1528 return err;
1529
1530 if (frag->next)
1531 skb_drop_list(&frag->next);
1532 break;
1533 }
1534
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001535done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001536 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 skb->data_len -= skb->len - len;
1538 skb->len = len;
1539 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001540 skb->len = len;
1541 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001542 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
1545 return 0;
1546}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001547EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549/**
1550 * __pskb_pull_tail - advance tail of skb header
1551 * @skb: buffer to reallocate
1552 * @delta: number of bytes to advance tail
1553 *
1554 * The function makes a sense only on a fragmented &sk_buff,
1555 * it expands header moving its tail forward and copying necessary
1556 * data from fragmented part.
1557 *
1558 * &sk_buff MUST have reference count of 1.
1559 *
1560 * Returns %NULL (and &sk_buff does not change) if pull failed
1561 * or value of new tail of skb in the case of success.
1562 *
1563 * All the pointers pointing into skb header may change and must be
1564 * reloaded after call to this function.
1565 */
1566
1567/* Moves tail of skb head forward, copying data from fragmented part,
1568 * when it is necessary.
1569 * 1. It may fail due to malloc failure.
1570 * 2. It may change skb pointers.
1571 *
1572 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1573 */
1574unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1575{
1576 /* If skb has not enough free space at tail, get new one
1577 * plus 128 bytes for future expansions. If we have enough
1578 * room at tail, reallocate without expansion only if skb is cloned.
1579 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001580 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (eat > 0 || skb_cloned(skb)) {
1583 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1584 GFP_ATOMIC))
1585 return NULL;
1586 }
1587
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001588 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 BUG();
1590
1591 /* Optimization: no fragments, no reasons to preestimate
1592 * size of pulled pages. Superb.
1593 */
David S. Miller21dc3302010-08-23 00:13:46 -07001594 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto pull_pages;
1596
1597 /* Estimate size of pulled pages. */
1598 eat = delta;
1599 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001600 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1601
1602 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001604 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 /* If we need update frag list, we are in troubles.
1608 * Certainly, it possible to add an offset to skb data,
1609 * but taking into account that pulling is expected to
1610 * be very rare operation, it is worth to fight against
1611 * further bloating skb head and crucify ourselves here instead.
1612 * Pure masohism, indeed. 8)8)
1613 */
1614 if (eat) {
1615 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1616 struct sk_buff *clone = NULL;
1617 struct sk_buff *insp = NULL;
1618
1619 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001620 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (list->len <= eat) {
1623 /* Eaten as whole. */
1624 eat -= list->len;
1625 list = list->next;
1626 insp = list;
1627 } else {
1628 /* Eaten partially. */
1629
1630 if (skb_shared(list)) {
1631 /* Sucks! We need to fork list. :-( */
1632 clone = skb_clone(list, GFP_ATOMIC);
1633 if (!clone)
1634 return NULL;
1635 insp = list->next;
1636 list = clone;
1637 } else {
1638 /* This may be pulled without
1639 * problems. */
1640 insp = list;
1641 }
1642 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001643 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return NULL;
1645 }
1646 break;
1647 }
1648 } while (eat);
1649
1650 /* Free pulled out fragments. */
1651 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1652 skb_shinfo(skb)->frag_list = list->next;
1653 kfree_skb(list);
1654 }
1655 /* And insert new clone at head. */
1656 if (clone) {
1657 clone->next = list;
1658 skb_shinfo(skb)->frag_list = clone;
1659 }
1660 }
1661 /* Success! Now we may commit changes to skb data. */
1662
1663pull_pages:
1664 eat = delta;
1665 k = 0;
1666 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001667 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1668
1669 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001670 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001671 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 } else {
1673 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1674 if (eat) {
1675 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001676 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 eat = 0;
1678 }
1679 k++;
1680 }
1681 }
1682 skb_shinfo(skb)->nr_frags = k;
1683
1684 skb->tail += delta;
1685 skb->data_len -= delta;
1686
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001687 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001689EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Eric Dumazet22019b12011-07-29 18:37:31 +00001691/**
1692 * skb_copy_bits - copy bits from skb to kernel buffer
1693 * @skb: source skb
1694 * @offset: offset in source
1695 * @to: destination buffer
1696 * @len: number of bytes to copy
1697 *
1698 * Copy the specified number of bytes from the source skb to the
1699 * destination buffer.
1700 *
1701 * CAUTION ! :
1702 * If its prototype is ever changed,
1703 * check arch/{*}/net/{*}.S files,
1704 * since it is called from BPF assembly code.
1705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1707{
David S. Miller1a028e52007-04-27 15:21:23 -07001708 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001709 struct sk_buff *frag_iter;
1710 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (offset > (int)skb->len - len)
1713 goto fault;
1714
1715 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001716 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (copy > len)
1718 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001719 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if ((len -= copy) == 0)
1721 return 0;
1722 offset += copy;
1723 to += copy;
1724 }
1725
1726 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001727 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001728 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001730 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001731
Eric Dumazet51c56b02012-04-05 11:35:15 +02001732 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if ((copy = end - offset) > 0) {
1734 u8 *vaddr;
1735
1736 if (copy > len)
1737 copy = len;
1738
Eric Dumazet51c56b02012-04-05 11:35:15 +02001739 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001741 vaddr + f->page_offset + offset - start,
1742 copy);
1743 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if ((len -= copy) == 0)
1746 return 0;
1747 offset += copy;
1748 to += copy;
1749 }
David S. Miller1a028e52007-04-27 15:21:23 -07001750 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
David S. Millerfbb398a2009-06-09 00:18:59 -07001753 skb_walk_frags(skb, frag_iter) {
1754 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
David S. Millerfbb398a2009-06-09 00:18:59 -07001756 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
David S. Millerfbb398a2009-06-09 00:18:59 -07001758 end = start + frag_iter->len;
1759 if ((copy = end - offset) > 0) {
1760 if (copy > len)
1761 copy = len;
1762 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1763 goto fault;
1764 if ((len -= copy) == 0)
1765 return 0;
1766 offset += copy;
1767 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001769 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Shirley Maa6686f22011-07-06 12:22:12 +00001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (!len)
1773 return 0;
1774
1775fault:
1776 return -EFAULT;
1777}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001778EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jens Axboe9c55e012007-11-06 23:30:13 -08001780/*
1781 * Callback from splice_to_pipe(), if we need to release some pages
1782 * at the end of the spd in case we error'ed out in filling the pipe.
1783 */
1784static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1785{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001786 put_page(spd->pages[i]);
1787}
Jens Axboe9c55e012007-11-06 23:30:13 -08001788
David S. Millera108d5f2012-04-23 23:06:11 -04001789static struct page *linear_to_page(struct page *page, unsigned int *len,
1790 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001791 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001792{
Eric Dumazet5640f762012-09-23 23:04:42 +00001793 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001794
Eric Dumazet5640f762012-09-23 23:04:42 +00001795 if (!sk_page_frag_refill(sk, pfrag))
1796 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001797
Eric Dumazet5640f762012-09-23 23:04:42 +00001798 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001799
Eric Dumazet5640f762012-09-23 23:04:42 +00001800 memcpy(page_address(pfrag->page) + pfrag->offset,
1801 page_address(page) + *offset, *len);
1802 *offset = pfrag->offset;
1803 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001804
Eric Dumazet5640f762012-09-23 23:04:42 +00001805 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001806}
1807
Eric Dumazet41c73a02012-04-22 12:26:16 +00001808static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1809 struct page *page,
1810 unsigned int offset)
1811{
1812 return spd->nr_pages &&
1813 spd->pages[spd->nr_pages - 1] == page &&
1814 (spd->partial[spd->nr_pages - 1].offset +
1815 spd->partial[spd->nr_pages - 1].len == offset);
1816}
1817
Jens Axboe9c55e012007-11-06 23:30:13 -08001818/*
1819 * Fill page/offset/length into spd, if it can hold more pages.
1820 */
David S. Millera108d5f2012-04-23 23:06:11 -04001821static bool spd_fill_page(struct splice_pipe_desc *spd,
1822 struct pipe_inode_info *pipe, struct page *page,
1823 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001824 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001825 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001826{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001827 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001828 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001829
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001830 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001831 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001832 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001833 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001834 }
1835 if (spd_can_coalesce(spd, page, offset)) {
1836 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001837 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001838 }
1839 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001840 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001841 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001842 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001843 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001844
David S. Millera108d5f2012-04-23 23:06:11 -04001845 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001846}
1847
David S. Millera108d5f2012-04-23 23:06:11 -04001848static bool __splice_segment(struct page *page, unsigned int poff,
1849 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001850 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001851 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001852 struct sock *sk,
1853 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001854{
1855 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001856 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001857
1858 /* skip this segment if already processed */
1859 if (*off >= plen) {
1860 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001861 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001862 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001863
Octavian Purdila2870c432008-07-15 00:49:11 -07001864 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001865 poff += *off;
1866 plen -= *off;
1867 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001868
Eric Dumazet18aafc62013-01-11 14:46:37 +00001869 do {
1870 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001871
Eric Dumazet18aafc62013-01-11 14:46:37 +00001872 if (spd_fill_page(spd, pipe, page, &flen, poff,
1873 linear, sk))
1874 return true;
1875 poff += flen;
1876 plen -= flen;
1877 *len -= flen;
1878 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001879
David S. Millera108d5f2012-04-23 23:06:11 -04001880 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001881}
1882
1883/*
David S. Millera108d5f2012-04-23 23:06:11 -04001884 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001885 * pipe is full or if we already spliced the requested length.
1886 */
David S. Millera108d5f2012-04-23 23:06:11 -04001887static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1888 unsigned int *offset, unsigned int *len,
1889 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001890{
1891 int seg;
1892
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001893 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001894 * If skb->head_frag is set, this 'linear' part is backed by a
1895 * fragment, and if the head is not shared with any clones then
1896 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001897 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001898 if (__splice_segment(virt_to_page(skb->data),
1899 (unsigned long) skb->data & (PAGE_SIZE - 1),
1900 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001901 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001902 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001903 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001904 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001905
1906 /*
1907 * then map the fragments
1908 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001909 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1910 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1911
Ian Campbellea2ab692011-08-22 23:44:58 +00001912 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001913 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001914 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001915 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001916 }
1917
David S. Millera108d5f2012-04-23 23:06:11 -04001918 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001919}
1920
1921/*
1922 * Map data from the skb to a pipe. Should handle both the linear part,
1923 * the fragments, and the frag list. It does NOT handle frag lists within
1924 * the frag list, if such a thing exists. We'd probably need to recurse to
1925 * handle that cleanly.
1926 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001927int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001928 struct pipe_inode_info *pipe, unsigned int tlen,
1929 unsigned int flags)
1930{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001931 struct partial_page partial[MAX_SKB_FRAGS];
1932 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001933 struct splice_pipe_desc spd = {
1934 .pages = pages,
1935 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001936 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001937 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001938 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 .spd_release = sock_spd_release,
1940 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001941 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001942 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001943 int ret = 0;
1944
Jens Axboe9c55e012007-11-06 23:30:13 -08001945 /*
1946 * __skb_splice_bits() only fails if the output has no room left,
1947 * so no point in going over the frag_list for the error case.
1948 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001949 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001950 goto done;
1951 else if (!tlen)
1952 goto done;
1953
1954 /*
1955 * now see if we have a frag_list to map
1956 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001957 skb_walk_frags(skb, frag_iter) {
1958 if (!tlen)
1959 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001960 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001961 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001962 }
1963
1964done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001965 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001966 /*
1967 * Drop the socket lock, otherwise we have reverse
1968 * locking dependencies between sk_lock and i_mutex
1969 * here as compared to sendfile(). We enter here
1970 * with the socket lock held, and splice_to_pipe() will
1971 * grab the pipe inode lock. For sendfile() emulation,
1972 * we call into ->sendpage() with the i_mutex lock held
1973 * and networking will grab the socket lock.
1974 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001975 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001976 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001977 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001978 }
1979
Jens Axboe35f3d142010-05-20 10:43:18 +02001980 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001981}
1982
Herbert Xu357b40a2005-04-19 22:30:14 -07001983/**
1984 * skb_store_bits - store bits from kernel buffer to skb
1985 * @skb: destination buffer
1986 * @offset: offset in destination
1987 * @from: source buffer
1988 * @len: number of bytes to copy
1989 *
1990 * Copy the specified number of bytes from the source buffer to the
1991 * destination skb. This function handles all the messy bits of
1992 * traversing fragment lists and such.
1993 */
1994
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001995int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001996{
David S. Miller1a028e52007-04-27 15:21:23 -07001997 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001998 struct sk_buff *frag_iter;
1999 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002000
2001 if (offset > (int)skb->len - len)
2002 goto fault;
2003
David S. Miller1a028e52007-04-27 15:21:23 -07002004 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002005 if (copy > len)
2006 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002007 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002008 if ((len -= copy) == 0)
2009 return 0;
2010 offset += copy;
2011 from += copy;
2012 }
2013
2014 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2015 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002016 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002017
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002018 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002019
Eric Dumazet9e903e02011-10-18 21:00:24 +00002020 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002021 if ((copy = end - offset) > 0) {
2022 u8 *vaddr;
2023
2024 if (copy > len)
2025 copy = len;
2026
Eric Dumazet51c56b02012-04-05 11:35:15 +02002027 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002028 memcpy(vaddr + frag->page_offset + offset - start,
2029 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002030 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002031
2032 if ((len -= copy) == 0)
2033 return 0;
2034 offset += copy;
2035 from += copy;
2036 }
David S. Miller1a028e52007-04-27 15:21:23 -07002037 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002038 }
2039
David S. Millerfbb398a2009-06-09 00:18:59 -07002040 skb_walk_frags(skb, frag_iter) {
2041 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002042
David S. Millerfbb398a2009-06-09 00:18:59 -07002043 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002044
David S. Millerfbb398a2009-06-09 00:18:59 -07002045 end = start + frag_iter->len;
2046 if ((copy = end - offset) > 0) {
2047 if (copy > len)
2048 copy = len;
2049 if (skb_store_bits(frag_iter, offset - start,
2050 from, copy))
2051 goto fault;
2052 if ((len -= copy) == 0)
2053 return 0;
2054 offset += copy;
2055 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002056 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002057 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002058 }
2059 if (!len)
2060 return 0;
2061
2062fault:
2063 return -EFAULT;
2064}
Herbert Xu357b40a2005-04-19 22:30:14 -07002065EXPORT_SYMBOL(skb_store_bits);
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002068__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2069 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
David S. Miller1a028e52007-04-27 15:21:23 -07002071 int start = skb_headlen(skb);
2072 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002073 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 int pos = 0;
2075
2076 /* Checksum header. */
2077 if (copy > 0) {
2078 if (copy > len)
2079 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002080 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if ((len -= copy) == 0)
2082 return csum;
2083 offset += copy;
2084 pos = copy;
2085 }
2086
2087 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002088 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002089 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002091 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002092
Eric Dumazet51c56b02012-04-05 11:35:15 +02002093 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002095 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (copy > len)
2099 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002100 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002101 csum2 = ops->update(vaddr + frag->page_offset +
2102 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002103 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002104 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (!(len -= copy))
2106 return csum;
2107 offset += copy;
2108 pos += copy;
2109 }
David S. Miller1a028e52007-04-27 15:21:23 -07002110 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
David S. Millerfbb398a2009-06-09 00:18:59 -07002113 skb_walk_frags(skb, frag_iter) {
2114 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
David S. Millerfbb398a2009-06-09 00:18:59 -07002116 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David S. Millerfbb398a2009-06-09 00:18:59 -07002118 end = start + frag_iter->len;
2119 if ((copy = end - offset) > 0) {
2120 __wsum csum2;
2121 if (copy > len)
2122 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002123 csum2 = __skb_checksum(frag_iter, offset - start,
2124 copy, 0, ops);
2125 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002126 if ((len -= copy) == 0)
2127 return csum;
2128 offset += copy;
2129 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002131 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002133 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 return csum;
2136}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002137EXPORT_SYMBOL(__skb_checksum);
2138
2139__wsum skb_checksum(const struct sk_buff *skb, int offset,
2140 int len, __wsum csum)
2141{
2142 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea82013-11-04 17:10:25 +01002143 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002144 .combine = csum_block_add_ext,
2145 };
2146
2147 return __skb_checksum(skb, offset, len, csum, &ops);
2148}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002149EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151/* Both of above in one bottle. */
2152
Al Viro81d77662006-11-14 21:37:33 -08002153__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2154 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
David S. Miller1a028e52007-04-27 15:21:23 -07002156 int start = skb_headlen(skb);
2157 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002158 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 int pos = 0;
2160
2161 /* Copy header. */
2162 if (copy > 0) {
2163 if (copy > len)
2164 copy = len;
2165 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2166 copy, csum);
2167 if ((len -= copy) == 0)
2168 return csum;
2169 offset += copy;
2170 to += copy;
2171 pos = copy;
2172 }
2173
2174 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002175 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002177 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002178
Eric Dumazet9e903e02011-10-18 21:00:24 +00002179 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002181 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 u8 *vaddr;
2183 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2184
2185 if (copy > len)
2186 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002187 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002189 frag->page_offset +
2190 offset - start, to,
2191 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002192 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 csum = csum_block_add(csum, csum2, pos);
2194 if (!(len -= copy))
2195 return csum;
2196 offset += copy;
2197 to += copy;
2198 pos += copy;
2199 }
David S. Miller1a028e52007-04-27 15:21:23 -07002200 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
David S. Millerfbb398a2009-06-09 00:18:59 -07002203 skb_walk_frags(skb, frag_iter) {
2204 __wsum csum2;
2205 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
David S. Millerfbb398a2009-06-09 00:18:59 -07002207 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
David S. Millerfbb398a2009-06-09 00:18:59 -07002209 end = start + frag_iter->len;
2210 if ((copy = end - offset) > 0) {
2211 if (copy > len)
2212 copy = len;
2213 csum2 = skb_copy_and_csum_bits(frag_iter,
2214 offset - start,
2215 to, copy, 0);
2216 csum = csum_block_add(csum, csum2, pos);
2217 if ((len -= copy) == 0)
2218 return csum;
2219 offset += copy;
2220 to += copy;
2221 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002223 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002225 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return csum;
2227}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002228EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Thomas Grafaf2806f2013-12-13 15:22:17 +01002230 /**
2231 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2232 * @from: source buffer
2233 *
2234 * Calculates the amount of linear headroom needed in the 'to' skb passed
2235 * into skb_zerocopy().
2236 */
2237unsigned int
2238skb_zerocopy_headlen(const struct sk_buff *from)
2239{
2240 unsigned int hlen = 0;
2241
2242 if (!from->head_frag ||
2243 skb_headlen(from) < L1_CACHE_BYTES ||
2244 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2245 hlen = skb_headlen(from);
2246
2247 if (skb_has_frag_list(from))
2248 hlen = from->len;
2249
2250 return hlen;
2251}
2252EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2253
2254/**
2255 * skb_zerocopy - Zero copy skb to skb
2256 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002257 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002258 * @len: number of bytes to copy from source buffer
2259 * @hlen: size of linear headroom in destination buffer
2260 *
2261 * Copies up to `len` bytes from `from` to `to` by creating references
2262 * to the frags in the source buffer.
2263 *
2264 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2265 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002266 *
2267 * Return value:
2268 * 0: everything is OK
2269 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2270 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002271 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002272int
2273skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002274{
2275 int i, j = 0;
2276 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002277 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002278 struct page *page;
2279 unsigned int offset;
2280
2281 BUG_ON(!from->head_frag && !hlen);
2282
2283 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002284 if (len <= skb_tailroom(to))
2285 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002286
2287 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002288 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2289 if (unlikely(ret))
2290 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002291 len -= hlen;
2292 } else {
2293 plen = min_t(int, skb_headlen(from), len);
2294 if (plen) {
2295 page = virt_to_head_page(from->head);
2296 offset = from->data - (unsigned char *)page_address(page);
2297 __skb_fill_page_desc(to, 0, page, offset, plen);
2298 get_page(page);
2299 j = 1;
2300 len -= plen;
2301 }
2302 }
2303
2304 to->truesize += len + plen;
2305 to->len += len + plen;
2306 to->data_len += len + plen;
2307
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002308 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2309 skb_tx_error(from);
2310 return -ENOMEM;
2311 }
2312
Thomas Grafaf2806f2013-12-13 15:22:17 +01002313 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2314 if (!len)
2315 break;
2316 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2317 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2318 len -= skb_shinfo(to)->frags[j].size;
2319 skb_frag_ref(to, j);
2320 j++;
2321 }
2322 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002323
2324 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002325}
2326EXPORT_SYMBOL_GPL(skb_zerocopy);
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2329{
Al Virod3bc23e2006-11-14 21:24:49 -08002330 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 long csstart;
2332
Patrick McHardy84fa7932006-08-29 16:44:56 -07002333 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002334 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 else
2336 csstart = skb_headlen(skb);
2337
Kris Katterjohn09a62662006-01-08 22:24:28 -08002338 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002340 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 csum = 0;
2343 if (csstart != skb->len)
2344 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2345 skb->len - csstart, 0);
2346
Patrick McHardy84fa7932006-08-29 16:44:56 -07002347 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002348 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Al Virod3bc23e2006-11-14 21:24:49 -08002350 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002353EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355/**
2356 * skb_dequeue - remove from the head of the queue
2357 * @list: list to dequeue from
2358 *
2359 * Remove the head of the list. The list lock is taken so the function
2360 * may be used safely with other locking list functions. The head item is
2361 * returned or %NULL if the list is empty.
2362 */
2363
2364struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2365{
2366 unsigned long flags;
2367 struct sk_buff *result;
2368
2369 spin_lock_irqsave(&list->lock, flags);
2370 result = __skb_dequeue(list);
2371 spin_unlock_irqrestore(&list->lock, flags);
2372 return result;
2373}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002374EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376/**
2377 * skb_dequeue_tail - remove from the tail of the queue
2378 * @list: list to dequeue from
2379 *
2380 * Remove the tail of the list. The list lock is taken so the function
2381 * may be used safely with other locking list functions. The tail item is
2382 * returned or %NULL if the list is empty.
2383 */
2384struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2385{
2386 unsigned long flags;
2387 struct sk_buff *result;
2388
2389 spin_lock_irqsave(&list->lock, flags);
2390 result = __skb_dequeue_tail(list);
2391 spin_unlock_irqrestore(&list->lock, flags);
2392 return result;
2393}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002394EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396/**
2397 * skb_queue_purge - empty a list
2398 * @list: list to empty
2399 *
2400 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2401 * the list and one reference dropped. This function takes the list
2402 * lock and is atomic with respect to other list locking functions.
2403 */
2404void skb_queue_purge(struct sk_buff_head *list)
2405{
2406 struct sk_buff *skb;
2407 while ((skb = skb_dequeue(list)) != NULL)
2408 kfree_skb(skb);
2409}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002410EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412/**
2413 * skb_queue_head - queue a buffer at the list head
2414 * @list: list to use
2415 * @newsk: buffer to queue
2416 *
2417 * Queue a buffer at the start of the list. This function takes the
2418 * list lock and can be used safely with other locking &sk_buff functions
2419 * safely.
2420 *
2421 * A buffer cannot be placed on two lists at the same time.
2422 */
2423void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2424{
2425 unsigned long flags;
2426
2427 spin_lock_irqsave(&list->lock, flags);
2428 __skb_queue_head(list, newsk);
2429 spin_unlock_irqrestore(&list->lock, flags);
2430}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002431EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433/**
2434 * skb_queue_tail - queue a buffer at the list tail
2435 * @list: list to use
2436 * @newsk: buffer to queue
2437 *
2438 * Queue a buffer at the tail of the list. This function takes the
2439 * list lock and can be used safely with other locking &sk_buff functions
2440 * safely.
2441 *
2442 * A buffer cannot be placed on two lists at the same time.
2443 */
2444void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2445{
2446 unsigned long flags;
2447
2448 spin_lock_irqsave(&list->lock, flags);
2449 __skb_queue_tail(list, newsk);
2450 spin_unlock_irqrestore(&list->lock, flags);
2451}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002452EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/**
2455 * skb_unlink - remove a buffer from a list
2456 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002457 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 *
David S. Miller8728b832005-08-09 19:25:21 -07002459 * Remove a packet from a list. The list locks are taken and this
2460 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 *
David S. Miller8728b832005-08-09 19:25:21 -07002462 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 */
David S. Miller8728b832005-08-09 19:25:21 -07002464void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
David S. Miller8728b832005-08-09 19:25:21 -07002466 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
David S. Miller8728b832005-08-09 19:25:21 -07002468 spin_lock_irqsave(&list->lock, flags);
2469 __skb_unlink(skb, list);
2470 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002472EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474/**
2475 * skb_append - append a buffer
2476 * @old: buffer to insert after
2477 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002478 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 *
2480 * Place a packet after a given packet in a list. The list locks are taken
2481 * and this function is atomic with respect to other list locked calls.
2482 * A buffer cannot be placed on two lists at the same time.
2483 */
David S. Miller8728b832005-08-09 19:25:21 -07002484void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 unsigned long flags;
2487
David S. Miller8728b832005-08-09 19:25:21 -07002488 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002489 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002490 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002492EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494/**
2495 * skb_insert - insert a buffer
2496 * @old: buffer to insert before
2497 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002498 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 *
David S. Miller8728b832005-08-09 19:25:21 -07002500 * Place a packet before a given packet in a list. The list locks are
2501 * taken and this function is atomic with respect to other list locked
2502 * calls.
2503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 * A buffer cannot be placed on two lists at the same time.
2505 */
David S. Miller8728b832005-08-09 19:25:21 -07002506void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 unsigned long flags;
2509
David S. Miller8728b832005-08-09 19:25:21 -07002510 spin_lock_irqsave(&list->lock, flags);
2511 __skb_insert(newsk, old->prev, old, list);
2512 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002514EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516static inline void skb_split_inside_header(struct sk_buff *skb,
2517 struct sk_buff* skb1,
2518 const u32 len, const int pos)
2519{
2520 int i;
2521
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002522 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2523 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* And move data appendix as is. */
2525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2526 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2527
2528 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2529 skb_shinfo(skb)->nr_frags = 0;
2530 skb1->data_len = skb->data_len;
2531 skb1->len += skb1->data_len;
2532 skb->data_len = 0;
2533 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002534 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537static inline void skb_split_no_header(struct sk_buff *skb,
2538 struct sk_buff* skb1,
2539 const u32 len, int pos)
2540{
2541 int i, k = 0;
2542 const int nfrags = skb_shinfo(skb)->nr_frags;
2543
2544 skb_shinfo(skb)->nr_frags = 0;
2545 skb1->len = skb1->data_len = skb->len - len;
2546 skb->len = len;
2547 skb->data_len = len - pos;
2548
2549 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002550 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 if (pos + size > len) {
2553 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2554
2555 if (pos < len) {
2556 /* Split frag.
2557 * We have two variants in this case:
2558 * 1. Move all the frag to the second
2559 * part, if it is possible. F.e.
2560 * this approach is mandatory for TUX,
2561 * where splitting is expensive.
2562 * 2. Split is accurately. We make this.
2563 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002564 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002566 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2567 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 skb_shinfo(skb)->nr_frags++;
2569 }
2570 k++;
2571 } else
2572 skb_shinfo(skb)->nr_frags++;
2573 pos += size;
2574 }
2575 skb_shinfo(skb1)->nr_frags = k;
2576}
2577
2578/**
2579 * skb_split - Split fragmented skb to two parts at length len.
2580 * @skb: the buffer to split
2581 * @skb1: the buffer to receive the second part
2582 * @len: new length for skb
2583 */
2584void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2585{
2586 int pos = skb_headlen(skb);
2587
Amerigo Wang68534c62013-02-19 22:51:30 +00002588 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (len < pos) /* Split line is inside header. */
2590 skb_split_inside_header(skb, skb1, len, pos);
2591 else /* Second chunk has no header, nothing to copy. */
2592 skb_split_no_header(skb, skb1, len, pos);
2593}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002594EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002596/* Shifting from/to a cloned skb is a no-go.
2597 *
2598 * Caller cannot keep skb_shinfo related pointers past calling here!
2599 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002600static int skb_prepare_for_shift(struct sk_buff *skb)
2601{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002602 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002603}
2604
2605/**
2606 * skb_shift - Shifts paged data partially from skb to another
2607 * @tgt: buffer into which tail data gets added
2608 * @skb: buffer from which the paged data comes from
2609 * @shiftlen: shift up to this many bytes
2610 *
2611 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002612 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002613 * It's up to caller to free skb if everything was shifted.
2614 *
2615 * If @tgt runs out of frags, the whole operation is aborted.
2616 *
2617 * Skb cannot include anything else but paged data while tgt is allowed
2618 * to have non-paged data as well.
2619 *
2620 * TODO: full sized shift could be optimized but that would need
2621 * specialized skb free'er to handle frags without up-to-date nr_frags.
2622 */
2623int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2624{
2625 int from, to, merge, todo;
2626 struct skb_frag_struct *fragfrom, *fragto;
2627
2628 BUG_ON(shiftlen > skb->len);
2629 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2630
2631 todo = shiftlen;
2632 from = 0;
2633 to = skb_shinfo(tgt)->nr_frags;
2634 fragfrom = &skb_shinfo(skb)->frags[from];
2635
2636 /* Actual merge is delayed until the point when we know we can
2637 * commit all, so that we don't have to undo partial changes
2638 */
2639 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002640 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2641 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002642 merge = -1;
2643 } else {
2644 merge = to - 1;
2645
Eric Dumazet9e903e02011-10-18 21:00:24 +00002646 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002647 if (todo < 0) {
2648 if (skb_prepare_for_shift(skb) ||
2649 skb_prepare_for_shift(tgt))
2650 return 0;
2651
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002652 /* All previous frag pointers might be stale! */
2653 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002654 fragto = &skb_shinfo(tgt)->frags[merge];
2655
Eric Dumazet9e903e02011-10-18 21:00:24 +00002656 skb_frag_size_add(fragto, shiftlen);
2657 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002658 fragfrom->page_offset += shiftlen;
2659
2660 goto onlymerged;
2661 }
2662
2663 from++;
2664 }
2665
2666 /* Skip full, not-fitting skb to avoid expensive operations */
2667 if ((shiftlen == skb->len) &&
2668 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2669 return 0;
2670
2671 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2672 return 0;
2673
2674 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2675 if (to == MAX_SKB_FRAGS)
2676 return 0;
2677
2678 fragfrom = &skb_shinfo(skb)->frags[from];
2679 fragto = &skb_shinfo(tgt)->frags[to];
2680
Eric Dumazet9e903e02011-10-18 21:00:24 +00002681 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002682 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002683 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002684 from++;
2685 to++;
2686
2687 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002688 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002689 fragto->page = fragfrom->page;
2690 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002691 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002692
2693 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002694 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002695 todo = 0;
2696
2697 to++;
2698 break;
2699 }
2700 }
2701
2702 /* Ready to "commit" this state change to tgt */
2703 skb_shinfo(tgt)->nr_frags = to;
2704
2705 if (merge >= 0) {
2706 fragfrom = &skb_shinfo(skb)->frags[0];
2707 fragto = &skb_shinfo(tgt)->frags[merge];
2708
Eric Dumazet9e903e02011-10-18 21:00:24 +00002709 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002710 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002711 }
2712
2713 /* Reposition in the original skb */
2714 to = 0;
2715 while (from < skb_shinfo(skb)->nr_frags)
2716 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2717 skb_shinfo(skb)->nr_frags = to;
2718
2719 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2720
2721onlymerged:
2722 /* Most likely the tgt won't ever need its checksum anymore, skb on
2723 * the other hand might need it if it needs to be resent
2724 */
2725 tgt->ip_summed = CHECKSUM_PARTIAL;
2726 skb->ip_summed = CHECKSUM_PARTIAL;
2727
2728 /* Yak, is it really working this way? Some helper please? */
2729 skb->len -= shiftlen;
2730 skb->data_len -= shiftlen;
2731 skb->truesize -= shiftlen;
2732 tgt->len += shiftlen;
2733 tgt->data_len += shiftlen;
2734 tgt->truesize += shiftlen;
2735
2736 return shiftlen;
2737}
2738
Thomas Graf677e90e2005-06-23 20:59:51 -07002739/**
2740 * skb_prepare_seq_read - Prepare a sequential read of skb data
2741 * @skb: the buffer to read
2742 * @from: lower offset of data to be read
2743 * @to: upper offset of data to be read
2744 * @st: state variable
2745 *
2746 * Initializes the specified state variable. Must be called before
2747 * invoking skb_seq_read() for the first time.
2748 */
2749void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2750 unsigned int to, struct skb_seq_state *st)
2751{
2752 st->lower_offset = from;
2753 st->upper_offset = to;
2754 st->root_skb = st->cur_skb = skb;
2755 st->frag_idx = st->stepped_offset = 0;
2756 st->frag_data = NULL;
2757}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002758EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002759
2760/**
2761 * skb_seq_read - Sequentially read skb data
2762 * @consumed: number of bytes consumed by the caller so far
2763 * @data: destination pointer for data to be returned
2764 * @st: state variable
2765 *
Mathias Krausebc323832013-11-07 14:18:26 +01002766 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002767 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002768 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002769 * of the block or 0 if the end of the skb data or the upper
2770 * offset has been reached.
2771 *
2772 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002773 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002774 * of bytes already consumed and the next call to
2775 * skb_seq_read() will return the remaining part of the block.
2776 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002777 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002778 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002779 * reads of potentially non linear data.
2780 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002781 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002782 * at the moment, state->root_skb could be replaced with
2783 * a stack for this purpose.
2784 */
2785unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2786 struct skb_seq_state *st)
2787{
2788 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2789 skb_frag_t *frag;
2790
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002791 if (unlikely(abs_offset >= st->upper_offset)) {
2792 if (st->frag_data) {
2793 kunmap_atomic(st->frag_data);
2794 st->frag_data = NULL;
2795 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002796 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002797 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002798
2799next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002800 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002801
Thomas Chenault995b3372009-05-18 21:43:27 -07002802 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002803 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002804 return block_limit - abs_offset;
2805 }
2806
2807 if (st->frag_idx == 0 && !st->frag_data)
2808 st->stepped_offset += skb_headlen(st->cur_skb);
2809
2810 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2811 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002812 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002813
2814 if (abs_offset < block_limit) {
2815 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002816 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002817
2818 *data = (u8 *) st->frag_data + frag->page_offset +
2819 (abs_offset - st->stepped_offset);
2820
2821 return block_limit - abs_offset;
2822 }
2823
2824 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002825 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002826 st->frag_data = NULL;
2827 }
2828
2829 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002830 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002831 }
2832
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002833 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002834 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002835 st->frag_data = NULL;
2836 }
2837
David S. Miller21dc3302010-08-23 00:13:46 -07002838 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002839 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002840 st->frag_idx = 0;
2841 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002842 } else if (st->cur_skb->next) {
2843 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002844 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002845 goto next_skb;
2846 }
2847
2848 return 0;
2849}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002850EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002851
2852/**
2853 * skb_abort_seq_read - Abort a sequential read of skb data
2854 * @st: state variable
2855 *
2856 * Must be called if skb_seq_read() was not called until it
2857 * returned 0.
2858 */
2859void skb_abort_seq_read(struct skb_seq_state *st)
2860{
2861 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002862 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002863}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002864EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002865
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002866#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2867
2868static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2869 struct ts_config *conf,
2870 struct ts_state *state)
2871{
2872 return skb_seq_read(offset, text, TS_SKB_CB(state));
2873}
2874
2875static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2876{
2877 skb_abort_seq_read(TS_SKB_CB(state));
2878}
2879
2880/**
2881 * skb_find_text - Find a text pattern in skb data
2882 * @skb: the buffer to look in
2883 * @from: search offset
2884 * @to: search limit
2885 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002886 *
2887 * Finds a pattern in the skb data according to the specified
2888 * textsearch configuration. Use textsearch_next() to retrieve
2889 * subsequent occurrences of the pattern. Returns the offset
2890 * to the first occurrence or UINT_MAX if no match was found.
2891 */
2892unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002893 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002894{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002895 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002896 unsigned int ret;
2897
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002898 config->get_next_block = skb_ts_get_next_block;
2899 config->finish = skb_ts_finish;
2900
Bojan Prtvar059a2442015-02-22 11:46:35 +01002901 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002902
Bojan Prtvar059a2442015-02-22 11:46:35 +01002903 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002904 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002905}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002906EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002907
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002908/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002909 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002910 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002911 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002912 * @getfrag: call back function to be used for getting the user data
2913 * @from: pointer to user message iov
2914 * @length: length of the iov message
2915 *
2916 * Description: This procedure append the user data in the fragment part
2917 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2918 */
2919int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002920 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002921 int len, int odd, struct sk_buff *skb),
2922 void *from, int length)
2923{
Eric Dumazetb2111722012-12-28 06:06:37 +00002924 int frg_cnt = skb_shinfo(skb)->nr_frags;
2925 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002926 int offset = 0;
2927 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002928 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002929
2930 do {
2931 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002932 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002933 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002934
Eric Dumazetb2111722012-12-28 06:06:37 +00002935 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002936 return -ENOMEM;
2937
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002938 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002939 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002940
Eric Dumazetb2111722012-12-28 06:06:37 +00002941 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2942 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002943 if (ret < 0)
2944 return -EFAULT;
2945
2946 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002947 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2948 copy);
2949 frg_cnt++;
2950 pfrag->offset += copy;
2951 get_page(pfrag->page);
2952
2953 skb->truesize += copy;
2954 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002955 skb->len += copy;
2956 skb->data_len += copy;
2957 offset += copy;
2958 length -= copy;
2959
2960 } while (length > 0);
2961
2962 return 0;
2963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002964EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002965
Herbert Xucbb042f92006-03-20 22:43:56 -08002966/**
2967 * skb_pull_rcsum - pull skb and update receive checksum
2968 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08002969 * @len: length of data pulled
2970 *
2971 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002972 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002973 * receive path processing instead of skb_pull unless you know
2974 * that the checksum difference is zero (e.g., a valid IP header)
2975 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08002976 */
2977unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2978{
2979 BUG_ON(len > skb->len);
2980 skb->len -= len;
2981 BUG_ON(skb->len < skb->data_len);
2982 skb_postpull_rcsum(skb, skb->data, len);
2983 return skb->data += len;
2984}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002985EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2986
Herbert Xuf4c50d92006-06-22 03:02:40 -07002987/**
2988 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002989 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002990 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002991 *
2992 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002993 * a pointer to the first in a list of new skbs for the segments.
2994 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002995 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002996struct sk_buff *skb_segment(struct sk_buff *head_skb,
2997 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002998{
2999 struct sk_buff *segs = NULL;
3000 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003001 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003002 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3003 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3004 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003005 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003006 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003007 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003008 unsigned int headroom;
3009 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003010 __be16 proto;
3011 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08003012 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003013 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003014 int err = -ENOMEM;
3015 int i = 0;
3016 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003017 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003018
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003019 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003020 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003021 if (unlikely(!proto))
3022 return ERR_PTR(-EINVAL);
3023
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003024 csum = !head_skb->encap_hdr_csum &&
3025 !!can_checksum_protocol(features, proto);
3026
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003027 headroom = skb_headroom(head_skb);
3028 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003029
3030 do {
3031 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003032 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003033 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003034 int size;
3035
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003036 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003037 if (len > mss)
3038 len = mss;
3039
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003040 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003041 if (hsize < 0)
3042 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003043 if (hsize > len || !sg)
3044 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003045
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003046 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3047 (skb_headlen(list_skb) == len || sg)) {
3048 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003049
Herbert Xu9d8506c2013-11-21 11:10:04 -08003050 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003051 nfrags = skb_shinfo(list_skb)->nr_frags;
3052 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003053 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003054 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003055
3056 while (pos < offset + len) {
3057 BUG_ON(i >= nfrags);
3058
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003059 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003060 if (pos + size > offset + len)
3061 break;
3062
3063 i++;
3064 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003065 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003066 }
3067
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003068 nskb = skb_clone(list_skb, GFP_ATOMIC);
3069 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003070
3071 if (unlikely(!nskb))
3072 goto err;
3073
Herbert Xu9d8506c2013-11-21 11:10:04 -08003074 if (unlikely(pskb_trim(nskb, len))) {
3075 kfree_skb(nskb);
3076 goto err;
3077 }
3078
Alexander Duyckec47ea82012-05-04 14:26:56 +00003079 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003080 if (skb_cow_head(nskb, doffset + headroom)) {
3081 kfree_skb(nskb);
3082 goto err;
3083 }
3084
Alexander Duyckec47ea82012-05-04 14:26:56 +00003085 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003086 skb_release_head_state(nskb);
3087 __skb_push(nskb, doffset);
3088 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003089 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003090 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003091 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003092
3093 if (unlikely(!nskb))
3094 goto err;
3095
3096 skb_reserve(nskb, headroom);
3097 __skb_put(nskb, doffset);
3098 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003099
3100 if (segs)
3101 tail->next = nskb;
3102 else
3103 segs = nskb;
3104 tail = nskb;
3105
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003106 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003107
Eric Dumazet030737b2013-10-19 11:42:54 -07003108 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003109 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003110
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003111 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003112 nskb->data - tnl_hlen,
3113 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003114
Herbert Xu9d8506c2013-11-21 11:10:04 -08003115 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003116 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003117
Tom Herberte585f232014-11-04 09:06:54 -08003118 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003119 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003120 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003121 skb_put(nskb, len),
3122 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003123 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003124 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003125 continue;
3126 }
3127
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003128 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003129
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003130 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003131 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003132
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003133 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3134 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003135
Herbert Xu9d8506c2013-11-21 11:10:04 -08003136 while (pos < offset + len) {
3137 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003138 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003139
3140 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003141 nfrags = skb_shinfo(list_skb)->nr_frags;
3142 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003143 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003144
3145 BUG_ON(!nfrags);
3146
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003147 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003148 }
3149
3150 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3151 MAX_SKB_FRAGS)) {
3152 net_warn_ratelimited(
3153 "skb_segment: too many frags: %u %u\n",
3154 pos, mss);
3155 goto err;
3156 }
3157
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003158 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3159 goto err;
3160
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003161 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003162 __skb_frag_ref(nskb_frag);
3163 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003164
3165 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003166 nskb_frag->page_offset += offset - pos;
3167 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003168 }
3169
Herbert Xu89319d382008-12-15 23:26:06 -08003170 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003171
3172 if (pos + size <= offset + len) {
3173 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003174 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003175 pos += size;
3176 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003177 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003178 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003179 }
3180
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003181 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003182 }
3183
Herbert Xu89319d382008-12-15 23:26:06 -08003184skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003185 nskb->data_len = len - hsize;
3186 nskb->len += nskb->data_len;
3187 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003188
Simon Horman1cdbcb72013-05-19 15:46:49 +00003189perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003190 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003191 nskb->csum = skb_checksum(nskb, doffset,
3192 nskb->len - doffset, 0);
3193 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003194 SKB_GSO_CB(nskb)->csum_start =
3195 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003196 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003197 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003198
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003199 /* Some callers want to get the end of the list.
3200 * Put it in segs->prev to avoid walking the list.
3201 * (see validate_xmit_skb_list() for example)
3202 */
3203 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003204
3205 /* Following permits correct backpressure, for protocols
3206 * using skb_set_owner_w().
3207 * Idea is to tranfert ownership from head_skb to last segment.
3208 */
3209 if (head_skb->destructor == sock_wfree) {
3210 swap(tail->truesize, head_skb->truesize);
3211 swap(tail->destructor, head_skb->destructor);
3212 swap(tail->sk, head_skb->sk);
3213 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003214 return segs;
3215
3216err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003217 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003218 return ERR_PTR(err);
3219}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003220EXPORT_SYMBOL_GPL(skb_segment);
3221
Herbert Xu71d93b32008-12-15 23:42:33 -08003222int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3223{
Eric Dumazet8a291112013-10-08 09:02:23 -07003224 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003225 unsigned int offset = skb_gro_offset(skb);
3226 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003227 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003228 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003229 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003230
Eric Dumazet8a291112013-10-08 09:02:23 -07003231 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003232 return -E2BIG;
3233
Eric Dumazet29e98242014-05-16 11:34:37 -07003234 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003235 pinfo = skb_shinfo(lp);
3236
3237 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003238 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003239 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003240 int i = skbinfo->nr_frags;
3241 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003242
Herbert Xu66e92fc2009-05-26 18:50:32 +00003243 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003244 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003245
Eric Dumazet8a291112013-10-08 09:02:23 -07003246 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003247 pinfo->nr_frags = nr_frags;
3248 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003249
Herbert Xu9aaa1562009-05-26 18:50:33 +00003250 frag = pinfo->frags + nr_frags;
3251 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003252 do {
3253 *--frag = *--frag2;
3254 } while (--i);
3255
3256 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003257 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003258
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003259 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003260 delta_truesize = skb->truesize -
3261 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003262
Herbert Xuf5572062009-01-14 20:40:03 -08003263 skb->truesize -= skb->data_len;
3264 skb->len -= skb->data_len;
3265 skb->data_len = 0;
3266
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003267 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003268 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003269 } else if (skb->head_frag) {
3270 int nr_frags = pinfo->nr_frags;
3271 skb_frag_t *frag = pinfo->frags + nr_frags;
3272 struct page *page = virt_to_head_page(skb->head);
3273 unsigned int first_size = headlen - offset;
3274 unsigned int first_offset;
3275
3276 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003277 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003278
3279 first_offset = skb->data -
3280 (unsigned char *)page_address(page) +
3281 offset;
3282
3283 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3284
3285 frag->page.p = page;
3286 frag->page_offset = first_offset;
3287 skb_frag_size_set(frag, first_size);
3288
3289 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3290 /* We dont need to clear skbinfo->nr_frags here */
3291
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003292 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003293 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3294 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003295 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003296
3297merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003298 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003299 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003300 unsigned int eat = offset - headlen;
3301
3302 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003303 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003304 skb->data_len -= eat;
3305 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003306 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003307 }
3308
Herbert Xu67147ba2009-05-26 18:50:22 +00003309 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003310
Eric Dumazet29e98242014-05-16 11:34:37 -07003311 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003312 skb_shinfo(p)->frag_list = skb;
3313 else
3314 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003315 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003316 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003317 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003318
Herbert Xu5d38a072009-01-04 16:13:40 -08003319done:
3320 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003321 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003322 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003323 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003324 if (lp != p) {
3325 lp->data_len += len;
3326 lp->truesize += delta_truesize;
3327 lp->len += len;
3328 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003329 NAPI_GRO_CB(skb)->same_flow = 1;
3330 return 0;
3331}
Herbert Xu71d93b32008-12-15 23:42:33 -08003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333void __init skb_init(void)
3334{
3335 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3336 sizeof(struct sk_buff),
3337 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003338 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003339 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003340 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003341 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003342 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003343 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003344 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
David Howells716ea3a2007-04-02 20:19:53 -07003347/**
3348 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3349 * @skb: Socket buffer containing the buffers to be mapped
3350 * @sg: The scatter-gather list to map into
3351 * @offset: The offset into the buffer's contents to start mapping
3352 * @len: Length of buffer space to be mapped
3353 *
3354 * Fill the specified scatter-gather list with mappings/pointers into a
3355 * region of the buffer space attached to a socket buffer.
3356 */
David S. Miller51c739d2007-10-30 21:29:29 -07003357static int
3358__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003359{
David S. Miller1a028e52007-04-27 15:21:23 -07003360 int start = skb_headlen(skb);
3361 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003362 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003363 int elt = 0;
3364
3365 if (copy > 0) {
3366 if (copy > len)
3367 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003368 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003369 elt++;
3370 if ((len -= copy) == 0)
3371 return elt;
3372 offset += copy;
3373 }
3374
3375 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003376 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003377
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003378 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003379
Eric Dumazet9e903e02011-10-18 21:00:24 +00003380 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003381 if ((copy = end - offset) > 0) {
3382 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3383
3384 if (copy > len)
3385 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003386 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003387 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003388 elt++;
3389 if (!(len -= copy))
3390 return elt;
3391 offset += copy;
3392 }
David S. Miller1a028e52007-04-27 15:21:23 -07003393 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003394 }
3395
David S. Millerfbb398a2009-06-09 00:18:59 -07003396 skb_walk_frags(skb, frag_iter) {
3397 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003398
David S. Millerfbb398a2009-06-09 00:18:59 -07003399 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003400
David S. Millerfbb398a2009-06-09 00:18:59 -07003401 end = start + frag_iter->len;
3402 if ((copy = end - offset) > 0) {
3403 if (copy > len)
3404 copy = len;
3405 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3406 copy);
3407 if ((len -= copy) == 0)
3408 return elt;
3409 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003410 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003411 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003412 }
3413 BUG_ON(len);
3414 return elt;
3415}
3416
Fan Du25a91d82014-01-18 09:54:23 +08003417/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3418 * sglist without mark the sg which contain last skb data as the end.
3419 * So the caller can mannipulate sg list as will when padding new data after
3420 * the first call without calling sg_unmark_end to expend sg list.
3421 *
3422 * Scenario to use skb_to_sgvec_nomark:
3423 * 1. sg_init_table
3424 * 2. skb_to_sgvec_nomark(payload1)
3425 * 3. skb_to_sgvec_nomark(payload2)
3426 *
3427 * This is equivalent to:
3428 * 1. sg_init_table
3429 * 2. skb_to_sgvec(payload1)
3430 * 3. sg_unmark_end
3431 * 4. skb_to_sgvec(payload2)
3432 *
3433 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3434 * is more preferable.
3435 */
3436int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3437 int offset, int len)
3438{
3439 return __skb_to_sgvec(skb, sg, offset, len);
3440}
3441EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3442
David S. Miller51c739d2007-10-30 21:29:29 -07003443int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3444{
3445 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3446
Jens Axboec46f2332007-10-31 12:06:37 +01003447 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003448
3449 return nsg;
3450}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003451EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003452
David Howells716ea3a2007-04-02 20:19:53 -07003453/**
3454 * skb_cow_data - Check that a socket buffer's data buffers are writable
3455 * @skb: The socket buffer to check.
3456 * @tailbits: Amount of trailing space to be added
3457 * @trailer: Returned pointer to the skb where the @tailbits space begins
3458 *
3459 * Make sure that the data buffers attached to a socket buffer are
3460 * writable. If they are not, private copies are made of the data buffers
3461 * and the socket buffer is set to use these instead.
3462 *
3463 * If @tailbits is given, make sure that there is space to write @tailbits
3464 * bytes of data beyond current end of socket buffer. @trailer will be
3465 * set to point to the skb in which this space begins.
3466 *
3467 * The number of scatterlist elements required to completely map the
3468 * COW'd and extended socket buffer will be returned.
3469 */
3470int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3471{
3472 int copyflag;
3473 int elt;
3474 struct sk_buff *skb1, **skb_p;
3475
3476 /* If skb is cloned or its head is paged, reallocate
3477 * head pulling out all the pages (pages are considered not writable
3478 * at the moment even if they are anonymous).
3479 */
3480 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3481 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3482 return -ENOMEM;
3483
3484 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003485 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003486 /* A little of trouble, not enough of space for trailer.
3487 * This should not happen, when stack is tuned to generate
3488 * good frames. OK, on miss we reallocate and reserve even more
3489 * space, 128 bytes is fair. */
3490
3491 if (skb_tailroom(skb) < tailbits &&
3492 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3493 return -ENOMEM;
3494
3495 /* Voila! */
3496 *trailer = skb;
3497 return 1;
3498 }
3499
3500 /* Misery. We are in troubles, going to mincer fragments... */
3501
3502 elt = 1;
3503 skb_p = &skb_shinfo(skb)->frag_list;
3504 copyflag = 0;
3505
3506 while ((skb1 = *skb_p) != NULL) {
3507 int ntail = 0;
3508
3509 /* The fragment is partially pulled by someone,
3510 * this can happen on input. Copy it and everything
3511 * after it. */
3512
3513 if (skb_shared(skb1))
3514 copyflag = 1;
3515
3516 /* If the skb is the last, worry about trailer. */
3517
3518 if (skb1->next == NULL && tailbits) {
3519 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003520 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003521 skb_tailroom(skb1) < tailbits)
3522 ntail = tailbits + 128;
3523 }
3524
3525 if (copyflag ||
3526 skb_cloned(skb1) ||
3527 ntail ||
3528 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003529 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003530 struct sk_buff *skb2;
3531
3532 /* Fuck, we are miserable poor guys... */
3533 if (ntail == 0)
3534 skb2 = skb_copy(skb1, GFP_ATOMIC);
3535 else
3536 skb2 = skb_copy_expand(skb1,
3537 skb_headroom(skb1),
3538 ntail,
3539 GFP_ATOMIC);
3540 if (unlikely(skb2 == NULL))
3541 return -ENOMEM;
3542
3543 if (skb1->sk)
3544 skb_set_owner_w(skb2, skb1->sk);
3545
3546 /* Looking around. Are we still alive?
3547 * OK, link new skb, drop old one */
3548
3549 skb2->next = skb1->next;
3550 *skb_p = skb2;
3551 kfree_skb(skb1);
3552 skb1 = skb2;
3553 }
3554 elt++;
3555 *trailer = skb1;
3556 skb_p = &skb1->next;
3557 }
3558
3559 return elt;
3560}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003561EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003562
Eric Dumazetb1faf562010-05-31 23:44:05 -07003563static void sock_rmem_free(struct sk_buff *skb)
3564{
3565 struct sock *sk = skb->sk;
3566
3567 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3568}
3569
3570/*
3571 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3572 */
3573int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3574{
3575 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003576 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003577 return -ENOMEM;
3578
3579 skb_orphan(skb);
3580 skb->sk = sk;
3581 skb->destructor = sock_rmem_free;
3582 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3583
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003584 /* before exiting rcu section, make sure dst is refcounted */
3585 skb_dst_force(skb);
3586
Eric Dumazetb1faf562010-05-31 23:44:05 -07003587 skb_queue_tail(&sk->sk_error_queue, skb);
3588 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003589 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003590 return 0;
3591}
3592EXPORT_SYMBOL(sock_queue_err_skb);
3593
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003594struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3595{
3596 struct sk_buff_head *q = &sk->sk_error_queue;
3597 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003598 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003599 int err = 0;
3600
Eric Dumazet997d5c32015-02-18 05:47:55 -08003601 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003602 skb = __skb_dequeue(q);
3603 if (skb && (skb_next = skb_peek(q)))
3604 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003605 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003606
3607 sk->sk_err = err;
3608 if (err)
3609 sk->sk_error_report(sk);
3610
3611 return skb;
3612}
3613EXPORT_SYMBOL(sock_dequeue_err_skb);
3614
Alexander Duyckcab41c42014-09-10 18:05:26 -04003615/**
3616 * skb_clone_sk - create clone of skb, and take reference to socket
3617 * @skb: the skb to clone
3618 *
3619 * This function creates a clone of a buffer that holds a reference on
3620 * sk_refcnt. Buffers created via this function are meant to be
3621 * returned using sock_queue_err_skb, or free via kfree_skb.
3622 *
3623 * When passing buffers allocated with this function to sock_queue_err_skb
3624 * it is necessary to wrap the call with sock_hold/sock_put in order to
3625 * prevent the socket from being released prior to being enqueued on
3626 * the sk_error_queue.
3627 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003628struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3629{
3630 struct sock *sk = skb->sk;
3631 struct sk_buff *clone;
3632
3633 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3634 return NULL;
3635
3636 clone = skb_clone(skb, GFP_ATOMIC);
3637 if (!clone) {
3638 sock_put(sk);
3639 return NULL;
3640 }
3641
3642 clone->sk = sk;
3643 clone->destructor = sock_efree;
3644
3645 return clone;
3646}
3647EXPORT_SYMBOL(skb_clone_sk);
3648
Alexander Duyck37846ef2014-09-04 13:31:10 -04003649static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3650 struct sock *sk,
3651 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003652{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003653 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003654 int err;
3655
Patrick Ohlyac45f602009-02-12 05:03:37 +00003656 serr = SKB_EXT_ERR(skb);
3657 memset(serr, 0, sizeof(*serr));
3658 serr->ee.ee_errno = ENOMSG;
3659 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003660 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003661 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003662 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003663 if (sk->sk_protocol == IPPROTO_TCP)
3664 serr->ee.ee_data -= sk->sk_tskey;
3665 }
Eric Dumazet29030372010-05-29 00:20:48 -07003666
Patrick Ohlyac45f602009-02-12 05:03:37 +00003667 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003668
Patrick Ohlyac45f602009-02-12 05:03:37 +00003669 if (err)
3670 kfree_skb(skb);
3671}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003672
Willem de Bruijnb245be12015-01-30 13:29:32 -05003673static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3674{
3675 bool ret;
3676
3677 if (likely(sysctl_tstamp_allow_data || tsonly))
3678 return true;
3679
3680 read_lock_bh(&sk->sk_callback_lock);
3681 ret = sk->sk_socket && sk->sk_socket->file &&
3682 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3683 read_unlock_bh(&sk->sk_callback_lock);
3684 return ret;
3685}
3686
Alexander Duyck37846ef2014-09-04 13:31:10 -04003687void skb_complete_tx_timestamp(struct sk_buff *skb,
3688 struct skb_shared_hwtstamps *hwtstamps)
3689{
3690 struct sock *sk = skb->sk;
3691
Willem de Bruijnb245be12015-01-30 13:29:32 -05003692 if (!skb_may_tx_timestamp(sk, false))
3693 return;
3694
Alexander Duyck62bccb82014-09-04 13:31:35 -04003695 /* take a reference to prevent skb_orphan() from freeing the socket */
3696 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003697
Alexander Duyck62bccb82014-09-04 13:31:35 -04003698 *skb_hwtstamps(skb) = *hwtstamps;
3699 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003700
3701 sock_put(sk);
3702}
3703EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3704
3705void __skb_tstamp_tx(struct sk_buff *orig_skb,
3706 struct skb_shared_hwtstamps *hwtstamps,
3707 struct sock *sk, int tstype)
3708{
3709 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003710 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003711
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003712 if (!sk)
3713 return;
3714
3715 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3716 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003717 return;
3718
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003719 if (tsonly)
3720 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003721 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003722 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003723 if (!skb)
3724 return;
3725
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003726 if (tsonly) {
3727 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3728 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3729 }
3730
3731 if (hwtstamps)
3732 *skb_hwtstamps(skb) = *hwtstamps;
3733 else
3734 skb->tstamp = ktime_get_real();
3735
Alexander Duyck37846ef2014-09-04 13:31:10 -04003736 __skb_complete_tx_timestamp(skb, sk, tstype);
3737}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003738EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3739
3740void skb_tstamp_tx(struct sk_buff *orig_skb,
3741 struct skb_shared_hwtstamps *hwtstamps)
3742{
3743 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3744 SCM_TSTAMP_SND);
3745}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003746EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3747
Johannes Berg6e3e9392011-11-09 10:15:42 +01003748void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3749{
3750 struct sock *sk = skb->sk;
3751 struct sock_exterr_skb *serr;
3752 int err;
3753
3754 skb->wifi_acked_valid = 1;
3755 skb->wifi_acked = acked;
3756
3757 serr = SKB_EXT_ERR(skb);
3758 memset(serr, 0, sizeof(*serr));
3759 serr->ee.ee_errno = ENOMSG;
3760 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3761
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003762 /* take a reference to prevent skb_orphan() from freeing the socket */
3763 sock_hold(sk);
3764
Johannes Berg6e3e9392011-11-09 10:15:42 +01003765 err = sock_queue_err_skb(sk, skb);
3766 if (err)
3767 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003768
3769 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003770}
3771EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3772
Rusty Russellf35d9d82008-02-04 23:49:54 -05003773/**
3774 * skb_partial_csum_set - set up and verify partial csum values for packet
3775 * @skb: the skb to set
3776 * @start: the number of bytes after skb->data to start checksumming.
3777 * @off: the offset from start to place the checksum.
3778 *
3779 * For untrusted partially-checksummed packets, we need to make sure the values
3780 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3781 *
3782 * This function checks and sets those values and skb->ip_summed: if this
3783 * returns false you should drop the packet.
3784 */
3785bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3786{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003787 if (unlikely(start > skb_headlen(skb)) ||
3788 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003789 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3790 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003791 return false;
3792 }
3793 skb->ip_summed = CHECKSUM_PARTIAL;
3794 skb->csum_start = skb_headroom(skb) + start;
3795 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003796 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003797 return true;
3798}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003799EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003800
Paul Durranted1f50c2014-01-09 10:02:46 +00003801static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3802 unsigned int max)
3803{
3804 if (skb_headlen(skb) >= len)
3805 return 0;
3806
3807 /* If we need to pullup then pullup to the max, so we
3808 * won't need to do it again.
3809 */
3810 if (max > skb->len)
3811 max = skb->len;
3812
3813 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3814 return -ENOMEM;
3815
3816 if (skb_headlen(skb) < len)
3817 return -EPROTO;
3818
3819 return 0;
3820}
3821
Jan Beulichf9708b42014-03-11 13:56:05 +00003822#define MAX_TCP_HDR_LEN (15 * 4)
3823
3824static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3825 typeof(IPPROTO_IP) proto,
3826 unsigned int off)
3827{
3828 switch (proto) {
3829 int err;
3830
3831 case IPPROTO_TCP:
3832 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3833 off + MAX_TCP_HDR_LEN);
3834 if (!err && !skb_partial_csum_set(skb, off,
3835 offsetof(struct tcphdr,
3836 check)))
3837 err = -EPROTO;
3838 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3839
3840 case IPPROTO_UDP:
3841 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3842 off + sizeof(struct udphdr));
3843 if (!err && !skb_partial_csum_set(skb, off,
3844 offsetof(struct udphdr,
3845 check)))
3846 err = -EPROTO;
3847 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3848 }
3849
3850 return ERR_PTR(-EPROTO);
3851}
3852
Paul Durranted1f50c2014-01-09 10:02:46 +00003853/* This value should be large enough to cover a tagged ethernet header plus
3854 * maximally sized IP and TCP or UDP headers.
3855 */
3856#define MAX_IP_HDR_LEN 128
3857
Jan Beulichf9708b42014-03-11 13:56:05 +00003858static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003859{
3860 unsigned int off;
3861 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003862 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003863 int err;
3864
3865 fragment = false;
3866
3867 err = skb_maybe_pull_tail(skb,
3868 sizeof(struct iphdr),
3869 MAX_IP_HDR_LEN);
3870 if (err < 0)
3871 goto out;
3872
3873 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3874 fragment = true;
3875
3876 off = ip_hdrlen(skb);
3877
3878 err = -EPROTO;
3879
3880 if (fragment)
3881 goto out;
3882
Jan Beulichf9708b42014-03-11 13:56:05 +00003883 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3884 if (IS_ERR(csum))
3885 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003886
Jan Beulichf9708b42014-03-11 13:56:05 +00003887 if (recalculate)
3888 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3889 ip_hdr(skb)->daddr,
3890 skb->len - off,
3891 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003892 err = 0;
3893
3894out:
3895 return err;
3896}
3897
3898/* This value should be large enough to cover a tagged ethernet header plus
3899 * an IPv6 header, all options, and a maximal TCP or UDP header.
3900 */
3901#define MAX_IPV6_HDR_LEN 256
3902
3903#define OPT_HDR(type, skb, off) \
3904 (type *)(skb_network_header(skb) + (off))
3905
3906static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3907{
3908 int err;
3909 u8 nexthdr;
3910 unsigned int off;
3911 unsigned int len;
3912 bool fragment;
3913 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003914 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003915
3916 fragment = false;
3917 done = false;
3918
3919 off = sizeof(struct ipv6hdr);
3920
3921 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3922 if (err < 0)
3923 goto out;
3924
3925 nexthdr = ipv6_hdr(skb)->nexthdr;
3926
3927 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3928 while (off <= len && !done) {
3929 switch (nexthdr) {
3930 case IPPROTO_DSTOPTS:
3931 case IPPROTO_HOPOPTS:
3932 case IPPROTO_ROUTING: {
3933 struct ipv6_opt_hdr *hp;
3934
3935 err = skb_maybe_pull_tail(skb,
3936 off +
3937 sizeof(struct ipv6_opt_hdr),
3938 MAX_IPV6_HDR_LEN);
3939 if (err < 0)
3940 goto out;
3941
3942 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3943 nexthdr = hp->nexthdr;
3944 off += ipv6_optlen(hp);
3945 break;
3946 }
3947 case IPPROTO_AH: {
3948 struct ip_auth_hdr *hp;
3949
3950 err = skb_maybe_pull_tail(skb,
3951 off +
3952 sizeof(struct ip_auth_hdr),
3953 MAX_IPV6_HDR_LEN);
3954 if (err < 0)
3955 goto out;
3956
3957 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3958 nexthdr = hp->nexthdr;
3959 off += ipv6_authlen(hp);
3960 break;
3961 }
3962 case IPPROTO_FRAGMENT: {
3963 struct frag_hdr *hp;
3964
3965 err = skb_maybe_pull_tail(skb,
3966 off +
3967 sizeof(struct frag_hdr),
3968 MAX_IPV6_HDR_LEN);
3969 if (err < 0)
3970 goto out;
3971
3972 hp = OPT_HDR(struct frag_hdr, skb, off);
3973
3974 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3975 fragment = true;
3976
3977 nexthdr = hp->nexthdr;
3978 off += sizeof(struct frag_hdr);
3979 break;
3980 }
3981 default:
3982 done = true;
3983 break;
3984 }
3985 }
3986
3987 err = -EPROTO;
3988
3989 if (!done || fragment)
3990 goto out;
3991
Jan Beulichf9708b42014-03-11 13:56:05 +00003992 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3993 if (IS_ERR(csum))
3994 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003995
Jan Beulichf9708b42014-03-11 13:56:05 +00003996 if (recalculate)
3997 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3998 &ipv6_hdr(skb)->daddr,
3999 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004000 err = 0;
4001
4002out:
4003 return err;
4004}
4005
4006/**
4007 * skb_checksum_setup - set up partial checksum offset
4008 * @skb: the skb to set up
4009 * @recalculate: if true the pseudo-header checksum will be recalculated
4010 */
4011int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4012{
4013 int err;
4014
4015 switch (skb->protocol) {
4016 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004017 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004018 break;
4019
4020 case htons(ETH_P_IPV6):
4021 err = skb_checksum_setup_ipv6(skb, recalculate);
4022 break;
4023
4024 default:
4025 err = -EPROTO;
4026 break;
4027 }
4028
4029 return err;
4030}
4031EXPORT_SYMBOL(skb_checksum_setup);
4032
Ben Hutchings4497b072008-06-19 16:22:28 -07004033void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4034{
Joe Perchese87cc472012-05-13 21:56:26 +00004035 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4036 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004037}
Ben Hutchings4497b072008-06-19 16:22:28 -07004038EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004039
4040void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4041{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004042 if (head_stolen) {
4043 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004044 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004045 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004046 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004047 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004048}
4049EXPORT_SYMBOL(kfree_skb_partial);
4050
4051/**
4052 * skb_try_coalesce - try to merge skb to prior one
4053 * @to: prior buffer
4054 * @from: buffer to add
4055 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004056 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004057 */
4058bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4059 bool *fragstolen, int *delta_truesize)
4060{
4061 int i, delta, len = from->len;
4062
4063 *fragstolen = false;
4064
4065 if (skb_cloned(to))
4066 return false;
4067
4068 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004069 if (len)
4070 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004071 *delta_truesize = 0;
4072 return true;
4073 }
4074
4075 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4076 return false;
4077
4078 if (skb_headlen(from) != 0) {
4079 struct page *page;
4080 unsigned int offset;
4081
4082 if (skb_shinfo(to)->nr_frags +
4083 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4084 return false;
4085
4086 if (skb_head_is_locked(from))
4087 return false;
4088
4089 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4090
4091 page = virt_to_head_page(from->head);
4092 offset = from->data - (unsigned char *)page_address(page);
4093
4094 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4095 page, offset, skb_headlen(from));
4096 *fragstolen = true;
4097 } else {
4098 if (skb_shinfo(to)->nr_frags +
4099 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4100 return false;
4101
Weiping Panf4b549a2012-09-28 20:15:30 +00004102 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004103 }
4104
4105 WARN_ON_ONCE(delta < len);
4106
4107 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4108 skb_shinfo(from)->frags,
4109 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4110 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4111
4112 if (!skb_cloned(from))
4113 skb_shinfo(from)->nr_frags = 0;
4114
Li RongQing8ea853f2012-09-18 16:53:21 +00004115 /* if the skb is not cloned this does nothing
4116 * since we set nr_frags to 0.
4117 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004118 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4119 skb_frag_ref(from, i);
4120
4121 to->truesize += delta;
4122 to->len += len;
4123 to->data_len += len;
4124
4125 *delta_truesize = delta;
4126 return true;
4127}
4128EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004129
4130/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004131 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004132 *
4133 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004134 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004135 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004136 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4137 * into/from a tunnel. Some information have to be cleared during these
4138 * operations.
4139 * skb_scrub_packet can also be used to clean a skb before injecting it in
4140 * another namespace (@xnet == true). We have to clear all information in the
4141 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004142 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004143void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004144{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004145 skb->tstamp.tv64 = 0;
4146 skb->pkt_type = PACKET_HOST;
4147 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004148 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004149 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004150 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004151 secpath_reset(skb);
4152 nf_reset(skb);
4153 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004154
4155 if (!xnet)
4156 return;
4157
4158 skb_orphan(skb);
4159 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004160}
4161EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004162
4163/**
4164 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4165 *
4166 * @skb: GSO skb
4167 *
4168 * skb_gso_transport_seglen is used to determine the real size of the
4169 * individual segments, including Layer4 headers (TCP/UDP).
4170 *
4171 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4172 */
4173unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4174{
4175 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004176 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004177
Florian Westphalf993bc22014-10-20 13:49:18 +02004178 if (skb->encapsulation) {
4179 thlen = skb_inner_transport_header(skb) -
4180 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004181
Florian Westphalf993bc22014-10-20 13:49:18 +02004182 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4183 thlen += inner_tcp_hdrlen(skb);
4184 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4185 thlen = tcp_hdrlen(skb);
4186 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004187 /* UFO sets gso_size to the size of the fragmentation
4188 * payload, i.e. the size of the L4 (UDP) header is already
4189 * accounted for.
4190 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004191 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004192}
4193EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004194
4195static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4196{
4197 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4198 kfree_skb(skb);
4199 return NULL;
4200 }
4201
4202 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4203 skb->mac_header += VLAN_HLEN;
4204 return skb;
4205}
4206
4207struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4208{
4209 struct vlan_hdr *vhdr;
4210 u16 vlan_tci;
4211
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004212 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004213 /* vlan_tci is already set-up so leave this for another time */
4214 return skb;
4215 }
4216
4217 skb = skb_share_check(skb, GFP_ATOMIC);
4218 if (unlikely(!skb))
4219 goto err_free;
4220
4221 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4222 goto err_free;
4223
4224 vhdr = (struct vlan_hdr *)skb->data;
4225 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4226 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4227
4228 skb_pull_rcsum(skb, VLAN_HLEN);
4229 vlan_set_encap_proto(skb, vhdr);
4230
4231 skb = skb_reorder_vlan_header(skb);
4232 if (unlikely(!skb))
4233 goto err_free;
4234
4235 skb_reset_network_header(skb);
4236 skb_reset_transport_header(skb);
4237 skb_reset_mac_len(skb);
4238
4239 return skb;
4240
4241err_free:
4242 kfree_skb(skb);
4243 return NULL;
4244}
4245EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004246
Jiri Pirkoe2195122014-11-19 14:05:01 +01004247int skb_ensure_writable(struct sk_buff *skb, int write_len)
4248{
4249 if (!pskb_may_pull(skb, write_len))
4250 return -ENOMEM;
4251
4252 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4253 return 0;
4254
4255 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4256}
4257EXPORT_SYMBOL(skb_ensure_writable);
4258
Jiri Pirko93515d52014-11-19 14:05:02 +01004259/* remove VLAN header from packet and update csum accordingly. */
4260static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4261{
4262 struct vlan_hdr *vhdr;
4263 unsigned int offset = skb->data - skb_mac_header(skb);
4264 int err;
4265
4266 __skb_push(skb, offset);
4267 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4268 if (unlikely(err))
4269 goto pull;
4270
4271 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4272
4273 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4274 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4275
4276 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4277 __skb_pull(skb, VLAN_HLEN);
4278
4279 vlan_set_encap_proto(skb, vhdr);
4280 skb->mac_header += VLAN_HLEN;
4281
4282 if (skb_network_offset(skb) < ETH_HLEN)
4283 skb_set_network_header(skb, ETH_HLEN);
4284
4285 skb_reset_mac_len(skb);
4286pull:
4287 __skb_pull(skb, offset);
4288
4289 return err;
4290}
4291
4292int skb_vlan_pop(struct sk_buff *skb)
4293{
4294 u16 vlan_tci;
4295 __be16 vlan_proto;
4296 int err;
4297
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004298 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004299 skb->vlan_tci = 0;
4300 } else {
4301 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4302 skb->protocol != htons(ETH_P_8021AD)) ||
4303 skb->len < VLAN_ETH_HLEN))
4304 return 0;
4305
4306 err = __skb_vlan_pop(skb, &vlan_tci);
4307 if (err)
4308 return err;
4309 }
4310 /* move next vlan tag to hw accel tag */
4311 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4312 skb->protocol != htons(ETH_P_8021AD)) ||
4313 skb->len < VLAN_ETH_HLEN))
4314 return 0;
4315
4316 vlan_proto = skb->protocol;
4317 err = __skb_vlan_pop(skb, &vlan_tci);
4318 if (unlikely(err))
4319 return err;
4320
4321 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4322 return 0;
4323}
4324EXPORT_SYMBOL(skb_vlan_pop);
4325
4326int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4327{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004328 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004329 unsigned int offset = skb->data - skb_mac_header(skb);
4330 int err;
4331
4332 /* __vlan_insert_tag expect skb->data pointing to mac header.
4333 * So change skb->data before calling it and change back to
4334 * original position later
4335 */
4336 __skb_push(skb, offset);
4337 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004338 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004339 if (err)
4340 return err;
4341 skb->protocol = skb->vlan_proto;
4342 skb->mac_len += VLAN_HLEN;
4343 __skb_pull(skb, offset);
4344
4345 if (skb->ip_summed == CHECKSUM_COMPLETE)
4346 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4347 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4348 }
4349 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4350 return 0;
4351}
4352EXPORT_SYMBOL(skb_vlan_push);
4353
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004354/**
4355 * alloc_skb_with_frags - allocate skb with page frags
4356 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004357 * @header_len: size of linear part
4358 * @data_len: needed length in frags
4359 * @max_page_order: max page order desired.
4360 * @errcode: pointer to error code if any
4361 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004362 *
4363 * This can be used to allocate a paged skb, given a maximal order for frags.
4364 */
4365struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4366 unsigned long data_len,
4367 int max_page_order,
4368 int *errcode,
4369 gfp_t gfp_mask)
4370{
4371 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4372 unsigned long chunk;
4373 struct sk_buff *skb;
4374 struct page *page;
4375 gfp_t gfp_head;
4376 int i;
4377
4378 *errcode = -EMSGSIZE;
4379 /* Note this test could be relaxed, if we succeed to allocate
4380 * high order pages...
4381 */
4382 if (npages > MAX_SKB_FRAGS)
4383 return NULL;
4384
4385 gfp_head = gfp_mask;
4386 if (gfp_head & __GFP_WAIT)
4387 gfp_head |= __GFP_REPEAT;
4388
4389 *errcode = -ENOBUFS;
4390 skb = alloc_skb(header_len, gfp_head);
4391 if (!skb)
4392 return NULL;
4393
4394 skb->truesize += npages << PAGE_SHIFT;
4395
4396 for (i = 0; npages > 0; i++) {
4397 int order = max_page_order;
4398
4399 while (order) {
4400 if (npages >= 1 << order) {
4401 page = alloc_pages(gfp_mask |
4402 __GFP_COMP |
4403 __GFP_NOWARN |
4404 __GFP_NORETRY,
4405 order);
4406 if (page)
4407 goto fill_page;
4408 /* Do not retry other high order allocations */
4409 order = 1;
4410 max_page_order = 0;
4411 }
4412 order--;
4413 }
4414 page = alloc_page(gfp_mask);
4415 if (!page)
4416 goto failure;
4417fill_page:
4418 chunk = min_t(unsigned long, data_len,
4419 PAGE_SIZE << order);
4420 skb_fill_page_desc(skb, i, page, 0, chunk);
4421 data_len -= chunk;
4422 npages -= 1 << order;
4423 }
4424 return skb;
4425
4426failure:
4427 kfree_skb(skb);
4428 return NULL;
4429}
4430EXPORT_SYMBOL(alloc_skb_with_frags);