blob: 330bc0f75a3d18de41823a88202770ca08fea7b1 [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>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030052#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070063#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000064#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070065#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040066#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000072#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
74
75#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Eric Dumazetd7e88832012-04-30 08:10:34 +000081struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010083int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Jean Sacrenf05de732013-02-11 13:30:38 +000087 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000091 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Jean Sacrenf05de732013-02-11 13:30:38 +000098static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000099 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Joe Perchese005d192012-05-16 19:58:40 +0000101 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 +0000102 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 BUG();
106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jean Sacrenf05de732013-02-11 13:30:38 +0000110 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000161struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
162{
163 struct sk_buff *skb;
164
165 /* Get the HEAD */
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
168 if (!skb)
169 goto out;
170
171 /*
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
175 */
176 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000177 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
180
Cong Wang35d04612013-05-29 15:16:05 +0800181 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000182out:
183 return skb;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
David S. Millerd179cd12005-08-17 14:57:30 -0700187 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * @size: size to allocate
189 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800194 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
201 * %GFP_ATOMIC.
202 */
Al Virodd0fc662005-10-07 07:46:04 +0100203struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700204 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Christoph Lametere18b8902006-12-06 20:33:20 -0800206 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800207 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct sk_buff *skb;
209 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700210 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Liam Mark284f2b82015-07-14 15:42:17 -0700212 if (IS_ENABLED(CONFIG_FORCE_ALLOC_FROM_DMA_ZONE))
213 gfp_mask |= GFP_DMA;
214
Mel Gormanc93bdd02012-07-31 16:44:19 -0700215 cache = (flags & SKB_ALLOC_FCLONE)
216 ? skbuff_fclone_cache : skbuff_head_cache;
217
218 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
219 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800222 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!skb)
224 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700225 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 /* We do our best to align skb_shared_info on a separate cache
228 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
229 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
230 * Both skb->head and skb_shared_info are cache line aligned.
231 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000232 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000233 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700234 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!data)
236 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000237 /* kmalloc(size) might give us more room than requested.
238 * Put skb_shared_info exactly at the end of allocated zone,
239 * to allow max possible filling before reallocation.
240 */
241 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700242 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300244 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700245 * Only clear those fields we need to clear, not those that we will
246 * actually initialise below. Hence, don't put any more fields after
247 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300248 */
249 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000250 /* Account for allocated memory : skb + skb->head */
251 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700252 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 atomic_set(&skb->users, 1);
254 skb->head = data;
255 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700256 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700257 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800258 skb->mac_header = (typeof(skb->mac_header))~0U;
259 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000260
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800261 /* make sure we initialize shinfo sequentially */
262 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700263 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800264 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000265 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800266
Mel Gormanc93bdd02012-07-31 16:44:19 -0700267 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700268 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700270 fclones = container_of(skb, struct sk_buff_fclones, skb1);
271
272 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700273 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700274 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700275
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800276 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700277 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279out:
280 return skb;
281nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800282 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 skb = NULL;
284 goto out;
285}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800286EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700289 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000290 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700291 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000292 *
293 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100294 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700295 * @frag_size is 0, otherwise data should come from the page allocator
296 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000297 * The return is the new skb buffer.
298 * On a failure the return is %NULL, and @data is not freed.
299 * Notes :
300 * Before IO, driver allocates only data buffer where NIC put incoming frame
301 * Driver should add room at head (NET_SKB_PAD) and
302 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
303 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
304 * before giving packet to stack.
305 * RX rings only contains data buffers, not full skbs.
306 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700307struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000308{
309 struct skb_shared_info *shinfo;
310 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312
313 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
314 if (!skb)
315 return NULL;
316
Eric Dumazetd3836f22012-04-27 00:33:38 +0000317 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000318
319 memset(skb, 0, offsetof(struct sk_buff, tail));
320 skb->truesize = SKB_TRUESIZE(size);
321 atomic_set(&skb->users, 1);
322 skb->head = data;
323 skb->data = data;
324 skb_reset_tail_pointer(skb);
325 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800326 skb->mac_header = (typeof(skb->mac_header))~0U;
327 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000328
329 /* make sure we initialize shinfo sequentially */
330 shinfo = skb_shinfo(skb);
331 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
332 atomic_set(&shinfo->dataref, 1);
333 kmemcheck_annotate_variable(shinfo->destructor_arg);
334
335 return skb;
336}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700337
338/* build_skb() is wrapper over __build_skb(), that specifically
339 * takes care of skb->head and skb->pfmemalloc
340 * This means that if @frag_size is not zero, then @data must be backed
341 * by a page fragment, not kmalloc() or vmalloc()
342 */
343struct sk_buff *build_skb(void *data, unsigned int frag_size)
344{
345 struct sk_buff *skb = __build_skb(data, frag_size);
346
347 if (skb && frag_size) {
348 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700349 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700350 skb->pfmemalloc = 1;
351 }
352 return skb;
353}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000354EXPORT_SYMBOL(build_skb);
355
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100356#define NAPI_SKB_CACHE_SIZE 64
357
358struct napi_alloc_cache {
359 struct page_frag_cache page;
360 size_t skb_count;
361 void *skb_cache[NAPI_SKB_CACHE_SIZE];
362};
363
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700364static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100365static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800366
367static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
368{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700369 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800370 unsigned long flags;
371 void *data;
372
373 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700374 nc = this_cpu_ptr(&netdev_alloc_cache);
375 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000376 local_irq_restore(flags);
377 return data;
378}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700379
380/**
381 * netdev_alloc_frag - allocate a page fragment
382 * @fragsz: fragment size
383 *
384 * Allocates a frag from a page for receive buffer.
385 * Uses GFP_ATOMIC allocations.
386 */
387void *netdev_alloc_frag(unsigned int fragsz)
388{
389 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
390}
Eric Dumazet6f532612012-05-18 05:12:12 +0000391EXPORT_SYMBOL(netdev_alloc_frag);
392
Alexander Duyckffde7322014-12-09 19:40:42 -0800393static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
394{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100395 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700396
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100397 return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800398}
399
400void *napi_alloc_frag(unsigned int fragsz)
401{
402 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
403}
404EXPORT_SYMBOL(napi_alloc_frag);
405
Eric Dumazet6f532612012-05-18 05:12:12 +0000406/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800407 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
408 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900409 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800410 * @gfp_mask: get_free_pages mask, passed to alloc_skb
411 *
412 * Allocate a new &sk_buff and assign it a usage count of one. The
413 * buffer has NET_SKB_PAD headroom built in. Users should allocate
414 * the headroom they think they need without accounting for the
415 * built in space. The built in space is used for optimisations.
416 *
417 * %NULL is returned if there is no free memory.
418 */
Alexander Duyck94519802015-05-06 21:11:40 -0700419struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
420 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800421{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700422 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700423 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800424 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700425 bool pfmemalloc;
426 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800427
Alexander Duyck94519802015-05-06 21:11:40 -0700428 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800429
Alexander Duyck94519802015-05-06 21:11:40 -0700430 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800431 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700432 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
433 if (!skb)
434 goto skb_fail;
435 goto skb_success;
436 }
Alexander Duyck94519802015-05-06 21:11:40 -0700437
438 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
439 len = SKB_DATA_ALIGN(len);
440
441 if (sk_memalloc_socks())
442 gfp_mask |= __GFP_MEMALLOC;
443
444 local_irq_save(flags);
445
446 nc = this_cpu_ptr(&netdev_alloc_cache);
447 data = __alloc_page_frag(nc, len, gfp_mask);
448 pfmemalloc = nc->pfmemalloc;
449
450 local_irq_restore(flags);
451
452 if (unlikely(!data))
453 return NULL;
454
455 skb = __build_skb(data, len);
456 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700457 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700458 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700459 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800460
Alexander Duyck94519802015-05-06 21:11:40 -0700461 /* use OR instead of assignment to avoid clearing of bits in mask */
462 if (pfmemalloc)
463 skb->pfmemalloc = 1;
464 skb->head_frag = 1;
465
Alexander Duycka080e7b2015-05-13 13:34:13 -0700466skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700467 skb_reserve(skb, NET_SKB_PAD);
468 skb->dev = dev;
469
Alexander Duycka080e7b2015-05-13 13:34:13 -0700470skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700471 return skb;
472}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800473EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alexander Duyckfd11a832014-12-09 19:40:49 -0800475/**
476 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
477 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900478 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800479 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
480 *
481 * Allocate a new sk_buff for use in NAPI receive. This buffer will
482 * attempt to allocate the head from a special reserved region used
483 * only for NAPI Rx allocation. By doing this we can save several
484 * CPU cycles by avoiding having to disable and re-enable IRQs.
485 *
486 * %NULL is returned if there is no free memory.
487 */
Alexander Duyck94519802015-05-06 21:11:40 -0700488struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
489 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100491 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800492 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700493 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800494
Alexander Duyck94519802015-05-06 21:11:40 -0700495 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800496
Alexander Duyck94519802015-05-06 21:11:40 -0700497 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800498 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700499 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
500 if (!skb)
501 goto skb_fail;
502 goto skb_success;
503 }
Alexander Duyck94519802015-05-06 21:11:40 -0700504
505 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
506 len = SKB_DATA_ALIGN(len);
507
508 if (sk_memalloc_socks())
509 gfp_mask |= __GFP_MEMALLOC;
510
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100511 data = __alloc_page_frag(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700512 if (unlikely(!data))
513 return NULL;
514
515 skb = __build_skb(data, len);
516 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700517 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700518 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800519 }
520
Alexander Duyck94519802015-05-06 21:11:40 -0700521 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100522 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700523 skb->pfmemalloc = 1;
524 skb->head_frag = 1;
525
Alexander Duycka080e7b2015-05-13 13:34:13 -0700526skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700527 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
528 skb->dev = napi->dev;
529
Alexander Duycka080e7b2015-05-13 13:34:13 -0700530skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800531 return skb;
532}
533EXPORT_SYMBOL(__napi_alloc_skb);
534
Peter Zijlstra654bed12008-10-07 14:22:33 -0700535void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000536 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700537{
538 skb_fill_page_desc(skb, i, page, off, size);
539 skb->len += size;
540 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000541 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700542}
543EXPORT_SYMBOL(skb_add_rx_frag);
544
Jason Wangf8e617e2013-11-01 14:07:47 +0800545void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
546 unsigned int truesize)
547{
548 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
549
550 skb_frag_size_add(frag, size);
551 skb->len += size;
552 skb->data_len += size;
553 skb->truesize += truesize;
554}
555EXPORT_SYMBOL(skb_coalesce_rx_frag);
556
Herbert Xu27b437c2006-07-13 19:26:39 -0700557static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700559 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700560 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Herbert Xu27b437c2006-07-13 19:26:39 -0700563static inline void skb_drop_fraglist(struct sk_buff *skb)
564{
565 skb_drop_list(&skb_shinfo(skb)->frag_list);
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static void skb_clone_fraglist(struct sk_buff *skb)
569{
570 struct sk_buff *list;
571
David S. Millerfbb398a2009-06-09 00:18:59 -0700572 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 skb_get(list);
574}
575
Eric Dumazetd3836f22012-04-27 00:33:38 +0000576static void skb_free_head(struct sk_buff *skb)
577{
Alexander Duyck181edb22015-05-06 21:12:03 -0700578 unsigned char *head = skb->head;
579
Eric Dumazetd3836f22012-04-27 00:33:38 +0000580 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700581 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000582 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700583 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000584}
585
Adrian Bunk5bba1712006-06-29 13:02:35 -0700586static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Eric Dumazetff04a772014-09-23 18:39:30 -0700588 struct skb_shared_info *shinfo = skb_shinfo(skb);
589 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Dumazetff04a772014-09-23 18:39:30 -0700591 if (skb->cloned &&
592 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
593 &shinfo->dataref))
594 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000595
Eric Dumazetff04a772014-09-23 18:39:30 -0700596 for (i = 0; i < shinfo->nr_frags; i++)
597 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000598
Eric Dumazetff04a772014-09-23 18:39:30 -0700599 /*
600 * If skb buf is from userspace, we need to notify the caller
601 * the lower device DMA has done;
602 */
603 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
604 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Eric Dumazetff04a772014-09-23 18:39:30 -0700606 uarg = shinfo->destructor_arg;
607 if (uarg->callback)
608 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700610
611 if (shinfo->frag_list)
612 kfree_skb_list(shinfo->frag_list);
613
614 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618 * Free an skbuff by memory without cleaning the state.
619 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800620static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700622 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700623
David S. Millerd179cd12005-08-17 14:57:30 -0700624 switch (skb->fclone) {
625 case SKB_FCLONE_UNAVAILABLE:
626 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800627 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700628
629 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700630 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800631
632 /* We usually free the clone (TX completion) before original skb
633 * This test would have no chance to be true for the clone,
634 * while here, branch prediction will be good.
635 */
636 if (atomic_read(&fclones->fclone_ref) == 1)
637 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700638 break;
639
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800640 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700641 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700642 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700643 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800644 if (!atomic_dec_and_test(&fclones->fclone_ref))
645 return;
646fastpath:
647 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700650static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Eric Dumazetadf30902009-06-02 05:19:30 +0000652 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#ifdef CONFIG_XFRM
654 secpath_put(skb->sp);
655#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700656 if (skb->destructor) {
657 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 skb->destructor(skb);
659 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000660#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700661 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100662#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200663#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 nf_bridge_put(skb->nf_bridge);
665#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700666}
667
668/* Free everything but the sk_buff shell. */
669static void skb_release_all(struct sk_buff *skb)
670{
671 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000672 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000673 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Herbert Xu2d4baff2007-11-26 23:11:19 +0800676/**
677 * __kfree_skb - private function
678 * @skb: buffer
679 *
680 * Free an sk_buff. Release anything attached to the buffer.
681 * Clean the state. This is an internal helper function. Users should
682 * always call kfree_skb
683 */
684
685void __kfree_skb(struct sk_buff *skb)
686{
687 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 kfree_skbmem(skb);
689}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800690EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800693 * kfree_skb - free an sk_buff
694 * @skb: buffer to free
695 *
696 * Drop a reference to the buffer and free it if the usage count has
697 * hit zero.
698 */
699void kfree_skb(struct sk_buff *skb)
700{
701 if (unlikely(!skb))
702 return;
703 if (likely(atomic_read(&skb->users) == 1))
704 smp_rmb();
705 else if (likely(!atomic_dec_and_test(&skb->users)))
706 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000707 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800708 __kfree_skb(skb);
709}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800710EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800711
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700712void kfree_skb_list(struct sk_buff *segs)
713{
714 while (segs) {
715 struct sk_buff *next = segs->next;
716
717 kfree_skb(segs);
718 segs = next;
719 }
720}
721EXPORT_SYMBOL(kfree_skb_list);
722
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700723/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000724 * skb_tx_error - report an sk_buff xmit error
725 * @skb: buffer that triggered an error
726 *
727 * Report xmit error if a device callback is tracking this skb.
728 * skb must be freed afterwards.
729 */
730void skb_tx_error(struct sk_buff *skb)
731{
732 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
733 struct ubuf_info *uarg;
734
735 uarg = skb_shinfo(skb)->destructor_arg;
736 if (uarg->callback)
737 uarg->callback(uarg, false);
738 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
739 }
740}
741EXPORT_SYMBOL(skb_tx_error);
742
743/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000744 * consume_skb - free an skbuff
745 * @skb: buffer to free
746 *
747 * Drop a ref to the buffer and free it if the usage count has hit zero
748 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
749 * is being dropped after a failure and notes that
750 */
751void consume_skb(struct sk_buff *skb)
752{
753 if (unlikely(!skb))
754 return;
755 if (likely(atomic_read(&skb->users) == 1))
756 smp_rmb();
757 else if (likely(!atomic_dec_and_test(&skb->users)))
758 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900759 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000760 __kfree_skb(skb);
761}
762EXPORT_SYMBOL(consume_skb);
763
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100764void __kfree_skb_flush(void)
765{
766 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
767
768 /* flush skb_cache if containing objects */
769 if (nc->skb_count) {
770 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
771 nc->skb_cache);
772 nc->skb_count = 0;
773 }
774}
775
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100776static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100777{
778 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
779
780 /* drop skb->head and call any destructors for packet */
781 skb_release_all(skb);
782
783 /* record skb to CPU local list */
784 nc->skb_cache[nc->skb_count++] = skb;
785
786#ifdef CONFIG_SLUB
787 /* SLUB writes into objects when freeing */
788 prefetchw(skb);
789#endif
790
791 /* flush skb_cache if it is filled */
792 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
793 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
794 nc->skb_cache);
795 nc->skb_count = 0;
796 }
797}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100798void __kfree_skb_defer(struct sk_buff *skb)
799{
800 _kfree_skb_defer(skb);
801}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100802
803void napi_consume_skb(struct sk_buff *skb, int budget)
804{
805 if (unlikely(!skb))
806 return;
807
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100808 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100809 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100810 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100811 return;
812 }
813
814 if (likely(atomic_read(&skb->users) == 1))
815 smp_rmb();
816 else if (likely(!atomic_dec_and_test(&skb->users)))
817 return;
818 /* if reaching here SKB is ready to free */
819 trace_consume_skb(skb);
820
821 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700822 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100823 __kfree_skb(skb);
824 return;
825 }
826
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100827 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100828}
829EXPORT_SYMBOL(napi_consume_skb);
830
Eric Dumazetb1937222014-09-28 22:18:47 -0700831/* Make sure a field is enclosed inside headers_start/headers_end section */
832#define CHECK_SKB_FIELD(field) \
833 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
834 offsetof(struct sk_buff, headers_start)); \
835 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
836 offsetof(struct sk_buff, headers_end)); \
837
Herbert Xudec18812007-10-14 00:37:30 -0700838static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
839{
840 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700841 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700842 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700843 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000844 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700845#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700846 new->sp = secpath_get(old->sp);
847#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700848 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700849
Eric Dumazetb1937222014-09-28 22:18:47 -0700850 /* Note : this field could be in headers_start/headers_end section
851 * It is not yet because we do not want to have a 16 bit hole
852 */
853 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300854
Eric Dumazetb1937222014-09-28 22:18:47 -0700855 memcpy(&new->headers_start, &old->headers_start,
856 offsetof(struct sk_buff, headers_end) -
857 offsetof(struct sk_buff, headers_start));
858 CHECK_SKB_FIELD(protocol);
859 CHECK_SKB_FIELD(csum);
860 CHECK_SKB_FIELD(hash);
861 CHECK_SKB_FIELD(priority);
862 CHECK_SKB_FIELD(skb_iif);
863 CHECK_SKB_FIELD(vlan_proto);
864 CHECK_SKB_FIELD(vlan_tci);
865 CHECK_SKB_FIELD(transport_header);
866 CHECK_SKB_FIELD(network_header);
867 CHECK_SKB_FIELD(mac_header);
868 CHECK_SKB_FIELD(inner_protocol);
869 CHECK_SKB_FIELD(inner_transport_header);
870 CHECK_SKB_FIELD(inner_network_header);
871 CHECK_SKB_FIELD(inner_mac_header);
872 CHECK_SKB_FIELD(mark);
873#ifdef CONFIG_NETWORK_SECMARK
874 CHECK_SKB_FIELD(secmark);
875#endif
Cong Wange0d10952013-08-01 11:10:25 +0800876#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700877 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300878#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800879#ifdef CONFIG_XPS
880 CHECK_SKB_FIELD(sender_cpu);
881#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700882#ifdef CONFIG_NET_SCHED
883 CHECK_SKB_FIELD(tc_index);
884#ifdef CONFIG_NET_CLS_ACT
885 CHECK_SKB_FIELD(tc_verd);
886#endif
887#endif
888
Herbert Xudec18812007-10-14 00:37:30 -0700889}
890
Herbert Xu82c49a32009-05-22 22:11:37 +0000891/*
892 * You should not add any new code to this function. Add it to
893 * __copy_skb_header above instead.
894 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700895static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#define C(x) n->x = skb->x
898
899 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700901 __copy_skb_header(n, skb);
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 C(len);
904 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700905 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700906 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800907 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 C(tail);
911 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800912 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000913 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800914 C(data);
915 C(truesize);
916 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 atomic_inc(&(skb_shinfo(skb)->dataref));
919 skb->cloned = 1;
920
921 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700922#undef C
923}
924
925/**
926 * skb_morph - morph one skb into another
927 * @dst: the skb to receive the contents
928 * @src: the skb to supply the contents
929 *
930 * This is identical to skb_clone except that the target skb is
931 * supplied by the user.
932 *
933 * The target skb is returned upon exit.
934 */
935struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
936{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800937 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700938 return __skb_clone(dst, src);
939}
940EXPORT_SYMBOL_GPL(skb_morph);
941
Ben Hutchings2c530402012-07-10 10:55:09 +0000942/**
943 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000944 * @skb: the skb to modify
945 * @gfp_mask: allocation priority
946 *
947 * This must be called on SKBTX_DEV_ZEROCOPY skb.
948 * It will copy all frags into kernel and drop the reference
949 * to userspace pages.
950 *
951 * If this function is called from an interrupt gfp_mask() must be
952 * %GFP_ATOMIC.
953 *
954 * Returns 0 on success or a negative error code on failure
955 * to allocate kernel memory to copy to.
956 */
957int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000958{
959 int i;
960 int num_frags = skb_shinfo(skb)->nr_frags;
961 struct page *page, *head = NULL;
962 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
963
964 for (i = 0; i < num_frags; i++) {
965 u8 *vaddr;
966 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
967
Krishna Kumar02756ed2012-07-17 02:05:29 +0000968 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000969 if (!page) {
970 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900971 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000972 put_page(head);
973 head = next;
974 }
975 return -ENOMEM;
976 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200977 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000978 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000979 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200980 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900981 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000982 head = page;
983 }
984
985 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000986 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000987 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000988
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000989 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000990
991 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000992 for (i = num_frags - 1; i >= 0; i--) {
993 __skb_fill_page_desc(skb, i, head, 0,
994 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900995 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000996 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000997
998 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000999 return 0;
1000}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001001EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001002
Herbert Xue0053ec2007-10-14 00:37:52 -07001003/**
1004 * skb_clone - duplicate an sk_buff
1005 * @skb: buffer to clone
1006 * @gfp_mask: allocation priority
1007 *
1008 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1009 * copies share the same packet data but not structure. The new
1010 * buffer has a reference count of 1. If the allocation fails the
1011 * function returns %NULL otherwise the new buffer is returned.
1012 *
1013 * If this function is called from an interrupt gfp_mask() must be
1014 * %GFP_ATOMIC.
1015 */
1016
1017struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1018{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001019 struct sk_buff_fclones *fclones = container_of(skb,
1020 struct sk_buff_fclones,
1021 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001022 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001023
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001024 if (skb_orphan_frags(skb, gfp_mask))
1025 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001026
Herbert Xue0053ec2007-10-14 00:37:52 -07001027 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001028 atomic_read(&fclones->fclone_ref) == 1) {
1029 n = &fclones->skb2;
1030 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001031 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001032 if (skb_pfmemalloc(skb))
1033 gfp_mask |= __GFP_MEMALLOC;
1034
Herbert Xue0053ec2007-10-14 00:37:52 -07001035 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1036 if (!n)
1037 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001038
1039 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001040 n->fclone = SKB_FCLONE_UNAVAILABLE;
1041 }
1042
1043 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001045EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001047static void skb_headers_offset_update(struct sk_buff *skb, int off)
1048{
Eric Dumazet030737b2013-10-19 11:42:54 -07001049 /* Only adjust this if it actually is csum_start rather than csum */
1050 if (skb->ip_summed == CHECKSUM_PARTIAL)
1051 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001052 /* {transport,network,mac}_header and tail are relative to skb->head */
1053 skb->transport_header += off;
1054 skb->network_header += off;
1055 if (skb_mac_header_was_set(skb))
1056 skb->mac_header += off;
1057 skb->inner_transport_header += off;
1058 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001059 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1063{
Herbert Xudec18812007-10-14 00:37:30 -07001064 __copy_skb_header(new, old);
1065
Herbert Xu79671682006-06-22 02:40:14 -07001066 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1067 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1068 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Mel Gormanc93bdd02012-07-31 16:44:19 -07001071static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1072{
1073 if (skb_pfmemalloc(skb))
1074 return SKB_ALLOC_RX;
1075 return 0;
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/**
1079 * skb_copy - create private copy of an sk_buff
1080 * @skb: buffer to copy
1081 * @gfp_mask: allocation priority
1082 *
1083 * Make a copy of both an &sk_buff and its data. This is used when the
1084 * caller wishes to modify the data and needs a private copy of the
1085 * data to alter. Returns %NULL on failure or the pointer to the buffer
1086 * on success. The returned buffer has a reference count of 1.
1087 *
1088 * As by-product this function converts non-linear &sk_buff to linear
1089 * one, so that &sk_buff becomes completely private and caller is allowed
1090 * to modify all the data of returned buffer. This means that this
1091 * function is not recommended for use in circumstances when only
1092 * header is going to be modified. Use pskb_copy() instead.
1093 */
1094
Al Virodd0fc662005-10-07 07:46:04 +01001095struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001097 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001098 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001099 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1100 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!n)
1103 return NULL;
1104
1105 /* Set the data pointer */
1106 skb_reserve(n, headerlen);
1107 /* Set the tail pointer and length */
1108 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1111 BUG();
1112
1113 copy_skb_header(n, skb);
1114 return n;
1115}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001116EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001119 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001121 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001123 * @fclone: if true allocate the copy of the skb from the fclone
1124 * cache instead of the head cache; it is recommended to set this
1125 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *
1127 * Make a copy of both an &sk_buff and part of its data, located
1128 * in header. Fragmented data remain shared. This is used when
1129 * the caller wishes to modify only header of &sk_buff and needs
1130 * private copy of the header to alter. Returns %NULL on failure
1131 * or the pointer to the buffer on success.
1132 * The returned buffer has a reference count of 1.
1133 */
1134
Octavian Purdilabad93e92014-06-12 01:36:26 +03001135struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1136 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Eric Dumazet117632e2011-12-03 21:39:53 +00001138 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001139 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1140 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!n)
1143 goto out;
1144
1145 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001146 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Set the tail pointer and length */
1148 skb_put(n, skb_headlen(skb));
1149 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001150 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Herbert Xu25f484a2006-11-07 14:57:15 -08001152 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 n->data_len = skb->data_len;
1154 n->len = skb->len;
1155
1156 if (skb_shinfo(skb)->nr_frags) {
1157 int i;
1158
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001159 if (skb_orphan_frags(skb, gfp_mask)) {
1160 kfree_skb(n);
1161 n = NULL;
1162 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1165 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001166 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 skb_shinfo(n)->nr_frags = i;
1169 }
1170
David S. Miller21dc3302010-08-23 00:13:46 -07001171 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1173 skb_clone_fraglist(n);
1174 }
1175
1176 copy_skb_header(n, skb);
1177out:
1178 return n;
1179}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001180EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182/**
1183 * pskb_expand_head - reallocate header of &sk_buff
1184 * @skb: buffer to reallocate
1185 * @nhead: room to add at head
1186 * @ntail: room to add at tail
1187 * @gfp_mask: allocation priority
1188 *
Mathias Krausebc323832013-11-07 14:18:26 +01001189 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1190 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * reference count of 1. Returns zero in the case of success or error,
1192 * if expansion failed. In the last case, &sk_buff is not changed.
1193 *
1194 * All the pointers pointing into skb header may change and must be
1195 * reloaded after call to this function.
1196 */
1197
Victor Fusco86a76ca2005-07-08 14:57:47 -07001198int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001199 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 int i;
1202 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001203 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 long off;
1205
Herbert Xu4edd87a2008-10-01 07:09:38 -07001206 BUG_ON(nhead < 0);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (skb_shared(skb))
1209 BUG();
1210
1211 size = SKB_DATA_ALIGN(size);
1212
Mel Gormanc93bdd02012-07-31 16:44:19 -07001213 if (skb_pfmemalloc(skb))
1214 gfp_mask |= __GFP_MEMALLOC;
1215 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1216 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!data)
1218 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001219 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001222 * optimized for the cases when header is void.
1223 */
1224 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1225
1226 memcpy((struct skb_shared_info *)(data + size),
1227 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001228 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Alexander Duyck3e245912012-05-04 14:26:51 +00001230 /*
1231 * if shinfo is shared we must drop the old head gracefully, but if it
1232 * is not we can just drop the old head and let the existing refcount
1233 * be since all we did is relocate the values
1234 */
1235 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001236 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001237 if (skb_orphan_frags(skb, gfp_mask))
1238 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001239 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001240 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Eric Dumazet1fd63042010-09-02 23:09:32 +00001242 if (skb_has_frag_list(skb))
1243 skb_clone_fraglist(skb);
1244
1245 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001246 } else {
1247 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 off = (data + nhead) - skb->head;
1250
1251 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001252 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001254#ifdef NET_SKBUFF_DATA_USES_OFFSET
1255 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001256 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001257#else
1258 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001259#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001260 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001261 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001263 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 skb->nohdr = 0;
1265 atomic_set(&skb_shinfo(skb)->dataref, 1);
1266 return 0;
1267
Shirley Maa6686f22011-07-06 12:22:12 +00001268nofrags:
1269 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270nodata:
1271 return -ENOMEM;
1272}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001273EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/* Make private copy of skb with writable head and some headroom */
1276
1277struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1278{
1279 struct sk_buff *skb2;
1280 int delta = headroom - skb_headroom(skb);
1281
1282 if (delta <= 0)
1283 skb2 = pskb_copy(skb, GFP_ATOMIC);
1284 else {
1285 skb2 = skb_clone(skb, GFP_ATOMIC);
1286 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1287 GFP_ATOMIC)) {
1288 kfree_skb(skb2);
1289 skb2 = NULL;
1290 }
1291 }
1292 return skb2;
1293}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001294EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296/**
1297 * skb_copy_expand - copy and expand sk_buff
1298 * @skb: buffer to copy
1299 * @newheadroom: new free bytes at head
1300 * @newtailroom: new free bytes at tail
1301 * @gfp_mask: allocation priority
1302 *
1303 * Make a copy of both an &sk_buff and its data and while doing so
1304 * allocate additional space.
1305 *
1306 * This is used when the caller wishes to modify the data and needs a
1307 * private copy of the data to alter as well as more space for new fields.
1308 * Returns %NULL on failure or the pointer to the buffer
1309 * on success. The returned buffer has a reference count of 1.
1310 *
1311 * You must pass %GFP_ATOMIC as the allocation priority if this function
1312 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
1314struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001315 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001316 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 /*
1319 * Allocate the copy buffer
1320 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001321 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1322 gfp_mask, skb_alloc_rx_flag(skb),
1323 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001324 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int head_copy_len, head_copy_off;
1326
1327 if (!n)
1328 return NULL;
1329
1330 skb_reserve(n, newheadroom);
1331
1332 /* Set the tail pointer and length */
1333 skb_put(n, skb->len);
1334
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001335 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 head_copy_off = 0;
1337 if (newheadroom <= head_copy_len)
1338 head_copy_len = newheadroom;
1339 else
1340 head_copy_off = newheadroom - head_copy_len;
1341
1342 /* Copy the linear header and data. */
1343 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1344 skb->len + head_copy_len))
1345 BUG();
1346
1347 copy_skb_header(n, skb);
1348
Eric Dumazet030737b2013-10-19 11:42:54 -07001349 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return n;
1352}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001353EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/**
1356 * skb_pad - zero pad the tail of an skb
1357 * @skb: buffer to pad
1358 * @pad: space to pad
1359 *
1360 * Ensure that a buffer is followed by a padding area that is zero
1361 * filled. Used by network drivers which may DMA or transfer data
1362 * beyond the buffer end onto the wire.
1363 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001364 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001366
Herbert Xu5b057c62006-06-23 02:06:41 -07001367int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Herbert Xu5b057c62006-06-23 02:06:41 -07001369 int err;
1370 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001373 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001377
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001378 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001379 if (likely(skb_cloned(skb) || ntail > 0)) {
1380 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1381 if (unlikely(err))
1382 goto free_skb;
1383 }
1384
1385 /* FIXME: The use of this function with non-linear skb's really needs
1386 * to be audited.
1387 */
1388 err = skb_linearize(skb);
1389 if (unlikely(err))
1390 goto free_skb;
1391
1392 memset(skb->data + skb->len, 0, pad);
1393 return 0;
1394
1395free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001397 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001398}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001399EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001400
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001401/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001402 * pskb_put - add data to the tail of a potentially fragmented buffer
1403 * @skb: start of the buffer to use
1404 * @tail: tail fragment of the buffer to use
1405 * @len: amount of data to add
1406 *
1407 * This function extends the used data area of the potentially
1408 * fragmented buffer. @tail must be the last fragment of @skb -- or
1409 * @skb itself. If this would exceed the total buffer size the kernel
1410 * will panic. A pointer to the first byte of the extra data is
1411 * returned.
1412 */
1413
1414unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1415{
1416 if (tail != skb) {
1417 skb->data_len += len;
1418 skb->len += len;
1419 }
1420 return skb_put(tail, len);
1421}
1422EXPORT_SYMBOL_GPL(pskb_put);
1423
1424/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001425 * skb_put - add data to a buffer
1426 * @skb: buffer to use
1427 * @len: amount of data to add
1428 *
1429 * This function extends the used data area of the buffer. If this would
1430 * exceed the total buffer size the kernel will panic. A pointer to the
1431 * first byte of the extra data is returned.
1432 */
1433unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1434{
1435 unsigned char *tmp = skb_tail_pointer(skb);
1436 SKB_LINEAR_ASSERT(skb);
1437 skb->tail += len;
1438 skb->len += len;
1439 if (unlikely(skb->tail > skb->end))
1440 skb_over_panic(skb, len, __builtin_return_address(0));
1441 return tmp;
1442}
1443EXPORT_SYMBOL(skb_put);
1444
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001445/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001446 * skb_push - add data to the start of a buffer
1447 * @skb: buffer to use
1448 * @len: amount of data to add
1449 *
1450 * This function extends the used data area of the buffer at the buffer
1451 * start. If this would exceed the total buffer headroom the kernel will
1452 * panic. A pointer to the first byte of the extra data is returned.
1453 */
1454unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1455{
1456 skb->data -= len;
1457 skb->len += len;
1458 if (unlikely(skb->data<skb->head))
1459 skb_under_panic(skb, len, __builtin_return_address(0));
1460 return skb->data;
1461}
1462EXPORT_SYMBOL(skb_push);
1463
1464/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001465 * skb_pull - remove data from the start of a buffer
1466 * @skb: buffer to use
1467 * @len: amount of data to remove
1468 *
1469 * This function removes data from the start of a buffer, returning
1470 * the memory to the headroom. A pointer to the next data in the buffer
1471 * is returned. Once the data has been pulled future pushes will overwrite
1472 * the old data.
1473 */
1474unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1475{
David S. Miller47d29642010-05-02 02:21:44 -07001476 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001477}
1478EXPORT_SYMBOL(skb_pull);
1479
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001480/**
1481 * skb_trim - remove end from a buffer
1482 * @skb: buffer to alter
1483 * @len: new length
1484 *
1485 * Cut the length of a buffer down by removing data from the tail. If
1486 * the buffer is already under the length specified it is not modified.
1487 * The skb must be linear.
1488 */
1489void skb_trim(struct sk_buff *skb, unsigned int len)
1490{
1491 if (skb->len > len)
1492 __skb_trim(skb, len);
1493}
1494EXPORT_SYMBOL(skb_trim);
1495
Herbert Xu3cc0e872006-06-09 16:13:38 -07001496/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
1498
Herbert Xu3cc0e872006-06-09 16:13:38 -07001499int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Herbert Xu27b437c2006-07-13 19:26:39 -07001501 struct sk_buff **fragp;
1502 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int offset = skb_headlen(skb);
1504 int nfrags = skb_shinfo(skb)->nr_frags;
1505 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001506 int err;
1507
1508 if (skb_cloned(skb) &&
1509 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1510 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001512 i = 0;
1513 if (offset >= len)
1514 goto drop_pages;
1515
1516 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001517 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001518
1519 if (end < len) {
1520 offset = end;
1521 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001523
Eric Dumazet9e903e02011-10-18 21:00:24 +00001524 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001525
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001526drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001527 skb_shinfo(skb)->nr_frags = i;
1528
1529 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001530 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001531
David S. Miller21dc3302010-08-23 00:13:46 -07001532 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001533 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001534 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
Herbert Xu27b437c2006-07-13 19:26:39 -07001537 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1538 fragp = &frag->next) {
1539 int end = offset + frag->len;
1540
1541 if (skb_shared(frag)) {
1542 struct sk_buff *nfrag;
1543
1544 nfrag = skb_clone(frag, GFP_ATOMIC);
1545 if (unlikely(!nfrag))
1546 return -ENOMEM;
1547
1548 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001549 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001550 frag = nfrag;
1551 *fragp = frag;
1552 }
1553
1554 if (end < len) {
1555 offset = end;
1556 continue;
1557 }
1558
1559 if (end > len &&
1560 unlikely((err = pskb_trim(frag, len - offset))))
1561 return err;
1562
1563 if (frag->next)
1564 skb_drop_list(&frag->next);
1565 break;
1566 }
1567
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001568done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001569 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 skb->data_len -= skb->len - len;
1571 skb->len = len;
1572 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001573 skb->len = len;
1574 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001575 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 return 0;
1579}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001580EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/**
1583 * __pskb_pull_tail - advance tail of skb header
1584 * @skb: buffer to reallocate
1585 * @delta: number of bytes to advance tail
1586 *
1587 * The function makes a sense only on a fragmented &sk_buff,
1588 * it expands header moving its tail forward and copying necessary
1589 * data from fragmented part.
1590 *
1591 * &sk_buff MUST have reference count of 1.
1592 *
1593 * Returns %NULL (and &sk_buff does not change) if pull failed
1594 * or value of new tail of skb in the case of success.
1595 *
1596 * All the pointers pointing into skb header may change and must be
1597 * reloaded after call to this function.
1598 */
1599
1600/* Moves tail of skb head forward, copying data from fragmented part,
1601 * when it is necessary.
1602 * 1. It may fail due to malloc failure.
1603 * 2. It may change skb pointers.
1604 *
1605 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1606 */
1607unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1608{
1609 /* If skb has not enough free space at tail, get new one
1610 * plus 128 bytes for future expansions. If we have enough
1611 * room at tail, reallocate without expansion only if skb is cloned.
1612 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001613 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 if (eat > 0 || skb_cloned(skb)) {
1616 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1617 GFP_ATOMIC))
1618 return NULL;
1619 }
1620
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001621 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 BUG();
1623
1624 /* Optimization: no fragments, no reasons to preestimate
1625 * size of pulled pages. Superb.
1626 */
David S. Miller21dc3302010-08-23 00:13:46 -07001627 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto pull_pages;
1629
1630 /* Estimate size of pulled pages. */
1631 eat = delta;
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001633 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1634
1635 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001637 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
1640 /* If we need update frag list, we are in troubles.
1641 * Certainly, it possible to add an offset to skb data,
1642 * but taking into account that pulling is expected to
1643 * be very rare operation, it is worth to fight against
1644 * further bloating skb head and crucify ourselves here instead.
1645 * Pure masohism, indeed. 8)8)
1646 */
1647 if (eat) {
1648 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1649 struct sk_buff *clone = NULL;
1650 struct sk_buff *insp = NULL;
1651
1652 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001653 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (list->len <= eat) {
1656 /* Eaten as whole. */
1657 eat -= list->len;
1658 list = list->next;
1659 insp = list;
1660 } else {
1661 /* Eaten partially. */
1662
1663 if (skb_shared(list)) {
1664 /* Sucks! We need to fork list. :-( */
1665 clone = skb_clone(list, GFP_ATOMIC);
1666 if (!clone)
1667 return NULL;
1668 insp = list->next;
1669 list = clone;
1670 } else {
1671 /* This may be pulled without
1672 * problems. */
1673 insp = list;
1674 }
1675 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001676 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return NULL;
1678 }
1679 break;
1680 }
1681 } while (eat);
1682
1683 /* Free pulled out fragments. */
1684 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1685 skb_shinfo(skb)->frag_list = list->next;
1686 kfree_skb(list);
1687 }
1688 /* And insert new clone at head. */
1689 if (clone) {
1690 clone->next = list;
1691 skb_shinfo(skb)->frag_list = clone;
1692 }
1693 }
1694 /* Success! Now we may commit changes to skb data. */
1695
1696pull_pages:
1697 eat = delta;
1698 k = 0;
1699 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001700 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1701
1702 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001703 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001704 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 } else {
1706 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1707 if (eat) {
1708 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001709 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 eat = 0;
1711 }
1712 k++;
1713 }
1714 }
1715 skb_shinfo(skb)->nr_frags = k;
1716
1717 skb->tail += delta;
1718 skb->data_len -= delta;
1719
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001720 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001722EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Eric Dumazet22019b12011-07-29 18:37:31 +00001724/**
1725 * skb_copy_bits - copy bits from skb to kernel buffer
1726 * @skb: source skb
1727 * @offset: offset in source
1728 * @to: destination buffer
1729 * @len: number of bytes to copy
1730 *
1731 * Copy the specified number of bytes from the source skb to the
1732 * destination buffer.
1733 *
1734 * CAUTION ! :
1735 * If its prototype is ever changed,
1736 * check arch/{*}/net/{*}.S files,
1737 * since it is called from BPF assembly code.
1738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1740{
David S. Miller1a028e52007-04-27 15:21:23 -07001741 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001742 struct sk_buff *frag_iter;
1743 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (offset > (int)skb->len - len)
1746 goto fault;
1747
1748 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001749 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (copy > len)
1751 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001752 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if ((len -= copy) == 0)
1754 return 0;
1755 offset += copy;
1756 to += copy;
1757 }
1758
1759 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001760 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001761 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001763 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001764
Eric Dumazet51c56b02012-04-05 11:35:15 +02001765 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if ((copy = end - offset) > 0) {
1767 u8 *vaddr;
1768
1769 if (copy > len)
1770 copy = len;
1771
Eric Dumazet51c56b02012-04-05 11:35:15 +02001772 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001774 vaddr + f->page_offset + offset - start,
1775 copy);
1776 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 if ((len -= copy) == 0)
1779 return 0;
1780 offset += copy;
1781 to += copy;
1782 }
David S. Miller1a028e52007-04-27 15:21:23 -07001783 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
David S. Millerfbb398a2009-06-09 00:18:59 -07001786 skb_walk_frags(skb, frag_iter) {
1787 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
David S. Millerfbb398a2009-06-09 00:18:59 -07001789 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
David S. Millerfbb398a2009-06-09 00:18:59 -07001791 end = start + frag_iter->len;
1792 if ((copy = end - offset) > 0) {
1793 if (copy > len)
1794 copy = len;
1795 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1796 goto fault;
1797 if ((len -= copy) == 0)
1798 return 0;
1799 offset += copy;
1800 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001802 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Shirley Maa6686f22011-07-06 12:22:12 +00001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!len)
1806 return 0;
1807
1808fault:
1809 return -EFAULT;
1810}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001811EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jens Axboe9c55e012007-11-06 23:30:13 -08001813/*
1814 * Callback from splice_to_pipe(), if we need to release some pages
1815 * at the end of the spd in case we error'ed out in filling the pipe.
1816 */
1817static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1818{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001819 put_page(spd->pages[i]);
1820}
Jens Axboe9c55e012007-11-06 23:30:13 -08001821
David S. Millera108d5f2012-04-23 23:06:11 -04001822static struct page *linear_to_page(struct page *page, unsigned int *len,
1823 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001824 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001825{
Eric Dumazet5640f762012-09-23 23:04:42 +00001826 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001827
Eric Dumazet5640f762012-09-23 23:04:42 +00001828 if (!sk_page_frag_refill(sk, pfrag))
1829 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001830
Eric Dumazet5640f762012-09-23 23:04:42 +00001831 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001832
Eric Dumazet5640f762012-09-23 23:04:42 +00001833 memcpy(page_address(pfrag->page) + pfrag->offset,
1834 page_address(page) + *offset, *len);
1835 *offset = pfrag->offset;
1836 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001837
Eric Dumazet5640f762012-09-23 23:04:42 +00001838 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001839}
1840
Eric Dumazet41c73a02012-04-22 12:26:16 +00001841static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1842 struct page *page,
1843 unsigned int offset)
1844{
1845 return spd->nr_pages &&
1846 spd->pages[spd->nr_pages - 1] == page &&
1847 (spd->partial[spd->nr_pages - 1].offset +
1848 spd->partial[spd->nr_pages - 1].len == offset);
1849}
1850
Jens Axboe9c55e012007-11-06 23:30:13 -08001851/*
1852 * Fill page/offset/length into spd, if it can hold more pages.
1853 */
David S. Millera108d5f2012-04-23 23:06:11 -04001854static bool spd_fill_page(struct splice_pipe_desc *spd,
1855 struct pipe_inode_info *pipe, struct page *page,
1856 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001857 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001858 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001859{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001860 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001861 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001862
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001863 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001864 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001865 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001866 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001867 }
1868 if (spd_can_coalesce(spd, page, offset)) {
1869 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001870 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001871 }
1872 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001873 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001874 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001875 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001876 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001877
David S. Millera108d5f2012-04-23 23:06:11 -04001878 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001879}
1880
David S. Millera108d5f2012-04-23 23:06:11 -04001881static bool __splice_segment(struct page *page, unsigned int poff,
1882 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001883 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001884 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001885 struct sock *sk,
1886 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001887{
1888 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001889 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001890
1891 /* skip this segment if already processed */
1892 if (*off >= plen) {
1893 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001894 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001895 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001896
Octavian Purdila2870c432008-07-15 00:49:11 -07001897 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001898 poff += *off;
1899 plen -= *off;
1900 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001901
Eric Dumazet18aafc62013-01-11 14:46:37 +00001902 do {
1903 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001904
Eric Dumazet18aafc62013-01-11 14:46:37 +00001905 if (spd_fill_page(spd, pipe, page, &flen, poff,
1906 linear, sk))
1907 return true;
1908 poff += flen;
1909 plen -= flen;
1910 *len -= flen;
1911 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001912
David S. Millera108d5f2012-04-23 23:06:11 -04001913 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001914}
1915
1916/*
David S. Millera108d5f2012-04-23 23:06:11 -04001917 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001918 * pipe is full or if we already spliced the requested length.
1919 */
David S. Millera108d5f2012-04-23 23:06:11 -04001920static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1921 unsigned int *offset, unsigned int *len,
1922 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001923{
1924 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001925 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001926
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001927 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001928 * If skb->head_frag is set, this 'linear' part is backed by a
1929 * fragment, and if the head is not shared with any clones then
1930 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001931 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001932 if (__splice_segment(virt_to_page(skb->data),
1933 (unsigned long) skb->data & (PAGE_SIZE - 1),
1934 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001935 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001936 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001937 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001938 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939
1940 /*
1941 * then map the fragments
1942 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001943 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1944 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1945
Ian Campbellea2ab692011-08-22 23:44:58 +00001946 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001947 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001948 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001949 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001950 }
1951
Tom Herbertfa9835e2016-03-07 14:11:04 -08001952 skb_walk_frags(skb, iter) {
1953 if (*offset >= iter->len) {
1954 *offset -= iter->len;
1955 continue;
1956 }
1957 /* __skb_splice_bits() only fails if the output has no room
1958 * left, so no point in going over the frag_list for the error
1959 * case.
1960 */
1961 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1962 return true;
1963 }
1964
David S. Millera108d5f2012-04-23 23:06:11 -04001965 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001966}
1967
1968/*
1969 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001970 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001971 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001972int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001973 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001974 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001975{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001976 struct partial_page partial[MAX_SKB_FRAGS];
1977 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001978 struct splice_pipe_desc spd = {
1979 .pages = pages,
1980 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001981 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001982 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001983 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001984 .spd_release = sock_spd_release,
1985 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001986 int ret = 0;
1987
Tom Herbertfa9835e2016-03-07 14:11:04 -08001988 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001989
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001990 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04001991 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001992
Jens Axboe35f3d142010-05-20 10:43:18 +02001993 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001994}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001995EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001996
Herbert Xu357b40a2005-04-19 22:30:14 -07001997/**
1998 * skb_store_bits - store bits from kernel buffer to skb
1999 * @skb: destination buffer
2000 * @offset: offset in destination
2001 * @from: source buffer
2002 * @len: number of bytes to copy
2003 *
2004 * Copy the specified number of bytes from the source buffer to the
2005 * destination skb. This function handles all the messy bits of
2006 * traversing fragment lists and such.
2007 */
2008
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002009int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002010{
David S. Miller1a028e52007-04-27 15:21:23 -07002011 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002012 struct sk_buff *frag_iter;
2013 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002014
2015 if (offset > (int)skb->len - len)
2016 goto fault;
2017
David S. Miller1a028e52007-04-27 15:21:23 -07002018 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002019 if (copy > len)
2020 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002021 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002022 if ((len -= copy) == 0)
2023 return 0;
2024 offset += copy;
2025 from += copy;
2026 }
2027
2028 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2029 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002030 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002031
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002032 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002033
Eric Dumazet9e903e02011-10-18 21:00:24 +00002034 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002035 if ((copy = end - offset) > 0) {
2036 u8 *vaddr;
2037
2038 if (copy > len)
2039 copy = len;
2040
Eric Dumazet51c56b02012-04-05 11:35:15 +02002041 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002042 memcpy(vaddr + frag->page_offset + offset - start,
2043 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002044 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002045
2046 if ((len -= copy) == 0)
2047 return 0;
2048 offset += copy;
2049 from += copy;
2050 }
David S. Miller1a028e52007-04-27 15:21:23 -07002051 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002052 }
2053
David S. Millerfbb398a2009-06-09 00:18:59 -07002054 skb_walk_frags(skb, frag_iter) {
2055 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002056
David S. Millerfbb398a2009-06-09 00:18:59 -07002057 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002058
David S. Millerfbb398a2009-06-09 00:18:59 -07002059 end = start + frag_iter->len;
2060 if ((copy = end - offset) > 0) {
2061 if (copy > len)
2062 copy = len;
2063 if (skb_store_bits(frag_iter, offset - start,
2064 from, copy))
2065 goto fault;
2066 if ((len -= copy) == 0)
2067 return 0;
2068 offset += copy;
2069 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002070 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002071 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002072 }
2073 if (!len)
2074 return 0;
2075
2076fault:
2077 return -EFAULT;
2078}
Herbert Xu357b40a2005-04-19 22:30:14 -07002079EXPORT_SYMBOL(skb_store_bits);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002082__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2083 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
David S. Miller1a028e52007-04-27 15:21:23 -07002085 int start = skb_headlen(skb);
2086 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002087 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int pos = 0;
2089
2090 /* Checksum header. */
2091 if (copy > 0) {
2092 if (copy > len)
2093 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002094 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if ((len -= copy) == 0)
2096 return csum;
2097 offset += copy;
2098 pos = copy;
2099 }
2100
2101 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002102 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002103 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002105 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002106
Eric Dumazet51c56b02012-04-05 11:35:15 +02002107 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002109 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 if (copy > len)
2113 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002114 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002115 csum2 = ops->update(vaddr + frag->page_offset +
2116 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002117 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002118 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (!(len -= copy))
2120 return csum;
2121 offset += copy;
2122 pos += copy;
2123 }
David S. Miller1a028e52007-04-27 15:21:23 -07002124 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
David S. Millerfbb398a2009-06-09 00:18:59 -07002127 skb_walk_frags(skb, frag_iter) {
2128 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
David S. Millerfbb398a2009-06-09 00:18:59 -07002130 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
David S. Millerfbb398a2009-06-09 00:18:59 -07002132 end = start + frag_iter->len;
2133 if ((copy = end - offset) > 0) {
2134 __wsum csum2;
2135 if (copy > len)
2136 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002137 csum2 = __skb_checksum(frag_iter, offset - start,
2138 copy, 0, ops);
2139 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002140 if ((len -= copy) == 0)
2141 return csum;
2142 offset += copy;
2143 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002145 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002147 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 return csum;
2150}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002151EXPORT_SYMBOL(__skb_checksum);
2152
2153__wsum skb_checksum(const struct sk_buff *skb, int offset,
2154 int len, __wsum csum)
2155{
2156 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002157 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002158 .combine = csum_block_add_ext,
2159 };
2160
2161 return __skb_checksum(skb, offset, len, csum, &ops);
2162}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002163EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165/* Both of above in one bottle. */
2166
Al Viro81d77662006-11-14 21:37:33 -08002167__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2168 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
David S. Miller1a028e52007-04-27 15:21:23 -07002170 int start = skb_headlen(skb);
2171 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002172 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 int pos = 0;
2174
2175 /* Copy header. */
2176 if (copy > 0) {
2177 if (copy > len)
2178 copy = len;
2179 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2180 copy, csum);
2181 if ((len -= copy) == 0)
2182 return csum;
2183 offset += copy;
2184 to += copy;
2185 pos = copy;
2186 }
2187
2188 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002189 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002191 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002192
Eric Dumazet9e903e02011-10-18 21:00:24 +00002193 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002195 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 u8 *vaddr;
2197 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2198
2199 if (copy > len)
2200 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002201 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002203 frag->page_offset +
2204 offset - start, to,
2205 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002206 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 csum = csum_block_add(csum, csum2, pos);
2208 if (!(len -= copy))
2209 return csum;
2210 offset += copy;
2211 to += copy;
2212 pos += copy;
2213 }
David S. Miller1a028e52007-04-27 15:21:23 -07002214 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216
David S. Millerfbb398a2009-06-09 00:18:59 -07002217 skb_walk_frags(skb, frag_iter) {
2218 __wsum csum2;
2219 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
David S. Millerfbb398a2009-06-09 00:18:59 -07002221 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
David S. Millerfbb398a2009-06-09 00:18:59 -07002223 end = start + frag_iter->len;
2224 if ((copy = end - offset) > 0) {
2225 if (copy > len)
2226 copy = len;
2227 csum2 = skb_copy_and_csum_bits(frag_iter,
2228 offset - start,
2229 to, copy, 0);
2230 csum = csum_block_add(csum, csum2, pos);
2231 if ((len -= copy) == 0)
2232 return csum;
2233 offset += copy;
2234 to += copy;
2235 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002237 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002239 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return csum;
2241}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002242EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Thomas Grafaf2806f2013-12-13 15:22:17 +01002244 /**
2245 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2246 * @from: source buffer
2247 *
2248 * Calculates the amount of linear headroom needed in the 'to' skb passed
2249 * into skb_zerocopy().
2250 */
2251unsigned int
2252skb_zerocopy_headlen(const struct sk_buff *from)
2253{
2254 unsigned int hlen = 0;
2255
2256 if (!from->head_frag ||
2257 skb_headlen(from) < L1_CACHE_BYTES ||
2258 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2259 hlen = skb_headlen(from);
2260
2261 if (skb_has_frag_list(from))
2262 hlen = from->len;
2263
2264 return hlen;
2265}
2266EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2267
2268/**
2269 * skb_zerocopy - Zero copy skb to skb
2270 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002271 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002272 * @len: number of bytes to copy from source buffer
2273 * @hlen: size of linear headroom in destination buffer
2274 *
2275 * Copies up to `len` bytes from `from` to `to` by creating references
2276 * to the frags in the source buffer.
2277 *
2278 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2279 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002280 *
2281 * Return value:
2282 * 0: everything is OK
2283 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2284 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002285 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002286int
2287skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002288{
2289 int i, j = 0;
2290 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002291 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002292 struct page *page;
2293 unsigned int offset;
2294
2295 BUG_ON(!from->head_frag && !hlen);
2296
2297 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002298 if (len <= skb_tailroom(to))
2299 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002300
2301 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002302 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2303 if (unlikely(ret))
2304 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002305 len -= hlen;
2306 } else {
2307 plen = min_t(int, skb_headlen(from), len);
2308 if (plen) {
2309 page = virt_to_head_page(from->head);
2310 offset = from->data - (unsigned char *)page_address(page);
2311 __skb_fill_page_desc(to, 0, page, offset, plen);
2312 get_page(page);
2313 j = 1;
2314 len -= plen;
2315 }
2316 }
2317
2318 to->truesize += len + plen;
2319 to->len += len + plen;
2320 to->data_len += len + plen;
2321
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002322 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2323 skb_tx_error(from);
2324 return -ENOMEM;
2325 }
2326
Thomas Grafaf2806f2013-12-13 15:22:17 +01002327 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2328 if (!len)
2329 break;
2330 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2331 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2332 len -= skb_shinfo(to)->frags[j].size;
2333 skb_frag_ref(to, j);
2334 j++;
2335 }
2336 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002337
2338 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002339}
2340EXPORT_SYMBOL_GPL(skb_zerocopy);
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2343{
Al Virod3bc23e2006-11-14 21:24:49 -08002344 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 long csstart;
2346
Patrick McHardy84fa7932006-08-29 16:44:56 -07002347 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002348 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 else
2350 csstart = skb_headlen(skb);
2351
Kris Katterjohn09a62662006-01-08 22:24:28 -08002352 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002354 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 csum = 0;
2357 if (csstart != skb->len)
2358 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2359 skb->len - csstart, 0);
2360
Patrick McHardy84fa7932006-08-29 16:44:56 -07002361 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002362 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Al Virod3bc23e2006-11-14 21:24:49 -08002364 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002367EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369/**
2370 * skb_dequeue - remove from the head of the queue
2371 * @list: list to dequeue from
2372 *
2373 * Remove the head of the list. The list lock is taken so the function
2374 * may be used safely with other locking list functions. The head item is
2375 * returned or %NULL if the list is empty.
2376 */
2377
2378struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2379{
2380 unsigned long flags;
2381 struct sk_buff *result;
2382
2383 spin_lock_irqsave(&list->lock, flags);
2384 result = __skb_dequeue(list);
2385 spin_unlock_irqrestore(&list->lock, flags);
2386 return result;
2387}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002388EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390/**
2391 * skb_dequeue_tail - remove from the tail of the queue
2392 * @list: list to dequeue from
2393 *
2394 * Remove the tail of the list. The list lock is taken so the function
2395 * may be used safely with other locking list functions. The tail item is
2396 * returned or %NULL if the list is empty.
2397 */
2398struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2399{
2400 unsigned long flags;
2401 struct sk_buff *result;
2402
2403 spin_lock_irqsave(&list->lock, flags);
2404 result = __skb_dequeue_tail(list);
2405 spin_unlock_irqrestore(&list->lock, flags);
2406 return result;
2407}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002408EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410/**
2411 * skb_queue_purge - empty a list
2412 * @list: list to empty
2413 *
2414 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2415 * the list and one reference dropped. This function takes the list
2416 * lock and is atomic with respect to other list locking functions.
2417 */
2418void skb_queue_purge(struct sk_buff_head *list)
2419{
2420 struct sk_buff *skb;
2421 while ((skb = skb_dequeue(list)) != NULL)
2422 kfree_skb(skb);
2423}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002424EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002427 * skb_rbtree_purge - empty a skb rbtree
2428 * @root: root of the rbtree to empty
2429 *
2430 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2431 * the list and one reference dropped. This function does not take
2432 * any lock. Synchronization should be handled by the caller (e.g., TCP
2433 * out-of-order queue is protected by the socket lock).
2434 */
2435void skb_rbtree_purge(struct rb_root *root)
2436{
2437 struct sk_buff *skb, *next;
2438
2439 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2440 kfree_skb(skb);
2441
2442 *root = RB_ROOT;
2443}
2444
2445/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * skb_queue_head - queue a buffer at the list head
2447 * @list: list to use
2448 * @newsk: buffer to queue
2449 *
2450 * Queue a buffer at the start of the list. This function takes the
2451 * list lock and can be used safely with other locking &sk_buff functions
2452 * safely.
2453 *
2454 * A buffer cannot be placed on two lists at the same time.
2455 */
2456void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2457{
2458 unsigned long flags;
2459
2460 spin_lock_irqsave(&list->lock, flags);
2461 __skb_queue_head(list, newsk);
2462 spin_unlock_irqrestore(&list->lock, flags);
2463}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002464EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466/**
2467 * skb_queue_tail - queue a buffer at the list tail
2468 * @list: list to use
2469 * @newsk: buffer to queue
2470 *
2471 * Queue a buffer at the tail of the list. This function takes the
2472 * list lock and can be used safely with other locking &sk_buff functions
2473 * safely.
2474 *
2475 * A buffer cannot be placed on two lists at the same time.
2476 */
2477void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2478{
2479 unsigned long flags;
2480
2481 spin_lock_irqsave(&list->lock, flags);
2482 __skb_queue_tail(list, newsk);
2483 spin_unlock_irqrestore(&list->lock, flags);
2484}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002485EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/**
2488 * skb_unlink - remove a buffer from a list
2489 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002490 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 *
David S. Miller8728b832005-08-09 19:25:21 -07002492 * Remove a packet from a list. The list locks are taken and this
2493 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 *
David S. Miller8728b832005-08-09 19:25:21 -07002495 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 */
David S. Miller8728b832005-08-09 19:25:21 -07002497void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
David S. Miller8728b832005-08-09 19:25:21 -07002499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
David S. Miller8728b832005-08-09 19:25:21 -07002501 spin_lock_irqsave(&list->lock, flags);
2502 __skb_unlink(skb, list);
2503 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002505EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/**
2508 * skb_append - append a buffer
2509 * @old: buffer to insert after
2510 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002511 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 *
2513 * Place a packet after a given packet in a list. The list locks are taken
2514 * and this function is atomic with respect to other list locked calls.
2515 * A buffer cannot be placed on two lists at the same time.
2516 */
David S. Miller8728b832005-08-09 19:25:21 -07002517void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 unsigned long flags;
2520
David S. Miller8728b832005-08-09 19:25:21 -07002521 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002522 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002523 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002525EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527/**
2528 * skb_insert - insert a buffer
2529 * @old: buffer to insert before
2530 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002531 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 *
David S. Miller8728b832005-08-09 19:25:21 -07002533 * Place a packet before a given packet in a list. The list locks are
2534 * taken and this function is atomic with respect to other list locked
2535 * calls.
2536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 * A buffer cannot be placed on two lists at the same time.
2538 */
David S. Miller8728b832005-08-09 19:25:21 -07002539void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 unsigned long flags;
2542
David S. Miller8728b832005-08-09 19:25:21 -07002543 spin_lock_irqsave(&list->lock, flags);
2544 __skb_insert(newsk, old->prev, old, list);
2545 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002547EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549static inline void skb_split_inside_header(struct sk_buff *skb,
2550 struct sk_buff* skb1,
2551 const u32 len, const int pos)
2552{
2553 int i;
2554
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002555 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2556 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* And move data appendix as is. */
2558 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2559 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2560
2561 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2562 skb_shinfo(skb)->nr_frags = 0;
2563 skb1->data_len = skb->data_len;
2564 skb1->len += skb1->data_len;
2565 skb->data_len = 0;
2566 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002567 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570static inline void skb_split_no_header(struct sk_buff *skb,
2571 struct sk_buff* skb1,
2572 const u32 len, int pos)
2573{
2574 int i, k = 0;
2575 const int nfrags = skb_shinfo(skb)->nr_frags;
2576
2577 skb_shinfo(skb)->nr_frags = 0;
2578 skb1->len = skb1->data_len = skb->len - len;
2579 skb->len = len;
2580 skb->data_len = len - pos;
2581
2582 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002583 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 if (pos + size > len) {
2586 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2587
2588 if (pos < len) {
2589 /* Split frag.
2590 * We have two variants in this case:
2591 * 1. Move all the frag to the second
2592 * part, if it is possible. F.e.
2593 * this approach is mandatory for TUX,
2594 * where splitting is expensive.
2595 * 2. Split is accurately. We make this.
2596 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002597 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002599 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2600 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 skb_shinfo(skb)->nr_frags++;
2602 }
2603 k++;
2604 } else
2605 skb_shinfo(skb)->nr_frags++;
2606 pos += size;
2607 }
2608 skb_shinfo(skb1)->nr_frags = k;
2609}
2610
2611/**
2612 * skb_split - Split fragmented skb to two parts at length len.
2613 * @skb: the buffer to split
2614 * @skb1: the buffer to receive the second part
2615 * @len: new length for skb
2616 */
2617void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2618{
2619 int pos = skb_headlen(skb);
2620
Amerigo Wang68534c62013-02-19 22:51:30 +00002621 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (len < pos) /* Split line is inside header. */
2623 skb_split_inside_header(skb, skb1, len, pos);
2624 else /* Second chunk has no header, nothing to copy. */
2625 skb_split_no_header(skb, skb1, len, pos);
2626}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002627EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002629/* Shifting from/to a cloned skb is a no-go.
2630 *
2631 * Caller cannot keep skb_shinfo related pointers past calling here!
2632 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002633static int skb_prepare_for_shift(struct sk_buff *skb)
2634{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002635 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002636}
2637
2638/**
2639 * skb_shift - Shifts paged data partially from skb to another
2640 * @tgt: buffer into which tail data gets added
2641 * @skb: buffer from which the paged data comes from
2642 * @shiftlen: shift up to this many bytes
2643 *
2644 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002645 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002646 * It's up to caller to free skb if everything was shifted.
2647 *
2648 * If @tgt runs out of frags, the whole operation is aborted.
2649 *
2650 * Skb cannot include anything else but paged data while tgt is allowed
2651 * to have non-paged data as well.
2652 *
2653 * TODO: full sized shift could be optimized but that would need
2654 * specialized skb free'er to handle frags without up-to-date nr_frags.
2655 */
2656int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2657{
2658 int from, to, merge, todo;
2659 struct skb_frag_struct *fragfrom, *fragto;
2660
2661 BUG_ON(shiftlen > skb->len);
2662 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2663
2664 todo = shiftlen;
2665 from = 0;
2666 to = skb_shinfo(tgt)->nr_frags;
2667 fragfrom = &skb_shinfo(skb)->frags[from];
2668
2669 /* Actual merge is delayed until the point when we know we can
2670 * commit all, so that we don't have to undo partial changes
2671 */
2672 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002673 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2674 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002675 merge = -1;
2676 } else {
2677 merge = to - 1;
2678
Eric Dumazet9e903e02011-10-18 21:00:24 +00002679 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002680 if (todo < 0) {
2681 if (skb_prepare_for_shift(skb) ||
2682 skb_prepare_for_shift(tgt))
2683 return 0;
2684
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002685 /* All previous frag pointers might be stale! */
2686 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002687 fragto = &skb_shinfo(tgt)->frags[merge];
2688
Eric Dumazet9e903e02011-10-18 21:00:24 +00002689 skb_frag_size_add(fragto, shiftlen);
2690 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002691 fragfrom->page_offset += shiftlen;
2692
2693 goto onlymerged;
2694 }
2695
2696 from++;
2697 }
2698
2699 /* Skip full, not-fitting skb to avoid expensive operations */
2700 if ((shiftlen == skb->len) &&
2701 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2702 return 0;
2703
2704 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2705 return 0;
2706
2707 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2708 if (to == MAX_SKB_FRAGS)
2709 return 0;
2710
2711 fragfrom = &skb_shinfo(skb)->frags[from];
2712 fragto = &skb_shinfo(tgt)->frags[to];
2713
Eric Dumazet9e903e02011-10-18 21:00:24 +00002714 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002715 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002716 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002717 from++;
2718 to++;
2719
2720 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002721 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002722 fragto->page = fragfrom->page;
2723 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002724 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002725
2726 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002727 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002728 todo = 0;
2729
2730 to++;
2731 break;
2732 }
2733 }
2734
2735 /* Ready to "commit" this state change to tgt */
2736 skb_shinfo(tgt)->nr_frags = to;
2737
2738 if (merge >= 0) {
2739 fragfrom = &skb_shinfo(skb)->frags[0];
2740 fragto = &skb_shinfo(tgt)->frags[merge];
2741
Eric Dumazet9e903e02011-10-18 21:00:24 +00002742 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002743 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002744 }
2745
2746 /* Reposition in the original skb */
2747 to = 0;
2748 while (from < skb_shinfo(skb)->nr_frags)
2749 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2750 skb_shinfo(skb)->nr_frags = to;
2751
2752 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2753
2754onlymerged:
2755 /* Most likely the tgt won't ever need its checksum anymore, skb on
2756 * the other hand might need it if it needs to be resent
2757 */
2758 tgt->ip_summed = CHECKSUM_PARTIAL;
2759 skb->ip_summed = CHECKSUM_PARTIAL;
2760
2761 /* Yak, is it really working this way? Some helper please? */
2762 skb->len -= shiftlen;
2763 skb->data_len -= shiftlen;
2764 skb->truesize -= shiftlen;
2765 tgt->len += shiftlen;
2766 tgt->data_len += shiftlen;
2767 tgt->truesize += shiftlen;
2768
2769 return shiftlen;
2770}
2771
Thomas Graf677e90e2005-06-23 20:59:51 -07002772/**
2773 * skb_prepare_seq_read - Prepare a sequential read of skb data
2774 * @skb: the buffer to read
2775 * @from: lower offset of data to be read
2776 * @to: upper offset of data to be read
2777 * @st: state variable
2778 *
2779 * Initializes the specified state variable. Must be called before
2780 * invoking skb_seq_read() for the first time.
2781 */
2782void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2783 unsigned int to, struct skb_seq_state *st)
2784{
2785 st->lower_offset = from;
2786 st->upper_offset = to;
2787 st->root_skb = st->cur_skb = skb;
2788 st->frag_idx = st->stepped_offset = 0;
2789 st->frag_data = NULL;
2790}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002791EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002792
2793/**
2794 * skb_seq_read - Sequentially read skb data
2795 * @consumed: number of bytes consumed by the caller so far
2796 * @data: destination pointer for data to be returned
2797 * @st: state variable
2798 *
Mathias Krausebc323832013-11-07 14:18:26 +01002799 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002800 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002801 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002802 * of the block or 0 if the end of the skb data or the upper
2803 * offset has been reached.
2804 *
2805 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002806 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002807 * of bytes already consumed and the next call to
2808 * skb_seq_read() will return the remaining part of the block.
2809 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002810 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002811 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002812 * reads of potentially non linear data.
2813 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002814 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002815 * at the moment, state->root_skb could be replaced with
2816 * a stack for this purpose.
2817 */
2818unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2819 struct skb_seq_state *st)
2820{
2821 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2822 skb_frag_t *frag;
2823
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002824 if (unlikely(abs_offset >= st->upper_offset)) {
2825 if (st->frag_data) {
2826 kunmap_atomic(st->frag_data);
2827 st->frag_data = NULL;
2828 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002829 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002830 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002831
2832next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002833 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002834
Thomas Chenault995b3372009-05-18 21:43:27 -07002835 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002836 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002837 return block_limit - abs_offset;
2838 }
2839
2840 if (st->frag_idx == 0 && !st->frag_data)
2841 st->stepped_offset += skb_headlen(st->cur_skb);
2842
2843 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2844 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002845 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002846
2847 if (abs_offset < block_limit) {
2848 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002849 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002850
2851 *data = (u8 *) st->frag_data + frag->page_offset +
2852 (abs_offset - st->stepped_offset);
2853
2854 return block_limit - abs_offset;
2855 }
2856
2857 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002858 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002859 st->frag_data = NULL;
2860 }
2861
2862 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002863 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002864 }
2865
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002866 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002867 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002868 st->frag_data = NULL;
2869 }
2870
David S. Miller21dc3302010-08-23 00:13:46 -07002871 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002872 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002873 st->frag_idx = 0;
2874 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002875 } else if (st->cur_skb->next) {
2876 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002877 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002878 goto next_skb;
2879 }
2880
2881 return 0;
2882}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002883EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002884
2885/**
2886 * skb_abort_seq_read - Abort a sequential read of skb data
2887 * @st: state variable
2888 *
2889 * Must be called if skb_seq_read() was not called until it
2890 * returned 0.
2891 */
2892void skb_abort_seq_read(struct skb_seq_state *st)
2893{
2894 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002895 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002896}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002897EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002898
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002899#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2900
2901static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2902 struct ts_config *conf,
2903 struct ts_state *state)
2904{
2905 return skb_seq_read(offset, text, TS_SKB_CB(state));
2906}
2907
2908static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2909{
2910 skb_abort_seq_read(TS_SKB_CB(state));
2911}
2912
2913/**
2914 * skb_find_text - Find a text pattern in skb data
2915 * @skb: the buffer to look in
2916 * @from: search offset
2917 * @to: search limit
2918 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002919 *
2920 * Finds a pattern in the skb data according to the specified
2921 * textsearch configuration. Use textsearch_next() to retrieve
2922 * subsequent occurrences of the pattern. Returns the offset
2923 * to the first occurrence or UINT_MAX if no match was found.
2924 */
2925unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002926 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002927{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002928 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002929 unsigned int ret;
2930
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002931 config->get_next_block = skb_ts_get_next_block;
2932 config->finish = skb_ts_finish;
2933
Bojan Prtvar059a2442015-02-22 11:46:35 +01002934 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002935
Bojan Prtvar059a2442015-02-22 11:46:35 +01002936 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002937 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002938}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002939EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002940
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002941/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002942 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002943 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002944 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002945 * @getfrag: call back function to be used for getting the user data
2946 * @from: pointer to user message iov
2947 * @length: length of the iov message
2948 *
2949 * Description: This procedure append the user data in the fragment part
2950 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2951 */
2952int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002953 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002954 int len, int odd, struct sk_buff *skb),
2955 void *from, int length)
2956{
Eric Dumazetb2111722012-12-28 06:06:37 +00002957 int frg_cnt = skb_shinfo(skb)->nr_frags;
2958 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002959 int offset = 0;
2960 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002961 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002962
2963 do {
2964 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002965 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002966 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002967
Eric Dumazetb2111722012-12-28 06:06:37 +00002968 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002969 return -ENOMEM;
2970
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002971 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002972 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002973
Eric Dumazetb2111722012-12-28 06:06:37 +00002974 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2975 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002976 if (ret < 0)
2977 return -EFAULT;
2978
2979 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002980 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2981 copy);
2982 frg_cnt++;
2983 pfrag->offset += copy;
2984 get_page(pfrag->page);
2985
2986 skb->truesize += copy;
2987 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002988 skb->len += copy;
2989 skb->data_len += copy;
2990 offset += copy;
2991 length -= copy;
2992
2993 } while (length > 0);
2994
2995 return 0;
2996}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002997EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002998
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02002999int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3000 int offset, size_t size)
3001{
3002 int i = skb_shinfo(skb)->nr_frags;
3003
3004 if (skb_can_coalesce(skb, i, page, offset)) {
3005 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3006 } else if (i < MAX_SKB_FRAGS) {
3007 get_page(page);
3008 skb_fill_page_desc(skb, i, page, offset, size);
3009 } else {
3010 return -EMSGSIZE;
3011 }
3012
3013 return 0;
3014}
3015EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3016
Herbert Xucbb042f2006-03-20 22:43:56 -08003017/**
3018 * skb_pull_rcsum - pull skb and update receive checksum
3019 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003020 * @len: length of data pulled
3021 *
3022 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003023 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003024 * receive path processing instead of skb_pull unless you know
3025 * that the checksum difference is zero (e.g., a valid IP header)
3026 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003027 */
3028unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3029{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003030 unsigned char *data = skb->data;
3031
Herbert Xucbb042f2006-03-20 22:43:56 -08003032 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003033 __skb_pull(skb, len);
3034 skb_postpull_rcsum(skb, data, len);
3035 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003036}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003037EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3038
Herbert Xuf4c50d92006-06-22 03:02:40 -07003039/**
3040 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003041 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003042 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003043 *
3044 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003045 * a pointer to the first in a list of new skbs for the segments.
3046 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003047 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003048struct sk_buff *skb_segment(struct sk_buff *head_skb,
3049 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003050{
3051 struct sk_buff *segs = NULL;
3052 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003053 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003054 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3055 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3056 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003057 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003058 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003059 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003060 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003061 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003062 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003063 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003064 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003065 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003066 int err = -ENOMEM;
3067 int i = 0;
3068 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003069 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003070
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003071 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003072 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003073 if (unlikely(!proto))
3074 return ERR_PTR(-EINVAL);
3075
Alexander Duyck36c98382016-05-02 09:38:18 -07003076 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003077 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003078
Steffen Klassert07b26c92016-09-19 12:58:47 +02003079 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3080 if (!(features & NETIF_F_GSO_PARTIAL)) {
3081 struct sk_buff *iter;
3082
3083 if (!list_skb ||
3084 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3085 goto normal;
3086
3087 /* Split the buffer at the frag_list pointer.
3088 * This is based on the assumption that all
3089 * buffers in the chain excluding the last
3090 * containing the same amount of data.
3091 */
3092 skb_walk_frags(head_skb, iter) {
3093 if (skb_headlen(iter))
3094 goto normal;
3095
3096 len -= iter->len;
3097 }
3098 }
3099
3100 /* GSO partial only requires that we trim off any excess that
3101 * doesn't fit into an MSS sized block, so take care of that
3102 * now.
3103 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003104 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003105 if (partial_segs > 1)
3106 mss *= partial_segs;
3107 else
3108 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003109 }
3110
Steffen Klassert07b26c92016-09-19 12:58:47 +02003111normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003112 headroom = skb_headroom(head_skb);
3113 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003114
3115 do {
3116 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003117 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003118 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003119 int size;
3120
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003121 if (unlikely(mss == GSO_BY_FRAGS)) {
3122 len = list_skb->len;
3123 } else {
3124 len = head_skb->len - offset;
3125 if (len > mss)
3126 len = mss;
3127 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003128
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003129 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003130 if (hsize < 0)
3131 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003132 if (hsize > len || !sg)
3133 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003134
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003135 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3136 (skb_headlen(list_skb) == len || sg)) {
3137 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003138
Herbert Xu9d8506c2013-11-21 11:10:04 -08003139 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003140 nfrags = skb_shinfo(list_skb)->nr_frags;
3141 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003142 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003143 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003144
3145 while (pos < offset + len) {
3146 BUG_ON(i >= nfrags);
3147
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003148 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003149 if (pos + size > offset + len)
3150 break;
3151
3152 i++;
3153 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003154 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003155 }
3156
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003157 nskb = skb_clone(list_skb, GFP_ATOMIC);
3158 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003159
3160 if (unlikely(!nskb))
3161 goto err;
3162
Herbert Xu9d8506c2013-11-21 11:10:04 -08003163 if (unlikely(pskb_trim(nskb, len))) {
3164 kfree_skb(nskb);
3165 goto err;
3166 }
3167
Alexander Duyckec47ea82012-05-04 14:26:56 +00003168 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003169 if (skb_cow_head(nskb, doffset + headroom)) {
3170 kfree_skb(nskb);
3171 goto err;
3172 }
3173
Alexander Duyckec47ea82012-05-04 14:26:56 +00003174 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003175 skb_release_head_state(nskb);
3176 __skb_push(nskb, doffset);
3177 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003178 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003179 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003180 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003181
3182 if (unlikely(!nskb))
3183 goto err;
3184
3185 skb_reserve(nskb, headroom);
3186 __skb_put(nskb, doffset);
3187 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003188
3189 if (segs)
3190 tail->next = nskb;
3191 else
3192 segs = nskb;
3193 tail = nskb;
3194
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003195 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003196
Eric Dumazet030737b2013-10-19 11:42:54 -07003197 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003198 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003199
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003200 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003201 nskb->data - tnl_hlen,
3202 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003203
Herbert Xu9d8506c2013-11-21 11:10:04 -08003204 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003205 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003206
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003207 if (!sg) {
3208 if (!nskb->remcsum_offload)
3209 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003210 SKB_GSO_CB(nskb)->csum =
3211 skb_copy_and_csum_bits(head_skb, offset,
3212 skb_put(nskb, len),
3213 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003214 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003215 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003216 continue;
3217 }
3218
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003219 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003220
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003221 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003222 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003223
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003224 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3225 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003226
Herbert Xu9d8506c2013-11-21 11:10:04 -08003227 while (pos < offset + len) {
3228 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003229 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003230
3231 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003232 nfrags = skb_shinfo(list_skb)->nr_frags;
3233 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003234 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003235
3236 BUG_ON(!nfrags);
3237
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003238 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003239 }
3240
3241 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3242 MAX_SKB_FRAGS)) {
3243 net_warn_ratelimited(
3244 "skb_segment: too many frags: %u %u\n",
3245 pos, mss);
3246 goto err;
3247 }
3248
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003249 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3250 goto err;
3251
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003252 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003253 __skb_frag_ref(nskb_frag);
3254 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003255
3256 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003257 nskb_frag->page_offset += offset - pos;
3258 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003259 }
3260
Herbert Xu89319d382008-12-15 23:26:06 -08003261 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003262
3263 if (pos + size <= offset + len) {
3264 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003265 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003266 pos += size;
3267 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003268 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003269 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003270 }
3271
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003272 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003273 }
3274
Herbert Xu89319d382008-12-15 23:26:06 -08003275skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003276 nskb->data_len = len - hsize;
3277 nskb->len += nskb->data_len;
3278 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003279
Simon Horman1cdbcb72013-05-19 15:46:49 +00003280perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003281 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003282 if (skb_has_shared_frag(nskb)) {
3283 err = __skb_linearize(nskb);
3284 if (err)
3285 goto err;
3286 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003287 if (!nskb->remcsum_offload)
3288 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003289 SKB_GSO_CB(nskb)->csum =
3290 skb_checksum(nskb, doffset,
3291 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003292 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003293 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003294 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003295 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003296
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003297 /* Some callers want to get the end of the list.
3298 * Put it in segs->prev to avoid walking the list.
3299 * (see validate_xmit_skb_list() for example)
3300 */
3301 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003302
Alexander Duyck802ab552016-04-10 21:45:03 -04003303 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003304 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003305 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003306 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003307
3308 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003309 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003310 type &= ~SKB_GSO_DODGY;
3311
3312 /* Update GSO info and prepare to start updating headers on
3313 * our way back down the stack of protocols.
3314 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003315 for (iter = segs; iter; iter = iter->next) {
3316 skb_shinfo(iter)->gso_size = gso_size;
3317 skb_shinfo(iter)->gso_segs = partial_segs;
3318 skb_shinfo(iter)->gso_type = type;
3319 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3320 }
3321
3322 if (tail->len - doffset <= gso_size)
3323 skb_shinfo(tail)->gso_size = 0;
3324 else if (tail != segs)
3325 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003326 }
3327
Toshiaki Makita432c8562014-10-27 10:30:51 -07003328 /* Following permits correct backpressure, for protocols
3329 * using skb_set_owner_w().
3330 * Idea is to tranfert ownership from head_skb to last segment.
3331 */
3332 if (head_skb->destructor == sock_wfree) {
3333 swap(tail->truesize, head_skb->truesize);
3334 swap(tail->destructor, head_skb->destructor);
3335 swap(tail->sk, head_skb->sk);
3336 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003337 return segs;
3338
3339err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003340 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003341 return ERR_PTR(err);
3342}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003343EXPORT_SYMBOL_GPL(skb_segment);
3344
Herbert Xu71d93b32008-12-15 23:42:33 -08003345int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3346{
Eric Dumazet8a291112013-10-08 09:02:23 -07003347 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003348 unsigned int offset = skb_gro_offset(skb);
3349 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003350 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003351 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003352 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003353
Eric Dumazet8a291112013-10-08 09:02:23 -07003354 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003355 return -E2BIG;
3356
Eric Dumazet29e98242014-05-16 11:34:37 -07003357 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003358 pinfo = skb_shinfo(lp);
3359
3360 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003361 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003362 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003363 int i = skbinfo->nr_frags;
3364 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003365
Herbert Xu66e92fc2009-05-26 18:50:32 +00003366 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003367 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003368
Eric Dumazet8a291112013-10-08 09:02:23 -07003369 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003370 pinfo->nr_frags = nr_frags;
3371 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003372
Herbert Xu9aaa1562009-05-26 18:50:33 +00003373 frag = pinfo->frags + nr_frags;
3374 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003375 do {
3376 *--frag = *--frag2;
3377 } while (--i);
3378
3379 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003380 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003381
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003382 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003383 delta_truesize = skb->truesize -
3384 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003385
Herbert Xuf5572062009-01-14 20:40:03 -08003386 skb->truesize -= skb->data_len;
3387 skb->len -= skb->data_len;
3388 skb->data_len = 0;
3389
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003390 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003391 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003392 } else if (skb->head_frag) {
3393 int nr_frags = pinfo->nr_frags;
3394 skb_frag_t *frag = pinfo->frags + nr_frags;
3395 struct page *page = virt_to_head_page(skb->head);
3396 unsigned int first_size = headlen - offset;
3397 unsigned int first_offset;
3398
3399 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003400 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003401
3402 first_offset = skb->data -
3403 (unsigned char *)page_address(page) +
3404 offset;
3405
3406 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3407
3408 frag->page.p = page;
3409 frag->page_offset = first_offset;
3410 skb_frag_size_set(frag, first_size);
3411
3412 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3413 /* We dont need to clear skbinfo->nr_frags here */
3414
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003415 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003416 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3417 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003418 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003419
3420merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003421 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003422 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003423 unsigned int eat = offset - headlen;
3424
3425 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003426 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003427 skb->data_len -= eat;
3428 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003429 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003430 }
3431
Herbert Xu67147ba2009-05-26 18:50:22 +00003432 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003433
Eric Dumazet29e98242014-05-16 11:34:37 -07003434 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003435 skb_shinfo(p)->frag_list = skb;
3436 else
3437 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003438 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003439 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003440 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003441
Herbert Xu5d38a072009-01-04 16:13:40 -08003442done:
3443 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003444 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003445 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003446 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003447 if (lp != p) {
3448 lp->data_len += len;
3449 lp->truesize += delta_truesize;
3450 lp->len += len;
3451 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003452 NAPI_GRO_CB(skb)->same_flow = 1;
3453 return 0;
3454}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003455EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457void __init skb_init(void)
3458{
3459 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3460 sizeof(struct sk_buff),
3461 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003462 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003463 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003464 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003465 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003466 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003467 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003468 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
David Howells716ea3a2007-04-02 20:19:53 -07003471/**
3472 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3473 * @skb: Socket buffer containing the buffers to be mapped
3474 * @sg: The scatter-gather list to map into
3475 * @offset: The offset into the buffer's contents to start mapping
3476 * @len: Length of buffer space to be mapped
3477 *
3478 * Fill the specified scatter-gather list with mappings/pointers into a
3479 * region of the buffer space attached to a socket buffer.
3480 */
David S. Miller51c739d2007-10-30 21:29:29 -07003481static int
3482__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003483{
David S. Miller1a028e52007-04-27 15:21:23 -07003484 int start = skb_headlen(skb);
3485 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003486 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003487 int elt = 0;
3488
3489 if (copy > 0) {
3490 if (copy > len)
3491 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003492 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003493 elt++;
3494 if ((len -= copy) == 0)
3495 return elt;
3496 offset += copy;
3497 }
3498
3499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003500 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003501
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003502 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003503
Eric Dumazet9e903e02011-10-18 21:00:24 +00003504 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003505 if ((copy = end - offset) > 0) {
3506 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3507
3508 if (copy > len)
3509 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003510 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003511 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003512 elt++;
3513 if (!(len -= copy))
3514 return elt;
3515 offset += copy;
3516 }
David S. Miller1a028e52007-04-27 15:21:23 -07003517 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003518 }
3519
David S. Millerfbb398a2009-06-09 00:18:59 -07003520 skb_walk_frags(skb, frag_iter) {
3521 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003522
David S. Millerfbb398a2009-06-09 00:18:59 -07003523 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003524
David S. Millerfbb398a2009-06-09 00:18:59 -07003525 end = start + frag_iter->len;
3526 if ((copy = end - offset) > 0) {
3527 if (copy > len)
3528 copy = len;
3529 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3530 copy);
3531 if ((len -= copy) == 0)
3532 return elt;
3533 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003534 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003535 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003536 }
3537 BUG_ON(len);
3538 return elt;
3539}
3540
Fan Du25a91d82014-01-18 09:54:23 +08003541/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3542 * sglist without mark the sg which contain last skb data as the end.
3543 * So the caller can mannipulate sg list as will when padding new data after
3544 * the first call without calling sg_unmark_end to expend sg list.
3545 *
3546 * Scenario to use skb_to_sgvec_nomark:
3547 * 1. sg_init_table
3548 * 2. skb_to_sgvec_nomark(payload1)
3549 * 3. skb_to_sgvec_nomark(payload2)
3550 *
3551 * This is equivalent to:
3552 * 1. sg_init_table
3553 * 2. skb_to_sgvec(payload1)
3554 * 3. sg_unmark_end
3555 * 4. skb_to_sgvec(payload2)
3556 *
3557 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3558 * is more preferable.
3559 */
3560int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3561 int offset, int len)
3562{
3563 return __skb_to_sgvec(skb, sg, offset, len);
3564}
3565EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3566
David S. Miller51c739d2007-10-30 21:29:29 -07003567int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3568{
3569 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3570
Jens Axboec46f2332007-10-31 12:06:37 +01003571 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003572
3573 return nsg;
3574}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003575EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003576
David Howells716ea3a2007-04-02 20:19:53 -07003577/**
3578 * skb_cow_data - Check that a socket buffer's data buffers are writable
3579 * @skb: The socket buffer to check.
3580 * @tailbits: Amount of trailing space to be added
3581 * @trailer: Returned pointer to the skb where the @tailbits space begins
3582 *
3583 * Make sure that the data buffers attached to a socket buffer are
3584 * writable. If they are not, private copies are made of the data buffers
3585 * and the socket buffer is set to use these instead.
3586 *
3587 * If @tailbits is given, make sure that there is space to write @tailbits
3588 * bytes of data beyond current end of socket buffer. @trailer will be
3589 * set to point to the skb in which this space begins.
3590 *
3591 * The number of scatterlist elements required to completely map the
3592 * COW'd and extended socket buffer will be returned.
3593 */
3594int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3595{
3596 int copyflag;
3597 int elt;
3598 struct sk_buff *skb1, **skb_p;
3599
3600 /* If skb is cloned or its head is paged, reallocate
3601 * head pulling out all the pages (pages are considered not writable
3602 * at the moment even if they are anonymous).
3603 */
3604 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3605 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3606 return -ENOMEM;
3607
3608 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003609 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003610 /* A little of trouble, not enough of space for trailer.
3611 * This should not happen, when stack is tuned to generate
3612 * good frames. OK, on miss we reallocate and reserve even more
3613 * space, 128 bytes is fair. */
3614
3615 if (skb_tailroom(skb) < tailbits &&
3616 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3617 return -ENOMEM;
3618
3619 /* Voila! */
3620 *trailer = skb;
3621 return 1;
3622 }
3623
3624 /* Misery. We are in troubles, going to mincer fragments... */
3625
3626 elt = 1;
3627 skb_p = &skb_shinfo(skb)->frag_list;
3628 copyflag = 0;
3629
3630 while ((skb1 = *skb_p) != NULL) {
3631 int ntail = 0;
3632
3633 /* The fragment is partially pulled by someone,
3634 * this can happen on input. Copy it and everything
3635 * after it. */
3636
3637 if (skb_shared(skb1))
3638 copyflag = 1;
3639
3640 /* If the skb is the last, worry about trailer. */
3641
3642 if (skb1->next == NULL && tailbits) {
3643 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003644 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003645 skb_tailroom(skb1) < tailbits)
3646 ntail = tailbits + 128;
3647 }
3648
3649 if (copyflag ||
3650 skb_cloned(skb1) ||
3651 ntail ||
3652 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003653 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003654 struct sk_buff *skb2;
3655
3656 /* Fuck, we are miserable poor guys... */
3657 if (ntail == 0)
3658 skb2 = skb_copy(skb1, GFP_ATOMIC);
3659 else
3660 skb2 = skb_copy_expand(skb1,
3661 skb_headroom(skb1),
3662 ntail,
3663 GFP_ATOMIC);
3664 if (unlikely(skb2 == NULL))
3665 return -ENOMEM;
3666
3667 if (skb1->sk)
3668 skb_set_owner_w(skb2, skb1->sk);
3669
3670 /* Looking around. Are we still alive?
3671 * OK, link new skb, drop old one */
3672
3673 skb2->next = skb1->next;
3674 *skb_p = skb2;
3675 kfree_skb(skb1);
3676 skb1 = skb2;
3677 }
3678 elt++;
3679 *trailer = skb1;
3680 skb_p = &skb1->next;
3681 }
3682
3683 return elt;
3684}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003685EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003686
Eric Dumazetb1faf562010-05-31 23:44:05 -07003687static void sock_rmem_free(struct sk_buff *skb)
3688{
3689 struct sock *sk = skb->sk;
3690
3691 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3692}
3693
3694/*
3695 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3696 */
3697int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3698{
3699 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003700 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003701 return -ENOMEM;
3702
3703 skb_orphan(skb);
3704 skb->sk = sk;
3705 skb->destructor = sock_rmem_free;
3706 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3707
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003708 /* before exiting rcu section, make sure dst is refcounted */
3709 skb_dst_force(skb);
3710
Eric Dumazetb1faf562010-05-31 23:44:05 -07003711 skb_queue_tail(&sk->sk_error_queue, skb);
3712 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003713 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003714 return 0;
3715}
3716EXPORT_SYMBOL(sock_queue_err_skb);
3717
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003718struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3719{
3720 struct sk_buff_head *q = &sk->sk_error_queue;
3721 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003722 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003723 int err = 0;
3724
Eric Dumazet997d5c32015-02-18 05:47:55 -08003725 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003726 skb = __skb_dequeue(q);
3727 if (skb && (skb_next = skb_peek(q)))
3728 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003729 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003730
3731 sk->sk_err = err;
3732 if (err)
3733 sk->sk_error_report(sk);
3734
3735 return skb;
3736}
3737EXPORT_SYMBOL(sock_dequeue_err_skb);
3738
Alexander Duyckcab41c42014-09-10 18:05:26 -04003739/**
3740 * skb_clone_sk - create clone of skb, and take reference to socket
3741 * @skb: the skb to clone
3742 *
3743 * This function creates a clone of a buffer that holds a reference on
3744 * sk_refcnt. Buffers created via this function are meant to be
3745 * returned using sock_queue_err_skb, or free via kfree_skb.
3746 *
3747 * When passing buffers allocated with this function to sock_queue_err_skb
3748 * it is necessary to wrap the call with sock_hold/sock_put in order to
3749 * prevent the socket from being released prior to being enqueued on
3750 * the sk_error_queue.
3751 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003752struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3753{
3754 struct sock *sk = skb->sk;
3755 struct sk_buff *clone;
3756
3757 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3758 return NULL;
3759
3760 clone = skb_clone(skb, GFP_ATOMIC);
3761 if (!clone) {
3762 sock_put(sk);
3763 return NULL;
3764 }
3765
3766 clone->sk = sk;
3767 clone->destructor = sock_efree;
3768
3769 return clone;
3770}
3771EXPORT_SYMBOL(skb_clone_sk);
3772
Alexander Duyck37846ef2014-09-04 13:31:10 -04003773static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3774 struct sock *sk,
3775 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003776{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003777 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003778 int err;
3779
Patrick Ohlyac45f602009-02-12 05:03:37 +00003780 serr = SKB_EXT_ERR(skb);
3781 memset(serr, 0, sizeof(*serr));
3782 serr->ee.ee_errno = ENOMSG;
3783 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003784 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003785 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003786 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003787 if (sk->sk_protocol == IPPROTO_TCP &&
3788 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003789 serr->ee.ee_data -= sk->sk_tskey;
3790 }
Eric Dumazet29030372010-05-29 00:20:48 -07003791
Patrick Ohlyac45f602009-02-12 05:03:37 +00003792 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003793
Patrick Ohlyac45f602009-02-12 05:03:37 +00003794 if (err)
3795 kfree_skb(skb);
3796}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003797
Willem de Bruijnb245be12015-01-30 13:29:32 -05003798static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3799{
3800 bool ret;
3801
3802 if (likely(sysctl_tstamp_allow_data || tsonly))
3803 return true;
3804
3805 read_lock_bh(&sk->sk_callback_lock);
3806 ret = sk->sk_socket && sk->sk_socket->file &&
3807 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3808 read_unlock_bh(&sk->sk_callback_lock);
3809 return ret;
3810}
3811
Alexander Duyck37846ef2014-09-04 13:31:10 -04003812void skb_complete_tx_timestamp(struct sk_buff *skb,
3813 struct skb_shared_hwtstamps *hwtstamps)
3814{
3815 struct sock *sk = skb->sk;
3816
Willem de Bruijnb245be12015-01-30 13:29:32 -05003817 if (!skb_may_tx_timestamp(sk, false))
3818 return;
3819
Eric Dumazetf157cc12017-03-03 21:01:03 -08003820 /* Take a reference to prevent skb_orphan() from freeing the socket,
3821 * but only if the socket refcount is not zero.
3822 */
3823 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3824 *skb_hwtstamps(skb) = *hwtstamps;
3825 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3826 sock_put(sk);
3827 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003828}
3829EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3830
3831void __skb_tstamp_tx(struct sk_buff *orig_skb,
3832 struct skb_shared_hwtstamps *hwtstamps,
3833 struct sock *sk, int tstype)
3834{
3835 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003836 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003837
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003838 if (!sk)
3839 return;
3840
3841 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3842 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003843 return;
3844
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003845 if (tsonly)
3846 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003847 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003848 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003849 if (!skb)
3850 return;
3851
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003852 if (tsonly) {
3853 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3854 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3855 }
3856
3857 if (hwtstamps)
3858 *skb_hwtstamps(skb) = *hwtstamps;
3859 else
3860 skb->tstamp = ktime_get_real();
3861
Alexander Duyck37846ef2014-09-04 13:31:10 -04003862 __skb_complete_tx_timestamp(skb, sk, tstype);
3863}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003864EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3865
3866void skb_tstamp_tx(struct sk_buff *orig_skb,
3867 struct skb_shared_hwtstamps *hwtstamps)
3868{
3869 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3870 SCM_TSTAMP_SND);
3871}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003872EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3873
Johannes Berg6e3e9392011-11-09 10:15:42 +01003874void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3875{
3876 struct sock *sk = skb->sk;
3877 struct sock_exterr_skb *serr;
Eric Dumazet98fa3d22017-03-03 21:01:02 -08003878 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01003879
3880 skb->wifi_acked_valid = 1;
3881 skb->wifi_acked = acked;
3882
3883 serr = SKB_EXT_ERR(skb);
3884 memset(serr, 0, sizeof(*serr));
3885 serr->ee.ee_errno = ENOMSG;
3886 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3887
Eric Dumazet98fa3d22017-03-03 21:01:02 -08003888 /* Take a reference to prevent skb_orphan() from freeing the socket,
3889 * but only if the socket refcount is not zero.
3890 */
3891 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3892 err = sock_queue_err_skb(sk, skb);
3893 sock_put(sk);
3894 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01003895 if (err)
3896 kfree_skb(skb);
3897}
3898EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3899
Rusty Russellf35d9d82008-02-04 23:49:54 -05003900/**
3901 * skb_partial_csum_set - set up and verify partial csum values for packet
3902 * @skb: the skb to set
3903 * @start: the number of bytes after skb->data to start checksumming.
3904 * @off: the offset from start to place the checksum.
3905 *
3906 * For untrusted partially-checksummed packets, we need to make sure the values
3907 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3908 *
3909 * This function checks and sets those values and skb->ip_summed: if this
3910 * returns false you should drop the packet.
3911 */
3912bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3913{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003914 if (unlikely(start > skb_headlen(skb)) ||
3915 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003916 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3917 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003918 return false;
3919 }
3920 skb->ip_summed = CHECKSUM_PARTIAL;
3921 skb->csum_start = skb_headroom(skb) + start;
3922 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003923 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003924 return true;
3925}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003926EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003927
Paul Durranted1f50c2014-01-09 10:02:46 +00003928static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3929 unsigned int max)
3930{
3931 if (skb_headlen(skb) >= len)
3932 return 0;
3933
3934 /* If we need to pullup then pullup to the max, so we
3935 * won't need to do it again.
3936 */
3937 if (max > skb->len)
3938 max = skb->len;
3939
3940 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3941 return -ENOMEM;
3942
3943 if (skb_headlen(skb) < len)
3944 return -EPROTO;
3945
3946 return 0;
3947}
3948
Jan Beulichf9708b42014-03-11 13:56:05 +00003949#define MAX_TCP_HDR_LEN (15 * 4)
3950
3951static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3952 typeof(IPPROTO_IP) proto,
3953 unsigned int off)
3954{
3955 switch (proto) {
3956 int err;
3957
3958 case IPPROTO_TCP:
3959 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3960 off + MAX_TCP_HDR_LEN);
3961 if (!err && !skb_partial_csum_set(skb, off,
3962 offsetof(struct tcphdr,
3963 check)))
3964 err = -EPROTO;
3965 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3966
3967 case IPPROTO_UDP:
3968 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3969 off + sizeof(struct udphdr));
3970 if (!err && !skb_partial_csum_set(skb, off,
3971 offsetof(struct udphdr,
3972 check)))
3973 err = -EPROTO;
3974 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3975 }
3976
3977 return ERR_PTR(-EPROTO);
3978}
3979
Paul Durranted1f50c2014-01-09 10:02:46 +00003980/* This value should be large enough to cover a tagged ethernet header plus
3981 * maximally sized IP and TCP or UDP headers.
3982 */
3983#define MAX_IP_HDR_LEN 128
3984
Jan Beulichf9708b42014-03-11 13:56:05 +00003985static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003986{
3987 unsigned int off;
3988 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003989 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003990 int err;
3991
3992 fragment = false;
3993
3994 err = skb_maybe_pull_tail(skb,
3995 sizeof(struct iphdr),
3996 MAX_IP_HDR_LEN);
3997 if (err < 0)
3998 goto out;
3999
4000 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4001 fragment = true;
4002
4003 off = ip_hdrlen(skb);
4004
4005 err = -EPROTO;
4006
4007 if (fragment)
4008 goto out;
4009
Jan Beulichf9708b42014-03-11 13:56:05 +00004010 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4011 if (IS_ERR(csum))
4012 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004013
Jan Beulichf9708b42014-03-11 13:56:05 +00004014 if (recalculate)
4015 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4016 ip_hdr(skb)->daddr,
4017 skb->len - off,
4018 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004019 err = 0;
4020
4021out:
4022 return err;
4023}
4024
4025/* This value should be large enough to cover a tagged ethernet header plus
4026 * an IPv6 header, all options, and a maximal TCP or UDP header.
4027 */
4028#define MAX_IPV6_HDR_LEN 256
4029
4030#define OPT_HDR(type, skb, off) \
4031 (type *)(skb_network_header(skb) + (off))
4032
4033static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4034{
4035 int err;
4036 u8 nexthdr;
4037 unsigned int off;
4038 unsigned int len;
4039 bool fragment;
4040 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004041 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004042
4043 fragment = false;
4044 done = false;
4045
4046 off = sizeof(struct ipv6hdr);
4047
4048 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4049 if (err < 0)
4050 goto out;
4051
4052 nexthdr = ipv6_hdr(skb)->nexthdr;
4053
4054 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4055 while (off <= len && !done) {
4056 switch (nexthdr) {
4057 case IPPROTO_DSTOPTS:
4058 case IPPROTO_HOPOPTS:
4059 case IPPROTO_ROUTING: {
4060 struct ipv6_opt_hdr *hp;
4061
4062 err = skb_maybe_pull_tail(skb,
4063 off +
4064 sizeof(struct ipv6_opt_hdr),
4065 MAX_IPV6_HDR_LEN);
4066 if (err < 0)
4067 goto out;
4068
4069 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4070 nexthdr = hp->nexthdr;
4071 off += ipv6_optlen(hp);
4072 break;
4073 }
4074 case IPPROTO_AH: {
4075 struct ip_auth_hdr *hp;
4076
4077 err = skb_maybe_pull_tail(skb,
4078 off +
4079 sizeof(struct ip_auth_hdr),
4080 MAX_IPV6_HDR_LEN);
4081 if (err < 0)
4082 goto out;
4083
4084 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4085 nexthdr = hp->nexthdr;
4086 off += ipv6_authlen(hp);
4087 break;
4088 }
4089 case IPPROTO_FRAGMENT: {
4090 struct frag_hdr *hp;
4091
4092 err = skb_maybe_pull_tail(skb,
4093 off +
4094 sizeof(struct frag_hdr),
4095 MAX_IPV6_HDR_LEN);
4096 if (err < 0)
4097 goto out;
4098
4099 hp = OPT_HDR(struct frag_hdr, skb, off);
4100
4101 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4102 fragment = true;
4103
4104 nexthdr = hp->nexthdr;
4105 off += sizeof(struct frag_hdr);
4106 break;
4107 }
4108 default:
4109 done = true;
4110 break;
4111 }
4112 }
4113
4114 err = -EPROTO;
4115
4116 if (!done || fragment)
4117 goto out;
4118
Jan Beulichf9708b42014-03-11 13:56:05 +00004119 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4120 if (IS_ERR(csum))
4121 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004122
Jan Beulichf9708b42014-03-11 13:56:05 +00004123 if (recalculate)
4124 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4125 &ipv6_hdr(skb)->daddr,
4126 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004127 err = 0;
4128
4129out:
4130 return err;
4131}
4132
4133/**
4134 * skb_checksum_setup - set up partial checksum offset
4135 * @skb: the skb to set up
4136 * @recalculate: if true the pseudo-header checksum will be recalculated
4137 */
4138int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4139{
4140 int err;
4141
4142 switch (skb->protocol) {
4143 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004144 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004145 break;
4146
4147 case htons(ETH_P_IPV6):
4148 err = skb_checksum_setup_ipv6(skb, recalculate);
4149 break;
4150
4151 default:
4152 err = -EPROTO;
4153 break;
4154 }
4155
4156 return err;
4157}
4158EXPORT_SYMBOL(skb_checksum_setup);
4159
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004160/**
4161 * skb_checksum_maybe_trim - maybe trims the given skb
4162 * @skb: the skb to check
4163 * @transport_len: the data length beyond the network header
4164 *
4165 * Checks whether the given skb has data beyond the given transport length.
4166 * If so, returns a cloned skb trimmed to this transport length.
4167 * Otherwise returns the provided skb. Returns NULL in error cases
4168 * (e.g. transport_len exceeds skb length or out-of-memory).
4169 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004170 * Caller needs to set the skb transport header and free any returned skb if it
4171 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004172 */
4173static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4174 unsigned int transport_len)
4175{
4176 struct sk_buff *skb_chk;
4177 unsigned int len = skb_transport_offset(skb) + transport_len;
4178 int ret;
4179
Linus Lüssinga5169932015-08-13 05:54:07 +02004180 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004181 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004182 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004183 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004184
4185 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004186 if (!skb_chk)
4187 return NULL;
4188
4189 ret = pskb_trim_rcsum(skb_chk, len);
4190 if (ret) {
4191 kfree_skb(skb_chk);
4192 return NULL;
4193 }
4194
4195 return skb_chk;
4196}
4197
4198/**
4199 * skb_checksum_trimmed - validate checksum of an skb
4200 * @skb: the skb to check
4201 * @transport_len: the data length beyond the network header
4202 * @skb_chkf: checksum function to use
4203 *
4204 * Applies the given checksum function skb_chkf to the provided skb.
4205 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4206 *
4207 * If the skb has data beyond the given transport length, then a
4208 * trimmed & cloned skb is checked and returned.
4209 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004210 * Caller needs to set the skb transport header and free any returned skb if it
4211 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004212 */
4213struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4214 unsigned int transport_len,
4215 __sum16(*skb_chkf)(struct sk_buff *skb))
4216{
4217 struct sk_buff *skb_chk;
4218 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004219 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004220
4221 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4222 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004223 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004224
Linus Lüssinga5169932015-08-13 05:54:07 +02004225 if (!pskb_may_pull(skb_chk, offset))
4226 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004227
Linus Lüssing9b368812016-02-24 04:21:42 +01004228 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004229 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004230 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004231
Linus Lüssinga5169932015-08-13 05:54:07 +02004232 if (ret)
4233 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004234
4235 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004236
4237err:
4238 if (skb_chk && skb_chk != skb)
4239 kfree_skb(skb_chk);
4240
4241 return NULL;
4242
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004243}
4244EXPORT_SYMBOL(skb_checksum_trimmed);
4245
Ben Hutchings4497b072008-06-19 16:22:28 -07004246void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4247{
Joe Perchese87cc472012-05-13 21:56:26 +00004248 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4249 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004250}
Ben Hutchings4497b072008-06-19 16:22:28 -07004251EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004252
4253void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4254{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004255 if (head_stolen) {
4256 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004257 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004258 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004259 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004260 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004261}
4262EXPORT_SYMBOL(kfree_skb_partial);
4263
4264/**
4265 * skb_try_coalesce - try to merge skb to prior one
4266 * @to: prior buffer
4267 * @from: buffer to add
4268 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004269 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004270 */
4271bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4272 bool *fragstolen, int *delta_truesize)
4273{
4274 int i, delta, len = from->len;
4275
4276 *fragstolen = false;
4277
4278 if (skb_cloned(to))
4279 return false;
4280
4281 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004282 if (len)
4283 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004284 *delta_truesize = 0;
4285 return true;
4286 }
4287
4288 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4289 return false;
4290
4291 if (skb_headlen(from) != 0) {
4292 struct page *page;
4293 unsigned int offset;
4294
4295 if (skb_shinfo(to)->nr_frags +
4296 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4297 return false;
4298
4299 if (skb_head_is_locked(from))
4300 return false;
4301
4302 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4303
4304 page = virt_to_head_page(from->head);
4305 offset = from->data - (unsigned char *)page_address(page);
4306
4307 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4308 page, offset, skb_headlen(from));
4309 *fragstolen = true;
4310 } else {
4311 if (skb_shinfo(to)->nr_frags +
4312 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4313 return false;
4314
Weiping Panf4b549a2012-09-28 20:15:30 +00004315 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004316 }
4317
4318 WARN_ON_ONCE(delta < len);
4319
4320 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4321 skb_shinfo(from)->frags,
4322 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4323 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4324
4325 if (!skb_cloned(from))
4326 skb_shinfo(from)->nr_frags = 0;
4327
Li RongQing8ea853f2012-09-18 16:53:21 +00004328 /* if the skb is not cloned this does nothing
4329 * since we set nr_frags to 0.
4330 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004331 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4332 skb_frag_ref(from, i);
4333
4334 to->truesize += delta;
4335 to->len += len;
4336 to->data_len += len;
4337
4338 *delta_truesize = delta;
4339 return true;
4340}
4341EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004342
4343/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004344 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004345 *
4346 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004347 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004348 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004349 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4350 * into/from a tunnel. Some information have to be cleared during these
4351 * operations.
4352 * skb_scrub_packet can also be used to clean a skb before injecting it in
4353 * another namespace (@xnet == true). We have to clear all information in the
4354 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004355 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004356void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004357{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004358 skb->tstamp.tv64 = 0;
4359 skb->pkt_type = PACKET_HOST;
4360 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004361 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004362 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004363 secpath_reset(skb);
4364 nf_reset(skb);
4365 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004366
4367 if (!xnet)
4368 return;
4369
4370 skb_orphan(skb);
4371 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004372}
4373EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004374
4375/**
4376 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4377 *
4378 * @skb: GSO skb
4379 *
4380 * skb_gso_transport_seglen is used to determine the real size of the
4381 * individual segments, including Layer4 headers (TCP/UDP).
4382 *
4383 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4384 */
4385unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4386{
4387 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004388 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004389
Florian Westphalf993bc22014-10-20 13:49:18 +02004390 if (skb->encapsulation) {
4391 thlen = skb_inner_transport_header(skb) -
4392 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004393
Florian Westphalf993bc22014-10-20 13:49:18 +02004394 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4395 thlen += inner_tcp_hdrlen(skb);
4396 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4397 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004398 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4399 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004400 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004401 /* UFO sets gso_size to the size of the fragmentation
4402 * payload, i.e. the size of the L4 (UDP) header is already
4403 * accounted for.
4404 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004405 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004406}
4407EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004408
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004409/**
4410 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4411 *
4412 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004413 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004414 *
4415 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4416 * once split.
4417 */
4418bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4419{
4420 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4421 const struct sk_buff *iter;
4422 unsigned int hlen;
4423
4424 hlen = skb_gso_network_seglen(skb);
4425
4426 if (shinfo->gso_size != GSO_BY_FRAGS)
4427 return hlen <= mtu;
4428
4429 /* Undo this so we can re-use header sizes */
4430 hlen -= GSO_BY_FRAGS;
4431
4432 skb_walk_frags(skb, iter) {
4433 if (hlen + skb_headlen(iter) > mtu)
4434 return false;
4435 }
4436
4437 return true;
4438}
4439EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4440
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004441static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4442{
4443 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4444 kfree_skb(skb);
4445 return NULL;
4446 }
4447
Vlad Yasevichf6548612015-12-14 17:44:10 -05004448 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004449 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004450 skb->mac_header += VLAN_HLEN;
4451 return skb;
4452}
4453
4454struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4455{
4456 struct vlan_hdr *vhdr;
4457 u16 vlan_tci;
4458
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004459 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004460 /* vlan_tci is already set-up so leave this for another time */
4461 return skb;
4462 }
4463
4464 skb = skb_share_check(skb, GFP_ATOMIC);
4465 if (unlikely(!skb))
4466 goto err_free;
4467
4468 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4469 goto err_free;
4470
4471 vhdr = (struct vlan_hdr *)skb->data;
4472 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4473 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4474
4475 skb_pull_rcsum(skb, VLAN_HLEN);
4476 vlan_set_encap_proto(skb, vhdr);
4477
4478 skb = skb_reorder_vlan_header(skb);
4479 if (unlikely(!skb))
4480 goto err_free;
4481
4482 skb_reset_network_header(skb);
4483 skb_reset_transport_header(skb);
4484 skb_reset_mac_len(skb);
4485
4486 return skb;
4487
4488err_free:
4489 kfree_skb(skb);
4490 return NULL;
4491}
4492EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004493
Jiri Pirkoe2195122014-11-19 14:05:01 +01004494int skb_ensure_writable(struct sk_buff *skb, int write_len)
4495{
4496 if (!pskb_may_pull(skb, write_len))
4497 return -ENOMEM;
4498
4499 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4500 return 0;
4501
4502 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4503}
4504EXPORT_SYMBOL(skb_ensure_writable);
4505
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004506/* remove VLAN header from packet and update csum accordingly.
4507 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4508 */
4509int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004510{
4511 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004512 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004513 int err;
4514
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004515 if (WARN_ONCE(offset,
4516 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4517 offset)) {
4518 return -EINVAL;
4519 }
4520
Jiri Pirko93515d52014-11-19 14:05:02 +01004521 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4522 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004523 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004524
4525 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4526
4527 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4528 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4529
4530 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4531 __skb_pull(skb, VLAN_HLEN);
4532
4533 vlan_set_encap_proto(skb, vhdr);
4534 skb->mac_header += VLAN_HLEN;
4535
4536 if (skb_network_offset(skb) < ETH_HLEN)
4537 skb_set_network_header(skb, ETH_HLEN);
4538
4539 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004540
4541 return err;
4542}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004543EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004544
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004545/* Pop a vlan tag either from hwaccel or from payload.
4546 * Expects skb->data at mac header.
4547 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004548int skb_vlan_pop(struct sk_buff *skb)
4549{
4550 u16 vlan_tci;
4551 __be16 vlan_proto;
4552 int err;
4553
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004554 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004555 skb->vlan_tci = 0;
4556 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004557 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004558 return 0;
4559
4560 err = __skb_vlan_pop(skb, &vlan_tci);
4561 if (err)
4562 return err;
4563 }
4564 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004565 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004566 return 0;
4567
4568 vlan_proto = skb->protocol;
4569 err = __skb_vlan_pop(skb, &vlan_tci);
4570 if (unlikely(err))
4571 return err;
4572
4573 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4574 return 0;
4575}
4576EXPORT_SYMBOL(skb_vlan_pop);
4577
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004578/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4579 * Expects skb->data at mac header.
4580 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004581int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4582{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004583 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004584 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004585 int err;
4586
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004587 if (WARN_ONCE(offset,
4588 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4589 offset)) {
4590 return -EINVAL;
4591 }
4592
Jiri Pirko93515d52014-11-19 14:05:02 +01004593 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004594 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004595 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004596 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004597
Jiri Pirko93515d52014-11-19 14:05:02 +01004598 skb->protocol = skb->vlan_proto;
4599 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004600
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004601 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004602 }
4603 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4604 return 0;
4605}
4606EXPORT_SYMBOL(skb_vlan_push);
4607
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004608/**
4609 * alloc_skb_with_frags - allocate skb with page frags
4610 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004611 * @header_len: size of linear part
4612 * @data_len: needed length in frags
4613 * @max_page_order: max page order desired.
4614 * @errcode: pointer to error code if any
4615 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004616 *
4617 * This can be used to allocate a paged skb, given a maximal order for frags.
4618 */
4619struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4620 unsigned long data_len,
4621 int max_page_order,
4622 int *errcode,
4623 gfp_t gfp_mask)
4624{
4625 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4626 unsigned long chunk;
4627 struct sk_buff *skb;
4628 struct page *page;
4629 gfp_t gfp_head;
4630 int i;
4631
4632 *errcode = -EMSGSIZE;
4633 /* Note this test could be relaxed, if we succeed to allocate
4634 * high order pages...
4635 */
4636 if (npages > MAX_SKB_FRAGS)
4637 return NULL;
4638
4639 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004640 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004641 gfp_head |= __GFP_REPEAT;
4642
4643 *errcode = -ENOBUFS;
4644 skb = alloc_skb(header_len, gfp_head);
4645 if (!skb)
4646 return NULL;
4647
4648 skb->truesize += npages << PAGE_SHIFT;
4649
4650 for (i = 0; npages > 0; i++) {
4651 int order = max_page_order;
4652
4653 while (order) {
4654 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004655 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004656 __GFP_COMP |
4657 __GFP_NOWARN |
4658 __GFP_NORETRY,
4659 order);
4660 if (page)
4661 goto fill_page;
4662 /* Do not retry other high order allocations */
4663 order = 1;
4664 max_page_order = 0;
4665 }
4666 order--;
4667 }
4668 page = alloc_page(gfp_mask);
4669 if (!page)
4670 goto failure;
4671fill_page:
4672 chunk = min_t(unsigned long, data_len,
4673 PAGE_SIZE << order);
4674 skb_fill_page_desc(skb, i, page, 0, chunk);
4675 data_len -= chunk;
4676 npages -= 1 << order;
4677 }
4678 return skb;
4679
4680failure:
4681 kfree_skb(skb);
4682 return NULL;
4683}
4684EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004685
4686/* carve out the first off bytes from skb when off < headlen */
4687static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4688 const int headlen, gfp_t gfp_mask)
4689{
4690 int i;
4691 int size = skb_end_offset(skb);
4692 int new_hlen = headlen - off;
4693 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004694
4695 size = SKB_DATA_ALIGN(size);
4696
4697 if (skb_pfmemalloc(skb))
4698 gfp_mask |= __GFP_MEMALLOC;
4699 data = kmalloc_reserve(size +
4700 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4701 gfp_mask, NUMA_NO_NODE, NULL);
4702 if (!data)
4703 return -ENOMEM;
4704
4705 size = SKB_WITH_OVERHEAD(ksize(data));
4706
4707 /* Copy real data, and all frags */
4708 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4709 skb->len -= off;
4710
4711 memcpy((struct skb_shared_info *)(data + size),
4712 skb_shinfo(skb),
4713 offsetof(struct skb_shared_info,
4714 frags[skb_shinfo(skb)->nr_frags]));
4715 if (skb_cloned(skb)) {
4716 /* drop the old head gracefully */
4717 if (skb_orphan_frags(skb, gfp_mask)) {
4718 kfree(data);
4719 return -ENOMEM;
4720 }
4721 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4722 skb_frag_ref(skb, i);
4723 if (skb_has_frag_list(skb))
4724 skb_clone_fraglist(skb);
4725 skb_release_data(skb);
4726 } else {
4727 /* we can reuse existing recount- all we did was
4728 * relocate values
4729 */
4730 skb_free_head(skb);
4731 }
4732
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004733 skb->head = data;
4734 skb->data = data;
4735 skb->head_frag = 0;
4736#ifdef NET_SKBUFF_DATA_USES_OFFSET
4737 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004738#else
4739 skb->end = skb->head + size;
4740#endif
4741 skb_set_tail_pointer(skb, skb_headlen(skb));
4742 skb_headers_offset_update(skb, 0);
4743 skb->cloned = 0;
4744 skb->hdr_len = 0;
4745 skb->nohdr = 0;
4746 atomic_set(&skb_shinfo(skb)->dataref, 1);
4747
4748 return 0;
4749}
4750
4751static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4752
4753/* carve out the first eat bytes from skb's frag_list. May recurse into
4754 * pskb_carve()
4755 */
4756static int pskb_carve_frag_list(struct sk_buff *skb,
4757 struct skb_shared_info *shinfo, int eat,
4758 gfp_t gfp_mask)
4759{
4760 struct sk_buff *list = shinfo->frag_list;
4761 struct sk_buff *clone = NULL;
4762 struct sk_buff *insp = NULL;
4763
4764 do {
4765 if (!list) {
4766 pr_err("Not enough bytes to eat. Want %d\n", eat);
4767 return -EFAULT;
4768 }
4769 if (list->len <= eat) {
4770 /* Eaten as whole. */
4771 eat -= list->len;
4772 list = list->next;
4773 insp = list;
4774 } else {
4775 /* Eaten partially. */
4776 if (skb_shared(list)) {
4777 clone = skb_clone(list, gfp_mask);
4778 if (!clone)
4779 return -ENOMEM;
4780 insp = list->next;
4781 list = clone;
4782 } else {
4783 /* This may be pulled without problems. */
4784 insp = list;
4785 }
4786 if (pskb_carve(list, eat, gfp_mask) < 0) {
4787 kfree_skb(clone);
4788 return -ENOMEM;
4789 }
4790 break;
4791 }
4792 } while (eat);
4793
4794 /* Free pulled out fragments. */
4795 while ((list = shinfo->frag_list) != insp) {
4796 shinfo->frag_list = list->next;
4797 kfree_skb(list);
4798 }
4799 /* And insert new clone at head. */
4800 if (clone) {
4801 clone->next = list;
4802 shinfo->frag_list = clone;
4803 }
4804 return 0;
4805}
4806
4807/* carve off first len bytes from skb. Split line (off) is in the
4808 * non-linear part of skb
4809 */
4810static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4811 int pos, gfp_t gfp_mask)
4812{
4813 int i, k = 0;
4814 int size = skb_end_offset(skb);
4815 u8 *data;
4816 const int nfrags = skb_shinfo(skb)->nr_frags;
4817 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004818
4819 size = SKB_DATA_ALIGN(size);
4820
4821 if (skb_pfmemalloc(skb))
4822 gfp_mask |= __GFP_MEMALLOC;
4823 data = kmalloc_reserve(size +
4824 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4825 gfp_mask, NUMA_NO_NODE, NULL);
4826 if (!data)
4827 return -ENOMEM;
4828
4829 size = SKB_WITH_OVERHEAD(ksize(data));
4830
4831 memcpy((struct skb_shared_info *)(data + size),
4832 skb_shinfo(skb), offsetof(struct skb_shared_info,
4833 frags[skb_shinfo(skb)->nr_frags]));
4834 if (skb_orphan_frags(skb, gfp_mask)) {
4835 kfree(data);
4836 return -ENOMEM;
4837 }
4838 shinfo = (struct skb_shared_info *)(data + size);
4839 for (i = 0; i < nfrags; i++) {
4840 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4841
4842 if (pos + fsize > off) {
4843 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4844
4845 if (pos < off) {
4846 /* Split frag.
4847 * We have two variants in this case:
4848 * 1. Move all the frag to the second
4849 * part, if it is possible. F.e.
4850 * this approach is mandatory for TUX,
4851 * where splitting is expensive.
4852 * 2. Split is accurately. We make this.
4853 */
4854 shinfo->frags[0].page_offset += off - pos;
4855 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4856 }
4857 skb_frag_ref(skb, i);
4858 k++;
4859 }
4860 pos += fsize;
4861 }
4862 shinfo->nr_frags = k;
4863 if (skb_has_frag_list(skb))
4864 skb_clone_fraglist(skb);
4865
4866 if (k == 0) {
4867 /* split line is in frag list */
4868 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4869 }
4870 skb_release_data(skb);
4871
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004872 skb->head = data;
4873 skb->head_frag = 0;
4874 skb->data = data;
4875#ifdef NET_SKBUFF_DATA_USES_OFFSET
4876 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004877#else
4878 skb->end = skb->head + size;
4879#endif
4880 skb_reset_tail_pointer(skb);
4881 skb_headers_offset_update(skb, 0);
4882 skb->cloned = 0;
4883 skb->hdr_len = 0;
4884 skb->nohdr = 0;
4885 skb->len -= off;
4886 skb->data_len = skb->len;
4887 atomic_set(&skb_shinfo(skb)->dataref, 1);
4888 return 0;
4889}
4890
4891/* remove len bytes from the beginning of the skb */
4892static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4893{
4894 int headlen = skb_headlen(skb);
4895
4896 if (len < headlen)
4897 return pskb_carve_inside_header(skb, len, headlen, gfp);
4898 else
4899 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4900}
4901
4902/* Extract to_copy bytes starting at off from skb, and return this in
4903 * a new skb
4904 */
4905struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
4906 int to_copy, gfp_t gfp)
4907{
4908 struct sk_buff *clone = skb_clone(skb, gfp);
4909
4910 if (!clone)
4911 return NULL;
4912
4913 if (pskb_carve(clone, off, gfp) < 0 ||
4914 pskb_trim(clone, to_copy)) {
4915 kfree_skb(clone);
4916 return NULL;
4917 }
4918 return clone;
4919}
4920EXPORT_SYMBOL(pskb_extract);