blob: d17b87aabc8b6345d8bb5253a2a447004942d742 [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{
Eric Dumazetadf30902009-06-02 05:19:30 +0000662 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700663 if (skb->destructor) {
664 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 skb->destructor(skb);
666 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000667#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100668 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100669#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100670 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700671}
672
673/* Free everything but the sk_buff shell. */
674static void skb_release_all(struct sk_buff *skb)
675{
676 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200677 if (likely(skb->head))
678 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Herbert Xu2d4baff2007-11-26 23:11:19 +0800681/**
682 * __kfree_skb - private function
683 * @skb: buffer
684 *
685 * Free an sk_buff. Release anything attached to the buffer.
686 * Clean the state. This is an internal helper function. Users should
687 * always call kfree_skb
688 */
689
690void __kfree_skb(struct sk_buff *skb)
691{
692 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree_skbmem(skb);
694}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800695EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800698 * kfree_skb - free an sk_buff
699 * @skb: buffer to free
700 *
701 * Drop a reference to the buffer and free it if the usage count has
702 * hit zero.
703 */
704void kfree_skb(struct sk_buff *skb)
705{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200706 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800707 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200708
Neil Hormanead2ceb2009-03-11 09:49:55 +0000709 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800710 __kfree_skb(skb);
711}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800712EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800713
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700714void kfree_skb_list(struct sk_buff *segs)
715{
716 while (segs) {
717 struct sk_buff *next = segs->next;
718
719 kfree_skb(segs);
720 segs = next;
721 }
722}
723EXPORT_SYMBOL(kfree_skb_list);
724
Willem de Bruijn64131392019-07-07 05:51:55 -0400725/* Dump skb information and contents.
726 *
727 * Must only be called from net_ratelimit()-ed paths.
728 *
Vladimir Oltean302af7c2020-10-05 17:48:38 +0300729 * Dumps whole packets if full_pkt, only headers otherwise.
Willem de Bruijn64131392019-07-07 05:51:55 -0400730 */
731void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
732{
Willem de Bruijn64131392019-07-07 05:51:55 -0400733 struct skb_shared_info *sh = skb_shinfo(skb);
734 struct net_device *dev = skb->dev;
735 struct sock *sk = skb->sk;
736 struct sk_buff *list_skb;
737 bool has_mac, has_trans;
738 int headroom, tailroom;
739 int i, len, seg_len;
740
741 if (full_pkt)
Willem de Bruijn64131392019-07-07 05:51:55 -0400742 len = skb->len;
743 else
744 len = min_t(int, skb->len, MAX_HEADER + 128);
745
746 headroom = skb_headroom(skb);
747 tailroom = skb_tailroom(skb);
748
749 has_mac = skb_mac_header_was_set(skb);
750 has_trans = skb_transport_header_was_set(skb);
751
752 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
753 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
754 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
755 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
756 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
757 level, skb->len, headroom, skb_headlen(skb), tailroom,
758 has_mac ? skb->mac_header : -1,
759 has_mac ? skb_mac_header_len(skb) : -1,
760 skb->network_header,
761 has_trans ? skb_network_header_len(skb) : -1,
762 has_trans ? skb->transport_header : -1,
763 sh->tx_flags, sh->nr_frags,
764 sh->gso_size, sh->gso_type, sh->gso_segs,
765 skb->csum, skb->ip_summed, skb->csum_complete_sw,
766 skb->csum_valid, skb->csum_level,
767 skb->hash, skb->sw_hash, skb->l4_hash,
768 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
769
770 if (dev)
771 printk("%sdev name=%s feat=0x%pNF\n",
772 level, dev->name, &dev->features);
773 if (sk)
Qian Caidb8051f2019-07-16 11:43:05 -0400774 printk("%ssk family=%hu type=%u proto=%u\n",
Willem de Bruijn64131392019-07-07 05:51:55 -0400775 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
776
777 if (full_pkt && headroom)
778 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
779 16, 1, skb->head, headroom, false);
780
781 seg_len = min_t(int, skb_headlen(skb), len);
782 if (seg_len)
783 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
784 16, 1, skb->data, seg_len, false);
785 len -= seg_len;
786
787 if (full_pkt && tailroom)
788 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
789 16, 1, skb_tail_pointer(skb), tailroom, false);
790
791 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
792 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
793 u32 p_off, p_len, copied;
794 struct page *p;
795 u8 *vaddr;
796
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700797 skb_frag_foreach_page(frag, skb_frag_off(frag),
Willem de Bruijn64131392019-07-07 05:51:55 -0400798 skb_frag_size(frag), p, p_off, p_len,
799 copied) {
800 seg_len = min_t(int, p_len, len);
801 vaddr = kmap_atomic(p);
802 print_hex_dump(level, "skb frag: ",
803 DUMP_PREFIX_OFFSET,
804 16, 1, vaddr + p_off, seg_len, false);
805 kunmap_atomic(vaddr);
806 len -= seg_len;
807 if (!len)
808 break;
809 }
810 }
811
812 if (full_pkt && skb_has_frag_list(skb)) {
813 printk("skb fraglist:\n");
814 skb_walk_frags(skb, list_skb)
815 skb_dump(level, list_skb, true);
816 }
817}
818EXPORT_SYMBOL(skb_dump);
819
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700820/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000821 * skb_tx_error - report an sk_buff xmit error
822 * @skb: buffer that triggered an error
823 *
824 * Report xmit error if a device callback is tracking this skb.
825 * skb must be freed afterwards.
826 */
827void skb_tx_error(struct sk_buff *skb)
828{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400829 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000830}
831EXPORT_SYMBOL(skb_tx_error);
832
Herbert Xube769db2020-08-22 08:23:29 +1000833#ifdef CONFIG_TRACEPOINTS
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000834/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000835 * consume_skb - free an skbuff
836 * @skb: buffer to free
837 *
838 * Drop a ref to the buffer and free it if the usage count has hit zero
839 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
840 * is being dropped after a failure and notes that
841 */
842void consume_skb(struct sk_buff *skb)
843{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200844 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000845 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200846
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900847 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000848 __kfree_skb(skb);
849}
850EXPORT_SYMBOL(consume_skb);
Herbert Xube769db2020-08-22 08:23:29 +1000851#endif
Neil Hormanead2ceb2009-03-11 09:49:55 +0000852
Paolo Abeni0a463c72017-06-12 11:23:42 +0200853/**
854 * consume_stateless_skb - free an skbuff, assuming it is stateless
855 * @skb: buffer to free
856 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200857 * Alike consume_skb(), but this variant assumes that this is the last
858 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200859 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200860void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200861{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200862 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200863 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200864 kfree_skbmem(skb);
865}
866
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100867void __kfree_skb_flush(void)
868{
869 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
870
871 /* flush skb_cache if containing objects */
872 if (nc->skb_count) {
873 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
874 nc->skb_cache);
875 nc->skb_count = 0;
876 }
877}
878
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100879static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100880{
881 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
882
883 /* drop skb->head and call any destructors for packet */
884 skb_release_all(skb);
885
886 /* record skb to CPU local list */
887 nc->skb_cache[nc->skb_count++] = skb;
888
889#ifdef CONFIG_SLUB
890 /* SLUB writes into objects when freeing */
891 prefetchw(skb);
892#endif
893
894 /* flush skb_cache if it is filled */
895 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
896 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
897 nc->skb_cache);
898 nc->skb_count = 0;
899 }
900}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100901void __kfree_skb_defer(struct sk_buff *skb)
902{
903 _kfree_skb_defer(skb);
904}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100905
906void napi_consume_skb(struct sk_buff *skb, int budget)
907{
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100908 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100909 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100910 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100911 return;
912 }
913
Paolo Abeni76088942017-06-14 11:48:48 +0200914 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100915 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200916
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100917 /* if reaching here SKB is ready to free */
918 trace_consume_skb(skb);
919
920 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700921 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100922 __kfree_skb(skb);
923 return;
924 }
925
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100926 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100927}
928EXPORT_SYMBOL(napi_consume_skb);
929
Eric Dumazetb1937222014-09-28 22:18:47 -0700930/* Make sure a field is enclosed inside headers_start/headers_end section */
931#define CHECK_SKB_FIELD(field) \
932 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
933 offsetof(struct sk_buff, headers_start)); \
934 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
935 offsetof(struct sk_buff, headers_end)); \
936
Herbert Xudec18812007-10-14 00:37:30 -0700937static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
938{
939 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700940 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700941 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700942 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000943 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100944 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -0700945 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700946
Eric Dumazetb1937222014-09-28 22:18:47 -0700947 /* Note : this field could be in headers_start/headers_end section
948 * It is not yet because we do not want to have a 16 bit hole
949 */
950 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300951
Eric Dumazetb1937222014-09-28 22:18:47 -0700952 memcpy(&new->headers_start, &old->headers_start,
953 offsetof(struct sk_buff, headers_end) -
954 offsetof(struct sk_buff, headers_start));
955 CHECK_SKB_FIELD(protocol);
956 CHECK_SKB_FIELD(csum);
957 CHECK_SKB_FIELD(hash);
958 CHECK_SKB_FIELD(priority);
959 CHECK_SKB_FIELD(skb_iif);
960 CHECK_SKB_FIELD(vlan_proto);
961 CHECK_SKB_FIELD(vlan_tci);
962 CHECK_SKB_FIELD(transport_header);
963 CHECK_SKB_FIELD(network_header);
964 CHECK_SKB_FIELD(mac_header);
965 CHECK_SKB_FIELD(inner_protocol);
966 CHECK_SKB_FIELD(inner_transport_header);
967 CHECK_SKB_FIELD(inner_network_header);
968 CHECK_SKB_FIELD(inner_mac_header);
969 CHECK_SKB_FIELD(mark);
970#ifdef CONFIG_NETWORK_SECMARK
971 CHECK_SKB_FIELD(secmark);
972#endif
Cong Wange0d10952013-08-01 11:10:25 +0800973#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700974 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300975#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800976#ifdef CONFIG_XPS
977 CHECK_SKB_FIELD(sender_cpu);
978#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700979#ifdef CONFIG_NET_SCHED
980 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700981#endif
982
Herbert Xudec18812007-10-14 00:37:30 -0700983}
984
Herbert Xu82c49a32009-05-22 22:11:37 +0000985/*
986 * You should not add any new code to this function. Add it to
987 * __copy_skb_header above instead.
988 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700989static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#define C(x) n->x = skb->x
992
993 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700995 __copy_skb_header(n, skb);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 C(len);
998 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700999 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -07001000 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -08001001 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -07001003 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +02001004 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 C(tail);
1007 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -08001008 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +00001009 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -08001010 C(data);
1011 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +03001012 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 atomic_inc(&(skb_shinfo(skb)->dataref));
1015 skb->cloned = 1;
1016
1017 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001018#undef C
1019}
1020
1021/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -07001022 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1023 * @first: first sk_buff of the msg
1024 */
1025struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1026{
1027 struct sk_buff *n;
1028
1029 n = alloc_skb(0, GFP_ATOMIC);
1030 if (!n)
1031 return NULL;
1032
1033 n->len = first->len;
1034 n->data_len = first->len;
1035 n->truesize = first->truesize;
1036
1037 skb_shinfo(n)->frag_list = first;
1038
1039 __copy_skb_header(n, first);
1040 n->destructor = NULL;
1041
1042 return n;
1043}
1044EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1045
1046/**
Herbert Xue0053ec2007-10-14 00:37:52 -07001047 * skb_morph - morph one skb into another
1048 * @dst: the skb to receive the contents
1049 * @src: the skb to supply the contents
1050 *
1051 * This is identical to skb_clone except that the target skb is
1052 * supplied by the user.
1053 *
1054 * The target skb is returned upon exit.
1055 */
1056struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1057{
Herbert Xu2d4baff2007-11-26 23:11:19 +08001058 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -07001059 return __skb_clone(dst, src);
1060}
1061EXPORT_SYMBOL_GPL(skb_morph);
1062
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001063int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001064{
1065 unsigned long max_pg, num_pg, new_pg, old_pg;
1066 struct user_struct *user;
1067
1068 if (capable(CAP_IPC_LOCK) || !size)
1069 return 0;
1070
1071 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1072 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1073 user = mmp->user ? : current_user();
1074
1075 do {
1076 old_pg = atomic_long_read(&user->locked_vm);
1077 new_pg = old_pg + num_pg;
1078 if (new_pg > max_pg)
1079 return -ENOBUFS;
1080 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1081 old_pg);
1082
1083 if (!mmp->user) {
1084 mmp->user = get_uid(user);
1085 mmp->num_pg = num_pg;
1086 } else {
1087 mmp->num_pg += num_pg;
1088 }
1089
1090 return 0;
1091}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001092EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001093
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001094void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001095{
1096 if (mmp->user) {
1097 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1098 free_uid(mmp->user);
1099 }
1100}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001101EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001102
Willem de Bruijn52267792017-08-03 16:29:39 -04001103struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
1104{
1105 struct ubuf_info *uarg;
1106 struct sk_buff *skb;
1107
1108 WARN_ON_ONCE(!in_task());
1109
1110 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1111 if (!skb)
1112 return NULL;
1113
1114 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1115 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001116 uarg->mmp.user = NULL;
1117
1118 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1119 kfree_skb(skb);
1120 return NULL;
1121 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001122
1123 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001124 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1125 uarg->len = 1;
1126 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001127 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001128 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001129 sock_hold(sk);
1130
1131 return uarg;
1132}
1133EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
1134
1135static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1136{
1137 return container_of((void *)uarg, struct sk_buff, cb);
1138}
1139
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001140struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1141 struct ubuf_info *uarg)
1142{
1143 if (uarg) {
1144 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1145 u32 bytelen, next;
1146
1147 /* realloc only when socket is locked (TCP, UDP cork),
1148 * so uarg->len and sk_zckey access is serialized
1149 */
1150 if (!sock_owned_by_user(sk)) {
1151 WARN_ON_ONCE(1);
1152 return NULL;
1153 }
1154
1155 bytelen = uarg->bytelen + size;
1156 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1157 /* TCP can create new skb to attach new uarg */
1158 if (sk->sk_type == SOCK_STREAM)
1159 goto new_alloc;
1160 return NULL;
1161 }
1162
1163 next = (u32)atomic_read(&sk->sk_zckey);
1164 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001165 if (mm_account_pinned_pages(&uarg->mmp, size))
1166 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001167 uarg->len++;
1168 uarg->bytelen = bytelen;
1169 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001170
1171 /* no extra ref when appending to datagram (MSG_MORE) */
1172 if (sk->sk_type == SOCK_STREAM)
1173 sock_zerocopy_get(uarg);
1174
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001175 return uarg;
1176 }
1177 }
1178
1179new_alloc:
1180 return sock_zerocopy_alloc(sk, size);
1181}
1182EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1183
1184static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1185{
1186 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1187 u32 old_lo, old_hi;
1188 u64 sum_len;
1189
1190 old_lo = serr->ee.ee_info;
1191 old_hi = serr->ee.ee_data;
1192 sum_len = old_hi - old_lo + 1ULL + len;
1193
1194 if (sum_len >= (1ULL << 32))
1195 return false;
1196
1197 if (lo != old_hi + 1)
1198 return false;
1199
1200 serr->ee.ee_data += len;
1201 return true;
1202}
1203
Willem de Bruijn52267792017-08-03 16:29:39 -04001204void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1205{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001206 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001207 struct sock_exterr_skb *serr;
1208 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001209 struct sk_buff_head *q;
1210 unsigned long flags;
1211 u32 lo, hi;
1212 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001213
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001214 mm_unaccount_pinned_pages(&uarg->mmp);
1215
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001216 /* if !len, there was only 1 call, and it was aborted
1217 * so do not queue a completion notification
1218 */
1219 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001220 goto release;
1221
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001222 len = uarg->len;
1223 lo = uarg->id;
1224 hi = uarg->id + len - 1;
1225
Willem de Bruijn52267792017-08-03 16:29:39 -04001226 serr = SKB_EXT_ERR(skb);
1227 memset(serr, 0, sizeof(*serr));
1228 serr->ee.ee_errno = 0;
1229 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001230 serr->ee.ee_data = hi;
1231 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001232 if (!success)
1233 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1234
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001235 q = &sk->sk_error_queue;
1236 spin_lock_irqsave(&q->lock, flags);
1237 tail = skb_peek_tail(q);
1238 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1239 !skb_zerocopy_notify_extend(tail, lo, len)) {
1240 __skb_queue_tail(q, skb);
1241 skb = NULL;
1242 }
1243 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001244
1245 sk->sk_error_report(sk);
1246
1247release:
1248 consume_skb(skb);
1249 sock_put(sk);
1250}
1251EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1252
1253void sock_zerocopy_put(struct ubuf_info *uarg)
1254{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001255 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001256 if (uarg->callback)
1257 uarg->callback(uarg, uarg->zerocopy);
1258 else
1259 consume_skb(skb_from_uarg(uarg));
1260 }
1261}
1262EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1263
Willem de Bruijn52900d22018-11-30 15:32:40 -05001264void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001265{
1266 if (uarg) {
1267 struct sock *sk = skb_from_uarg(uarg)->sk;
1268
1269 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001270 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001271
Willem de Bruijn52900d22018-11-30 15:32:40 -05001272 if (have_uref)
1273 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001274 }
1275}
1276EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1277
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001278int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1279{
1280 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1281}
1282EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1283
Willem de Bruijn52267792017-08-03 16:29:39 -04001284int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1285 struct msghdr *msg, int len,
1286 struct ubuf_info *uarg)
1287{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001288 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001289 struct iov_iter orig_iter = msg->msg_iter;
1290 int err, orig_len = skb->len;
1291
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001292 /* An skb can only point to one uarg. This edge case happens when
1293 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1294 */
1295 if (orig_uarg && uarg != orig_uarg)
1296 return -EEXIST;
1297
Willem de Bruijn52267792017-08-03 16:29:39 -04001298 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1299 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001300 struct sock *save_sk = skb->sk;
1301
Willem de Bruijn52267792017-08-03 16:29:39 -04001302 /* Streams do not free skb on error. Reset to prev state. */
1303 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001304 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001305 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001306 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001307 return err;
1308 }
1309
Willem de Bruijn52900d22018-11-30 15:32:40 -05001310 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001311 return skb->len - orig_len;
1312}
1313EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1314
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001315static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001316 gfp_t gfp_mask)
1317{
1318 if (skb_zcopy(orig)) {
1319 if (skb_zcopy(nskb)) {
1320 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1321 if (!gfp_mask) {
1322 WARN_ON_ONCE(1);
1323 return -ENOMEM;
1324 }
1325 if (skb_uarg(nskb) == skb_uarg(orig))
1326 return 0;
1327 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1328 return -EIO;
1329 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001330 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001331 }
1332 return 0;
1333}
1334
Ben Hutchings2c530402012-07-10 10:55:09 +00001335/**
1336 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001337 * @skb: the skb to modify
1338 * @gfp_mask: allocation priority
1339 *
1340 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1341 * It will copy all frags into kernel and drop the reference
1342 * to userspace pages.
1343 *
1344 * If this function is called from an interrupt gfp_mask() must be
1345 * %GFP_ATOMIC.
1346 *
1347 * Returns 0 on success or a negative error code on failure
1348 * to allocate kernel memory to copy to.
1349 */
1350int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001351{
Shirley Maa6686f22011-07-06 12:22:12 +00001352 int num_frags = skb_shinfo(skb)->nr_frags;
1353 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001354 int i, new_frags;
1355 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001356
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001357 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1358 return -EINVAL;
1359
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001360 if (!num_frags)
1361 goto release;
1362
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001363 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1364 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001365 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001366 if (!page) {
1367 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001368 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001369 put_page(head);
1370 head = next;
1371 }
1372 return -ENOMEM;
1373 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001374 set_page_private(page, (unsigned long)head);
1375 head = page;
1376 }
1377
1378 page = head;
1379 d_off = 0;
1380 for (i = 0; i < num_frags; i++) {
1381 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1382 u32 p_off, p_len, copied;
1383 struct page *p;
1384 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001385
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001386 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
Willem de Bruijnc613c202017-07-31 08:15:47 -04001387 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001388 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001389 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001390
1391 while (done < p_len) {
1392 if (d_off == PAGE_SIZE) {
1393 d_off = 0;
1394 page = (struct page *)page_private(page);
1395 }
1396 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1397 memcpy(page_address(page) + d_off,
1398 vaddr + p_off + done, copy);
1399 done += copy;
1400 d_off += copy;
1401 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001402 kunmap_atomic(vaddr);
1403 }
Shirley Maa6686f22011-07-06 12:22:12 +00001404 }
1405
1406 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001407 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001408 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001409
Shirley Maa6686f22011-07-06 12:22:12 +00001410 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001411 for (i = 0; i < new_frags - 1; i++) {
1412 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001413 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001414 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001415 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1416 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001417
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001418release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001419 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001420 return 0;
1421}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001422EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001423
Herbert Xue0053ec2007-10-14 00:37:52 -07001424/**
1425 * skb_clone - duplicate an sk_buff
1426 * @skb: buffer to clone
1427 * @gfp_mask: allocation priority
1428 *
1429 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1430 * copies share the same packet data but not structure. The new
1431 * buffer has a reference count of 1. If the allocation fails the
1432 * function returns %NULL otherwise the new buffer is returned.
1433 *
1434 * If this function is called from an interrupt gfp_mask() must be
1435 * %GFP_ATOMIC.
1436 */
1437
1438struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1439{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001440 struct sk_buff_fclones *fclones = container_of(skb,
1441 struct sk_buff_fclones,
1442 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001443 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001444
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001445 if (skb_orphan_frags(skb, gfp_mask))
1446 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001447
Herbert Xue0053ec2007-10-14 00:37:52 -07001448 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001449 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001450 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001451 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001452 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001453 if (skb_pfmemalloc(skb))
1454 gfp_mask |= __GFP_MEMALLOC;
1455
Herbert Xue0053ec2007-10-14 00:37:52 -07001456 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1457 if (!n)
1458 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001459
Herbert Xue0053ec2007-10-14 00:37:52 -07001460 n->fclone = SKB_FCLONE_UNAVAILABLE;
1461 }
1462
1463 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001465EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001467void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001468{
Eric Dumazet030737b2013-10-19 11:42:54 -07001469 /* Only adjust this if it actually is csum_start rather than csum */
1470 if (skb->ip_summed == CHECKSUM_PARTIAL)
1471 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001472 /* {transport,network,mac}_header and tail are relative to skb->head */
1473 skb->transport_header += off;
1474 skb->network_header += off;
1475 if (skb_mac_header_was_set(skb))
1476 skb->mac_header += off;
1477 skb->inner_transport_header += off;
1478 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001479 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001480}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001481EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001482
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001483void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Herbert Xudec18812007-10-14 00:37:30 -07001485 __copy_skb_header(new, old);
1486
Herbert Xu79671682006-06-22 02:40:14 -07001487 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1488 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1489 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001491EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Mel Gormanc93bdd02012-07-31 16:44:19 -07001493static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1494{
1495 if (skb_pfmemalloc(skb))
1496 return SKB_ALLOC_RX;
1497 return 0;
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/**
1501 * skb_copy - create private copy of an sk_buff
1502 * @skb: buffer to copy
1503 * @gfp_mask: allocation priority
1504 *
1505 * Make a copy of both an &sk_buff and its data. This is used when the
1506 * caller wishes to modify the data and needs a private copy of the
1507 * data to alter. Returns %NULL on failure or the pointer to the buffer
1508 * on success. The returned buffer has a reference count of 1.
1509 *
1510 * As by-product this function converts non-linear &sk_buff to linear
1511 * one, so that &sk_buff becomes completely private and caller is allowed
1512 * to modify all the data of returned buffer. This means that this
1513 * function is not recommended for use in circumstances when only
1514 * header is going to be modified. Use pskb_copy() instead.
1515 */
1516
Al Virodd0fc662005-10-07 07:46:04 +01001517struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001519 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001520 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001521 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1522 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (!n)
1525 return NULL;
1526
1527 /* Set the data pointer */
1528 skb_reserve(n, headerlen);
1529 /* Set the tail pointer and length */
1530 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Tim Hansen9f77fad2017-10-09 11:37:59 -04001532 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001534 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return n;
1536}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001537EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001540 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001542 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001544 * @fclone: if true allocate the copy of the skb from the fclone
1545 * cache instead of the head cache; it is recommended to set this
1546 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 *
1548 * Make a copy of both an &sk_buff and part of its data, located
1549 * in header. Fragmented data remain shared. This is used when
1550 * the caller wishes to modify only header of &sk_buff and needs
1551 * private copy of the header to alter. Returns %NULL on failure
1552 * or the pointer to the buffer on success.
1553 * The returned buffer has a reference count of 1.
1554 */
1555
Octavian Purdilabad93e92014-06-12 01:36:26 +03001556struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1557 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Eric Dumazet117632e2011-12-03 21:39:53 +00001559 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001560 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1561 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (!n)
1564 goto out;
1565
1566 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001567 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* Set the tail pointer and length */
1569 skb_put(n, skb_headlen(skb));
1570 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001571 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Herbert Xu25f484a2006-11-07 14:57:15 -08001573 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 n->data_len = skb->data_len;
1575 n->len = skb->len;
1576
1577 if (skb_shinfo(skb)->nr_frags) {
1578 int i;
1579
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001580 if (skb_orphan_frags(skb, gfp_mask) ||
1581 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001582 kfree_skb(n);
1583 n = NULL;
1584 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1587 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001588 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 skb_shinfo(n)->nr_frags = i;
1591 }
1592
David S. Miller21dc3302010-08-23 00:13:46 -07001593 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1595 skb_clone_fraglist(n);
1596 }
1597
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001598 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599out:
1600 return n;
1601}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001602EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604/**
1605 * pskb_expand_head - reallocate header of &sk_buff
1606 * @skb: buffer to reallocate
1607 * @nhead: room to add at head
1608 * @ntail: room to add at tail
1609 * @gfp_mask: allocation priority
1610 *
Mathias Krausebc323832013-11-07 14:18:26 +01001611 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1612 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * reference count of 1. Returns zero in the case of success or error,
1614 * if expansion failed. In the last case, &sk_buff is not changed.
1615 *
1616 * All the pointers pointing into skb header may change and must be
1617 * reloaded after call to this function.
1618 */
1619
Victor Fusco86a76ca2005-07-08 14:57:47 -07001620int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001621 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Eric Dumazet158f3232017-01-27 07:11:27 -08001623 int i, osize = skb_end_offset(skb);
1624 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001626 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Herbert Xu4edd87a2008-10-01 07:09:38 -07001628 BUG_ON(nhead < 0);
1629
Tim Hansen9f77fad2017-10-09 11:37:59 -04001630 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 size = SKB_DATA_ALIGN(size);
1633
Mel Gormanc93bdd02012-07-31 16:44:19 -07001634 if (skb_pfmemalloc(skb))
1635 gfp_mask |= __GFP_MEMALLOC;
1636 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1637 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!data)
1639 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001640 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001643 * optimized for the cases when header is void.
1644 */
1645 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1646
1647 memcpy((struct skb_shared_info *)(data + size),
1648 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001649 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Alexander Duyck3e245912012-05-04 14:26:51 +00001651 /*
1652 * if shinfo is shared we must drop the old head gracefully, but if it
1653 * is not we can just drop the old head and let the existing refcount
1654 * be since all we did is relocate the values
1655 */
1656 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001657 if (skb_orphan_frags(skb, gfp_mask))
1658 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001659 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001660 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001661 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001662 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Eric Dumazet1fd63042010-09-02 23:09:32 +00001664 if (skb_has_frag_list(skb))
1665 skb_clone_fraglist(skb);
1666
1667 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001668 } else {
1669 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 off = (data + nhead) - skb->head;
1672
1673 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001674 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001676#ifdef NET_SKBUFF_DATA_USES_OFFSET
1677 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001678 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001679#else
1680 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001681#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001682 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001683 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001685 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 skb->nohdr = 0;
1687 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001688
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001689 skb_metadata_clear(skb);
1690
Eric Dumazet158f3232017-01-27 07:11:27 -08001691 /* It is not generally safe to change skb->truesize.
1692 * For the moment, we really care of rx path, or
1693 * when skb is orphaned (not attached to a socket).
1694 */
1695 if (!skb->sk || skb->destructor == sock_edemux)
1696 skb->truesize += size - osize;
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699
Shirley Maa6686f22011-07-06 12:22:12 +00001700nofrags:
1701 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702nodata:
1703 return -ENOMEM;
1704}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001705EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707/* Make private copy of skb with writable head and some headroom */
1708
1709struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1710{
1711 struct sk_buff *skb2;
1712 int delta = headroom - skb_headroom(skb);
1713
1714 if (delta <= 0)
1715 skb2 = pskb_copy(skb, GFP_ATOMIC);
1716 else {
1717 skb2 = skb_clone(skb, GFP_ATOMIC);
1718 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1719 GFP_ATOMIC)) {
1720 kfree_skb(skb2);
1721 skb2 = NULL;
1722 }
1723 }
1724 return skb2;
1725}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001726EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728/**
1729 * skb_copy_expand - copy and expand sk_buff
1730 * @skb: buffer to copy
1731 * @newheadroom: new free bytes at head
1732 * @newtailroom: new free bytes at tail
1733 * @gfp_mask: allocation priority
1734 *
1735 * Make a copy of both an &sk_buff and its data and while doing so
1736 * allocate additional space.
1737 *
1738 * This is used when the caller wishes to modify the data and needs a
1739 * private copy of the data to alter as well as more space for new fields.
1740 * Returns %NULL on failure or the pointer to the buffer
1741 * on success. The returned buffer has a reference count of 1.
1742 *
1743 * You must pass %GFP_ATOMIC as the allocation priority if this function
1744 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
1746struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001747 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001748 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 /*
1751 * Allocate the copy buffer
1752 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001753 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1754 gfp_mask, skb_alloc_rx_flag(skb),
1755 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001756 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int head_copy_len, head_copy_off;
1758
1759 if (!n)
1760 return NULL;
1761
1762 skb_reserve(n, newheadroom);
1763
1764 /* Set the tail pointer and length */
1765 skb_put(n, skb->len);
1766
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001767 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 head_copy_off = 0;
1769 if (newheadroom <= head_copy_len)
1770 head_copy_len = newheadroom;
1771 else
1772 head_copy_off = newheadroom - head_copy_len;
1773
1774 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001775 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1776 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001778 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Eric Dumazet030737b2013-10-19 11:42:54 -07001780 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return n;
1783}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001784EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001787 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * @skb: buffer to pad
1789 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001790 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *
1792 * Ensure that a buffer is followed by a padding area that is zero
1793 * filled. Used by network drivers which may DMA or transfer data
1794 * beyond the buffer end onto the wire.
1795 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001796 * May return error in out of memory cases. The skb is freed on error
1797 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001799
Florian Fainellicd0a1372017-08-22 15:12:14 -07001800int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Herbert Xu5b057c62006-06-23 02:06:41 -07001802 int err;
1803 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001806 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001810
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001811 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001812 if (likely(skb_cloned(skb) || ntail > 0)) {
1813 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1814 if (unlikely(err))
1815 goto free_skb;
1816 }
1817
1818 /* FIXME: The use of this function with non-linear skb's really needs
1819 * to be audited.
1820 */
1821 err = skb_linearize(skb);
1822 if (unlikely(err))
1823 goto free_skb;
1824
1825 memset(skb->data + skb->len, 0, pad);
1826 return 0;
1827
1828free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001829 if (free_on_error)
1830 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001831 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001832}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001833EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001834
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001835/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001836 * pskb_put - add data to the tail of a potentially fragmented buffer
1837 * @skb: start of the buffer to use
1838 * @tail: tail fragment of the buffer to use
1839 * @len: amount of data to add
1840 *
1841 * This function extends the used data area of the potentially
1842 * fragmented buffer. @tail must be the last fragment of @skb -- or
1843 * @skb itself. If this would exceed the total buffer size the kernel
1844 * will panic. A pointer to the first byte of the extra data is
1845 * returned.
1846 */
1847
Johannes Berg4df864c2017-06-16 14:29:21 +02001848void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001849{
1850 if (tail != skb) {
1851 skb->data_len += len;
1852 skb->len += len;
1853 }
1854 return skb_put(tail, len);
1855}
1856EXPORT_SYMBOL_GPL(pskb_put);
1857
1858/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001859 * skb_put - add data to a buffer
1860 * @skb: buffer to use
1861 * @len: amount of data to add
1862 *
1863 * This function extends the used data area of the buffer. If this would
1864 * exceed the total buffer size the kernel will panic. A pointer to the
1865 * first byte of the extra data is returned.
1866 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001867void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001868{
Johannes Berg4df864c2017-06-16 14:29:21 +02001869 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001870 SKB_LINEAR_ASSERT(skb);
1871 skb->tail += len;
1872 skb->len += len;
1873 if (unlikely(skb->tail > skb->end))
1874 skb_over_panic(skb, len, __builtin_return_address(0));
1875 return tmp;
1876}
1877EXPORT_SYMBOL(skb_put);
1878
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001879/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001880 * skb_push - add data to the start of a buffer
1881 * @skb: buffer to use
1882 * @len: amount of data to add
1883 *
1884 * This function extends the used data area of the buffer at the buffer
1885 * start. If this would exceed the total buffer headroom the kernel will
1886 * panic. A pointer to the first byte of the extra data is returned.
1887 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001888void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001889{
1890 skb->data -= len;
1891 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301892 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001893 skb_under_panic(skb, len, __builtin_return_address(0));
1894 return skb->data;
1895}
1896EXPORT_SYMBOL(skb_push);
1897
1898/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001899 * skb_pull - remove data from the start of a buffer
1900 * @skb: buffer to use
1901 * @len: amount of data to remove
1902 *
1903 * This function removes data from the start of a buffer, returning
1904 * the memory to the headroom. A pointer to the next data in the buffer
1905 * is returned. Once the data has been pulled future pushes will overwrite
1906 * the old data.
1907 */
Johannes Bergaf728682017-06-16 14:29:22 +02001908void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001909{
David S. Miller47d29642010-05-02 02:21:44 -07001910 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001911}
1912EXPORT_SYMBOL(skb_pull);
1913
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001914/**
1915 * skb_trim - remove end from a buffer
1916 * @skb: buffer to alter
1917 * @len: new length
1918 *
1919 * Cut the length of a buffer down by removing data from the tail. If
1920 * the buffer is already under the length specified it is not modified.
1921 * The skb must be linear.
1922 */
1923void skb_trim(struct sk_buff *skb, unsigned int len)
1924{
1925 if (skb->len > len)
1926 __skb_trim(skb, len);
1927}
1928EXPORT_SYMBOL(skb_trim);
1929
Herbert Xu3cc0e872006-06-09 16:13:38 -07001930/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
1932
Herbert Xu3cc0e872006-06-09 16:13:38 -07001933int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Herbert Xu27b437c2006-07-13 19:26:39 -07001935 struct sk_buff **fragp;
1936 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int offset = skb_headlen(skb);
1938 int nfrags = skb_shinfo(skb)->nr_frags;
1939 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001940 int err;
1941
1942 if (skb_cloned(skb) &&
1943 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001946 i = 0;
1947 if (offset >= len)
1948 goto drop_pages;
1949
1950 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001951 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001952
1953 if (end < len) {
1954 offset = end;
1955 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001957
Eric Dumazet9e903e02011-10-18 21:00:24 +00001958 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001959
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001960drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001961 skb_shinfo(skb)->nr_frags = i;
1962
1963 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001964 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001965
David S. Miller21dc3302010-08-23 00:13:46 -07001966 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001967 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001968 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
Herbert Xu27b437c2006-07-13 19:26:39 -07001971 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1972 fragp = &frag->next) {
1973 int end = offset + frag->len;
1974
1975 if (skb_shared(frag)) {
1976 struct sk_buff *nfrag;
1977
1978 nfrag = skb_clone(frag, GFP_ATOMIC);
1979 if (unlikely(!nfrag))
1980 return -ENOMEM;
1981
1982 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001983 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001984 frag = nfrag;
1985 *fragp = frag;
1986 }
1987
1988 if (end < len) {
1989 offset = end;
1990 continue;
1991 }
1992
1993 if (end > len &&
1994 unlikely((err = pskb_trim(frag, len - offset))))
1995 return err;
1996
1997 if (frag->next)
1998 skb_drop_list(&frag->next);
1999 break;
2000 }
2001
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002002done:
Herbert Xu27b437c2006-07-13 19:26:39 -07002003 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 skb->data_len -= skb->len - len;
2005 skb->len = len;
2006 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07002007 skb->len = len;
2008 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002009 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002012 if (!skb->sk || skb->destructor == sock_edemux)
2013 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002016EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Eric Dumazet88078d92018-04-18 11:43:15 -07002018/* Note : use pskb_trim_rcsum() instead of calling this directly
2019 */
2020int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2021{
2022 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2023 int delta = skb->len - len;
2024
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002025 skb->csum = csum_block_sub(skb->csum,
2026 skb_checksum(skb, len, delta, 0),
2027 len);
Vasily Averin43f6ea42020-12-14 22:07:39 +03002028 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2029 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2030 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2031
2032 if (offset + sizeof(__sum16) > hdlen)
2033 return -EINVAL;
Eric Dumazet88078d92018-04-18 11:43:15 -07002034 }
2035 return __pskb_trim(skb, len);
2036}
2037EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/**
2040 * __pskb_pull_tail - advance tail of skb header
2041 * @skb: buffer to reallocate
2042 * @delta: number of bytes to advance tail
2043 *
2044 * The function makes a sense only on a fragmented &sk_buff,
2045 * it expands header moving its tail forward and copying necessary
2046 * data from fragmented part.
2047 *
2048 * &sk_buff MUST have reference count of 1.
2049 *
2050 * Returns %NULL (and &sk_buff does not change) if pull failed
2051 * or value of new tail of skb in the case of success.
2052 *
2053 * All the pointers pointing into skb header may change and must be
2054 * reloaded after call to this function.
2055 */
2056
2057/* Moves tail of skb head forward, copying data from fragmented part,
2058 * when it is necessary.
2059 * 1. It may fail due to malloc failure.
2060 * 2. It may change skb pointers.
2061 *
2062 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2063 */
Johannes Bergaf728682017-06-16 14:29:22 +02002064void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 /* If skb has not enough free space at tail, get new one
2067 * plus 128 bytes for future expansions. If we have enough
2068 * room at tail, reallocate without expansion only if skb is cloned.
2069 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002070 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (eat > 0 || skb_cloned(skb)) {
2073 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2074 GFP_ATOMIC))
2075 return NULL;
2076 }
2077
Tim Hansen9f77fad2017-10-09 11:37:59 -04002078 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2079 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /* Optimization: no fragments, no reasons to preestimate
2082 * size of pulled pages. Superb.
2083 */
David S. Miller21dc3302010-08-23 00:13:46 -07002084 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto pull_pages;
2086
2087 /* Estimate size of pulled pages. */
2088 eat = delta;
2089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002090 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2091
2092 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002094 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
2097 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002098 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * but taking into account that pulling is expected to
2100 * be very rare operation, it is worth to fight against
2101 * further bloating skb head and crucify ourselves here instead.
2102 * Pure masohism, indeed. 8)8)
2103 */
2104 if (eat) {
2105 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2106 struct sk_buff *clone = NULL;
2107 struct sk_buff *insp = NULL;
2108
2109 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (list->len <= eat) {
2111 /* Eaten as whole. */
2112 eat -= list->len;
2113 list = list->next;
2114 insp = list;
2115 } else {
2116 /* Eaten partially. */
2117
2118 if (skb_shared(list)) {
2119 /* Sucks! We need to fork list. :-( */
2120 clone = skb_clone(list, GFP_ATOMIC);
2121 if (!clone)
2122 return NULL;
2123 insp = list->next;
2124 list = clone;
2125 } else {
2126 /* This may be pulled without
2127 * problems. */
2128 insp = list;
2129 }
2130 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002131 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return NULL;
2133 }
2134 break;
2135 }
2136 } while (eat);
2137
2138 /* Free pulled out fragments. */
2139 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2140 skb_shinfo(skb)->frag_list = list->next;
2141 kfree_skb(list);
2142 }
2143 /* And insert new clone at head. */
2144 if (clone) {
2145 clone->next = list;
2146 skb_shinfo(skb)->frag_list = clone;
2147 }
2148 }
2149 /* Success! Now we may commit changes to skb data. */
2150
2151pull_pages:
2152 eat = delta;
2153 k = 0;
2154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002155 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2156
2157 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002158 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002159 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 } else {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002161 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2162
2163 *frag = skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (eat) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002165 skb_frag_off_add(frag, eat);
2166 skb_frag_size_sub(frag, eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002167 if (!i)
2168 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 eat = 0;
2170 }
2171 k++;
2172 }
2173 }
2174 skb_shinfo(skb)->nr_frags = k;
2175
linzhang3ccc6c62017-07-17 17:25:02 +08002176end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 skb->tail += delta;
2178 skb->data_len -= delta;
2179
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002180 if (!skb->data_len)
2181 skb_zcopy_clear(skb, false);
2182
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002183 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002185EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Eric Dumazet22019b12011-07-29 18:37:31 +00002187/**
2188 * skb_copy_bits - copy bits from skb to kernel buffer
2189 * @skb: source skb
2190 * @offset: offset in source
2191 * @to: destination buffer
2192 * @len: number of bytes to copy
2193 *
2194 * Copy the specified number of bytes from the source skb to the
2195 * destination buffer.
2196 *
2197 * CAUTION ! :
2198 * If its prototype is ever changed,
2199 * check arch/{*}/net/{*}.S files,
2200 * since it is called from BPF assembly code.
2201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2203{
David S. Miller1a028e52007-04-27 15:21:23 -07002204 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002205 struct sk_buff *frag_iter;
2206 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (offset > (int)skb->len - len)
2209 goto fault;
2210
2211 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002212 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (copy > len)
2214 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002215 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if ((len -= copy) == 0)
2217 return 0;
2218 offset += copy;
2219 to += copy;
2220 }
2221
2222 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002223 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002224 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002226 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002227
Eric Dumazet51c56b02012-04-05 11:35:15 +02002228 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002230 u32 p_off, p_len, copied;
2231 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 u8 *vaddr;
2233
2234 if (copy > len)
2235 copy = len;
2236
Willem de Bruijnc613c202017-07-31 08:15:47 -04002237 skb_frag_foreach_page(f,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002238 skb_frag_off(f) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002239 copy, p, p_off, p_len, copied) {
2240 vaddr = kmap_atomic(p);
2241 memcpy(to + copied, vaddr + p_off, p_len);
2242 kunmap_atomic(vaddr);
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 if ((len -= copy) == 0)
2246 return 0;
2247 offset += copy;
2248 to += copy;
2249 }
David S. Miller1a028e52007-04-27 15:21:23 -07002250 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252
David S. Millerfbb398a2009-06-09 00:18:59 -07002253 skb_walk_frags(skb, frag_iter) {
2254 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
David S. Millerfbb398a2009-06-09 00:18:59 -07002256 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
David S. Millerfbb398a2009-06-09 00:18:59 -07002258 end = start + frag_iter->len;
2259 if ((copy = end - offset) > 0) {
2260 if (copy > len)
2261 copy = len;
2262 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2263 goto fault;
2264 if ((len -= copy) == 0)
2265 return 0;
2266 offset += copy;
2267 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002269 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Shirley Maa6686f22011-07-06 12:22:12 +00002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (!len)
2273 return 0;
2274
2275fault:
2276 return -EFAULT;
2277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002278EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Jens Axboe9c55e012007-11-06 23:30:13 -08002280/*
2281 * Callback from splice_to_pipe(), if we need to release some pages
2282 * at the end of the spd in case we error'ed out in filling the pipe.
2283 */
2284static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2285{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002286 put_page(spd->pages[i]);
2287}
Jens Axboe9c55e012007-11-06 23:30:13 -08002288
David S. Millera108d5f2012-04-23 23:06:11 -04002289static struct page *linear_to_page(struct page *page, unsigned int *len,
2290 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002291 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002292{
Eric Dumazet5640f762012-09-23 23:04:42 +00002293 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002294
Eric Dumazet5640f762012-09-23 23:04:42 +00002295 if (!sk_page_frag_refill(sk, pfrag))
2296 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002297
Eric Dumazet5640f762012-09-23 23:04:42 +00002298 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002299
Eric Dumazet5640f762012-09-23 23:04:42 +00002300 memcpy(page_address(pfrag->page) + pfrag->offset,
2301 page_address(page) + *offset, *len);
2302 *offset = pfrag->offset;
2303 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002304
Eric Dumazet5640f762012-09-23 23:04:42 +00002305 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002306}
2307
Eric Dumazet41c73a02012-04-22 12:26:16 +00002308static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2309 struct page *page,
2310 unsigned int offset)
2311{
2312 return spd->nr_pages &&
2313 spd->pages[spd->nr_pages - 1] == page &&
2314 (spd->partial[spd->nr_pages - 1].offset +
2315 spd->partial[spd->nr_pages - 1].len == offset);
2316}
2317
Jens Axboe9c55e012007-11-06 23:30:13 -08002318/*
2319 * Fill page/offset/length into spd, if it can hold more pages.
2320 */
David S. Millera108d5f2012-04-23 23:06:11 -04002321static bool spd_fill_page(struct splice_pipe_desc *spd,
2322 struct pipe_inode_info *pipe, struct page *page,
2323 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002324 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002325 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002326{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002327 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002328 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002329
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002330 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002331 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002332 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002333 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002334 }
2335 if (spd_can_coalesce(spd, page, offset)) {
2336 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002337 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002338 }
2339 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002340 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002341 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002342 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002343 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002344
David S. Millera108d5f2012-04-23 23:06:11 -04002345 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002346}
2347
David S. Millera108d5f2012-04-23 23:06:11 -04002348static bool __splice_segment(struct page *page, unsigned int poff,
2349 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002350 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002351 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002352 struct sock *sk,
2353 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002354{
2355 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002356 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002357
2358 /* skip this segment if already processed */
2359 if (*off >= plen) {
2360 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002361 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002362 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002363
Octavian Purdila2870c432008-07-15 00:49:11 -07002364 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002365 poff += *off;
2366 plen -= *off;
2367 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002368
Eric Dumazet18aafc62013-01-11 14:46:37 +00002369 do {
2370 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002371
Eric Dumazet18aafc62013-01-11 14:46:37 +00002372 if (spd_fill_page(spd, pipe, page, &flen, poff,
2373 linear, sk))
2374 return true;
2375 poff += flen;
2376 plen -= flen;
2377 *len -= flen;
2378 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002379
David S. Millera108d5f2012-04-23 23:06:11 -04002380 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002381}
2382
2383/*
David S. Millera108d5f2012-04-23 23:06:11 -04002384 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002385 * pipe is full or if we already spliced the requested length.
2386 */
David S. Millera108d5f2012-04-23 23:06:11 -04002387static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2388 unsigned int *offset, unsigned int *len,
2389 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002390{
2391 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002392 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002393
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002394 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002395 * If skb->head_frag is set, this 'linear' part is backed by a
2396 * fragment, and if the head is not shared with any clones then
2397 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002398 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002399 if (__splice_segment(virt_to_page(skb->data),
2400 (unsigned long) skb->data & (PAGE_SIZE - 1),
2401 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002402 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002403 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002404 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002405 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002406
2407 /*
2408 * then map the fragments
2409 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002410 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2411 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2412
Ian Campbellea2ab692011-08-22 23:44:58 +00002413 if (__splice_segment(skb_frag_page(f),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002414 skb_frag_off(f), skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002415 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002416 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002417 }
2418
Tom Herbertfa9835e2016-03-07 14:11:04 -08002419 skb_walk_frags(skb, iter) {
2420 if (*offset >= iter->len) {
2421 *offset -= iter->len;
2422 continue;
2423 }
2424 /* __skb_splice_bits() only fails if the output has no room
2425 * left, so no point in going over the frag_list for the error
2426 * case.
2427 */
2428 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2429 return true;
2430 }
2431
David S. Millera108d5f2012-04-23 23:06:11 -04002432 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002433}
2434
2435/*
2436 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002437 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002438 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002439int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002440 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002441 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002442{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002443 struct partial_page partial[MAX_SKB_FRAGS];
2444 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002445 struct splice_pipe_desc spd = {
2446 .pages = pages,
2447 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002448 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002449 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002450 .spd_release = sock_spd_release,
2451 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002452 int ret = 0;
2453
Tom Herbertfa9835e2016-03-07 14:11:04 -08002454 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002455
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002456 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002457 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002458
Jens Axboe35f3d142010-05-20 10:43:18 +02002459 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002460}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002461EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002462
Tom Herbert20bf50d2017-07-28 16:22:42 -07002463/* Send skb data on a socket. Socket must be locked. */
2464int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2465 int len)
2466{
2467 unsigned int orig_len = len;
2468 struct sk_buff *head = skb;
2469 unsigned short fragidx;
2470 int slen, ret;
2471
2472do_frag_list:
2473
2474 /* Deal with head data */
2475 while (offset < skb_headlen(skb) && len) {
2476 struct kvec kv;
2477 struct msghdr msg;
2478
2479 slen = min_t(int, len, skb_headlen(skb) - offset);
2480 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002481 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002482 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002483 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002484
2485 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2486 if (ret <= 0)
2487 goto error;
2488
2489 offset += ret;
2490 len -= ret;
2491 }
2492
2493 /* All the data was skb head? */
2494 if (!len)
2495 goto out;
2496
2497 /* Make offset relative to start of frags */
2498 offset -= skb_headlen(skb);
2499
2500 /* Find where we are in frag list */
2501 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2502 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2503
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002504 if (offset < skb_frag_size(frag))
Tom Herbert20bf50d2017-07-28 16:22:42 -07002505 break;
2506
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002507 offset -= skb_frag_size(frag);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002508 }
2509
2510 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2511 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2512
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002513 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002514
2515 while (slen) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002516 ret = kernel_sendpage_locked(sk, skb_frag_page(frag),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002517 skb_frag_off(frag) + offset,
Tom Herbert20bf50d2017-07-28 16:22:42 -07002518 slen, MSG_DONTWAIT);
2519 if (ret <= 0)
2520 goto error;
2521
2522 len -= ret;
2523 offset += ret;
2524 slen -= ret;
2525 }
2526
2527 offset = 0;
2528 }
2529
2530 if (len) {
2531 /* Process any frag lists */
2532
2533 if (skb == head) {
2534 if (skb_has_frag_list(skb)) {
2535 skb = skb_shinfo(skb)->frag_list;
2536 goto do_frag_list;
2537 }
2538 } else if (skb->next) {
2539 skb = skb->next;
2540 goto do_frag_list;
2541 }
2542 }
2543
2544out:
2545 return orig_len - len;
2546
2547error:
2548 return orig_len == len ? ret : orig_len - len;
2549}
2550EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2551
Herbert Xu357b40a2005-04-19 22:30:14 -07002552/**
2553 * skb_store_bits - store bits from kernel buffer to skb
2554 * @skb: destination buffer
2555 * @offset: offset in destination
2556 * @from: source buffer
2557 * @len: number of bytes to copy
2558 *
2559 * Copy the specified number of bytes from the source buffer to the
2560 * destination skb. This function handles all the messy bits of
2561 * traversing fragment lists and such.
2562 */
2563
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002564int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002565{
David S. Miller1a028e52007-04-27 15:21:23 -07002566 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002567 struct sk_buff *frag_iter;
2568 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002569
2570 if (offset > (int)skb->len - len)
2571 goto fault;
2572
David S. Miller1a028e52007-04-27 15:21:23 -07002573 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002574 if (copy > len)
2575 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002576 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002577 if ((len -= copy) == 0)
2578 return 0;
2579 offset += copy;
2580 from += copy;
2581 }
2582
2583 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2584 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002585 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002586
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002587 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002588
Eric Dumazet9e903e02011-10-18 21:00:24 +00002589 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002590 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002591 u32 p_off, p_len, copied;
2592 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002593 u8 *vaddr;
2594
2595 if (copy > len)
2596 copy = len;
2597
Willem de Bruijnc613c202017-07-31 08:15:47 -04002598 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002599 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002600 copy, p, p_off, p_len, copied) {
2601 vaddr = kmap_atomic(p);
2602 memcpy(vaddr + p_off, from + copied, p_len);
2603 kunmap_atomic(vaddr);
2604 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002605
2606 if ((len -= copy) == 0)
2607 return 0;
2608 offset += copy;
2609 from += copy;
2610 }
David S. Miller1a028e52007-04-27 15:21:23 -07002611 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002612 }
2613
David S. Millerfbb398a2009-06-09 00:18:59 -07002614 skb_walk_frags(skb, frag_iter) {
2615 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002616
David S. Millerfbb398a2009-06-09 00:18:59 -07002617 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002618
David S. Millerfbb398a2009-06-09 00:18:59 -07002619 end = start + frag_iter->len;
2620 if ((copy = end - offset) > 0) {
2621 if (copy > len)
2622 copy = len;
2623 if (skb_store_bits(frag_iter, offset - start,
2624 from, copy))
2625 goto fault;
2626 if ((len -= copy) == 0)
2627 return 0;
2628 offset += copy;
2629 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002630 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002631 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002632 }
2633 if (!len)
2634 return 0;
2635
2636fault:
2637 return -EFAULT;
2638}
Herbert Xu357b40a2005-04-19 22:30:14 -07002639EXPORT_SYMBOL(skb_store_bits);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002642__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2643 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
David S. Miller1a028e52007-04-27 15:21:23 -07002645 int start = skb_headlen(skb);
2646 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002647 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 int pos = 0;
2649
2650 /* Checksum header. */
2651 if (copy > 0) {
2652 if (copy > len)
2653 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002654 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2655 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if ((len -= copy) == 0)
2657 return csum;
2658 offset += copy;
2659 pos = copy;
2660 }
2661
2662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002663 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002664 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002666 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002667
Eric Dumazet51c56b02012-04-05 11:35:15 +02002668 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002670 u32 p_off, p_len, copied;
2671 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002672 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 if (copy > len)
2676 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002677
2678 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002679 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002680 copy, p, p_off, p_len, copied) {
2681 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002682 csum2 = INDIRECT_CALL_1(ops->update,
2683 csum_partial_ext,
2684 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002685 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002686 csum = INDIRECT_CALL_1(ops->combine,
2687 csum_block_add_ext, csum,
2688 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002689 pos += p_len;
2690 }
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (!(len -= copy))
2693 return csum;
2694 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
David S. Miller1a028e52007-04-27 15:21:23 -07002696 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
David S. Millerfbb398a2009-06-09 00:18:59 -07002699 skb_walk_frags(skb, frag_iter) {
2700 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
David S. Millerfbb398a2009-06-09 00:18:59 -07002702 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
David S. Millerfbb398a2009-06-09 00:18:59 -07002704 end = start + frag_iter->len;
2705 if ((copy = end - offset) > 0) {
2706 __wsum csum2;
2707 if (copy > len)
2708 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002709 csum2 = __skb_checksum(frag_iter, offset - start,
2710 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002711 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2712 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002713 if ((len -= copy) == 0)
2714 return csum;
2715 offset += copy;
2716 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002718 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002720 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 return csum;
2723}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002724EXPORT_SYMBOL(__skb_checksum);
2725
2726__wsum skb_checksum(const struct sk_buff *skb, int offset,
2727 int len, __wsum csum)
2728{
2729 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002730 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002731 .combine = csum_block_add_ext,
2732 };
2733
2734 return __skb_checksum(skb, offset, len, csum, &ops);
2735}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002736EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738/* Both of above in one bottle. */
2739
Al Viro81d77662006-11-14 21:37:33 -08002740__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
Al Viro8d5930d2020-07-10 20:07:10 -04002741 u8 *to, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
David S. Miller1a028e52007-04-27 15:21:23 -07002743 int start = skb_headlen(skb);
2744 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002745 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 int pos = 0;
Al Viro8d5930d2020-07-10 20:07:10 -04002747 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 /* Copy header. */
2750 if (copy > 0) {
2751 if (copy > len)
2752 copy = len;
2753 csum = csum_partial_copy_nocheck(skb->data + offset, to,
Al Virocc44c172020-07-11 00:12:07 -04002754 copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if ((len -= copy) == 0)
2756 return csum;
2757 offset += copy;
2758 to += copy;
2759 pos = copy;
2760 }
2761
2762 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002763 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002765 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002766
Eric Dumazet9e903e02011-10-18 21:00:24 +00002767 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002769 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2770 u32 p_off, p_len, copied;
2771 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002772 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 if (copy > len)
2776 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002777
2778 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002779 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002780 copy, p, p_off, p_len, copied) {
2781 vaddr = kmap_atomic(p);
2782 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2783 to + copied,
Al Virocc44c172020-07-11 00:12:07 -04002784 p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002785 kunmap_atomic(vaddr);
2786 csum = csum_block_add(csum, csum2, pos);
2787 pos += p_len;
2788 }
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (!(len -= copy))
2791 return csum;
2792 offset += copy;
2793 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
David S. Miller1a028e52007-04-27 15:21:23 -07002795 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797
David S. Millerfbb398a2009-06-09 00:18:59 -07002798 skb_walk_frags(skb, frag_iter) {
2799 __wsum csum2;
2800 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
David S. Millerfbb398a2009-06-09 00:18:59 -07002802 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
David S. Millerfbb398a2009-06-09 00:18:59 -07002804 end = start + frag_iter->len;
2805 if ((copy = end - offset) > 0) {
2806 if (copy > len)
2807 copy = len;
2808 csum2 = skb_copy_and_csum_bits(frag_iter,
2809 offset - start,
Al Viro8d5930d2020-07-10 20:07:10 -04002810 to, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002811 csum = csum_block_add(csum, csum2, pos);
2812 if ((len -= copy) == 0)
2813 return csum;
2814 offset += copy;
2815 to += copy;
2816 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002818 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002820 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return csum;
2822}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002823EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Cong Wang49f8e832018-11-08 14:05:42 -08002825__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2826{
2827 __sum16 sum;
2828
2829 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002830 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002831 if (likely(!sum)) {
2832 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2833 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002834 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002835 }
2836 if (!skb_shared(skb))
2837 skb->csum_valid = !sum;
2838 return sum;
2839}
2840EXPORT_SYMBOL(__skb_checksum_complete_head);
2841
Cong Wang14641932018-11-26 09:31:26 -08002842/* This function assumes skb->csum already holds pseudo header's checksum,
2843 * which has been changed from the hardware checksum, for example, by
2844 * __skb_checksum_validate_complete(). And, the original skb->csum must
2845 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2846 *
2847 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2848 * zero. The new checksum is stored back into skb->csum unless the skb is
2849 * shared.
2850 */
Cong Wang49f8e832018-11-08 14:05:42 -08002851__sum16 __skb_checksum_complete(struct sk_buff *skb)
2852{
2853 __wsum csum;
2854 __sum16 sum;
2855
2856 csum = skb_checksum(skb, 0, skb->len, 0);
2857
Cong Wang49f8e832018-11-08 14:05:42 -08002858 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002859 /* This check is inverted, because we already knew the hardware
2860 * checksum is invalid before calling this function. So, if the
2861 * re-computed checksum is valid instead, then we have a mismatch
2862 * between the original skb->csum and skb_checksum(). This means either
2863 * the original hardware checksum is incorrect or we screw up skb->csum
2864 * when moving skb->data around.
2865 */
Cong Wang49f8e832018-11-08 14:05:42 -08002866 if (likely(!sum)) {
2867 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2868 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002869 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002870 }
2871
2872 if (!skb_shared(skb)) {
2873 /* Save full packet checksum */
2874 skb->csum = csum;
2875 skb->ip_summed = CHECKSUM_COMPLETE;
2876 skb->csum_complete_sw = 1;
2877 skb->csum_valid = !sum;
2878 }
2879
2880 return sum;
2881}
2882EXPORT_SYMBOL(__skb_checksum_complete);
2883
Davide Caratti96178132017-05-18 15:44:37 +02002884static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2885{
2886 net_warn_ratelimited(
2887 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2888 __func__);
2889 return 0;
2890}
2891
2892static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2893 int offset, int len)
2894{
2895 net_warn_ratelimited(
2896 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2897 __func__);
2898 return 0;
2899}
2900
2901static const struct skb_checksum_ops default_crc32c_ops = {
2902 .update = warn_crc32c_csum_update,
2903 .combine = warn_crc32c_csum_combine,
2904};
2905
2906const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2907 &default_crc32c_ops;
2908EXPORT_SYMBOL(crc32c_csum_stub);
2909
Thomas Grafaf2806f2013-12-13 15:22:17 +01002910 /**
2911 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2912 * @from: source buffer
2913 *
2914 * Calculates the amount of linear headroom needed in the 'to' skb passed
2915 * into skb_zerocopy().
2916 */
2917unsigned int
2918skb_zerocopy_headlen(const struct sk_buff *from)
2919{
2920 unsigned int hlen = 0;
2921
2922 if (!from->head_frag ||
2923 skb_headlen(from) < L1_CACHE_BYTES ||
2924 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2925 hlen = skb_headlen(from);
2926
2927 if (skb_has_frag_list(from))
2928 hlen = from->len;
2929
2930 return hlen;
2931}
2932EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2933
2934/**
2935 * skb_zerocopy - Zero copy skb to skb
2936 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002937 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002938 * @len: number of bytes to copy from source buffer
2939 * @hlen: size of linear headroom in destination buffer
2940 *
2941 * Copies up to `len` bytes from `from` to `to` by creating references
2942 * to the frags in the source buffer.
2943 *
2944 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2945 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002946 *
2947 * Return value:
2948 * 0: everything is OK
2949 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2950 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002951 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002952int
2953skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002954{
2955 int i, j = 0;
2956 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002957 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002958 struct page *page;
2959 unsigned int offset;
2960
2961 BUG_ON(!from->head_frag && !hlen);
2962
2963 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002964 if (len <= skb_tailroom(to))
2965 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002966
2967 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002968 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2969 if (unlikely(ret))
2970 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002971 len -= hlen;
2972 } else {
2973 plen = min_t(int, skb_headlen(from), len);
2974 if (plen) {
2975 page = virt_to_head_page(from->head);
2976 offset = from->data - (unsigned char *)page_address(page);
2977 __skb_fill_page_desc(to, 0, page, offset, plen);
2978 get_page(page);
2979 j = 1;
2980 len -= plen;
2981 }
2982 }
2983
2984 to->truesize += len + plen;
2985 to->len += len + plen;
2986 to->data_len += len + plen;
2987
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002988 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2989 skb_tx_error(from);
2990 return -ENOMEM;
2991 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002992 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002993
Thomas Grafaf2806f2013-12-13 15:22:17 +01002994 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002995 int size;
2996
Thomas Grafaf2806f2013-12-13 15:22:17 +01002997 if (!len)
2998 break;
2999 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003000 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3001 len);
3002 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3003 len -= size;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003004 skb_frag_ref(to, j);
3005 j++;
3006 }
3007 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003008
3009 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003010}
3011EXPORT_SYMBOL_GPL(skb_zerocopy);
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3014{
Al Virod3bc23e2006-11-14 21:24:49 -08003015 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 long csstart;
3017
Patrick McHardy84fa7932006-08-29 16:44:56 -07003018 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00003019 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 else
3021 csstart = skb_headlen(skb);
3022
Kris Katterjohn09a62662006-01-08 22:24:28 -08003023 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003025 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 csum = 0;
3028 if (csstart != skb->len)
3029 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
Al Viro8d5930d2020-07-10 20:07:10 -04003030 skb->len - csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Patrick McHardy84fa7932006-08-29 16:44:56 -07003032 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003033 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Al Virod3bc23e2006-11-14 21:24:49 -08003035 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003038EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040/**
3041 * skb_dequeue - remove from the head of the queue
3042 * @list: list to dequeue from
3043 *
3044 * Remove the head of the list. The list lock is taken so the function
3045 * may be used safely with other locking list functions. The head item is
3046 * returned or %NULL if the list is empty.
3047 */
3048
3049struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3050{
3051 unsigned long flags;
3052 struct sk_buff *result;
3053
3054 spin_lock_irqsave(&list->lock, flags);
3055 result = __skb_dequeue(list);
3056 spin_unlock_irqrestore(&list->lock, flags);
3057 return result;
3058}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003059EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061/**
3062 * skb_dequeue_tail - remove from the tail of the queue
3063 * @list: list to dequeue from
3064 *
3065 * Remove the tail of the list. The list lock is taken so the function
3066 * may be used safely with other locking list functions. The tail item is
3067 * returned or %NULL if the list is empty.
3068 */
3069struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3070{
3071 unsigned long flags;
3072 struct sk_buff *result;
3073
3074 spin_lock_irqsave(&list->lock, flags);
3075 result = __skb_dequeue_tail(list);
3076 spin_unlock_irqrestore(&list->lock, flags);
3077 return result;
3078}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003079EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081/**
3082 * skb_queue_purge - empty a list
3083 * @list: list to empty
3084 *
3085 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3086 * the list and one reference dropped. This function takes the list
3087 * lock and is atomic with respect to other list locking functions.
3088 */
3089void skb_queue_purge(struct sk_buff_head *list)
3090{
3091 struct sk_buff *skb;
3092 while ((skb = skb_dequeue(list)) != NULL)
3093 kfree_skb(skb);
3094}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003095EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003098 * skb_rbtree_purge - empty a skb rbtree
3099 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003100 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003101 *
3102 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3103 * the list and one reference dropped. This function does not take
3104 * any lock. Synchronization should be handled by the caller (e.g., TCP
3105 * out-of-order queue is protected by the socket lock).
3106 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003107unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003108{
Eric Dumazet7c905842017-09-23 12:39:12 -07003109 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003110 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003111
Eric Dumazet7c905842017-09-23 12:39:12 -07003112 while (p) {
3113 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3114
3115 p = rb_next(p);
3116 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003117 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003118 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003119 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003120 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003121}
3122
3123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 * skb_queue_head - queue a buffer at the list head
3125 * @list: list to use
3126 * @newsk: buffer to queue
3127 *
3128 * Queue a buffer at the start of the list. This function takes the
3129 * list lock and can be used safely with other locking &sk_buff functions
3130 * safely.
3131 *
3132 * A buffer cannot be placed on two lists at the same time.
3133 */
3134void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3135{
3136 unsigned long flags;
3137
3138 spin_lock_irqsave(&list->lock, flags);
3139 __skb_queue_head(list, newsk);
3140 spin_unlock_irqrestore(&list->lock, flags);
3141}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003142EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144/**
3145 * skb_queue_tail - queue a buffer at the list tail
3146 * @list: list to use
3147 * @newsk: buffer to queue
3148 *
3149 * Queue a buffer at the tail of the list. This function takes the
3150 * list lock and can be used safely with other locking &sk_buff functions
3151 * safely.
3152 *
3153 * A buffer cannot be placed on two lists at the same time.
3154 */
3155void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3156{
3157 unsigned long flags;
3158
3159 spin_lock_irqsave(&list->lock, flags);
3160 __skb_queue_tail(list, newsk);
3161 spin_unlock_irqrestore(&list->lock, flags);
3162}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003163EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/**
3166 * skb_unlink - remove a buffer from a list
3167 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003168 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 *
David S. Miller8728b832005-08-09 19:25:21 -07003170 * Remove a packet from a list. The list locks are taken and this
3171 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 *
David S. Miller8728b832005-08-09 19:25:21 -07003173 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 */
David S. Miller8728b832005-08-09 19:25:21 -07003175void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
David S. Miller8728b832005-08-09 19:25:21 -07003177 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
David S. Miller8728b832005-08-09 19:25:21 -07003179 spin_lock_irqsave(&list->lock, flags);
3180 __skb_unlink(skb, list);
3181 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003183EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/**
3186 * skb_append - append a buffer
3187 * @old: buffer to insert after
3188 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003189 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 *
3191 * Place a packet after a given packet in a list. The list locks are taken
3192 * and this function is atomic with respect to other list locked calls.
3193 * A buffer cannot be placed on two lists at the same time.
3194 */
David S. Miller8728b832005-08-09 19:25:21 -07003195void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 unsigned long flags;
3198
David S. Miller8728b832005-08-09 19:25:21 -07003199 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003200 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003201 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003203EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205static inline void skb_split_inside_header(struct sk_buff *skb,
3206 struct sk_buff* skb1,
3207 const u32 len, const int pos)
3208{
3209 int i;
3210
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003211 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3212 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 /* And move data appendix as is. */
3214 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3215 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3216
3217 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3218 skb_shinfo(skb)->nr_frags = 0;
3219 skb1->data_len = skb->data_len;
3220 skb1->len += skb1->data_len;
3221 skb->data_len = 0;
3222 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003223 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
3226static inline void skb_split_no_header(struct sk_buff *skb,
3227 struct sk_buff* skb1,
3228 const u32 len, int pos)
3229{
3230 int i, k = 0;
3231 const int nfrags = skb_shinfo(skb)->nr_frags;
3232
3233 skb_shinfo(skb)->nr_frags = 0;
3234 skb1->len = skb1->data_len = skb->len - len;
3235 skb->len = len;
3236 skb->data_len = len - pos;
3237
3238 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003239 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241 if (pos + size > len) {
3242 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3243
3244 if (pos < len) {
3245 /* Split frag.
3246 * We have two variants in this case:
3247 * 1. Move all the frag to the second
3248 * part, if it is possible. F.e.
3249 * this approach is mandatory for TUX,
3250 * where splitting is expensive.
3251 * 2. Split is accurately. We make this.
3252 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003253 skb_frag_ref(skb, i);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003254 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003255 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3256 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 skb_shinfo(skb)->nr_frags++;
3258 }
3259 k++;
3260 } else
3261 skb_shinfo(skb)->nr_frags++;
3262 pos += size;
3263 }
3264 skb_shinfo(skb1)->nr_frags = k;
3265}
3266
3267/**
3268 * skb_split - Split fragmented skb to two parts at length len.
3269 * @skb: the buffer to split
3270 * @skb1: the buffer to receive the second part
3271 * @len: new length for skb
3272 */
3273void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3274{
3275 int pos = skb_headlen(skb);
3276
Willem de Bruijnfff88032017-06-08 11:35:03 -04003277 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3278 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003279 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (len < pos) /* Split line is inside header. */
3281 skb_split_inside_header(skb, skb1, len, pos);
3282 else /* Second chunk has no header, nothing to copy. */
3283 skb_split_no_header(skb, skb1, len, pos);
3284}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003285EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003287/* Shifting from/to a cloned skb is a no-go.
3288 *
3289 * Caller cannot keep skb_shinfo related pointers past calling here!
3290 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003291static int skb_prepare_for_shift(struct sk_buff *skb)
3292{
Marco Elver97ff09a2021-02-01 17:04:20 +01003293 int ret = 0;
3294
3295 if (skb_cloned(skb)) {
3296 /* Save and restore truesize: pskb_expand_head() may reallocate
3297 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3298 * cannot change truesize at this point.
3299 */
3300 unsigned int save_truesize = skb->truesize;
3301
3302 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3303 skb->truesize = save_truesize;
3304 }
3305 return ret;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003306}
3307
3308/**
3309 * skb_shift - Shifts paged data partially from skb to another
3310 * @tgt: buffer into which tail data gets added
3311 * @skb: buffer from which the paged data comes from
3312 * @shiftlen: shift up to this many bytes
3313 *
3314 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003315 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003316 * It's up to caller to free skb if everything was shifted.
3317 *
3318 * If @tgt runs out of frags, the whole operation is aborted.
3319 *
3320 * Skb cannot include anything else but paged data while tgt is allowed
3321 * to have non-paged data as well.
3322 *
3323 * TODO: full sized shift could be optimized but that would need
3324 * specialized skb free'er to handle frags without up-to-date nr_frags.
3325 */
3326int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3327{
3328 int from, to, merge, todo;
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003329 skb_frag_t *fragfrom, *fragto;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003330
3331 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003332
3333 if (skb_headlen(skb))
3334 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003335 if (skb_zcopy(tgt) || skb_zcopy(skb))
3336 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003337
3338 todo = shiftlen;
3339 from = 0;
3340 to = skb_shinfo(tgt)->nr_frags;
3341 fragfrom = &skb_shinfo(skb)->frags[from];
3342
3343 /* Actual merge is delayed until the point when we know we can
3344 * commit all, so that we don't have to undo partial changes
3345 */
3346 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003347 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003348 skb_frag_off(fragfrom))) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003349 merge = -1;
3350 } else {
3351 merge = to - 1;
3352
Eric Dumazet9e903e02011-10-18 21:00:24 +00003353 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003354 if (todo < 0) {
3355 if (skb_prepare_for_shift(skb) ||
3356 skb_prepare_for_shift(tgt))
3357 return 0;
3358
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003359 /* All previous frag pointers might be stale! */
3360 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003361 fragto = &skb_shinfo(tgt)->frags[merge];
3362
Eric Dumazet9e903e02011-10-18 21:00:24 +00003363 skb_frag_size_add(fragto, shiftlen);
3364 skb_frag_size_sub(fragfrom, shiftlen);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003365 skb_frag_off_add(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003366
3367 goto onlymerged;
3368 }
3369
3370 from++;
3371 }
3372
3373 /* Skip full, not-fitting skb to avoid expensive operations */
3374 if ((shiftlen == skb->len) &&
3375 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3376 return 0;
3377
3378 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3379 return 0;
3380
3381 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3382 if (to == MAX_SKB_FRAGS)
3383 return 0;
3384
3385 fragfrom = &skb_shinfo(skb)->frags[from];
3386 fragto = &skb_shinfo(tgt)->frags[to];
3387
Eric Dumazet9e903e02011-10-18 21:00:24 +00003388 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003389 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003390 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003391 from++;
3392 to++;
3393
3394 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003395 __skb_frag_ref(fragfrom);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003396 skb_frag_page_copy(fragto, fragfrom);
3397 skb_frag_off_copy(fragto, fragfrom);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003398 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003399
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003400 skb_frag_off_add(fragfrom, todo);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003401 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003402 todo = 0;
3403
3404 to++;
3405 break;
3406 }
3407 }
3408
3409 /* Ready to "commit" this state change to tgt */
3410 skb_shinfo(tgt)->nr_frags = to;
3411
3412 if (merge >= 0) {
3413 fragfrom = &skb_shinfo(skb)->frags[0];
3414 fragto = &skb_shinfo(tgt)->frags[merge];
3415
Eric Dumazet9e903e02011-10-18 21:00:24 +00003416 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003417 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003418 }
3419
3420 /* Reposition in the original skb */
3421 to = 0;
3422 while (from < skb_shinfo(skb)->nr_frags)
3423 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3424 skb_shinfo(skb)->nr_frags = to;
3425
3426 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3427
3428onlymerged:
3429 /* Most likely the tgt won't ever need its checksum anymore, skb on
3430 * the other hand might need it if it needs to be resent
3431 */
3432 tgt->ip_summed = CHECKSUM_PARTIAL;
3433 skb->ip_summed = CHECKSUM_PARTIAL;
3434
3435 /* Yak, is it really working this way? Some helper please? */
3436 skb->len -= shiftlen;
3437 skb->data_len -= shiftlen;
3438 skb->truesize -= shiftlen;
3439 tgt->len += shiftlen;
3440 tgt->data_len += shiftlen;
3441 tgt->truesize += shiftlen;
3442
3443 return shiftlen;
3444}
3445
Thomas Graf677e90e2005-06-23 20:59:51 -07003446/**
3447 * skb_prepare_seq_read - Prepare a sequential read of skb data
3448 * @skb: the buffer to read
3449 * @from: lower offset of data to be read
3450 * @to: upper offset of data to be read
3451 * @st: state variable
3452 *
3453 * Initializes the specified state variable. Must be called before
3454 * invoking skb_seq_read() for the first time.
3455 */
3456void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3457 unsigned int to, struct skb_seq_state *st)
3458{
3459 st->lower_offset = from;
3460 st->upper_offset = to;
3461 st->root_skb = st->cur_skb = skb;
3462 st->frag_idx = st->stepped_offset = 0;
3463 st->frag_data = NULL;
3464}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003465EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003466
3467/**
3468 * skb_seq_read - Sequentially read skb data
3469 * @consumed: number of bytes consumed by the caller so far
3470 * @data: destination pointer for data to be returned
3471 * @st: state variable
3472 *
Mathias Krausebc323832013-11-07 14:18:26 +01003473 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003474 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003475 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003476 * of the block or 0 if the end of the skb data or the upper
3477 * offset has been reached.
3478 *
3479 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003480 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003481 * of bytes already consumed and the next call to
3482 * skb_seq_read() will return the remaining part of the block.
3483 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003484 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003485 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003486 * reads of potentially non linear data.
3487 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003488 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003489 * at the moment, state->root_skb could be replaced with
3490 * a stack for this purpose.
3491 */
3492unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3493 struct skb_seq_state *st)
3494{
3495 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3496 skb_frag_t *frag;
3497
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003498 if (unlikely(abs_offset >= st->upper_offset)) {
3499 if (st->frag_data) {
3500 kunmap_atomic(st->frag_data);
3501 st->frag_data = NULL;
3502 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003503 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003504 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003505
3506next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003507 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003508
Thomas Chenault995b3372009-05-18 21:43:27 -07003509 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003510 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003511 return block_limit - abs_offset;
3512 }
3513
3514 if (st->frag_idx == 0 && !st->frag_data)
3515 st->stepped_offset += skb_headlen(st->cur_skb);
3516
3517 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3518 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003519 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003520
3521 if (abs_offset < block_limit) {
3522 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003523 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003524
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003525 *data = (u8 *) st->frag_data + skb_frag_off(frag) +
Thomas Graf677e90e2005-06-23 20:59:51 -07003526 (abs_offset - st->stepped_offset);
3527
3528 return block_limit - abs_offset;
3529 }
3530
3531 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003532 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003533 st->frag_data = NULL;
3534 }
3535
3536 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003537 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003538 }
3539
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003540 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003541 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003542 st->frag_data = NULL;
3543 }
3544
David S. Miller21dc3302010-08-23 00:13:46 -07003545 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003546 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003547 st->frag_idx = 0;
3548 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003549 } else if (st->cur_skb->next) {
3550 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003551 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003552 goto next_skb;
3553 }
3554
3555 return 0;
3556}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003557EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003558
3559/**
3560 * skb_abort_seq_read - Abort a sequential read of skb data
3561 * @st: state variable
3562 *
3563 * Must be called if skb_seq_read() was not called until it
3564 * returned 0.
3565 */
3566void skb_abort_seq_read(struct skb_seq_state *st)
3567{
3568 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003569 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003570}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003571EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003572
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003573#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3574
3575static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3576 struct ts_config *conf,
3577 struct ts_state *state)
3578{
3579 return skb_seq_read(offset, text, TS_SKB_CB(state));
3580}
3581
3582static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3583{
3584 skb_abort_seq_read(TS_SKB_CB(state));
3585}
3586
3587/**
3588 * skb_find_text - Find a text pattern in skb data
3589 * @skb: the buffer to look in
3590 * @from: search offset
3591 * @to: search limit
3592 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003593 *
3594 * Finds a pattern in the skb data according to the specified
3595 * textsearch configuration. Use textsearch_next() to retrieve
3596 * subsequent occurrences of the pattern. Returns the offset
3597 * to the first occurrence or UINT_MAX if no match was found.
3598 */
3599unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003600 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003601{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003602 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003603 unsigned int ret;
3604
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003605 config->get_next_block = skb_ts_get_next_block;
3606 config->finish = skb_ts_finish;
3607
Bojan Prtvar059a2442015-02-22 11:46:35 +01003608 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003609
Bojan Prtvar059a2442015-02-22 11:46:35 +01003610 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003611 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003612}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003613EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003614
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003615int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3616 int offset, size_t size)
3617{
3618 int i = skb_shinfo(skb)->nr_frags;
3619
3620 if (skb_can_coalesce(skb, i, page, offset)) {
3621 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3622 } else if (i < MAX_SKB_FRAGS) {
3623 get_page(page);
3624 skb_fill_page_desc(skb, i, page, offset, size);
3625 } else {
3626 return -EMSGSIZE;
3627 }
3628
3629 return 0;
3630}
3631EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3632
Herbert Xucbb042f2006-03-20 22:43:56 -08003633/**
3634 * skb_pull_rcsum - pull skb and update receive checksum
3635 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003636 * @len: length of data pulled
3637 *
3638 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003639 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003640 * receive path processing instead of skb_pull unless you know
3641 * that the checksum difference is zero (e.g., a valid IP header)
3642 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003643 */
Johannes Bergaf728682017-06-16 14:29:22 +02003644void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003645{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003646 unsigned char *data = skb->data;
3647
Herbert Xucbb042f2006-03-20 22:43:56 -08003648 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003649 __skb_pull(skb, len);
3650 skb_postpull_rcsum(skb, data, len);
3651 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003652}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003653EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3654
Yonghong Song13acc942018-03-21 16:31:03 -07003655static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3656{
3657 skb_frag_t head_frag;
3658 struct page *page;
3659
3660 page = virt_to_head_page(frag_skb->head);
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003661 __skb_frag_set_page(&head_frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003662 skb_frag_off_set(&head_frag, frag_skb->data -
3663 (unsigned char *)page_address(page));
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003664 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
Yonghong Song13acc942018-03-21 16:31:03 -07003665 return head_frag;
3666}
3667
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003668struct sk_buff *skb_segment_list(struct sk_buff *skb,
3669 netdev_features_t features,
3670 unsigned int offset)
3671{
3672 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3673 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3674 unsigned int delta_truesize = 0;
3675 unsigned int delta_len = 0;
3676 struct sk_buff *tail = NULL;
Dongseok Yi24cd3312021-01-08 11:28:38 +09003677 struct sk_buff *nskb, *tmp;
3678 int err;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003679
3680 skb_push(skb, -skb_network_offset(skb) + offset);
3681
3682 skb_shinfo(skb)->frag_list = NULL;
3683
3684 do {
3685 nskb = list_skb;
3686 list_skb = list_skb->next;
3687
Dongseok Yi24cd3312021-01-08 11:28:38 +09003688 err = 0;
3689 if (skb_shared(nskb)) {
3690 tmp = skb_clone(nskb, GFP_ATOMIC);
3691 if (tmp) {
3692 consume_skb(nskb);
3693 nskb = tmp;
3694 err = skb_unclone(nskb, GFP_ATOMIC);
3695 } else {
3696 err = -ENOMEM;
3697 }
3698 }
3699
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003700 if (!tail)
3701 skb->next = nskb;
3702 else
3703 tail->next = nskb;
3704
Dongseok Yi24cd3312021-01-08 11:28:38 +09003705 if (unlikely(err)) {
3706 nskb->next = list_skb;
3707 goto err_linearize;
3708 }
3709
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003710 tail = nskb;
3711
3712 delta_len += nskb->len;
3713 delta_truesize += nskb->truesize;
3714
3715 skb_push(nskb, -skb_network_offset(nskb) + offset);
3716
Florian Westphalcf673ed2020-03-30 18:51:29 +02003717 skb_release_head_state(nskb);
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003718 __copy_skb_header(nskb, skb);
3719
3720 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3721 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3722 nskb->data - tnl_hlen,
3723 offset + tnl_hlen);
3724
3725 if (skb_needs_linearize(nskb, features) &&
3726 __skb_linearize(nskb))
3727 goto err_linearize;
3728
3729 } while (list_skb);
3730
3731 skb->truesize = skb->truesize - delta_truesize;
3732 skb->data_len = skb->data_len - delta_len;
3733 skb->len = skb->len - delta_len;
3734
3735 skb_gso_reset(skb);
3736
3737 skb->prev = tail;
3738
3739 if (skb_needs_linearize(skb, features) &&
3740 __skb_linearize(skb))
3741 goto err_linearize;
3742
3743 skb_get(skb);
3744
3745 return skb;
3746
3747err_linearize:
3748 kfree_skb_list(skb->next);
3749 skb->next = NULL;
3750 return ERR_PTR(-ENOMEM);
3751}
3752EXPORT_SYMBOL_GPL(skb_segment_list);
3753
3754int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3755{
3756 if (unlikely(p->len + skb->len >= 65536))
3757 return -E2BIG;
3758
3759 if (NAPI_GRO_CB(p)->last == p)
3760 skb_shinfo(p)->frag_list = skb;
3761 else
3762 NAPI_GRO_CB(p)->last->next = skb;
3763
3764 skb_pull(skb, skb_gro_offset(skb));
3765
3766 NAPI_GRO_CB(p)->last = skb;
3767 NAPI_GRO_CB(p)->count++;
3768 p->data_len += skb->len;
3769 p->truesize += skb->truesize;
3770 p->len += skb->len;
3771
3772 NAPI_GRO_CB(skb)->same_flow = 1;
3773
3774 return 0;
3775}
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003776
Herbert Xuf4c50d92006-06-22 03:02:40 -07003777/**
3778 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003779 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003780 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003781 *
3782 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003783 * a pointer to the first in a list of new skbs for the segments.
3784 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003785 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003786struct sk_buff *skb_segment(struct sk_buff *head_skb,
3787 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003788{
3789 struct sk_buff *segs = NULL;
3790 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003791 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003792 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3793 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3794 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003795 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003796 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003797 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003798 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003799 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003800 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003801 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003802 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003803 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003804 int err = -ENOMEM;
3805 int i = 0;
3806 int pos;
3807
Shmulik Ladkani3dcbdb12019-09-06 12:23:50 +03003808 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3809 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3810 /* gso_size is untrusted, and we have a frag_list with a linear
3811 * non head_frag head.
3812 *
3813 * (we assume checking the first list_skb member suffices;
3814 * i.e if either of the list_skb members have non head_frag
3815 * head, then the first one has too).
3816 *
3817 * If head_skb's headlen does not fit requested gso_size, it
3818 * means that the frag_list members do NOT terminate on exact
3819 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3820 * sharing. Therefore we must fallback to copying the frag_list
3821 * skbs; we do so by disabling SG.
3822 */
3823 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3824 features &= ~NETIF_F_SG;
3825 }
3826
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003827 __skb_push(head_skb, doffset);
Miaohe Lin2f631132020-08-01 17:36:05 +08003828 proto = skb_network_protocol(head_skb, NULL);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003829 if (unlikely(!proto))
3830 return ERR_PTR(-EINVAL);
3831
Alexander Duyck36c98382016-05-02 09:38:18 -07003832 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003833 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003834
Steffen Klassert07b26c92016-09-19 12:58:47 +02003835 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3836 if (!(features & NETIF_F_GSO_PARTIAL)) {
3837 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003838 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003839
3840 if (!list_skb ||
3841 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3842 goto normal;
3843
Ilan Tayari43170c42017-04-19 21:26:07 +03003844 /* If we get here then all the required
3845 * GSO features except frag_list are supported.
3846 * Try to split the SKB to multiple GSO SKBs
3847 * with no frag_list.
3848 * Currently we can do that only when the buffers don't
3849 * have a linear part and all the buffers except
3850 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003851 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003852 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003853 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003854 if (frag_len != iter->len && iter->next)
3855 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003856 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003857 goto normal;
3858
3859 len -= iter->len;
3860 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003861
3862 if (len != frag_len)
3863 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003864 }
3865
3866 /* GSO partial only requires that we trim off any excess that
3867 * doesn't fit into an MSS sized block, so take care of that
3868 * now.
3869 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003870 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003871 if (partial_segs > 1)
3872 mss *= partial_segs;
3873 else
3874 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003875 }
3876
Steffen Klassert07b26c92016-09-19 12:58:47 +02003877normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003878 headroom = skb_headroom(head_skb);
3879 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003880
3881 do {
3882 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003883 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003884 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003885 int size;
3886
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003887 if (unlikely(mss == GSO_BY_FRAGS)) {
3888 len = list_skb->len;
3889 } else {
3890 len = head_skb->len - offset;
3891 if (len > mss)
3892 len = mss;
3893 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003894
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003895 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003896 if (hsize < 0)
3897 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003898 if (hsize > len || !sg)
3899 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003900
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003901 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3902 (skb_headlen(list_skb) == len || sg)) {
3903 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003904
Herbert Xu9d8506c2013-11-21 11:10:04 -08003905 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003906 nfrags = skb_shinfo(list_skb)->nr_frags;
3907 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003908 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003909 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003910
3911 while (pos < offset + len) {
3912 BUG_ON(i >= nfrags);
3913
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003914 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003915 if (pos + size > offset + len)
3916 break;
3917
3918 i++;
3919 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003920 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003921 }
3922
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003923 nskb = skb_clone(list_skb, GFP_ATOMIC);
3924 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003925
3926 if (unlikely(!nskb))
3927 goto err;
3928
Herbert Xu9d8506c2013-11-21 11:10:04 -08003929 if (unlikely(pskb_trim(nskb, len))) {
3930 kfree_skb(nskb);
3931 goto err;
3932 }
3933
Alexander Duyckec47ea82012-05-04 14:26:56 +00003934 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003935 if (skb_cow_head(nskb, doffset + headroom)) {
3936 kfree_skb(nskb);
3937 goto err;
3938 }
3939
Alexander Duyckec47ea82012-05-04 14:26:56 +00003940 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003941 skb_release_head_state(nskb);
3942 __skb_push(nskb, doffset);
3943 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003944 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003945 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003946 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003947
3948 if (unlikely(!nskb))
3949 goto err;
3950
3951 skb_reserve(nskb, headroom);
3952 __skb_put(nskb, doffset);
3953 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003954
3955 if (segs)
3956 tail->next = nskb;
3957 else
3958 segs = nskb;
3959 tail = nskb;
3960
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003961 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003962
Eric Dumazet030737b2013-10-19 11:42:54 -07003963 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003964 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003965
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003966 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003967 nskb->data - tnl_hlen,
3968 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003969
Herbert Xu9d8506c2013-11-21 11:10:04 -08003970 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003971 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003972
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003973 if (!sg) {
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303974 if (!csum) {
3975 if (!nskb->remcsum_offload)
3976 nskb->ip_summed = CHECKSUM_NONE;
3977 SKB_GSO_CB(nskb)->csum =
3978 skb_copy_and_csum_bits(head_skb, offset,
3979 skb_put(nskb,
3980 len),
Al Viro8d5930d2020-07-10 20:07:10 -04003981 len);
Yadu Kishore1454c9f2020-03-17 14:08:38 +05303982 SKB_GSO_CB(nskb)->csum_start =
3983 skb_headroom(nskb) + doffset;
3984 } else {
3985 skb_copy_bits(head_skb, offset,
3986 skb_put(nskb, len),
3987 len);
3988 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003989 continue;
3990 }
3991
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003992 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003993
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003994 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003995 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003996
Willem de Bruijnfff88032017-06-08 11:35:03 -04003997 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3998 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003999
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004000 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4001 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4002 goto err;
4003
Herbert Xu9d8506c2013-11-21 11:10:04 -08004004 while (pos < offset + len) {
4005 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08004006 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004007 nfrags = skb_shinfo(list_skb)->nr_frags;
4008 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004009 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07004010 if (!skb_headlen(list_skb)) {
4011 BUG_ON(!nfrags);
4012 } else {
4013 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004014
Yonghong Song13acc942018-03-21 16:31:03 -07004015 /* to make room for head_frag. */
4016 i--;
4017 frag--;
4018 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004019 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4020 skb_zerocopy_clone(nskb, frag_skb,
4021 GFP_ATOMIC))
4022 goto err;
4023
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004024 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004025 }
4026
4027 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4028 MAX_SKB_FRAGS)) {
4029 net_warn_ratelimited(
4030 "skb_segment: too many frags: %u %u\n",
4031 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07004032 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004033 goto err;
4034 }
4035
Yonghong Song13acc942018-03-21 16:31:03 -07004036 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004037 __skb_frag_ref(nskb_frag);
4038 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004039
4040 if (pos < offset) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004041 skb_frag_off_add(nskb_frag, offset - pos);
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004042 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004043 }
4044
Herbert Xu89319d382008-12-15 23:26:06 -08004045 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004046
4047 if (pos + size <= offset + len) {
4048 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004049 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004050 pos += size;
4051 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004052 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08004053 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004054 }
4055
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004056 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004057 }
4058
Herbert Xu89319d382008-12-15 23:26:06 -08004059skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07004060 nskb->data_len = len - hsize;
4061 nskb->len += nskb->data_len;
4062 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004063
Simon Horman1cdbcb72013-05-19 15:46:49 +00004064perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004065 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07004066 if (skb_has_shared_frag(nskb) &&
4067 __skb_linearize(nskb))
4068 goto err;
4069
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004070 if (!nskb->remcsum_offload)
4071 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08004072 SKB_GSO_CB(nskb)->csum =
4073 skb_checksum(nskb, doffset,
4074 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004075 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08004076 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004077 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004078 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004079
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07004080 /* Some callers want to get the end of the list.
4081 * Put it in segs->prev to avoid walking the list.
4082 * (see validate_xmit_skb_list() for example)
4083 */
4084 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07004085
Alexander Duyck802ab552016-04-10 21:45:03 -04004086 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02004087 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04004088 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004089 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04004090
4091 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004092 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04004093 type &= ~SKB_GSO_DODGY;
4094
4095 /* Update GSO info and prepare to start updating headers on
4096 * our way back down the stack of protocols.
4097 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004098 for (iter = segs; iter; iter = iter->next) {
4099 skb_shinfo(iter)->gso_size = gso_size;
4100 skb_shinfo(iter)->gso_segs = partial_segs;
4101 skb_shinfo(iter)->gso_type = type;
4102 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4103 }
4104
4105 if (tail->len - doffset <= gso_size)
4106 skb_shinfo(tail)->gso_size = 0;
4107 else if (tail != segs)
4108 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04004109 }
4110
Toshiaki Makita432c8562014-10-27 10:30:51 -07004111 /* Following permits correct backpressure, for protocols
4112 * using skb_set_owner_w().
4113 * Idea is to tranfert ownership from head_skb to last segment.
4114 */
4115 if (head_skb->destructor == sock_wfree) {
4116 swap(tail->truesize, head_skb->truesize);
4117 swap(tail->destructor, head_skb->destructor);
4118 swap(tail->sk, head_skb->sk);
4119 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004120 return segs;
4121
4122err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08004123 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004124 return ERR_PTR(err);
4125}
Herbert Xuf4c50d92006-06-22 03:02:40 -07004126EXPORT_SYMBOL_GPL(skb_segment);
4127
David Millerd4546c22018-06-24 14:13:49 +09004128int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08004129{
Eric Dumazet8a291112013-10-08 09:02:23 -07004130 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00004131 unsigned int offset = skb_gro_offset(skb);
4132 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004133 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004134 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09004135 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08004136
Steffen Klassert0ab03f32019-04-02 08:16:03 +02004137 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08004138 return -E2BIG;
4139
Eric Dumazet29e98242014-05-16 11:34:37 -07004140 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07004141 pinfo = skb_shinfo(lp);
4142
4143 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00004144 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004145 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004146 int i = skbinfo->nr_frags;
4147 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00004148
Herbert Xu66e92fc2009-05-26 18:50:32 +00004149 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004150 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00004151
Eric Dumazet8a291112013-10-08 09:02:23 -07004152 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004153 pinfo->nr_frags = nr_frags;
4154 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08004155
Herbert Xu9aaa1562009-05-26 18:50:33 +00004156 frag = pinfo->frags + nr_frags;
4157 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004158 do {
4159 *--frag = *--frag2;
4160 } while (--i);
4161
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004162 skb_frag_off_add(frag, offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004163 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00004164
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004165 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00004166 delta_truesize = skb->truesize -
4167 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004168
Herbert Xuf5572062009-01-14 20:40:03 -08004169 skb->truesize -= skb->data_len;
4170 skb->len -= skb->data_len;
4171 skb->data_len = 0;
4172
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004173 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004174 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004175 } else if (skb->head_frag) {
4176 int nr_frags = pinfo->nr_frags;
4177 skb_frag_t *frag = pinfo->frags + nr_frags;
4178 struct page *page = virt_to_head_page(skb->head);
4179 unsigned int first_size = headlen - offset;
4180 unsigned int first_offset;
4181
4182 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004183 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004184
4185 first_offset = skb->data -
4186 (unsigned char *)page_address(page) +
4187 offset;
4188
4189 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4190
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07004191 __skb_frag_set_page(frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004192 skb_frag_off_set(frag, first_offset);
Eric Dumazetd7e88832012-04-30 08:10:34 +00004193 skb_frag_size_set(frag, first_size);
4194
4195 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4196 /* We dont need to clear skbinfo->nr_frags here */
4197
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004198 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00004199 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4200 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004201 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004202
4203merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004204 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004205 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004206 unsigned int eat = offset - headlen;
4207
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004208 skb_frag_off_add(&skbinfo->frags[0], eat);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004209 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004210 skb->data_len -= eat;
4211 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004212 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004213 }
4214
Herbert Xu67147ba2009-05-26 18:50:22 +00004215 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004216
Eric Dumazet29e98242014-05-16 11:34:37 -07004217 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004218 skb_shinfo(p)->frag_list = skb;
4219 else
4220 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004221 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004222 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004223 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004224
Herbert Xu5d38a072009-01-04 16:13:40 -08004225done:
4226 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004227 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004228 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004229 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004230 if (lp != p) {
4231 lp->data_len += len;
4232 lp->truesize += delta_truesize;
4233 lp->len += len;
4234 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004235 NAPI_GRO_CB(skb)->same_flow = 1;
4236 return 0;
4237}
Herbert Xu71d93b32008-12-15 23:42:33 -08004238
Florian Westphaldf5042f2018-12-18 17:15:16 +01004239#ifdef CONFIG_SKB_EXTENSIONS
4240#define SKB_EXT_ALIGN_VALUE 8
4241#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4242
4243static const u8 skb_ext_type_len[] = {
4244#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4245 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4246#endif
Florian Westphal41650792018-12-18 17:15:27 +01004247#ifdef CONFIG_XFRM
4248 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4249#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004250#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4251 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4252#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004253#if IS_ENABLED(CONFIG_MPTCP)
4254 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4255#endif
Aleksandr Nogikh4a31baf2020-10-29 17:36:19 +00004256#if IS_ENABLED(CONFIG_KCOV)
4257 [SKB_EXT_KCOV_HANDLE] = SKB_EXT_CHUNKSIZEOF(u64),
4258#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004259};
4260
4261static __always_inline unsigned int skb_ext_total_length(void)
4262{
4263 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4264#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4265 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4266#endif
Florian Westphal41650792018-12-18 17:15:27 +01004267#ifdef CONFIG_XFRM
4268 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4269#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004270#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4271 skb_ext_type_len[TC_SKB_EXT] +
4272#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004273#if IS_ENABLED(CONFIG_MPTCP)
4274 skb_ext_type_len[SKB_EXT_MPTCP] +
4275#endif
Aleksandr Nogikh4a31baf2020-10-29 17:36:19 +00004276#if IS_ENABLED(CONFIG_KCOV)
4277 skb_ext_type_len[SKB_EXT_KCOV_HANDLE] +
4278#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004279 0;
4280}
4281
4282static void skb_extensions_init(void)
4283{
4284 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4285 BUILD_BUG_ON(skb_ext_total_length() > 255);
4286
4287 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4288 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4289 0,
4290 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4291 NULL);
4292}
4293#else
4294static void skb_extensions_init(void) {}
4295#endif
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297void __init skb_init(void)
4298{
Kees Cook79a8a642018-02-07 17:44:38 -08004299 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 sizeof(struct sk_buff),
4301 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004302 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004303 offsetof(struct sk_buff, cb),
4304 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004305 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004306 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004307 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004308 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004309 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004310 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004311 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
David S. Miller51c739d2007-10-30 21:29:29 -07004314static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004315__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4316 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004317{
David S. Miller1a028e52007-04-27 15:21:23 -07004318 int start = skb_headlen(skb);
4319 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004320 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004321 int elt = 0;
4322
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004323 if (unlikely(recursion_level >= 24))
4324 return -EMSGSIZE;
4325
David Howells716ea3a2007-04-02 20:19:53 -07004326 if (copy > 0) {
4327 if (copy > len)
4328 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004329 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004330 elt++;
4331 if ((len -= copy) == 0)
4332 return elt;
4333 offset += copy;
4334 }
4335
4336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004337 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004338
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004339 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004340
Eric Dumazet9e903e02011-10-18 21:00:24 +00004341 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004342 if ((copy = end - offset) > 0) {
4343 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004344 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4345 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004346
4347 if (copy > len)
4348 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004349 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004350 skb_frag_off(frag) + offset - start);
David Howells716ea3a2007-04-02 20:19:53 -07004351 elt++;
4352 if (!(len -= copy))
4353 return elt;
4354 offset += copy;
4355 }
David S. Miller1a028e52007-04-27 15:21:23 -07004356 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004357 }
4358
David S. Millerfbb398a2009-06-09 00:18:59 -07004359 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004360 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004361
David S. Millerfbb398a2009-06-09 00:18:59 -07004362 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004363
David S. Millerfbb398a2009-06-09 00:18:59 -07004364 end = start + frag_iter->len;
4365 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004366 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4367 return -EMSGSIZE;
4368
David S. Millerfbb398a2009-06-09 00:18:59 -07004369 if (copy > len)
4370 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004371 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4372 copy, recursion_level + 1);
4373 if (unlikely(ret < 0))
4374 return ret;
4375 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004376 if ((len -= copy) == 0)
4377 return elt;
4378 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004379 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004380 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004381 }
4382 BUG_ON(len);
4383 return elt;
4384}
4385
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004386/**
4387 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4388 * @skb: Socket buffer containing the buffers to be mapped
4389 * @sg: The scatter-gather list to map into
4390 * @offset: The offset into the buffer's contents to start mapping
4391 * @len: Length of buffer space to be mapped
4392 *
4393 * Fill the specified scatter-gather list with mappings/pointers into a
4394 * region of the buffer space attached to a socket buffer. Returns either
4395 * the number of scatterlist items used, or -EMSGSIZE if the contents
4396 * could not fit.
4397 */
4398int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4399{
4400 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4401
4402 if (nsg <= 0)
4403 return nsg;
4404
4405 sg_mark_end(&sg[nsg - 1]);
4406
4407 return nsg;
4408}
4409EXPORT_SYMBOL_GPL(skb_to_sgvec);
4410
Fan Du25a91d82014-01-18 09:54:23 +08004411/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4412 * sglist without mark the sg which contain last skb data as the end.
4413 * So the caller can mannipulate sg list as will when padding new data after
4414 * the first call without calling sg_unmark_end to expend sg list.
4415 *
4416 * Scenario to use skb_to_sgvec_nomark:
4417 * 1. sg_init_table
4418 * 2. skb_to_sgvec_nomark(payload1)
4419 * 3. skb_to_sgvec_nomark(payload2)
4420 *
4421 * This is equivalent to:
4422 * 1. sg_init_table
4423 * 2. skb_to_sgvec(payload1)
4424 * 3. sg_unmark_end
4425 * 4. skb_to_sgvec(payload2)
4426 *
4427 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4428 * is more preferable.
4429 */
4430int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4431 int offset, int len)
4432{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004433 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004434}
4435EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4436
David S. Miller51c739d2007-10-30 21:29:29 -07004437
David S. Miller51c739d2007-10-30 21:29:29 -07004438
David Howells716ea3a2007-04-02 20:19:53 -07004439/**
4440 * skb_cow_data - Check that a socket buffer's data buffers are writable
4441 * @skb: The socket buffer to check.
4442 * @tailbits: Amount of trailing space to be added
4443 * @trailer: Returned pointer to the skb where the @tailbits space begins
4444 *
4445 * Make sure that the data buffers attached to a socket buffer are
4446 * writable. If they are not, private copies are made of the data buffers
4447 * and the socket buffer is set to use these instead.
4448 *
4449 * If @tailbits is given, make sure that there is space to write @tailbits
4450 * bytes of data beyond current end of socket buffer. @trailer will be
4451 * set to point to the skb in which this space begins.
4452 *
4453 * The number of scatterlist elements required to completely map the
4454 * COW'd and extended socket buffer will be returned.
4455 */
4456int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4457{
4458 int copyflag;
4459 int elt;
4460 struct sk_buff *skb1, **skb_p;
4461
4462 /* If skb is cloned or its head is paged, reallocate
4463 * head pulling out all the pages (pages are considered not writable
4464 * at the moment even if they are anonymous).
4465 */
4466 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
Miaohe Linc15fc192020-08-01 17:30:23 +08004467 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
David Howells716ea3a2007-04-02 20:19:53 -07004468 return -ENOMEM;
4469
4470 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004471 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004472 /* A little of trouble, not enough of space for trailer.
4473 * This should not happen, when stack is tuned to generate
4474 * good frames. OK, on miss we reallocate and reserve even more
4475 * space, 128 bytes is fair. */
4476
4477 if (skb_tailroom(skb) < tailbits &&
4478 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4479 return -ENOMEM;
4480
4481 /* Voila! */
4482 *trailer = skb;
4483 return 1;
4484 }
4485
4486 /* Misery. We are in troubles, going to mincer fragments... */
4487
4488 elt = 1;
4489 skb_p = &skb_shinfo(skb)->frag_list;
4490 copyflag = 0;
4491
4492 while ((skb1 = *skb_p) != NULL) {
4493 int ntail = 0;
4494
4495 /* The fragment is partially pulled by someone,
4496 * this can happen on input. Copy it and everything
4497 * after it. */
4498
4499 if (skb_shared(skb1))
4500 copyflag = 1;
4501
4502 /* If the skb is the last, worry about trailer. */
4503
4504 if (skb1->next == NULL && tailbits) {
4505 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004506 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004507 skb_tailroom(skb1) < tailbits)
4508 ntail = tailbits + 128;
4509 }
4510
4511 if (copyflag ||
4512 skb_cloned(skb1) ||
4513 ntail ||
4514 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004515 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004516 struct sk_buff *skb2;
4517
4518 /* Fuck, we are miserable poor guys... */
4519 if (ntail == 0)
4520 skb2 = skb_copy(skb1, GFP_ATOMIC);
4521 else
4522 skb2 = skb_copy_expand(skb1,
4523 skb_headroom(skb1),
4524 ntail,
4525 GFP_ATOMIC);
4526 if (unlikely(skb2 == NULL))
4527 return -ENOMEM;
4528
4529 if (skb1->sk)
4530 skb_set_owner_w(skb2, skb1->sk);
4531
4532 /* Looking around. Are we still alive?
4533 * OK, link new skb, drop old one */
4534
4535 skb2->next = skb1->next;
4536 *skb_p = skb2;
4537 kfree_skb(skb1);
4538 skb1 = skb2;
4539 }
4540 elt++;
4541 *trailer = skb1;
4542 skb_p = &skb1->next;
4543 }
4544
4545 return elt;
4546}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004547EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004548
Eric Dumazetb1faf562010-05-31 23:44:05 -07004549static void sock_rmem_free(struct sk_buff *skb)
4550{
4551 struct sock *sk = skb->sk;
4552
4553 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4554}
4555
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004556static void skb_set_err_queue(struct sk_buff *skb)
4557{
4558 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4559 * So, it is safe to (mis)use it to mark skbs on the error queue.
4560 */
4561 skb->pkt_type = PACKET_OUTGOING;
4562 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4563}
4564
Eric Dumazetb1faf562010-05-31 23:44:05 -07004565/*
4566 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4567 */
4568int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4569{
4570 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazetebb3b782019-10-10 20:17:44 -07004571 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
Eric Dumazetb1faf562010-05-31 23:44:05 -07004572 return -ENOMEM;
4573
4574 skb_orphan(skb);
4575 skb->sk = sk;
4576 skb->destructor = sock_rmem_free;
4577 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004578 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004579
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004580 /* before exiting rcu section, make sure dst is refcounted */
4581 skb_dst_force(skb);
4582
Eric Dumazetb1faf562010-05-31 23:44:05 -07004583 skb_queue_tail(&sk->sk_error_queue, skb);
4584 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004585 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004586 return 0;
4587}
4588EXPORT_SYMBOL(sock_queue_err_skb);
4589
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004590static bool is_icmp_err_skb(const struct sk_buff *skb)
4591{
4592 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4593 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4594}
4595
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004596struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4597{
4598 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004599 struct sk_buff *skb, *skb_next = NULL;
4600 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004601 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004602
Eric Dumazet997d5c32015-02-18 05:47:55 -08004603 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004604 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004605 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004606 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004607 if (icmp_next)
Willem de Bruijn985f7332020-11-26 10:12:20 -05004608 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004609 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004610 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004611
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004612 if (is_icmp_err_skb(skb) && !icmp_next)
4613 sk->sk_err = 0;
4614
4615 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004616 sk->sk_error_report(sk);
4617
4618 return skb;
4619}
4620EXPORT_SYMBOL(sock_dequeue_err_skb);
4621
Alexander Duyckcab41c42014-09-10 18:05:26 -04004622/**
4623 * skb_clone_sk - create clone of skb, and take reference to socket
4624 * @skb: the skb to clone
4625 *
4626 * This function creates a clone of a buffer that holds a reference on
4627 * sk_refcnt. Buffers created via this function are meant to be
4628 * returned using sock_queue_err_skb, or free via kfree_skb.
4629 *
4630 * When passing buffers allocated with this function to sock_queue_err_skb
4631 * it is necessary to wrap the call with sock_hold/sock_put in order to
4632 * prevent the socket from being released prior to being enqueued on
4633 * the sk_error_queue.
4634 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004635struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4636{
4637 struct sock *sk = skb->sk;
4638 struct sk_buff *clone;
4639
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004640 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004641 return NULL;
4642
4643 clone = skb_clone(skb, GFP_ATOMIC);
4644 if (!clone) {
4645 sock_put(sk);
4646 return NULL;
4647 }
4648
4649 clone->sk = sk;
4650 clone->destructor = sock_efree;
4651
4652 return clone;
4653}
4654EXPORT_SYMBOL(skb_clone_sk);
4655
Alexander Duyck37846ef2014-09-04 13:31:10 -04004656static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4657 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004658 int tstype,
4659 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004660{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004661 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004662 int err;
4663
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004664 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4665
Patrick Ohlyac45f602009-02-12 05:03:37 +00004666 serr = SKB_EXT_ERR(skb);
4667 memset(serr, 0, sizeof(*serr));
4668 serr->ee.ee_errno = ENOMSG;
4669 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004670 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004671 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004672 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004673 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004674 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004675 if (sk->sk_protocol == IPPROTO_TCP &&
4676 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004677 serr->ee.ee_data -= sk->sk_tskey;
4678 }
Eric Dumazet29030372010-05-29 00:20:48 -07004679
Patrick Ohlyac45f602009-02-12 05:03:37 +00004680 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004681
Patrick Ohlyac45f602009-02-12 05:03:37 +00004682 if (err)
4683 kfree_skb(skb);
4684}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004685
Willem de Bruijnb245be12015-01-30 13:29:32 -05004686static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4687{
4688 bool ret;
4689
4690 if (likely(sysctl_tstamp_allow_data || tsonly))
4691 return true;
4692
4693 read_lock_bh(&sk->sk_callback_lock);
4694 ret = sk->sk_socket && sk->sk_socket->file &&
4695 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4696 read_unlock_bh(&sk->sk_callback_lock);
4697 return ret;
4698}
4699
Alexander Duyck37846ef2014-09-04 13:31:10 -04004700void skb_complete_tx_timestamp(struct sk_buff *skb,
4701 struct skb_shared_hwtstamps *hwtstamps)
4702{
4703 struct sock *sk = skb->sk;
4704
Willem de Bruijnb245be12015-01-30 13:29:32 -05004705 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004706 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004707
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004708 /* Take a reference to prevent skb_orphan() from freeing the socket,
4709 * but only if the socket refcount is not zero.
4710 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004711 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004712 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004713 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004714 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004715 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004716 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004717
4718err:
4719 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004720}
4721EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4722
4723void __skb_tstamp_tx(struct sk_buff *orig_skb,
4724 struct skb_shared_hwtstamps *hwtstamps,
4725 struct sock *sk, int tstype)
4726{
4727 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004728 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004729
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004730 if (!sk)
4731 return;
4732
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004733 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4734 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4735 return;
4736
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004737 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4738 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004739 return;
4740
Francis Yan1c885802016-11-27 23:07:18 -08004741 if (tsonly) {
4742#ifdef CONFIG_INET
4743 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4744 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004745 sk->sk_type == SOCK_STREAM) {
Yousuk Seung48040792020-07-30 15:44:40 -07004746 skb = tcp_get_timestamping_opt_stats(sk, orig_skb);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004747 opt_stats = true;
4748 } else
Francis Yan1c885802016-11-27 23:07:18 -08004749#endif
4750 skb = alloc_skb(0, GFP_ATOMIC);
4751 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004752 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004753 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004754 if (!skb)
4755 return;
4756
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004757 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004758 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4759 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004760 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4761 }
4762
4763 if (hwtstamps)
4764 *skb_hwtstamps(skb) = *hwtstamps;
4765 else
4766 skb->tstamp = ktime_get_real();
4767
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004768 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004769}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004770EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4771
4772void skb_tstamp_tx(struct sk_buff *orig_skb,
4773 struct skb_shared_hwtstamps *hwtstamps)
4774{
4775 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4776 SCM_TSTAMP_SND);
4777}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004778EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4779
Johannes Berg6e3e9392011-11-09 10:15:42 +01004780void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4781{
4782 struct sock *sk = skb->sk;
4783 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004784 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004785
4786 skb->wifi_acked_valid = 1;
4787 skb->wifi_acked = acked;
4788
4789 serr = SKB_EXT_ERR(skb);
4790 memset(serr, 0, sizeof(*serr));
4791 serr->ee.ee_errno = ENOMSG;
4792 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4793
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004794 /* Take a reference to prevent skb_orphan() from freeing the socket,
4795 * but only if the socket refcount is not zero.
4796 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004797 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004798 err = sock_queue_err_skb(sk, skb);
4799 sock_put(sk);
4800 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004801 if (err)
4802 kfree_skb(skb);
4803}
4804EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4805
Rusty Russellf35d9d82008-02-04 23:49:54 -05004806/**
4807 * skb_partial_csum_set - set up and verify partial csum values for packet
4808 * @skb: the skb to set
4809 * @start: the number of bytes after skb->data to start checksumming.
4810 * @off: the offset from start to place the checksum.
4811 *
4812 * For untrusted partially-checksummed packets, we need to make sure the values
4813 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4814 *
4815 * This function checks and sets those values and skb->ip_summed: if this
4816 * returns false you should drop the packet.
4817 */
4818bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4819{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004820 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4821 u32 csum_start = skb_headroom(skb) + (u32)start;
4822
4823 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4824 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4825 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004826 return false;
4827 }
4828 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004829 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004830 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004831 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004832 return true;
4833}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004834EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004835
Paul Durranted1f50c2014-01-09 10:02:46 +00004836static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4837 unsigned int max)
4838{
4839 if (skb_headlen(skb) >= len)
4840 return 0;
4841
4842 /* If we need to pullup then pullup to the max, so we
4843 * won't need to do it again.
4844 */
4845 if (max > skb->len)
4846 max = skb->len;
4847
4848 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4849 return -ENOMEM;
4850
4851 if (skb_headlen(skb) < len)
4852 return -EPROTO;
4853
4854 return 0;
4855}
4856
Jan Beulichf9708b42014-03-11 13:56:05 +00004857#define MAX_TCP_HDR_LEN (15 * 4)
4858
4859static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4860 typeof(IPPROTO_IP) proto,
4861 unsigned int off)
4862{
Kees Cook161d1792020-02-19 22:23:04 -08004863 int err;
Jan Beulichf9708b42014-03-11 13:56:05 +00004864
Kees Cook161d1792020-02-19 22:23:04 -08004865 switch (proto) {
Jan Beulichf9708b42014-03-11 13:56:05 +00004866 case IPPROTO_TCP:
4867 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4868 off + MAX_TCP_HDR_LEN);
4869 if (!err && !skb_partial_csum_set(skb, off,
4870 offsetof(struct tcphdr,
4871 check)))
4872 err = -EPROTO;
4873 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4874
4875 case IPPROTO_UDP:
4876 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4877 off + sizeof(struct udphdr));
4878 if (!err && !skb_partial_csum_set(skb, off,
4879 offsetof(struct udphdr,
4880 check)))
4881 err = -EPROTO;
4882 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4883 }
4884
4885 return ERR_PTR(-EPROTO);
4886}
4887
Paul Durranted1f50c2014-01-09 10:02:46 +00004888/* This value should be large enough to cover a tagged ethernet header plus
4889 * maximally sized IP and TCP or UDP headers.
4890 */
4891#define MAX_IP_HDR_LEN 128
4892
Jan Beulichf9708b42014-03-11 13:56:05 +00004893static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004894{
4895 unsigned int off;
4896 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004897 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004898 int err;
4899
4900 fragment = false;
4901
4902 err = skb_maybe_pull_tail(skb,
4903 sizeof(struct iphdr),
4904 MAX_IP_HDR_LEN);
4905 if (err < 0)
4906 goto out;
4907
Miaohe Lin11f920d2020-08-06 19:57:18 +08004908 if (ip_is_fragment(ip_hdr(skb)))
Paul Durranted1f50c2014-01-09 10:02:46 +00004909 fragment = true;
4910
4911 off = ip_hdrlen(skb);
4912
4913 err = -EPROTO;
4914
4915 if (fragment)
4916 goto out;
4917
Jan Beulichf9708b42014-03-11 13:56:05 +00004918 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4919 if (IS_ERR(csum))
4920 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004921
Jan Beulichf9708b42014-03-11 13:56:05 +00004922 if (recalculate)
4923 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4924 ip_hdr(skb)->daddr,
4925 skb->len - off,
4926 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004927 err = 0;
4928
4929out:
4930 return err;
4931}
4932
4933/* This value should be large enough to cover a tagged ethernet header plus
4934 * an IPv6 header, all options, and a maximal TCP or UDP header.
4935 */
4936#define MAX_IPV6_HDR_LEN 256
4937
4938#define OPT_HDR(type, skb, off) \
4939 (type *)(skb_network_header(skb) + (off))
4940
4941static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4942{
4943 int err;
4944 u8 nexthdr;
4945 unsigned int off;
4946 unsigned int len;
4947 bool fragment;
4948 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004949 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004950
4951 fragment = false;
4952 done = false;
4953
4954 off = sizeof(struct ipv6hdr);
4955
4956 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4957 if (err < 0)
4958 goto out;
4959
4960 nexthdr = ipv6_hdr(skb)->nexthdr;
4961
4962 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4963 while (off <= len && !done) {
4964 switch (nexthdr) {
4965 case IPPROTO_DSTOPTS:
4966 case IPPROTO_HOPOPTS:
4967 case IPPROTO_ROUTING: {
4968 struct ipv6_opt_hdr *hp;
4969
4970 err = skb_maybe_pull_tail(skb,
4971 off +
4972 sizeof(struct ipv6_opt_hdr),
4973 MAX_IPV6_HDR_LEN);
4974 if (err < 0)
4975 goto out;
4976
4977 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4978 nexthdr = hp->nexthdr;
4979 off += ipv6_optlen(hp);
4980 break;
4981 }
4982 case IPPROTO_AH: {
4983 struct ip_auth_hdr *hp;
4984
4985 err = skb_maybe_pull_tail(skb,
4986 off +
4987 sizeof(struct ip_auth_hdr),
4988 MAX_IPV6_HDR_LEN);
4989 if (err < 0)
4990 goto out;
4991
4992 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4993 nexthdr = hp->nexthdr;
4994 off += ipv6_authlen(hp);
4995 break;
4996 }
4997 case IPPROTO_FRAGMENT: {
4998 struct frag_hdr *hp;
4999
5000 err = skb_maybe_pull_tail(skb,
5001 off +
5002 sizeof(struct frag_hdr),
5003 MAX_IPV6_HDR_LEN);
5004 if (err < 0)
5005 goto out;
5006
5007 hp = OPT_HDR(struct frag_hdr, skb, off);
5008
5009 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5010 fragment = true;
5011
5012 nexthdr = hp->nexthdr;
5013 off += sizeof(struct frag_hdr);
5014 break;
5015 }
5016 default:
5017 done = true;
5018 break;
5019 }
5020 }
5021
5022 err = -EPROTO;
5023
5024 if (!done || fragment)
5025 goto out;
5026
Jan Beulichf9708b42014-03-11 13:56:05 +00005027 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5028 if (IS_ERR(csum))
5029 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005030
Jan Beulichf9708b42014-03-11 13:56:05 +00005031 if (recalculate)
5032 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5033 &ipv6_hdr(skb)->daddr,
5034 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005035 err = 0;
5036
5037out:
5038 return err;
5039}
5040
5041/**
5042 * skb_checksum_setup - set up partial checksum offset
5043 * @skb: the skb to set up
5044 * @recalculate: if true the pseudo-header checksum will be recalculated
5045 */
5046int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5047{
5048 int err;
5049
5050 switch (skb->protocol) {
5051 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00005052 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00005053 break;
5054
5055 case htons(ETH_P_IPV6):
5056 err = skb_checksum_setup_ipv6(skb, recalculate);
5057 break;
5058
5059 default:
5060 err = -EPROTO;
5061 break;
5062 }
5063
5064 return err;
5065}
5066EXPORT_SYMBOL(skb_checksum_setup);
5067
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005068/**
5069 * skb_checksum_maybe_trim - maybe trims the given skb
5070 * @skb: the skb to check
5071 * @transport_len: the data length beyond the network header
5072 *
5073 * Checks whether the given skb has data beyond the given transport length.
5074 * If so, returns a cloned skb trimmed to this transport length.
5075 * Otherwise returns the provided skb. Returns NULL in error cases
5076 * (e.g. transport_len exceeds skb length or out-of-memory).
5077 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005078 * Caller needs to set the skb transport header and free any returned skb if it
5079 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005080 */
5081static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5082 unsigned int transport_len)
5083{
5084 struct sk_buff *skb_chk;
5085 unsigned int len = skb_transport_offset(skb) + transport_len;
5086 int ret;
5087
Linus Lüssinga5169932015-08-13 05:54:07 +02005088 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005089 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02005090 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005091 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005092
5093 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005094 if (!skb_chk)
5095 return NULL;
5096
5097 ret = pskb_trim_rcsum(skb_chk, len);
5098 if (ret) {
5099 kfree_skb(skb_chk);
5100 return NULL;
5101 }
5102
5103 return skb_chk;
5104}
5105
5106/**
5107 * skb_checksum_trimmed - validate checksum of an skb
5108 * @skb: the skb to check
5109 * @transport_len: the data length beyond the network header
5110 * @skb_chkf: checksum function to use
5111 *
5112 * Applies the given checksum function skb_chkf to the provided skb.
5113 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5114 *
5115 * If the skb has data beyond the given transport length, then a
5116 * trimmed & cloned skb is checked and returned.
5117 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005118 * Caller needs to set the skb transport header and free any returned skb if it
5119 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005120 */
5121struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5122 unsigned int transport_len,
5123 __sum16(*skb_chkf)(struct sk_buff *skb))
5124{
5125 struct sk_buff *skb_chk;
5126 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02005127 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005128
5129 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5130 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02005131 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005132
Linus Lüssinga5169932015-08-13 05:54:07 +02005133 if (!pskb_may_pull(skb_chk, offset))
5134 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005135
Linus Lüssing9b368812016-02-24 04:21:42 +01005136 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005137 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01005138 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005139
Linus Lüssinga5169932015-08-13 05:54:07 +02005140 if (ret)
5141 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005142
5143 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02005144
5145err:
5146 if (skb_chk && skb_chk != skb)
5147 kfree_skb(skb_chk);
5148
5149 return NULL;
5150
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005151}
5152EXPORT_SYMBOL(skb_checksum_trimmed);
5153
Ben Hutchings4497b072008-06-19 16:22:28 -07005154void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5155{
Joe Perchese87cc472012-05-13 21:56:26 +00005156 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5157 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07005158}
Ben Hutchings4497b072008-06-19 16:22:28 -07005159EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005160
5161void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5162{
Eric Dumazet3d861f62012-10-22 09:03:40 +00005163 if (head_stolen) {
5164 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005165 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005166 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005167 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005168 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005169}
5170EXPORT_SYMBOL(kfree_skb_partial);
5171
5172/**
5173 * skb_try_coalesce - try to merge skb to prior one
5174 * @to: prior buffer
5175 * @from: buffer to add
5176 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00005177 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005178 */
5179bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5180 bool *fragstolen, int *delta_truesize)
5181{
Eric Dumazetc818fa92017-10-04 10:48:35 -07005182 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005183 int i, delta, len = from->len;
5184
5185 *fragstolen = false;
5186
5187 if (skb_cloned(to))
5188 return false;
5189
5190 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005191 if (len)
5192 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005193 *delta_truesize = 0;
5194 return true;
5195 }
5196
Eric Dumazetc818fa92017-10-04 10:48:35 -07005197 to_shinfo = skb_shinfo(to);
5198 from_shinfo = skb_shinfo(from);
5199 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005200 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005201 if (skb_zcopy(to) || skb_zcopy(from))
5202 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005203
5204 if (skb_headlen(from) != 0) {
5205 struct page *page;
5206 unsigned int offset;
5207
Eric Dumazetc818fa92017-10-04 10:48:35 -07005208 if (to_shinfo->nr_frags +
5209 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005210 return false;
5211
5212 if (skb_head_is_locked(from))
5213 return false;
5214
5215 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5216
5217 page = virt_to_head_page(from->head);
5218 offset = from->data - (unsigned char *)page_address(page);
5219
Eric Dumazetc818fa92017-10-04 10:48:35 -07005220 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005221 page, offset, skb_headlen(from));
5222 *fragstolen = true;
5223 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005224 if (to_shinfo->nr_frags +
5225 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005226 return false;
5227
Weiping Panf4b549a2012-09-28 20:15:30 +00005228 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005229 }
5230
5231 WARN_ON_ONCE(delta < len);
5232
Eric Dumazetc818fa92017-10-04 10:48:35 -07005233 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5234 from_shinfo->frags,
5235 from_shinfo->nr_frags * sizeof(skb_frag_t));
5236 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005237
5238 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005239 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005240
Li RongQing8ea853f2012-09-18 16:53:21 +00005241 /* if the skb is not cloned this does nothing
5242 * since we set nr_frags to 0.
5243 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005244 for (i = 0; i < from_shinfo->nr_frags; i++)
5245 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005246
5247 to->truesize += delta;
5248 to->len += len;
5249 to->data_len += len;
5250
5251 *delta_truesize = delta;
5252 return true;
5253}
5254EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005255
5256/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005257 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005258 *
5259 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005260 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005261 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005262 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5263 * into/from a tunnel. Some information have to be cleared during these
5264 * operations.
5265 * skb_scrub_packet can also be used to clean a skb before injecting it in
5266 * another namespace (@xnet == true). We have to clear all information in the
5267 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005268 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005269void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005270{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005271 skb->pkt_type = PACKET_HOST;
5272 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005273 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005274 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005275 skb_ext_reset(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02005276 nf_reset_ct(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005277 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005278
Petr Machata6f9a5062018-11-19 16:11:07 +00005279#ifdef CONFIG_NET_SWITCHDEV
5280 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005281 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005282#endif
5283
Herbert Xu213dd742015-04-16 09:03:27 +08005284 if (!xnet)
5285 return;
5286
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005287 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005288 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005289 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005290}
5291EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005292
5293/**
5294 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5295 *
5296 * @skb: GSO skb
5297 *
5298 * skb_gso_transport_seglen is used to determine the real size of the
5299 * individual segments, including Layer4 headers (TCP/UDP).
5300 *
5301 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5302 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005303static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005304{
5305 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005306 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005307
Florian Westphalf993bc22014-10-20 13:49:18 +02005308 if (skb->encapsulation) {
5309 thlen = skb_inner_transport_header(skb) -
5310 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005311
Florian Westphalf993bc22014-10-20 13:49:18 +02005312 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5313 thlen += inner_tcp_hdrlen(skb);
5314 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5315 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005316 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005317 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005318 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5319 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005320 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005321 /* UFO sets gso_size to the size of the fragmentation
5322 * payload, i.e. the size of the L4 (UDP) header is already
5323 * accounted for.
5324 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005325 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005326}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005327
5328/**
5329 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5330 *
5331 * @skb: GSO skb
5332 *
5333 * skb_gso_network_seglen is used to determine the real size of the
5334 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5335 *
5336 * The MAC/L2 header is not accounted for.
5337 */
5338static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5339{
5340 unsigned int hdr_len = skb_transport_header(skb) -
5341 skb_network_header(skb);
5342
5343 return hdr_len + skb_gso_transport_seglen(skb);
5344}
5345
5346/**
5347 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5348 *
5349 * @skb: GSO skb
5350 *
5351 * skb_gso_mac_seglen is used to determine the real size of the
5352 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5353 * headers (TCP/UDP).
5354 */
5355static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5356{
5357 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5358
5359 return hdr_len + skb_gso_transport_seglen(skb);
5360}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005361
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005362/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005363 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5364 *
5365 * There are a couple of instances where we have a GSO skb, and we
5366 * want to determine what size it would be after it is segmented.
5367 *
5368 * We might want to check:
5369 * - L3+L4+payload size (e.g. IP forwarding)
5370 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5371 *
5372 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5373 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005374 * @skb: GSO skb
5375 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005376 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5377 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5378 *
5379 * @max_len: The maximum permissible length.
5380 *
5381 * Returns true if the segmented length <= max length.
5382 */
5383static inline bool skb_gso_size_check(const struct sk_buff *skb,
5384 unsigned int seg_len,
5385 unsigned int max_len) {
5386 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5387 const struct sk_buff *iter;
5388
5389 if (shinfo->gso_size != GSO_BY_FRAGS)
5390 return seg_len <= max_len;
5391
5392 /* Undo this so we can re-use header sizes */
5393 seg_len -= GSO_BY_FRAGS;
5394
5395 skb_walk_frags(skb, iter) {
5396 if (seg_len + skb_headlen(iter) > max_len)
5397 return false;
5398 }
5399
5400 return true;
5401}
5402
5403/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005404 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005405 *
5406 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005407 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005408 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005409 * skb_gso_validate_network_len validates if a given skb will fit a
5410 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5411 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005412 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005413bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005414{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005415 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005416}
Daniel Axtens779b7932018-03-01 17:13:37 +11005417EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005418
Daniel Axtens2b16f042018-01-31 14:15:33 +11005419/**
5420 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5421 *
5422 * @skb: GSO skb
5423 * @len: length to validate against
5424 *
5425 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5426 * length once split, including L2, L3 and L4 headers and the payload.
5427 */
5428bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5429{
5430 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5431}
5432EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5433
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005434static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5435{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005436 int mac_len, meta_len;
5437 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005438
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005439 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5440 kfree_skb(skb);
5441 return NULL;
5442 }
5443
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005444 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005445 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5446 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5447 mac_len - VLAN_HLEN - ETH_TLEN);
5448 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005449
5450 meta_len = skb_metadata_len(skb);
5451 if (meta_len) {
5452 meta = skb_metadata_end(skb) - meta_len;
5453 memmove(meta + VLAN_HLEN, meta, meta_len);
5454 }
5455
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005456 skb->mac_header += VLAN_HLEN;
5457 return skb;
5458}
5459
5460struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5461{
5462 struct vlan_hdr *vhdr;
5463 u16 vlan_tci;
5464
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005465 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005466 /* vlan_tci is already set-up so leave this for another time */
5467 return skb;
5468 }
5469
5470 skb = skb_share_check(skb, GFP_ATOMIC);
5471 if (unlikely(!skb))
5472 goto err_free;
Miaohe Lin55eff0e2020-08-15 04:44:31 -04005473 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5474 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005475 goto err_free;
5476
5477 vhdr = (struct vlan_hdr *)skb->data;
5478 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5479 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5480
5481 skb_pull_rcsum(skb, VLAN_HLEN);
5482 vlan_set_encap_proto(skb, vhdr);
5483
5484 skb = skb_reorder_vlan_header(skb);
5485 if (unlikely(!skb))
5486 goto err_free;
5487
5488 skb_reset_network_header(skb);
5489 skb_reset_transport_header(skb);
5490 skb_reset_mac_len(skb);
5491
5492 return skb;
5493
5494err_free:
5495 kfree_skb(skb);
5496 return NULL;
5497}
5498EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005499
Jiri Pirkoe2195122014-11-19 14:05:01 +01005500int skb_ensure_writable(struct sk_buff *skb, int write_len)
5501{
5502 if (!pskb_may_pull(skb, write_len))
5503 return -ENOMEM;
5504
5505 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5506 return 0;
5507
5508 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5509}
5510EXPORT_SYMBOL(skb_ensure_writable);
5511
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005512/* remove VLAN header from packet and update csum accordingly.
5513 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5514 */
5515int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005516{
5517 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005518 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005519 int err;
5520
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005521 if (WARN_ONCE(offset,
5522 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5523 offset)) {
5524 return -EINVAL;
5525 }
5526
Jiri Pirko93515d52014-11-19 14:05:02 +01005527 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5528 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005529 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005530
5531 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5532
5533 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5534 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5535
5536 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5537 __skb_pull(skb, VLAN_HLEN);
5538
5539 vlan_set_encap_proto(skb, vhdr);
5540 skb->mac_header += VLAN_HLEN;
5541
5542 if (skb_network_offset(skb) < ETH_HLEN)
5543 skb_set_network_header(skb, ETH_HLEN);
5544
5545 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005546
5547 return err;
5548}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005549EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005550
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005551/* Pop a vlan tag either from hwaccel or from payload.
5552 * Expects skb->data at mac header.
5553 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005554int skb_vlan_pop(struct sk_buff *skb)
5555{
5556 u16 vlan_tci;
5557 __be16 vlan_proto;
5558 int err;
5559
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005560 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005561 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005562 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005563 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005564 return 0;
5565
5566 err = __skb_vlan_pop(skb, &vlan_tci);
5567 if (err)
5568 return err;
5569 }
5570 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005571 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005572 return 0;
5573
5574 vlan_proto = skb->protocol;
5575 err = __skb_vlan_pop(skb, &vlan_tci);
5576 if (unlikely(err))
5577 return err;
5578
5579 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5580 return 0;
5581}
5582EXPORT_SYMBOL(skb_vlan_pop);
5583
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005584/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5585 * Expects skb->data at mac header.
5586 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005587int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5588{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005589 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005590 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005591 int err;
5592
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005593 if (WARN_ONCE(offset,
5594 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5595 offset)) {
5596 return -EINVAL;
5597 }
5598
Jiri Pirko93515d52014-11-19 14:05:02 +01005599 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005600 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005601 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005602 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005603
Jiri Pirko93515d52014-11-19 14:05:02 +01005604 skb->protocol = skb->vlan_proto;
5605 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005606
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005607 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005608 }
5609 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5610 return 0;
5611}
5612EXPORT_SYMBOL(skb_vlan_push);
5613
Guillaume Nault19fbcb32020-10-03 00:44:28 +02005614/**
5615 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5616 *
5617 * @skb: Socket buffer to modify
5618 *
5619 * Drop the Ethernet header of @skb.
5620 *
5621 * Expects that skb->data points to the mac header and that no VLAN tags are
5622 * present.
5623 *
5624 * Returns 0 on success, -errno otherwise.
5625 */
5626int skb_eth_pop(struct sk_buff *skb)
5627{
5628 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5629 skb_network_offset(skb) < ETH_HLEN)
5630 return -EPROTO;
5631
5632 skb_pull_rcsum(skb, ETH_HLEN);
5633 skb_reset_mac_header(skb);
5634 skb_reset_mac_len(skb);
5635
5636 return 0;
5637}
5638EXPORT_SYMBOL(skb_eth_pop);
5639
5640/**
5641 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5642 *
5643 * @skb: Socket buffer to modify
5644 * @dst: Destination MAC address of the new header
5645 * @src: Source MAC address of the new header
5646 *
5647 * Prepend @skb with a new Ethernet header.
5648 *
5649 * Expects that skb->data points to the mac header, which must be empty.
5650 *
5651 * Returns 0 on success, -errno otherwise.
5652 */
5653int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5654 const unsigned char *src)
5655{
5656 struct ethhdr *eth;
5657 int err;
5658
5659 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5660 return -EPROTO;
5661
5662 err = skb_cow_head(skb, sizeof(*eth));
5663 if (err < 0)
5664 return err;
5665
5666 skb_push(skb, sizeof(*eth));
5667 skb_reset_mac_header(skb);
5668 skb_reset_mac_len(skb);
5669
5670 eth = eth_hdr(skb);
5671 ether_addr_copy(eth->h_dest, dst);
5672 ether_addr_copy(eth->h_source, src);
5673 eth->h_proto = skb->protocol;
5674
5675 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5676
5677 return 0;
5678}
5679EXPORT_SYMBOL(skb_eth_push);
5680
John Hurley8822e272019-07-07 15:01:54 +01005681/* Update the ethertype of hdr and the skb csum value if required. */
5682static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5683 __be16 ethertype)
5684{
5685 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5686 __be16 diff[] = { ~hdr->h_proto, ethertype };
5687
5688 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5689 }
5690
5691 hdr->h_proto = ethertype;
5692}
5693
5694/**
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305695 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5696 * the packet
John Hurley8822e272019-07-07 15:01:54 +01005697 *
5698 * @skb: buffer
5699 * @mpls_lse: MPLS label stack entry to push
5700 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
Davide Carattifa4e0f82019-10-12 13:55:07 +02005701 * @mac_len: length of the MAC header
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305702 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5703 * ethernet
John Hurley8822e272019-07-07 15:01:54 +01005704 *
5705 * Expects skb->data at mac header.
5706 *
5707 * Returns 0 on success, -errno otherwise.
5708 */
Davide Carattifa4e0f82019-10-12 13:55:07 +02005709int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
Martin Varghesed04ac222019-12-05 05:57:22 +05305710 int mac_len, bool ethernet)
John Hurley8822e272019-07-07 15:01:54 +01005711{
5712 struct mpls_shim_hdr *lse;
5713 int err;
5714
5715 if (unlikely(!eth_p_mpls(mpls_proto)))
5716 return -EINVAL;
5717
5718 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5719 if (skb->encapsulation)
5720 return -EINVAL;
5721
5722 err = skb_cow_head(skb, MPLS_HLEN);
5723 if (unlikely(err))
5724 return err;
5725
5726 if (!skb->inner_protocol) {
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305727 skb_set_inner_network_header(skb, skb_network_offset(skb));
John Hurley8822e272019-07-07 15:01:54 +01005728 skb_set_inner_protocol(skb, skb->protocol);
5729 }
5730
5731 skb_push(skb, MPLS_HLEN);
5732 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005733 mac_len);
John Hurley8822e272019-07-07 15:01:54 +01005734 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005735 skb_set_network_header(skb, mac_len);
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305736 skb_reset_mac_len(skb);
John Hurley8822e272019-07-07 15:01:54 +01005737
5738 lse = mpls_hdr(skb);
5739 lse->label_stack_entry = mpls_lse;
5740 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5741
Guillaume Nault4296adc2020-10-02 21:53:08 +02005742 if (ethernet && mac_len >= ETH_HLEN)
John Hurley8822e272019-07-07 15:01:54 +01005743 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5744 skb->protocol = mpls_proto;
5745
5746 return 0;
5747}
5748EXPORT_SYMBOL_GPL(skb_mpls_push);
5749
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005750/**
John Hurleyed246ce2019-07-07 15:01:55 +01005751 * skb_mpls_pop() - pop the outermost MPLS header
5752 *
5753 * @skb: buffer
5754 * @next_proto: ethertype of header after popped MPLS header
Davide Carattifa4e0f82019-10-12 13:55:07 +02005755 * @mac_len: length of the MAC header
Martin Varghese76f99f92019-12-21 08:50:23 +05305756 * @ethernet: flag to indicate if the packet is ethernet
John Hurleyed246ce2019-07-07 15:01:55 +01005757 *
5758 * Expects skb->data at mac header.
5759 *
5760 * Returns 0 on success, -errno otherwise.
5761 */
Martin Varghese040b5cf2019-12-02 10:49:51 +05305762int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5763 bool ethernet)
John Hurleyed246ce2019-07-07 15:01:55 +01005764{
5765 int err;
5766
5767 if (unlikely(!eth_p_mpls(skb->protocol)))
Davide Carattidedc5a02019-10-12 13:55:06 +02005768 return 0;
John Hurleyed246ce2019-07-07 15:01:55 +01005769
Davide Carattifa4e0f82019-10-12 13:55:07 +02005770 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
John Hurleyed246ce2019-07-07 15:01:55 +01005771 if (unlikely(err))
5772 return err;
5773
5774 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5775 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005776 mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005777
5778 __skb_pull(skb, MPLS_HLEN);
5779 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005780 skb_set_network_header(skb, mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005781
Guillaume Nault4296adc2020-10-02 21:53:08 +02005782 if (ethernet && mac_len >= ETH_HLEN) {
John Hurleyed246ce2019-07-07 15:01:55 +01005783 struct ethhdr *hdr;
5784
5785 /* use mpls_hdr() to get ethertype to account for VLANs. */
5786 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5787 skb_mod_eth_type(skb, hdr, next_proto);
5788 }
5789 skb->protocol = next_proto;
5790
5791 return 0;
5792}
5793EXPORT_SYMBOL_GPL(skb_mpls_pop);
5794
5795/**
John Hurleyd27cf5c2019-07-07 15:01:56 +01005796 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5797 *
5798 * @skb: buffer
5799 * @mpls_lse: new MPLS label stack entry to update to
5800 *
5801 * Expects skb->data at mac header.
5802 *
5803 * Returns 0 on success, -errno otherwise.
5804 */
5805int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5806{
5807 int err;
5808
5809 if (unlikely(!eth_p_mpls(skb->protocol)))
5810 return -EINVAL;
5811
5812 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5813 if (unlikely(err))
5814 return err;
5815
5816 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5817 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5818
5819 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5820 }
5821
5822 mpls_hdr(skb)->label_stack_entry = mpls_lse;
5823
5824 return 0;
5825}
5826EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5827
5828/**
John Hurley2a2ea502019-07-07 15:01:57 +01005829 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
5830 *
5831 * @skb: buffer
5832 *
5833 * Expects skb->data at mac header.
5834 *
5835 * Returns 0 on success, -errno otherwise.
5836 */
5837int skb_mpls_dec_ttl(struct sk_buff *skb)
5838{
5839 u32 lse;
5840 u8 ttl;
5841
5842 if (unlikely(!eth_p_mpls(skb->protocol)))
5843 return -EINVAL;
5844
Davide Caratti13de4ed2020-12-03 10:58:21 +01005845 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
5846 return -ENOMEM;
5847
John Hurley2a2ea502019-07-07 15:01:57 +01005848 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
5849 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
5850 if (!--ttl)
5851 return -EINVAL;
5852
5853 lse &= ~MPLS_LS_TTL_MASK;
5854 lse |= ttl << MPLS_LS_TTL_SHIFT;
5855
5856 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
5857}
5858EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
5859
5860/**
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005861 * alloc_skb_with_frags - allocate skb with page frags
5862 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005863 * @header_len: size of linear part
5864 * @data_len: needed length in frags
5865 * @max_page_order: max page order desired.
5866 * @errcode: pointer to error code if any
5867 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005868 *
5869 * This can be used to allocate a paged skb, given a maximal order for frags.
5870 */
5871struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5872 unsigned long data_len,
5873 int max_page_order,
5874 int *errcode,
5875 gfp_t gfp_mask)
5876{
5877 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5878 unsigned long chunk;
5879 struct sk_buff *skb;
5880 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005881 int i;
5882
5883 *errcode = -EMSGSIZE;
5884 /* Note this test could be relaxed, if we succeed to allocate
5885 * high order pages...
5886 */
5887 if (npages > MAX_SKB_FRAGS)
5888 return NULL;
5889
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005890 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005891 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005892 if (!skb)
5893 return NULL;
5894
5895 skb->truesize += npages << PAGE_SHIFT;
5896
5897 for (i = 0; npages > 0; i++) {
5898 int order = max_page_order;
5899
5900 while (order) {
5901 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005902 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005903 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005904 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005905 order);
5906 if (page)
5907 goto fill_page;
5908 /* Do not retry other high order allocations */
5909 order = 1;
5910 max_page_order = 0;
5911 }
5912 order--;
5913 }
5914 page = alloc_page(gfp_mask);
5915 if (!page)
5916 goto failure;
5917fill_page:
5918 chunk = min_t(unsigned long, data_len,
5919 PAGE_SIZE << order);
5920 skb_fill_page_desc(skb, i, page, 0, chunk);
5921 data_len -= chunk;
5922 npages -= 1 << order;
5923 }
5924 return skb;
5925
5926failure:
5927 kfree_skb(skb);
5928 return NULL;
5929}
5930EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005931
5932/* carve out the first off bytes from skb when off < headlen */
5933static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5934 const int headlen, gfp_t gfp_mask)
5935{
5936 int i;
5937 int size = skb_end_offset(skb);
5938 int new_hlen = headlen - off;
5939 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005940
5941 size = SKB_DATA_ALIGN(size);
5942
5943 if (skb_pfmemalloc(skb))
5944 gfp_mask |= __GFP_MEMALLOC;
5945 data = kmalloc_reserve(size +
5946 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5947 gfp_mask, NUMA_NO_NODE, NULL);
5948 if (!data)
5949 return -ENOMEM;
5950
5951 size = SKB_WITH_OVERHEAD(ksize(data));
5952
5953 /* Copy real data, and all frags */
5954 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5955 skb->len -= off;
5956
5957 memcpy((struct skb_shared_info *)(data + size),
5958 skb_shinfo(skb),
5959 offsetof(struct skb_shared_info,
5960 frags[skb_shinfo(skb)->nr_frags]));
5961 if (skb_cloned(skb)) {
5962 /* drop the old head gracefully */
5963 if (skb_orphan_frags(skb, gfp_mask)) {
5964 kfree(data);
5965 return -ENOMEM;
5966 }
5967 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5968 skb_frag_ref(skb, i);
5969 if (skb_has_frag_list(skb))
5970 skb_clone_fraglist(skb);
5971 skb_release_data(skb);
5972 } else {
5973 /* we can reuse existing recount- all we did was
5974 * relocate values
5975 */
5976 skb_free_head(skb);
5977 }
5978
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005979 skb->head = data;
5980 skb->data = data;
5981 skb->head_frag = 0;
5982#ifdef NET_SKBUFF_DATA_USES_OFFSET
5983 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005984#else
5985 skb->end = skb->head + size;
5986#endif
5987 skb_set_tail_pointer(skb, skb_headlen(skb));
5988 skb_headers_offset_update(skb, 0);
5989 skb->cloned = 0;
5990 skb->hdr_len = 0;
5991 skb->nohdr = 0;
5992 atomic_set(&skb_shinfo(skb)->dataref, 1);
5993
5994 return 0;
5995}
5996
5997static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5998
5999/* carve out the first eat bytes from skb's frag_list. May recurse into
6000 * pskb_carve()
6001 */
6002static int pskb_carve_frag_list(struct sk_buff *skb,
6003 struct skb_shared_info *shinfo, int eat,
6004 gfp_t gfp_mask)
6005{
6006 struct sk_buff *list = shinfo->frag_list;
6007 struct sk_buff *clone = NULL;
6008 struct sk_buff *insp = NULL;
6009
6010 do {
6011 if (!list) {
6012 pr_err("Not enough bytes to eat. Want %d\n", eat);
6013 return -EFAULT;
6014 }
6015 if (list->len <= eat) {
6016 /* Eaten as whole. */
6017 eat -= list->len;
6018 list = list->next;
6019 insp = list;
6020 } else {
6021 /* Eaten partially. */
6022 if (skb_shared(list)) {
6023 clone = skb_clone(list, gfp_mask);
6024 if (!clone)
6025 return -ENOMEM;
6026 insp = list->next;
6027 list = clone;
6028 } else {
6029 /* This may be pulled without problems. */
6030 insp = list;
6031 }
6032 if (pskb_carve(list, eat, gfp_mask) < 0) {
6033 kfree_skb(clone);
6034 return -ENOMEM;
6035 }
6036 break;
6037 }
6038 } while (eat);
6039
6040 /* Free pulled out fragments. */
6041 while ((list = shinfo->frag_list) != insp) {
6042 shinfo->frag_list = list->next;
6043 kfree_skb(list);
6044 }
6045 /* And insert new clone at head. */
6046 if (clone) {
6047 clone->next = list;
6048 shinfo->frag_list = clone;
6049 }
6050 return 0;
6051}
6052
6053/* carve off first len bytes from skb. Split line (off) is in the
6054 * non-linear part of skb
6055 */
6056static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6057 int pos, gfp_t gfp_mask)
6058{
6059 int i, k = 0;
6060 int size = skb_end_offset(skb);
6061 u8 *data;
6062 const int nfrags = skb_shinfo(skb)->nr_frags;
6063 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006064
6065 size = SKB_DATA_ALIGN(size);
6066
6067 if (skb_pfmemalloc(skb))
6068 gfp_mask |= __GFP_MEMALLOC;
6069 data = kmalloc_reserve(size +
6070 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6071 gfp_mask, NUMA_NO_NODE, NULL);
6072 if (!data)
6073 return -ENOMEM;
6074
6075 size = SKB_WITH_OVERHEAD(ksize(data));
6076
6077 memcpy((struct skb_shared_info *)(data + size),
Miaohe Line3ec1e82020-08-15 04:48:53 -04006078 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006079 if (skb_orphan_frags(skb, gfp_mask)) {
6080 kfree(data);
6081 return -ENOMEM;
6082 }
6083 shinfo = (struct skb_shared_info *)(data + size);
6084 for (i = 0; i < nfrags; i++) {
6085 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6086
6087 if (pos + fsize > off) {
6088 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6089
6090 if (pos < off) {
6091 /* Split frag.
6092 * We have two variants in this case:
6093 * 1. Move all the frag to the second
6094 * part, if it is possible. F.e.
6095 * this approach is mandatory for TUX,
6096 * where splitting is expensive.
6097 * 2. Split is accurately. We make this.
6098 */
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006099 skb_frag_off_add(&shinfo->frags[0], off - pos);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006100 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6101 }
6102 skb_frag_ref(skb, i);
6103 k++;
6104 }
6105 pos += fsize;
6106 }
6107 shinfo->nr_frags = k;
6108 if (skb_has_frag_list(skb))
6109 skb_clone_fraglist(skb);
6110
Miaohe Lineabe8612020-08-15 04:46:41 -04006111 /* split line is in frag list */
6112 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6113 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6114 if (skb_has_frag_list(skb))
6115 kfree_skb_list(skb_shinfo(skb)->frag_list);
6116 kfree(data);
6117 return -ENOMEM;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006118 }
6119 skb_release_data(skb);
6120
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006121 skb->head = data;
6122 skb->head_frag = 0;
6123 skb->data = data;
6124#ifdef NET_SKBUFF_DATA_USES_OFFSET
6125 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006126#else
6127 skb->end = skb->head + size;
6128#endif
6129 skb_reset_tail_pointer(skb);
6130 skb_headers_offset_update(skb, 0);
6131 skb->cloned = 0;
6132 skb->hdr_len = 0;
6133 skb->nohdr = 0;
6134 skb->len -= off;
6135 skb->data_len = skb->len;
6136 atomic_set(&skb_shinfo(skb)->dataref, 1);
6137 return 0;
6138}
6139
6140/* remove len bytes from the beginning of the skb */
6141static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6142{
6143 int headlen = skb_headlen(skb);
6144
6145 if (len < headlen)
6146 return pskb_carve_inside_header(skb, len, headlen, gfp);
6147 else
6148 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6149}
6150
6151/* Extract to_copy bytes starting at off from skb, and return this in
6152 * a new skb
6153 */
6154struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6155 int to_copy, gfp_t gfp)
6156{
6157 struct sk_buff *clone = skb_clone(skb, gfp);
6158
6159 if (!clone)
6160 return NULL;
6161
6162 if (pskb_carve(clone, off, gfp) < 0 ||
6163 pskb_trim(clone, to_copy)) {
6164 kfree_skb(clone);
6165 return NULL;
6166 }
6167 return clone;
6168}
6169EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006170
6171/**
6172 * skb_condense - try to get rid of fragments/frag_list if possible
6173 * @skb: buffer
6174 *
6175 * Can be used to save memory before skb is added to a busy queue.
6176 * If packet has bytes in frags and enough tail room in skb->head,
6177 * pull all of them, so that we can free the frags right now and adjust
6178 * truesize.
6179 * Notes:
6180 * We do not reallocate skb->head thus can not fail.
6181 * Caller must re-evaluate skb->truesize if needed.
6182 */
6183void skb_condense(struct sk_buff *skb)
6184{
Eric Dumazet3174fed2016-12-09 08:02:05 -08006185 if (skb->data_len) {
6186 if (skb->data_len > skb->end - skb->tail ||
6187 skb_cloned(skb))
6188 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006189
Eric Dumazet3174fed2016-12-09 08:02:05 -08006190 /* Nice, we can free page frag(s) right now */
6191 __pskb_pull_tail(skb, skb->data_len);
6192 }
6193 /* At this point, skb->truesize might be over estimated,
6194 * because skb had a fragment, and fragments do not tell
6195 * their truesize.
6196 * When we pulled its content into skb->head, fragment
6197 * was freed, but __pskb_pull_tail() could not possibly
6198 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006199 */
6200 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6201}
Florian Westphaldf5042f2018-12-18 17:15:16 +01006202
6203#ifdef CONFIG_SKB_EXTENSIONS
6204static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6205{
6206 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6207}
6208
Paolo Abeni8b69a802020-01-09 07:59:24 -08006209/**
6210 * __skb_ext_alloc - allocate a new skb extensions storage
6211 *
Florian Westphal4930f482020-05-16 10:46:23 +02006212 * @flags: See kmalloc().
6213 *
Paolo Abeni8b69a802020-01-09 07:59:24 -08006214 * Returns the newly allocated pointer. The pointer can later attached to a
6215 * skb via __skb_ext_set().
6216 * Note: caller must handle the skb_ext as an opaque data.
6217 */
Florian Westphal4930f482020-05-16 10:46:23 +02006218struct skb_ext *__skb_ext_alloc(gfp_t flags)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006219{
Florian Westphal4930f482020-05-16 10:46:23 +02006220 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006221
6222 if (new) {
6223 memset(new->offset, 0, sizeof(new->offset));
6224 refcount_set(&new->refcnt, 1);
6225 }
6226
6227 return new;
6228}
6229
Florian Westphal41650792018-12-18 17:15:27 +01006230static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6231 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006232{
6233 struct skb_ext *new;
6234
6235 if (refcount_read(&old->refcnt) == 1)
6236 return old;
6237
6238 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6239 if (!new)
6240 return NULL;
6241
6242 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6243 refcount_set(&new->refcnt, 1);
6244
Florian Westphal41650792018-12-18 17:15:27 +01006245#ifdef CONFIG_XFRM
6246 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6247 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6248 unsigned int i;
6249
6250 for (i = 0; i < sp->len; i++)
6251 xfrm_state_hold(sp->xvec[i]);
6252 }
6253#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006254 __skb_ext_put(old);
6255 return new;
6256}
6257
6258/**
Paolo Abeni8b69a802020-01-09 07:59:24 -08006259 * __skb_ext_set - attach the specified extension storage to this skb
6260 * @skb: buffer
6261 * @id: extension id
6262 * @ext: extension storage previously allocated via __skb_ext_alloc()
6263 *
6264 * Existing extensions, if any, are cleared.
6265 *
6266 * Returns the pointer to the extension.
6267 */
6268void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6269 struct skb_ext *ext)
6270{
6271 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6272
6273 skb_ext_put(skb);
6274 newlen = newoff + skb_ext_type_len[id];
6275 ext->chunks = newlen;
6276 ext->offset[id] = newoff;
6277 skb->extensions = ext;
6278 skb->active_extensions = 1 << id;
6279 return skb_ext_get_ptr(ext, id);
6280}
6281
6282/**
Florian Westphaldf5042f2018-12-18 17:15:16 +01006283 * skb_ext_add - allocate space for given extension, COW if needed
6284 * @skb: buffer
6285 * @id: extension to allocate space for
6286 *
6287 * Allocates enough space for the given extension.
6288 * If the extension is already present, a pointer to that extension
6289 * is returned.
6290 *
6291 * If the skb was cloned, COW applies and the returned memory can be
6292 * modified without changing the extension space of clones buffers.
6293 *
6294 * Returns pointer to the extension or NULL on allocation failure.
6295 */
6296void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6297{
6298 struct skb_ext *new, *old = NULL;
6299 unsigned int newlen, newoff;
6300
6301 if (skb->active_extensions) {
6302 old = skb->extensions;
6303
Florian Westphal41650792018-12-18 17:15:27 +01006304 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006305 if (!new)
6306 return NULL;
6307
Paolo Abeni682ec852018-12-21 19:03:15 +01006308 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01006309 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006310
Paolo Abenie94e50b2018-12-21 19:03:13 +01006311 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006312 } else {
6313 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6314
Florian Westphal4930f482020-05-16 10:46:23 +02006315 new = __skb_ext_alloc(GFP_ATOMIC);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006316 if (!new)
6317 return NULL;
6318 }
6319
6320 newlen = newoff + skb_ext_type_len[id];
6321 new->chunks = newlen;
6322 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006323set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01006324 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006325 skb->active_extensions |= 1 << id;
6326 return skb_ext_get_ptr(new, id);
6327}
6328EXPORT_SYMBOL(skb_ext_add);
6329
Florian Westphal41650792018-12-18 17:15:27 +01006330#ifdef CONFIG_XFRM
6331static void skb_ext_put_sp(struct sec_path *sp)
6332{
6333 unsigned int i;
6334
6335 for (i = 0; i < sp->len; i++)
6336 xfrm_state_put(sp->xvec[i]);
6337}
6338#endif
6339
Florian Westphaldf5042f2018-12-18 17:15:16 +01006340void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6341{
6342 struct skb_ext *ext = skb->extensions;
6343
6344 skb->active_extensions &= ~(1 << id);
6345 if (skb->active_extensions == 0) {
6346 skb->extensions = NULL;
6347 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01006348#ifdef CONFIG_XFRM
6349 } else if (id == SKB_EXT_SEC_PATH &&
6350 refcount_read(&ext->refcnt) == 1) {
6351 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6352
6353 skb_ext_put_sp(sp);
6354 sp->len = 0;
6355#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006356 }
6357}
6358EXPORT_SYMBOL(__skb_ext_del);
6359
6360void __skb_ext_put(struct skb_ext *ext)
6361{
6362 /* If this is last clone, nothing can increment
6363 * it after check passes. Avoids one atomic op.
6364 */
6365 if (refcount_read(&ext->refcnt) == 1)
6366 goto free_now;
6367
6368 if (!refcount_dec_and_test(&ext->refcnt))
6369 return;
6370free_now:
Florian Westphal41650792018-12-18 17:15:27 +01006371#ifdef CONFIG_XFRM
6372 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6373 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6374#endif
6375
Florian Westphaldf5042f2018-12-18 17:15:16 +01006376 kmem_cache_free(skbuff_ext_cache, ext);
6377}
6378EXPORT_SYMBOL(__skb_ext_put);
6379#endif /* CONFIG_SKB_EXTENSIONS */