blob: 825e6b9880030c02717bf21a6060fb5a50943367 [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 Ry5f74f822016-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 Dumazet6ffe75e2014-12-03 17:04:39 -0800250 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700251 }
Aleksandr Nogikh4a31baf2020-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 Nogikh4a31baf2020-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 Lobakinb65578c2021-01-15 15:04:40 +0000440 /* If requested length is either too small or too big,
441 * we use kmalloc() for skb->head allocation.
442 */
443 if (len <= SKB_WITH_OVERHEAD(1024) ||
444 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800445 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700446 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
447 if (!skb)
448 goto skb_fail;
449 goto skb_success;
450 }
Alexander Duyck94519802015-05-06 21:11:40 -0700451
452 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
453 len = SKB_DATA_ALIGN(len);
454
455 if (sk_memalloc_socks())
456 gfp_mask |= __GFP_MEMALLOC;
457
Sebastian Andrzej Siewior92dcabd2019-06-07 21:20:35 +0200458 if (in_irq() || irqs_disabled()) {
459 nc = this_cpu_ptr(&netdev_alloc_cache);
460 data = page_frag_alloc(nc, len, gfp_mask);
461 pfmemalloc = nc->pfmemalloc;
462 } else {
463 local_bh_disable();
464 nc = this_cpu_ptr(&napi_alloc_cache.page);
465 data = page_frag_alloc(nc, len, gfp_mask);
466 pfmemalloc = nc->pfmemalloc;
467 local_bh_enable();
468 }
Alexander Duyck94519802015-05-06 21:11:40 -0700469
470 if (unlikely(!data))
471 return NULL;
472
473 skb = __build_skb(data, len);
474 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700475 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700476 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700477 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800478
Alexander Duyck94519802015-05-06 21:11:40 -0700479 if (pfmemalloc)
480 skb->pfmemalloc = 1;
481 skb->head_frag = 1;
482
Alexander Duycka080e7b2015-05-13 13:34:13 -0700483skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700484 skb_reserve(skb, NET_SKB_PAD);
485 skb->dev = dev;
486
Alexander Duycka080e7b2015-05-13 13:34:13 -0700487skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700488 return skb;
489}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800490EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alexander Duyckfd11a832014-12-09 19:40:49 -0800492/**
493 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
494 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900495 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800496 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
497 *
498 * Allocate a new sk_buff for use in NAPI receive. This buffer will
499 * attempt to allocate the head from a special reserved region used
500 * only for NAPI Rx allocation. By doing this we can save several
501 * CPU cycles by avoiding having to disable and re-enable IRQs.
502 *
503 * %NULL is returned if there is no free memory.
504 */
Alexander Duyck94519802015-05-06 21:11:40 -0700505struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
506 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800507{
Eric Dumazet024158d2021-01-13 08:18:19 -0800508 struct napi_alloc_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800509 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700510 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800511
Alexander Duyck94519802015-05-06 21:11:40 -0700512 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800513
Eric Dumazet024158d2021-01-13 08:18:19 -0800514 /* If requested length is either too small or too big,
515 * we use kmalloc() for skb->head allocation.
516 */
517 if (len <= SKB_WITH_OVERHEAD(1024) ||
518 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800519 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700520 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
521 if (!skb)
522 goto skb_fail;
523 goto skb_success;
524 }
Alexander Duyck94519802015-05-06 21:11:40 -0700525
Eric Dumazet024158d2021-01-13 08:18:19 -0800526 nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700527 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
528 len = SKB_DATA_ALIGN(len);
529
530 if (sk_memalloc_socks())
531 gfp_mask |= __GFP_MEMALLOC;
532
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800533 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700534 if (unlikely(!data))
535 return NULL;
536
537 skb = __build_skb(data, len);
538 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700539 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700540 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800541 }
542
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100543 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700544 skb->pfmemalloc = 1;
545 skb->head_frag = 1;
546
Alexander Duycka080e7b2015-05-13 13:34:13 -0700547skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700548 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
549 skb->dev = napi->dev;
550
Alexander Duycka080e7b2015-05-13 13:34:13 -0700551skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800552 return skb;
553}
554EXPORT_SYMBOL(__napi_alloc_skb);
555
Peter Zijlstra654bed12008-10-07 14:22:33 -0700556void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000557 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700558{
559 skb_fill_page_desc(skb, i, page, off, size);
560 skb->len += size;
561 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000562 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700563}
564EXPORT_SYMBOL(skb_add_rx_frag);
565
Jason Wangf8e617e2013-11-01 14:07:47 +0800566void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
567 unsigned int truesize)
568{
569 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
570
571 skb_frag_size_add(frag, size);
572 skb->len += size;
573 skb->data_len += size;
574 skb->truesize += truesize;
575}
576EXPORT_SYMBOL(skb_coalesce_rx_frag);
577
Herbert Xu27b437c2006-07-13 19:26:39 -0700578static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700580 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700581 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Herbert Xu27b437c2006-07-13 19:26:39 -0700584static inline void skb_drop_fraglist(struct sk_buff *skb)
585{
586 skb_drop_list(&skb_shinfo(skb)->frag_list);
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static void skb_clone_fraglist(struct sk_buff *skb)
590{
591 struct sk_buff *list;
592
David S. Millerfbb398a2009-06-09 00:18:59 -0700593 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 skb_get(list);
595}
596
Eric Dumazetd3836f22012-04-27 00:33:38 +0000597static void skb_free_head(struct sk_buff *skb)
598{
Alexander Duyck181edb22015-05-06 21:12:03 -0700599 unsigned char *head = skb->head;
600
Eric Dumazetd3836f22012-04-27 00:33:38 +0000601 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700602 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000603 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700604 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000605}
606
Adrian Bunk5bba1712006-06-29 13:02:35 -0700607static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Eric Dumazetff04a772014-09-23 18:39:30 -0700609 struct skb_shared_info *shinfo = skb_shinfo(skb);
610 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Eric Dumazetff04a772014-09-23 18:39:30 -0700612 if (skb->cloned &&
613 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
614 &shinfo->dataref))
615 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000616
Eric Dumazetff04a772014-09-23 18:39:30 -0700617 for (i = 0; i < shinfo->nr_frags; i++)
618 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000619
Eric Dumazetff04a772014-09-23 18:39:30 -0700620 if (shinfo->frag_list)
621 kfree_skb_list(shinfo->frag_list);
622
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400623 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700624 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*
628 * Free an skbuff by memory without cleaning the state.
629 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800630static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700632 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700633
David S. Millerd179cd12005-08-17 14:57:30 -0700634 switch (skb->fclone) {
635 case SKB_FCLONE_UNAVAILABLE:
636 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800637 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700638
639 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700640 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800641
642 /* We usually free the clone (TX completion) before original skb
643 * This test would have no chance to be true for the clone,
644 * while here, branch prediction will be good.
645 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300646 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800647 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700648 break;
649
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800650 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700651 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700652 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700653 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300654 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800655 return;
656fastpath:
657 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Paolo Abeni0a463c72017-06-12 11:23:42 +0200660void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Paul Blakey570341f2021-07-05 13:54:51 +0300662 nf_reset_ct(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000663 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700664 if (skb->destructor) {
665 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 skb->destructor(skb);
667 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000668#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100669 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100670#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100671 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700672}
673
674/* Free everything but the sk_buff shell. */
675static void skb_release_all(struct sk_buff *skb)
676{
677 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200678 if (likely(skb->head))
679 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Herbert Xu2d4baff2007-11-26 23:11:19 +0800682/**
683 * __kfree_skb - private function
684 * @skb: buffer
685 *
686 * Free an sk_buff. Release anything attached to the buffer.
687 * Clean the state. This is an internal helper function. Users should
688 * always call kfree_skb
689 */
690
691void __kfree_skb(struct sk_buff *skb)
692{
693 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 kfree_skbmem(skb);
695}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800696EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800699 * kfree_skb - free an sk_buff
700 * @skb: buffer to free
701 *
702 * Drop a reference to the buffer and free it if the usage count has
703 * hit zero.
704 */
705void kfree_skb(struct sk_buff *skb)
706{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200707 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800708 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200709
Neil Hormanead2ceb2009-03-11 09:49:55 +0000710 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800711 __kfree_skb(skb);
712}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800713EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800714
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700715void kfree_skb_list(struct sk_buff *segs)
716{
717 while (segs) {
718 struct sk_buff *next = segs->next;
719
720 kfree_skb(segs);
721 segs = next;
722 }
723}
724EXPORT_SYMBOL(kfree_skb_list);
725
Willem de Bruijn64131392019-07-07 05:51:55 -0400726/* Dump skb information and contents.
727 *
728 * Must only be called from net_ratelimit()-ed paths.
729 *
Vladimir Oltean302af7c2020-10-05 17:48:38 +0300730 * Dumps whole packets if full_pkt, only headers otherwise.
Willem de Bruijn64131392019-07-07 05:51:55 -0400731 */
732void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
733{
Willem de Bruijn64131392019-07-07 05:51:55 -0400734 struct skb_shared_info *sh = skb_shinfo(skb);
735 struct net_device *dev = skb->dev;
736 struct sock *sk = skb->sk;
737 struct sk_buff *list_skb;
738 bool has_mac, has_trans;
739 int headroom, tailroom;
740 int i, len, seg_len;
741
742 if (full_pkt)
Willem de Bruijn64131392019-07-07 05:51:55 -0400743 len = skb->len;
744 else
745 len = min_t(int, skb->len, MAX_HEADER + 128);
746
747 headroom = skb_headroom(skb);
748 tailroom = skb_tailroom(skb);
749
750 has_mac = skb_mac_header_was_set(skb);
751 has_trans = skb_transport_header_was_set(skb);
752
753 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
754 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
755 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
756 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
757 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
758 level, skb->len, headroom, skb_headlen(skb), tailroom,
759 has_mac ? skb->mac_header : -1,
760 has_mac ? skb_mac_header_len(skb) : -1,
761 skb->network_header,
762 has_trans ? skb_network_header_len(skb) : -1,
763 has_trans ? skb->transport_header : -1,
764 sh->tx_flags, sh->nr_frags,
765 sh->gso_size, sh->gso_type, sh->gso_segs,
766 skb->csum, skb->ip_summed, skb->csum_complete_sw,
767 skb->csum_valid, skb->csum_level,
768 skb->hash, skb->sw_hash, skb->l4_hash,
769 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
770
771 if (dev)
772 printk("%sdev name=%s feat=0x%pNF\n",
773 level, dev->name, &dev->features);
774 if (sk)
Qian Caidb8051f2019-07-16 11:43:05 -0400775 printk("%ssk family=%hu type=%u proto=%u\n",
Willem de Bruijn64131392019-07-07 05:51:55 -0400776 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
777
778 if (full_pkt && headroom)
779 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
780 16, 1, skb->head, headroom, false);
781
782 seg_len = min_t(int, skb_headlen(skb), len);
783 if (seg_len)
784 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
785 16, 1, skb->data, seg_len, false);
786 len -= seg_len;
787
788 if (full_pkt && tailroom)
789 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
790 16, 1, skb_tail_pointer(skb), tailroom, false);
791
792 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
793 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
794 u32 p_off, p_len, copied;
795 struct page *p;
796 u8 *vaddr;
797
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700798 skb_frag_foreach_page(frag, skb_frag_off(frag),
Willem de Bruijn64131392019-07-07 05:51:55 -0400799 skb_frag_size(frag), p, p_off, p_len,
800 copied) {
801 seg_len = min_t(int, p_len, len);
802 vaddr = kmap_atomic(p);
803 print_hex_dump(level, "skb frag: ",
804 DUMP_PREFIX_OFFSET,
805 16, 1, vaddr + p_off, seg_len, false);
806 kunmap_atomic(vaddr);
807 len -= seg_len;
808 if (!len)
809 break;
810 }
811 }
812
813 if (full_pkt && skb_has_frag_list(skb)) {
814 printk("skb fraglist:\n");
815 skb_walk_frags(skb, list_skb)
816 skb_dump(level, list_skb, true);
817 }
818}
819EXPORT_SYMBOL(skb_dump);
820
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700821/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000822 * skb_tx_error - report an sk_buff xmit error
823 * @skb: buffer that triggered an error
824 *
825 * Report xmit error if a device callback is tracking this skb.
826 * skb must be freed afterwards.
827 */
828void skb_tx_error(struct sk_buff *skb)
829{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400830 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000831}
832EXPORT_SYMBOL(skb_tx_error);
833
Herbert Xube769db2020-08-22 08:23:29 +1000834#ifdef CONFIG_TRACEPOINTS
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000835/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000836 * consume_skb - free an skbuff
837 * @skb: buffer to free
838 *
839 * Drop a ref to the buffer and free it if the usage count has hit zero
840 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
841 * is being dropped after a failure and notes that
842 */
843void consume_skb(struct sk_buff *skb)
844{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200845 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000846 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200847
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900848 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000849 __kfree_skb(skb);
850}
851EXPORT_SYMBOL(consume_skb);
Herbert Xube769db2020-08-22 08:23:29 +1000852#endif
Neil Hormanead2ceb2009-03-11 09:49:55 +0000853
Paolo Abeni0a463c72017-06-12 11:23:42 +0200854/**
855 * consume_stateless_skb - free an skbuff, assuming it is stateless
856 * @skb: buffer to free
857 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200858 * Alike consume_skb(), but this variant assumes that this is the last
859 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200860 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200861void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200862{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200863 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200864 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200865 kfree_skbmem(skb);
866}
867
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100868void __kfree_skb_flush(void)
869{
870 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
871
872 /* flush skb_cache if containing objects */
873 if (nc->skb_count) {
874 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
875 nc->skb_cache);
876 nc->skb_count = 0;
877 }
878}
879
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100880static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100881{
882 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
883
884 /* drop skb->head and call any destructors for packet */
885 skb_release_all(skb);
886
887 /* record skb to CPU local list */
888 nc->skb_cache[nc->skb_count++] = skb;
889
890#ifdef CONFIG_SLUB
891 /* SLUB writes into objects when freeing */
892 prefetchw(skb);
893#endif
894
895 /* flush skb_cache if it is filled */
896 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
897 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
898 nc->skb_cache);
899 nc->skb_count = 0;
900 }
901}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100902void __kfree_skb_defer(struct sk_buff *skb)
903{
904 _kfree_skb_defer(skb);
905}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100906
907void napi_consume_skb(struct sk_buff *skb, int budget)
908{
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100909 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100910 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100911 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100912 return;
913 }
914
Paolo Abeni76088942017-06-14 11:48:48 +0200915 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100916 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200917
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100918 /* if reaching here SKB is ready to free */
919 trace_consume_skb(skb);
920
921 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700922 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100923 __kfree_skb(skb);
924 return;
925 }
926
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100927 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100928}
929EXPORT_SYMBOL(napi_consume_skb);
930
Eric Dumazetb1937222014-09-28 22:18:47 -0700931/* Make sure a field is enclosed inside headers_start/headers_end section */
932#define CHECK_SKB_FIELD(field) \
933 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
934 offsetof(struct sk_buff, headers_start)); \
935 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
936 offsetof(struct sk_buff, headers_end)); \
937
Herbert Xudec18812007-10-14 00:37:30 -0700938static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
939{
940 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700941 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700942 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700943 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000944 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100945 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -0700946 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700947
Eric Dumazetb1937222014-09-28 22:18:47 -0700948 /* Note : this field could be in headers_start/headers_end section
949 * It is not yet because we do not want to have a 16 bit hole
950 */
951 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300952
Eric Dumazetb1937222014-09-28 22:18:47 -0700953 memcpy(&new->headers_start, &old->headers_start,
954 offsetof(struct sk_buff, headers_end) -
955 offsetof(struct sk_buff, headers_start));
956 CHECK_SKB_FIELD(protocol);
957 CHECK_SKB_FIELD(csum);
958 CHECK_SKB_FIELD(hash);
959 CHECK_SKB_FIELD(priority);
960 CHECK_SKB_FIELD(skb_iif);
961 CHECK_SKB_FIELD(vlan_proto);
962 CHECK_SKB_FIELD(vlan_tci);
963 CHECK_SKB_FIELD(transport_header);
964 CHECK_SKB_FIELD(network_header);
965 CHECK_SKB_FIELD(mac_header);
966 CHECK_SKB_FIELD(inner_protocol);
967 CHECK_SKB_FIELD(inner_transport_header);
968 CHECK_SKB_FIELD(inner_network_header);
969 CHECK_SKB_FIELD(inner_mac_header);
970 CHECK_SKB_FIELD(mark);
971#ifdef CONFIG_NETWORK_SECMARK
972 CHECK_SKB_FIELD(secmark);
973#endif
Cong Wange0d10952013-08-01 11:10:25 +0800974#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700975 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300976#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800977#ifdef CONFIG_XPS
978 CHECK_SKB_FIELD(sender_cpu);
979#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700980#ifdef CONFIG_NET_SCHED
981 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700982#endif
983
Herbert Xudec18812007-10-14 00:37:30 -0700984}
985
Herbert Xu82c49a32009-05-22 22:11:37 +0000986/*
987 * You should not add any new code to this function. Add it to
988 * __copy_skb_header above instead.
989 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700990static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#define C(x) n->x = skb->x
993
994 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700996 __copy_skb_header(n, skb);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 C(len);
999 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -07001000 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -07001001 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -08001002 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -07001004 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +02001005 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 C(tail);
1008 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -08001009 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +00001010 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -08001011 C(data);
1012 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +03001013 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 atomic_inc(&(skb_shinfo(skb)->dataref));
1016 skb->cloned = 1;
1017
1018 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001019#undef C
1020}
1021
1022/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -07001023 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1024 * @first: first sk_buff of the msg
1025 */
1026struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1027{
1028 struct sk_buff *n;
1029
1030 n = alloc_skb(0, GFP_ATOMIC);
1031 if (!n)
1032 return NULL;
1033
1034 n->len = first->len;
1035 n->data_len = first->len;
1036 n->truesize = first->truesize;
1037
1038 skb_shinfo(n)->frag_list = first;
1039
1040 __copy_skb_header(n, first);
1041 n->destructor = NULL;
1042
1043 return n;
1044}
1045EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1046
1047/**
Herbert Xue0053ec2007-10-14 00:37:52 -07001048 * skb_morph - morph one skb into another
1049 * @dst: the skb to receive the contents
1050 * @src: the skb to supply the contents
1051 *
1052 * This is identical to skb_clone except that the target skb is
1053 * supplied by the user.
1054 *
1055 * The target skb is returned upon exit.
1056 */
1057struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1058{
Herbert Xu2d4baff2007-11-26 23:11:19 +08001059 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -07001060 return __skb_clone(dst, src);
1061}
1062EXPORT_SYMBOL_GPL(skb_morph);
1063
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001064int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001065{
1066 unsigned long max_pg, num_pg, new_pg, old_pg;
1067 struct user_struct *user;
1068
1069 if (capable(CAP_IPC_LOCK) || !size)
1070 return 0;
1071
1072 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1073 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1074 user = mmp->user ? : current_user();
1075
1076 do {
1077 old_pg = atomic_long_read(&user->locked_vm);
1078 new_pg = old_pg + num_pg;
1079 if (new_pg > max_pg)
1080 return -ENOBUFS;
1081 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1082 old_pg);
1083
1084 if (!mmp->user) {
1085 mmp->user = get_uid(user);
1086 mmp->num_pg = num_pg;
1087 } else {
1088 mmp->num_pg += num_pg;
1089 }
1090
1091 return 0;
1092}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001093EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001094
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001095void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001096{
1097 if (mmp->user) {
1098 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1099 free_uid(mmp->user);
1100 }
1101}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001102EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001103
Willem de Bruijn52267792017-08-03 16:29:39 -04001104struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
1105{
1106 struct ubuf_info *uarg;
1107 struct sk_buff *skb;
1108
1109 WARN_ON_ONCE(!in_task());
1110
1111 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1112 if (!skb)
1113 return NULL;
1114
1115 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1116 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001117 uarg->mmp.user = NULL;
1118
1119 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1120 kfree_skb(skb);
1121 return NULL;
1122 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001123
1124 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001125 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1126 uarg->len = 1;
1127 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001128 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001129 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001130 sock_hold(sk);
1131
1132 return uarg;
1133}
1134EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
1135
1136static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1137{
1138 return container_of((void *)uarg, struct sk_buff, cb);
1139}
1140
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001141struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1142 struct ubuf_info *uarg)
1143{
1144 if (uarg) {
1145 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1146 u32 bytelen, next;
1147
1148 /* realloc only when socket is locked (TCP, UDP cork),
1149 * so uarg->len and sk_zckey access is serialized
1150 */
1151 if (!sock_owned_by_user(sk)) {
1152 WARN_ON_ONCE(1);
1153 return NULL;
1154 }
1155
1156 bytelen = uarg->bytelen + size;
1157 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1158 /* TCP can create new skb to attach new uarg */
1159 if (sk->sk_type == SOCK_STREAM)
1160 goto new_alloc;
1161 return NULL;
1162 }
1163
1164 next = (u32)atomic_read(&sk->sk_zckey);
1165 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001166 if (mm_account_pinned_pages(&uarg->mmp, size))
1167 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001168 uarg->len++;
1169 uarg->bytelen = bytelen;
1170 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001171
1172 /* no extra ref when appending to datagram (MSG_MORE) */
1173 if (sk->sk_type == SOCK_STREAM)
1174 sock_zerocopy_get(uarg);
1175
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001176 return uarg;
1177 }
1178 }
1179
1180new_alloc:
1181 return sock_zerocopy_alloc(sk, size);
1182}
1183EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1184
1185static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1186{
1187 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1188 u32 old_lo, old_hi;
1189 u64 sum_len;
1190
1191 old_lo = serr->ee.ee_info;
1192 old_hi = serr->ee.ee_data;
1193 sum_len = old_hi - old_lo + 1ULL + len;
1194
1195 if (sum_len >= (1ULL << 32))
1196 return false;
1197
1198 if (lo != old_hi + 1)
1199 return false;
1200
1201 serr->ee.ee_data += len;
1202 return true;
1203}
1204
Willem de Bruijn52267792017-08-03 16:29:39 -04001205void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1206{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001207 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001208 struct sock_exterr_skb *serr;
1209 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001210 struct sk_buff_head *q;
1211 unsigned long flags;
1212 u32 lo, hi;
1213 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001214
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001215 mm_unaccount_pinned_pages(&uarg->mmp);
1216
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001217 /* if !len, there was only 1 call, and it was aborted
1218 * so do not queue a completion notification
1219 */
1220 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001221 goto release;
1222
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001223 len = uarg->len;
1224 lo = uarg->id;
1225 hi = uarg->id + len - 1;
1226
Willem de Bruijn52267792017-08-03 16:29:39 -04001227 serr = SKB_EXT_ERR(skb);
1228 memset(serr, 0, sizeof(*serr));
1229 serr->ee.ee_errno = 0;
1230 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001231 serr->ee.ee_data = hi;
1232 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001233 if (!success)
1234 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1235
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001236 q = &sk->sk_error_queue;
1237 spin_lock_irqsave(&q->lock, flags);
1238 tail = skb_peek_tail(q);
1239 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1240 !skb_zerocopy_notify_extend(tail, lo, len)) {
1241 __skb_queue_tail(q, skb);
1242 skb = NULL;
1243 }
1244 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001245
1246 sk->sk_error_report(sk);
1247
1248release:
1249 consume_skb(skb);
1250 sock_put(sk);
1251}
1252EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1253
1254void sock_zerocopy_put(struct ubuf_info *uarg)
1255{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001256 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001257 if (uarg->callback)
1258 uarg->callback(uarg, uarg->zerocopy);
1259 else
1260 consume_skb(skb_from_uarg(uarg));
1261 }
1262}
1263EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1264
Willem de Bruijn52900d22018-11-30 15:32:40 -05001265void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001266{
1267 if (uarg) {
1268 struct sock *sk = skb_from_uarg(uarg)->sk;
1269
1270 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001271 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001272
Willem de Bruijn52900d22018-11-30 15:32:40 -05001273 if (have_uref)
1274 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001275 }
1276}
1277EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1278
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001279int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1280{
1281 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1282}
1283EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1284
Willem de Bruijn52267792017-08-03 16:29:39 -04001285int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1286 struct msghdr *msg, int len,
1287 struct ubuf_info *uarg)
1288{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001289 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001290 struct iov_iter orig_iter = msg->msg_iter;
1291 int err, orig_len = skb->len;
1292
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001293 /* An skb can only point to one uarg. This edge case happens when
1294 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1295 */
1296 if (orig_uarg && uarg != orig_uarg)
1297 return -EEXIST;
1298
Willem de Bruijn52267792017-08-03 16:29:39 -04001299 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1300 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001301 struct sock *save_sk = skb->sk;
1302
Willem de Bruijn52267792017-08-03 16:29:39 -04001303 /* Streams do not free skb on error. Reset to prev state. */
1304 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001305 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001306 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001307 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001308 return err;
1309 }
1310
Willem de Bruijn52900d22018-11-30 15:32:40 -05001311 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001312 return skb->len - orig_len;
1313}
1314EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1315
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001316static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001317 gfp_t gfp_mask)
1318{
1319 if (skb_zcopy(orig)) {
1320 if (skb_zcopy(nskb)) {
1321 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1322 if (!gfp_mask) {
1323 WARN_ON_ONCE(1);
1324 return -ENOMEM;
1325 }
1326 if (skb_uarg(nskb) == skb_uarg(orig))
1327 return 0;
1328 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1329 return -EIO;
1330 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001331 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001332 }
1333 return 0;
1334}
1335
Ben Hutchings2c530402012-07-10 10:55:09 +00001336/**
1337 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001338 * @skb: the skb to modify
1339 * @gfp_mask: allocation priority
1340 *
1341 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1342 * It will copy all frags into kernel and drop the reference
1343 * to userspace pages.
1344 *
1345 * If this function is called from an interrupt gfp_mask() must be
1346 * %GFP_ATOMIC.
1347 *
1348 * Returns 0 on success or a negative error code on failure
1349 * to allocate kernel memory to copy to.
1350 */
1351int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001352{
Shirley Maa6686f22011-07-06 12:22:12 +00001353 int num_frags = skb_shinfo(skb)->nr_frags;
1354 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001355 int i, new_frags;
1356 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001357
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001358 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1359 return -EINVAL;
1360
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001361 if (!num_frags)
1362 goto release;
1363
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001364 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1365 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001366 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001367 if (!page) {
1368 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001369 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001370 put_page(head);
1371 head = next;
1372 }
1373 return -ENOMEM;
1374 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001375 set_page_private(page, (unsigned long)head);
1376 head = page;
1377 }
1378
1379 page = head;
1380 d_off = 0;
1381 for (i = 0; i < num_frags; i++) {
1382 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1383 u32 p_off, p_len, copied;
1384 struct page *p;
1385 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001386
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001387 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
Willem de Bruijnc613c202017-07-31 08:15:47 -04001388 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001389 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001390 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001391
1392 while (done < p_len) {
1393 if (d_off == PAGE_SIZE) {
1394 d_off = 0;
1395 page = (struct page *)page_private(page);
1396 }
1397 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1398 memcpy(page_address(page) + d_off,
1399 vaddr + p_off + done, copy);
1400 done += copy;
1401 d_off += copy;
1402 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001403 kunmap_atomic(vaddr);
1404 }
Shirley Maa6686f22011-07-06 12:22:12 +00001405 }
1406
1407 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001408 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001409 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001410
Shirley Maa6686f22011-07-06 12:22:12 +00001411 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001412 for (i = 0; i < new_frags - 1; i++) {
1413 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001414 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001415 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001416 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1417 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001418
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001419release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001420 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001421 return 0;
1422}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001423EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001424
Herbert Xue0053ec2007-10-14 00:37:52 -07001425/**
1426 * skb_clone - duplicate an sk_buff
1427 * @skb: buffer to clone
1428 * @gfp_mask: allocation priority
1429 *
1430 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1431 * copies share the same packet data but not structure. The new
1432 * buffer has a reference count of 1. If the allocation fails the
1433 * function returns %NULL otherwise the new buffer is returned.
1434 *
1435 * If this function is called from an interrupt gfp_mask() must be
1436 * %GFP_ATOMIC.
1437 */
1438
1439struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1440{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001441 struct sk_buff_fclones *fclones = container_of(skb,
1442 struct sk_buff_fclones,
1443 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001444 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001445
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001446 if (skb_orphan_frags(skb, gfp_mask))
1447 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001448
Herbert Xue0053ec2007-10-14 00:37:52 -07001449 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001450 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001451 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001452 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001453 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001454 if (skb_pfmemalloc(skb))
1455 gfp_mask |= __GFP_MEMALLOC;
1456
Herbert Xue0053ec2007-10-14 00:37:52 -07001457 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1458 if (!n)
1459 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001460
Herbert Xue0053ec2007-10-14 00:37:52 -07001461 n->fclone = SKB_FCLONE_UNAVAILABLE;
1462 }
1463
1464 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001466EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001468void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001469{
Eric Dumazet030737b2013-10-19 11:42:54 -07001470 /* Only adjust this if it actually is csum_start rather than csum */
1471 if (skb->ip_summed == CHECKSUM_PARTIAL)
1472 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001473 /* {transport,network,mac}_header and tail are relative to skb->head */
1474 skb->transport_header += off;
1475 skb->network_header += off;
1476 if (skb_mac_header_was_set(skb))
1477 skb->mac_header += off;
1478 skb->inner_transport_header += off;
1479 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001480 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001481}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001482EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001483
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001484void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Herbert Xudec18812007-10-14 00:37:30 -07001486 __copy_skb_header(new, old);
1487
Herbert Xu79671682006-06-22 02:40:14 -07001488 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1489 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1490 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001492EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Mel Gormanc93bdd02012-07-31 16:44:19 -07001494static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1495{
1496 if (skb_pfmemalloc(skb))
1497 return SKB_ALLOC_RX;
1498 return 0;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501/**
1502 * skb_copy - create private copy of an sk_buff
1503 * @skb: buffer to copy
1504 * @gfp_mask: allocation priority
1505 *
1506 * Make a copy of both an &sk_buff and its data. This is used when the
1507 * caller wishes to modify the data and needs a private copy of the
1508 * data to alter. Returns %NULL on failure or the pointer to the buffer
1509 * on success. The returned buffer has a reference count of 1.
1510 *
1511 * As by-product this function converts non-linear &sk_buff to linear
1512 * one, so that &sk_buff becomes completely private and caller is allowed
1513 * to modify all the data of returned buffer. This means that this
1514 * function is not recommended for use in circumstances when only
1515 * header is going to be modified. Use pskb_copy() instead.
1516 */
1517
Al Virodd0fc662005-10-07 07:46:04 +01001518struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001520 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001521 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001522 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1523 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!n)
1526 return NULL;
1527
1528 /* Set the data pointer */
1529 skb_reserve(n, headerlen);
1530 /* Set the tail pointer and length */
1531 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Tim Hansen9f77fad2017-10-09 11:37:59 -04001533 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001535 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return n;
1537}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001538EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001541 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001543 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001545 * @fclone: if true allocate the copy of the skb from the fclone
1546 * cache instead of the head cache; it is recommended to set this
1547 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 *
1549 * Make a copy of both an &sk_buff and part of its data, located
1550 * in header. Fragmented data remain shared. This is used when
1551 * the caller wishes to modify only header of &sk_buff and needs
1552 * private copy of the header to alter. Returns %NULL on failure
1553 * or the pointer to the buffer on success.
1554 * The returned buffer has a reference count of 1.
1555 */
1556
Octavian Purdilabad93e92014-06-12 01:36:26 +03001557struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1558 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Eric Dumazet117632e2011-12-03 21:39:53 +00001560 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001561 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1562 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (!n)
1565 goto out;
1566
1567 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001568 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /* Set the tail pointer and length */
1570 skb_put(n, skb_headlen(skb));
1571 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001572 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Herbert Xu25f484a2006-11-07 14:57:15 -08001574 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 n->data_len = skb->data_len;
1576 n->len = skb->len;
1577
1578 if (skb_shinfo(skb)->nr_frags) {
1579 int i;
1580
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001581 if (skb_orphan_frags(skb, gfp_mask) ||
1582 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001583 kfree_skb(n);
1584 n = NULL;
1585 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1588 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001589 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 skb_shinfo(n)->nr_frags = i;
1592 }
1593
David S. Miller21dc3302010-08-23 00:13:46 -07001594 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1596 skb_clone_fraglist(n);
1597 }
1598
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001599 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600out:
1601 return n;
1602}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001603EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/**
1606 * pskb_expand_head - reallocate header of &sk_buff
1607 * @skb: buffer to reallocate
1608 * @nhead: room to add at head
1609 * @ntail: room to add at tail
1610 * @gfp_mask: allocation priority
1611 *
Mathias Krausebc323832013-11-07 14:18:26 +01001612 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1613 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * reference count of 1. Returns zero in the case of success or error,
1615 * if expansion failed. In the last case, &sk_buff is not changed.
1616 *
1617 * All the pointers pointing into skb header may change and must be
1618 * reloaded after call to this function.
1619 */
1620
Victor Fusco86a76ca2005-07-08 14:57:47 -07001621int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001622 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Eric Dumazet158f3232017-01-27 07:11:27 -08001624 int i, osize = skb_end_offset(skb);
1625 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001627 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Herbert Xu4edd87a2008-10-01 07:09:38 -07001629 BUG_ON(nhead < 0);
1630
Tim Hansen9f77fad2017-10-09 11:37:59 -04001631 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 size = SKB_DATA_ALIGN(size);
1634
Mel Gormanc93bdd02012-07-31 16:44:19 -07001635 if (skb_pfmemalloc(skb))
1636 gfp_mask |= __GFP_MEMALLOC;
1637 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1638 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!data)
1640 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001641 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001644 * optimized for the cases when header is void.
1645 */
1646 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1647
1648 memcpy((struct skb_shared_info *)(data + size),
1649 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001650 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Alexander Duyck3e245912012-05-04 14:26:51 +00001652 /*
1653 * if shinfo is shared we must drop the old head gracefully, but if it
1654 * is not we can just drop the old head and let the existing refcount
1655 * be since all we did is relocate the values
1656 */
1657 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001658 if (skb_orphan_frags(skb, gfp_mask))
1659 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001660 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001661 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001663 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Eric Dumazet1fd63042010-09-02 23:09:32 +00001665 if (skb_has_frag_list(skb))
1666 skb_clone_fraglist(skb);
1667
1668 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001669 } else {
1670 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 off = (data + nhead) - skb->head;
1673
1674 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001675 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001677#ifdef NET_SKBUFF_DATA_USES_OFFSET
1678 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001679 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001680#else
1681 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001682#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001683 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001684 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001686 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 skb->nohdr = 0;
1688 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001689
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001690 skb_metadata_clear(skb);
1691
Eric Dumazet158f3232017-01-27 07:11:27 -08001692 /* It is not generally safe to change skb->truesize.
1693 * For the moment, we really care of rx path, or
1694 * when skb is orphaned (not attached to a socket).
1695 */
1696 if (!skb->sk || skb->destructor == sock_edemux)
1697 skb->truesize += size - osize;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700
Shirley Maa6686f22011-07-06 12:22:12 +00001701nofrags:
1702 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703nodata:
1704 return -ENOMEM;
1705}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001706EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708/* Make private copy of skb with writable head and some headroom */
1709
1710struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1711{
1712 struct sk_buff *skb2;
1713 int delta = headroom - skb_headroom(skb);
1714
1715 if (delta <= 0)
1716 skb2 = pskb_copy(skb, GFP_ATOMIC);
1717 else {
1718 skb2 = skb_clone(skb, GFP_ATOMIC);
1719 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1720 GFP_ATOMIC)) {
1721 kfree_skb(skb2);
1722 skb2 = NULL;
1723 }
1724 }
1725 return skb2;
1726}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001727EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729/**
1730 * skb_copy_expand - copy and expand sk_buff
1731 * @skb: buffer to copy
1732 * @newheadroom: new free bytes at head
1733 * @newtailroom: new free bytes at tail
1734 * @gfp_mask: allocation priority
1735 *
1736 * Make a copy of both an &sk_buff and its data and while doing so
1737 * allocate additional space.
1738 *
1739 * This is used when the caller wishes to modify the data and needs a
1740 * private copy of the data to alter as well as more space for new fields.
1741 * Returns %NULL on failure or the pointer to the buffer
1742 * on success. The returned buffer has a reference count of 1.
1743 *
1744 * You must pass %GFP_ATOMIC as the allocation priority if this function
1745 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
1747struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001748 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001749 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 /*
1752 * Allocate the copy buffer
1753 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001754 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1755 gfp_mask, skb_alloc_rx_flag(skb),
1756 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001757 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int head_copy_len, head_copy_off;
1759
1760 if (!n)
1761 return NULL;
1762
1763 skb_reserve(n, newheadroom);
1764
1765 /* Set the tail pointer and length */
1766 skb_put(n, skb->len);
1767
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001768 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 head_copy_off = 0;
1770 if (newheadroom <= head_copy_len)
1771 head_copy_len = newheadroom;
1772 else
1773 head_copy_off = newheadroom - head_copy_len;
1774
1775 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001776 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1777 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001779 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Eric Dumazet030737b2013-10-19 11:42:54 -07001781 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return n;
1784}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001785EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001788 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 * @skb: buffer to pad
1790 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001791 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 *
1793 * Ensure that a buffer is followed by a padding area that is zero
1794 * filled. Used by network drivers which may DMA or transfer data
1795 * beyond the buffer end onto the wire.
1796 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001797 * May return error in out of memory cases. The skb is freed on error
1798 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001800
Florian Fainellicd0a1372017-08-22 15:12:14 -07001801int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Herbert Xu5b057c62006-06-23 02:06:41 -07001803 int err;
1804 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001807 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001811
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001812 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001813 if (likely(skb_cloned(skb) || ntail > 0)) {
1814 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1815 if (unlikely(err))
1816 goto free_skb;
1817 }
1818
1819 /* FIXME: The use of this function with non-linear skb's really needs
1820 * to be audited.
1821 */
1822 err = skb_linearize(skb);
1823 if (unlikely(err))
1824 goto free_skb;
1825
1826 memset(skb->data + skb->len, 0, pad);
1827 return 0;
1828
1829free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001830 if (free_on_error)
1831 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001832 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001833}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001834EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001835
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001836/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001837 * pskb_put - add data to the tail of a potentially fragmented buffer
1838 * @skb: start of the buffer to use
1839 * @tail: tail fragment of the buffer to use
1840 * @len: amount of data to add
1841 *
1842 * This function extends the used data area of the potentially
1843 * fragmented buffer. @tail must be the last fragment of @skb -- or
1844 * @skb itself. If this would exceed the total buffer size the kernel
1845 * will panic. A pointer to the first byte of the extra data is
1846 * returned.
1847 */
1848
Johannes Berg4df864c2017-06-16 14:29:21 +02001849void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001850{
1851 if (tail != skb) {
1852 skb->data_len += len;
1853 skb->len += len;
1854 }
1855 return skb_put(tail, len);
1856}
1857EXPORT_SYMBOL_GPL(pskb_put);
1858
1859/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001860 * skb_put - add data to a buffer
1861 * @skb: buffer to use
1862 * @len: amount of data to add
1863 *
1864 * This function extends the used data area of the buffer. If this would
1865 * exceed the total buffer size the kernel will panic. A pointer to the
1866 * first byte of the extra data is returned.
1867 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001868void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001869{
Johannes Berg4df864c2017-06-16 14:29:21 +02001870 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001871 SKB_LINEAR_ASSERT(skb);
1872 skb->tail += len;
1873 skb->len += len;
1874 if (unlikely(skb->tail > skb->end))
1875 skb_over_panic(skb, len, __builtin_return_address(0));
1876 return tmp;
1877}
1878EXPORT_SYMBOL(skb_put);
1879
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001880/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001881 * skb_push - add data to the start of a buffer
1882 * @skb: buffer to use
1883 * @len: amount of data to add
1884 *
1885 * This function extends the used data area of the buffer at the buffer
1886 * start. If this would exceed the total buffer headroom the kernel will
1887 * panic. A pointer to the first byte of the extra data is returned.
1888 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001889void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001890{
1891 skb->data -= len;
1892 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301893 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001894 skb_under_panic(skb, len, __builtin_return_address(0));
1895 return skb->data;
1896}
1897EXPORT_SYMBOL(skb_push);
1898
1899/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001900 * skb_pull - remove data from the start of a buffer
1901 * @skb: buffer to use
1902 * @len: amount of data to remove
1903 *
1904 * This function removes data from the start of a buffer, returning
1905 * the memory to the headroom. A pointer to the next data in the buffer
1906 * is returned. Once the data has been pulled future pushes will overwrite
1907 * the old data.
1908 */
Johannes Bergaf728682017-06-16 14:29:22 +02001909void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001910{
David S. Miller47d29642010-05-02 02:21:44 -07001911 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001912}
1913EXPORT_SYMBOL(skb_pull);
1914
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001915/**
1916 * skb_trim - remove end from a buffer
1917 * @skb: buffer to alter
1918 * @len: new length
1919 *
1920 * Cut the length of a buffer down by removing data from the tail. If
1921 * the buffer is already under the length specified it is not modified.
1922 * The skb must be linear.
1923 */
1924void skb_trim(struct sk_buff *skb, unsigned int len)
1925{
1926 if (skb->len > len)
1927 __skb_trim(skb, len);
1928}
1929EXPORT_SYMBOL(skb_trim);
1930
Herbert Xu3cc0e872006-06-09 16:13:38 -07001931/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
1933
Herbert Xu3cc0e872006-06-09 16:13:38 -07001934int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Herbert Xu27b437c2006-07-13 19:26:39 -07001936 struct sk_buff **fragp;
1937 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int offset = skb_headlen(skb);
1939 int nfrags = skb_shinfo(skb)->nr_frags;
1940 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001941 int err;
1942
1943 if (skb_cloned(skb) &&
1944 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001947 i = 0;
1948 if (offset >= len)
1949 goto drop_pages;
1950
1951 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001952 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001953
1954 if (end < len) {
1955 offset = end;
1956 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001958
Eric Dumazet9e903e02011-10-18 21:00:24 +00001959 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001960
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001961drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001962 skb_shinfo(skb)->nr_frags = i;
1963
1964 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001965 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001966
David S. Miller21dc3302010-08-23 00:13:46 -07001967 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001968 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001969 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
Herbert Xu27b437c2006-07-13 19:26:39 -07001972 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1973 fragp = &frag->next) {
1974 int end = offset + frag->len;
1975
1976 if (skb_shared(frag)) {
1977 struct sk_buff *nfrag;
1978
1979 nfrag = skb_clone(frag, GFP_ATOMIC);
1980 if (unlikely(!nfrag))
1981 return -ENOMEM;
1982
1983 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001984 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001985 frag = nfrag;
1986 *fragp = frag;
1987 }
1988
1989 if (end < len) {
1990 offset = end;
1991 continue;
1992 }
1993
1994 if (end > len &&
1995 unlikely((err = pskb_trim(frag, len - offset))))
1996 return err;
1997
1998 if (frag->next)
1999 skb_drop_list(&frag->next);
2000 break;
2001 }
2002
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002003done:
Herbert Xu27b437c2006-07-13 19:26:39 -07002004 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 skb->data_len -= skb->len - len;
2006 skb->len = len;
2007 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07002008 skb->len = len;
2009 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002010 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002013 if (!skb->sk || skb->destructor == sock_edemux)
2014 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return 0;
2016}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002017EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Eric Dumazet88078d92018-04-18 11:43:15 -07002019/* Note : use pskb_trim_rcsum() instead of calling this directly
2020 */
2021int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2022{
2023 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2024 int delta = skb->len - len;
2025
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002026 skb->csum = csum_block_sub(skb->csum,
2027 skb_checksum(skb, len, delta, 0),
2028 len);
Vasily Averin43f6ea42020-12-14 22:07:39 +03002029 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2030 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2031 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2032
2033 if (offset + sizeof(__sum16) > hdlen)
2034 return -EINVAL;
Eric Dumazet88078d92018-04-18 11:43:15 -07002035 }
2036 return __pskb_trim(skb, len);
2037}
2038EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040/**
2041 * __pskb_pull_tail - advance tail of skb header
2042 * @skb: buffer to reallocate
2043 * @delta: number of bytes to advance tail
2044 *
2045 * The function makes a sense only on a fragmented &sk_buff,
2046 * it expands header moving its tail forward and copying necessary
2047 * data from fragmented part.
2048 *
2049 * &sk_buff MUST have reference count of 1.
2050 *
2051 * Returns %NULL (and &sk_buff does not change) if pull failed
2052 * or value of new tail of skb in the case of success.
2053 *
2054 * All the pointers pointing into skb header may change and must be
2055 * reloaded after call to this function.
2056 */
2057
2058/* Moves tail of skb head forward, copying data from fragmented part,
2059 * when it is necessary.
2060 * 1. It may fail due to malloc failure.
2061 * 2. It may change skb pointers.
2062 *
2063 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2064 */
Johannes Bergaf728682017-06-16 14:29:22 +02002065void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 /* If skb has not enough free space at tail, get new one
2068 * plus 128 bytes for future expansions. If we have enough
2069 * room at tail, reallocate without expansion only if skb is cloned.
2070 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002071 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (eat > 0 || skb_cloned(skb)) {
2074 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2075 GFP_ATOMIC))
2076 return NULL;
2077 }
2078
Tim Hansen9f77fad2017-10-09 11:37:59 -04002079 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2080 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* Optimization: no fragments, no reasons to preestimate
2083 * size of pulled pages. Superb.
2084 */
David S. Miller21dc3302010-08-23 00:13:46 -07002085 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto pull_pages;
2087
2088 /* Estimate size of pulled pages. */
2089 eat = delta;
2090 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002091 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2092
2093 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002095 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
2098 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002099 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 * but taking into account that pulling is expected to
2101 * be very rare operation, it is worth to fight against
2102 * further bloating skb head and crucify ourselves here instead.
2103 * Pure masohism, indeed. 8)8)
2104 */
2105 if (eat) {
2106 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2107 struct sk_buff *clone = NULL;
2108 struct sk_buff *insp = NULL;
2109
2110 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (list->len <= eat) {
2112 /* Eaten as whole. */
2113 eat -= list->len;
2114 list = list->next;
2115 insp = list;
2116 } else {
2117 /* Eaten partially. */
2118
2119 if (skb_shared(list)) {
2120 /* Sucks! We need to fork list. :-( */
2121 clone = skb_clone(list, GFP_ATOMIC);
2122 if (!clone)
2123 return NULL;
2124 insp = list->next;
2125 list = clone;
2126 } else {
2127 /* This may be pulled without
2128 * problems. */
2129 insp = list;
2130 }
2131 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002132 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return NULL;
2134 }
2135 break;
2136 }
2137 } while (eat);
2138
2139 /* Free pulled out fragments. */
2140 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2141 skb_shinfo(skb)->frag_list = list->next;
2142 kfree_skb(list);
2143 }
2144 /* And insert new clone at head. */
2145 if (clone) {
2146 clone->next = list;
2147 skb_shinfo(skb)->frag_list = clone;
2148 }
2149 }
2150 /* Success! Now we may commit changes to skb data. */
2151
2152pull_pages:
2153 eat = delta;
2154 k = 0;
2155 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002156 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2157
2158 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002159 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002160 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 } else {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002162 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2163
2164 *frag = skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (eat) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002166 skb_frag_off_add(frag, eat);
2167 skb_frag_size_sub(frag, eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002168 if (!i)
2169 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 eat = 0;
2171 }
2172 k++;
2173 }
2174 }
2175 skb_shinfo(skb)->nr_frags = k;
2176
linzhang3ccc6c62017-07-17 17:25:02 +08002177end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 skb->tail += delta;
2179 skb->data_len -= delta;
2180
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002181 if (!skb->data_len)
2182 skb_zcopy_clear(skb, false);
2183
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002184 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002186EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Eric Dumazet22019b12011-07-29 18:37:31 +00002188/**
2189 * skb_copy_bits - copy bits from skb to kernel buffer
2190 * @skb: source skb
2191 * @offset: offset in source
2192 * @to: destination buffer
2193 * @len: number of bytes to copy
2194 *
2195 * Copy the specified number of bytes from the source skb to the
2196 * destination buffer.
2197 *
2198 * CAUTION ! :
2199 * If its prototype is ever changed,
2200 * check arch/{*}/net/{*}.S files,
2201 * since it is called from BPF assembly code.
2202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2204{
David S. Miller1a028e52007-04-27 15:21:23 -07002205 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002206 struct sk_buff *frag_iter;
2207 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 if (offset > (int)skb->len - len)
2210 goto fault;
2211
2212 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002213 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (copy > len)
2215 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002216 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if ((len -= copy) == 0)
2218 return 0;
2219 offset += copy;
2220 to += copy;
2221 }
2222
2223 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002224 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002225 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002227 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002228
Eric Dumazet51c56b02012-04-05 11:35:15 +02002229 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002231 u32 p_off, p_len, copied;
2232 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 u8 *vaddr;
2234
2235 if (copy > len)
2236 copy = len;
2237
Willem de Bruijnc613c202017-07-31 08:15:47 -04002238 skb_frag_foreach_page(f,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002239 skb_frag_off(f) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002240 copy, p, p_off, p_len, copied) {
2241 vaddr = kmap_atomic(p);
2242 memcpy(to + copied, vaddr + p_off, p_len);
2243 kunmap_atomic(vaddr);
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 if ((len -= copy) == 0)
2247 return 0;
2248 offset += copy;
2249 to += copy;
2250 }
David S. Miller1a028e52007-04-27 15:21:23 -07002251 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253
David S. Millerfbb398a2009-06-09 00:18:59 -07002254 skb_walk_frags(skb, frag_iter) {
2255 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
David S. Millerfbb398a2009-06-09 00:18:59 -07002257 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
David S. Millerfbb398a2009-06-09 00:18:59 -07002259 end = start + frag_iter->len;
2260 if ((copy = end - offset) > 0) {
2261 if (copy > len)
2262 copy = len;
2263 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2264 goto fault;
2265 if ((len -= copy) == 0)
2266 return 0;
2267 offset += copy;
2268 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002270 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Shirley Maa6686f22011-07-06 12:22:12 +00002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!len)
2274 return 0;
2275
2276fault:
2277 return -EFAULT;
2278}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002279EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Jens Axboe9c55e012007-11-06 23:30:13 -08002281/*
2282 * Callback from splice_to_pipe(), if we need to release some pages
2283 * at the end of the spd in case we error'ed out in filling the pipe.
2284 */
2285static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2286{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002287 put_page(spd->pages[i]);
2288}
Jens Axboe9c55e012007-11-06 23:30:13 -08002289
David S. Millera108d5f2012-04-23 23:06:11 -04002290static struct page *linear_to_page(struct page *page, unsigned int *len,
2291 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002292 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002293{
Eric Dumazet5640f762012-09-23 23:04:42 +00002294 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002295
Eric Dumazet5640f762012-09-23 23:04:42 +00002296 if (!sk_page_frag_refill(sk, pfrag))
2297 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002298
Eric Dumazet5640f762012-09-23 23:04:42 +00002299 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002300
Eric Dumazet5640f762012-09-23 23:04:42 +00002301 memcpy(page_address(pfrag->page) + pfrag->offset,
2302 page_address(page) + *offset, *len);
2303 *offset = pfrag->offset;
2304 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002305
Eric Dumazet5640f762012-09-23 23:04:42 +00002306 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002307}
2308
Eric Dumazet41c73a02012-04-22 12:26:16 +00002309static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2310 struct page *page,
2311 unsigned int offset)
2312{
2313 return spd->nr_pages &&
2314 spd->pages[spd->nr_pages - 1] == page &&
2315 (spd->partial[spd->nr_pages - 1].offset +
2316 spd->partial[spd->nr_pages - 1].len == offset);
2317}
2318
Jens Axboe9c55e012007-11-06 23:30:13 -08002319/*
2320 * Fill page/offset/length into spd, if it can hold more pages.
2321 */
David S. Millera108d5f2012-04-23 23:06:11 -04002322static bool spd_fill_page(struct splice_pipe_desc *spd,
2323 struct pipe_inode_info *pipe, struct page *page,
2324 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002325 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002326 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002327{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002328 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002329 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002330
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002331 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002332 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002333 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002334 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002335 }
2336 if (spd_can_coalesce(spd, page, offset)) {
2337 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002338 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002339 }
2340 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002341 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002342 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002343 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002344 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002345
David S. Millera108d5f2012-04-23 23:06:11 -04002346 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002347}
2348
David S. Millera108d5f2012-04-23 23:06:11 -04002349static bool __splice_segment(struct page *page, unsigned int poff,
2350 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002351 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002352 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002353 struct sock *sk,
2354 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002355{
2356 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002357 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002358
2359 /* skip this segment if already processed */
2360 if (*off >= plen) {
2361 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002362 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002363 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002364
Octavian Purdila2870c432008-07-15 00:49:11 -07002365 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002366 poff += *off;
2367 plen -= *off;
2368 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002369
Eric Dumazet18aafc62013-01-11 14:46:37 +00002370 do {
2371 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002372
Eric Dumazet18aafc62013-01-11 14:46:37 +00002373 if (spd_fill_page(spd, pipe, page, &flen, poff,
2374 linear, sk))
2375 return true;
2376 poff += flen;
2377 plen -= flen;
2378 *len -= flen;
2379 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002380
David S. Millera108d5f2012-04-23 23:06:11 -04002381 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002382}
2383
2384/*
David S. Millera108d5f2012-04-23 23:06:11 -04002385 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002386 * pipe is full or if we already spliced the requested length.
2387 */
David S. Millera108d5f2012-04-23 23:06:11 -04002388static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2389 unsigned int *offset, unsigned int *len,
2390 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002391{
2392 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002393 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002394
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002395 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002396 * If skb->head_frag is set, this 'linear' part is backed by a
2397 * fragment, and if the head is not shared with any clones then
2398 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002399 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002400 if (__splice_segment(virt_to_page(skb->data),
2401 (unsigned long) skb->data & (PAGE_SIZE - 1),
2402 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002403 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002404 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002405 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002406 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002407
2408 /*
2409 * then map the fragments
2410 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002411 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2412 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2413
Ian Campbellea2ab692011-08-22 23:44:58 +00002414 if (__splice_segment(skb_frag_page(f),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002415 skb_frag_off(f), skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002416 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002417 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002418 }
2419
Tom Herbertfa9835e2016-03-07 14:11:04 -08002420 skb_walk_frags(skb, iter) {
2421 if (*offset >= iter->len) {
2422 *offset -= iter->len;
2423 continue;
2424 }
2425 /* __skb_splice_bits() only fails if the output has no room
2426 * left, so no point in going over the frag_list for the error
2427 * case.
2428 */
2429 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2430 return true;
2431 }
2432
David S. Millera108d5f2012-04-23 23:06:11 -04002433 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002434}
2435
2436/*
2437 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002438 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002439 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002440int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002441 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002442 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002443{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002444 struct partial_page partial[MAX_SKB_FRAGS];
2445 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002446 struct splice_pipe_desc spd = {
2447 .pages = pages,
2448 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002449 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002450 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002451 .spd_release = sock_spd_release,
2452 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002453 int ret = 0;
2454
Tom Herbertfa9835e2016-03-07 14:11:04 -08002455 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002456
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002457 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002458 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002459
Jens Axboe35f3d142010-05-20 10:43:18 +02002460 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002461}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002462EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002463
Tom Herbert20bf50d2017-07-28 16:22:42 -07002464/* Send skb data on a socket. Socket must be locked. */
2465int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2466 int len)
2467{
2468 unsigned int orig_len = len;
2469 struct sk_buff *head = skb;
2470 unsigned short fragidx;
2471 int slen, ret;
2472
2473do_frag_list:
2474
2475 /* Deal with head data */
2476 while (offset < skb_headlen(skb) && len) {
2477 struct kvec kv;
2478 struct msghdr msg;
2479
2480 slen = min_t(int, len, skb_headlen(skb) - offset);
2481 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002482 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002483 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002484 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002485
2486 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2487 if (ret <= 0)
2488 goto error;
2489
2490 offset += ret;
2491 len -= ret;
2492 }
2493
2494 /* All the data was skb head? */
2495 if (!len)
2496 goto out;
2497
2498 /* Make offset relative to start of frags */
2499 offset -= skb_headlen(skb);
2500
2501 /* Find where we are in frag list */
2502 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2503 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2504
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002505 if (offset < skb_frag_size(frag))
Tom Herbert20bf50d2017-07-28 16:22:42 -07002506 break;
2507
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002508 offset -= skb_frag_size(frag);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002509 }
2510
2511 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2512 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2513
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002514 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002515
2516 while (slen) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002517 ret = kernel_sendpage_locked(sk, skb_frag_page(frag),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002518 skb_frag_off(frag) + offset,
Tom Herbert20bf50d2017-07-28 16:22:42 -07002519 slen, MSG_DONTWAIT);
2520 if (ret <= 0)
2521 goto error;
2522
2523 len -= ret;
2524 offset += ret;
2525 slen -= ret;
2526 }
2527
2528 offset = 0;
2529 }
2530
2531 if (len) {
2532 /* Process any frag lists */
2533
2534 if (skb == head) {
2535 if (skb_has_frag_list(skb)) {
2536 skb = skb_shinfo(skb)->frag_list;
2537 goto do_frag_list;
2538 }
2539 } else if (skb->next) {
2540 skb = skb->next;
2541 goto do_frag_list;
2542 }
2543 }
2544
2545out:
2546 return orig_len - len;
2547
2548error:
2549 return orig_len == len ? ret : orig_len - len;
2550}
2551EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2552
Herbert Xu357b40a2005-04-19 22:30:14 -07002553/**
2554 * skb_store_bits - store bits from kernel buffer to skb
2555 * @skb: destination buffer
2556 * @offset: offset in destination
2557 * @from: source buffer
2558 * @len: number of bytes to copy
2559 *
2560 * Copy the specified number of bytes from the source buffer to the
2561 * destination skb. This function handles all the messy bits of
2562 * traversing fragment lists and such.
2563 */
2564
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002565int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002566{
David S. Miller1a028e52007-04-27 15:21:23 -07002567 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002568 struct sk_buff *frag_iter;
2569 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002570
2571 if (offset > (int)skb->len - len)
2572 goto fault;
2573
David S. Miller1a028e52007-04-27 15:21:23 -07002574 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002575 if (copy > len)
2576 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002577 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002578 if ((len -= copy) == 0)
2579 return 0;
2580 offset += copy;
2581 from += copy;
2582 }
2583
2584 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2585 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002586 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002587
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002588 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002589
Eric Dumazet9e903e02011-10-18 21:00:24 +00002590 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002591 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002592 u32 p_off, p_len, copied;
2593 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002594 u8 *vaddr;
2595
2596 if (copy > len)
2597 copy = len;
2598
Willem de Bruijnc613c202017-07-31 08:15:47 -04002599 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002600 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002601 copy, p, p_off, p_len, copied) {
2602 vaddr = kmap_atomic(p);
2603 memcpy(vaddr + p_off, from + copied, p_len);
2604 kunmap_atomic(vaddr);
2605 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002606
2607 if ((len -= copy) == 0)
2608 return 0;
2609 offset += copy;
2610 from += copy;
2611 }
David S. Miller1a028e52007-04-27 15:21:23 -07002612 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002613 }
2614
David S. Millerfbb398a2009-06-09 00:18:59 -07002615 skb_walk_frags(skb, frag_iter) {
2616 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002617
David S. Millerfbb398a2009-06-09 00:18:59 -07002618 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002619
David S. Millerfbb398a2009-06-09 00:18:59 -07002620 end = start + frag_iter->len;
2621 if ((copy = end - offset) > 0) {
2622 if (copy > len)
2623 copy = len;
2624 if (skb_store_bits(frag_iter, offset - start,
2625 from, copy))
2626 goto fault;
2627 if ((len -= copy) == 0)
2628 return 0;
2629 offset += copy;
2630 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002631 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002632 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002633 }
2634 if (!len)
2635 return 0;
2636
2637fault:
2638 return -EFAULT;
2639}
Herbert Xu357b40a2005-04-19 22:30:14 -07002640EXPORT_SYMBOL(skb_store_bits);
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002643__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2644 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
David S. Miller1a028e52007-04-27 15:21:23 -07002646 int start = skb_headlen(skb);
2647 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002648 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 int pos = 0;
2650
2651 /* Checksum header. */
2652 if (copy > 0) {
2653 if (copy > len)
2654 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002655 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2656 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if ((len -= copy) == 0)
2658 return csum;
2659 offset += copy;
2660 pos = copy;
2661 }
2662
2663 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002664 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002665 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002667 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002668
Eric Dumazet51c56b02012-04-05 11:35:15 +02002669 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002671 u32 p_off, p_len, copied;
2672 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002673 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 if (copy > len)
2677 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002678
2679 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002680 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002681 copy, p, p_off, p_len, copied) {
2682 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002683 csum2 = INDIRECT_CALL_1(ops->update,
2684 csum_partial_ext,
2685 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002686 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002687 csum = INDIRECT_CALL_1(ops->combine,
2688 csum_block_add_ext, csum,
2689 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002690 pos += p_len;
2691 }
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (!(len -= copy))
2694 return csum;
2695 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
David S. Miller1a028e52007-04-27 15:21:23 -07002697 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
David S. Millerfbb398a2009-06-09 00:18:59 -07002700 skb_walk_frags(skb, frag_iter) {
2701 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
David S. Millerfbb398a2009-06-09 00:18:59 -07002703 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
David S. Millerfbb398a2009-06-09 00:18:59 -07002705 end = start + frag_iter->len;
2706 if ((copy = end - offset) > 0) {
2707 __wsum csum2;
2708 if (copy > len)
2709 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002710 csum2 = __skb_checksum(frag_iter, offset - start,
2711 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002712 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2713 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002714 if ((len -= copy) == 0)
2715 return csum;
2716 offset += copy;
2717 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002719 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002721 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 return csum;
2724}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002725EXPORT_SYMBOL(__skb_checksum);
2726
2727__wsum skb_checksum(const struct sk_buff *skb, int offset,
2728 int len, __wsum csum)
2729{
2730 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002731 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002732 .combine = csum_block_add_ext,
2733 };
2734
2735 return __skb_checksum(skb, offset, len, csum, &ops);
2736}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002737EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739/* Both of above in one bottle. */
2740
Al Viro81d77662006-11-14 21:37:33 -08002741__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
Al Viro8d5930d2020-07-10 20:07:10 -04002742 u8 *to, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
David S. Miller1a028e52007-04-27 15:21:23 -07002744 int start = skb_headlen(skb);
2745 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002746 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int pos = 0;
Al Viro8d5930d2020-07-10 20:07:10 -04002748 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 /* Copy header. */
2751 if (copy > 0) {
2752 if (copy > len)
2753 copy = len;
2754 csum = csum_partial_copy_nocheck(skb->data + offset, to,
Al Virocc44c172020-07-11 00:12:07 -04002755 copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if ((len -= copy) == 0)
2757 return csum;
2758 offset += copy;
2759 to += copy;
2760 pos = copy;
2761 }
2762
2763 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002764 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002766 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002767
Eric Dumazet9e903e02011-10-18 21:00:24 +00002768 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002770 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2771 u32 p_off, p_len, copied;
2772 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002773 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 if (copy > len)
2777 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002778
2779 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002780 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002781 copy, p, p_off, p_len, copied) {
2782 vaddr = kmap_atomic(p);
2783 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2784 to + copied,
Al Virocc44c172020-07-11 00:12:07 -04002785 p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002786 kunmap_atomic(vaddr);
2787 csum = csum_block_add(csum, csum2, pos);
2788 pos += p_len;
2789 }
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (!(len -= copy))
2792 return csum;
2793 offset += copy;
2794 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
David S. Miller1a028e52007-04-27 15:21:23 -07002796 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
David S. Millerfbb398a2009-06-09 00:18:59 -07002799 skb_walk_frags(skb, frag_iter) {
2800 __wsum csum2;
2801 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
David S. Millerfbb398a2009-06-09 00:18:59 -07002803 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
David S. Millerfbb398a2009-06-09 00:18:59 -07002805 end = start + frag_iter->len;
2806 if ((copy = end - offset) > 0) {
2807 if (copy > len)
2808 copy = len;
2809 csum2 = skb_copy_and_csum_bits(frag_iter,
2810 offset - start,
Al Viro8d5930d2020-07-10 20:07:10 -04002811 to, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002812 csum = csum_block_add(csum, csum2, pos);
2813 if ((len -= copy) == 0)
2814 return csum;
2815 offset += copy;
2816 to += copy;
2817 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002819 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002821 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return csum;
2823}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002824EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Cong Wang49f8e832018-11-08 14:05:42 -08002826__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2827{
2828 __sum16 sum;
2829
2830 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002831 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002832 if (likely(!sum)) {
2833 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2834 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002835 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002836 }
2837 if (!skb_shared(skb))
2838 skb->csum_valid = !sum;
2839 return sum;
2840}
2841EXPORT_SYMBOL(__skb_checksum_complete_head);
2842
Cong Wang14641932018-11-26 09:31:26 -08002843/* This function assumes skb->csum already holds pseudo header's checksum,
2844 * which has been changed from the hardware checksum, for example, by
2845 * __skb_checksum_validate_complete(). And, the original skb->csum must
2846 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2847 *
2848 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2849 * zero. The new checksum is stored back into skb->csum unless the skb is
2850 * shared.
2851 */
Cong Wang49f8e832018-11-08 14:05:42 -08002852__sum16 __skb_checksum_complete(struct sk_buff *skb)
2853{
2854 __wsum csum;
2855 __sum16 sum;
2856
2857 csum = skb_checksum(skb, 0, skb->len, 0);
2858
Cong Wang49f8e832018-11-08 14:05:42 -08002859 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002860 /* This check is inverted, because we already knew the hardware
2861 * checksum is invalid before calling this function. So, if the
2862 * re-computed checksum is valid instead, then we have a mismatch
2863 * between the original skb->csum and skb_checksum(). This means either
2864 * the original hardware checksum is incorrect or we screw up skb->csum
2865 * when moving skb->data around.
2866 */
Cong Wang49f8e832018-11-08 14:05:42 -08002867 if (likely(!sum)) {
2868 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2869 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002870 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002871 }
2872
2873 if (!skb_shared(skb)) {
2874 /* Save full packet checksum */
2875 skb->csum = csum;
2876 skb->ip_summed = CHECKSUM_COMPLETE;
2877 skb->csum_complete_sw = 1;
2878 skb->csum_valid = !sum;
2879 }
2880
2881 return sum;
2882}
2883EXPORT_SYMBOL(__skb_checksum_complete);
2884
Davide Caratti96178132017-05-18 15:44:37 +02002885static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2886{
2887 net_warn_ratelimited(
2888 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2889 __func__);
2890 return 0;
2891}
2892
2893static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2894 int offset, int len)
2895{
2896 net_warn_ratelimited(
2897 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2898 __func__);
2899 return 0;
2900}
2901
2902static const struct skb_checksum_ops default_crc32c_ops = {
2903 .update = warn_crc32c_csum_update,
2904 .combine = warn_crc32c_csum_combine,
2905};
2906
2907const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2908 &default_crc32c_ops;
2909EXPORT_SYMBOL(crc32c_csum_stub);
2910
Thomas Grafaf2806f2013-12-13 15:22:17 +01002911 /**
2912 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2913 * @from: source buffer
2914 *
2915 * Calculates the amount of linear headroom needed in the 'to' skb passed
2916 * into skb_zerocopy().
2917 */
2918unsigned int
2919skb_zerocopy_headlen(const struct sk_buff *from)
2920{
2921 unsigned int hlen = 0;
2922
2923 if (!from->head_frag ||
2924 skb_headlen(from) < L1_CACHE_BYTES ||
Pravin B Shelare8b287e2021-07-15 16:59:00 -07002925 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
Thomas Grafaf2806f2013-12-13 15:22:17 +01002926 hlen = skb_headlen(from);
Pravin B Shelare8b287e2021-07-15 16:59:00 -07002927 if (!hlen)
2928 hlen = from->len;
2929 }
Thomas Grafaf2806f2013-12-13 15:22:17 +01002930
2931 if (skb_has_frag_list(from))
2932 hlen = from->len;
2933
2934 return hlen;
2935}
2936EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2937
2938/**
2939 * skb_zerocopy - Zero copy skb to skb
2940 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002941 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002942 * @len: number of bytes to copy from source buffer
2943 * @hlen: size of linear headroom in destination buffer
2944 *
2945 * Copies up to `len` bytes from `from` to `to` by creating references
2946 * to the frags in the source buffer.
2947 *
2948 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2949 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002950 *
2951 * Return value:
2952 * 0: everything is OK
2953 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2954 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002955 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002956int
2957skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002958{
2959 int i, j = 0;
2960 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002961 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002962 struct page *page;
2963 unsigned int offset;
2964
2965 BUG_ON(!from->head_frag && !hlen);
2966
2967 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002968 if (len <= skb_tailroom(to))
2969 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002970
2971 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002972 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2973 if (unlikely(ret))
2974 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002975 len -= hlen;
2976 } else {
2977 plen = min_t(int, skb_headlen(from), len);
2978 if (plen) {
2979 page = virt_to_head_page(from->head);
2980 offset = from->data - (unsigned char *)page_address(page);
2981 __skb_fill_page_desc(to, 0, page, offset, plen);
2982 get_page(page);
2983 j = 1;
2984 len -= plen;
2985 }
2986 }
2987
2988 to->truesize += len + plen;
2989 to->len += len + plen;
2990 to->data_len += len + plen;
2991
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002992 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2993 skb_tx_error(from);
2994 return -ENOMEM;
2995 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002996 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002997
Thomas Grafaf2806f2013-12-13 15:22:17 +01002998 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002999 int size;
3000
Thomas Grafaf2806f2013-12-13 15:22:17 +01003001 if (!len)
3002 break;
3003 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003004 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3005 len);
3006 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3007 len -= size;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003008 skb_frag_ref(to, j);
3009 j++;
3010 }
3011 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003012
3013 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003014}
3015EXPORT_SYMBOL_GPL(skb_zerocopy);
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3018{
Al Virod3bc23e2006-11-14 21:24:49 -08003019 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 long csstart;
3021
Patrick McHardy84fa7932006-08-29 16:44:56 -07003022 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00003023 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 else
3025 csstart = skb_headlen(skb);
3026
Kris Katterjohn09a62662006-01-08 22:24:28 -08003027 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003029 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 csum = 0;
3032 if (csstart != skb->len)
3033 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
Al Viro8d5930d2020-07-10 20:07:10 -04003034 skb->len - csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Patrick McHardy84fa7932006-08-29 16:44:56 -07003036 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003037 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Al Virod3bc23e2006-11-14 21:24:49 -08003039 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003042EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044/**
3045 * skb_dequeue - remove from the head of the queue
3046 * @list: list to dequeue from
3047 *
3048 * Remove the head of the list. The list lock is taken so the function
3049 * may be used safely with other locking list functions. The head item is
3050 * returned or %NULL if the list is empty.
3051 */
3052
3053struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3054{
3055 unsigned long flags;
3056 struct sk_buff *result;
3057
3058 spin_lock_irqsave(&list->lock, flags);
3059 result = __skb_dequeue(list);
3060 spin_unlock_irqrestore(&list->lock, flags);
3061 return result;
3062}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003063EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065/**
3066 * skb_dequeue_tail - remove from the tail of the queue
3067 * @list: list to dequeue from
3068 *
3069 * Remove the tail of the list. The list lock is taken so the function
3070 * may be used safely with other locking list functions. The tail item is
3071 * returned or %NULL if the list is empty.
3072 */
3073struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3074{
3075 unsigned long flags;
3076 struct sk_buff *result;
3077
3078 spin_lock_irqsave(&list->lock, flags);
3079 result = __skb_dequeue_tail(list);
3080 spin_unlock_irqrestore(&list->lock, flags);
3081 return result;
3082}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003083EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085/**
3086 * skb_queue_purge - empty a list
3087 * @list: list to empty
3088 *
3089 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3090 * the list and one reference dropped. This function takes the list
3091 * lock and is atomic with respect to other list locking functions.
3092 */
3093void skb_queue_purge(struct sk_buff_head *list)
3094{
3095 struct sk_buff *skb;
3096 while ((skb = skb_dequeue(list)) != NULL)
3097 kfree_skb(skb);
3098}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003099EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003102 * skb_rbtree_purge - empty a skb rbtree
3103 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003104 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003105 *
3106 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3107 * the list and one reference dropped. This function does not take
3108 * any lock. Synchronization should be handled by the caller (e.g., TCP
3109 * out-of-order queue is protected by the socket lock).
3110 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003111unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003112{
Eric Dumazet7c905842017-09-23 12:39:12 -07003113 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003114 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003115
Eric Dumazet7c905842017-09-23 12:39:12 -07003116 while (p) {
3117 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3118
3119 p = rb_next(p);
3120 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003121 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003122 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003123 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003124 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003125}
3126
3127/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 * skb_queue_head - queue a buffer at the list head
3129 * @list: list to use
3130 * @newsk: buffer to queue
3131 *
3132 * Queue a buffer at the start of the list. This function takes the
3133 * list lock and can be used safely with other locking &sk_buff functions
3134 * safely.
3135 *
3136 * A buffer cannot be placed on two lists at the same time.
3137 */
3138void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3139{
3140 unsigned long flags;
3141
3142 spin_lock_irqsave(&list->lock, flags);
3143 __skb_queue_head(list, newsk);
3144 spin_unlock_irqrestore(&list->lock, flags);
3145}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003146EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148/**
3149 * skb_queue_tail - queue a buffer at the list tail
3150 * @list: list to use
3151 * @newsk: buffer to queue
3152 *
3153 * Queue a buffer at the tail of the list. This function takes the
3154 * list lock and can be used safely with other locking &sk_buff functions
3155 * safely.
3156 *
3157 * A buffer cannot be placed on two lists at the same time.
3158 */
3159void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3160{
3161 unsigned long flags;
3162
3163 spin_lock_irqsave(&list->lock, flags);
3164 __skb_queue_tail(list, newsk);
3165 spin_unlock_irqrestore(&list->lock, flags);
3166}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003167EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169/**
3170 * skb_unlink - remove a buffer from a list
3171 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003172 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 *
David S. Miller8728b832005-08-09 19:25:21 -07003174 * Remove a packet from a list. The list locks are taken and this
3175 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 *
David S. Miller8728b832005-08-09 19:25:21 -07003177 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
David S. Miller8728b832005-08-09 19:25:21 -07003179void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
David S. Miller8728b832005-08-09 19:25:21 -07003181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
David S. Miller8728b832005-08-09 19:25:21 -07003183 spin_lock_irqsave(&list->lock, flags);
3184 __skb_unlink(skb, list);
3185 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003187EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/**
3190 * skb_append - append a buffer
3191 * @old: buffer to insert after
3192 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003193 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 *
3195 * Place a packet after a given packet in a list. The list locks are taken
3196 * and this function is atomic with respect to other list locked calls.
3197 * A buffer cannot be placed on two lists at the same time.
3198 */
David S. Miller8728b832005-08-09 19:25:21 -07003199void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 unsigned long flags;
3202
David S. Miller8728b832005-08-09 19:25:21 -07003203 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003204 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003205 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003207EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209static inline void skb_split_inside_header(struct sk_buff *skb,
3210 struct sk_buff* skb1,
3211 const u32 len, const int pos)
3212{
3213 int i;
3214
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003215 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3216 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 /* And move data appendix as is. */
3218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3219 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3220
3221 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3222 skb_shinfo(skb)->nr_frags = 0;
3223 skb1->data_len = skb->data_len;
3224 skb1->len += skb1->data_len;
3225 skb->data_len = 0;
3226 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003227 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
3230static inline void skb_split_no_header(struct sk_buff *skb,
3231 struct sk_buff* skb1,
3232 const u32 len, int pos)
3233{
3234 int i, k = 0;
3235 const int nfrags = skb_shinfo(skb)->nr_frags;
3236
3237 skb_shinfo(skb)->nr_frags = 0;
3238 skb1->len = skb1->data_len = skb->len - len;
3239 skb->len = len;
3240 skb->data_len = len - pos;
3241
3242 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003243 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 if (pos + size > len) {
3246 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3247
3248 if (pos < len) {
3249 /* Split frag.
3250 * We have two variants in this case:
3251 * 1. Move all the frag to the second
3252 * part, if it is possible. F.e.
3253 * this approach is mandatory for TUX,
3254 * where splitting is expensive.
3255 * 2. Split is accurately. We make this.
3256 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003257 skb_frag_ref(skb, i);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003258 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003259 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3260 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 skb_shinfo(skb)->nr_frags++;
3262 }
3263 k++;
3264 } else
3265 skb_shinfo(skb)->nr_frags++;
3266 pos += size;
3267 }
3268 skb_shinfo(skb1)->nr_frags = k;
3269}
3270
3271/**
3272 * skb_split - Split fragmented skb to two parts at length len.
3273 * @skb: the buffer to split
3274 * @skb1: the buffer to receive the second part
3275 * @len: new length for skb
3276 */
3277void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3278{
3279 int pos = skb_headlen(skb);
3280
Willem de Bruijnfff88032017-06-08 11:35:03 -04003281 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3282 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003283 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (len < pos) /* Split line is inside header. */
3285 skb_split_inside_header(skb, skb1, len, pos);
3286 else /* Second chunk has no header, nothing to copy. */
3287 skb_split_no_header(skb, skb1, len, pos);
3288}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003289EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003291/* Shifting from/to a cloned skb is a no-go.
3292 *
3293 * Caller cannot keep skb_shinfo related pointers past calling here!
3294 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003295static int skb_prepare_for_shift(struct sk_buff *skb)
3296{
Marco Elver97ff09a2021-02-01 17:04:20 +01003297 int ret = 0;
3298
3299 if (skb_cloned(skb)) {
3300 /* Save and restore truesize: pskb_expand_head() may reallocate
3301 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3302 * cannot change truesize at this point.
3303 */
3304 unsigned int save_truesize = skb->truesize;
3305
3306 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3307 skb->truesize = save_truesize;
3308 }
3309 return ret;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003310}
3311
3312/**
3313 * skb_shift - Shifts paged data partially from skb to another
3314 * @tgt: buffer into which tail data gets added
3315 * @skb: buffer from which the paged data comes from
3316 * @shiftlen: shift up to this many bytes
3317 *
3318 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003319 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003320 * It's up to caller to free skb if everything was shifted.
3321 *
3322 * If @tgt runs out of frags, the whole operation is aborted.
3323 *
3324 * Skb cannot include anything else but paged data while tgt is allowed
3325 * to have non-paged data as well.
3326 *
3327 * TODO: full sized shift could be optimized but that would need
3328 * specialized skb free'er to handle frags without up-to-date nr_frags.
3329 */
3330int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3331{
3332 int from, to, merge, todo;
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003333 skb_frag_t *fragfrom, *fragto;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003334
3335 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003336
3337 if (skb_headlen(skb))
3338 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003339 if (skb_zcopy(tgt) || skb_zcopy(skb))
3340 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003341
3342 todo = shiftlen;
3343 from = 0;
3344 to = skb_shinfo(tgt)->nr_frags;
3345 fragfrom = &skb_shinfo(skb)->frags[from];
3346
3347 /* Actual merge is delayed until the point when we know we can
3348 * commit all, so that we don't have to undo partial changes
3349 */
3350 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003351 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003352 skb_frag_off(fragfrom))) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003353 merge = -1;
3354 } else {
3355 merge = to - 1;
3356
Eric Dumazet9e903e02011-10-18 21:00:24 +00003357 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003358 if (todo < 0) {
3359 if (skb_prepare_for_shift(skb) ||
3360 skb_prepare_for_shift(tgt))
3361 return 0;
3362
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003363 /* All previous frag pointers might be stale! */
3364 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003365 fragto = &skb_shinfo(tgt)->frags[merge];
3366
Eric Dumazet9e903e02011-10-18 21:00:24 +00003367 skb_frag_size_add(fragto, shiftlen);
3368 skb_frag_size_sub(fragfrom, shiftlen);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003369 skb_frag_off_add(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003370
3371 goto onlymerged;
3372 }
3373
3374 from++;
3375 }
3376
3377 /* Skip full, not-fitting skb to avoid expensive operations */
3378 if ((shiftlen == skb->len) &&
3379 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3380 return 0;
3381
3382 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3383 return 0;
3384
3385 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3386 if (to == MAX_SKB_FRAGS)
3387 return 0;
3388
3389 fragfrom = &skb_shinfo(skb)->frags[from];
3390 fragto = &skb_shinfo(tgt)->frags[to];
3391
Eric Dumazet9e903e02011-10-18 21:00:24 +00003392 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003393 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003394 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003395 from++;
3396 to++;
3397
3398 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003399 __skb_frag_ref(fragfrom);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003400 skb_frag_page_copy(fragto, fragfrom);
3401 skb_frag_off_copy(fragto, fragfrom);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003402 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003403
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003404 skb_frag_off_add(fragfrom, todo);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003405 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003406 todo = 0;
3407
3408 to++;
3409 break;
3410 }
3411 }
3412
3413 /* Ready to "commit" this state change to tgt */
3414 skb_shinfo(tgt)->nr_frags = to;
3415
3416 if (merge >= 0) {
3417 fragfrom = &skb_shinfo(skb)->frags[0];
3418 fragto = &skb_shinfo(tgt)->frags[merge];
3419
Eric Dumazet9e903e02011-10-18 21:00:24 +00003420 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003421 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003422 }
3423
3424 /* Reposition in the original skb */
3425 to = 0;
3426 while (from < skb_shinfo(skb)->nr_frags)
3427 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3428 skb_shinfo(skb)->nr_frags = to;
3429
3430 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3431
3432onlymerged:
3433 /* Most likely the tgt won't ever need its checksum anymore, skb on
3434 * the other hand might need it if it needs to be resent
3435 */
3436 tgt->ip_summed = CHECKSUM_PARTIAL;
3437 skb->ip_summed = CHECKSUM_PARTIAL;
3438
3439 /* Yak, is it really working this way? Some helper please? */
3440 skb->len -= shiftlen;
3441 skb->data_len -= shiftlen;
3442 skb->truesize -= shiftlen;
3443 tgt->len += shiftlen;
3444 tgt->data_len += shiftlen;
3445 tgt->truesize += shiftlen;
3446
3447 return shiftlen;
3448}
3449
Thomas Graf677e90e2005-06-23 20:59:51 -07003450/**
3451 * skb_prepare_seq_read - Prepare a sequential read of skb data
3452 * @skb: the buffer to read
3453 * @from: lower offset of data to be read
3454 * @to: upper offset of data to be read
3455 * @st: state variable
3456 *
3457 * Initializes the specified state variable. Must be called before
3458 * invoking skb_seq_read() for the first time.
3459 */
3460void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3461 unsigned int to, struct skb_seq_state *st)
3462{
3463 st->lower_offset = from;
3464 st->upper_offset = to;
3465 st->root_skb = st->cur_skb = skb;
3466 st->frag_idx = st->stepped_offset = 0;
3467 st->frag_data = NULL;
3468}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003469EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003470
3471/**
3472 * skb_seq_read - Sequentially read skb data
3473 * @consumed: number of bytes consumed by the caller so far
3474 * @data: destination pointer for data to be returned
3475 * @st: state variable
3476 *
Mathias Krausebc323832013-11-07 14:18:26 +01003477 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003478 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003479 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003480 * of the block or 0 if the end of the skb data or the upper
3481 * offset has been reached.
3482 *
3483 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003484 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003485 * of bytes already consumed and the next call to
3486 * skb_seq_read() will return the remaining part of the block.
3487 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003488 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003489 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003490 * reads of potentially non linear data.
3491 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003492 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003493 * at the moment, state->root_skb could be replaced with
3494 * a stack for this purpose.
3495 */
3496unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3497 struct skb_seq_state *st)
3498{
3499 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3500 skb_frag_t *frag;
3501
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003502 if (unlikely(abs_offset >= st->upper_offset)) {
3503 if (st->frag_data) {
3504 kunmap_atomic(st->frag_data);
3505 st->frag_data = NULL;
3506 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003507 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003508 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003509
3510next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003511 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003512
Thomas Chenault995b3372009-05-18 21:43:27 -07003513 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003514 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003515 return block_limit - abs_offset;
3516 }
3517
3518 if (st->frag_idx == 0 && !st->frag_data)
3519 st->stepped_offset += skb_headlen(st->cur_skb);
3520
3521 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3522 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003523 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003524
3525 if (abs_offset < block_limit) {
3526 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003527 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003528
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003529 *data = (u8 *) st->frag_data + skb_frag_off(frag) +
Thomas Graf677e90e2005-06-23 20:59:51 -07003530 (abs_offset - st->stepped_offset);
3531
3532 return block_limit - abs_offset;
3533 }
3534
3535 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003536 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003537 st->frag_data = NULL;
3538 }
3539
3540 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003541 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003542 }
3543
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003544 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003545 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003546 st->frag_data = NULL;
3547 }
3548
David S. Miller21dc3302010-08-23 00:13:46 -07003549 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003550 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003551 st->frag_idx = 0;
3552 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003553 } else if (st->cur_skb->next) {
3554 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003555 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003556 goto next_skb;
3557 }
3558
3559 return 0;
3560}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003561EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003562
3563/**
3564 * skb_abort_seq_read - Abort a sequential read of skb data
3565 * @st: state variable
3566 *
3567 * Must be called if skb_seq_read() was not called until it
3568 * returned 0.
3569 */
3570void skb_abort_seq_read(struct skb_seq_state *st)
3571{
3572 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003573 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003574}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003575EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003576
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003577#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3578
3579static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3580 struct ts_config *conf,
3581 struct ts_state *state)
3582{
3583 return skb_seq_read(offset, text, TS_SKB_CB(state));
3584}
3585
3586static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3587{
3588 skb_abort_seq_read(TS_SKB_CB(state));
3589}
3590
3591/**
3592 * skb_find_text - Find a text pattern in skb data
3593 * @skb: the buffer to look in
3594 * @from: search offset
3595 * @to: search limit
3596 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003597 *
3598 * Finds a pattern in the skb data according to the specified
3599 * textsearch configuration. Use textsearch_next() to retrieve
3600 * subsequent occurrences of the pattern. Returns the offset
3601 * to the first occurrence or UINT_MAX if no match was found.
3602 */
3603unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003604 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003605{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003606 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003607 unsigned int ret;
3608
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003609 config->get_next_block = skb_ts_get_next_block;
3610 config->finish = skb_ts_finish;
3611
Bojan Prtvar059a2442015-02-22 11:46:35 +01003612 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003613
Bojan Prtvar059a2442015-02-22 11:46:35 +01003614 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003615 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003616}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003617EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003618
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003619int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3620 int offset, size_t size)
3621{
3622 int i = skb_shinfo(skb)->nr_frags;
3623
3624 if (skb_can_coalesce(skb, i, page, offset)) {
3625 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3626 } else if (i < MAX_SKB_FRAGS) {
3627 get_page(page);
3628 skb_fill_page_desc(skb, i, page, offset, size);
3629 } else {
3630 return -EMSGSIZE;
3631 }
3632
3633 return 0;
3634}
3635EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3636
Herbert Xucbb042f2006-03-20 22:43:56 -08003637/**
3638 * skb_pull_rcsum - pull skb and update receive checksum
3639 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003640 * @len: length of data pulled
3641 *
3642 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003643 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003644 * receive path processing instead of skb_pull unless you know
3645 * that the checksum difference is zero (e.g., a valid IP header)
3646 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003647 */
Johannes Bergaf728682017-06-16 14:29:22 +02003648void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003649{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003650 unsigned char *data = skb->data;
3651
Herbert Xucbb042f2006-03-20 22:43:56 -08003652 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003653 __skb_pull(skb, len);
3654 skb_postpull_rcsum(skb, data, len);
3655 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003656}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003657EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3658
Yonghong Song13acc942018-03-21 16:31:03 -07003659static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3660{
3661 skb_frag_t head_frag;
3662 struct page *page;
3663
3664 page = virt_to_head_page(frag_skb->head);
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003665 __skb_frag_set_page(&head_frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003666 skb_frag_off_set(&head_frag, frag_skb->data -
3667 (unsigned char *)page_address(page));
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003668 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
Yonghong Song13acc942018-03-21 16:31:03 -07003669 return head_frag;
3670}
3671
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003672struct sk_buff *skb_segment_list(struct sk_buff *skb,
3673 netdev_features_t features,
3674 unsigned int offset)
3675{
3676 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3677 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3678 unsigned int delta_truesize = 0;
3679 unsigned int delta_len = 0;
3680 struct sk_buff *tail = NULL;
Dongseok Yi24cd3312021-01-08 11:28:38 +09003681 struct sk_buff *nskb, *tmp;
3682 int err;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003683
3684 skb_push(skb, -skb_network_offset(skb) + offset);
3685
3686 skb_shinfo(skb)->frag_list = NULL;
3687
3688 do {
3689 nskb = list_skb;
3690 list_skb = list_skb->next;
3691
Dongseok Yi24cd3312021-01-08 11:28:38 +09003692 err = 0;
3693 if (skb_shared(nskb)) {
3694 tmp = skb_clone(nskb, GFP_ATOMIC);
3695 if (tmp) {
3696 consume_skb(nskb);
3697 nskb = tmp;
3698 err = skb_unclone(nskb, GFP_ATOMIC);
3699 } else {
3700 err = -ENOMEM;
3701 }
3702 }
3703
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003704 if (!tail)
3705 skb->next = nskb;
3706 else
3707 tail->next = nskb;
3708
Dongseok Yi24cd3312021-01-08 11:28:38 +09003709 if (unlikely(err)) {
3710 nskb->next = list_skb;
3711 goto err_linearize;
3712 }
3713
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003714 tail = nskb;
3715
3716 delta_len += nskb->len;
3717 delta_truesize += nskb->truesize;
3718
3719 skb_push(nskb, -skb_network_offset(nskb) + offset);
3720
Florian Westphalcf673ed2020-03-30 18:51:29 +02003721 skb_release_head_state(nskb);
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003722 __copy_skb_header(nskb, skb);
3723
3724 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3725 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3726 nskb->data - tnl_hlen,
3727 offset + tnl_hlen);
3728
3729 if (skb_needs_linearize(nskb, features) &&
3730 __skb_linearize(nskb))
3731 goto err_linearize;
3732
3733 } while (list_skb);
3734
3735 skb->truesize = skb->truesize - delta_truesize;
3736 skb->data_len = skb->data_len - delta_len;
3737 skb->len = skb->len - delta_len;
3738
3739 skb_gso_reset(skb);
3740
3741 skb->prev = tail;
3742
3743 if (skb_needs_linearize(skb, features) &&
3744 __skb_linearize(skb))
3745 goto err_linearize;
3746
3747 skb_get(skb);
3748
3749 return skb;
3750
3751err_linearize:
3752 kfree_skb_list(skb->next);
3753 skb->next = NULL;
3754 return ERR_PTR(-ENOMEM);
3755}
3756EXPORT_SYMBOL_GPL(skb_segment_list);
3757
3758int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3759{
3760 if (unlikely(p->len + skb->len >= 65536))
3761 return -E2BIG;
3762
3763 if (NAPI_GRO_CB(p)->last == p)
3764 skb_shinfo(p)->frag_list = skb;
3765 else
3766 NAPI_GRO_CB(p)->last->next = skb;
3767
3768 skb_pull(skb, skb_gro_offset(skb));
3769
3770 NAPI_GRO_CB(p)->last = skb;
3771 NAPI_GRO_CB(p)->count++;
3772 p->data_len += skb->len;
3773 p->truesize += skb->truesize;
3774 p->len += skb->len;
3775
3776 NAPI_GRO_CB(skb)->same_flow = 1;
3777
3778 return 0;
3779}
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003780
Herbert Xuf4c50d92006-06-22 03:02:40 -07003781/**
3782 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003783 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003784 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003785 *
3786 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003787 * a pointer to the first in a list of new skbs for the segments.
3788 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003789 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003790struct sk_buff *skb_segment(struct sk_buff *head_skb,
3791 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003792{
3793 struct sk_buff *segs = NULL;
3794 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003795 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003796 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3797 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3798 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003799 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003800 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003801 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003802 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003803 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003804 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003805 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003806 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003807 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003808 int err = -ENOMEM;
3809 int i = 0;
3810 int pos;
3811
Shmulik Ladkani3dcbdb12019-09-06 12:23:50 +03003812 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3813 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3814 /* gso_size is untrusted, and we have a frag_list with a linear
3815 * non head_frag head.
3816 *
3817 * (we assume checking the first list_skb member suffices;
3818 * i.e if either of the list_skb members have non head_frag
3819 * head, then the first one has too).
3820 *
3821 * If head_skb's headlen does not fit requested gso_size, it
3822 * means that the frag_list members do NOT terminate on exact
3823 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3824 * sharing. Therefore we must fallback to copying the frag_list
3825 * skbs; we do so by disabling SG.
3826 */
3827 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3828 features &= ~NETIF_F_SG;
3829 }
3830
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003831 __skb_push(head_skb, doffset);
Miaohe Lin2f631132020-08-01 17:36:05 +08003832 proto = skb_network_protocol(head_skb, NULL);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003833 if (unlikely(!proto))
3834 return ERR_PTR(-EINVAL);
3835
Alexander Duyck36c98382016-05-02 09:38:18 -07003836 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003837 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003838
Steffen Klassert07b26c92016-09-19 12:58:47 +02003839 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3840 if (!(features & NETIF_F_GSO_PARTIAL)) {
3841 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003842 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003843
3844 if (!list_skb ||
3845 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3846 goto normal;
3847
Ilan Tayari43170c42017-04-19 21:26:07 +03003848 /* If we get here then all the required
3849 * GSO features except frag_list are supported.
3850 * Try to split the SKB to multiple GSO SKBs
3851 * with no frag_list.
3852 * Currently we can do that only when the buffers don't
3853 * have a linear part and all the buffers except
3854 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003855 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003856 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003857 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003858 if (frag_len != iter->len && iter->next)
3859 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003860 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003861 goto normal;
3862
3863 len -= iter->len;
3864 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003865
3866 if (len != frag_len)
3867 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003868 }
3869
3870 /* GSO partial only requires that we trim off any excess that
3871 * doesn't fit into an MSS sized block, so take care of that
3872 * now.
3873 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003874 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003875 if (partial_segs > 1)
3876 mss *= partial_segs;
3877 else
3878 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003879 }
3880
Steffen Klassert07b26c92016-09-19 12:58:47 +02003881normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003882 headroom = skb_headroom(head_skb);
3883 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003884
3885 do {
3886 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003887 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003888 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003889 int size;
3890
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003891 if (unlikely(mss == GSO_BY_FRAGS)) {
3892 len = list_skb->len;
3893 } else {
3894 len = head_skb->len - offset;
3895 if (len > mss)
3896 len = mss;
3897 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003898
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003899 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003900 if (hsize < 0)
3901 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003902 if (hsize > len || !sg)
3903 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003904
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003905 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3906 (skb_headlen(list_skb) == len || sg)) {
3907 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003908
Herbert Xu9d8506c2013-11-21 11:10:04 -08003909 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003910 nfrags = skb_shinfo(list_skb)->nr_frags;
3911 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003912 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003913 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003914
3915 while (pos < offset + len) {
3916 BUG_ON(i >= nfrags);
3917
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003918 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003919 if (pos + size > offset + len)
3920 break;
3921
3922 i++;
3923 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003924 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003925 }
3926
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003927 nskb = skb_clone(list_skb, GFP_ATOMIC);
3928 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003929
3930 if (unlikely(!nskb))
3931 goto err;
3932
Herbert Xu9d8506c2013-11-21 11:10:04 -08003933 if (unlikely(pskb_trim(nskb, len))) {
3934 kfree_skb(nskb);
3935 goto err;
3936 }
3937
Alexander Duyckec47ea82012-05-04 14:26:56 +00003938 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003939 if (skb_cow_head(nskb, doffset + headroom)) {
3940 kfree_skb(nskb);
3941 goto err;
3942 }
3943
Alexander Duyckec47ea82012-05-04 14:26:56 +00003944 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003945 skb_release_head_state(nskb);
3946 __skb_push(nskb, doffset);
3947 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003948 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003949 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003950 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003951
3952 if (unlikely(!nskb))
3953 goto err;
3954
3955 skb_reserve(nskb, headroom);
3956 __skb_put(nskb, doffset);
3957 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003958
3959 if (segs)
3960 tail->next = nskb;
3961 else
3962 segs = nskb;
3963 tail = nskb;
3964
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003965 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003966
Eric Dumazet030737b2013-10-19 11:42:54 -07003967 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003968 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003969
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003970 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003971 nskb->data - tnl_hlen,
3972 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003973
Herbert Xu9d8506c2013-11-21 11:10:04 -08003974 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003975 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003976
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003977 if (!sg) {
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303978 if (!csum) {
3979 if (!nskb->remcsum_offload)
3980 nskb->ip_summed = CHECKSUM_NONE;
3981 SKB_GSO_CB(nskb)->csum =
3982 skb_copy_and_csum_bits(head_skb, offset,
3983 skb_put(nskb,
3984 len),
Al Viro8d5930d2020-07-10 20:07:10 -04003985 len);
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303986 SKB_GSO_CB(nskb)->csum_start =
3987 skb_headroom(nskb) + doffset;
3988 } else {
3989 skb_copy_bits(head_skb, offset,
3990 skb_put(nskb, len),
3991 len);
3992 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003993 continue;
3994 }
3995
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003996 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003997
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003998 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003999 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004000
Willem de Bruijnfff88032017-06-08 11:35:03 -04004001 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
4002 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00004003
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004004 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4005 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4006 goto err;
4007
Herbert Xu9d8506c2013-11-21 11:10:04 -08004008 while (pos < offset + len) {
4009 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08004010 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004011 nfrags = skb_shinfo(list_skb)->nr_frags;
4012 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004013 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07004014 if (!skb_headlen(list_skb)) {
4015 BUG_ON(!nfrags);
4016 } else {
4017 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004018
Yonghong Song13acc942018-03-21 16:31:03 -07004019 /* to make room for head_frag. */
4020 i--;
4021 frag--;
4022 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004023 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4024 skb_zerocopy_clone(nskb, frag_skb,
4025 GFP_ATOMIC))
4026 goto err;
4027
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004028 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004029 }
4030
4031 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4032 MAX_SKB_FRAGS)) {
4033 net_warn_ratelimited(
4034 "skb_segment: too many frags: %u %u\n",
4035 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07004036 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004037 goto err;
4038 }
4039
Yonghong Song13acc942018-03-21 16:31:03 -07004040 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004041 __skb_frag_ref(nskb_frag);
4042 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004043
4044 if (pos < offset) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004045 skb_frag_off_add(nskb_frag, offset - pos);
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004046 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004047 }
4048
Herbert Xu89319d382008-12-15 23:26:06 -08004049 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004050
4051 if (pos + size <= offset + len) {
4052 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004053 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004054 pos += size;
4055 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004056 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08004057 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004058 }
4059
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004060 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004061 }
4062
Herbert Xu89319d382008-12-15 23:26:06 -08004063skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07004064 nskb->data_len = len - hsize;
4065 nskb->len += nskb->data_len;
4066 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004067
Simon Horman1cdbcb72013-05-19 15:46:49 +00004068perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004069 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07004070 if (skb_has_shared_frag(nskb) &&
4071 __skb_linearize(nskb))
4072 goto err;
4073
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004074 if (!nskb->remcsum_offload)
4075 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08004076 SKB_GSO_CB(nskb)->csum =
4077 skb_checksum(nskb, doffset,
4078 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004079 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08004080 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004081 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004082 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004083
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07004084 /* Some callers want to get the end of the list.
4085 * Put it in segs->prev to avoid walking the list.
4086 * (see validate_xmit_skb_list() for example)
4087 */
4088 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07004089
Alexander Duyck802ab552016-04-10 21:45:03 -04004090 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02004091 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04004092 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004093 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04004094
4095 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004096 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04004097 type &= ~SKB_GSO_DODGY;
4098
4099 /* Update GSO info and prepare to start updating headers on
4100 * our way back down the stack of protocols.
4101 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004102 for (iter = segs; iter; iter = iter->next) {
4103 skb_shinfo(iter)->gso_size = gso_size;
4104 skb_shinfo(iter)->gso_segs = partial_segs;
4105 skb_shinfo(iter)->gso_type = type;
4106 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4107 }
4108
4109 if (tail->len - doffset <= gso_size)
4110 skb_shinfo(tail)->gso_size = 0;
4111 else if (tail != segs)
4112 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04004113 }
4114
Toshiaki Makita432c8562014-10-27 10:30:51 -07004115 /* Following permits correct backpressure, for protocols
4116 * using skb_set_owner_w().
4117 * Idea is to tranfert ownership from head_skb to last segment.
4118 */
4119 if (head_skb->destructor == sock_wfree) {
4120 swap(tail->truesize, head_skb->truesize);
4121 swap(tail->destructor, head_skb->destructor);
4122 swap(tail->sk, head_skb->sk);
4123 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004124 return segs;
4125
4126err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08004127 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004128 return ERR_PTR(err);
4129}
Herbert Xuf4c50d92006-06-22 03:02:40 -07004130EXPORT_SYMBOL_GPL(skb_segment);
4131
David Millerd4546c22018-06-24 14:13:49 +09004132int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08004133{
Eric Dumazet8a291112013-10-08 09:02:23 -07004134 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00004135 unsigned int offset = skb_gro_offset(skb);
4136 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004137 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004138 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09004139 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08004140
Steffen Klassert0ab03f32019-04-02 08:16:03 +02004141 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08004142 return -E2BIG;
4143
Eric Dumazet29e98242014-05-16 11:34:37 -07004144 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07004145 pinfo = skb_shinfo(lp);
4146
4147 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00004148 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004149 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004150 int i = skbinfo->nr_frags;
4151 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00004152
Herbert Xu66e92fc2009-05-26 18:50:32 +00004153 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004154 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00004155
Eric Dumazet8a291112013-10-08 09:02:23 -07004156 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004157 pinfo->nr_frags = nr_frags;
4158 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08004159
Herbert Xu9aaa1562009-05-26 18:50:33 +00004160 frag = pinfo->frags + nr_frags;
4161 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004162 do {
4163 *--frag = *--frag2;
4164 } while (--i);
4165
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004166 skb_frag_off_add(frag, offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004167 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00004168
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004169 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00004170 delta_truesize = skb->truesize -
4171 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004172
Herbert Xuf5572062009-01-14 20:40:03 -08004173 skb->truesize -= skb->data_len;
4174 skb->len -= skb->data_len;
4175 skb->data_len = 0;
4176
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004177 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004178 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004179 } else if (skb->head_frag) {
4180 int nr_frags = pinfo->nr_frags;
4181 skb_frag_t *frag = pinfo->frags + nr_frags;
4182 struct page *page = virt_to_head_page(skb->head);
4183 unsigned int first_size = headlen - offset;
4184 unsigned int first_offset;
4185
4186 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004187 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004188
4189 first_offset = skb->data -
4190 (unsigned char *)page_address(page) +
4191 offset;
4192
4193 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4194
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07004195 __skb_frag_set_page(frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004196 skb_frag_off_set(frag, first_offset);
Eric Dumazetd7e88832012-04-30 08:10:34 +00004197 skb_frag_size_set(frag, first_size);
4198
4199 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4200 /* We dont need to clear skbinfo->nr_frags here */
4201
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004202 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00004203 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4204 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004205 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004206
4207merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004208 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004209 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004210 unsigned int eat = offset - headlen;
4211
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004212 skb_frag_off_add(&skbinfo->frags[0], eat);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004213 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004214 skb->data_len -= eat;
4215 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004216 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004217 }
4218
Herbert Xu67147ba2009-05-26 18:50:22 +00004219 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004220
Eric Dumazet29e98242014-05-16 11:34:37 -07004221 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004222 skb_shinfo(p)->frag_list = skb;
4223 else
4224 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004225 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004226 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004227 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004228
Herbert Xu5d38a072009-01-04 16:13:40 -08004229done:
4230 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004231 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004232 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004233 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004234 if (lp != p) {
4235 lp->data_len += len;
4236 lp->truesize += delta_truesize;
4237 lp->len += len;
4238 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004239 NAPI_GRO_CB(skb)->same_flow = 1;
4240 return 0;
4241}
Herbert Xu71d93b32008-12-15 23:42:33 -08004242
Florian Westphaldf5042f2018-12-18 17:15:16 +01004243#ifdef CONFIG_SKB_EXTENSIONS
4244#define SKB_EXT_ALIGN_VALUE 8
4245#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4246
4247static const u8 skb_ext_type_len[] = {
4248#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4249 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4250#endif
Florian Westphal41650792018-12-18 17:15:27 +01004251#ifdef CONFIG_XFRM
4252 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4253#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004254#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4255 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4256#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004257#if IS_ENABLED(CONFIG_MPTCP)
4258 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4259#endif
Aleksandr Nogikh4a31baf2020-10-29 17:36:19 +00004260#if IS_ENABLED(CONFIG_KCOV)
4261 [SKB_EXT_KCOV_HANDLE] = SKB_EXT_CHUNKSIZEOF(u64),
4262#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004263};
4264
4265static __always_inline unsigned int skb_ext_total_length(void)
4266{
4267 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4268#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4269 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4270#endif
Florian Westphal41650792018-12-18 17:15:27 +01004271#ifdef CONFIG_XFRM
4272 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4273#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004274#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4275 skb_ext_type_len[TC_SKB_EXT] +
4276#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004277#if IS_ENABLED(CONFIG_MPTCP)
4278 skb_ext_type_len[SKB_EXT_MPTCP] +
4279#endif
Aleksandr Nogikh4a31baf2020-10-29 17:36:19 +00004280#if IS_ENABLED(CONFIG_KCOV)
4281 skb_ext_type_len[SKB_EXT_KCOV_HANDLE] +
4282#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004283 0;
4284}
4285
4286static void skb_extensions_init(void)
4287{
4288 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4289 BUILD_BUG_ON(skb_ext_total_length() > 255);
4290
4291 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4292 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4293 0,
4294 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4295 NULL);
4296}
4297#else
4298static void skb_extensions_init(void) {}
4299#endif
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301void __init skb_init(void)
4302{
Kees Cook79a8a642018-02-07 17:44:38 -08004303 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 sizeof(struct sk_buff),
4305 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004306 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004307 offsetof(struct sk_buff, cb),
4308 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004309 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004310 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004311 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004312 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004313 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004314 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004315 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
David S. Miller51c739d2007-10-30 21:29:29 -07004318static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004319__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4320 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004321{
David S. Miller1a028e52007-04-27 15:21:23 -07004322 int start = skb_headlen(skb);
4323 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004324 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004325 int elt = 0;
4326
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004327 if (unlikely(recursion_level >= 24))
4328 return -EMSGSIZE;
4329
David Howells716ea3a2007-04-02 20:19:53 -07004330 if (copy > 0) {
4331 if (copy > len)
4332 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004333 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004334 elt++;
4335 if ((len -= copy) == 0)
4336 return elt;
4337 offset += copy;
4338 }
4339
4340 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004341 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004342
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004343 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004344
Eric Dumazet9e903e02011-10-18 21:00:24 +00004345 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004346 if ((copy = end - offset) > 0) {
4347 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004348 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4349 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004350
4351 if (copy > len)
4352 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004353 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004354 skb_frag_off(frag) + offset - start);
David Howells716ea3a2007-04-02 20:19:53 -07004355 elt++;
4356 if (!(len -= copy))
4357 return elt;
4358 offset += copy;
4359 }
David S. Miller1a028e52007-04-27 15:21:23 -07004360 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004361 }
4362
David S. Millerfbb398a2009-06-09 00:18:59 -07004363 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004364 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004365
David S. Millerfbb398a2009-06-09 00:18:59 -07004366 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004367
David S. Millerfbb398a2009-06-09 00:18:59 -07004368 end = start + frag_iter->len;
4369 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004370 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4371 return -EMSGSIZE;
4372
David S. Millerfbb398a2009-06-09 00:18:59 -07004373 if (copy > len)
4374 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004375 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4376 copy, recursion_level + 1);
4377 if (unlikely(ret < 0))
4378 return ret;
4379 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004380 if ((len -= copy) == 0)
4381 return elt;
4382 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004383 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004384 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004385 }
4386 BUG_ON(len);
4387 return elt;
4388}
4389
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004390/**
4391 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4392 * @skb: Socket buffer containing the buffers to be mapped
4393 * @sg: The scatter-gather list to map into
4394 * @offset: The offset into the buffer's contents to start mapping
4395 * @len: Length of buffer space to be mapped
4396 *
4397 * Fill the specified scatter-gather list with mappings/pointers into a
4398 * region of the buffer space attached to a socket buffer. Returns either
4399 * the number of scatterlist items used, or -EMSGSIZE if the contents
4400 * could not fit.
4401 */
4402int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4403{
4404 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4405
4406 if (nsg <= 0)
4407 return nsg;
4408
4409 sg_mark_end(&sg[nsg - 1]);
4410
4411 return nsg;
4412}
4413EXPORT_SYMBOL_GPL(skb_to_sgvec);
4414
Fan Du25a91d82014-01-18 09:54:23 +08004415/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4416 * sglist without mark the sg which contain last skb data as the end.
4417 * So the caller can mannipulate sg list as will when padding new data after
4418 * the first call without calling sg_unmark_end to expend sg list.
4419 *
4420 * Scenario to use skb_to_sgvec_nomark:
4421 * 1. sg_init_table
4422 * 2. skb_to_sgvec_nomark(payload1)
4423 * 3. skb_to_sgvec_nomark(payload2)
4424 *
4425 * This is equivalent to:
4426 * 1. sg_init_table
4427 * 2. skb_to_sgvec(payload1)
4428 * 3. sg_unmark_end
4429 * 4. skb_to_sgvec(payload2)
4430 *
4431 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4432 * is more preferable.
4433 */
4434int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4435 int offset, int len)
4436{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004437 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004438}
4439EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4440
David S. Miller51c739d2007-10-30 21:29:29 -07004441
David S. Miller51c739d2007-10-30 21:29:29 -07004442
David Howells716ea3a2007-04-02 20:19:53 -07004443/**
4444 * skb_cow_data - Check that a socket buffer's data buffers are writable
4445 * @skb: The socket buffer to check.
4446 * @tailbits: Amount of trailing space to be added
4447 * @trailer: Returned pointer to the skb where the @tailbits space begins
4448 *
4449 * Make sure that the data buffers attached to a socket buffer are
4450 * writable. If they are not, private copies are made of the data buffers
4451 * and the socket buffer is set to use these instead.
4452 *
4453 * If @tailbits is given, make sure that there is space to write @tailbits
4454 * bytes of data beyond current end of socket buffer. @trailer will be
4455 * set to point to the skb in which this space begins.
4456 *
4457 * The number of scatterlist elements required to completely map the
4458 * COW'd and extended socket buffer will be returned.
4459 */
4460int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4461{
4462 int copyflag;
4463 int elt;
4464 struct sk_buff *skb1, **skb_p;
4465
4466 /* If skb is cloned or its head is paged, reallocate
4467 * head pulling out all the pages (pages are considered not writable
4468 * at the moment even if they are anonymous).
4469 */
4470 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
Miaohe Linc15fc192020-08-01 17:30:23 +08004471 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
David Howells716ea3a2007-04-02 20:19:53 -07004472 return -ENOMEM;
4473
4474 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004475 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004476 /* A little of trouble, not enough of space for trailer.
4477 * This should not happen, when stack is tuned to generate
4478 * good frames. OK, on miss we reallocate and reserve even more
4479 * space, 128 bytes is fair. */
4480
4481 if (skb_tailroom(skb) < tailbits &&
4482 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4483 return -ENOMEM;
4484
4485 /* Voila! */
4486 *trailer = skb;
4487 return 1;
4488 }
4489
4490 /* Misery. We are in troubles, going to mincer fragments... */
4491
4492 elt = 1;
4493 skb_p = &skb_shinfo(skb)->frag_list;
4494 copyflag = 0;
4495
4496 while ((skb1 = *skb_p) != NULL) {
4497 int ntail = 0;
4498
4499 /* The fragment is partially pulled by someone,
4500 * this can happen on input. Copy it and everything
4501 * after it. */
4502
4503 if (skb_shared(skb1))
4504 copyflag = 1;
4505
4506 /* If the skb is the last, worry about trailer. */
4507
4508 if (skb1->next == NULL && tailbits) {
4509 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004510 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004511 skb_tailroom(skb1) < tailbits)
4512 ntail = tailbits + 128;
4513 }
4514
4515 if (copyflag ||
4516 skb_cloned(skb1) ||
4517 ntail ||
4518 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004519 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004520 struct sk_buff *skb2;
4521
4522 /* Fuck, we are miserable poor guys... */
4523 if (ntail == 0)
4524 skb2 = skb_copy(skb1, GFP_ATOMIC);
4525 else
4526 skb2 = skb_copy_expand(skb1,
4527 skb_headroom(skb1),
4528 ntail,
4529 GFP_ATOMIC);
4530 if (unlikely(skb2 == NULL))
4531 return -ENOMEM;
4532
4533 if (skb1->sk)
4534 skb_set_owner_w(skb2, skb1->sk);
4535
4536 /* Looking around. Are we still alive?
4537 * OK, link new skb, drop old one */
4538
4539 skb2->next = skb1->next;
4540 *skb_p = skb2;
4541 kfree_skb(skb1);
4542 skb1 = skb2;
4543 }
4544 elt++;
4545 *trailer = skb1;
4546 skb_p = &skb1->next;
4547 }
4548
4549 return elt;
4550}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004551EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004552
Eric Dumazetb1faf562010-05-31 23:44:05 -07004553static void sock_rmem_free(struct sk_buff *skb)
4554{
4555 struct sock *sk = skb->sk;
4556
4557 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4558}
4559
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004560static void skb_set_err_queue(struct sk_buff *skb)
4561{
4562 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4563 * So, it is safe to (mis)use it to mark skbs on the error queue.
4564 */
4565 skb->pkt_type = PACKET_OUTGOING;
4566 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4567}
4568
Eric Dumazetb1faf562010-05-31 23:44:05 -07004569/*
4570 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4571 */
4572int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4573{
4574 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazetebb3b782019-10-10 20:17:44 -07004575 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
Eric Dumazetb1faf562010-05-31 23:44:05 -07004576 return -ENOMEM;
4577
4578 skb_orphan(skb);
4579 skb->sk = sk;
4580 skb->destructor = sock_rmem_free;
4581 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004582 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004583
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004584 /* before exiting rcu section, make sure dst is refcounted */
4585 skb_dst_force(skb);
4586
Eric Dumazetb1faf562010-05-31 23:44:05 -07004587 skb_queue_tail(&sk->sk_error_queue, skb);
4588 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004589 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004590 return 0;
4591}
4592EXPORT_SYMBOL(sock_queue_err_skb);
4593
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004594static bool is_icmp_err_skb(const struct sk_buff *skb)
4595{
4596 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4597 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4598}
4599
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004600struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4601{
4602 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004603 struct sk_buff *skb, *skb_next = NULL;
4604 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004605 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004606
Eric Dumazet997d5c32015-02-18 05:47:55 -08004607 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004608 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004609 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004610 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004611 if (icmp_next)
Willem de Bruijn985f7332020-11-26 10:12:20 -05004612 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004613 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004614 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004615
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004616 if (is_icmp_err_skb(skb) && !icmp_next)
4617 sk->sk_err = 0;
4618
4619 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004620 sk->sk_error_report(sk);
4621
4622 return skb;
4623}
4624EXPORT_SYMBOL(sock_dequeue_err_skb);
4625
Alexander Duyckcab41c42014-09-10 18:05:26 -04004626/**
4627 * skb_clone_sk - create clone of skb, and take reference to socket
4628 * @skb: the skb to clone
4629 *
4630 * This function creates a clone of a buffer that holds a reference on
4631 * sk_refcnt. Buffers created via this function are meant to be
4632 * returned using sock_queue_err_skb, or free via kfree_skb.
4633 *
4634 * When passing buffers allocated with this function to sock_queue_err_skb
4635 * it is necessary to wrap the call with sock_hold/sock_put in order to
4636 * prevent the socket from being released prior to being enqueued on
4637 * the sk_error_queue.
4638 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004639struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4640{
4641 struct sock *sk = skb->sk;
4642 struct sk_buff *clone;
4643
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004644 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004645 return NULL;
4646
4647 clone = skb_clone(skb, GFP_ATOMIC);
4648 if (!clone) {
4649 sock_put(sk);
4650 return NULL;
4651 }
4652
4653 clone->sk = sk;
4654 clone->destructor = sock_efree;
4655
4656 return clone;
4657}
4658EXPORT_SYMBOL(skb_clone_sk);
4659
Alexander Duyck37846ef2014-09-04 13:31:10 -04004660static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4661 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004662 int tstype,
4663 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004664{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004665 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004666 int err;
4667
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004668 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4669
Patrick Ohlyac45f602009-02-12 05:03:37 +00004670 serr = SKB_EXT_ERR(skb);
4671 memset(serr, 0, sizeof(*serr));
4672 serr->ee.ee_errno = ENOMSG;
4673 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004674 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004675 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004676 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004677 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004678 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004679 if (sk->sk_protocol == IPPROTO_TCP &&
4680 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004681 serr->ee.ee_data -= sk->sk_tskey;
4682 }
Eric Dumazet29030372010-05-29 00:20:48 -07004683
Patrick Ohlyac45f602009-02-12 05:03:37 +00004684 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004685
Patrick Ohlyac45f602009-02-12 05:03:37 +00004686 if (err)
4687 kfree_skb(skb);
4688}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004689
Willem de Bruijnb245be12015-01-30 13:29:32 -05004690static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4691{
4692 bool ret;
4693
4694 if (likely(sysctl_tstamp_allow_data || tsonly))
4695 return true;
4696
4697 read_lock_bh(&sk->sk_callback_lock);
4698 ret = sk->sk_socket && sk->sk_socket->file &&
4699 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4700 read_unlock_bh(&sk->sk_callback_lock);
4701 return ret;
4702}
4703
Alexander Duyck37846ef2014-09-04 13:31:10 -04004704void skb_complete_tx_timestamp(struct sk_buff *skb,
4705 struct skb_shared_hwtstamps *hwtstamps)
4706{
4707 struct sock *sk = skb->sk;
4708
Willem de Bruijnb245be12015-01-30 13:29:32 -05004709 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004710 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004711
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004712 /* Take a reference to prevent skb_orphan() from freeing the socket,
4713 * but only if the socket refcount is not zero.
4714 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004715 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004716 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004717 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004718 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004719 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004720 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004721
4722err:
4723 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004724}
4725EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4726
4727void __skb_tstamp_tx(struct sk_buff *orig_skb,
4728 struct skb_shared_hwtstamps *hwtstamps,
4729 struct sock *sk, int tstype)
4730{
4731 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004732 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004733
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004734 if (!sk)
4735 return;
4736
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004737 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4738 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4739 return;
4740
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004741 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4742 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004743 return;
4744
Francis Yan1c885802016-11-27 23:07:18 -08004745 if (tsonly) {
4746#ifdef CONFIG_INET
4747 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4748 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004749 sk->sk_type == SOCK_STREAM) {
Yousuk Seung48040792020-07-30 15:44:40 -07004750 skb = tcp_get_timestamping_opt_stats(sk, orig_skb);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004751 opt_stats = true;
4752 } else
Francis Yan1c885802016-11-27 23:07:18 -08004753#endif
4754 skb = alloc_skb(0, GFP_ATOMIC);
4755 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004756 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004757 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004758 if (!skb)
4759 return;
4760
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004761 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004762 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4763 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004764 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4765 }
4766
4767 if (hwtstamps)
4768 *skb_hwtstamps(skb) = *hwtstamps;
4769 else
4770 skb->tstamp = ktime_get_real();
4771
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004772 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004773}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004774EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4775
4776void skb_tstamp_tx(struct sk_buff *orig_skb,
4777 struct skb_shared_hwtstamps *hwtstamps)
4778{
4779 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4780 SCM_TSTAMP_SND);
4781}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004782EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4783
Johannes Berg6e3e9392011-11-09 10:15:42 +01004784void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4785{
4786 struct sock *sk = skb->sk;
4787 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004788 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004789
4790 skb->wifi_acked_valid = 1;
4791 skb->wifi_acked = acked;
4792
4793 serr = SKB_EXT_ERR(skb);
4794 memset(serr, 0, sizeof(*serr));
4795 serr->ee.ee_errno = ENOMSG;
4796 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4797
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004798 /* Take a reference to prevent skb_orphan() from freeing the socket,
4799 * but only if the socket refcount is not zero.
4800 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004801 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004802 err = sock_queue_err_skb(sk, skb);
4803 sock_put(sk);
4804 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004805 if (err)
4806 kfree_skb(skb);
4807}
4808EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4809
Rusty Russellf35d9d82008-02-04 23:49:54 -05004810/**
4811 * skb_partial_csum_set - set up and verify partial csum values for packet
4812 * @skb: the skb to set
4813 * @start: the number of bytes after skb->data to start checksumming.
4814 * @off: the offset from start to place the checksum.
4815 *
4816 * For untrusted partially-checksummed packets, we need to make sure the values
4817 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4818 *
4819 * This function checks and sets those values and skb->ip_summed: if this
4820 * returns false you should drop the packet.
4821 */
4822bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4823{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004824 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4825 u32 csum_start = skb_headroom(skb) + (u32)start;
4826
4827 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4828 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4829 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004830 return false;
4831 }
4832 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004833 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004834 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004835 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004836 return true;
4837}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004838EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004839
Paul Durranted1f50c2014-01-09 10:02:46 +00004840static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4841 unsigned int max)
4842{
4843 if (skb_headlen(skb) >= len)
4844 return 0;
4845
4846 /* If we need to pullup then pullup to the max, so we
4847 * won't need to do it again.
4848 */
4849 if (max > skb->len)
4850 max = skb->len;
4851
4852 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4853 return -ENOMEM;
4854
4855 if (skb_headlen(skb) < len)
4856 return -EPROTO;
4857
4858 return 0;
4859}
4860
Jan Beulichf9708b42014-03-11 13:56:05 +00004861#define MAX_TCP_HDR_LEN (15 * 4)
4862
4863static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4864 typeof(IPPROTO_IP) proto,
4865 unsigned int off)
4866{
Kees Cook161d1792020-02-19 22:23:04 -08004867 int err;
Jan Beulichf9708b42014-03-11 13:56:05 +00004868
Kees Cook161d1792020-02-19 22:23:04 -08004869 switch (proto) {
Jan Beulichf9708b42014-03-11 13:56:05 +00004870 case IPPROTO_TCP:
4871 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4872 off + MAX_TCP_HDR_LEN);
4873 if (!err && !skb_partial_csum_set(skb, off,
4874 offsetof(struct tcphdr,
4875 check)))
4876 err = -EPROTO;
4877 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4878
4879 case IPPROTO_UDP:
4880 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4881 off + sizeof(struct udphdr));
4882 if (!err && !skb_partial_csum_set(skb, off,
4883 offsetof(struct udphdr,
4884 check)))
4885 err = -EPROTO;
4886 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4887 }
4888
4889 return ERR_PTR(-EPROTO);
4890}
4891
Paul Durranted1f50c2014-01-09 10:02:46 +00004892/* This value should be large enough to cover a tagged ethernet header plus
4893 * maximally sized IP and TCP or UDP headers.
4894 */
4895#define MAX_IP_HDR_LEN 128
4896
Jan Beulichf9708b42014-03-11 13:56:05 +00004897static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004898{
4899 unsigned int off;
4900 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004901 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004902 int err;
4903
4904 fragment = false;
4905
4906 err = skb_maybe_pull_tail(skb,
4907 sizeof(struct iphdr),
4908 MAX_IP_HDR_LEN);
4909 if (err < 0)
4910 goto out;
4911
Miaohe Lin11f920d2020-08-06 19:57:18 +08004912 if (ip_is_fragment(ip_hdr(skb)))
Paul Durranted1f50c2014-01-09 10:02:46 +00004913 fragment = true;
4914
4915 off = ip_hdrlen(skb);
4916
4917 err = -EPROTO;
4918
4919 if (fragment)
4920 goto out;
4921
Jan Beulichf9708b42014-03-11 13:56:05 +00004922 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4923 if (IS_ERR(csum))
4924 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004925
Jan Beulichf9708b42014-03-11 13:56:05 +00004926 if (recalculate)
4927 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4928 ip_hdr(skb)->daddr,
4929 skb->len - off,
4930 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004931 err = 0;
4932
4933out:
4934 return err;
4935}
4936
4937/* This value should be large enough to cover a tagged ethernet header plus
4938 * an IPv6 header, all options, and a maximal TCP or UDP header.
4939 */
4940#define MAX_IPV6_HDR_LEN 256
4941
4942#define OPT_HDR(type, skb, off) \
4943 (type *)(skb_network_header(skb) + (off))
4944
4945static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4946{
4947 int err;
4948 u8 nexthdr;
4949 unsigned int off;
4950 unsigned int len;
4951 bool fragment;
4952 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004953 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004954
4955 fragment = false;
4956 done = false;
4957
4958 off = sizeof(struct ipv6hdr);
4959
4960 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4961 if (err < 0)
4962 goto out;
4963
4964 nexthdr = ipv6_hdr(skb)->nexthdr;
4965
4966 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4967 while (off <= len && !done) {
4968 switch (nexthdr) {
4969 case IPPROTO_DSTOPTS:
4970 case IPPROTO_HOPOPTS:
4971 case IPPROTO_ROUTING: {
4972 struct ipv6_opt_hdr *hp;
4973
4974 err = skb_maybe_pull_tail(skb,
4975 off +
4976 sizeof(struct ipv6_opt_hdr),
4977 MAX_IPV6_HDR_LEN);
4978 if (err < 0)
4979 goto out;
4980
4981 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4982 nexthdr = hp->nexthdr;
4983 off += ipv6_optlen(hp);
4984 break;
4985 }
4986 case IPPROTO_AH: {
4987 struct ip_auth_hdr *hp;
4988
4989 err = skb_maybe_pull_tail(skb,
4990 off +
4991 sizeof(struct ip_auth_hdr),
4992 MAX_IPV6_HDR_LEN);
4993 if (err < 0)
4994 goto out;
4995
4996 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4997 nexthdr = hp->nexthdr;
4998 off += ipv6_authlen(hp);
4999 break;
5000 }
5001 case IPPROTO_FRAGMENT: {
5002 struct frag_hdr *hp;
5003
5004 err = skb_maybe_pull_tail(skb,
5005 off +
5006 sizeof(struct frag_hdr),
5007 MAX_IPV6_HDR_LEN);
5008 if (err < 0)
5009 goto out;
5010
5011 hp = OPT_HDR(struct frag_hdr, skb, off);
5012
5013 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5014 fragment = true;
5015
5016 nexthdr = hp->nexthdr;
5017 off += sizeof(struct frag_hdr);
5018 break;
5019 }
5020 default:
5021 done = true;
5022 break;
5023 }
5024 }
5025
5026 err = -EPROTO;
5027
5028 if (!done || fragment)
5029 goto out;
5030
Jan Beulichf9708b42014-03-11 13:56:05 +00005031 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5032 if (IS_ERR(csum))
5033 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005034
Jan Beulichf9708b42014-03-11 13:56:05 +00005035 if (recalculate)
5036 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5037 &ipv6_hdr(skb)->daddr,
5038 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005039 err = 0;
5040
5041out:
5042 return err;
5043}
5044
5045/**
5046 * skb_checksum_setup - set up partial checksum offset
5047 * @skb: the skb to set up
5048 * @recalculate: if true the pseudo-header checksum will be recalculated
5049 */
5050int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5051{
5052 int err;
5053
5054 switch (skb->protocol) {
5055 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00005056 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00005057 break;
5058
5059 case htons(ETH_P_IPV6):
5060 err = skb_checksum_setup_ipv6(skb, recalculate);
5061 break;
5062
5063 default:
5064 err = -EPROTO;
5065 break;
5066 }
5067
5068 return err;
5069}
5070EXPORT_SYMBOL(skb_checksum_setup);
5071
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005072/**
5073 * skb_checksum_maybe_trim - maybe trims the given skb
5074 * @skb: the skb to check
5075 * @transport_len: the data length beyond the network header
5076 *
5077 * Checks whether the given skb has data beyond the given transport length.
5078 * If so, returns a cloned skb trimmed to this transport length.
5079 * Otherwise returns the provided skb. Returns NULL in error cases
5080 * (e.g. transport_len exceeds skb length or out-of-memory).
5081 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005082 * Caller needs to set the skb transport header and free any returned skb if it
5083 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005084 */
5085static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5086 unsigned int transport_len)
5087{
5088 struct sk_buff *skb_chk;
5089 unsigned int len = skb_transport_offset(skb) + transport_len;
5090 int ret;
5091
Linus Lüssinga5169932015-08-13 05:54:07 +02005092 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005093 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02005094 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005095 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005096
5097 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005098 if (!skb_chk)
5099 return NULL;
5100
5101 ret = pskb_trim_rcsum(skb_chk, len);
5102 if (ret) {
5103 kfree_skb(skb_chk);
5104 return NULL;
5105 }
5106
5107 return skb_chk;
5108}
5109
5110/**
5111 * skb_checksum_trimmed - validate checksum of an skb
5112 * @skb: the skb to check
5113 * @transport_len: the data length beyond the network header
5114 * @skb_chkf: checksum function to use
5115 *
5116 * Applies the given checksum function skb_chkf to the provided skb.
5117 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5118 *
5119 * If the skb has data beyond the given transport length, then a
5120 * trimmed & cloned skb is checked and returned.
5121 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005122 * Caller needs to set the skb transport header and free any returned skb if it
5123 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005124 */
5125struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5126 unsigned int transport_len,
5127 __sum16(*skb_chkf)(struct sk_buff *skb))
5128{
5129 struct sk_buff *skb_chk;
5130 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02005131 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005132
5133 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5134 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02005135 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005136
Linus Lüssinga5169932015-08-13 05:54:07 +02005137 if (!pskb_may_pull(skb_chk, offset))
5138 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005139
Linus Lüssing9b368812016-02-24 04:21:42 +01005140 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005141 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01005142 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005143
Linus Lüssinga5169932015-08-13 05:54:07 +02005144 if (ret)
5145 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005146
5147 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02005148
5149err:
5150 if (skb_chk && skb_chk != skb)
5151 kfree_skb(skb_chk);
5152
5153 return NULL;
5154
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005155}
5156EXPORT_SYMBOL(skb_checksum_trimmed);
5157
Ben Hutchings4497b072008-06-19 16:22:28 -07005158void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5159{
Joe Perchese87cc472012-05-13 21:56:26 +00005160 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5161 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07005162}
Ben Hutchings4497b072008-06-19 16:22:28 -07005163EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005164
5165void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5166{
Eric Dumazet3d861f62012-10-22 09:03:40 +00005167 if (head_stolen) {
5168 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005169 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005170 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005171 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005172 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005173}
5174EXPORT_SYMBOL(kfree_skb_partial);
5175
5176/**
5177 * skb_try_coalesce - try to merge skb to prior one
5178 * @to: prior buffer
5179 * @from: buffer to add
5180 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00005181 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005182 */
5183bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5184 bool *fragstolen, int *delta_truesize)
5185{
Eric Dumazetc818fa92017-10-04 10:48:35 -07005186 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005187 int i, delta, len = from->len;
5188
5189 *fragstolen = false;
5190
5191 if (skb_cloned(to))
5192 return false;
5193
5194 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005195 if (len)
5196 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005197 *delta_truesize = 0;
5198 return true;
5199 }
5200
Eric Dumazetc818fa92017-10-04 10:48:35 -07005201 to_shinfo = skb_shinfo(to);
5202 from_shinfo = skb_shinfo(from);
5203 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005204 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005205 if (skb_zcopy(to) || skb_zcopy(from))
5206 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005207
5208 if (skb_headlen(from) != 0) {
5209 struct page *page;
5210 unsigned int offset;
5211
Eric Dumazetc818fa92017-10-04 10:48:35 -07005212 if (to_shinfo->nr_frags +
5213 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005214 return false;
5215
5216 if (skb_head_is_locked(from))
5217 return false;
5218
5219 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5220
5221 page = virt_to_head_page(from->head);
5222 offset = from->data - (unsigned char *)page_address(page);
5223
Eric Dumazetc818fa92017-10-04 10:48:35 -07005224 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005225 page, offset, skb_headlen(from));
5226 *fragstolen = true;
5227 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005228 if (to_shinfo->nr_frags +
5229 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005230 return false;
5231
Weiping Panf4b549a2012-09-28 20:15:30 +00005232 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005233 }
5234
5235 WARN_ON_ONCE(delta < len);
5236
Eric Dumazetc818fa92017-10-04 10:48:35 -07005237 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5238 from_shinfo->frags,
5239 from_shinfo->nr_frags * sizeof(skb_frag_t));
5240 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005241
5242 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005243 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005244
Li RongQing8ea853f2012-09-18 16:53:21 +00005245 /* if the skb is not cloned this does nothing
5246 * since we set nr_frags to 0.
5247 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005248 for (i = 0; i < from_shinfo->nr_frags; i++)
5249 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005250
5251 to->truesize += delta;
5252 to->len += len;
5253 to->data_len += len;
5254
5255 *delta_truesize = delta;
5256 return true;
5257}
5258EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005259
5260/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005261 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005262 *
5263 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005264 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005265 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005266 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5267 * into/from a tunnel. Some information have to be cleared during these
5268 * operations.
5269 * skb_scrub_packet can also be used to clean a skb before injecting it in
5270 * another namespace (@xnet == true). We have to clear all information in the
5271 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005272 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005273void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005274{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005275 skb->pkt_type = PACKET_HOST;
5276 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005277 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005278 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005279 skb_ext_reset(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02005280 nf_reset_ct(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005281 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005282
Petr Machata6f9a5062018-11-19 16:11:07 +00005283#ifdef CONFIG_NET_SWITCHDEV
5284 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005285 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005286#endif
5287
Herbert Xu213dd742015-04-16 09:03:27 +08005288 if (!xnet)
5289 return;
5290
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005291 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005292 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005293 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005294}
5295EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005296
5297/**
5298 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5299 *
5300 * @skb: GSO skb
5301 *
5302 * skb_gso_transport_seglen is used to determine the real size of the
5303 * individual segments, including Layer4 headers (TCP/UDP).
5304 *
5305 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5306 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005307static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005308{
5309 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005310 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005311
Florian Westphalf993bc22014-10-20 13:49:18 +02005312 if (skb->encapsulation) {
5313 thlen = skb_inner_transport_header(skb) -
5314 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005315
Florian Westphalf993bc22014-10-20 13:49:18 +02005316 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5317 thlen += inner_tcp_hdrlen(skb);
5318 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5319 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005320 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005321 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005322 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5323 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005324 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005325 /* UFO sets gso_size to the size of the fragmentation
5326 * payload, i.e. the size of the L4 (UDP) header is already
5327 * accounted for.
5328 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005329 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005330}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005331
5332/**
5333 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5334 *
5335 * @skb: GSO skb
5336 *
5337 * skb_gso_network_seglen is used to determine the real size of the
5338 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5339 *
5340 * The MAC/L2 header is not accounted for.
5341 */
5342static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5343{
5344 unsigned int hdr_len = skb_transport_header(skb) -
5345 skb_network_header(skb);
5346
5347 return hdr_len + skb_gso_transport_seglen(skb);
5348}
5349
5350/**
5351 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5352 *
5353 * @skb: GSO skb
5354 *
5355 * skb_gso_mac_seglen is used to determine the real size of the
5356 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5357 * headers (TCP/UDP).
5358 */
5359static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5360{
5361 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5362
5363 return hdr_len + skb_gso_transport_seglen(skb);
5364}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005365
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005366/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005367 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5368 *
5369 * There are a couple of instances where we have a GSO skb, and we
5370 * want to determine what size it would be after it is segmented.
5371 *
5372 * We might want to check:
5373 * - L3+L4+payload size (e.g. IP forwarding)
5374 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5375 *
5376 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5377 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005378 * @skb: GSO skb
5379 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005380 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5381 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5382 *
5383 * @max_len: The maximum permissible length.
5384 *
5385 * Returns true if the segmented length <= max length.
5386 */
5387static inline bool skb_gso_size_check(const struct sk_buff *skb,
5388 unsigned int seg_len,
5389 unsigned int max_len) {
5390 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5391 const struct sk_buff *iter;
5392
5393 if (shinfo->gso_size != GSO_BY_FRAGS)
5394 return seg_len <= max_len;
5395
5396 /* Undo this so we can re-use header sizes */
5397 seg_len -= GSO_BY_FRAGS;
5398
5399 skb_walk_frags(skb, iter) {
5400 if (seg_len + skb_headlen(iter) > max_len)
5401 return false;
5402 }
5403
5404 return true;
5405}
5406
5407/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005408 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005409 *
5410 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005411 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005412 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005413 * skb_gso_validate_network_len validates if a given skb will fit a
5414 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5415 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005416 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005417bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005418{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005419 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005420}
Daniel Axtens779b7932018-03-01 17:13:37 +11005421EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005422
Daniel Axtens2b16f042018-01-31 14:15:33 +11005423/**
5424 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5425 *
5426 * @skb: GSO skb
5427 * @len: length to validate against
5428 *
5429 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5430 * length once split, including L2, L3 and L4 headers and the payload.
5431 */
5432bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5433{
5434 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5435}
5436EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5437
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005438static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5439{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005440 int mac_len, meta_len;
5441 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005442
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005443 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5444 kfree_skb(skb);
5445 return NULL;
5446 }
5447
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005448 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005449 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5450 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5451 mac_len - VLAN_HLEN - ETH_TLEN);
5452 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005453
5454 meta_len = skb_metadata_len(skb);
5455 if (meta_len) {
5456 meta = skb_metadata_end(skb) - meta_len;
5457 memmove(meta + VLAN_HLEN, meta, meta_len);
5458 }
5459
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005460 skb->mac_header += VLAN_HLEN;
5461 return skb;
5462}
5463
5464struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5465{
5466 struct vlan_hdr *vhdr;
5467 u16 vlan_tci;
5468
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005469 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005470 /* vlan_tci is already set-up so leave this for another time */
5471 return skb;
5472 }
5473
5474 skb = skb_share_check(skb, GFP_ATOMIC);
5475 if (unlikely(!skb))
5476 goto err_free;
Miaohe Lin55eff0e2020-08-15 04:44:31 -04005477 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5478 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005479 goto err_free;
5480
5481 vhdr = (struct vlan_hdr *)skb->data;
5482 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5483 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5484
5485 skb_pull_rcsum(skb, VLAN_HLEN);
5486 vlan_set_encap_proto(skb, vhdr);
5487
5488 skb = skb_reorder_vlan_header(skb);
5489 if (unlikely(!skb))
5490 goto err_free;
5491
5492 skb_reset_network_header(skb);
5493 skb_reset_transport_header(skb);
5494 skb_reset_mac_len(skb);
5495
5496 return skb;
5497
5498err_free:
5499 kfree_skb(skb);
5500 return NULL;
5501}
5502EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005503
Jiri Pirkoe2195122014-11-19 14:05:01 +01005504int skb_ensure_writable(struct sk_buff *skb, int write_len)
5505{
5506 if (!pskb_may_pull(skb, write_len))
5507 return -ENOMEM;
5508
5509 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5510 return 0;
5511
5512 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5513}
5514EXPORT_SYMBOL(skb_ensure_writable);
5515
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005516/* remove VLAN header from packet and update csum accordingly.
5517 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5518 */
5519int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005520{
5521 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005522 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005523 int err;
5524
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005525 if (WARN_ONCE(offset,
5526 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5527 offset)) {
5528 return -EINVAL;
5529 }
5530
Jiri Pirko93515d52014-11-19 14:05:02 +01005531 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5532 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005533 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005534
5535 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5536
5537 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5538 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5539
5540 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5541 __skb_pull(skb, VLAN_HLEN);
5542
5543 vlan_set_encap_proto(skb, vhdr);
5544 skb->mac_header += VLAN_HLEN;
5545
5546 if (skb_network_offset(skb) < ETH_HLEN)
5547 skb_set_network_header(skb, ETH_HLEN);
5548
5549 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005550
5551 return err;
5552}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005553EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005554
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005555/* Pop a vlan tag either from hwaccel or from payload.
5556 * Expects skb->data at mac header.
5557 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005558int skb_vlan_pop(struct sk_buff *skb)
5559{
5560 u16 vlan_tci;
5561 __be16 vlan_proto;
5562 int err;
5563
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005564 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005565 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005566 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005567 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005568 return 0;
5569
5570 err = __skb_vlan_pop(skb, &vlan_tci);
5571 if (err)
5572 return err;
5573 }
5574 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005575 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005576 return 0;
5577
5578 vlan_proto = skb->protocol;
5579 err = __skb_vlan_pop(skb, &vlan_tci);
5580 if (unlikely(err))
5581 return err;
5582
5583 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5584 return 0;
5585}
5586EXPORT_SYMBOL(skb_vlan_pop);
5587
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005588/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5589 * Expects skb->data at mac header.
5590 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005591int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5592{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005593 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005594 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005595 int err;
5596
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005597 if (WARN_ONCE(offset,
5598 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5599 offset)) {
5600 return -EINVAL;
5601 }
5602
Jiri Pirko93515d52014-11-19 14:05:02 +01005603 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005604 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005605 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005606 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005607
Jiri Pirko93515d52014-11-19 14:05:02 +01005608 skb->protocol = skb->vlan_proto;
5609 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005610
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005611 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005612 }
5613 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5614 return 0;
5615}
5616EXPORT_SYMBOL(skb_vlan_push);
5617
Guillaume Nault19fbcb32020-10-03 00:44:28 +02005618/**
5619 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5620 *
5621 * @skb: Socket buffer to modify
5622 *
5623 * Drop the Ethernet header of @skb.
5624 *
5625 * Expects that skb->data points to the mac header and that no VLAN tags are
5626 * present.
5627 *
5628 * Returns 0 on success, -errno otherwise.
5629 */
5630int skb_eth_pop(struct sk_buff *skb)
5631{
5632 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5633 skb_network_offset(skb) < ETH_HLEN)
5634 return -EPROTO;
5635
5636 skb_pull_rcsum(skb, ETH_HLEN);
5637 skb_reset_mac_header(skb);
5638 skb_reset_mac_len(skb);
5639
5640 return 0;
5641}
5642EXPORT_SYMBOL(skb_eth_pop);
5643
5644/**
5645 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5646 *
5647 * @skb: Socket buffer to modify
5648 * @dst: Destination MAC address of the new header
5649 * @src: Source MAC address of the new header
5650 *
5651 * Prepend @skb with a new Ethernet header.
5652 *
5653 * Expects that skb->data points to the mac header, which must be empty.
5654 *
5655 * Returns 0 on success, -errno otherwise.
5656 */
5657int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5658 const unsigned char *src)
5659{
5660 struct ethhdr *eth;
5661 int err;
5662
5663 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5664 return -EPROTO;
5665
5666 err = skb_cow_head(skb, sizeof(*eth));
5667 if (err < 0)
5668 return err;
5669
5670 skb_push(skb, sizeof(*eth));
5671 skb_reset_mac_header(skb);
5672 skb_reset_mac_len(skb);
5673
5674 eth = eth_hdr(skb);
5675 ether_addr_copy(eth->h_dest, dst);
5676 ether_addr_copy(eth->h_source, src);
5677 eth->h_proto = skb->protocol;
5678
5679 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5680
5681 return 0;
5682}
5683EXPORT_SYMBOL(skb_eth_push);
5684
John Hurley8822e272019-07-07 15:01:54 +01005685/* Update the ethertype of hdr and the skb csum value if required. */
5686static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5687 __be16 ethertype)
5688{
5689 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5690 __be16 diff[] = { ~hdr->h_proto, ethertype };
5691
5692 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5693 }
5694
5695 hdr->h_proto = ethertype;
5696}
5697
5698/**
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305699 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5700 * the packet
John Hurley8822e272019-07-07 15:01:54 +01005701 *
5702 * @skb: buffer
5703 * @mpls_lse: MPLS label stack entry to push
5704 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
Davide Carattifa4e0f82019-10-12 13:55:07 +02005705 * @mac_len: length of the MAC header
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305706 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5707 * ethernet
John Hurley8822e272019-07-07 15:01:54 +01005708 *
5709 * Expects skb->data at mac header.
5710 *
5711 * Returns 0 on success, -errno otherwise.
5712 */
Davide Carattifa4e0f82019-10-12 13:55:07 +02005713int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
Martin Varghesed04ac222019-12-05 05:57:22 +05305714 int mac_len, bool ethernet)
John Hurley8822e272019-07-07 15:01:54 +01005715{
5716 struct mpls_shim_hdr *lse;
5717 int err;
5718
5719 if (unlikely(!eth_p_mpls(mpls_proto)))
5720 return -EINVAL;
5721
5722 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5723 if (skb->encapsulation)
5724 return -EINVAL;
5725
5726 err = skb_cow_head(skb, MPLS_HLEN);
5727 if (unlikely(err))
5728 return err;
5729
5730 if (!skb->inner_protocol) {
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305731 skb_set_inner_network_header(skb, skb_network_offset(skb));
John Hurley8822e272019-07-07 15:01:54 +01005732 skb_set_inner_protocol(skb, skb->protocol);
5733 }
5734
5735 skb_push(skb, MPLS_HLEN);
5736 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005737 mac_len);
John Hurley8822e272019-07-07 15:01:54 +01005738 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005739 skb_set_network_header(skb, mac_len);
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305740 skb_reset_mac_len(skb);
John Hurley8822e272019-07-07 15:01:54 +01005741
5742 lse = mpls_hdr(skb);
5743 lse->label_stack_entry = mpls_lse;
5744 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5745
Guillaume Nault4296adc2020-10-02 21:53:08 +02005746 if (ethernet && mac_len >= ETH_HLEN)
John Hurley8822e272019-07-07 15:01:54 +01005747 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5748 skb->protocol = mpls_proto;
5749
5750 return 0;
5751}
5752EXPORT_SYMBOL_GPL(skb_mpls_push);
5753
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005754/**
John Hurleyed246ce2019-07-07 15:01:55 +01005755 * skb_mpls_pop() - pop the outermost MPLS header
5756 *
5757 * @skb: buffer
5758 * @next_proto: ethertype of header after popped MPLS header
Davide Carattifa4e0f82019-10-12 13:55:07 +02005759 * @mac_len: length of the MAC header
Martin Varghese76f99f92019-12-21 08:50:23 +05305760 * @ethernet: flag to indicate if the packet is ethernet
John Hurleyed246ce2019-07-07 15:01:55 +01005761 *
5762 * Expects skb->data at mac header.
5763 *
5764 * Returns 0 on success, -errno otherwise.
5765 */
Martin Varghese040b5cf2019-12-02 10:49:51 +05305766int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5767 bool ethernet)
John Hurleyed246ce2019-07-07 15:01:55 +01005768{
5769 int err;
5770
5771 if (unlikely(!eth_p_mpls(skb->protocol)))
Davide Carattidedc5a02019-10-12 13:55:06 +02005772 return 0;
John Hurleyed246ce2019-07-07 15:01:55 +01005773
Davide Carattifa4e0f82019-10-12 13:55:07 +02005774 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
John Hurleyed246ce2019-07-07 15:01:55 +01005775 if (unlikely(err))
5776 return err;
5777
5778 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5779 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005780 mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005781
5782 __skb_pull(skb, MPLS_HLEN);
5783 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005784 skb_set_network_header(skb, mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005785
Guillaume Nault4296adc2020-10-02 21:53:08 +02005786 if (ethernet && mac_len >= ETH_HLEN) {
John Hurleyed246ce2019-07-07 15:01:55 +01005787 struct ethhdr *hdr;
5788
5789 /* use mpls_hdr() to get ethertype to account for VLANs. */
5790 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5791 skb_mod_eth_type(skb, hdr, next_proto);
5792 }
5793 skb->protocol = next_proto;
5794
5795 return 0;
5796}
5797EXPORT_SYMBOL_GPL(skb_mpls_pop);
5798
5799/**
John Hurleyd27cf5c2019-07-07 15:01:56 +01005800 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5801 *
5802 * @skb: buffer
5803 * @mpls_lse: new MPLS label stack entry to update to
5804 *
5805 * Expects skb->data at mac header.
5806 *
5807 * Returns 0 on success, -errno otherwise.
5808 */
5809int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5810{
5811 int err;
5812
5813 if (unlikely(!eth_p_mpls(skb->protocol)))
5814 return -EINVAL;
5815
5816 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5817 if (unlikely(err))
5818 return err;
5819
5820 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5821 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5822
5823 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5824 }
5825
5826 mpls_hdr(skb)->label_stack_entry = mpls_lse;
5827
5828 return 0;
5829}
5830EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5831
5832/**
John Hurley2a2ea502019-07-07 15:01:57 +01005833 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
5834 *
5835 * @skb: buffer
5836 *
5837 * Expects skb->data at mac header.
5838 *
5839 * Returns 0 on success, -errno otherwise.
5840 */
5841int skb_mpls_dec_ttl(struct sk_buff *skb)
5842{
5843 u32 lse;
5844 u8 ttl;
5845
5846 if (unlikely(!eth_p_mpls(skb->protocol)))
5847 return -EINVAL;
5848
Davide Caratti13de4ed2020-12-03 10:58:21 +01005849 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
5850 return -ENOMEM;
5851
John Hurley2a2ea502019-07-07 15:01:57 +01005852 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
5853 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
5854 if (!--ttl)
5855 return -EINVAL;
5856
5857 lse &= ~MPLS_LS_TTL_MASK;
5858 lse |= ttl << MPLS_LS_TTL_SHIFT;
5859
5860 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
5861}
5862EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
5863
5864/**
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005865 * alloc_skb_with_frags - allocate skb with page frags
5866 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005867 * @header_len: size of linear part
5868 * @data_len: needed length in frags
5869 * @max_page_order: max page order desired.
5870 * @errcode: pointer to error code if any
5871 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005872 *
5873 * This can be used to allocate a paged skb, given a maximal order for frags.
5874 */
5875struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5876 unsigned long data_len,
5877 int max_page_order,
5878 int *errcode,
5879 gfp_t gfp_mask)
5880{
5881 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5882 unsigned long chunk;
5883 struct sk_buff *skb;
5884 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005885 int i;
5886
5887 *errcode = -EMSGSIZE;
5888 /* Note this test could be relaxed, if we succeed to allocate
5889 * high order pages...
5890 */
5891 if (npages > MAX_SKB_FRAGS)
5892 return NULL;
5893
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005894 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005895 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005896 if (!skb)
5897 return NULL;
5898
5899 skb->truesize += npages << PAGE_SHIFT;
5900
5901 for (i = 0; npages > 0; i++) {
5902 int order = max_page_order;
5903
5904 while (order) {
5905 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005906 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005907 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005908 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005909 order);
5910 if (page)
5911 goto fill_page;
5912 /* Do not retry other high order allocations */
5913 order = 1;
5914 max_page_order = 0;
5915 }
5916 order--;
5917 }
5918 page = alloc_page(gfp_mask);
5919 if (!page)
5920 goto failure;
5921fill_page:
5922 chunk = min_t(unsigned long, data_len,
5923 PAGE_SIZE << order);
5924 skb_fill_page_desc(skb, i, page, 0, chunk);
5925 data_len -= chunk;
5926 npages -= 1 << order;
5927 }
5928 return skb;
5929
5930failure:
5931 kfree_skb(skb);
5932 return NULL;
5933}
5934EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005935
5936/* carve out the first off bytes from skb when off < headlen */
5937static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5938 const int headlen, gfp_t gfp_mask)
5939{
5940 int i;
5941 int size = skb_end_offset(skb);
5942 int new_hlen = headlen - off;
5943 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005944
5945 size = SKB_DATA_ALIGN(size);
5946
5947 if (skb_pfmemalloc(skb))
5948 gfp_mask |= __GFP_MEMALLOC;
5949 data = kmalloc_reserve(size +
5950 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5951 gfp_mask, NUMA_NO_NODE, NULL);
5952 if (!data)
5953 return -ENOMEM;
5954
5955 size = SKB_WITH_OVERHEAD(ksize(data));
5956
5957 /* Copy real data, and all frags */
5958 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5959 skb->len -= off;
5960
5961 memcpy((struct skb_shared_info *)(data + size),
5962 skb_shinfo(skb),
5963 offsetof(struct skb_shared_info,
5964 frags[skb_shinfo(skb)->nr_frags]));
5965 if (skb_cloned(skb)) {
5966 /* drop the old head gracefully */
5967 if (skb_orphan_frags(skb, gfp_mask)) {
5968 kfree(data);
5969 return -ENOMEM;
5970 }
5971 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5972 skb_frag_ref(skb, i);
5973 if (skb_has_frag_list(skb))
5974 skb_clone_fraglist(skb);
5975 skb_release_data(skb);
5976 } else {
5977 /* we can reuse existing recount- all we did was
5978 * relocate values
5979 */
5980 skb_free_head(skb);
5981 }
5982
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005983 skb->head = data;
5984 skb->data = data;
5985 skb->head_frag = 0;
5986#ifdef NET_SKBUFF_DATA_USES_OFFSET
5987 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005988#else
5989 skb->end = skb->head + size;
5990#endif
5991 skb_set_tail_pointer(skb, skb_headlen(skb));
5992 skb_headers_offset_update(skb, 0);
5993 skb->cloned = 0;
5994 skb->hdr_len = 0;
5995 skb->nohdr = 0;
5996 atomic_set(&skb_shinfo(skb)->dataref, 1);
5997
5998 return 0;
5999}
6000
6001static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6002
6003/* carve out the first eat bytes from skb's frag_list. May recurse into
6004 * pskb_carve()
6005 */
6006static int pskb_carve_frag_list(struct sk_buff *skb,
6007 struct skb_shared_info *shinfo, int eat,
6008 gfp_t gfp_mask)
6009{
6010 struct sk_buff *list = shinfo->frag_list;
6011 struct sk_buff *clone = NULL;
6012 struct sk_buff *insp = NULL;
6013
6014 do {
6015 if (!list) {
6016 pr_err("Not enough bytes to eat. Want %d\n", eat);
6017 return -EFAULT;
6018 }
6019 if (list->len <= eat) {
6020 /* Eaten as whole. */
6021 eat -= list->len;
6022 list = list->next;
6023 insp = list;
6024 } else {
6025 /* Eaten partially. */
6026 if (skb_shared(list)) {
6027 clone = skb_clone(list, gfp_mask);
6028 if (!clone)
6029 return -ENOMEM;
6030 insp = list->next;
6031 list = clone;
6032 } else {
6033 /* This may be pulled without problems. */
6034 insp = list;
6035 }
6036 if (pskb_carve(list, eat, gfp_mask) < 0) {
6037 kfree_skb(clone);
6038 return -ENOMEM;
6039 }
6040 break;
6041 }
6042 } while (eat);
6043
6044 /* Free pulled out fragments. */
6045 while ((list = shinfo->frag_list) != insp) {
6046 shinfo->frag_list = list->next;
6047 kfree_skb(list);
6048 }
6049 /* And insert new clone at head. */
6050 if (clone) {
6051 clone->next = list;
6052 shinfo->frag_list = clone;
6053 }
6054 return 0;
6055}
6056
6057/* carve off first len bytes from skb. Split line (off) is in the
6058 * non-linear part of skb
6059 */
6060static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6061 int pos, gfp_t gfp_mask)
6062{
6063 int i, k = 0;
6064 int size = skb_end_offset(skb);
6065 u8 *data;
6066 const int nfrags = skb_shinfo(skb)->nr_frags;
6067 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006068
6069 size = SKB_DATA_ALIGN(size);
6070
6071 if (skb_pfmemalloc(skb))
6072 gfp_mask |= __GFP_MEMALLOC;
6073 data = kmalloc_reserve(size +
6074 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6075 gfp_mask, NUMA_NO_NODE, NULL);
6076 if (!data)
6077 return -ENOMEM;
6078
6079 size = SKB_WITH_OVERHEAD(ksize(data));
6080
6081 memcpy((struct skb_shared_info *)(data + size),
Miaohe Line3ec1e82020-08-15 04:48:53 -04006082 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006083 if (skb_orphan_frags(skb, gfp_mask)) {
6084 kfree(data);
6085 return -ENOMEM;
6086 }
6087 shinfo = (struct skb_shared_info *)(data + size);
6088 for (i = 0; i < nfrags; i++) {
6089 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6090
6091 if (pos + fsize > off) {
6092 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6093
6094 if (pos < off) {
6095 /* Split frag.
6096 * We have two variants in this case:
6097 * 1. Move all the frag to the second
6098 * part, if it is possible. F.e.
6099 * this approach is mandatory for TUX,
6100 * where splitting is expensive.
6101 * 2. Split is accurately. We make this.
6102 */
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006103 skb_frag_off_add(&shinfo->frags[0], off - pos);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006104 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6105 }
6106 skb_frag_ref(skb, i);
6107 k++;
6108 }
6109 pos += fsize;
6110 }
6111 shinfo->nr_frags = k;
6112 if (skb_has_frag_list(skb))
6113 skb_clone_fraglist(skb);
6114
Miaohe Lineabe8612020-08-15 04:46:41 -04006115 /* split line is in frag list */
6116 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6117 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6118 if (skb_has_frag_list(skb))
6119 kfree_skb_list(skb_shinfo(skb)->frag_list);
6120 kfree(data);
6121 return -ENOMEM;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006122 }
6123 skb_release_data(skb);
6124
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006125 skb->head = data;
6126 skb->head_frag = 0;
6127 skb->data = data;
6128#ifdef NET_SKBUFF_DATA_USES_OFFSET
6129 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006130#else
6131 skb->end = skb->head + size;
6132#endif
6133 skb_reset_tail_pointer(skb);
6134 skb_headers_offset_update(skb, 0);
6135 skb->cloned = 0;
6136 skb->hdr_len = 0;
6137 skb->nohdr = 0;
6138 skb->len -= off;
6139 skb->data_len = skb->len;
6140 atomic_set(&skb_shinfo(skb)->dataref, 1);
6141 return 0;
6142}
6143
6144/* remove len bytes from the beginning of the skb */
6145static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6146{
6147 int headlen = skb_headlen(skb);
6148
6149 if (len < headlen)
6150 return pskb_carve_inside_header(skb, len, headlen, gfp);
6151 else
6152 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6153}
6154
6155/* Extract to_copy bytes starting at off from skb, and return this in
6156 * a new skb
6157 */
6158struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6159 int to_copy, gfp_t gfp)
6160{
6161 struct sk_buff *clone = skb_clone(skb, gfp);
6162
6163 if (!clone)
6164 return NULL;
6165
6166 if (pskb_carve(clone, off, gfp) < 0 ||
6167 pskb_trim(clone, to_copy)) {
6168 kfree_skb(clone);
6169 return NULL;
6170 }
6171 return clone;
6172}
6173EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006174
6175/**
6176 * skb_condense - try to get rid of fragments/frag_list if possible
6177 * @skb: buffer
6178 *
6179 * Can be used to save memory before skb is added to a busy queue.
6180 * If packet has bytes in frags and enough tail room in skb->head,
6181 * pull all of them, so that we can free the frags right now and adjust
6182 * truesize.
6183 * Notes:
6184 * We do not reallocate skb->head thus can not fail.
6185 * Caller must re-evaluate skb->truesize if needed.
6186 */
6187void skb_condense(struct sk_buff *skb)
6188{
Eric Dumazet3174fed2016-12-09 08:02:05 -08006189 if (skb->data_len) {
6190 if (skb->data_len > skb->end - skb->tail ||
6191 skb_cloned(skb))
6192 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006193
Eric Dumazet3174fed2016-12-09 08:02:05 -08006194 /* Nice, we can free page frag(s) right now */
6195 __pskb_pull_tail(skb, skb->data_len);
6196 }
6197 /* At this point, skb->truesize might be over estimated,
6198 * because skb had a fragment, and fragments do not tell
6199 * their truesize.
6200 * When we pulled its content into skb->head, fragment
6201 * was freed, but __pskb_pull_tail() could not possibly
6202 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006203 */
6204 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6205}
Florian Westphaldf5042f2018-12-18 17:15:16 +01006206
6207#ifdef CONFIG_SKB_EXTENSIONS
6208static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6209{
6210 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6211}
6212
Paolo Abeni8b69a802020-01-09 07:59:24 -08006213/**
6214 * __skb_ext_alloc - allocate a new skb extensions storage
6215 *
Florian Westphal4930f482020-05-16 10:46:23 +02006216 * @flags: See kmalloc().
6217 *
Paolo Abeni8b69a802020-01-09 07:59:24 -08006218 * Returns the newly allocated pointer. The pointer can later attached to a
6219 * skb via __skb_ext_set().
6220 * Note: caller must handle the skb_ext as an opaque data.
6221 */
Florian Westphal4930f482020-05-16 10:46:23 +02006222struct skb_ext *__skb_ext_alloc(gfp_t flags)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006223{
Florian Westphal4930f482020-05-16 10:46:23 +02006224 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006225
6226 if (new) {
6227 memset(new->offset, 0, sizeof(new->offset));
6228 refcount_set(&new->refcnt, 1);
6229 }
6230
6231 return new;
6232}
6233
Florian Westphal41650792018-12-18 17:15:27 +01006234static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6235 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006236{
6237 struct skb_ext *new;
6238
6239 if (refcount_read(&old->refcnt) == 1)
6240 return old;
6241
6242 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6243 if (!new)
6244 return NULL;
6245
6246 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6247 refcount_set(&new->refcnt, 1);
6248
Florian Westphal41650792018-12-18 17:15:27 +01006249#ifdef CONFIG_XFRM
6250 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6251 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6252 unsigned int i;
6253
6254 for (i = 0; i < sp->len; i++)
6255 xfrm_state_hold(sp->xvec[i]);
6256 }
6257#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006258 __skb_ext_put(old);
6259 return new;
6260}
6261
6262/**
Paolo Abeni8b69a802020-01-09 07:59:24 -08006263 * __skb_ext_set - attach the specified extension storage to this skb
6264 * @skb: buffer
6265 * @id: extension id
6266 * @ext: extension storage previously allocated via __skb_ext_alloc()
6267 *
6268 * Existing extensions, if any, are cleared.
6269 *
6270 * Returns the pointer to the extension.
6271 */
6272void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6273 struct skb_ext *ext)
6274{
6275 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6276
6277 skb_ext_put(skb);
6278 newlen = newoff + skb_ext_type_len[id];
6279 ext->chunks = newlen;
6280 ext->offset[id] = newoff;
6281 skb->extensions = ext;
6282 skb->active_extensions = 1 << id;
6283 return skb_ext_get_ptr(ext, id);
6284}
6285
6286/**
Florian Westphaldf5042f2018-12-18 17:15:16 +01006287 * skb_ext_add - allocate space for given extension, COW if needed
6288 * @skb: buffer
6289 * @id: extension to allocate space for
6290 *
6291 * Allocates enough space for the given extension.
6292 * If the extension is already present, a pointer to that extension
6293 * is returned.
6294 *
6295 * If the skb was cloned, COW applies and the returned memory can be
6296 * modified without changing the extension space of clones buffers.
6297 *
6298 * Returns pointer to the extension or NULL on allocation failure.
6299 */
6300void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6301{
6302 struct skb_ext *new, *old = NULL;
6303 unsigned int newlen, newoff;
6304
6305 if (skb->active_extensions) {
6306 old = skb->extensions;
6307
Florian Westphal41650792018-12-18 17:15:27 +01006308 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006309 if (!new)
6310 return NULL;
6311
Paolo Abeni682ec852018-12-21 19:03:15 +01006312 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01006313 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006314
Paolo Abenie94e50b2018-12-21 19:03:13 +01006315 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006316 } else {
6317 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6318
Florian Westphal4930f482020-05-16 10:46:23 +02006319 new = __skb_ext_alloc(GFP_ATOMIC);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006320 if (!new)
6321 return NULL;
6322 }
6323
6324 newlen = newoff + skb_ext_type_len[id];
6325 new->chunks = newlen;
6326 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006327set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01006328 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006329 skb->active_extensions |= 1 << id;
6330 return skb_ext_get_ptr(new, id);
6331}
6332EXPORT_SYMBOL(skb_ext_add);
6333
Florian Westphal41650792018-12-18 17:15:27 +01006334#ifdef CONFIG_XFRM
6335static void skb_ext_put_sp(struct sec_path *sp)
6336{
6337 unsigned int i;
6338
6339 for (i = 0; i < sp->len; i++)
6340 xfrm_state_put(sp->xvec[i]);
6341}
6342#endif
6343
Florian Westphaldf5042f2018-12-18 17:15:16 +01006344void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6345{
6346 struct skb_ext *ext = skb->extensions;
6347
6348 skb->active_extensions &= ~(1 << id);
6349 if (skb->active_extensions == 0) {
6350 skb->extensions = NULL;
6351 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01006352#ifdef CONFIG_XFRM
6353 } else if (id == SKB_EXT_SEC_PATH &&
6354 refcount_read(&ext->refcnt) == 1) {
6355 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6356
6357 skb_ext_put_sp(sp);
6358 sp->len = 0;
6359#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006360 }
6361}
6362EXPORT_SYMBOL(__skb_ext_del);
6363
6364void __skb_ext_put(struct skb_ext *ext)
6365{
6366 /* If this is last clone, nothing can increment
6367 * it after check passes. Avoids one atomic op.
6368 */
6369 if (refcount_read(&ext->refcnt) == 1)
6370 goto free_now;
6371
6372 if (!refcount_dec_and_test(&ext->refcnt))
6373 return;
6374free_now:
Florian Westphal41650792018-12-18 17:15:27 +01006375#ifdef CONFIG_XFRM
6376 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6377 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6378#endif
6379
Florian Westphaldf5042f2018-12-18 17:15:16 +01006380 kmem_cache_free(skbuff_ext_cache, ext);
6381}
6382EXPORT_SYMBOL(__skb_ext_put);
6383#endif /* CONFIG_SKB_EXTENSIONS */