blob: 747263c498388cfdf49e6891869dadee62242ddf [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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/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
Mel Gormanc93bdd02012-07-31 16:44:19 -0700212 cache = (flags & SKB_ALLOC_FCLONE)
213 ? skbuff_fclone_cache : skbuff_head_cache;
214
215 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800219 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!skb)
221 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700222 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000224 /* We do our best to align skb_shared_info on a separate cache
225 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
226 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
227 * Both skb->head and skb_shared_info are cache line aligned.
228 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000229 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000230 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700231 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!data)
233 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000234 /* kmalloc(size) might give us more room than requested.
235 * Put skb_shared_info exactly at the end of allocated zone,
236 * to allow max possible filling before reallocation.
237 */
238 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700239 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300241 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700242 * Only clear those fields we need to clear, not those that we will
243 * actually initialise below. Hence, don't put any more fields after
244 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300245 */
246 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000247 /* Account for allocated memory : skb + skb->head */
248 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700249 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 atomic_set(&skb->users, 1);
251 skb->head = data;
252 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700253 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700254 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800255 skb->mac_header = (typeof(skb->mac_header))~0U;
256 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000257
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258 /* make sure we initialize shinfo sequentially */
259 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700260 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800261 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000262 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800263
Mel Gormanc93bdd02012-07-31 16:44:19 -0700264 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700265 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700267 fclones = container_of(skb, struct sk_buff_fclones, skb1);
268
269 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700270 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700271 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700272
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800273 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275out:
276 return skb;
277nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800278 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 skb = NULL;
280 goto out;
281}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800282EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700285 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700287 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000288 *
289 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100290 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700291 * @frag_size is 0, otherwise data should come from the page allocator
292 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
295 * Notes :
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
302 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700303struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000304{
305 struct skb_shared_info *shinfo;
306 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000307 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000308
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310 if (!skb)
311 return NULL;
312
Eric Dumazetd3836f22012-04-27 00:33:38 +0000313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
318 skb->head = data;
319 skb->data = data;
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000324
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
330
331 return skb;
332}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700333
334/* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
338 */
339struct sk_buff *build_skb(void *data, unsigned int frag_size)
340{
341 struct sk_buff *skb = __build_skb(data, frag_size);
342
343 if (skb && frag_size) {
344 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700345 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700346 skb->pfmemalloc = 1;
347 }
348 return skb;
349}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000350EXPORT_SYMBOL(build_skb);
351
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100352#define NAPI_SKB_CACHE_SIZE 64
353
354struct napi_alloc_cache {
355 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300356 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100357 void *skb_cache[NAPI_SKB_CACHE_SIZE];
358};
359
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700360static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100361static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800362
363static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700365 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800366 unsigned long flags;
367 void *data;
368
369 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700370 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800371 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000372 local_irq_restore(flags);
373 return data;
374}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700375
376/**
377 * netdev_alloc_frag - allocate a page fragment
378 * @fragsz: fragment size
379 *
380 * Allocates a frag from a page for receive buffer.
381 * Uses GFP_ATOMIC allocations.
382 */
383void *netdev_alloc_frag(unsigned int fragsz)
384{
385 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
386}
Eric Dumazet6f532612012-05-18 05:12:12 +0000387EXPORT_SYMBOL(netdev_alloc_frag);
388
Alexander Duyckffde7322014-12-09 19:40:42 -0800389static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
390{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100391 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700392
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800393 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800394}
395
396void *napi_alloc_frag(unsigned int fragsz)
397{
398 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399}
400EXPORT_SYMBOL(napi_alloc_frag);
401
Eric Dumazet6f532612012-05-18 05:12:12 +0000402/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800403 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
404 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900405 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800406 * @gfp_mask: get_free_pages mask, passed to alloc_skb
407 *
408 * Allocate a new &sk_buff and assign it a usage count of one. The
409 * buffer has NET_SKB_PAD headroom built in. Users should allocate
410 * the headroom they think they need without accounting for the
411 * built in space. The built in space is used for optimisations.
412 *
413 * %NULL is returned if there is no free memory.
414 */
Alexander Duyck94519802015-05-06 21:11:40 -0700415struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
416 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800417{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700418 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700419 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800420 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700421 bool pfmemalloc;
422 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800423
Alexander Duyck94519802015-05-06 21:11:40 -0700424 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800425
Alexander Duyck94519802015-05-06 21:11:40 -0700426 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800427 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700428 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
429 if (!skb)
430 goto skb_fail;
431 goto skb_success;
432 }
Alexander Duyck94519802015-05-06 21:11:40 -0700433
434 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
435 len = SKB_DATA_ALIGN(len);
436
437 if (sk_memalloc_socks())
438 gfp_mask |= __GFP_MEMALLOC;
439
440 local_irq_save(flags);
441
442 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800443 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700444 pfmemalloc = nc->pfmemalloc;
445
446 local_irq_restore(flags);
447
448 if (unlikely(!data))
449 return NULL;
450
451 skb = __build_skb(data, len);
452 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700453 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700454 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700455 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800456
Alexander Duyck94519802015-05-06 21:11:40 -0700457 /* use OR instead of assignment to avoid clearing of bits in mask */
458 if (pfmemalloc)
459 skb->pfmemalloc = 1;
460 skb->head_frag = 1;
461
Alexander Duycka080e7b2015-05-13 13:34:13 -0700462skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700463 skb_reserve(skb, NET_SKB_PAD);
464 skb->dev = dev;
465
Alexander Duycka080e7b2015-05-13 13:34:13 -0700466skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700467 return skb;
468}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800469EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Alexander Duyckfd11a832014-12-09 19:40:49 -0800471/**
472 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
473 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900474 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800475 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
476 *
477 * Allocate a new sk_buff for use in NAPI receive. This buffer will
478 * attempt to allocate the head from a special reserved region used
479 * only for NAPI Rx allocation. By doing this we can save several
480 * CPU cycles by avoiding having to disable and re-enable IRQs.
481 *
482 * %NULL is returned if there is no free memory.
483 */
Alexander Duyck94519802015-05-06 21:11:40 -0700484struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
485 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800486{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100487 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800488 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700489 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490
Alexander Duyck94519802015-05-06 21:11:40 -0700491 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800492
Alexander Duyck94519802015-05-06 21:11:40 -0700493 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800494 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700495 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
496 if (!skb)
497 goto skb_fail;
498 goto skb_success;
499 }
Alexander Duyck94519802015-05-06 21:11:40 -0700500
501 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
502 len = SKB_DATA_ALIGN(len);
503
504 if (sk_memalloc_socks())
505 gfp_mask |= __GFP_MEMALLOC;
506
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800507 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700508 if (unlikely(!data))
509 return NULL;
510
511 skb = __build_skb(data, len);
512 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700513 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700514 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800515 }
516
Alexander Duyck94519802015-05-06 21:11:40 -0700517 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100518 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700519 skb->pfmemalloc = 1;
520 skb->head_frag = 1;
521
Alexander Duycka080e7b2015-05-13 13:34:13 -0700522skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700523 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
524 skb->dev = napi->dev;
525
Alexander Duycka080e7b2015-05-13 13:34:13 -0700526skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800527 return skb;
528}
529EXPORT_SYMBOL(__napi_alloc_skb);
530
Peter Zijlstra654bed12008-10-07 14:22:33 -0700531void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000532 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700533{
534 skb_fill_page_desc(skb, i, page, off, size);
535 skb->len += size;
536 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000537 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700538}
539EXPORT_SYMBOL(skb_add_rx_frag);
540
Jason Wangf8e617e2013-11-01 14:07:47 +0800541void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
542 unsigned int truesize)
543{
544 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
545
546 skb_frag_size_add(frag, size);
547 skb->len += size;
548 skb->data_len += size;
549 skb->truesize += truesize;
550}
551EXPORT_SYMBOL(skb_coalesce_rx_frag);
552
Herbert Xu27b437c2006-07-13 19:26:39 -0700553static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700555 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700556 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Herbert Xu27b437c2006-07-13 19:26:39 -0700559static inline void skb_drop_fraglist(struct sk_buff *skb)
560{
561 skb_drop_list(&skb_shinfo(skb)->frag_list);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static void skb_clone_fraglist(struct sk_buff *skb)
565{
566 struct sk_buff *list;
567
David S. Millerfbb398a2009-06-09 00:18:59 -0700568 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 skb_get(list);
570}
571
Eric Dumazetd3836f22012-04-27 00:33:38 +0000572static void skb_free_head(struct sk_buff *skb)
573{
Alexander Duyck181edb22015-05-06 21:12:03 -0700574 unsigned char *head = skb->head;
575
Eric Dumazetd3836f22012-04-27 00:33:38 +0000576 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700577 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000578 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700579 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000580}
581
Adrian Bunk5bba1712006-06-29 13:02:35 -0700582static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Eric Dumazetff04a772014-09-23 18:39:30 -0700584 struct skb_shared_info *shinfo = skb_shinfo(skb);
585 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric Dumazetff04a772014-09-23 18:39:30 -0700587 if (skb->cloned &&
588 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
589 &shinfo->dataref))
590 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000591
Eric Dumazetff04a772014-09-23 18:39:30 -0700592 for (i = 0; i < shinfo->nr_frags; i++)
593 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000594
Eric Dumazetff04a772014-09-23 18:39:30 -0700595 /*
596 * If skb buf is from userspace, we need to notify the caller
597 * the lower device DMA has done;
598 */
599 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
600 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric Dumazetff04a772014-09-23 18:39:30 -0700602 uarg = shinfo->destructor_arg;
603 if (uarg->callback)
604 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700606
607 if (shinfo->frag_list)
608 kfree_skb_list(shinfo->frag_list);
609
610 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*
614 * Free an skbuff by memory without cleaning the state.
615 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800616static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700618 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700619
David S. Millerd179cd12005-08-17 14:57:30 -0700620 switch (skb->fclone) {
621 case SKB_FCLONE_UNAVAILABLE:
622 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800623 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700624
625 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700626 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800627
628 /* We usually free the clone (TX completion) before original skb
629 * This test would have no chance to be true for the clone,
630 * while here, branch prediction will be good.
631 */
632 if (atomic_read(&fclones->fclone_ref) == 1)
633 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700634 break;
635
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800636 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700637 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700638 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700639 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800640 if (!atomic_dec_and_test(&fclones->fclone_ref))
641 return;
642fastpath:
643 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700646static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Eric Dumazetadf30902009-06-02 05:19:30 +0000648 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#ifdef CONFIG_XFRM
650 secpath_put(skb->sp);
651#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700652 if (skb->destructor) {
653 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 skb->destructor(skb);
655 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000656#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100657 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100658#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200659#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 nf_bridge_put(skb->nf_bridge);
661#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700662}
663
664/* Free everything but the sk_buff shell. */
665static void skb_release_all(struct sk_buff *skb)
666{
667 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000668 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000669 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Herbert Xu2d4baff2007-11-26 23:11:19 +0800672/**
673 * __kfree_skb - private function
674 * @skb: buffer
675 *
676 * Free an sk_buff. Release anything attached to the buffer.
677 * Clean the state. This is an internal helper function. Users should
678 * always call kfree_skb
679 */
680
681void __kfree_skb(struct sk_buff *skb)
682{
683 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 kfree_skbmem(skb);
685}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800686EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800689 * kfree_skb - free an sk_buff
690 * @skb: buffer to free
691 *
692 * Drop a reference to the buffer and free it if the usage count has
693 * hit zero.
694 */
695void kfree_skb(struct sk_buff *skb)
696{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200697 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800698 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200699
Neil Hormanead2ceb2009-03-11 09:49:55 +0000700 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800701 __kfree_skb(skb);
702}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800703EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800704
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700705void kfree_skb_list(struct sk_buff *segs)
706{
707 while (segs) {
708 struct sk_buff *next = segs->next;
709
710 kfree_skb(segs);
711 segs = next;
712 }
713}
714EXPORT_SYMBOL(kfree_skb_list);
715
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700716/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000717 * skb_tx_error - report an sk_buff xmit error
718 * @skb: buffer that triggered an error
719 *
720 * Report xmit error if a device callback is tracking this skb.
721 * skb must be freed afterwards.
722 */
723void skb_tx_error(struct sk_buff *skb)
724{
725 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
726 struct ubuf_info *uarg;
727
728 uarg = skb_shinfo(skb)->destructor_arg;
729 if (uarg->callback)
730 uarg->callback(uarg, false);
731 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
732 }
733}
734EXPORT_SYMBOL(skb_tx_error);
735
736/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000737 * consume_skb - free an skbuff
738 * @skb: buffer to free
739 *
740 * Drop a ref to the buffer and free it if the usage count has hit zero
741 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
742 * is being dropped after a failure and notes that
743 */
744void consume_skb(struct sk_buff *skb)
745{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200746 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000747 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200748
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900749 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000750 __kfree_skb(skb);
751}
752EXPORT_SYMBOL(consume_skb);
753
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100754void __kfree_skb_flush(void)
755{
756 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
757
758 /* flush skb_cache if containing objects */
759 if (nc->skb_count) {
760 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
761 nc->skb_cache);
762 nc->skb_count = 0;
763 }
764}
765
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100766static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100767{
768 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
769
770 /* drop skb->head and call any destructors for packet */
771 skb_release_all(skb);
772
773 /* record skb to CPU local list */
774 nc->skb_cache[nc->skb_count++] = skb;
775
776#ifdef CONFIG_SLUB
777 /* SLUB writes into objects when freeing */
778 prefetchw(skb);
779#endif
780
781 /* flush skb_cache if it is filled */
782 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
783 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
784 nc->skb_cache);
785 nc->skb_count = 0;
786 }
787}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100788void __kfree_skb_defer(struct sk_buff *skb)
789{
790 _kfree_skb_defer(skb);
791}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100792
793void napi_consume_skb(struct sk_buff *skb, int budget)
794{
795 if (unlikely(!skb))
796 return;
797
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100798 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100799 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100800 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100801 return;
802 }
803
804 if (likely(atomic_read(&skb->users) == 1))
805 smp_rmb();
806 else if (likely(!atomic_dec_and_test(&skb->users)))
807 return;
808 /* if reaching here SKB is ready to free */
809 trace_consume_skb(skb);
810
811 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700812 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100813 __kfree_skb(skb);
814 return;
815 }
816
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100817 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100818}
819EXPORT_SYMBOL(napi_consume_skb);
820
Eric Dumazetb1937222014-09-28 22:18:47 -0700821/* Make sure a field is enclosed inside headers_start/headers_end section */
822#define CHECK_SKB_FIELD(field) \
823 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
824 offsetof(struct sk_buff, headers_start)); \
825 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
826 offsetof(struct sk_buff, headers_end)); \
827
Herbert Xudec18812007-10-14 00:37:30 -0700828static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
829{
830 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700831 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700832 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700833 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000834 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700835#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700836 new->sp = secpath_get(old->sp);
837#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700838 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700839
Eric Dumazetb1937222014-09-28 22:18:47 -0700840 /* Note : this field could be in headers_start/headers_end section
841 * It is not yet because we do not want to have a 16 bit hole
842 */
843 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300844
Eric Dumazetb1937222014-09-28 22:18:47 -0700845 memcpy(&new->headers_start, &old->headers_start,
846 offsetof(struct sk_buff, headers_end) -
847 offsetof(struct sk_buff, headers_start));
848 CHECK_SKB_FIELD(protocol);
849 CHECK_SKB_FIELD(csum);
850 CHECK_SKB_FIELD(hash);
851 CHECK_SKB_FIELD(priority);
852 CHECK_SKB_FIELD(skb_iif);
853 CHECK_SKB_FIELD(vlan_proto);
854 CHECK_SKB_FIELD(vlan_tci);
855 CHECK_SKB_FIELD(transport_header);
856 CHECK_SKB_FIELD(network_header);
857 CHECK_SKB_FIELD(mac_header);
858 CHECK_SKB_FIELD(inner_protocol);
859 CHECK_SKB_FIELD(inner_transport_header);
860 CHECK_SKB_FIELD(inner_network_header);
861 CHECK_SKB_FIELD(inner_mac_header);
862 CHECK_SKB_FIELD(mark);
863#ifdef CONFIG_NETWORK_SECMARK
864 CHECK_SKB_FIELD(secmark);
865#endif
Cong Wange0d10952013-08-01 11:10:25 +0800866#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700867 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300868#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800869#ifdef CONFIG_XPS
870 CHECK_SKB_FIELD(sender_cpu);
871#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700872#ifdef CONFIG_NET_SCHED
873 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700874#endif
875
Herbert Xudec18812007-10-14 00:37:30 -0700876}
877
Herbert Xu82c49a32009-05-22 22:11:37 +0000878/*
879 * You should not add any new code to this function. Add it to
880 * __copy_skb_header above instead.
881 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700882static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#define C(x) n->x = skb->x
885
886 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700888 __copy_skb_header(n, skb);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 C(len);
891 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700892 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700893 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800894 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 C(tail);
898 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800899 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000900 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800901 C(data);
902 C(truesize);
903 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 atomic_inc(&(skb_shinfo(skb)->dataref));
906 skb->cloned = 1;
907
908 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700909#undef C
910}
911
912/**
913 * skb_morph - morph one skb into another
914 * @dst: the skb to receive the contents
915 * @src: the skb to supply the contents
916 *
917 * This is identical to skb_clone except that the target skb is
918 * supplied by the user.
919 *
920 * The target skb is returned upon exit.
921 */
922struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
923{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800924 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700925 return __skb_clone(dst, src);
926}
927EXPORT_SYMBOL_GPL(skb_morph);
928
Ben Hutchings2c530402012-07-10 10:55:09 +0000929/**
930 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000931 * @skb: the skb to modify
932 * @gfp_mask: allocation priority
933 *
934 * This must be called on SKBTX_DEV_ZEROCOPY skb.
935 * It will copy all frags into kernel and drop the reference
936 * to userspace pages.
937 *
938 * If this function is called from an interrupt gfp_mask() must be
939 * %GFP_ATOMIC.
940 *
941 * Returns 0 on success or a negative error code on failure
942 * to allocate kernel memory to copy to.
943 */
944int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000945{
946 int i;
947 int num_frags = skb_shinfo(skb)->nr_frags;
948 struct page *page, *head = NULL;
949 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
950
951 for (i = 0; i < num_frags; i++) {
952 u8 *vaddr;
953 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
954
Krishna Kumar02756ed2012-07-17 02:05:29 +0000955 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000956 if (!page) {
957 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900958 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000959 put_page(head);
960 head = next;
961 }
962 return -ENOMEM;
963 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200964 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000965 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000966 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200967 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900968 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000969 head = page;
970 }
971
972 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000973 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000974 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000975
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000976 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000977
978 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000979 for (i = num_frags - 1; i >= 0; i--) {
980 __skb_fill_page_desc(skb, i, head, 0,
981 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900982 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000983 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000984
985 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000986 return 0;
987}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000988EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000989
Herbert Xue0053ec2007-10-14 00:37:52 -0700990/**
991 * skb_clone - duplicate an sk_buff
992 * @skb: buffer to clone
993 * @gfp_mask: allocation priority
994 *
995 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
996 * copies share the same packet data but not structure. The new
997 * buffer has a reference count of 1. If the allocation fails the
998 * function returns %NULL otherwise the new buffer is returned.
999 *
1000 * If this function is called from an interrupt gfp_mask() must be
1001 * %GFP_ATOMIC.
1002 */
1003
1004struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1005{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001006 struct sk_buff_fclones *fclones = container_of(skb,
1007 struct sk_buff_fclones,
1008 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001009 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001010
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001011 if (skb_orphan_frags(skb, gfp_mask))
1012 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001013
Herbert Xue0053ec2007-10-14 00:37:52 -07001014 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001015 atomic_read(&fclones->fclone_ref) == 1) {
1016 n = &fclones->skb2;
1017 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001018 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001019 if (skb_pfmemalloc(skb))
1020 gfp_mask |= __GFP_MEMALLOC;
1021
Herbert Xue0053ec2007-10-14 00:37:52 -07001022 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1023 if (!n)
1024 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001025
1026 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001027 n->fclone = SKB_FCLONE_UNAVAILABLE;
1028 }
1029
1030 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001032EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001034static void skb_headers_offset_update(struct sk_buff *skb, int off)
1035{
Eric Dumazet030737b2013-10-19 11:42:54 -07001036 /* Only adjust this if it actually is csum_start rather than csum */
1037 if (skb->ip_summed == CHECKSUM_PARTIAL)
1038 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001039 /* {transport,network,mac}_header and tail are relative to skb->head */
1040 skb->transport_header += off;
1041 skb->network_header += off;
1042 if (skb_mac_header_was_set(skb))
1043 skb->mac_header += off;
1044 skb->inner_transport_header += off;
1045 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001046 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1050{
Herbert Xudec18812007-10-14 00:37:30 -07001051 __copy_skb_header(new, old);
1052
Herbert Xu79671682006-06-22 02:40:14 -07001053 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1054 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1055 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Mel Gormanc93bdd02012-07-31 16:44:19 -07001058static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1059{
1060 if (skb_pfmemalloc(skb))
1061 return SKB_ALLOC_RX;
1062 return 0;
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/**
1066 * skb_copy - create private copy of an sk_buff
1067 * @skb: buffer to copy
1068 * @gfp_mask: allocation priority
1069 *
1070 * Make a copy of both an &sk_buff and its data. This is used when the
1071 * caller wishes to modify the data and needs a private copy of the
1072 * data to alter. Returns %NULL on failure or the pointer to the buffer
1073 * on success. The returned buffer has a reference count of 1.
1074 *
1075 * As by-product this function converts non-linear &sk_buff to linear
1076 * one, so that &sk_buff becomes completely private and caller is allowed
1077 * to modify all the data of returned buffer. This means that this
1078 * function is not recommended for use in circumstances when only
1079 * header is going to be modified. Use pskb_copy() instead.
1080 */
1081
Al Virodd0fc662005-10-07 07:46:04 +01001082struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001084 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001085 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001086 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1087 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!n)
1090 return NULL;
1091
1092 /* Set the data pointer */
1093 skb_reserve(n, headerlen);
1094 /* Set the tail pointer and length */
1095 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1098 BUG();
1099
1100 copy_skb_header(n, skb);
1101 return n;
1102}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001103EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001106 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001108 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001110 * @fclone: if true allocate the copy of the skb from the fclone
1111 * cache instead of the head cache; it is recommended to set this
1112 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
1114 * Make a copy of both an &sk_buff and part of its data, located
1115 * in header. Fragmented data remain shared. This is used when
1116 * the caller wishes to modify only header of &sk_buff and needs
1117 * private copy of the header to alter. Returns %NULL on failure
1118 * or the pointer to the buffer on success.
1119 * The returned buffer has a reference count of 1.
1120 */
1121
Octavian Purdilabad93e92014-06-12 01:36:26 +03001122struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1123 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Eric Dumazet117632e2011-12-03 21:39:53 +00001125 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001126 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1127 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!n)
1130 goto out;
1131
1132 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001133 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Set the tail pointer and length */
1135 skb_put(n, skb_headlen(skb));
1136 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001137 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Herbert Xu25f484a2006-11-07 14:57:15 -08001139 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 n->data_len = skb->data_len;
1141 n->len = skb->len;
1142
1143 if (skb_shinfo(skb)->nr_frags) {
1144 int i;
1145
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001146 if (skb_orphan_frags(skb, gfp_mask)) {
1147 kfree_skb(n);
1148 n = NULL;
1149 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1152 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001153 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 skb_shinfo(n)->nr_frags = i;
1156 }
1157
David S. Miller21dc3302010-08-23 00:13:46 -07001158 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1160 skb_clone_fraglist(n);
1161 }
1162
1163 copy_skb_header(n, skb);
1164out:
1165 return n;
1166}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001167EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169/**
1170 * pskb_expand_head - reallocate header of &sk_buff
1171 * @skb: buffer to reallocate
1172 * @nhead: room to add at head
1173 * @ntail: room to add at tail
1174 * @gfp_mask: allocation priority
1175 *
Mathias Krausebc323832013-11-07 14:18:26 +01001176 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1177 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 * reference count of 1. Returns zero in the case of success or error,
1179 * if expansion failed. In the last case, &sk_buff is not changed.
1180 *
1181 * All the pointers pointing into skb header may change and must be
1182 * reloaded after call to this function.
1183 */
1184
Victor Fusco86a76ca2005-07-08 14:57:47 -07001185int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001186 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Eric Dumazet158f3232017-01-27 07:11:27 -08001188 int i, osize = skb_end_offset(skb);
1189 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001191 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Herbert Xu4edd87a2008-10-01 07:09:38 -07001193 BUG_ON(nhead < 0);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (skb_shared(skb))
1196 BUG();
1197
1198 size = SKB_DATA_ALIGN(size);
1199
Mel Gormanc93bdd02012-07-31 16:44:19 -07001200 if (skb_pfmemalloc(skb))
1201 gfp_mask |= __GFP_MEMALLOC;
1202 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1203 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!data)
1205 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001206 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001209 * optimized for the cases when header is void.
1210 */
1211 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1212
1213 memcpy((struct skb_shared_info *)(data + size),
1214 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001215 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Alexander Duyck3e245912012-05-04 14:26:51 +00001217 /*
1218 * if shinfo is shared we must drop the old head gracefully, but if it
1219 * is not we can just drop the old head and let the existing refcount
1220 * be since all we did is relocate the values
1221 */
1222 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001223 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001224 if (skb_orphan_frags(skb, gfp_mask))
1225 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001226 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001227 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Eric Dumazet1fd63042010-09-02 23:09:32 +00001229 if (skb_has_frag_list(skb))
1230 skb_clone_fraglist(skb);
1231
1232 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001233 } else {
1234 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 off = (data + nhead) - skb->head;
1237
1238 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001239 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001241#ifdef NET_SKBUFF_DATA_USES_OFFSET
1242 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001243 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001244#else
1245 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001246#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001247 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001248 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001250 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 skb->nohdr = 0;
1252 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001253
1254 /* It is not generally safe to change skb->truesize.
1255 * For the moment, we really care of rx path, or
1256 * when skb is orphaned (not attached to a socket).
1257 */
1258 if (!skb->sk || skb->destructor == sock_edemux)
1259 skb->truesize += size - osize;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262
Shirley Maa6686f22011-07-06 12:22:12 +00001263nofrags:
1264 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265nodata:
1266 return -ENOMEM;
1267}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001268EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270/* Make private copy of skb with writable head and some headroom */
1271
1272struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1273{
1274 struct sk_buff *skb2;
1275 int delta = headroom - skb_headroom(skb);
1276
1277 if (delta <= 0)
1278 skb2 = pskb_copy(skb, GFP_ATOMIC);
1279 else {
1280 skb2 = skb_clone(skb, GFP_ATOMIC);
1281 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1282 GFP_ATOMIC)) {
1283 kfree_skb(skb2);
1284 skb2 = NULL;
1285 }
1286 }
1287 return skb2;
1288}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001289EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291/**
1292 * skb_copy_expand - copy and expand sk_buff
1293 * @skb: buffer to copy
1294 * @newheadroom: new free bytes at head
1295 * @newtailroom: new free bytes at tail
1296 * @gfp_mask: allocation priority
1297 *
1298 * Make a copy of both an &sk_buff and its data and while doing so
1299 * allocate additional space.
1300 *
1301 * This is used when the caller wishes to modify the data and needs a
1302 * private copy of the data to alter as well as more space for new fields.
1303 * Returns %NULL on failure or the pointer to the buffer
1304 * on success. The returned buffer has a reference count of 1.
1305 *
1306 * You must pass %GFP_ATOMIC as the allocation priority if this function
1307 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
1309struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001310 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001311 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 /*
1314 * Allocate the copy buffer
1315 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001316 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1317 gfp_mask, skb_alloc_rx_flag(skb),
1318 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001319 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int head_copy_len, head_copy_off;
1321
1322 if (!n)
1323 return NULL;
1324
1325 skb_reserve(n, newheadroom);
1326
1327 /* Set the tail pointer and length */
1328 skb_put(n, skb->len);
1329
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001330 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 head_copy_off = 0;
1332 if (newheadroom <= head_copy_len)
1333 head_copy_len = newheadroom;
1334 else
1335 head_copy_off = newheadroom - head_copy_len;
1336
1337 /* Copy the linear header and data. */
1338 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1339 skb->len + head_copy_len))
1340 BUG();
1341
1342 copy_skb_header(n, skb);
1343
Eric Dumazet030737b2013-10-19 11:42:54 -07001344 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return n;
1347}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001348EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350/**
1351 * skb_pad - zero pad the tail of an skb
1352 * @skb: buffer to pad
1353 * @pad: space to pad
1354 *
1355 * Ensure that a buffer is followed by a padding area that is zero
1356 * filled. Used by network drivers which may DMA or transfer data
1357 * beyond the buffer end onto the wire.
1358 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001359 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001361
Herbert Xu5b057c62006-06-23 02:06:41 -07001362int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Herbert Xu5b057c62006-06-23 02:06:41 -07001364 int err;
1365 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001368 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001372
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001373 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001374 if (likely(skb_cloned(skb) || ntail > 0)) {
1375 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1376 if (unlikely(err))
1377 goto free_skb;
1378 }
1379
1380 /* FIXME: The use of this function with non-linear skb's really needs
1381 * to be audited.
1382 */
1383 err = skb_linearize(skb);
1384 if (unlikely(err))
1385 goto free_skb;
1386
1387 memset(skb->data + skb->len, 0, pad);
1388 return 0;
1389
1390free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001392 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001393}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001394EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001395
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001396/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001397 * pskb_put - add data to the tail of a potentially fragmented buffer
1398 * @skb: start of the buffer to use
1399 * @tail: tail fragment of the buffer to use
1400 * @len: amount of data to add
1401 *
1402 * This function extends the used data area of the potentially
1403 * fragmented buffer. @tail must be the last fragment of @skb -- or
1404 * @skb itself. If this would exceed the total buffer size the kernel
1405 * will panic. A pointer to the first byte of the extra data is
1406 * returned.
1407 */
1408
1409unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1410{
1411 if (tail != skb) {
1412 skb->data_len += len;
1413 skb->len += len;
1414 }
1415 return skb_put(tail, len);
1416}
1417EXPORT_SYMBOL_GPL(pskb_put);
1418
1419/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001420 * skb_put - add data to a buffer
1421 * @skb: buffer to use
1422 * @len: amount of data to add
1423 *
1424 * This function extends the used data area of the buffer. If this would
1425 * exceed the total buffer size the kernel will panic. A pointer to the
1426 * first byte of the extra data is returned.
1427 */
1428unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1429{
1430 unsigned char *tmp = skb_tail_pointer(skb);
1431 SKB_LINEAR_ASSERT(skb);
1432 skb->tail += len;
1433 skb->len += len;
1434 if (unlikely(skb->tail > skb->end))
1435 skb_over_panic(skb, len, __builtin_return_address(0));
1436 return tmp;
1437}
1438EXPORT_SYMBOL(skb_put);
1439
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001440/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001441 * skb_push - add data to the start of a buffer
1442 * @skb: buffer to use
1443 * @len: amount of data to add
1444 *
1445 * This function extends the used data area of the buffer at the buffer
1446 * start. If this would exceed the total buffer headroom the kernel will
1447 * panic. A pointer to the first byte of the extra data is returned.
1448 */
1449unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1450{
1451 skb->data -= len;
1452 skb->len += len;
1453 if (unlikely(skb->data<skb->head))
1454 skb_under_panic(skb, len, __builtin_return_address(0));
1455 return skb->data;
1456}
1457EXPORT_SYMBOL(skb_push);
1458
1459/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001460 * skb_pull - remove data from the start of a buffer
1461 * @skb: buffer to use
1462 * @len: amount of data to remove
1463 *
1464 * This function removes data from the start of a buffer, returning
1465 * the memory to the headroom. A pointer to the next data in the buffer
1466 * is returned. Once the data has been pulled future pushes will overwrite
1467 * the old data.
1468 */
1469unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1470{
David S. Miller47d29642010-05-02 02:21:44 -07001471 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001472}
1473EXPORT_SYMBOL(skb_pull);
1474
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001475/**
1476 * skb_trim - remove end from a buffer
1477 * @skb: buffer to alter
1478 * @len: new length
1479 *
1480 * Cut the length of a buffer down by removing data from the tail. If
1481 * the buffer is already under the length specified it is not modified.
1482 * The skb must be linear.
1483 */
1484void skb_trim(struct sk_buff *skb, unsigned int len)
1485{
1486 if (skb->len > len)
1487 __skb_trim(skb, len);
1488}
1489EXPORT_SYMBOL(skb_trim);
1490
Herbert Xu3cc0e872006-06-09 16:13:38 -07001491/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 */
1493
Herbert Xu3cc0e872006-06-09 16:13:38 -07001494int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Herbert Xu27b437c2006-07-13 19:26:39 -07001496 struct sk_buff **fragp;
1497 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int offset = skb_headlen(skb);
1499 int nfrags = skb_shinfo(skb)->nr_frags;
1500 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001501 int err;
1502
1503 if (skb_cloned(skb) &&
1504 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001507 i = 0;
1508 if (offset >= len)
1509 goto drop_pages;
1510
1511 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001512 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001513
1514 if (end < len) {
1515 offset = end;
1516 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001518
Eric Dumazet9e903e02011-10-18 21:00:24 +00001519 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001520
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001521drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001522 skb_shinfo(skb)->nr_frags = i;
1523
1524 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001525 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001526
David S. Miller21dc3302010-08-23 00:13:46 -07001527 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001528 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001529 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
Herbert Xu27b437c2006-07-13 19:26:39 -07001532 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1533 fragp = &frag->next) {
1534 int end = offset + frag->len;
1535
1536 if (skb_shared(frag)) {
1537 struct sk_buff *nfrag;
1538
1539 nfrag = skb_clone(frag, GFP_ATOMIC);
1540 if (unlikely(!nfrag))
1541 return -ENOMEM;
1542
1543 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001544 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001545 frag = nfrag;
1546 *fragp = frag;
1547 }
1548
1549 if (end < len) {
1550 offset = end;
1551 continue;
1552 }
1553
1554 if (end > len &&
1555 unlikely((err = pskb_trim(frag, len - offset))))
1556 return err;
1557
1558 if (frag->next)
1559 skb_drop_list(&frag->next);
1560 break;
1561 }
1562
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001563done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001564 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 skb->data_len -= skb->len - len;
1566 skb->len = len;
1567 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001568 skb->len = len;
1569 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001570 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001573 if (!skb->sk || skb->destructor == sock_edemux)
1574 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001577EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579/**
1580 * __pskb_pull_tail - advance tail of skb header
1581 * @skb: buffer to reallocate
1582 * @delta: number of bytes to advance tail
1583 *
1584 * The function makes a sense only on a fragmented &sk_buff,
1585 * it expands header moving its tail forward and copying necessary
1586 * data from fragmented part.
1587 *
1588 * &sk_buff MUST have reference count of 1.
1589 *
1590 * Returns %NULL (and &sk_buff does not change) if pull failed
1591 * or value of new tail of skb in the case of success.
1592 *
1593 * All the pointers pointing into skb header may change and must be
1594 * reloaded after call to this function.
1595 */
1596
1597/* Moves tail of skb head forward, copying data from fragmented part,
1598 * when it is necessary.
1599 * 1. It may fail due to malloc failure.
1600 * 2. It may change skb pointers.
1601 *
1602 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1603 */
1604unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1605{
1606 /* If skb has not enough free space at tail, get new one
1607 * plus 128 bytes for future expansions. If we have enough
1608 * room at tail, reallocate without expansion only if skb is cloned.
1609 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001610 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (eat > 0 || skb_cloned(skb)) {
1613 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1614 GFP_ATOMIC))
1615 return NULL;
1616 }
1617
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001618 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 BUG();
1620
1621 /* Optimization: no fragments, no reasons to preestimate
1622 * size of pulled pages. Superb.
1623 */
David S. Miller21dc3302010-08-23 00:13:46 -07001624 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto pull_pages;
1626
1627 /* Estimate size of pulled pages. */
1628 eat = delta;
1629 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001630 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1631
1632 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001634 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 /* If we need update frag list, we are in troubles.
1638 * Certainly, it possible to add an offset to skb data,
1639 * but taking into account that pulling is expected to
1640 * be very rare operation, it is worth to fight against
1641 * further bloating skb head and crucify ourselves here instead.
1642 * Pure masohism, indeed. 8)8)
1643 */
1644 if (eat) {
1645 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1646 struct sk_buff *clone = NULL;
1647 struct sk_buff *insp = NULL;
1648
1649 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001650 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (list->len <= eat) {
1653 /* Eaten as whole. */
1654 eat -= list->len;
1655 list = list->next;
1656 insp = list;
1657 } else {
1658 /* Eaten partially. */
1659
1660 if (skb_shared(list)) {
1661 /* Sucks! We need to fork list. :-( */
1662 clone = skb_clone(list, GFP_ATOMIC);
1663 if (!clone)
1664 return NULL;
1665 insp = list->next;
1666 list = clone;
1667 } else {
1668 /* This may be pulled without
1669 * problems. */
1670 insp = list;
1671 }
1672 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001673 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return NULL;
1675 }
1676 break;
1677 }
1678 } while (eat);
1679
1680 /* Free pulled out fragments. */
1681 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1682 skb_shinfo(skb)->frag_list = list->next;
1683 kfree_skb(list);
1684 }
1685 /* And insert new clone at head. */
1686 if (clone) {
1687 clone->next = list;
1688 skb_shinfo(skb)->frag_list = clone;
1689 }
1690 }
1691 /* Success! Now we may commit changes to skb data. */
1692
1693pull_pages:
1694 eat = delta;
1695 k = 0;
1696 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001697 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1698
1699 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001700 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001701 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 } else {
1703 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1704 if (eat) {
1705 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001706 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 eat = 0;
1708 }
1709 k++;
1710 }
1711 }
1712 skb_shinfo(skb)->nr_frags = k;
1713
1714 skb->tail += delta;
1715 skb->data_len -= delta;
1716
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001717 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001719EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Eric Dumazet22019b12011-07-29 18:37:31 +00001721/**
1722 * skb_copy_bits - copy bits from skb to kernel buffer
1723 * @skb: source skb
1724 * @offset: offset in source
1725 * @to: destination buffer
1726 * @len: number of bytes to copy
1727 *
1728 * Copy the specified number of bytes from the source skb to the
1729 * destination buffer.
1730 *
1731 * CAUTION ! :
1732 * If its prototype is ever changed,
1733 * check arch/{*}/net/{*}.S files,
1734 * since it is called from BPF assembly code.
1735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1737{
David S. Miller1a028e52007-04-27 15:21:23 -07001738 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001739 struct sk_buff *frag_iter;
1740 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (offset > (int)skb->len - len)
1743 goto fault;
1744
1745 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001746 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (copy > len)
1748 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001749 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if ((len -= copy) == 0)
1751 return 0;
1752 offset += copy;
1753 to += copy;
1754 }
1755
1756 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001757 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001758 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001760 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001761
Eric Dumazet51c56b02012-04-05 11:35:15 +02001762 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if ((copy = end - offset) > 0) {
1764 u8 *vaddr;
1765
1766 if (copy > len)
1767 copy = len;
1768
Eric Dumazet51c56b02012-04-05 11:35:15 +02001769 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001771 vaddr + f->page_offset + offset - start,
1772 copy);
1773 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 if ((len -= copy) == 0)
1776 return 0;
1777 offset += copy;
1778 to += copy;
1779 }
David S. Miller1a028e52007-04-27 15:21:23 -07001780 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
David S. Millerfbb398a2009-06-09 00:18:59 -07001783 skb_walk_frags(skb, frag_iter) {
1784 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
David S. Millerfbb398a2009-06-09 00:18:59 -07001786 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
David S. Millerfbb398a2009-06-09 00:18:59 -07001788 end = start + frag_iter->len;
1789 if ((copy = end - offset) > 0) {
1790 if (copy > len)
1791 copy = len;
1792 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1793 goto fault;
1794 if ((len -= copy) == 0)
1795 return 0;
1796 offset += copy;
1797 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001799 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Shirley Maa6686f22011-07-06 12:22:12 +00001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (!len)
1803 return 0;
1804
1805fault:
1806 return -EFAULT;
1807}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001808EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jens Axboe9c55e012007-11-06 23:30:13 -08001810/*
1811 * Callback from splice_to_pipe(), if we need to release some pages
1812 * at the end of the spd in case we error'ed out in filling the pipe.
1813 */
1814static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1815{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001816 put_page(spd->pages[i]);
1817}
Jens Axboe9c55e012007-11-06 23:30:13 -08001818
David S. Millera108d5f2012-04-23 23:06:11 -04001819static struct page *linear_to_page(struct page *page, unsigned int *len,
1820 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001821 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001822{
Eric Dumazet5640f762012-09-23 23:04:42 +00001823 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001824
Eric Dumazet5640f762012-09-23 23:04:42 +00001825 if (!sk_page_frag_refill(sk, pfrag))
1826 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001827
Eric Dumazet5640f762012-09-23 23:04:42 +00001828 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001829
Eric Dumazet5640f762012-09-23 23:04:42 +00001830 memcpy(page_address(pfrag->page) + pfrag->offset,
1831 page_address(page) + *offset, *len);
1832 *offset = pfrag->offset;
1833 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001834
Eric Dumazet5640f762012-09-23 23:04:42 +00001835 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001836}
1837
Eric Dumazet41c73a02012-04-22 12:26:16 +00001838static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1839 struct page *page,
1840 unsigned int offset)
1841{
1842 return spd->nr_pages &&
1843 spd->pages[spd->nr_pages - 1] == page &&
1844 (spd->partial[spd->nr_pages - 1].offset +
1845 spd->partial[spd->nr_pages - 1].len == offset);
1846}
1847
Jens Axboe9c55e012007-11-06 23:30:13 -08001848/*
1849 * Fill page/offset/length into spd, if it can hold more pages.
1850 */
David S. Millera108d5f2012-04-23 23:06:11 -04001851static bool spd_fill_page(struct splice_pipe_desc *spd,
1852 struct pipe_inode_info *pipe, struct page *page,
1853 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001854 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001855 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001856{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001857 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001858 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001859
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001860 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001861 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001862 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001863 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001864 }
1865 if (spd_can_coalesce(spd, page, offset)) {
1866 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001867 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001868 }
1869 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001870 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001871 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001872 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001873 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001874
David S. Millera108d5f2012-04-23 23:06:11 -04001875 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001876}
1877
David S. Millera108d5f2012-04-23 23:06:11 -04001878static bool __splice_segment(struct page *page, unsigned int poff,
1879 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001880 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001881 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001882 struct sock *sk,
1883 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001884{
1885 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001886 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001887
1888 /* skip this segment if already processed */
1889 if (*off >= plen) {
1890 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001891 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001892 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001893
Octavian Purdila2870c432008-07-15 00:49:11 -07001894 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001895 poff += *off;
1896 plen -= *off;
1897 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001898
Eric Dumazet18aafc62013-01-11 14:46:37 +00001899 do {
1900 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001901
Eric Dumazet18aafc62013-01-11 14:46:37 +00001902 if (spd_fill_page(spd, pipe, page, &flen, poff,
1903 linear, sk))
1904 return true;
1905 poff += flen;
1906 plen -= flen;
1907 *len -= flen;
1908 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001909
David S. Millera108d5f2012-04-23 23:06:11 -04001910 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001911}
1912
1913/*
David S. Millera108d5f2012-04-23 23:06:11 -04001914 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001915 * pipe is full or if we already spliced the requested length.
1916 */
David S. Millera108d5f2012-04-23 23:06:11 -04001917static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1918 unsigned int *offset, unsigned int *len,
1919 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001920{
1921 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001922 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001923
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001924 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001925 * If skb->head_frag is set, this 'linear' part is backed by a
1926 * fragment, and if the head is not shared with any clones then
1927 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001928 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001929 if (__splice_segment(virt_to_page(skb->data),
1930 (unsigned long) skb->data & (PAGE_SIZE - 1),
1931 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001932 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001933 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001934 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001935 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001936
1937 /*
1938 * then map the fragments
1939 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001940 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1941 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1942
Ian Campbellea2ab692011-08-22 23:44:58 +00001943 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001944 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001945 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001946 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001947 }
1948
Tom Herbertfa9835e2016-03-07 14:11:04 -08001949 skb_walk_frags(skb, iter) {
1950 if (*offset >= iter->len) {
1951 *offset -= iter->len;
1952 continue;
1953 }
1954 /* __skb_splice_bits() only fails if the output has no room
1955 * left, so no point in going over the frag_list for the error
1956 * case.
1957 */
1958 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1959 return true;
1960 }
1961
David S. Millera108d5f2012-04-23 23:06:11 -04001962 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001963}
1964
1965/*
1966 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001967 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001968 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001969int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001970 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001971 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001972{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001973 struct partial_page partial[MAX_SKB_FRAGS];
1974 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001975 struct splice_pipe_desc spd = {
1976 .pages = pages,
1977 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001978 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001979 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001980 .spd_release = sock_spd_release,
1981 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001982 int ret = 0;
1983
Tom Herbertfa9835e2016-03-07 14:11:04 -08001984 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001985
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001986 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04001987 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001988
Jens Axboe35f3d142010-05-20 10:43:18 +02001989 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001990}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001991EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001992
Herbert Xu357b40a2005-04-19 22:30:14 -07001993/**
1994 * skb_store_bits - store bits from kernel buffer to skb
1995 * @skb: destination buffer
1996 * @offset: offset in destination
1997 * @from: source buffer
1998 * @len: number of bytes to copy
1999 *
2000 * Copy the specified number of bytes from the source buffer to the
2001 * destination skb. This function handles all the messy bits of
2002 * traversing fragment lists and such.
2003 */
2004
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002005int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002006{
David S. Miller1a028e52007-04-27 15:21:23 -07002007 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002008 struct sk_buff *frag_iter;
2009 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002010
2011 if (offset > (int)skb->len - len)
2012 goto fault;
2013
David S. Miller1a028e52007-04-27 15:21:23 -07002014 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002015 if (copy > len)
2016 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002017 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002018 if ((len -= copy) == 0)
2019 return 0;
2020 offset += copy;
2021 from += copy;
2022 }
2023
2024 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2025 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002026 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002027
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002028 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002029
Eric Dumazet9e903e02011-10-18 21:00:24 +00002030 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002031 if ((copy = end - offset) > 0) {
2032 u8 *vaddr;
2033
2034 if (copy > len)
2035 copy = len;
2036
Eric Dumazet51c56b02012-04-05 11:35:15 +02002037 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002038 memcpy(vaddr + frag->page_offset + offset - start,
2039 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002040 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002041
2042 if ((len -= copy) == 0)
2043 return 0;
2044 offset += copy;
2045 from += copy;
2046 }
David S. Miller1a028e52007-04-27 15:21:23 -07002047 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002048 }
2049
David S. Millerfbb398a2009-06-09 00:18:59 -07002050 skb_walk_frags(skb, frag_iter) {
2051 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002052
David S. Millerfbb398a2009-06-09 00:18:59 -07002053 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002054
David S. Millerfbb398a2009-06-09 00:18:59 -07002055 end = start + frag_iter->len;
2056 if ((copy = end - offset) > 0) {
2057 if (copy > len)
2058 copy = len;
2059 if (skb_store_bits(frag_iter, offset - start,
2060 from, copy))
2061 goto fault;
2062 if ((len -= copy) == 0)
2063 return 0;
2064 offset += copy;
2065 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002066 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002067 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002068 }
2069 if (!len)
2070 return 0;
2071
2072fault:
2073 return -EFAULT;
2074}
Herbert Xu357b40a2005-04-19 22:30:14 -07002075EXPORT_SYMBOL(skb_store_bits);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002078__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2079 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
David S. Miller1a028e52007-04-27 15:21:23 -07002081 int start = skb_headlen(skb);
2082 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002083 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int pos = 0;
2085
2086 /* Checksum header. */
2087 if (copy > 0) {
2088 if (copy > len)
2089 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002090 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if ((len -= copy) == 0)
2092 return csum;
2093 offset += copy;
2094 pos = copy;
2095 }
2096
2097 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002098 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002099 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002101 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002102
Eric Dumazet51c56b02012-04-05 11:35:15 +02002103 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002105 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (copy > len)
2109 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002110 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002111 csum2 = ops->update(vaddr + frag->page_offset +
2112 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002113 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002114 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (!(len -= copy))
2116 return csum;
2117 offset += copy;
2118 pos += copy;
2119 }
David S. Miller1a028e52007-04-27 15:21:23 -07002120 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
David S. Millerfbb398a2009-06-09 00:18:59 -07002123 skb_walk_frags(skb, frag_iter) {
2124 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
David S. Millerfbb398a2009-06-09 00:18:59 -07002126 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David S. Millerfbb398a2009-06-09 00:18:59 -07002128 end = start + frag_iter->len;
2129 if ((copy = end - offset) > 0) {
2130 __wsum csum2;
2131 if (copy > len)
2132 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002133 csum2 = __skb_checksum(frag_iter, offset - start,
2134 copy, 0, ops);
2135 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002136 if ((len -= copy) == 0)
2137 return csum;
2138 offset += copy;
2139 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002141 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002143 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 return csum;
2146}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002147EXPORT_SYMBOL(__skb_checksum);
2148
2149__wsum skb_checksum(const struct sk_buff *skb, int offset,
2150 int len, __wsum csum)
2151{
2152 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002153 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002154 .combine = csum_block_add_ext,
2155 };
2156
2157 return __skb_checksum(skb, offset, len, csum, &ops);
2158}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002159EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161/* Both of above in one bottle. */
2162
Al Viro81d77662006-11-14 21:37:33 -08002163__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2164 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
David S. Miller1a028e52007-04-27 15:21:23 -07002166 int start = skb_headlen(skb);
2167 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002168 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int pos = 0;
2170
2171 /* Copy header. */
2172 if (copy > 0) {
2173 if (copy > len)
2174 copy = len;
2175 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2176 copy, csum);
2177 if ((len -= copy) == 0)
2178 return csum;
2179 offset += copy;
2180 to += copy;
2181 pos = copy;
2182 }
2183
2184 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002185 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002187 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002188
Eric Dumazet9e903e02011-10-18 21:00:24 +00002189 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002191 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 u8 *vaddr;
2193 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2194
2195 if (copy > len)
2196 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002197 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002199 frag->page_offset +
2200 offset - start, to,
2201 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002202 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 csum = csum_block_add(csum, csum2, pos);
2204 if (!(len -= copy))
2205 return csum;
2206 offset += copy;
2207 to += copy;
2208 pos += copy;
2209 }
David S. Miller1a028e52007-04-27 15:21:23 -07002210 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
David S. Millerfbb398a2009-06-09 00:18:59 -07002213 skb_walk_frags(skb, frag_iter) {
2214 __wsum csum2;
2215 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
David S. Millerfbb398a2009-06-09 00:18:59 -07002217 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
David S. Millerfbb398a2009-06-09 00:18:59 -07002219 end = start + frag_iter->len;
2220 if ((copy = end - offset) > 0) {
2221 if (copy > len)
2222 copy = len;
2223 csum2 = skb_copy_and_csum_bits(frag_iter,
2224 offset - start,
2225 to, copy, 0);
2226 csum = csum_block_add(csum, csum2, pos);
2227 if ((len -= copy) == 0)
2228 return csum;
2229 offset += copy;
2230 to += copy;
2231 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002233 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002235 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return csum;
2237}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002238EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Davide Caratti96178132017-05-18 15:44:37 +02002240static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2241{
2242 net_warn_ratelimited(
2243 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2244 __func__);
2245 return 0;
2246}
2247
2248static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2249 int offset, int len)
2250{
2251 net_warn_ratelimited(
2252 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2253 __func__);
2254 return 0;
2255}
2256
2257static const struct skb_checksum_ops default_crc32c_ops = {
2258 .update = warn_crc32c_csum_update,
2259 .combine = warn_crc32c_csum_combine,
2260};
2261
2262const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2263 &default_crc32c_ops;
2264EXPORT_SYMBOL(crc32c_csum_stub);
2265
Thomas Grafaf2806f2013-12-13 15:22:17 +01002266 /**
2267 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2268 * @from: source buffer
2269 *
2270 * Calculates the amount of linear headroom needed in the 'to' skb passed
2271 * into skb_zerocopy().
2272 */
2273unsigned int
2274skb_zerocopy_headlen(const struct sk_buff *from)
2275{
2276 unsigned int hlen = 0;
2277
2278 if (!from->head_frag ||
2279 skb_headlen(from) < L1_CACHE_BYTES ||
2280 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2281 hlen = skb_headlen(from);
2282
2283 if (skb_has_frag_list(from))
2284 hlen = from->len;
2285
2286 return hlen;
2287}
2288EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2289
2290/**
2291 * skb_zerocopy - Zero copy skb to skb
2292 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002293 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002294 * @len: number of bytes to copy from source buffer
2295 * @hlen: size of linear headroom in destination buffer
2296 *
2297 * Copies up to `len` bytes from `from` to `to` by creating references
2298 * to the frags in the source buffer.
2299 *
2300 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2301 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002302 *
2303 * Return value:
2304 * 0: everything is OK
2305 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2306 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002307 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002308int
2309skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002310{
2311 int i, j = 0;
2312 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002313 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002314 struct page *page;
2315 unsigned int offset;
2316
2317 BUG_ON(!from->head_frag && !hlen);
2318
2319 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002320 if (len <= skb_tailroom(to))
2321 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002322
2323 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002324 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2325 if (unlikely(ret))
2326 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002327 len -= hlen;
2328 } else {
2329 plen = min_t(int, skb_headlen(from), len);
2330 if (plen) {
2331 page = virt_to_head_page(from->head);
2332 offset = from->data - (unsigned char *)page_address(page);
2333 __skb_fill_page_desc(to, 0, page, offset, plen);
2334 get_page(page);
2335 j = 1;
2336 len -= plen;
2337 }
2338 }
2339
2340 to->truesize += len + plen;
2341 to->len += len + plen;
2342 to->data_len += len + plen;
2343
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002344 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2345 skb_tx_error(from);
2346 return -ENOMEM;
2347 }
2348
Thomas Grafaf2806f2013-12-13 15:22:17 +01002349 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2350 if (!len)
2351 break;
2352 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2353 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2354 len -= skb_shinfo(to)->frags[j].size;
2355 skb_frag_ref(to, j);
2356 j++;
2357 }
2358 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002359
2360 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002361}
2362EXPORT_SYMBOL_GPL(skb_zerocopy);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2365{
Al Virod3bc23e2006-11-14 21:24:49 -08002366 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 long csstart;
2368
Patrick McHardy84fa7932006-08-29 16:44:56 -07002369 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002370 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 else
2372 csstart = skb_headlen(skb);
2373
Kris Katterjohn09a62662006-01-08 22:24:28 -08002374 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002376 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 csum = 0;
2379 if (csstart != skb->len)
2380 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2381 skb->len - csstart, 0);
2382
Patrick McHardy84fa7932006-08-29 16:44:56 -07002383 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002384 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Al Virod3bc23e2006-11-14 21:24:49 -08002386 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002389EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391/**
2392 * skb_dequeue - remove from the head of the queue
2393 * @list: list to dequeue from
2394 *
2395 * Remove the head of the list. The list lock is taken so the function
2396 * may be used safely with other locking list functions. The head item is
2397 * returned or %NULL if the list is empty.
2398 */
2399
2400struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2401{
2402 unsigned long flags;
2403 struct sk_buff *result;
2404
2405 spin_lock_irqsave(&list->lock, flags);
2406 result = __skb_dequeue(list);
2407 spin_unlock_irqrestore(&list->lock, flags);
2408 return result;
2409}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002410EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412/**
2413 * skb_dequeue_tail - remove from the tail of the queue
2414 * @list: list to dequeue from
2415 *
2416 * Remove the tail of the list. The list lock is taken so the function
2417 * may be used safely with other locking list functions. The tail item is
2418 * returned or %NULL if the list is empty.
2419 */
2420struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2421{
2422 unsigned long flags;
2423 struct sk_buff *result;
2424
2425 spin_lock_irqsave(&list->lock, flags);
2426 result = __skb_dequeue_tail(list);
2427 spin_unlock_irqrestore(&list->lock, flags);
2428 return result;
2429}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002430EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432/**
2433 * skb_queue_purge - empty a list
2434 * @list: list to empty
2435 *
2436 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2437 * the list and one reference dropped. This function takes the list
2438 * lock and is atomic with respect to other list locking functions.
2439 */
2440void skb_queue_purge(struct sk_buff_head *list)
2441{
2442 struct sk_buff *skb;
2443 while ((skb = skb_dequeue(list)) != NULL)
2444 kfree_skb(skb);
2445}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002446EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002449 * skb_rbtree_purge - empty a skb rbtree
2450 * @root: root of the rbtree to empty
2451 *
2452 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2453 * the list and one reference dropped. This function does not take
2454 * any lock. Synchronization should be handled by the caller (e.g., TCP
2455 * out-of-order queue is protected by the socket lock).
2456 */
2457void skb_rbtree_purge(struct rb_root *root)
2458{
2459 struct sk_buff *skb, *next;
2460
2461 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2462 kfree_skb(skb);
2463
2464 *root = RB_ROOT;
2465}
2466
2467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * skb_queue_head - queue a buffer at the list head
2469 * @list: list to use
2470 * @newsk: buffer to queue
2471 *
2472 * Queue a buffer at the start of the list. This function takes the
2473 * list lock and can be used safely with other locking &sk_buff functions
2474 * safely.
2475 *
2476 * A buffer cannot be placed on two lists at the same time.
2477 */
2478void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2479{
2480 unsigned long flags;
2481
2482 spin_lock_irqsave(&list->lock, flags);
2483 __skb_queue_head(list, newsk);
2484 spin_unlock_irqrestore(&list->lock, flags);
2485}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002486EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488/**
2489 * skb_queue_tail - queue a buffer at the list tail
2490 * @list: list to use
2491 * @newsk: buffer to queue
2492 *
2493 * Queue a buffer at the tail of the list. This function takes the
2494 * list lock and can be used safely with other locking &sk_buff functions
2495 * safely.
2496 *
2497 * A buffer cannot be placed on two lists at the same time.
2498 */
2499void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2500{
2501 unsigned long flags;
2502
2503 spin_lock_irqsave(&list->lock, flags);
2504 __skb_queue_tail(list, newsk);
2505 spin_unlock_irqrestore(&list->lock, flags);
2506}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002507EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/**
2510 * skb_unlink - remove a buffer from a list
2511 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002512 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
David S. Miller8728b832005-08-09 19:25:21 -07002514 * Remove a packet from a list. The list locks are taken and this
2515 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 *
David S. Miller8728b832005-08-09 19:25:21 -07002517 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
David S. Miller8728b832005-08-09 19:25:21 -07002519void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
David S. Miller8728b832005-08-09 19:25:21 -07002521 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
David S. Miller8728b832005-08-09 19:25:21 -07002523 spin_lock_irqsave(&list->lock, flags);
2524 __skb_unlink(skb, list);
2525 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002527EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529/**
2530 * skb_append - append a buffer
2531 * @old: buffer to insert after
2532 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002533 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 *
2535 * Place a packet after a given packet in a list. The list locks are taken
2536 * and this function is atomic with respect to other list locked calls.
2537 * A buffer cannot be placed on two lists at the same time.
2538 */
David S. Miller8728b832005-08-09 19:25:21 -07002539void skb_append(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);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002544 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002545 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_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549/**
2550 * skb_insert - insert a buffer
2551 * @old: buffer to insert before
2552 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002553 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 *
David S. Miller8728b832005-08-09 19:25:21 -07002555 * Place a packet before a given packet in a list. The list locks are
2556 * taken and this function is atomic with respect to other list locked
2557 * calls.
2558 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 * A buffer cannot be placed on two lists at the same time.
2560 */
David S. Miller8728b832005-08-09 19:25:21 -07002561void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 unsigned long flags;
2564
David S. Miller8728b832005-08-09 19:25:21 -07002565 spin_lock_irqsave(&list->lock, flags);
2566 __skb_insert(newsk, old->prev, old, list);
2567 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002569EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571static inline void skb_split_inside_header(struct sk_buff *skb,
2572 struct sk_buff* skb1,
2573 const u32 len, const int pos)
2574{
2575 int i;
2576
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002577 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2578 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 /* And move data appendix as is. */
2580 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2581 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2582
2583 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2584 skb_shinfo(skb)->nr_frags = 0;
2585 skb1->data_len = skb->data_len;
2586 skb1->len += skb1->data_len;
2587 skb->data_len = 0;
2588 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002589 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592static inline void skb_split_no_header(struct sk_buff *skb,
2593 struct sk_buff* skb1,
2594 const u32 len, int pos)
2595{
2596 int i, k = 0;
2597 const int nfrags = skb_shinfo(skb)->nr_frags;
2598
2599 skb_shinfo(skb)->nr_frags = 0;
2600 skb1->len = skb1->data_len = skb->len - len;
2601 skb->len = len;
2602 skb->data_len = len - pos;
2603
2604 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002605 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 if (pos + size > len) {
2608 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2609
2610 if (pos < len) {
2611 /* Split frag.
2612 * We have two variants in this case:
2613 * 1. Move all the frag to the second
2614 * part, if it is possible. F.e.
2615 * this approach is mandatory for TUX,
2616 * where splitting is expensive.
2617 * 2. Split is accurately. We make this.
2618 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002619 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002621 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2622 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 skb_shinfo(skb)->nr_frags++;
2624 }
2625 k++;
2626 } else
2627 skb_shinfo(skb)->nr_frags++;
2628 pos += size;
2629 }
2630 skb_shinfo(skb1)->nr_frags = k;
2631}
2632
2633/**
2634 * skb_split - Split fragmented skb to two parts at length len.
2635 * @skb: the buffer to split
2636 * @skb1: the buffer to receive the second part
2637 * @len: new length for skb
2638 */
2639void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2640{
2641 int pos = skb_headlen(skb);
2642
Willem de Bruijnfff88032017-06-08 11:35:03 -04002643 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2644 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (len < pos) /* Split line is inside header. */
2646 skb_split_inside_header(skb, skb1, len, pos);
2647 else /* Second chunk has no header, nothing to copy. */
2648 skb_split_no_header(skb, skb1, len, pos);
2649}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002650EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002652/* Shifting from/to a cloned skb is a no-go.
2653 *
2654 * Caller cannot keep skb_shinfo related pointers past calling here!
2655 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002656static int skb_prepare_for_shift(struct sk_buff *skb)
2657{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002658 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002659}
2660
2661/**
2662 * skb_shift - Shifts paged data partially from skb to another
2663 * @tgt: buffer into which tail data gets added
2664 * @skb: buffer from which the paged data comes from
2665 * @shiftlen: shift up to this many bytes
2666 *
2667 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002668 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002669 * It's up to caller to free skb if everything was shifted.
2670 *
2671 * If @tgt runs out of frags, the whole operation is aborted.
2672 *
2673 * Skb cannot include anything else but paged data while tgt is allowed
2674 * to have non-paged data as well.
2675 *
2676 * TODO: full sized shift could be optimized but that would need
2677 * specialized skb free'er to handle frags without up-to-date nr_frags.
2678 */
2679int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2680{
2681 int from, to, merge, todo;
2682 struct skb_frag_struct *fragfrom, *fragto;
2683
2684 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002685
2686 if (skb_headlen(skb))
2687 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002688
2689 todo = shiftlen;
2690 from = 0;
2691 to = skb_shinfo(tgt)->nr_frags;
2692 fragfrom = &skb_shinfo(skb)->frags[from];
2693
2694 /* Actual merge is delayed until the point when we know we can
2695 * commit all, so that we don't have to undo partial changes
2696 */
2697 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002698 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2699 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002700 merge = -1;
2701 } else {
2702 merge = to - 1;
2703
Eric Dumazet9e903e02011-10-18 21:00:24 +00002704 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002705 if (todo < 0) {
2706 if (skb_prepare_for_shift(skb) ||
2707 skb_prepare_for_shift(tgt))
2708 return 0;
2709
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002710 /* All previous frag pointers might be stale! */
2711 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002712 fragto = &skb_shinfo(tgt)->frags[merge];
2713
Eric Dumazet9e903e02011-10-18 21:00:24 +00002714 skb_frag_size_add(fragto, shiftlen);
2715 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002716 fragfrom->page_offset += shiftlen;
2717
2718 goto onlymerged;
2719 }
2720
2721 from++;
2722 }
2723
2724 /* Skip full, not-fitting skb to avoid expensive operations */
2725 if ((shiftlen == skb->len) &&
2726 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2727 return 0;
2728
2729 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2730 return 0;
2731
2732 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2733 if (to == MAX_SKB_FRAGS)
2734 return 0;
2735
2736 fragfrom = &skb_shinfo(skb)->frags[from];
2737 fragto = &skb_shinfo(tgt)->frags[to];
2738
Eric Dumazet9e903e02011-10-18 21:00:24 +00002739 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002740 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002741 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002742 from++;
2743 to++;
2744
2745 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002746 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002747 fragto->page = fragfrom->page;
2748 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002749 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002750
2751 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002752 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002753 todo = 0;
2754
2755 to++;
2756 break;
2757 }
2758 }
2759
2760 /* Ready to "commit" this state change to tgt */
2761 skb_shinfo(tgt)->nr_frags = to;
2762
2763 if (merge >= 0) {
2764 fragfrom = &skb_shinfo(skb)->frags[0];
2765 fragto = &skb_shinfo(tgt)->frags[merge];
2766
Eric Dumazet9e903e02011-10-18 21:00:24 +00002767 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002768 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002769 }
2770
2771 /* Reposition in the original skb */
2772 to = 0;
2773 while (from < skb_shinfo(skb)->nr_frags)
2774 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2775 skb_shinfo(skb)->nr_frags = to;
2776
2777 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2778
2779onlymerged:
2780 /* Most likely the tgt won't ever need its checksum anymore, skb on
2781 * the other hand might need it if it needs to be resent
2782 */
2783 tgt->ip_summed = CHECKSUM_PARTIAL;
2784 skb->ip_summed = CHECKSUM_PARTIAL;
2785
2786 /* Yak, is it really working this way? Some helper please? */
2787 skb->len -= shiftlen;
2788 skb->data_len -= shiftlen;
2789 skb->truesize -= shiftlen;
2790 tgt->len += shiftlen;
2791 tgt->data_len += shiftlen;
2792 tgt->truesize += shiftlen;
2793
2794 return shiftlen;
2795}
2796
Thomas Graf677e90e2005-06-23 20:59:51 -07002797/**
2798 * skb_prepare_seq_read - Prepare a sequential read of skb data
2799 * @skb: the buffer to read
2800 * @from: lower offset of data to be read
2801 * @to: upper offset of data to be read
2802 * @st: state variable
2803 *
2804 * Initializes the specified state variable. Must be called before
2805 * invoking skb_seq_read() for the first time.
2806 */
2807void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2808 unsigned int to, struct skb_seq_state *st)
2809{
2810 st->lower_offset = from;
2811 st->upper_offset = to;
2812 st->root_skb = st->cur_skb = skb;
2813 st->frag_idx = st->stepped_offset = 0;
2814 st->frag_data = NULL;
2815}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002816EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002817
2818/**
2819 * skb_seq_read - Sequentially read skb data
2820 * @consumed: number of bytes consumed by the caller so far
2821 * @data: destination pointer for data to be returned
2822 * @st: state variable
2823 *
Mathias Krausebc323832013-11-07 14:18:26 +01002824 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002825 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002826 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002827 * of the block or 0 if the end of the skb data or the upper
2828 * offset has been reached.
2829 *
2830 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002831 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002832 * of bytes already consumed and the next call to
2833 * skb_seq_read() will return the remaining part of the block.
2834 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002835 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002836 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002837 * reads of potentially non linear data.
2838 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002839 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002840 * at the moment, state->root_skb could be replaced with
2841 * a stack for this purpose.
2842 */
2843unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2844 struct skb_seq_state *st)
2845{
2846 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2847 skb_frag_t *frag;
2848
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002849 if (unlikely(abs_offset >= st->upper_offset)) {
2850 if (st->frag_data) {
2851 kunmap_atomic(st->frag_data);
2852 st->frag_data = NULL;
2853 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002854 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002855 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002856
2857next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002858 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002859
Thomas Chenault995b3372009-05-18 21:43:27 -07002860 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002861 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002862 return block_limit - abs_offset;
2863 }
2864
2865 if (st->frag_idx == 0 && !st->frag_data)
2866 st->stepped_offset += skb_headlen(st->cur_skb);
2867
2868 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2869 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002870 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002871
2872 if (abs_offset < block_limit) {
2873 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002874 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002875
2876 *data = (u8 *) st->frag_data + frag->page_offset +
2877 (abs_offset - st->stepped_offset);
2878
2879 return block_limit - abs_offset;
2880 }
2881
2882 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002883 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002884 st->frag_data = NULL;
2885 }
2886
2887 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002888 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002889 }
2890
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002891 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002892 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002893 st->frag_data = NULL;
2894 }
2895
David S. Miller21dc3302010-08-23 00:13:46 -07002896 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002897 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002898 st->frag_idx = 0;
2899 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002900 } else if (st->cur_skb->next) {
2901 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002902 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002903 goto next_skb;
2904 }
2905
2906 return 0;
2907}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002908EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002909
2910/**
2911 * skb_abort_seq_read - Abort a sequential read of skb data
2912 * @st: state variable
2913 *
2914 * Must be called if skb_seq_read() was not called until it
2915 * returned 0.
2916 */
2917void skb_abort_seq_read(struct skb_seq_state *st)
2918{
2919 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002920 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002921}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002922EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002923
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002924#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2925
2926static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2927 struct ts_config *conf,
2928 struct ts_state *state)
2929{
2930 return skb_seq_read(offset, text, TS_SKB_CB(state));
2931}
2932
2933static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2934{
2935 skb_abort_seq_read(TS_SKB_CB(state));
2936}
2937
2938/**
2939 * skb_find_text - Find a text pattern in skb data
2940 * @skb: the buffer to look in
2941 * @from: search offset
2942 * @to: search limit
2943 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002944 *
2945 * Finds a pattern in the skb data according to the specified
2946 * textsearch configuration. Use textsearch_next() to retrieve
2947 * subsequent occurrences of the pattern. Returns the offset
2948 * to the first occurrence or UINT_MAX if no match was found.
2949 */
2950unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002951 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002952{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002953 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002954 unsigned int ret;
2955
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002956 config->get_next_block = skb_ts_get_next_block;
2957 config->finish = skb_ts_finish;
2958
Bojan Prtvar059a2442015-02-22 11:46:35 +01002959 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002960
Bojan Prtvar059a2442015-02-22 11:46:35 +01002961 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002962 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002964EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002965
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002966/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002967 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002968 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002969 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002970 * @getfrag: call back function to be used for getting the user data
2971 * @from: pointer to user message iov
2972 * @length: length of the iov message
2973 *
2974 * Description: This procedure append the user data in the fragment part
2975 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2976 */
2977int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002978 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002979 int len, int odd, struct sk_buff *skb),
2980 void *from, int length)
2981{
Eric Dumazetb2111722012-12-28 06:06:37 +00002982 int frg_cnt = skb_shinfo(skb)->nr_frags;
2983 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002984 int offset = 0;
2985 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002986 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002987
2988 do {
2989 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002990 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002991 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002992
Eric Dumazetb2111722012-12-28 06:06:37 +00002993 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002994 return -ENOMEM;
2995
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002996 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002997 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002998
Eric Dumazetb2111722012-12-28 06:06:37 +00002999 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3000 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003001 if (ret < 0)
3002 return -EFAULT;
3003
3004 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003005 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3006 copy);
3007 frg_cnt++;
3008 pfrag->offset += copy;
3009 get_page(pfrag->page);
3010
3011 skb->truesize += copy;
3012 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003013 skb->len += copy;
3014 skb->data_len += copy;
3015 offset += copy;
3016 length -= copy;
3017
3018 } while (length > 0);
3019
3020 return 0;
3021}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003022EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003023
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003024int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3025 int offset, size_t size)
3026{
3027 int i = skb_shinfo(skb)->nr_frags;
3028
3029 if (skb_can_coalesce(skb, i, page, offset)) {
3030 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3031 } else if (i < MAX_SKB_FRAGS) {
3032 get_page(page);
3033 skb_fill_page_desc(skb, i, page, offset, size);
3034 } else {
3035 return -EMSGSIZE;
3036 }
3037
3038 return 0;
3039}
3040EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3041
Herbert Xucbb042f2006-03-20 22:43:56 -08003042/**
3043 * skb_pull_rcsum - pull skb and update receive checksum
3044 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003045 * @len: length of data pulled
3046 *
3047 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003048 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003049 * receive path processing instead of skb_pull unless you know
3050 * that the checksum difference is zero (e.g., a valid IP header)
3051 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003052 */
3053unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3054{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003055 unsigned char *data = skb->data;
3056
Herbert Xucbb042f2006-03-20 22:43:56 -08003057 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003058 __skb_pull(skb, len);
3059 skb_postpull_rcsum(skb, data, len);
3060 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003061}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003062EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3063
Herbert Xuf4c50d92006-06-22 03:02:40 -07003064/**
3065 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003066 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003067 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003068 *
3069 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003070 * a pointer to the first in a list of new skbs for the segments.
3071 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003072 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003073struct sk_buff *skb_segment(struct sk_buff *head_skb,
3074 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003075{
3076 struct sk_buff *segs = NULL;
3077 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003078 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003079 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3080 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3081 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003082 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003083 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003084 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003085 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003086 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003087 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003088 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003089 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003090 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003091 int err = -ENOMEM;
3092 int i = 0;
3093 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003094 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003095
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003096 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003097 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003098 if (unlikely(!proto))
3099 return ERR_PTR(-EINVAL);
3100
Alexander Duyck36c98382016-05-02 09:38:18 -07003101 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003102 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003103
Steffen Klassert07b26c92016-09-19 12:58:47 +02003104 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3105 if (!(features & NETIF_F_GSO_PARTIAL)) {
3106 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003107 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003108
3109 if (!list_skb ||
3110 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3111 goto normal;
3112
Ilan Tayari43170c42017-04-19 21:26:07 +03003113 /* If we get here then all the required
3114 * GSO features except frag_list are supported.
3115 * Try to split the SKB to multiple GSO SKBs
3116 * with no frag_list.
3117 * Currently we can do that only when the buffers don't
3118 * have a linear part and all the buffers except
3119 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003120 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003121 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003122 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003123 if (frag_len != iter->len && iter->next)
3124 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003125 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003126 goto normal;
3127
3128 len -= iter->len;
3129 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003130
3131 if (len != frag_len)
3132 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003133 }
3134
3135 /* GSO partial only requires that we trim off any excess that
3136 * doesn't fit into an MSS sized block, so take care of that
3137 * now.
3138 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003139 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003140 if (partial_segs > 1)
3141 mss *= partial_segs;
3142 else
3143 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003144 }
3145
Steffen Klassert07b26c92016-09-19 12:58:47 +02003146normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003147 headroom = skb_headroom(head_skb);
3148 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003149
3150 do {
3151 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003152 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003153 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003154 int size;
3155
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003156 if (unlikely(mss == GSO_BY_FRAGS)) {
3157 len = list_skb->len;
3158 } else {
3159 len = head_skb->len - offset;
3160 if (len > mss)
3161 len = mss;
3162 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003163
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003164 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003165 if (hsize < 0)
3166 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003167 if (hsize > len || !sg)
3168 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003169
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003170 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3171 (skb_headlen(list_skb) == len || sg)) {
3172 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003173
Herbert Xu9d8506c2013-11-21 11:10:04 -08003174 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003175 nfrags = skb_shinfo(list_skb)->nr_frags;
3176 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003177 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003178 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003179
3180 while (pos < offset + len) {
3181 BUG_ON(i >= nfrags);
3182
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003183 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003184 if (pos + size > offset + len)
3185 break;
3186
3187 i++;
3188 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003189 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003190 }
3191
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003192 nskb = skb_clone(list_skb, GFP_ATOMIC);
3193 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003194
3195 if (unlikely(!nskb))
3196 goto err;
3197
Herbert Xu9d8506c2013-11-21 11:10:04 -08003198 if (unlikely(pskb_trim(nskb, len))) {
3199 kfree_skb(nskb);
3200 goto err;
3201 }
3202
Alexander Duyckec47ea82012-05-04 14:26:56 +00003203 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003204 if (skb_cow_head(nskb, doffset + headroom)) {
3205 kfree_skb(nskb);
3206 goto err;
3207 }
3208
Alexander Duyckec47ea82012-05-04 14:26:56 +00003209 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003210 skb_release_head_state(nskb);
3211 __skb_push(nskb, doffset);
3212 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003213 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003214 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003215 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003216
3217 if (unlikely(!nskb))
3218 goto err;
3219
3220 skb_reserve(nskb, headroom);
3221 __skb_put(nskb, doffset);
3222 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003223
3224 if (segs)
3225 tail->next = nskb;
3226 else
3227 segs = nskb;
3228 tail = nskb;
3229
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003230 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003231
Eric Dumazet030737b2013-10-19 11:42:54 -07003232 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003233 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003234
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003235 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003236 nskb->data - tnl_hlen,
3237 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003238
Herbert Xu9d8506c2013-11-21 11:10:04 -08003239 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003240 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003241
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003242 if (!sg) {
3243 if (!nskb->remcsum_offload)
3244 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003245 SKB_GSO_CB(nskb)->csum =
3246 skb_copy_and_csum_bits(head_skb, offset,
3247 skb_put(nskb, len),
3248 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003249 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003250 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003251 continue;
3252 }
3253
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003254 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003255
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003256 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003257 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003258
Willem de Bruijnfff88032017-06-08 11:35:03 -04003259 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3260 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003261
Herbert Xu9d8506c2013-11-21 11:10:04 -08003262 while (pos < offset + len) {
3263 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003264 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003265
3266 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003267 nfrags = skb_shinfo(list_skb)->nr_frags;
3268 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003269 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003270
3271 BUG_ON(!nfrags);
3272
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003273 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003274 }
3275
3276 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3277 MAX_SKB_FRAGS)) {
3278 net_warn_ratelimited(
3279 "skb_segment: too many frags: %u %u\n",
3280 pos, mss);
3281 goto err;
3282 }
3283
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003284 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3285 goto err;
3286
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003287 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003288 __skb_frag_ref(nskb_frag);
3289 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003290
3291 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003292 nskb_frag->page_offset += offset - pos;
3293 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003294 }
3295
Herbert Xu89319d382008-12-15 23:26:06 -08003296 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003297
3298 if (pos + size <= offset + len) {
3299 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003300 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003301 pos += size;
3302 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003303 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003304 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003305 }
3306
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003307 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003308 }
3309
Herbert Xu89319d382008-12-15 23:26:06 -08003310skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003311 nskb->data_len = len - hsize;
3312 nskb->len += nskb->data_len;
3313 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003314
Simon Horman1cdbcb72013-05-19 15:46:49 +00003315perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003316 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003317 if (skb_has_shared_frag(nskb)) {
3318 err = __skb_linearize(nskb);
3319 if (err)
3320 goto err;
3321 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003322 if (!nskb->remcsum_offload)
3323 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003324 SKB_GSO_CB(nskb)->csum =
3325 skb_checksum(nskb, doffset,
3326 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003327 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003328 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003329 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003330 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003331
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003332 /* Some callers want to get the end of the list.
3333 * Put it in segs->prev to avoid walking the list.
3334 * (see validate_xmit_skb_list() for example)
3335 */
3336 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003337
Alexander Duyck802ab552016-04-10 21:45:03 -04003338 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003339 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003340 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003341 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003342
3343 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003344 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003345 type &= ~SKB_GSO_DODGY;
3346
3347 /* Update GSO info and prepare to start updating headers on
3348 * our way back down the stack of protocols.
3349 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003350 for (iter = segs; iter; iter = iter->next) {
3351 skb_shinfo(iter)->gso_size = gso_size;
3352 skb_shinfo(iter)->gso_segs = partial_segs;
3353 skb_shinfo(iter)->gso_type = type;
3354 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3355 }
3356
3357 if (tail->len - doffset <= gso_size)
3358 skb_shinfo(tail)->gso_size = 0;
3359 else if (tail != segs)
3360 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003361 }
3362
Toshiaki Makita432c8562014-10-27 10:30:51 -07003363 /* Following permits correct backpressure, for protocols
3364 * using skb_set_owner_w().
3365 * Idea is to tranfert ownership from head_skb to last segment.
3366 */
3367 if (head_skb->destructor == sock_wfree) {
3368 swap(tail->truesize, head_skb->truesize);
3369 swap(tail->destructor, head_skb->destructor);
3370 swap(tail->sk, head_skb->sk);
3371 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003372 return segs;
3373
3374err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003375 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003376 return ERR_PTR(err);
3377}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003378EXPORT_SYMBOL_GPL(skb_segment);
3379
Herbert Xu71d93b32008-12-15 23:42:33 -08003380int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3381{
Eric Dumazet8a291112013-10-08 09:02:23 -07003382 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003383 unsigned int offset = skb_gro_offset(skb);
3384 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003385 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003386 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003387 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003388
Eric Dumazet8a291112013-10-08 09:02:23 -07003389 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003390 return -E2BIG;
3391
Eric Dumazet29e98242014-05-16 11:34:37 -07003392 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003393 pinfo = skb_shinfo(lp);
3394
3395 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003396 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003397 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003398 int i = skbinfo->nr_frags;
3399 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003400
Herbert Xu66e92fc2009-05-26 18:50:32 +00003401 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003402 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003403
Eric Dumazet8a291112013-10-08 09:02:23 -07003404 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003405 pinfo->nr_frags = nr_frags;
3406 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003407
Herbert Xu9aaa1562009-05-26 18:50:33 +00003408 frag = pinfo->frags + nr_frags;
3409 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003410 do {
3411 *--frag = *--frag2;
3412 } while (--i);
3413
3414 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003415 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003416
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003417 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003418 delta_truesize = skb->truesize -
3419 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003420
Herbert Xuf5572062009-01-14 20:40:03 -08003421 skb->truesize -= skb->data_len;
3422 skb->len -= skb->data_len;
3423 skb->data_len = 0;
3424
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003425 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003426 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003427 } else if (skb->head_frag) {
3428 int nr_frags = pinfo->nr_frags;
3429 skb_frag_t *frag = pinfo->frags + nr_frags;
3430 struct page *page = virt_to_head_page(skb->head);
3431 unsigned int first_size = headlen - offset;
3432 unsigned int first_offset;
3433
3434 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003435 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003436
3437 first_offset = skb->data -
3438 (unsigned char *)page_address(page) +
3439 offset;
3440
3441 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3442
3443 frag->page.p = page;
3444 frag->page_offset = first_offset;
3445 skb_frag_size_set(frag, first_size);
3446
3447 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3448 /* We dont need to clear skbinfo->nr_frags here */
3449
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003450 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003451 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3452 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003453 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003454
3455merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003456 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003457 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003458 unsigned int eat = offset - headlen;
3459
3460 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003461 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003462 skb->data_len -= eat;
3463 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003464 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003465 }
3466
Herbert Xu67147ba2009-05-26 18:50:22 +00003467 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003468
Eric Dumazet29e98242014-05-16 11:34:37 -07003469 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003470 skb_shinfo(p)->frag_list = skb;
3471 else
3472 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003473 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003474 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003475 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003476
Herbert Xu5d38a072009-01-04 16:13:40 -08003477done:
3478 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003479 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003480 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003481 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003482 if (lp != p) {
3483 lp->data_len += len;
3484 lp->truesize += delta_truesize;
3485 lp->len += len;
3486 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003487 NAPI_GRO_CB(skb)->same_flow = 1;
3488 return 0;
3489}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003490EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492void __init skb_init(void)
3493{
3494 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3495 sizeof(struct sk_buff),
3496 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003497 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003498 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003499 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003500 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003501 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003502 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003503 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
David S. Miller51c739d2007-10-30 21:29:29 -07003506static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003507__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3508 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003509{
David S. Miller1a028e52007-04-27 15:21:23 -07003510 int start = skb_headlen(skb);
3511 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003512 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003513 int elt = 0;
3514
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003515 if (unlikely(recursion_level >= 24))
3516 return -EMSGSIZE;
3517
David Howells716ea3a2007-04-02 20:19:53 -07003518 if (copy > 0) {
3519 if (copy > len)
3520 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003521 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003522 elt++;
3523 if ((len -= copy) == 0)
3524 return elt;
3525 offset += copy;
3526 }
3527
3528 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003529 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003530
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003531 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003532
Eric Dumazet9e903e02011-10-18 21:00:24 +00003533 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003534 if ((copy = end - offset) > 0) {
3535 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003536 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3537 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003538
3539 if (copy > len)
3540 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003541 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003542 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003543 elt++;
3544 if (!(len -= copy))
3545 return elt;
3546 offset += copy;
3547 }
David S. Miller1a028e52007-04-27 15:21:23 -07003548 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003549 }
3550
David S. Millerfbb398a2009-06-09 00:18:59 -07003551 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003552 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003553
David S. Millerfbb398a2009-06-09 00:18:59 -07003554 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003555
David S. Millerfbb398a2009-06-09 00:18:59 -07003556 end = start + frag_iter->len;
3557 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003558 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3559 return -EMSGSIZE;
3560
David S. Millerfbb398a2009-06-09 00:18:59 -07003561 if (copy > len)
3562 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003563 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3564 copy, recursion_level + 1);
3565 if (unlikely(ret < 0))
3566 return ret;
3567 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003568 if ((len -= copy) == 0)
3569 return elt;
3570 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003571 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003572 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003573 }
3574 BUG_ON(len);
3575 return elt;
3576}
3577
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003578/**
3579 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3580 * @skb: Socket buffer containing the buffers to be mapped
3581 * @sg: The scatter-gather list to map into
3582 * @offset: The offset into the buffer's contents to start mapping
3583 * @len: Length of buffer space to be mapped
3584 *
3585 * Fill the specified scatter-gather list with mappings/pointers into a
3586 * region of the buffer space attached to a socket buffer. Returns either
3587 * the number of scatterlist items used, or -EMSGSIZE if the contents
3588 * could not fit.
3589 */
3590int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3591{
3592 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3593
3594 if (nsg <= 0)
3595 return nsg;
3596
3597 sg_mark_end(&sg[nsg - 1]);
3598
3599 return nsg;
3600}
3601EXPORT_SYMBOL_GPL(skb_to_sgvec);
3602
Fan Du25a91d82014-01-18 09:54:23 +08003603/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3604 * sglist without mark the sg which contain last skb data as the end.
3605 * So the caller can mannipulate sg list as will when padding new data after
3606 * the first call without calling sg_unmark_end to expend sg list.
3607 *
3608 * Scenario to use skb_to_sgvec_nomark:
3609 * 1. sg_init_table
3610 * 2. skb_to_sgvec_nomark(payload1)
3611 * 3. skb_to_sgvec_nomark(payload2)
3612 *
3613 * This is equivalent to:
3614 * 1. sg_init_table
3615 * 2. skb_to_sgvec(payload1)
3616 * 3. sg_unmark_end
3617 * 4. skb_to_sgvec(payload2)
3618 *
3619 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3620 * is more preferable.
3621 */
3622int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3623 int offset, int len)
3624{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003625 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003626}
3627EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3628
David S. Miller51c739d2007-10-30 21:29:29 -07003629
David S. Miller51c739d2007-10-30 21:29:29 -07003630
David Howells716ea3a2007-04-02 20:19:53 -07003631/**
3632 * skb_cow_data - Check that a socket buffer's data buffers are writable
3633 * @skb: The socket buffer to check.
3634 * @tailbits: Amount of trailing space to be added
3635 * @trailer: Returned pointer to the skb where the @tailbits space begins
3636 *
3637 * Make sure that the data buffers attached to a socket buffer are
3638 * writable. If they are not, private copies are made of the data buffers
3639 * and the socket buffer is set to use these instead.
3640 *
3641 * If @tailbits is given, make sure that there is space to write @tailbits
3642 * bytes of data beyond current end of socket buffer. @trailer will be
3643 * set to point to the skb in which this space begins.
3644 *
3645 * The number of scatterlist elements required to completely map the
3646 * COW'd and extended socket buffer will be returned.
3647 */
3648int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3649{
3650 int copyflag;
3651 int elt;
3652 struct sk_buff *skb1, **skb_p;
3653
3654 /* If skb is cloned or its head is paged, reallocate
3655 * head pulling out all the pages (pages are considered not writable
3656 * at the moment even if they are anonymous).
3657 */
3658 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3659 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3660 return -ENOMEM;
3661
3662 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003663 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003664 /* A little of trouble, not enough of space for trailer.
3665 * This should not happen, when stack is tuned to generate
3666 * good frames. OK, on miss we reallocate and reserve even more
3667 * space, 128 bytes is fair. */
3668
3669 if (skb_tailroom(skb) < tailbits &&
3670 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3671 return -ENOMEM;
3672
3673 /* Voila! */
3674 *trailer = skb;
3675 return 1;
3676 }
3677
3678 /* Misery. We are in troubles, going to mincer fragments... */
3679
3680 elt = 1;
3681 skb_p = &skb_shinfo(skb)->frag_list;
3682 copyflag = 0;
3683
3684 while ((skb1 = *skb_p) != NULL) {
3685 int ntail = 0;
3686
3687 /* The fragment is partially pulled by someone,
3688 * this can happen on input. Copy it and everything
3689 * after it. */
3690
3691 if (skb_shared(skb1))
3692 copyflag = 1;
3693
3694 /* If the skb is the last, worry about trailer. */
3695
3696 if (skb1->next == NULL && tailbits) {
3697 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003698 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003699 skb_tailroom(skb1) < tailbits)
3700 ntail = tailbits + 128;
3701 }
3702
3703 if (copyflag ||
3704 skb_cloned(skb1) ||
3705 ntail ||
3706 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003707 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003708 struct sk_buff *skb2;
3709
3710 /* Fuck, we are miserable poor guys... */
3711 if (ntail == 0)
3712 skb2 = skb_copy(skb1, GFP_ATOMIC);
3713 else
3714 skb2 = skb_copy_expand(skb1,
3715 skb_headroom(skb1),
3716 ntail,
3717 GFP_ATOMIC);
3718 if (unlikely(skb2 == NULL))
3719 return -ENOMEM;
3720
3721 if (skb1->sk)
3722 skb_set_owner_w(skb2, skb1->sk);
3723
3724 /* Looking around. Are we still alive?
3725 * OK, link new skb, drop old one */
3726
3727 skb2->next = skb1->next;
3728 *skb_p = skb2;
3729 kfree_skb(skb1);
3730 skb1 = skb2;
3731 }
3732 elt++;
3733 *trailer = skb1;
3734 skb_p = &skb1->next;
3735 }
3736
3737 return elt;
3738}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003739EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003740
Eric Dumazetb1faf562010-05-31 23:44:05 -07003741static void sock_rmem_free(struct sk_buff *skb)
3742{
3743 struct sock *sk = skb->sk;
3744
3745 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3746}
3747
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003748static void skb_set_err_queue(struct sk_buff *skb)
3749{
3750 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3751 * So, it is safe to (mis)use it to mark skbs on the error queue.
3752 */
3753 skb->pkt_type = PACKET_OUTGOING;
3754 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3755}
3756
Eric Dumazetb1faf562010-05-31 23:44:05 -07003757/*
3758 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3759 */
3760int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3761{
3762 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003763 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003764 return -ENOMEM;
3765
3766 skb_orphan(skb);
3767 skb->sk = sk;
3768 skb->destructor = sock_rmem_free;
3769 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003770 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003771
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003772 /* before exiting rcu section, make sure dst is refcounted */
3773 skb_dst_force(skb);
3774
Eric Dumazetb1faf562010-05-31 23:44:05 -07003775 skb_queue_tail(&sk->sk_error_queue, skb);
3776 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003777 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003778 return 0;
3779}
3780EXPORT_SYMBOL(sock_queue_err_skb);
3781
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003782static bool is_icmp_err_skb(const struct sk_buff *skb)
3783{
3784 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3785 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3786}
3787
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003788struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3789{
3790 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003791 struct sk_buff *skb, *skb_next = NULL;
3792 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003793 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003794
Eric Dumazet997d5c32015-02-18 05:47:55 -08003795 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003796 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003797 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003798 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003799 if (icmp_next)
3800 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3801 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08003802 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003803
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003804 if (is_icmp_err_skb(skb) && !icmp_next)
3805 sk->sk_err = 0;
3806
3807 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003808 sk->sk_error_report(sk);
3809
3810 return skb;
3811}
3812EXPORT_SYMBOL(sock_dequeue_err_skb);
3813
Alexander Duyckcab41c42014-09-10 18:05:26 -04003814/**
3815 * skb_clone_sk - create clone of skb, and take reference to socket
3816 * @skb: the skb to clone
3817 *
3818 * This function creates a clone of a buffer that holds a reference on
3819 * sk_refcnt. Buffers created via this function are meant to be
3820 * returned using sock_queue_err_skb, or free via kfree_skb.
3821 *
3822 * When passing buffers allocated with this function to sock_queue_err_skb
3823 * it is necessary to wrap the call with sock_hold/sock_put in order to
3824 * prevent the socket from being released prior to being enqueued on
3825 * the sk_error_queue.
3826 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003827struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3828{
3829 struct sock *sk = skb->sk;
3830 struct sk_buff *clone;
3831
3832 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3833 return NULL;
3834
3835 clone = skb_clone(skb, GFP_ATOMIC);
3836 if (!clone) {
3837 sock_put(sk);
3838 return NULL;
3839 }
3840
3841 clone->sk = sk;
3842 clone->destructor = sock_efree;
3843
3844 return clone;
3845}
3846EXPORT_SYMBOL(skb_clone_sk);
3847
Alexander Duyck37846ef2014-09-04 13:31:10 -04003848static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3849 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003850 int tstype,
3851 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003852{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003853 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003854 int err;
3855
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003856 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
3857
Patrick Ohlyac45f602009-02-12 05:03:37 +00003858 serr = SKB_EXT_ERR(skb);
3859 memset(serr, 0, sizeof(*serr));
3860 serr->ee.ee_errno = ENOMSG;
3861 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003862 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003863 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04003864 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003865 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003866 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003867 if (sk->sk_protocol == IPPROTO_TCP &&
3868 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003869 serr->ee.ee_data -= sk->sk_tskey;
3870 }
Eric Dumazet29030372010-05-29 00:20:48 -07003871
Patrick Ohlyac45f602009-02-12 05:03:37 +00003872 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003873
Patrick Ohlyac45f602009-02-12 05:03:37 +00003874 if (err)
3875 kfree_skb(skb);
3876}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003877
Willem de Bruijnb245be12015-01-30 13:29:32 -05003878static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3879{
3880 bool ret;
3881
3882 if (likely(sysctl_tstamp_allow_data || tsonly))
3883 return true;
3884
3885 read_lock_bh(&sk->sk_callback_lock);
3886 ret = sk->sk_socket && sk->sk_socket->file &&
3887 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3888 read_unlock_bh(&sk->sk_callback_lock);
3889 return ret;
3890}
3891
Alexander Duyck37846ef2014-09-04 13:31:10 -04003892void skb_complete_tx_timestamp(struct sk_buff *skb,
3893 struct skb_shared_hwtstamps *hwtstamps)
3894{
3895 struct sock *sk = skb->sk;
3896
Willem de Bruijnb245be12015-01-30 13:29:32 -05003897 if (!skb_may_tx_timestamp(sk, false))
3898 return;
3899
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003900 /* Take a reference to prevent skb_orphan() from freeing the socket,
3901 * but only if the socket refcount is not zero.
3902 */
3903 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3904 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003905 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003906 sock_put(sk);
3907 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003908}
3909EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3910
3911void __skb_tstamp_tx(struct sk_buff *orig_skb,
3912 struct skb_shared_hwtstamps *hwtstamps,
3913 struct sock *sk, int tstype)
3914{
3915 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003916 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003917
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003918 if (!sk)
3919 return;
3920
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02003921 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
3922 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
3923 return;
3924
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003925 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3926 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003927 return;
3928
Francis Yan1c885802016-11-27 23:07:18 -08003929 if (tsonly) {
3930#ifdef CONFIG_INET
3931 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
3932 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003933 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08003934 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003935 opt_stats = true;
3936 } else
Francis Yan1c885802016-11-27 23:07:18 -08003937#endif
3938 skb = alloc_skb(0, GFP_ATOMIC);
3939 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003940 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08003941 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003942 if (!skb)
3943 return;
3944
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003945 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04003946 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3947 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003948 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3949 }
3950
3951 if (hwtstamps)
3952 *skb_hwtstamps(skb) = *hwtstamps;
3953 else
3954 skb->tstamp = ktime_get_real();
3955
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003956 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003957}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003958EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3959
3960void skb_tstamp_tx(struct sk_buff *orig_skb,
3961 struct skb_shared_hwtstamps *hwtstamps)
3962{
3963 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3964 SCM_TSTAMP_SND);
3965}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003966EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3967
Johannes Berg6e3e9392011-11-09 10:15:42 +01003968void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3969{
3970 struct sock *sk = skb->sk;
3971 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003972 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01003973
3974 skb->wifi_acked_valid = 1;
3975 skb->wifi_acked = acked;
3976
3977 serr = SKB_EXT_ERR(skb);
3978 memset(serr, 0, sizeof(*serr));
3979 serr->ee.ee_errno = ENOMSG;
3980 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3981
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003982 /* Take a reference to prevent skb_orphan() from freeing the socket,
3983 * but only if the socket refcount is not zero.
3984 */
3985 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3986 err = sock_queue_err_skb(sk, skb);
3987 sock_put(sk);
3988 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01003989 if (err)
3990 kfree_skb(skb);
3991}
3992EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3993
Rusty Russellf35d9d82008-02-04 23:49:54 -05003994/**
3995 * skb_partial_csum_set - set up and verify partial csum values for packet
3996 * @skb: the skb to set
3997 * @start: the number of bytes after skb->data to start checksumming.
3998 * @off: the offset from start to place the checksum.
3999 *
4000 * For untrusted partially-checksummed packets, we need to make sure the values
4001 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4002 *
4003 * This function checks and sets those values and skb->ip_summed: if this
4004 * returns false you should drop the packet.
4005 */
4006bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4007{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004008 if (unlikely(start > skb_headlen(skb)) ||
4009 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004010 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4011 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004012 return false;
4013 }
4014 skb->ip_summed = CHECKSUM_PARTIAL;
4015 skb->csum_start = skb_headroom(skb) + start;
4016 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004017 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004018 return true;
4019}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004020EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004021
Paul Durranted1f50c2014-01-09 10:02:46 +00004022static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4023 unsigned int max)
4024{
4025 if (skb_headlen(skb) >= len)
4026 return 0;
4027
4028 /* If we need to pullup then pullup to the max, so we
4029 * won't need to do it again.
4030 */
4031 if (max > skb->len)
4032 max = skb->len;
4033
4034 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4035 return -ENOMEM;
4036
4037 if (skb_headlen(skb) < len)
4038 return -EPROTO;
4039
4040 return 0;
4041}
4042
Jan Beulichf9708b42014-03-11 13:56:05 +00004043#define MAX_TCP_HDR_LEN (15 * 4)
4044
4045static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4046 typeof(IPPROTO_IP) proto,
4047 unsigned int off)
4048{
4049 switch (proto) {
4050 int err;
4051
4052 case IPPROTO_TCP:
4053 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4054 off + MAX_TCP_HDR_LEN);
4055 if (!err && !skb_partial_csum_set(skb, off,
4056 offsetof(struct tcphdr,
4057 check)))
4058 err = -EPROTO;
4059 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4060
4061 case IPPROTO_UDP:
4062 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4063 off + sizeof(struct udphdr));
4064 if (!err && !skb_partial_csum_set(skb, off,
4065 offsetof(struct udphdr,
4066 check)))
4067 err = -EPROTO;
4068 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4069 }
4070
4071 return ERR_PTR(-EPROTO);
4072}
4073
Paul Durranted1f50c2014-01-09 10:02:46 +00004074/* This value should be large enough to cover a tagged ethernet header plus
4075 * maximally sized IP and TCP or UDP headers.
4076 */
4077#define MAX_IP_HDR_LEN 128
4078
Jan Beulichf9708b42014-03-11 13:56:05 +00004079static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004080{
4081 unsigned int off;
4082 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004083 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004084 int err;
4085
4086 fragment = false;
4087
4088 err = skb_maybe_pull_tail(skb,
4089 sizeof(struct iphdr),
4090 MAX_IP_HDR_LEN);
4091 if (err < 0)
4092 goto out;
4093
4094 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4095 fragment = true;
4096
4097 off = ip_hdrlen(skb);
4098
4099 err = -EPROTO;
4100
4101 if (fragment)
4102 goto out;
4103
Jan Beulichf9708b42014-03-11 13:56:05 +00004104 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4105 if (IS_ERR(csum))
4106 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004107
Jan Beulichf9708b42014-03-11 13:56:05 +00004108 if (recalculate)
4109 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4110 ip_hdr(skb)->daddr,
4111 skb->len - off,
4112 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004113 err = 0;
4114
4115out:
4116 return err;
4117}
4118
4119/* This value should be large enough to cover a tagged ethernet header plus
4120 * an IPv6 header, all options, and a maximal TCP or UDP header.
4121 */
4122#define MAX_IPV6_HDR_LEN 256
4123
4124#define OPT_HDR(type, skb, off) \
4125 (type *)(skb_network_header(skb) + (off))
4126
4127static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4128{
4129 int err;
4130 u8 nexthdr;
4131 unsigned int off;
4132 unsigned int len;
4133 bool fragment;
4134 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004135 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004136
4137 fragment = false;
4138 done = false;
4139
4140 off = sizeof(struct ipv6hdr);
4141
4142 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4143 if (err < 0)
4144 goto out;
4145
4146 nexthdr = ipv6_hdr(skb)->nexthdr;
4147
4148 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4149 while (off <= len && !done) {
4150 switch (nexthdr) {
4151 case IPPROTO_DSTOPTS:
4152 case IPPROTO_HOPOPTS:
4153 case IPPROTO_ROUTING: {
4154 struct ipv6_opt_hdr *hp;
4155
4156 err = skb_maybe_pull_tail(skb,
4157 off +
4158 sizeof(struct ipv6_opt_hdr),
4159 MAX_IPV6_HDR_LEN);
4160 if (err < 0)
4161 goto out;
4162
4163 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4164 nexthdr = hp->nexthdr;
4165 off += ipv6_optlen(hp);
4166 break;
4167 }
4168 case IPPROTO_AH: {
4169 struct ip_auth_hdr *hp;
4170
4171 err = skb_maybe_pull_tail(skb,
4172 off +
4173 sizeof(struct ip_auth_hdr),
4174 MAX_IPV6_HDR_LEN);
4175 if (err < 0)
4176 goto out;
4177
4178 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4179 nexthdr = hp->nexthdr;
4180 off += ipv6_authlen(hp);
4181 break;
4182 }
4183 case IPPROTO_FRAGMENT: {
4184 struct frag_hdr *hp;
4185
4186 err = skb_maybe_pull_tail(skb,
4187 off +
4188 sizeof(struct frag_hdr),
4189 MAX_IPV6_HDR_LEN);
4190 if (err < 0)
4191 goto out;
4192
4193 hp = OPT_HDR(struct frag_hdr, skb, off);
4194
4195 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4196 fragment = true;
4197
4198 nexthdr = hp->nexthdr;
4199 off += sizeof(struct frag_hdr);
4200 break;
4201 }
4202 default:
4203 done = true;
4204 break;
4205 }
4206 }
4207
4208 err = -EPROTO;
4209
4210 if (!done || fragment)
4211 goto out;
4212
Jan Beulichf9708b42014-03-11 13:56:05 +00004213 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4214 if (IS_ERR(csum))
4215 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004216
Jan Beulichf9708b42014-03-11 13:56:05 +00004217 if (recalculate)
4218 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4219 &ipv6_hdr(skb)->daddr,
4220 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004221 err = 0;
4222
4223out:
4224 return err;
4225}
4226
4227/**
4228 * skb_checksum_setup - set up partial checksum offset
4229 * @skb: the skb to set up
4230 * @recalculate: if true the pseudo-header checksum will be recalculated
4231 */
4232int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4233{
4234 int err;
4235
4236 switch (skb->protocol) {
4237 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004238 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004239 break;
4240
4241 case htons(ETH_P_IPV6):
4242 err = skb_checksum_setup_ipv6(skb, recalculate);
4243 break;
4244
4245 default:
4246 err = -EPROTO;
4247 break;
4248 }
4249
4250 return err;
4251}
4252EXPORT_SYMBOL(skb_checksum_setup);
4253
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004254/**
4255 * skb_checksum_maybe_trim - maybe trims the given skb
4256 * @skb: the skb to check
4257 * @transport_len: the data length beyond the network header
4258 *
4259 * Checks whether the given skb has data beyond the given transport length.
4260 * If so, returns a cloned skb trimmed to this transport length.
4261 * Otherwise returns the provided skb. Returns NULL in error cases
4262 * (e.g. transport_len exceeds skb length or out-of-memory).
4263 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004264 * Caller needs to set the skb transport header and free any returned skb if it
4265 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004266 */
4267static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4268 unsigned int transport_len)
4269{
4270 struct sk_buff *skb_chk;
4271 unsigned int len = skb_transport_offset(skb) + transport_len;
4272 int ret;
4273
Linus Lüssinga5169932015-08-13 05:54:07 +02004274 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004275 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004276 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004277 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004278
4279 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004280 if (!skb_chk)
4281 return NULL;
4282
4283 ret = pskb_trim_rcsum(skb_chk, len);
4284 if (ret) {
4285 kfree_skb(skb_chk);
4286 return NULL;
4287 }
4288
4289 return skb_chk;
4290}
4291
4292/**
4293 * skb_checksum_trimmed - validate checksum of an skb
4294 * @skb: the skb to check
4295 * @transport_len: the data length beyond the network header
4296 * @skb_chkf: checksum function to use
4297 *
4298 * Applies the given checksum function skb_chkf to the provided skb.
4299 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4300 *
4301 * If the skb has data beyond the given transport length, then a
4302 * trimmed & cloned skb is checked and returned.
4303 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004304 * Caller needs to set the skb transport header and free any returned skb if it
4305 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004306 */
4307struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4308 unsigned int transport_len,
4309 __sum16(*skb_chkf)(struct sk_buff *skb))
4310{
4311 struct sk_buff *skb_chk;
4312 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004313 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004314
4315 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4316 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004317 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004318
Linus Lüssinga5169932015-08-13 05:54:07 +02004319 if (!pskb_may_pull(skb_chk, offset))
4320 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004321
Linus Lüssing9b368812016-02-24 04:21:42 +01004322 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004323 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004324 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004325
Linus Lüssinga5169932015-08-13 05:54:07 +02004326 if (ret)
4327 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004328
4329 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004330
4331err:
4332 if (skb_chk && skb_chk != skb)
4333 kfree_skb(skb_chk);
4334
4335 return NULL;
4336
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004337}
4338EXPORT_SYMBOL(skb_checksum_trimmed);
4339
Ben Hutchings4497b072008-06-19 16:22:28 -07004340void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4341{
Joe Perchese87cc472012-05-13 21:56:26 +00004342 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4343 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004344}
Ben Hutchings4497b072008-06-19 16:22:28 -07004345EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004346
4347void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4348{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004349 if (head_stolen) {
4350 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004351 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004352 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004353 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004354 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004355}
4356EXPORT_SYMBOL(kfree_skb_partial);
4357
4358/**
4359 * skb_try_coalesce - try to merge skb to prior one
4360 * @to: prior buffer
4361 * @from: buffer to add
4362 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004363 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004364 */
4365bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4366 bool *fragstolen, int *delta_truesize)
4367{
4368 int i, delta, len = from->len;
4369
4370 *fragstolen = false;
4371
4372 if (skb_cloned(to))
4373 return false;
4374
4375 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004376 if (len)
4377 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004378 *delta_truesize = 0;
4379 return true;
4380 }
4381
4382 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4383 return false;
4384
4385 if (skb_headlen(from) != 0) {
4386 struct page *page;
4387 unsigned int offset;
4388
4389 if (skb_shinfo(to)->nr_frags +
4390 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4391 return false;
4392
4393 if (skb_head_is_locked(from))
4394 return false;
4395
4396 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4397
4398 page = virt_to_head_page(from->head);
4399 offset = from->data - (unsigned char *)page_address(page);
4400
4401 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4402 page, offset, skb_headlen(from));
4403 *fragstolen = true;
4404 } else {
4405 if (skb_shinfo(to)->nr_frags +
4406 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4407 return false;
4408
Weiping Panf4b549a2012-09-28 20:15:30 +00004409 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004410 }
4411
4412 WARN_ON_ONCE(delta < len);
4413
4414 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4415 skb_shinfo(from)->frags,
4416 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4417 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4418
4419 if (!skb_cloned(from))
4420 skb_shinfo(from)->nr_frags = 0;
4421
Li RongQing8ea853f2012-09-18 16:53:21 +00004422 /* if the skb is not cloned this does nothing
4423 * since we set nr_frags to 0.
4424 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004425 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4426 skb_frag_ref(from, i);
4427
4428 to->truesize += delta;
4429 to->len += len;
4430 to->data_len += len;
4431
4432 *delta_truesize = delta;
4433 return true;
4434}
4435EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004436
4437/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004438 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004439 *
4440 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004441 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004442 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004443 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4444 * into/from a tunnel. Some information have to be cleared during these
4445 * operations.
4446 * skb_scrub_packet can also be used to clean a skb before injecting it in
4447 * another namespace (@xnet == true). We have to clear all information in the
4448 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004449 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004450void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004451{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004452 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004453 skb->pkt_type = PACKET_HOST;
4454 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004455 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004456 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004457 secpath_reset(skb);
4458 nf_reset(skb);
4459 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004460
4461 if (!xnet)
4462 return;
4463
4464 skb_orphan(skb);
4465 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004466}
4467EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004468
4469/**
4470 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4471 *
4472 * @skb: GSO skb
4473 *
4474 * skb_gso_transport_seglen is used to determine the real size of the
4475 * individual segments, including Layer4 headers (TCP/UDP).
4476 *
4477 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4478 */
4479unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4480{
4481 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004482 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004483
Florian Westphalf993bc22014-10-20 13:49:18 +02004484 if (skb->encapsulation) {
4485 thlen = skb_inner_transport_header(skb) -
4486 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004487
Florian Westphalf993bc22014-10-20 13:49:18 +02004488 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4489 thlen += inner_tcp_hdrlen(skb);
4490 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4491 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004492 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4493 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004494 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004495 /* UFO sets gso_size to the size of the fragmentation
4496 * payload, i.e. the size of the L4 (UDP) header is already
4497 * accounted for.
4498 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004499 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004500}
4501EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004502
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004503/**
4504 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4505 *
4506 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004507 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004508 *
4509 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4510 * once split.
4511 */
4512bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4513{
4514 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4515 const struct sk_buff *iter;
4516 unsigned int hlen;
4517
4518 hlen = skb_gso_network_seglen(skb);
4519
4520 if (shinfo->gso_size != GSO_BY_FRAGS)
4521 return hlen <= mtu;
4522
4523 /* Undo this so we can re-use header sizes */
4524 hlen -= GSO_BY_FRAGS;
4525
4526 skb_walk_frags(skb, iter) {
4527 if (hlen + skb_headlen(iter) > mtu)
4528 return false;
4529 }
4530
4531 return true;
4532}
4533EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4534
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004535static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4536{
4537 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4538 kfree_skb(skb);
4539 return NULL;
4540 }
4541
Vlad Yasevichf6548612015-12-14 17:44:10 -05004542 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004543 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004544 skb->mac_header += VLAN_HLEN;
4545 return skb;
4546}
4547
4548struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4549{
4550 struct vlan_hdr *vhdr;
4551 u16 vlan_tci;
4552
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004553 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004554 /* vlan_tci is already set-up so leave this for another time */
4555 return skb;
4556 }
4557
4558 skb = skb_share_check(skb, GFP_ATOMIC);
4559 if (unlikely(!skb))
4560 goto err_free;
4561
4562 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4563 goto err_free;
4564
4565 vhdr = (struct vlan_hdr *)skb->data;
4566 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4567 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4568
4569 skb_pull_rcsum(skb, VLAN_HLEN);
4570 vlan_set_encap_proto(skb, vhdr);
4571
4572 skb = skb_reorder_vlan_header(skb);
4573 if (unlikely(!skb))
4574 goto err_free;
4575
4576 skb_reset_network_header(skb);
4577 skb_reset_transport_header(skb);
4578 skb_reset_mac_len(skb);
4579
4580 return skb;
4581
4582err_free:
4583 kfree_skb(skb);
4584 return NULL;
4585}
4586EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004587
Jiri Pirkoe2195122014-11-19 14:05:01 +01004588int skb_ensure_writable(struct sk_buff *skb, int write_len)
4589{
4590 if (!pskb_may_pull(skb, write_len))
4591 return -ENOMEM;
4592
4593 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4594 return 0;
4595
4596 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4597}
4598EXPORT_SYMBOL(skb_ensure_writable);
4599
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004600/* remove VLAN header from packet and update csum accordingly.
4601 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4602 */
4603int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004604{
4605 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004606 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004607 int err;
4608
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004609 if (WARN_ONCE(offset,
4610 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4611 offset)) {
4612 return -EINVAL;
4613 }
4614
Jiri Pirko93515d52014-11-19 14:05:02 +01004615 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4616 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004617 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004618
4619 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4620
4621 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4622 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4623
4624 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4625 __skb_pull(skb, VLAN_HLEN);
4626
4627 vlan_set_encap_proto(skb, vhdr);
4628 skb->mac_header += VLAN_HLEN;
4629
4630 if (skb_network_offset(skb) < ETH_HLEN)
4631 skb_set_network_header(skb, ETH_HLEN);
4632
4633 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004634
4635 return err;
4636}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004637EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004638
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004639/* Pop a vlan tag either from hwaccel or from payload.
4640 * Expects skb->data at mac header.
4641 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004642int skb_vlan_pop(struct sk_buff *skb)
4643{
4644 u16 vlan_tci;
4645 __be16 vlan_proto;
4646 int err;
4647
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004648 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004649 skb->vlan_tci = 0;
4650 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004651 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004652 return 0;
4653
4654 err = __skb_vlan_pop(skb, &vlan_tci);
4655 if (err)
4656 return err;
4657 }
4658 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004659 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004660 return 0;
4661
4662 vlan_proto = skb->protocol;
4663 err = __skb_vlan_pop(skb, &vlan_tci);
4664 if (unlikely(err))
4665 return err;
4666
4667 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4668 return 0;
4669}
4670EXPORT_SYMBOL(skb_vlan_pop);
4671
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004672/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4673 * Expects skb->data at mac header.
4674 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004675int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4676{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004677 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004678 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004679 int err;
4680
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004681 if (WARN_ONCE(offset,
4682 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4683 offset)) {
4684 return -EINVAL;
4685 }
4686
Jiri Pirko93515d52014-11-19 14:05:02 +01004687 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004688 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004689 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004690 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004691
Jiri Pirko93515d52014-11-19 14:05:02 +01004692 skb->protocol = skb->vlan_proto;
4693 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004694
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004695 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004696 }
4697 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4698 return 0;
4699}
4700EXPORT_SYMBOL(skb_vlan_push);
4701
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004702/**
4703 * alloc_skb_with_frags - allocate skb with page frags
4704 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004705 * @header_len: size of linear part
4706 * @data_len: needed length in frags
4707 * @max_page_order: max page order desired.
4708 * @errcode: pointer to error code if any
4709 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004710 *
4711 * This can be used to allocate a paged skb, given a maximal order for frags.
4712 */
4713struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4714 unsigned long data_len,
4715 int max_page_order,
4716 int *errcode,
4717 gfp_t gfp_mask)
4718{
4719 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4720 unsigned long chunk;
4721 struct sk_buff *skb;
4722 struct page *page;
4723 gfp_t gfp_head;
4724 int i;
4725
4726 *errcode = -EMSGSIZE;
4727 /* Note this test could be relaxed, if we succeed to allocate
4728 * high order pages...
4729 */
4730 if (npages > MAX_SKB_FRAGS)
4731 return NULL;
4732
4733 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004734 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004735 gfp_head |= __GFP_REPEAT;
4736
4737 *errcode = -ENOBUFS;
4738 skb = alloc_skb(header_len, gfp_head);
4739 if (!skb)
4740 return NULL;
4741
4742 skb->truesize += npages << PAGE_SHIFT;
4743
4744 for (i = 0; npages > 0; i++) {
4745 int order = max_page_order;
4746
4747 while (order) {
4748 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004749 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004750 __GFP_COMP |
4751 __GFP_NOWARN |
4752 __GFP_NORETRY,
4753 order);
4754 if (page)
4755 goto fill_page;
4756 /* Do not retry other high order allocations */
4757 order = 1;
4758 max_page_order = 0;
4759 }
4760 order--;
4761 }
4762 page = alloc_page(gfp_mask);
4763 if (!page)
4764 goto failure;
4765fill_page:
4766 chunk = min_t(unsigned long, data_len,
4767 PAGE_SIZE << order);
4768 skb_fill_page_desc(skb, i, page, 0, chunk);
4769 data_len -= chunk;
4770 npages -= 1 << order;
4771 }
4772 return skb;
4773
4774failure:
4775 kfree_skb(skb);
4776 return NULL;
4777}
4778EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004779
4780/* carve out the first off bytes from skb when off < headlen */
4781static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4782 const int headlen, gfp_t gfp_mask)
4783{
4784 int i;
4785 int size = skb_end_offset(skb);
4786 int new_hlen = headlen - off;
4787 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004788
4789 size = SKB_DATA_ALIGN(size);
4790
4791 if (skb_pfmemalloc(skb))
4792 gfp_mask |= __GFP_MEMALLOC;
4793 data = kmalloc_reserve(size +
4794 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4795 gfp_mask, NUMA_NO_NODE, NULL);
4796 if (!data)
4797 return -ENOMEM;
4798
4799 size = SKB_WITH_OVERHEAD(ksize(data));
4800
4801 /* Copy real data, and all frags */
4802 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4803 skb->len -= off;
4804
4805 memcpy((struct skb_shared_info *)(data + size),
4806 skb_shinfo(skb),
4807 offsetof(struct skb_shared_info,
4808 frags[skb_shinfo(skb)->nr_frags]));
4809 if (skb_cloned(skb)) {
4810 /* drop the old head gracefully */
4811 if (skb_orphan_frags(skb, gfp_mask)) {
4812 kfree(data);
4813 return -ENOMEM;
4814 }
4815 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4816 skb_frag_ref(skb, i);
4817 if (skb_has_frag_list(skb))
4818 skb_clone_fraglist(skb);
4819 skb_release_data(skb);
4820 } else {
4821 /* we can reuse existing recount- all we did was
4822 * relocate values
4823 */
4824 skb_free_head(skb);
4825 }
4826
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004827 skb->head = data;
4828 skb->data = data;
4829 skb->head_frag = 0;
4830#ifdef NET_SKBUFF_DATA_USES_OFFSET
4831 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004832#else
4833 skb->end = skb->head + size;
4834#endif
4835 skb_set_tail_pointer(skb, skb_headlen(skb));
4836 skb_headers_offset_update(skb, 0);
4837 skb->cloned = 0;
4838 skb->hdr_len = 0;
4839 skb->nohdr = 0;
4840 atomic_set(&skb_shinfo(skb)->dataref, 1);
4841
4842 return 0;
4843}
4844
4845static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4846
4847/* carve out the first eat bytes from skb's frag_list. May recurse into
4848 * pskb_carve()
4849 */
4850static int pskb_carve_frag_list(struct sk_buff *skb,
4851 struct skb_shared_info *shinfo, int eat,
4852 gfp_t gfp_mask)
4853{
4854 struct sk_buff *list = shinfo->frag_list;
4855 struct sk_buff *clone = NULL;
4856 struct sk_buff *insp = NULL;
4857
4858 do {
4859 if (!list) {
4860 pr_err("Not enough bytes to eat. Want %d\n", eat);
4861 return -EFAULT;
4862 }
4863 if (list->len <= eat) {
4864 /* Eaten as whole. */
4865 eat -= list->len;
4866 list = list->next;
4867 insp = list;
4868 } else {
4869 /* Eaten partially. */
4870 if (skb_shared(list)) {
4871 clone = skb_clone(list, gfp_mask);
4872 if (!clone)
4873 return -ENOMEM;
4874 insp = list->next;
4875 list = clone;
4876 } else {
4877 /* This may be pulled without problems. */
4878 insp = list;
4879 }
4880 if (pskb_carve(list, eat, gfp_mask) < 0) {
4881 kfree_skb(clone);
4882 return -ENOMEM;
4883 }
4884 break;
4885 }
4886 } while (eat);
4887
4888 /* Free pulled out fragments. */
4889 while ((list = shinfo->frag_list) != insp) {
4890 shinfo->frag_list = list->next;
4891 kfree_skb(list);
4892 }
4893 /* And insert new clone at head. */
4894 if (clone) {
4895 clone->next = list;
4896 shinfo->frag_list = clone;
4897 }
4898 return 0;
4899}
4900
4901/* carve off first len bytes from skb. Split line (off) is in the
4902 * non-linear part of skb
4903 */
4904static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4905 int pos, gfp_t gfp_mask)
4906{
4907 int i, k = 0;
4908 int size = skb_end_offset(skb);
4909 u8 *data;
4910 const int nfrags = skb_shinfo(skb)->nr_frags;
4911 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004912
4913 size = SKB_DATA_ALIGN(size);
4914
4915 if (skb_pfmemalloc(skb))
4916 gfp_mask |= __GFP_MEMALLOC;
4917 data = kmalloc_reserve(size +
4918 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4919 gfp_mask, NUMA_NO_NODE, NULL);
4920 if (!data)
4921 return -ENOMEM;
4922
4923 size = SKB_WITH_OVERHEAD(ksize(data));
4924
4925 memcpy((struct skb_shared_info *)(data + size),
4926 skb_shinfo(skb), offsetof(struct skb_shared_info,
4927 frags[skb_shinfo(skb)->nr_frags]));
4928 if (skb_orphan_frags(skb, gfp_mask)) {
4929 kfree(data);
4930 return -ENOMEM;
4931 }
4932 shinfo = (struct skb_shared_info *)(data + size);
4933 for (i = 0; i < nfrags; i++) {
4934 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4935
4936 if (pos + fsize > off) {
4937 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4938
4939 if (pos < off) {
4940 /* Split frag.
4941 * We have two variants in this case:
4942 * 1. Move all the frag to the second
4943 * part, if it is possible. F.e.
4944 * this approach is mandatory for TUX,
4945 * where splitting is expensive.
4946 * 2. Split is accurately. We make this.
4947 */
4948 shinfo->frags[0].page_offset += off - pos;
4949 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4950 }
4951 skb_frag_ref(skb, i);
4952 k++;
4953 }
4954 pos += fsize;
4955 }
4956 shinfo->nr_frags = k;
4957 if (skb_has_frag_list(skb))
4958 skb_clone_fraglist(skb);
4959
4960 if (k == 0) {
4961 /* split line is in frag list */
4962 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4963 }
4964 skb_release_data(skb);
4965
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004966 skb->head = data;
4967 skb->head_frag = 0;
4968 skb->data = data;
4969#ifdef NET_SKBUFF_DATA_USES_OFFSET
4970 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004971#else
4972 skb->end = skb->head + size;
4973#endif
4974 skb_reset_tail_pointer(skb);
4975 skb_headers_offset_update(skb, 0);
4976 skb->cloned = 0;
4977 skb->hdr_len = 0;
4978 skb->nohdr = 0;
4979 skb->len -= off;
4980 skb->data_len = skb->len;
4981 atomic_set(&skb_shinfo(skb)->dataref, 1);
4982 return 0;
4983}
4984
4985/* remove len bytes from the beginning of the skb */
4986static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4987{
4988 int headlen = skb_headlen(skb);
4989
4990 if (len < headlen)
4991 return pskb_carve_inside_header(skb, len, headlen, gfp);
4992 else
4993 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4994}
4995
4996/* Extract to_copy bytes starting at off from skb, and return this in
4997 * a new skb
4998 */
4999struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5000 int to_copy, gfp_t gfp)
5001{
5002 struct sk_buff *clone = skb_clone(skb, gfp);
5003
5004 if (!clone)
5005 return NULL;
5006
5007 if (pskb_carve(clone, off, gfp) < 0 ||
5008 pskb_trim(clone, to_copy)) {
5009 kfree_skb(clone);
5010 return NULL;
5011 }
5012 return clone;
5013}
5014EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005015
5016/**
5017 * skb_condense - try to get rid of fragments/frag_list if possible
5018 * @skb: buffer
5019 *
5020 * Can be used to save memory before skb is added to a busy queue.
5021 * If packet has bytes in frags and enough tail room in skb->head,
5022 * pull all of them, so that we can free the frags right now and adjust
5023 * truesize.
5024 * Notes:
5025 * We do not reallocate skb->head thus can not fail.
5026 * Caller must re-evaluate skb->truesize if needed.
5027 */
5028void skb_condense(struct sk_buff *skb)
5029{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005030 if (skb->data_len) {
5031 if (skb->data_len > skb->end - skb->tail ||
5032 skb_cloned(skb))
5033 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005034
Eric Dumazet3174fed2016-12-09 08:02:05 -08005035 /* Nice, we can free page frag(s) right now */
5036 __pskb_pull_tail(skb, skb->data_len);
5037 }
5038 /* At this point, skb->truesize might be over estimated,
5039 * because skb had a fragment, and fragments do not tell
5040 * their truesize.
5041 * When we pulled its content into skb->head, fragment
5042 * was freed, but __pskb_pull_tail() could not possibly
5043 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005044 */
5045 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5046}