blob: cf2c4dcf425790f51625a6a1c8e0bb21ce7db919 [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>
John Hurley2a2ea502019-07-07 15:01:57 +010062#include <linux/mpls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <net/protocol.h>
65#include <net/dst.h>
66#include <net/sock.h>
67#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000068#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/xfrm.h>
John Hurley8822e272019-07-07 15:01:54 +010070#include <net/mpls.h>
Mat Martineau3ee17bc2020-01-09 07:59:19 -080071#include <net/mptcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080073#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040074#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020075#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050076#include <linux/capability.h>
77#include <linux/user_namespace.h>
Matteo Croce2544af02019-05-29 17:13:48 +020078#include <linux/indirect_call_wrapper.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079
Bart Van Assche7b7ed882019-03-25 09:17:23 -070080#include "datagram.h"
81
Alexey Dobriyan08009a72018-02-24 21:20:33 +030082struct kmem_cache *skbuff_head_cache __ro_after_init;
83static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
Florian Westphaldf5042f2018-12-18 17:15:16 +010084#ifdef CONFIG_SKB_EXTENSIONS
85static struct kmem_cache *skbuff_ext_cache __ro_after_init;
86#endif
Hans Westgaard Ry5f74f82e2016-02-03 09:26:57 +010087int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
88EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/**
Jean Sacrenf05de732013-02-11 13:30:38 +000091 * skb_panic - private function for out-of-line support
92 * @skb: buffer
93 * @sz: size
94 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000095 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Jean Sacrenf05de732013-02-11 13:30:38 +000097 * Out-of-line support for skb_put() and skb_push().
98 * Called via the wrapper skb_over_panic() or skb_under_panic().
99 * Keep out of line to prevent kernel bloat.
100 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Jean Sacrenf05de732013-02-11 13:30:38 +0000102static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +0000103 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jesper Dangaard Brouer41a46912020-04-27 18:37:43 +0200105 pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000106 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000107 (unsigned long)skb->tail, (unsigned long)skb->end,
108 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 BUG();
110}
111
Jean Sacrenf05de732013-02-11 13:30:38 +0000112static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Jean Sacrenf05de732013-02-11 13:30:38 +0000114 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Jean Sacrenf05de732013-02-11 13:30:38 +0000117static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
118{
119 skb_panic(skb, sz, addr, __func__);
120}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700121
122/*
123 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
124 * the caller if emergency pfmemalloc reserves are being used. If it is and
125 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
126 * may be used. Otherwise, the packet data may be discarded until enough
127 * memory is free
128 */
129#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
130 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000131
132static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
133 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700134{
135 void *obj;
136 bool ret_pfmemalloc = false;
137
138 /*
139 * Try a regular allocation, when that fails and we're not entitled
140 * to the reserves, fail.
141 */
142 obj = kmalloc_node_track_caller(size,
143 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
144 node);
145 if (obj || !(gfp_pfmemalloc_allowed(flags)))
146 goto out;
147
148 /* Try again but now we are using pfmemalloc reserves */
149 ret_pfmemalloc = true;
150 obj = kmalloc_node_track_caller(size, flags, node);
151
152out:
153 if (pfmemalloc)
154 *pfmemalloc = ret_pfmemalloc;
155
156 return obj;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Allocate a new skbuff. We do this ourselves so we can fill in a few
160 * 'private' fields and also do memory statistics to find all the
161 * [BEEP] leaks.
162 *
163 */
164
165/**
David S. Millerd179cd12005-08-17 14:57:30 -0700166 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * @size: size to allocate
168 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700169 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
170 * instead of head cache and allocate a cloned (child) skb.
171 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
172 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800173 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000176 * tail room of at least size bytes. The object has a reference count
177 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *
179 * Buffers may only be allocated from interrupts using a @gfp_mask of
180 * %GFP_ATOMIC.
181 */
Al Virodd0fc662005-10-07 07:46:04 +0100182struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700183 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Christoph Lametere18b8902006-12-06 20:33:20 -0800185 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800186 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct sk_buff *skb;
188 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700189 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mel Gormanc93bdd02012-07-31 16:44:19 -0700191 cache = (flags & SKB_ALLOC_FCLONE)
192 ? skbuff_fclone_cache : skbuff_head_cache;
193
194 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
195 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800198 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!skb)
200 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700201 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000203 /* We do our best to align skb_shared_info on a separate cache
204 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
205 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
206 * Both skb->head and skb_shared_info are cache line aligned.
207 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000208 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000209 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700210 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!data)
212 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000213 /* kmalloc(size) might give us more room than requested.
214 * Put skb_shared_info exactly at the end of allocated zone,
215 * to allow max possible filling before reallocation.
216 */
217 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700218 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300220 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700221 * Only clear those fields we need to clear, not those that we will
222 * actually initialise below. Hence, don't put any more fields after
223 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300224 */
225 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000226 /* Account for allocated memory : skb + skb->head */
227 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700228 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300229 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 skb->head = data;
231 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700232 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700233 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800234 skb->mac_header = (typeof(skb->mac_header))~0U;
235 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000236
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800237 /* make sure we initialize shinfo sequentially */
238 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700239 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800240 atomic_set(&shinfo->dataref, 1);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800241
Mel Gormanc93bdd02012-07-31 16:44:19 -0700242 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700243 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700245 fclones = container_of(skb, struct sk_buff_fclones, skb1);
246
David S. Millerd179cd12005-08-17 14:57:30 -0700247 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300248 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700249
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800250 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700251 }
Aleksandr Nogikh6370cc32020-10-29 17:36:19 +0000252
253 skb_set_kcov_handle(skb, kcov_common_handle());
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255out:
256 return skb;
257nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800258 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 skb = NULL;
260 goto out;
261}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800262EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200264/* Caller must provide SKB that is memset cleared */
265static struct sk_buff *__build_skb_around(struct sk_buff *skb,
266 void *data, unsigned int frag_size)
267{
268 struct skb_shared_info *shinfo;
269 unsigned int size = frag_size ? : ksize(data);
270
271 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
272
273 /* Assumes caller memset cleared SKB */
274 skb->truesize = SKB_TRUESIZE(size);
275 refcount_set(&skb->users, 1);
276 skb->head = data;
277 skb->data = data;
278 skb_reset_tail_pointer(skb);
279 skb->end = skb->tail + size;
280 skb->mac_header = (typeof(skb->mac_header))~0U;
281 skb->transport_header = (typeof(skb->transport_header))~0U;
282
283 /* make sure we initialize shinfo sequentially */
284 shinfo = skb_shinfo(skb);
285 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
286 atomic_set(&shinfo->dataref, 1);
287
Aleksandr Nogikh6370cc32020-10-29 17:36:19 +0000288 skb_set_kcov_handle(skb, kcov_common_handle());
289
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200290 return skb;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700294 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000295 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700296 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000297 *
298 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100299 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700300 * @frag_size is 0, otherwise data should come from the page allocator
301 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000302 * The return is the new skb buffer.
303 * On a failure the return is %NULL, and @data is not freed.
304 * Notes :
305 * Before IO, driver allocates only data buffer where NIC put incoming frame
306 * Driver should add room at head (NET_SKB_PAD) and
307 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
308 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
309 * before giving packet to stack.
310 * RX rings only contains data buffers, not full skbs.
311 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700312struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313{
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314 struct sk_buff *skb;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000315
316 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200317 if (unlikely(!skb))
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000318 return NULL;
319
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000320 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000321
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200322 return __build_skb_around(skb, data, frag_size);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000323}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700324
325/* build_skb() is wrapper over __build_skb(), that specifically
326 * takes care of skb->head and skb->pfmemalloc
327 * This means that if @frag_size is not zero, then @data must be backed
328 * by a page fragment, not kmalloc() or vmalloc()
329 */
330struct sk_buff *build_skb(void *data, unsigned int frag_size)
331{
332 struct sk_buff *skb = __build_skb(data, frag_size);
333
334 if (skb && frag_size) {
335 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700336 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700337 skb->pfmemalloc = 1;
338 }
339 return skb;
340}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000341EXPORT_SYMBOL(build_skb);
342
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200343/**
344 * build_skb_around - build a network buffer around provided skb
345 * @skb: sk_buff provide by caller, must be memset cleared
346 * @data: data buffer provided by caller
347 * @frag_size: size of data, or 0 if head was kmalloced
348 */
349struct sk_buff *build_skb_around(struct sk_buff *skb,
350 void *data, unsigned int frag_size)
351{
352 if (unlikely(!skb))
353 return NULL;
354
355 skb = __build_skb_around(skb, data, frag_size);
356
357 if (skb && frag_size) {
358 skb->head_frag = 1;
359 if (page_is_pfmemalloc(virt_to_head_page(data)))
360 skb->pfmemalloc = 1;
361 }
362 return skb;
363}
364EXPORT_SYMBOL(build_skb_around);
365
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100366#define NAPI_SKB_CACHE_SIZE 64
367
368struct napi_alloc_cache {
369 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300370 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100371 void *skb_cache[NAPI_SKB_CACHE_SIZE];
372};
373
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700374static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100375static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800376
Alexander Duyckffde7322014-12-09 19:40:42 -0800377static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
378{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100379 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700380
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800381 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800382}
383
384void *napi_alloc_frag(unsigned int fragsz)
385{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800386 fragsz = SKB_DATA_ALIGN(fragsz);
387
Mel Gorman453f85d2017-11-15 17:38:03 -0800388 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
Alexander Duyckffde7322014-12-09 19:40:42 -0800389}
390EXPORT_SYMBOL(napi_alloc_frag);
391
Eric Dumazet6f532612012-05-18 05:12:12 +0000392/**
Sebastian Andrzej Siewior7ba7aea2019-06-07 21:20:34 +0200393 * netdev_alloc_frag - allocate a page fragment
394 * @fragsz: fragment size
395 *
396 * Allocates a frag from a page for receive buffer.
397 * Uses GFP_ATOMIC allocations.
398 */
399void *netdev_alloc_frag(unsigned int fragsz)
400{
401 struct page_frag_cache *nc;
402 void *data;
403
404 fragsz = SKB_DATA_ALIGN(fragsz);
405 if (in_irq() || irqs_disabled()) {
406 nc = this_cpu_ptr(&netdev_alloc_cache);
407 data = page_frag_alloc(nc, fragsz, GFP_ATOMIC);
408 } else {
409 local_bh_disable();
410 data = __napi_alloc_frag(fragsz, GFP_ATOMIC);
411 local_bh_enable();
412 }
413 return data;
414}
415EXPORT_SYMBOL(netdev_alloc_frag);
416
417/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800418 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
419 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900420 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800421 * @gfp_mask: get_free_pages mask, passed to alloc_skb
422 *
423 * Allocate a new &sk_buff and assign it a usage count of one. The
424 * buffer has NET_SKB_PAD headroom built in. Users should allocate
425 * the headroom they think they need without accounting for the
426 * built in space. The built in space is used for optimisations.
427 *
428 * %NULL is returned if there is no free memory.
429 */
Alexander Duyck94519802015-05-06 21:11:40 -0700430struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
431 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800432{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700433 struct page_frag_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800434 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700435 bool pfmemalloc;
436 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800437
Alexander Duyck94519802015-05-06 21:11:40 -0700438 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800439
Alexander Duyck94519802015-05-06 21:11:40 -0700440 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800441 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700442 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
443 if (!skb)
444 goto skb_fail;
445 goto skb_success;
446 }
Alexander Duyck94519802015-05-06 21:11:40 -0700447
448 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
449 len = SKB_DATA_ALIGN(len);
450
451 if (sk_memalloc_socks())
452 gfp_mask |= __GFP_MEMALLOC;
453
Sebastian Andrzej Siewior92dcabd2019-06-07 21:20:35 +0200454 if (in_irq() || irqs_disabled()) {
455 nc = this_cpu_ptr(&netdev_alloc_cache);
456 data = page_frag_alloc(nc, len, gfp_mask);
457 pfmemalloc = nc->pfmemalloc;
458 } else {
459 local_bh_disable();
460 nc = this_cpu_ptr(&napi_alloc_cache.page);
461 data = page_frag_alloc(nc, len, gfp_mask);
462 pfmemalloc = nc->pfmemalloc;
463 local_bh_enable();
464 }
Alexander Duyck94519802015-05-06 21:11:40 -0700465
466 if (unlikely(!data))
467 return NULL;
468
469 skb = __build_skb(data, len);
470 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700471 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700472 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700473 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800474
Alexander Duyck94519802015-05-06 21:11:40 -0700475 if (pfmemalloc)
476 skb->pfmemalloc = 1;
477 skb->head_frag = 1;
478
Alexander Duycka080e7b2015-05-13 13:34:13 -0700479skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700480 skb_reserve(skb, NET_SKB_PAD);
481 skb->dev = dev;
482
Alexander Duycka080e7b2015-05-13 13:34:13 -0700483skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700484 return skb;
485}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800486EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alexander Duyckfd11a832014-12-09 19:40:49 -0800488/**
489 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
490 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900491 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800492 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
493 *
494 * Allocate a new sk_buff for use in NAPI receive. This buffer will
495 * attempt to allocate the head from a special reserved region used
496 * only for NAPI Rx allocation. By doing this we can save several
497 * CPU cycles by avoiding having to disable and re-enable IRQs.
498 *
499 * %NULL is returned if there is no free memory.
500 */
Alexander Duyck94519802015-05-06 21:11:40 -0700501struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
502 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800503{
Eric Dumazet3226b152021-01-13 08:18:19 -0800504 struct napi_alloc_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800505 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700506 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800507
Alexander Duyck94519802015-05-06 21:11:40 -0700508 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800509
Eric Dumazet3226b152021-01-13 08:18:19 -0800510 /* If requested length is either too small or too big,
511 * we use kmalloc() for skb->head allocation.
512 */
513 if (len <= SKB_WITH_OVERHEAD(1024) ||
514 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800515 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700516 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
517 if (!skb)
518 goto skb_fail;
519 goto skb_success;
520 }
Alexander Duyck94519802015-05-06 21:11:40 -0700521
Eric Dumazet3226b152021-01-13 08:18:19 -0800522 nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700523 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
524 len = SKB_DATA_ALIGN(len);
525
526 if (sk_memalloc_socks())
527 gfp_mask |= __GFP_MEMALLOC;
528
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800529 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700530 if (unlikely(!data))
531 return NULL;
532
533 skb = __build_skb(data, len);
534 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700535 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700536 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800537 }
538
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100539 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700540 skb->pfmemalloc = 1;
541 skb->head_frag = 1;
542
Alexander Duycka080e7b2015-05-13 13:34:13 -0700543skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700544 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
545 skb->dev = napi->dev;
546
Alexander Duycka080e7b2015-05-13 13:34:13 -0700547skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800548 return skb;
549}
550EXPORT_SYMBOL(__napi_alloc_skb);
551
Peter Zijlstra654bed12008-10-07 14:22:33 -0700552void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000553 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700554{
555 skb_fill_page_desc(skb, i, page, off, size);
556 skb->len += size;
557 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000558 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700559}
560EXPORT_SYMBOL(skb_add_rx_frag);
561
Jason Wangf8e617e2013-11-01 14:07:47 +0800562void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
563 unsigned int truesize)
564{
565 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
566
567 skb_frag_size_add(frag, size);
568 skb->len += size;
569 skb->data_len += size;
570 skb->truesize += truesize;
571}
572EXPORT_SYMBOL(skb_coalesce_rx_frag);
573
Herbert Xu27b437c2006-07-13 19:26:39 -0700574static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700576 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700577 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Herbert Xu27b437c2006-07-13 19:26:39 -0700580static inline void skb_drop_fraglist(struct sk_buff *skb)
581{
582 skb_drop_list(&skb_shinfo(skb)->frag_list);
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static void skb_clone_fraglist(struct sk_buff *skb)
586{
587 struct sk_buff *list;
588
David S. Millerfbb398a2009-06-09 00:18:59 -0700589 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 skb_get(list);
591}
592
Eric Dumazetd3836f22012-04-27 00:33:38 +0000593static void skb_free_head(struct sk_buff *skb)
594{
Alexander Duyck181edb22015-05-06 21:12:03 -0700595 unsigned char *head = skb->head;
596
Eric Dumazetd3836f22012-04-27 00:33:38 +0000597 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700598 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000599 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700600 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000601}
602
Adrian Bunk5bba1712006-06-29 13:02:35 -0700603static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Eric Dumazetff04a772014-09-23 18:39:30 -0700605 struct skb_shared_info *shinfo = skb_shinfo(skb);
606 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Eric Dumazetff04a772014-09-23 18:39:30 -0700608 if (skb->cloned &&
609 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
610 &shinfo->dataref))
611 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000612
Jonathan Lemon70c43162021-01-06 14:18:36 -0800613 skb_zcopy_clear(skb, true);
614
Eric Dumazetff04a772014-09-23 18:39:30 -0700615 for (i = 0; i < shinfo->nr_frags; i++)
616 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000617
Eric Dumazetff04a772014-09-23 18:39:30 -0700618 if (shinfo->frag_list)
619 kfree_skb_list(shinfo->frag_list);
620
621 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * Free an skbuff by memory without cleaning the state.
626 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800627static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700629 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700630
David S. Millerd179cd12005-08-17 14:57:30 -0700631 switch (skb->fclone) {
632 case SKB_FCLONE_UNAVAILABLE:
633 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800634 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700635
636 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700637 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800638
639 /* We usually free the clone (TX completion) before original skb
640 * This test would have no chance to be true for the clone,
641 * while here, branch prediction will be good.
642 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300643 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800644 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700645 break;
646
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800647 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700648 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700649 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700650 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300651 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800652 return;
653fastpath:
654 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Paolo Abeni0a463c72017-06-12 11:23:42 +0200657void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Eric Dumazetadf30902009-06-02 05:19:30 +0000659 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700660 if (skb->destructor) {
661 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 skb->destructor(skb);
663 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000664#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100665 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100666#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100667 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700668}
669
670/* Free everything but the sk_buff shell. */
671static void skb_release_all(struct sk_buff *skb)
672{
673 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200674 if (likely(skb->head))
675 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Herbert Xu2d4baff2007-11-26 23:11:19 +0800678/**
679 * __kfree_skb - private function
680 * @skb: buffer
681 *
682 * Free an sk_buff. Release anything attached to the buffer.
683 * Clean the state. This is an internal helper function. Users should
684 * always call kfree_skb
685 */
686
687void __kfree_skb(struct sk_buff *skb)
688{
689 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree_skbmem(skb);
691}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800692EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800695 * kfree_skb - free an sk_buff
696 * @skb: buffer to free
697 *
698 * Drop a reference to the buffer and free it if the usage count has
699 * hit zero.
700 */
701void kfree_skb(struct sk_buff *skb)
702{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200703 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800704 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200705
Neil Hormanead2ceb2009-03-11 09:49:55 +0000706 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800707 __kfree_skb(skb);
708}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800709EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800710
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700711void kfree_skb_list(struct sk_buff *segs)
712{
713 while (segs) {
714 struct sk_buff *next = segs->next;
715
716 kfree_skb(segs);
717 segs = next;
718 }
719}
720EXPORT_SYMBOL(kfree_skb_list);
721
Willem de Bruijn64131392019-07-07 05:51:55 -0400722/* Dump skb information and contents.
723 *
724 * Must only be called from net_ratelimit()-ed paths.
725 *
Vladimir Oltean302af7c2020-10-05 17:48:38 +0300726 * Dumps whole packets if full_pkt, only headers otherwise.
Willem de Bruijn64131392019-07-07 05:51:55 -0400727 */
728void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
729{
Willem de Bruijn64131392019-07-07 05:51:55 -0400730 struct skb_shared_info *sh = skb_shinfo(skb);
731 struct net_device *dev = skb->dev;
732 struct sock *sk = skb->sk;
733 struct sk_buff *list_skb;
734 bool has_mac, has_trans;
735 int headroom, tailroom;
736 int i, len, seg_len;
737
738 if (full_pkt)
Willem de Bruijn64131392019-07-07 05:51:55 -0400739 len = skb->len;
740 else
741 len = min_t(int, skb->len, MAX_HEADER + 128);
742
743 headroom = skb_headroom(skb);
744 tailroom = skb_tailroom(skb);
745
746 has_mac = skb_mac_header_was_set(skb);
747 has_trans = skb_transport_header_was_set(skb);
748
749 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
750 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
751 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
752 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
753 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
754 level, skb->len, headroom, skb_headlen(skb), tailroom,
755 has_mac ? skb->mac_header : -1,
756 has_mac ? skb_mac_header_len(skb) : -1,
757 skb->network_header,
758 has_trans ? skb_network_header_len(skb) : -1,
759 has_trans ? skb->transport_header : -1,
760 sh->tx_flags, sh->nr_frags,
761 sh->gso_size, sh->gso_type, sh->gso_segs,
762 skb->csum, skb->ip_summed, skb->csum_complete_sw,
763 skb->csum_valid, skb->csum_level,
764 skb->hash, skb->sw_hash, skb->l4_hash,
765 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
766
767 if (dev)
768 printk("%sdev name=%s feat=0x%pNF\n",
769 level, dev->name, &dev->features);
770 if (sk)
Qian Caidb8051f2019-07-16 11:43:05 -0400771 printk("%ssk family=%hu type=%u proto=%u\n",
Willem de Bruijn64131392019-07-07 05:51:55 -0400772 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
773
774 if (full_pkt && headroom)
775 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
776 16, 1, skb->head, headroom, false);
777
778 seg_len = min_t(int, skb_headlen(skb), len);
779 if (seg_len)
780 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
781 16, 1, skb->data, seg_len, false);
782 len -= seg_len;
783
784 if (full_pkt && tailroom)
785 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
786 16, 1, skb_tail_pointer(skb), tailroom, false);
787
788 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
789 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
790 u32 p_off, p_len, copied;
791 struct page *p;
792 u8 *vaddr;
793
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700794 skb_frag_foreach_page(frag, skb_frag_off(frag),
Willem de Bruijn64131392019-07-07 05:51:55 -0400795 skb_frag_size(frag), p, p_off, p_len,
796 copied) {
797 seg_len = min_t(int, p_len, len);
798 vaddr = kmap_atomic(p);
799 print_hex_dump(level, "skb frag: ",
800 DUMP_PREFIX_OFFSET,
801 16, 1, vaddr + p_off, seg_len, false);
802 kunmap_atomic(vaddr);
803 len -= seg_len;
804 if (!len)
805 break;
806 }
807 }
808
809 if (full_pkt && skb_has_frag_list(skb)) {
810 printk("skb fraglist:\n");
811 skb_walk_frags(skb, list_skb)
812 skb_dump(level, list_skb, true);
813 }
814}
815EXPORT_SYMBOL(skb_dump);
816
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700817/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000818 * skb_tx_error - report an sk_buff xmit error
819 * @skb: buffer that triggered an error
820 *
821 * Report xmit error if a device callback is tracking this skb.
822 * skb must be freed afterwards.
823 */
824void skb_tx_error(struct sk_buff *skb)
825{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400826 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000827}
828EXPORT_SYMBOL(skb_tx_error);
829
Herbert Xube769db2020-08-22 08:23:29 +1000830#ifdef CONFIG_TRACEPOINTS
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000831/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000832 * consume_skb - free an skbuff
833 * @skb: buffer to free
834 *
835 * Drop a ref to the buffer and free it if the usage count has hit zero
836 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
837 * is being dropped after a failure and notes that
838 */
839void consume_skb(struct sk_buff *skb)
840{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200841 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000842 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200843
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900844 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000845 __kfree_skb(skb);
846}
847EXPORT_SYMBOL(consume_skb);
Herbert Xube769db2020-08-22 08:23:29 +1000848#endif
Neil Hormanead2ceb2009-03-11 09:49:55 +0000849
Paolo Abeni0a463c72017-06-12 11:23:42 +0200850/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +0100851 * __consume_stateless_skb - free an skbuff, assuming it is stateless
Paolo Abeni0a463c72017-06-12 11:23:42 +0200852 * @skb: buffer to free
853 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200854 * Alike consume_skb(), but this variant assumes that this is the last
855 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200856 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200857void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200858{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200859 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200860 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200861 kfree_skbmem(skb);
862}
863
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100864void __kfree_skb_flush(void)
865{
866 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
867
868 /* flush skb_cache if containing objects */
869 if (nc->skb_count) {
870 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
871 nc->skb_cache);
872 nc->skb_count = 0;
873 }
874}
875
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100876static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100877{
878 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
879
880 /* drop skb->head and call any destructors for packet */
881 skb_release_all(skb);
882
883 /* record skb to CPU local list */
884 nc->skb_cache[nc->skb_count++] = skb;
885
886#ifdef CONFIG_SLUB
887 /* SLUB writes into objects when freeing */
888 prefetchw(skb);
889#endif
890
891 /* flush skb_cache if it is filled */
892 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
893 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
894 nc->skb_cache);
895 nc->skb_count = 0;
896 }
897}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100898void __kfree_skb_defer(struct sk_buff *skb)
899{
900 _kfree_skb_defer(skb);
901}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100902
903void napi_consume_skb(struct sk_buff *skb, int budget)
904{
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100905 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100906 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100907 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100908 return;
909 }
910
Yunsheng Lin6454eca2020-11-24 18:49:29 +0800911 lockdep_assert_in_softirq();
912
Paolo Abeni76088942017-06-14 11:48:48 +0200913 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100914 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200915
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100916 /* if reaching here SKB is ready to free */
917 trace_consume_skb(skb);
918
919 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700920 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100921 __kfree_skb(skb);
922 return;
923 }
924
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100925 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100926}
927EXPORT_SYMBOL(napi_consume_skb);
928
Eric Dumazetb1937222014-09-28 22:18:47 -0700929/* Make sure a field is enclosed inside headers_start/headers_end section */
930#define CHECK_SKB_FIELD(field) \
931 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
932 offsetof(struct sk_buff, headers_start)); \
933 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
934 offsetof(struct sk_buff, headers_end)); \
935
Herbert Xudec18812007-10-14 00:37:30 -0700936static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
937{
938 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700939 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700940 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700941 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000942 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100943 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -0700944 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700945
Eric Dumazetb1937222014-09-28 22:18:47 -0700946 /* Note : this field could be in headers_start/headers_end section
947 * It is not yet because we do not want to have a 16 bit hole
948 */
949 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300950
Eric Dumazetb1937222014-09-28 22:18:47 -0700951 memcpy(&new->headers_start, &old->headers_start,
952 offsetof(struct sk_buff, headers_end) -
953 offsetof(struct sk_buff, headers_start));
954 CHECK_SKB_FIELD(protocol);
955 CHECK_SKB_FIELD(csum);
956 CHECK_SKB_FIELD(hash);
957 CHECK_SKB_FIELD(priority);
958 CHECK_SKB_FIELD(skb_iif);
959 CHECK_SKB_FIELD(vlan_proto);
960 CHECK_SKB_FIELD(vlan_tci);
961 CHECK_SKB_FIELD(transport_header);
962 CHECK_SKB_FIELD(network_header);
963 CHECK_SKB_FIELD(mac_header);
964 CHECK_SKB_FIELD(inner_protocol);
965 CHECK_SKB_FIELD(inner_transport_header);
966 CHECK_SKB_FIELD(inner_network_header);
967 CHECK_SKB_FIELD(inner_mac_header);
968 CHECK_SKB_FIELD(mark);
969#ifdef CONFIG_NETWORK_SECMARK
970 CHECK_SKB_FIELD(secmark);
971#endif
Cong Wange0d10952013-08-01 11:10:25 +0800972#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700973 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300974#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800975#ifdef CONFIG_XPS
976 CHECK_SKB_FIELD(sender_cpu);
977#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700978#ifdef CONFIG_NET_SCHED
979 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700980#endif
981
Herbert Xudec18812007-10-14 00:37:30 -0700982}
983
Herbert Xu82c49a32009-05-22 22:11:37 +0000984/*
985 * You should not add any new code to this function. Add it to
986 * __copy_skb_header above instead.
987 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700988static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#define C(x) n->x = skb->x
991
992 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700994 __copy_skb_header(n, skb);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 C(len);
997 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700998 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700999 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -08001000 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -07001002 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +02001003 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 C(tail);
1006 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -08001007 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +00001008 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -08001009 C(data);
1010 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +03001011 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 atomic_inc(&(skb_shinfo(skb)->dataref));
1014 skb->cloned = 1;
1015
1016 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001017#undef C
1018}
1019
1020/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -07001021 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1022 * @first: first sk_buff of the msg
1023 */
1024struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1025{
1026 struct sk_buff *n;
1027
1028 n = alloc_skb(0, GFP_ATOMIC);
1029 if (!n)
1030 return NULL;
1031
1032 n->len = first->len;
1033 n->data_len = first->len;
1034 n->truesize = first->truesize;
1035
1036 skb_shinfo(n)->frag_list = first;
1037
1038 __copy_skb_header(n, first);
1039 n->destructor = NULL;
1040
1041 return n;
1042}
1043EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1044
1045/**
Herbert Xue0053ec2007-10-14 00:37:52 -07001046 * skb_morph - morph one skb into another
1047 * @dst: the skb to receive the contents
1048 * @src: the skb to supply the contents
1049 *
1050 * This is identical to skb_clone except that the target skb is
1051 * supplied by the user.
1052 *
1053 * The target skb is returned upon exit.
1054 */
1055struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1056{
Herbert Xu2d4baff2007-11-26 23:11:19 +08001057 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -07001058 return __skb_clone(dst, src);
1059}
1060EXPORT_SYMBOL_GPL(skb_morph);
1061
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001062int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001063{
1064 unsigned long max_pg, num_pg, new_pg, old_pg;
1065 struct user_struct *user;
1066
1067 if (capable(CAP_IPC_LOCK) || !size)
1068 return 0;
1069
1070 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1071 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1072 user = mmp->user ? : current_user();
1073
1074 do {
1075 old_pg = atomic_long_read(&user->locked_vm);
1076 new_pg = old_pg + num_pg;
1077 if (new_pg > max_pg)
1078 return -ENOBUFS;
1079 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1080 old_pg);
1081
1082 if (!mmp->user) {
1083 mmp->user = get_uid(user);
1084 mmp->num_pg = num_pg;
1085 } else {
1086 mmp->num_pg += num_pg;
1087 }
1088
1089 return 0;
1090}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001091EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001092
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001093void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001094{
1095 if (mmp->user) {
1096 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1097 free_uid(mmp->user);
1098 }
1099}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001100EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001101
Jonathan Lemon8c793822021-01-06 14:18:37 -08001102struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
Willem de Bruijn52267792017-08-03 16:29:39 -04001103{
1104 struct ubuf_info *uarg;
1105 struct sk_buff *skb;
1106
1107 WARN_ON_ONCE(!in_task());
1108
1109 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1110 if (!skb)
1111 return NULL;
1112
1113 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1114 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001115 uarg->mmp.user = NULL;
1116
1117 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1118 kfree_skb(skb);
1119 return NULL;
1120 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001121
Jonathan Lemon8c793822021-01-06 14:18:37 -08001122 uarg->callback = msg_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001123 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1124 uarg->len = 1;
1125 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001126 uarg->zerocopy = 1;
Jonathan Lemon04c2d332021-01-06 14:18:39 -08001127 uarg->flags = SKBFL_ZEROCOPY_FRAG;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001128 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001129 sock_hold(sk);
1130
1131 return uarg;
1132}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001133EXPORT_SYMBOL_GPL(msg_zerocopy_alloc);
Willem de Bruijn52267792017-08-03 16:29:39 -04001134
1135static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1136{
1137 return container_of((void *)uarg, struct sk_buff, cb);
1138}
1139
Jonathan Lemon8c793822021-01-06 14:18:37 -08001140struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1141 struct ubuf_info *uarg)
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001142{
1143 if (uarg) {
1144 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1145 u32 bytelen, next;
1146
1147 /* realloc only when socket is locked (TCP, UDP cork),
1148 * so uarg->len and sk_zckey access is serialized
1149 */
1150 if (!sock_owned_by_user(sk)) {
1151 WARN_ON_ONCE(1);
1152 return NULL;
1153 }
1154
1155 bytelen = uarg->bytelen + size;
1156 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1157 /* TCP can create new skb to attach new uarg */
1158 if (sk->sk_type == SOCK_STREAM)
1159 goto new_alloc;
1160 return NULL;
1161 }
1162
1163 next = (u32)atomic_read(&sk->sk_zckey);
1164 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001165 if (mm_account_pinned_pages(&uarg->mmp, size))
1166 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001167 uarg->len++;
1168 uarg->bytelen = bytelen;
1169 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001170
1171 /* no extra ref when appending to datagram (MSG_MORE) */
1172 if (sk->sk_type == SOCK_STREAM)
Jonathan Lemon8e044912021-01-06 14:18:41 -08001173 net_zcopy_get(uarg);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001174
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001175 return uarg;
1176 }
1177 }
1178
1179new_alloc:
Jonathan Lemon8c793822021-01-06 14:18:37 -08001180 return msg_zerocopy_alloc(sk, size);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001181}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001182EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001183
1184static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1185{
1186 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1187 u32 old_lo, old_hi;
1188 u64 sum_len;
1189
1190 old_lo = serr->ee.ee_info;
1191 old_hi = serr->ee.ee_data;
1192 sum_len = old_hi - old_lo + 1ULL + len;
1193
1194 if (sum_len >= (1ULL << 32))
1195 return false;
1196
1197 if (lo != old_hi + 1)
1198 return false;
1199
1200 serr->ee.ee_data += len;
1201 return true;
1202}
1203
Jonathan Lemon8c793822021-01-06 14:18:37 -08001204static void __msg_zerocopy_callback(struct ubuf_info *uarg)
Willem de Bruijn52267792017-08-03 16:29:39 -04001205{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001206 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001207 struct sock_exterr_skb *serr;
1208 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001209 struct sk_buff_head *q;
1210 unsigned long flags;
1211 u32 lo, hi;
1212 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001213
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001214 mm_unaccount_pinned_pages(&uarg->mmp);
1215
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001216 /* if !len, there was only 1 call, and it was aborted
1217 * so do not queue a completion notification
1218 */
1219 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001220 goto release;
1221
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001222 len = uarg->len;
1223 lo = uarg->id;
1224 hi = uarg->id + len - 1;
1225
Willem de Bruijn52267792017-08-03 16:29:39 -04001226 serr = SKB_EXT_ERR(skb);
1227 memset(serr, 0, sizeof(*serr));
1228 serr->ee.ee_errno = 0;
1229 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001230 serr->ee.ee_data = hi;
1231 serr->ee.ee_info = lo;
Jonathan Lemon75518852021-01-06 14:18:31 -08001232 if (!uarg->zerocopy)
Willem de Bruijn52267792017-08-03 16:29:39 -04001233 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1234
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001235 q = &sk->sk_error_queue;
1236 spin_lock_irqsave(&q->lock, flags);
1237 tail = skb_peek_tail(q);
1238 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1239 !skb_zerocopy_notify_extend(tail, lo, len)) {
1240 __skb_queue_tail(q, skb);
1241 skb = NULL;
1242 }
1243 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001244
1245 sk->sk_error_report(sk);
1246
1247release:
1248 consume_skb(skb);
1249 sock_put(sk);
1250}
Jonathan Lemon75518852021-01-06 14:18:31 -08001251
Jonathan Lemon8c793822021-01-06 14:18:37 -08001252void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
1253 bool success)
Jonathan Lemon75518852021-01-06 14:18:31 -08001254{
1255 uarg->zerocopy = uarg->zerocopy & success;
1256
1257 if (refcount_dec_and_test(&uarg->refcnt))
Jonathan Lemon8c793822021-01-06 14:18:37 -08001258 __msg_zerocopy_callback(uarg);
Jonathan Lemon75518852021-01-06 14:18:31 -08001259}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001260EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
Willem de Bruijn52267792017-08-03 16:29:39 -04001261
Jonathan Lemon8c793822021-01-06 14:18:37 -08001262void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001263{
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001264 struct sock *sk = skb_from_uarg(uarg)->sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001265
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001266 atomic_dec(&sk->sk_zckey);
1267 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001268
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001269 if (have_uref)
Jonathan Lemon8c793822021-01-06 14:18:37 -08001270 msg_zerocopy_callback(NULL, uarg, true);
Willem de Bruijn52267792017-08-03 16:29:39 -04001271}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001272EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
Willem de Bruijn52267792017-08-03 16:29:39 -04001273
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001274int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1275{
1276 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1277}
1278EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1279
Willem de Bruijn52267792017-08-03 16:29:39 -04001280int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1281 struct msghdr *msg, int len,
1282 struct ubuf_info *uarg)
1283{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001284 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001285 struct iov_iter orig_iter = msg->msg_iter;
1286 int err, orig_len = skb->len;
1287
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001288 /* An skb can only point to one uarg. This edge case happens when
1289 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1290 */
1291 if (orig_uarg && uarg != orig_uarg)
1292 return -EEXIST;
1293
Willem de Bruijn52267792017-08-03 16:29:39 -04001294 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1295 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001296 struct sock *save_sk = skb->sk;
1297
Willem de Bruijn52267792017-08-03 16:29:39 -04001298 /* Streams do not free skb on error. Reset to prev state. */
1299 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001300 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001301 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001302 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001303 return err;
1304 }
1305
Willem de Bruijn52900d22018-11-30 15:32:40 -05001306 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001307 return skb->len - orig_len;
1308}
1309EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1310
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001311static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001312 gfp_t gfp_mask)
1313{
1314 if (skb_zcopy(orig)) {
1315 if (skb_zcopy(nskb)) {
1316 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1317 if (!gfp_mask) {
1318 WARN_ON_ONCE(1);
1319 return -ENOMEM;
1320 }
1321 if (skb_uarg(nskb) == skb_uarg(orig))
1322 return 0;
1323 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1324 return -EIO;
1325 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001326 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001327 }
1328 return 0;
1329}
1330
Ben Hutchings2c530402012-07-10 10:55:09 +00001331/**
1332 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001333 * @skb: the skb to modify
1334 * @gfp_mask: allocation priority
1335 *
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08001336 * This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001337 * It will copy all frags into kernel and drop the reference
1338 * to userspace pages.
1339 *
1340 * If this function is called from an interrupt gfp_mask() must be
1341 * %GFP_ATOMIC.
1342 *
1343 * Returns 0 on success or a negative error code on failure
1344 * to allocate kernel memory to copy to.
1345 */
1346int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001347{
Shirley Maa6686f22011-07-06 12:22:12 +00001348 int num_frags = skb_shinfo(skb)->nr_frags;
1349 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001350 int i, new_frags;
1351 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001352
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001353 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1354 return -EINVAL;
1355
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001356 if (!num_frags)
1357 goto release;
1358
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001359 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1360 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001361 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001362 if (!page) {
1363 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001364 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001365 put_page(head);
1366 head = next;
1367 }
1368 return -ENOMEM;
1369 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001370 set_page_private(page, (unsigned long)head);
1371 head = page;
1372 }
1373
1374 page = head;
1375 d_off = 0;
1376 for (i = 0; i < num_frags; i++) {
1377 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1378 u32 p_off, p_len, copied;
1379 struct page *p;
1380 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001381
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001382 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
Willem de Bruijnc613c202017-07-31 08:15:47 -04001383 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001384 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001385 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001386
1387 while (done < p_len) {
1388 if (d_off == PAGE_SIZE) {
1389 d_off = 0;
1390 page = (struct page *)page_private(page);
1391 }
1392 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1393 memcpy(page_address(page) + d_off,
1394 vaddr + p_off + done, copy);
1395 done += copy;
1396 d_off += copy;
1397 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001398 kunmap_atomic(vaddr);
1399 }
Shirley Maa6686f22011-07-06 12:22:12 +00001400 }
1401
1402 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001403 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001404 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001405
Shirley Maa6686f22011-07-06 12:22:12 +00001406 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001407 for (i = 0; i < new_frags - 1; i++) {
1408 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001409 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001410 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001411 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1412 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001413
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001414release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001415 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001416 return 0;
1417}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001418EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001419
Herbert Xue0053ec2007-10-14 00:37:52 -07001420/**
1421 * skb_clone - duplicate an sk_buff
1422 * @skb: buffer to clone
1423 * @gfp_mask: allocation priority
1424 *
1425 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1426 * copies share the same packet data but not structure. The new
1427 * buffer has a reference count of 1. If the allocation fails the
1428 * function returns %NULL otherwise the new buffer is returned.
1429 *
1430 * If this function is called from an interrupt gfp_mask() must be
1431 * %GFP_ATOMIC.
1432 */
1433
1434struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1435{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001436 struct sk_buff_fclones *fclones = container_of(skb,
1437 struct sk_buff_fclones,
1438 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001439 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001440
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001441 if (skb_orphan_frags(skb, gfp_mask))
1442 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001443
Herbert Xue0053ec2007-10-14 00:37:52 -07001444 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001445 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001446 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001447 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001448 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001449 if (skb_pfmemalloc(skb))
1450 gfp_mask |= __GFP_MEMALLOC;
1451
Herbert Xue0053ec2007-10-14 00:37:52 -07001452 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1453 if (!n)
1454 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001455
Herbert Xue0053ec2007-10-14 00:37:52 -07001456 n->fclone = SKB_FCLONE_UNAVAILABLE;
1457 }
1458
1459 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001461EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001463void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001464{
Eric Dumazet030737b2013-10-19 11:42:54 -07001465 /* Only adjust this if it actually is csum_start rather than csum */
1466 if (skb->ip_summed == CHECKSUM_PARTIAL)
1467 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001468 /* {transport,network,mac}_header and tail are relative to skb->head */
1469 skb->transport_header += off;
1470 skb->network_header += off;
1471 if (skb_mac_header_was_set(skb))
1472 skb->mac_header += off;
1473 skb->inner_transport_header += off;
1474 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001475 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001476}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001477EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001478
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001479void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Herbert Xudec18812007-10-14 00:37:30 -07001481 __copy_skb_header(new, old);
1482
Herbert Xu79671682006-06-22 02:40:14 -07001483 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1484 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1485 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001487EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Mel Gormanc93bdd02012-07-31 16:44:19 -07001489static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1490{
1491 if (skb_pfmemalloc(skb))
1492 return SKB_ALLOC_RX;
1493 return 0;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/**
1497 * skb_copy - create private copy of an sk_buff
1498 * @skb: buffer to copy
1499 * @gfp_mask: allocation priority
1500 *
1501 * Make a copy of both an &sk_buff and its data. This is used when the
1502 * caller wishes to modify the data and needs a private copy of the
1503 * data to alter. Returns %NULL on failure or the pointer to the buffer
1504 * on success. The returned buffer has a reference count of 1.
1505 *
1506 * As by-product this function converts non-linear &sk_buff to linear
1507 * one, so that &sk_buff becomes completely private and caller is allowed
1508 * to modify all the data of returned buffer. This means that this
1509 * function is not recommended for use in circumstances when only
1510 * header is going to be modified. Use pskb_copy() instead.
1511 */
1512
Al Virodd0fc662005-10-07 07:46:04 +01001513struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001515 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001516 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001517 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1518 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!n)
1521 return NULL;
1522
1523 /* Set the data pointer */
1524 skb_reserve(n, headerlen);
1525 /* Set the tail pointer and length */
1526 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Tim Hansen9f77fad2017-10-09 11:37:59 -04001528 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001530 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return n;
1532}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001533EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001536 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001538 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001540 * @fclone: if true allocate the copy of the skb from the fclone
1541 * cache instead of the head cache; it is recommended to set this
1542 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 *
1544 * Make a copy of both an &sk_buff and part of its data, located
1545 * in header. Fragmented data remain shared. This is used when
1546 * the caller wishes to modify only header of &sk_buff and needs
1547 * private copy of the header to alter. Returns %NULL on failure
1548 * or the pointer to the buffer on success.
1549 * The returned buffer has a reference count of 1.
1550 */
1551
Octavian Purdilabad93e92014-06-12 01:36:26 +03001552struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1553 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Eric Dumazet117632e2011-12-03 21:39:53 +00001555 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001556 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1557 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!n)
1560 goto out;
1561
1562 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001563 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Set the tail pointer and length */
1565 skb_put(n, skb_headlen(skb));
1566 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001567 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Herbert Xu25f484a2006-11-07 14:57:15 -08001569 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 n->data_len = skb->data_len;
1571 n->len = skb->len;
1572
1573 if (skb_shinfo(skb)->nr_frags) {
1574 int i;
1575
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001576 if (skb_orphan_frags(skb, gfp_mask) ||
1577 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001578 kfree_skb(n);
1579 n = NULL;
1580 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1583 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001584 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586 skb_shinfo(n)->nr_frags = i;
1587 }
1588
David S. Miller21dc3302010-08-23 00:13:46 -07001589 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1591 skb_clone_fraglist(n);
1592 }
1593
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001594 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595out:
1596 return n;
1597}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001598EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600/**
1601 * pskb_expand_head - reallocate header of &sk_buff
1602 * @skb: buffer to reallocate
1603 * @nhead: room to add at head
1604 * @ntail: room to add at tail
1605 * @gfp_mask: allocation priority
1606 *
Mathias Krausebc323832013-11-07 14:18:26 +01001607 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1608 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 * reference count of 1. Returns zero in the case of success or error,
1610 * if expansion failed. In the last case, &sk_buff is not changed.
1611 *
1612 * All the pointers pointing into skb header may change and must be
1613 * reloaded after call to this function.
1614 */
1615
Victor Fusco86a76ca2005-07-08 14:57:47 -07001616int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001617 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Eric Dumazet158f3232017-01-27 07:11:27 -08001619 int i, osize = skb_end_offset(skb);
1620 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001622 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Herbert Xu4edd87a2008-10-01 07:09:38 -07001624 BUG_ON(nhead < 0);
1625
Tim Hansen9f77fad2017-10-09 11:37:59 -04001626 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 size = SKB_DATA_ALIGN(size);
1629
Mel Gormanc93bdd02012-07-31 16:44:19 -07001630 if (skb_pfmemalloc(skb))
1631 gfp_mask |= __GFP_MEMALLOC;
1632 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1633 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (!data)
1635 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001636 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001639 * optimized for the cases when header is void.
1640 */
1641 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1642
1643 memcpy((struct skb_shared_info *)(data + size),
1644 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001645 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alexander Duyck3e245912012-05-04 14:26:51 +00001647 /*
1648 * if shinfo is shared we must drop the old head gracefully, but if it
1649 * is not we can just drop the old head and let the existing refcount
1650 * be since all we did is relocate the values
1651 */
1652 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001653 if (skb_orphan_frags(skb, gfp_mask))
1654 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001655 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001656 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001657 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001658 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric Dumazet1fd63042010-09-02 23:09:32 +00001660 if (skb_has_frag_list(skb))
1661 skb_clone_fraglist(skb);
1662
1663 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001664 } else {
1665 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 off = (data + nhead) - skb->head;
1668
1669 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001670 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001672#ifdef NET_SKBUFF_DATA_USES_OFFSET
1673 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001674 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001675#else
1676 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001677#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001678 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001679 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001681 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 skb->nohdr = 0;
1683 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001684
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001685 skb_metadata_clear(skb);
1686
Eric Dumazet158f3232017-01-27 07:11:27 -08001687 /* It is not generally safe to change skb->truesize.
1688 * For the moment, we really care of rx path, or
1689 * when skb is orphaned (not attached to a socket).
1690 */
1691 if (!skb->sk || skb->destructor == sock_edemux)
1692 skb->truesize += size - osize;
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return 0;
1695
Shirley Maa6686f22011-07-06 12:22:12 +00001696nofrags:
1697 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698nodata:
1699 return -ENOMEM;
1700}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001701EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703/* Make private copy of skb with writable head and some headroom */
1704
1705struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1706{
1707 struct sk_buff *skb2;
1708 int delta = headroom - skb_headroom(skb);
1709
1710 if (delta <= 0)
1711 skb2 = pskb_copy(skb, GFP_ATOMIC);
1712 else {
1713 skb2 = skb_clone(skb, GFP_ATOMIC);
1714 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1715 GFP_ATOMIC)) {
1716 kfree_skb(skb2);
1717 skb2 = NULL;
1718 }
1719 }
1720 return skb2;
1721}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001722EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724/**
1725 * skb_copy_expand - copy and expand sk_buff
1726 * @skb: buffer to copy
1727 * @newheadroom: new free bytes at head
1728 * @newtailroom: new free bytes at tail
1729 * @gfp_mask: allocation priority
1730 *
1731 * Make a copy of both an &sk_buff and its data and while doing so
1732 * allocate additional space.
1733 *
1734 * This is used when the caller wishes to modify the data and needs a
1735 * private copy of the data to alter as well as more space for new fields.
1736 * Returns %NULL on failure or the pointer to the buffer
1737 * on success. The returned buffer has a reference count of 1.
1738 *
1739 * You must pass %GFP_ATOMIC as the allocation priority if this function
1740 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
1742struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001743 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001744 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 /*
1747 * Allocate the copy buffer
1748 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001749 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1750 gfp_mask, skb_alloc_rx_flag(skb),
1751 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001752 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int head_copy_len, head_copy_off;
1754
1755 if (!n)
1756 return NULL;
1757
1758 skb_reserve(n, newheadroom);
1759
1760 /* Set the tail pointer and length */
1761 skb_put(n, skb->len);
1762
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001763 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 head_copy_off = 0;
1765 if (newheadroom <= head_copy_len)
1766 head_copy_len = newheadroom;
1767 else
1768 head_copy_off = newheadroom - head_copy_len;
1769
1770 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001771 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1772 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001774 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Eric Dumazet030737b2013-10-19 11:42:54 -07001776 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return n;
1779}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001780EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001783 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * @skb: buffer to pad
1785 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001786 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *
1788 * Ensure that a buffer is followed by a padding area that is zero
1789 * filled. Used by network drivers which may DMA or transfer data
1790 * beyond the buffer end onto the wire.
1791 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001792 * May return error in out of memory cases. The skb is freed on error
1793 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001795
Florian Fainellicd0a1372017-08-22 15:12:14 -07001796int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Herbert Xu5b057c62006-06-23 02:06:41 -07001798 int err;
1799 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001802 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001806
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001807 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001808 if (likely(skb_cloned(skb) || ntail > 0)) {
1809 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1810 if (unlikely(err))
1811 goto free_skb;
1812 }
1813
1814 /* FIXME: The use of this function with non-linear skb's really needs
1815 * to be audited.
1816 */
1817 err = skb_linearize(skb);
1818 if (unlikely(err))
1819 goto free_skb;
1820
1821 memset(skb->data + skb->len, 0, pad);
1822 return 0;
1823
1824free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001825 if (free_on_error)
1826 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001827 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001828}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001829EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001830
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001831/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001832 * pskb_put - add data to the tail of a potentially fragmented buffer
1833 * @skb: start of the buffer to use
1834 * @tail: tail fragment of the buffer to use
1835 * @len: amount of data to add
1836 *
1837 * This function extends the used data area of the potentially
1838 * fragmented buffer. @tail must be the last fragment of @skb -- or
1839 * @skb itself. If this would exceed the total buffer size the kernel
1840 * will panic. A pointer to the first byte of the extra data is
1841 * returned.
1842 */
1843
Johannes Berg4df864c2017-06-16 14:29:21 +02001844void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001845{
1846 if (tail != skb) {
1847 skb->data_len += len;
1848 skb->len += len;
1849 }
1850 return skb_put(tail, len);
1851}
1852EXPORT_SYMBOL_GPL(pskb_put);
1853
1854/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001855 * skb_put - add data to a buffer
1856 * @skb: buffer to use
1857 * @len: amount of data to add
1858 *
1859 * This function extends the used data area of the buffer. If this would
1860 * exceed the total buffer size the kernel will panic. A pointer to the
1861 * first byte of the extra data is returned.
1862 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001863void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001864{
Johannes Berg4df864c2017-06-16 14:29:21 +02001865 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001866 SKB_LINEAR_ASSERT(skb);
1867 skb->tail += len;
1868 skb->len += len;
1869 if (unlikely(skb->tail > skb->end))
1870 skb_over_panic(skb, len, __builtin_return_address(0));
1871 return tmp;
1872}
1873EXPORT_SYMBOL(skb_put);
1874
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001875/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001876 * skb_push - add data to the start of a buffer
1877 * @skb: buffer to use
1878 * @len: amount of data to add
1879 *
1880 * This function extends the used data area of the buffer at the buffer
1881 * start. If this would exceed the total buffer headroom the kernel will
1882 * panic. A pointer to the first byte of the extra data is returned.
1883 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001884void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001885{
1886 skb->data -= len;
1887 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301888 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001889 skb_under_panic(skb, len, __builtin_return_address(0));
1890 return skb->data;
1891}
1892EXPORT_SYMBOL(skb_push);
1893
1894/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001895 * skb_pull - remove data from the start of a buffer
1896 * @skb: buffer to use
1897 * @len: amount of data to remove
1898 *
1899 * This function removes data from the start of a buffer, returning
1900 * the memory to the headroom. A pointer to the next data in the buffer
1901 * is returned. Once the data has been pulled future pushes will overwrite
1902 * the old data.
1903 */
Johannes Bergaf728682017-06-16 14:29:22 +02001904void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001905{
David S. Miller47d29642010-05-02 02:21:44 -07001906 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001907}
1908EXPORT_SYMBOL(skb_pull);
1909
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001910/**
1911 * skb_trim - remove end from a buffer
1912 * @skb: buffer to alter
1913 * @len: new length
1914 *
1915 * Cut the length of a buffer down by removing data from the tail. If
1916 * the buffer is already under the length specified it is not modified.
1917 * The skb must be linear.
1918 */
1919void skb_trim(struct sk_buff *skb, unsigned int len)
1920{
1921 if (skb->len > len)
1922 __skb_trim(skb, len);
1923}
1924EXPORT_SYMBOL(skb_trim);
1925
Herbert Xu3cc0e872006-06-09 16:13:38 -07001926/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
1928
Herbert Xu3cc0e872006-06-09 16:13:38 -07001929int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Herbert Xu27b437c2006-07-13 19:26:39 -07001931 struct sk_buff **fragp;
1932 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 int offset = skb_headlen(skb);
1934 int nfrags = skb_shinfo(skb)->nr_frags;
1935 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001936 int err;
1937
1938 if (skb_cloned(skb) &&
1939 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001942 i = 0;
1943 if (offset >= len)
1944 goto drop_pages;
1945
1946 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001947 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001948
1949 if (end < len) {
1950 offset = end;
1951 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001953
Eric Dumazet9e903e02011-10-18 21:00:24 +00001954 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001955
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001956drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001957 skb_shinfo(skb)->nr_frags = i;
1958
1959 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001960 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001961
David S. Miller21dc3302010-08-23 00:13:46 -07001962 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001963 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001964 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
Herbert Xu27b437c2006-07-13 19:26:39 -07001967 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1968 fragp = &frag->next) {
1969 int end = offset + frag->len;
1970
1971 if (skb_shared(frag)) {
1972 struct sk_buff *nfrag;
1973
1974 nfrag = skb_clone(frag, GFP_ATOMIC);
1975 if (unlikely(!nfrag))
1976 return -ENOMEM;
1977
1978 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001979 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001980 frag = nfrag;
1981 *fragp = frag;
1982 }
1983
1984 if (end < len) {
1985 offset = end;
1986 continue;
1987 }
1988
1989 if (end > len &&
1990 unlikely((err = pskb_trim(frag, len - offset))))
1991 return err;
1992
1993 if (frag->next)
1994 skb_drop_list(&frag->next);
1995 break;
1996 }
1997
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001998done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001999 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 skb->data_len -= skb->len - len;
2001 skb->len = len;
2002 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07002003 skb->len = len;
2004 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002005 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002008 if (!skb->sk || skb->destructor == sock_edemux)
2009 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002012EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Eric Dumazet88078d92018-04-18 11:43:15 -07002014/* Note : use pskb_trim_rcsum() instead of calling this directly
2015 */
2016int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2017{
2018 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2019 int delta = skb->len - len;
2020
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002021 skb->csum = csum_block_sub(skb->csum,
2022 skb_checksum(skb, len, delta, 0),
2023 len);
Vasily Averin54970a22020-12-14 22:07:39 +03002024 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2025 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2026 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2027
2028 if (offset + sizeof(__sum16) > hdlen)
2029 return -EINVAL;
Eric Dumazet88078d92018-04-18 11:43:15 -07002030 }
2031 return __pskb_trim(skb, len);
2032}
2033EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/**
2036 * __pskb_pull_tail - advance tail of skb header
2037 * @skb: buffer to reallocate
2038 * @delta: number of bytes to advance tail
2039 *
2040 * The function makes a sense only on a fragmented &sk_buff,
2041 * it expands header moving its tail forward and copying necessary
2042 * data from fragmented part.
2043 *
2044 * &sk_buff MUST have reference count of 1.
2045 *
2046 * Returns %NULL (and &sk_buff does not change) if pull failed
2047 * or value of new tail of skb in the case of success.
2048 *
2049 * All the pointers pointing into skb header may change and must be
2050 * reloaded after call to this function.
2051 */
2052
2053/* Moves tail of skb head forward, copying data from fragmented part,
2054 * when it is necessary.
2055 * 1. It may fail due to malloc failure.
2056 * 2. It may change skb pointers.
2057 *
2058 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2059 */
Johannes Bergaf728682017-06-16 14:29:22 +02002060void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 /* If skb has not enough free space at tail, get new one
2063 * plus 128 bytes for future expansions. If we have enough
2064 * room at tail, reallocate without expansion only if skb is cloned.
2065 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002066 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 if (eat > 0 || skb_cloned(skb)) {
2069 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2070 GFP_ATOMIC))
2071 return NULL;
2072 }
2073
Tim Hansen9f77fad2017-10-09 11:37:59 -04002074 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2075 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* Optimization: no fragments, no reasons to preestimate
2078 * size of pulled pages. Superb.
2079 */
David S. Miller21dc3302010-08-23 00:13:46 -07002080 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 goto pull_pages;
2082
2083 /* Estimate size of pulled pages. */
2084 eat = delta;
2085 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002086 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2087
2088 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002090 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
2093 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002094 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * but taking into account that pulling is expected to
2096 * be very rare operation, it is worth to fight against
2097 * further bloating skb head and crucify ourselves here instead.
2098 * Pure masohism, indeed. 8)8)
2099 */
2100 if (eat) {
2101 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2102 struct sk_buff *clone = NULL;
2103 struct sk_buff *insp = NULL;
2104
2105 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (list->len <= eat) {
2107 /* Eaten as whole. */
2108 eat -= list->len;
2109 list = list->next;
2110 insp = list;
2111 } else {
2112 /* Eaten partially. */
2113
2114 if (skb_shared(list)) {
2115 /* Sucks! We need to fork list. :-( */
2116 clone = skb_clone(list, GFP_ATOMIC);
2117 if (!clone)
2118 return NULL;
2119 insp = list->next;
2120 list = clone;
2121 } else {
2122 /* This may be pulled without
2123 * problems. */
2124 insp = list;
2125 }
2126 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002127 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return NULL;
2129 }
2130 break;
2131 }
2132 } while (eat);
2133
2134 /* Free pulled out fragments. */
2135 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2136 skb_shinfo(skb)->frag_list = list->next;
2137 kfree_skb(list);
2138 }
2139 /* And insert new clone at head. */
2140 if (clone) {
2141 clone->next = list;
2142 skb_shinfo(skb)->frag_list = clone;
2143 }
2144 }
2145 /* Success! Now we may commit changes to skb data. */
2146
2147pull_pages:
2148 eat = delta;
2149 k = 0;
2150 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002151 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2152
2153 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002154 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002155 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 } else {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002157 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2158
2159 *frag = skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (eat) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002161 skb_frag_off_add(frag, eat);
2162 skb_frag_size_sub(frag, eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002163 if (!i)
2164 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 eat = 0;
2166 }
2167 k++;
2168 }
2169 }
2170 skb_shinfo(skb)->nr_frags = k;
2171
linzhang3ccc6c62017-07-17 17:25:02 +08002172end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 skb->tail += delta;
2174 skb->data_len -= delta;
2175
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002176 if (!skb->data_len)
2177 skb_zcopy_clear(skb, false);
2178
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002179 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002181EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Eric Dumazet22019b12011-07-29 18:37:31 +00002183/**
2184 * skb_copy_bits - copy bits from skb to kernel buffer
2185 * @skb: source skb
2186 * @offset: offset in source
2187 * @to: destination buffer
2188 * @len: number of bytes to copy
2189 *
2190 * Copy the specified number of bytes from the source skb to the
2191 * destination buffer.
2192 *
2193 * CAUTION ! :
2194 * If its prototype is ever changed,
2195 * check arch/{*}/net/{*}.S files,
2196 * since it is called from BPF assembly code.
2197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2199{
David S. Miller1a028e52007-04-27 15:21:23 -07002200 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002201 struct sk_buff *frag_iter;
2202 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 if (offset > (int)skb->len - len)
2205 goto fault;
2206
2207 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002208 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (copy > len)
2210 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002211 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if ((len -= copy) == 0)
2213 return 0;
2214 offset += copy;
2215 to += copy;
2216 }
2217
2218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002219 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002220 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002222 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002223
Eric Dumazet51c56b02012-04-05 11:35:15 +02002224 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002226 u32 p_off, p_len, copied;
2227 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 u8 *vaddr;
2229
2230 if (copy > len)
2231 copy = len;
2232
Willem de Bruijnc613c202017-07-31 08:15:47 -04002233 skb_frag_foreach_page(f,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002234 skb_frag_off(f) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002235 copy, p, p_off, p_len, copied) {
2236 vaddr = kmap_atomic(p);
2237 memcpy(to + copied, vaddr + p_off, p_len);
2238 kunmap_atomic(vaddr);
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 if ((len -= copy) == 0)
2242 return 0;
2243 offset += copy;
2244 to += copy;
2245 }
David S. Miller1a028e52007-04-27 15:21:23 -07002246 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
David S. Millerfbb398a2009-06-09 00:18:59 -07002249 skb_walk_frags(skb, frag_iter) {
2250 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
David S. Millerfbb398a2009-06-09 00:18:59 -07002252 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
David S. Millerfbb398a2009-06-09 00:18:59 -07002254 end = start + frag_iter->len;
2255 if ((copy = end - offset) > 0) {
2256 if (copy > len)
2257 copy = len;
2258 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2259 goto fault;
2260 if ((len -= copy) == 0)
2261 return 0;
2262 offset += copy;
2263 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002265 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Shirley Maa6686f22011-07-06 12:22:12 +00002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!len)
2269 return 0;
2270
2271fault:
2272 return -EFAULT;
2273}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002274EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Jens Axboe9c55e012007-11-06 23:30:13 -08002276/*
2277 * Callback from splice_to_pipe(), if we need to release some pages
2278 * at the end of the spd in case we error'ed out in filling the pipe.
2279 */
2280static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2281{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002282 put_page(spd->pages[i]);
2283}
Jens Axboe9c55e012007-11-06 23:30:13 -08002284
David S. Millera108d5f2012-04-23 23:06:11 -04002285static struct page *linear_to_page(struct page *page, unsigned int *len,
2286 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002287 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002288{
Eric Dumazet5640f762012-09-23 23:04:42 +00002289 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002290
Eric Dumazet5640f762012-09-23 23:04:42 +00002291 if (!sk_page_frag_refill(sk, pfrag))
2292 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002293
Eric Dumazet5640f762012-09-23 23:04:42 +00002294 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002295
Eric Dumazet5640f762012-09-23 23:04:42 +00002296 memcpy(page_address(pfrag->page) + pfrag->offset,
2297 page_address(page) + *offset, *len);
2298 *offset = pfrag->offset;
2299 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002300
Eric Dumazet5640f762012-09-23 23:04:42 +00002301 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002302}
2303
Eric Dumazet41c73a02012-04-22 12:26:16 +00002304static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2305 struct page *page,
2306 unsigned int offset)
2307{
2308 return spd->nr_pages &&
2309 spd->pages[spd->nr_pages - 1] == page &&
2310 (spd->partial[spd->nr_pages - 1].offset +
2311 spd->partial[spd->nr_pages - 1].len == offset);
2312}
2313
Jens Axboe9c55e012007-11-06 23:30:13 -08002314/*
2315 * Fill page/offset/length into spd, if it can hold more pages.
2316 */
David S. Millera108d5f2012-04-23 23:06:11 -04002317static bool spd_fill_page(struct splice_pipe_desc *spd,
2318 struct pipe_inode_info *pipe, struct page *page,
2319 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002320 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002321 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002322{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002323 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002324 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002325
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002326 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002327 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002328 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002329 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002330 }
2331 if (spd_can_coalesce(spd, page, offset)) {
2332 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002333 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002334 }
2335 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002336 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002337 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002338 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002339 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002340
David S. Millera108d5f2012-04-23 23:06:11 -04002341 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002342}
2343
David S. Millera108d5f2012-04-23 23:06:11 -04002344static bool __splice_segment(struct page *page, unsigned int poff,
2345 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002346 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002347 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002348 struct sock *sk,
2349 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002350{
2351 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002352 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002353
2354 /* skip this segment if already processed */
2355 if (*off >= plen) {
2356 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002357 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002358 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002359
Octavian Purdila2870c432008-07-15 00:49:11 -07002360 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002361 poff += *off;
2362 plen -= *off;
2363 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002364
Eric Dumazet18aafc62013-01-11 14:46:37 +00002365 do {
2366 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002367
Eric Dumazet18aafc62013-01-11 14:46:37 +00002368 if (spd_fill_page(spd, pipe, page, &flen, poff,
2369 linear, sk))
2370 return true;
2371 poff += flen;
2372 plen -= flen;
2373 *len -= flen;
2374 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002375
David S. Millera108d5f2012-04-23 23:06:11 -04002376 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002377}
2378
2379/*
David S. Millera108d5f2012-04-23 23:06:11 -04002380 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002381 * pipe is full or if we already spliced the requested length.
2382 */
David S. Millera108d5f2012-04-23 23:06:11 -04002383static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2384 unsigned int *offset, unsigned int *len,
2385 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002386{
2387 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002388 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002389
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002390 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002391 * If skb->head_frag is set, this 'linear' part is backed by a
2392 * fragment, and if the head is not shared with any clones then
2393 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002394 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002395 if (__splice_segment(virt_to_page(skb->data),
2396 (unsigned long) skb->data & (PAGE_SIZE - 1),
2397 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002398 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002399 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002400 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002401 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002402
2403 /*
2404 * then map the fragments
2405 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002406 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2407 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2408
Ian Campbellea2ab692011-08-22 23:44:58 +00002409 if (__splice_segment(skb_frag_page(f),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002410 skb_frag_off(f), skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002411 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002412 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002413 }
2414
Tom Herbertfa9835e2016-03-07 14:11:04 -08002415 skb_walk_frags(skb, iter) {
2416 if (*offset >= iter->len) {
2417 *offset -= iter->len;
2418 continue;
2419 }
2420 /* __skb_splice_bits() only fails if the output has no room
2421 * left, so no point in going over the frag_list for the error
2422 * case.
2423 */
2424 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2425 return true;
2426 }
2427
David S. Millera108d5f2012-04-23 23:06:11 -04002428 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002429}
2430
2431/*
2432 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002433 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002434 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002435int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002436 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002437 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002438{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002439 struct partial_page partial[MAX_SKB_FRAGS];
2440 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002441 struct splice_pipe_desc spd = {
2442 .pages = pages,
2443 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002444 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002445 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002446 .spd_release = sock_spd_release,
2447 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002448 int ret = 0;
2449
Tom Herbertfa9835e2016-03-07 14:11:04 -08002450 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002451
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002452 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002453 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002454
Jens Axboe35f3d142010-05-20 10:43:18 +02002455 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002456}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002457EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002458
Tom Herbert20bf50d2017-07-28 16:22:42 -07002459/* Send skb data on a socket. Socket must be locked. */
2460int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2461 int len)
2462{
2463 unsigned int orig_len = len;
2464 struct sk_buff *head = skb;
2465 unsigned short fragidx;
2466 int slen, ret;
2467
2468do_frag_list:
2469
2470 /* Deal with head data */
2471 while (offset < skb_headlen(skb) && len) {
2472 struct kvec kv;
2473 struct msghdr msg;
2474
2475 slen = min_t(int, len, skb_headlen(skb) - offset);
2476 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002477 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002478 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002479 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002480
2481 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2482 if (ret <= 0)
2483 goto error;
2484
2485 offset += ret;
2486 len -= ret;
2487 }
2488
2489 /* All the data was skb head? */
2490 if (!len)
2491 goto out;
2492
2493 /* Make offset relative to start of frags */
2494 offset -= skb_headlen(skb);
2495
2496 /* Find where we are in frag list */
2497 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2498 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2499
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002500 if (offset < skb_frag_size(frag))
Tom Herbert20bf50d2017-07-28 16:22:42 -07002501 break;
2502
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002503 offset -= skb_frag_size(frag);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002504 }
2505
2506 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2507 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2508
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002509 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002510
2511 while (slen) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002512 ret = kernel_sendpage_locked(sk, skb_frag_page(frag),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002513 skb_frag_off(frag) + offset,
Tom Herbert20bf50d2017-07-28 16:22:42 -07002514 slen, MSG_DONTWAIT);
2515 if (ret <= 0)
2516 goto error;
2517
2518 len -= ret;
2519 offset += ret;
2520 slen -= ret;
2521 }
2522
2523 offset = 0;
2524 }
2525
2526 if (len) {
2527 /* Process any frag lists */
2528
2529 if (skb == head) {
2530 if (skb_has_frag_list(skb)) {
2531 skb = skb_shinfo(skb)->frag_list;
2532 goto do_frag_list;
2533 }
2534 } else if (skb->next) {
2535 skb = skb->next;
2536 goto do_frag_list;
2537 }
2538 }
2539
2540out:
2541 return orig_len - len;
2542
2543error:
2544 return orig_len == len ? ret : orig_len - len;
2545}
2546EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2547
Herbert Xu357b40a2005-04-19 22:30:14 -07002548/**
2549 * skb_store_bits - store bits from kernel buffer to skb
2550 * @skb: destination buffer
2551 * @offset: offset in destination
2552 * @from: source buffer
2553 * @len: number of bytes to copy
2554 *
2555 * Copy the specified number of bytes from the source buffer to the
2556 * destination skb. This function handles all the messy bits of
2557 * traversing fragment lists and such.
2558 */
2559
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002560int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002561{
David S. Miller1a028e52007-04-27 15:21:23 -07002562 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002563 struct sk_buff *frag_iter;
2564 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002565
2566 if (offset > (int)skb->len - len)
2567 goto fault;
2568
David S. Miller1a028e52007-04-27 15:21:23 -07002569 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002570 if (copy > len)
2571 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002572 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002573 if ((len -= copy) == 0)
2574 return 0;
2575 offset += copy;
2576 from += copy;
2577 }
2578
2579 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2580 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002581 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002582
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002583 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002584
Eric Dumazet9e903e02011-10-18 21:00:24 +00002585 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002586 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002587 u32 p_off, p_len, copied;
2588 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002589 u8 *vaddr;
2590
2591 if (copy > len)
2592 copy = len;
2593
Willem de Bruijnc613c202017-07-31 08:15:47 -04002594 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002595 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002596 copy, p, p_off, p_len, copied) {
2597 vaddr = kmap_atomic(p);
2598 memcpy(vaddr + p_off, from + copied, p_len);
2599 kunmap_atomic(vaddr);
2600 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002601
2602 if ((len -= copy) == 0)
2603 return 0;
2604 offset += copy;
2605 from += copy;
2606 }
David S. Miller1a028e52007-04-27 15:21:23 -07002607 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002608 }
2609
David S. Millerfbb398a2009-06-09 00:18:59 -07002610 skb_walk_frags(skb, frag_iter) {
2611 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002612
David S. Millerfbb398a2009-06-09 00:18:59 -07002613 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002614
David S. Millerfbb398a2009-06-09 00:18:59 -07002615 end = start + frag_iter->len;
2616 if ((copy = end - offset) > 0) {
2617 if (copy > len)
2618 copy = len;
2619 if (skb_store_bits(frag_iter, offset - start,
2620 from, copy))
2621 goto fault;
2622 if ((len -= copy) == 0)
2623 return 0;
2624 offset += copy;
2625 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002626 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002627 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002628 }
2629 if (!len)
2630 return 0;
2631
2632fault:
2633 return -EFAULT;
2634}
Herbert Xu357b40a2005-04-19 22:30:14 -07002635EXPORT_SYMBOL(skb_store_bits);
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002638__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2639 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
David S. Miller1a028e52007-04-27 15:21:23 -07002641 int start = skb_headlen(skb);
2642 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002643 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 int pos = 0;
2645
2646 /* Checksum header. */
2647 if (copy > 0) {
2648 if (copy > len)
2649 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002650 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2651 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if ((len -= copy) == 0)
2653 return csum;
2654 offset += copy;
2655 pos = copy;
2656 }
2657
2658 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002659 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002660 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002662 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002663
Eric Dumazet51c56b02012-04-05 11:35:15 +02002664 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002666 u32 p_off, p_len, copied;
2667 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002668 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 if (copy > len)
2672 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002673
2674 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002675 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002676 copy, p, p_off, p_len, copied) {
2677 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002678 csum2 = INDIRECT_CALL_1(ops->update,
2679 csum_partial_ext,
2680 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002681 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002682 csum = INDIRECT_CALL_1(ops->combine,
2683 csum_block_add_ext, csum,
2684 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002685 pos += p_len;
2686 }
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (!(len -= copy))
2689 return csum;
2690 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
David S. Miller1a028e52007-04-27 15:21:23 -07002692 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694
David S. Millerfbb398a2009-06-09 00:18:59 -07002695 skb_walk_frags(skb, frag_iter) {
2696 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
David S. Millerfbb398a2009-06-09 00:18:59 -07002698 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
David S. Millerfbb398a2009-06-09 00:18:59 -07002700 end = start + frag_iter->len;
2701 if ((copy = end - offset) > 0) {
2702 __wsum csum2;
2703 if (copy > len)
2704 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002705 csum2 = __skb_checksum(frag_iter, offset - start,
2706 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002707 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2708 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002709 if ((len -= copy) == 0)
2710 return csum;
2711 offset += copy;
2712 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002714 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002716 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 return csum;
2719}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002720EXPORT_SYMBOL(__skb_checksum);
2721
2722__wsum skb_checksum(const struct sk_buff *skb, int offset,
2723 int len, __wsum csum)
2724{
2725 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002726 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002727 .combine = csum_block_add_ext,
2728 };
2729
2730 return __skb_checksum(skb, offset, len, csum, &ops);
2731}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002732EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734/* Both of above in one bottle. */
2735
Al Viro81d77662006-11-14 21:37:33 -08002736__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
Al Viro8d5930d2020-07-10 20:07:10 -04002737 u8 *to, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
David S. Miller1a028e52007-04-27 15:21:23 -07002739 int start = skb_headlen(skb);
2740 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002741 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 int pos = 0;
Al Viro8d5930d2020-07-10 20:07:10 -04002743 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 /* Copy header. */
2746 if (copy > 0) {
2747 if (copy > len)
2748 copy = len;
2749 csum = csum_partial_copy_nocheck(skb->data + offset, to,
Al Virocc44c172020-07-11 00:12:07 -04002750 copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if ((len -= copy) == 0)
2752 return csum;
2753 offset += copy;
2754 to += copy;
2755 pos = copy;
2756 }
2757
2758 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002759 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002761 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002762
Eric Dumazet9e903e02011-10-18 21:00:24 +00002763 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002765 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2766 u32 p_off, p_len, copied;
2767 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002768 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 if (copy > len)
2772 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002773
2774 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002775 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002776 copy, p, p_off, p_len, copied) {
2777 vaddr = kmap_atomic(p);
2778 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2779 to + copied,
Al Virocc44c172020-07-11 00:12:07 -04002780 p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002781 kunmap_atomic(vaddr);
2782 csum = csum_block_add(csum, csum2, pos);
2783 pos += p_len;
2784 }
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (!(len -= copy))
2787 return csum;
2788 offset += copy;
2789 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
David S. Miller1a028e52007-04-27 15:21:23 -07002791 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
David S. Millerfbb398a2009-06-09 00:18:59 -07002794 skb_walk_frags(skb, frag_iter) {
2795 __wsum csum2;
2796 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
David S. Millerfbb398a2009-06-09 00:18:59 -07002798 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
David S. Millerfbb398a2009-06-09 00:18:59 -07002800 end = start + frag_iter->len;
2801 if ((copy = end - offset) > 0) {
2802 if (copy > len)
2803 copy = len;
2804 csum2 = skb_copy_and_csum_bits(frag_iter,
2805 offset - start,
Al Viro8d5930d2020-07-10 20:07:10 -04002806 to, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002807 csum = csum_block_add(csum, csum2, pos);
2808 if ((len -= copy) == 0)
2809 return csum;
2810 offset += copy;
2811 to += copy;
2812 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002814 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002816 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return csum;
2818}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002819EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Cong Wang49f8e832018-11-08 14:05:42 -08002821__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2822{
2823 __sum16 sum;
2824
2825 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002826 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002827 if (likely(!sum)) {
2828 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2829 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002830 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002831 }
2832 if (!skb_shared(skb))
2833 skb->csum_valid = !sum;
2834 return sum;
2835}
2836EXPORT_SYMBOL(__skb_checksum_complete_head);
2837
Cong Wang14641932018-11-26 09:31:26 -08002838/* This function assumes skb->csum already holds pseudo header's checksum,
2839 * which has been changed from the hardware checksum, for example, by
2840 * __skb_checksum_validate_complete(). And, the original skb->csum must
2841 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2842 *
2843 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2844 * zero. The new checksum is stored back into skb->csum unless the skb is
2845 * shared.
2846 */
Cong Wang49f8e832018-11-08 14:05:42 -08002847__sum16 __skb_checksum_complete(struct sk_buff *skb)
2848{
2849 __wsum csum;
2850 __sum16 sum;
2851
2852 csum = skb_checksum(skb, 0, skb->len, 0);
2853
Cong Wang49f8e832018-11-08 14:05:42 -08002854 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002855 /* This check is inverted, because we already knew the hardware
2856 * checksum is invalid before calling this function. So, if the
2857 * re-computed checksum is valid instead, then we have a mismatch
2858 * between the original skb->csum and skb_checksum(). This means either
2859 * the original hardware checksum is incorrect or we screw up skb->csum
2860 * when moving skb->data around.
2861 */
Cong Wang49f8e832018-11-08 14:05:42 -08002862 if (likely(!sum)) {
2863 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2864 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002865 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002866 }
2867
2868 if (!skb_shared(skb)) {
2869 /* Save full packet checksum */
2870 skb->csum = csum;
2871 skb->ip_summed = CHECKSUM_COMPLETE;
2872 skb->csum_complete_sw = 1;
2873 skb->csum_valid = !sum;
2874 }
2875
2876 return sum;
2877}
2878EXPORT_SYMBOL(__skb_checksum_complete);
2879
Davide Caratti96178132017-05-18 15:44:37 +02002880static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2881{
2882 net_warn_ratelimited(
2883 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2884 __func__);
2885 return 0;
2886}
2887
2888static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2889 int offset, int len)
2890{
2891 net_warn_ratelimited(
2892 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2893 __func__);
2894 return 0;
2895}
2896
2897static const struct skb_checksum_ops default_crc32c_ops = {
2898 .update = warn_crc32c_csum_update,
2899 .combine = warn_crc32c_csum_combine,
2900};
2901
2902const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2903 &default_crc32c_ops;
2904EXPORT_SYMBOL(crc32c_csum_stub);
2905
Thomas Grafaf2806f2013-12-13 15:22:17 +01002906 /**
2907 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2908 * @from: source buffer
2909 *
2910 * Calculates the amount of linear headroom needed in the 'to' skb passed
2911 * into skb_zerocopy().
2912 */
2913unsigned int
2914skb_zerocopy_headlen(const struct sk_buff *from)
2915{
2916 unsigned int hlen = 0;
2917
2918 if (!from->head_frag ||
2919 skb_headlen(from) < L1_CACHE_BYTES ||
2920 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2921 hlen = skb_headlen(from);
2922
2923 if (skb_has_frag_list(from))
2924 hlen = from->len;
2925
2926 return hlen;
2927}
2928EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2929
2930/**
2931 * skb_zerocopy - Zero copy skb to skb
2932 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002933 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002934 * @len: number of bytes to copy from source buffer
2935 * @hlen: size of linear headroom in destination buffer
2936 *
2937 * Copies up to `len` bytes from `from` to `to` by creating references
2938 * to the frags in the source buffer.
2939 *
2940 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2941 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002942 *
2943 * Return value:
2944 * 0: everything is OK
2945 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2946 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002947 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002948int
2949skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002950{
2951 int i, j = 0;
2952 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002953 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002954 struct page *page;
2955 unsigned int offset;
2956
2957 BUG_ON(!from->head_frag && !hlen);
2958
2959 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002960 if (len <= skb_tailroom(to))
2961 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002962
2963 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002964 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2965 if (unlikely(ret))
2966 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002967 len -= hlen;
2968 } else {
2969 plen = min_t(int, skb_headlen(from), len);
2970 if (plen) {
2971 page = virt_to_head_page(from->head);
2972 offset = from->data - (unsigned char *)page_address(page);
2973 __skb_fill_page_desc(to, 0, page, offset, plen);
2974 get_page(page);
2975 j = 1;
2976 len -= plen;
2977 }
2978 }
2979
2980 to->truesize += len + plen;
2981 to->len += len + plen;
2982 to->data_len += len + plen;
2983
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002984 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2985 skb_tx_error(from);
2986 return -ENOMEM;
2987 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002988 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002989
Thomas Grafaf2806f2013-12-13 15:22:17 +01002990 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002991 int size;
2992
Thomas Grafaf2806f2013-12-13 15:22:17 +01002993 if (!len)
2994 break;
2995 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002996 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
2997 len);
2998 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
2999 len -= size;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003000 skb_frag_ref(to, j);
3001 j++;
3002 }
3003 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003004
3005 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003006}
3007EXPORT_SYMBOL_GPL(skb_zerocopy);
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3010{
Al Virod3bc23e2006-11-14 21:24:49 -08003011 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 long csstart;
3013
Patrick McHardy84fa7932006-08-29 16:44:56 -07003014 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00003015 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 else
3017 csstart = skb_headlen(skb);
3018
Kris Katterjohn09a62662006-01-08 22:24:28 -08003019 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003021 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 csum = 0;
3024 if (csstart != skb->len)
3025 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
Al Viro8d5930d2020-07-10 20:07:10 -04003026 skb->len - csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Patrick McHardy84fa7932006-08-29 16:44:56 -07003028 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003029 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Al Virod3bc23e2006-11-14 21:24:49 -08003031 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003034EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036/**
3037 * skb_dequeue - remove from the head of the queue
3038 * @list: list to dequeue from
3039 *
3040 * Remove the head of the list. The list lock is taken so the function
3041 * may be used safely with other locking list functions. The head item is
3042 * returned or %NULL if the list is empty.
3043 */
3044
3045struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3046{
3047 unsigned long flags;
3048 struct sk_buff *result;
3049
3050 spin_lock_irqsave(&list->lock, flags);
3051 result = __skb_dequeue(list);
3052 spin_unlock_irqrestore(&list->lock, flags);
3053 return result;
3054}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003055EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057/**
3058 * skb_dequeue_tail - remove from the tail of the queue
3059 * @list: list to dequeue from
3060 *
3061 * Remove the tail of the list. The list lock is taken so the function
3062 * may be used safely with other locking list functions. The tail item is
3063 * returned or %NULL if the list is empty.
3064 */
3065struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3066{
3067 unsigned long flags;
3068 struct sk_buff *result;
3069
3070 spin_lock_irqsave(&list->lock, flags);
3071 result = __skb_dequeue_tail(list);
3072 spin_unlock_irqrestore(&list->lock, flags);
3073 return result;
3074}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003075EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077/**
3078 * skb_queue_purge - empty a list
3079 * @list: list to empty
3080 *
3081 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3082 * the list and one reference dropped. This function takes the list
3083 * lock and is atomic with respect to other list locking functions.
3084 */
3085void skb_queue_purge(struct sk_buff_head *list)
3086{
3087 struct sk_buff *skb;
3088 while ((skb = skb_dequeue(list)) != NULL)
3089 kfree_skb(skb);
3090}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003091EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003094 * skb_rbtree_purge - empty a skb rbtree
3095 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003096 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003097 *
3098 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3099 * the list and one reference dropped. This function does not take
3100 * any lock. Synchronization should be handled by the caller (e.g., TCP
3101 * out-of-order queue is protected by the socket lock).
3102 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003103unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003104{
Eric Dumazet7c905842017-09-23 12:39:12 -07003105 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003106 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003107
Eric Dumazet7c905842017-09-23 12:39:12 -07003108 while (p) {
3109 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3110
3111 p = rb_next(p);
3112 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003113 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003114 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003115 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003116 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003117}
3118
3119/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 * skb_queue_head - queue a buffer at the list head
3121 * @list: list to use
3122 * @newsk: buffer to queue
3123 *
3124 * Queue a buffer at the start of the list. This function takes the
3125 * list lock and can be used safely with other locking &sk_buff functions
3126 * safely.
3127 *
3128 * A buffer cannot be placed on two lists at the same time.
3129 */
3130void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3131{
3132 unsigned long flags;
3133
3134 spin_lock_irqsave(&list->lock, flags);
3135 __skb_queue_head(list, newsk);
3136 spin_unlock_irqrestore(&list->lock, flags);
3137}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003138EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140/**
3141 * skb_queue_tail - queue a buffer at the list tail
3142 * @list: list to use
3143 * @newsk: buffer to queue
3144 *
3145 * Queue a buffer at the tail of the list. This function takes the
3146 * list lock and can be used safely with other locking &sk_buff functions
3147 * safely.
3148 *
3149 * A buffer cannot be placed on two lists at the same time.
3150 */
3151void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3152{
3153 unsigned long flags;
3154
3155 spin_lock_irqsave(&list->lock, flags);
3156 __skb_queue_tail(list, newsk);
3157 spin_unlock_irqrestore(&list->lock, flags);
3158}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003159EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161/**
3162 * skb_unlink - remove a buffer from a list
3163 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003164 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 *
David S. Miller8728b832005-08-09 19:25:21 -07003166 * Remove a packet from a list. The list locks are taken and this
3167 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 *
David S. Miller8728b832005-08-09 19:25:21 -07003169 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 */
David S. Miller8728b832005-08-09 19:25:21 -07003171void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
David S. Miller8728b832005-08-09 19:25:21 -07003173 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
David S. Miller8728b832005-08-09 19:25:21 -07003175 spin_lock_irqsave(&list->lock, flags);
3176 __skb_unlink(skb, list);
3177 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003179EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181/**
3182 * skb_append - append a buffer
3183 * @old: buffer to insert after
3184 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003185 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 *
3187 * Place a packet after a given packet in a list. The list locks are taken
3188 * and this function is atomic with respect to other list locked calls.
3189 * A buffer cannot be placed on two lists at the same time.
3190 */
David S. Miller8728b832005-08-09 19:25:21 -07003191void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 unsigned long flags;
3194
David S. Miller8728b832005-08-09 19:25:21 -07003195 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003196 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003197 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003199EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201static inline void skb_split_inside_header(struct sk_buff *skb,
3202 struct sk_buff* skb1,
3203 const u32 len, const int pos)
3204{
3205 int i;
3206
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003207 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3208 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 /* And move data appendix as is. */
3210 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3211 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3212
3213 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3214 skb_shinfo(skb)->nr_frags = 0;
3215 skb1->data_len = skb->data_len;
3216 skb1->len += skb1->data_len;
3217 skb->data_len = 0;
3218 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003219 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
3222static inline void skb_split_no_header(struct sk_buff *skb,
3223 struct sk_buff* skb1,
3224 const u32 len, int pos)
3225{
3226 int i, k = 0;
3227 const int nfrags = skb_shinfo(skb)->nr_frags;
3228
3229 skb_shinfo(skb)->nr_frags = 0;
3230 skb1->len = skb1->data_len = skb->len - len;
3231 skb->len = len;
3232 skb->data_len = len - pos;
3233
3234 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003235 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 if (pos + size > len) {
3238 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3239
3240 if (pos < len) {
3241 /* Split frag.
3242 * We have two variants in this case:
3243 * 1. Move all the frag to the second
3244 * part, if it is possible. F.e.
3245 * this approach is mandatory for TUX,
3246 * where splitting is expensive.
3247 * 2. Split is accurately. We make this.
3248 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003249 skb_frag_ref(skb, i);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003250 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003251 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3252 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 skb_shinfo(skb)->nr_frags++;
3254 }
3255 k++;
3256 } else
3257 skb_shinfo(skb)->nr_frags++;
3258 pos += size;
3259 }
3260 skb_shinfo(skb1)->nr_frags = k;
3261}
3262
3263/**
3264 * skb_split - Split fragmented skb to two parts at length len.
3265 * @skb: the buffer to split
3266 * @skb1: the buffer to receive the second part
3267 * @len: new length for skb
3268 */
3269void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3270{
3271 int pos = skb_headlen(skb);
3272
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08003273 skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003274 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (len < pos) /* Split line is inside header. */
3276 skb_split_inside_header(skb, skb1, len, pos);
3277 else /* Second chunk has no header, nothing to copy. */
3278 skb_split_no_header(skb, skb1, len, pos);
3279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003280EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003282/* Shifting from/to a cloned skb is a no-go.
3283 *
3284 * Caller cannot keep skb_shinfo related pointers past calling here!
3285 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003286static int skb_prepare_for_shift(struct sk_buff *skb)
3287{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08003288 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003289}
3290
3291/**
3292 * skb_shift - Shifts paged data partially from skb to another
3293 * @tgt: buffer into which tail data gets added
3294 * @skb: buffer from which the paged data comes from
3295 * @shiftlen: shift up to this many bytes
3296 *
3297 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003298 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003299 * It's up to caller to free skb if everything was shifted.
3300 *
3301 * If @tgt runs out of frags, the whole operation is aborted.
3302 *
3303 * Skb cannot include anything else but paged data while tgt is allowed
3304 * to have non-paged data as well.
3305 *
3306 * TODO: full sized shift could be optimized but that would need
3307 * specialized skb free'er to handle frags without up-to-date nr_frags.
3308 */
3309int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3310{
3311 int from, to, merge, todo;
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003312 skb_frag_t *fragfrom, *fragto;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003313
3314 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003315
3316 if (skb_headlen(skb))
3317 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003318 if (skb_zcopy(tgt) || skb_zcopy(skb))
3319 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003320
3321 todo = shiftlen;
3322 from = 0;
3323 to = skb_shinfo(tgt)->nr_frags;
3324 fragfrom = &skb_shinfo(skb)->frags[from];
3325
3326 /* Actual merge is delayed until the point when we know we can
3327 * commit all, so that we don't have to undo partial changes
3328 */
3329 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003330 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003331 skb_frag_off(fragfrom))) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003332 merge = -1;
3333 } else {
3334 merge = to - 1;
3335
Eric Dumazet9e903e02011-10-18 21:00:24 +00003336 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003337 if (todo < 0) {
3338 if (skb_prepare_for_shift(skb) ||
3339 skb_prepare_for_shift(tgt))
3340 return 0;
3341
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003342 /* All previous frag pointers might be stale! */
3343 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003344 fragto = &skb_shinfo(tgt)->frags[merge];
3345
Eric Dumazet9e903e02011-10-18 21:00:24 +00003346 skb_frag_size_add(fragto, shiftlen);
3347 skb_frag_size_sub(fragfrom, shiftlen);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003348 skb_frag_off_add(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003349
3350 goto onlymerged;
3351 }
3352
3353 from++;
3354 }
3355
3356 /* Skip full, not-fitting skb to avoid expensive operations */
3357 if ((shiftlen == skb->len) &&
3358 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3359 return 0;
3360
3361 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3362 return 0;
3363
3364 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3365 if (to == MAX_SKB_FRAGS)
3366 return 0;
3367
3368 fragfrom = &skb_shinfo(skb)->frags[from];
3369 fragto = &skb_shinfo(tgt)->frags[to];
3370
Eric Dumazet9e903e02011-10-18 21:00:24 +00003371 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003372 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003373 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003374 from++;
3375 to++;
3376
3377 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003378 __skb_frag_ref(fragfrom);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003379 skb_frag_page_copy(fragto, fragfrom);
3380 skb_frag_off_copy(fragto, fragfrom);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003381 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003382
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003383 skb_frag_off_add(fragfrom, todo);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003384 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003385 todo = 0;
3386
3387 to++;
3388 break;
3389 }
3390 }
3391
3392 /* Ready to "commit" this state change to tgt */
3393 skb_shinfo(tgt)->nr_frags = to;
3394
3395 if (merge >= 0) {
3396 fragfrom = &skb_shinfo(skb)->frags[0];
3397 fragto = &skb_shinfo(tgt)->frags[merge];
3398
Eric Dumazet9e903e02011-10-18 21:00:24 +00003399 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003400 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003401 }
3402
3403 /* Reposition in the original skb */
3404 to = 0;
3405 while (from < skb_shinfo(skb)->nr_frags)
3406 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3407 skb_shinfo(skb)->nr_frags = to;
3408
3409 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3410
3411onlymerged:
3412 /* Most likely the tgt won't ever need its checksum anymore, skb on
3413 * the other hand might need it if it needs to be resent
3414 */
3415 tgt->ip_summed = CHECKSUM_PARTIAL;
3416 skb->ip_summed = CHECKSUM_PARTIAL;
3417
3418 /* Yak, is it really working this way? Some helper please? */
3419 skb->len -= shiftlen;
3420 skb->data_len -= shiftlen;
3421 skb->truesize -= shiftlen;
3422 tgt->len += shiftlen;
3423 tgt->data_len += shiftlen;
3424 tgt->truesize += shiftlen;
3425
3426 return shiftlen;
3427}
3428
Thomas Graf677e90e2005-06-23 20:59:51 -07003429/**
3430 * skb_prepare_seq_read - Prepare a sequential read of skb data
3431 * @skb: the buffer to read
3432 * @from: lower offset of data to be read
3433 * @to: upper offset of data to be read
3434 * @st: state variable
3435 *
3436 * Initializes the specified state variable. Must be called before
3437 * invoking skb_seq_read() for the first time.
3438 */
3439void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3440 unsigned int to, struct skb_seq_state *st)
3441{
3442 st->lower_offset = from;
3443 st->upper_offset = to;
3444 st->root_skb = st->cur_skb = skb;
3445 st->frag_idx = st->stepped_offset = 0;
3446 st->frag_data = NULL;
Willem de Bruijn97550f62021-01-09 17:18:33 -05003447 st->frag_off = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003448}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003449EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003450
3451/**
3452 * skb_seq_read - Sequentially read skb data
3453 * @consumed: number of bytes consumed by the caller so far
3454 * @data: destination pointer for data to be returned
3455 * @st: state variable
3456 *
Mathias Krausebc323832013-11-07 14:18:26 +01003457 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003458 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003459 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003460 * of the block or 0 if the end of the skb data or the upper
3461 * offset has been reached.
3462 *
3463 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003464 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003465 * of bytes already consumed and the next call to
3466 * skb_seq_read() will return the remaining part of the block.
3467 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003468 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003469 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003470 * reads of potentially non linear data.
3471 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003472 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003473 * at the moment, state->root_skb could be replaced with
3474 * a stack for this purpose.
3475 */
3476unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3477 struct skb_seq_state *st)
3478{
3479 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3480 skb_frag_t *frag;
3481
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003482 if (unlikely(abs_offset >= st->upper_offset)) {
3483 if (st->frag_data) {
3484 kunmap_atomic(st->frag_data);
3485 st->frag_data = NULL;
3486 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003487 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003488 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003489
3490next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003491 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003492
Thomas Chenault995b3372009-05-18 21:43:27 -07003493 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003494 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003495 return block_limit - abs_offset;
3496 }
3497
3498 if (st->frag_idx == 0 && !st->frag_data)
3499 st->stepped_offset += skb_headlen(st->cur_skb);
3500
3501 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
Willem de Bruijn97550f62021-01-09 17:18:33 -05003502 unsigned int pg_idx, pg_off, pg_sz;
Thomas Graf677e90e2005-06-23 20:59:51 -07003503
Willem de Bruijn97550f62021-01-09 17:18:33 -05003504 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3505
3506 pg_idx = 0;
3507 pg_off = skb_frag_off(frag);
3508 pg_sz = skb_frag_size(frag);
3509
3510 if (skb_frag_must_loop(skb_frag_page(frag))) {
3511 pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3512 pg_off = offset_in_page(pg_off + st->frag_off);
3513 pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3514 PAGE_SIZE - pg_off);
3515 }
3516
3517 block_limit = pg_sz + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003518 if (abs_offset < block_limit) {
3519 if (!st->frag_data)
Willem de Bruijn97550f62021-01-09 17:18:33 -05003520 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
Thomas Graf677e90e2005-06-23 20:59:51 -07003521
Willem de Bruijn97550f62021-01-09 17:18:33 -05003522 *data = (u8 *)st->frag_data + pg_off +
Thomas Graf677e90e2005-06-23 20:59:51 -07003523 (abs_offset - st->stepped_offset);
3524
3525 return block_limit - abs_offset;
3526 }
3527
3528 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003529 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003530 st->frag_data = NULL;
3531 }
3532
Willem de Bruijn97550f62021-01-09 17:18:33 -05003533 st->stepped_offset += pg_sz;
3534 st->frag_off += pg_sz;
3535 if (st->frag_off == skb_frag_size(frag)) {
3536 st->frag_off = 0;
3537 st->frag_idx++;
3538 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003539 }
3540
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003541 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003542 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003543 st->frag_data = NULL;
3544 }
3545
David S. Miller21dc3302010-08-23 00:13:46 -07003546 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003547 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003548 st->frag_idx = 0;
3549 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003550 } else if (st->cur_skb->next) {
3551 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003552 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003553 goto next_skb;
3554 }
3555
3556 return 0;
3557}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003558EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003559
3560/**
3561 * skb_abort_seq_read - Abort a sequential read of skb data
3562 * @st: state variable
3563 *
3564 * Must be called if skb_seq_read() was not called until it
3565 * returned 0.
3566 */
3567void skb_abort_seq_read(struct skb_seq_state *st)
3568{
3569 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003570 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003571}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003572EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003573
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003574#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3575
3576static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3577 struct ts_config *conf,
3578 struct ts_state *state)
3579{
3580 return skb_seq_read(offset, text, TS_SKB_CB(state));
3581}
3582
3583static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3584{
3585 skb_abort_seq_read(TS_SKB_CB(state));
3586}
3587
3588/**
3589 * skb_find_text - Find a text pattern in skb data
3590 * @skb: the buffer to look in
3591 * @from: search offset
3592 * @to: search limit
3593 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003594 *
3595 * Finds a pattern in the skb data according to the specified
3596 * textsearch configuration. Use textsearch_next() to retrieve
3597 * subsequent occurrences of the pattern. Returns the offset
3598 * to the first occurrence or UINT_MAX if no match was found.
3599 */
3600unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003601 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003602{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003603 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003604 unsigned int ret;
3605
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003606 config->get_next_block = skb_ts_get_next_block;
3607 config->finish = skb_ts_finish;
3608
Bojan Prtvar059a2442015-02-22 11:46:35 +01003609 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003610
Bojan Prtvar059a2442015-02-22 11:46:35 +01003611 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003612 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003613}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003614EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003615
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003616int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3617 int offset, size_t size)
3618{
3619 int i = skb_shinfo(skb)->nr_frags;
3620
3621 if (skb_can_coalesce(skb, i, page, offset)) {
3622 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3623 } else if (i < MAX_SKB_FRAGS) {
3624 get_page(page);
3625 skb_fill_page_desc(skb, i, page, offset, size);
3626 } else {
3627 return -EMSGSIZE;
3628 }
3629
3630 return 0;
3631}
3632EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3633
Herbert Xucbb042f2006-03-20 22:43:56 -08003634/**
3635 * skb_pull_rcsum - pull skb and update receive checksum
3636 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003637 * @len: length of data pulled
3638 *
3639 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003640 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003641 * receive path processing instead of skb_pull unless you know
3642 * that the checksum difference is zero (e.g., a valid IP header)
3643 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003644 */
Johannes Bergaf728682017-06-16 14:29:22 +02003645void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003646{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003647 unsigned char *data = skb->data;
3648
Herbert Xucbb042f2006-03-20 22:43:56 -08003649 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003650 __skb_pull(skb, len);
3651 skb_postpull_rcsum(skb, data, len);
3652 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003653}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003654EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3655
Yonghong Song13acc942018-03-21 16:31:03 -07003656static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3657{
3658 skb_frag_t head_frag;
3659 struct page *page;
3660
3661 page = virt_to_head_page(frag_skb->head);
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003662 __skb_frag_set_page(&head_frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003663 skb_frag_off_set(&head_frag, frag_skb->data -
3664 (unsigned char *)page_address(page));
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003665 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
Yonghong Song13acc942018-03-21 16:31:03 -07003666 return head_frag;
3667}
3668
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003669struct sk_buff *skb_segment_list(struct sk_buff *skb,
3670 netdev_features_t features,
3671 unsigned int offset)
3672{
3673 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3674 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3675 unsigned int delta_truesize = 0;
3676 unsigned int delta_len = 0;
3677 struct sk_buff *tail = NULL;
Dongseok Yi53475c52021-01-08 11:28:38 +09003678 struct sk_buff *nskb, *tmp;
3679 int err;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003680
3681 skb_push(skb, -skb_network_offset(skb) + offset);
3682
3683 skb_shinfo(skb)->frag_list = NULL;
3684
3685 do {
3686 nskb = list_skb;
3687 list_skb = list_skb->next;
3688
Dongseok Yi53475c52021-01-08 11:28:38 +09003689 err = 0;
3690 if (skb_shared(nskb)) {
3691 tmp = skb_clone(nskb, GFP_ATOMIC);
3692 if (tmp) {
3693 consume_skb(nskb);
3694 nskb = tmp;
3695 err = skb_unclone(nskb, GFP_ATOMIC);
3696 } else {
3697 err = -ENOMEM;
3698 }
3699 }
3700
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003701 if (!tail)
3702 skb->next = nskb;
3703 else
3704 tail->next = nskb;
3705
Dongseok Yi53475c52021-01-08 11:28:38 +09003706 if (unlikely(err)) {
3707 nskb->next = list_skb;
3708 goto err_linearize;
3709 }
3710
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003711 tail = nskb;
3712
3713 delta_len += nskb->len;
3714 delta_truesize += nskb->truesize;
3715
3716 skb_push(nskb, -skb_network_offset(nskb) + offset);
3717
Florian Westphalcf673ed2020-03-30 18:51:29 +02003718 skb_release_head_state(nskb);
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003719 __copy_skb_header(nskb, skb);
3720
3721 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3722 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3723 nskb->data - tnl_hlen,
3724 offset + tnl_hlen);
3725
3726 if (skb_needs_linearize(nskb, features) &&
3727 __skb_linearize(nskb))
3728 goto err_linearize;
3729
3730 } while (list_skb);
3731
3732 skb->truesize = skb->truesize - delta_truesize;
3733 skb->data_len = skb->data_len - delta_len;
3734 skb->len = skb->len - delta_len;
3735
3736 skb_gso_reset(skb);
3737
3738 skb->prev = tail;
3739
3740 if (skb_needs_linearize(skb, features) &&
3741 __skb_linearize(skb))
3742 goto err_linearize;
3743
3744 skb_get(skb);
3745
3746 return skb;
3747
3748err_linearize:
3749 kfree_skb_list(skb->next);
3750 skb->next = NULL;
3751 return ERR_PTR(-ENOMEM);
3752}
3753EXPORT_SYMBOL_GPL(skb_segment_list);
3754
3755int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3756{
3757 if (unlikely(p->len + skb->len >= 65536))
3758 return -E2BIG;
3759
3760 if (NAPI_GRO_CB(p)->last == p)
3761 skb_shinfo(p)->frag_list = skb;
3762 else
3763 NAPI_GRO_CB(p)->last->next = skb;
3764
3765 skb_pull(skb, skb_gro_offset(skb));
3766
3767 NAPI_GRO_CB(p)->last = skb;
3768 NAPI_GRO_CB(p)->count++;
3769 p->data_len += skb->len;
3770 p->truesize += skb->truesize;
3771 p->len += skb->len;
3772
3773 NAPI_GRO_CB(skb)->same_flow = 1;
3774
3775 return 0;
3776}
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003777
Herbert Xuf4c50d92006-06-22 03:02:40 -07003778/**
3779 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003780 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003781 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003782 *
3783 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003784 * a pointer to the first in a list of new skbs for the segments.
3785 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003786 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003787struct sk_buff *skb_segment(struct sk_buff *head_skb,
3788 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003789{
3790 struct sk_buff *segs = NULL;
3791 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003792 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003793 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3794 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3795 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003796 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003797 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003798 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003799 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003800 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003801 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003802 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003803 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003804 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003805 int err = -ENOMEM;
3806 int i = 0;
3807 int pos;
3808
Shmulik Ladkani3dcbdb12019-09-06 12:23:50 +03003809 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3810 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3811 /* gso_size is untrusted, and we have a frag_list with a linear
3812 * non head_frag head.
3813 *
3814 * (we assume checking the first list_skb member suffices;
3815 * i.e if either of the list_skb members have non head_frag
3816 * head, then the first one has too).
3817 *
3818 * If head_skb's headlen does not fit requested gso_size, it
3819 * means that the frag_list members do NOT terminate on exact
3820 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3821 * sharing. Therefore we must fallback to copying the frag_list
3822 * skbs; we do so by disabling SG.
3823 */
3824 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3825 features &= ~NETIF_F_SG;
3826 }
3827
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003828 __skb_push(head_skb, doffset);
Miaohe Lin2f631132020-08-01 17:36:05 +08003829 proto = skb_network_protocol(head_skb, NULL);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003830 if (unlikely(!proto))
3831 return ERR_PTR(-EINVAL);
3832
Alexander Duyck36c98382016-05-02 09:38:18 -07003833 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003834 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003835
Steffen Klassert07b26c92016-09-19 12:58:47 +02003836 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3837 if (!(features & NETIF_F_GSO_PARTIAL)) {
3838 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003839 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003840
3841 if (!list_skb ||
3842 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3843 goto normal;
3844
Ilan Tayari43170c42017-04-19 21:26:07 +03003845 /* If we get here then all the required
3846 * GSO features except frag_list are supported.
3847 * Try to split the SKB to multiple GSO SKBs
3848 * with no frag_list.
3849 * Currently we can do that only when the buffers don't
3850 * have a linear part and all the buffers except
3851 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003852 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003853 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003854 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003855 if (frag_len != iter->len && iter->next)
3856 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003857 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003858 goto normal;
3859
3860 len -= iter->len;
3861 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003862
3863 if (len != frag_len)
3864 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003865 }
3866
3867 /* GSO partial only requires that we trim off any excess that
3868 * doesn't fit into an MSS sized block, so take care of that
3869 * now.
3870 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003871 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003872 if (partial_segs > 1)
3873 mss *= partial_segs;
3874 else
3875 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003876 }
3877
Steffen Klassert07b26c92016-09-19 12:58:47 +02003878normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003879 headroom = skb_headroom(head_skb);
3880 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003881
3882 do {
3883 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003884 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003885 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003886 int size;
3887
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003888 if (unlikely(mss == GSO_BY_FRAGS)) {
3889 len = list_skb->len;
3890 } else {
3891 len = head_skb->len - offset;
3892 if (len > mss)
3893 len = mss;
3894 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003895
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003896 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003897
Xin Longdbd50f22021-01-15 17:36:38 +08003898 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003899 (skb_headlen(list_skb) == len || sg)) {
3900 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003901
Herbert Xu9d8506c2013-11-21 11:10:04 -08003902 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003903 nfrags = skb_shinfo(list_skb)->nr_frags;
3904 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003905 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003906 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003907
3908 while (pos < offset + len) {
3909 BUG_ON(i >= nfrags);
3910
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003911 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003912 if (pos + size > offset + len)
3913 break;
3914
3915 i++;
3916 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003917 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003918 }
3919
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003920 nskb = skb_clone(list_skb, GFP_ATOMIC);
3921 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003922
3923 if (unlikely(!nskb))
3924 goto err;
3925
Herbert Xu9d8506c2013-11-21 11:10:04 -08003926 if (unlikely(pskb_trim(nskb, len))) {
3927 kfree_skb(nskb);
3928 goto err;
3929 }
3930
Alexander Duyckec47ea82012-05-04 14:26:56 +00003931 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003932 if (skb_cow_head(nskb, doffset + headroom)) {
3933 kfree_skb(nskb);
3934 goto err;
3935 }
3936
Alexander Duyckec47ea82012-05-04 14:26:56 +00003937 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003938 skb_release_head_state(nskb);
3939 __skb_push(nskb, doffset);
3940 } else {
Paolo Abeni00b229f2021-01-19 17:56:56 +01003941 if (hsize < 0)
3942 hsize = 0;
Xin Longdbd50f22021-01-15 17:36:38 +08003943 if (hsize > len || !sg)
3944 hsize = len;
Xin Longdbd50f22021-01-15 17:36:38 +08003945
Mel Gormanc93bdd02012-07-31 16:44:19 -07003946 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003947 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003948 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003949
3950 if (unlikely(!nskb))
3951 goto err;
3952
3953 skb_reserve(nskb, headroom);
3954 __skb_put(nskb, doffset);
3955 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003956
3957 if (segs)
3958 tail->next = nskb;
3959 else
3960 segs = nskb;
3961 tail = nskb;
3962
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003963 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003964
Eric Dumazet030737b2013-10-19 11:42:54 -07003965 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003966 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003967
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003968 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003969 nskb->data - tnl_hlen,
3970 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003971
Herbert Xu9d8506c2013-11-21 11:10:04 -08003972 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003973 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003974
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003975 if (!sg) {
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303976 if (!csum) {
3977 if (!nskb->remcsum_offload)
3978 nskb->ip_summed = CHECKSUM_NONE;
3979 SKB_GSO_CB(nskb)->csum =
3980 skb_copy_and_csum_bits(head_skb, offset,
3981 skb_put(nskb,
3982 len),
Al Viro8d5930d2020-07-10 20:07:10 -04003983 len);
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303984 SKB_GSO_CB(nskb)->csum_start =
3985 skb_headroom(nskb) + doffset;
3986 } else {
3987 skb_copy_bits(head_skb, offset,
3988 skb_put(nskb, len),
3989 len);
3990 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003991 continue;
3992 }
3993
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003994 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003995
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003996 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003997 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003998
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08003999 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4000 SKBFL_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00004001
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004002 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4003 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4004 goto err;
4005
Herbert Xu9d8506c2013-11-21 11:10:04 -08004006 while (pos < offset + len) {
4007 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08004008 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004009 nfrags = skb_shinfo(list_skb)->nr_frags;
4010 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004011 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07004012 if (!skb_headlen(list_skb)) {
4013 BUG_ON(!nfrags);
4014 } else {
4015 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004016
Yonghong Song13acc942018-03-21 16:31:03 -07004017 /* to make room for head_frag. */
4018 i--;
4019 frag--;
4020 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004021 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4022 skb_zerocopy_clone(nskb, frag_skb,
4023 GFP_ATOMIC))
4024 goto err;
4025
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004026 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004027 }
4028
4029 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4030 MAX_SKB_FRAGS)) {
4031 net_warn_ratelimited(
4032 "skb_segment: too many frags: %u %u\n",
4033 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07004034 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004035 goto err;
4036 }
4037
Yonghong Song13acc942018-03-21 16:31:03 -07004038 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004039 __skb_frag_ref(nskb_frag);
4040 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004041
4042 if (pos < offset) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004043 skb_frag_off_add(nskb_frag, offset - pos);
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004044 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004045 }
4046
Herbert Xu89319d382008-12-15 23:26:06 -08004047 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004048
4049 if (pos + size <= offset + len) {
4050 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004051 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004052 pos += size;
4053 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004054 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08004055 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004056 }
4057
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004058 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004059 }
4060
Herbert Xu89319d382008-12-15 23:26:06 -08004061skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07004062 nskb->data_len = len - hsize;
4063 nskb->len += nskb->data_len;
4064 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004065
Simon Horman1cdbcb72013-05-19 15:46:49 +00004066perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004067 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07004068 if (skb_has_shared_frag(nskb) &&
4069 __skb_linearize(nskb))
4070 goto err;
4071
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004072 if (!nskb->remcsum_offload)
4073 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08004074 SKB_GSO_CB(nskb)->csum =
4075 skb_checksum(nskb, doffset,
4076 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004077 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08004078 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004079 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004080 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004081
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07004082 /* Some callers want to get the end of the list.
4083 * Put it in segs->prev to avoid walking the list.
4084 * (see validate_xmit_skb_list() for example)
4085 */
4086 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07004087
Alexander Duyck802ab552016-04-10 21:45:03 -04004088 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02004089 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04004090 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004091 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04004092
4093 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004094 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04004095 type &= ~SKB_GSO_DODGY;
4096
4097 /* Update GSO info and prepare to start updating headers on
4098 * our way back down the stack of protocols.
4099 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004100 for (iter = segs; iter; iter = iter->next) {
4101 skb_shinfo(iter)->gso_size = gso_size;
4102 skb_shinfo(iter)->gso_segs = partial_segs;
4103 skb_shinfo(iter)->gso_type = type;
4104 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4105 }
4106
4107 if (tail->len - doffset <= gso_size)
4108 skb_shinfo(tail)->gso_size = 0;
4109 else if (tail != segs)
4110 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04004111 }
4112
Toshiaki Makita432c8562014-10-27 10:30:51 -07004113 /* Following permits correct backpressure, for protocols
4114 * using skb_set_owner_w().
4115 * Idea is to tranfert ownership from head_skb to last segment.
4116 */
4117 if (head_skb->destructor == sock_wfree) {
4118 swap(tail->truesize, head_skb->truesize);
4119 swap(tail->destructor, head_skb->destructor);
4120 swap(tail->sk, head_skb->sk);
4121 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004122 return segs;
4123
4124err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08004125 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004126 return ERR_PTR(err);
4127}
Herbert Xuf4c50d92006-06-22 03:02:40 -07004128EXPORT_SYMBOL_GPL(skb_segment);
4129
David Millerd4546c22018-06-24 14:13:49 +09004130int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08004131{
Eric Dumazet8a291112013-10-08 09:02:23 -07004132 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00004133 unsigned int offset = skb_gro_offset(skb);
4134 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004135 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004136 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09004137 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08004138
Steffen Klassert0ab03f32019-04-02 08:16:03 +02004139 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08004140 return -E2BIG;
4141
Eric Dumazet29e98242014-05-16 11:34:37 -07004142 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07004143 pinfo = skb_shinfo(lp);
4144
4145 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00004146 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004147 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004148 int i = skbinfo->nr_frags;
4149 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00004150
Herbert Xu66e92fc2009-05-26 18:50:32 +00004151 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004152 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00004153
Eric Dumazet8a291112013-10-08 09:02:23 -07004154 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004155 pinfo->nr_frags = nr_frags;
4156 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08004157
Herbert Xu9aaa1562009-05-26 18:50:33 +00004158 frag = pinfo->frags + nr_frags;
4159 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004160 do {
4161 *--frag = *--frag2;
4162 } while (--i);
4163
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004164 skb_frag_off_add(frag, offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004165 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00004166
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004167 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00004168 delta_truesize = skb->truesize -
4169 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004170
Herbert Xuf5572062009-01-14 20:40:03 -08004171 skb->truesize -= skb->data_len;
4172 skb->len -= skb->data_len;
4173 skb->data_len = 0;
4174
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004175 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004176 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004177 } else if (skb->head_frag) {
4178 int nr_frags = pinfo->nr_frags;
4179 skb_frag_t *frag = pinfo->frags + nr_frags;
4180 struct page *page = virt_to_head_page(skb->head);
4181 unsigned int first_size = headlen - offset;
4182 unsigned int first_offset;
4183
4184 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004185 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004186
4187 first_offset = skb->data -
4188 (unsigned char *)page_address(page) +
4189 offset;
4190
4191 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4192
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07004193 __skb_frag_set_page(frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004194 skb_frag_off_set(frag, first_offset);
Eric Dumazetd7e88832012-04-30 08:10:34 +00004195 skb_frag_size_set(frag, first_size);
4196
4197 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4198 /* We dont need to clear skbinfo->nr_frags here */
4199
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004200 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00004201 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4202 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004203 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004204
4205merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004206 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004207 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004208 unsigned int eat = offset - headlen;
4209
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004210 skb_frag_off_add(&skbinfo->frags[0], eat);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004211 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004212 skb->data_len -= eat;
4213 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004214 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004215 }
4216
Herbert Xu67147ba2009-05-26 18:50:22 +00004217 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004218
Eric Dumazet29e98242014-05-16 11:34:37 -07004219 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004220 skb_shinfo(p)->frag_list = skb;
4221 else
4222 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004223 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004224 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004225 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004226
Herbert Xu5d38a072009-01-04 16:13:40 -08004227done:
4228 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004229 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004230 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004231 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004232 if (lp != p) {
4233 lp->data_len += len;
4234 lp->truesize += delta_truesize;
4235 lp->len += len;
4236 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004237 NAPI_GRO_CB(skb)->same_flow = 1;
4238 return 0;
4239}
Herbert Xu71d93b32008-12-15 23:42:33 -08004240
Florian Westphaldf5042f2018-12-18 17:15:16 +01004241#ifdef CONFIG_SKB_EXTENSIONS
4242#define SKB_EXT_ALIGN_VALUE 8
4243#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4244
4245static const u8 skb_ext_type_len[] = {
4246#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4247 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4248#endif
Florian Westphal41650792018-12-18 17:15:27 +01004249#ifdef CONFIG_XFRM
4250 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4251#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004252#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4253 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4254#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004255#if IS_ENABLED(CONFIG_MPTCP)
4256 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4257#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004258};
4259
4260static __always_inline unsigned int skb_ext_total_length(void)
4261{
4262 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4263#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4264 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4265#endif
Florian Westphal41650792018-12-18 17:15:27 +01004266#ifdef CONFIG_XFRM
4267 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4268#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004269#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4270 skb_ext_type_len[TC_SKB_EXT] +
4271#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004272#if IS_ENABLED(CONFIG_MPTCP)
4273 skb_ext_type_len[SKB_EXT_MPTCP] +
4274#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004275 0;
4276}
4277
4278static void skb_extensions_init(void)
4279{
4280 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4281 BUILD_BUG_ON(skb_ext_total_length() > 255);
4282
4283 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4284 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4285 0,
4286 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4287 NULL);
4288}
4289#else
4290static void skb_extensions_init(void) {}
4291#endif
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293void __init skb_init(void)
4294{
Kees Cook79a8a642018-02-07 17:44:38 -08004295 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 sizeof(struct sk_buff),
4297 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004298 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004299 offsetof(struct sk_buff, cb),
4300 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004301 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004302 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004303 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004304 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004305 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004306 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004307 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
David S. Miller51c739d2007-10-30 21:29:29 -07004310static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004311__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4312 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004313{
David S. Miller1a028e52007-04-27 15:21:23 -07004314 int start = skb_headlen(skb);
4315 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004316 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004317 int elt = 0;
4318
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004319 if (unlikely(recursion_level >= 24))
4320 return -EMSGSIZE;
4321
David Howells716ea3a2007-04-02 20:19:53 -07004322 if (copy > 0) {
4323 if (copy > len)
4324 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004325 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004326 elt++;
4327 if ((len -= copy) == 0)
4328 return elt;
4329 offset += copy;
4330 }
4331
4332 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004333 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004334
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004335 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004336
Eric Dumazet9e903e02011-10-18 21:00:24 +00004337 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004338 if ((copy = end - offset) > 0) {
4339 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004340 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4341 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004342
4343 if (copy > len)
4344 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004345 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004346 skb_frag_off(frag) + offset - start);
David Howells716ea3a2007-04-02 20:19:53 -07004347 elt++;
4348 if (!(len -= copy))
4349 return elt;
4350 offset += copy;
4351 }
David S. Miller1a028e52007-04-27 15:21:23 -07004352 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004353 }
4354
David S. Millerfbb398a2009-06-09 00:18:59 -07004355 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004356 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004357
David S. Millerfbb398a2009-06-09 00:18:59 -07004358 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004359
David S. Millerfbb398a2009-06-09 00:18:59 -07004360 end = start + frag_iter->len;
4361 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004362 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4363 return -EMSGSIZE;
4364
David S. Millerfbb398a2009-06-09 00:18:59 -07004365 if (copy > len)
4366 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004367 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4368 copy, recursion_level + 1);
4369 if (unlikely(ret < 0))
4370 return ret;
4371 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004372 if ((len -= copy) == 0)
4373 return elt;
4374 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004375 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004376 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004377 }
4378 BUG_ON(len);
4379 return elt;
4380}
4381
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004382/**
4383 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4384 * @skb: Socket buffer containing the buffers to be mapped
4385 * @sg: The scatter-gather list to map into
4386 * @offset: The offset into the buffer's contents to start mapping
4387 * @len: Length of buffer space to be mapped
4388 *
4389 * Fill the specified scatter-gather list with mappings/pointers into a
4390 * region of the buffer space attached to a socket buffer. Returns either
4391 * the number of scatterlist items used, or -EMSGSIZE if the contents
4392 * could not fit.
4393 */
4394int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4395{
4396 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4397
4398 if (nsg <= 0)
4399 return nsg;
4400
4401 sg_mark_end(&sg[nsg - 1]);
4402
4403 return nsg;
4404}
4405EXPORT_SYMBOL_GPL(skb_to_sgvec);
4406
Fan Du25a91d82014-01-18 09:54:23 +08004407/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4408 * sglist without mark the sg which contain last skb data as the end.
4409 * So the caller can mannipulate sg list as will when padding new data after
4410 * the first call without calling sg_unmark_end to expend sg list.
4411 *
4412 * Scenario to use skb_to_sgvec_nomark:
4413 * 1. sg_init_table
4414 * 2. skb_to_sgvec_nomark(payload1)
4415 * 3. skb_to_sgvec_nomark(payload2)
4416 *
4417 * This is equivalent to:
4418 * 1. sg_init_table
4419 * 2. skb_to_sgvec(payload1)
4420 * 3. sg_unmark_end
4421 * 4. skb_to_sgvec(payload2)
4422 *
4423 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4424 * is more preferable.
4425 */
4426int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4427 int offset, int len)
4428{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004429 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004430}
4431EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4432
David S. Miller51c739d2007-10-30 21:29:29 -07004433
David S. Miller51c739d2007-10-30 21:29:29 -07004434
David Howells716ea3a2007-04-02 20:19:53 -07004435/**
4436 * skb_cow_data - Check that a socket buffer's data buffers are writable
4437 * @skb: The socket buffer to check.
4438 * @tailbits: Amount of trailing space to be added
4439 * @trailer: Returned pointer to the skb where the @tailbits space begins
4440 *
4441 * Make sure that the data buffers attached to a socket buffer are
4442 * writable. If they are not, private copies are made of the data buffers
4443 * and the socket buffer is set to use these instead.
4444 *
4445 * If @tailbits is given, make sure that there is space to write @tailbits
4446 * bytes of data beyond current end of socket buffer. @trailer will be
4447 * set to point to the skb in which this space begins.
4448 *
4449 * The number of scatterlist elements required to completely map the
4450 * COW'd and extended socket buffer will be returned.
4451 */
4452int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4453{
4454 int copyflag;
4455 int elt;
4456 struct sk_buff *skb1, **skb_p;
4457
4458 /* If skb is cloned or its head is paged, reallocate
4459 * head pulling out all the pages (pages are considered not writable
4460 * at the moment even if they are anonymous).
4461 */
4462 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
Miaohe Linc15fc192020-08-01 17:30:23 +08004463 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
David Howells716ea3a2007-04-02 20:19:53 -07004464 return -ENOMEM;
4465
4466 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004467 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004468 /* A little of trouble, not enough of space for trailer.
4469 * This should not happen, when stack is tuned to generate
4470 * good frames. OK, on miss we reallocate and reserve even more
4471 * space, 128 bytes is fair. */
4472
4473 if (skb_tailroom(skb) < tailbits &&
4474 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4475 return -ENOMEM;
4476
4477 /* Voila! */
4478 *trailer = skb;
4479 return 1;
4480 }
4481
4482 /* Misery. We are in troubles, going to mincer fragments... */
4483
4484 elt = 1;
4485 skb_p = &skb_shinfo(skb)->frag_list;
4486 copyflag = 0;
4487
4488 while ((skb1 = *skb_p) != NULL) {
4489 int ntail = 0;
4490
4491 /* The fragment is partially pulled by someone,
4492 * this can happen on input. Copy it and everything
4493 * after it. */
4494
4495 if (skb_shared(skb1))
4496 copyflag = 1;
4497
4498 /* If the skb is the last, worry about trailer. */
4499
4500 if (skb1->next == NULL && tailbits) {
4501 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004502 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004503 skb_tailroom(skb1) < tailbits)
4504 ntail = tailbits + 128;
4505 }
4506
4507 if (copyflag ||
4508 skb_cloned(skb1) ||
4509 ntail ||
4510 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004511 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004512 struct sk_buff *skb2;
4513
4514 /* Fuck, we are miserable poor guys... */
4515 if (ntail == 0)
4516 skb2 = skb_copy(skb1, GFP_ATOMIC);
4517 else
4518 skb2 = skb_copy_expand(skb1,
4519 skb_headroom(skb1),
4520 ntail,
4521 GFP_ATOMIC);
4522 if (unlikely(skb2 == NULL))
4523 return -ENOMEM;
4524
4525 if (skb1->sk)
4526 skb_set_owner_w(skb2, skb1->sk);
4527
4528 /* Looking around. Are we still alive?
4529 * OK, link new skb, drop old one */
4530
4531 skb2->next = skb1->next;
4532 *skb_p = skb2;
4533 kfree_skb(skb1);
4534 skb1 = skb2;
4535 }
4536 elt++;
4537 *trailer = skb1;
4538 skb_p = &skb1->next;
4539 }
4540
4541 return elt;
4542}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004543EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004544
Eric Dumazetb1faf562010-05-31 23:44:05 -07004545static void sock_rmem_free(struct sk_buff *skb)
4546{
4547 struct sock *sk = skb->sk;
4548
4549 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4550}
4551
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004552static void skb_set_err_queue(struct sk_buff *skb)
4553{
4554 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4555 * So, it is safe to (mis)use it to mark skbs on the error queue.
4556 */
4557 skb->pkt_type = PACKET_OUTGOING;
4558 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4559}
4560
Eric Dumazetb1faf562010-05-31 23:44:05 -07004561/*
4562 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4563 */
4564int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4565{
4566 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazetebb3b782019-10-10 20:17:44 -07004567 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
Eric Dumazetb1faf562010-05-31 23:44:05 -07004568 return -ENOMEM;
4569
4570 skb_orphan(skb);
4571 skb->sk = sk;
4572 skb->destructor = sock_rmem_free;
4573 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004574 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004575
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004576 /* before exiting rcu section, make sure dst is refcounted */
4577 skb_dst_force(skb);
4578
Eric Dumazetb1faf562010-05-31 23:44:05 -07004579 skb_queue_tail(&sk->sk_error_queue, skb);
4580 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004581 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004582 return 0;
4583}
4584EXPORT_SYMBOL(sock_queue_err_skb);
4585
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004586static bool is_icmp_err_skb(const struct sk_buff *skb)
4587{
4588 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4589 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4590}
4591
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004592struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4593{
4594 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004595 struct sk_buff *skb, *skb_next = NULL;
4596 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004597 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004598
Eric Dumazet997d5c32015-02-18 05:47:55 -08004599 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004600 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004601 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004602 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004603 if (icmp_next)
Willem de Bruijn985f7332020-11-26 10:12:20 -05004604 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004605 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004606 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004607
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004608 if (is_icmp_err_skb(skb) && !icmp_next)
4609 sk->sk_err = 0;
4610
4611 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004612 sk->sk_error_report(sk);
4613
4614 return skb;
4615}
4616EXPORT_SYMBOL(sock_dequeue_err_skb);
4617
Alexander Duyckcab41c42014-09-10 18:05:26 -04004618/**
4619 * skb_clone_sk - create clone of skb, and take reference to socket
4620 * @skb: the skb to clone
4621 *
4622 * This function creates a clone of a buffer that holds a reference on
4623 * sk_refcnt. Buffers created via this function are meant to be
4624 * returned using sock_queue_err_skb, or free via kfree_skb.
4625 *
4626 * When passing buffers allocated with this function to sock_queue_err_skb
4627 * it is necessary to wrap the call with sock_hold/sock_put in order to
4628 * prevent the socket from being released prior to being enqueued on
4629 * the sk_error_queue.
4630 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004631struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4632{
4633 struct sock *sk = skb->sk;
4634 struct sk_buff *clone;
4635
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004636 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004637 return NULL;
4638
4639 clone = skb_clone(skb, GFP_ATOMIC);
4640 if (!clone) {
4641 sock_put(sk);
4642 return NULL;
4643 }
4644
4645 clone->sk = sk;
4646 clone->destructor = sock_efree;
4647
4648 return clone;
4649}
4650EXPORT_SYMBOL(skb_clone_sk);
4651
Alexander Duyck37846ef2014-09-04 13:31:10 -04004652static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4653 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004654 int tstype,
4655 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004656{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004657 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004658 int err;
4659
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004660 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4661
Patrick Ohlyac45f602009-02-12 05:03:37 +00004662 serr = SKB_EXT_ERR(skb);
4663 memset(serr, 0, sizeof(*serr));
4664 serr->ee.ee_errno = ENOMSG;
4665 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004666 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004667 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004668 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004669 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004670 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004671 if (sk->sk_protocol == IPPROTO_TCP &&
4672 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004673 serr->ee.ee_data -= sk->sk_tskey;
4674 }
Eric Dumazet29030372010-05-29 00:20:48 -07004675
Patrick Ohlyac45f602009-02-12 05:03:37 +00004676 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004677
Patrick Ohlyac45f602009-02-12 05:03:37 +00004678 if (err)
4679 kfree_skb(skb);
4680}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004681
Willem de Bruijnb245be12015-01-30 13:29:32 -05004682static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4683{
4684 bool ret;
4685
4686 if (likely(sysctl_tstamp_allow_data || tsonly))
4687 return true;
4688
4689 read_lock_bh(&sk->sk_callback_lock);
4690 ret = sk->sk_socket && sk->sk_socket->file &&
4691 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4692 read_unlock_bh(&sk->sk_callback_lock);
4693 return ret;
4694}
4695
Alexander Duyck37846ef2014-09-04 13:31:10 -04004696void skb_complete_tx_timestamp(struct sk_buff *skb,
4697 struct skb_shared_hwtstamps *hwtstamps)
4698{
4699 struct sock *sk = skb->sk;
4700
Willem de Bruijnb245be12015-01-30 13:29:32 -05004701 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004702 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004703
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004704 /* Take a reference to prevent skb_orphan() from freeing the socket,
4705 * but only if the socket refcount is not zero.
4706 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004707 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004708 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004709 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004710 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004711 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004712 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004713
4714err:
4715 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004716}
4717EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4718
4719void __skb_tstamp_tx(struct sk_buff *orig_skb,
4720 struct skb_shared_hwtstamps *hwtstamps,
4721 struct sock *sk, int tstype)
4722{
4723 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004724 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004725
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004726 if (!sk)
4727 return;
4728
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004729 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4730 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4731 return;
4732
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004733 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4734 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004735 return;
4736
Francis Yan1c885802016-11-27 23:07:18 -08004737 if (tsonly) {
4738#ifdef CONFIG_INET
4739 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4740 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004741 sk->sk_type == SOCK_STREAM) {
Yousuk Seung48040792020-07-30 15:44:40 -07004742 skb = tcp_get_timestamping_opt_stats(sk, orig_skb);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004743 opt_stats = true;
4744 } else
Francis Yan1c885802016-11-27 23:07:18 -08004745#endif
4746 skb = alloc_skb(0, GFP_ATOMIC);
4747 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004748 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004749 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004750 if (!skb)
4751 return;
4752
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004753 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004754 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4755 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004756 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4757 }
4758
4759 if (hwtstamps)
4760 *skb_hwtstamps(skb) = *hwtstamps;
4761 else
4762 skb->tstamp = ktime_get_real();
4763
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004764 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004765}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004766EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4767
4768void skb_tstamp_tx(struct sk_buff *orig_skb,
4769 struct skb_shared_hwtstamps *hwtstamps)
4770{
4771 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4772 SCM_TSTAMP_SND);
4773}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004774EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4775
Johannes Berg6e3e9392011-11-09 10:15:42 +01004776void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4777{
4778 struct sock *sk = skb->sk;
4779 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004780 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004781
4782 skb->wifi_acked_valid = 1;
4783 skb->wifi_acked = acked;
4784
4785 serr = SKB_EXT_ERR(skb);
4786 memset(serr, 0, sizeof(*serr));
4787 serr->ee.ee_errno = ENOMSG;
4788 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4789
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004790 /* Take a reference to prevent skb_orphan() from freeing the socket,
4791 * but only if the socket refcount is not zero.
4792 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004793 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004794 err = sock_queue_err_skb(sk, skb);
4795 sock_put(sk);
4796 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004797 if (err)
4798 kfree_skb(skb);
4799}
4800EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4801
Rusty Russellf35d9d82008-02-04 23:49:54 -05004802/**
4803 * skb_partial_csum_set - set up and verify partial csum values for packet
4804 * @skb: the skb to set
4805 * @start: the number of bytes after skb->data to start checksumming.
4806 * @off: the offset from start to place the checksum.
4807 *
4808 * For untrusted partially-checksummed packets, we need to make sure the values
4809 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4810 *
4811 * This function checks and sets those values and skb->ip_summed: if this
4812 * returns false you should drop the packet.
4813 */
4814bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4815{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004816 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4817 u32 csum_start = skb_headroom(skb) + (u32)start;
4818
4819 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4820 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4821 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004822 return false;
4823 }
4824 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004825 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004826 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004827 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004828 return true;
4829}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004830EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004831
Paul Durranted1f50c2014-01-09 10:02:46 +00004832static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4833 unsigned int max)
4834{
4835 if (skb_headlen(skb) >= len)
4836 return 0;
4837
4838 /* If we need to pullup then pullup to the max, so we
4839 * won't need to do it again.
4840 */
4841 if (max > skb->len)
4842 max = skb->len;
4843
4844 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4845 return -ENOMEM;
4846
4847 if (skb_headlen(skb) < len)
4848 return -EPROTO;
4849
4850 return 0;
4851}
4852
Jan Beulichf9708b42014-03-11 13:56:05 +00004853#define MAX_TCP_HDR_LEN (15 * 4)
4854
4855static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4856 typeof(IPPROTO_IP) proto,
4857 unsigned int off)
4858{
Kees Cook161d1792020-02-19 22:23:04 -08004859 int err;
Jan Beulichf9708b42014-03-11 13:56:05 +00004860
Kees Cook161d1792020-02-19 22:23:04 -08004861 switch (proto) {
Jan Beulichf9708b42014-03-11 13:56:05 +00004862 case IPPROTO_TCP:
4863 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4864 off + MAX_TCP_HDR_LEN);
4865 if (!err && !skb_partial_csum_set(skb, off,
4866 offsetof(struct tcphdr,
4867 check)))
4868 err = -EPROTO;
4869 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4870
4871 case IPPROTO_UDP:
4872 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4873 off + sizeof(struct udphdr));
4874 if (!err && !skb_partial_csum_set(skb, off,
4875 offsetof(struct udphdr,
4876 check)))
4877 err = -EPROTO;
4878 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4879 }
4880
4881 return ERR_PTR(-EPROTO);
4882}
4883
Paul Durranted1f50c2014-01-09 10:02:46 +00004884/* This value should be large enough to cover a tagged ethernet header plus
4885 * maximally sized IP and TCP or UDP headers.
4886 */
4887#define MAX_IP_HDR_LEN 128
4888
Jan Beulichf9708b42014-03-11 13:56:05 +00004889static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004890{
4891 unsigned int off;
4892 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004893 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004894 int err;
4895
4896 fragment = false;
4897
4898 err = skb_maybe_pull_tail(skb,
4899 sizeof(struct iphdr),
4900 MAX_IP_HDR_LEN);
4901 if (err < 0)
4902 goto out;
4903
Miaohe Lin11f920d2020-08-06 19:57:18 +08004904 if (ip_is_fragment(ip_hdr(skb)))
Paul Durranted1f50c2014-01-09 10:02:46 +00004905 fragment = true;
4906
4907 off = ip_hdrlen(skb);
4908
4909 err = -EPROTO;
4910
4911 if (fragment)
4912 goto out;
4913
Jan Beulichf9708b42014-03-11 13:56:05 +00004914 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4915 if (IS_ERR(csum))
4916 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004917
Jan Beulichf9708b42014-03-11 13:56:05 +00004918 if (recalculate)
4919 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4920 ip_hdr(skb)->daddr,
4921 skb->len - off,
4922 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004923 err = 0;
4924
4925out:
4926 return err;
4927}
4928
4929/* This value should be large enough to cover a tagged ethernet header plus
4930 * an IPv6 header, all options, and a maximal TCP or UDP header.
4931 */
4932#define MAX_IPV6_HDR_LEN 256
4933
4934#define OPT_HDR(type, skb, off) \
4935 (type *)(skb_network_header(skb) + (off))
4936
4937static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4938{
4939 int err;
4940 u8 nexthdr;
4941 unsigned int off;
4942 unsigned int len;
4943 bool fragment;
4944 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004945 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004946
4947 fragment = false;
4948 done = false;
4949
4950 off = sizeof(struct ipv6hdr);
4951
4952 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4953 if (err < 0)
4954 goto out;
4955
4956 nexthdr = ipv6_hdr(skb)->nexthdr;
4957
4958 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4959 while (off <= len && !done) {
4960 switch (nexthdr) {
4961 case IPPROTO_DSTOPTS:
4962 case IPPROTO_HOPOPTS:
4963 case IPPROTO_ROUTING: {
4964 struct ipv6_opt_hdr *hp;
4965
4966 err = skb_maybe_pull_tail(skb,
4967 off +
4968 sizeof(struct ipv6_opt_hdr),
4969 MAX_IPV6_HDR_LEN);
4970 if (err < 0)
4971 goto out;
4972
4973 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4974 nexthdr = hp->nexthdr;
4975 off += ipv6_optlen(hp);
4976 break;
4977 }
4978 case IPPROTO_AH: {
4979 struct ip_auth_hdr *hp;
4980
4981 err = skb_maybe_pull_tail(skb,
4982 off +
4983 sizeof(struct ip_auth_hdr),
4984 MAX_IPV6_HDR_LEN);
4985 if (err < 0)
4986 goto out;
4987
4988 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4989 nexthdr = hp->nexthdr;
4990 off += ipv6_authlen(hp);
4991 break;
4992 }
4993 case IPPROTO_FRAGMENT: {
4994 struct frag_hdr *hp;
4995
4996 err = skb_maybe_pull_tail(skb,
4997 off +
4998 sizeof(struct frag_hdr),
4999 MAX_IPV6_HDR_LEN);
5000 if (err < 0)
5001 goto out;
5002
5003 hp = OPT_HDR(struct frag_hdr, skb, off);
5004
5005 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5006 fragment = true;
5007
5008 nexthdr = hp->nexthdr;
5009 off += sizeof(struct frag_hdr);
5010 break;
5011 }
5012 default:
5013 done = true;
5014 break;
5015 }
5016 }
5017
5018 err = -EPROTO;
5019
5020 if (!done || fragment)
5021 goto out;
5022
Jan Beulichf9708b42014-03-11 13:56:05 +00005023 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5024 if (IS_ERR(csum))
5025 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005026
Jan Beulichf9708b42014-03-11 13:56:05 +00005027 if (recalculate)
5028 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5029 &ipv6_hdr(skb)->daddr,
5030 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005031 err = 0;
5032
5033out:
5034 return err;
5035}
5036
5037/**
5038 * skb_checksum_setup - set up partial checksum offset
5039 * @skb: the skb to set up
5040 * @recalculate: if true the pseudo-header checksum will be recalculated
5041 */
5042int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5043{
5044 int err;
5045
5046 switch (skb->protocol) {
5047 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00005048 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00005049 break;
5050
5051 case htons(ETH_P_IPV6):
5052 err = skb_checksum_setup_ipv6(skb, recalculate);
5053 break;
5054
5055 default:
5056 err = -EPROTO;
5057 break;
5058 }
5059
5060 return err;
5061}
5062EXPORT_SYMBOL(skb_checksum_setup);
5063
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005064/**
5065 * skb_checksum_maybe_trim - maybe trims the given skb
5066 * @skb: the skb to check
5067 * @transport_len: the data length beyond the network header
5068 *
5069 * Checks whether the given skb has data beyond the given transport length.
5070 * If so, returns a cloned skb trimmed to this transport length.
5071 * Otherwise returns the provided skb. Returns NULL in error cases
5072 * (e.g. transport_len exceeds skb length or out-of-memory).
5073 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005074 * Caller needs to set the skb transport header and free any returned skb if it
5075 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005076 */
5077static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5078 unsigned int transport_len)
5079{
5080 struct sk_buff *skb_chk;
5081 unsigned int len = skb_transport_offset(skb) + transport_len;
5082 int ret;
5083
Linus Lüssinga5169932015-08-13 05:54:07 +02005084 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005085 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02005086 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005087 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005088
5089 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005090 if (!skb_chk)
5091 return NULL;
5092
5093 ret = pskb_trim_rcsum(skb_chk, len);
5094 if (ret) {
5095 kfree_skb(skb_chk);
5096 return NULL;
5097 }
5098
5099 return skb_chk;
5100}
5101
5102/**
5103 * skb_checksum_trimmed - validate checksum of an skb
5104 * @skb: the skb to check
5105 * @transport_len: the data length beyond the network header
5106 * @skb_chkf: checksum function to use
5107 *
5108 * Applies the given checksum function skb_chkf to the provided skb.
5109 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5110 *
5111 * If the skb has data beyond the given transport length, then a
5112 * trimmed & cloned skb is checked and returned.
5113 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005114 * Caller needs to set the skb transport header and free any returned skb if it
5115 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005116 */
5117struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5118 unsigned int transport_len,
5119 __sum16(*skb_chkf)(struct sk_buff *skb))
5120{
5121 struct sk_buff *skb_chk;
5122 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02005123 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005124
5125 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5126 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02005127 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005128
Linus Lüssinga5169932015-08-13 05:54:07 +02005129 if (!pskb_may_pull(skb_chk, offset))
5130 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005131
Linus Lüssing9b368812016-02-24 04:21:42 +01005132 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005133 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01005134 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005135
Linus Lüssinga5169932015-08-13 05:54:07 +02005136 if (ret)
5137 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005138
5139 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02005140
5141err:
5142 if (skb_chk && skb_chk != skb)
5143 kfree_skb(skb_chk);
5144
5145 return NULL;
5146
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005147}
5148EXPORT_SYMBOL(skb_checksum_trimmed);
5149
Ben Hutchings4497b072008-06-19 16:22:28 -07005150void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5151{
Joe Perchese87cc472012-05-13 21:56:26 +00005152 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5153 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07005154}
Ben Hutchings4497b072008-06-19 16:22:28 -07005155EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005156
5157void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5158{
Eric Dumazet3d861f62012-10-22 09:03:40 +00005159 if (head_stolen) {
5160 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005161 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005162 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005163 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005164 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005165}
5166EXPORT_SYMBOL(kfree_skb_partial);
5167
5168/**
5169 * skb_try_coalesce - try to merge skb to prior one
5170 * @to: prior buffer
5171 * @from: buffer to add
5172 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00005173 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005174 */
5175bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5176 bool *fragstolen, int *delta_truesize)
5177{
Eric Dumazetc818fa92017-10-04 10:48:35 -07005178 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005179 int i, delta, len = from->len;
5180
5181 *fragstolen = false;
5182
5183 if (skb_cloned(to))
5184 return false;
5185
5186 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005187 if (len)
5188 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005189 *delta_truesize = 0;
5190 return true;
5191 }
5192
Eric Dumazetc818fa92017-10-04 10:48:35 -07005193 to_shinfo = skb_shinfo(to);
5194 from_shinfo = skb_shinfo(from);
5195 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005196 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005197 if (skb_zcopy(to) || skb_zcopy(from))
5198 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005199
5200 if (skb_headlen(from) != 0) {
5201 struct page *page;
5202 unsigned int offset;
5203
Eric Dumazetc818fa92017-10-04 10:48:35 -07005204 if (to_shinfo->nr_frags +
5205 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005206 return false;
5207
5208 if (skb_head_is_locked(from))
5209 return false;
5210
5211 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5212
5213 page = virt_to_head_page(from->head);
5214 offset = from->data - (unsigned char *)page_address(page);
5215
Eric Dumazetc818fa92017-10-04 10:48:35 -07005216 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005217 page, offset, skb_headlen(from));
5218 *fragstolen = true;
5219 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005220 if (to_shinfo->nr_frags +
5221 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005222 return false;
5223
Weiping Panf4b549a2012-09-28 20:15:30 +00005224 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005225 }
5226
5227 WARN_ON_ONCE(delta < len);
5228
Eric Dumazetc818fa92017-10-04 10:48:35 -07005229 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5230 from_shinfo->frags,
5231 from_shinfo->nr_frags * sizeof(skb_frag_t));
5232 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005233
5234 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005235 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005236
Li RongQing8ea853f2012-09-18 16:53:21 +00005237 /* if the skb is not cloned this does nothing
5238 * since we set nr_frags to 0.
5239 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005240 for (i = 0; i < from_shinfo->nr_frags; i++)
5241 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005242
5243 to->truesize += delta;
5244 to->len += len;
5245 to->data_len += len;
5246
5247 *delta_truesize = delta;
5248 return true;
5249}
5250EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005251
5252/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005253 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005254 *
5255 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005256 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005257 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005258 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5259 * into/from a tunnel. Some information have to be cleared during these
5260 * operations.
5261 * skb_scrub_packet can also be used to clean a skb before injecting it in
5262 * another namespace (@xnet == true). We have to clear all information in the
5263 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005264 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005265void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005266{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005267 skb->pkt_type = PACKET_HOST;
5268 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005269 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005270 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005271 skb_ext_reset(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02005272 nf_reset_ct(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005273 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005274
Petr Machata6f9a5062018-11-19 16:11:07 +00005275#ifdef CONFIG_NET_SWITCHDEV
5276 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005277 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005278#endif
5279
Herbert Xu213dd742015-04-16 09:03:27 +08005280 if (!xnet)
5281 return;
5282
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005283 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005284 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005285 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005286}
5287EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005288
5289/**
5290 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5291 *
5292 * @skb: GSO skb
5293 *
5294 * skb_gso_transport_seglen is used to determine the real size of the
5295 * individual segments, including Layer4 headers (TCP/UDP).
5296 *
5297 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5298 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005299static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005300{
5301 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005302 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005303
Florian Westphalf993bc22014-10-20 13:49:18 +02005304 if (skb->encapsulation) {
5305 thlen = skb_inner_transport_header(skb) -
5306 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005307
Florian Westphalf993bc22014-10-20 13:49:18 +02005308 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5309 thlen += inner_tcp_hdrlen(skb);
5310 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5311 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005312 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005313 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005314 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5315 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005316 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005317 /* UFO sets gso_size to the size of the fragmentation
5318 * payload, i.e. the size of the L4 (UDP) header is already
5319 * accounted for.
5320 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005321 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005322}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005323
5324/**
5325 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5326 *
5327 * @skb: GSO skb
5328 *
5329 * skb_gso_network_seglen is used to determine the real size of the
5330 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5331 *
5332 * The MAC/L2 header is not accounted for.
5333 */
5334static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5335{
5336 unsigned int hdr_len = skb_transport_header(skb) -
5337 skb_network_header(skb);
5338
5339 return hdr_len + skb_gso_transport_seglen(skb);
5340}
5341
5342/**
5343 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5344 *
5345 * @skb: GSO skb
5346 *
5347 * skb_gso_mac_seglen is used to determine the real size of the
5348 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5349 * headers (TCP/UDP).
5350 */
5351static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5352{
5353 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5354
5355 return hdr_len + skb_gso_transport_seglen(skb);
5356}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005357
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005358/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005359 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5360 *
5361 * There are a couple of instances where we have a GSO skb, and we
5362 * want to determine what size it would be after it is segmented.
5363 *
5364 * We might want to check:
5365 * - L3+L4+payload size (e.g. IP forwarding)
5366 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5367 *
5368 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5369 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005370 * @skb: GSO skb
5371 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005372 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5373 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5374 *
5375 * @max_len: The maximum permissible length.
5376 *
5377 * Returns true if the segmented length <= max length.
5378 */
5379static inline bool skb_gso_size_check(const struct sk_buff *skb,
5380 unsigned int seg_len,
5381 unsigned int max_len) {
5382 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5383 const struct sk_buff *iter;
5384
5385 if (shinfo->gso_size != GSO_BY_FRAGS)
5386 return seg_len <= max_len;
5387
5388 /* Undo this so we can re-use header sizes */
5389 seg_len -= GSO_BY_FRAGS;
5390
5391 skb_walk_frags(skb, iter) {
5392 if (seg_len + skb_headlen(iter) > max_len)
5393 return false;
5394 }
5395
5396 return true;
5397}
5398
5399/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005400 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005401 *
5402 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005403 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005404 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005405 * skb_gso_validate_network_len validates if a given skb will fit a
5406 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5407 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005408 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005409bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005410{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005411 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005412}
Daniel Axtens779b7932018-03-01 17:13:37 +11005413EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005414
Daniel Axtens2b16f042018-01-31 14:15:33 +11005415/**
5416 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5417 *
5418 * @skb: GSO skb
5419 * @len: length to validate against
5420 *
5421 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5422 * length once split, including L2, L3 and L4 headers and the payload.
5423 */
5424bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5425{
5426 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5427}
5428EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5429
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005430static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5431{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005432 int mac_len, meta_len;
5433 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005434
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005435 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5436 kfree_skb(skb);
5437 return NULL;
5438 }
5439
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005440 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005441 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5442 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5443 mac_len - VLAN_HLEN - ETH_TLEN);
5444 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005445
5446 meta_len = skb_metadata_len(skb);
5447 if (meta_len) {
5448 meta = skb_metadata_end(skb) - meta_len;
5449 memmove(meta + VLAN_HLEN, meta, meta_len);
5450 }
5451
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005452 skb->mac_header += VLAN_HLEN;
5453 return skb;
5454}
5455
5456struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5457{
5458 struct vlan_hdr *vhdr;
5459 u16 vlan_tci;
5460
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005461 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005462 /* vlan_tci is already set-up so leave this for another time */
5463 return skb;
5464 }
5465
5466 skb = skb_share_check(skb, GFP_ATOMIC);
5467 if (unlikely(!skb))
5468 goto err_free;
Miaohe Lin55eff0e2020-08-15 04:44:31 -04005469 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5470 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005471 goto err_free;
5472
5473 vhdr = (struct vlan_hdr *)skb->data;
5474 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5475 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5476
5477 skb_pull_rcsum(skb, VLAN_HLEN);
5478 vlan_set_encap_proto(skb, vhdr);
5479
5480 skb = skb_reorder_vlan_header(skb);
5481 if (unlikely(!skb))
5482 goto err_free;
5483
5484 skb_reset_network_header(skb);
Alexander Lobakin8be33ec2020-11-09 23:47:23 +00005485 if (!skb_transport_header_was_set(skb))
5486 skb_reset_transport_header(skb);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005487 skb_reset_mac_len(skb);
5488
5489 return skb;
5490
5491err_free:
5492 kfree_skb(skb);
5493 return NULL;
5494}
5495EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005496
Jiri Pirkoe2195122014-11-19 14:05:01 +01005497int skb_ensure_writable(struct sk_buff *skb, int write_len)
5498{
5499 if (!pskb_may_pull(skb, write_len))
5500 return -ENOMEM;
5501
5502 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5503 return 0;
5504
5505 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5506}
5507EXPORT_SYMBOL(skb_ensure_writable);
5508
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005509/* remove VLAN header from packet and update csum accordingly.
5510 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5511 */
5512int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005513{
5514 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005515 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005516 int err;
5517
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005518 if (WARN_ONCE(offset,
5519 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5520 offset)) {
5521 return -EINVAL;
5522 }
5523
Jiri Pirko93515d52014-11-19 14:05:02 +01005524 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5525 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005526 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005527
5528 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5529
5530 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5531 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5532
5533 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5534 __skb_pull(skb, VLAN_HLEN);
5535
5536 vlan_set_encap_proto(skb, vhdr);
5537 skb->mac_header += VLAN_HLEN;
5538
5539 if (skb_network_offset(skb) < ETH_HLEN)
5540 skb_set_network_header(skb, ETH_HLEN);
5541
5542 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005543
5544 return err;
5545}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005546EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005547
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005548/* Pop a vlan tag either from hwaccel or from payload.
5549 * Expects skb->data at mac header.
5550 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005551int skb_vlan_pop(struct sk_buff *skb)
5552{
5553 u16 vlan_tci;
5554 __be16 vlan_proto;
5555 int err;
5556
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005557 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005558 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005559 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005560 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005561 return 0;
5562
5563 err = __skb_vlan_pop(skb, &vlan_tci);
5564 if (err)
5565 return err;
5566 }
5567 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005568 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005569 return 0;
5570
5571 vlan_proto = skb->protocol;
5572 err = __skb_vlan_pop(skb, &vlan_tci);
5573 if (unlikely(err))
5574 return err;
5575
5576 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5577 return 0;
5578}
5579EXPORT_SYMBOL(skb_vlan_pop);
5580
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005581/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5582 * Expects skb->data at mac header.
5583 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005584int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5585{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005586 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005587 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005588 int err;
5589
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005590 if (WARN_ONCE(offset,
5591 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5592 offset)) {
5593 return -EINVAL;
5594 }
5595
Jiri Pirko93515d52014-11-19 14:05:02 +01005596 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005597 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005598 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005599 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005600
Jiri Pirko93515d52014-11-19 14:05:02 +01005601 skb->protocol = skb->vlan_proto;
5602 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005603
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005604 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005605 }
5606 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5607 return 0;
5608}
5609EXPORT_SYMBOL(skb_vlan_push);
5610
Guillaume Nault19fbcb32020-10-03 00:44:28 +02005611/**
5612 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5613 *
5614 * @skb: Socket buffer to modify
5615 *
5616 * Drop the Ethernet header of @skb.
5617 *
5618 * Expects that skb->data points to the mac header and that no VLAN tags are
5619 * present.
5620 *
5621 * Returns 0 on success, -errno otherwise.
5622 */
5623int skb_eth_pop(struct sk_buff *skb)
5624{
5625 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5626 skb_network_offset(skb) < ETH_HLEN)
5627 return -EPROTO;
5628
5629 skb_pull_rcsum(skb, ETH_HLEN);
5630 skb_reset_mac_header(skb);
5631 skb_reset_mac_len(skb);
5632
5633 return 0;
5634}
5635EXPORT_SYMBOL(skb_eth_pop);
5636
5637/**
5638 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5639 *
5640 * @skb: Socket buffer to modify
5641 * @dst: Destination MAC address of the new header
5642 * @src: Source MAC address of the new header
5643 *
5644 * Prepend @skb with a new Ethernet header.
5645 *
5646 * Expects that skb->data points to the mac header, which must be empty.
5647 *
5648 * Returns 0 on success, -errno otherwise.
5649 */
5650int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5651 const unsigned char *src)
5652{
5653 struct ethhdr *eth;
5654 int err;
5655
5656 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5657 return -EPROTO;
5658
5659 err = skb_cow_head(skb, sizeof(*eth));
5660 if (err < 0)
5661 return err;
5662
5663 skb_push(skb, sizeof(*eth));
5664 skb_reset_mac_header(skb);
5665 skb_reset_mac_len(skb);
5666
5667 eth = eth_hdr(skb);
5668 ether_addr_copy(eth->h_dest, dst);
5669 ether_addr_copy(eth->h_source, src);
5670 eth->h_proto = skb->protocol;
5671
5672 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5673
5674 return 0;
5675}
5676EXPORT_SYMBOL(skb_eth_push);
5677
John Hurley8822e272019-07-07 15:01:54 +01005678/* Update the ethertype of hdr and the skb csum value if required. */
5679static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5680 __be16 ethertype)
5681{
5682 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5683 __be16 diff[] = { ~hdr->h_proto, ethertype };
5684
5685 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5686 }
5687
5688 hdr->h_proto = ethertype;
5689}
5690
5691/**
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305692 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5693 * the packet
John Hurley8822e272019-07-07 15:01:54 +01005694 *
5695 * @skb: buffer
5696 * @mpls_lse: MPLS label stack entry to push
5697 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
Davide Carattifa4e0f82019-10-12 13:55:07 +02005698 * @mac_len: length of the MAC header
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305699 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5700 * ethernet
John Hurley8822e272019-07-07 15:01:54 +01005701 *
5702 * Expects skb->data at mac header.
5703 *
5704 * Returns 0 on success, -errno otherwise.
5705 */
Davide Carattifa4e0f82019-10-12 13:55:07 +02005706int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
Martin Varghesed04ac222019-12-05 05:57:22 +05305707 int mac_len, bool ethernet)
John Hurley8822e272019-07-07 15:01:54 +01005708{
5709 struct mpls_shim_hdr *lse;
5710 int err;
5711
5712 if (unlikely(!eth_p_mpls(mpls_proto)))
5713 return -EINVAL;
5714
5715 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5716 if (skb->encapsulation)
5717 return -EINVAL;
5718
5719 err = skb_cow_head(skb, MPLS_HLEN);
5720 if (unlikely(err))
5721 return err;
5722
5723 if (!skb->inner_protocol) {
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305724 skb_set_inner_network_header(skb, skb_network_offset(skb));
John Hurley8822e272019-07-07 15:01:54 +01005725 skb_set_inner_protocol(skb, skb->protocol);
5726 }
5727
5728 skb_push(skb, MPLS_HLEN);
5729 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005730 mac_len);
John Hurley8822e272019-07-07 15:01:54 +01005731 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005732 skb_set_network_header(skb, mac_len);
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305733 skb_reset_mac_len(skb);
John Hurley8822e272019-07-07 15:01:54 +01005734
5735 lse = mpls_hdr(skb);
5736 lse->label_stack_entry = mpls_lse;
5737 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5738
Guillaume Nault4296adc2020-10-02 21:53:08 +02005739 if (ethernet && mac_len >= ETH_HLEN)
John Hurley8822e272019-07-07 15:01:54 +01005740 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5741 skb->protocol = mpls_proto;
5742
5743 return 0;
5744}
5745EXPORT_SYMBOL_GPL(skb_mpls_push);
5746
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005747/**
John Hurleyed246ce2019-07-07 15:01:55 +01005748 * skb_mpls_pop() - pop the outermost MPLS header
5749 *
5750 * @skb: buffer
5751 * @next_proto: ethertype of header after popped MPLS header
Davide Carattifa4e0f82019-10-12 13:55:07 +02005752 * @mac_len: length of the MAC header
Martin Varghese76f99f92019-12-21 08:50:23 +05305753 * @ethernet: flag to indicate if the packet is ethernet
John Hurleyed246ce2019-07-07 15:01:55 +01005754 *
5755 * Expects skb->data at mac header.
5756 *
5757 * Returns 0 on success, -errno otherwise.
5758 */
Martin Varghese040b5cf2019-12-02 10:49:51 +05305759int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5760 bool ethernet)
John Hurleyed246ce2019-07-07 15:01:55 +01005761{
5762 int err;
5763
5764 if (unlikely(!eth_p_mpls(skb->protocol)))
Davide Carattidedc5a02019-10-12 13:55:06 +02005765 return 0;
John Hurleyed246ce2019-07-07 15:01:55 +01005766
Davide Carattifa4e0f82019-10-12 13:55:07 +02005767 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
John Hurleyed246ce2019-07-07 15:01:55 +01005768 if (unlikely(err))
5769 return err;
5770
5771 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5772 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005773 mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005774
5775 __skb_pull(skb, MPLS_HLEN);
5776 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005777 skb_set_network_header(skb, mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005778
Guillaume Nault4296adc2020-10-02 21:53:08 +02005779 if (ethernet && mac_len >= ETH_HLEN) {
John Hurleyed246ce2019-07-07 15:01:55 +01005780 struct ethhdr *hdr;
5781
5782 /* use mpls_hdr() to get ethertype to account for VLANs. */
5783 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5784 skb_mod_eth_type(skb, hdr, next_proto);
5785 }
5786 skb->protocol = next_proto;
5787
5788 return 0;
5789}
5790EXPORT_SYMBOL_GPL(skb_mpls_pop);
5791
5792/**
John Hurleyd27cf5c2019-07-07 15:01:56 +01005793 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5794 *
5795 * @skb: buffer
5796 * @mpls_lse: new MPLS label stack entry to update to
5797 *
5798 * Expects skb->data at mac header.
5799 *
5800 * Returns 0 on success, -errno otherwise.
5801 */
5802int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5803{
5804 int err;
5805
5806 if (unlikely(!eth_p_mpls(skb->protocol)))
5807 return -EINVAL;
5808
5809 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5810 if (unlikely(err))
5811 return err;
5812
5813 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5814 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5815
5816 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5817 }
5818
5819 mpls_hdr(skb)->label_stack_entry = mpls_lse;
5820
5821 return 0;
5822}
5823EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5824
5825/**
John Hurley2a2ea502019-07-07 15:01:57 +01005826 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
5827 *
5828 * @skb: buffer
5829 *
5830 * Expects skb->data at mac header.
5831 *
5832 * Returns 0 on success, -errno otherwise.
5833 */
5834int skb_mpls_dec_ttl(struct sk_buff *skb)
5835{
5836 u32 lse;
5837 u8 ttl;
5838
5839 if (unlikely(!eth_p_mpls(skb->protocol)))
5840 return -EINVAL;
5841
Davide Caratti13de4ed2020-12-03 10:58:21 +01005842 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
5843 return -ENOMEM;
5844
John Hurley2a2ea502019-07-07 15:01:57 +01005845 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
5846 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
5847 if (!--ttl)
5848 return -EINVAL;
5849
5850 lse &= ~MPLS_LS_TTL_MASK;
5851 lse |= ttl << MPLS_LS_TTL_SHIFT;
5852
5853 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
5854}
5855EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
5856
5857/**
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005858 * alloc_skb_with_frags - allocate skb with page frags
5859 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005860 * @header_len: size of linear part
5861 * @data_len: needed length in frags
5862 * @max_page_order: max page order desired.
5863 * @errcode: pointer to error code if any
5864 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005865 *
5866 * This can be used to allocate a paged skb, given a maximal order for frags.
5867 */
5868struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5869 unsigned long data_len,
5870 int max_page_order,
5871 int *errcode,
5872 gfp_t gfp_mask)
5873{
5874 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5875 unsigned long chunk;
5876 struct sk_buff *skb;
5877 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005878 int i;
5879
5880 *errcode = -EMSGSIZE;
5881 /* Note this test could be relaxed, if we succeed to allocate
5882 * high order pages...
5883 */
5884 if (npages > MAX_SKB_FRAGS)
5885 return NULL;
5886
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005887 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005888 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005889 if (!skb)
5890 return NULL;
5891
5892 skb->truesize += npages << PAGE_SHIFT;
5893
5894 for (i = 0; npages > 0; i++) {
5895 int order = max_page_order;
5896
5897 while (order) {
5898 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005899 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005900 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005901 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005902 order);
5903 if (page)
5904 goto fill_page;
5905 /* Do not retry other high order allocations */
5906 order = 1;
5907 max_page_order = 0;
5908 }
5909 order--;
5910 }
5911 page = alloc_page(gfp_mask);
5912 if (!page)
5913 goto failure;
5914fill_page:
5915 chunk = min_t(unsigned long, data_len,
5916 PAGE_SIZE << order);
5917 skb_fill_page_desc(skb, i, page, 0, chunk);
5918 data_len -= chunk;
5919 npages -= 1 << order;
5920 }
5921 return skb;
5922
5923failure:
5924 kfree_skb(skb);
5925 return NULL;
5926}
5927EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005928
5929/* carve out the first off bytes from skb when off < headlen */
5930static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5931 const int headlen, gfp_t gfp_mask)
5932{
5933 int i;
5934 int size = skb_end_offset(skb);
5935 int new_hlen = headlen - off;
5936 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005937
5938 size = SKB_DATA_ALIGN(size);
5939
5940 if (skb_pfmemalloc(skb))
5941 gfp_mask |= __GFP_MEMALLOC;
5942 data = kmalloc_reserve(size +
5943 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5944 gfp_mask, NUMA_NO_NODE, NULL);
5945 if (!data)
5946 return -ENOMEM;
5947
5948 size = SKB_WITH_OVERHEAD(ksize(data));
5949
5950 /* Copy real data, and all frags */
5951 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5952 skb->len -= off;
5953
5954 memcpy((struct skb_shared_info *)(data + size),
5955 skb_shinfo(skb),
5956 offsetof(struct skb_shared_info,
5957 frags[skb_shinfo(skb)->nr_frags]));
5958 if (skb_cloned(skb)) {
5959 /* drop the old head gracefully */
5960 if (skb_orphan_frags(skb, gfp_mask)) {
5961 kfree(data);
5962 return -ENOMEM;
5963 }
5964 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5965 skb_frag_ref(skb, i);
5966 if (skb_has_frag_list(skb))
5967 skb_clone_fraglist(skb);
5968 skb_release_data(skb);
5969 } else {
5970 /* we can reuse existing recount- all we did was
5971 * relocate values
5972 */
5973 skb_free_head(skb);
5974 }
5975
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005976 skb->head = data;
5977 skb->data = data;
5978 skb->head_frag = 0;
5979#ifdef NET_SKBUFF_DATA_USES_OFFSET
5980 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005981#else
5982 skb->end = skb->head + size;
5983#endif
5984 skb_set_tail_pointer(skb, skb_headlen(skb));
5985 skb_headers_offset_update(skb, 0);
5986 skb->cloned = 0;
5987 skb->hdr_len = 0;
5988 skb->nohdr = 0;
5989 atomic_set(&skb_shinfo(skb)->dataref, 1);
5990
5991 return 0;
5992}
5993
5994static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5995
5996/* carve out the first eat bytes from skb's frag_list. May recurse into
5997 * pskb_carve()
5998 */
5999static int pskb_carve_frag_list(struct sk_buff *skb,
6000 struct skb_shared_info *shinfo, int eat,
6001 gfp_t gfp_mask)
6002{
6003 struct sk_buff *list = shinfo->frag_list;
6004 struct sk_buff *clone = NULL;
6005 struct sk_buff *insp = NULL;
6006
6007 do {
6008 if (!list) {
6009 pr_err("Not enough bytes to eat. Want %d\n", eat);
6010 return -EFAULT;
6011 }
6012 if (list->len <= eat) {
6013 /* Eaten as whole. */
6014 eat -= list->len;
6015 list = list->next;
6016 insp = list;
6017 } else {
6018 /* Eaten partially. */
6019 if (skb_shared(list)) {
6020 clone = skb_clone(list, gfp_mask);
6021 if (!clone)
6022 return -ENOMEM;
6023 insp = list->next;
6024 list = clone;
6025 } else {
6026 /* This may be pulled without problems. */
6027 insp = list;
6028 }
6029 if (pskb_carve(list, eat, gfp_mask) < 0) {
6030 kfree_skb(clone);
6031 return -ENOMEM;
6032 }
6033 break;
6034 }
6035 } while (eat);
6036
6037 /* Free pulled out fragments. */
6038 while ((list = shinfo->frag_list) != insp) {
6039 shinfo->frag_list = list->next;
6040 kfree_skb(list);
6041 }
6042 /* And insert new clone at head. */
6043 if (clone) {
6044 clone->next = list;
6045 shinfo->frag_list = clone;
6046 }
6047 return 0;
6048}
6049
6050/* carve off first len bytes from skb. Split line (off) is in the
6051 * non-linear part of skb
6052 */
6053static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6054 int pos, gfp_t gfp_mask)
6055{
6056 int i, k = 0;
6057 int size = skb_end_offset(skb);
6058 u8 *data;
6059 const int nfrags = skb_shinfo(skb)->nr_frags;
6060 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006061
6062 size = SKB_DATA_ALIGN(size);
6063
6064 if (skb_pfmemalloc(skb))
6065 gfp_mask |= __GFP_MEMALLOC;
6066 data = kmalloc_reserve(size +
6067 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6068 gfp_mask, NUMA_NO_NODE, NULL);
6069 if (!data)
6070 return -ENOMEM;
6071
6072 size = SKB_WITH_OVERHEAD(ksize(data));
6073
6074 memcpy((struct skb_shared_info *)(data + size),
Miaohe Line3ec1e82020-08-15 04:48:53 -04006075 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006076 if (skb_orphan_frags(skb, gfp_mask)) {
6077 kfree(data);
6078 return -ENOMEM;
6079 }
6080 shinfo = (struct skb_shared_info *)(data + size);
6081 for (i = 0; i < nfrags; i++) {
6082 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6083
6084 if (pos + fsize > off) {
6085 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6086
6087 if (pos < off) {
6088 /* Split frag.
6089 * We have two variants in this case:
6090 * 1. Move all the frag to the second
6091 * part, if it is possible. F.e.
6092 * this approach is mandatory for TUX,
6093 * where splitting is expensive.
6094 * 2. Split is accurately. We make this.
6095 */
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006096 skb_frag_off_add(&shinfo->frags[0], off - pos);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006097 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6098 }
6099 skb_frag_ref(skb, i);
6100 k++;
6101 }
6102 pos += fsize;
6103 }
6104 shinfo->nr_frags = k;
6105 if (skb_has_frag_list(skb))
6106 skb_clone_fraglist(skb);
6107
Miaohe Lineabe8612020-08-15 04:46:41 -04006108 /* split line is in frag list */
6109 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6110 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6111 if (skb_has_frag_list(skb))
6112 kfree_skb_list(skb_shinfo(skb)->frag_list);
6113 kfree(data);
6114 return -ENOMEM;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006115 }
6116 skb_release_data(skb);
6117
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006118 skb->head = data;
6119 skb->head_frag = 0;
6120 skb->data = data;
6121#ifdef NET_SKBUFF_DATA_USES_OFFSET
6122 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006123#else
6124 skb->end = skb->head + size;
6125#endif
6126 skb_reset_tail_pointer(skb);
6127 skb_headers_offset_update(skb, 0);
6128 skb->cloned = 0;
6129 skb->hdr_len = 0;
6130 skb->nohdr = 0;
6131 skb->len -= off;
6132 skb->data_len = skb->len;
6133 atomic_set(&skb_shinfo(skb)->dataref, 1);
6134 return 0;
6135}
6136
6137/* remove len bytes from the beginning of the skb */
6138static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6139{
6140 int headlen = skb_headlen(skb);
6141
6142 if (len < headlen)
6143 return pskb_carve_inside_header(skb, len, headlen, gfp);
6144 else
6145 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6146}
6147
6148/* Extract to_copy bytes starting at off from skb, and return this in
6149 * a new skb
6150 */
6151struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6152 int to_copy, gfp_t gfp)
6153{
6154 struct sk_buff *clone = skb_clone(skb, gfp);
6155
6156 if (!clone)
6157 return NULL;
6158
6159 if (pskb_carve(clone, off, gfp) < 0 ||
6160 pskb_trim(clone, to_copy)) {
6161 kfree_skb(clone);
6162 return NULL;
6163 }
6164 return clone;
6165}
6166EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006167
6168/**
6169 * skb_condense - try to get rid of fragments/frag_list if possible
6170 * @skb: buffer
6171 *
6172 * Can be used to save memory before skb is added to a busy queue.
6173 * If packet has bytes in frags and enough tail room in skb->head,
6174 * pull all of them, so that we can free the frags right now and adjust
6175 * truesize.
6176 * Notes:
6177 * We do not reallocate skb->head thus can not fail.
6178 * Caller must re-evaluate skb->truesize if needed.
6179 */
6180void skb_condense(struct sk_buff *skb)
6181{
Eric Dumazet3174fed2016-12-09 08:02:05 -08006182 if (skb->data_len) {
6183 if (skb->data_len > skb->end - skb->tail ||
6184 skb_cloned(skb))
6185 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006186
Eric Dumazet3174fed2016-12-09 08:02:05 -08006187 /* Nice, we can free page frag(s) right now */
6188 __pskb_pull_tail(skb, skb->data_len);
6189 }
6190 /* At this point, skb->truesize might be over estimated,
6191 * because skb had a fragment, and fragments do not tell
6192 * their truesize.
6193 * When we pulled its content into skb->head, fragment
6194 * was freed, but __pskb_pull_tail() could not possibly
6195 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006196 */
6197 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6198}
Florian Westphaldf5042f2018-12-18 17:15:16 +01006199
6200#ifdef CONFIG_SKB_EXTENSIONS
6201static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6202{
6203 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6204}
6205
Paolo Abeni8b69a802020-01-09 07:59:24 -08006206/**
6207 * __skb_ext_alloc - allocate a new skb extensions storage
6208 *
Florian Westphal4930f482020-05-16 10:46:23 +02006209 * @flags: See kmalloc().
6210 *
Paolo Abeni8b69a802020-01-09 07:59:24 -08006211 * Returns the newly allocated pointer. The pointer can later attached to a
6212 * skb via __skb_ext_set().
6213 * Note: caller must handle the skb_ext as an opaque data.
6214 */
Florian Westphal4930f482020-05-16 10:46:23 +02006215struct skb_ext *__skb_ext_alloc(gfp_t flags)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006216{
Florian Westphal4930f482020-05-16 10:46:23 +02006217 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006218
6219 if (new) {
6220 memset(new->offset, 0, sizeof(new->offset));
6221 refcount_set(&new->refcnt, 1);
6222 }
6223
6224 return new;
6225}
6226
Florian Westphal41650792018-12-18 17:15:27 +01006227static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6228 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006229{
6230 struct skb_ext *new;
6231
6232 if (refcount_read(&old->refcnt) == 1)
6233 return old;
6234
6235 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6236 if (!new)
6237 return NULL;
6238
6239 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6240 refcount_set(&new->refcnt, 1);
6241
Florian Westphal41650792018-12-18 17:15:27 +01006242#ifdef CONFIG_XFRM
6243 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6244 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6245 unsigned int i;
6246
6247 for (i = 0; i < sp->len; i++)
6248 xfrm_state_hold(sp->xvec[i]);
6249 }
6250#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006251 __skb_ext_put(old);
6252 return new;
6253}
6254
6255/**
Paolo Abeni8b69a802020-01-09 07:59:24 -08006256 * __skb_ext_set - attach the specified extension storage to this skb
6257 * @skb: buffer
6258 * @id: extension id
6259 * @ext: extension storage previously allocated via __skb_ext_alloc()
6260 *
6261 * Existing extensions, if any, are cleared.
6262 *
6263 * Returns the pointer to the extension.
6264 */
6265void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6266 struct skb_ext *ext)
6267{
6268 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6269
6270 skb_ext_put(skb);
6271 newlen = newoff + skb_ext_type_len[id];
6272 ext->chunks = newlen;
6273 ext->offset[id] = newoff;
6274 skb->extensions = ext;
6275 skb->active_extensions = 1 << id;
6276 return skb_ext_get_ptr(ext, id);
6277}
6278
6279/**
Florian Westphaldf5042f2018-12-18 17:15:16 +01006280 * skb_ext_add - allocate space for given extension, COW if needed
6281 * @skb: buffer
6282 * @id: extension to allocate space for
6283 *
6284 * Allocates enough space for the given extension.
6285 * If the extension is already present, a pointer to that extension
6286 * is returned.
6287 *
6288 * If the skb was cloned, COW applies and the returned memory can be
6289 * modified without changing the extension space of clones buffers.
6290 *
6291 * Returns pointer to the extension or NULL on allocation failure.
6292 */
6293void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6294{
6295 struct skb_ext *new, *old = NULL;
6296 unsigned int newlen, newoff;
6297
6298 if (skb->active_extensions) {
6299 old = skb->extensions;
6300
Florian Westphal41650792018-12-18 17:15:27 +01006301 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006302 if (!new)
6303 return NULL;
6304
Paolo Abeni682ec852018-12-21 19:03:15 +01006305 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01006306 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006307
Paolo Abenie94e50b2018-12-21 19:03:13 +01006308 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006309 } else {
6310 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6311
Florian Westphal4930f482020-05-16 10:46:23 +02006312 new = __skb_ext_alloc(GFP_ATOMIC);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006313 if (!new)
6314 return NULL;
6315 }
6316
6317 newlen = newoff + skb_ext_type_len[id];
6318 new->chunks = newlen;
6319 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006320set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01006321 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006322 skb->active_extensions |= 1 << id;
6323 return skb_ext_get_ptr(new, id);
6324}
6325EXPORT_SYMBOL(skb_ext_add);
6326
Florian Westphal41650792018-12-18 17:15:27 +01006327#ifdef CONFIG_XFRM
6328static void skb_ext_put_sp(struct sec_path *sp)
6329{
6330 unsigned int i;
6331
6332 for (i = 0; i < sp->len; i++)
6333 xfrm_state_put(sp->xvec[i]);
6334}
6335#endif
6336
Florian Westphaldf5042f2018-12-18 17:15:16 +01006337void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6338{
6339 struct skb_ext *ext = skb->extensions;
6340
6341 skb->active_extensions &= ~(1 << id);
6342 if (skb->active_extensions == 0) {
6343 skb->extensions = NULL;
6344 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01006345#ifdef CONFIG_XFRM
6346 } else if (id == SKB_EXT_SEC_PATH &&
6347 refcount_read(&ext->refcnt) == 1) {
6348 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6349
6350 skb_ext_put_sp(sp);
6351 sp->len = 0;
6352#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006353 }
6354}
6355EXPORT_SYMBOL(__skb_ext_del);
6356
6357void __skb_ext_put(struct skb_ext *ext)
6358{
6359 /* If this is last clone, nothing can increment
6360 * it after check passes. Avoids one atomic op.
6361 */
6362 if (refcount_read(&ext->refcnt) == 1)
6363 goto free_now;
6364
6365 if (!refcount_dec_and_test(&ext->refcnt))
6366 return;
6367free_now:
Florian Westphal41650792018-12-18 17:15:27 +01006368#ifdef CONFIG_XFRM
6369 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6370 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6371#endif
6372
Florian Westphaldf5042f2018-12-18 17:15:16 +01006373 kmem_cache_free(skbuff_ext_cache, ext);
6374}
6375EXPORT_SYMBOL(__skb_ext_put);
6376#endif /* CONFIG_SKB_EXTENSIONS */