blob: dcc0e07abf478e8422b959ece91826627cb6299a [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
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700350static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
351static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800352
353static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
354{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700355 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800356 unsigned long flags;
357 void *data;
358
359 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700360 nc = this_cpu_ptr(&netdev_alloc_cache);
361 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000362 local_irq_restore(flags);
363 return data;
364}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700365
366/**
367 * netdev_alloc_frag - allocate a page fragment
368 * @fragsz: fragment size
369 *
370 * Allocates a frag from a page for receive buffer.
371 * Uses GFP_ATOMIC allocations.
372 */
373void *netdev_alloc_frag(unsigned int fragsz)
374{
375 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
376}
Eric Dumazet6f532612012-05-18 05:12:12 +0000377EXPORT_SYMBOL(netdev_alloc_frag);
378
Alexander Duyckffde7322014-12-09 19:40:42 -0800379static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
380{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700381 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700382
383 return __alloc_page_frag(nc, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800384}
385
386void *napi_alloc_frag(unsigned int fragsz)
387{
388 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
389}
390EXPORT_SYMBOL(napi_alloc_frag);
391
Eric Dumazet6f532612012-05-18 05:12:12 +0000392/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800393 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
394 * @dev: network device to receive on
395 * @length: length to allocate
396 * @gfp_mask: get_free_pages mask, passed to alloc_skb
397 *
398 * Allocate a new &sk_buff and assign it a usage count of one. The
399 * buffer has NET_SKB_PAD headroom built in. Users should allocate
400 * the headroom they think they need without accounting for the
401 * built in space. The built in space is used for optimisations.
402 *
403 * %NULL is returned if there is no free memory.
404 */
Alexander Duyck94519802015-05-06 21:11:40 -0700405struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
406 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800407{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700408 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700409 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800410 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700411 bool pfmemalloc;
412 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800413
Alexander Duyck94519802015-05-06 21:11:40 -0700414 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800415
Alexander Duyck94519802015-05-06 21:11:40 -0700416 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
417 (gfp_mask & (__GFP_WAIT | GFP_DMA)))
418 return __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
419
420 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
421 len = SKB_DATA_ALIGN(len);
422
423 if (sk_memalloc_socks())
424 gfp_mask |= __GFP_MEMALLOC;
425
426 local_irq_save(flags);
427
428 nc = this_cpu_ptr(&netdev_alloc_cache);
429 data = __alloc_page_frag(nc, len, gfp_mask);
430 pfmemalloc = nc->pfmemalloc;
431
432 local_irq_restore(flags);
433
434 if (unlikely(!data))
435 return NULL;
436
437 skb = __build_skb(data, len);
438 if (unlikely(!skb)) {
439 put_page(virt_to_head_page(data));
440 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700441 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800442
Alexander Duyck94519802015-05-06 21:11:40 -0700443 /* use OR instead of assignment to avoid clearing of bits in mask */
444 if (pfmemalloc)
445 skb->pfmemalloc = 1;
446 skb->head_frag = 1;
447
448 skb_reserve(skb, NET_SKB_PAD);
449 skb->dev = dev;
450
Christoph Hellwig8af27452006-07-31 22:35:23 -0700451 return skb;
452}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800453EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alexander Duyckfd11a832014-12-09 19:40:49 -0800455/**
456 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
457 * @napi: napi instance this buffer was allocated for
458 * @length: length to allocate
459 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
460 *
461 * Allocate a new sk_buff for use in NAPI receive. This buffer will
462 * attempt to allocate the head from a special reserved region used
463 * only for NAPI Rx allocation. By doing this we can save several
464 * CPU cycles by avoiding having to disable and re-enable IRQs.
465 *
466 * %NULL is returned if there is no free memory.
467 */
Alexander Duyck94519802015-05-06 21:11:40 -0700468struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
469 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800470{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700471 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800472 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700473 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800474
Alexander Duyck94519802015-05-06 21:11:40 -0700475 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800476
Alexander Duyck94519802015-05-06 21:11:40 -0700477 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
478 (gfp_mask & (__GFP_WAIT | GFP_DMA)))
479 return __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
480
481 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
482 len = SKB_DATA_ALIGN(len);
483
484 if (sk_memalloc_socks())
485 gfp_mask |= __GFP_MEMALLOC;
486
487 data = __alloc_page_frag(nc, len, gfp_mask);
488 if (unlikely(!data))
489 return NULL;
490
491 skb = __build_skb(data, len);
492 if (unlikely(!skb)) {
493 put_page(virt_to_head_page(data));
494 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800495 }
496
Alexander Duyck94519802015-05-06 21:11:40 -0700497 /* use OR instead of assignment to avoid clearing of bits in mask */
498 if (nc->pfmemalloc)
499 skb->pfmemalloc = 1;
500 skb->head_frag = 1;
501
502 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
503 skb->dev = napi->dev;
504
Alexander Duyckfd11a832014-12-09 19:40:49 -0800505 return skb;
506}
507EXPORT_SYMBOL(__napi_alloc_skb);
508
Peter Zijlstra654bed12008-10-07 14:22:33 -0700509void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000510 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700511{
512 skb_fill_page_desc(skb, i, page, off, size);
513 skb->len += size;
514 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000515 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700516}
517EXPORT_SYMBOL(skb_add_rx_frag);
518
Jason Wangf8e617e2013-11-01 14:07:47 +0800519void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
520 unsigned int truesize)
521{
522 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
523
524 skb_frag_size_add(frag, size);
525 skb->len += size;
526 skb->data_len += size;
527 skb->truesize += truesize;
528}
529EXPORT_SYMBOL(skb_coalesce_rx_frag);
530
Herbert Xu27b437c2006-07-13 19:26:39 -0700531static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700533 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700534 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Herbert Xu27b437c2006-07-13 19:26:39 -0700537static inline void skb_drop_fraglist(struct sk_buff *skb)
538{
539 skb_drop_list(&skb_shinfo(skb)->frag_list);
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void skb_clone_fraglist(struct sk_buff *skb)
543{
544 struct sk_buff *list;
545
David S. Millerfbb398a2009-06-09 00:18:59 -0700546 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 skb_get(list);
548}
549
Eric Dumazetd3836f22012-04-27 00:33:38 +0000550static void skb_free_head(struct sk_buff *skb)
551{
552 if (skb->head_frag)
553 put_page(virt_to_head_page(skb->head));
554 else
555 kfree(skb->head);
556}
557
Adrian Bunk5bba1712006-06-29 13:02:35 -0700558static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Eric Dumazetff04a772014-09-23 18:39:30 -0700560 struct skb_shared_info *shinfo = skb_shinfo(skb);
561 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Eric Dumazetff04a772014-09-23 18:39:30 -0700563 if (skb->cloned &&
564 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
565 &shinfo->dataref))
566 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000567
Eric Dumazetff04a772014-09-23 18:39:30 -0700568 for (i = 0; i < shinfo->nr_frags; i++)
569 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000570
Eric Dumazetff04a772014-09-23 18:39:30 -0700571 /*
572 * If skb buf is from userspace, we need to notify the caller
573 * the lower device DMA has done;
574 */
575 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
576 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Eric Dumazetff04a772014-09-23 18:39:30 -0700578 uarg = shinfo->destructor_arg;
579 if (uarg->callback)
580 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700582
583 if (shinfo->frag_list)
584 kfree_skb_list(shinfo->frag_list);
585
586 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Free an skbuff by memory without cleaning the state.
591 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800592static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700594 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700595
David S. Millerd179cd12005-08-17 14:57:30 -0700596 switch (skb->fclone) {
597 case SKB_FCLONE_UNAVAILABLE:
598 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800599 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700600
601 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700602 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800603
604 /* We usually free the clone (TX completion) before original skb
605 * This test would have no chance to be true for the clone,
606 * while here, branch prediction will be good.
607 */
608 if (atomic_read(&fclones->fclone_ref) == 1)
609 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700610 break;
611
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800612 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700613 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700614 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700615 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800616 if (!atomic_dec_and_test(&fclones->fclone_ref))
617 return;
618fastpath:
619 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700622static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Eric Dumazetadf30902009-06-02 05:19:30 +0000624 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#ifdef CONFIG_XFRM
626 secpath_put(skb->sp);
627#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700628 if (skb->destructor) {
629 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 skb->destructor(skb);
631 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000632#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700633 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100634#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200635#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 nf_bridge_put(skb->nf_bridge);
637#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700638}
639
640/* Free everything but the sk_buff shell. */
641static void skb_release_all(struct sk_buff *skb)
642{
643 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000644 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000645 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Herbert Xu2d4baff2007-11-26 23:11:19 +0800648/**
649 * __kfree_skb - private function
650 * @skb: buffer
651 *
652 * Free an sk_buff. Release anything attached to the buffer.
653 * Clean the state. This is an internal helper function. Users should
654 * always call kfree_skb
655 */
656
657void __kfree_skb(struct sk_buff *skb)
658{
659 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 kfree_skbmem(skb);
661}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800662EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800665 * kfree_skb - free an sk_buff
666 * @skb: buffer to free
667 *
668 * Drop a reference to the buffer and free it if the usage count has
669 * hit zero.
670 */
671void kfree_skb(struct sk_buff *skb)
672{
673 if (unlikely(!skb))
674 return;
675 if (likely(atomic_read(&skb->users) == 1))
676 smp_rmb();
677 else if (likely(!atomic_dec_and_test(&skb->users)))
678 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000679 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800680 __kfree_skb(skb);
681}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800682EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800683
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700684void kfree_skb_list(struct sk_buff *segs)
685{
686 while (segs) {
687 struct sk_buff *next = segs->next;
688
689 kfree_skb(segs);
690 segs = next;
691 }
692}
693EXPORT_SYMBOL(kfree_skb_list);
694
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700695/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000696 * skb_tx_error - report an sk_buff xmit error
697 * @skb: buffer that triggered an error
698 *
699 * Report xmit error if a device callback is tracking this skb.
700 * skb must be freed afterwards.
701 */
702void skb_tx_error(struct sk_buff *skb)
703{
704 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
705 struct ubuf_info *uarg;
706
707 uarg = skb_shinfo(skb)->destructor_arg;
708 if (uarg->callback)
709 uarg->callback(uarg, false);
710 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
711 }
712}
713EXPORT_SYMBOL(skb_tx_error);
714
715/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000716 * consume_skb - free an skbuff
717 * @skb: buffer to free
718 *
719 * Drop a ref to the buffer and free it if the usage count has hit zero
720 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
721 * is being dropped after a failure and notes that
722 */
723void consume_skb(struct sk_buff *skb)
724{
725 if (unlikely(!skb))
726 return;
727 if (likely(atomic_read(&skb->users) == 1))
728 smp_rmb();
729 else if (likely(!atomic_dec_and_test(&skb->users)))
730 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900731 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000732 __kfree_skb(skb);
733}
734EXPORT_SYMBOL(consume_skb);
735
Eric Dumazetb1937222014-09-28 22:18:47 -0700736/* Make sure a field is enclosed inside headers_start/headers_end section */
737#define CHECK_SKB_FIELD(field) \
738 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
739 offsetof(struct sk_buff, headers_start)); \
740 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
741 offsetof(struct sk_buff, headers_end)); \
742
Herbert Xudec18812007-10-14 00:37:30 -0700743static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
744{
745 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700746 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700747 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700748 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000749 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700750#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700751 new->sp = secpath_get(old->sp);
752#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700753 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700754
Eric Dumazetb1937222014-09-28 22:18:47 -0700755 /* Note : this field could be in headers_start/headers_end section
756 * It is not yet because we do not want to have a 16 bit hole
757 */
758 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300759
Eric Dumazetb1937222014-09-28 22:18:47 -0700760 memcpy(&new->headers_start, &old->headers_start,
761 offsetof(struct sk_buff, headers_end) -
762 offsetof(struct sk_buff, headers_start));
763 CHECK_SKB_FIELD(protocol);
764 CHECK_SKB_FIELD(csum);
765 CHECK_SKB_FIELD(hash);
766 CHECK_SKB_FIELD(priority);
767 CHECK_SKB_FIELD(skb_iif);
768 CHECK_SKB_FIELD(vlan_proto);
769 CHECK_SKB_FIELD(vlan_tci);
770 CHECK_SKB_FIELD(transport_header);
771 CHECK_SKB_FIELD(network_header);
772 CHECK_SKB_FIELD(mac_header);
773 CHECK_SKB_FIELD(inner_protocol);
774 CHECK_SKB_FIELD(inner_transport_header);
775 CHECK_SKB_FIELD(inner_network_header);
776 CHECK_SKB_FIELD(inner_mac_header);
777 CHECK_SKB_FIELD(mark);
778#ifdef CONFIG_NETWORK_SECMARK
779 CHECK_SKB_FIELD(secmark);
780#endif
Cong Wange0d10952013-08-01 11:10:25 +0800781#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700782 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300783#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800784#ifdef CONFIG_XPS
785 CHECK_SKB_FIELD(sender_cpu);
786#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700787#ifdef CONFIG_NET_SCHED
788 CHECK_SKB_FIELD(tc_index);
789#ifdef CONFIG_NET_CLS_ACT
790 CHECK_SKB_FIELD(tc_verd);
791#endif
792#endif
793
Herbert Xudec18812007-10-14 00:37:30 -0700794}
795
Herbert Xu82c49a32009-05-22 22:11:37 +0000796/*
797 * You should not add any new code to this function. Add it to
798 * __copy_skb_header above instead.
799 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700800static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#define C(x) n->x = skb->x
803
804 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700806 __copy_skb_header(n, skb);
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 C(len);
809 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700810 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700811 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800812 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 C(tail);
816 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800817 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000818 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800819 C(data);
820 C(truesize);
821 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 atomic_inc(&(skb_shinfo(skb)->dataref));
824 skb->cloned = 1;
825
826 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700827#undef C
828}
829
830/**
831 * skb_morph - morph one skb into another
832 * @dst: the skb to receive the contents
833 * @src: the skb to supply the contents
834 *
835 * This is identical to skb_clone except that the target skb is
836 * supplied by the user.
837 *
838 * The target skb is returned upon exit.
839 */
840struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
841{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800842 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700843 return __skb_clone(dst, src);
844}
845EXPORT_SYMBOL_GPL(skb_morph);
846
Ben Hutchings2c530402012-07-10 10:55:09 +0000847/**
848 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000849 * @skb: the skb to modify
850 * @gfp_mask: allocation priority
851 *
852 * This must be called on SKBTX_DEV_ZEROCOPY skb.
853 * It will copy all frags into kernel and drop the reference
854 * to userspace pages.
855 *
856 * If this function is called from an interrupt gfp_mask() must be
857 * %GFP_ATOMIC.
858 *
859 * Returns 0 on success or a negative error code on failure
860 * to allocate kernel memory to copy to.
861 */
862int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000863{
864 int i;
865 int num_frags = skb_shinfo(skb)->nr_frags;
866 struct page *page, *head = NULL;
867 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
868
869 for (i = 0; i < num_frags; i++) {
870 u8 *vaddr;
871 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
872
Krishna Kumar02756ed2012-07-17 02:05:29 +0000873 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000874 if (!page) {
875 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900876 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000877 put_page(head);
878 head = next;
879 }
880 return -ENOMEM;
881 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200882 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000883 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000884 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200885 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900886 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000887 head = page;
888 }
889
890 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000891 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000892 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000893
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000894 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000895
896 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000897 for (i = num_frags - 1; i >= 0; i--) {
898 __skb_fill_page_desc(skb, i, head, 0,
899 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900900 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000901 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000902
903 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000904 return 0;
905}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000906EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000907
Herbert Xue0053ec2007-10-14 00:37:52 -0700908/**
909 * skb_clone - duplicate an sk_buff
910 * @skb: buffer to clone
911 * @gfp_mask: allocation priority
912 *
913 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
914 * copies share the same packet data but not structure. The new
915 * buffer has a reference count of 1. If the allocation fails the
916 * function returns %NULL otherwise the new buffer is returned.
917 *
918 * If this function is called from an interrupt gfp_mask() must be
919 * %GFP_ATOMIC.
920 */
921
922struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
923{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700924 struct sk_buff_fclones *fclones = container_of(skb,
925 struct sk_buff_fclones,
926 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800927 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700928
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000929 if (skb_orphan_frags(skb, gfp_mask))
930 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000931
Herbert Xue0053ec2007-10-14 00:37:52 -0700932 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800933 atomic_read(&fclones->fclone_ref) == 1) {
934 n = &fclones->skb2;
935 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700936 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700937 if (skb_pfmemalloc(skb))
938 gfp_mask |= __GFP_MEMALLOC;
939
Herbert Xue0053ec2007-10-14 00:37:52 -0700940 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
941 if (!n)
942 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200943
944 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700945 n->fclone = SKB_FCLONE_UNAVAILABLE;
946 }
947
948 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800950EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000952static void skb_headers_offset_update(struct sk_buff *skb, int off)
953{
Eric Dumazet030737b2013-10-19 11:42:54 -0700954 /* Only adjust this if it actually is csum_start rather than csum */
955 if (skb->ip_summed == CHECKSUM_PARTIAL)
956 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000957 /* {transport,network,mac}_header and tail are relative to skb->head */
958 skb->transport_header += off;
959 skb->network_header += off;
960 if (skb_mac_header_was_set(skb))
961 skb->mac_header += off;
962 skb->inner_transport_header += off;
963 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000964 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
968{
Herbert Xudec18812007-10-14 00:37:30 -0700969 __copy_skb_header(new, old);
970
Herbert Xu79671682006-06-22 02:40:14 -0700971 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
972 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
973 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Mel Gormanc93bdd02012-07-31 16:44:19 -0700976static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
977{
978 if (skb_pfmemalloc(skb))
979 return SKB_ALLOC_RX;
980 return 0;
981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/**
984 * skb_copy - create private copy of an sk_buff
985 * @skb: buffer to copy
986 * @gfp_mask: allocation priority
987 *
988 * Make a copy of both an &sk_buff and its data. This is used when the
989 * caller wishes to modify the data and needs a private copy of the
990 * data to alter. Returns %NULL on failure or the pointer to the buffer
991 * on success. The returned buffer has a reference count of 1.
992 *
993 * As by-product this function converts non-linear &sk_buff to linear
994 * one, so that &sk_buff becomes completely private and caller is allowed
995 * to modify all the data of returned buffer. This means that this
996 * function is not recommended for use in circumstances when only
997 * header is going to be modified. Use pskb_copy() instead.
998 */
999
Al Virodd0fc662005-10-07 07:46:04 +01001000struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001002 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001003 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001004 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1005 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!n)
1008 return NULL;
1009
1010 /* Set the data pointer */
1011 skb_reserve(n, headerlen);
1012 /* Set the tail pointer and length */
1013 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1016 BUG();
1017
1018 copy_skb_header(n, skb);
1019 return n;
1020}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001021EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001024 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001026 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001028 * @fclone: if true allocate the copy of the skb from the fclone
1029 * cache instead of the head cache; it is recommended to set this
1030 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *
1032 * Make a copy of both an &sk_buff and part of its data, located
1033 * in header. Fragmented data remain shared. This is used when
1034 * the caller wishes to modify only header of &sk_buff and needs
1035 * private copy of the header to alter. Returns %NULL on failure
1036 * or the pointer to the buffer on success.
1037 * The returned buffer has a reference count of 1.
1038 */
1039
Octavian Purdilabad93e92014-06-12 01:36:26 +03001040struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1041 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Eric Dumazet117632e2011-12-03 21:39:53 +00001043 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001044 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1045 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!n)
1048 goto out;
1049
1050 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001051 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* Set the tail pointer and length */
1053 skb_put(n, skb_headlen(skb));
1054 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001055 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Herbert Xu25f484a2006-11-07 14:57:15 -08001057 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 n->data_len = skb->data_len;
1059 n->len = skb->len;
1060
1061 if (skb_shinfo(skb)->nr_frags) {
1062 int i;
1063
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001064 if (skb_orphan_frags(skb, gfp_mask)) {
1065 kfree_skb(n);
1066 n = NULL;
1067 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1070 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001071 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 skb_shinfo(n)->nr_frags = i;
1074 }
1075
David S. Miller21dc3302010-08-23 00:13:46 -07001076 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1078 skb_clone_fraglist(n);
1079 }
1080
1081 copy_skb_header(n, skb);
1082out:
1083 return n;
1084}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001085EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087/**
1088 * pskb_expand_head - reallocate header of &sk_buff
1089 * @skb: buffer to reallocate
1090 * @nhead: room to add at head
1091 * @ntail: room to add at tail
1092 * @gfp_mask: allocation priority
1093 *
Mathias Krausebc323832013-11-07 14:18:26 +01001094 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1095 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * reference count of 1. Returns zero in the case of success or error,
1097 * if expansion failed. In the last case, &sk_buff is not changed.
1098 *
1099 * All the pointers pointing into skb header may change and must be
1100 * reloaded after call to this function.
1101 */
1102
Victor Fusco86a76ca2005-07-08 14:57:47 -07001103int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001104 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 int i;
1107 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001108 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 long off;
1110
Herbert Xu4edd87a2008-10-01 07:09:38 -07001111 BUG_ON(nhead < 0);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (skb_shared(skb))
1114 BUG();
1115
1116 size = SKB_DATA_ALIGN(size);
1117
Mel Gormanc93bdd02012-07-31 16:44:19 -07001118 if (skb_pfmemalloc(skb))
1119 gfp_mask |= __GFP_MEMALLOC;
1120 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1121 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!data)
1123 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001124 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001127 * optimized for the cases when header is void.
1128 */
1129 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1130
1131 memcpy((struct skb_shared_info *)(data + size),
1132 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001133 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Alexander Duyck3e245912012-05-04 14:26:51 +00001135 /*
1136 * if shinfo is shared we must drop the old head gracefully, but if it
1137 * is not we can just drop the old head and let the existing refcount
1138 * be since all we did is relocate the values
1139 */
1140 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001141 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001142 if (skb_orphan_frags(skb, gfp_mask))
1143 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001144 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001145 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Eric Dumazet1fd63042010-09-02 23:09:32 +00001147 if (skb_has_frag_list(skb))
1148 skb_clone_fraglist(skb);
1149
1150 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001151 } else {
1152 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 off = (data + nhead) - skb->head;
1155
1156 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001157 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001159#ifdef NET_SKBUFF_DATA_USES_OFFSET
1160 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001161 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001162#else
1163 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001164#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001165 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001166 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001168 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 skb->nohdr = 0;
1170 atomic_set(&skb_shinfo(skb)->dataref, 1);
1171 return 0;
1172
Shirley Maa6686f22011-07-06 12:22:12 +00001173nofrags:
1174 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175nodata:
1176 return -ENOMEM;
1177}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001178EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180/* Make private copy of skb with writable head and some headroom */
1181
1182struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1183{
1184 struct sk_buff *skb2;
1185 int delta = headroom - skb_headroom(skb);
1186
1187 if (delta <= 0)
1188 skb2 = pskb_copy(skb, GFP_ATOMIC);
1189 else {
1190 skb2 = skb_clone(skb, GFP_ATOMIC);
1191 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1192 GFP_ATOMIC)) {
1193 kfree_skb(skb2);
1194 skb2 = NULL;
1195 }
1196 }
1197 return skb2;
1198}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001199EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201/**
1202 * skb_copy_expand - copy and expand sk_buff
1203 * @skb: buffer to copy
1204 * @newheadroom: new free bytes at head
1205 * @newtailroom: new free bytes at tail
1206 * @gfp_mask: allocation priority
1207 *
1208 * Make a copy of both an &sk_buff and its data and while doing so
1209 * allocate additional space.
1210 *
1211 * This is used when the caller wishes to modify the data and needs a
1212 * private copy of the data to alter as well as more space for new fields.
1213 * Returns %NULL on failure or the pointer to the buffer
1214 * on success. The returned buffer has a reference count of 1.
1215 *
1216 * You must pass %GFP_ATOMIC as the allocation priority if this function
1217 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001220 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001221 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 /*
1224 * Allocate the copy buffer
1225 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001226 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1227 gfp_mask, skb_alloc_rx_flag(skb),
1228 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001229 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int head_copy_len, head_copy_off;
1231
1232 if (!n)
1233 return NULL;
1234
1235 skb_reserve(n, newheadroom);
1236
1237 /* Set the tail pointer and length */
1238 skb_put(n, skb->len);
1239
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001240 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 head_copy_off = 0;
1242 if (newheadroom <= head_copy_len)
1243 head_copy_len = newheadroom;
1244 else
1245 head_copy_off = newheadroom - head_copy_len;
1246
1247 /* Copy the linear header and data. */
1248 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1249 skb->len + head_copy_len))
1250 BUG();
1251
1252 copy_skb_header(n, skb);
1253
Eric Dumazet030737b2013-10-19 11:42:54 -07001254 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return n;
1257}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001258EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/**
1261 * skb_pad - zero pad the tail of an skb
1262 * @skb: buffer to pad
1263 * @pad: space to pad
1264 *
1265 * Ensure that a buffer is followed by a padding area that is zero
1266 * filled. Used by network drivers which may DMA or transfer data
1267 * beyond the buffer end onto the wire.
1268 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001269 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001271
Herbert Xu5b057c62006-06-23 02:06:41 -07001272int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Herbert Xu5b057c62006-06-23 02:06:41 -07001274 int err;
1275 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001278 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001282
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001283 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001284 if (likely(skb_cloned(skb) || ntail > 0)) {
1285 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1286 if (unlikely(err))
1287 goto free_skb;
1288 }
1289
1290 /* FIXME: The use of this function with non-linear skb's really needs
1291 * to be audited.
1292 */
1293 err = skb_linearize(skb);
1294 if (unlikely(err))
1295 goto free_skb;
1296
1297 memset(skb->data + skb->len, 0, pad);
1298 return 0;
1299
1300free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001302 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001303}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001304EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001305
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001306/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001307 * pskb_put - add data to the tail of a potentially fragmented buffer
1308 * @skb: start of the buffer to use
1309 * @tail: tail fragment of the buffer to use
1310 * @len: amount of data to add
1311 *
1312 * This function extends the used data area of the potentially
1313 * fragmented buffer. @tail must be the last fragment of @skb -- or
1314 * @skb itself. If this would exceed the total buffer size the kernel
1315 * will panic. A pointer to the first byte of the extra data is
1316 * returned.
1317 */
1318
1319unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1320{
1321 if (tail != skb) {
1322 skb->data_len += len;
1323 skb->len += len;
1324 }
1325 return skb_put(tail, len);
1326}
1327EXPORT_SYMBOL_GPL(pskb_put);
1328
1329/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001330 * skb_put - add data to a buffer
1331 * @skb: buffer to use
1332 * @len: amount of data to add
1333 *
1334 * This function extends the used data area of the buffer. If this would
1335 * exceed the total buffer size the kernel will panic. A pointer to the
1336 * first byte of the extra data is returned.
1337 */
1338unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1339{
1340 unsigned char *tmp = skb_tail_pointer(skb);
1341 SKB_LINEAR_ASSERT(skb);
1342 skb->tail += len;
1343 skb->len += len;
1344 if (unlikely(skb->tail > skb->end))
1345 skb_over_panic(skb, len, __builtin_return_address(0));
1346 return tmp;
1347}
1348EXPORT_SYMBOL(skb_put);
1349
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001350/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001351 * skb_push - add data to the start of a buffer
1352 * @skb: buffer to use
1353 * @len: amount of data to add
1354 *
1355 * This function extends the used data area of the buffer at the buffer
1356 * start. If this would exceed the total buffer headroom the kernel will
1357 * panic. A pointer to the first byte of the extra data is returned.
1358 */
1359unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1360{
1361 skb->data -= len;
1362 skb->len += len;
1363 if (unlikely(skb->data<skb->head))
1364 skb_under_panic(skb, len, __builtin_return_address(0));
1365 return skb->data;
1366}
1367EXPORT_SYMBOL(skb_push);
1368
1369/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001370 * skb_pull - remove data from the start of a buffer
1371 * @skb: buffer to use
1372 * @len: amount of data to remove
1373 *
1374 * This function removes data from the start of a buffer, returning
1375 * the memory to the headroom. A pointer to the next data in the buffer
1376 * is returned. Once the data has been pulled future pushes will overwrite
1377 * the old data.
1378 */
1379unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1380{
David S. Miller47d29642010-05-02 02:21:44 -07001381 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001382}
1383EXPORT_SYMBOL(skb_pull);
1384
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001385/**
1386 * skb_trim - remove end from a buffer
1387 * @skb: buffer to alter
1388 * @len: new length
1389 *
1390 * Cut the length of a buffer down by removing data from the tail. If
1391 * the buffer is already under the length specified it is not modified.
1392 * The skb must be linear.
1393 */
1394void skb_trim(struct sk_buff *skb, unsigned int len)
1395{
1396 if (skb->len > len)
1397 __skb_trim(skb, len);
1398}
1399EXPORT_SYMBOL(skb_trim);
1400
Herbert Xu3cc0e872006-06-09 16:13:38 -07001401/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
1403
Herbert Xu3cc0e872006-06-09 16:13:38 -07001404int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Herbert Xu27b437c2006-07-13 19:26:39 -07001406 struct sk_buff **fragp;
1407 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int offset = skb_headlen(skb);
1409 int nfrags = skb_shinfo(skb)->nr_frags;
1410 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001411 int err;
1412
1413 if (skb_cloned(skb) &&
1414 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001417 i = 0;
1418 if (offset >= len)
1419 goto drop_pages;
1420
1421 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001422 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001423
1424 if (end < len) {
1425 offset = end;
1426 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001428
Eric Dumazet9e903e02011-10-18 21:00:24 +00001429 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001430
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001431drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001432 skb_shinfo(skb)->nr_frags = i;
1433
1434 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001435 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001436
David S. Miller21dc3302010-08-23 00:13:46 -07001437 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001438 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001439 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Herbert Xu27b437c2006-07-13 19:26:39 -07001442 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1443 fragp = &frag->next) {
1444 int end = offset + frag->len;
1445
1446 if (skb_shared(frag)) {
1447 struct sk_buff *nfrag;
1448
1449 nfrag = skb_clone(frag, GFP_ATOMIC);
1450 if (unlikely(!nfrag))
1451 return -ENOMEM;
1452
1453 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001454 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001455 frag = nfrag;
1456 *fragp = frag;
1457 }
1458
1459 if (end < len) {
1460 offset = end;
1461 continue;
1462 }
1463
1464 if (end > len &&
1465 unlikely((err = pskb_trim(frag, len - offset))))
1466 return err;
1467
1468 if (frag->next)
1469 skb_drop_list(&frag->next);
1470 break;
1471 }
1472
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001473done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001474 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 skb->data_len -= skb->len - len;
1476 skb->len = len;
1477 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001478 skb->len = len;
1479 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001480 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
1483 return 0;
1484}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001485EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487/**
1488 * __pskb_pull_tail - advance tail of skb header
1489 * @skb: buffer to reallocate
1490 * @delta: number of bytes to advance tail
1491 *
1492 * The function makes a sense only on a fragmented &sk_buff,
1493 * it expands header moving its tail forward and copying necessary
1494 * data from fragmented part.
1495 *
1496 * &sk_buff MUST have reference count of 1.
1497 *
1498 * Returns %NULL (and &sk_buff does not change) if pull failed
1499 * or value of new tail of skb in the case of success.
1500 *
1501 * All the pointers pointing into skb header may change and must be
1502 * reloaded after call to this function.
1503 */
1504
1505/* Moves tail of skb head forward, copying data from fragmented part,
1506 * when it is necessary.
1507 * 1. It may fail due to malloc failure.
1508 * 2. It may change skb pointers.
1509 *
1510 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1511 */
1512unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1513{
1514 /* If skb has not enough free space at tail, get new one
1515 * plus 128 bytes for future expansions. If we have enough
1516 * room at tail, reallocate without expansion only if skb is cloned.
1517 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001518 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (eat > 0 || skb_cloned(skb)) {
1521 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1522 GFP_ATOMIC))
1523 return NULL;
1524 }
1525
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001526 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 BUG();
1528
1529 /* Optimization: no fragments, no reasons to preestimate
1530 * size of pulled pages. Superb.
1531 */
David S. Miller21dc3302010-08-23 00:13:46 -07001532 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 goto pull_pages;
1534
1535 /* Estimate size of pulled pages. */
1536 eat = delta;
1537 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001538 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1539
1540 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001542 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
1545 /* If we need update frag list, we are in troubles.
1546 * Certainly, it possible to add an offset to skb data,
1547 * but taking into account that pulling is expected to
1548 * be very rare operation, it is worth to fight against
1549 * further bloating skb head and crucify ourselves here instead.
1550 * Pure masohism, indeed. 8)8)
1551 */
1552 if (eat) {
1553 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1554 struct sk_buff *clone = NULL;
1555 struct sk_buff *insp = NULL;
1556
1557 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001558 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (list->len <= eat) {
1561 /* Eaten as whole. */
1562 eat -= list->len;
1563 list = list->next;
1564 insp = list;
1565 } else {
1566 /* Eaten partially. */
1567
1568 if (skb_shared(list)) {
1569 /* Sucks! We need to fork list. :-( */
1570 clone = skb_clone(list, GFP_ATOMIC);
1571 if (!clone)
1572 return NULL;
1573 insp = list->next;
1574 list = clone;
1575 } else {
1576 /* This may be pulled without
1577 * problems. */
1578 insp = list;
1579 }
1580 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001581 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return NULL;
1583 }
1584 break;
1585 }
1586 } while (eat);
1587
1588 /* Free pulled out fragments. */
1589 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1590 skb_shinfo(skb)->frag_list = list->next;
1591 kfree_skb(list);
1592 }
1593 /* And insert new clone at head. */
1594 if (clone) {
1595 clone->next = list;
1596 skb_shinfo(skb)->frag_list = clone;
1597 }
1598 }
1599 /* Success! Now we may commit changes to skb data. */
1600
1601pull_pages:
1602 eat = delta;
1603 k = 0;
1604 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001605 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1606
1607 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001608 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001609 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 } else {
1611 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1612 if (eat) {
1613 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001614 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 eat = 0;
1616 }
1617 k++;
1618 }
1619 }
1620 skb_shinfo(skb)->nr_frags = k;
1621
1622 skb->tail += delta;
1623 skb->data_len -= delta;
1624
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001625 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001627EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Eric Dumazet22019b12011-07-29 18:37:31 +00001629/**
1630 * skb_copy_bits - copy bits from skb to kernel buffer
1631 * @skb: source skb
1632 * @offset: offset in source
1633 * @to: destination buffer
1634 * @len: number of bytes to copy
1635 *
1636 * Copy the specified number of bytes from the source skb to the
1637 * destination buffer.
1638 *
1639 * CAUTION ! :
1640 * If its prototype is ever changed,
1641 * check arch/{*}/net/{*}.S files,
1642 * since it is called from BPF assembly code.
1643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1645{
David S. Miller1a028e52007-04-27 15:21:23 -07001646 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001647 struct sk_buff *frag_iter;
1648 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (offset > (int)skb->len - len)
1651 goto fault;
1652
1653 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001654 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (copy > len)
1656 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001657 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if ((len -= copy) == 0)
1659 return 0;
1660 offset += copy;
1661 to += copy;
1662 }
1663
1664 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001665 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001666 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001668 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001669
Eric Dumazet51c56b02012-04-05 11:35:15 +02001670 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if ((copy = end - offset) > 0) {
1672 u8 *vaddr;
1673
1674 if (copy > len)
1675 copy = len;
1676
Eric Dumazet51c56b02012-04-05 11:35:15 +02001677 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001679 vaddr + f->page_offset + offset - start,
1680 copy);
1681 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if ((len -= copy) == 0)
1684 return 0;
1685 offset += copy;
1686 to += copy;
1687 }
David S. Miller1a028e52007-04-27 15:21:23 -07001688 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
David S. Millerfbb398a2009-06-09 00:18:59 -07001691 skb_walk_frags(skb, frag_iter) {
1692 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
David S. Millerfbb398a2009-06-09 00:18:59 -07001694 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David S. Millerfbb398a2009-06-09 00:18:59 -07001696 end = start + frag_iter->len;
1697 if ((copy = end - offset) > 0) {
1698 if (copy > len)
1699 copy = len;
1700 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1701 goto fault;
1702 if ((len -= copy) == 0)
1703 return 0;
1704 offset += copy;
1705 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001707 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Shirley Maa6686f22011-07-06 12:22:12 +00001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (!len)
1711 return 0;
1712
1713fault:
1714 return -EFAULT;
1715}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001716EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Jens Axboe9c55e012007-11-06 23:30:13 -08001718/*
1719 * Callback from splice_to_pipe(), if we need to release some pages
1720 * at the end of the spd in case we error'ed out in filling the pipe.
1721 */
1722static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1723{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001724 put_page(spd->pages[i]);
1725}
Jens Axboe9c55e012007-11-06 23:30:13 -08001726
David S. Millera108d5f2012-04-23 23:06:11 -04001727static struct page *linear_to_page(struct page *page, unsigned int *len,
1728 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001729 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001730{
Eric Dumazet5640f762012-09-23 23:04:42 +00001731 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001732
Eric Dumazet5640f762012-09-23 23:04:42 +00001733 if (!sk_page_frag_refill(sk, pfrag))
1734 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001735
Eric Dumazet5640f762012-09-23 23:04:42 +00001736 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001737
Eric Dumazet5640f762012-09-23 23:04:42 +00001738 memcpy(page_address(pfrag->page) + pfrag->offset,
1739 page_address(page) + *offset, *len);
1740 *offset = pfrag->offset;
1741 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001742
Eric Dumazet5640f762012-09-23 23:04:42 +00001743 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001744}
1745
Eric Dumazet41c73a02012-04-22 12:26:16 +00001746static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1747 struct page *page,
1748 unsigned int offset)
1749{
1750 return spd->nr_pages &&
1751 spd->pages[spd->nr_pages - 1] == page &&
1752 (spd->partial[spd->nr_pages - 1].offset +
1753 spd->partial[spd->nr_pages - 1].len == offset);
1754}
1755
Jens Axboe9c55e012007-11-06 23:30:13 -08001756/*
1757 * Fill page/offset/length into spd, if it can hold more pages.
1758 */
David S. Millera108d5f2012-04-23 23:06:11 -04001759static bool spd_fill_page(struct splice_pipe_desc *spd,
1760 struct pipe_inode_info *pipe, struct page *page,
1761 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001762 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001763 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001764{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001765 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001766 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001767
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001768 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001769 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001770 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001771 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001772 }
1773 if (spd_can_coalesce(spd, page, offset)) {
1774 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001775 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001776 }
1777 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001778 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001779 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001780 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001781 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001782
David S. Millera108d5f2012-04-23 23:06:11 -04001783 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001784}
1785
David S. Millera108d5f2012-04-23 23:06:11 -04001786static bool __splice_segment(struct page *page, unsigned int poff,
1787 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001788 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001789 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001790 struct sock *sk,
1791 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001792{
1793 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001794 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001795
1796 /* skip this segment if already processed */
1797 if (*off >= plen) {
1798 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001799 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001800 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001801
Octavian Purdila2870c432008-07-15 00:49:11 -07001802 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001803 poff += *off;
1804 plen -= *off;
1805 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001806
Eric Dumazet18aafc62013-01-11 14:46:37 +00001807 do {
1808 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001809
Eric Dumazet18aafc62013-01-11 14:46:37 +00001810 if (spd_fill_page(spd, pipe, page, &flen, poff,
1811 linear, sk))
1812 return true;
1813 poff += flen;
1814 plen -= flen;
1815 *len -= flen;
1816 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001817
David S. Millera108d5f2012-04-23 23:06:11 -04001818 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001819}
1820
1821/*
David S. Millera108d5f2012-04-23 23:06:11 -04001822 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001823 * pipe is full or if we already spliced the requested length.
1824 */
David S. Millera108d5f2012-04-23 23:06:11 -04001825static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1826 unsigned int *offset, unsigned int *len,
1827 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001828{
1829 int seg;
1830
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001831 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001832 * If skb->head_frag is set, this 'linear' part is backed by a
1833 * fragment, and if the head is not shared with any clones then
1834 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001835 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001836 if (__splice_segment(virt_to_page(skb->data),
1837 (unsigned long) skb->data & (PAGE_SIZE - 1),
1838 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001839 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001840 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001841 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001842 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001843
1844 /*
1845 * then map the fragments
1846 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001847 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1848 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1849
Ian Campbellea2ab692011-08-22 23:44:58 +00001850 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001851 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001852 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001853 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001854 }
1855
David S. Millera108d5f2012-04-23 23:06:11 -04001856 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001857}
1858
1859/*
1860 * Map data from the skb to a pipe. Should handle both the linear part,
1861 * the fragments, and the frag list. It does NOT handle frag lists within
1862 * the frag list, if such a thing exists. We'd probably need to recurse to
1863 * handle that cleanly.
1864 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001865int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001866 struct pipe_inode_info *pipe, unsigned int tlen,
1867 unsigned int flags)
1868{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001869 struct partial_page partial[MAX_SKB_FRAGS];
1870 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001871 struct splice_pipe_desc spd = {
1872 .pages = pages,
1873 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001874 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001875 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001876 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001877 .spd_release = sock_spd_release,
1878 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001879 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001880 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001881 int ret = 0;
1882
Jens Axboe9c55e012007-11-06 23:30:13 -08001883 /*
1884 * __skb_splice_bits() only fails if the output has no room left,
1885 * so no point in going over the frag_list for the error case.
1886 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001887 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001888 goto done;
1889 else if (!tlen)
1890 goto done;
1891
1892 /*
1893 * now see if we have a frag_list to map
1894 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001895 skb_walk_frags(skb, frag_iter) {
1896 if (!tlen)
1897 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001898 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001899 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001900 }
1901
1902done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001903 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001904 /*
1905 * Drop the socket lock, otherwise we have reverse
1906 * locking dependencies between sk_lock and i_mutex
1907 * here as compared to sendfile(). We enter here
1908 * with the socket lock held, and splice_to_pipe() will
1909 * grab the pipe inode lock. For sendfile() emulation,
1910 * we call into ->sendpage() with the i_mutex lock held
1911 * and networking will grab the socket lock.
1912 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001913 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001914 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001915 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001916 }
1917
Jens Axboe35f3d142010-05-20 10:43:18 +02001918 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001919}
1920
Herbert Xu357b40a2005-04-19 22:30:14 -07001921/**
1922 * skb_store_bits - store bits from kernel buffer to skb
1923 * @skb: destination buffer
1924 * @offset: offset in destination
1925 * @from: source buffer
1926 * @len: number of bytes to copy
1927 *
1928 * Copy the specified number of bytes from the source buffer to the
1929 * destination skb. This function handles all the messy bits of
1930 * traversing fragment lists and such.
1931 */
1932
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001933int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001934{
David S. Miller1a028e52007-04-27 15:21:23 -07001935 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001936 struct sk_buff *frag_iter;
1937 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001938
1939 if (offset > (int)skb->len - len)
1940 goto fault;
1941
David S. Miller1a028e52007-04-27 15:21:23 -07001942 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001943 if (copy > len)
1944 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001945 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001946 if ((len -= copy) == 0)
1947 return 0;
1948 offset += copy;
1949 from += copy;
1950 }
1951
1952 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1953 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001954 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001955
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001956 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001957
Eric Dumazet9e903e02011-10-18 21:00:24 +00001958 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001959 if ((copy = end - offset) > 0) {
1960 u8 *vaddr;
1961
1962 if (copy > len)
1963 copy = len;
1964
Eric Dumazet51c56b02012-04-05 11:35:15 +02001965 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001966 memcpy(vaddr + frag->page_offset + offset - start,
1967 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001968 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001969
1970 if ((len -= copy) == 0)
1971 return 0;
1972 offset += copy;
1973 from += copy;
1974 }
David S. Miller1a028e52007-04-27 15:21:23 -07001975 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001976 }
1977
David S. Millerfbb398a2009-06-09 00:18:59 -07001978 skb_walk_frags(skb, frag_iter) {
1979 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001980
David S. Millerfbb398a2009-06-09 00:18:59 -07001981 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001982
David S. Millerfbb398a2009-06-09 00:18:59 -07001983 end = start + frag_iter->len;
1984 if ((copy = end - offset) > 0) {
1985 if (copy > len)
1986 copy = len;
1987 if (skb_store_bits(frag_iter, offset - start,
1988 from, copy))
1989 goto fault;
1990 if ((len -= copy) == 0)
1991 return 0;
1992 offset += copy;
1993 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001994 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001995 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001996 }
1997 if (!len)
1998 return 0;
1999
2000fault:
2001 return -EFAULT;
2002}
Herbert Xu357b40a2005-04-19 22:30:14 -07002003EXPORT_SYMBOL(skb_store_bits);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002006__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2007 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
David S. Miller1a028e52007-04-27 15:21:23 -07002009 int start = skb_headlen(skb);
2010 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002011 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 int pos = 0;
2013
2014 /* Checksum header. */
2015 if (copy > 0) {
2016 if (copy > len)
2017 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002018 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if ((len -= copy) == 0)
2020 return csum;
2021 offset += copy;
2022 pos = copy;
2023 }
2024
2025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002026 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002027 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002029 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002030
Eric Dumazet51c56b02012-04-05 11:35:15 +02002031 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002033 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 if (copy > len)
2037 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002038 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002039 csum2 = ops->update(vaddr + frag->page_offset +
2040 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002041 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002042 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!(len -= copy))
2044 return csum;
2045 offset += copy;
2046 pos += copy;
2047 }
David S. Miller1a028e52007-04-27 15:21:23 -07002048 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
David S. Millerfbb398a2009-06-09 00:18:59 -07002051 skb_walk_frags(skb, frag_iter) {
2052 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
David S. Millerfbb398a2009-06-09 00:18:59 -07002054 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
David S. Millerfbb398a2009-06-09 00:18:59 -07002056 end = start + frag_iter->len;
2057 if ((copy = end - offset) > 0) {
2058 __wsum csum2;
2059 if (copy > len)
2060 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002061 csum2 = __skb_checksum(frag_iter, offset - start,
2062 copy, 0, ops);
2063 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002064 if ((len -= copy) == 0)
2065 return csum;
2066 offset += copy;
2067 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002069 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002071 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 return csum;
2074}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002075EXPORT_SYMBOL(__skb_checksum);
2076
2077__wsum skb_checksum(const struct sk_buff *skb, int offset,
2078 int len, __wsum csum)
2079{
2080 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002081 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002082 .combine = csum_block_add_ext,
2083 };
2084
2085 return __skb_checksum(skb, offset, len, csum, &ops);
2086}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002087EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089/* Both of above in one bottle. */
2090
Al Viro81d77662006-11-14 21:37:33 -08002091__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2092 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
David S. Miller1a028e52007-04-27 15:21:23 -07002094 int start = skb_headlen(skb);
2095 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002096 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 int pos = 0;
2098
2099 /* Copy header. */
2100 if (copy > 0) {
2101 if (copy > len)
2102 copy = len;
2103 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2104 copy, csum);
2105 if ((len -= copy) == 0)
2106 return csum;
2107 offset += copy;
2108 to += copy;
2109 pos = copy;
2110 }
2111
2112 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002113 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002115 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002116
Eric Dumazet9e903e02011-10-18 21:00:24 +00002117 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002119 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 u8 *vaddr;
2121 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2122
2123 if (copy > len)
2124 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002125 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002127 frag->page_offset +
2128 offset - start, to,
2129 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002130 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 csum = csum_block_add(csum, csum2, pos);
2132 if (!(len -= copy))
2133 return csum;
2134 offset += copy;
2135 to += copy;
2136 pos += copy;
2137 }
David S. Miller1a028e52007-04-27 15:21:23 -07002138 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
David S. Millerfbb398a2009-06-09 00:18:59 -07002141 skb_walk_frags(skb, frag_iter) {
2142 __wsum csum2;
2143 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
David S. Millerfbb398a2009-06-09 00:18:59 -07002145 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
David S. Millerfbb398a2009-06-09 00:18:59 -07002147 end = start + frag_iter->len;
2148 if ((copy = end - offset) > 0) {
2149 if (copy > len)
2150 copy = len;
2151 csum2 = skb_copy_and_csum_bits(frag_iter,
2152 offset - start,
2153 to, copy, 0);
2154 csum = csum_block_add(csum, csum2, pos);
2155 if ((len -= copy) == 0)
2156 return csum;
2157 offset += copy;
2158 to += copy;
2159 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002161 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002163 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return csum;
2165}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002166EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Thomas Grafaf2806f2013-12-13 15:22:17 +01002168 /**
2169 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2170 * @from: source buffer
2171 *
2172 * Calculates the amount of linear headroom needed in the 'to' skb passed
2173 * into skb_zerocopy().
2174 */
2175unsigned int
2176skb_zerocopy_headlen(const struct sk_buff *from)
2177{
2178 unsigned int hlen = 0;
2179
2180 if (!from->head_frag ||
2181 skb_headlen(from) < L1_CACHE_BYTES ||
2182 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2183 hlen = skb_headlen(from);
2184
2185 if (skb_has_frag_list(from))
2186 hlen = from->len;
2187
2188 return hlen;
2189}
2190EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2191
2192/**
2193 * skb_zerocopy - Zero copy skb to skb
2194 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002195 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002196 * @len: number of bytes to copy from source buffer
2197 * @hlen: size of linear headroom in destination buffer
2198 *
2199 * Copies up to `len` bytes from `from` to `to` by creating references
2200 * to the frags in the source buffer.
2201 *
2202 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2203 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002204 *
2205 * Return value:
2206 * 0: everything is OK
2207 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2208 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002209 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002210int
2211skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002212{
2213 int i, j = 0;
2214 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002215 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002216 struct page *page;
2217 unsigned int offset;
2218
2219 BUG_ON(!from->head_frag && !hlen);
2220
2221 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002222 if (len <= skb_tailroom(to))
2223 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002224
2225 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002226 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2227 if (unlikely(ret))
2228 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002229 len -= hlen;
2230 } else {
2231 plen = min_t(int, skb_headlen(from), len);
2232 if (plen) {
2233 page = virt_to_head_page(from->head);
2234 offset = from->data - (unsigned char *)page_address(page);
2235 __skb_fill_page_desc(to, 0, page, offset, plen);
2236 get_page(page);
2237 j = 1;
2238 len -= plen;
2239 }
2240 }
2241
2242 to->truesize += len + plen;
2243 to->len += len + plen;
2244 to->data_len += len + plen;
2245
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002246 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2247 skb_tx_error(from);
2248 return -ENOMEM;
2249 }
2250
Thomas Grafaf2806f2013-12-13 15:22:17 +01002251 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2252 if (!len)
2253 break;
2254 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2255 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2256 len -= skb_shinfo(to)->frags[j].size;
2257 skb_frag_ref(to, j);
2258 j++;
2259 }
2260 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002261
2262 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002263}
2264EXPORT_SYMBOL_GPL(skb_zerocopy);
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2267{
Al Virod3bc23e2006-11-14 21:24:49 -08002268 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 long csstart;
2270
Patrick McHardy84fa7932006-08-29 16:44:56 -07002271 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002272 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 else
2274 csstart = skb_headlen(skb);
2275
Kris Katterjohn09a62662006-01-08 22:24:28 -08002276 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002278 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 csum = 0;
2281 if (csstart != skb->len)
2282 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2283 skb->len - csstart, 0);
2284
Patrick McHardy84fa7932006-08-29 16:44:56 -07002285 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002286 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Al Virod3bc23e2006-11-14 21:24:49 -08002288 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002291EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293/**
2294 * skb_dequeue - remove from the head of the queue
2295 * @list: list to dequeue from
2296 *
2297 * Remove the head of the list. The list lock is taken so the function
2298 * may be used safely with other locking list functions. The head item is
2299 * returned or %NULL if the list is empty.
2300 */
2301
2302struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2303{
2304 unsigned long flags;
2305 struct sk_buff *result;
2306
2307 spin_lock_irqsave(&list->lock, flags);
2308 result = __skb_dequeue(list);
2309 spin_unlock_irqrestore(&list->lock, flags);
2310 return result;
2311}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002312EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314/**
2315 * skb_dequeue_tail - remove from the tail of the queue
2316 * @list: list to dequeue from
2317 *
2318 * Remove the tail of the list. The list lock is taken so the function
2319 * may be used safely with other locking list functions. The tail item is
2320 * returned or %NULL if the list is empty.
2321 */
2322struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2323{
2324 unsigned long flags;
2325 struct sk_buff *result;
2326
2327 spin_lock_irqsave(&list->lock, flags);
2328 result = __skb_dequeue_tail(list);
2329 spin_unlock_irqrestore(&list->lock, flags);
2330 return result;
2331}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002332EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334/**
2335 * skb_queue_purge - empty a list
2336 * @list: list to empty
2337 *
2338 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2339 * the list and one reference dropped. This function takes the list
2340 * lock and is atomic with respect to other list locking functions.
2341 */
2342void skb_queue_purge(struct sk_buff_head *list)
2343{
2344 struct sk_buff *skb;
2345 while ((skb = skb_dequeue(list)) != NULL)
2346 kfree_skb(skb);
2347}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002348EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350/**
2351 * skb_queue_head - queue a buffer at the list head
2352 * @list: list to use
2353 * @newsk: buffer to queue
2354 *
2355 * Queue a buffer at the start of the list. This function takes the
2356 * list lock and can be used safely with other locking &sk_buff functions
2357 * safely.
2358 *
2359 * A buffer cannot be placed on two lists at the same time.
2360 */
2361void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2362{
2363 unsigned long flags;
2364
2365 spin_lock_irqsave(&list->lock, flags);
2366 __skb_queue_head(list, newsk);
2367 spin_unlock_irqrestore(&list->lock, flags);
2368}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002369EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371/**
2372 * skb_queue_tail - queue a buffer at the list tail
2373 * @list: list to use
2374 * @newsk: buffer to queue
2375 *
2376 * Queue a buffer at the tail of the list. This function takes the
2377 * list lock and can be used safely with other locking &sk_buff functions
2378 * safely.
2379 *
2380 * A buffer cannot be placed on two lists at the same time.
2381 */
2382void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2383{
2384 unsigned long flags;
2385
2386 spin_lock_irqsave(&list->lock, flags);
2387 __skb_queue_tail(list, newsk);
2388 spin_unlock_irqrestore(&list->lock, flags);
2389}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002390EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/**
2393 * skb_unlink - remove a buffer from a list
2394 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002395 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 *
David S. Miller8728b832005-08-09 19:25:21 -07002397 * Remove a packet from a list. The list locks are taken and this
2398 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *
David S. Miller8728b832005-08-09 19:25:21 -07002400 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 */
David S. Miller8728b832005-08-09 19:25:21 -07002402void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
David S. Miller8728b832005-08-09 19:25:21 -07002404 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
David S. Miller8728b832005-08-09 19:25:21 -07002406 spin_lock_irqsave(&list->lock, flags);
2407 __skb_unlink(skb, list);
2408 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002410EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412/**
2413 * skb_append - append a buffer
2414 * @old: buffer to insert after
2415 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002416 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 *
2418 * Place a packet after a given packet in a list. The list locks are taken
2419 * and this function is atomic with respect to other list locked calls.
2420 * A buffer cannot be placed on two lists at the same time.
2421 */
David S. Miller8728b832005-08-09 19:25:21 -07002422void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 unsigned long flags;
2425
David S. Miller8728b832005-08-09 19:25:21 -07002426 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002427 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002428 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002430EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432/**
2433 * skb_insert - insert a buffer
2434 * @old: buffer to insert before
2435 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002436 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 *
David S. Miller8728b832005-08-09 19:25:21 -07002438 * Place a packet before a given packet in a list. The list locks are
2439 * taken and this function is atomic with respect to other list locked
2440 * calls.
2441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 * A buffer cannot be placed on two lists at the same time.
2443 */
David S. Miller8728b832005-08-09 19:25:21 -07002444void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 unsigned long flags;
2447
David S. Miller8728b832005-08-09 19:25:21 -07002448 spin_lock_irqsave(&list->lock, flags);
2449 __skb_insert(newsk, old->prev, old, list);
2450 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002452EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454static inline void skb_split_inside_header(struct sk_buff *skb,
2455 struct sk_buff* skb1,
2456 const u32 len, const int pos)
2457{
2458 int i;
2459
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002460 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2461 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* And move data appendix as is. */
2463 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2464 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2465
2466 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2467 skb_shinfo(skb)->nr_frags = 0;
2468 skb1->data_len = skb->data_len;
2469 skb1->len += skb1->data_len;
2470 skb->data_len = 0;
2471 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002472 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475static inline void skb_split_no_header(struct sk_buff *skb,
2476 struct sk_buff* skb1,
2477 const u32 len, int pos)
2478{
2479 int i, k = 0;
2480 const int nfrags = skb_shinfo(skb)->nr_frags;
2481
2482 skb_shinfo(skb)->nr_frags = 0;
2483 skb1->len = skb1->data_len = skb->len - len;
2484 skb->len = len;
2485 skb->data_len = len - pos;
2486
2487 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002488 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 if (pos + size > len) {
2491 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2492
2493 if (pos < len) {
2494 /* Split frag.
2495 * We have two variants in this case:
2496 * 1. Move all the frag to the second
2497 * part, if it is possible. F.e.
2498 * this approach is mandatory for TUX,
2499 * where splitting is expensive.
2500 * 2. Split is accurately. We make this.
2501 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002502 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002504 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2505 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 skb_shinfo(skb)->nr_frags++;
2507 }
2508 k++;
2509 } else
2510 skb_shinfo(skb)->nr_frags++;
2511 pos += size;
2512 }
2513 skb_shinfo(skb1)->nr_frags = k;
2514}
2515
2516/**
2517 * skb_split - Split fragmented skb to two parts at length len.
2518 * @skb: the buffer to split
2519 * @skb1: the buffer to receive the second part
2520 * @len: new length for skb
2521 */
2522void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2523{
2524 int pos = skb_headlen(skb);
2525
Amerigo Wang68534c62013-02-19 22:51:30 +00002526 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (len < pos) /* Split line is inside header. */
2528 skb_split_inside_header(skb, skb1, len, pos);
2529 else /* Second chunk has no header, nothing to copy. */
2530 skb_split_no_header(skb, skb1, len, pos);
2531}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002532EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002534/* Shifting from/to a cloned skb is a no-go.
2535 *
2536 * Caller cannot keep skb_shinfo related pointers past calling here!
2537 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002538static int skb_prepare_for_shift(struct sk_buff *skb)
2539{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002540 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002541}
2542
2543/**
2544 * skb_shift - Shifts paged data partially from skb to another
2545 * @tgt: buffer into which tail data gets added
2546 * @skb: buffer from which the paged data comes from
2547 * @shiftlen: shift up to this many bytes
2548 *
2549 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002550 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002551 * It's up to caller to free skb if everything was shifted.
2552 *
2553 * If @tgt runs out of frags, the whole operation is aborted.
2554 *
2555 * Skb cannot include anything else but paged data while tgt is allowed
2556 * to have non-paged data as well.
2557 *
2558 * TODO: full sized shift could be optimized but that would need
2559 * specialized skb free'er to handle frags without up-to-date nr_frags.
2560 */
2561int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2562{
2563 int from, to, merge, todo;
2564 struct skb_frag_struct *fragfrom, *fragto;
2565
2566 BUG_ON(shiftlen > skb->len);
2567 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2568
2569 todo = shiftlen;
2570 from = 0;
2571 to = skb_shinfo(tgt)->nr_frags;
2572 fragfrom = &skb_shinfo(skb)->frags[from];
2573
2574 /* Actual merge is delayed until the point when we know we can
2575 * commit all, so that we don't have to undo partial changes
2576 */
2577 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002578 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2579 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002580 merge = -1;
2581 } else {
2582 merge = to - 1;
2583
Eric Dumazet9e903e02011-10-18 21:00:24 +00002584 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002585 if (todo < 0) {
2586 if (skb_prepare_for_shift(skb) ||
2587 skb_prepare_for_shift(tgt))
2588 return 0;
2589
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002590 /* All previous frag pointers might be stale! */
2591 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002592 fragto = &skb_shinfo(tgt)->frags[merge];
2593
Eric Dumazet9e903e02011-10-18 21:00:24 +00002594 skb_frag_size_add(fragto, shiftlen);
2595 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002596 fragfrom->page_offset += shiftlen;
2597
2598 goto onlymerged;
2599 }
2600
2601 from++;
2602 }
2603
2604 /* Skip full, not-fitting skb to avoid expensive operations */
2605 if ((shiftlen == skb->len) &&
2606 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2607 return 0;
2608
2609 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2610 return 0;
2611
2612 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2613 if (to == MAX_SKB_FRAGS)
2614 return 0;
2615
2616 fragfrom = &skb_shinfo(skb)->frags[from];
2617 fragto = &skb_shinfo(tgt)->frags[to];
2618
Eric Dumazet9e903e02011-10-18 21:00:24 +00002619 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002620 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002621 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002622 from++;
2623 to++;
2624
2625 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002626 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002627 fragto->page = fragfrom->page;
2628 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002629 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002630
2631 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002632 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002633 todo = 0;
2634
2635 to++;
2636 break;
2637 }
2638 }
2639
2640 /* Ready to "commit" this state change to tgt */
2641 skb_shinfo(tgt)->nr_frags = to;
2642
2643 if (merge >= 0) {
2644 fragfrom = &skb_shinfo(skb)->frags[0];
2645 fragto = &skb_shinfo(tgt)->frags[merge];
2646
Eric Dumazet9e903e02011-10-18 21:00:24 +00002647 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002648 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002649 }
2650
2651 /* Reposition in the original skb */
2652 to = 0;
2653 while (from < skb_shinfo(skb)->nr_frags)
2654 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2655 skb_shinfo(skb)->nr_frags = to;
2656
2657 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2658
2659onlymerged:
2660 /* Most likely the tgt won't ever need its checksum anymore, skb on
2661 * the other hand might need it if it needs to be resent
2662 */
2663 tgt->ip_summed = CHECKSUM_PARTIAL;
2664 skb->ip_summed = CHECKSUM_PARTIAL;
2665
2666 /* Yak, is it really working this way? Some helper please? */
2667 skb->len -= shiftlen;
2668 skb->data_len -= shiftlen;
2669 skb->truesize -= shiftlen;
2670 tgt->len += shiftlen;
2671 tgt->data_len += shiftlen;
2672 tgt->truesize += shiftlen;
2673
2674 return shiftlen;
2675}
2676
Thomas Graf677e90e2005-06-23 20:59:51 -07002677/**
2678 * skb_prepare_seq_read - Prepare a sequential read of skb data
2679 * @skb: the buffer to read
2680 * @from: lower offset of data to be read
2681 * @to: upper offset of data to be read
2682 * @st: state variable
2683 *
2684 * Initializes the specified state variable. Must be called before
2685 * invoking skb_seq_read() for the first time.
2686 */
2687void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2688 unsigned int to, struct skb_seq_state *st)
2689{
2690 st->lower_offset = from;
2691 st->upper_offset = to;
2692 st->root_skb = st->cur_skb = skb;
2693 st->frag_idx = st->stepped_offset = 0;
2694 st->frag_data = NULL;
2695}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002696EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002697
2698/**
2699 * skb_seq_read - Sequentially read skb data
2700 * @consumed: number of bytes consumed by the caller so far
2701 * @data: destination pointer for data to be returned
2702 * @st: state variable
2703 *
Mathias Krausebc323832013-11-07 14:18:26 +01002704 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002705 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002706 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002707 * of the block or 0 if the end of the skb data or the upper
2708 * offset has been reached.
2709 *
2710 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002711 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002712 * of bytes already consumed and the next call to
2713 * skb_seq_read() will return the remaining part of the block.
2714 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002715 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002716 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002717 * reads of potentially non linear data.
2718 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002719 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002720 * at the moment, state->root_skb could be replaced with
2721 * a stack for this purpose.
2722 */
2723unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2724 struct skb_seq_state *st)
2725{
2726 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2727 skb_frag_t *frag;
2728
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002729 if (unlikely(abs_offset >= st->upper_offset)) {
2730 if (st->frag_data) {
2731 kunmap_atomic(st->frag_data);
2732 st->frag_data = NULL;
2733 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002734 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002735 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002736
2737next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002738 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002739
Thomas Chenault995b3372009-05-18 21:43:27 -07002740 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002741 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002742 return block_limit - abs_offset;
2743 }
2744
2745 if (st->frag_idx == 0 && !st->frag_data)
2746 st->stepped_offset += skb_headlen(st->cur_skb);
2747
2748 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2749 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002750 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002751
2752 if (abs_offset < block_limit) {
2753 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002754 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002755
2756 *data = (u8 *) st->frag_data + frag->page_offset +
2757 (abs_offset - st->stepped_offset);
2758
2759 return block_limit - abs_offset;
2760 }
2761
2762 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002763 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002764 st->frag_data = NULL;
2765 }
2766
2767 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002768 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002769 }
2770
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002771 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002772 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002773 st->frag_data = NULL;
2774 }
2775
David S. Miller21dc3302010-08-23 00:13:46 -07002776 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002777 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002778 st->frag_idx = 0;
2779 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002780 } else if (st->cur_skb->next) {
2781 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002782 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002783 goto next_skb;
2784 }
2785
2786 return 0;
2787}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002788EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002789
2790/**
2791 * skb_abort_seq_read - Abort a sequential read of skb data
2792 * @st: state variable
2793 *
2794 * Must be called if skb_seq_read() was not called until it
2795 * returned 0.
2796 */
2797void skb_abort_seq_read(struct skb_seq_state *st)
2798{
2799 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002800 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002801}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002802EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002803
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002804#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2805
2806static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2807 struct ts_config *conf,
2808 struct ts_state *state)
2809{
2810 return skb_seq_read(offset, text, TS_SKB_CB(state));
2811}
2812
2813static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2814{
2815 skb_abort_seq_read(TS_SKB_CB(state));
2816}
2817
2818/**
2819 * skb_find_text - Find a text pattern in skb data
2820 * @skb: the buffer to look in
2821 * @from: search offset
2822 * @to: search limit
2823 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002824 *
2825 * Finds a pattern in the skb data according to the specified
2826 * textsearch configuration. Use textsearch_next() to retrieve
2827 * subsequent occurrences of the pattern. Returns the offset
2828 * to the first occurrence or UINT_MAX if no match was found.
2829 */
2830unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002831 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002832{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002833 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002834 unsigned int ret;
2835
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002836 config->get_next_block = skb_ts_get_next_block;
2837 config->finish = skb_ts_finish;
2838
Bojan Prtvar059a2442015-02-22 11:46:35 +01002839 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002840
Bojan Prtvar059a2442015-02-22 11:46:35 +01002841 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002842 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002843}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002844EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002845
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002846/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002847 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002848 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002849 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002850 * @getfrag: call back function to be used for getting the user data
2851 * @from: pointer to user message iov
2852 * @length: length of the iov message
2853 *
2854 * Description: This procedure append the user data in the fragment part
2855 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2856 */
2857int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002858 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002859 int len, int odd, struct sk_buff *skb),
2860 void *from, int length)
2861{
Eric Dumazetb2111722012-12-28 06:06:37 +00002862 int frg_cnt = skb_shinfo(skb)->nr_frags;
2863 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002864 int offset = 0;
2865 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002866 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002867
2868 do {
2869 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002870 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002871 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002872
Eric Dumazetb2111722012-12-28 06:06:37 +00002873 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002874 return -ENOMEM;
2875
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002876 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002877 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002878
Eric Dumazetb2111722012-12-28 06:06:37 +00002879 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2880 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002881 if (ret < 0)
2882 return -EFAULT;
2883
2884 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002885 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2886 copy);
2887 frg_cnt++;
2888 pfrag->offset += copy;
2889 get_page(pfrag->page);
2890
2891 skb->truesize += copy;
2892 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002893 skb->len += copy;
2894 skb->data_len += copy;
2895 offset += copy;
2896 length -= copy;
2897
2898 } while (length > 0);
2899
2900 return 0;
2901}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002902EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002903
Herbert Xucbb042f2006-03-20 22:43:56 -08002904/**
2905 * skb_pull_rcsum - pull skb and update receive checksum
2906 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002907 * @len: length of data pulled
2908 *
2909 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002910 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002911 * receive path processing instead of skb_pull unless you know
2912 * that the checksum difference is zero (e.g., a valid IP header)
2913 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002914 */
2915unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2916{
2917 BUG_ON(len > skb->len);
2918 skb->len -= len;
2919 BUG_ON(skb->len < skb->data_len);
2920 skb_postpull_rcsum(skb, skb->data, len);
2921 return skb->data += len;
2922}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002923EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2924
Herbert Xuf4c50d92006-06-22 03:02:40 -07002925/**
2926 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002927 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002928 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002929 *
2930 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002931 * a pointer to the first in a list of new skbs for the segments.
2932 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002933 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002934struct sk_buff *skb_segment(struct sk_buff *head_skb,
2935 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002936{
2937 struct sk_buff *segs = NULL;
2938 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002939 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002940 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2941 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2942 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002943 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002944 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002945 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002946 unsigned int headroom;
2947 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002948 __be16 proto;
2949 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002950 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002951 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002952 int err = -ENOMEM;
2953 int i = 0;
2954 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002955 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002956
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002957 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002958 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002959 if (unlikely(!proto))
2960 return ERR_PTR(-EINVAL);
2961
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002962 csum = !head_skb->encap_hdr_csum &&
2963 !!can_checksum_protocol(features, proto);
2964
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002965 headroom = skb_headroom(head_skb);
2966 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002967
2968 do {
2969 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002970 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002971 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002972 int size;
2973
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002974 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002975 if (len > mss)
2976 len = mss;
2977
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002978 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002979 if (hsize < 0)
2980 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002981 if (hsize > len || !sg)
2982 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002983
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002984 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2985 (skb_headlen(list_skb) == len || sg)) {
2986 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002987
Herbert Xu9d8506c2013-11-21 11:10:04 -08002988 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002989 nfrags = skb_shinfo(list_skb)->nr_frags;
2990 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002991 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002992 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002993
2994 while (pos < offset + len) {
2995 BUG_ON(i >= nfrags);
2996
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002997 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002998 if (pos + size > offset + len)
2999 break;
3000
3001 i++;
3002 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003003 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003004 }
3005
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003006 nskb = skb_clone(list_skb, GFP_ATOMIC);
3007 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003008
3009 if (unlikely(!nskb))
3010 goto err;
3011
Herbert Xu9d8506c2013-11-21 11:10:04 -08003012 if (unlikely(pskb_trim(nskb, len))) {
3013 kfree_skb(nskb);
3014 goto err;
3015 }
3016
Alexander Duyckec47ea82012-05-04 14:26:56 +00003017 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003018 if (skb_cow_head(nskb, doffset + headroom)) {
3019 kfree_skb(nskb);
3020 goto err;
3021 }
3022
Alexander Duyckec47ea82012-05-04 14:26:56 +00003023 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003024 skb_release_head_state(nskb);
3025 __skb_push(nskb, doffset);
3026 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003027 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003028 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003029 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003030
3031 if (unlikely(!nskb))
3032 goto err;
3033
3034 skb_reserve(nskb, headroom);
3035 __skb_put(nskb, doffset);
3036 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003037
3038 if (segs)
3039 tail->next = nskb;
3040 else
3041 segs = nskb;
3042 tail = nskb;
3043
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003044 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003045
Eric Dumazet030737b2013-10-19 11:42:54 -07003046 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003047 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003048
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003049 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003050 nskb->data - tnl_hlen,
3051 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003052
Herbert Xu9d8506c2013-11-21 11:10:04 -08003053 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003054 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003055
Tom Herberte585f232014-11-04 09:06:54 -08003056 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003057 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003058 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003059 skb_put(nskb, len),
3060 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003061 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003062 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003063 continue;
3064 }
3065
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003066 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003067
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003068 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003069 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003070
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003071 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3072 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003073
Herbert Xu9d8506c2013-11-21 11:10:04 -08003074 while (pos < offset + len) {
3075 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003076 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003077
3078 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003079 nfrags = skb_shinfo(list_skb)->nr_frags;
3080 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003081 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003082
3083 BUG_ON(!nfrags);
3084
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003085 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003086 }
3087
3088 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3089 MAX_SKB_FRAGS)) {
3090 net_warn_ratelimited(
3091 "skb_segment: too many frags: %u %u\n",
3092 pos, mss);
3093 goto err;
3094 }
3095
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003096 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3097 goto err;
3098
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003099 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003100 __skb_frag_ref(nskb_frag);
3101 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003102
3103 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003104 nskb_frag->page_offset += offset - pos;
3105 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003106 }
3107
Herbert Xu89319d382008-12-15 23:26:06 -08003108 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003109
3110 if (pos + size <= offset + len) {
3111 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003112 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003113 pos += size;
3114 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003115 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003116 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003117 }
3118
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003119 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003120 }
3121
Herbert Xu89319d382008-12-15 23:26:06 -08003122skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003123 nskb->data_len = len - hsize;
3124 nskb->len += nskb->data_len;
3125 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003126
Simon Horman1cdbcb72013-05-19 15:46:49 +00003127perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003128 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003129 nskb->csum = skb_checksum(nskb, doffset,
3130 nskb->len - doffset, 0);
3131 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003132 SKB_GSO_CB(nskb)->csum_start =
3133 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003134 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003135 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003136
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003137 /* Some callers want to get the end of the list.
3138 * Put it in segs->prev to avoid walking the list.
3139 * (see validate_xmit_skb_list() for example)
3140 */
3141 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003142
3143 /* Following permits correct backpressure, for protocols
3144 * using skb_set_owner_w().
3145 * Idea is to tranfert ownership from head_skb to last segment.
3146 */
3147 if (head_skb->destructor == sock_wfree) {
3148 swap(tail->truesize, head_skb->truesize);
3149 swap(tail->destructor, head_skb->destructor);
3150 swap(tail->sk, head_skb->sk);
3151 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003152 return segs;
3153
3154err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003155 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003156 return ERR_PTR(err);
3157}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003158EXPORT_SYMBOL_GPL(skb_segment);
3159
Herbert Xu71d93b32008-12-15 23:42:33 -08003160int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3161{
Eric Dumazet8a291112013-10-08 09:02:23 -07003162 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003163 unsigned int offset = skb_gro_offset(skb);
3164 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003165 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003166 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003167 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003168
Eric Dumazet8a291112013-10-08 09:02:23 -07003169 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003170 return -E2BIG;
3171
Eric Dumazet29e98242014-05-16 11:34:37 -07003172 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003173 pinfo = skb_shinfo(lp);
3174
3175 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003176 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003177 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003178 int i = skbinfo->nr_frags;
3179 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003180
Herbert Xu66e92fc2009-05-26 18:50:32 +00003181 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003182 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003183
Eric Dumazet8a291112013-10-08 09:02:23 -07003184 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003185 pinfo->nr_frags = nr_frags;
3186 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003187
Herbert Xu9aaa1562009-05-26 18:50:33 +00003188 frag = pinfo->frags + nr_frags;
3189 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003190 do {
3191 *--frag = *--frag2;
3192 } while (--i);
3193
3194 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003195 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003196
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003197 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003198 delta_truesize = skb->truesize -
3199 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003200
Herbert Xuf5572062009-01-14 20:40:03 -08003201 skb->truesize -= skb->data_len;
3202 skb->len -= skb->data_len;
3203 skb->data_len = 0;
3204
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003205 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003206 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003207 } else if (skb->head_frag) {
3208 int nr_frags = pinfo->nr_frags;
3209 skb_frag_t *frag = pinfo->frags + nr_frags;
3210 struct page *page = virt_to_head_page(skb->head);
3211 unsigned int first_size = headlen - offset;
3212 unsigned int first_offset;
3213
3214 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003215 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003216
3217 first_offset = skb->data -
3218 (unsigned char *)page_address(page) +
3219 offset;
3220
3221 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3222
3223 frag->page.p = page;
3224 frag->page_offset = first_offset;
3225 skb_frag_size_set(frag, first_size);
3226
3227 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3228 /* We dont need to clear skbinfo->nr_frags here */
3229
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003230 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003231 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3232 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003233 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003234
3235merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003236 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003237 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003238 unsigned int eat = offset - headlen;
3239
3240 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003241 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003242 skb->data_len -= eat;
3243 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003244 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003245 }
3246
Herbert Xu67147ba2009-05-26 18:50:22 +00003247 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003248
Eric Dumazet29e98242014-05-16 11:34:37 -07003249 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003250 skb_shinfo(p)->frag_list = skb;
3251 else
3252 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003253 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003254 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003255 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003256
Herbert Xu5d38a072009-01-04 16:13:40 -08003257done:
3258 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003259 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003260 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003261 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003262 if (lp != p) {
3263 lp->data_len += len;
3264 lp->truesize += delta_truesize;
3265 lp->len += len;
3266 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003267 NAPI_GRO_CB(skb)->same_flow = 1;
3268 return 0;
3269}
Herbert Xu71d93b32008-12-15 23:42:33 -08003270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271void __init skb_init(void)
3272{
3273 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3274 sizeof(struct sk_buff),
3275 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003276 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003277 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003278 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003279 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003280 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003281 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003282 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
David Howells716ea3a2007-04-02 20:19:53 -07003285/**
3286 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3287 * @skb: Socket buffer containing the buffers to be mapped
3288 * @sg: The scatter-gather list to map into
3289 * @offset: The offset into the buffer's contents to start mapping
3290 * @len: Length of buffer space to be mapped
3291 *
3292 * Fill the specified scatter-gather list with mappings/pointers into a
3293 * region of the buffer space attached to a socket buffer.
3294 */
David S. Miller51c739d2007-10-30 21:29:29 -07003295static int
3296__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003297{
David S. Miller1a028e52007-04-27 15:21:23 -07003298 int start = skb_headlen(skb);
3299 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003300 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003301 int elt = 0;
3302
3303 if (copy > 0) {
3304 if (copy > len)
3305 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003306 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003307 elt++;
3308 if ((len -= copy) == 0)
3309 return elt;
3310 offset += copy;
3311 }
3312
3313 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003314 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003315
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003316 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003317
Eric Dumazet9e903e02011-10-18 21:00:24 +00003318 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003319 if ((copy = end - offset) > 0) {
3320 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3321
3322 if (copy > len)
3323 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003324 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003325 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003326 elt++;
3327 if (!(len -= copy))
3328 return elt;
3329 offset += copy;
3330 }
David S. Miller1a028e52007-04-27 15:21:23 -07003331 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003332 }
3333
David S. Millerfbb398a2009-06-09 00:18:59 -07003334 skb_walk_frags(skb, frag_iter) {
3335 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003336
David S. Millerfbb398a2009-06-09 00:18:59 -07003337 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003338
David S. Millerfbb398a2009-06-09 00:18:59 -07003339 end = start + frag_iter->len;
3340 if ((copy = end - offset) > 0) {
3341 if (copy > len)
3342 copy = len;
3343 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3344 copy);
3345 if ((len -= copy) == 0)
3346 return elt;
3347 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003348 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003349 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003350 }
3351 BUG_ON(len);
3352 return elt;
3353}
3354
Fan Du25a91d82014-01-18 09:54:23 +08003355/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3356 * sglist without mark the sg which contain last skb data as the end.
3357 * So the caller can mannipulate sg list as will when padding new data after
3358 * the first call without calling sg_unmark_end to expend sg list.
3359 *
3360 * Scenario to use skb_to_sgvec_nomark:
3361 * 1. sg_init_table
3362 * 2. skb_to_sgvec_nomark(payload1)
3363 * 3. skb_to_sgvec_nomark(payload2)
3364 *
3365 * This is equivalent to:
3366 * 1. sg_init_table
3367 * 2. skb_to_sgvec(payload1)
3368 * 3. sg_unmark_end
3369 * 4. skb_to_sgvec(payload2)
3370 *
3371 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3372 * is more preferable.
3373 */
3374int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3375 int offset, int len)
3376{
3377 return __skb_to_sgvec(skb, sg, offset, len);
3378}
3379EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3380
David S. Miller51c739d2007-10-30 21:29:29 -07003381int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3382{
3383 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3384
Jens Axboec46f2332007-10-31 12:06:37 +01003385 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003386
3387 return nsg;
3388}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003389EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003390
David Howells716ea3a2007-04-02 20:19:53 -07003391/**
3392 * skb_cow_data - Check that a socket buffer's data buffers are writable
3393 * @skb: The socket buffer to check.
3394 * @tailbits: Amount of trailing space to be added
3395 * @trailer: Returned pointer to the skb where the @tailbits space begins
3396 *
3397 * Make sure that the data buffers attached to a socket buffer are
3398 * writable. If they are not, private copies are made of the data buffers
3399 * and the socket buffer is set to use these instead.
3400 *
3401 * If @tailbits is given, make sure that there is space to write @tailbits
3402 * bytes of data beyond current end of socket buffer. @trailer will be
3403 * set to point to the skb in which this space begins.
3404 *
3405 * The number of scatterlist elements required to completely map the
3406 * COW'd and extended socket buffer will be returned.
3407 */
3408int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3409{
3410 int copyflag;
3411 int elt;
3412 struct sk_buff *skb1, **skb_p;
3413
3414 /* If skb is cloned or its head is paged, reallocate
3415 * head pulling out all the pages (pages are considered not writable
3416 * at the moment even if they are anonymous).
3417 */
3418 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3419 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3420 return -ENOMEM;
3421
3422 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003423 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003424 /* A little of trouble, not enough of space for trailer.
3425 * This should not happen, when stack is tuned to generate
3426 * good frames. OK, on miss we reallocate and reserve even more
3427 * space, 128 bytes is fair. */
3428
3429 if (skb_tailroom(skb) < tailbits &&
3430 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3431 return -ENOMEM;
3432
3433 /* Voila! */
3434 *trailer = skb;
3435 return 1;
3436 }
3437
3438 /* Misery. We are in troubles, going to mincer fragments... */
3439
3440 elt = 1;
3441 skb_p = &skb_shinfo(skb)->frag_list;
3442 copyflag = 0;
3443
3444 while ((skb1 = *skb_p) != NULL) {
3445 int ntail = 0;
3446
3447 /* The fragment is partially pulled by someone,
3448 * this can happen on input. Copy it and everything
3449 * after it. */
3450
3451 if (skb_shared(skb1))
3452 copyflag = 1;
3453
3454 /* If the skb is the last, worry about trailer. */
3455
3456 if (skb1->next == NULL && tailbits) {
3457 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003458 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003459 skb_tailroom(skb1) < tailbits)
3460 ntail = tailbits + 128;
3461 }
3462
3463 if (copyflag ||
3464 skb_cloned(skb1) ||
3465 ntail ||
3466 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003467 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003468 struct sk_buff *skb2;
3469
3470 /* Fuck, we are miserable poor guys... */
3471 if (ntail == 0)
3472 skb2 = skb_copy(skb1, GFP_ATOMIC);
3473 else
3474 skb2 = skb_copy_expand(skb1,
3475 skb_headroom(skb1),
3476 ntail,
3477 GFP_ATOMIC);
3478 if (unlikely(skb2 == NULL))
3479 return -ENOMEM;
3480
3481 if (skb1->sk)
3482 skb_set_owner_w(skb2, skb1->sk);
3483
3484 /* Looking around. Are we still alive?
3485 * OK, link new skb, drop old one */
3486
3487 skb2->next = skb1->next;
3488 *skb_p = skb2;
3489 kfree_skb(skb1);
3490 skb1 = skb2;
3491 }
3492 elt++;
3493 *trailer = skb1;
3494 skb_p = &skb1->next;
3495 }
3496
3497 return elt;
3498}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003499EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003500
Eric Dumazetb1faf562010-05-31 23:44:05 -07003501static void sock_rmem_free(struct sk_buff *skb)
3502{
3503 struct sock *sk = skb->sk;
3504
3505 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3506}
3507
3508/*
3509 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3510 */
3511int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3512{
3513 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003514 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003515 return -ENOMEM;
3516
3517 skb_orphan(skb);
3518 skb->sk = sk;
3519 skb->destructor = sock_rmem_free;
3520 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3521
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003522 /* before exiting rcu section, make sure dst is refcounted */
3523 skb_dst_force(skb);
3524
Eric Dumazetb1faf562010-05-31 23:44:05 -07003525 skb_queue_tail(&sk->sk_error_queue, skb);
3526 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003527 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003528 return 0;
3529}
3530EXPORT_SYMBOL(sock_queue_err_skb);
3531
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003532struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3533{
3534 struct sk_buff_head *q = &sk->sk_error_queue;
3535 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003536 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003537 int err = 0;
3538
Eric Dumazet997d5c32015-02-18 05:47:55 -08003539 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003540 skb = __skb_dequeue(q);
3541 if (skb && (skb_next = skb_peek(q)))
3542 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003543 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003544
3545 sk->sk_err = err;
3546 if (err)
3547 sk->sk_error_report(sk);
3548
3549 return skb;
3550}
3551EXPORT_SYMBOL(sock_dequeue_err_skb);
3552
Alexander Duyckcab41c42014-09-10 18:05:26 -04003553/**
3554 * skb_clone_sk - create clone of skb, and take reference to socket
3555 * @skb: the skb to clone
3556 *
3557 * This function creates a clone of a buffer that holds a reference on
3558 * sk_refcnt. Buffers created via this function are meant to be
3559 * returned using sock_queue_err_skb, or free via kfree_skb.
3560 *
3561 * When passing buffers allocated with this function to sock_queue_err_skb
3562 * it is necessary to wrap the call with sock_hold/sock_put in order to
3563 * prevent the socket from being released prior to being enqueued on
3564 * the sk_error_queue.
3565 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003566struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3567{
3568 struct sock *sk = skb->sk;
3569 struct sk_buff *clone;
3570
3571 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3572 return NULL;
3573
3574 clone = skb_clone(skb, GFP_ATOMIC);
3575 if (!clone) {
3576 sock_put(sk);
3577 return NULL;
3578 }
3579
3580 clone->sk = sk;
3581 clone->destructor = sock_efree;
3582
3583 return clone;
3584}
3585EXPORT_SYMBOL(skb_clone_sk);
3586
Alexander Duyck37846ef2014-09-04 13:31:10 -04003587static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3588 struct sock *sk,
3589 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003590{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003591 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003592 int err;
3593
Patrick Ohlyac45f602009-02-12 05:03:37 +00003594 serr = SKB_EXT_ERR(skb);
3595 memset(serr, 0, sizeof(*serr));
3596 serr->ee.ee_errno = ENOMSG;
3597 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003598 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003599 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003600 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003601 if (sk->sk_protocol == IPPROTO_TCP)
3602 serr->ee.ee_data -= sk->sk_tskey;
3603 }
Eric Dumazet29030372010-05-29 00:20:48 -07003604
Patrick Ohlyac45f602009-02-12 05:03:37 +00003605 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003606
Patrick Ohlyac45f602009-02-12 05:03:37 +00003607 if (err)
3608 kfree_skb(skb);
3609}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003610
Willem de Bruijnb245be12015-01-30 13:29:32 -05003611static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3612{
3613 bool ret;
3614
3615 if (likely(sysctl_tstamp_allow_data || tsonly))
3616 return true;
3617
3618 read_lock_bh(&sk->sk_callback_lock);
3619 ret = sk->sk_socket && sk->sk_socket->file &&
3620 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3621 read_unlock_bh(&sk->sk_callback_lock);
3622 return ret;
3623}
3624
Alexander Duyck37846ef2014-09-04 13:31:10 -04003625void skb_complete_tx_timestamp(struct sk_buff *skb,
3626 struct skb_shared_hwtstamps *hwtstamps)
3627{
3628 struct sock *sk = skb->sk;
3629
Willem de Bruijnb245be12015-01-30 13:29:32 -05003630 if (!skb_may_tx_timestamp(sk, false))
3631 return;
3632
Alexander Duyck62bccb82014-09-04 13:31:35 -04003633 /* take a reference to prevent skb_orphan() from freeing the socket */
3634 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003635
Alexander Duyck62bccb82014-09-04 13:31:35 -04003636 *skb_hwtstamps(skb) = *hwtstamps;
3637 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003638
3639 sock_put(sk);
3640}
3641EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3642
3643void __skb_tstamp_tx(struct sk_buff *orig_skb,
3644 struct skb_shared_hwtstamps *hwtstamps,
3645 struct sock *sk, int tstype)
3646{
3647 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003648 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003649
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003650 if (!sk)
3651 return;
3652
3653 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3654 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003655 return;
3656
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003657 if (tsonly)
3658 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003659 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003660 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003661 if (!skb)
3662 return;
3663
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003664 if (tsonly) {
3665 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3666 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3667 }
3668
3669 if (hwtstamps)
3670 *skb_hwtstamps(skb) = *hwtstamps;
3671 else
3672 skb->tstamp = ktime_get_real();
3673
Alexander Duyck37846ef2014-09-04 13:31:10 -04003674 __skb_complete_tx_timestamp(skb, sk, tstype);
3675}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003676EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3677
3678void skb_tstamp_tx(struct sk_buff *orig_skb,
3679 struct skb_shared_hwtstamps *hwtstamps)
3680{
3681 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3682 SCM_TSTAMP_SND);
3683}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003684EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3685
Johannes Berg6e3e9392011-11-09 10:15:42 +01003686void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3687{
3688 struct sock *sk = skb->sk;
3689 struct sock_exterr_skb *serr;
3690 int err;
3691
3692 skb->wifi_acked_valid = 1;
3693 skb->wifi_acked = acked;
3694
3695 serr = SKB_EXT_ERR(skb);
3696 memset(serr, 0, sizeof(*serr));
3697 serr->ee.ee_errno = ENOMSG;
3698 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3699
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003700 /* take a reference to prevent skb_orphan() from freeing the socket */
3701 sock_hold(sk);
3702
Johannes Berg6e3e9392011-11-09 10:15:42 +01003703 err = sock_queue_err_skb(sk, skb);
3704 if (err)
3705 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003706
3707 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003708}
3709EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3710
Rusty Russellf35d9d82008-02-04 23:49:54 -05003711/**
3712 * skb_partial_csum_set - set up and verify partial csum values for packet
3713 * @skb: the skb to set
3714 * @start: the number of bytes after skb->data to start checksumming.
3715 * @off: the offset from start to place the checksum.
3716 *
3717 * For untrusted partially-checksummed packets, we need to make sure the values
3718 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3719 *
3720 * This function checks and sets those values and skb->ip_summed: if this
3721 * returns false you should drop the packet.
3722 */
3723bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3724{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003725 if (unlikely(start > skb_headlen(skb)) ||
3726 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003727 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3728 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003729 return false;
3730 }
3731 skb->ip_summed = CHECKSUM_PARTIAL;
3732 skb->csum_start = skb_headroom(skb) + start;
3733 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003734 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003735 return true;
3736}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003737EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003738
Paul Durranted1f50c2014-01-09 10:02:46 +00003739static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3740 unsigned int max)
3741{
3742 if (skb_headlen(skb) >= len)
3743 return 0;
3744
3745 /* If we need to pullup then pullup to the max, so we
3746 * won't need to do it again.
3747 */
3748 if (max > skb->len)
3749 max = skb->len;
3750
3751 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3752 return -ENOMEM;
3753
3754 if (skb_headlen(skb) < len)
3755 return -EPROTO;
3756
3757 return 0;
3758}
3759
Jan Beulichf9708b42014-03-11 13:56:05 +00003760#define MAX_TCP_HDR_LEN (15 * 4)
3761
3762static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3763 typeof(IPPROTO_IP) proto,
3764 unsigned int off)
3765{
3766 switch (proto) {
3767 int err;
3768
3769 case IPPROTO_TCP:
3770 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3771 off + MAX_TCP_HDR_LEN);
3772 if (!err && !skb_partial_csum_set(skb, off,
3773 offsetof(struct tcphdr,
3774 check)))
3775 err = -EPROTO;
3776 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3777
3778 case IPPROTO_UDP:
3779 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3780 off + sizeof(struct udphdr));
3781 if (!err && !skb_partial_csum_set(skb, off,
3782 offsetof(struct udphdr,
3783 check)))
3784 err = -EPROTO;
3785 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3786 }
3787
3788 return ERR_PTR(-EPROTO);
3789}
3790
Paul Durranted1f50c2014-01-09 10:02:46 +00003791/* This value should be large enough to cover a tagged ethernet header plus
3792 * maximally sized IP and TCP or UDP headers.
3793 */
3794#define MAX_IP_HDR_LEN 128
3795
Jan Beulichf9708b42014-03-11 13:56:05 +00003796static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003797{
3798 unsigned int off;
3799 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003800 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003801 int err;
3802
3803 fragment = false;
3804
3805 err = skb_maybe_pull_tail(skb,
3806 sizeof(struct iphdr),
3807 MAX_IP_HDR_LEN);
3808 if (err < 0)
3809 goto out;
3810
3811 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3812 fragment = true;
3813
3814 off = ip_hdrlen(skb);
3815
3816 err = -EPROTO;
3817
3818 if (fragment)
3819 goto out;
3820
Jan Beulichf9708b42014-03-11 13:56:05 +00003821 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3822 if (IS_ERR(csum))
3823 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003824
Jan Beulichf9708b42014-03-11 13:56:05 +00003825 if (recalculate)
3826 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3827 ip_hdr(skb)->daddr,
3828 skb->len - off,
3829 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003830 err = 0;
3831
3832out:
3833 return err;
3834}
3835
3836/* This value should be large enough to cover a tagged ethernet header plus
3837 * an IPv6 header, all options, and a maximal TCP or UDP header.
3838 */
3839#define MAX_IPV6_HDR_LEN 256
3840
3841#define OPT_HDR(type, skb, off) \
3842 (type *)(skb_network_header(skb) + (off))
3843
3844static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3845{
3846 int err;
3847 u8 nexthdr;
3848 unsigned int off;
3849 unsigned int len;
3850 bool fragment;
3851 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003852 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003853
3854 fragment = false;
3855 done = false;
3856
3857 off = sizeof(struct ipv6hdr);
3858
3859 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3860 if (err < 0)
3861 goto out;
3862
3863 nexthdr = ipv6_hdr(skb)->nexthdr;
3864
3865 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3866 while (off <= len && !done) {
3867 switch (nexthdr) {
3868 case IPPROTO_DSTOPTS:
3869 case IPPROTO_HOPOPTS:
3870 case IPPROTO_ROUTING: {
3871 struct ipv6_opt_hdr *hp;
3872
3873 err = skb_maybe_pull_tail(skb,
3874 off +
3875 sizeof(struct ipv6_opt_hdr),
3876 MAX_IPV6_HDR_LEN);
3877 if (err < 0)
3878 goto out;
3879
3880 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3881 nexthdr = hp->nexthdr;
3882 off += ipv6_optlen(hp);
3883 break;
3884 }
3885 case IPPROTO_AH: {
3886 struct ip_auth_hdr *hp;
3887
3888 err = skb_maybe_pull_tail(skb,
3889 off +
3890 sizeof(struct ip_auth_hdr),
3891 MAX_IPV6_HDR_LEN);
3892 if (err < 0)
3893 goto out;
3894
3895 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3896 nexthdr = hp->nexthdr;
3897 off += ipv6_authlen(hp);
3898 break;
3899 }
3900 case IPPROTO_FRAGMENT: {
3901 struct frag_hdr *hp;
3902
3903 err = skb_maybe_pull_tail(skb,
3904 off +
3905 sizeof(struct frag_hdr),
3906 MAX_IPV6_HDR_LEN);
3907 if (err < 0)
3908 goto out;
3909
3910 hp = OPT_HDR(struct frag_hdr, skb, off);
3911
3912 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3913 fragment = true;
3914
3915 nexthdr = hp->nexthdr;
3916 off += sizeof(struct frag_hdr);
3917 break;
3918 }
3919 default:
3920 done = true;
3921 break;
3922 }
3923 }
3924
3925 err = -EPROTO;
3926
3927 if (!done || fragment)
3928 goto out;
3929
Jan Beulichf9708b42014-03-11 13:56:05 +00003930 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3931 if (IS_ERR(csum))
3932 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003933
Jan Beulichf9708b42014-03-11 13:56:05 +00003934 if (recalculate)
3935 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3936 &ipv6_hdr(skb)->daddr,
3937 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003938 err = 0;
3939
3940out:
3941 return err;
3942}
3943
3944/**
3945 * skb_checksum_setup - set up partial checksum offset
3946 * @skb: the skb to set up
3947 * @recalculate: if true the pseudo-header checksum will be recalculated
3948 */
3949int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3950{
3951 int err;
3952
3953 switch (skb->protocol) {
3954 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003955 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003956 break;
3957
3958 case htons(ETH_P_IPV6):
3959 err = skb_checksum_setup_ipv6(skb, recalculate);
3960 break;
3961
3962 default:
3963 err = -EPROTO;
3964 break;
3965 }
3966
3967 return err;
3968}
3969EXPORT_SYMBOL(skb_checksum_setup);
3970
Linus Lüssing9afd85c2015-05-02 14:01:07 +02003971/**
3972 * skb_checksum_maybe_trim - maybe trims the given skb
3973 * @skb: the skb to check
3974 * @transport_len: the data length beyond the network header
3975 *
3976 * Checks whether the given skb has data beyond the given transport length.
3977 * If so, returns a cloned skb trimmed to this transport length.
3978 * Otherwise returns the provided skb. Returns NULL in error cases
3979 * (e.g. transport_len exceeds skb length or out-of-memory).
3980 *
3981 * Caller needs to set the skb transport header and release the returned skb.
3982 * Provided skb is consumed.
3983 */
3984static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
3985 unsigned int transport_len)
3986{
3987 struct sk_buff *skb_chk;
3988 unsigned int len = skb_transport_offset(skb) + transport_len;
3989 int ret;
3990
3991 if (skb->len < len) {
3992 kfree_skb(skb);
3993 return NULL;
3994 } else if (skb->len == len) {
3995 return skb;
3996 }
3997
3998 skb_chk = skb_clone(skb, GFP_ATOMIC);
3999 kfree_skb(skb);
4000
4001 if (!skb_chk)
4002 return NULL;
4003
4004 ret = pskb_trim_rcsum(skb_chk, len);
4005 if (ret) {
4006 kfree_skb(skb_chk);
4007 return NULL;
4008 }
4009
4010 return skb_chk;
4011}
4012
4013/**
4014 * skb_checksum_trimmed - validate checksum of an skb
4015 * @skb: the skb to check
4016 * @transport_len: the data length beyond the network header
4017 * @skb_chkf: checksum function to use
4018 *
4019 * Applies the given checksum function skb_chkf to the provided skb.
4020 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4021 *
4022 * If the skb has data beyond the given transport length, then a
4023 * trimmed & cloned skb is checked and returned.
4024 *
4025 * Caller needs to set the skb transport header and release the returned skb.
4026 * Provided skb is consumed.
4027 */
4028struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4029 unsigned int transport_len,
4030 __sum16(*skb_chkf)(struct sk_buff *skb))
4031{
4032 struct sk_buff *skb_chk;
4033 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004034 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004035
4036 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4037 if (!skb_chk)
4038 return NULL;
4039
4040 if (!pskb_may_pull(skb_chk, offset)) {
4041 kfree_skb(skb_chk);
4042 return NULL;
4043 }
4044
4045 __skb_pull(skb_chk, offset);
4046 ret = skb_chkf(skb_chk);
4047 __skb_push(skb_chk, offset);
4048
4049 if (ret) {
4050 kfree_skb(skb_chk);
4051 return NULL;
4052 }
4053
4054 return skb_chk;
4055}
4056EXPORT_SYMBOL(skb_checksum_trimmed);
4057
Ben Hutchings4497b072008-06-19 16:22:28 -07004058void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4059{
Joe Perchese87cc472012-05-13 21:56:26 +00004060 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4061 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004062}
Ben Hutchings4497b072008-06-19 16:22:28 -07004063EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004064
4065void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4066{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004067 if (head_stolen) {
4068 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004069 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004070 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004071 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004072 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004073}
4074EXPORT_SYMBOL(kfree_skb_partial);
4075
4076/**
4077 * skb_try_coalesce - try to merge skb to prior one
4078 * @to: prior buffer
4079 * @from: buffer to add
4080 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004081 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004082 */
4083bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4084 bool *fragstolen, int *delta_truesize)
4085{
4086 int i, delta, len = from->len;
4087
4088 *fragstolen = false;
4089
4090 if (skb_cloned(to))
4091 return false;
4092
4093 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004094 if (len)
4095 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004096 *delta_truesize = 0;
4097 return true;
4098 }
4099
4100 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4101 return false;
4102
4103 if (skb_headlen(from) != 0) {
4104 struct page *page;
4105 unsigned int offset;
4106
4107 if (skb_shinfo(to)->nr_frags +
4108 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4109 return false;
4110
4111 if (skb_head_is_locked(from))
4112 return false;
4113
4114 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4115
4116 page = virt_to_head_page(from->head);
4117 offset = from->data - (unsigned char *)page_address(page);
4118
4119 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4120 page, offset, skb_headlen(from));
4121 *fragstolen = true;
4122 } else {
4123 if (skb_shinfo(to)->nr_frags +
4124 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4125 return false;
4126
Weiping Panf4b549a2012-09-28 20:15:30 +00004127 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004128 }
4129
4130 WARN_ON_ONCE(delta < len);
4131
4132 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4133 skb_shinfo(from)->frags,
4134 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4135 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4136
4137 if (!skb_cloned(from))
4138 skb_shinfo(from)->nr_frags = 0;
4139
Li RongQing8ea853f2012-09-18 16:53:21 +00004140 /* if the skb is not cloned this does nothing
4141 * since we set nr_frags to 0.
4142 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004143 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4144 skb_frag_ref(from, i);
4145
4146 to->truesize += delta;
4147 to->len += len;
4148 to->data_len += len;
4149
4150 *delta_truesize = delta;
4151 return true;
4152}
4153EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004154
4155/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004156 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004157 *
4158 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004159 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004160 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004161 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4162 * into/from a tunnel. Some information have to be cleared during these
4163 * operations.
4164 * skb_scrub_packet can also be used to clean a skb before injecting it in
4165 * another namespace (@xnet == true). We have to clear all information in the
4166 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004167 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004168void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004169{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004170 skb->tstamp.tv64 = 0;
4171 skb->pkt_type = PACKET_HOST;
4172 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004173 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004174 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004175 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004176 secpath_reset(skb);
4177 nf_reset(skb);
4178 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004179
4180 if (!xnet)
4181 return;
4182
4183 skb_orphan(skb);
4184 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004185}
4186EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004187
4188/**
4189 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4190 *
4191 * @skb: GSO skb
4192 *
4193 * skb_gso_transport_seglen is used to determine the real size of the
4194 * individual segments, including Layer4 headers (TCP/UDP).
4195 *
4196 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4197 */
4198unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4199{
4200 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004201 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004202
Florian Westphalf993bc22014-10-20 13:49:18 +02004203 if (skb->encapsulation) {
4204 thlen = skb_inner_transport_header(skb) -
4205 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004206
Florian Westphalf993bc22014-10-20 13:49:18 +02004207 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4208 thlen += inner_tcp_hdrlen(skb);
4209 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4210 thlen = tcp_hdrlen(skb);
4211 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004212 /* UFO sets gso_size to the size of the fragmentation
4213 * payload, i.e. the size of the L4 (UDP) header is already
4214 * accounted for.
4215 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004216 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004217}
4218EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004219
4220static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4221{
4222 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4223 kfree_skb(skb);
4224 return NULL;
4225 }
4226
4227 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4228 skb->mac_header += VLAN_HLEN;
4229 return skb;
4230}
4231
4232struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4233{
4234 struct vlan_hdr *vhdr;
4235 u16 vlan_tci;
4236
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004237 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004238 /* vlan_tci is already set-up so leave this for another time */
4239 return skb;
4240 }
4241
4242 skb = skb_share_check(skb, GFP_ATOMIC);
4243 if (unlikely(!skb))
4244 goto err_free;
4245
4246 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4247 goto err_free;
4248
4249 vhdr = (struct vlan_hdr *)skb->data;
4250 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4251 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4252
4253 skb_pull_rcsum(skb, VLAN_HLEN);
4254 vlan_set_encap_proto(skb, vhdr);
4255
4256 skb = skb_reorder_vlan_header(skb);
4257 if (unlikely(!skb))
4258 goto err_free;
4259
4260 skb_reset_network_header(skb);
4261 skb_reset_transport_header(skb);
4262 skb_reset_mac_len(skb);
4263
4264 return skb;
4265
4266err_free:
4267 kfree_skb(skb);
4268 return NULL;
4269}
4270EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004271
Jiri Pirkoe2195122014-11-19 14:05:01 +01004272int skb_ensure_writable(struct sk_buff *skb, int write_len)
4273{
4274 if (!pskb_may_pull(skb, write_len))
4275 return -ENOMEM;
4276
4277 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4278 return 0;
4279
4280 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4281}
4282EXPORT_SYMBOL(skb_ensure_writable);
4283
Jiri Pirko93515d52014-11-19 14:05:02 +01004284/* remove VLAN header from packet and update csum accordingly. */
4285static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4286{
4287 struct vlan_hdr *vhdr;
4288 unsigned int offset = skb->data - skb_mac_header(skb);
4289 int err;
4290
4291 __skb_push(skb, offset);
4292 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4293 if (unlikely(err))
4294 goto pull;
4295
4296 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4297
4298 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4299 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4300
4301 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4302 __skb_pull(skb, VLAN_HLEN);
4303
4304 vlan_set_encap_proto(skb, vhdr);
4305 skb->mac_header += VLAN_HLEN;
4306
4307 if (skb_network_offset(skb) < ETH_HLEN)
4308 skb_set_network_header(skb, ETH_HLEN);
4309
4310 skb_reset_mac_len(skb);
4311pull:
4312 __skb_pull(skb, offset);
4313
4314 return err;
4315}
4316
4317int skb_vlan_pop(struct sk_buff *skb)
4318{
4319 u16 vlan_tci;
4320 __be16 vlan_proto;
4321 int err;
4322
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004323 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004324 skb->vlan_tci = 0;
4325 } else {
4326 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4327 skb->protocol != htons(ETH_P_8021AD)) ||
4328 skb->len < VLAN_ETH_HLEN))
4329 return 0;
4330
4331 err = __skb_vlan_pop(skb, &vlan_tci);
4332 if (err)
4333 return err;
4334 }
4335 /* move next vlan tag to hw accel tag */
4336 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4337 skb->protocol != htons(ETH_P_8021AD)) ||
4338 skb->len < VLAN_ETH_HLEN))
4339 return 0;
4340
4341 vlan_proto = skb->protocol;
4342 err = __skb_vlan_pop(skb, &vlan_tci);
4343 if (unlikely(err))
4344 return err;
4345
4346 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4347 return 0;
4348}
4349EXPORT_SYMBOL(skb_vlan_pop);
4350
4351int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4352{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004353 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004354 unsigned int offset = skb->data - skb_mac_header(skb);
4355 int err;
4356
4357 /* __vlan_insert_tag expect skb->data pointing to mac header.
4358 * So change skb->data before calling it and change back to
4359 * original position later
4360 */
4361 __skb_push(skb, offset);
4362 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004363 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004364 if (err)
4365 return err;
4366 skb->protocol = skb->vlan_proto;
4367 skb->mac_len += VLAN_HLEN;
4368 __skb_pull(skb, offset);
4369
4370 if (skb->ip_summed == CHECKSUM_COMPLETE)
4371 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4372 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4373 }
4374 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4375 return 0;
4376}
4377EXPORT_SYMBOL(skb_vlan_push);
4378
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004379/**
4380 * alloc_skb_with_frags - allocate skb with page frags
4381 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004382 * @header_len: size of linear part
4383 * @data_len: needed length in frags
4384 * @max_page_order: max page order desired.
4385 * @errcode: pointer to error code if any
4386 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004387 *
4388 * This can be used to allocate a paged skb, given a maximal order for frags.
4389 */
4390struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4391 unsigned long data_len,
4392 int max_page_order,
4393 int *errcode,
4394 gfp_t gfp_mask)
4395{
4396 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4397 unsigned long chunk;
4398 struct sk_buff *skb;
4399 struct page *page;
4400 gfp_t gfp_head;
4401 int i;
4402
4403 *errcode = -EMSGSIZE;
4404 /* Note this test could be relaxed, if we succeed to allocate
4405 * high order pages...
4406 */
4407 if (npages > MAX_SKB_FRAGS)
4408 return NULL;
4409
4410 gfp_head = gfp_mask;
4411 if (gfp_head & __GFP_WAIT)
4412 gfp_head |= __GFP_REPEAT;
4413
4414 *errcode = -ENOBUFS;
4415 skb = alloc_skb(header_len, gfp_head);
4416 if (!skb)
4417 return NULL;
4418
4419 skb->truesize += npages << PAGE_SHIFT;
4420
4421 for (i = 0; npages > 0; i++) {
4422 int order = max_page_order;
4423
4424 while (order) {
4425 if (npages >= 1 << order) {
4426 page = alloc_pages(gfp_mask |
4427 __GFP_COMP |
4428 __GFP_NOWARN |
4429 __GFP_NORETRY,
4430 order);
4431 if (page)
4432 goto fill_page;
4433 /* Do not retry other high order allocations */
4434 order = 1;
4435 max_page_order = 0;
4436 }
4437 order--;
4438 }
4439 page = alloc_page(gfp_mask);
4440 if (!page)
4441 goto failure;
4442fill_page:
4443 chunk = min_t(unsigned long, data_len,
4444 PAGE_SIZE << order);
4445 skb_fill_page_desc(skb, i, page, 0, chunk);
4446 data_len -= chunk;
4447 npages -= 1 << order;
4448 }
4449 return skb;
4450
4451failure:
4452 kfree_skb(skb);
4453 return NULL;
4454}
4455EXPORT_SYMBOL(alloc_skb_with_frags);