blob: cdb0ccdaac0bf3c27b3f7b2ab2a4d299b205926b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Routines having to do with the 'struct sk_buff' memory handlers.
4 *
Alan Cox113aa832008-10-13 19:01:08 -07005 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fixes:
9 * Alan Cox : Fixed the worst of the load
10 * balancer bugs.
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
23 *
24 * NOTE:
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31/*
32 * The functions in this file will not compile correctly with gcc 2.4.x
33 */
34
Joe Perchese005d192012-05-16 19:58:40 +000035#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010045#include <linux/tcp.h>
46#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030047#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/netdevice.h>
49#ifdef CONFIG_NET_CLS_ACT
50#include <net/pkt_sched.h>
51#endif
52#include <linux/string.h>
53#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080054#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070058#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000059#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070060#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040061#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <net/protocol.h>
64#include <net/dst.h>
65#include <net/sock.h>
66#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000067#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/xfrm.h>
69
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080070#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040071#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020072#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050073#include <linux/capability.h>
74#include <linux/user_namespace.h>
Matteo Croce2544af02019-05-29 17:13:48 +020075#include <linux/indirect_call_wrapper.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040076
Bart Van Assche7b7ed882019-03-25 09:17:23 -070077#include "datagram.h"
78
Alexey Dobriyan08009a72018-02-24 21:20:33 +030079struct kmem_cache *skbuff_head_cache __ro_after_init;
80static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
Florian Westphaldf5042f2018-12-18 17:15:16 +010081#ifdef CONFIG_SKB_EXTENSIONS
82static struct kmem_cache *skbuff_ext_cache __ro_after_init;
83#endif
Hans Westgaard Ry5f74f82e2016-02-03 09:26:57 +010084int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
85EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/**
Jean Sacrenf05de732013-02-11 13:30:38 +000088 * skb_panic - private function for out-of-line support
89 * @skb: buffer
90 * @sz: size
91 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000092 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Jean Sacrenf05de732013-02-11 13:30:38 +000094 * Out-of-line support for skb_put() and skb_push().
95 * Called via the wrapper skb_over_panic() or skb_under_panic().
96 * Keep out of line to prevent kernel bloat.
97 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Jean Sacrenf05de732013-02-11 13:30:38 +000099static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +0000100 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Joe Perchese005d192012-05-16 19:58:40 +0000102 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 +0000103 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000104 (unsigned long)skb->tail, (unsigned long)skb->end,
105 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 BUG();
107}
108
Jean Sacrenf05de732013-02-11 13:30:38 +0000109static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jean Sacrenf05de732013-02-11 13:30:38 +0000111 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jean Sacrenf05de732013-02-11 13:30:38 +0000114static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115{
116 skb_panic(skb, sz, addr, __func__);
117}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700118
119/*
120 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
121 * the caller if emergency pfmemalloc reserves are being used. If it is and
122 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
123 * may be used. Otherwise, the packet data may be discarded until enough
124 * memory is free
125 */
126#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
127 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000128
129static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
130 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700131{
132 void *obj;
133 bool ret_pfmemalloc = false;
134
135 /*
136 * Try a regular allocation, when that fails and we're not entitled
137 * to the reserves, fail.
138 */
139 obj = kmalloc_node_track_caller(size,
140 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
141 node);
142 if (obj || !(gfp_pfmemalloc_allowed(flags)))
143 goto out;
144
145 /* Try again but now we are using pfmemalloc reserves */
146 ret_pfmemalloc = true;
147 obj = kmalloc_node_track_caller(size, flags, node);
148
149out:
150 if (pfmemalloc)
151 *pfmemalloc = ret_pfmemalloc;
152
153 return obj;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Allocate a new skbuff. We do this ourselves so we can fill in a few
157 * 'private' fields and also do memory statistics to find all the
158 * [BEEP] leaks.
159 *
160 */
161
162/**
David S. Millerd179cd12005-08-17 14:57:30 -0700163 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * @size: size to allocate
165 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700166 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
167 * instead of head cache and allocate a cloned (child) skb.
168 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
169 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800170 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000173 * tail room of at least size bytes. The object has a reference count
174 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 * %GFP_ATOMIC.
178 */
Al Virodd0fc662005-10-07 07:46:04 +0100179struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700180 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Christoph Lametere18b8902006-12-06 20:33:20 -0800182 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800183 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct sk_buff *skb;
185 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700186 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Mel Gormanc93bdd02012-07-31 16:44:19 -0700188 cache = (flags & SKB_ALLOC_FCLONE)
189 ? skbuff_fclone_cache : skbuff_head_cache;
190
191 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
192 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800195 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!skb)
197 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700198 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000200 /* We do our best to align skb_shared_info on a separate cache
201 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
202 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
203 * Both skb->head and skb_shared_info are cache line aligned.
204 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000205 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000206 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!data)
209 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000210 /* kmalloc(size) might give us more room than requested.
211 * Put skb_shared_info exactly at the end of allocated zone,
212 * to allow max possible filling before reallocation.
213 */
214 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700215 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300217 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700218 * Only clear those fields we need to clear, not those that we will
219 * actually initialise below. Hence, don't put any more fields after
220 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300221 */
222 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000223 /* Account for allocated memory : skb + skb->head */
224 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700225 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300226 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 skb->head = data;
228 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700229 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700230 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800231 skb->mac_header = (typeof(skb->mac_header))~0U;
232 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000233
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800234 /* make sure we initialize shinfo sequentially */
235 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700236 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800237 atomic_set(&shinfo->dataref, 1);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238
Mel Gormanc93bdd02012-07-31 16:44:19 -0700239 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700240 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700242 fclones = container_of(skb, struct sk_buff_fclones, skb1);
243
David S. Millerd179cd12005-08-17 14:57:30 -0700244 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300245 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700246
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800247 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249out:
250 return skb;
251nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800252 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 skb = NULL;
254 goto out;
255}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800256EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200258/* Caller must provide SKB that is memset cleared */
259static struct sk_buff *__build_skb_around(struct sk_buff *skb,
260 void *data, unsigned int frag_size)
261{
262 struct skb_shared_info *shinfo;
263 unsigned int size = frag_size ? : ksize(data);
264
265 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
266
267 /* Assumes caller memset cleared SKB */
268 skb->truesize = SKB_TRUESIZE(size);
269 refcount_set(&skb->users, 1);
270 skb->head = data;
271 skb->data = data;
272 skb_reset_tail_pointer(skb);
273 skb->end = skb->tail + size;
274 skb->mac_header = (typeof(skb->mac_header))~0U;
275 skb->transport_header = (typeof(skb->transport_header))~0U;
276
277 /* make sure we initialize shinfo sequentially */
278 shinfo = skb_shinfo(skb);
279 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
280 atomic_set(&shinfo->dataref, 1);
281
282 return skb;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700286 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000287 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700288 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000289 *
290 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100291 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700292 * @frag_size is 0, otherwise data should come from the page allocator
293 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000294 * The return is the new skb buffer.
295 * On a failure the return is %NULL, and @data is not freed.
296 * Notes :
297 * Before IO, driver allocates only data buffer where NIC put incoming frame
298 * Driver should add room at head (NET_SKB_PAD) and
299 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
300 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
301 * before giving packet to stack.
302 * RX rings only contains data buffers, not full skbs.
303 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700304struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000305{
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000306 struct sk_buff *skb;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000307
308 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200309 if (unlikely(!skb))
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000310 return NULL;
311
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200314 return __build_skb_around(skb, data, frag_size);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000315}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700316
317/* build_skb() is wrapper over __build_skb(), that specifically
318 * takes care of skb->head and skb->pfmemalloc
319 * This means that if @frag_size is not zero, then @data must be backed
320 * by a page fragment, not kmalloc() or vmalloc()
321 */
322struct sk_buff *build_skb(void *data, unsigned int frag_size)
323{
324 struct sk_buff *skb = __build_skb(data, frag_size);
325
326 if (skb && frag_size) {
327 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700328 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700329 skb->pfmemalloc = 1;
330 }
331 return skb;
332}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000333EXPORT_SYMBOL(build_skb);
334
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200335/**
336 * build_skb_around - build a network buffer around provided skb
337 * @skb: sk_buff provide by caller, must be memset cleared
338 * @data: data buffer provided by caller
339 * @frag_size: size of data, or 0 if head was kmalloced
340 */
341struct sk_buff *build_skb_around(struct sk_buff *skb,
342 void *data, unsigned int frag_size)
343{
344 if (unlikely(!skb))
345 return NULL;
346
347 skb = __build_skb_around(skb, data, frag_size);
348
349 if (skb && frag_size) {
350 skb->head_frag = 1;
351 if (page_is_pfmemalloc(virt_to_head_page(data)))
352 skb->pfmemalloc = 1;
353 }
354 return skb;
355}
356EXPORT_SYMBOL(build_skb_around);
357
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100358#define NAPI_SKB_CACHE_SIZE 64
359
360struct napi_alloc_cache {
361 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300362 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100363 void *skb_cache[NAPI_SKB_CACHE_SIZE];
364};
365
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700366static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100367static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800368
Alexander Duyckffde7322014-12-09 19:40:42 -0800369static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
370{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100371 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700372
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800373 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800374}
375
376void *napi_alloc_frag(unsigned int fragsz)
377{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800378 fragsz = SKB_DATA_ALIGN(fragsz);
379
Mel Gorman453f85d2017-11-15 17:38:03 -0800380 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
Alexander Duyckffde7322014-12-09 19:40:42 -0800381}
382EXPORT_SYMBOL(napi_alloc_frag);
383
Eric Dumazet6f532612012-05-18 05:12:12 +0000384/**
Sebastian Andrzej Siewior7ba7aea2019-06-07 21:20:34 +0200385 * netdev_alloc_frag - allocate a page fragment
386 * @fragsz: fragment size
387 *
388 * Allocates a frag from a page for receive buffer.
389 * Uses GFP_ATOMIC allocations.
390 */
391void *netdev_alloc_frag(unsigned int fragsz)
392{
393 struct page_frag_cache *nc;
394 void *data;
395
396 fragsz = SKB_DATA_ALIGN(fragsz);
397 if (in_irq() || irqs_disabled()) {
398 nc = this_cpu_ptr(&netdev_alloc_cache);
399 data = page_frag_alloc(nc, fragsz, GFP_ATOMIC);
400 } else {
401 local_bh_disable();
402 data = __napi_alloc_frag(fragsz, GFP_ATOMIC);
403 local_bh_enable();
404 }
405 return data;
406}
407EXPORT_SYMBOL(netdev_alloc_frag);
408
409/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800410 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
411 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900412 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800413 * @gfp_mask: get_free_pages mask, passed to alloc_skb
414 *
415 * Allocate a new &sk_buff and assign it a usage count of one. The
416 * buffer has NET_SKB_PAD headroom built in. Users should allocate
417 * the headroom they think they need without accounting for the
418 * built in space. The built in space is used for optimisations.
419 *
420 * %NULL is returned if there is no free memory.
421 */
Alexander Duyck94519802015-05-06 21:11:40 -0700422struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
423 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800424{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700425 struct page_frag_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800426 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700427 bool pfmemalloc;
428 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800429
Alexander Duyck94519802015-05-06 21:11:40 -0700430 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431
Alexander Duyck94519802015-05-06 21:11:40 -0700432 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800433 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700434 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
435 if (!skb)
436 goto skb_fail;
437 goto skb_success;
438 }
Alexander Duyck94519802015-05-06 21:11:40 -0700439
440 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
441 len = SKB_DATA_ALIGN(len);
442
443 if (sk_memalloc_socks())
444 gfp_mask |= __GFP_MEMALLOC;
445
Sebastian Andrzej Siewior92dcabd2019-06-07 21:20:35 +0200446 if (in_irq() || irqs_disabled()) {
447 nc = this_cpu_ptr(&netdev_alloc_cache);
448 data = page_frag_alloc(nc, len, gfp_mask);
449 pfmemalloc = nc->pfmemalloc;
450 } else {
451 local_bh_disable();
452 nc = this_cpu_ptr(&napi_alloc_cache.page);
453 data = page_frag_alloc(nc, len, gfp_mask);
454 pfmemalloc = nc->pfmemalloc;
455 local_bh_enable();
456 }
Alexander Duyck94519802015-05-06 21:11:40 -0700457
458 if (unlikely(!data))
459 return NULL;
460
461 skb = __build_skb(data, len);
462 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700463 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700464 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700465 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800466
Alexander Duyck94519802015-05-06 21:11:40 -0700467 /* use OR instead of assignment to avoid clearing of bits in mask */
468 if (pfmemalloc)
469 skb->pfmemalloc = 1;
470 skb->head_frag = 1;
471
Alexander Duycka080e7b2015-05-13 13:34:13 -0700472skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700473 skb_reserve(skb, NET_SKB_PAD);
474 skb->dev = dev;
475
Alexander Duycka080e7b2015-05-13 13:34:13 -0700476skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700477 return skb;
478}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800479EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Alexander Duyckfd11a832014-12-09 19:40:49 -0800481/**
482 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
483 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900484 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800485 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
486 *
487 * Allocate a new sk_buff for use in NAPI receive. This buffer will
488 * attempt to allocate the head from a special reserved region used
489 * only for NAPI Rx allocation. By doing this we can save several
490 * CPU cycles by avoiding having to disable and re-enable IRQs.
491 *
492 * %NULL is returned if there is no free memory.
493 */
Alexander Duyck94519802015-05-06 21:11:40 -0700494struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
495 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800496{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100497 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800498 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700499 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800500
Alexander Duyck94519802015-05-06 21:11:40 -0700501 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800502
Alexander Duyck94519802015-05-06 21:11:40 -0700503 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800504 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700505 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
506 if (!skb)
507 goto skb_fail;
508 goto skb_success;
509 }
Alexander Duyck94519802015-05-06 21:11:40 -0700510
511 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
512 len = SKB_DATA_ALIGN(len);
513
514 if (sk_memalloc_socks())
515 gfp_mask |= __GFP_MEMALLOC;
516
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800517 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700518 if (unlikely(!data))
519 return NULL;
520
521 skb = __build_skb(data, len);
522 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700523 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700524 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800525 }
526
Alexander Duyck94519802015-05-06 21:11:40 -0700527 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100528 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700529 skb->pfmemalloc = 1;
530 skb->head_frag = 1;
531
Alexander Duycka080e7b2015-05-13 13:34:13 -0700532skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700533 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
534 skb->dev = napi->dev;
535
Alexander Duycka080e7b2015-05-13 13:34:13 -0700536skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800537 return skb;
538}
539EXPORT_SYMBOL(__napi_alloc_skb);
540
Peter Zijlstra654bed12008-10-07 14:22:33 -0700541void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000542 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700543{
544 skb_fill_page_desc(skb, i, page, off, size);
545 skb->len += size;
546 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000547 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700548}
549EXPORT_SYMBOL(skb_add_rx_frag);
550
Jason Wangf8e617e2013-11-01 14:07:47 +0800551void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
552 unsigned int truesize)
553{
554 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
555
556 skb_frag_size_add(frag, size);
557 skb->len += size;
558 skb->data_len += size;
559 skb->truesize += truesize;
560}
561EXPORT_SYMBOL(skb_coalesce_rx_frag);
562
Herbert Xu27b437c2006-07-13 19:26:39 -0700563static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700565 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700566 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Herbert Xu27b437c2006-07-13 19:26:39 -0700569static inline void skb_drop_fraglist(struct sk_buff *skb)
570{
571 skb_drop_list(&skb_shinfo(skb)->frag_list);
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static void skb_clone_fraglist(struct sk_buff *skb)
575{
576 struct sk_buff *list;
577
David S. Millerfbb398a2009-06-09 00:18:59 -0700578 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 skb_get(list);
580}
581
Eric Dumazetd3836f22012-04-27 00:33:38 +0000582static void skb_free_head(struct sk_buff *skb)
583{
Alexander Duyck181edb22015-05-06 21:12:03 -0700584 unsigned char *head = skb->head;
585
Eric Dumazetd3836f22012-04-27 00:33:38 +0000586 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700587 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000588 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700589 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000590}
591
Adrian Bunk5bba1712006-06-29 13:02:35 -0700592static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Eric Dumazetff04a772014-09-23 18:39:30 -0700594 struct skb_shared_info *shinfo = skb_shinfo(skb);
595 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric Dumazetff04a772014-09-23 18:39:30 -0700597 if (skb->cloned &&
598 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
599 &shinfo->dataref))
600 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000601
Eric Dumazetff04a772014-09-23 18:39:30 -0700602 for (i = 0; i < shinfo->nr_frags; i++)
603 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000604
Eric Dumazetff04a772014-09-23 18:39:30 -0700605 if (shinfo->frag_list)
606 kfree_skb_list(shinfo->frag_list);
607
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400608 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700609 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * Free an skbuff by memory without cleaning the state.
614 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800615static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700617 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700618
David S. Millerd179cd12005-08-17 14:57:30 -0700619 switch (skb->fclone) {
620 case SKB_FCLONE_UNAVAILABLE:
621 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800622 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700623
624 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700625 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800626
627 /* We usually free the clone (TX completion) before original skb
628 * This test would have no chance to be true for the clone,
629 * while here, branch prediction will be good.
630 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300631 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800632 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700633 break;
634
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800635 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700636 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700637 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700638 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300639 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800640 return;
641fastpath:
642 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Paolo Abeni0a463c72017-06-12 11:23:42 +0200645void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Eric Dumazetadf30902009-06-02 05:19:30 +0000647 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700648 if (skb->destructor) {
649 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 skb->destructor(skb);
651 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000652#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100653 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100654#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100655 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700656}
657
658/* Free everything but the sk_buff shell. */
659static void skb_release_all(struct sk_buff *skb)
660{
661 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200662 if (likely(skb->head))
663 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800664}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Herbert Xu2d4baff2007-11-26 23:11:19 +0800666/**
667 * __kfree_skb - private function
668 * @skb: buffer
669 *
670 * Free an sk_buff. Release anything attached to the buffer.
671 * Clean the state. This is an internal helper function. Users should
672 * always call kfree_skb
673 */
674
675void __kfree_skb(struct sk_buff *skb)
676{
677 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 kfree_skbmem(skb);
679}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800680EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800683 * kfree_skb - free an sk_buff
684 * @skb: buffer to free
685 *
686 * Drop a reference to the buffer and free it if the usage count has
687 * hit zero.
688 */
689void kfree_skb(struct sk_buff *skb)
690{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200691 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800692 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200693
Neil Hormanead2ceb2009-03-11 09:49:55 +0000694 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800695 __kfree_skb(skb);
696}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800697EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800698
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700699void kfree_skb_list(struct sk_buff *segs)
700{
701 while (segs) {
702 struct sk_buff *next = segs->next;
703
704 kfree_skb(segs);
705 segs = next;
706 }
707}
708EXPORT_SYMBOL(kfree_skb_list);
709
Willem de Bruijn64131392019-07-07 05:51:55 -0400710/* Dump skb information and contents.
711 *
712 * Must only be called from net_ratelimit()-ed paths.
713 *
714 * Dumps up to can_dump_full whole packets if full_pkt, headers otherwise.
715 */
716void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
717{
718 static atomic_t can_dump_full = ATOMIC_INIT(5);
719 struct skb_shared_info *sh = skb_shinfo(skb);
720 struct net_device *dev = skb->dev;
721 struct sock *sk = skb->sk;
722 struct sk_buff *list_skb;
723 bool has_mac, has_trans;
724 int headroom, tailroom;
725 int i, len, seg_len;
726
727 if (full_pkt)
728 full_pkt = atomic_dec_if_positive(&can_dump_full) >= 0;
729
730 if (full_pkt)
731 len = skb->len;
732 else
733 len = min_t(int, skb->len, MAX_HEADER + 128);
734
735 headroom = skb_headroom(skb);
736 tailroom = skb_tailroom(skb);
737
738 has_mac = skb_mac_header_was_set(skb);
739 has_trans = skb_transport_header_was_set(skb);
740
741 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
742 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
743 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
744 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
745 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
746 level, skb->len, headroom, skb_headlen(skb), tailroom,
747 has_mac ? skb->mac_header : -1,
748 has_mac ? skb_mac_header_len(skb) : -1,
749 skb->network_header,
750 has_trans ? skb_network_header_len(skb) : -1,
751 has_trans ? skb->transport_header : -1,
752 sh->tx_flags, sh->nr_frags,
753 sh->gso_size, sh->gso_type, sh->gso_segs,
754 skb->csum, skb->ip_summed, skb->csum_complete_sw,
755 skb->csum_valid, skb->csum_level,
756 skb->hash, skb->sw_hash, skb->l4_hash,
757 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
758
759 if (dev)
760 printk("%sdev name=%s feat=0x%pNF\n",
761 level, dev->name, &dev->features);
762 if (sk)
763 printk("%ssk family=%hu type=%hu proto=%hu\n",
764 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
765
766 if (full_pkt && headroom)
767 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
768 16, 1, skb->head, headroom, false);
769
770 seg_len = min_t(int, skb_headlen(skb), len);
771 if (seg_len)
772 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
773 16, 1, skb->data, seg_len, false);
774 len -= seg_len;
775
776 if (full_pkt && tailroom)
777 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
778 16, 1, skb_tail_pointer(skb), tailroom, false);
779
780 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
781 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
782 u32 p_off, p_len, copied;
783 struct page *p;
784 u8 *vaddr;
785
786 skb_frag_foreach_page(frag, frag->page_offset,
787 skb_frag_size(frag), p, p_off, p_len,
788 copied) {
789 seg_len = min_t(int, p_len, len);
790 vaddr = kmap_atomic(p);
791 print_hex_dump(level, "skb frag: ",
792 DUMP_PREFIX_OFFSET,
793 16, 1, vaddr + p_off, seg_len, false);
794 kunmap_atomic(vaddr);
795 len -= seg_len;
796 if (!len)
797 break;
798 }
799 }
800
801 if (full_pkt && skb_has_frag_list(skb)) {
802 printk("skb fraglist:\n");
803 skb_walk_frags(skb, list_skb)
804 skb_dump(level, list_skb, true);
805 }
806}
807EXPORT_SYMBOL(skb_dump);
808
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700809/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000810 * skb_tx_error - report an sk_buff xmit error
811 * @skb: buffer that triggered an error
812 *
813 * Report xmit error if a device callback is tracking this skb.
814 * skb must be freed afterwards.
815 */
816void skb_tx_error(struct sk_buff *skb)
817{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400818 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000819}
820EXPORT_SYMBOL(skb_tx_error);
821
822/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000823 * consume_skb - free an skbuff
824 * @skb: buffer to free
825 *
826 * Drop a ref to the buffer and free it if the usage count has hit zero
827 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
828 * is being dropped after a failure and notes that
829 */
830void consume_skb(struct sk_buff *skb)
831{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200832 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000833 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200834
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900835 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000836 __kfree_skb(skb);
837}
838EXPORT_SYMBOL(consume_skb);
839
Paolo Abeni0a463c72017-06-12 11:23:42 +0200840/**
841 * consume_stateless_skb - free an skbuff, assuming it is stateless
842 * @skb: buffer to free
843 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200844 * Alike consume_skb(), but this variant assumes that this is the last
845 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200846 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200847void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200848{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200849 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200850 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200851 kfree_skbmem(skb);
852}
853
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100854void __kfree_skb_flush(void)
855{
856 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
857
858 /* flush skb_cache if containing objects */
859 if (nc->skb_count) {
860 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
861 nc->skb_cache);
862 nc->skb_count = 0;
863 }
864}
865
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100866static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100867{
868 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
869
870 /* drop skb->head and call any destructors for packet */
871 skb_release_all(skb);
872
873 /* record skb to CPU local list */
874 nc->skb_cache[nc->skb_count++] = skb;
875
876#ifdef CONFIG_SLUB
877 /* SLUB writes into objects when freeing */
878 prefetchw(skb);
879#endif
880
881 /* flush skb_cache if it is filled */
882 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
883 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
884 nc->skb_cache);
885 nc->skb_count = 0;
886 }
887}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100888void __kfree_skb_defer(struct sk_buff *skb)
889{
890 _kfree_skb_defer(skb);
891}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100892
893void napi_consume_skb(struct sk_buff *skb, int budget)
894{
895 if (unlikely(!skb))
896 return;
897
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100898 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100899 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100900 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100901 return;
902 }
903
Paolo Abeni76088942017-06-14 11:48:48 +0200904 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100905 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200906
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100907 /* if reaching here SKB is ready to free */
908 trace_consume_skb(skb);
909
910 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700911 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100912 __kfree_skb(skb);
913 return;
914 }
915
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100916 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100917}
918EXPORT_SYMBOL(napi_consume_skb);
919
Eric Dumazetb1937222014-09-28 22:18:47 -0700920/* Make sure a field is enclosed inside headers_start/headers_end section */
921#define CHECK_SKB_FIELD(field) \
922 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
923 offsetof(struct sk_buff, headers_start)); \
924 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
925 offsetof(struct sk_buff, headers_end)); \
926
Herbert Xudec18812007-10-14 00:37:30 -0700927static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
928{
929 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700930 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700931 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700932 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000933 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100934 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -0700935 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700936
Eric Dumazetb1937222014-09-28 22:18:47 -0700937 /* Note : this field could be in headers_start/headers_end section
938 * It is not yet because we do not want to have a 16 bit hole
939 */
940 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300941
Eric Dumazetb1937222014-09-28 22:18:47 -0700942 memcpy(&new->headers_start, &old->headers_start,
943 offsetof(struct sk_buff, headers_end) -
944 offsetof(struct sk_buff, headers_start));
945 CHECK_SKB_FIELD(protocol);
946 CHECK_SKB_FIELD(csum);
947 CHECK_SKB_FIELD(hash);
948 CHECK_SKB_FIELD(priority);
949 CHECK_SKB_FIELD(skb_iif);
950 CHECK_SKB_FIELD(vlan_proto);
951 CHECK_SKB_FIELD(vlan_tci);
952 CHECK_SKB_FIELD(transport_header);
953 CHECK_SKB_FIELD(network_header);
954 CHECK_SKB_FIELD(mac_header);
955 CHECK_SKB_FIELD(inner_protocol);
956 CHECK_SKB_FIELD(inner_transport_header);
957 CHECK_SKB_FIELD(inner_network_header);
958 CHECK_SKB_FIELD(inner_mac_header);
959 CHECK_SKB_FIELD(mark);
960#ifdef CONFIG_NETWORK_SECMARK
961 CHECK_SKB_FIELD(secmark);
962#endif
Cong Wange0d10952013-08-01 11:10:25 +0800963#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700964 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300965#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800966#ifdef CONFIG_XPS
967 CHECK_SKB_FIELD(sender_cpu);
968#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700969#ifdef CONFIG_NET_SCHED
970 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700971#endif
972
Herbert Xudec18812007-10-14 00:37:30 -0700973}
974
Herbert Xu82c49a32009-05-22 22:11:37 +0000975/*
976 * You should not add any new code to this function. Add it to
977 * __copy_skb_header above instead.
978 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700979static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#define C(x) n->x = skb->x
982
983 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700985 __copy_skb_header(n, skb);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 C(len);
988 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700989 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700990 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800991 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -0700993 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +0200994 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 C(tail);
997 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800998 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000999 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -08001000 C(data);
1001 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +03001002 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 atomic_inc(&(skb_shinfo(skb)->dataref));
1005 skb->cloned = 1;
1006
1007 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001008#undef C
1009}
1010
1011/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -07001012 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1013 * @first: first sk_buff of the msg
1014 */
1015struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1016{
1017 struct sk_buff *n;
1018
1019 n = alloc_skb(0, GFP_ATOMIC);
1020 if (!n)
1021 return NULL;
1022
1023 n->len = first->len;
1024 n->data_len = first->len;
1025 n->truesize = first->truesize;
1026
1027 skb_shinfo(n)->frag_list = first;
1028
1029 __copy_skb_header(n, first);
1030 n->destructor = NULL;
1031
1032 return n;
1033}
1034EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1035
1036/**
Herbert Xue0053ec2007-10-14 00:37:52 -07001037 * skb_morph - morph one skb into another
1038 * @dst: the skb to receive the contents
1039 * @src: the skb to supply the contents
1040 *
1041 * This is identical to skb_clone except that the target skb is
1042 * supplied by the user.
1043 *
1044 * The target skb is returned upon exit.
1045 */
1046struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1047{
Herbert Xu2d4baff2007-11-26 23:11:19 +08001048 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -07001049 return __skb_clone(dst, src);
1050}
1051EXPORT_SYMBOL_GPL(skb_morph);
1052
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001053int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001054{
1055 unsigned long max_pg, num_pg, new_pg, old_pg;
1056 struct user_struct *user;
1057
1058 if (capable(CAP_IPC_LOCK) || !size)
1059 return 0;
1060
1061 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1062 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1063 user = mmp->user ? : current_user();
1064
1065 do {
1066 old_pg = atomic_long_read(&user->locked_vm);
1067 new_pg = old_pg + num_pg;
1068 if (new_pg > max_pg)
1069 return -ENOBUFS;
1070 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1071 old_pg);
1072
1073 if (!mmp->user) {
1074 mmp->user = get_uid(user);
1075 mmp->num_pg = num_pg;
1076 } else {
1077 mmp->num_pg += num_pg;
1078 }
1079
1080 return 0;
1081}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001082EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001083
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001084void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001085{
1086 if (mmp->user) {
1087 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1088 free_uid(mmp->user);
1089 }
1090}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001091EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001092
Willem de Bruijn52267792017-08-03 16:29:39 -04001093struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
1094{
1095 struct ubuf_info *uarg;
1096 struct sk_buff *skb;
1097
1098 WARN_ON_ONCE(!in_task());
1099
1100 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1101 if (!skb)
1102 return NULL;
1103
1104 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1105 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001106 uarg->mmp.user = NULL;
1107
1108 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1109 kfree_skb(skb);
1110 return NULL;
1111 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001112
1113 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001114 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1115 uarg->len = 1;
1116 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001117 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001118 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001119 sock_hold(sk);
1120
1121 return uarg;
1122}
1123EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
1124
1125static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1126{
1127 return container_of((void *)uarg, struct sk_buff, cb);
1128}
1129
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001130struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1131 struct ubuf_info *uarg)
1132{
1133 if (uarg) {
1134 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1135 u32 bytelen, next;
1136
1137 /* realloc only when socket is locked (TCP, UDP cork),
1138 * so uarg->len and sk_zckey access is serialized
1139 */
1140 if (!sock_owned_by_user(sk)) {
1141 WARN_ON_ONCE(1);
1142 return NULL;
1143 }
1144
1145 bytelen = uarg->bytelen + size;
1146 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1147 /* TCP can create new skb to attach new uarg */
1148 if (sk->sk_type == SOCK_STREAM)
1149 goto new_alloc;
1150 return NULL;
1151 }
1152
1153 next = (u32)atomic_read(&sk->sk_zckey);
1154 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001155 if (mm_account_pinned_pages(&uarg->mmp, size))
1156 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001157 uarg->len++;
1158 uarg->bytelen = bytelen;
1159 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001160
1161 /* no extra ref when appending to datagram (MSG_MORE) */
1162 if (sk->sk_type == SOCK_STREAM)
1163 sock_zerocopy_get(uarg);
1164
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001165 return uarg;
1166 }
1167 }
1168
1169new_alloc:
1170 return sock_zerocopy_alloc(sk, size);
1171}
1172EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1173
1174static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1175{
1176 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1177 u32 old_lo, old_hi;
1178 u64 sum_len;
1179
1180 old_lo = serr->ee.ee_info;
1181 old_hi = serr->ee.ee_data;
1182 sum_len = old_hi - old_lo + 1ULL + len;
1183
1184 if (sum_len >= (1ULL << 32))
1185 return false;
1186
1187 if (lo != old_hi + 1)
1188 return false;
1189
1190 serr->ee.ee_data += len;
1191 return true;
1192}
1193
Willem de Bruijn52267792017-08-03 16:29:39 -04001194void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1195{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001196 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001197 struct sock_exterr_skb *serr;
1198 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001199 struct sk_buff_head *q;
1200 unsigned long flags;
1201 u32 lo, hi;
1202 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001203
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001204 mm_unaccount_pinned_pages(&uarg->mmp);
1205
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001206 /* if !len, there was only 1 call, and it was aborted
1207 * so do not queue a completion notification
1208 */
1209 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001210 goto release;
1211
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001212 len = uarg->len;
1213 lo = uarg->id;
1214 hi = uarg->id + len - 1;
1215
Willem de Bruijn52267792017-08-03 16:29:39 -04001216 serr = SKB_EXT_ERR(skb);
1217 memset(serr, 0, sizeof(*serr));
1218 serr->ee.ee_errno = 0;
1219 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001220 serr->ee.ee_data = hi;
1221 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001222 if (!success)
1223 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1224
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001225 q = &sk->sk_error_queue;
1226 spin_lock_irqsave(&q->lock, flags);
1227 tail = skb_peek_tail(q);
1228 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1229 !skb_zerocopy_notify_extend(tail, lo, len)) {
1230 __skb_queue_tail(q, skb);
1231 skb = NULL;
1232 }
1233 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001234
1235 sk->sk_error_report(sk);
1236
1237release:
1238 consume_skb(skb);
1239 sock_put(sk);
1240}
1241EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1242
1243void sock_zerocopy_put(struct ubuf_info *uarg)
1244{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001245 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001246 if (uarg->callback)
1247 uarg->callback(uarg, uarg->zerocopy);
1248 else
1249 consume_skb(skb_from_uarg(uarg));
1250 }
1251}
1252EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1253
Willem de Bruijn52900d22018-11-30 15:32:40 -05001254void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001255{
1256 if (uarg) {
1257 struct sock *sk = skb_from_uarg(uarg)->sk;
1258
1259 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001260 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001261
Willem de Bruijn52900d22018-11-30 15:32:40 -05001262 if (have_uref)
1263 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001264 }
1265}
1266EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1267
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001268int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1269{
1270 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1271}
1272EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1273
Willem de Bruijn52267792017-08-03 16:29:39 -04001274int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1275 struct msghdr *msg, int len,
1276 struct ubuf_info *uarg)
1277{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001278 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001279 struct iov_iter orig_iter = msg->msg_iter;
1280 int err, orig_len = skb->len;
1281
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001282 /* An skb can only point to one uarg. This edge case happens when
1283 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1284 */
1285 if (orig_uarg && uarg != orig_uarg)
1286 return -EEXIST;
1287
Willem de Bruijn52267792017-08-03 16:29:39 -04001288 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1289 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001290 struct sock *save_sk = skb->sk;
1291
Willem de Bruijn52267792017-08-03 16:29:39 -04001292 /* Streams do not free skb on error. Reset to prev state. */
1293 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001294 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001295 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001296 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001297 return err;
1298 }
1299
Willem de Bruijn52900d22018-11-30 15:32:40 -05001300 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001301 return skb->len - orig_len;
1302}
1303EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1304
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001305static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001306 gfp_t gfp_mask)
1307{
1308 if (skb_zcopy(orig)) {
1309 if (skb_zcopy(nskb)) {
1310 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1311 if (!gfp_mask) {
1312 WARN_ON_ONCE(1);
1313 return -ENOMEM;
1314 }
1315 if (skb_uarg(nskb) == skb_uarg(orig))
1316 return 0;
1317 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1318 return -EIO;
1319 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001320 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001321 }
1322 return 0;
1323}
1324
Ben Hutchings2c530402012-07-10 10:55:09 +00001325/**
1326 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001327 * @skb: the skb to modify
1328 * @gfp_mask: allocation priority
1329 *
1330 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1331 * It will copy all frags into kernel and drop the reference
1332 * to userspace pages.
1333 *
1334 * If this function is called from an interrupt gfp_mask() must be
1335 * %GFP_ATOMIC.
1336 *
1337 * Returns 0 on success or a negative error code on failure
1338 * to allocate kernel memory to copy to.
1339 */
1340int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001341{
Shirley Maa6686f22011-07-06 12:22:12 +00001342 int num_frags = skb_shinfo(skb)->nr_frags;
1343 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001344 int i, new_frags;
1345 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001346
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001347 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1348 return -EINVAL;
1349
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001350 if (!num_frags)
1351 goto release;
1352
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001353 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1354 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001355 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001356 if (!page) {
1357 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001358 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001359 put_page(head);
1360 head = next;
1361 }
1362 return -ENOMEM;
1363 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001364 set_page_private(page, (unsigned long)head);
1365 head = page;
1366 }
1367
1368 page = head;
1369 d_off = 0;
1370 for (i = 0; i < num_frags; i++) {
1371 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1372 u32 p_off, p_len, copied;
1373 struct page *p;
1374 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001375
1376 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1377 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001378 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001379 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001380
1381 while (done < p_len) {
1382 if (d_off == PAGE_SIZE) {
1383 d_off = 0;
1384 page = (struct page *)page_private(page);
1385 }
1386 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1387 memcpy(page_address(page) + d_off,
1388 vaddr + p_off + done, copy);
1389 done += copy;
1390 d_off += copy;
1391 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001392 kunmap_atomic(vaddr);
1393 }
Shirley Maa6686f22011-07-06 12:22:12 +00001394 }
1395
1396 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001397 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001398 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001399
Shirley Maa6686f22011-07-06 12:22:12 +00001400 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001401 for (i = 0; i < new_frags - 1; i++) {
1402 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001403 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001404 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001405 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1406 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001407
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001408release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001409 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001410 return 0;
1411}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001412EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001413
Herbert Xue0053ec2007-10-14 00:37:52 -07001414/**
1415 * skb_clone - duplicate an sk_buff
1416 * @skb: buffer to clone
1417 * @gfp_mask: allocation priority
1418 *
1419 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1420 * copies share the same packet data but not structure. The new
1421 * buffer has a reference count of 1. If the allocation fails the
1422 * function returns %NULL otherwise the new buffer is returned.
1423 *
1424 * If this function is called from an interrupt gfp_mask() must be
1425 * %GFP_ATOMIC.
1426 */
1427
1428struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1429{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001430 struct sk_buff_fclones *fclones = container_of(skb,
1431 struct sk_buff_fclones,
1432 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001433 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001434
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001435 if (skb_orphan_frags(skb, gfp_mask))
1436 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001437
Herbert Xue0053ec2007-10-14 00:37:52 -07001438 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001439 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001440 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001441 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001442 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001443 if (skb_pfmemalloc(skb))
1444 gfp_mask |= __GFP_MEMALLOC;
1445
Herbert Xue0053ec2007-10-14 00:37:52 -07001446 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1447 if (!n)
1448 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001449
Herbert Xue0053ec2007-10-14 00:37:52 -07001450 n->fclone = SKB_FCLONE_UNAVAILABLE;
1451 }
1452
1453 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001455EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001457void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001458{
Eric Dumazet030737b2013-10-19 11:42:54 -07001459 /* Only adjust this if it actually is csum_start rather than csum */
1460 if (skb->ip_summed == CHECKSUM_PARTIAL)
1461 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001462 /* {transport,network,mac}_header and tail are relative to skb->head */
1463 skb->transport_header += off;
1464 skb->network_header += off;
1465 if (skb_mac_header_was_set(skb))
1466 skb->mac_header += off;
1467 skb->inner_transport_header += off;
1468 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001469 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001470}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001471EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001472
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001473void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Herbert Xudec18812007-10-14 00:37:30 -07001475 __copy_skb_header(new, old);
1476
Herbert Xu79671682006-06-22 02:40:14 -07001477 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1478 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1479 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001481EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Mel Gormanc93bdd02012-07-31 16:44:19 -07001483static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1484{
1485 if (skb_pfmemalloc(skb))
1486 return SKB_ALLOC_RX;
1487 return 0;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/**
1491 * skb_copy - create private copy of an sk_buff
1492 * @skb: buffer to copy
1493 * @gfp_mask: allocation priority
1494 *
1495 * Make a copy of both an &sk_buff and its data. This is used when the
1496 * caller wishes to modify the data and needs a private copy of the
1497 * data to alter. Returns %NULL on failure or the pointer to the buffer
1498 * on success. The returned buffer has a reference count of 1.
1499 *
1500 * As by-product this function converts non-linear &sk_buff to linear
1501 * one, so that &sk_buff becomes completely private and caller is allowed
1502 * to modify all the data of returned buffer. This means that this
1503 * function is not recommended for use in circumstances when only
1504 * header is going to be modified. Use pskb_copy() instead.
1505 */
1506
Al Virodd0fc662005-10-07 07:46:04 +01001507struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001509 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001510 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001511 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1512 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!n)
1515 return NULL;
1516
1517 /* Set the data pointer */
1518 skb_reserve(n, headerlen);
1519 /* Set the tail pointer and length */
1520 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Tim Hansen9f77fad2017-10-09 11:37:59 -04001522 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001524 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return n;
1526}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001527EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001530 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001532 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001534 * @fclone: if true allocate the copy of the skb from the fclone
1535 * cache instead of the head cache; it is recommended to set this
1536 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
1538 * Make a copy of both an &sk_buff and part of its data, located
1539 * in header. Fragmented data remain shared. This is used when
1540 * the caller wishes to modify only header of &sk_buff and needs
1541 * private copy of the header to alter. Returns %NULL on failure
1542 * or the pointer to the buffer on success.
1543 * The returned buffer has a reference count of 1.
1544 */
1545
Octavian Purdilabad93e92014-06-12 01:36:26 +03001546struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1547 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Eric Dumazet117632e2011-12-03 21:39:53 +00001549 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001550 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1551 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (!n)
1554 goto out;
1555
1556 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001557 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Set the tail pointer and length */
1559 skb_put(n, skb_headlen(skb));
1560 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001561 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Herbert Xu25f484a2006-11-07 14:57:15 -08001563 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 n->data_len = skb->data_len;
1565 n->len = skb->len;
1566
1567 if (skb_shinfo(skb)->nr_frags) {
1568 int i;
1569
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001570 if (skb_orphan_frags(skb, gfp_mask) ||
1571 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001572 kfree_skb(n);
1573 n = NULL;
1574 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1577 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001578 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 skb_shinfo(n)->nr_frags = i;
1581 }
1582
David S. Miller21dc3302010-08-23 00:13:46 -07001583 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1585 skb_clone_fraglist(n);
1586 }
1587
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001588 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589out:
1590 return n;
1591}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001592EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594/**
1595 * pskb_expand_head - reallocate header of &sk_buff
1596 * @skb: buffer to reallocate
1597 * @nhead: room to add at head
1598 * @ntail: room to add at tail
1599 * @gfp_mask: allocation priority
1600 *
Mathias Krausebc323832013-11-07 14:18:26 +01001601 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1602 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * reference count of 1. Returns zero in the case of success or error,
1604 * if expansion failed. In the last case, &sk_buff is not changed.
1605 *
1606 * All the pointers pointing into skb header may change and must be
1607 * reloaded after call to this function.
1608 */
1609
Victor Fusco86a76ca2005-07-08 14:57:47 -07001610int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001611 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Eric Dumazet158f3232017-01-27 07:11:27 -08001613 int i, osize = skb_end_offset(skb);
1614 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001616 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Herbert Xu4edd87a2008-10-01 07:09:38 -07001618 BUG_ON(nhead < 0);
1619
Tim Hansen9f77fad2017-10-09 11:37:59 -04001620 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 size = SKB_DATA_ALIGN(size);
1623
Mel Gormanc93bdd02012-07-31 16:44:19 -07001624 if (skb_pfmemalloc(skb))
1625 gfp_mask |= __GFP_MEMALLOC;
1626 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1627 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!data)
1629 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001630 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001633 * optimized for the cases when header is void.
1634 */
1635 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1636
1637 memcpy((struct skb_shared_info *)(data + size),
1638 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001639 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Alexander Duyck3e245912012-05-04 14:26:51 +00001641 /*
1642 * if shinfo is shared we must drop the old head gracefully, but if it
1643 * is not we can just drop the old head and let the existing refcount
1644 * be since all we did is relocate the values
1645 */
1646 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001647 if (skb_orphan_frags(skb, gfp_mask))
1648 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001649 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001650 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001651 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001652 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Dumazet1fd63042010-09-02 23:09:32 +00001654 if (skb_has_frag_list(skb))
1655 skb_clone_fraglist(skb);
1656
1657 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001658 } else {
1659 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 off = (data + nhead) - skb->head;
1662
1663 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001664 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001666#ifdef NET_SKBUFF_DATA_USES_OFFSET
1667 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001668 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001669#else
1670 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001671#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001672 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001673 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001675 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 skb->nohdr = 0;
1677 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001678
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001679 skb_metadata_clear(skb);
1680
Eric Dumazet158f3232017-01-27 07:11:27 -08001681 /* It is not generally safe to change skb->truesize.
1682 * For the moment, we really care of rx path, or
1683 * when skb is orphaned (not attached to a socket).
1684 */
1685 if (!skb->sk || skb->destructor == sock_edemux)
1686 skb->truesize += size - osize;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return 0;
1689
Shirley Maa6686f22011-07-06 12:22:12 +00001690nofrags:
1691 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692nodata:
1693 return -ENOMEM;
1694}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001695EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697/* Make private copy of skb with writable head and some headroom */
1698
1699struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1700{
1701 struct sk_buff *skb2;
1702 int delta = headroom - skb_headroom(skb);
1703
1704 if (delta <= 0)
1705 skb2 = pskb_copy(skb, GFP_ATOMIC);
1706 else {
1707 skb2 = skb_clone(skb, GFP_ATOMIC);
1708 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1709 GFP_ATOMIC)) {
1710 kfree_skb(skb2);
1711 skb2 = NULL;
1712 }
1713 }
1714 return skb2;
1715}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001716EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718/**
1719 * skb_copy_expand - copy and expand sk_buff
1720 * @skb: buffer to copy
1721 * @newheadroom: new free bytes at head
1722 * @newtailroom: new free bytes at tail
1723 * @gfp_mask: allocation priority
1724 *
1725 * Make a copy of both an &sk_buff and its data and while doing so
1726 * allocate additional space.
1727 *
1728 * This is used when the caller wishes to modify the data and needs a
1729 * private copy of the data to alter as well as more space for new fields.
1730 * Returns %NULL on failure or the pointer to the buffer
1731 * on success. The returned buffer has a reference count of 1.
1732 *
1733 * You must pass %GFP_ATOMIC as the allocation priority if this function
1734 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001737 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001738 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 /*
1741 * Allocate the copy buffer
1742 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001743 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1744 gfp_mask, skb_alloc_rx_flag(skb),
1745 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001746 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int head_copy_len, head_copy_off;
1748
1749 if (!n)
1750 return NULL;
1751
1752 skb_reserve(n, newheadroom);
1753
1754 /* Set the tail pointer and length */
1755 skb_put(n, skb->len);
1756
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001757 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 head_copy_off = 0;
1759 if (newheadroom <= head_copy_len)
1760 head_copy_len = newheadroom;
1761 else
1762 head_copy_off = newheadroom - head_copy_len;
1763
1764 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001765 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1766 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001768 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Eric Dumazet030737b2013-10-19 11:42:54 -07001770 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return n;
1773}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001774EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001777 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * @skb: buffer to pad
1779 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001780 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 *
1782 * Ensure that a buffer is followed by a padding area that is zero
1783 * filled. Used by network drivers which may DMA or transfer data
1784 * beyond the buffer end onto the wire.
1785 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001786 * May return error in out of memory cases. The skb is freed on error
1787 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001789
Florian Fainellicd0a1372017-08-22 15:12:14 -07001790int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Herbert Xu5b057c62006-06-23 02:06:41 -07001792 int err;
1793 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001796 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001800
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001801 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001802 if (likely(skb_cloned(skb) || ntail > 0)) {
1803 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1804 if (unlikely(err))
1805 goto free_skb;
1806 }
1807
1808 /* FIXME: The use of this function with non-linear skb's really needs
1809 * to be audited.
1810 */
1811 err = skb_linearize(skb);
1812 if (unlikely(err))
1813 goto free_skb;
1814
1815 memset(skb->data + skb->len, 0, pad);
1816 return 0;
1817
1818free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001819 if (free_on_error)
1820 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001821 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001822}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001823EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001824
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001825/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001826 * pskb_put - add data to the tail of a potentially fragmented buffer
1827 * @skb: start of the buffer to use
1828 * @tail: tail fragment of the buffer to use
1829 * @len: amount of data to add
1830 *
1831 * This function extends the used data area of the potentially
1832 * fragmented buffer. @tail must be the last fragment of @skb -- or
1833 * @skb itself. If this would exceed the total buffer size the kernel
1834 * will panic. A pointer to the first byte of the extra data is
1835 * returned.
1836 */
1837
Johannes Berg4df864c2017-06-16 14:29:21 +02001838void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001839{
1840 if (tail != skb) {
1841 skb->data_len += len;
1842 skb->len += len;
1843 }
1844 return skb_put(tail, len);
1845}
1846EXPORT_SYMBOL_GPL(pskb_put);
1847
1848/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001849 * skb_put - add data to a buffer
1850 * @skb: buffer to use
1851 * @len: amount of data to add
1852 *
1853 * This function extends the used data area of the buffer. If this would
1854 * exceed the total buffer size the kernel will panic. A pointer to the
1855 * first byte of the extra data is returned.
1856 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001857void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001858{
Johannes Berg4df864c2017-06-16 14:29:21 +02001859 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001860 SKB_LINEAR_ASSERT(skb);
1861 skb->tail += len;
1862 skb->len += len;
1863 if (unlikely(skb->tail > skb->end))
1864 skb_over_panic(skb, len, __builtin_return_address(0));
1865 return tmp;
1866}
1867EXPORT_SYMBOL(skb_put);
1868
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001869/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001870 * skb_push - add data to the start of a buffer
1871 * @skb: buffer to use
1872 * @len: amount of data to add
1873 *
1874 * This function extends the used data area of the buffer at the buffer
1875 * start. If this would exceed the total buffer headroom the kernel will
1876 * panic. A pointer to the first byte of the extra data is returned.
1877 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001878void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001879{
1880 skb->data -= len;
1881 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301882 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001883 skb_under_panic(skb, len, __builtin_return_address(0));
1884 return skb->data;
1885}
1886EXPORT_SYMBOL(skb_push);
1887
1888/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001889 * skb_pull - remove data from the start of a buffer
1890 * @skb: buffer to use
1891 * @len: amount of data to remove
1892 *
1893 * This function removes data from the start of a buffer, returning
1894 * the memory to the headroom. A pointer to the next data in the buffer
1895 * is returned. Once the data has been pulled future pushes will overwrite
1896 * the old data.
1897 */
Johannes Bergaf728682017-06-16 14:29:22 +02001898void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001899{
David S. Miller47d29642010-05-02 02:21:44 -07001900 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001901}
1902EXPORT_SYMBOL(skb_pull);
1903
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001904/**
1905 * skb_trim - remove end from a buffer
1906 * @skb: buffer to alter
1907 * @len: new length
1908 *
1909 * Cut the length of a buffer down by removing data from the tail. If
1910 * the buffer is already under the length specified it is not modified.
1911 * The skb must be linear.
1912 */
1913void skb_trim(struct sk_buff *skb, unsigned int len)
1914{
1915 if (skb->len > len)
1916 __skb_trim(skb, len);
1917}
1918EXPORT_SYMBOL(skb_trim);
1919
Herbert Xu3cc0e872006-06-09 16:13:38 -07001920/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
1922
Herbert Xu3cc0e872006-06-09 16:13:38 -07001923int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Herbert Xu27b437c2006-07-13 19:26:39 -07001925 struct sk_buff **fragp;
1926 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int offset = skb_headlen(skb);
1928 int nfrags = skb_shinfo(skb)->nr_frags;
1929 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001930 int err;
1931
1932 if (skb_cloned(skb) &&
1933 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001936 i = 0;
1937 if (offset >= len)
1938 goto drop_pages;
1939
1940 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001941 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001942
1943 if (end < len) {
1944 offset = end;
1945 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001947
Eric Dumazet9e903e02011-10-18 21:00:24 +00001948 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001949
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001950drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001951 skb_shinfo(skb)->nr_frags = i;
1952
1953 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001954 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001955
David S. Miller21dc3302010-08-23 00:13:46 -07001956 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001957 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001958 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
Herbert Xu27b437c2006-07-13 19:26:39 -07001961 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1962 fragp = &frag->next) {
1963 int end = offset + frag->len;
1964
1965 if (skb_shared(frag)) {
1966 struct sk_buff *nfrag;
1967
1968 nfrag = skb_clone(frag, GFP_ATOMIC);
1969 if (unlikely(!nfrag))
1970 return -ENOMEM;
1971
1972 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001973 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001974 frag = nfrag;
1975 *fragp = frag;
1976 }
1977
1978 if (end < len) {
1979 offset = end;
1980 continue;
1981 }
1982
1983 if (end > len &&
1984 unlikely((err = pskb_trim(frag, len - offset))))
1985 return err;
1986
1987 if (frag->next)
1988 skb_drop_list(&frag->next);
1989 break;
1990 }
1991
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001992done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001993 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 skb->data_len -= skb->len - len;
1995 skb->len = len;
1996 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001997 skb->len = len;
1998 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001999 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002002 if (!skb->sk || skb->destructor == sock_edemux)
2003 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002006EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Eric Dumazet88078d92018-04-18 11:43:15 -07002008/* Note : use pskb_trim_rcsum() instead of calling this directly
2009 */
2010int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2011{
2012 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2013 int delta = skb->len - len;
2014
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002015 skb->csum = csum_block_sub(skb->csum,
2016 skb_checksum(skb, len, delta, 0),
2017 len);
Eric Dumazet88078d92018-04-18 11:43:15 -07002018 }
2019 return __pskb_trim(skb, len);
2020}
2021EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/**
2024 * __pskb_pull_tail - advance tail of skb header
2025 * @skb: buffer to reallocate
2026 * @delta: number of bytes to advance tail
2027 *
2028 * The function makes a sense only on a fragmented &sk_buff,
2029 * it expands header moving its tail forward and copying necessary
2030 * data from fragmented part.
2031 *
2032 * &sk_buff MUST have reference count of 1.
2033 *
2034 * Returns %NULL (and &sk_buff does not change) if pull failed
2035 * or value of new tail of skb in the case of success.
2036 *
2037 * All the pointers pointing into skb header may change and must be
2038 * reloaded after call to this function.
2039 */
2040
2041/* Moves tail of skb head forward, copying data from fragmented part,
2042 * when it is necessary.
2043 * 1. It may fail due to malloc failure.
2044 * 2. It may change skb pointers.
2045 *
2046 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2047 */
Johannes Bergaf728682017-06-16 14:29:22 +02002048void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 /* If skb has not enough free space at tail, get new one
2051 * plus 128 bytes for future expansions. If we have enough
2052 * room at tail, reallocate without expansion only if skb is cloned.
2053 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002054 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if (eat > 0 || skb_cloned(skb)) {
2057 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2058 GFP_ATOMIC))
2059 return NULL;
2060 }
2061
Tim Hansen9f77fad2017-10-09 11:37:59 -04002062 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2063 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /* Optimization: no fragments, no reasons to preestimate
2066 * size of pulled pages. Superb.
2067 */
David S. Miller21dc3302010-08-23 00:13:46 -07002068 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 goto pull_pages;
2070
2071 /* Estimate size of pulled pages. */
2072 eat = delta;
2073 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002074 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2075
2076 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002078 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080
2081 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002082 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 * but taking into account that pulling is expected to
2084 * be very rare operation, it is worth to fight against
2085 * further bloating skb head and crucify ourselves here instead.
2086 * Pure masohism, indeed. 8)8)
2087 */
2088 if (eat) {
2089 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2090 struct sk_buff *clone = NULL;
2091 struct sk_buff *insp = NULL;
2092
2093 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (list->len <= eat) {
2095 /* Eaten as whole. */
2096 eat -= list->len;
2097 list = list->next;
2098 insp = list;
2099 } else {
2100 /* Eaten partially. */
2101
2102 if (skb_shared(list)) {
2103 /* Sucks! We need to fork list. :-( */
2104 clone = skb_clone(list, GFP_ATOMIC);
2105 if (!clone)
2106 return NULL;
2107 insp = list->next;
2108 list = clone;
2109 } else {
2110 /* This may be pulled without
2111 * problems. */
2112 insp = list;
2113 }
2114 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002115 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return NULL;
2117 }
2118 break;
2119 }
2120 } while (eat);
2121
2122 /* Free pulled out fragments. */
2123 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2124 skb_shinfo(skb)->frag_list = list->next;
2125 kfree_skb(list);
2126 }
2127 /* And insert new clone at head. */
2128 if (clone) {
2129 clone->next = list;
2130 skb_shinfo(skb)->frag_list = clone;
2131 }
2132 }
2133 /* Success! Now we may commit changes to skb data. */
2134
2135pull_pages:
2136 eat = delta;
2137 k = 0;
2138 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002139 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2140
2141 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002142 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002143 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 } else {
2145 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
2146 if (eat) {
2147 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002148 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002149 if (!i)
2150 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 eat = 0;
2152 }
2153 k++;
2154 }
2155 }
2156 skb_shinfo(skb)->nr_frags = k;
2157
linzhang3ccc6c62017-07-17 17:25:02 +08002158end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 skb->tail += delta;
2160 skb->data_len -= delta;
2161
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002162 if (!skb->data_len)
2163 skb_zcopy_clear(skb, false);
2164
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002165 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002167EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Eric Dumazet22019b12011-07-29 18:37:31 +00002169/**
2170 * skb_copy_bits - copy bits from skb to kernel buffer
2171 * @skb: source skb
2172 * @offset: offset in source
2173 * @to: destination buffer
2174 * @len: number of bytes to copy
2175 *
2176 * Copy the specified number of bytes from the source skb to the
2177 * destination buffer.
2178 *
2179 * CAUTION ! :
2180 * If its prototype is ever changed,
2181 * check arch/{*}/net/{*}.S files,
2182 * since it is called from BPF assembly code.
2183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2185{
David S. Miller1a028e52007-04-27 15:21:23 -07002186 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002187 struct sk_buff *frag_iter;
2188 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 if (offset > (int)skb->len - len)
2191 goto fault;
2192
2193 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002194 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (copy > len)
2196 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002197 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if ((len -= copy) == 0)
2199 return 0;
2200 offset += copy;
2201 to += copy;
2202 }
2203
2204 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002205 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002206 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002208 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002209
Eric Dumazet51c56b02012-04-05 11:35:15 +02002210 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002212 u32 p_off, p_len, copied;
2213 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 u8 *vaddr;
2215
2216 if (copy > len)
2217 copy = len;
2218
Willem de Bruijnc613c202017-07-31 08:15:47 -04002219 skb_frag_foreach_page(f,
2220 f->page_offset + offset - start,
2221 copy, p, p_off, p_len, copied) {
2222 vaddr = kmap_atomic(p);
2223 memcpy(to + copied, vaddr + p_off, p_len);
2224 kunmap_atomic(vaddr);
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 if ((len -= copy) == 0)
2228 return 0;
2229 offset += copy;
2230 to += copy;
2231 }
David S. Miller1a028e52007-04-27 15:21:23 -07002232 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234
David S. Millerfbb398a2009-06-09 00:18:59 -07002235 skb_walk_frags(skb, frag_iter) {
2236 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
David S. Millerfbb398a2009-06-09 00:18:59 -07002238 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
David S. Millerfbb398a2009-06-09 00:18:59 -07002240 end = start + frag_iter->len;
2241 if ((copy = end - offset) > 0) {
2242 if (copy > len)
2243 copy = len;
2244 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2245 goto fault;
2246 if ((len -= copy) == 0)
2247 return 0;
2248 offset += copy;
2249 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002251 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Shirley Maa6686f22011-07-06 12:22:12 +00002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!len)
2255 return 0;
2256
2257fault:
2258 return -EFAULT;
2259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002260EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Jens Axboe9c55e012007-11-06 23:30:13 -08002262/*
2263 * Callback from splice_to_pipe(), if we need to release some pages
2264 * at the end of the spd in case we error'ed out in filling the pipe.
2265 */
2266static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2267{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002268 put_page(spd->pages[i]);
2269}
Jens Axboe9c55e012007-11-06 23:30:13 -08002270
David S. Millera108d5f2012-04-23 23:06:11 -04002271static struct page *linear_to_page(struct page *page, unsigned int *len,
2272 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002273 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002274{
Eric Dumazet5640f762012-09-23 23:04:42 +00002275 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002276
Eric Dumazet5640f762012-09-23 23:04:42 +00002277 if (!sk_page_frag_refill(sk, pfrag))
2278 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002279
Eric Dumazet5640f762012-09-23 23:04:42 +00002280 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002281
Eric Dumazet5640f762012-09-23 23:04:42 +00002282 memcpy(page_address(pfrag->page) + pfrag->offset,
2283 page_address(page) + *offset, *len);
2284 *offset = pfrag->offset;
2285 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002286
Eric Dumazet5640f762012-09-23 23:04:42 +00002287 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002288}
2289
Eric Dumazet41c73a02012-04-22 12:26:16 +00002290static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2291 struct page *page,
2292 unsigned int offset)
2293{
2294 return spd->nr_pages &&
2295 spd->pages[spd->nr_pages - 1] == page &&
2296 (spd->partial[spd->nr_pages - 1].offset +
2297 spd->partial[spd->nr_pages - 1].len == offset);
2298}
2299
Jens Axboe9c55e012007-11-06 23:30:13 -08002300/*
2301 * Fill page/offset/length into spd, if it can hold more pages.
2302 */
David S. Millera108d5f2012-04-23 23:06:11 -04002303static bool spd_fill_page(struct splice_pipe_desc *spd,
2304 struct pipe_inode_info *pipe, struct page *page,
2305 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002306 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002307 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002308{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002309 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002310 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002311
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002312 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002313 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002314 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002315 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002316 }
2317 if (spd_can_coalesce(spd, page, offset)) {
2318 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002319 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002320 }
2321 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002322 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002323 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002324 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002325 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002326
David S. Millera108d5f2012-04-23 23:06:11 -04002327 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002328}
2329
David S. Millera108d5f2012-04-23 23:06:11 -04002330static bool __splice_segment(struct page *page, unsigned int poff,
2331 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002332 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002333 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002334 struct sock *sk,
2335 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002336{
2337 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002338 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002339
2340 /* skip this segment if already processed */
2341 if (*off >= plen) {
2342 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002343 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002344 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002345
Octavian Purdila2870c432008-07-15 00:49:11 -07002346 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002347 poff += *off;
2348 plen -= *off;
2349 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002350
Eric Dumazet18aafc62013-01-11 14:46:37 +00002351 do {
2352 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002353
Eric Dumazet18aafc62013-01-11 14:46:37 +00002354 if (spd_fill_page(spd, pipe, page, &flen, poff,
2355 linear, sk))
2356 return true;
2357 poff += flen;
2358 plen -= flen;
2359 *len -= flen;
2360 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002361
David S. Millera108d5f2012-04-23 23:06:11 -04002362 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002363}
2364
2365/*
David S. Millera108d5f2012-04-23 23:06:11 -04002366 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002367 * pipe is full or if we already spliced the requested length.
2368 */
David S. Millera108d5f2012-04-23 23:06:11 -04002369static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2370 unsigned int *offset, unsigned int *len,
2371 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002372{
2373 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002374 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002375
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002376 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002377 * If skb->head_frag is set, this 'linear' part is backed by a
2378 * fragment, and if the head is not shared with any clones then
2379 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002380 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002381 if (__splice_segment(virt_to_page(skb->data),
2382 (unsigned long) skb->data & (PAGE_SIZE - 1),
2383 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002384 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002385 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002386 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002387 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002388
2389 /*
2390 * then map the fragments
2391 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002392 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2393 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2394
Ian Campbellea2ab692011-08-22 23:44:58 +00002395 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002396 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002397 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002398 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002399 }
2400
Tom Herbertfa9835e2016-03-07 14:11:04 -08002401 skb_walk_frags(skb, iter) {
2402 if (*offset >= iter->len) {
2403 *offset -= iter->len;
2404 continue;
2405 }
2406 /* __skb_splice_bits() only fails if the output has no room
2407 * left, so no point in going over the frag_list for the error
2408 * case.
2409 */
2410 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2411 return true;
2412 }
2413
David S. Millera108d5f2012-04-23 23:06:11 -04002414 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002415}
2416
2417/*
2418 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002419 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002420 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002421int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002422 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002423 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002424{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002425 struct partial_page partial[MAX_SKB_FRAGS];
2426 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002427 struct splice_pipe_desc spd = {
2428 .pages = pages,
2429 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002430 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002431 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002432 .spd_release = sock_spd_release,
2433 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002434 int ret = 0;
2435
Tom Herbertfa9835e2016-03-07 14:11:04 -08002436 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002437
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002438 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002439 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002440
Jens Axboe35f3d142010-05-20 10:43:18 +02002441 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002442}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002443EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002444
Tom Herbert20bf50d2017-07-28 16:22:42 -07002445/* Send skb data on a socket. Socket must be locked. */
2446int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2447 int len)
2448{
2449 unsigned int orig_len = len;
2450 struct sk_buff *head = skb;
2451 unsigned short fragidx;
2452 int slen, ret;
2453
2454do_frag_list:
2455
2456 /* Deal with head data */
2457 while (offset < skb_headlen(skb) && len) {
2458 struct kvec kv;
2459 struct msghdr msg;
2460
2461 slen = min_t(int, len, skb_headlen(skb) - offset);
2462 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002463 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002464 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002465 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002466
2467 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2468 if (ret <= 0)
2469 goto error;
2470
2471 offset += ret;
2472 len -= ret;
2473 }
2474
2475 /* All the data was skb head? */
2476 if (!len)
2477 goto out;
2478
2479 /* Make offset relative to start of frags */
2480 offset -= skb_headlen(skb);
2481
2482 /* Find where we are in frag list */
2483 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2484 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2485
2486 if (offset < frag->size)
2487 break;
2488
2489 offset -= frag->size;
2490 }
2491
2492 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2493 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2494
2495 slen = min_t(size_t, len, frag->size - offset);
2496
2497 while (slen) {
2498 ret = kernel_sendpage_locked(sk, frag->page.p,
2499 frag->page_offset + offset,
2500 slen, MSG_DONTWAIT);
2501 if (ret <= 0)
2502 goto error;
2503
2504 len -= ret;
2505 offset += ret;
2506 slen -= ret;
2507 }
2508
2509 offset = 0;
2510 }
2511
2512 if (len) {
2513 /* Process any frag lists */
2514
2515 if (skb == head) {
2516 if (skb_has_frag_list(skb)) {
2517 skb = skb_shinfo(skb)->frag_list;
2518 goto do_frag_list;
2519 }
2520 } else if (skb->next) {
2521 skb = skb->next;
2522 goto do_frag_list;
2523 }
2524 }
2525
2526out:
2527 return orig_len - len;
2528
2529error:
2530 return orig_len == len ? ret : orig_len - len;
2531}
2532EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2533
Herbert Xu357b40a2005-04-19 22:30:14 -07002534/**
2535 * skb_store_bits - store bits from kernel buffer to skb
2536 * @skb: destination buffer
2537 * @offset: offset in destination
2538 * @from: source buffer
2539 * @len: number of bytes to copy
2540 *
2541 * Copy the specified number of bytes from the source buffer to the
2542 * destination skb. This function handles all the messy bits of
2543 * traversing fragment lists and such.
2544 */
2545
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002546int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002547{
David S. Miller1a028e52007-04-27 15:21:23 -07002548 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002549 struct sk_buff *frag_iter;
2550 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002551
2552 if (offset > (int)skb->len - len)
2553 goto fault;
2554
David S. Miller1a028e52007-04-27 15:21:23 -07002555 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002556 if (copy > len)
2557 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002558 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002559 if ((len -= copy) == 0)
2560 return 0;
2561 offset += copy;
2562 from += copy;
2563 }
2564
2565 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2566 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002567 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002568
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002569 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002570
Eric Dumazet9e903e02011-10-18 21:00:24 +00002571 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002572 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002573 u32 p_off, p_len, copied;
2574 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002575 u8 *vaddr;
2576
2577 if (copy > len)
2578 copy = len;
2579
Willem de Bruijnc613c202017-07-31 08:15:47 -04002580 skb_frag_foreach_page(frag,
2581 frag->page_offset + offset - start,
2582 copy, p, p_off, p_len, copied) {
2583 vaddr = kmap_atomic(p);
2584 memcpy(vaddr + p_off, from + copied, p_len);
2585 kunmap_atomic(vaddr);
2586 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002587
2588 if ((len -= copy) == 0)
2589 return 0;
2590 offset += copy;
2591 from += copy;
2592 }
David S. Miller1a028e52007-04-27 15:21:23 -07002593 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002594 }
2595
David S. Millerfbb398a2009-06-09 00:18:59 -07002596 skb_walk_frags(skb, frag_iter) {
2597 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002598
David S. Millerfbb398a2009-06-09 00:18:59 -07002599 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002600
David S. Millerfbb398a2009-06-09 00:18:59 -07002601 end = start + frag_iter->len;
2602 if ((copy = end - offset) > 0) {
2603 if (copy > len)
2604 copy = len;
2605 if (skb_store_bits(frag_iter, offset - start,
2606 from, copy))
2607 goto fault;
2608 if ((len -= copy) == 0)
2609 return 0;
2610 offset += copy;
2611 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002612 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002613 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002614 }
2615 if (!len)
2616 return 0;
2617
2618fault:
2619 return -EFAULT;
2620}
Herbert Xu357b40a2005-04-19 22:30:14 -07002621EXPORT_SYMBOL(skb_store_bits);
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002624__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2625 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
David S. Miller1a028e52007-04-27 15:21:23 -07002627 int start = skb_headlen(skb);
2628 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002629 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int pos = 0;
2631
2632 /* Checksum header. */
2633 if (copy > 0) {
2634 if (copy > len)
2635 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002636 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2637 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if ((len -= copy) == 0)
2639 return csum;
2640 offset += copy;
2641 pos = copy;
2642 }
2643
2644 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002645 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002646 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002648 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002649
Eric Dumazet51c56b02012-04-05 11:35:15 +02002650 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002652 u32 p_off, p_len, copied;
2653 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002654 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 if (copy > len)
2658 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002659
2660 skb_frag_foreach_page(frag,
2661 frag->page_offset + offset - start,
2662 copy, p, p_off, p_len, copied) {
2663 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002664 csum2 = INDIRECT_CALL_1(ops->update,
2665 csum_partial_ext,
2666 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002667 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002668 csum = INDIRECT_CALL_1(ops->combine,
2669 csum_block_add_ext, csum,
2670 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002671 pos += p_len;
2672 }
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (!(len -= copy))
2675 return csum;
2676 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
David S. Miller1a028e52007-04-27 15:21:23 -07002678 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680
David S. Millerfbb398a2009-06-09 00:18:59 -07002681 skb_walk_frags(skb, frag_iter) {
2682 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
David S. Millerfbb398a2009-06-09 00:18:59 -07002684 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
David S. Millerfbb398a2009-06-09 00:18:59 -07002686 end = start + frag_iter->len;
2687 if ((copy = end - offset) > 0) {
2688 __wsum csum2;
2689 if (copy > len)
2690 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002691 csum2 = __skb_checksum(frag_iter, offset - start,
2692 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002693 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2694 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002695 if ((len -= copy) == 0)
2696 return csum;
2697 offset += copy;
2698 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002700 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002702 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 return csum;
2705}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002706EXPORT_SYMBOL(__skb_checksum);
2707
2708__wsum skb_checksum(const struct sk_buff *skb, int offset,
2709 int len, __wsum csum)
2710{
2711 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002712 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002713 .combine = csum_block_add_ext,
2714 };
2715
2716 return __skb_checksum(skb, offset, len, csum, &ops);
2717}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002718EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720/* Both of above in one bottle. */
2721
Al Viro81d77662006-11-14 21:37:33 -08002722__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2723 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
David S. Miller1a028e52007-04-27 15:21:23 -07002725 int start = skb_headlen(skb);
2726 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002727 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 int pos = 0;
2729
2730 /* Copy header. */
2731 if (copy > 0) {
2732 if (copy > len)
2733 copy = len;
2734 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2735 copy, csum);
2736 if ((len -= copy) == 0)
2737 return csum;
2738 offset += copy;
2739 to += copy;
2740 pos = copy;
2741 }
2742
2743 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002744 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002746 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002747
Eric Dumazet9e903e02011-10-18 21:00:24 +00002748 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002750 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2751 u32 p_off, p_len, copied;
2752 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002753 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 if (copy > len)
2757 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002758
2759 skb_frag_foreach_page(frag,
2760 frag->page_offset + offset - start,
2761 copy, p, p_off, p_len, copied) {
2762 vaddr = kmap_atomic(p);
2763 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2764 to + copied,
2765 p_len, 0);
2766 kunmap_atomic(vaddr);
2767 csum = csum_block_add(csum, csum2, pos);
2768 pos += p_len;
2769 }
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (!(len -= copy))
2772 return csum;
2773 offset += copy;
2774 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
David S. Miller1a028e52007-04-27 15:21:23 -07002776 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778
David S. Millerfbb398a2009-06-09 00:18:59 -07002779 skb_walk_frags(skb, frag_iter) {
2780 __wsum csum2;
2781 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
David S. Millerfbb398a2009-06-09 00:18:59 -07002783 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
David S. Millerfbb398a2009-06-09 00:18:59 -07002785 end = start + frag_iter->len;
2786 if ((copy = end - offset) > 0) {
2787 if (copy > len)
2788 copy = len;
2789 csum2 = skb_copy_and_csum_bits(frag_iter,
2790 offset - start,
2791 to, copy, 0);
2792 csum = csum_block_add(csum, csum2, pos);
2793 if ((len -= copy) == 0)
2794 return csum;
2795 offset += copy;
2796 to += copy;
2797 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002799 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002801 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return csum;
2803}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002804EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Cong Wang49f8e832018-11-08 14:05:42 -08002806__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2807{
2808 __sum16 sum;
2809
2810 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002811 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002812 if (likely(!sum)) {
2813 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2814 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002815 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002816 }
2817 if (!skb_shared(skb))
2818 skb->csum_valid = !sum;
2819 return sum;
2820}
2821EXPORT_SYMBOL(__skb_checksum_complete_head);
2822
Cong Wang14641932018-11-26 09:31:26 -08002823/* This function assumes skb->csum already holds pseudo header's checksum,
2824 * which has been changed from the hardware checksum, for example, by
2825 * __skb_checksum_validate_complete(). And, the original skb->csum must
2826 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2827 *
2828 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2829 * zero. The new checksum is stored back into skb->csum unless the skb is
2830 * shared.
2831 */
Cong Wang49f8e832018-11-08 14:05:42 -08002832__sum16 __skb_checksum_complete(struct sk_buff *skb)
2833{
2834 __wsum csum;
2835 __sum16 sum;
2836
2837 csum = skb_checksum(skb, 0, skb->len, 0);
2838
Cong Wang49f8e832018-11-08 14:05:42 -08002839 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002840 /* This check is inverted, because we already knew the hardware
2841 * checksum is invalid before calling this function. So, if the
2842 * re-computed checksum is valid instead, then we have a mismatch
2843 * between the original skb->csum and skb_checksum(). This means either
2844 * the original hardware checksum is incorrect or we screw up skb->csum
2845 * when moving skb->data around.
2846 */
Cong Wang49f8e832018-11-08 14:05:42 -08002847 if (likely(!sum)) {
2848 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2849 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002850 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002851 }
2852
2853 if (!skb_shared(skb)) {
2854 /* Save full packet checksum */
2855 skb->csum = csum;
2856 skb->ip_summed = CHECKSUM_COMPLETE;
2857 skb->csum_complete_sw = 1;
2858 skb->csum_valid = !sum;
2859 }
2860
2861 return sum;
2862}
2863EXPORT_SYMBOL(__skb_checksum_complete);
2864
Davide Caratti96178132017-05-18 15:44:37 +02002865static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2866{
2867 net_warn_ratelimited(
2868 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2869 __func__);
2870 return 0;
2871}
2872
2873static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2874 int offset, int len)
2875{
2876 net_warn_ratelimited(
2877 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2878 __func__);
2879 return 0;
2880}
2881
2882static const struct skb_checksum_ops default_crc32c_ops = {
2883 .update = warn_crc32c_csum_update,
2884 .combine = warn_crc32c_csum_combine,
2885};
2886
2887const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2888 &default_crc32c_ops;
2889EXPORT_SYMBOL(crc32c_csum_stub);
2890
Thomas Grafaf2806f2013-12-13 15:22:17 +01002891 /**
2892 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2893 * @from: source buffer
2894 *
2895 * Calculates the amount of linear headroom needed in the 'to' skb passed
2896 * into skb_zerocopy().
2897 */
2898unsigned int
2899skb_zerocopy_headlen(const struct sk_buff *from)
2900{
2901 unsigned int hlen = 0;
2902
2903 if (!from->head_frag ||
2904 skb_headlen(from) < L1_CACHE_BYTES ||
2905 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2906 hlen = skb_headlen(from);
2907
2908 if (skb_has_frag_list(from))
2909 hlen = from->len;
2910
2911 return hlen;
2912}
2913EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2914
2915/**
2916 * skb_zerocopy - Zero copy skb to skb
2917 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002918 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002919 * @len: number of bytes to copy from source buffer
2920 * @hlen: size of linear headroom in destination buffer
2921 *
2922 * Copies up to `len` bytes from `from` to `to` by creating references
2923 * to the frags in the source buffer.
2924 *
2925 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2926 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002927 *
2928 * Return value:
2929 * 0: everything is OK
2930 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2931 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002932 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002933int
2934skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002935{
2936 int i, j = 0;
2937 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002938 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002939 struct page *page;
2940 unsigned int offset;
2941
2942 BUG_ON(!from->head_frag && !hlen);
2943
2944 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002945 if (len <= skb_tailroom(to))
2946 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002947
2948 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002949 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2950 if (unlikely(ret))
2951 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002952 len -= hlen;
2953 } else {
2954 plen = min_t(int, skb_headlen(from), len);
2955 if (plen) {
2956 page = virt_to_head_page(from->head);
2957 offset = from->data - (unsigned char *)page_address(page);
2958 __skb_fill_page_desc(to, 0, page, offset, plen);
2959 get_page(page);
2960 j = 1;
2961 len -= plen;
2962 }
2963 }
2964
2965 to->truesize += len + plen;
2966 to->len += len + plen;
2967 to->data_len += len + plen;
2968
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002969 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2970 skb_tx_error(from);
2971 return -ENOMEM;
2972 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002973 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002974
Thomas Grafaf2806f2013-12-13 15:22:17 +01002975 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2976 if (!len)
2977 break;
2978 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2979 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2980 len -= skb_shinfo(to)->frags[j].size;
2981 skb_frag_ref(to, j);
2982 j++;
2983 }
2984 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002985
2986 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002987}
2988EXPORT_SYMBOL_GPL(skb_zerocopy);
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2991{
Al Virod3bc23e2006-11-14 21:24:49 -08002992 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 long csstart;
2994
Patrick McHardy84fa7932006-08-29 16:44:56 -07002995 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002996 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 else
2998 csstart = skb_headlen(skb);
2999
Kris Katterjohn09a62662006-01-08 22:24:28 -08003000 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003002 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 csum = 0;
3005 if (csstart != skb->len)
3006 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3007 skb->len - csstart, 0);
3008
Patrick McHardy84fa7932006-08-29 16:44:56 -07003009 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003010 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Al Virod3bc23e2006-11-14 21:24:49 -08003012 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003015EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017/**
3018 * skb_dequeue - remove from the head of the queue
3019 * @list: list to dequeue from
3020 *
3021 * Remove the head of the list. The list lock is taken so the function
3022 * may be used safely with other locking list functions. The head item is
3023 * returned or %NULL if the list is empty.
3024 */
3025
3026struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3027{
3028 unsigned long flags;
3029 struct sk_buff *result;
3030
3031 spin_lock_irqsave(&list->lock, flags);
3032 result = __skb_dequeue(list);
3033 spin_unlock_irqrestore(&list->lock, flags);
3034 return result;
3035}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003036EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038/**
3039 * skb_dequeue_tail - remove from the tail of the queue
3040 * @list: list to dequeue from
3041 *
3042 * Remove the tail of the list. The list lock is taken so the function
3043 * may be used safely with other locking list functions. The tail item is
3044 * returned or %NULL if the list is empty.
3045 */
3046struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3047{
3048 unsigned long flags;
3049 struct sk_buff *result;
3050
3051 spin_lock_irqsave(&list->lock, flags);
3052 result = __skb_dequeue_tail(list);
3053 spin_unlock_irqrestore(&list->lock, flags);
3054 return result;
3055}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003056EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058/**
3059 * skb_queue_purge - empty a list
3060 * @list: list to empty
3061 *
3062 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3063 * the list and one reference dropped. This function takes the list
3064 * lock and is atomic with respect to other list locking functions.
3065 */
3066void skb_queue_purge(struct sk_buff_head *list)
3067{
3068 struct sk_buff *skb;
3069 while ((skb = skb_dequeue(list)) != NULL)
3070 kfree_skb(skb);
3071}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003072EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003075 * skb_rbtree_purge - empty a skb rbtree
3076 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003077 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003078 *
3079 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3080 * the list and one reference dropped. This function does not take
3081 * any lock. Synchronization should be handled by the caller (e.g., TCP
3082 * out-of-order queue is protected by the socket lock).
3083 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003084unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003085{
Eric Dumazet7c905842017-09-23 12:39:12 -07003086 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003087 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003088
Eric Dumazet7c905842017-09-23 12:39:12 -07003089 while (p) {
3090 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3091
3092 p = rb_next(p);
3093 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003094 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003095 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003096 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003097 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003098}
3099
3100/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 * skb_queue_head - queue a buffer at the list head
3102 * @list: list to use
3103 * @newsk: buffer to queue
3104 *
3105 * Queue a buffer at the start of the list. This function takes the
3106 * list lock and can be used safely with other locking &sk_buff functions
3107 * safely.
3108 *
3109 * A buffer cannot be placed on two lists at the same time.
3110 */
3111void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3112{
3113 unsigned long flags;
3114
3115 spin_lock_irqsave(&list->lock, flags);
3116 __skb_queue_head(list, newsk);
3117 spin_unlock_irqrestore(&list->lock, flags);
3118}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003119EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121/**
3122 * skb_queue_tail - queue a buffer at the list tail
3123 * @list: list to use
3124 * @newsk: buffer to queue
3125 *
3126 * Queue a buffer at the tail of the list. This function takes the
3127 * list lock and can be used safely with other locking &sk_buff functions
3128 * safely.
3129 *
3130 * A buffer cannot be placed on two lists at the same time.
3131 */
3132void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3133{
3134 unsigned long flags;
3135
3136 spin_lock_irqsave(&list->lock, flags);
3137 __skb_queue_tail(list, newsk);
3138 spin_unlock_irqrestore(&list->lock, flags);
3139}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003140EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142/**
3143 * skb_unlink - remove a buffer from a list
3144 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003145 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 *
David S. Miller8728b832005-08-09 19:25:21 -07003147 * Remove a packet from a list. The list locks are taken and this
3148 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 *
David S. Miller8728b832005-08-09 19:25:21 -07003150 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 */
David S. Miller8728b832005-08-09 19:25:21 -07003152void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
David S. Miller8728b832005-08-09 19:25:21 -07003154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
David S. Miller8728b832005-08-09 19:25:21 -07003156 spin_lock_irqsave(&list->lock, flags);
3157 __skb_unlink(skb, list);
3158 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003160EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162/**
3163 * skb_append - append a buffer
3164 * @old: buffer to insert after
3165 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003166 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 *
3168 * Place a packet after a given packet in a list. The list locks are taken
3169 * and this function is atomic with respect to other list locked calls.
3170 * A buffer cannot be placed on two lists at the same time.
3171 */
David S. Miller8728b832005-08-09 19:25:21 -07003172void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
3174 unsigned long flags;
3175
David S. Miller8728b832005-08-09 19:25:21 -07003176 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003177 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003178 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003180EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182static inline void skb_split_inside_header(struct sk_buff *skb,
3183 struct sk_buff* skb1,
3184 const u32 len, const int pos)
3185{
3186 int i;
3187
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003188 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3189 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /* And move data appendix as is. */
3191 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3192 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3193
3194 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3195 skb_shinfo(skb)->nr_frags = 0;
3196 skb1->data_len = skb->data_len;
3197 skb1->len += skb1->data_len;
3198 skb->data_len = 0;
3199 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003200 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
3203static inline void skb_split_no_header(struct sk_buff *skb,
3204 struct sk_buff* skb1,
3205 const u32 len, int pos)
3206{
3207 int i, k = 0;
3208 const int nfrags = skb_shinfo(skb)->nr_frags;
3209
3210 skb_shinfo(skb)->nr_frags = 0;
3211 skb1->len = skb1->data_len = skb->len - len;
3212 skb->len = len;
3213 skb->data_len = len - pos;
3214
3215 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003216 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 if (pos + size > len) {
3219 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3220
3221 if (pos < len) {
3222 /* Split frag.
3223 * We have two variants in this case:
3224 * 1. Move all the frag to the second
3225 * part, if it is possible. F.e.
3226 * this approach is mandatory for TUX,
3227 * where splitting is expensive.
3228 * 2. Split is accurately. We make this.
3229 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003230 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003232 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3233 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 skb_shinfo(skb)->nr_frags++;
3235 }
3236 k++;
3237 } else
3238 skb_shinfo(skb)->nr_frags++;
3239 pos += size;
3240 }
3241 skb_shinfo(skb1)->nr_frags = k;
3242}
3243
3244/**
3245 * skb_split - Split fragmented skb to two parts at length len.
3246 * @skb: the buffer to split
3247 * @skb1: the buffer to receive the second part
3248 * @len: new length for skb
3249 */
3250void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3251{
3252 int pos = skb_headlen(skb);
3253
Willem de Bruijnfff88032017-06-08 11:35:03 -04003254 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3255 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003256 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (len < pos) /* Split line is inside header. */
3258 skb_split_inside_header(skb, skb1, len, pos);
3259 else /* Second chunk has no header, nothing to copy. */
3260 skb_split_no_header(skb, skb1, len, pos);
3261}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003262EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003264/* Shifting from/to a cloned skb is a no-go.
3265 *
3266 * Caller cannot keep skb_shinfo related pointers past calling here!
3267 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003268static int skb_prepare_for_shift(struct sk_buff *skb)
3269{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08003270 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003271}
3272
3273/**
3274 * skb_shift - Shifts paged data partially from skb to another
3275 * @tgt: buffer into which tail data gets added
3276 * @skb: buffer from which the paged data comes from
3277 * @shiftlen: shift up to this many bytes
3278 *
3279 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003280 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003281 * It's up to caller to free skb if everything was shifted.
3282 *
3283 * If @tgt runs out of frags, the whole operation is aborted.
3284 *
3285 * Skb cannot include anything else but paged data while tgt is allowed
3286 * to have non-paged data as well.
3287 *
3288 * TODO: full sized shift could be optimized but that would need
3289 * specialized skb free'er to handle frags without up-to-date nr_frags.
3290 */
3291int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3292{
3293 int from, to, merge, todo;
3294 struct skb_frag_struct *fragfrom, *fragto;
3295
3296 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003297
3298 if (skb_headlen(skb))
3299 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003300 if (skb_zcopy(tgt) || skb_zcopy(skb))
3301 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003302
3303 todo = shiftlen;
3304 from = 0;
3305 to = skb_shinfo(tgt)->nr_frags;
3306 fragfrom = &skb_shinfo(skb)->frags[from];
3307
3308 /* Actual merge is delayed until the point when we know we can
3309 * commit all, so that we don't have to undo partial changes
3310 */
3311 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003312 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3313 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003314 merge = -1;
3315 } else {
3316 merge = to - 1;
3317
Eric Dumazet9e903e02011-10-18 21:00:24 +00003318 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003319 if (todo < 0) {
3320 if (skb_prepare_for_shift(skb) ||
3321 skb_prepare_for_shift(tgt))
3322 return 0;
3323
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003324 /* All previous frag pointers might be stale! */
3325 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003326 fragto = &skb_shinfo(tgt)->frags[merge];
3327
Eric Dumazet9e903e02011-10-18 21:00:24 +00003328 skb_frag_size_add(fragto, shiftlen);
3329 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003330 fragfrom->page_offset += shiftlen;
3331
3332 goto onlymerged;
3333 }
3334
3335 from++;
3336 }
3337
3338 /* Skip full, not-fitting skb to avoid expensive operations */
3339 if ((shiftlen == skb->len) &&
3340 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3341 return 0;
3342
3343 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3344 return 0;
3345
3346 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3347 if (to == MAX_SKB_FRAGS)
3348 return 0;
3349
3350 fragfrom = &skb_shinfo(skb)->frags[from];
3351 fragto = &skb_shinfo(tgt)->frags[to];
3352
Eric Dumazet9e903e02011-10-18 21:00:24 +00003353 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003354 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003355 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003356 from++;
3357 to++;
3358
3359 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003360 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003361 fragto->page = fragfrom->page;
3362 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003363 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003364
3365 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003366 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003367 todo = 0;
3368
3369 to++;
3370 break;
3371 }
3372 }
3373
3374 /* Ready to "commit" this state change to tgt */
3375 skb_shinfo(tgt)->nr_frags = to;
3376
3377 if (merge >= 0) {
3378 fragfrom = &skb_shinfo(skb)->frags[0];
3379 fragto = &skb_shinfo(tgt)->frags[merge];
3380
Eric Dumazet9e903e02011-10-18 21:00:24 +00003381 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003382 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003383 }
3384
3385 /* Reposition in the original skb */
3386 to = 0;
3387 while (from < skb_shinfo(skb)->nr_frags)
3388 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3389 skb_shinfo(skb)->nr_frags = to;
3390
3391 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3392
3393onlymerged:
3394 /* Most likely the tgt won't ever need its checksum anymore, skb on
3395 * the other hand might need it if it needs to be resent
3396 */
3397 tgt->ip_summed = CHECKSUM_PARTIAL;
3398 skb->ip_summed = CHECKSUM_PARTIAL;
3399
3400 /* Yak, is it really working this way? Some helper please? */
3401 skb->len -= shiftlen;
3402 skb->data_len -= shiftlen;
3403 skb->truesize -= shiftlen;
3404 tgt->len += shiftlen;
3405 tgt->data_len += shiftlen;
3406 tgt->truesize += shiftlen;
3407
3408 return shiftlen;
3409}
3410
Thomas Graf677e90e2005-06-23 20:59:51 -07003411/**
3412 * skb_prepare_seq_read - Prepare a sequential read of skb data
3413 * @skb: the buffer to read
3414 * @from: lower offset of data to be read
3415 * @to: upper offset of data to be read
3416 * @st: state variable
3417 *
3418 * Initializes the specified state variable. Must be called before
3419 * invoking skb_seq_read() for the first time.
3420 */
3421void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3422 unsigned int to, struct skb_seq_state *st)
3423{
3424 st->lower_offset = from;
3425 st->upper_offset = to;
3426 st->root_skb = st->cur_skb = skb;
3427 st->frag_idx = st->stepped_offset = 0;
3428 st->frag_data = NULL;
3429}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003430EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003431
3432/**
3433 * skb_seq_read - Sequentially read skb data
3434 * @consumed: number of bytes consumed by the caller so far
3435 * @data: destination pointer for data to be returned
3436 * @st: state variable
3437 *
Mathias Krausebc323832013-11-07 14:18:26 +01003438 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003439 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003440 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003441 * of the block or 0 if the end of the skb data or the upper
3442 * offset has been reached.
3443 *
3444 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003445 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003446 * of bytes already consumed and the next call to
3447 * skb_seq_read() will return the remaining part of the block.
3448 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003449 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003450 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003451 * reads of potentially non linear data.
3452 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003453 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003454 * at the moment, state->root_skb could be replaced with
3455 * a stack for this purpose.
3456 */
3457unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3458 struct skb_seq_state *st)
3459{
3460 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3461 skb_frag_t *frag;
3462
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003463 if (unlikely(abs_offset >= st->upper_offset)) {
3464 if (st->frag_data) {
3465 kunmap_atomic(st->frag_data);
3466 st->frag_data = NULL;
3467 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003468 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003469 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003470
3471next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003472 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003473
Thomas Chenault995b3372009-05-18 21:43:27 -07003474 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003475 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003476 return block_limit - abs_offset;
3477 }
3478
3479 if (st->frag_idx == 0 && !st->frag_data)
3480 st->stepped_offset += skb_headlen(st->cur_skb);
3481
3482 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3483 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003484 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003485
3486 if (abs_offset < block_limit) {
3487 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003488 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003489
3490 *data = (u8 *) st->frag_data + frag->page_offset +
3491 (abs_offset - st->stepped_offset);
3492
3493 return block_limit - abs_offset;
3494 }
3495
3496 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003497 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003498 st->frag_data = NULL;
3499 }
3500
3501 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003502 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003503 }
3504
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003505 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003506 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003507 st->frag_data = NULL;
3508 }
3509
David S. Miller21dc3302010-08-23 00:13:46 -07003510 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003511 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003512 st->frag_idx = 0;
3513 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003514 } else if (st->cur_skb->next) {
3515 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003516 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003517 goto next_skb;
3518 }
3519
3520 return 0;
3521}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003522EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003523
3524/**
3525 * skb_abort_seq_read - Abort a sequential read of skb data
3526 * @st: state variable
3527 *
3528 * Must be called if skb_seq_read() was not called until it
3529 * returned 0.
3530 */
3531void skb_abort_seq_read(struct skb_seq_state *st)
3532{
3533 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003534 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003535}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003536EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003537
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003538#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3539
3540static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3541 struct ts_config *conf,
3542 struct ts_state *state)
3543{
3544 return skb_seq_read(offset, text, TS_SKB_CB(state));
3545}
3546
3547static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3548{
3549 skb_abort_seq_read(TS_SKB_CB(state));
3550}
3551
3552/**
3553 * skb_find_text - Find a text pattern in skb data
3554 * @skb: the buffer to look in
3555 * @from: search offset
3556 * @to: search limit
3557 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003558 *
3559 * Finds a pattern in the skb data according to the specified
3560 * textsearch configuration. Use textsearch_next() to retrieve
3561 * subsequent occurrences of the pattern. Returns the offset
3562 * to the first occurrence or UINT_MAX if no match was found.
3563 */
3564unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003565 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003566{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003567 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003568 unsigned int ret;
3569
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003570 config->get_next_block = skb_ts_get_next_block;
3571 config->finish = skb_ts_finish;
3572
Bojan Prtvar059a2442015-02-22 11:46:35 +01003573 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003574
Bojan Prtvar059a2442015-02-22 11:46:35 +01003575 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003576 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003577}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003578EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003579
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003580int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3581 int offset, size_t size)
3582{
3583 int i = skb_shinfo(skb)->nr_frags;
3584
3585 if (skb_can_coalesce(skb, i, page, offset)) {
3586 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3587 } else if (i < MAX_SKB_FRAGS) {
3588 get_page(page);
3589 skb_fill_page_desc(skb, i, page, offset, size);
3590 } else {
3591 return -EMSGSIZE;
3592 }
3593
3594 return 0;
3595}
3596EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3597
Herbert Xucbb042f2006-03-20 22:43:56 -08003598/**
3599 * skb_pull_rcsum - pull skb and update receive checksum
3600 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003601 * @len: length of data pulled
3602 *
3603 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003604 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003605 * receive path processing instead of skb_pull unless you know
3606 * that the checksum difference is zero (e.g., a valid IP header)
3607 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003608 */
Johannes Bergaf728682017-06-16 14:29:22 +02003609void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003610{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003611 unsigned char *data = skb->data;
3612
Herbert Xucbb042f2006-03-20 22:43:56 -08003613 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003614 __skb_pull(skb, len);
3615 skb_postpull_rcsum(skb, data, len);
3616 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003617}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003618EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3619
Yonghong Song13acc942018-03-21 16:31:03 -07003620static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3621{
3622 skb_frag_t head_frag;
3623 struct page *page;
3624
3625 page = virt_to_head_page(frag_skb->head);
3626 head_frag.page.p = page;
3627 head_frag.page_offset = frag_skb->data -
3628 (unsigned char *)page_address(page);
3629 head_frag.size = skb_headlen(frag_skb);
3630 return head_frag;
3631}
3632
Herbert Xuf4c50d92006-06-22 03:02:40 -07003633/**
3634 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003635 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003636 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003637 *
3638 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003639 * a pointer to the first in a list of new skbs for the segments.
3640 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003641 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003642struct sk_buff *skb_segment(struct sk_buff *head_skb,
3643 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003644{
3645 struct sk_buff *segs = NULL;
3646 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003647 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003648 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3649 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3650 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003651 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003652 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003653 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003654 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003655 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003656 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003657 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003658 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003659 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003660 int err = -ENOMEM;
3661 int i = 0;
3662 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003663 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003664
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003665 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003666 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003667 if (unlikely(!proto))
3668 return ERR_PTR(-EINVAL);
3669
Alexander Duyck36c98382016-05-02 09:38:18 -07003670 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003671 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003672
Steffen Klassert07b26c92016-09-19 12:58:47 +02003673 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3674 if (!(features & NETIF_F_GSO_PARTIAL)) {
3675 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003676 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003677
3678 if (!list_skb ||
3679 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3680 goto normal;
3681
Ilan Tayari43170c42017-04-19 21:26:07 +03003682 /* If we get here then all the required
3683 * GSO features except frag_list are supported.
3684 * Try to split the SKB to multiple GSO SKBs
3685 * with no frag_list.
3686 * Currently we can do that only when the buffers don't
3687 * have a linear part and all the buffers except
3688 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003689 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003690 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003691 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003692 if (frag_len != iter->len && iter->next)
3693 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003694 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003695 goto normal;
3696
3697 len -= iter->len;
3698 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003699
3700 if (len != frag_len)
3701 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003702 }
3703
3704 /* GSO partial only requires that we trim off any excess that
3705 * doesn't fit into an MSS sized block, so take care of that
3706 * now.
3707 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003708 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003709 if (partial_segs > 1)
3710 mss *= partial_segs;
3711 else
3712 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003713 }
3714
Steffen Klassert07b26c92016-09-19 12:58:47 +02003715normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003716 headroom = skb_headroom(head_skb);
3717 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003718
3719 do {
3720 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003721 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003722 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003723 int size;
3724
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003725 if (unlikely(mss == GSO_BY_FRAGS)) {
3726 len = list_skb->len;
3727 } else {
3728 len = head_skb->len - offset;
3729 if (len > mss)
3730 len = mss;
3731 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003732
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003733 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003734 if (hsize < 0)
3735 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003736 if (hsize > len || !sg)
3737 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003738
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003739 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3740 (skb_headlen(list_skb) == len || sg)) {
3741 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003742
Herbert Xu9d8506c2013-11-21 11:10:04 -08003743 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003744 nfrags = skb_shinfo(list_skb)->nr_frags;
3745 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003746 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003747 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003748
3749 while (pos < offset + len) {
3750 BUG_ON(i >= nfrags);
3751
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003752 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003753 if (pos + size > offset + len)
3754 break;
3755
3756 i++;
3757 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003758 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003759 }
3760
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003761 nskb = skb_clone(list_skb, GFP_ATOMIC);
3762 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003763
3764 if (unlikely(!nskb))
3765 goto err;
3766
Herbert Xu9d8506c2013-11-21 11:10:04 -08003767 if (unlikely(pskb_trim(nskb, len))) {
3768 kfree_skb(nskb);
3769 goto err;
3770 }
3771
Alexander Duyckec47ea82012-05-04 14:26:56 +00003772 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003773 if (skb_cow_head(nskb, doffset + headroom)) {
3774 kfree_skb(nskb);
3775 goto err;
3776 }
3777
Alexander Duyckec47ea82012-05-04 14:26:56 +00003778 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003779 skb_release_head_state(nskb);
3780 __skb_push(nskb, doffset);
3781 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003782 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003783 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003784 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003785
3786 if (unlikely(!nskb))
3787 goto err;
3788
3789 skb_reserve(nskb, headroom);
3790 __skb_put(nskb, doffset);
3791 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003792
3793 if (segs)
3794 tail->next = nskb;
3795 else
3796 segs = nskb;
3797 tail = nskb;
3798
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003799 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003800
Eric Dumazet030737b2013-10-19 11:42:54 -07003801 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003802 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003803
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003804 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003805 nskb->data - tnl_hlen,
3806 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003807
Herbert Xu9d8506c2013-11-21 11:10:04 -08003808 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003809 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003810
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003811 if (!sg) {
3812 if (!nskb->remcsum_offload)
3813 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003814 SKB_GSO_CB(nskb)->csum =
3815 skb_copy_and_csum_bits(head_skb, offset,
3816 skb_put(nskb, len),
3817 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003818 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003819 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003820 continue;
3821 }
3822
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003823 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003824
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003825 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003826 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003827
Willem de Bruijnfff88032017-06-08 11:35:03 -04003828 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3829 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003830
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003831 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3832 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3833 goto err;
3834
Herbert Xu9d8506c2013-11-21 11:10:04 -08003835 while (pos < offset + len) {
3836 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08003837 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003838 nfrags = skb_shinfo(list_skb)->nr_frags;
3839 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003840 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07003841 if (!skb_headlen(list_skb)) {
3842 BUG_ON(!nfrags);
3843 } else {
3844 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003845
Yonghong Song13acc942018-03-21 16:31:03 -07003846 /* to make room for head_frag. */
3847 i--;
3848 frag--;
3849 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003850 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3851 skb_zerocopy_clone(nskb, frag_skb,
3852 GFP_ATOMIC))
3853 goto err;
3854
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003855 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003856 }
3857
3858 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3859 MAX_SKB_FRAGS)) {
3860 net_warn_ratelimited(
3861 "skb_segment: too many frags: %u %u\n",
3862 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07003863 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003864 goto err;
3865 }
3866
Yonghong Song13acc942018-03-21 16:31:03 -07003867 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003868 __skb_frag_ref(nskb_frag);
3869 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003870
3871 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003872 nskb_frag->page_offset += offset - pos;
3873 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003874 }
3875
Herbert Xu89319d382008-12-15 23:26:06 -08003876 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003877
3878 if (pos + size <= offset + len) {
3879 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003880 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003881 pos += size;
3882 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003883 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003884 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003885 }
3886
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003887 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003888 }
3889
Herbert Xu89319d382008-12-15 23:26:06 -08003890skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003891 nskb->data_len = len - hsize;
3892 nskb->len += nskb->data_len;
3893 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003894
Simon Horman1cdbcb72013-05-19 15:46:49 +00003895perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003896 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07003897 if (skb_has_shared_frag(nskb) &&
3898 __skb_linearize(nskb))
3899 goto err;
3900
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003901 if (!nskb->remcsum_offload)
3902 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003903 SKB_GSO_CB(nskb)->csum =
3904 skb_checksum(nskb, doffset,
3905 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003906 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003907 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003908 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003909 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003910
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003911 /* Some callers want to get the end of the list.
3912 * Put it in segs->prev to avoid walking the list.
3913 * (see validate_xmit_skb_list() for example)
3914 */
3915 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003916
Alexander Duyck802ab552016-04-10 21:45:03 -04003917 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003918 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003919 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003920 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003921
3922 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003923 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003924 type &= ~SKB_GSO_DODGY;
3925
3926 /* Update GSO info and prepare to start updating headers on
3927 * our way back down the stack of protocols.
3928 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003929 for (iter = segs; iter; iter = iter->next) {
3930 skb_shinfo(iter)->gso_size = gso_size;
3931 skb_shinfo(iter)->gso_segs = partial_segs;
3932 skb_shinfo(iter)->gso_type = type;
3933 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3934 }
3935
3936 if (tail->len - doffset <= gso_size)
3937 skb_shinfo(tail)->gso_size = 0;
3938 else if (tail != segs)
3939 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003940 }
3941
Toshiaki Makita432c8562014-10-27 10:30:51 -07003942 /* Following permits correct backpressure, for protocols
3943 * using skb_set_owner_w().
3944 * Idea is to tranfert ownership from head_skb to last segment.
3945 */
3946 if (head_skb->destructor == sock_wfree) {
3947 swap(tail->truesize, head_skb->truesize);
3948 swap(tail->destructor, head_skb->destructor);
3949 swap(tail->sk, head_skb->sk);
3950 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003951 return segs;
3952
3953err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003954 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003955 return ERR_PTR(err);
3956}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003957EXPORT_SYMBOL_GPL(skb_segment);
3958
David Millerd4546c22018-06-24 14:13:49 +09003959int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08003960{
Eric Dumazet8a291112013-10-08 09:02:23 -07003961 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003962 unsigned int offset = skb_gro_offset(skb);
3963 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003964 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003965 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09003966 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08003967
Steffen Klassert0ab03f32019-04-02 08:16:03 +02003968 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08003969 return -E2BIG;
3970
Eric Dumazet29e98242014-05-16 11:34:37 -07003971 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003972 pinfo = skb_shinfo(lp);
3973
3974 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003975 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003976 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003977 int i = skbinfo->nr_frags;
3978 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003979
Herbert Xu66e92fc2009-05-26 18:50:32 +00003980 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003981 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003982
Eric Dumazet8a291112013-10-08 09:02:23 -07003983 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003984 pinfo->nr_frags = nr_frags;
3985 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003986
Herbert Xu9aaa1562009-05-26 18:50:33 +00003987 frag = pinfo->frags + nr_frags;
3988 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003989 do {
3990 *--frag = *--frag2;
3991 } while (--i);
3992
3993 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003994 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003995
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003996 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003997 delta_truesize = skb->truesize -
3998 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003999
Herbert Xuf5572062009-01-14 20:40:03 -08004000 skb->truesize -= skb->data_len;
4001 skb->len -= skb->data_len;
4002 skb->data_len = 0;
4003
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004004 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004005 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004006 } else if (skb->head_frag) {
4007 int nr_frags = pinfo->nr_frags;
4008 skb_frag_t *frag = pinfo->frags + nr_frags;
4009 struct page *page = virt_to_head_page(skb->head);
4010 unsigned int first_size = headlen - offset;
4011 unsigned int first_offset;
4012
4013 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004014 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004015
4016 first_offset = skb->data -
4017 (unsigned char *)page_address(page) +
4018 offset;
4019
4020 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4021
4022 frag->page.p = page;
4023 frag->page_offset = first_offset;
4024 skb_frag_size_set(frag, first_size);
4025
4026 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4027 /* We dont need to clear skbinfo->nr_frags here */
4028
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004029 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00004030 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4031 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004032 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004033
4034merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004035 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004036 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004037 unsigned int eat = offset - headlen;
4038
4039 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00004040 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004041 skb->data_len -= eat;
4042 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004043 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004044 }
4045
Herbert Xu67147ba2009-05-26 18:50:22 +00004046 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004047
Eric Dumazet29e98242014-05-16 11:34:37 -07004048 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004049 skb_shinfo(p)->frag_list = skb;
4050 else
4051 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004052 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004053 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004054 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004055
Herbert Xu5d38a072009-01-04 16:13:40 -08004056done:
4057 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004058 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004059 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004060 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004061 if (lp != p) {
4062 lp->data_len += len;
4063 lp->truesize += delta_truesize;
4064 lp->len += len;
4065 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004066 NAPI_GRO_CB(skb)->same_flow = 1;
4067 return 0;
4068}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03004069EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08004070
Florian Westphaldf5042f2018-12-18 17:15:16 +01004071#ifdef CONFIG_SKB_EXTENSIONS
4072#define SKB_EXT_ALIGN_VALUE 8
4073#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4074
4075static const u8 skb_ext_type_len[] = {
4076#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4077 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4078#endif
Florian Westphal41650792018-12-18 17:15:27 +01004079#ifdef CONFIG_XFRM
4080 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4081#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004082};
4083
4084static __always_inline unsigned int skb_ext_total_length(void)
4085{
4086 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4087#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4088 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4089#endif
Florian Westphal41650792018-12-18 17:15:27 +01004090#ifdef CONFIG_XFRM
4091 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4092#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004093 0;
4094}
4095
4096static void skb_extensions_init(void)
4097{
4098 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4099 BUILD_BUG_ON(skb_ext_total_length() > 255);
4100
4101 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4102 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4103 0,
4104 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4105 NULL);
4106}
4107#else
4108static void skb_extensions_init(void) {}
4109#endif
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111void __init skb_init(void)
4112{
Kees Cook79a8a642018-02-07 17:44:38 -08004113 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 sizeof(struct sk_buff),
4115 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004116 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004117 offsetof(struct sk_buff, cb),
4118 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004119 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004120 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004121 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004122 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004123 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004124 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004125 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
David S. Miller51c739d2007-10-30 21:29:29 -07004128static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004129__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4130 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004131{
David S. Miller1a028e52007-04-27 15:21:23 -07004132 int start = skb_headlen(skb);
4133 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004134 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004135 int elt = 0;
4136
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004137 if (unlikely(recursion_level >= 24))
4138 return -EMSGSIZE;
4139
David Howells716ea3a2007-04-02 20:19:53 -07004140 if (copy > 0) {
4141 if (copy > len)
4142 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004143 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004144 elt++;
4145 if ((len -= copy) == 0)
4146 return elt;
4147 offset += copy;
4148 }
4149
4150 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004151 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004152
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004153 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004154
Eric Dumazet9e903e02011-10-18 21:00:24 +00004155 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004156 if ((copy = end - offset) > 0) {
4157 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004158 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4159 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004160
4161 if (copy > len)
4162 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004163 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02004164 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07004165 elt++;
4166 if (!(len -= copy))
4167 return elt;
4168 offset += copy;
4169 }
David S. Miller1a028e52007-04-27 15:21:23 -07004170 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004171 }
4172
David S. Millerfbb398a2009-06-09 00:18:59 -07004173 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004174 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004175
David S. Millerfbb398a2009-06-09 00:18:59 -07004176 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004177
David S. Millerfbb398a2009-06-09 00:18:59 -07004178 end = start + frag_iter->len;
4179 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004180 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4181 return -EMSGSIZE;
4182
David S. Millerfbb398a2009-06-09 00:18:59 -07004183 if (copy > len)
4184 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004185 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4186 copy, recursion_level + 1);
4187 if (unlikely(ret < 0))
4188 return ret;
4189 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004190 if ((len -= copy) == 0)
4191 return elt;
4192 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004193 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004194 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004195 }
4196 BUG_ON(len);
4197 return elt;
4198}
4199
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004200/**
4201 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4202 * @skb: Socket buffer containing the buffers to be mapped
4203 * @sg: The scatter-gather list to map into
4204 * @offset: The offset into the buffer's contents to start mapping
4205 * @len: Length of buffer space to be mapped
4206 *
4207 * Fill the specified scatter-gather list with mappings/pointers into a
4208 * region of the buffer space attached to a socket buffer. Returns either
4209 * the number of scatterlist items used, or -EMSGSIZE if the contents
4210 * could not fit.
4211 */
4212int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4213{
4214 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4215
4216 if (nsg <= 0)
4217 return nsg;
4218
4219 sg_mark_end(&sg[nsg - 1]);
4220
4221 return nsg;
4222}
4223EXPORT_SYMBOL_GPL(skb_to_sgvec);
4224
Fan Du25a91d82014-01-18 09:54:23 +08004225/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4226 * sglist without mark the sg which contain last skb data as the end.
4227 * So the caller can mannipulate sg list as will when padding new data after
4228 * the first call without calling sg_unmark_end to expend sg list.
4229 *
4230 * Scenario to use skb_to_sgvec_nomark:
4231 * 1. sg_init_table
4232 * 2. skb_to_sgvec_nomark(payload1)
4233 * 3. skb_to_sgvec_nomark(payload2)
4234 *
4235 * This is equivalent to:
4236 * 1. sg_init_table
4237 * 2. skb_to_sgvec(payload1)
4238 * 3. sg_unmark_end
4239 * 4. skb_to_sgvec(payload2)
4240 *
4241 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4242 * is more preferable.
4243 */
4244int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4245 int offset, int len)
4246{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004247 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004248}
4249EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4250
David S. Miller51c739d2007-10-30 21:29:29 -07004251
David S. Miller51c739d2007-10-30 21:29:29 -07004252
David Howells716ea3a2007-04-02 20:19:53 -07004253/**
4254 * skb_cow_data - Check that a socket buffer's data buffers are writable
4255 * @skb: The socket buffer to check.
4256 * @tailbits: Amount of trailing space to be added
4257 * @trailer: Returned pointer to the skb where the @tailbits space begins
4258 *
4259 * Make sure that the data buffers attached to a socket buffer are
4260 * writable. If they are not, private copies are made of the data buffers
4261 * and the socket buffer is set to use these instead.
4262 *
4263 * If @tailbits is given, make sure that there is space to write @tailbits
4264 * bytes of data beyond current end of socket buffer. @trailer will be
4265 * set to point to the skb in which this space begins.
4266 *
4267 * The number of scatterlist elements required to completely map the
4268 * COW'd and extended socket buffer will be returned.
4269 */
4270int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4271{
4272 int copyflag;
4273 int elt;
4274 struct sk_buff *skb1, **skb_p;
4275
4276 /* If skb is cloned or its head is paged, reallocate
4277 * head pulling out all the pages (pages are considered not writable
4278 * at the moment even if they are anonymous).
4279 */
4280 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4281 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4282 return -ENOMEM;
4283
4284 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004285 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004286 /* A little of trouble, not enough of space for trailer.
4287 * This should not happen, when stack is tuned to generate
4288 * good frames. OK, on miss we reallocate and reserve even more
4289 * space, 128 bytes is fair. */
4290
4291 if (skb_tailroom(skb) < tailbits &&
4292 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4293 return -ENOMEM;
4294
4295 /* Voila! */
4296 *trailer = skb;
4297 return 1;
4298 }
4299
4300 /* Misery. We are in troubles, going to mincer fragments... */
4301
4302 elt = 1;
4303 skb_p = &skb_shinfo(skb)->frag_list;
4304 copyflag = 0;
4305
4306 while ((skb1 = *skb_p) != NULL) {
4307 int ntail = 0;
4308
4309 /* The fragment is partially pulled by someone,
4310 * this can happen on input. Copy it and everything
4311 * after it. */
4312
4313 if (skb_shared(skb1))
4314 copyflag = 1;
4315
4316 /* If the skb is the last, worry about trailer. */
4317
4318 if (skb1->next == NULL && tailbits) {
4319 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004320 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004321 skb_tailroom(skb1) < tailbits)
4322 ntail = tailbits + 128;
4323 }
4324
4325 if (copyflag ||
4326 skb_cloned(skb1) ||
4327 ntail ||
4328 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004329 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004330 struct sk_buff *skb2;
4331
4332 /* Fuck, we are miserable poor guys... */
4333 if (ntail == 0)
4334 skb2 = skb_copy(skb1, GFP_ATOMIC);
4335 else
4336 skb2 = skb_copy_expand(skb1,
4337 skb_headroom(skb1),
4338 ntail,
4339 GFP_ATOMIC);
4340 if (unlikely(skb2 == NULL))
4341 return -ENOMEM;
4342
4343 if (skb1->sk)
4344 skb_set_owner_w(skb2, skb1->sk);
4345
4346 /* Looking around. Are we still alive?
4347 * OK, link new skb, drop old one */
4348
4349 skb2->next = skb1->next;
4350 *skb_p = skb2;
4351 kfree_skb(skb1);
4352 skb1 = skb2;
4353 }
4354 elt++;
4355 *trailer = skb1;
4356 skb_p = &skb1->next;
4357 }
4358
4359 return elt;
4360}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004361EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004362
Eric Dumazetb1faf562010-05-31 23:44:05 -07004363static void sock_rmem_free(struct sk_buff *skb)
4364{
4365 struct sock *sk = skb->sk;
4366
4367 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4368}
4369
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004370static void skb_set_err_queue(struct sk_buff *skb)
4371{
4372 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4373 * So, it is safe to (mis)use it to mark skbs on the error queue.
4374 */
4375 skb->pkt_type = PACKET_OUTGOING;
4376 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4377}
4378
Eric Dumazetb1faf562010-05-31 23:44:05 -07004379/*
4380 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4381 */
4382int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4383{
4384 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004385 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004386 return -ENOMEM;
4387
4388 skb_orphan(skb);
4389 skb->sk = sk;
4390 skb->destructor = sock_rmem_free;
4391 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004392 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004393
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004394 /* before exiting rcu section, make sure dst is refcounted */
4395 skb_dst_force(skb);
4396
Eric Dumazetb1faf562010-05-31 23:44:05 -07004397 skb_queue_tail(&sk->sk_error_queue, skb);
4398 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004399 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004400 return 0;
4401}
4402EXPORT_SYMBOL(sock_queue_err_skb);
4403
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004404static bool is_icmp_err_skb(const struct sk_buff *skb)
4405{
4406 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4407 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4408}
4409
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004410struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4411{
4412 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004413 struct sk_buff *skb, *skb_next = NULL;
4414 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004415 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004416
Eric Dumazet997d5c32015-02-18 05:47:55 -08004417 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004418 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004419 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004420 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004421 if (icmp_next)
4422 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4423 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004424 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004425
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004426 if (is_icmp_err_skb(skb) && !icmp_next)
4427 sk->sk_err = 0;
4428
4429 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004430 sk->sk_error_report(sk);
4431
4432 return skb;
4433}
4434EXPORT_SYMBOL(sock_dequeue_err_skb);
4435
Alexander Duyckcab41c42014-09-10 18:05:26 -04004436/**
4437 * skb_clone_sk - create clone of skb, and take reference to socket
4438 * @skb: the skb to clone
4439 *
4440 * This function creates a clone of a buffer that holds a reference on
4441 * sk_refcnt. Buffers created via this function are meant to be
4442 * returned using sock_queue_err_skb, or free via kfree_skb.
4443 *
4444 * When passing buffers allocated with this function to sock_queue_err_skb
4445 * it is necessary to wrap the call with sock_hold/sock_put in order to
4446 * prevent the socket from being released prior to being enqueued on
4447 * the sk_error_queue.
4448 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004449struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4450{
4451 struct sock *sk = skb->sk;
4452 struct sk_buff *clone;
4453
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004454 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004455 return NULL;
4456
4457 clone = skb_clone(skb, GFP_ATOMIC);
4458 if (!clone) {
4459 sock_put(sk);
4460 return NULL;
4461 }
4462
4463 clone->sk = sk;
4464 clone->destructor = sock_efree;
4465
4466 return clone;
4467}
4468EXPORT_SYMBOL(skb_clone_sk);
4469
Alexander Duyck37846ef2014-09-04 13:31:10 -04004470static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4471 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004472 int tstype,
4473 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004474{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004475 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004476 int err;
4477
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004478 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4479
Patrick Ohlyac45f602009-02-12 05:03:37 +00004480 serr = SKB_EXT_ERR(skb);
4481 memset(serr, 0, sizeof(*serr));
4482 serr->ee.ee_errno = ENOMSG;
4483 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004484 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004485 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004486 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004487 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004488 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004489 if (sk->sk_protocol == IPPROTO_TCP &&
4490 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004491 serr->ee.ee_data -= sk->sk_tskey;
4492 }
Eric Dumazet29030372010-05-29 00:20:48 -07004493
Patrick Ohlyac45f602009-02-12 05:03:37 +00004494 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004495
Patrick Ohlyac45f602009-02-12 05:03:37 +00004496 if (err)
4497 kfree_skb(skb);
4498}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004499
Willem de Bruijnb245be12015-01-30 13:29:32 -05004500static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4501{
4502 bool ret;
4503
4504 if (likely(sysctl_tstamp_allow_data || tsonly))
4505 return true;
4506
4507 read_lock_bh(&sk->sk_callback_lock);
4508 ret = sk->sk_socket && sk->sk_socket->file &&
4509 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4510 read_unlock_bh(&sk->sk_callback_lock);
4511 return ret;
4512}
4513
Alexander Duyck37846ef2014-09-04 13:31:10 -04004514void skb_complete_tx_timestamp(struct sk_buff *skb,
4515 struct skb_shared_hwtstamps *hwtstamps)
4516{
4517 struct sock *sk = skb->sk;
4518
Willem de Bruijnb245be12015-01-30 13:29:32 -05004519 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004520 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004521
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004522 /* Take a reference to prevent skb_orphan() from freeing the socket,
4523 * but only if the socket refcount is not zero.
4524 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004525 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004526 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004527 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004528 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004529 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004530 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004531
4532err:
4533 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004534}
4535EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4536
4537void __skb_tstamp_tx(struct sk_buff *orig_skb,
4538 struct skb_shared_hwtstamps *hwtstamps,
4539 struct sock *sk, int tstype)
4540{
4541 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004542 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004543
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004544 if (!sk)
4545 return;
4546
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004547 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4548 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4549 return;
4550
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004551 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4552 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004553 return;
4554
Francis Yan1c885802016-11-27 23:07:18 -08004555 if (tsonly) {
4556#ifdef CONFIG_INET
4557 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4558 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004559 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004560 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004561 opt_stats = true;
4562 } else
Francis Yan1c885802016-11-27 23:07:18 -08004563#endif
4564 skb = alloc_skb(0, GFP_ATOMIC);
4565 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004566 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004567 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004568 if (!skb)
4569 return;
4570
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004571 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004572 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4573 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004574 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4575 }
4576
4577 if (hwtstamps)
4578 *skb_hwtstamps(skb) = *hwtstamps;
4579 else
4580 skb->tstamp = ktime_get_real();
4581
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004582 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004583}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004584EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4585
4586void skb_tstamp_tx(struct sk_buff *orig_skb,
4587 struct skb_shared_hwtstamps *hwtstamps)
4588{
4589 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4590 SCM_TSTAMP_SND);
4591}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004592EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4593
Johannes Berg6e3e9392011-11-09 10:15:42 +01004594void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4595{
4596 struct sock *sk = skb->sk;
4597 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004598 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004599
4600 skb->wifi_acked_valid = 1;
4601 skb->wifi_acked = acked;
4602
4603 serr = SKB_EXT_ERR(skb);
4604 memset(serr, 0, sizeof(*serr));
4605 serr->ee.ee_errno = ENOMSG;
4606 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4607
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004608 /* Take a reference to prevent skb_orphan() from freeing the socket,
4609 * but only if the socket refcount is not zero.
4610 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004611 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004612 err = sock_queue_err_skb(sk, skb);
4613 sock_put(sk);
4614 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004615 if (err)
4616 kfree_skb(skb);
4617}
4618EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4619
Rusty Russellf35d9d82008-02-04 23:49:54 -05004620/**
4621 * skb_partial_csum_set - set up and verify partial csum values for packet
4622 * @skb: the skb to set
4623 * @start: the number of bytes after skb->data to start checksumming.
4624 * @off: the offset from start to place the checksum.
4625 *
4626 * For untrusted partially-checksummed packets, we need to make sure the values
4627 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4628 *
4629 * This function checks and sets those values and skb->ip_summed: if this
4630 * returns false you should drop the packet.
4631 */
4632bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4633{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004634 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4635 u32 csum_start = skb_headroom(skb) + (u32)start;
4636
4637 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4638 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4639 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004640 return false;
4641 }
4642 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004643 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004644 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004645 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004646 return true;
4647}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004648EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004649
Paul Durranted1f50c2014-01-09 10:02:46 +00004650static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4651 unsigned int max)
4652{
4653 if (skb_headlen(skb) >= len)
4654 return 0;
4655
4656 /* If we need to pullup then pullup to the max, so we
4657 * won't need to do it again.
4658 */
4659 if (max > skb->len)
4660 max = skb->len;
4661
4662 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4663 return -ENOMEM;
4664
4665 if (skb_headlen(skb) < len)
4666 return -EPROTO;
4667
4668 return 0;
4669}
4670
Jan Beulichf9708b42014-03-11 13:56:05 +00004671#define MAX_TCP_HDR_LEN (15 * 4)
4672
4673static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4674 typeof(IPPROTO_IP) proto,
4675 unsigned int off)
4676{
4677 switch (proto) {
4678 int err;
4679
4680 case IPPROTO_TCP:
4681 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4682 off + MAX_TCP_HDR_LEN);
4683 if (!err && !skb_partial_csum_set(skb, off,
4684 offsetof(struct tcphdr,
4685 check)))
4686 err = -EPROTO;
4687 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4688
4689 case IPPROTO_UDP:
4690 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4691 off + sizeof(struct udphdr));
4692 if (!err && !skb_partial_csum_set(skb, off,
4693 offsetof(struct udphdr,
4694 check)))
4695 err = -EPROTO;
4696 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4697 }
4698
4699 return ERR_PTR(-EPROTO);
4700}
4701
Paul Durranted1f50c2014-01-09 10:02:46 +00004702/* This value should be large enough to cover a tagged ethernet header plus
4703 * maximally sized IP and TCP or UDP headers.
4704 */
4705#define MAX_IP_HDR_LEN 128
4706
Jan Beulichf9708b42014-03-11 13:56:05 +00004707static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004708{
4709 unsigned int off;
4710 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004711 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004712 int err;
4713
4714 fragment = false;
4715
4716 err = skb_maybe_pull_tail(skb,
4717 sizeof(struct iphdr),
4718 MAX_IP_HDR_LEN);
4719 if (err < 0)
4720 goto out;
4721
4722 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4723 fragment = true;
4724
4725 off = ip_hdrlen(skb);
4726
4727 err = -EPROTO;
4728
4729 if (fragment)
4730 goto out;
4731
Jan Beulichf9708b42014-03-11 13:56:05 +00004732 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4733 if (IS_ERR(csum))
4734 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004735
Jan Beulichf9708b42014-03-11 13:56:05 +00004736 if (recalculate)
4737 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4738 ip_hdr(skb)->daddr,
4739 skb->len - off,
4740 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004741 err = 0;
4742
4743out:
4744 return err;
4745}
4746
4747/* This value should be large enough to cover a tagged ethernet header plus
4748 * an IPv6 header, all options, and a maximal TCP or UDP header.
4749 */
4750#define MAX_IPV6_HDR_LEN 256
4751
4752#define OPT_HDR(type, skb, off) \
4753 (type *)(skb_network_header(skb) + (off))
4754
4755static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4756{
4757 int err;
4758 u8 nexthdr;
4759 unsigned int off;
4760 unsigned int len;
4761 bool fragment;
4762 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004763 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004764
4765 fragment = false;
4766 done = false;
4767
4768 off = sizeof(struct ipv6hdr);
4769
4770 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4771 if (err < 0)
4772 goto out;
4773
4774 nexthdr = ipv6_hdr(skb)->nexthdr;
4775
4776 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4777 while (off <= len && !done) {
4778 switch (nexthdr) {
4779 case IPPROTO_DSTOPTS:
4780 case IPPROTO_HOPOPTS:
4781 case IPPROTO_ROUTING: {
4782 struct ipv6_opt_hdr *hp;
4783
4784 err = skb_maybe_pull_tail(skb,
4785 off +
4786 sizeof(struct ipv6_opt_hdr),
4787 MAX_IPV6_HDR_LEN);
4788 if (err < 0)
4789 goto out;
4790
4791 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4792 nexthdr = hp->nexthdr;
4793 off += ipv6_optlen(hp);
4794 break;
4795 }
4796 case IPPROTO_AH: {
4797 struct ip_auth_hdr *hp;
4798
4799 err = skb_maybe_pull_tail(skb,
4800 off +
4801 sizeof(struct ip_auth_hdr),
4802 MAX_IPV6_HDR_LEN);
4803 if (err < 0)
4804 goto out;
4805
4806 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4807 nexthdr = hp->nexthdr;
4808 off += ipv6_authlen(hp);
4809 break;
4810 }
4811 case IPPROTO_FRAGMENT: {
4812 struct frag_hdr *hp;
4813
4814 err = skb_maybe_pull_tail(skb,
4815 off +
4816 sizeof(struct frag_hdr),
4817 MAX_IPV6_HDR_LEN);
4818 if (err < 0)
4819 goto out;
4820
4821 hp = OPT_HDR(struct frag_hdr, skb, off);
4822
4823 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4824 fragment = true;
4825
4826 nexthdr = hp->nexthdr;
4827 off += sizeof(struct frag_hdr);
4828 break;
4829 }
4830 default:
4831 done = true;
4832 break;
4833 }
4834 }
4835
4836 err = -EPROTO;
4837
4838 if (!done || fragment)
4839 goto out;
4840
Jan Beulichf9708b42014-03-11 13:56:05 +00004841 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4842 if (IS_ERR(csum))
4843 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004844
Jan Beulichf9708b42014-03-11 13:56:05 +00004845 if (recalculate)
4846 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4847 &ipv6_hdr(skb)->daddr,
4848 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004849 err = 0;
4850
4851out:
4852 return err;
4853}
4854
4855/**
4856 * skb_checksum_setup - set up partial checksum offset
4857 * @skb: the skb to set up
4858 * @recalculate: if true the pseudo-header checksum will be recalculated
4859 */
4860int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4861{
4862 int err;
4863
4864 switch (skb->protocol) {
4865 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004866 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004867 break;
4868
4869 case htons(ETH_P_IPV6):
4870 err = skb_checksum_setup_ipv6(skb, recalculate);
4871 break;
4872
4873 default:
4874 err = -EPROTO;
4875 break;
4876 }
4877
4878 return err;
4879}
4880EXPORT_SYMBOL(skb_checksum_setup);
4881
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004882/**
4883 * skb_checksum_maybe_trim - maybe trims the given skb
4884 * @skb: the skb to check
4885 * @transport_len: the data length beyond the network header
4886 *
4887 * Checks whether the given skb has data beyond the given transport length.
4888 * If so, returns a cloned skb trimmed to this transport length.
4889 * Otherwise returns the provided skb. Returns NULL in error cases
4890 * (e.g. transport_len exceeds skb length or out-of-memory).
4891 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004892 * Caller needs to set the skb transport header and free any returned skb if it
4893 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004894 */
4895static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4896 unsigned int transport_len)
4897{
4898 struct sk_buff *skb_chk;
4899 unsigned int len = skb_transport_offset(skb) + transport_len;
4900 int ret;
4901
Linus Lüssinga5169932015-08-13 05:54:07 +02004902 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004903 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004904 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004905 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004906
4907 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004908 if (!skb_chk)
4909 return NULL;
4910
4911 ret = pskb_trim_rcsum(skb_chk, len);
4912 if (ret) {
4913 kfree_skb(skb_chk);
4914 return NULL;
4915 }
4916
4917 return skb_chk;
4918}
4919
4920/**
4921 * skb_checksum_trimmed - validate checksum of an skb
4922 * @skb: the skb to check
4923 * @transport_len: the data length beyond the network header
4924 * @skb_chkf: checksum function to use
4925 *
4926 * Applies the given checksum function skb_chkf to the provided skb.
4927 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4928 *
4929 * If the skb has data beyond the given transport length, then a
4930 * trimmed & cloned skb is checked and returned.
4931 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004932 * Caller needs to set the skb transport header and free any returned skb if it
4933 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004934 */
4935struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4936 unsigned int transport_len,
4937 __sum16(*skb_chkf)(struct sk_buff *skb))
4938{
4939 struct sk_buff *skb_chk;
4940 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004941 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004942
4943 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4944 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004945 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004946
Linus Lüssinga5169932015-08-13 05:54:07 +02004947 if (!pskb_may_pull(skb_chk, offset))
4948 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004949
Linus Lüssing9b368812016-02-24 04:21:42 +01004950 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004951 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004952 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004953
Linus Lüssinga5169932015-08-13 05:54:07 +02004954 if (ret)
4955 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004956
4957 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004958
4959err:
4960 if (skb_chk && skb_chk != skb)
4961 kfree_skb(skb_chk);
4962
4963 return NULL;
4964
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004965}
4966EXPORT_SYMBOL(skb_checksum_trimmed);
4967
Ben Hutchings4497b072008-06-19 16:22:28 -07004968void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4969{
Joe Perchese87cc472012-05-13 21:56:26 +00004970 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4971 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004972}
Ben Hutchings4497b072008-06-19 16:22:28 -07004973EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004974
4975void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4976{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004977 if (head_stolen) {
4978 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004979 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004980 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004981 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004982 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004983}
4984EXPORT_SYMBOL(kfree_skb_partial);
4985
4986/**
4987 * skb_try_coalesce - try to merge skb to prior one
4988 * @to: prior buffer
4989 * @from: buffer to add
4990 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004991 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004992 */
4993bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4994 bool *fragstolen, int *delta_truesize)
4995{
Eric Dumazetc818fa92017-10-04 10:48:35 -07004996 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004997 int i, delta, len = from->len;
4998
4999 *fragstolen = false;
5000
5001 if (skb_cloned(to))
5002 return false;
5003
5004 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005005 if (len)
5006 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005007 *delta_truesize = 0;
5008 return true;
5009 }
5010
Eric Dumazetc818fa92017-10-04 10:48:35 -07005011 to_shinfo = skb_shinfo(to);
5012 from_shinfo = skb_shinfo(from);
5013 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005014 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005015 if (skb_zcopy(to) || skb_zcopy(from))
5016 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005017
5018 if (skb_headlen(from) != 0) {
5019 struct page *page;
5020 unsigned int offset;
5021
Eric Dumazetc818fa92017-10-04 10:48:35 -07005022 if (to_shinfo->nr_frags +
5023 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005024 return false;
5025
5026 if (skb_head_is_locked(from))
5027 return false;
5028
5029 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5030
5031 page = virt_to_head_page(from->head);
5032 offset = from->data - (unsigned char *)page_address(page);
5033
Eric Dumazetc818fa92017-10-04 10:48:35 -07005034 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005035 page, offset, skb_headlen(from));
5036 *fragstolen = true;
5037 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005038 if (to_shinfo->nr_frags +
5039 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005040 return false;
5041
Weiping Panf4b549a2012-09-28 20:15:30 +00005042 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005043 }
5044
5045 WARN_ON_ONCE(delta < len);
5046
Eric Dumazetc818fa92017-10-04 10:48:35 -07005047 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5048 from_shinfo->frags,
5049 from_shinfo->nr_frags * sizeof(skb_frag_t));
5050 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005051
5052 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005053 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005054
Li RongQing8ea853f2012-09-18 16:53:21 +00005055 /* if the skb is not cloned this does nothing
5056 * since we set nr_frags to 0.
5057 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005058 for (i = 0; i < from_shinfo->nr_frags; i++)
5059 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005060
5061 to->truesize += delta;
5062 to->len += len;
5063 to->data_len += len;
5064
5065 *delta_truesize = delta;
5066 return true;
5067}
5068EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005069
5070/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005071 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005072 *
5073 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005074 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005075 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005076 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5077 * into/from a tunnel. Some information have to be cleared during these
5078 * operations.
5079 * skb_scrub_packet can also be used to clean a skb before injecting it in
5080 * another namespace (@xnet == true). We have to clear all information in the
5081 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005082 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005083void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005084{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005085 skb->pkt_type = PACKET_HOST;
5086 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005087 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005088 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005089 secpath_reset(skb);
5090 nf_reset(skb);
5091 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005092
Petr Machata6f9a5062018-11-19 16:11:07 +00005093#ifdef CONFIG_NET_SWITCHDEV
5094 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005095 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005096#endif
5097
Herbert Xu213dd742015-04-16 09:03:27 +08005098 if (!xnet)
5099 return;
5100
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005101 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005102 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005103 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005104}
5105EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005106
5107/**
5108 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5109 *
5110 * @skb: GSO skb
5111 *
5112 * skb_gso_transport_seglen is used to determine the real size of the
5113 * individual segments, including Layer4 headers (TCP/UDP).
5114 *
5115 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5116 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005117static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005118{
5119 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005120 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005121
Florian Westphalf993bc22014-10-20 13:49:18 +02005122 if (skb->encapsulation) {
5123 thlen = skb_inner_transport_header(skb) -
5124 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005125
Florian Westphalf993bc22014-10-20 13:49:18 +02005126 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5127 thlen += inner_tcp_hdrlen(skb);
5128 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5129 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005130 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005131 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005132 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5133 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005134 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005135 /* UFO sets gso_size to the size of the fragmentation
5136 * payload, i.e. the size of the L4 (UDP) header is already
5137 * accounted for.
5138 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005139 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005140}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005141
5142/**
5143 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5144 *
5145 * @skb: GSO skb
5146 *
5147 * skb_gso_network_seglen is used to determine the real size of the
5148 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5149 *
5150 * The MAC/L2 header is not accounted for.
5151 */
5152static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5153{
5154 unsigned int hdr_len = skb_transport_header(skb) -
5155 skb_network_header(skb);
5156
5157 return hdr_len + skb_gso_transport_seglen(skb);
5158}
5159
5160/**
5161 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5162 *
5163 * @skb: GSO skb
5164 *
5165 * skb_gso_mac_seglen is used to determine the real size of the
5166 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5167 * headers (TCP/UDP).
5168 */
5169static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5170{
5171 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5172
5173 return hdr_len + skb_gso_transport_seglen(skb);
5174}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005175
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005176/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005177 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5178 *
5179 * There are a couple of instances where we have a GSO skb, and we
5180 * want to determine what size it would be after it is segmented.
5181 *
5182 * We might want to check:
5183 * - L3+L4+payload size (e.g. IP forwarding)
5184 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5185 *
5186 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5187 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005188 * @skb: GSO skb
5189 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005190 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5191 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5192 *
5193 * @max_len: The maximum permissible length.
5194 *
5195 * Returns true if the segmented length <= max length.
5196 */
5197static inline bool skb_gso_size_check(const struct sk_buff *skb,
5198 unsigned int seg_len,
5199 unsigned int max_len) {
5200 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5201 const struct sk_buff *iter;
5202
5203 if (shinfo->gso_size != GSO_BY_FRAGS)
5204 return seg_len <= max_len;
5205
5206 /* Undo this so we can re-use header sizes */
5207 seg_len -= GSO_BY_FRAGS;
5208
5209 skb_walk_frags(skb, iter) {
5210 if (seg_len + skb_headlen(iter) > max_len)
5211 return false;
5212 }
5213
5214 return true;
5215}
5216
5217/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005218 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005219 *
5220 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005221 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005222 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005223 * skb_gso_validate_network_len validates if a given skb will fit a
5224 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5225 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005226 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005227bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005228{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005229 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005230}
Daniel Axtens779b7932018-03-01 17:13:37 +11005231EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005232
Daniel Axtens2b16f042018-01-31 14:15:33 +11005233/**
5234 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5235 *
5236 * @skb: GSO skb
5237 * @len: length to validate against
5238 *
5239 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5240 * length once split, including L2, L3 and L4 headers and the payload.
5241 */
5242bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5243{
5244 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5245}
5246EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5247
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005248static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5249{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005250 int mac_len, meta_len;
5251 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005252
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005253 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5254 kfree_skb(skb);
5255 return NULL;
5256 }
5257
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005258 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005259 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5260 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5261 mac_len - VLAN_HLEN - ETH_TLEN);
5262 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005263
5264 meta_len = skb_metadata_len(skb);
5265 if (meta_len) {
5266 meta = skb_metadata_end(skb) - meta_len;
5267 memmove(meta + VLAN_HLEN, meta, meta_len);
5268 }
5269
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005270 skb->mac_header += VLAN_HLEN;
5271 return skb;
5272}
5273
5274struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5275{
5276 struct vlan_hdr *vhdr;
5277 u16 vlan_tci;
5278
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005279 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005280 /* vlan_tci is already set-up so leave this for another time */
5281 return skb;
5282 }
5283
5284 skb = skb_share_check(skb, GFP_ATOMIC);
5285 if (unlikely(!skb))
5286 goto err_free;
5287
5288 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5289 goto err_free;
5290
5291 vhdr = (struct vlan_hdr *)skb->data;
5292 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5293 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5294
5295 skb_pull_rcsum(skb, VLAN_HLEN);
5296 vlan_set_encap_proto(skb, vhdr);
5297
5298 skb = skb_reorder_vlan_header(skb);
5299 if (unlikely(!skb))
5300 goto err_free;
5301
5302 skb_reset_network_header(skb);
5303 skb_reset_transport_header(skb);
5304 skb_reset_mac_len(skb);
5305
5306 return skb;
5307
5308err_free:
5309 kfree_skb(skb);
5310 return NULL;
5311}
5312EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005313
Jiri Pirkoe2195122014-11-19 14:05:01 +01005314int skb_ensure_writable(struct sk_buff *skb, int write_len)
5315{
5316 if (!pskb_may_pull(skb, write_len))
5317 return -ENOMEM;
5318
5319 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5320 return 0;
5321
5322 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5323}
5324EXPORT_SYMBOL(skb_ensure_writable);
5325
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005326/* remove VLAN header from packet and update csum accordingly.
5327 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5328 */
5329int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005330{
5331 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005332 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005333 int err;
5334
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005335 if (WARN_ONCE(offset,
5336 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5337 offset)) {
5338 return -EINVAL;
5339 }
5340
Jiri Pirko93515d52014-11-19 14:05:02 +01005341 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5342 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005343 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005344
5345 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5346
5347 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5348 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5349
5350 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5351 __skb_pull(skb, VLAN_HLEN);
5352
5353 vlan_set_encap_proto(skb, vhdr);
5354 skb->mac_header += VLAN_HLEN;
5355
5356 if (skb_network_offset(skb) < ETH_HLEN)
5357 skb_set_network_header(skb, ETH_HLEN);
5358
5359 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005360
5361 return err;
5362}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005363EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005364
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005365/* Pop a vlan tag either from hwaccel or from payload.
5366 * Expects skb->data at mac header.
5367 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005368int skb_vlan_pop(struct sk_buff *skb)
5369{
5370 u16 vlan_tci;
5371 __be16 vlan_proto;
5372 int err;
5373
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005374 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005375 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005376 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005377 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005378 return 0;
5379
5380 err = __skb_vlan_pop(skb, &vlan_tci);
5381 if (err)
5382 return err;
5383 }
5384 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005385 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005386 return 0;
5387
5388 vlan_proto = skb->protocol;
5389 err = __skb_vlan_pop(skb, &vlan_tci);
5390 if (unlikely(err))
5391 return err;
5392
5393 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5394 return 0;
5395}
5396EXPORT_SYMBOL(skb_vlan_pop);
5397
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005398/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5399 * Expects skb->data at mac header.
5400 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005401int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5402{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005403 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005404 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005405 int err;
5406
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005407 if (WARN_ONCE(offset,
5408 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5409 offset)) {
5410 return -EINVAL;
5411 }
5412
Jiri Pirko93515d52014-11-19 14:05:02 +01005413 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005414 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005415 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005416 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005417
Jiri Pirko93515d52014-11-19 14:05:02 +01005418 skb->protocol = skb->vlan_proto;
5419 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005420
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005421 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005422 }
5423 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5424 return 0;
5425}
5426EXPORT_SYMBOL(skb_vlan_push);
5427
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005428/**
5429 * alloc_skb_with_frags - allocate skb with page frags
5430 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005431 * @header_len: size of linear part
5432 * @data_len: needed length in frags
5433 * @max_page_order: max page order desired.
5434 * @errcode: pointer to error code if any
5435 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005436 *
5437 * This can be used to allocate a paged skb, given a maximal order for frags.
5438 */
5439struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5440 unsigned long data_len,
5441 int max_page_order,
5442 int *errcode,
5443 gfp_t gfp_mask)
5444{
5445 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5446 unsigned long chunk;
5447 struct sk_buff *skb;
5448 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005449 int i;
5450
5451 *errcode = -EMSGSIZE;
5452 /* Note this test could be relaxed, if we succeed to allocate
5453 * high order pages...
5454 */
5455 if (npages > MAX_SKB_FRAGS)
5456 return NULL;
5457
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005458 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005459 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005460 if (!skb)
5461 return NULL;
5462
5463 skb->truesize += npages << PAGE_SHIFT;
5464
5465 for (i = 0; npages > 0; i++) {
5466 int order = max_page_order;
5467
5468 while (order) {
5469 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005470 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005471 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005472 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005473 order);
5474 if (page)
5475 goto fill_page;
5476 /* Do not retry other high order allocations */
5477 order = 1;
5478 max_page_order = 0;
5479 }
5480 order--;
5481 }
5482 page = alloc_page(gfp_mask);
5483 if (!page)
5484 goto failure;
5485fill_page:
5486 chunk = min_t(unsigned long, data_len,
5487 PAGE_SIZE << order);
5488 skb_fill_page_desc(skb, i, page, 0, chunk);
5489 data_len -= chunk;
5490 npages -= 1 << order;
5491 }
5492 return skb;
5493
5494failure:
5495 kfree_skb(skb);
5496 return NULL;
5497}
5498EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005499
5500/* carve out the first off bytes from skb when off < headlen */
5501static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5502 const int headlen, gfp_t gfp_mask)
5503{
5504 int i;
5505 int size = skb_end_offset(skb);
5506 int new_hlen = headlen - off;
5507 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005508
5509 size = SKB_DATA_ALIGN(size);
5510
5511 if (skb_pfmemalloc(skb))
5512 gfp_mask |= __GFP_MEMALLOC;
5513 data = kmalloc_reserve(size +
5514 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5515 gfp_mask, NUMA_NO_NODE, NULL);
5516 if (!data)
5517 return -ENOMEM;
5518
5519 size = SKB_WITH_OVERHEAD(ksize(data));
5520
5521 /* Copy real data, and all frags */
5522 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5523 skb->len -= off;
5524
5525 memcpy((struct skb_shared_info *)(data + size),
5526 skb_shinfo(skb),
5527 offsetof(struct skb_shared_info,
5528 frags[skb_shinfo(skb)->nr_frags]));
5529 if (skb_cloned(skb)) {
5530 /* drop the old head gracefully */
5531 if (skb_orphan_frags(skb, gfp_mask)) {
5532 kfree(data);
5533 return -ENOMEM;
5534 }
5535 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5536 skb_frag_ref(skb, i);
5537 if (skb_has_frag_list(skb))
5538 skb_clone_fraglist(skb);
5539 skb_release_data(skb);
5540 } else {
5541 /* we can reuse existing recount- all we did was
5542 * relocate values
5543 */
5544 skb_free_head(skb);
5545 }
5546
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005547 skb->head = data;
5548 skb->data = data;
5549 skb->head_frag = 0;
5550#ifdef NET_SKBUFF_DATA_USES_OFFSET
5551 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005552#else
5553 skb->end = skb->head + size;
5554#endif
5555 skb_set_tail_pointer(skb, skb_headlen(skb));
5556 skb_headers_offset_update(skb, 0);
5557 skb->cloned = 0;
5558 skb->hdr_len = 0;
5559 skb->nohdr = 0;
5560 atomic_set(&skb_shinfo(skb)->dataref, 1);
5561
5562 return 0;
5563}
5564
5565static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5566
5567/* carve out the first eat bytes from skb's frag_list. May recurse into
5568 * pskb_carve()
5569 */
5570static int pskb_carve_frag_list(struct sk_buff *skb,
5571 struct skb_shared_info *shinfo, int eat,
5572 gfp_t gfp_mask)
5573{
5574 struct sk_buff *list = shinfo->frag_list;
5575 struct sk_buff *clone = NULL;
5576 struct sk_buff *insp = NULL;
5577
5578 do {
5579 if (!list) {
5580 pr_err("Not enough bytes to eat. Want %d\n", eat);
5581 return -EFAULT;
5582 }
5583 if (list->len <= eat) {
5584 /* Eaten as whole. */
5585 eat -= list->len;
5586 list = list->next;
5587 insp = list;
5588 } else {
5589 /* Eaten partially. */
5590 if (skb_shared(list)) {
5591 clone = skb_clone(list, gfp_mask);
5592 if (!clone)
5593 return -ENOMEM;
5594 insp = list->next;
5595 list = clone;
5596 } else {
5597 /* This may be pulled without problems. */
5598 insp = list;
5599 }
5600 if (pskb_carve(list, eat, gfp_mask) < 0) {
5601 kfree_skb(clone);
5602 return -ENOMEM;
5603 }
5604 break;
5605 }
5606 } while (eat);
5607
5608 /* Free pulled out fragments. */
5609 while ((list = shinfo->frag_list) != insp) {
5610 shinfo->frag_list = list->next;
5611 kfree_skb(list);
5612 }
5613 /* And insert new clone at head. */
5614 if (clone) {
5615 clone->next = list;
5616 shinfo->frag_list = clone;
5617 }
5618 return 0;
5619}
5620
5621/* carve off first len bytes from skb. Split line (off) is in the
5622 * non-linear part of skb
5623 */
5624static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5625 int pos, gfp_t gfp_mask)
5626{
5627 int i, k = 0;
5628 int size = skb_end_offset(skb);
5629 u8 *data;
5630 const int nfrags = skb_shinfo(skb)->nr_frags;
5631 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005632
5633 size = SKB_DATA_ALIGN(size);
5634
5635 if (skb_pfmemalloc(skb))
5636 gfp_mask |= __GFP_MEMALLOC;
5637 data = kmalloc_reserve(size +
5638 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5639 gfp_mask, NUMA_NO_NODE, NULL);
5640 if (!data)
5641 return -ENOMEM;
5642
5643 size = SKB_WITH_OVERHEAD(ksize(data));
5644
5645 memcpy((struct skb_shared_info *)(data + size),
5646 skb_shinfo(skb), offsetof(struct skb_shared_info,
5647 frags[skb_shinfo(skb)->nr_frags]));
5648 if (skb_orphan_frags(skb, gfp_mask)) {
5649 kfree(data);
5650 return -ENOMEM;
5651 }
5652 shinfo = (struct skb_shared_info *)(data + size);
5653 for (i = 0; i < nfrags; i++) {
5654 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5655
5656 if (pos + fsize > off) {
5657 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5658
5659 if (pos < off) {
5660 /* Split frag.
5661 * We have two variants in this case:
5662 * 1. Move all the frag to the second
5663 * part, if it is possible. F.e.
5664 * this approach is mandatory for TUX,
5665 * where splitting is expensive.
5666 * 2. Split is accurately. We make this.
5667 */
5668 shinfo->frags[0].page_offset += off - pos;
5669 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5670 }
5671 skb_frag_ref(skb, i);
5672 k++;
5673 }
5674 pos += fsize;
5675 }
5676 shinfo->nr_frags = k;
5677 if (skb_has_frag_list(skb))
5678 skb_clone_fraglist(skb);
5679
5680 if (k == 0) {
5681 /* split line is in frag list */
5682 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5683 }
5684 skb_release_data(skb);
5685
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005686 skb->head = data;
5687 skb->head_frag = 0;
5688 skb->data = data;
5689#ifdef NET_SKBUFF_DATA_USES_OFFSET
5690 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005691#else
5692 skb->end = skb->head + size;
5693#endif
5694 skb_reset_tail_pointer(skb);
5695 skb_headers_offset_update(skb, 0);
5696 skb->cloned = 0;
5697 skb->hdr_len = 0;
5698 skb->nohdr = 0;
5699 skb->len -= off;
5700 skb->data_len = skb->len;
5701 atomic_set(&skb_shinfo(skb)->dataref, 1);
5702 return 0;
5703}
5704
5705/* remove len bytes from the beginning of the skb */
5706static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5707{
5708 int headlen = skb_headlen(skb);
5709
5710 if (len < headlen)
5711 return pskb_carve_inside_header(skb, len, headlen, gfp);
5712 else
5713 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5714}
5715
5716/* Extract to_copy bytes starting at off from skb, and return this in
5717 * a new skb
5718 */
5719struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5720 int to_copy, gfp_t gfp)
5721{
5722 struct sk_buff *clone = skb_clone(skb, gfp);
5723
5724 if (!clone)
5725 return NULL;
5726
5727 if (pskb_carve(clone, off, gfp) < 0 ||
5728 pskb_trim(clone, to_copy)) {
5729 kfree_skb(clone);
5730 return NULL;
5731 }
5732 return clone;
5733}
5734EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005735
5736/**
5737 * skb_condense - try to get rid of fragments/frag_list if possible
5738 * @skb: buffer
5739 *
5740 * Can be used to save memory before skb is added to a busy queue.
5741 * If packet has bytes in frags and enough tail room in skb->head,
5742 * pull all of them, so that we can free the frags right now and adjust
5743 * truesize.
5744 * Notes:
5745 * We do not reallocate skb->head thus can not fail.
5746 * Caller must re-evaluate skb->truesize if needed.
5747 */
5748void skb_condense(struct sk_buff *skb)
5749{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005750 if (skb->data_len) {
5751 if (skb->data_len > skb->end - skb->tail ||
5752 skb_cloned(skb))
5753 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005754
Eric Dumazet3174fed2016-12-09 08:02:05 -08005755 /* Nice, we can free page frag(s) right now */
5756 __pskb_pull_tail(skb, skb->data_len);
5757 }
5758 /* At this point, skb->truesize might be over estimated,
5759 * because skb had a fragment, and fragments do not tell
5760 * their truesize.
5761 * When we pulled its content into skb->head, fragment
5762 * was freed, but __pskb_pull_tail() could not possibly
5763 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005764 */
5765 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5766}
Florian Westphaldf5042f2018-12-18 17:15:16 +01005767
5768#ifdef CONFIG_SKB_EXTENSIONS
5769static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5770{
5771 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5772}
5773
5774static struct skb_ext *skb_ext_alloc(void)
5775{
5776 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5777
5778 if (new) {
5779 memset(new->offset, 0, sizeof(new->offset));
5780 refcount_set(&new->refcnt, 1);
5781 }
5782
5783 return new;
5784}
5785
Florian Westphal41650792018-12-18 17:15:27 +01005786static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5787 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01005788{
5789 struct skb_ext *new;
5790
5791 if (refcount_read(&old->refcnt) == 1)
5792 return old;
5793
5794 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5795 if (!new)
5796 return NULL;
5797
5798 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5799 refcount_set(&new->refcnt, 1);
5800
Florian Westphal41650792018-12-18 17:15:27 +01005801#ifdef CONFIG_XFRM
5802 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5803 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5804 unsigned int i;
5805
5806 for (i = 0; i < sp->len; i++)
5807 xfrm_state_hold(sp->xvec[i]);
5808 }
5809#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005810 __skb_ext_put(old);
5811 return new;
5812}
5813
5814/**
5815 * skb_ext_add - allocate space for given extension, COW if needed
5816 * @skb: buffer
5817 * @id: extension to allocate space for
5818 *
5819 * Allocates enough space for the given extension.
5820 * If the extension is already present, a pointer to that extension
5821 * is returned.
5822 *
5823 * If the skb was cloned, COW applies and the returned memory can be
5824 * modified without changing the extension space of clones buffers.
5825 *
5826 * Returns pointer to the extension or NULL on allocation failure.
5827 */
5828void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5829{
5830 struct skb_ext *new, *old = NULL;
5831 unsigned int newlen, newoff;
5832
5833 if (skb->active_extensions) {
5834 old = skb->extensions;
5835
Florian Westphal41650792018-12-18 17:15:27 +01005836 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01005837 if (!new)
5838 return NULL;
5839
Paolo Abeni682ec852018-12-21 19:03:15 +01005840 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01005841 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005842
Paolo Abenie94e50b2018-12-21 19:03:13 +01005843 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005844 } else {
5845 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5846
5847 new = skb_ext_alloc();
5848 if (!new)
5849 return NULL;
5850 }
5851
5852 newlen = newoff + skb_ext_type_len[id];
5853 new->chunks = newlen;
5854 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005855set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01005856 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005857 skb->active_extensions |= 1 << id;
5858 return skb_ext_get_ptr(new, id);
5859}
5860EXPORT_SYMBOL(skb_ext_add);
5861
Florian Westphal41650792018-12-18 17:15:27 +01005862#ifdef CONFIG_XFRM
5863static void skb_ext_put_sp(struct sec_path *sp)
5864{
5865 unsigned int i;
5866
5867 for (i = 0; i < sp->len; i++)
5868 xfrm_state_put(sp->xvec[i]);
5869}
5870#endif
5871
Florian Westphaldf5042f2018-12-18 17:15:16 +01005872void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5873{
5874 struct skb_ext *ext = skb->extensions;
5875
5876 skb->active_extensions &= ~(1 << id);
5877 if (skb->active_extensions == 0) {
5878 skb->extensions = NULL;
5879 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01005880#ifdef CONFIG_XFRM
5881 } else if (id == SKB_EXT_SEC_PATH &&
5882 refcount_read(&ext->refcnt) == 1) {
5883 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5884
5885 skb_ext_put_sp(sp);
5886 sp->len = 0;
5887#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005888 }
5889}
5890EXPORT_SYMBOL(__skb_ext_del);
5891
5892void __skb_ext_put(struct skb_ext *ext)
5893{
5894 /* If this is last clone, nothing can increment
5895 * it after check passes. Avoids one atomic op.
5896 */
5897 if (refcount_read(&ext->refcnt) == 1)
5898 goto free_now;
5899
5900 if (!refcount_dec_and_test(&ext->refcnt))
5901 return;
5902free_now:
Florian Westphal41650792018-12-18 17:15:27 +01005903#ifdef CONFIG_XFRM
5904 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5905 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5906#endif
5907
Florian Westphaldf5042f2018-12-18 17:15:16 +01005908 kmem_cache_free(skbuff_ext_cache, ext);
5909}
5910EXPORT_SYMBOL(__skb_ext_put);
5911#endif /* CONFIG_SKB_EXTENSIONS */