blob: d67e612bf0efc093664aa8d83873e56bf11a4cdf [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)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700439 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700440 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)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700493 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700494 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{
Alexander Duyck181edb22015-05-06 21:12:03 -0700552 unsigned char *head = skb->head;
553
Eric Dumazetd3836f22012-04-27 00:33:38 +0000554 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700555 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000556 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700557 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000558}
559
Adrian Bunk5bba1712006-06-29 13:02:35 -0700560static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Eric Dumazetff04a772014-09-23 18:39:30 -0700562 struct skb_shared_info *shinfo = skb_shinfo(skb);
563 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Eric Dumazetff04a772014-09-23 18:39:30 -0700565 if (skb->cloned &&
566 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
567 &shinfo->dataref))
568 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000569
Eric Dumazetff04a772014-09-23 18:39:30 -0700570 for (i = 0; i < shinfo->nr_frags; i++)
571 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000572
Eric Dumazetff04a772014-09-23 18:39:30 -0700573 /*
574 * If skb buf is from userspace, we need to notify the caller
575 * the lower device DMA has done;
576 */
577 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
578 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Eric Dumazetff04a772014-09-23 18:39:30 -0700580 uarg = shinfo->destructor_arg;
581 if (uarg->callback)
582 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700584
585 if (shinfo->frag_list)
586 kfree_skb_list(shinfo->frag_list);
587
588 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
592 * Free an skbuff by memory without cleaning the state.
593 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800594static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700596 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700597
David S. Millerd179cd12005-08-17 14:57:30 -0700598 switch (skb->fclone) {
599 case SKB_FCLONE_UNAVAILABLE:
600 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800601 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700602
603 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700604 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800605
606 /* We usually free the clone (TX completion) before original skb
607 * This test would have no chance to be true for the clone,
608 * while here, branch prediction will be good.
609 */
610 if (atomic_read(&fclones->fclone_ref) == 1)
611 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700612 break;
613
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800614 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700615 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700616 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700617 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800618 if (!atomic_dec_and_test(&fclones->fclone_ref))
619 return;
620fastpath:
621 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700624static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Eric Dumazetadf30902009-06-02 05:19:30 +0000626 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#ifdef CONFIG_XFRM
628 secpath_put(skb->sp);
629#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700630 if (skb->destructor) {
631 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 skb->destructor(skb);
633 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000634#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700635 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100636#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200637#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 nf_bridge_put(skb->nf_bridge);
639#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700640}
641
642/* Free everything but the sk_buff shell. */
643static void skb_release_all(struct sk_buff *skb)
644{
645 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000646 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000647 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Herbert Xu2d4baff2007-11-26 23:11:19 +0800650/**
651 * __kfree_skb - private function
652 * @skb: buffer
653 *
654 * Free an sk_buff. Release anything attached to the buffer.
655 * Clean the state. This is an internal helper function. Users should
656 * always call kfree_skb
657 */
658
659void __kfree_skb(struct sk_buff *skb)
660{
661 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kfree_skbmem(skb);
663}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800664EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800667 * kfree_skb - free an sk_buff
668 * @skb: buffer to free
669 *
670 * Drop a reference to the buffer and free it if the usage count has
671 * hit zero.
672 */
673void kfree_skb(struct sk_buff *skb)
674{
675 if (unlikely(!skb))
676 return;
677 if (likely(atomic_read(&skb->users) == 1))
678 smp_rmb();
679 else if (likely(!atomic_dec_and_test(&skb->users)))
680 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000681 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800682 __kfree_skb(skb);
683}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800684EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800685
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700686void kfree_skb_list(struct sk_buff *segs)
687{
688 while (segs) {
689 struct sk_buff *next = segs->next;
690
691 kfree_skb(segs);
692 segs = next;
693 }
694}
695EXPORT_SYMBOL(kfree_skb_list);
696
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700697/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000698 * skb_tx_error - report an sk_buff xmit error
699 * @skb: buffer that triggered an error
700 *
701 * Report xmit error if a device callback is tracking this skb.
702 * skb must be freed afterwards.
703 */
704void skb_tx_error(struct sk_buff *skb)
705{
706 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
707 struct ubuf_info *uarg;
708
709 uarg = skb_shinfo(skb)->destructor_arg;
710 if (uarg->callback)
711 uarg->callback(uarg, false);
712 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
713 }
714}
715EXPORT_SYMBOL(skb_tx_error);
716
717/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000718 * consume_skb - free an skbuff
719 * @skb: buffer to free
720 *
721 * Drop a ref to the buffer and free it if the usage count has hit zero
722 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
723 * is being dropped after a failure and notes that
724 */
725void consume_skb(struct sk_buff *skb)
726{
727 if (unlikely(!skb))
728 return;
729 if (likely(atomic_read(&skb->users) == 1))
730 smp_rmb();
731 else if (likely(!atomic_dec_and_test(&skb->users)))
732 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900733 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000734 __kfree_skb(skb);
735}
736EXPORT_SYMBOL(consume_skb);
737
Eric Dumazetb1937222014-09-28 22:18:47 -0700738/* Make sure a field is enclosed inside headers_start/headers_end section */
739#define CHECK_SKB_FIELD(field) \
740 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
741 offsetof(struct sk_buff, headers_start)); \
742 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
743 offsetof(struct sk_buff, headers_end)); \
744
Herbert Xudec18812007-10-14 00:37:30 -0700745static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
746{
747 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700748 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700749 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700750 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000751 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700752#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700753 new->sp = secpath_get(old->sp);
754#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700755 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700756
Eric Dumazetb1937222014-09-28 22:18:47 -0700757 /* Note : this field could be in headers_start/headers_end section
758 * It is not yet because we do not want to have a 16 bit hole
759 */
760 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300761
Eric Dumazetb1937222014-09-28 22:18:47 -0700762 memcpy(&new->headers_start, &old->headers_start,
763 offsetof(struct sk_buff, headers_end) -
764 offsetof(struct sk_buff, headers_start));
765 CHECK_SKB_FIELD(protocol);
766 CHECK_SKB_FIELD(csum);
767 CHECK_SKB_FIELD(hash);
768 CHECK_SKB_FIELD(priority);
769 CHECK_SKB_FIELD(skb_iif);
770 CHECK_SKB_FIELD(vlan_proto);
771 CHECK_SKB_FIELD(vlan_tci);
772 CHECK_SKB_FIELD(transport_header);
773 CHECK_SKB_FIELD(network_header);
774 CHECK_SKB_FIELD(mac_header);
775 CHECK_SKB_FIELD(inner_protocol);
776 CHECK_SKB_FIELD(inner_transport_header);
777 CHECK_SKB_FIELD(inner_network_header);
778 CHECK_SKB_FIELD(inner_mac_header);
779 CHECK_SKB_FIELD(mark);
780#ifdef CONFIG_NETWORK_SECMARK
781 CHECK_SKB_FIELD(secmark);
782#endif
Cong Wange0d10952013-08-01 11:10:25 +0800783#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700784 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300785#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800786#ifdef CONFIG_XPS
787 CHECK_SKB_FIELD(sender_cpu);
788#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700789#ifdef CONFIG_NET_SCHED
790 CHECK_SKB_FIELD(tc_index);
791#ifdef CONFIG_NET_CLS_ACT
792 CHECK_SKB_FIELD(tc_verd);
793#endif
794#endif
795
Herbert Xudec18812007-10-14 00:37:30 -0700796}
797
Herbert Xu82c49a32009-05-22 22:11:37 +0000798/*
799 * You should not add any new code to this function. Add it to
800 * __copy_skb_header above instead.
801 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700802static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#define C(x) n->x = skb->x
805
806 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700808 __copy_skb_header(n, skb);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 C(len);
811 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700812 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700813 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800814 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 C(tail);
818 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800819 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000820 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800821 C(data);
822 C(truesize);
823 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 atomic_inc(&(skb_shinfo(skb)->dataref));
826 skb->cloned = 1;
827
828 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700829#undef C
830}
831
832/**
833 * skb_morph - morph one skb into another
834 * @dst: the skb to receive the contents
835 * @src: the skb to supply the contents
836 *
837 * This is identical to skb_clone except that the target skb is
838 * supplied by the user.
839 *
840 * The target skb is returned upon exit.
841 */
842struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
843{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800844 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700845 return __skb_clone(dst, src);
846}
847EXPORT_SYMBOL_GPL(skb_morph);
848
Ben Hutchings2c530402012-07-10 10:55:09 +0000849/**
850 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000851 * @skb: the skb to modify
852 * @gfp_mask: allocation priority
853 *
854 * This must be called on SKBTX_DEV_ZEROCOPY skb.
855 * It will copy all frags into kernel and drop the reference
856 * to userspace pages.
857 *
858 * If this function is called from an interrupt gfp_mask() must be
859 * %GFP_ATOMIC.
860 *
861 * Returns 0 on success or a negative error code on failure
862 * to allocate kernel memory to copy to.
863 */
864int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000865{
866 int i;
867 int num_frags = skb_shinfo(skb)->nr_frags;
868 struct page *page, *head = NULL;
869 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
870
871 for (i = 0; i < num_frags; i++) {
872 u8 *vaddr;
873 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
874
Krishna Kumar02756ed2012-07-17 02:05:29 +0000875 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000876 if (!page) {
877 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900878 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000879 put_page(head);
880 head = next;
881 }
882 return -ENOMEM;
883 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200884 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000885 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000886 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200887 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900888 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000889 head = page;
890 }
891
892 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000893 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000894 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000895
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000896 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000897
898 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000899 for (i = num_frags - 1; i >= 0; i--) {
900 __skb_fill_page_desc(skb, i, head, 0,
901 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900902 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000903 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000904
905 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000906 return 0;
907}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000908EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000909
Herbert Xue0053ec2007-10-14 00:37:52 -0700910/**
911 * skb_clone - duplicate an sk_buff
912 * @skb: buffer to clone
913 * @gfp_mask: allocation priority
914 *
915 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
916 * copies share the same packet data but not structure. The new
917 * buffer has a reference count of 1. If the allocation fails the
918 * function returns %NULL otherwise the new buffer is returned.
919 *
920 * If this function is called from an interrupt gfp_mask() must be
921 * %GFP_ATOMIC.
922 */
923
924struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
925{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700926 struct sk_buff_fclones *fclones = container_of(skb,
927 struct sk_buff_fclones,
928 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800929 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700930
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000931 if (skb_orphan_frags(skb, gfp_mask))
932 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000933
Herbert Xue0053ec2007-10-14 00:37:52 -0700934 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800935 atomic_read(&fclones->fclone_ref) == 1) {
936 n = &fclones->skb2;
937 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700938 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700939 if (skb_pfmemalloc(skb))
940 gfp_mask |= __GFP_MEMALLOC;
941
Herbert Xue0053ec2007-10-14 00:37:52 -0700942 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
943 if (!n)
944 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200945
946 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700947 n->fclone = SKB_FCLONE_UNAVAILABLE;
948 }
949
950 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800952EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000954static void skb_headers_offset_update(struct sk_buff *skb, int off)
955{
Eric Dumazet030737b2013-10-19 11:42:54 -0700956 /* Only adjust this if it actually is csum_start rather than csum */
957 if (skb->ip_summed == CHECKSUM_PARTIAL)
958 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000959 /* {transport,network,mac}_header and tail are relative to skb->head */
960 skb->transport_header += off;
961 skb->network_header += off;
962 if (skb_mac_header_was_set(skb))
963 skb->mac_header += off;
964 skb->inner_transport_header += off;
965 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000966 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
970{
Herbert Xudec18812007-10-14 00:37:30 -0700971 __copy_skb_header(new, old);
972
Herbert Xu79671682006-06-22 02:40:14 -0700973 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
974 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
975 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Mel Gormanc93bdd02012-07-31 16:44:19 -0700978static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
979{
980 if (skb_pfmemalloc(skb))
981 return SKB_ALLOC_RX;
982 return 0;
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/**
986 * skb_copy - create private copy of an sk_buff
987 * @skb: buffer to copy
988 * @gfp_mask: allocation priority
989 *
990 * Make a copy of both an &sk_buff and its data. This is used when the
991 * caller wishes to modify the data and needs a private copy of the
992 * data to alter. Returns %NULL on failure or the pointer to the buffer
993 * on success. The returned buffer has a reference count of 1.
994 *
995 * As by-product this function converts non-linear &sk_buff to linear
996 * one, so that &sk_buff becomes completely private and caller is allowed
997 * to modify all the data of returned buffer. This means that this
998 * function is not recommended for use in circumstances when only
999 * header is going to be modified. Use pskb_copy() instead.
1000 */
1001
Al Virodd0fc662005-10-07 07:46:04 +01001002struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001004 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001005 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001006 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1007 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (!n)
1010 return NULL;
1011
1012 /* Set the data pointer */
1013 skb_reserve(n, headerlen);
1014 /* Set the tail pointer and length */
1015 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1018 BUG();
1019
1020 copy_skb_header(n, skb);
1021 return n;
1022}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001023EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001026 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001028 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001030 * @fclone: if true allocate the copy of the skb from the fclone
1031 * cache instead of the head cache; it is recommended to set this
1032 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
1034 * Make a copy of both an &sk_buff and part of its data, located
1035 * in header. Fragmented data remain shared. This is used when
1036 * the caller wishes to modify only header of &sk_buff and needs
1037 * private copy of the header to alter. Returns %NULL on failure
1038 * or the pointer to the buffer on success.
1039 * The returned buffer has a reference count of 1.
1040 */
1041
Octavian Purdilabad93e92014-06-12 01:36:26 +03001042struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1043 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Eric Dumazet117632e2011-12-03 21:39:53 +00001045 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001046 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1047 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!n)
1050 goto out;
1051
1052 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001053 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Set the tail pointer and length */
1055 skb_put(n, skb_headlen(skb));
1056 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001057 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Herbert Xu25f484a2006-11-07 14:57:15 -08001059 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 n->data_len = skb->data_len;
1061 n->len = skb->len;
1062
1063 if (skb_shinfo(skb)->nr_frags) {
1064 int i;
1065
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001066 if (skb_orphan_frags(skb, gfp_mask)) {
1067 kfree_skb(n);
1068 n = NULL;
1069 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1072 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001073 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 skb_shinfo(n)->nr_frags = i;
1076 }
1077
David S. Miller21dc3302010-08-23 00:13:46 -07001078 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1080 skb_clone_fraglist(n);
1081 }
1082
1083 copy_skb_header(n, skb);
1084out:
1085 return n;
1086}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001087EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089/**
1090 * pskb_expand_head - reallocate header of &sk_buff
1091 * @skb: buffer to reallocate
1092 * @nhead: room to add at head
1093 * @ntail: room to add at tail
1094 * @gfp_mask: allocation priority
1095 *
Mathias Krausebc323832013-11-07 14:18:26 +01001096 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1097 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * reference count of 1. Returns zero in the case of success or error,
1099 * if expansion failed. In the last case, &sk_buff is not changed.
1100 *
1101 * All the pointers pointing into skb header may change and must be
1102 * reloaded after call to this function.
1103 */
1104
Victor Fusco86a76ca2005-07-08 14:57:47 -07001105int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001106 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 int i;
1109 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001110 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 long off;
1112
Herbert Xu4edd87a2008-10-01 07:09:38 -07001113 BUG_ON(nhead < 0);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (skb_shared(skb))
1116 BUG();
1117
1118 size = SKB_DATA_ALIGN(size);
1119
Mel Gormanc93bdd02012-07-31 16:44:19 -07001120 if (skb_pfmemalloc(skb))
1121 gfp_mask |= __GFP_MEMALLOC;
1122 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1123 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!data)
1125 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001126 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001129 * optimized for the cases when header is void.
1130 */
1131 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1132
1133 memcpy((struct skb_shared_info *)(data + size),
1134 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001135 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alexander Duyck3e245912012-05-04 14:26:51 +00001137 /*
1138 * if shinfo is shared we must drop the old head gracefully, but if it
1139 * is not we can just drop the old head and let the existing refcount
1140 * be since all we did is relocate the values
1141 */
1142 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001143 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001144 if (skb_orphan_frags(skb, gfp_mask))
1145 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001146 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001147 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Eric Dumazet1fd63042010-09-02 23:09:32 +00001149 if (skb_has_frag_list(skb))
1150 skb_clone_fraglist(skb);
1151
1152 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001153 } else {
1154 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 off = (data + nhead) - skb->head;
1157
1158 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001159 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001161#ifdef NET_SKBUFF_DATA_USES_OFFSET
1162 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001163 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001164#else
1165 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001166#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001167 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001168 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001170 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 skb->nohdr = 0;
1172 atomic_set(&skb_shinfo(skb)->dataref, 1);
1173 return 0;
1174
Shirley Maa6686f22011-07-06 12:22:12 +00001175nofrags:
1176 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177nodata:
1178 return -ENOMEM;
1179}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001180EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182/* Make private copy of skb with writable head and some headroom */
1183
1184struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1185{
1186 struct sk_buff *skb2;
1187 int delta = headroom - skb_headroom(skb);
1188
1189 if (delta <= 0)
1190 skb2 = pskb_copy(skb, GFP_ATOMIC);
1191 else {
1192 skb2 = skb_clone(skb, GFP_ATOMIC);
1193 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1194 GFP_ATOMIC)) {
1195 kfree_skb(skb2);
1196 skb2 = NULL;
1197 }
1198 }
1199 return skb2;
1200}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001201EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203/**
1204 * skb_copy_expand - copy and expand sk_buff
1205 * @skb: buffer to copy
1206 * @newheadroom: new free bytes at head
1207 * @newtailroom: new free bytes at tail
1208 * @gfp_mask: allocation priority
1209 *
1210 * Make a copy of both an &sk_buff and its data and while doing so
1211 * allocate additional space.
1212 *
1213 * This is used when the caller wishes to modify the data and needs a
1214 * private copy of the data to alter as well as more space for new fields.
1215 * Returns %NULL on failure or the pointer to the buffer
1216 * on success. The returned buffer has a reference count of 1.
1217 *
1218 * You must pass %GFP_ATOMIC as the allocation priority if this function
1219 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
1221struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001222 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001223 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 /*
1226 * Allocate the copy buffer
1227 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001228 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1229 gfp_mask, skb_alloc_rx_flag(skb),
1230 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001231 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int head_copy_len, head_copy_off;
1233
1234 if (!n)
1235 return NULL;
1236
1237 skb_reserve(n, newheadroom);
1238
1239 /* Set the tail pointer and length */
1240 skb_put(n, skb->len);
1241
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001242 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 head_copy_off = 0;
1244 if (newheadroom <= head_copy_len)
1245 head_copy_len = newheadroom;
1246 else
1247 head_copy_off = newheadroom - head_copy_len;
1248
1249 /* Copy the linear header and data. */
1250 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1251 skb->len + head_copy_len))
1252 BUG();
1253
1254 copy_skb_header(n, skb);
1255
Eric Dumazet030737b2013-10-19 11:42:54 -07001256 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return n;
1259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001260EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262/**
1263 * skb_pad - zero pad the tail of an skb
1264 * @skb: buffer to pad
1265 * @pad: space to pad
1266 *
1267 * Ensure that a buffer is followed by a padding area that is zero
1268 * filled. Used by network drivers which may DMA or transfer data
1269 * beyond the buffer end onto the wire.
1270 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001271 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001273
Herbert Xu5b057c62006-06-23 02:06:41 -07001274int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Herbert Xu5b057c62006-06-23 02:06:41 -07001276 int err;
1277 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001280 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001284
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001285 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001286 if (likely(skb_cloned(skb) || ntail > 0)) {
1287 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1288 if (unlikely(err))
1289 goto free_skb;
1290 }
1291
1292 /* FIXME: The use of this function with non-linear skb's really needs
1293 * to be audited.
1294 */
1295 err = skb_linearize(skb);
1296 if (unlikely(err))
1297 goto free_skb;
1298
1299 memset(skb->data + skb->len, 0, pad);
1300 return 0;
1301
1302free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001304 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001305}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001306EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001307
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001308/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001309 * pskb_put - add data to the tail of a potentially fragmented buffer
1310 * @skb: start of the buffer to use
1311 * @tail: tail fragment of the buffer to use
1312 * @len: amount of data to add
1313 *
1314 * This function extends the used data area of the potentially
1315 * fragmented buffer. @tail must be the last fragment of @skb -- or
1316 * @skb itself. If this would exceed the total buffer size the kernel
1317 * will panic. A pointer to the first byte of the extra data is
1318 * returned.
1319 */
1320
1321unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1322{
1323 if (tail != skb) {
1324 skb->data_len += len;
1325 skb->len += len;
1326 }
1327 return skb_put(tail, len);
1328}
1329EXPORT_SYMBOL_GPL(pskb_put);
1330
1331/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001332 * skb_put - add data to a buffer
1333 * @skb: buffer to use
1334 * @len: amount of data to add
1335 *
1336 * This function extends the used data area of the buffer. If this would
1337 * exceed the total buffer size the kernel will panic. A pointer to the
1338 * first byte of the extra data is returned.
1339 */
1340unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1341{
1342 unsigned char *tmp = skb_tail_pointer(skb);
1343 SKB_LINEAR_ASSERT(skb);
1344 skb->tail += len;
1345 skb->len += len;
1346 if (unlikely(skb->tail > skb->end))
1347 skb_over_panic(skb, len, __builtin_return_address(0));
1348 return tmp;
1349}
1350EXPORT_SYMBOL(skb_put);
1351
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001352/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001353 * skb_push - add data to the start of a buffer
1354 * @skb: buffer to use
1355 * @len: amount of data to add
1356 *
1357 * This function extends the used data area of the buffer at the buffer
1358 * start. If this would exceed the total buffer headroom the kernel will
1359 * panic. A pointer to the first byte of the extra data is returned.
1360 */
1361unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1362{
1363 skb->data -= len;
1364 skb->len += len;
1365 if (unlikely(skb->data<skb->head))
1366 skb_under_panic(skb, len, __builtin_return_address(0));
1367 return skb->data;
1368}
1369EXPORT_SYMBOL(skb_push);
1370
1371/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001372 * skb_pull - remove data from the start of a buffer
1373 * @skb: buffer to use
1374 * @len: amount of data to remove
1375 *
1376 * This function removes data from the start of a buffer, returning
1377 * the memory to the headroom. A pointer to the next data in the buffer
1378 * is returned. Once the data has been pulled future pushes will overwrite
1379 * the old data.
1380 */
1381unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1382{
David S. Miller47d29642010-05-02 02:21:44 -07001383 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001384}
1385EXPORT_SYMBOL(skb_pull);
1386
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001387/**
1388 * skb_trim - remove end from a buffer
1389 * @skb: buffer to alter
1390 * @len: new length
1391 *
1392 * Cut the length of a buffer down by removing data from the tail. If
1393 * the buffer is already under the length specified it is not modified.
1394 * The skb must be linear.
1395 */
1396void skb_trim(struct sk_buff *skb, unsigned int len)
1397{
1398 if (skb->len > len)
1399 __skb_trim(skb, len);
1400}
1401EXPORT_SYMBOL(skb_trim);
1402
Herbert Xu3cc0e872006-06-09 16:13:38 -07001403/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
1405
Herbert Xu3cc0e872006-06-09 16:13:38 -07001406int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Herbert Xu27b437c2006-07-13 19:26:39 -07001408 struct sk_buff **fragp;
1409 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int offset = skb_headlen(skb);
1411 int nfrags = skb_shinfo(skb)->nr_frags;
1412 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001413 int err;
1414
1415 if (skb_cloned(skb) &&
1416 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001419 i = 0;
1420 if (offset >= len)
1421 goto drop_pages;
1422
1423 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001424 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001425
1426 if (end < len) {
1427 offset = end;
1428 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001430
Eric Dumazet9e903e02011-10-18 21:00:24 +00001431 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001432
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001433drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001434 skb_shinfo(skb)->nr_frags = i;
1435
1436 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001437 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001438
David S. Miller21dc3302010-08-23 00:13:46 -07001439 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001440 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001441 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Herbert Xu27b437c2006-07-13 19:26:39 -07001444 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1445 fragp = &frag->next) {
1446 int end = offset + frag->len;
1447
1448 if (skb_shared(frag)) {
1449 struct sk_buff *nfrag;
1450
1451 nfrag = skb_clone(frag, GFP_ATOMIC);
1452 if (unlikely(!nfrag))
1453 return -ENOMEM;
1454
1455 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001456 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001457 frag = nfrag;
1458 *fragp = frag;
1459 }
1460
1461 if (end < len) {
1462 offset = end;
1463 continue;
1464 }
1465
1466 if (end > len &&
1467 unlikely((err = pskb_trim(frag, len - offset))))
1468 return err;
1469
1470 if (frag->next)
1471 skb_drop_list(&frag->next);
1472 break;
1473 }
1474
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001475done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001476 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 skb->data_len -= skb->len - len;
1478 skb->len = len;
1479 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001480 skb->len = len;
1481 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001482 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
1485 return 0;
1486}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001487EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489/**
1490 * __pskb_pull_tail - advance tail of skb header
1491 * @skb: buffer to reallocate
1492 * @delta: number of bytes to advance tail
1493 *
1494 * The function makes a sense only on a fragmented &sk_buff,
1495 * it expands header moving its tail forward and copying necessary
1496 * data from fragmented part.
1497 *
1498 * &sk_buff MUST have reference count of 1.
1499 *
1500 * Returns %NULL (and &sk_buff does not change) if pull failed
1501 * or value of new tail of skb in the case of success.
1502 *
1503 * All the pointers pointing into skb header may change and must be
1504 * reloaded after call to this function.
1505 */
1506
1507/* Moves tail of skb head forward, copying data from fragmented part,
1508 * when it is necessary.
1509 * 1. It may fail due to malloc failure.
1510 * 2. It may change skb pointers.
1511 *
1512 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1513 */
1514unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1515{
1516 /* If skb has not enough free space at tail, get new one
1517 * plus 128 bytes for future expansions. If we have enough
1518 * room at tail, reallocate without expansion only if skb is cloned.
1519 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001520 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (eat > 0 || skb_cloned(skb)) {
1523 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1524 GFP_ATOMIC))
1525 return NULL;
1526 }
1527
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001528 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 BUG();
1530
1531 /* Optimization: no fragments, no reasons to preestimate
1532 * size of pulled pages. Superb.
1533 */
David S. Miller21dc3302010-08-23 00:13:46 -07001534 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto pull_pages;
1536
1537 /* Estimate size of pulled pages. */
1538 eat = delta;
1539 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001540 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1541
1542 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001544 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
1547 /* If we need update frag list, we are in troubles.
1548 * Certainly, it possible to add an offset to skb data,
1549 * but taking into account that pulling is expected to
1550 * be very rare operation, it is worth to fight against
1551 * further bloating skb head and crucify ourselves here instead.
1552 * Pure masohism, indeed. 8)8)
1553 */
1554 if (eat) {
1555 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1556 struct sk_buff *clone = NULL;
1557 struct sk_buff *insp = NULL;
1558
1559 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001560 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 if (list->len <= eat) {
1563 /* Eaten as whole. */
1564 eat -= list->len;
1565 list = list->next;
1566 insp = list;
1567 } else {
1568 /* Eaten partially. */
1569
1570 if (skb_shared(list)) {
1571 /* Sucks! We need to fork list. :-( */
1572 clone = skb_clone(list, GFP_ATOMIC);
1573 if (!clone)
1574 return NULL;
1575 insp = list->next;
1576 list = clone;
1577 } else {
1578 /* This may be pulled without
1579 * problems. */
1580 insp = list;
1581 }
1582 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001583 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return NULL;
1585 }
1586 break;
1587 }
1588 } while (eat);
1589
1590 /* Free pulled out fragments. */
1591 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1592 skb_shinfo(skb)->frag_list = list->next;
1593 kfree_skb(list);
1594 }
1595 /* And insert new clone at head. */
1596 if (clone) {
1597 clone->next = list;
1598 skb_shinfo(skb)->frag_list = clone;
1599 }
1600 }
1601 /* Success! Now we may commit changes to skb data. */
1602
1603pull_pages:
1604 eat = delta;
1605 k = 0;
1606 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001607 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1608
1609 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001610 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001611 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 } else {
1613 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1614 if (eat) {
1615 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001616 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 eat = 0;
1618 }
1619 k++;
1620 }
1621 }
1622 skb_shinfo(skb)->nr_frags = k;
1623
1624 skb->tail += delta;
1625 skb->data_len -= delta;
1626
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001627 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001629EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Eric Dumazet22019b12011-07-29 18:37:31 +00001631/**
1632 * skb_copy_bits - copy bits from skb to kernel buffer
1633 * @skb: source skb
1634 * @offset: offset in source
1635 * @to: destination buffer
1636 * @len: number of bytes to copy
1637 *
1638 * Copy the specified number of bytes from the source skb to the
1639 * destination buffer.
1640 *
1641 * CAUTION ! :
1642 * If its prototype is ever changed,
1643 * check arch/{*}/net/{*}.S files,
1644 * since it is called from BPF assembly code.
1645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1647{
David S. Miller1a028e52007-04-27 15:21:23 -07001648 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001649 struct sk_buff *frag_iter;
1650 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (offset > (int)skb->len - len)
1653 goto fault;
1654
1655 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001656 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (copy > len)
1658 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001659 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if ((len -= copy) == 0)
1661 return 0;
1662 offset += copy;
1663 to += copy;
1664 }
1665
1666 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001667 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001668 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001670 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001671
Eric Dumazet51c56b02012-04-05 11:35:15 +02001672 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if ((copy = end - offset) > 0) {
1674 u8 *vaddr;
1675
1676 if (copy > len)
1677 copy = len;
1678
Eric Dumazet51c56b02012-04-05 11:35:15 +02001679 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001681 vaddr + f->page_offset + offset - start,
1682 copy);
1683 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if ((len -= copy) == 0)
1686 return 0;
1687 offset += copy;
1688 to += copy;
1689 }
David S. Miller1a028e52007-04-27 15:21:23 -07001690 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
David S. Millerfbb398a2009-06-09 00:18:59 -07001693 skb_walk_frags(skb, frag_iter) {
1694 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David S. Millerfbb398a2009-06-09 00:18:59 -07001696 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
David S. Millerfbb398a2009-06-09 00:18:59 -07001698 end = start + frag_iter->len;
1699 if ((copy = end - offset) > 0) {
1700 if (copy > len)
1701 copy = len;
1702 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1703 goto fault;
1704 if ((len -= copy) == 0)
1705 return 0;
1706 offset += copy;
1707 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001709 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Shirley Maa6686f22011-07-06 12:22:12 +00001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!len)
1713 return 0;
1714
1715fault:
1716 return -EFAULT;
1717}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001718EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jens Axboe9c55e012007-11-06 23:30:13 -08001720/*
1721 * Callback from splice_to_pipe(), if we need to release some pages
1722 * at the end of the spd in case we error'ed out in filling the pipe.
1723 */
1724static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1725{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001726 put_page(spd->pages[i]);
1727}
Jens Axboe9c55e012007-11-06 23:30:13 -08001728
David S. Millera108d5f2012-04-23 23:06:11 -04001729static struct page *linear_to_page(struct page *page, unsigned int *len,
1730 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001731 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001732{
Eric Dumazet5640f762012-09-23 23:04:42 +00001733 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001734
Eric Dumazet5640f762012-09-23 23:04:42 +00001735 if (!sk_page_frag_refill(sk, pfrag))
1736 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001737
Eric Dumazet5640f762012-09-23 23:04:42 +00001738 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001739
Eric Dumazet5640f762012-09-23 23:04:42 +00001740 memcpy(page_address(pfrag->page) + pfrag->offset,
1741 page_address(page) + *offset, *len);
1742 *offset = pfrag->offset;
1743 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001744
Eric Dumazet5640f762012-09-23 23:04:42 +00001745 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001746}
1747
Eric Dumazet41c73a02012-04-22 12:26:16 +00001748static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1749 struct page *page,
1750 unsigned int offset)
1751{
1752 return spd->nr_pages &&
1753 spd->pages[spd->nr_pages - 1] == page &&
1754 (spd->partial[spd->nr_pages - 1].offset +
1755 spd->partial[spd->nr_pages - 1].len == offset);
1756}
1757
Jens Axboe9c55e012007-11-06 23:30:13 -08001758/*
1759 * Fill page/offset/length into spd, if it can hold more pages.
1760 */
David S. Millera108d5f2012-04-23 23:06:11 -04001761static bool spd_fill_page(struct splice_pipe_desc *spd,
1762 struct pipe_inode_info *pipe, struct page *page,
1763 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001764 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001765 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001766{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001767 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001768 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001769
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001770 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001771 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001772 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001773 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001774 }
1775 if (spd_can_coalesce(spd, page, offset)) {
1776 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001777 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001778 }
1779 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001780 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001781 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001782 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001783 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001784
David S. Millera108d5f2012-04-23 23:06:11 -04001785 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001786}
1787
David S. Millera108d5f2012-04-23 23:06:11 -04001788static bool __splice_segment(struct page *page, unsigned int poff,
1789 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001790 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001791 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001792 struct sock *sk,
1793 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001794{
1795 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001796 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001797
1798 /* skip this segment if already processed */
1799 if (*off >= plen) {
1800 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001801 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001802 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001803
Octavian Purdila2870c432008-07-15 00:49:11 -07001804 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001805 poff += *off;
1806 plen -= *off;
1807 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001808
Eric Dumazet18aafc62013-01-11 14:46:37 +00001809 do {
1810 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001811
Eric Dumazet18aafc62013-01-11 14:46:37 +00001812 if (spd_fill_page(spd, pipe, page, &flen, poff,
1813 linear, sk))
1814 return true;
1815 poff += flen;
1816 plen -= flen;
1817 *len -= flen;
1818 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001819
David S. Millera108d5f2012-04-23 23:06:11 -04001820 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001821}
1822
1823/*
David S. Millera108d5f2012-04-23 23:06:11 -04001824 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001825 * pipe is full or if we already spliced the requested length.
1826 */
David S. Millera108d5f2012-04-23 23:06:11 -04001827static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1828 unsigned int *offset, unsigned int *len,
1829 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001830{
1831 int seg;
1832
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001833 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001834 * If skb->head_frag is set, this 'linear' part is backed by a
1835 * fragment, and if the head is not shared with any clones then
1836 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001837 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001838 if (__splice_segment(virt_to_page(skb->data),
1839 (unsigned long) skb->data & (PAGE_SIZE - 1),
1840 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001841 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001842 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001843 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001844 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001845
1846 /*
1847 * then map the fragments
1848 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001849 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1850 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1851
Ian Campbellea2ab692011-08-22 23:44:58 +00001852 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001853 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001854 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001855 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001856 }
1857
David S. Millera108d5f2012-04-23 23:06:11 -04001858 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001859}
1860
1861/*
1862 * Map data from the skb to a pipe. Should handle both the linear part,
1863 * the fragments, and the frag list. It does NOT handle frag lists within
1864 * the frag list, if such a thing exists. We'd probably need to recurse to
1865 * handle that cleanly.
1866 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001867int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001868 struct pipe_inode_info *pipe, unsigned int tlen,
1869 unsigned int flags)
1870{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001871 struct partial_page partial[MAX_SKB_FRAGS];
1872 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001873 struct splice_pipe_desc spd = {
1874 .pages = pages,
1875 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001876 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001877 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001878 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001879 .spd_release = sock_spd_release,
1880 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001881 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001882 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001883 int ret = 0;
1884
Jens Axboe9c55e012007-11-06 23:30:13 -08001885 /*
1886 * __skb_splice_bits() only fails if the output has no room left,
1887 * so no point in going over the frag_list for the error case.
1888 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001889 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001890 goto done;
1891 else if (!tlen)
1892 goto done;
1893
1894 /*
1895 * now see if we have a frag_list to map
1896 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001897 skb_walk_frags(skb, frag_iter) {
1898 if (!tlen)
1899 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001900 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001901 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001902 }
1903
1904done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001905 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001906 /*
1907 * Drop the socket lock, otherwise we have reverse
1908 * locking dependencies between sk_lock and i_mutex
1909 * here as compared to sendfile(). We enter here
1910 * with the socket lock held, and splice_to_pipe() will
1911 * grab the pipe inode lock. For sendfile() emulation,
1912 * we call into ->sendpage() with the i_mutex lock held
1913 * and networking will grab the socket lock.
1914 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001915 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001916 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001917 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001918 }
1919
Jens Axboe35f3d142010-05-20 10:43:18 +02001920 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001921}
1922
Herbert Xu357b40a2005-04-19 22:30:14 -07001923/**
1924 * skb_store_bits - store bits from kernel buffer to skb
1925 * @skb: destination buffer
1926 * @offset: offset in destination
1927 * @from: source buffer
1928 * @len: number of bytes to copy
1929 *
1930 * Copy the specified number of bytes from the source buffer to the
1931 * destination skb. This function handles all the messy bits of
1932 * traversing fragment lists and such.
1933 */
1934
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001935int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001936{
David S. Miller1a028e52007-04-27 15:21:23 -07001937 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001938 struct sk_buff *frag_iter;
1939 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001940
1941 if (offset > (int)skb->len - len)
1942 goto fault;
1943
David S. Miller1a028e52007-04-27 15:21:23 -07001944 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001945 if (copy > len)
1946 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001947 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001948 if ((len -= copy) == 0)
1949 return 0;
1950 offset += copy;
1951 from += copy;
1952 }
1953
1954 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1955 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001956 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001957
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001958 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001959
Eric Dumazet9e903e02011-10-18 21:00:24 +00001960 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001961 if ((copy = end - offset) > 0) {
1962 u8 *vaddr;
1963
1964 if (copy > len)
1965 copy = len;
1966
Eric Dumazet51c56b02012-04-05 11:35:15 +02001967 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001968 memcpy(vaddr + frag->page_offset + offset - start,
1969 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001970 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001971
1972 if ((len -= copy) == 0)
1973 return 0;
1974 offset += copy;
1975 from += copy;
1976 }
David S. Miller1a028e52007-04-27 15:21:23 -07001977 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001978 }
1979
David S. Millerfbb398a2009-06-09 00:18:59 -07001980 skb_walk_frags(skb, frag_iter) {
1981 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001982
David S. Millerfbb398a2009-06-09 00:18:59 -07001983 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001984
David S. Millerfbb398a2009-06-09 00:18:59 -07001985 end = start + frag_iter->len;
1986 if ((copy = end - offset) > 0) {
1987 if (copy > len)
1988 copy = len;
1989 if (skb_store_bits(frag_iter, offset - start,
1990 from, copy))
1991 goto fault;
1992 if ((len -= copy) == 0)
1993 return 0;
1994 offset += copy;
1995 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001996 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001997 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001998 }
1999 if (!len)
2000 return 0;
2001
2002fault:
2003 return -EFAULT;
2004}
Herbert Xu357b40a2005-04-19 22:30:14 -07002005EXPORT_SYMBOL(skb_store_bits);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002008__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2009 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
David S. Miller1a028e52007-04-27 15:21:23 -07002011 int start = skb_headlen(skb);
2012 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002013 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int pos = 0;
2015
2016 /* Checksum header. */
2017 if (copy > 0) {
2018 if (copy > len)
2019 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002020 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if ((len -= copy) == 0)
2022 return csum;
2023 offset += copy;
2024 pos = copy;
2025 }
2026
2027 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002028 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002029 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002031 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002032
Eric Dumazet51c56b02012-04-05 11:35:15 +02002033 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002035 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 if (copy > len)
2039 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002040 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002041 csum2 = ops->update(vaddr + frag->page_offset +
2042 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002043 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002044 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (!(len -= copy))
2046 return csum;
2047 offset += copy;
2048 pos += copy;
2049 }
David S. Miller1a028e52007-04-27 15:21:23 -07002050 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
David S. Millerfbb398a2009-06-09 00:18:59 -07002053 skb_walk_frags(skb, frag_iter) {
2054 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
David S. Millerfbb398a2009-06-09 00:18:59 -07002056 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
David S. Millerfbb398a2009-06-09 00:18:59 -07002058 end = start + frag_iter->len;
2059 if ((copy = end - offset) > 0) {
2060 __wsum csum2;
2061 if (copy > len)
2062 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002063 csum2 = __skb_checksum(frag_iter, offset - start,
2064 copy, 0, ops);
2065 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002066 if ((len -= copy) == 0)
2067 return csum;
2068 offset += copy;
2069 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002071 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002073 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 return csum;
2076}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002077EXPORT_SYMBOL(__skb_checksum);
2078
2079__wsum skb_checksum(const struct sk_buff *skb, int offset,
2080 int len, __wsum csum)
2081{
2082 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002083 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002084 .combine = csum_block_add_ext,
2085 };
2086
2087 return __skb_checksum(skb, offset, len, csum, &ops);
2088}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002089EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091/* Both of above in one bottle. */
2092
Al Viro81d77662006-11-14 21:37:33 -08002093__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2094 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
David S. Miller1a028e52007-04-27 15:21:23 -07002096 int start = skb_headlen(skb);
2097 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002098 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int pos = 0;
2100
2101 /* Copy header. */
2102 if (copy > 0) {
2103 if (copy > len)
2104 copy = len;
2105 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2106 copy, csum);
2107 if ((len -= copy) == 0)
2108 return csum;
2109 offset += copy;
2110 to += copy;
2111 pos = copy;
2112 }
2113
2114 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002115 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002117 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002118
Eric Dumazet9e903e02011-10-18 21:00:24 +00002119 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002121 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 u8 *vaddr;
2123 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2124
2125 if (copy > len)
2126 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002127 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002129 frag->page_offset +
2130 offset - start, to,
2131 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002132 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 csum = csum_block_add(csum, csum2, pos);
2134 if (!(len -= copy))
2135 return csum;
2136 offset += copy;
2137 to += copy;
2138 pos += copy;
2139 }
David S. Miller1a028e52007-04-27 15:21:23 -07002140 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
David S. Millerfbb398a2009-06-09 00:18:59 -07002143 skb_walk_frags(skb, frag_iter) {
2144 __wsum csum2;
2145 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
David S. Millerfbb398a2009-06-09 00:18:59 -07002147 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
David S. Millerfbb398a2009-06-09 00:18:59 -07002149 end = start + frag_iter->len;
2150 if ((copy = end - offset) > 0) {
2151 if (copy > len)
2152 copy = len;
2153 csum2 = skb_copy_and_csum_bits(frag_iter,
2154 offset - start,
2155 to, copy, 0);
2156 csum = csum_block_add(csum, csum2, pos);
2157 if ((len -= copy) == 0)
2158 return csum;
2159 offset += copy;
2160 to += copy;
2161 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002163 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002165 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return csum;
2167}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002168EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Thomas Grafaf2806f2013-12-13 15:22:17 +01002170 /**
2171 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2172 * @from: source buffer
2173 *
2174 * Calculates the amount of linear headroom needed in the 'to' skb passed
2175 * into skb_zerocopy().
2176 */
2177unsigned int
2178skb_zerocopy_headlen(const struct sk_buff *from)
2179{
2180 unsigned int hlen = 0;
2181
2182 if (!from->head_frag ||
2183 skb_headlen(from) < L1_CACHE_BYTES ||
2184 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2185 hlen = skb_headlen(from);
2186
2187 if (skb_has_frag_list(from))
2188 hlen = from->len;
2189
2190 return hlen;
2191}
2192EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2193
2194/**
2195 * skb_zerocopy - Zero copy skb to skb
2196 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002197 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002198 * @len: number of bytes to copy from source buffer
2199 * @hlen: size of linear headroom in destination buffer
2200 *
2201 * Copies up to `len` bytes from `from` to `to` by creating references
2202 * to the frags in the source buffer.
2203 *
2204 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2205 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002206 *
2207 * Return value:
2208 * 0: everything is OK
2209 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2210 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002211 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002212int
2213skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002214{
2215 int i, j = 0;
2216 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002217 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002218 struct page *page;
2219 unsigned int offset;
2220
2221 BUG_ON(!from->head_frag && !hlen);
2222
2223 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002224 if (len <= skb_tailroom(to))
2225 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002226
2227 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002228 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2229 if (unlikely(ret))
2230 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002231 len -= hlen;
2232 } else {
2233 plen = min_t(int, skb_headlen(from), len);
2234 if (plen) {
2235 page = virt_to_head_page(from->head);
2236 offset = from->data - (unsigned char *)page_address(page);
2237 __skb_fill_page_desc(to, 0, page, offset, plen);
2238 get_page(page);
2239 j = 1;
2240 len -= plen;
2241 }
2242 }
2243
2244 to->truesize += len + plen;
2245 to->len += len + plen;
2246 to->data_len += len + plen;
2247
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002248 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2249 skb_tx_error(from);
2250 return -ENOMEM;
2251 }
2252
Thomas Grafaf2806f2013-12-13 15:22:17 +01002253 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2254 if (!len)
2255 break;
2256 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2257 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2258 len -= skb_shinfo(to)->frags[j].size;
2259 skb_frag_ref(to, j);
2260 j++;
2261 }
2262 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002263
2264 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002265}
2266EXPORT_SYMBOL_GPL(skb_zerocopy);
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2269{
Al Virod3bc23e2006-11-14 21:24:49 -08002270 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 long csstart;
2272
Patrick McHardy84fa7932006-08-29 16:44:56 -07002273 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002274 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 else
2276 csstart = skb_headlen(skb);
2277
Kris Katterjohn09a62662006-01-08 22:24:28 -08002278 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002280 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 csum = 0;
2283 if (csstart != skb->len)
2284 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2285 skb->len - csstart, 0);
2286
Patrick McHardy84fa7932006-08-29 16:44:56 -07002287 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002288 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Al Virod3bc23e2006-11-14 21:24:49 -08002290 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002293EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295/**
2296 * skb_dequeue - remove from the head of the queue
2297 * @list: list to dequeue from
2298 *
2299 * Remove the head of the list. The list lock is taken so the function
2300 * may be used safely with other locking list functions. The head item is
2301 * returned or %NULL if the list is empty.
2302 */
2303
2304struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2305{
2306 unsigned long flags;
2307 struct sk_buff *result;
2308
2309 spin_lock_irqsave(&list->lock, flags);
2310 result = __skb_dequeue(list);
2311 spin_unlock_irqrestore(&list->lock, flags);
2312 return result;
2313}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002314EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316/**
2317 * skb_dequeue_tail - remove from the tail of the queue
2318 * @list: list to dequeue from
2319 *
2320 * Remove the tail of the list. The list lock is taken so the function
2321 * may be used safely with other locking list functions. The tail item is
2322 * returned or %NULL if the list is empty.
2323 */
2324struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2325{
2326 unsigned long flags;
2327 struct sk_buff *result;
2328
2329 spin_lock_irqsave(&list->lock, flags);
2330 result = __skb_dequeue_tail(list);
2331 spin_unlock_irqrestore(&list->lock, flags);
2332 return result;
2333}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002334EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336/**
2337 * skb_queue_purge - empty a list
2338 * @list: list to empty
2339 *
2340 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2341 * the list and one reference dropped. This function takes the list
2342 * lock and is atomic with respect to other list locking functions.
2343 */
2344void skb_queue_purge(struct sk_buff_head *list)
2345{
2346 struct sk_buff *skb;
2347 while ((skb = skb_dequeue(list)) != NULL)
2348 kfree_skb(skb);
2349}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002350EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352/**
2353 * skb_queue_head - queue a buffer at the list head
2354 * @list: list to use
2355 * @newsk: buffer to queue
2356 *
2357 * Queue a buffer at the start of the list. This function takes the
2358 * list lock and can be used safely with other locking &sk_buff functions
2359 * safely.
2360 *
2361 * A buffer cannot be placed on two lists at the same time.
2362 */
2363void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2364{
2365 unsigned long flags;
2366
2367 spin_lock_irqsave(&list->lock, flags);
2368 __skb_queue_head(list, newsk);
2369 spin_unlock_irqrestore(&list->lock, flags);
2370}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002371EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373/**
2374 * skb_queue_tail - queue a buffer at the list tail
2375 * @list: list to use
2376 * @newsk: buffer to queue
2377 *
2378 * Queue a buffer at the tail of the list. This function takes the
2379 * list lock and can be used safely with other locking &sk_buff functions
2380 * safely.
2381 *
2382 * A buffer cannot be placed on two lists at the same time.
2383 */
2384void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2385{
2386 unsigned long flags;
2387
2388 spin_lock_irqsave(&list->lock, flags);
2389 __skb_queue_tail(list, newsk);
2390 spin_unlock_irqrestore(&list->lock, flags);
2391}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002392EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394/**
2395 * skb_unlink - remove a buffer from a list
2396 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002397 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 *
David S. Miller8728b832005-08-09 19:25:21 -07002399 * Remove a packet from a list. The list locks are taken and this
2400 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 *
David S. Miller8728b832005-08-09 19:25:21 -07002402 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 */
David S. Miller8728b832005-08-09 19:25:21 -07002404void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
David S. Miller8728b832005-08-09 19:25:21 -07002406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
David S. Miller8728b832005-08-09 19:25:21 -07002408 spin_lock_irqsave(&list->lock, flags);
2409 __skb_unlink(skb, list);
2410 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002412EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/**
2415 * skb_append - append a buffer
2416 * @old: buffer to insert after
2417 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002418 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 *
2420 * Place a packet after a given packet in a list. The list locks are taken
2421 * and this function is atomic with respect to other list locked calls.
2422 * A buffer cannot be placed on two lists at the same time.
2423 */
David S. Miller8728b832005-08-09 19:25:21 -07002424void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 unsigned long flags;
2427
David S. Miller8728b832005-08-09 19:25:21 -07002428 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002429 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002430 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002432EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434/**
2435 * skb_insert - insert a buffer
2436 * @old: buffer to insert before
2437 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002438 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 *
David S. Miller8728b832005-08-09 19:25:21 -07002440 * Place a packet before a given packet in a list. The list locks are
2441 * taken and this function is atomic with respect to other list locked
2442 * calls.
2443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 * A buffer cannot be placed on two lists at the same time.
2445 */
David S. Miller8728b832005-08-09 19:25:21 -07002446void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 unsigned long flags;
2449
David S. Miller8728b832005-08-09 19:25:21 -07002450 spin_lock_irqsave(&list->lock, flags);
2451 __skb_insert(newsk, old->prev, old, list);
2452 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002454EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456static inline void skb_split_inside_header(struct sk_buff *skb,
2457 struct sk_buff* skb1,
2458 const u32 len, const int pos)
2459{
2460 int i;
2461
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002462 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2463 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* And move data appendix as is. */
2465 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2466 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2467
2468 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2469 skb_shinfo(skb)->nr_frags = 0;
2470 skb1->data_len = skb->data_len;
2471 skb1->len += skb1->data_len;
2472 skb->data_len = 0;
2473 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002474 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static inline void skb_split_no_header(struct sk_buff *skb,
2478 struct sk_buff* skb1,
2479 const u32 len, int pos)
2480{
2481 int i, k = 0;
2482 const int nfrags = skb_shinfo(skb)->nr_frags;
2483
2484 skb_shinfo(skb)->nr_frags = 0;
2485 skb1->len = skb1->data_len = skb->len - len;
2486 skb->len = len;
2487 skb->data_len = len - pos;
2488
2489 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002490 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 if (pos + size > len) {
2493 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2494
2495 if (pos < len) {
2496 /* Split frag.
2497 * We have two variants in this case:
2498 * 1. Move all the frag to the second
2499 * part, if it is possible. F.e.
2500 * this approach is mandatory for TUX,
2501 * where splitting is expensive.
2502 * 2. Split is accurately. We make this.
2503 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002504 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002506 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2507 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 skb_shinfo(skb)->nr_frags++;
2509 }
2510 k++;
2511 } else
2512 skb_shinfo(skb)->nr_frags++;
2513 pos += size;
2514 }
2515 skb_shinfo(skb1)->nr_frags = k;
2516}
2517
2518/**
2519 * skb_split - Split fragmented skb to two parts at length len.
2520 * @skb: the buffer to split
2521 * @skb1: the buffer to receive the second part
2522 * @len: new length for skb
2523 */
2524void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2525{
2526 int pos = skb_headlen(skb);
2527
Amerigo Wang68534c62013-02-19 22:51:30 +00002528 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (len < pos) /* Split line is inside header. */
2530 skb_split_inside_header(skb, skb1, len, pos);
2531 else /* Second chunk has no header, nothing to copy. */
2532 skb_split_no_header(skb, skb1, len, pos);
2533}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002534EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002536/* Shifting from/to a cloned skb is a no-go.
2537 *
2538 * Caller cannot keep skb_shinfo related pointers past calling here!
2539 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002540static int skb_prepare_for_shift(struct sk_buff *skb)
2541{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002542 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002543}
2544
2545/**
2546 * skb_shift - Shifts paged data partially from skb to another
2547 * @tgt: buffer into which tail data gets added
2548 * @skb: buffer from which the paged data comes from
2549 * @shiftlen: shift up to this many bytes
2550 *
2551 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002552 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002553 * It's up to caller to free skb if everything was shifted.
2554 *
2555 * If @tgt runs out of frags, the whole operation is aborted.
2556 *
2557 * Skb cannot include anything else but paged data while tgt is allowed
2558 * to have non-paged data as well.
2559 *
2560 * TODO: full sized shift could be optimized but that would need
2561 * specialized skb free'er to handle frags without up-to-date nr_frags.
2562 */
2563int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2564{
2565 int from, to, merge, todo;
2566 struct skb_frag_struct *fragfrom, *fragto;
2567
2568 BUG_ON(shiftlen > skb->len);
2569 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2570
2571 todo = shiftlen;
2572 from = 0;
2573 to = skb_shinfo(tgt)->nr_frags;
2574 fragfrom = &skb_shinfo(skb)->frags[from];
2575
2576 /* Actual merge is delayed until the point when we know we can
2577 * commit all, so that we don't have to undo partial changes
2578 */
2579 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002580 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2581 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002582 merge = -1;
2583 } else {
2584 merge = to - 1;
2585
Eric Dumazet9e903e02011-10-18 21:00:24 +00002586 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002587 if (todo < 0) {
2588 if (skb_prepare_for_shift(skb) ||
2589 skb_prepare_for_shift(tgt))
2590 return 0;
2591
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002592 /* All previous frag pointers might be stale! */
2593 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002594 fragto = &skb_shinfo(tgt)->frags[merge];
2595
Eric Dumazet9e903e02011-10-18 21:00:24 +00002596 skb_frag_size_add(fragto, shiftlen);
2597 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002598 fragfrom->page_offset += shiftlen;
2599
2600 goto onlymerged;
2601 }
2602
2603 from++;
2604 }
2605
2606 /* Skip full, not-fitting skb to avoid expensive operations */
2607 if ((shiftlen == skb->len) &&
2608 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2609 return 0;
2610
2611 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2612 return 0;
2613
2614 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2615 if (to == MAX_SKB_FRAGS)
2616 return 0;
2617
2618 fragfrom = &skb_shinfo(skb)->frags[from];
2619 fragto = &skb_shinfo(tgt)->frags[to];
2620
Eric Dumazet9e903e02011-10-18 21:00:24 +00002621 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002622 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002623 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002624 from++;
2625 to++;
2626
2627 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002628 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002629 fragto->page = fragfrom->page;
2630 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002631 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002632
2633 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002634 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002635 todo = 0;
2636
2637 to++;
2638 break;
2639 }
2640 }
2641
2642 /* Ready to "commit" this state change to tgt */
2643 skb_shinfo(tgt)->nr_frags = to;
2644
2645 if (merge >= 0) {
2646 fragfrom = &skb_shinfo(skb)->frags[0];
2647 fragto = &skb_shinfo(tgt)->frags[merge];
2648
Eric Dumazet9e903e02011-10-18 21:00:24 +00002649 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002650 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002651 }
2652
2653 /* Reposition in the original skb */
2654 to = 0;
2655 while (from < skb_shinfo(skb)->nr_frags)
2656 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2657 skb_shinfo(skb)->nr_frags = to;
2658
2659 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2660
2661onlymerged:
2662 /* Most likely the tgt won't ever need its checksum anymore, skb on
2663 * the other hand might need it if it needs to be resent
2664 */
2665 tgt->ip_summed = CHECKSUM_PARTIAL;
2666 skb->ip_summed = CHECKSUM_PARTIAL;
2667
2668 /* Yak, is it really working this way? Some helper please? */
2669 skb->len -= shiftlen;
2670 skb->data_len -= shiftlen;
2671 skb->truesize -= shiftlen;
2672 tgt->len += shiftlen;
2673 tgt->data_len += shiftlen;
2674 tgt->truesize += shiftlen;
2675
2676 return shiftlen;
2677}
2678
Thomas Graf677e90e2005-06-23 20:59:51 -07002679/**
2680 * skb_prepare_seq_read - Prepare a sequential read of skb data
2681 * @skb: the buffer to read
2682 * @from: lower offset of data to be read
2683 * @to: upper offset of data to be read
2684 * @st: state variable
2685 *
2686 * Initializes the specified state variable. Must be called before
2687 * invoking skb_seq_read() for the first time.
2688 */
2689void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2690 unsigned int to, struct skb_seq_state *st)
2691{
2692 st->lower_offset = from;
2693 st->upper_offset = to;
2694 st->root_skb = st->cur_skb = skb;
2695 st->frag_idx = st->stepped_offset = 0;
2696 st->frag_data = NULL;
2697}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002698EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002699
2700/**
2701 * skb_seq_read - Sequentially read skb data
2702 * @consumed: number of bytes consumed by the caller so far
2703 * @data: destination pointer for data to be returned
2704 * @st: state variable
2705 *
Mathias Krausebc323832013-11-07 14:18:26 +01002706 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002707 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002708 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002709 * of the block or 0 if the end of the skb data or the upper
2710 * offset has been reached.
2711 *
2712 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002713 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002714 * of bytes already consumed and the next call to
2715 * skb_seq_read() will return the remaining part of the block.
2716 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002717 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002718 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002719 * reads of potentially non linear data.
2720 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002721 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002722 * at the moment, state->root_skb could be replaced with
2723 * a stack for this purpose.
2724 */
2725unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2726 struct skb_seq_state *st)
2727{
2728 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2729 skb_frag_t *frag;
2730
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002731 if (unlikely(abs_offset >= st->upper_offset)) {
2732 if (st->frag_data) {
2733 kunmap_atomic(st->frag_data);
2734 st->frag_data = NULL;
2735 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002736 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002737 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002738
2739next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002740 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002741
Thomas Chenault995b3372009-05-18 21:43:27 -07002742 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002743 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002744 return block_limit - abs_offset;
2745 }
2746
2747 if (st->frag_idx == 0 && !st->frag_data)
2748 st->stepped_offset += skb_headlen(st->cur_skb);
2749
2750 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2751 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002752 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002753
2754 if (abs_offset < block_limit) {
2755 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002756 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002757
2758 *data = (u8 *) st->frag_data + frag->page_offset +
2759 (abs_offset - st->stepped_offset);
2760
2761 return block_limit - abs_offset;
2762 }
2763
2764 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002765 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002766 st->frag_data = NULL;
2767 }
2768
2769 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002770 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002771 }
2772
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002773 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002774 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002775 st->frag_data = NULL;
2776 }
2777
David S. Miller21dc3302010-08-23 00:13:46 -07002778 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002779 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002780 st->frag_idx = 0;
2781 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002782 } else if (st->cur_skb->next) {
2783 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002784 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002785 goto next_skb;
2786 }
2787
2788 return 0;
2789}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002790EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002791
2792/**
2793 * skb_abort_seq_read - Abort a sequential read of skb data
2794 * @st: state variable
2795 *
2796 * Must be called if skb_seq_read() was not called until it
2797 * returned 0.
2798 */
2799void skb_abort_seq_read(struct skb_seq_state *st)
2800{
2801 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002802 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002803}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002804EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002805
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002806#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2807
2808static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2809 struct ts_config *conf,
2810 struct ts_state *state)
2811{
2812 return skb_seq_read(offset, text, TS_SKB_CB(state));
2813}
2814
2815static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2816{
2817 skb_abort_seq_read(TS_SKB_CB(state));
2818}
2819
2820/**
2821 * skb_find_text - Find a text pattern in skb data
2822 * @skb: the buffer to look in
2823 * @from: search offset
2824 * @to: search limit
2825 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002826 *
2827 * Finds a pattern in the skb data according to the specified
2828 * textsearch configuration. Use textsearch_next() to retrieve
2829 * subsequent occurrences of the pattern. Returns the offset
2830 * to the first occurrence or UINT_MAX if no match was found.
2831 */
2832unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002833 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002834{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002835 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002836 unsigned int ret;
2837
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002838 config->get_next_block = skb_ts_get_next_block;
2839 config->finish = skb_ts_finish;
2840
Bojan Prtvar059a2442015-02-22 11:46:35 +01002841 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002842
Bojan Prtvar059a2442015-02-22 11:46:35 +01002843 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002844 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002845}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002846EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002847
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002848/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002849 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002850 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002851 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002852 * @getfrag: call back function to be used for getting the user data
2853 * @from: pointer to user message iov
2854 * @length: length of the iov message
2855 *
2856 * Description: This procedure append the user data in the fragment part
2857 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2858 */
2859int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002860 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002861 int len, int odd, struct sk_buff *skb),
2862 void *from, int length)
2863{
Eric Dumazetb2111722012-12-28 06:06:37 +00002864 int frg_cnt = skb_shinfo(skb)->nr_frags;
2865 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002866 int offset = 0;
2867 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002868 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002869
2870 do {
2871 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002872 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002873 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002874
Eric Dumazetb2111722012-12-28 06:06:37 +00002875 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002876 return -ENOMEM;
2877
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002878 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002879 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002880
Eric Dumazetb2111722012-12-28 06:06:37 +00002881 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2882 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002883 if (ret < 0)
2884 return -EFAULT;
2885
2886 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002887 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2888 copy);
2889 frg_cnt++;
2890 pfrag->offset += copy;
2891 get_page(pfrag->page);
2892
2893 skb->truesize += copy;
2894 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002895 skb->len += copy;
2896 skb->data_len += copy;
2897 offset += copy;
2898 length -= copy;
2899
2900 } while (length > 0);
2901
2902 return 0;
2903}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002904EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002905
Herbert Xucbb042f2006-03-20 22:43:56 -08002906/**
2907 * skb_pull_rcsum - pull skb and update receive checksum
2908 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002909 * @len: length of data pulled
2910 *
2911 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002912 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002913 * receive path processing instead of skb_pull unless you know
2914 * that the checksum difference is zero (e.g., a valid IP header)
2915 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002916 */
2917unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2918{
2919 BUG_ON(len > skb->len);
2920 skb->len -= len;
2921 BUG_ON(skb->len < skb->data_len);
2922 skb_postpull_rcsum(skb, skb->data, len);
2923 return skb->data += len;
2924}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002925EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2926
Herbert Xuf4c50d92006-06-22 03:02:40 -07002927/**
2928 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002929 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002930 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002931 *
2932 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002933 * a pointer to the first in a list of new skbs for the segments.
2934 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002935 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002936struct sk_buff *skb_segment(struct sk_buff *head_skb,
2937 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002938{
2939 struct sk_buff *segs = NULL;
2940 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002941 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002942 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2943 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2944 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002945 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002946 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002947 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002948 unsigned int headroom;
2949 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002950 __be16 proto;
2951 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002952 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002953 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002954 int err = -ENOMEM;
2955 int i = 0;
2956 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002957 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002958
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002959 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002960 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002961 if (unlikely(!proto))
2962 return ERR_PTR(-EINVAL);
2963
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002964 csum = !head_skb->encap_hdr_csum &&
2965 !!can_checksum_protocol(features, proto);
2966
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002967 headroom = skb_headroom(head_skb);
2968 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002969
2970 do {
2971 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002972 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002973 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002974 int size;
2975
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002976 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002977 if (len > mss)
2978 len = mss;
2979
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002980 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002981 if (hsize < 0)
2982 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002983 if (hsize > len || !sg)
2984 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002985
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002986 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2987 (skb_headlen(list_skb) == len || sg)) {
2988 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002989
Herbert Xu9d8506c2013-11-21 11:10:04 -08002990 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002991 nfrags = skb_shinfo(list_skb)->nr_frags;
2992 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002993 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002994 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002995
2996 while (pos < offset + len) {
2997 BUG_ON(i >= nfrags);
2998
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002999 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003000 if (pos + size > offset + len)
3001 break;
3002
3003 i++;
3004 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003005 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003006 }
3007
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003008 nskb = skb_clone(list_skb, GFP_ATOMIC);
3009 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003010
3011 if (unlikely(!nskb))
3012 goto err;
3013
Herbert Xu9d8506c2013-11-21 11:10:04 -08003014 if (unlikely(pskb_trim(nskb, len))) {
3015 kfree_skb(nskb);
3016 goto err;
3017 }
3018
Alexander Duyckec47ea82012-05-04 14:26:56 +00003019 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003020 if (skb_cow_head(nskb, doffset + headroom)) {
3021 kfree_skb(nskb);
3022 goto err;
3023 }
3024
Alexander Duyckec47ea82012-05-04 14:26:56 +00003025 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003026 skb_release_head_state(nskb);
3027 __skb_push(nskb, doffset);
3028 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003029 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003030 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003031 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003032
3033 if (unlikely(!nskb))
3034 goto err;
3035
3036 skb_reserve(nskb, headroom);
3037 __skb_put(nskb, doffset);
3038 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003039
3040 if (segs)
3041 tail->next = nskb;
3042 else
3043 segs = nskb;
3044 tail = nskb;
3045
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003046 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003047
Eric Dumazet030737b2013-10-19 11:42:54 -07003048 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003049 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003050
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003051 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003052 nskb->data - tnl_hlen,
3053 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003054
Herbert Xu9d8506c2013-11-21 11:10:04 -08003055 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003056 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003057
Tom Herberte585f232014-11-04 09:06:54 -08003058 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003059 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003060 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003061 skb_put(nskb, len),
3062 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003063 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003064 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003065 continue;
3066 }
3067
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003068 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003069
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003070 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003071 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003072
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003073 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3074 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003075
Herbert Xu9d8506c2013-11-21 11:10:04 -08003076 while (pos < offset + len) {
3077 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003078 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003079
3080 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003081 nfrags = skb_shinfo(list_skb)->nr_frags;
3082 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003083 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003084
3085 BUG_ON(!nfrags);
3086
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003087 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003088 }
3089
3090 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3091 MAX_SKB_FRAGS)) {
3092 net_warn_ratelimited(
3093 "skb_segment: too many frags: %u %u\n",
3094 pos, mss);
3095 goto err;
3096 }
3097
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003098 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3099 goto err;
3100
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003101 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003102 __skb_frag_ref(nskb_frag);
3103 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003104
3105 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003106 nskb_frag->page_offset += offset - pos;
3107 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003108 }
3109
Herbert Xu89319d382008-12-15 23:26:06 -08003110 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003111
3112 if (pos + size <= offset + len) {
3113 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003114 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003115 pos += size;
3116 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003117 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003118 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003119 }
3120
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003121 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003122 }
3123
Herbert Xu89319d382008-12-15 23:26:06 -08003124skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003125 nskb->data_len = len - hsize;
3126 nskb->len += nskb->data_len;
3127 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003128
Simon Horman1cdbcb72013-05-19 15:46:49 +00003129perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003130 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003131 nskb->csum = skb_checksum(nskb, doffset,
3132 nskb->len - doffset, 0);
3133 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003134 SKB_GSO_CB(nskb)->csum_start =
3135 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003136 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003137 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003138
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003139 /* Some callers want to get the end of the list.
3140 * Put it in segs->prev to avoid walking the list.
3141 * (see validate_xmit_skb_list() for example)
3142 */
3143 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003144
3145 /* Following permits correct backpressure, for protocols
3146 * using skb_set_owner_w().
3147 * Idea is to tranfert ownership from head_skb to last segment.
3148 */
3149 if (head_skb->destructor == sock_wfree) {
3150 swap(tail->truesize, head_skb->truesize);
3151 swap(tail->destructor, head_skb->destructor);
3152 swap(tail->sk, head_skb->sk);
3153 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003154 return segs;
3155
3156err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003157 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003158 return ERR_PTR(err);
3159}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003160EXPORT_SYMBOL_GPL(skb_segment);
3161
Herbert Xu71d93b32008-12-15 23:42:33 -08003162int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3163{
Eric Dumazet8a291112013-10-08 09:02:23 -07003164 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003165 unsigned int offset = skb_gro_offset(skb);
3166 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003167 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003168 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003169 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003170
Eric Dumazet8a291112013-10-08 09:02:23 -07003171 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003172 return -E2BIG;
3173
Eric Dumazet29e98242014-05-16 11:34:37 -07003174 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003175 pinfo = skb_shinfo(lp);
3176
3177 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003178 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003179 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003180 int i = skbinfo->nr_frags;
3181 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003182
Herbert Xu66e92fc2009-05-26 18:50:32 +00003183 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003184 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003185
Eric Dumazet8a291112013-10-08 09:02:23 -07003186 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003187 pinfo->nr_frags = nr_frags;
3188 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003189
Herbert Xu9aaa1562009-05-26 18:50:33 +00003190 frag = pinfo->frags + nr_frags;
3191 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003192 do {
3193 *--frag = *--frag2;
3194 } while (--i);
3195
3196 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003197 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003198
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003199 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003200 delta_truesize = skb->truesize -
3201 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003202
Herbert Xuf5572062009-01-14 20:40:03 -08003203 skb->truesize -= skb->data_len;
3204 skb->len -= skb->data_len;
3205 skb->data_len = 0;
3206
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003207 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003208 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003209 } else if (skb->head_frag) {
3210 int nr_frags = pinfo->nr_frags;
3211 skb_frag_t *frag = pinfo->frags + nr_frags;
3212 struct page *page = virt_to_head_page(skb->head);
3213 unsigned int first_size = headlen - offset;
3214 unsigned int first_offset;
3215
3216 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003217 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003218
3219 first_offset = skb->data -
3220 (unsigned char *)page_address(page) +
3221 offset;
3222
3223 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3224
3225 frag->page.p = page;
3226 frag->page_offset = first_offset;
3227 skb_frag_size_set(frag, first_size);
3228
3229 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3230 /* We dont need to clear skbinfo->nr_frags here */
3231
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003232 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003233 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3234 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003235 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003236
3237merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003238 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003239 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003240 unsigned int eat = offset - headlen;
3241
3242 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003243 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003244 skb->data_len -= eat;
3245 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003246 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003247 }
3248
Herbert Xu67147ba2009-05-26 18:50:22 +00003249 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003250
Eric Dumazet29e98242014-05-16 11:34:37 -07003251 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003252 skb_shinfo(p)->frag_list = skb;
3253 else
3254 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003255 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003256 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003257 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003258
Herbert Xu5d38a072009-01-04 16:13:40 -08003259done:
3260 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003261 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003262 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003263 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003264 if (lp != p) {
3265 lp->data_len += len;
3266 lp->truesize += delta_truesize;
3267 lp->len += len;
3268 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003269 NAPI_GRO_CB(skb)->same_flow = 1;
3270 return 0;
3271}
Herbert Xu71d93b32008-12-15 23:42:33 -08003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273void __init skb_init(void)
3274{
3275 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3276 sizeof(struct sk_buff),
3277 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003278 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003279 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003280 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003281 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003282 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003283 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003284 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
David Howells716ea3a2007-04-02 20:19:53 -07003287/**
3288 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3289 * @skb: Socket buffer containing the buffers to be mapped
3290 * @sg: The scatter-gather list to map into
3291 * @offset: The offset into the buffer's contents to start mapping
3292 * @len: Length of buffer space to be mapped
3293 *
3294 * Fill the specified scatter-gather list with mappings/pointers into a
3295 * region of the buffer space attached to a socket buffer.
3296 */
David S. Miller51c739d2007-10-30 21:29:29 -07003297static int
3298__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003299{
David S. Miller1a028e52007-04-27 15:21:23 -07003300 int start = skb_headlen(skb);
3301 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003302 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003303 int elt = 0;
3304
3305 if (copy > 0) {
3306 if (copy > len)
3307 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003308 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003309 elt++;
3310 if ((len -= copy) == 0)
3311 return elt;
3312 offset += copy;
3313 }
3314
3315 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003316 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003317
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003318 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003319
Eric Dumazet9e903e02011-10-18 21:00:24 +00003320 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003321 if ((copy = end - offset) > 0) {
3322 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3323
3324 if (copy > len)
3325 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003326 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003327 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003328 elt++;
3329 if (!(len -= copy))
3330 return elt;
3331 offset += copy;
3332 }
David S. Miller1a028e52007-04-27 15:21:23 -07003333 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003334 }
3335
David S. Millerfbb398a2009-06-09 00:18:59 -07003336 skb_walk_frags(skb, frag_iter) {
3337 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003338
David S. Millerfbb398a2009-06-09 00:18:59 -07003339 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003340
David S. Millerfbb398a2009-06-09 00:18:59 -07003341 end = start + frag_iter->len;
3342 if ((copy = end - offset) > 0) {
3343 if (copy > len)
3344 copy = len;
3345 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3346 copy);
3347 if ((len -= copy) == 0)
3348 return elt;
3349 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003350 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003351 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003352 }
3353 BUG_ON(len);
3354 return elt;
3355}
3356
Fan Du25a91d82014-01-18 09:54:23 +08003357/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3358 * sglist without mark the sg which contain last skb data as the end.
3359 * So the caller can mannipulate sg list as will when padding new data after
3360 * the first call without calling sg_unmark_end to expend sg list.
3361 *
3362 * Scenario to use skb_to_sgvec_nomark:
3363 * 1. sg_init_table
3364 * 2. skb_to_sgvec_nomark(payload1)
3365 * 3. skb_to_sgvec_nomark(payload2)
3366 *
3367 * This is equivalent to:
3368 * 1. sg_init_table
3369 * 2. skb_to_sgvec(payload1)
3370 * 3. sg_unmark_end
3371 * 4. skb_to_sgvec(payload2)
3372 *
3373 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3374 * is more preferable.
3375 */
3376int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3377 int offset, int len)
3378{
3379 return __skb_to_sgvec(skb, sg, offset, len);
3380}
3381EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3382
David S. Miller51c739d2007-10-30 21:29:29 -07003383int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3384{
3385 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3386
Jens Axboec46f2332007-10-31 12:06:37 +01003387 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003388
3389 return nsg;
3390}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003391EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003392
David Howells716ea3a2007-04-02 20:19:53 -07003393/**
3394 * skb_cow_data - Check that a socket buffer's data buffers are writable
3395 * @skb: The socket buffer to check.
3396 * @tailbits: Amount of trailing space to be added
3397 * @trailer: Returned pointer to the skb where the @tailbits space begins
3398 *
3399 * Make sure that the data buffers attached to a socket buffer are
3400 * writable. If they are not, private copies are made of the data buffers
3401 * and the socket buffer is set to use these instead.
3402 *
3403 * If @tailbits is given, make sure that there is space to write @tailbits
3404 * bytes of data beyond current end of socket buffer. @trailer will be
3405 * set to point to the skb in which this space begins.
3406 *
3407 * The number of scatterlist elements required to completely map the
3408 * COW'd and extended socket buffer will be returned.
3409 */
3410int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3411{
3412 int copyflag;
3413 int elt;
3414 struct sk_buff *skb1, **skb_p;
3415
3416 /* If skb is cloned or its head is paged, reallocate
3417 * head pulling out all the pages (pages are considered not writable
3418 * at the moment even if they are anonymous).
3419 */
3420 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3421 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3422 return -ENOMEM;
3423
3424 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003425 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003426 /* A little of trouble, not enough of space for trailer.
3427 * This should not happen, when stack is tuned to generate
3428 * good frames. OK, on miss we reallocate and reserve even more
3429 * space, 128 bytes is fair. */
3430
3431 if (skb_tailroom(skb) < tailbits &&
3432 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3433 return -ENOMEM;
3434
3435 /* Voila! */
3436 *trailer = skb;
3437 return 1;
3438 }
3439
3440 /* Misery. We are in troubles, going to mincer fragments... */
3441
3442 elt = 1;
3443 skb_p = &skb_shinfo(skb)->frag_list;
3444 copyflag = 0;
3445
3446 while ((skb1 = *skb_p) != NULL) {
3447 int ntail = 0;
3448
3449 /* The fragment is partially pulled by someone,
3450 * this can happen on input. Copy it and everything
3451 * after it. */
3452
3453 if (skb_shared(skb1))
3454 copyflag = 1;
3455
3456 /* If the skb is the last, worry about trailer. */
3457
3458 if (skb1->next == NULL && tailbits) {
3459 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003460 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003461 skb_tailroom(skb1) < tailbits)
3462 ntail = tailbits + 128;
3463 }
3464
3465 if (copyflag ||
3466 skb_cloned(skb1) ||
3467 ntail ||
3468 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003469 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003470 struct sk_buff *skb2;
3471
3472 /* Fuck, we are miserable poor guys... */
3473 if (ntail == 0)
3474 skb2 = skb_copy(skb1, GFP_ATOMIC);
3475 else
3476 skb2 = skb_copy_expand(skb1,
3477 skb_headroom(skb1),
3478 ntail,
3479 GFP_ATOMIC);
3480 if (unlikely(skb2 == NULL))
3481 return -ENOMEM;
3482
3483 if (skb1->sk)
3484 skb_set_owner_w(skb2, skb1->sk);
3485
3486 /* Looking around. Are we still alive?
3487 * OK, link new skb, drop old one */
3488
3489 skb2->next = skb1->next;
3490 *skb_p = skb2;
3491 kfree_skb(skb1);
3492 skb1 = skb2;
3493 }
3494 elt++;
3495 *trailer = skb1;
3496 skb_p = &skb1->next;
3497 }
3498
3499 return elt;
3500}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003501EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003502
Eric Dumazetb1faf562010-05-31 23:44:05 -07003503static void sock_rmem_free(struct sk_buff *skb)
3504{
3505 struct sock *sk = skb->sk;
3506
3507 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3508}
3509
3510/*
3511 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3512 */
3513int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3514{
3515 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003516 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003517 return -ENOMEM;
3518
3519 skb_orphan(skb);
3520 skb->sk = sk;
3521 skb->destructor = sock_rmem_free;
3522 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3523
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003524 /* before exiting rcu section, make sure dst is refcounted */
3525 skb_dst_force(skb);
3526
Eric Dumazetb1faf562010-05-31 23:44:05 -07003527 skb_queue_tail(&sk->sk_error_queue, skb);
3528 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003529 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003530 return 0;
3531}
3532EXPORT_SYMBOL(sock_queue_err_skb);
3533
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003534struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3535{
3536 struct sk_buff_head *q = &sk->sk_error_queue;
3537 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003538 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003539 int err = 0;
3540
Eric Dumazet997d5c32015-02-18 05:47:55 -08003541 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003542 skb = __skb_dequeue(q);
3543 if (skb && (skb_next = skb_peek(q)))
3544 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003545 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003546
3547 sk->sk_err = err;
3548 if (err)
3549 sk->sk_error_report(sk);
3550
3551 return skb;
3552}
3553EXPORT_SYMBOL(sock_dequeue_err_skb);
3554
Alexander Duyckcab41c42014-09-10 18:05:26 -04003555/**
3556 * skb_clone_sk - create clone of skb, and take reference to socket
3557 * @skb: the skb to clone
3558 *
3559 * This function creates a clone of a buffer that holds a reference on
3560 * sk_refcnt. Buffers created via this function are meant to be
3561 * returned using sock_queue_err_skb, or free via kfree_skb.
3562 *
3563 * When passing buffers allocated with this function to sock_queue_err_skb
3564 * it is necessary to wrap the call with sock_hold/sock_put in order to
3565 * prevent the socket from being released prior to being enqueued on
3566 * the sk_error_queue.
3567 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003568struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3569{
3570 struct sock *sk = skb->sk;
3571 struct sk_buff *clone;
3572
3573 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3574 return NULL;
3575
3576 clone = skb_clone(skb, GFP_ATOMIC);
3577 if (!clone) {
3578 sock_put(sk);
3579 return NULL;
3580 }
3581
3582 clone->sk = sk;
3583 clone->destructor = sock_efree;
3584
3585 return clone;
3586}
3587EXPORT_SYMBOL(skb_clone_sk);
3588
Alexander Duyck37846ef2014-09-04 13:31:10 -04003589static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3590 struct sock *sk,
3591 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003592{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003593 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003594 int err;
3595
Patrick Ohlyac45f602009-02-12 05:03:37 +00003596 serr = SKB_EXT_ERR(skb);
3597 memset(serr, 0, sizeof(*serr));
3598 serr->ee.ee_errno = ENOMSG;
3599 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003600 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003601 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003602 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003603 if (sk->sk_protocol == IPPROTO_TCP)
3604 serr->ee.ee_data -= sk->sk_tskey;
3605 }
Eric Dumazet29030372010-05-29 00:20:48 -07003606
Patrick Ohlyac45f602009-02-12 05:03:37 +00003607 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003608
Patrick Ohlyac45f602009-02-12 05:03:37 +00003609 if (err)
3610 kfree_skb(skb);
3611}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003612
Willem de Bruijnb245be12015-01-30 13:29:32 -05003613static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3614{
3615 bool ret;
3616
3617 if (likely(sysctl_tstamp_allow_data || tsonly))
3618 return true;
3619
3620 read_lock_bh(&sk->sk_callback_lock);
3621 ret = sk->sk_socket && sk->sk_socket->file &&
3622 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3623 read_unlock_bh(&sk->sk_callback_lock);
3624 return ret;
3625}
3626
Alexander Duyck37846ef2014-09-04 13:31:10 -04003627void skb_complete_tx_timestamp(struct sk_buff *skb,
3628 struct skb_shared_hwtstamps *hwtstamps)
3629{
3630 struct sock *sk = skb->sk;
3631
Willem de Bruijnb245be12015-01-30 13:29:32 -05003632 if (!skb_may_tx_timestamp(sk, false))
3633 return;
3634
Alexander Duyck62bccb82014-09-04 13:31:35 -04003635 /* take a reference to prevent skb_orphan() from freeing the socket */
3636 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003637
Alexander Duyck62bccb82014-09-04 13:31:35 -04003638 *skb_hwtstamps(skb) = *hwtstamps;
3639 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003640
3641 sock_put(sk);
3642}
3643EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3644
3645void __skb_tstamp_tx(struct sk_buff *orig_skb,
3646 struct skb_shared_hwtstamps *hwtstamps,
3647 struct sock *sk, int tstype)
3648{
3649 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003650 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003651
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003652 if (!sk)
3653 return;
3654
3655 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3656 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003657 return;
3658
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003659 if (tsonly)
3660 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003661 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003662 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003663 if (!skb)
3664 return;
3665
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003666 if (tsonly) {
3667 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3668 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3669 }
3670
3671 if (hwtstamps)
3672 *skb_hwtstamps(skb) = *hwtstamps;
3673 else
3674 skb->tstamp = ktime_get_real();
3675
Alexander Duyck37846ef2014-09-04 13:31:10 -04003676 __skb_complete_tx_timestamp(skb, sk, tstype);
3677}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003678EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3679
3680void skb_tstamp_tx(struct sk_buff *orig_skb,
3681 struct skb_shared_hwtstamps *hwtstamps)
3682{
3683 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3684 SCM_TSTAMP_SND);
3685}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003686EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3687
Johannes Berg6e3e9392011-11-09 10:15:42 +01003688void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3689{
3690 struct sock *sk = skb->sk;
3691 struct sock_exterr_skb *serr;
3692 int err;
3693
3694 skb->wifi_acked_valid = 1;
3695 skb->wifi_acked = acked;
3696
3697 serr = SKB_EXT_ERR(skb);
3698 memset(serr, 0, sizeof(*serr));
3699 serr->ee.ee_errno = ENOMSG;
3700 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3701
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003702 /* take a reference to prevent skb_orphan() from freeing the socket */
3703 sock_hold(sk);
3704
Johannes Berg6e3e9392011-11-09 10:15:42 +01003705 err = sock_queue_err_skb(sk, skb);
3706 if (err)
3707 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003708
3709 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003710}
3711EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3712
Rusty Russellf35d9d82008-02-04 23:49:54 -05003713/**
3714 * skb_partial_csum_set - set up and verify partial csum values for packet
3715 * @skb: the skb to set
3716 * @start: the number of bytes after skb->data to start checksumming.
3717 * @off: the offset from start to place the checksum.
3718 *
3719 * For untrusted partially-checksummed packets, we need to make sure the values
3720 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3721 *
3722 * This function checks and sets those values and skb->ip_summed: if this
3723 * returns false you should drop the packet.
3724 */
3725bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3726{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003727 if (unlikely(start > skb_headlen(skb)) ||
3728 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003729 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3730 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003731 return false;
3732 }
3733 skb->ip_summed = CHECKSUM_PARTIAL;
3734 skb->csum_start = skb_headroom(skb) + start;
3735 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003736 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003737 return true;
3738}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003739EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003740
Paul Durranted1f50c2014-01-09 10:02:46 +00003741static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3742 unsigned int max)
3743{
3744 if (skb_headlen(skb) >= len)
3745 return 0;
3746
3747 /* If we need to pullup then pullup to the max, so we
3748 * won't need to do it again.
3749 */
3750 if (max > skb->len)
3751 max = skb->len;
3752
3753 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3754 return -ENOMEM;
3755
3756 if (skb_headlen(skb) < len)
3757 return -EPROTO;
3758
3759 return 0;
3760}
3761
Jan Beulichf9708b42014-03-11 13:56:05 +00003762#define MAX_TCP_HDR_LEN (15 * 4)
3763
3764static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3765 typeof(IPPROTO_IP) proto,
3766 unsigned int off)
3767{
3768 switch (proto) {
3769 int err;
3770
3771 case IPPROTO_TCP:
3772 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3773 off + MAX_TCP_HDR_LEN);
3774 if (!err && !skb_partial_csum_set(skb, off,
3775 offsetof(struct tcphdr,
3776 check)))
3777 err = -EPROTO;
3778 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3779
3780 case IPPROTO_UDP:
3781 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3782 off + sizeof(struct udphdr));
3783 if (!err && !skb_partial_csum_set(skb, off,
3784 offsetof(struct udphdr,
3785 check)))
3786 err = -EPROTO;
3787 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3788 }
3789
3790 return ERR_PTR(-EPROTO);
3791}
3792
Paul Durranted1f50c2014-01-09 10:02:46 +00003793/* This value should be large enough to cover a tagged ethernet header plus
3794 * maximally sized IP and TCP or UDP headers.
3795 */
3796#define MAX_IP_HDR_LEN 128
3797
Jan Beulichf9708b42014-03-11 13:56:05 +00003798static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003799{
3800 unsigned int off;
3801 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003802 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003803 int err;
3804
3805 fragment = false;
3806
3807 err = skb_maybe_pull_tail(skb,
3808 sizeof(struct iphdr),
3809 MAX_IP_HDR_LEN);
3810 if (err < 0)
3811 goto out;
3812
3813 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3814 fragment = true;
3815
3816 off = ip_hdrlen(skb);
3817
3818 err = -EPROTO;
3819
3820 if (fragment)
3821 goto out;
3822
Jan Beulichf9708b42014-03-11 13:56:05 +00003823 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3824 if (IS_ERR(csum))
3825 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003826
Jan Beulichf9708b42014-03-11 13:56:05 +00003827 if (recalculate)
3828 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3829 ip_hdr(skb)->daddr,
3830 skb->len - off,
3831 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003832 err = 0;
3833
3834out:
3835 return err;
3836}
3837
3838/* This value should be large enough to cover a tagged ethernet header plus
3839 * an IPv6 header, all options, and a maximal TCP or UDP header.
3840 */
3841#define MAX_IPV6_HDR_LEN 256
3842
3843#define OPT_HDR(type, skb, off) \
3844 (type *)(skb_network_header(skb) + (off))
3845
3846static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3847{
3848 int err;
3849 u8 nexthdr;
3850 unsigned int off;
3851 unsigned int len;
3852 bool fragment;
3853 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003854 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003855
3856 fragment = false;
3857 done = false;
3858
3859 off = sizeof(struct ipv6hdr);
3860
3861 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3862 if (err < 0)
3863 goto out;
3864
3865 nexthdr = ipv6_hdr(skb)->nexthdr;
3866
3867 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3868 while (off <= len && !done) {
3869 switch (nexthdr) {
3870 case IPPROTO_DSTOPTS:
3871 case IPPROTO_HOPOPTS:
3872 case IPPROTO_ROUTING: {
3873 struct ipv6_opt_hdr *hp;
3874
3875 err = skb_maybe_pull_tail(skb,
3876 off +
3877 sizeof(struct ipv6_opt_hdr),
3878 MAX_IPV6_HDR_LEN);
3879 if (err < 0)
3880 goto out;
3881
3882 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3883 nexthdr = hp->nexthdr;
3884 off += ipv6_optlen(hp);
3885 break;
3886 }
3887 case IPPROTO_AH: {
3888 struct ip_auth_hdr *hp;
3889
3890 err = skb_maybe_pull_tail(skb,
3891 off +
3892 sizeof(struct ip_auth_hdr),
3893 MAX_IPV6_HDR_LEN);
3894 if (err < 0)
3895 goto out;
3896
3897 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3898 nexthdr = hp->nexthdr;
3899 off += ipv6_authlen(hp);
3900 break;
3901 }
3902 case IPPROTO_FRAGMENT: {
3903 struct frag_hdr *hp;
3904
3905 err = skb_maybe_pull_tail(skb,
3906 off +
3907 sizeof(struct frag_hdr),
3908 MAX_IPV6_HDR_LEN);
3909 if (err < 0)
3910 goto out;
3911
3912 hp = OPT_HDR(struct frag_hdr, skb, off);
3913
3914 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3915 fragment = true;
3916
3917 nexthdr = hp->nexthdr;
3918 off += sizeof(struct frag_hdr);
3919 break;
3920 }
3921 default:
3922 done = true;
3923 break;
3924 }
3925 }
3926
3927 err = -EPROTO;
3928
3929 if (!done || fragment)
3930 goto out;
3931
Jan Beulichf9708b42014-03-11 13:56:05 +00003932 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3933 if (IS_ERR(csum))
3934 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003935
Jan Beulichf9708b42014-03-11 13:56:05 +00003936 if (recalculate)
3937 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3938 &ipv6_hdr(skb)->daddr,
3939 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003940 err = 0;
3941
3942out:
3943 return err;
3944}
3945
3946/**
3947 * skb_checksum_setup - set up partial checksum offset
3948 * @skb: the skb to set up
3949 * @recalculate: if true the pseudo-header checksum will be recalculated
3950 */
3951int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3952{
3953 int err;
3954
3955 switch (skb->protocol) {
3956 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003957 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003958 break;
3959
3960 case htons(ETH_P_IPV6):
3961 err = skb_checksum_setup_ipv6(skb, recalculate);
3962 break;
3963
3964 default:
3965 err = -EPROTO;
3966 break;
3967 }
3968
3969 return err;
3970}
3971EXPORT_SYMBOL(skb_checksum_setup);
3972
Linus Lüssing9afd85c2015-05-02 14:01:07 +02003973/**
3974 * skb_checksum_maybe_trim - maybe trims the given skb
3975 * @skb: the skb to check
3976 * @transport_len: the data length beyond the network header
3977 *
3978 * Checks whether the given skb has data beyond the given transport length.
3979 * If so, returns a cloned skb trimmed to this transport length.
3980 * Otherwise returns the provided skb. Returns NULL in error cases
3981 * (e.g. transport_len exceeds skb length or out-of-memory).
3982 *
3983 * Caller needs to set the skb transport header and release the returned skb.
3984 * Provided skb is consumed.
3985 */
3986static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
3987 unsigned int transport_len)
3988{
3989 struct sk_buff *skb_chk;
3990 unsigned int len = skb_transport_offset(skb) + transport_len;
3991 int ret;
3992
3993 if (skb->len < len) {
3994 kfree_skb(skb);
3995 return NULL;
3996 } else if (skb->len == len) {
3997 return skb;
3998 }
3999
4000 skb_chk = skb_clone(skb, GFP_ATOMIC);
4001 kfree_skb(skb);
4002
4003 if (!skb_chk)
4004 return NULL;
4005
4006 ret = pskb_trim_rcsum(skb_chk, len);
4007 if (ret) {
4008 kfree_skb(skb_chk);
4009 return NULL;
4010 }
4011
4012 return skb_chk;
4013}
4014
4015/**
4016 * skb_checksum_trimmed - validate checksum of an skb
4017 * @skb: the skb to check
4018 * @transport_len: the data length beyond the network header
4019 * @skb_chkf: checksum function to use
4020 *
4021 * Applies the given checksum function skb_chkf to the provided skb.
4022 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4023 *
4024 * If the skb has data beyond the given transport length, then a
4025 * trimmed & cloned skb is checked and returned.
4026 *
4027 * Caller needs to set the skb transport header and release the returned skb.
4028 * Provided skb is consumed.
4029 */
4030struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4031 unsigned int transport_len,
4032 __sum16(*skb_chkf)(struct sk_buff *skb))
4033{
4034 struct sk_buff *skb_chk;
4035 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004036 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004037
4038 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4039 if (!skb_chk)
4040 return NULL;
4041
4042 if (!pskb_may_pull(skb_chk, offset)) {
4043 kfree_skb(skb_chk);
4044 return NULL;
4045 }
4046
4047 __skb_pull(skb_chk, offset);
4048 ret = skb_chkf(skb_chk);
4049 __skb_push(skb_chk, offset);
4050
4051 if (ret) {
4052 kfree_skb(skb_chk);
4053 return NULL;
4054 }
4055
4056 return skb_chk;
4057}
4058EXPORT_SYMBOL(skb_checksum_trimmed);
4059
Ben Hutchings4497b072008-06-19 16:22:28 -07004060void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4061{
Joe Perchese87cc472012-05-13 21:56:26 +00004062 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4063 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004064}
Ben Hutchings4497b072008-06-19 16:22:28 -07004065EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004066
4067void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4068{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004069 if (head_stolen) {
4070 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004071 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004072 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004073 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004074 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004075}
4076EXPORT_SYMBOL(kfree_skb_partial);
4077
4078/**
4079 * skb_try_coalesce - try to merge skb to prior one
4080 * @to: prior buffer
4081 * @from: buffer to add
4082 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004083 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004084 */
4085bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4086 bool *fragstolen, int *delta_truesize)
4087{
4088 int i, delta, len = from->len;
4089
4090 *fragstolen = false;
4091
4092 if (skb_cloned(to))
4093 return false;
4094
4095 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004096 if (len)
4097 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004098 *delta_truesize = 0;
4099 return true;
4100 }
4101
4102 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4103 return false;
4104
4105 if (skb_headlen(from) != 0) {
4106 struct page *page;
4107 unsigned int offset;
4108
4109 if (skb_shinfo(to)->nr_frags +
4110 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4111 return false;
4112
4113 if (skb_head_is_locked(from))
4114 return false;
4115
4116 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4117
4118 page = virt_to_head_page(from->head);
4119 offset = from->data - (unsigned char *)page_address(page);
4120
4121 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4122 page, offset, skb_headlen(from));
4123 *fragstolen = true;
4124 } else {
4125 if (skb_shinfo(to)->nr_frags +
4126 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4127 return false;
4128
Weiping Panf4b549a2012-09-28 20:15:30 +00004129 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004130 }
4131
4132 WARN_ON_ONCE(delta < len);
4133
4134 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4135 skb_shinfo(from)->frags,
4136 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4137 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4138
4139 if (!skb_cloned(from))
4140 skb_shinfo(from)->nr_frags = 0;
4141
Li RongQing8ea853f2012-09-18 16:53:21 +00004142 /* if the skb is not cloned this does nothing
4143 * since we set nr_frags to 0.
4144 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004145 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4146 skb_frag_ref(from, i);
4147
4148 to->truesize += delta;
4149 to->len += len;
4150 to->data_len += len;
4151
4152 *delta_truesize = delta;
4153 return true;
4154}
4155EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004156
4157/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004158 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004159 *
4160 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004161 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004162 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004163 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4164 * into/from a tunnel. Some information have to be cleared during these
4165 * operations.
4166 * skb_scrub_packet can also be used to clean a skb before injecting it in
4167 * another namespace (@xnet == true). We have to clear all information in the
4168 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004169 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004170void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004171{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004172 skb->tstamp.tv64 = 0;
4173 skb->pkt_type = PACKET_HOST;
4174 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004175 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004176 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004177 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004178 secpath_reset(skb);
4179 nf_reset(skb);
4180 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004181
4182 if (!xnet)
4183 return;
4184
4185 skb_orphan(skb);
4186 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004187}
4188EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004189
4190/**
4191 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4192 *
4193 * @skb: GSO skb
4194 *
4195 * skb_gso_transport_seglen is used to determine the real size of the
4196 * individual segments, including Layer4 headers (TCP/UDP).
4197 *
4198 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4199 */
4200unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4201{
4202 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004203 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004204
Florian Westphalf993bc22014-10-20 13:49:18 +02004205 if (skb->encapsulation) {
4206 thlen = skb_inner_transport_header(skb) -
4207 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004208
Florian Westphalf993bc22014-10-20 13:49:18 +02004209 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4210 thlen += inner_tcp_hdrlen(skb);
4211 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4212 thlen = tcp_hdrlen(skb);
4213 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004214 /* UFO sets gso_size to the size of the fragmentation
4215 * payload, i.e. the size of the L4 (UDP) header is already
4216 * accounted for.
4217 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004218 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004219}
4220EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004221
4222static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4223{
4224 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4225 kfree_skb(skb);
4226 return NULL;
4227 }
4228
4229 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4230 skb->mac_header += VLAN_HLEN;
4231 return skb;
4232}
4233
4234struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4235{
4236 struct vlan_hdr *vhdr;
4237 u16 vlan_tci;
4238
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004239 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004240 /* vlan_tci is already set-up so leave this for another time */
4241 return skb;
4242 }
4243
4244 skb = skb_share_check(skb, GFP_ATOMIC);
4245 if (unlikely(!skb))
4246 goto err_free;
4247
4248 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4249 goto err_free;
4250
4251 vhdr = (struct vlan_hdr *)skb->data;
4252 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4253 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4254
4255 skb_pull_rcsum(skb, VLAN_HLEN);
4256 vlan_set_encap_proto(skb, vhdr);
4257
4258 skb = skb_reorder_vlan_header(skb);
4259 if (unlikely(!skb))
4260 goto err_free;
4261
4262 skb_reset_network_header(skb);
4263 skb_reset_transport_header(skb);
4264 skb_reset_mac_len(skb);
4265
4266 return skb;
4267
4268err_free:
4269 kfree_skb(skb);
4270 return NULL;
4271}
4272EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004273
Jiri Pirkoe2195122014-11-19 14:05:01 +01004274int skb_ensure_writable(struct sk_buff *skb, int write_len)
4275{
4276 if (!pskb_may_pull(skb, write_len))
4277 return -ENOMEM;
4278
4279 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4280 return 0;
4281
4282 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4283}
4284EXPORT_SYMBOL(skb_ensure_writable);
4285
Jiri Pirko93515d52014-11-19 14:05:02 +01004286/* remove VLAN header from packet and update csum accordingly. */
4287static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4288{
4289 struct vlan_hdr *vhdr;
4290 unsigned int offset = skb->data - skb_mac_header(skb);
4291 int err;
4292
4293 __skb_push(skb, offset);
4294 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4295 if (unlikely(err))
4296 goto pull;
4297
4298 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4299
4300 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4301 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4302
4303 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4304 __skb_pull(skb, VLAN_HLEN);
4305
4306 vlan_set_encap_proto(skb, vhdr);
4307 skb->mac_header += VLAN_HLEN;
4308
4309 if (skb_network_offset(skb) < ETH_HLEN)
4310 skb_set_network_header(skb, ETH_HLEN);
4311
4312 skb_reset_mac_len(skb);
4313pull:
4314 __skb_pull(skb, offset);
4315
4316 return err;
4317}
4318
4319int skb_vlan_pop(struct sk_buff *skb)
4320{
4321 u16 vlan_tci;
4322 __be16 vlan_proto;
4323 int err;
4324
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004325 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004326 skb->vlan_tci = 0;
4327 } else {
4328 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4329 skb->protocol != htons(ETH_P_8021AD)) ||
4330 skb->len < VLAN_ETH_HLEN))
4331 return 0;
4332
4333 err = __skb_vlan_pop(skb, &vlan_tci);
4334 if (err)
4335 return err;
4336 }
4337 /* move next vlan tag to hw accel tag */
4338 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4339 skb->protocol != htons(ETH_P_8021AD)) ||
4340 skb->len < VLAN_ETH_HLEN))
4341 return 0;
4342
4343 vlan_proto = skb->protocol;
4344 err = __skb_vlan_pop(skb, &vlan_tci);
4345 if (unlikely(err))
4346 return err;
4347
4348 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4349 return 0;
4350}
4351EXPORT_SYMBOL(skb_vlan_pop);
4352
4353int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4354{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004355 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004356 unsigned int offset = skb->data - skb_mac_header(skb);
4357 int err;
4358
4359 /* __vlan_insert_tag expect skb->data pointing to mac header.
4360 * So change skb->data before calling it and change back to
4361 * original position later
4362 */
4363 __skb_push(skb, offset);
4364 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004365 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004366 if (err)
4367 return err;
4368 skb->protocol = skb->vlan_proto;
4369 skb->mac_len += VLAN_HLEN;
4370 __skb_pull(skb, offset);
4371
4372 if (skb->ip_summed == CHECKSUM_COMPLETE)
4373 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4374 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4375 }
4376 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4377 return 0;
4378}
4379EXPORT_SYMBOL(skb_vlan_push);
4380
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004381/**
4382 * alloc_skb_with_frags - allocate skb with page frags
4383 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004384 * @header_len: size of linear part
4385 * @data_len: needed length in frags
4386 * @max_page_order: max page order desired.
4387 * @errcode: pointer to error code if any
4388 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004389 *
4390 * This can be used to allocate a paged skb, given a maximal order for frags.
4391 */
4392struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4393 unsigned long data_len,
4394 int max_page_order,
4395 int *errcode,
4396 gfp_t gfp_mask)
4397{
4398 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4399 unsigned long chunk;
4400 struct sk_buff *skb;
4401 struct page *page;
4402 gfp_t gfp_head;
4403 int i;
4404
4405 *errcode = -EMSGSIZE;
4406 /* Note this test could be relaxed, if we succeed to allocate
4407 * high order pages...
4408 */
4409 if (npages > MAX_SKB_FRAGS)
4410 return NULL;
4411
4412 gfp_head = gfp_mask;
4413 if (gfp_head & __GFP_WAIT)
4414 gfp_head |= __GFP_REPEAT;
4415
4416 *errcode = -ENOBUFS;
4417 skb = alloc_skb(header_len, gfp_head);
4418 if (!skb)
4419 return NULL;
4420
4421 skb->truesize += npages << PAGE_SHIFT;
4422
4423 for (i = 0; npages > 0; i++) {
4424 int order = max_page_order;
4425
4426 while (order) {
4427 if (npages >= 1 << order) {
4428 page = alloc_pages(gfp_mask |
4429 __GFP_COMP |
4430 __GFP_NOWARN |
4431 __GFP_NORETRY,
4432 order);
4433 if (page)
4434 goto fill_page;
4435 /* Do not retry other high order allocations */
4436 order = 1;
4437 max_page_order = 0;
4438 }
4439 order--;
4440 }
4441 page = alloc_page(gfp_mask);
4442 if (!page)
4443 goto failure;
4444fill_page:
4445 chunk = min_t(unsigned long, data_len,
4446 PAGE_SIZE << order);
4447 skb_fill_page_desc(skb, i, page, 0, chunk);
4448 data_len -= chunk;
4449 npages -= 1 << order;
4450 }
4451 return skb;
4452
4453failure:
4454 kfree_skb(skb);
4455 return NULL;
4456}
4457EXPORT_SYMBOL(alloc_skb_with_frags);