blob: 3d91d43882748e540b8661864b690d05b3420233 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030052#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070063#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000064#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070065#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040066#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000072#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
74
75#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Eric Dumazetd7e88832012-04-30 08:10:34 +000081struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010083int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Jean Sacrenf05de732013-02-11 13:30:38 +000087 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000091 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Jean Sacrenf05de732013-02-11 13:30:38 +000098static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000099 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Joe Perchese005d192012-05-16 19:58:40 +0000101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000102 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 BUG();
106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jean Sacrenf05de732013-02-11 13:30:38 +0000110 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000161struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
162{
163 struct sk_buff *skb;
164
165 /* Get the HEAD */
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
168 if (!skb)
169 goto out;
170
171 /*
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
175 */
176 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000177 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
180
Cong Wang35d04612013-05-29 15:16:05 +0800181 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000182out:
183 return skb;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
David S. Millerd179cd12005-08-17 14:57:30 -0700187 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * @size: size to allocate
189 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800194 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
201 * %GFP_ATOMIC.
202 */
Al Virodd0fc662005-10-07 07:46:04 +0100203struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700204 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Christoph Lametere18b8902006-12-06 20:33:20 -0800206 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800207 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct sk_buff *skb;
209 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700210 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Liam Mark284f2b82015-07-14 15:42:17 -0700212 if (IS_ENABLED(CONFIG_FORCE_ALLOC_FROM_DMA_ZONE))
213 gfp_mask |= GFP_DMA;
214
Mel Gormanc93bdd02012-07-31 16:44:19 -0700215 cache = (flags & SKB_ALLOC_FCLONE)
216 ? skbuff_fclone_cache : skbuff_head_cache;
217
218 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
219 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800222 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!skb)
224 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700225 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 /* We do our best to align skb_shared_info on a separate cache
228 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
229 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
230 * Both skb->head and skb_shared_info are cache line aligned.
231 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000232 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000233 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700234 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!data)
236 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000237 /* kmalloc(size) might give us more room than requested.
238 * Put skb_shared_info exactly at the end of allocated zone,
239 * to allow max possible filling before reallocation.
240 */
241 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700242 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300244 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700245 * Only clear those fields we need to clear, not those that we will
246 * actually initialise below. Hence, don't put any more fields after
247 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300248 */
249 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000250 /* Account for allocated memory : skb + skb->head */
251 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700252 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 atomic_set(&skb->users, 1);
254 skb->head = data;
255 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700256 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700257 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800258 skb->mac_header = (typeof(skb->mac_header))~0U;
259 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000260
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800261 /* make sure we initialize shinfo sequentially */
262 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700263 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800264 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000265 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800266
Mel Gormanc93bdd02012-07-31 16:44:19 -0700267 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700268 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700270 fclones = container_of(skb, struct sk_buff_fclones, skb1);
271
272 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700273 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700274 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700275
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800276 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700277 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279out:
280 return skb;
281nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800282 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 skb = NULL;
284 goto out;
285}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800286EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700289 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000290 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700291 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000292 *
293 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100294 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700295 * @frag_size is 0, otherwise data should come from the page allocator
296 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000297 * The return is the new skb buffer.
298 * On a failure the return is %NULL, and @data is not freed.
299 * Notes :
300 * Before IO, driver allocates only data buffer where NIC put incoming frame
301 * Driver should add room at head (NET_SKB_PAD) and
302 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
303 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
304 * before giving packet to stack.
305 * RX rings only contains data buffers, not full skbs.
306 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700307struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000308{
309 struct skb_shared_info *shinfo;
310 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312
313 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
314 if (!skb)
315 return NULL;
316
Eric Dumazetd3836f22012-04-27 00:33:38 +0000317 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000318
319 memset(skb, 0, offsetof(struct sk_buff, tail));
320 skb->truesize = SKB_TRUESIZE(size);
321 atomic_set(&skb->users, 1);
322 skb->head = data;
323 skb->data = data;
324 skb_reset_tail_pointer(skb);
325 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800326 skb->mac_header = (typeof(skb->mac_header))~0U;
327 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000328
329 /* make sure we initialize shinfo sequentially */
330 shinfo = skb_shinfo(skb);
331 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
332 atomic_set(&shinfo->dataref, 1);
333 kmemcheck_annotate_variable(shinfo->destructor_arg);
334
335 return skb;
336}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700337
338/* build_skb() is wrapper over __build_skb(), that specifically
339 * takes care of skb->head and skb->pfmemalloc
340 * This means that if @frag_size is not zero, then @data must be backed
341 * by a page fragment, not kmalloc() or vmalloc()
342 */
343struct sk_buff *build_skb(void *data, unsigned int frag_size)
344{
345 struct sk_buff *skb = __build_skb(data, frag_size);
346
347 if (skb && frag_size) {
348 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700349 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700350 skb->pfmemalloc = 1;
351 }
352 return skb;
353}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000354EXPORT_SYMBOL(build_skb);
355
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100356#define NAPI_SKB_CACHE_SIZE 64
357
358struct napi_alloc_cache {
359 struct page_frag_cache page;
360 size_t skb_count;
361 void *skb_cache[NAPI_SKB_CACHE_SIZE];
362};
363
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700364static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100365static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800366
367static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
368{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700369 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800370 unsigned long flags;
371 void *data;
372
Liam Markdc8e3de2015-08-27 09:25:36 -0700373 if (IS_ENABLED(CONFIG_FORCE_ALLOC_FROM_DMA_ZONE))
374 gfp_mask |= GFP_DMA;
375
Alexander Duyckffde7322014-12-09 19:40:42 -0800376 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700377 nc = this_cpu_ptr(&netdev_alloc_cache);
378 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000379 local_irq_restore(flags);
380 return data;
381}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700382
383/**
384 * netdev_alloc_frag - allocate a page fragment
385 * @fragsz: fragment size
386 *
387 * Allocates a frag from a page for receive buffer.
388 * Uses GFP_ATOMIC allocations.
389 */
390void *netdev_alloc_frag(unsigned int fragsz)
391{
Alexander Duyck8b4ceed2019-02-15 14:44:18 -0800392 fragsz = SKB_DATA_ALIGN(fragsz);
393
Mel Gormanc93bdd02012-07-31 16:44:19 -0700394 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
395}
Eric Dumazet6f532612012-05-18 05:12:12 +0000396EXPORT_SYMBOL(netdev_alloc_frag);
397
Alexander Duyckffde7322014-12-09 19:40:42 -0800398static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
399{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100400 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700401
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100402 return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800403}
404
405void *napi_alloc_frag(unsigned int fragsz)
406{
Alexander Duyck8b4ceed2019-02-15 14:44:18 -0800407 fragsz = SKB_DATA_ALIGN(fragsz);
408
Alexander Duyckffde7322014-12-09 19:40:42 -0800409 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
410}
411EXPORT_SYMBOL(napi_alloc_frag);
412
Eric Dumazet6f532612012-05-18 05:12:12 +0000413/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800414 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
415 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900416 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800417 * @gfp_mask: get_free_pages mask, passed to alloc_skb
418 *
419 * Allocate a new &sk_buff and assign it a usage count of one. The
420 * buffer has NET_SKB_PAD headroom built in. Users should allocate
421 * the headroom they think they need without accounting for the
422 * built in space. The built in space is used for optimisations.
423 *
424 * %NULL is returned if there is no free memory.
425 */
Alexander Duyck94519802015-05-06 21:11:40 -0700426struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
427 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800428{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700429 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700430 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700432 bool pfmemalloc;
433 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800434
Alexander Duyck94519802015-05-06 21:11:40 -0700435 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800436
Alexander Duyck94519802015-05-06 21:11:40 -0700437 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800438 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700439 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
440 if (!skb)
441 goto skb_fail;
442 goto skb_success;
443 }
Alexander Duyck94519802015-05-06 21:11:40 -0700444
445 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
446 len = SKB_DATA_ALIGN(len);
447
448 if (sk_memalloc_socks())
449 gfp_mask |= __GFP_MEMALLOC;
450
451 local_irq_save(flags);
452
453 nc = this_cpu_ptr(&netdev_alloc_cache);
454 data = __alloc_page_frag(nc, len, gfp_mask);
455 pfmemalloc = nc->pfmemalloc;
456
457 local_irq_restore(flags);
458
459 if (unlikely(!data))
460 return NULL;
461
462 skb = __build_skb(data, len);
463 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700464 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700465 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700466 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467
Alexander Duyck94519802015-05-06 21:11:40 -0700468 /* use OR instead of assignment to avoid clearing of bits in mask */
469 if (pfmemalloc)
470 skb->pfmemalloc = 1;
471 skb->head_frag = 1;
472
Alexander Duycka080e7b2015-05-13 13:34:13 -0700473skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700474 skb_reserve(skb, NET_SKB_PAD);
475 skb->dev = dev;
476
Alexander Duycka080e7b2015-05-13 13:34:13 -0700477skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700478 return skb;
479}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800480EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alexander Duyckfd11a832014-12-09 19:40:49 -0800482/**
483 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
484 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900485 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800486 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
487 *
488 * Allocate a new sk_buff for use in NAPI receive. This buffer will
489 * attempt to allocate the head from a special reserved region used
490 * only for NAPI Rx allocation. By doing this we can save several
491 * CPU cycles by avoiding having to disable and re-enable IRQs.
492 *
493 * %NULL is returned if there is no free memory.
494 */
Alexander Duyck94519802015-05-06 21:11:40 -0700495struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
496 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800497{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100498 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800499 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700500 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800501
Alexander Duyck94519802015-05-06 21:11:40 -0700502 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800503
Alexander Duyck94519802015-05-06 21:11:40 -0700504 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800505 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700506 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
507 if (!skb)
508 goto skb_fail;
509 goto skb_success;
510 }
Alexander Duyck94519802015-05-06 21:11:40 -0700511
512 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
513 len = SKB_DATA_ALIGN(len);
514
515 if (sk_memalloc_socks())
516 gfp_mask |= __GFP_MEMALLOC;
517
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100518 data = __alloc_page_frag(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700519 if (unlikely(!data))
520 return NULL;
521
522 skb = __build_skb(data, len);
523 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700524 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700525 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800526 }
527
Alexander Duyck94519802015-05-06 21:11:40 -0700528 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100529 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700530 skb->pfmemalloc = 1;
531 skb->head_frag = 1;
532
Alexander Duycka080e7b2015-05-13 13:34:13 -0700533skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700534 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
535 skb->dev = napi->dev;
536
Alexander Duycka080e7b2015-05-13 13:34:13 -0700537skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800538 return skb;
539}
540EXPORT_SYMBOL(__napi_alloc_skb);
541
Peter Zijlstra654bed12008-10-07 14:22:33 -0700542void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000543 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700544{
545 skb_fill_page_desc(skb, i, page, off, size);
546 skb->len += size;
547 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000548 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700549}
550EXPORT_SYMBOL(skb_add_rx_frag);
551
Jason Wangf8e617e2013-11-01 14:07:47 +0800552void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
553 unsigned int truesize)
554{
555 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
556
557 skb_frag_size_add(frag, size);
558 skb->len += size;
559 skb->data_len += size;
560 skb->truesize += truesize;
561}
562EXPORT_SYMBOL(skb_coalesce_rx_frag);
563
Herbert Xu27b437c2006-07-13 19:26:39 -0700564static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700566 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700567 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Herbert Xu27b437c2006-07-13 19:26:39 -0700570static inline void skb_drop_fraglist(struct sk_buff *skb)
571{
572 skb_drop_list(&skb_shinfo(skb)->frag_list);
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void skb_clone_fraglist(struct sk_buff *skb)
576{
577 struct sk_buff *list;
578
David S. Millerfbb398a2009-06-09 00:18:59 -0700579 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 skb_get(list);
581}
582
Eric Dumazetd3836f22012-04-27 00:33:38 +0000583static void skb_free_head(struct sk_buff *skb)
584{
Alexander Duyck181edb22015-05-06 21:12:03 -0700585 unsigned char *head = skb->head;
586
Eric Dumazetd3836f22012-04-27 00:33:38 +0000587 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700588 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000589 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700590 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000591}
592
Adrian Bunk5bba1712006-06-29 13:02:35 -0700593static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Eric Dumazetff04a772014-09-23 18:39:30 -0700595 struct skb_shared_info *shinfo = skb_shinfo(skb);
596 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Eric Dumazetff04a772014-09-23 18:39:30 -0700598 if (skb->cloned &&
599 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
600 &shinfo->dataref))
601 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000602
Eric Dumazetff04a772014-09-23 18:39:30 -0700603 for (i = 0; i < shinfo->nr_frags; i++)
604 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000605
Eric Dumazetff04a772014-09-23 18:39:30 -0700606 /*
607 * If skb buf is from userspace, we need to notify the caller
608 * the lower device DMA has done;
609 */
610 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
611 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Eric Dumazetff04a772014-09-23 18:39:30 -0700613 uarg = shinfo->destructor_arg;
614 if (uarg->callback)
615 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700617
618 if (shinfo->frag_list)
619 kfree_skb_list(shinfo->frag_list);
620
621 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * Free an skbuff by memory without cleaning the state.
626 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800627static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700629 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700630
David S. Millerd179cd12005-08-17 14:57:30 -0700631 switch (skb->fclone) {
632 case SKB_FCLONE_UNAVAILABLE:
633 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800634 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700635
636 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700637 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800638
639 /* We usually free the clone (TX completion) before original skb
640 * This test would have no chance to be true for the clone,
641 * while here, branch prediction will be good.
642 */
643 if (atomic_read(&fclones->fclone_ref) == 1)
644 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700645 break;
646
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800647 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700648 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700649 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700650 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800651 if (!atomic_dec_and_test(&fclones->fclone_ref))
652 return;
653fastpath:
654 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700657static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Eric Dumazetadf30902009-06-02 05:19:30 +0000659 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#ifdef CONFIG_XFRM
661 secpath_put(skb->sp);
662#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700663 if (skb->destructor) {
664 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 skb->destructor(skb);
666 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000667#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700668 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100669#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200670#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 nf_bridge_put(skb->nf_bridge);
672#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700673}
674
675/* Free everything but the sk_buff shell. */
676static void skb_release_all(struct sk_buff *skb)
677{
678 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000679 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000680 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Herbert Xu2d4baff2007-11-26 23:11:19 +0800683/**
684 * __kfree_skb - private function
685 * @skb: buffer
686 *
687 * Free an sk_buff. Release anything attached to the buffer.
688 * Clean the state. This is an internal helper function. Users should
689 * always call kfree_skb
690 */
691
692void __kfree_skb(struct sk_buff *skb)
693{
694 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kfree_skbmem(skb);
696}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800697EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800700 * kfree_skb - free an sk_buff
701 * @skb: buffer to free
702 *
703 * Drop a reference to the buffer and free it if the usage count has
704 * hit zero.
705 */
706void kfree_skb(struct sk_buff *skb)
707{
708 if (unlikely(!skb))
709 return;
710 if (likely(atomic_read(&skb->users) == 1))
711 smp_rmb();
712 else if (likely(!atomic_dec_and_test(&skb->users)))
713 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000714 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800715 __kfree_skb(skb);
716}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800717EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800718
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700719void kfree_skb_list(struct sk_buff *segs)
720{
721 while (segs) {
722 struct sk_buff *next = segs->next;
723
724 kfree_skb(segs);
725 segs = next;
726 }
727}
728EXPORT_SYMBOL(kfree_skb_list);
729
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700730/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000731 * skb_tx_error - report an sk_buff xmit error
732 * @skb: buffer that triggered an error
733 *
734 * Report xmit error if a device callback is tracking this skb.
735 * skb must be freed afterwards.
736 */
737void skb_tx_error(struct sk_buff *skb)
738{
739 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
740 struct ubuf_info *uarg;
741
742 uarg = skb_shinfo(skb)->destructor_arg;
743 if (uarg->callback)
744 uarg->callback(uarg, false);
745 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
746 }
747}
748EXPORT_SYMBOL(skb_tx_error);
749
750/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000751 * consume_skb - free an skbuff
752 * @skb: buffer to free
753 *
754 * Drop a ref to the buffer and free it if the usage count has hit zero
755 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
756 * is being dropped after a failure and notes that
757 */
758void consume_skb(struct sk_buff *skb)
759{
760 if (unlikely(!skb))
761 return;
762 if (likely(atomic_read(&skb->users) == 1))
763 smp_rmb();
764 else if (likely(!atomic_dec_and_test(&skb->users)))
765 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900766 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000767 __kfree_skb(skb);
768}
769EXPORT_SYMBOL(consume_skb);
770
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100771void __kfree_skb_flush(void)
772{
773 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
774
775 /* flush skb_cache if containing objects */
776 if (nc->skb_count) {
777 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
778 nc->skb_cache);
779 nc->skb_count = 0;
780 }
781}
782
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100783static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100784{
785 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
786
787 /* drop skb->head and call any destructors for packet */
788 skb_release_all(skb);
789
790 /* record skb to CPU local list */
791 nc->skb_cache[nc->skb_count++] = skb;
792
793#ifdef CONFIG_SLUB
794 /* SLUB writes into objects when freeing */
795 prefetchw(skb);
796#endif
797
798 /* flush skb_cache if it is filled */
799 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
800 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
801 nc->skb_cache);
802 nc->skb_count = 0;
803 }
804}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100805void __kfree_skb_defer(struct sk_buff *skb)
806{
807 _kfree_skb_defer(skb);
808}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100809
810void napi_consume_skb(struct sk_buff *skb, int budget)
811{
812 if (unlikely(!skb))
813 return;
814
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100815 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100816 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100817 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100818 return;
819 }
820
821 if (likely(atomic_read(&skb->users) == 1))
822 smp_rmb();
823 else if (likely(!atomic_dec_and_test(&skb->users)))
824 return;
825 /* if reaching here SKB is ready to free */
826 trace_consume_skb(skb);
827
828 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700829 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100830 __kfree_skb(skb);
831 return;
832 }
833
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100834 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100835}
836EXPORT_SYMBOL(napi_consume_skb);
837
Eric Dumazetb1937222014-09-28 22:18:47 -0700838/* Make sure a field is enclosed inside headers_start/headers_end section */
839#define CHECK_SKB_FIELD(field) \
840 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
841 offsetof(struct sk_buff, headers_start)); \
842 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
843 offsetof(struct sk_buff, headers_end)); \
844
Herbert Xudec18812007-10-14 00:37:30 -0700845static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
846{
847 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700848 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700849 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700850 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000851 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700852#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700853 new->sp = secpath_get(old->sp);
854#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700855 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700856
Eric Dumazetb1937222014-09-28 22:18:47 -0700857 /* Note : this field could be in headers_start/headers_end section
858 * It is not yet because we do not want to have a 16 bit hole
859 */
860 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300861
Eric Dumazetb1937222014-09-28 22:18:47 -0700862 memcpy(&new->headers_start, &old->headers_start,
863 offsetof(struct sk_buff, headers_end) -
864 offsetof(struct sk_buff, headers_start));
865 CHECK_SKB_FIELD(protocol);
866 CHECK_SKB_FIELD(csum);
867 CHECK_SKB_FIELD(hash);
868 CHECK_SKB_FIELD(priority);
869 CHECK_SKB_FIELD(skb_iif);
870 CHECK_SKB_FIELD(vlan_proto);
871 CHECK_SKB_FIELD(vlan_tci);
872 CHECK_SKB_FIELD(transport_header);
873 CHECK_SKB_FIELD(network_header);
874 CHECK_SKB_FIELD(mac_header);
875 CHECK_SKB_FIELD(inner_protocol);
876 CHECK_SKB_FIELD(inner_transport_header);
877 CHECK_SKB_FIELD(inner_network_header);
878 CHECK_SKB_FIELD(inner_mac_header);
879 CHECK_SKB_FIELD(mark);
880#ifdef CONFIG_NETWORK_SECMARK
881 CHECK_SKB_FIELD(secmark);
882#endif
Cong Wange0d10952013-08-01 11:10:25 +0800883#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700884 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300885#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800886#ifdef CONFIG_XPS
887 CHECK_SKB_FIELD(sender_cpu);
888#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700889#ifdef CONFIG_NET_SCHED
890 CHECK_SKB_FIELD(tc_index);
891#ifdef CONFIG_NET_CLS_ACT
892 CHECK_SKB_FIELD(tc_verd);
893#endif
894#endif
895
Herbert Xudec18812007-10-14 00:37:30 -0700896}
897
Herbert Xu82c49a32009-05-22 22:11:37 +0000898/*
899 * You should not add any new code to this function. Add it to
900 * __copy_skb_header above instead.
901 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700902static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#define C(x) n->x = skb->x
905
906 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700908 __copy_skb_header(n, skb);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 C(len);
911 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700912 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700913 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800914 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 n->nohdr = 0;
Eric Dumazetec986182018-04-07 13:42:39 -0700916 n->peeked = 0;
Stefano Brivioad375ea2018-07-13 13:21:07 +0200917 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 C(tail);
920 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800921 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000922 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800923 C(data);
924 C(truesize);
925 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 atomic_inc(&(skb_shinfo(skb)->dataref));
928 skb->cloned = 1;
929
930 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700931#undef C
932}
933
934/**
935 * skb_morph - morph one skb into another
936 * @dst: the skb to receive the contents
937 * @src: the skb to supply the contents
938 *
939 * This is identical to skb_clone except that the target skb is
940 * supplied by the user.
941 *
942 * The target skb is returned upon exit.
943 */
944struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
945{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800946 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700947 return __skb_clone(dst, src);
948}
949EXPORT_SYMBOL_GPL(skb_morph);
950
Ben Hutchings2c530402012-07-10 10:55:09 +0000951/**
952 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000953 * @skb: the skb to modify
954 * @gfp_mask: allocation priority
955 *
956 * This must be called on SKBTX_DEV_ZEROCOPY skb.
957 * It will copy all frags into kernel and drop the reference
958 * to userspace pages.
959 *
960 * If this function is called from an interrupt gfp_mask() must be
961 * %GFP_ATOMIC.
962 *
963 * Returns 0 on success or a negative error code on failure
964 * to allocate kernel memory to copy to.
965 */
966int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000967{
968 int i;
969 int num_frags = skb_shinfo(skb)->nr_frags;
970 struct page *page, *head = NULL;
971 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
972
973 for (i = 0; i < num_frags; i++) {
974 u8 *vaddr;
975 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
976
Krishna Kumar02756ed2012-07-17 02:05:29 +0000977 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000978 if (!page) {
979 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900980 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000981 put_page(head);
982 head = next;
983 }
984 return -ENOMEM;
985 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200986 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000987 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000988 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200989 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900990 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000991 head = page;
992 }
993
994 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000995 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000996 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000997
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000998 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000999
1000 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001001 for (i = num_frags - 1; i >= 0; i--) {
1002 __skb_fill_page_desc(skb, i, head, 0,
1003 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001004 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001005 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001006
1007 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +00001008 return 0;
1009}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001010EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001011
Herbert Xue0053ec2007-10-14 00:37:52 -07001012/**
1013 * skb_clone - duplicate an sk_buff
1014 * @skb: buffer to clone
1015 * @gfp_mask: allocation priority
1016 *
1017 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1018 * copies share the same packet data but not structure. The new
1019 * buffer has a reference count of 1. If the allocation fails the
1020 * function returns %NULL otherwise the new buffer is returned.
1021 *
1022 * If this function is called from an interrupt gfp_mask() must be
1023 * %GFP_ATOMIC.
1024 */
1025
1026struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1027{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001028 struct sk_buff_fclones *fclones = container_of(skb,
1029 struct sk_buff_fclones,
1030 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001031 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001032
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001033 if (skb_orphan_frags(skb, gfp_mask))
1034 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001035
Herbert Xue0053ec2007-10-14 00:37:52 -07001036 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001037 atomic_read(&fclones->fclone_ref) == 1) {
1038 n = &fclones->skb2;
1039 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001040 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001041 if (skb_pfmemalloc(skb))
1042 gfp_mask |= __GFP_MEMALLOC;
1043
Herbert Xue0053ec2007-10-14 00:37:52 -07001044 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1045 if (!n)
1046 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001047
1048 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001049 n->fclone = SKB_FCLONE_UNAVAILABLE;
1050 }
1051
1052 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001054EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001056static void skb_headers_offset_update(struct sk_buff *skb, int off)
1057{
Eric Dumazet030737b2013-10-19 11:42:54 -07001058 /* Only adjust this if it actually is csum_start rather than csum */
1059 if (skb->ip_summed == CHECKSUM_PARTIAL)
1060 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001061 /* {transport,network,mac}_header and tail are relative to skb->head */
1062 skb->transport_header += off;
1063 skb->network_header += off;
1064 if (skb_mac_header_was_set(skb))
1065 skb->mac_header += off;
1066 skb->inner_transport_header += off;
1067 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001068 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1072{
Herbert Xudec18812007-10-14 00:37:30 -07001073 __copy_skb_header(new, old);
1074
Herbert Xu79671682006-06-22 02:40:14 -07001075 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1076 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1077 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Mel Gormanc93bdd02012-07-31 16:44:19 -07001080static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1081{
1082 if (skb_pfmemalloc(skb))
1083 return SKB_ALLOC_RX;
1084 return 0;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/**
1088 * skb_copy - create private copy of an sk_buff
1089 * @skb: buffer to copy
1090 * @gfp_mask: allocation priority
1091 *
1092 * Make a copy of both an &sk_buff and its data. This is used when the
1093 * caller wishes to modify the data and needs a private copy of the
1094 * data to alter. Returns %NULL on failure or the pointer to the buffer
1095 * on success. The returned buffer has a reference count of 1.
1096 *
1097 * As by-product this function converts non-linear &sk_buff to linear
1098 * one, so that &sk_buff becomes completely private and caller is allowed
1099 * to modify all the data of returned buffer. This means that this
1100 * function is not recommended for use in circumstances when only
1101 * header is going to be modified. Use pskb_copy() instead.
1102 */
1103
Al Virodd0fc662005-10-07 07:46:04 +01001104struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001106 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001107 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001108 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1109 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!n)
1112 return NULL;
1113
1114 /* Set the data pointer */
1115 skb_reserve(n, headerlen);
1116 /* Set the tail pointer and length */
1117 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1120 BUG();
1121
1122 copy_skb_header(n, skb);
1123 return n;
1124}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001125EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001128 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001130 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001132 * @fclone: if true allocate the copy of the skb from the fclone
1133 * cache instead of the head cache; it is recommended to set this
1134 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
1136 * Make a copy of both an &sk_buff and part of its data, located
1137 * in header. Fragmented data remain shared. This is used when
1138 * the caller wishes to modify only header of &sk_buff and needs
1139 * private copy of the header to alter. Returns %NULL on failure
1140 * or the pointer to the buffer on success.
1141 * The returned buffer has a reference count of 1.
1142 */
1143
Octavian Purdilabad93e92014-06-12 01:36:26 +03001144struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1145 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Eric Dumazet117632e2011-12-03 21:39:53 +00001147 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001148 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1149 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!n)
1152 goto out;
1153
1154 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001155 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* Set the tail pointer and length */
1157 skb_put(n, skb_headlen(skb));
1158 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001159 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Herbert Xu25f484a2006-11-07 14:57:15 -08001161 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 n->data_len = skb->data_len;
1163 n->len = skb->len;
1164
1165 if (skb_shinfo(skb)->nr_frags) {
1166 int i;
1167
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001168 if (skb_orphan_frags(skb, gfp_mask)) {
1169 kfree_skb(n);
1170 n = NULL;
1171 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1174 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001175 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 skb_shinfo(n)->nr_frags = i;
1178 }
1179
David S. Miller21dc3302010-08-23 00:13:46 -07001180 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1182 skb_clone_fraglist(n);
1183 }
1184
1185 copy_skb_header(n, skb);
1186out:
1187 return n;
1188}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001189EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191/**
1192 * pskb_expand_head - reallocate header of &sk_buff
1193 * @skb: buffer to reallocate
1194 * @nhead: room to add at head
1195 * @ntail: room to add at tail
1196 * @gfp_mask: allocation priority
1197 *
Mathias Krausebc323832013-11-07 14:18:26 +01001198 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1199 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * reference count of 1. Returns zero in the case of success or error,
1201 * if expansion failed. In the last case, &sk_buff is not changed.
1202 *
1203 * All the pointers pointing into skb header may change and must be
1204 * reloaded after call to this function.
1205 */
1206
Victor Fusco86a76ca2005-07-08 14:57:47 -07001207int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001208 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int i;
1211 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001212 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 long off;
1214
Herbert Xu4edd87a2008-10-01 07:09:38 -07001215 BUG_ON(nhead < 0);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (skb_shared(skb))
1218 BUG();
1219
1220 size = SKB_DATA_ALIGN(size);
1221
Mel Gormanc93bdd02012-07-31 16:44:19 -07001222 if (skb_pfmemalloc(skb))
1223 gfp_mask |= __GFP_MEMALLOC;
1224 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1225 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!data)
1227 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001228 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001231 * optimized for the cases when header is void.
1232 */
1233 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1234
1235 memcpy((struct skb_shared_info *)(data + size),
1236 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001237 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alexander Duyck3e245912012-05-04 14:26:51 +00001239 /*
1240 * if shinfo is shared we must drop the old head gracefully, but if it
1241 * is not we can just drop the old head and let the existing refcount
1242 * be since all we did is relocate the values
1243 */
1244 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001245 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001246 if (skb_orphan_frags(skb, gfp_mask))
1247 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001248 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001249 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Eric Dumazet1fd63042010-09-02 23:09:32 +00001251 if (skb_has_frag_list(skb))
1252 skb_clone_fraglist(skb);
1253
1254 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001255 } else {
1256 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 off = (data + nhead) - skb->head;
1259
1260 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001261 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001263#ifdef NET_SKBUFF_DATA_USES_OFFSET
1264 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001265 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001266#else
1267 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001268#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001269 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001270 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001272 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 skb->nohdr = 0;
1274 atomic_set(&skb_shinfo(skb)->dataref, 1);
1275 return 0;
1276
Shirley Maa6686f22011-07-06 12:22:12 +00001277nofrags:
1278 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279nodata:
1280 return -ENOMEM;
1281}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001282EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284/* Make private copy of skb with writable head and some headroom */
1285
1286struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1287{
1288 struct sk_buff *skb2;
1289 int delta = headroom - skb_headroom(skb);
1290
1291 if (delta <= 0)
1292 skb2 = pskb_copy(skb, GFP_ATOMIC);
1293 else {
1294 skb2 = skb_clone(skb, GFP_ATOMIC);
1295 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1296 GFP_ATOMIC)) {
1297 kfree_skb(skb2);
1298 skb2 = NULL;
1299 }
1300 }
1301 return skb2;
1302}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001303EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305/**
1306 * skb_copy_expand - copy and expand sk_buff
1307 * @skb: buffer to copy
1308 * @newheadroom: new free bytes at head
1309 * @newtailroom: new free bytes at tail
1310 * @gfp_mask: allocation priority
1311 *
1312 * Make a copy of both an &sk_buff and its data and while doing so
1313 * allocate additional space.
1314 *
1315 * This is used when the caller wishes to modify the data and needs a
1316 * private copy of the data to alter as well as more space for new fields.
1317 * Returns %NULL on failure or the pointer to the buffer
1318 * on success. The returned buffer has a reference count of 1.
1319 *
1320 * You must pass %GFP_ATOMIC as the allocation priority if this function
1321 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
1323struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001324 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001325 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 /*
1328 * Allocate the copy buffer
1329 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001330 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1331 gfp_mask, skb_alloc_rx_flag(skb),
1332 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001333 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int head_copy_len, head_copy_off;
1335
1336 if (!n)
1337 return NULL;
1338
1339 skb_reserve(n, newheadroom);
1340
1341 /* Set the tail pointer and length */
1342 skb_put(n, skb->len);
1343
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001344 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 head_copy_off = 0;
1346 if (newheadroom <= head_copy_len)
1347 head_copy_len = newheadroom;
1348 else
1349 head_copy_off = newheadroom - head_copy_len;
1350
1351 /* Copy the linear header and data. */
1352 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1353 skb->len + head_copy_len))
1354 BUG();
1355
1356 copy_skb_header(n, skb);
1357
Eric Dumazet030737b2013-10-19 11:42:54 -07001358 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return n;
1361}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001362EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364/**
1365 * skb_pad - zero pad the tail of an skb
1366 * @skb: buffer to pad
1367 * @pad: space to pad
1368 *
1369 * Ensure that a buffer is followed by a padding area that is zero
1370 * filled. Used by network drivers which may DMA or transfer data
1371 * beyond the buffer end onto the wire.
1372 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001373 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001375
Herbert Xu5b057c62006-06-23 02:06:41 -07001376int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Herbert Xu5b057c62006-06-23 02:06:41 -07001378 int err;
1379 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001382 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001386
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001387 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001388 if (likely(skb_cloned(skb) || ntail > 0)) {
1389 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1390 if (unlikely(err))
1391 goto free_skb;
1392 }
1393
1394 /* FIXME: The use of this function with non-linear skb's really needs
1395 * to be audited.
1396 */
1397 err = skb_linearize(skb);
1398 if (unlikely(err))
1399 goto free_skb;
1400
1401 memset(skb->data + skb->len, 0, pad);
1402 return 0;
1403
1404free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001406 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001407}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001408EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001409
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001410/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001411 * pskb_put - add data to the tail of a potentially fragmented buffer
1412 * @skb: start of the buffer to use
1413 * @tail: tail fragment of the buffer to use
1414 * @len: amount of data to add
1415 *
1416 * This function extends the used data area of the potentially
1417 * fragmented buffer. @tail must be the last fragment of @skb -- or
1418 * @skb itself. If this would exceed the total buffer size the kernel
1419 * will panic. A pointer to the first byte of the extra data is
1420 * returned.
1421 */
1422
1423unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1424{
1425 if (tail != skb) {
1426 skb->data_len += len;
1427 skb->len += len;
1428 }
1429 return skb_put(tail, len);
1430}
1431EXPORT_SYMBOL_GPL(pskb_put);
1432
1433/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001434 * skb_put - add data to a buffer
1435 * @skb: buffer to use
1436 * @len: amount of data to add
1437 *
1438 * This function extends the used data area of the buffer. If this would
1439 * exceed the total buffer size the kernel will panic. A pointer to the
1440 * first byte of the extra data is returned.
1441 */
1442unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1443{
1444 unsigned char *tmp = skb_tail_pointer(skb);
1445 SKB_LINEAR_ASSERT(skb);
1446 skb->tail += len;
1447 skb->len += len;
1448 if (unlikely(skb->tail > skb->end))
1449 skb_over_panic(skb, len, __builtin_return_address(0));
1450 return tmp;
1451}
1452EXPORT_SYMBOL(skb_put);
1453
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001454/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001455 * skb_push - add data to the start of a buffer
1456 * @skb: buffer to use
1457 * @len: amount of data to add
1458 *
1459 * This function extends the used data area of the buffer at the buffer
1460 * start. If this would exceed the total buffer headroom the kernel will
1461 * panic. A pointer to the first byte of the extra data is returned.
1462 */
1463unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1464{
1465 skb->data -= len;
1466 skb->len += len;
1467 if (unlikely(skb->data<skb->head))
1468 skb_under_panic(skb, len, __builtin_return_address(0));
1469 return skb->data;
1470}
1471EXPORT_SYMBOL(skb_push);
1472
1473/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001474 * skb_pull - remove data from the start of a buffer
1475 * @skb: buffer to use
1476 * @len: amount of data to remove
1477 *
1478 * This function removes data from the start of a buffer, returning
1479 * the memory to the headroom. A pointer to the next data in the buffer
1480 * is returned. Once the data has been pulled future pushes will overwrite
1481 * the old data.
1482 */
1483unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1484{
David S. Miller47d29642010-05-02 02:21:44 -07001485 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001486}
1487EXPORT_SYMBOL(skb_pull);
1488
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001489/**
1490 * skb_trim - remove end from a buffer
1491 * @skb: buffer to alter
1492 * @len: new length
1493 *
1494 * Cut the length of a buffer down by removing data from the tail. If
1495 * the buffer is already under the length specified it is not modified.
1496 * The skb must be linear.
1497 */
1498void skb_trim(struct sk_buff *skb, unsigned int len)
1499{
1500 if (skb->len > len)
1501 __skb_trim(skb, len);
1502}
1503EXPORT_SYMBOL(skb_trim);
1504
Herbert Xu3cc0e872006-06-09 16:13:38 -07001505/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
1507
Herbert Xu3cc0e872006-06-09 16:13:38 -07001508int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Herbert Xu27b437c2006-07-13 19:26:39 -07001510 struct sk_buff **fragp;
1511 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int offset = skb_headlen(skb);
1513 int nfrags = skb_shinfo(skb)->nr_frags;
1514 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001515 int err;
1516
1517 if (skb_cloned(skb) &&
1518 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001521 i = 0;
1522 if (offset >= len)
1523 goto drop_pages;
1524
1525 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001526 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001527
1528 if (end < len) {
1529 offset = end;
1530 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001532
Eric Dumazet9e903e02011-10-18 21:00:24 +00001533 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001534
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001535drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001536 skb_shinfo(skb)->nr_frags = i;
1537
1538 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001539 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001540
David S. Miller21dc3302010-08-23 00:13:46 -07001541 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001542 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001543 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Herbert Xu27b437c2006-07-13 19:26:39 -07001546 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1547 fragp = &frag->next) {
1548 int end = offset + frag->len;
1549
1550 if (skb_shared(frag)) {
1551 struct sk_buff *nfrag;
1552
1553 nfrag = skb_clone(frag, GFP_ATOMIC);
1554 if (unlikely(!nfrag))
1555 return -ENOMEM;
1556
1557 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001558 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001559 frag = nfrag;
1560 *fragp = frag;
1561 }
1562
1563 if (end < len) {
1564 offset = end;
1565 continue;
1566 }
1567
1568 if (end > len &&
1569 unlikely((err = pskb_trim(frag, len - offset))))
1570 return err;
1571
1572 if (frag->next)
1573 skb_drop_list(&frag->next);
1574 break;
1575 }
1576
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001577done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001578 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 skb->data_len -= skb->len - len;
1580 skb->len = len;
1581 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001582 skb->len = len;
1583 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001584 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
1587 return 0;
1588}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001589EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Eric Dumazet791521e2018-10-10 12:30:11 -07001591/* Note : use pskb_trim_rcsum() instead of calling this directly
1592 */
1593int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1594{
1595 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1596 int delta = skb->len - len;
1597
Dimitris Michailidis0c49b5e2018-10-19 17:07:13 -07001598 skb->csum = csum_block_sub(skb->csum,
1599 skb_checksum(skb, len, delta, 0),
1600 len);
Eric Dumazet791521e2018-10-10 12:30:11 -07001601 }
1602 return __pskb_trim(skb, len);
1603}
1604EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/**
1607 * __pskb_pull_tail - advance tail of skb header
1608 * @skb: buffer to reallocate
1609 * @delta: number of bytes to advance tail
1610 *
1611 * The function makes a sense only on a fragmented &sk_buff,
1612 * it expands header moving its tail forward and copying necessary
1613 * data from fragmented part.
1614 *
1615 * &sk_buff MUST have reference count of 1.
1616 *
1617 * Returns %NULL (and &sk_buff does not change) if pull failed
1618 * or value of new tail of skb in the case of success.
1619 *
1620 * All the pointers pointing into skb header may change and must be
1621 * reloaded after call to this function.
1622 */
1623
1624/* Moves tail of skb head forward, copying data from fragmented part,
1625 * when it is necessary.
1626 * 1. It may fail due to malloc failure.
1627 * 2. It may change skb pointers.
1628 *
1629 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1630 */
1631unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1632{
1633 /* If skb has not enough free space at tail, get new one
1634 * plus 128 bytes for future expansions. If we have enough
1635 * room at tail, reallocate without expansion only if skb is cloned.
1636 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001637 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 if (eat > 0 || skb_cloned(skb)) {
1640 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1641 GFP_ATOMIC))
1642 return NULL;
1643 }
1644
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001645 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 BUG();
1647
1648 /* Optimization: no fragments, no reasons to preestimate
1649 * size of pulled pages. Superb.
1650 */
David S. Miller21dc3302010-08-23 00:13:46 -07001651 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto pull_pages;
1653
1654 /* Estimate size of pulled pages. */
1655 eat = delta;
1656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001657 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1658
1659 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001661 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
1664 /* If we need update frag list, we are in troubles.
1665 * Certainly, it possible to add an offset to skb data,
1666 * but taking into account that pulling is expected to
1667 * be very rare operation, it is worth to fight against
1668 * further bloating skb head and crucify ourselves here instead.
1669 * Pure masohism, indeed. 8)8)
1670 */
1671 if (eat) {
1672 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1673 struct sk_buff *clone = NULL;
1674 struct sk_buff *insp = NULL;
1675
1676 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001677 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 if (list->len <= eat) {
1680 /* Eaten as whole. */
1681 eat -= list->len;
1682 list = list->next;
1683 insp = list;
1684 } else {
1685 /* Eaten partially. */
1686
1687 if (skb_shared(list)) {
1688 /* Sucks! We need to fork list. :-( */
1689 clone = skb_clone(list, GFP_ATOMIC);
1690 if (!clone)
1691 return NULL;
1692 insp = list->next;
1693 list = clone;
1694 } else {
1695 /* This may be pulled without
1696 * problems. */
1697 insp = list;
1698 }
1699 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001700 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return NULL;
1702 }
1703 break;
1704 }
1705 } while (eat);
1706
1707 /* Free pulled out fragments. */
1708 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1709 skb_shinfo(skb)->frag_list = list->next;
1710 kfree_skb(list);
1711 }
1712 /* And insert new clone at head. */
1713 if (clone) {
1714 clone->next = list;
1715 skb_shinfo(skb)->frag_list = clone;
1716 }
1717 }
1718 /* Success! Now we may commit changes to skb data. */
1719
1720pull_pages:
1721 eat = delta;
1722 k = 0;
1723 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001724 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1725
1726 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001727 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001728 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else {
1730 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1731 if (eat) {
1732 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001733 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 eat = 0;
1735 }
1736 k++;
1737 }
1738 }
1739 skb_shinfo(skb)->nr_frags = k;
1740
1741 skb->tail += delta;
1742 skb->data_len -= delta;
1743
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001744 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001746EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Eric Dumazet22019b12011-07-29 18:37:31 +00001748/**
1749 * skb_copy_bits - copy bits from skb to kernel buffer
1750 * @skb: source skb
1751 * @offset: offset in source
1752 * @to: destination buffer
1753 * @len: number of bytes to copy
1754 *
1755 * Copy the specified number of bytes from the source skb to the
1756 * destination buffer.
1757 *
1758 * CAUTION ! :
1759 * If its prototype is ever changed,
1760 * check arch/{*}/net/{*}.S files,
1761 * since it is called from BPF assembly code.
1762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1764{
David S. Miller1a028e52007-04-27 15:21:23 -07001765 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001766 struct sk_buff *frag_iter;
1767 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (offset > (int)skb->len - len)
1770 goto fault;
1771
1772 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001773 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (copy > len)
1775 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001776 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if ((len -= copy) == 0)
1778 return 0;
1779 offset += copy;
1780 to += copy;
1781 }
1782
1783 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001784 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001785 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001787 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001788
Eric Dumazet51c56b02012-04-05 11:35:15 +02001789 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if ((copy = end - offset) > 0) {
1791 u8 *vaddr;
1792
1793 if (copy > len)
1794 copy = len;
1795
Eric Dumazet51c56b02012-04-05 11:35:15 +02001796 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001798 vaddr + f->page_offset + offset - start,
1799 copy);
1800 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 if ((len -= copy) == 0)
1803 return 0;
1804 offset += copy;
1805 to += copy;
1806 }
David S. Miller1a028e52007-04-27 15:21:23 -07001807 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809
David S. Millerfbb398a2009-06-09 00:18:59 -07001810 skb_walk_frags(skb, frag_iter) {
1811 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
David S. Millerfbb398a2009-06-09 00:18:59 -07001813 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
David S. Millerfbb398a2009-06-09 00:18:59 -07001815 end = start + frag_iter->len;
1816 if ((copy = end - offset) > 0) {
1817 if (copy > len)
1818 copy = len;
1819 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1820 goto fault;
1821 if ((len -= copy) == 0)
1822 return 0;
1823 offset += copy;
1824 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001826 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Shirley Maa6686f22011-07-06 12:22:12 +00001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (!len)
1830 return 0;
1831
1832fault:
1833 return -EFAULT;
1834}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001835EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jens Axboe9c55e012007-11-06 23:30:13 -08001837/*
1838 * Callback from splice_to_pipe(), if we need to release some pages
1839 * at the end of the spd in case we error'ed out in filling the pipe.
1840 */
1841static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1842{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001843 put_page(spd->pages[i]);
1844}
Jens Axboe9c55e012007-11-06 23:30:13 -08001845
David S. Millera108d5f2012-04-23 23:06:11 -04001846static struct page *linear_to_page(struct page *page, unsigned int *len,
1847 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001848 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001849{
Eric Dumazet5640f762012-09-23 23:04:42 +00001850 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001851
Eric Dumazet5640f762012-09-23 23:04:42 +00001852 if (!sk_page_frag_refill(sk, pfrag))
1853 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001854
Eric Dumazet5640f762012-09-23 23:04:42 +00001855 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001856
Eric Dumazet5640f762012-09-23 23:04:42 +00001857 memcpy(page_address(pfrag->page) + pfrag->offset,
1858 page_address(page) + *offset, *len);
1859 *offset = pfrag->offset;
1860 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001861
Eric Dumazet5640f762012-09-23 23:04:42 +00001862 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001863}
1864
Eric Dumazet41c73a02012-04-22 12:26:16 +00001865static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1866 struct page *page,
1867 unsigned int offset)
1868{
1869 return spd->nr_pages &&
1870 spd->pages[spd->nr_pages - 1] == page &&
1871 (spd->partial[spd->nr_pages - 1].offset +
1872 spd->partial[spd->nr_pages - 1].len == offset);
1873}
1874
Jens Axboe9c55e012007-11-06 23:30:13 -08001875/*
1876 * Fill page/offset/length into spd, if it can hold more pages.
1877 */
David S. Millera108d5f2012-04-23 23:06:11 -04001878static bool spd_fill_page(struct splice_pipe_desc *spd,
1879 struct pipe_inode_info *pipe, struct page *page,
1880 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001881 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001882 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001883{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001884 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001885 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001886
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001887 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001888 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001889 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001890 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001891 }
1892 if (spd_can_coalesce(spd, page, offset)) {
1893 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001894 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001895 }
1896 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001897 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001898 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001899 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001900 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001901
David S. Millera108d5f2012-04-23 23:06:11 -04001902 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001903}
1904
David S. Millera108d5f2012-04-23 23:06:11 -04001905static bool __splice_segment(struct page *page, unsigned int poff,
1906 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001907 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001908 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001909 struct sock *sk,
1910 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001911{
1912 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001913 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001914
1915 /* skip this segment if already processed */
1916 if (*off >= plen) {
1917 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001918 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001919 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001920
Octavian Purdila2870c432008-07-15 00:49:11 -07001921 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001922 poff += *off;
1923 plen -= *off;
1924 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001925
Eric Dumazet18aafc62013-01-11 14:46:37 +00001926 do {
1927 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001928
Eric Dumazet18aafc62013-01-11 14:46:37 +00001929 if (spd_fill_page(spd, pipe, page, &flen, poff,
1930 linear, sk))
1931 return true;
1932 poff += flen;
1933 plen -= flen;
1934 *len -= flen;
1935 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001936
David S. Millera108d5f2012-04-23 23:06:11 -04001937 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001938}
1939
1940/*
David S. Millera108d5f2012-04-23 23:06:11 -04001941 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001942 * pipe is full or if we already spliced the requested length.
1943 */
David S. Millera108d5f2012-04-23 23:06:11 -04001944static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1945 unsigned int *offset, unsigned int *len,
1946 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001947{
1948 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001949 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001950
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001951 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001952 * If skb->head_frag is set, this 'linear' part is backed by a
1953 * fragment, and if the head is not shared with any clones then
1954 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001955 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001956 if (__splice_segment(virt_to_page(skb->data),
1957 (unsigned long) skb->data & (PAGE_SIZE - 1),
1958 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001959 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001960 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001961 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001962 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001963
1964 /*
1965 * then map the fragments
1966 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001967 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1968 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1969
Ian Campbellea2ab692011-08-22 23:44:58 +00001970 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001971 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001972 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001973 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001974 }
1975
Tom Herbertfa9835e2016-03-07 14:11:04 -08001976 skb_walk_frags(skb, iter) {
1977 if (*offset >= iter->len) {
1978 *offset -= iter->len;
1979 continue;
1980 }
1981 /* __skb_splice_bits() only fails if the output has no room
1982 * left, so no point in going over the frag_list for the error
1983 * case.
1984 */
1985 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1986 return true;
1987 }
1988
David S. Millera108d5f2012-04-23 23:06:11 -04001989 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001990}
1991
1992/*
1993 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001994 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001995 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001996int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001997 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001998 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001999{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002000 struct partial_page partial[MAX_SKB_FRAGS];
2001 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002002 struct splice_pipe_desc spd = {
2003 .pages = pages,
2004 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002005 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08002006 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002007 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002008 .spd_release = sock_spd_release,
2009 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002010 int ret = 0;
2011
Tom Herbertfa9835e2016-03-07 14:11:04 -08002012 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002013
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002014 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002015 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002016
Jens Axboe35f3d142010-05-20 10:43:18 +02002017 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002018}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002019EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002020
Herbert Xu357b40a2005-04-19 22:30:14 -07002021/**
2022 * skb_store_bits - store bits from kernel buffer to skb
2023 * @skb: destination buffer
2024 * @offset: offset in destination
2025 * @from: source buffer
2026 * @len: number of bytes to copy
2027 *
2028 * Copy the specified number of bytes from the source buffer to the
2029 * destination skb. This function handles all the messy bits of
2030 * traversing fragment lists and such.
2031 */
2032
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002033int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002034{
David S. Miller1a028e52007-04-27 15:21:23 -07002035 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002036 struct sk_buff *frag_iter;
2037 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002038
2039 if (offset > (int)skb->len - len)
2040 goto fault;
2041
David S. Miller1a028e52007-04-27 15:21:23 -07002042 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002043 if (copy > len)
2044 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002045 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002046 if ((len -= copy) == 0)
2047 return 0;
2048 offset += copy;
2049 from += copy;
2050 }
2051
2052 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2053 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002054 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002055
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002056 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002057
Eric Dumazet9e903e02011-10-18 21:00:24 +00002058 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002059 if ((copy = end - offset) > 0) {
2060 u8 *vaddr;
2061
2062 if (copy > len)
2063 copy = len;
2064
Eric Dumazet51c56b02012-04-05 11:35:15 +02002065 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002066 memcpy(vaddr + frag->page_offset + offset - start,
2067 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002068 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002069
2070 if ((len -= copy) == 0)
2071 return 0;
2072 offset += copy;
2073 from += copy;
2074 }
David S. Miller1a028e52007-04-27 15:21:23 -07002075 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002076 }
2077
David S. Millerfbb398a2009-06-09 00:18:59 -07002078 skb_walk_frags(skb, frag_iter) {
2079 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002080
David S. Millerfbb398a2009-06-09 00:18:59 -07002081 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002082
David S. Millerfbb398a2009-06-09 00:18:59 -07002083 end = start + frag_iter->len;
2084 if ((copy = end - offset) > 0) {
2085 if (copy > len)
2086 copy = len;
2087 if (skb_store_bits(frag_iter, offset - start,
2088 from, copy))
2089 goto fault;
2090 if ((len -= copy) == 0)
2091 return 0;
2092 offset += copy;
2093 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002094 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002096 }
2097 if (!len)
2098 return 0;
2099
2100fault:
2101 return -EFAULT;
2102}
Herbert Xu357b40a2005-04-19 22:30:14 -07002103EXPORT_SYMBOL(skb_store_bits);
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002106__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2107 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
David S. Miller1a028e52007-04-27 15:21:23 -07002109 int start = skb_headlen(skb);
2110 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002111 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int pos = 0;
2113
2114 /* Checksum header. */
2115 if (copy > 0) {
2116 if (copy > len)
2117 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002118 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if ((len -= copy) == 0)
2120 return csum;
2121 offset += copy;
2122 pos = copy;
2123 }
2124
2125 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002126 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002127 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002129 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002130
Eric Dumazet51c56b02012-04-05 11:35:15 +02002131 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002133 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (copy > len)
2137 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002138 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002139 csum2 = ops->update(vaddr + frag->page_offset +
2140 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002141 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002142 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!(len -= copy))
2144 return csum;
2145 offset += copy;
2146 pos += copy;
2147 }
David S. Miller1a028e52007-04-27 15:21:23 -07002148 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
David S. Millerfbb398a2009-06-09 00:18:59 -07002151 skb_walk_frags(skb, frag_iter) {
2152 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David S. Millerfbb398a2009-06-09 00:18:59 -07002154 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
David S. Millerfbb398a2009-06-09 00:18:59 -07002156 end = start + frag_iter->len;
2157 if ((copy = end - offset) > 0) {
2158 __wsum csum2;
2159 if (copy > len)
2160 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002161 csum2 = __skb_checksum(frag_iter, offset - start,
2162 copy, 0, ops);
2163 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002164 if ((len -= copy) == 0)
2165 return csum;
2166 offset += copy;
2167 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002169 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002171 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 return csum;
2174}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002175EXPORT_SYMBOL(__skb_checksum);
2176
2177__wsum skb_checksum(const struct sk_buff *skb, int offset,
2178 int len, __wsum csum)
2179{
2180 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002181 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002182 .combine = csum_block_add_ext,
2183 };
2184
2185 return __skb_checksum(skb, offset, len, csum, &ops);
2186}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002187EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189/* Both of above in one bottle. */
2190
Al Viro81d77662006-11-14 21:37:33 -08002191__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2192 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
David S. Miller1a028e52007-04-27 15:21:23 -07002194 int start = skb_headlen(skb);
2195 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002196 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int pos = 0;
2198
2199 /* Copy header. */
2200 if (copy > 0) {
2201 if (copy > len)
2202 copy = len;
2203 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2204 copy, csum);
2205 if ((len -= copy) == 0)
2206 return csum;
2207 offset += copy;
2208 to += copy;
2209 pos = copy;
2210 }
2211
2212 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002213 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002215 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002216
Eric Dumazet9e903e02011-10-18 21:00:24 +00002217 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002219 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 u8 *vaddr;
2221 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2222
2223 if (copy > len)
2224 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002225 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002227 frag->page_offset +
2228 offset - start, to,
2229 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002230 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 csum = csum_block_add(csum, csum2, pos);
2232 if (!(len -= copy))
2233 return csum;
2234 offset += copy;
2235 to += copy;
2236 pos += copy;
2237 }
David S. Miller1a028e52007-04-27 15:21:23 -07002238 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
2240
David S. Millerfbb398a2009-06-09 00:18:59 -07002241 skb_walk_frags(skb, frag_iter) {
2242 __wsum csum2;
2243 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
David S. Millerfbb398a2009-06-09 00:18:59 -07002245 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
David S. Millerfbb398a2009-06-09 00:18:59 -07002247 end = start + frag_iter->len;
2248 if ((copy = end - offset) > 0) {
2249 if (copy > len)
2250 copy = len;
2251 csum2 = skb_copy_and_csum_bits(frag_iter,
2252 offset - start,
2253 to, copy, 0);
2254 csum = csum_block_add(csum, csum2, pos);
2255 if ((len -= copy) == 0)
2256 return csum;
2257 offset += copy;
2258 to += copy;
2259 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002261 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002263 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return csum;
2265}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002266EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Thomas Grafaf2806f2013-12-13 15:22:17 +01002268 /**
2269 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2270 * @from: source buffer
2271 *
2272 * Calculates the amount of linear headroom needed in the 'to' skb passed
2273 * into skb_zerocopy().
2274 */
2275unsigned int
2276skb_zerocopy_headlen(const struct sk_buff *from)
2277{
2278 unsigned int hlen = 0;
2279
2280 if (!from->head_frag ||
2281 skb_headlen(from) < L1_CACHE_BYTES ||
2282 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2283 hlen = skb_headlen(from);
2284
2285 if (skb_has_frag_list(from))
2286 hlen = from->len;
2287
2288 return hlen;
2289}
2290EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2291
2292/**
2293 * skb_zerocopy - Zero copy skb to skb
2294 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002295 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002296 * @len: number of bytes to copy from source buffer
2297 * @hlen: size of linear headroom in destination buffer
2298 *
2299 * Copies up to `len` bytes from `from` to `to` by creating references
2300 * to the frags in the source buffer.
2301 *
2302 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2303 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002304 *
2305 * Return value:
2306 * 0: everything is OK
2307 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2308 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002309 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002310int
2311skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002312{
2313 int i, j = 0;
2314 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002315 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002316 struct page *page;
2317 unsigned int offset;
2318
2319 BUG_ON(!from->head_frag && !hlen);
2320
2321 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002322 if (len <= skb_tailroom(to))
2323 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002324
2325 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002326 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2327 if (unlikely(ret))
2328 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002329 len -= hlen;
2330 } else {
2331 plen = min_t(int, skb_headlen(from), len);
2332 if (plen) {
2333 page = virt_to_head_page(from->head);
2334 offset = from->data - (unsigned char *)page_address(page);
2335 __skb_fill_page_desc(to, 0, page, offset, plen);
2336 get_page(page);
2337 j = 1;
2338 len -= plen;
2339 }
2340 }
2341
2342 to->truesize += len + plen;
2343 to->len += len + plen;
2344 to->data_len += len + plen;
2345
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002346 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2347 skb_tx_error(from);
2348 return -ENOMEM;
2349 }
2350
Thomas Grafaf2806f2013-12-13 15:22:17 +01002351 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2352 if (!len)
2353 break;
2354 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2355 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2356 len -= skb_shinfo(to)->frags[j].size;
2357 skb_frag_ref(to, j);
2358 j++;
2359 }
2360 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002361
2362 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002363}
2364EXPORT_SYMBOL_GPL(skb_zerocopy);
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2367{
Al Virod3bc23e2006-11-14 21:24:49 -08002368 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 long csstart;
2370
Patrick McHardy84fa7932006-08-29 16:44:56 -07002371 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002372 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 else
2374 csstart = skb_headlen(skb);
2375
Kris Katterjohn09a62662006-01-08 22:24:28 -08002376 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002378 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 csum = 0;
2381 if (csstart != skb->len)
2382 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2383 skb->len - csstart, 0);
2384
Patrick McHardy84fa7932006-08-29 16:44:56 -07002385 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002386 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Al Virod3bc23e2006-11-14 21:24:49 -08002388 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002391EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393/**
2394 * skb_dequeue - remove from the head of the queue
2395 * @list: list to dequeue from
2396 *
2397 * Remove the head of the list. The list lock is taken so the function
2398 * may be used safely with other locking list functions. The head item is
2399 * returned or %NULL if the list is empty.
2400 */
2401
2402struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2403{
2404 unsigned long flags;
2405 struct sk_buff *result;
2406
2407 spin_lock_irqsave(&list->lock, flags);
2408 result = __skb_dequeue(list);
2409 spin_unlock_irqrestore(&list->lock, flags);
2410 return result;
2411}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002412EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414/**
2415 * skb_dequeue_tail - remove from the tail of the queue
2416 * @list: list to dequeue from
2417 *
2418 * Remove the tail of the list. The list lock is taken so the function
2419 * may be used safely with other locking list functions. The tail item is
2420 * returned or %NULL if the list is empty.
2421 */
2422struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2423{
2424 unsigned long flags;
2425 struct sk_buff *result;
2426
2427 spin_lock_irqsave(&list->lock, flags);
2428 result = __skb_dequeue_tail(list);
2429 spin_unlock_irqrestore(&list->lock, flags);
2430 return result;
2431}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002432EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434/**
2435 * skb_queue_purge - empty a list
2436 * @list: list to empty
2437 *
2438 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2439 * the list and one reference dropped. This function takes the list
2440 * lock and is atomic with respect to other list locking functions.
2441 */
2442void skb_queue_purge(struct sk_buff_head *list)
2443{
2444 struct sk_buff *skb;
2445 while ((skb = skb_dequeue(list)) != NULL)
2446 kfree_skb(skb);
2447}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002448EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002451 * skb_rbtree_purge - empty a skb rbtree
2452 * @root: root of the rbtree to empty
Peter Oskolkov87169592018-10-10 12:30:09 -07002453 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002454 *
2455 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2456 * the list and one reference dropped. This function does not take
2457 * any lock. Synchronization should be handled by the caller (e.g., TCP
2458 * out-of-order queue is protected by the socket lock).
2459 */
Peter Oskolkov87169592018-10-10 12:30:09 -07002460unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002461{
Eric Dumazetf5d17b52018-10-10 12:30:08 -07002462 struct rb_node *p = rb_first(root);
Peter Oskolkov87169592018-10-10 12:30:09 -07002463 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002464
Eric Dumazetf5d17b52018-10-10 12:30:08 -07002465 while (p) {
2466 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2467
2468 p = rb_next(p);
2469 rb_erase(&skb->rbnode, root);
Peter Oskolkov87169592018-10-10 12:30:09 -07002470 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002471 kfree_skb(skb);
Eric Dumazetf5d17b52018-10-10 12:30:08 -07002472 }
Peter Oskolkov87169592018-10-10 12:30:09 -07002473 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002474}
2475
2476/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 * skb_queue_head - queue a buffer at the list head
2478 * @list: list to use
2479 * @newsk: buffer to queue
2480 *
2481 * Queue a buffer at the start of the list. This function takes the
2482 * list lock and can be used safely with other locking &sk_buff functions
2483 * safely.
2484 *
2485 * A buffer cannot be placed on two lists at the same time.
2486 */
2487void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2488{
2489 unsigned long flags;
2490
2491 spin_lock_irqsave(&list->lock, flags);
2492 __skb_queue_head(list, newsk);
2493 spin_unlock_irqrestore(&list->lock, flags);
2494}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002495EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497/**
2498 * skb_queue_tail - queue a buffer at the list tail
2499 * @list: list to use
2500 * @newsk: buffer to queue
2501 *
2502 * Queue a buffer at the tail of the list. This function takes the
2503 * list lock and can be used safely with other locking &sk_buff functions
2504 * safely.
2505 *
2506 * A buffer cannot be placed on two lists at the same time.
2507 */
2508void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2509{
2510 unsigned long flags;
2511
2512 spin_lock_irqsave(&list->lock, flags);
2513 __skb_queue_tail(list, newsk);
2514 spin_unlock_irqrestore(&list->lock, flags);
2515}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002516EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518/**
2519 * skb_unlink - remove a buffer from a list
2520 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002521 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 *
David S. Miller8728b832005-08-09 19:25:21 -07002523 * Remove a packet from a list. The list locks are taken and this
2524 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 *
David S. Miller8728b832005-08-09 19:25:21 -07002526 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 */
David S. Miller8728b832005-08-09 19:25:21 -07002528void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
David S. Miller8728b832005-08-09 19:25:21 -07002530 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
David S. Miller8728b832005-08-09 19:25:21 -07002532 spin_lock_irqsave(&list->lock, flags);
2533 __skb_unlink(skb, list);
2534 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002536EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538/**
2539 * skb_append - append a buffer
2540 * @old: buffer to insert after
2541 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002542 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 *
2544 * Place a packet after a given packet in a list. The list locks are taken
2545 * and this function is atomic with respect to other list locked calls.
2546 * A buffer cannot be placed on two lists at the same time.
2547 */
David S. Miller8728b832005-08-09 19:25:21 -07002548void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 unsigned long flags;
2551
David S. Miller8728b832005-08-09 19:25:21 -07002552 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002553 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002554 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002556EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558/**
2559 * skb_insert - insert a buffer
2560 * @old: buffer to insert before
2561 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002562 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 *
David S. Miller8728b832005-08-09 19:25:21 -07002564 * Place a packet before a given packet in a list. The list locks are
2565 * taken and this function is atomic with respect to other list locked
2566 * calls.
2567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * A buffer cannot be placed on two lists at the same time.
2569 */
David S. Miller8728b832005-08-09 19:25:21 -07002570void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 unsigned long flags;
2573
David S. Miller8728b832005-08-09 19:25:21 -07002574 spin_lock_irqsave(&list->lock, flags);
2575 __skb_insert(newsk, old->prev, old, list);
2576 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002578EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580static inline void skb_split_inside_header(struct sk_buff *skb,
2581 struct sk_buff* skb1,
2582 const u32 len, const int pos)
2583{
2584 int i;
2585
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002586 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2587 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /* And move data appendix as is. */
2589 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2590 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2591
2592 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2593 skb_shinfo(skb)->nr_frags = 0;
2594 skb1->data_len = skb->data_len;
2595 skb1->len += skb1->data_len;
2596 skb->data_len = 0;
2597 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002598 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static inline void skb_split_no_header(struct sk_buff *skb,
2602 struct sk_buff* skb1,
2603 const u32 len, int pos)
2604{
2605 int i, k = 0;
2606 const int nfrags = skb_shinfo(skb)->nr_frags;
2607
2608 skb_shinfo(skb)->nr_frags = 0;
2609 skb1->len = skb1->data_len = skb->len - len;
2610 skb->len = len;
2611 skb->data_len = len - pos;
2612
2613 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002614 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (pos + size > len) {
2617 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2618
2619 if (pos < len) {
2620 /* Split frag.
2621 * We have two variants in this case:
2622 * 1. Move all the frag to the second
2623 * part, if it is possible. F.e.
2624 * this approach is mandatory for TUX,
2625 * where splitting is expensive.
2626 * 2. Split is accurately. We make this.
2627 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002628 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002630 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2631 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 skb_shinfo(skb)->nr_frags++;
2633 }
2634 k++;
2635 } else
2636 skb_shinfo(skb)->nr_frags++;
2637 pos += size;
2638 }
2639 skb_shinfo(skb1)->nr_frags = k;
2640}
2641
2642/**
2643 * skb_split - Split fragmented skb to two parts at length len.
2644 * @skb: the buffer to split
2645 * @skb1: the buffer to receive the second part
2646 * @len: new length for skb
2647 */
2648void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2649{
2650 int pos = skb_headlen(skb);
2651
Willem de Bruijnfcfb53c2017-06-08 11:35:03 -04002652 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2653 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (len < pos) /* Split line is inside header. */
2655 skb_split_inside_header(skb, skb1, len, pos);
2656 else /* Second chunk has no header, nothing to copy. */
2657 skb_split_no_header(skb, skb1, len, pos);
2658}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002659EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002661/* Shifting from/to a cloned skb is a no-go.
2662 *
2663 * Caller cannot keep skb_shinfo related pointers past calling here!
2664 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002665static int skb_prepare_for_shift(struct sk_buff *skb)
2666{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002667 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002668}
2669
2670/**
2671 * skb_shift - Shifts paged data partially from skb to another
2672 * @tgt: buffer into which tail data gets added
2673 * @skb: buffer from which the paged data comes from
2674 * @shiftlen: shift up to this many bytes
2675 *
2676 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002677 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002678 * It's up to caller to free skb if everything was shifted.
2679 *
2680 * If @tgt runs out of frags, the whole operation is aborted.
2681 *
2682 * Skb cannot include anything else but paged data while tgt is allowed
2683 * to have non-paged data as well.
2684 *
2685 * TODO: full sized shift could be optimized but that would need
2686 * specialized skb free'er to handle frags without up-to-date nr_frags.
2687 */
2688int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2689{
2690 int from, to, merge, todo;
2691 struct skb_frag_struct *fragfrom, *fragto;
2692
2693 BUG_ON(shiftlen > skb->len);
2694 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2695
2696 todo = shiftlen;
2697 from = 0;
2698 to = skb_shinfo(tgt)->nr_frags;
2699 fragfrom = &skb_shinfo(skb)->frags[from];
2700
2701 /* Actual merge is delayed until the point when we know we can
2702 * commit all, so that we don't have to undo partial changes
2703 */
2704 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002705 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2706 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002707 merge = -1;
2708 } else {
2709 merge = to - 1;
2710
Eric Dumazet9e903e02011-10-18 21:00:24 +00002711 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002712 if (todo < 0) {
2713 if (skb_prepare_for_shift(skb) ||
2714 skb_prepare_for_shift(tgt))
2715 return 0;
2716
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002717 /* All previous frag pointers might be stale! */
2718 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002719 fragto = &skb_shinfo(tgt)->frags[merge];
2720
Eric Dumazet9e903e02011-10-18 21:00:24 +00002721 skb_frag_size_add(fragto, shiftlen);
2722 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002723 fragfrom->page_offset += shiftlen;
2724
2725 goto onlymerged;
2726 }
2727
2728 from++;
2729 }
2730
2731 /* Skip full, not-fitting skb to avoid expensive operations */
2732 if ((shiftlen == skb->len) &&
2733 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2734 return 0;
2735
2736 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2737 return 0;
2738
2739 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2740 if (to == MAX_SKB_FRAGS)
2741 return 0;
2742
2743 fragfrom = &skb_shinfo(skb)->frags[from];
2744 fragto = &skb_shinfo(tgt)->frags[to];
2745
Eric Dumazet9e903e02011-10-18 21:00:24 +00002746 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002747 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002748 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002749 from++;
2750 to++;
2751
2752 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002753 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002754 fragto->page = fragfrom->page;
2755 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002756 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002757
2758 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002759 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002760 todo = 0;
2761
2762 to++;
2763 break;
2764 }
2765 }
2766
2767 /* Ready to "commit" this state change to tgt */
2768 skb_shinfo(tgt)->nr_frags = to;
2769
2770 if (merge >= 0) {
2771 fragfrom = &skb_shinfo(skb)->frags[0];
2772 fragto = &skb_shinfo(tgt)->frags[merge];
2773
Eric Dumazet9e903e02011-10-18 21:00:24 +00002774 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002775 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002776 }
2777
2778 /* Reposition in the original skb */
2779 to = 0;
2780 while (from < skb_shinfo(skb)->nr_frags)
2781 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2782 skb_shinfo(skb)->nr_frags = to;
2783
2784 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2785
2786onlymerged:
2787 /* Most likely the tgt won't ever need its checksum anymore, skb on
2788 * the other hand might need it if it needs to be resent
2789 */
2790 tgt->ip_summed = CHECKSUM_PARTIAL;
2791 skb->ip_summed = CHECKSUM_PARTIAL;
2792
2793 /* Yak, is it really working this way? Some helper please? */
2794 skb->len -= shiftlen;
2795 skb->data_len -= shiftlen;
2796 skb->truesize -= shiftlen;
2797 tgt->len += shiftlen;
2798 tgt->data_len += shiftlen;
2799 tgt->truesize += shiftlen;
2800
2801 return shiftlen;
2802}
2803
Thomas Graf677e90e2005-06-23 20:59:51 -07002804/**
2805 * skb_prepare_seq_read - Prepare a sequential read of skb data
2806 * @skb: the buffer to read
2807 * @from: lower offset of data to be read
2808 * @to: upper offset of data to be read
2809 * @st: state variable
2810 *
2811 * Initializes the specified state variable. Must be called before
2812 * invoking skb_seq_read() for the first time.
2813 */
2814void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2815 unsigned int to, struct skb_seq_state *st)
2816{
2817 st->lower_offset = from;
2818 st->upper_offset = to;
2819 st->root_skb = st->cur_skb = skb;
2820 st->frag_idx = st->stepped_offset = 0;
2821 st->frag_data = NULL;
2822}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002823EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002824
2825/**
2826 * skb_seq_read - Sequentially read skb data
2827 * @consumed: number of bytes consumed by the caller so far
2828 * @data: destination pointer for data to be returned
2829 * @st: state variable
2830 *
Mathias Krausebc323832013-11-07 14:18:26 +01002831 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002832 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002833 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002834 * of the block or 0 if the end of the skb data or the upper
2835 * offset has been reached.
2836 *
2837 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002838 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002839 * of bytes already consumed and the next call to
2840 * skb_seq_read() will return the remaining part of the block.
2841 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002842 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002843 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002844 * reads of potentially non linear data.
2845 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002846 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002847 * at the moment, state->root_skb could be replaced with
2848 * a stack for this purpose.
2849 */
2850unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2851 struct skb_seq_state *st)
2852{
2853 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2854 skb_frag_t *frag;
2855
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002856 if (unlikely(abs_offset >= st->upper_offset)) {
2857 if (st->frag_data) {
2858 kunmap_atomic(st->frag_data);
2859 st->frag_data = NULL;
2860 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002861 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002862 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002863
2864next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002865 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002866
Thomas Chenault995b3372009-05-18 21:43:27 -07002867 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002868 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002869 return block_limit - abs_offset;
2870 }
2871
2872 if (st->frag_idx == 0 && !st->frag_data)
2873 st->stepped_offset += skb_headlen(st->cur_skb);
2874
2875 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2876 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002877 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002878
2879 if (abs_offset < block_limit) {
2880 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002881 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002882
2883 *data = (u8 *) st->frag_data + frag->page_offset +
2884 (abs_offset - st->stepped_offset);
2885
2886 return block_limit - abs_offset;
2887 }
2888
2889 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002890 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002891 st->frag_data = NULL;
2892 }
2893
2894 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002895 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002896 }
2897
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002898 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002899 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002900 st->frag_data = NULL;
2901 }
2902
David S. Miller21dc3302010-08-23 00:13:46 -07002903 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002904 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002905 st->frag_idx = 0;
2906 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002907 } else if (st->cur_skb->next) {
2908 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002909 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002910 goto next_skb;
2911 }
2912
2913 return 0;
2914}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002915EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002916
2917/**
2918 * skb_abort_seq_read - Abort a sequential read of skb data
2919 * @st: state variable
2920 *
2921 * Must be called if skb_seq_read() was not called until it
2922 * returned 0.
2923 */
2924void skb_abort_seq_read(struct skb_seq_state *st)
2925{
2926 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002927 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002928}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002929EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002930
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002931#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2932
2933static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2934 struct ts_config *conf,
2935 struct ts_state *state)
2936{
2937 return skb_seq_read(offset, text, TS_SKB_CB(state));
2938}
2939
2940static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2941{
2942 skb_abort_seq_read(TS_SKB_CB(state));
2943}
2944
2945/**
2946 * skb_find_text - Find a text pattern in skb data
2947 * @skb: the buffer to look in
2948 * @from: search offset
2949 * @to: search limit
2950 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002951 *
2952 * Finds a pattern in the skb data according to the specified
2953 * textsearch configuration. Use textsearch_next() to retrieve
2954 * subsequent occurrences of the pattern. Returns the offset
2955 * to the first occurrence or UINT_MAX if no match was found.
2956 */
2957unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002958 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002959{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002960 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002961 unsigned int ret;
2962
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002963 config->get_next_block = skb_ts_get_next_block;
2964 config->finish = skb_ts_finish;
2965
Bojan Prtvar059a2442015-02-22 11:46:35 +01002966 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002967
Bojan Prtvar059a2442015-02-22 11:46:35 +01002968 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002969 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002970}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002971EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002972
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002973/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002974 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002975 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002976 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002977 * @getfrag: call back function to be used for getting the user data
2978 * @from: pointer to user message iov
2979 * @length: length of the iov message
2980 *
2981 * Description: This procedure append the user data in the fragment part
2982 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2983 */
2984int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002985 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002986 int len, int odd, struct sk_buff *skb),
2987 void *from, int length)
2988{
Eric Dumazetb2111722012-12-28 06:06:37 +00002989 int frg_cnt = skb_shinfo(skb)->nr_frags;
2990 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002991 int offset = 0;
2992 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002993 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002994
2995 do {
2996 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002997 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002998 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002999
Eric Dumazetb2111722012-12-28 06:06:37 +00003000 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003001 return -ENOMEM;
3002
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003003 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00003004 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003005
Eric Dumazetb2111722012-12-28 06:06:37 +00003006 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3007 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003008 if (ret < 0)
3009 return -EFAULT;
3010
3011 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003012 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3013 copy);
3014 frg_cnt++;
3015 pfrag->offset += copy;
3016 get_page(pfrag->page);
3017
3018 skb->truesize += copy;
3019 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003020 skb->len += copy;
3021 skb->data_len += copy;
3022 offset += copy;
3023 length -= copy;
3024
3025 } while (length > 0);
3026
3027 return 0;
3028}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003029EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003030
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003031int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3032 int offset, size_t size)
3033{
3034 int i = skb_shinfo(skb)->nr_frags;
3035
3036 if (skb_can_coalesce(skb, i, page, offset)) {
3037 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3038 } else if (i < MAX_SKB_FRAGS) {
3039 get_page(page);
3040 skb_fill_page_desc(skb, i, page, offset, size);
3041 } else {
3042 return -EMSGSIZE;
3043 }
3044
3045 return 0;
3046}
3047EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3048
Herbert Xucbb042f2006-03-20 22:43:56 -08003049/**
3050 * skb_pull_rcsum - pull skb and update receive checksum
3051 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003052 * @len: length of data pulled
3053 *
3054 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003055 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003056 * receive path processing instead of skb_pull unless you know
3057 * that the checksum difference is zero (e.g., a valid IP header)
3058 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003059 */
3060unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3061{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003062 unsigned char *data = skb->data;
3063
Herbert Xucbb042f2006-03-20 22:43:56 -08003064 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003065 __skb_pull(skb, len);
3066 skb_postpull_rcsum(skb, data, len);
3067 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003068}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003069EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3070
Herbert Xuf4c50d92006-06-22 03:02:40 -07003071/**
3072 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003073 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003074 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003075 *
3076 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003077 * a pointer to the first in a list of new skbs for the segments.
3078 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003079 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003080struct sk_buff *skb_segment(struct sk_buff *head_skb,
3081 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003082{
3083 struct sk_buff *segs = NULL;
3084 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003085 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003086 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3087 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3088 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003089 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003090 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003091 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003092 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003093 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003094 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003095 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003096 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003097 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003098 int err = -ENOMEM;
3099 int i = 0;
3100 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003101 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003102
Shmulik Ladkani162a5a82019-09-06 12:23:50 +03003103 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3104 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3105 /* gso_size is untrusted, and we have a frag_list with a linear
3106 * non head_frag head.
3107 *
3108 * (we assume checking the first list_skb member suffices;
3109 * i.e if either of the list_skb members have non head_frag
3110 * head, then the first one has too).
3111 *
3112 * If head_skb's headlen does not fit requested gso_size, it
3113 * means that the frag_list members do NOT terminate on exact
3114 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3115 * sharing. Therefore we must fallback to copying the frag_list
3116 * skbs; we do so by disabling SG.
3117 */
3118 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3119 features &= ~NETIF_F_SG;
3120 }
3121
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003122 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003123 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003124 if (unlikely(!proto))
3125 return ERR_PTR(-EINVAL);
3126
Alexander Duyck36c98382016-05-02 09:38:18 -07003127 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003128 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003129
Steffen Klassert07b26c92016-09-19 12:58:47 +02003130 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3131 if (!(features & NETIF_F_GSO_PARTIAL)) {
3132 struct sk_buff *iter;
Ilan Tayari7ab89b1762017-04-19 21:26:07 +03003133 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003134
3135 if (!list_skb ||
3136 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3137 goto normal;
3138
Ilan Tayari7ab89b1762017-04-19 21:26:07 +03003139 /* If we get here then all the required
3140 * GSO features except frag_list are supported.
3141 * Try to split the SKB to multiple GSO SKBs
3142 * with no frag_list.
3143 * Currently we can do that only when the buffers don't
3144 * have a linear part and all the buffers except
3145 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003146 */
Ilan Tayari7ab89b1762017-04-19 21:26:07 +03003147 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003148 skb_walk_frags(head_skb, iter) {
Ilan Tayari7ab89b1762017-04-19 21:26:07 +03003149 if (frag_len != iter->len && iter->next)
3150 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003151 if (skb_headlen(iter))
3152 goto normal;
3153
3154 len -= iter->len;
3155 }
Ilan Tayari7ab89b1762017-04-19 21:26:07 +03003156
3157 if (len != frag_len)
3158 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003159 }
3160
3161 /* GSO partial only requires that we trim off any excess that
3162 * doesn't fit into an MSS sized block, so take care of that
3163 * now.
3164 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003165 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003166 if (partial_segs > 1)
3167 mss *= partial_segs;
3168 else
3169 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003170 }
3171
Steffen Klassert07b26c92016-09-19 12:58:47 +02003172normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003173 headroom = skb_headroom(head_skb);
3174 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003175
3176 do {
3177 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003178 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003179 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003180 int size;
3181
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003182 if (unlikely(mss == GSO_BY_FRAGS)) {
3183 len = list_skb->len;
3184 } else {
3185 len = head_skb->len - offset;
3186 if (len > mss)
3187 len = mss;
3188 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003189
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003190 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003191 if (hsize < 0)
3192 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003193 if (hsize > len || !sg)
3194 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003195
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003196 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3197 (skb_headlen(list_skb) == len || sg)) {
3198 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003199
Herbert Xu9d8506c2013-11-21 11:10:04 -08003200 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003201 nfrags = skb_shinfo(list_skb)->nr_frags;
3202 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003203 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003204 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003205
3206 while (pos < offset + len) {
3207 BUG_ON(i >= nfrags);
3208
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003209 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003210 if (pos + size > offset + len)
3211 break;
3212
3213 i++;
3214 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003215 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003216 }
3217
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003218 nskb = skb_clone(list_skb, GFP_ATOMIC);
3219 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003220
3221 if (unlikely(!nskb))
3222 goto err;
3223
Herbert Xu9d8506c2013-11-21 11:10:04 -08003224 if (unlikely(pskb_trim(nskb, len))) {
3225 kfree_skb(nskb);
3226 goto err;
3227 }
3228
Alexander Duyckec47ea82012-05-04 14:26:56 +00003229 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003230 if (skb_cow_head(nskb, doffset + headroom)) {
3231 kfree_skb(nskb);
3232 goto err;
3233 }
3234
Alexander Duyckec47ea82012-05-04 14:26:56 +00003235 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003236 skb_release_head_state(nskb);
3237 __skb_push(nskb, doffset);
3238 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003239 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003240 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003241 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003242
3243 if (unlikely(!nskb))
3244 goto err;
3245
3246 skb_reserve(nskb, headroom);
3247 __skb_put(nskb, doffset);
3248 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003249
3250 if (segs)
3251 tail->next = nskb;
3252 else
3253 segs = nskb;
3254 tail = nskb;
3255
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003256 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003257
Eric Dumazet030737b2013-10-19 11:42:54 -07003258 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003259 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003260
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003261 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003262 nskb->data - tnl_hlen,
3263 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003264
Herbert Xu9d8506c2013-11-21 11:10:04 -08003265 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003266 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003267
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003268 if (!sg) {
3269 if (!nskb->remcsum_offload)
3270 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003271 SKB_GSO_CB(nskb)->csum =
3272 skb_copy_and_csum_bits(head_skb, offset,
3273 skb_put(nskb, len),
3274 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003275 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003276 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003277 continue;
3278 }
3279
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003280 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003281
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003282 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003283 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003284
Willem de Bruijnfcfb53c2017-06-08 11:35:03 -04003285 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3286 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003287
Herbert Xu9d8506c2013-11-21 11:10:04 -08003288 while (pos < offset + len) {
3289 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003290 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003291
3292 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003293 nfrags = skb_shinfo(list_skb)->nr_frags;
3294 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003295 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003296
3297 BUG_ON(!nfrags);
3298
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003299 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003300 }
3301
3302 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3303 MAX_SKB_FRAGS)) {
3304 net_warn_ratelimited(
3305 "skb_segment: too many frags: %u %u\n",
3306 pos, mss);
Eric Dumazete2ffdd62018-07-19 16:04:38 -07003307 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003308 goto err;
3309 }
3310
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003311 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3312 goto err;
3313
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003314 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003315 __skb_frag_ref(nskb_frag);
3316 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003317
3318 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003319 nskb_frag->page_offset += offset - pos;
3320 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003321 }
3322
Herbert Xu89319d382008-12-15 23:26:06 -08003323 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003324
3325 if (pos + size <= offset + len) {
3326 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003327 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003328 pos += size;
3329 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003330 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003331 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003332 }
3333
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003334 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003335 }
3336
Herbert Xu89319d382008-12-15 23:26:06 -08003337skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003338 nskb->data_len = len - hsize;
3339 nskb->len += nskb->data_len;
3340 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003341
Simon Horman1cdbcb72013-05-19 15:46:49 +00003342perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003343 if (!csum) {
Eric Dumazete2ffdd62018-07-19 16:04:38 -07003344 if (skb_has_shared_frag(nskb) &&
3345 __skb_linearize(nskb))
3346 goto err;
3347
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003348 if (!nskb->remcsum_offload)
3349 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003350 SKB_GSO_CB(nskb)->csum =
3351 skb_checksum(nskb, doffset,
3352 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003353 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003354 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003355 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003356 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003357
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003358 /* Some callers want to get the end of the list.
3359 * Put it in segs->prev to avoid walking the list.
3360 * (see validate_xmit_skb_list() for example)
3361 */
3362 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003363
Alexander Duyck802ab552016-04-10 21:45:03 -04003364 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003365 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003366 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003367 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003368
3369 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003370 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003371 type &= ~SKB_GSO_DODGY;
3372
3373 /* Update GSO info and prepare to start updating headers on
3374 * our way back down the stack of protocols.
3375 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003376 for (iter = segs; iter; iter = iter->next) {
3377 skb_shinfo(iter)->gso_size = gso_size;
3378 skb_shinfo(iter)->gso_segs = partial_segs;
3379 skb_shinfo(iter)->gso_type = type;
3380 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3381 }
3382
3383 if (tail->len - doffset <= gso_size)
3384 skb_shinfo(tail)->gso_size = 0;
3385 else if (tail != segs)
3386 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003387 }
3388
Toshiaki Makita432c8562014-10-27 10:30:51 -07003389 /* Following permits correct backpressure, for protocols
3390 * using skb_set_owner_w().
3391 * Idea is to tranfert ownership from head_skb to last segment.
3392 */
3393 if (head_skb->destructor == sock_wfree) {
3394 swap(tail->truesize, head_skb->truesize);
3395 swap(tail->destructor, head_skb->destructor);
3396 swap(tail->sk, head_skb->sk);
3397 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003398 return segs;
3399
3400err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003401 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003402 return ERR_PTR(err);
3403}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003404EXPORT_SYMBOL_GPL(skb_segment);
3405
Herbert Xu71d93b32008-12-15 23:42:33 -08003406int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3407{
Eric Dumazet8a291112013-10-08 09:02:23 -07003408 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003409 unsigned int offset = skb_gro_offset(skb);
3410 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003411 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003412 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003413 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003414
Eric Dumazet8a291112013-10-08 09:02:23 -07003415 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003416 return -E2BIG;
3417
Eric Dumazet29e98242014-05-16 11:34:37 -07003418 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003419 pinfo = skb_shinfo(lp);
3420
3421 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003422 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003423 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003424 int i = skbinfo->nr_frags;
3425 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003426
Herbert Xu66e92fc2009-05-26 18:50:32 +00003427 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003428 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003429
Eric Dumazet8a291112013-10-08 09:02:23 -07003430 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003431 pinfo->nr_frags = nr_frags;
3432 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003433
Herbert Xu9aaa1562009-05-26 18:50:33 +00003434 frag = pinfo->frags + nr_frags;
3435 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003436 do {
3437 *--frag = *--frag2;
3438 } while (--i);
3439
3440 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003441 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003442
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003443 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003444 delta_truesize = skb->truesize -
3445 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003446
Herbert Xuf5572062009-01-14 20:40:03 -08003447 skb->truesize -= skb->data_len;
3448 skb->len -= skb->data_len;
3449 skb->data_len = 0;
3450
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003451 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003452 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003453 } else if (skb->head_frag) {
3454 int nr_frags = pinfo->nr_frags;
3455 skb_frag_t *frag = pinfo->frags + nr_frags;
3456 struct page *page = virt_to_head_page(skb->head);
3457 unsigned int first_size = headlen - offset;
3458 unsigned int first_offset;
3459
3460 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003461 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003462
3463 first_offset = skb->data -
3464 (unsigned char *)page_address(page) +
3465 offset;
3466
3467 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3468
3469 frag->page.p = page;
3470 frag->page_offset = first_offset;
3471 skb_frag_size_set(frag, first_size);
3472
3473 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3474 /* We dont need to clear skbinfo->nr_frags here */
3475
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003476 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003477 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3478 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003479 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003480
3481merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003482 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003483 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003484 unsigned int eat = offset - headlen;
3485
3486 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003487 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003488 skb->data_len -= eat;
3489 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003490 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003491 }
3492
Herbert Xu67147ba2009-05-26 18:50:22 +00003493 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003494
Eric Dumazet29e98242014-05-16 11:34:37 -07003495 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003496 skb_shinfo(p)->frag_list = skb;
3497 else
3498 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003499 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003500 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003501 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003502
Herbert Xu5d38a072009-01-04 16:13:40 -08003503done:
3504 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003505 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003506 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003507 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003508 if (lp != p) {
3509 lp->data_len += len;
3510 lp->truesize += delta_truesize;
3511 lp->len += len;
3512 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003513 NAPI_GRO_CB(skb)->same_flow = 1;
3514 return 0;
3515}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003516EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518void __init skb_init(void)
3519{
3520 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3521 sizeof(struct sk_buff),
3522 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003523 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003524 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003525 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003526 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003527 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003528 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003529 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
David S. Miller51c739d2007-10-30 21:29:29 -07003532static int
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003533__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3534 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003535{
David S. Miller1a028e52007-04-27 15:21:23 -07003536 int start = skb_headlen(skb);
3537 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003538 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003539 int elt = 0;
3540
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003541 if (unlikely(recursion_level >= 24))
3542 return -EMSGSIZE;
3543
David Howells716ea3a2007-04-02 20:19:53 -07003544 if (copy > 0) {
3545 if (copy > len)
3546 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003547 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003548 elt++;
3549 if ((len -= copy) == 0)
3550 return elt;
3551 offset += copy;
3552 }
3553
3554 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003555 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003556
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003557 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003558
Eric Dumazet9e903e02011-10-18 21:00:24 +00003559 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003560 if ((copy = end - offset) > 0) {
3561 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003562 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3563 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003564
3565 if (copy > len)
3566 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003567 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003568 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003569 elt++;
3570 if (!(len -= copy))
3571 return elt;
3572 offset += copy;
3573 }
David S. Miller1a028e52007-04-27 15:21:23 -07003574 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003575 }
3576
David S. Millerfbb398a2009-06-09 00:18:59 -07003577 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003578 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003579
David S. Millerfbb398a2009-06-09 00:18:59 -07003580 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003581
David S. Millerfbb398a2009-06-09 00:18:59 -07003582 end = start + frag_iter->len;
3583 if ((copy = end - offset) > 0) {
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003584 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3585 return -EMSGSIZE;
3586
David S. Millerfbb398a2009-06-09 00:18:59 -07003587 if (copy > len)
3588 copy = len;
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003589 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3590 copy, recursion_level + 1);
3591 if (unlikely(ret < 0))
3592 return ret;
3593 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003594 if ((len -= copy) == 0)
3595 return elt;
3596 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003597 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003598 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003599 }
3600 BUG_ON(len);
3601 return elt;
3602}
3603
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003604/**
3605 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3606 * @skb: Socket buffer containing the buffers to be mapped
3607 * @sg: The scatter-gather list to map into
3608 * @offset: The offset into the buffer's contents to start mapping
3609 * @len: Length of buffer space to be mapped
3610 *
3611 * Fill the specified scatter-gather list with mappings/pointers into a
3612 * region of the buffer space attached to a socket buffer. Returns either
3613 * the number of scatterlist items used, or -EMSGSIZE if the contents
3614 * could not fit.
3615 */
3616int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3617{
3618 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3619
3620 if (nsg <= 0)
3621 return nsg;
3622
3623 sg_mark_end(&sg[nsg - 1]);
3624
3625 return nsg;
3626}
3627EXPORT_SYMBOL_GPL(skb_to_sgvec);
3628
Fan Du25a91d82014-01-18 09:54:23 +08003629/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3630 * sglist without mark the sg which contain last skb data as the end.
3631 * So the caller can mannipulate sg list as will when padding new data after
3632 * the first call without calling sg_unmark_end to expend sg list.
3633 *
3634 * Scenario to use skb_to_sgvec_nomark:
3635 * 1. sg_init_table
3636 * 2. skb_to_sgvec_nomark(payload1)
3637 * 3. skb_to_sgvec_nomark(payload2)
3638 *
3639 * This is equivalent to:
3640 * 1. sg_init_table
3641 * 2. skb_to_sgvec(payload1)
3642 * 3. sg_unmark_end
3643 * 4. skb_to_sgvec(payload2)
3644 *
3645 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3646 * is more preferable.
3647 */
3648int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3649 int offset, int len)
3650{
Jason A. Donenfeld2cd3aa52017-06-04 04:16:22 +02003651 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003652}
3653EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3654
David S. Miller51c739d2007-10-30 21:29:29 -07003655
David S. Miller51c739d2007-10-30 21:29:29 -07003656
David Howells716ea3a2007-04-02 20:19:53 -07003657/**
3658 * skb_cow_data - Check that a socket buffer's data buffers are writable
3659 * @skb: The socket buffer to check.
3660 * @tailbits: Amount of trailing space to be added
3661 * @trailer: Returned pointer to the skb where the @tailbits space begins
3662 *
3663 * Make sure that the data buffers attached to a socket buffer are
3664 * writable. If they are not, private copies are made of the data buffers
3665 * and the socket buffer is set to use these instead.
3666 *
3667 * If @tailbits is given, make sure that there is space to write @tailbits
3668 * bytes of data beyond current end of socket buffer. @trailer will be
3669 * set to point to the skb in which this space begins.
3670 *
3671 * The number of scatterlist elements required to completely map the
3672 * COW'd and extended socket buffer will be returned.
3673 */
3674int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3675{
3676 int copyflag;
3677 int elt;
3678 struct sk_buff *skb1, **skb_p;
3679
3680 /* If skb is cloned or its head is paged, reallocate
3681 * head pulling out all the pages (pages are considered not writable
3682 * at the moment even if they are anonymous).
3683 */
3684 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3685 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3686 return -ENOMEM;
3687
3688 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003689 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003690 /* A little of trouble, not enough of space for trailer.
3691 * This should not happen, when stack is tuned to generate
3692 * good frames. OK, on miss we reallocate and reserve even more
3693 * space, 128 bytes is fair. */
3694
3695 if (skb_tailroom(skb) < tailbits &&
3696 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3697 return -ENOMEM;
3698
3699 /* Voila! */
3700 *trailer = skb;
3701 return 1;
3702 }
3703
3704 /* Misery. We are in troubles, going to mincer fragments... */
3705
3706 elt = 1;
3707 skb_p = &skb_shinfo(skb)->frag_list;
3708 copyflag = 0;
3709
3710 while ((skb1 = *skb_p) != NULL) {
3711 int ntail = 0;
3712
3713 /* The fragment is partially pulled by someone,
3714 * this can happen on input. Copy it and everything
3715 * after it. */
3716
3717 if (skb_shared(skb1))
3718 copyflag = 1;
3719
3720 /* If the skb is the last, worry about trailer. */
3721
3722 if (skb1->next == NULL && tailbits) {
3723 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003724 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003725 skb_tailroom(skb1) < tailbits)
3726 ntail = tailbits + 128;
3727 }
3728
3729 if (copyflag ||
3730 skb_cloned(skb1) ||
3731 ntail ||
3732 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003733 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003734 struct sk_buff *skb2;
3735
3736 /* Fuck, we are miserable poor guys... */
3737 if (ntail == 0)
3738 skb2 = skb_copy(skb1, GFP_ATOMIC);
3739 else
3740 skb2 = skb_copy_expand(skb1,
3741 skb_headroom(skb1),
3742 ntail,
3743 GFP_ATOMIC);
3744 if (unlikely(skb2 == NULL))
3745 return -ENOMEM;
3746
3747 if (skb1->sk)
3748 skb_set_owner_w(skb2, skb1->sk);
3749
3750 /* Looking around. Are we still alive?
3751 * OK, link new skb, drop old one */
3752
3753 skb2->next = skb1->next;
3754 *skb_p = skb2;
3755 kfree_skb(skb1);
3756 skb1 = skb2;
3757 }
3758 elt++;
3759 *trailer = skb1;
3760 skb_p = &skb1->next;
3761 }
3762
3763 return elt;
3764}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003765EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003766
Eric Dumazetb1faf562010-05-31 23:44:05 -07003767static void sock_rmem_free(struct sk_buff *skb)
3768{
3769 struct sock *sk = skb->sk;
3770
3771 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3772}
3773
3774/*
3775 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3776 */
3777int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3778{
3779 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003780 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003781 return -ENOMEM;
3782
3783 skb_orphan(skb);
3784 skb->sk = sk;
3785 skb->destructor = sock_rmem_free;
3786 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3787
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003788 /* before exiting rcu section, make sure dst is refcounted */
3789 skb_dst_force(skb);
3790
Eric Dumazetb1faf562010-05-31 23:44:05 -07003791 skb_queue_tail(&sk->sk_error_queue, skb);
3792 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomesd5862b02018-03-14 13:32:09 -07003793 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003794 return 0;
3795}
3796EXPORT_SYMBOL(sock_queue_err_skb);
3797
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003798struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3799{
3800 struct sk_buff_head *q = &sk->sk_error_queue;
3801 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003802 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003803 int err = 0;
3804
Eric Dumazet997d5c32015-02-18 05:47:55 -08003805 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003806 skb = __skb_dequeue(q);
3807 if (skb && (skb_next = skb_peek(q)))
3808 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003809 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003810
3811 sk->sk_err = err;
3812 if (err)
3813 sk->sk_error_report(sk);
3814
3815 return skb;
3816}
3817EXPORT_SYMBOL(sock_dequeue_err_skb);
3818
Alexander Duyckcab41c42014-09-10 18:05:26 -04003819/**
3820 * skb_clone_sk - create clone of skb, and take reference to socket
3821 * @skb: the skb to clone
3822 *
3823 * This function creates a clone of a buffer that holds a reference on
3824 * sk_refcnt. Buffers created via this function are meant to be
3825 * returned using sock_queue_err_skb, or free via kfree_skb.
3826 *
3827 * When passing buffers allocated with this function to sock_queue_err_skb
3828 * it is necessary to wrap the call with sock_hold/sock_put in order to
3829 * prevent the socket from being released prior to being enqueued on
3830 * the sk_error_queue.
3831 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003832struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3833{
3834 struct sock *sk = skb->sk;
3835 struct sk_buff *clone;
3836
3837 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3838 return NULL;
3839
3840 clone = skb_clone(skb, GFP_ATOMIC);
3841 if (!clone) {
3842 sock_put(sk);
3843 return NULL;
3844 }
3845
3846 clone->sk = sk;
3847 clone->destructor = sock_efree;
3848
3849 return clone;
3850}
3851EXPORT_SYMBOL(skb_clone_sk);
3852
Alexander Duyck37846ef2014-09-04 13:31:10 -04003853static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3854 struct sock *sk,
3855 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003856{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003857 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003858 int err;
3859
Patrick Ohlyac45f602009-02-12 05:03:37 +00003860 serr = SKB_EXT_ERR(skb);
3861 memset(serr, 0, sizeof(*serr));
3862 serr->ee.ee_errno = ENOMSG;
3863 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003864 serr->ee.ee_info = tstype;
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -04003865 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003866 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003867 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003868 if (sk->sk_protocol == IPPROTO_TCP &&
3869 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003870 serr->ee.ee_data -= sk->sk_tskey;
3871 }
Eric Dumazet29030372010-05-29 00:20:48 -07003872
Patrick Ohlyac45f602009-02-12 05:03:37 +00003873 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003874
Patrick Ohlyac45f602009-02-12 05:03:37 +00003875 if (err)
3876 kfree_skb(skb);
3877}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003878
Willem de Bruijnb245be12015-01-30 13:29:32 -05003879static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3880{
3881 bool ret;
3882
3883 if (likely(sysctl_tstamp_allow_data || tsonly))
3884 return true;
3885
3886 read_lock_bh(&sk->sk_callback_lock);
3887 ret = sk->sk_socket && sk->sk_socket->file &&
3888 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3889 read_unlock_bh(&sk->sk_callback_lock);
3890 return ret;
3891}
3892
Alexander Duyck37846ef2014-09-04 13:31:10 -04003893void skb_complete_tx_timestamp(struct sk_buff *skb,
3894 struct skb_shared_hwtstamps *hwtstamps)
3895{
3896 struct sock *sk = skb->sk;
3897
Willem de Bruijnb245be12015-01-30 13:29:32 -05003898 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn58f6ebb2017-12-13 14:41:06 -05003899 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05003900
Eric Dumazetf157cc12017-03-03 21:01:03 -08003901 /* Take a reference to prevent skb_orphan() from freeing the socket,
3902 * but only if the socket refcount is not zero.
3903 */
3904 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3905 *skb_hwtstamps(skb) = *hwtstamps;
3906 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3907 sock_put(sk);
Willem de Bruijn58f6ebb2017-12-13 14:41:06 -05003908 return;
Eric Dumazetf157cc12017-03-03 21:01:03 -08003909 }
Willem de Bruijn58f6ebb2017-12-13 14:41:06 -05003910
3911err:
3912 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003913}
3914EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3915
3916void __skb_tstamp_tx(struct sk_buff *orig_skb,
3917 struct skb_shared_hwtstamps *hwtstamps,
3918 struct sock *sk, int tstype)
3919{
3920 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003921 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003922
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003923 if (!sk)
3924 return;
3925
3926 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3927 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003928 return;
3929
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003930 if (tsonly)
3931 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003932 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003933 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003934 if (!skb)
3935 return;
3936
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003937 if (tsonly) {
Willem de Bruijnfcfb53c2017-06-08 11:35:03 -04003938 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3939 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003940 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3941 }
3942
3943 if (hwtstamps)
3944 *skb_hwtstamps(skb) = *hwtstamps;
3945 else
3946 skb->tstamp = ktime_get_real();
3947
Alexander Duyck37846ef2014-09-04 13:31:10 -04003948 __skb_complete_tx_timestamp(skb, sk, tstype);
3949}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003950EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3951
3952void skb_tstamp_tx(struct sk_buff *orig_skb,
3953 struct skb_shared_hwtstamps *hwtstamps)
3954{
3955 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3956 SCM_TSTAMP_SND);
3957}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003958EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3959
Johannes Berg6e3e9392011-11-09 10:15:42 +01003960void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3961{
3962 struct sock *sk = skb->sk;
3963 struct sock_exterr_skb *serr;
Eric Dumazet98fa3d22017-03-03 21:01:02 -08003964 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01003965
3966 skb->wifi_acked_valid = 1;
3967 skb->wifi_acked = acked;
3968
3969 serr = SKB_EXT_ERR(skb);
3970 memset(serr, 0, sizeof(*serr));
3971 serr->ee.ee_errno = ENOMSG;
3972 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3973
Eric Dumazet98fa3d22017-03-03 21:01:02 -08003974 /* Take a reference to prevent skb_orphan() from freeing the socket,
3975 * but only if the socket refcount is not zero.
3976 */
3977 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3978 err = sock_queue_err_skb(sk, skb);
3979 sock_put(sk);
3980 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01003981 if (err)
3982 kfree_skb(skb);
3983}
3984EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3985
Rusty Russellf35d9d82008-02-04 23:49:54 -05003986/**
3987 * skb_partial_csum_set - set up and verify partial csum values for packet
3988 * @skb: the skb to set
3989 * @start: the number of bytes after skb->data to start checksumming.
3990 * @off: the offset from start to place the checksum.
3991 *
3992 * For untrusted partially-checksummed packets, we need to make sure the values
3993 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3994 *
3995 * This function checks and sets those values and skb->ip_summed: if this
3996 * returns false you should drop the packet.
3997 */
3998bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3999{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004000 if (unlikely(start > skb_headlen(skb)) ||
4001 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004002 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4003 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004004 return false;
4005 }
4006 skb->ip_summed = CHECKSUM_PARTIAL;
4007 skb->csum_start = skb_headroom(skb) + start;
4008 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004009 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004010 return true;
4011}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004012EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004013
Paul Durranted1f50c2014-01-09 10:02:46 +00004014static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4015 unsigned int max)
4016{
4017 if (skb_headlen(skb) >= len)
4018 return 0;
4019
4020 /* If we need to pullup then pullup to the max, so we
4021 * won't need to do it again.
4022 */
4023 if (max > skb->len)
4024 max = skb->len;
4025
4026 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4027 return -ENOMEM;
4028
4029 if (skb_headlen(skb) < len)
4030 return -EPROTO;
4031
4032 return 0;
4033}
4034
Jan Beulichf9708b42014-03-11 13:56:05 +00004035#define MAX_TCP_HDR_LEN (15 * 4)
4036
4037static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4038 typeof(IPPROTO_IP) proto,
4039 unsigned int off)
4040{
4041 switch (proto) {
4042 int err;
4043
4044 case IPPROTO_TCP:
4045 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4046 off + MAX_TCP_HDR_LEN);
4047 if (!err && !skb_partial_csum_set(skb, off,
4048 offsetof(struct tcphdr,
4049 check)))
4050 err = -EPROTO;
4051 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4052
4053 case IPPROTO_UDP:
4054 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4055 off + sizeof(struct udphdr));
4056 if (!err && !skb_partial_csum_set(skb, off,
4057 offsetof(struct udphdr,
4058 check)))
4059 err = -EPROTO;
4060 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4061 }
4062
4063 return ERR_PTR(-EPROTO);
4064}
4065
Paul Durranted1f50c2014-01-09 10:02:46 +00004066/* This value should be large enough to cover a tagged ethernet header plus
4067 * maximally sized IP and TCP or UDP headers.
4068 */
4069#define MAX_IP_HDR_LEN 128
4070
Jan Beulichf9708b42014-03-11 13:56:05 +00004071static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004072{
4073 unsigned int off;
4074 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004075 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004076 int err;
4077
4078 fragment = false;
4079
4080 err = skb_maybe_pull_tail(skb,
4081 sizeof(struct iphdr),
4082 MAX_IP_HDR_LEN);
4083 if (err < 0)
4084 goto out;
4085
4086 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4087 fragment = true;
4088
4089 off = ip_hdrlen(skb);
4090
4091 err = -EPROTO;
4092
4093 if (fragment)
4094 goto out;
4095
Jan Beulichf9708b42014-03-11 13:56:05 +00004096 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4097 if (IS_ERR(csum))
4098 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004099
Jan Beulichf9708b42014-03-11 13:56:05 +00004100 if (recalculate)
4101 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4102 ip_hdr(skb)->daddr,
4103 skb->len - off,
4104 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004105 err = 0;
4106
4107out:
4108 return err;
4109}
4110
4111/* This value should be large enough to cover a tagged ethernet header plus
4112 * an IPv6 header, all options, and a maximal TCP or UDP header.
4113 */
4114#define MAX_IPV6_HDR_LEN 256
4115
4116#define OPT_HDR(type, skb, off) \
4117 (type *)(skb_network_header(skb) + (off))
4118
4119static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4120{
4121 int err;
4122 u8 nexthdr;
4123 unsigned int off;
4124 unsigned int len;
4125 bool fragment;
4126 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004127 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004128
4129 fragment = false;
4130 done = false;
4131
4132 off = sizeof(struct ipv6hdr);
4133
4134 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4135 if (err < 0)
4136 goto out;
4137
4138 nexthdr = ipv6_hdr(skb)->nexthdr;
4139
4140 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4141 while (off <= len && !done) {
4142 switch (nexthdr) {
4143 case IPPROTO_DSTOPTS:
4144 case IPPROTO_HOPOPTS:
4145 case IPPROTO_ROUTING: {
4146 struct ipv6_opt_hdr *hp;
4147
4148 err = skb_maybe_pull_tail(skb,
4149 off +
4150 sizeof(struct ipv6_opt_hdr),
4151 MAX_IPV6_HDR_LEN);
4152 if (err < 0)
4153 goto out;
4154
4155 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4156 nexthdr = hp->nexthdr;
4157 off += ipv6_optlen(hp);
4158 break;
4159 }
4160 case IPPROTO_AH: {
4161 struct ip_auth_hdr *hp;
4162
4163 err = skb_maybe_pull_tail(skb,
4164 off +
4165 sizeof(struct ip_auth_hdr),
4166 MAX_IPV6_HDR_LEN);
4167 if (err < 0)
4168 goto out;
4169
4170 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4171 nexthdr = hp->nexthdr;
4172 off += ipv6_authlen(hp);
4173 break;
4174 }
4175 case IPPROTO_FRAGMENT: {
4176 struct frag_hdr *hp;
4177
4178 err = skb_maybe_pull_tail(skb,
4179 off +
4180 sizeof(struct frag_hdr),
4181 MAX_IPV6_HDR_LEN);
4182 if (err < 0)
4183 goto out;
4184
4185 hp = OPT_HDR(struct frag_hdr, skb, off);
4186
4187 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4188 fragment = true;
4189
4190 nexthdr = hp->nexthdr;
4191 off += sizeof(struct frag_hdr);
4192 break;
4193 }
4194 default:
4195 done = true;
4196 break;
4197 }
4198 }
4199
4200 err = -EPROTO;
4201
4202 if (!done || fragment)
4203 goto out;
4204
Jan Beulichf9708b42014-03-11 13:56:05 +00004205 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4206 if (IS_ERR(csum))
4207 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004208
Jan Beulichf9708b42014-03-11 13:56:05 +00004209 if (recalculate)
4210 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4211 &ipv6_hdr(skb)->daddr,
4212 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004213 err = 0;
4214
4215out:
4216 return err;
4217}
4218
4219/**
4220 * skb_checksum_setup - set up partial checksum offset
4221 * @skb: the skb to set up
4222 * @recalculate: if true the pseudo-header checksum will be recalculated
4223 */
4224int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4225{
4226 int err;
4227
4228 switch (skb->protocol) {
4229 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004230 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004231 break;
4232
4233 case htons(ETH_P_IPV6):
4234 err = skb_checksum_setup_ipv6(skb, recalculate);
4235 break;
4236
4237 default:
4238 err = -EPROTO;
4239 break;
4240 }
4241
4242 return err;
4243}
4244EXPORT_SYMBOL(skb_checksum_setup);
4245
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004246/**
4247 * skb_checksum_maybe_trim - maybe trims the given skb
4248 * @skb: the skb to check
4249 * @transport_len: the data length beyond the network header
4250 *
4251 * Checks whether the given skb has data beyond the given transport length.
4252 * If so, returns a cloned skb trimmed to this transport length.
4253 * Otherwise returns the provided skb. Returns NULL in error cases
4254 * (e.g. transport_len exceeds skb length or out-of-memory).
4255 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004256 * Caller needs to set the skb transport header and free any returned skb if it
4257 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004258 */
4259static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4260 unsigned int transport_len)
4261{
4262 struct sk_buff *skb_chk;
4263 unsigned int len = skb_transport_offset(skb) + transport_len;
4264 int ret;
4265
Linus Lüssinga5169932015-08-13 05:54:07 +02004266 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004267 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004268 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004269 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004270
4271 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004272 if (!skb_chk)
4273 return NULL;
4274
4275 ret = pskb_trim_rcsum(skb_chk, len);
4276 if (ret) {
4277 kfree_skb(skb_chk);
4278 return NULL;
4279 }
4280
4281 return skb_chk;
4282}
4283
4284/**
4285 * skb_checksum_trimmed - validate checksum of an skb
4286 * @skb: the skb to check
4287 * @transport_len: the data length beyond the network header
4288 * @skb_chkf: checksum function to use
4289 *
4290 * Applies the given checksum function skb_chkf to the provided skb.
4291 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4292 *
4293 * If the skb has data beyond the given transport length, then a
4294 * trimmed & cloned skb is checked and returned.
4295 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004296 * Caller needs to set the skb transport header and free any returned skb if it
4297 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004298 */
4299struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4300 unsigned int transport_len,
4301 __sum16(*skb_chkf)(struct sk_buff *skb))
4302{
4303 struct sk_buff *skb_chk;
4304 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004305 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004306
4307 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4308 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004309 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004310
Linus Lüssinga5169932015-08-13 05:54:07 +02004311 if (!pskb_may_pull(skb_chk, offset))
4312 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004313
Linus Lüssing9b368812016-02-24 04:21:42 +01004314 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004315 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004316 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004317
Linus Lüssinga5169932015-08-13 05:54:07 +02004318 if (ret)
4319 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004320
4321 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004322
4323err:
4324 if (skb_chk && skb_chk != skb)
4325 kfree_skb(skb_chk);
4326
4327 return NULL;
4328
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004329}
4330EXPORT_SYMBOL(skb_checksum_trimmed);
4331
Ben Hutchings4497b072008-06-19 16:22:28 -07004332void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4333{
Joe Perchese87cc472012-05-13 21:56:26 +00004334 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4335 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004336}
Ben Hutchings4497b072008-06-19 16:22:28 -07004337EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004338
4339void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4340{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004341 if (head_stolen) {
4342 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004343 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004344 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004345 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004346 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004347}
4348EXPORT_SYMBOL(kfree_skb_partial);
4349
4350/**
4351 * skb_try_coalesce - try to merge skb to prior one
4352 * @to: prior buffer
4353 * @from: buffer to add
4354 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004355 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004356 */
4357bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4358 bool *fragstolen, int *delta_truesize)
4359{
4360 int i, delta, len = from->len;
4361
4362 *fragstolen = false;
4363
4364 if (skb_cloned(to))
4365 return false;
4366
4367 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004368 if (len)
4369 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004370 *delta_truesize = 0;
4371 return true;
4372 }
4373
4374 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4375 return false;
4376
4377 if (skb_headlen(from) != 0) {
4378 struct page *page;
4379 unsigned int offset;
4380
4381 if (skb_shinfo(to)->nr_frags +
4382 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4383 return false;
4384
4385 if (skb_head_is_locked(from))
4386 return false;
4387
4388 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4389
4390 page = virt_to_head_page(from->head);
4391 offset = from->data - (unsigned char *)page_address(page);
4392
4393 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4394 page, offset, skb_headlen(from));
4395 *fragstolen = true;
4396 } else {
4397 if (skb_shinfo(to)->nr_frags +
4398 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4399 return false;
4400
Weiping Panf4b549a2012-09-28 20:15:30 +00004401 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004402 }
4403
4404 WARN_ON_ONCE(delta < len);
4405
4406 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4407 skb_shinfo(from)->frags,
4408 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4409 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4410
4411 if (!skb_cloned(from))
4412 skb_shinfo(from)->nr_frags = 0;
4413
Li RongQing8ea853f2012-09-18 16:53:21 +00004414 /* if the skb is not cloned this does nothing
4415 * since we set nr_frags to 0.
4416 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004417 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4418 skb_frag_ref(from, i);
4419
4420 to->truesize += delta;
4421 to->len += len;
4422 to->data_len += len;
4423
4424 *delta_truesize = delta;
4425 return true;
4426}
4427EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004428
4429/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004430 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004431 *
4432 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004433 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004434 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004435 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4436 * into/from a tunnel. Some information have to be cleared during these
4437 * operations.
4438 * skb_scrub_packet can also be used to clean a skb before injecting it in
4439 * another namespace (@xnet == true). We have to clear all information in the
4440 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004441 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004442void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004443{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004444 skb->tstamp.tv64 = 0;
4445 skb->pkt_type = PACKET_HOST;
4446 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004447 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004448 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004449 secpath_reset(skb);
4450 nf_reset(skb);
4451 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004452
Petr Machatad39ebd12018-11-20 11:39:56 +00004453#ifdef CONFIG_NET_SWITCHDEV
4454 skb->offload_fwd_mark = 0;
4455#endif
4456
Herbert Xu213dd742015-04-16 09:03:27 +08004457 if (!xnet)
4458 return;
4459
Ye Yinafd9fa62017-10-26 16:57:05 +08004460 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004461 skb_orphan(skb);
4462 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004463}
4464EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004465
4466/**
4467 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4468 *
4469 * @skb: GSO skb
4470 *
4471 * skb_gso_transport_seglen is used to determine the real size of the
4472 * individual segments, including Layer4 headers (TCP/UDP).
4473 *
4474 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4475 */
4476unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4477{
4478 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004479 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004480
Florian Westphalf993bc22014-10-20 13:49:18 +02004481 if (skb->encapsulation) {
4482 thlen = skb_inner_transport_header(skb) -
4483 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004484
Florian Westphalf993bc22014-10-20 13:49:18 +02004485 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4486 thlen += inner_tcp_hdrlen(skb);
4487 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4488 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004489 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4490 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004491 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004492 /* UFO sets gso_size to the size of the fragmentation
4493 * payload, i.e. the size of the L4 (UDP) header is already
4494 * accounted for.
4495 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004496 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004497}
4498EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004499
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004500/**
Daniel Axtens84d8c3a2019-02-14 11:31:17 +01004501 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
4502 *
4503 * There are a couple of instances where we have a GSO skb, and we
4504 * want to determine what size it would be after it is segmented.
4505 *
4506 * We might want to check:
4507 * - L3+L4+payload size (e.g. IP forwarding)
4508 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
4509 *
4510 * This is a helper to do that correctly considering GSO_BY_FRAGS.
4511 *
4512 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
4513 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
4514 *
4515 * @max_len: The maximum permissible length.
4516 *
4517 * Returns true if the segmented length <= max length.
4518 */
4519static inline bool skb_gso_size_check(const struct sk_buff *skb,
4520 unsigned int seg_len,
4521 unsigned int max_len) {
4522 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4523 const struct sk_buff *iter;
4524
4525 if (shinfo->gso_size != GSO_BY_FRAGS)
4526 return seg_len <= max_len;
4527
4528 /* Undo this so we can re-use header sizes */
4529 seg_len -= GSO_BY_FRAGS;
4530
4531 skb_walk_frags(skb, iter) {
4532 if (seg_len + skb_headlen(iter) > max_len)
4533 return false;
4534 }
4535
4536 return true;
4537}
4538
4539/**
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004540 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4541 *
4542 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004543 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004544 *
4545 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4546 * once split.
4547 */
4548bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4549{
Daniel Axtens84d8c3a2019-02-14 11:31:17 +01004550 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004551}
4552EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4553
Daniel Axtens84d8c3a2019-02-14 11:31:17 +01004554/**
4555 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
4556 *
4557 * @skb: GSO skb
4558 * @len: length to validate against
4559 *
4560 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
4561 * length once split, including L2, L3 and L4 headers and the payload.
4562 */
4563bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
4564{
4565 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
4566}
4567EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
4568
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004569static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4570{
Toshiaki Makita58df2892018-03-13 14:51:27 +09004571 int mac_len;
4572
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004573 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4574 kfree_skb(skb);
4575 return NULL;
4576 }
4577
Toshiaki Makita58df2892018-03-13 14:51:27 +09004578 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitab389e042018-03-29 19:05:29 +09004579 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
4580 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
4581 mac_len - VLAN_HLEN - ETH_TLEN);
4582 }
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004583 skb->mac_header += VLAN_HLEN;
4584 return skb;
4585}
4586
4587struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4588{
4589 struct vlan_hdr *vhdr;
4590 u16 vlan_tci;
4591
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004592 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004593 /* vlan_tci is already set-up so leave this for another time */
4594 return skb;
4595 }
4596
4597 skb = skb_share_check(skb, GFP_ATOMIC);
4598 if (unlikely(!skb))
4599 goto err_free;
4600
4601 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4602 goto err_free;
4603
4604 vhdr = (struct vlan_hdr *)skb->data;
4605 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4606 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4607
4608 skb_pull_rcsum(skb, VLAN_HLEN);
4609 vlan_set_encap_proto(skb, vhdr);
4610
4611 skb = skb_reorder_vlan_header(skb);
4612 if (unlikely(!skb))
4613 goto err_free;
4614
4615 skb_reset_network_header(skb);
4616 skb_reset_transport_header(skb);
4617 skb_reset_mac_len(skb);
4618
4619 return skb;
4620
4621err_free:
4622 kfree_skb(skb);
4623 return NULL;
4624}
4625EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004626
Jiri Pirkoe2195122014-11-19 14:05:01 +01004627int skb_ensure_writable(struct sk_buff *skb, int write_len)
4628{
4629 if (!pskb_may_pull(skb, write_len))
4630 return -ENOMEM;
4631
4632 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4633 return 0;
4634
4635 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4636}
4637EXPORT_SYMBOL(skb_ensure_writable);
4638
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004639/* remove VLAN header from packet and update csum accordingly.
4640 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4641 */
4642int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004643{
4644 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004645 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004646 int err;
4647
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004648 if (WARN_ONCE(offset,
4649 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4650 offset)) {
4651 return -EINVAL;
4652 }
4653
Jiri Pirko93515d52014-11-19 14:05:02 +01004654 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4655 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004656 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004657
4658 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4659
4660 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4661 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4662
4663 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4664 __skb_pull(skb, VLAN_HLEN);
4665
4666 vlan_set_encap_proto(skb, vhdr);
4667 skb->mac_header += VLAN_HLEN;
4668
4669 if (skb_network_offset(skb) < ETH_HLEN)
4670 skb_set_network_header(skb, ETH_HLEN);
4671
4672 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004673
4674 return err;
4675}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004676EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004677
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004678/* Pop a vlan tag either from hwaccel or from payload.
4679 * Expects skb->data at mac header.
4680 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004681int skb_vlan_pop(struct sk_buff *skb)
4682{
4683 u16 vlan_tci;
4684 __be16 vlan_proto;
4685 int err;
4686
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004687 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004688 skb->vlan_tci = 0;
4689 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004690 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004691 return 0;
4692
4693 err = __skb_vlan_pop(skb, &vlan_tci);
4694 if (err)
4695 return err;
4696 }
4697 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004698 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004699 return 0;
4700
4701 vlan_proto = skb->protocol;
4702 err = __skb_vlan_pop(skb, &vlan_tci);
4703 if (unlikely(err))
4704 return err;
4705
4706 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4707 return 0;
4708}
4709EXPORT_SYMBOL(skb_vlan_pop);
4710
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004711/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4712 * Expects skb->data at mac header.
4713 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004714int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4715{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004716 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004717 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004718 int err;
4719
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004720 if (WARN_ONCE(offset,
4721 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4722 offset)) {
4723 return -EINVAL;
4724 }
4725
Jiri Pirko93515d52014-11-19 14:05:02 +01004726 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004727 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004728 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004729 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004730
Jiri Pirko93515d52014-11-19 14:05:02 +01004731 skb->protocol = skb->vlan_proto;
4732 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004733
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004734 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004735 }
4736 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4737 return 0;
4738}
4739EXPORT_SYMBOL(skb_vlan_push);
4740
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004741/**
4742 * alloc_skb_with_frags - allocate skb with page frags
4743 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004744 * @header_len: size of linear part
4745 * @data_len: needed length in frags
4746 * @max_page_order: max page order desired.
4747 * @errcode: pointer to error code if any
4748 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004749 *
4750 * This can be used to allocate a paged skb, given a maximal order for frags.
4751 */
4752struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4753 unsigned long data_len,
4754 int max_page_order,
4755 int *errcode,
4756 gfp_t gfp_mask)
4757{
4758 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4759 unsigned long chunk;
4760 struct sk_buff *skb;
4761 struct page *page;
4762 gfp_t gfp_head;
4763 int i;
4764
4765 *errcode = -EMSGSIZE;
4766 /* Note this test could be relaxed, if we succeed to allocate
4767 * high order pages...
4768 */
4769 if (npages > MAX_SKB_FRAGS)
4770 return NULL;
4771
4772 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004773 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004774 gfp_head |= __GFP_REPEAT;
4775
4776 *errcode = -ENOBUFS;
4777 skb = alloc_skb(header_len, gfp_head);
4778 if (!skb)
4779 return NULL;
4780
4781 skb->truesize += npages << PAGE_SHIFT;
4782
4783 for (i = 0; npages > 0; i++) {
4784 int order = max_page_order;
4785
4786 while (order) {
4787 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004788 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004789 __GFP_COMP |
4790 __GFP_NOWARN |
4791 __GFP_NORETRY,
4792 order);
4793 if (page)
4794 goto fill_page;
4795 /* Do not retry other high order allocations */
4796 order = 1;
4797 max_page_order = 0;
4798 }
4799 order--;
4800 }
4801 page = alloc_page(gfp_mask);
4802 if (!page)
4803 goto failure;
4804fill_page:
4805 chunk = min_t(unsigned long, data_len,
4806 PAGE_SIZE << order);
4807 skb_fill_page_desc(skb, i, page, 0, chunk);
4808 data_len -= chunk;
4809 npages -= 1 << order;
4810 }
4811 return skb;
4812
4813failure:
4814 kfree_skb(skb);
4815 return NULL;
4816}
4817EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004818
4819/* carve out the first off bytes from skb when off < headlen */
4820static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4821 const int headlen, gfp_t gfp_mask)
4822{
4823 int i;
4824 int size = skb_end_offset(skb);
4825 int new_hlen = headlen - off;
4826 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004827
4828 size = SKB_DATA_ALIGN(size);
4829
4830 if (skb_pfmemalloc(skb))
4831 gfp_mask |= __GFP_MEMALLOC;
4832 data = kmalloc_reserve(size +
4833 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4834 gfp_mask, NUMA_NO_NODE, NULL);
4835 if (!data)
4836 return -ENOMEM;
4837
4838 size = SKB_WITH_OVERHEAD(ksize(data));
4839
4840 /* Copy real data, and all frags */
4841 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4842 skb->len -= off;
4843
4844 memcpy((struct skb_shared_info *)(data + size),
4845 skb_shinfo(skb),
4846 offsetof(struct skb_shared_info,
4847 frags[skb_shinfo(skb)->nr_frags]));
4848 if (skb_cloned(skb)) {
4849 /* drop the old head gracefully */
4850 if (skb_orphan_frags(skb, gfp_mask)) {
4851 kfree(data);
4852 return -ENOMEM;
4853 }
4854 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4855 skb_frag_ref(skb, i);
4856 if (skb_has_frag_list(skb))
4857 skb_clone_fraglist(skb);
4858 skb_release_data(skb);
4859 } else {
4860 /* we can reuse existing recount- all we did was
4861 * relocate values
4862 */
4863 skb_free_head(skb);
4864 }
4865
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004866 skb->head = data;
4867 skb->data = data;
4868 skb->head_frag = 0;
4869#ifdef NET_SKBUFF_DATA_USES_OFFSET
4870 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004871#else
4872 skb->end = skb->head + size;
4873#endif
4874 skb_set_tail_pointer(skb, skb_headlen(skb));
4875 skb_headers_offset_update(skb, 0);
4876 skb->cloned = 0;
4877 skb->hdr_len = 0;
4878 skb->nohdr = 0;
4879 atomic_set(&skb_shinfo(skb)->dataref, 1);
4880
4881 return 0;
4882}
4883
4884static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4885
4886/* carve out the first eat bytes from skb's frag_list. May recurse into
4887 * pskb_carve()
4888 */
4889static int pskb_carve_frag_list(struct sk_buff *skb,
4890 struct skb_shared_info *shinfo, int eat,
4891 gfp_t gfp_mask)
4892{
4893 struct sk_buff *list = shinfo->frag_list;
4894 struct sk_buff *clone = NULL;
4895 struct sk_buff *insp = NULL;
4896
4897 do {
4898 if (!list) {
4899 pr_err("Not enough bytes to eat. Want %d\n", eat);
4900 return -EFAULT;
4901 }
4902 if (list->len <= eat) {
4903 /* Eaten as whole. */
4904 eat -= list->len;
4905 list = list->next;
4906 insp = list;
4907 } else {
4908 /* Eaten partially. */
4909 if (skb_shared(list)) {
4910 clone = skb_clone(list, gfp_mask);
4911 if (!clone)
4912 return -ENOMEM;
4913 insp = list->next;
4914 list = clone;
4915 } else {
4916 /* This may be pulled without problems. */
4917 insp = list;
4918 }
4919 if (pskb_carve(list, eat, gfp_mask) < 0) {
4920 kfree_skb(clone);
4921 return -ENOMEM;
4922 }
4923 break;
4924 }
4925 } while (eat);
4926
4927 /* Free pulled out fragments. */
4928 while ((list = shinfo->frag_list) != insp) {
4929 shinfo->frag_list = list->next;
4930 kfree_skb(list);
4931 }
4932 /* And insert new clone at head. */
4933 if (clone) {
4934 clone->next = list;
4935 shinfo->frag_list = clone;
4936 }
4937 return 0;
4938}
4939
4940/* carve off first len bytes from skb. Split line (off) is in the
4941 * non-linear part of skb
4942 */
4943static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4944 int pos, gfp_t gfp_mask)
4945{
4946 int i, k = 0;
4947 int size = skb_end_offset(skb);
4948 u8 *data;
4949 const int nfrags = skb_shinfo(skb)->nr_frags;
4950 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004951
4952 size = SKB_DATA_ALIGN(size);
4953
4954 if (skb_pfmemalloc(skb))
4955 gfp_mask |= __GFP_MEMALLOC;
4956 data = kmalloc_reserve(size +
4957 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4958 gfp_mask, NUMA_NO_NODE, NULL);
4959 if (!data)
4960 return -ENOMEM;
4961
4962 size = SKB_WITH_OVERHEAD(ksize(data));
4963
4964 memcpy((struct skb_shared_info *)(data + size),
4965 skb_shinfo(skb), offsetof(struct skb_shared_info,
4966 frags[skb_shinfo(skb)->nr_frags]));
4967 if (skb_orphan_frags(skb, gfp_mask)) {
4968 kfree(data);
4969 return -ENOMEM;
4970 }
4971 shinfo = (struct skb_shared_info *)(data + size);
4972 for (i = 0; i < nfrags; i++) {
4973 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4974
4975 if (pos + fsize > off) {
4976 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4977
4978 if (pos < off) {
4979 /* Split frag.
4980 * We have two variants in this case:
4981 * 1. Move all the frag to the second
4982 * part, if it is possible. F.e.
4983 * this approach is mandatory for TUX,
4984 * where splitting is expensive.
4985 * 2. Split is accurately. We make this.
4986 */
4987 shinfo->frags[0].page_offset += off - pos;
4988 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4989 }
4990 skb_frag_ref(skb, i);
4991 k++;
4992 }
4993 pos += fsize;
4994 }
4995 shinfo->nr_frags = k;
4996 if (skb_has_frag_list(skb))
4997 skb_clone_fraglist(skb);
4998
4999 if (k == 0) {
5000 /* split line is in frag list */
5001 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5002 }
5003 skb_release_data(skb);
5004
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005005 skb->head = data;
5006 skb->head_frag = 0;
5007 skb->data = data;
5008#ifdef NET_SKBUFF_DATA_USES_OFFSET
5009 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005010#else
5011 skb->end = skb->head + size;
5012#endif
5013 skb_reset_tail_pointer(skb);
5014 skb_headers_offset_update(skb, 0);
5015 skb->cloned = 0;
5016 skb->hdr_len = 0;
5017 skb->nohdr = 0;
5018 skb->len -= off;
5019 skb->data_len = skb->len;
5020 atomic_set(&skb_shinfo(skb)->dataref, 1);
5021 return 0;
5022}
5023
5024/* remove len bytes from the beginning of the skb */
5025static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5026{
5027 int headlen = skb_headlen(skb);
5028
5029 if (len < headlen)
5030 return pskb_carve_inside_header(skb, len, headlen, gfp);
5031 else
5032 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5033}
5034
5035/* Extract to_copy bytes starting at off from skb, and return this in
5036 * a new skb
5037 */
5038struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5039 int to_copy, gfp_t gfp)
5040{
5041 struct sk_buff *clone = skb_clone(skb, gfp);
5042
5043 if (!clone)
5044 return NULL;
5045
5046 if (pskb_carve(clone, off, gfp) < 0 ||
5047 pskb_trim(clone, to_copy)) {
5048 kfree_skb(clone);
5049 return NULL;
5050 }
5051 return clone;
5052}
5053EXPORT_SYMBOL(pskb_extract);