xref: /openbmc/linux/net/core/skbuff.c (revision 4025d3e7)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
41da177e4SLinus Torvalds  *
5113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
61da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Fixes:
91da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
101da177e4SLinus Torvalds  *					balancer bugs.
111da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
121da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
131da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
141da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
151da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
171da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
181da177e4SLinus Torvalds  *					only put in the headers
191da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
201da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
211da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
221da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *	NOTE:
251da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
261da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
271da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
281da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
35e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36e005d193SJoe Perches 
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/types.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/mm.h>
411da177e4SLinus Torvalds #include <linux/interrupt.h>
421da177e4SLinus Torvalds #include <linux/in.h>
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/slab.h>
45de960aa9SFlorian Westphal #include <linux/tcp.h>
46de960aa9SFlorian Westphal #include <linux/udp.h>
4790017accSMarcelo Ricardo Leitner #include <linux/sctp.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
501da177e4SLinus Torvalds #include <net/pkt_sched.h>
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds #include <linux/string.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
549c55e01cSJens Axboe #include <linux/splice.h>
551da177e4SLinus Torvalds #include <linux/cache.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <linux/init.h>
58716ea3a7SDavid Howells #include <linux/scatterlist.h>
59ac45f602SPatrick Ohly #include <linux/errqueue.h>
60268bb0ceSLinus Torvalds #include <linux/prefetch.h>
61071c0fc6SJohannes Berg #include <linux/bitfield.h>
620d5501c1SVlad Yasevich #include <linux/if_vlan.h>
632a2ea508SJohn Hurley #include <linux/mpls.h>
64183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <net/protocol.h>
671da177e4SLinus Torvalds #include <net/dst.h>
681da177e4SLinus Torvalds #include <net/sock.h>
691da177e4SLinus Torvalds #include <net/checksum.h>
70d457a0e3SEric Dumazet #include <net/gso.h>
71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
721da177e4SLinus Torvalds #include <net/xfrm.h>
738822e270SJohn Hurley #include <net/mpls.h>
743ee17bc7SMat Martineau #include <net/mptcp.h>
7578476d31SJeremy Kerr #include <net/mctp.h>
7675eaf63eSAlexander Lobakin #include <net/page_pool/helpers.h>
77071c0fc6SJohannes Berg #include <net/dropreason.h>
781da177e4SLinus Torvalds 
797c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
80ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
8151c56b00SEric Dumazet #include <linux/highmem.h>
82b245be1fSWillem de Bruijn #include <linux/capability.h>
83b245be1fSWillem de Bruijn #include <linux/user_namespace.h>
842544af03SMatteo Croce #include <linux/indirect_call_wrapper.h>
852195e2a0SJakub Kicinski #include <linux/textsearch.h>
86a1f8e7f7SAl Viro 
8739564c3fSEric Dumazet #include "dev.h"
887f678defSVasily Averin #include "sock_destructor.h"
897b7ed885SBart Van Assche 
90025a785fSJakub Kicinski struct kmem_cache *skbuff_cache __ro_after_init;
9108009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
92df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
93df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init;
94df5042f4SFlorian Westphal #endif
95bf9f1baaSEric Dumazet 
96bf9f1baaSEric Dumazet 
97bf9f1baaSEric Dumazet static struct kmem_cache *skb_small_head_cache __ro_after_init;
98bf9f1baaSEric Dumazet 
99bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER)
100bf9f1baaSEric Dumazet 
101bf9f1baaSEric Dumazet /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two.
102bf9f1baaSEric Dumazet  * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique
103bf9f1baaSEric Dumazet  * size, and we can differentiate heads from skb_small_head_cache
104bf9f1baaSEric Dumazet  * vs system slabs by looking at their size (skb_end_offset()).
105bf9f1baaSEric Dumazet  */
106bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_CACHE_SIZE					\
107bf9f1baaSEric Dumazet 	(is_power_of_2(SKB_SMALL_HEAD_SIZE) ?			\
108bf9f1baaSEric Dumazet 		(SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) :	\
109bf9f1baaSEric Dumazet 		SKB_SMALL_HEAD_SIZE)
110bf9f1baaSEric Dumazet 
111bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_HEADROOM						\
112bf9f1baaSEric Dumazet 	SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)
113bf9f1baaSEric Dumazet 
1145f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
1155f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags);
1161da177e4SLinus Torvalds 
1179cb252c4SMenglong Dong #undef FN
1189cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
119071c0fc6SJohannes Berg static const char * const drop_reasons[] = {
1200e84afe8SEric Dumazet 	[SKB_CONSUMED] = "CONSUMED",
1219cb252c4SMenglong Dong 	DEFINE_DROP_REASON(FN, FN)
1229cb252c4SMenglong Dong };
123071c0fc6SJohannes Berg 
124071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = {
125071c0fc6SJohannes Berg 	.reasons = drop_reasons,
126071c0fc6SJohannes Berg 	.n_reasons = ARRAY_SIZE(drop_reasons),
127071c0fc6SJohannes Berg };
128071c0fc6SJohannes Berg 
129071c0fc6SJohannes Berg const struct drop_reason_list __rcu *
130071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = {
131071c0fc6SJohannes Berg 	[SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core),
132071c0fc6SJohannes Berg };
133071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys);
134071c0fc6SJohannes Berg 
135071c0fc6SJohannes Berg /**
136071c0fc6SJohannes Berg  * drop_reasons_register_subsys - register another drop reason subsystem
137071c0fc6SJohannes Berg  * @subsys: the subsystem to register, must not be the core
138071c0fc6SJohannes Berg  * @list: the list of drop reasons within the subsystem, must point to
139071c0fc6SJohannes Berg  *	a statically initialized list
140071c0fc6SJohannes Berg  */
141071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,
142071c0fc6SJohannes Berg 				  const struct drop_reason_list *list)
143071c0fc6SJohannes Berg {
144071c0fc6SJohannes Berg 	if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
145071c0fc6SJohannes Berg 		 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
146071c0fc6SJohannes Berg 		 "invalid subsystem %d\n", subsys))
147071c0fc6SJohannes Berg 		return;
148071c0fc6SJohannes Berg 
149071c0fc6SJohannes Berg 	/* must point to statically allocated memory, so INIT is OK */
150071c0fc6SJohannes Berg 	RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list);
151071c0fc6SJohannes Berg }
152071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys);
153071c0fc6SJohannes Berg 
154071c0fc6SJohannes Berg /**
155071c0fc6SJohannes Berg  * drop_reasons_unregister_subsys - unregister a drop reason subsystem
156071c0fc6SJohannes Berg  * @subsys: the subsystem to remove, must not be the core
157071c0fc6SJohannes Berg  *
158071c0fc6SJohannes Berg  * Note: This will synchronize_rcu() to ensure no users when it returns.
159071c0fc6SJohannes Berg  */
160071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)
161071c0fc6SJohannes Berg {
162071c0fc6SJohannes Berg 	if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
163071c0fc6SJohannes Berg 		 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
164071c0fc6SJohannes Berg 		 "invalid subsystem %d\n", subsys))
165071c0fc6SJohannes Berg 		return;
166071c0fc6SJohannes Berg 
167071c0fc6SJohannes Berg 	RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL);
168071c0fc6SJohannes Berg 
169071c0fc6SJohannes Berg 	synchronize_rcu();
170071c0fc6SJohannes Berg }
171071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys);
172ec43908dSMenglong Dong 
1731da177e4SLinus Torvalds /**
174f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
1751da177e4SLinus Torvalds  *	@skb:	buffer
1761da177e4SLinus Torvalds  *	@sz:	size
177f05de73bSJean Sacren  *	@addr:	address
17899d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
1791da177e4SLinus Torvalds  *
180f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
181f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
182f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
183f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
1841da177e4SLinus Torvalds  */
185f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
18699d5851eSJames Hogan 		      const char msg[])
1871da177e4SLinus Torvalds {
18841a46913SJesper Dangaard Brouer 	pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
18999d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
1904305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
19126095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
1921da177e4SLinus Torvalds 	BUG();
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
195f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1961da177e4SLinus Torvalds {
197f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
201f05de73bSJean Sacren {
202f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
203f05de73bSJean Sacren }
204c93bdd0eSMel Gorman 
20550fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE	64
206f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK	16
207f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF	(NAPI_SKB_CACHE_SIZE / 2)
20850fad4b5SAlexander Lobakin 
209dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K
210dbae2b06SPaolo Abeni 
211dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG	1
212dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	((nc).pfmemalloc)
213dbae2b06SPaolo Abeni 
214dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page
215dbae2b06SPaolo Abeni  * and slicing it into 1K sized fragment. Constrained to systems
216dbae2b06SPaolo Abeni  * with a very limited amount of 1K fragments fitting a single
217dbae2b06SPaolo Abeni  * page - to avoid excessive truesize underestimation
218dbae2b06SPaolo Abeni  */
219dbae2b06SPaolo Abeni 
220dbae2b06SPaolo Abeni struct page_frag_1k {
221dbae2b06SPaolo Abeni 	void *va;
222dbae2b06SPaolo Abeni 	u16 offset;
223dbae2b06SPaolo Abeni 	bool pfmemalloc;
224dbae2b06SPaolo Abeni };
225dbae2b06SPaolo Abeni 
226dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
227dbae2b06SPaolo Abeni {
228dbae2b06SPaolo Abeni 	struct page *page;
229dbae2b06SPaolo Abeni 	int offset;
230dbae2b06SPaolo Abeni 
231dbae2b06SPaolo Abeni 	offset = nc->offset - SZ_1K;
232dbae2b06SPaolo Abeni 	if (likely(offset >= 0))
233dbae2b06SPaolo Abeni 		goto use_frag;
234dbae2b06SPaolo Abeni 
235dbae2b06SPaolo Abeni 	page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
236dbae2b06SPaolo Abeni 	if (!page)
237dbae2b06SPaolo Abeni 		return NULL;
238dbae2b06SPaolo Abeni 
239dbae2b06SPaolo Abeni 	nc->va = page_address(page);
240dbae2b06SPaolo Abeni 	nc->pfmemalloc = page_is_pfmemalloc(page);
241dbae2b06SPaolo Abeni 	offset = PAGE_SIZE - SZ_1K;
242dbae2b06SPaolo Abeni 	page_ref_add(page, offset / SZ_1K);
243dbae2b06SPaolo Abeni 
244dbae2b06SPaolo Abeni use_frag:
245dbae2b06SPaolo Abeni 	nc->offset = offset;
246dbae2b06SPaolo Abeni 	return nc->va + offset;
247dbae2b06SPaolo Abeni }
248dbae2b06SPaolo Abeni #else
249dbae2b06SPaolo Abeni 
250dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers
251dbae2b06SPaolo Abeni  * to please the compiler and avoid later preprocessor's conditionals
252dbae2b06SPaolo Abeni  */
253dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG	0
254dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	false
255dbae2b06SPaolo Abeni 
256dbae2b06SPaolo Abeni struct page_frag_1k {
257dbae2b06SPaolo Abeni };
258dbae2b06SPaolo Abeni 
259dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
260dbae2b06SPaolo Abeni {
261dbae2b06SPaolo Abeni 	return NULL;
262dbae2b06SPaolo Abeni }
263dbae2b06SPaolo Abeni 
264dbae2b06SPaolo Abeni #endif
265dbae2b06SPaolo Abeni 
26650fad4b5SAlexander Lobakin struct napi_alloc_cache {
26750fad4b5SAlexander Lobakin 	struct page_frag_cache page;
268dbae2b06SPaolo Abeni 	struct page_frag_1k page_small;
26950fad4b5SAlexander Lobakin 	unsigned int skb_count;
27050fad4b5SAlexander Lobakin 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
27150fad4b5SAlexander Lobakin };
27250fad4b5SAlexander Lobakin 
27350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
27450fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
27550fad4b5SAlexander Lobakin 
276dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with
277dbae2b06SPaolo Abeni  * skb->head being backed by slab, not a page fragment.
278dbae2b06SPaolo Abeni  * This is to make sure bug fixed in 3226b158e67c
279dbae2b06SPaolo Abeni  * ("net: avoid 32 x truesize under-estimation for tiny skbs")
280dbae2b06SPaolo Abeni  * does not accidentally come back.
281dbae2b06SPaolo Abeni  */
282dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi)
283dbae2b06SPaolo Abeni {
284dbae2b06SPaolo Abeni 	struct sk_buff *skb;
285dbae2b06SPaolo Abeni 
286dbae2b06SPaolo Abeni 	local_bh_disable();
287dbae2b06SPaolo Abeni 	skb = napi_get_frags(napi);
288dbae2b06SPaolo Abeni 	WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
289dbae2b06SPaolo Abeni 	napi_free_frags(napi);
290dbae2b06SPaolo Abeni 	local_bh_enable();
291dbae2b06SPaolo Abeni }
292dbae2b06SPaolo Abeni 
29332e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
29450fad4b5SAlexander Lobakin {
29550fad4b5SAlexander Lobakin 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
29650fad4b5SAlexander Lobakin 
29750fad4b5SAlexander Lobakin 	fragsz = SKB_DATA_ALIGN(fragsz);
29850fad4b5SAlexander Lobakin 
29932e3573fSYajun Deng 	return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
30050fad4b5SAlexander Lobakin }
30150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align);
30250fad4b5SAlexander Lobakin 
30350fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
30450fad4b5SAlexander Lobakin {
30550fad4b5SAlexander Lobakin 	void *data;
30650fad4b5SAlexander Lobakin 
30750fad4b5SAlexander Lobakin 	fragsz = SKB_DATA_ALIGN(fragsz);
308afa79d08SChangbin Du 	if (in_hardirq() || irqs_disabled()) {
30932e3573fSYajun Deng 		struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
31032e3573fSYajun Deng 
31150fad4b5SAlexander Lobakin 		data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
31250fad4b5SAlexander Lobakin 	} else {
31332e3573fSYajun Deng 		struct napi_alloc_cache *nc;
31432e3573fSYajun Deng 
31550fad4b5SAlexander Lobakin 		local_bh_disable();
31632e3573fSYajun Deng 		nc = this_cpu_ptr(&napi_alloc_cache);
31732e3573fSYajun Deng 		data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
31850fad4b5SAlexander Lobakin 		local_bh_enable();
31950fad4b5SAlexander Lobakin 	}
32050fad4b5SAlexander Lobakin 	return data;
32150fad4b5SAlexander Lobakin }
32250fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align);
32350fad4b5SAlexander Lobakin 
324f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void)
325f450d539SAlexander Lobakin {
326f450d539SAlexander Lobakin 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
327f450d539SAlexander Lobakin 	struct sk_buff *skb;
328f450d539SAlexander Lobakin 
32949ae83fcSSieng Piaw Liew 	if (unlikely(!nc->skb_count)) {
330025a785fSJakub Kicinski 		nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache,
331f450d539SAlexander Lobakin 						      GFP_ATOMIC,
332f450d539SAlexander Lobakin 						      NAPI_SKB_CACHE_BULK,
333f450d539SAlexander Lobakin 						      nc->skb_cache);
334f450d539SAlexander Lobakin 		if (unlikely(!nc->skb_count))
335f450d539SAlexander Lobakin 			return NULL;
33649ae83fcSSieng Piaw Liew 	}
337f450d539SAlexander Lobakin 
338f450d539SAlexander Lobakin 	skb = nc->skb_cache[--nc->skb_count];
339025a785fSJakub Kicinski 	kasan_unpoison_object_data(skbuff_cache, skb);
340f450d539SAlexander Lobakin 
341f450d539SAlexander Lobakin 	return skb;
342f450d539SAlexander Lobakin }
343f450d539SAlexander Lobakin 
344ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
345ce098da1SKees Cook 					 unsigned int size)
346ba0509b6SJesper Dangaard Brouer {
347ba0509b6SJesper Dangaard Brouer 	struct skb_shared_info *shinfo;
348ba0509b6SJesper Dangaard Brouer 
349ba0509b6SJesper Dangaard Brouer 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
350ba0509b6SJesper Dangaard Brouer 
351ba0509b6SJesper Dangaard Brouer 	/* Assumes caller memset cleared SKB */
352ba0509b6SJesper Dangaard Brouer 	skb->truesize = SKB_TRUESIZE(size);
353ba0509b6SJesper Dangaard Brouer 	refcount_set(&skb->users, 1);
354ba0509b6SJesper Dangaard Brouer 	skb->head = data;
355ba0509b6SJesper Dangaard Brouer 	skb->data = data;
356ba0509b6SJesper Dangaard Brouer 	skb_reset_tail_pointer(skb);
357763087daSEric Dumazet 	skb_set_end_offset(skb, size);
358ba0509b6SJesper Dangaard Brouer 	skb->mac_header = (typeof(skb->mac_header))~0U;
359ba0509b6SJesper Dangaard Brouer 	skb->transport_header = (typeof(skb->transport_header))~0U;
36068822bdfSEric Dumazet 	skb->alloc_cpu = raw_smp_processor_id();
361ba0509b6SJesper Dangaard Brouer 	/* make sure we initialize shinfo sequentially */
362ba0509b6SJesper Dangaard Brouer 	shinfo = skb_shinfo(skb);
363ba0509b6SJesper Dangaard Brouer 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
364ba0509b6SJesper Dangaard Brouer 	atomic_set(&shinfo->dataref, 1);
365ba0509b6SJesper Dangaard Brouer 
3666370cc3bSAleksandr Nogikh 	skb_set_kcov_handle(skb, kcov_common_handle());
367ba0509b6SJesper Dangaard Brouer }
368ba0509b6SJesper Dangaard Brouer 
369ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
370ce098da1SKees Cook 				     unsigned int *size)
371ce098da1SKees Cook {
372ce098da1SKees Cook 	void *resized;
373ce098da1SKees Cook 
374ce098da1SKees Cook 	/* Must find the allocation size (and grow it to match). */
375ce098da1SKees Cook 	*size = ksize(data);
376ce098da1SKees Cook 	/* krealloc() will immediately return "data" when
377ce098da1SKees Cook 	 * "ksize(data)" is requested: it is the existing upper
378ce098da1SKees Cook 	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
379ce098da1SKees Cook 	 * that this "new" pointer needs to be passed back to the
380ce098da1SKees Cook 	 * caller for use so the __alloc_size hinting will be
381ce098da1SKees Cook 	 * tracked correctly.
382ce098da1SKees Cook 	 */
383ce098da1SKees Cook 	resized = krealloc(data, *size, GFP_ATOMIC);
384ce098da1SKees Cook 	WARN_ON_ONCE(resized != data);
385ce098da1SKees Cook 	return resized;
386ce098da1SKees Cook }
387ce098da1SKees Cook 
388ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers.
389ce098da1SKees Cook  * Note that this should be used sparingly as slab buffers
390ce098da1SKees Cook  * cannot be combined efficiently by GRO!
391ce098da1SKees Cook  */
392ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data)
393ce098da1SKees Cook {
394ce098da1SKees Cook 	struct sk_buff *skb;
395ce098da1SKees Cook 	unsigned int size;
396ce098da1SKees Cook 
397025a785fSJakub Kicinski 	skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC);
398ce098da1SKees Cook 	if (unlikely(!skb))
399ce098da1SKees Cook 		return NULL;
400ce098da1SKees Cook 
401ce098da1SKees Cook 	memset(skb, 0, offsetof(struct sk_buff, tail));
402ce098da1SKees Cook 	data = __slab_build_skb(skb, data, &size);
403ce098da1SKees Cook 	__finalize_skb_around(skb, data, size);
404ce098da1SKees Cook 
405ce098da1SKees Cook 	return skb;
406ce098da1SKees Cook }
407ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb);
408ce098da1SKees Cook 
409ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */
410ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data,
411ce098da1SKees Cook 			       unsigned int frag_size)
412ce098da1SKees Cook {
413ce098da1SKees Cook 	unsigned int size = frag_size;
414ce098da1SKees Cook 
415ce098da1SKees Cook 	/* frag_size == 0 is considered deprecated now. Callers
416ce098da1SKees Cook 	 * using slab buffer should use slab_build_skb() instead.
417ce098da1SKees Cook 	 */
418ce098da1SKees Cook 	if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
419ce098da1SKees Cook 		data = __slab_build_skb(skb, data, &size);
420ce098da1SKees Cook 
421ce098da1SKees Cook 	__finalize_skb_around(skb, data, size);
422ce098da1SKees Cook }
423ce098da1SKees Cook 
4241da177e4SLinus Torvalds /**
4252ea2f62cSEric Dumazet  * __build_skb - build a network buffer
426b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
427ce098da1SKees Cook  * @frag_size: size of data (must not be 0)
428b2b5ce9dSEric Dumazet  *
429b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
430ce098da1SKees Cook  * skb_shared_info. @data must have been allocated from the page
431ce098da1SKees Cook  * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
432ce098da1SKees Cook  * allocation is deprecated, and callers should use slab_build_skb()
433ce098da1SKees Cook  * instead.)
434b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
435b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
436b2b5ce9dSEric Dumazet  * Notes :
437b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
438b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
439b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
440b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
441b2b5ce9dSEric Dumazet  *  before giving packet to stack.
442b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
443b2b5ce9dSEric Dumazet  */
4442ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size)
445b2b5ce9dSEric Dumazet {
446b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
447b2b5ce9dSEric Dumazet 
448025a785fSJakub Kicinski 	skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC);
449ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
450b2b5ce9dSEric Dumazet 		return NULL;
451b2b5ce9dSEric Dumazet 
452b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
453483126b3SAlexander Lobakin 	__build_skb_around(skb, data, frag_size);
454b2b5ce9dSEric Dumazet 
455483126b3SAlexander Lobakin 	return skb;
456b2b5ce9dSEric Dumazet }
4572ea2f62cSEric Dumazet 
4582ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically
4592ea2f62cSEric Dumazet  * takes care of skb->head and skb->pfmemalloc
4602ea2f62cSEric Dumazet  */
4612ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
4622ea2f62cSEric Dumazet {
4632ea2f62cSEric Dumazet 	struct sk_buff *skb = __build_skb(data, frag_size);
4642ea2f62cSEric Dumazet 
4653c640126SGal Pressman 	if (likely(skb && frag_size)) {
4662ea2f62cSEric Dumazet 		skb->head_frag = 1;
467566b6701SGal Pressman 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
4682ea2f62cSEric Dumazet 	}
4692ea2f62cSEric Dumazet 	return skb;
4702ea2f62cSEric Dumazet }
471b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
472b2b5ce9dSEric Dumazet 
473ba0509b6SJesper Dangaard Brouer /**
474ba0509b6SJesper Dangaard Brouer  * build_skb_around - build a network buffer around provided skb
475ba0509b6SJesper Dangaard Brouer  * @skb: sk_buff provide by caller, must be memset cleared
476ba0509b6SJesper Dangaard Brouer  * @data: data buffer provided by caller
47712c1604aSJakub Kicinski  * @frag_size: size of data
478ba0509b6SJesper Dangaard Brouer  */
479ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb,
480ba0509b6SJesper Dangaard Brouer 				 void *data, unsigned int frag_size)
481ba0509b6SJesper Dangaard Brouer {
482ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
483ba0509b6SJesper Dangaard Brouer 		return NULL;
484ba0509b6SJesper Dangaard Brouer 
485483126b3SAlexander Lobakin 	__build_skb_around(skb, data, frag_size);
486ba0509b6SJesper Dangaard Brouer 
487483126b3SAlexander Lobakin 	if (frag_size) {
488ba0509b6SJesper Dangaard Brouer 		skb->head_frag = 1;
489566b6701SGal Pressman 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
490ba0509b6SJesper Dangaard Brouer 	}
491ba0509b6SJesper Dangaard Brouer 	return skb;
492ba0509b6SJesper Dangaard Brouer }
493ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around);
494ba0509b6SJesper Dangaard Brouer 
495f450d539SAlexander Lobakin /**
496f450d539SAlexander Lobakin  * __napi_build_skb - build a network buffer
497f450d539SAlexander Lobakin  * @data: data buffer provided by caller
49812c1604aSJakub Kicinski  * @frag_size: size of data
499f450d539SAlexander Lobakin  *
500f450d539SAlexander Lobakin  * Version of __build_skb() that uses NAPI percpu caches to obtain
501f450d539SAlexander Lobakin  * skbuff_head instead of inplace allocation.
502f450d539SAlexander Lobakin  *
503f450d539SAlexander Lobakin  * Returns a new &sk_buff on success, %NULL on allocation failure.
504f450d539SAlexander Lobakin  */
505f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
506f450d539SAlexander Lobakin {
507f450d539SAlexander Lobakin 	struct sk_buff *skb;
508f450d539SAlexander Lobakin 
509f450d539SAlexander Lobakin 	skb = napi_skb_cache_get();
510f450d539SAlexander Lobakin 	if (unlikely(!skb))
511f450d539SAlexander Lobakin 		return NULL;
512f450d539SAlexander Lobakin 
513f450d539SAlexander Lobakin 	memset(skb, 0, offsetof(struct sk_buff, tail));
514f450d539SAlexander Lobakin 	__build_skb_around(skb, data, frag_size);
515f450d539SAlexander Lobakin 
516f450d539SAlexander Lobakin 	return skb;
517f450d539SAlexander Lobakin }
518f450d539SAlexander Lobakin 
519f450d539SAlexander Lobakin /**
520f450d539SAlexander Lobakin  * napi_build_skb - build a network buffer
521f450d539SAlexander Lobakin  * @data: data buffer provided by caller
52212c1604aSJakub Kicinski  * @frag_size: size of data
523f450d539SAlexander Lobakin  *
524f450d539SAlexander Lobakin  * Version of __napi_build_skb() that takes care of skb->head_frag
525f450d539SAlexander Lobakin  * and skb->pfmemalloc when the data is a page or page fragment.
526f450d539SAlexander Lobakin  *
527f450d539SAlexander Lobakin  * Returns a new &sk_buff on success, %NULL on allocation failure.
528f450d539SAlexander Lobakin  */
529f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
530f450d539SAlexander Lobakin {
531f450d539SAlexander Lobakin 	struct sk_buff *skb = __napi_build_skb(data, frag_size);
532f450d539SAlexander Lobakin 
533f450d539SAlexander Lobakin 	if (likely(skb) && frag_size) {
534f450d539SAlexander Lobakin 		skb->head_frag = 1;
535f450d539SAlexander Lobakin 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
536f450d539SAlexander Lobakin 	}
537f450d539SAlexander Lobakin 
538f450d539SAlexander Lobakin 	return skb;
539f450d539SAlexander Lobakin }
540f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb);
541f450d539SAlexander Lobakin 
5425381b23dSAlexander Lobakin /*
5435381b23dSAlexander Lobakin  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
5445381b23dSAlexander Lobakin  * the caller if emergency pfmemalloc reserves are being used. If it is and
5455381b23dSAlexander Lobakin  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
5465381b23dSAlexander Lobakin  * may be used. Otherwise, the packet data may be discarded until enough
5475381b23dSAlexander Lobakin  * memory is free
5485381b23dSAlexander Lobakin  */
5495c0e820cSEric Dumazet static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
550ef28095fSAlexander Lobakin 			     bool *pfmemalloc)
5515381b23dSAlexander Lobakin {
5525381b23dSAlexander Lobakin 	bool ret_pfmemalloc = false;
5535c0e820cSEric Dumazet 	unsigned int obj_size;
5545c0e820cSEric Dumazet 	void *obj;
5555381b23dSAlexander Lobakin 
5565c0e820cSEric Dumazet 	obj_size = SKB_HEAD_ALIGN(*size);
557bf9f1baaSEric Dumazet 	if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
558bf9f1baaSEric Dumazet 	    !(flags & KMALLOC_NOT_NORMAL_BITS)) {
559bf9f1baaSEric Dumazet 		obj = kmem_cache_alloc_node(skb_small_head_cache,
560bf9f1baaSEric Dumazet 				flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
561bf9f1baaSEric Dumazet 				node);
562bf9f1baaSEric Dumazet 		*size = SKB_SMALL_HEAD_CACHE_SIZE;
563880ce5f2SEric Dumazet 		if (obj || !(gfp_pfmemalloc_allowed(flags)))
564bf9f1baaSEric Dumazet 			goto out;
565880ce5f2SEric Dumazet 		/* Try again but now we are using pfmemalloc reserves */
566880ce5f2SEric Dumazet 		ret_pfmemalloc = true;
567880ce5f2SEric Dumazet 		obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node);
568880ce5f2SEric Dumazet 		goto out;
569bf9f1baaSEric Dumazet 	}
5705c0e820cSEric Dumazet 	*size = obj_size = kmalloc_size_roundup(obj_size);
5715381b23dSAlexander Lobakin 	/*
5725381b23dSAlexander Lobakin 	 * Try a regular allocation, when that fails and we're not entitled
5735381b23dSAlexander Lobakin 	 * to the reserves, fail.
5745381b23dSAlexander Lobakin 	 */
5755c0e820cSEric Dumazet 	obj = kmalloc_node_track_caller(obj_size,
5765381b23dSAlexander Lobakin 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
5775381b23dSAlexander Lobakin 					node);
5785381b23dSAlexander Lobakin 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
5795381b23dSAlexander Lobakin 		goto out;
5805381b23dSAlexander Lobakin 
5815381b23dSAlexander Lobakin 	/* Try again but now we are using pfmemalloc reserves */
5825381b23dSAlexander Lobakin 	ret_pfmemalloc = true;
5835c0e820cSEric Dumazet 	obj = kmalloc_node_track_caller(obj_size, flags, node);
5845381b23dSAlexander Lobakin 
5855381b23dSAlexander Lobakin out:
5865381b23dSAlexander Lobakin 	if (pfmemalloc)
5875381b23dSAlexander Lobakin 		*pfmemalloc = ret_pfmemalloc;
5885381b23dSAlexander Lobakin 
5895381b23dSAlexander Lobakin 	return obj;
5905381b23dSAlexander Lobakin }
5915381b23dSAlexander Lobakin 
5925381b23dSAlexander Lobakin /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
5935381b23dSAlexander Lobakin  *	'private' fields and also do memory statistics to find all the
5945381b23dSAlexander Lobakin  *	[BEEP] leaks.
5955381b23dSAlexander Lobakin  *
5965381b23dSAlexander Lobakin  */
5975381b23dSAlexander Lobakin 
5985381b23dSAlexander Lobakin /**
5995381b23dSAlexander Lobakin  *	__alloc_skb	-	allocate a network buffer
6005381b23dSAlexander Lobakin  *	@size: size to allocate
6015381b23dSAlexander Lobakin  *	@gfp_mask: allocation mask
6025381b23dSAlexander Lobakin  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
6035381b23dSAlexander Lobakin  *		instead of head cache and allocate a cloned (child) skb.
6045381b23dSAlexander Lobakin  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
6055381b23dSAlexander Lobakin  *		allocations in case the data is required for writeback
6065381b23dSAlexander Lobakin  *	@node: numa node to allocate memory on
6075381b23dSAlexander Lobakin  *
6085381b23dSAlexander Lobakin  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
6095381b23dSAlexander Lobakin  *	tail room of at least size bytes. The object has a reference count
6105381b23dSAlexander Lobakin  *	of one. The return is the buffer. On a failure the return is %NULL.
6115381b23dSAlexander Lobakin  *
6125381b23dSAlexander Lobakin  *	Buffers may only be allocated from interrupts using a @gfp_mask of
6135381b23dSAlexander Lobakin  *	%GFP_ATOMIC.
6145381b23dSAlexander Lobakin  */
6155381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
6165381b23dSAlexander Lobakin 			    int flags, int node)
6175381b23dSAlexander Lobakin {
6185381b23dSAlexander Lobakin 	struct kmem_cache *cache;
6195381b23dSAlexander Lobakin 	struct sk_buff *skb;
6205381b23dSAlexander Lobakin 	bool pfmemalloc;
621a5df6333SLi RongQing 	u8 *data;
6225381b23dSAlexander Lobakin 
6235381b23dSAlexander Lobakin 	cache = (flags & SKB_ALLOC_FCLONE)
624025a785fSJakub Kicinski 		? skbuff_fclone_cache : skbuff_cache;
6255381b23dSAlexander Lobakin 
6265381b23dSAlexander Lobakin 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
6275381b23dSAlexander Lobakin 		gfp_mask |= __GFP_MEMALLOC;
6285381b23dSAlexander Lobakin 
6295381b23dSAlexander Lobakin 	/* Get the HEAD */
630d13612b5SAlexander Lobakin 	if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
631d13612b5SAlexander Lobakin 	    likely(node == NUMA_NO_NODE || node == numa_mem_id()))
632d13612b5SAlexander Lobakin 		skb = napi_skb_cache_get();
633d13612b5SAlexander Lobakin 	else
634d13612b5SAlexander Lobakin 		skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
635df1ae022SAlexander Lobakin 	if (unlikely(!skb))
636df1ae022SAlexander Lobakin 		return NULL;
6375381b23dSAlexander Lobakin 	prefetchw(skb);
6385381b23dSAlexander Lobakin 
6395381b23dSAlexander Lobakin 	/* We do our best to align skb_shared_info on a separate cache
6405381b23dSAlexander Lobakin 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
6415381b23dSAlexander Lobakin 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
6425381b23dSAlexander Lobakin 	 * Both skb->head and skb_shared_info are cache line aligned.
6435381b23dSAlexander Lobakin 	 */
6445c0e820cSEric Dumazet 	data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
645df1ae022SAlexander Lobakin 	if (unlikely(!data))
6465381b23dSAlexander Lobakin 		goto nodata;
64712d6c1d3SKees Cook 	/* kmalloc_size_roundup() might give us more room than requested.
6485381b23dSAlexander Lobakin 	 * Put skb_shared_info exactly at the end of allocated zone,
6495381b23dSAlexander Lobakin 	 * to allow max possible filling before reallocation.
6505381b23dSAlexander Lobakin 	 */
65165998d2bSEric Dumazet 	prefetchw(data + SKB_WITH_OVERHEAD(size));
6525381b23dSAlexander Lobakin 
6535381b23dSAlexander Lobakin 	/*
6545381b23dSAlexander Lobakin 	 * Only clear those fields we need to clear, not those that we will
6555381b23dSAlexander Lobakin 	 * actually initialise below. Hence, don't put any more fields after
6565381b23dSAlexander Lobakin 	 * the tail pointer in struct sk_buff!
6575381b23dSAlexander Lobakin 	 */
6585381b23dSAlexander Lobakin 	memset(skb, 0, offsetof(struct sk_buff, tail));
65965998d2bSEric Dumazet 	__build_skb_around(skb, data, size);
6605381b23dSAlexander Lobakin 	skb->pfmemalloc = pfmemalloc;
6615381b23dSAlexander Lobakin 
6625381b23dSAlexander Lobakin 	if (flags & SKB_ALLOC_FCLONE) {
6635381b23dSAlexander Lobakin 		struct sk_buff_fclones *fclones;
6645381b23dSAlexander Lobakin 
6655381b23dSAlexander Lobakin 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6665381b23dSAlexander Lobakin 
6675381b23dSAlexander Lobakin 		skb->fclone = SKB_FCLONE_ORIG;
6685381b23dSAlexander Lobakin 		refcount_set(&fclones->fclone_ref, 1);
6695381b23dSAlexander Lobakin 	}
6705381b23dSAlexander Lobakin 
6715381b23dSAlexander Lobakin 	return skb;
672df1ae022SAlexander Lobakin 
6735381b23dSAlexander Lobakin nodata:
6745381b23dSAlexander Lobakin 	kmem_cache_free(cache, skb);
675df1ae022SAlexander Lobakin 	return NULL;
6765381b23dSAlexander Lobakin }
6775381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb);
6785381b23dSAlexander Lobakin 
6797ba7aeabSSebastian Andrzej Siewior /**
680fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
681fd11a83dSAlexander Duyck  *	@dev: network device to receive on
682d7499160SMasanari Iida  *	@len: length to allocate
683fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
684fd11a83dSAlexander Duyck  *
685fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
686fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
687fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
688fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
689fd11a83dSAlexander Duyck  *
690fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
691fd11a83dSAlexander Duyck  */
6929451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
6939451980aSAlexander Duyck 				   gfp_t gfp_mask)
694fd11a83dSAlexander Duyck {
695b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
696fd11a83dSAlexander Duyck 	struct sk_buff *skb;
6979451980aSAlexander Duyck 	bool pfmemalloc;
6989451980aSAlexander Duyck 	void *data;
699fd11a83dSAlexander Duyck 
7009451980aSAlexander Duyck 	len += NET_SKB_PAD;
701fd11a83dSAlexander Duyck 
70266c55602SAlexander Lobakin 	/* If requested length is either too small or too big,
70366c55602SAlexander Lobakin 	 * we use kmalloc() for skb->head allocation.
70466c55602SAlexander Lobakin 	 */
70566c55602SAlexander Lobakin 	if (len <= SKB_WITH_OVERHEAD(1024) ||
70666c55602SAlexander Lobakin 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
707d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
708a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
709a080e7bdSAlexander Duyck 		if (!skb)
710a080e7bdSAlexander Duyck 			goto skb_fail;
711a080e7bdSAlexander Duyck 		goto skb_success;
712a080e7bdSAlexander Duyck 	}
7139451980aSAlexander Duyck 
714115f1a5cSEric Dumazet 	len = SKB_HEAD_ALIGN(len);
7159451980aSAlexander Duyck 
7169451980aSAlexander Duyck 	if (sk_memalloc_socks())
7179451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
7189451980aSAlexander Duyck 
719afa79d08SChangbin Du 	if (in_hardirq() || irqs_disabled()) {
7209451980aSAlexander Duyck 		nc = this_cpu_ptr(&netdev_alloc_cache);
7218c2dd3e4SAlexander Duyck 		data = page_frag_alloc(nc, len, gfp_mask);
7229451980aSAlexander Duyck 		pfmemalloc = nc->pfmemalloc;
72392dcabd7SSebastian Andrzej Siewior 	} else {
72492dcabd7SSebastian Andrzej Siewior 		local_bh_disable();
72592dcabd7SSebastian Andrzej Siewior 		nc = this_cpu_ptr(&napi_alloc_cache.page);
72692dcabd7SSebastian Andrzej Siewior 		data = page_frag_alloc(nc, len, gfp_mask);
72792dcabd7SSebastian Andrzej Siewior 		pfmemalloc = nc->pfmemalloc;
72892dcabd7SSebastian Andrzej Siewior 		local_bh_enable();
72992dcabd7SSebastian Andrzej Siewior 	}
7309451980aSAlexander Duyck 
7319451980aSAlexander Duyck 	if (unlikely(!data))
7329451980aSAlexander Duyck 		return NULL;
7339451980aSAlexander Duyck 
7349451980aSAlexander Duyck 	skb = __build_skb(data, len);
7359451980aSAlexander Duyck 	if (unlikely(!skb)) {
736181edb2bSAlexander Duyck 		skb_free_frag(data);
7379451980aSAlexander Duyck 		return NULL;
7389451980aSAlexander Duyck 	}
7399451980aSAlexander Duyck 
7409451980aSAlexander Duyck 	if (pfmemalloc)
7419451980aSAlexander Duyck 		skb->pfmemalloc = 1;
7429451980aSAlexander Duyck 	skb->head_frag = 1;
7439451980aSAlexander Duyck 
744a080e7bdSAlexander Duyck skb_success:
7458af27456SChristoph Hellwig 	skb_reserve(skb, NET_SKB_PAD);
7467b2e497aSChristoph Hellwig 	skb->dev = dev;
747fd11a83dSAlexander Duyck 
748a080e7bdSAlexander Duyck skb_fail:
7498af27456SChristoph Hellwig 	return skb;
7508af27456SChristoph Hellwig }
751b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
7521da177e4SLinus Torvalds 
753fd11a83dSAlexander Duyck /**
754fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
755fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
756d7499160SMasanari Iida  *	@len: length to allocate
757fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
758fd11a83dSAlexander Duyck  *
759fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
760fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
761fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
762fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
763fd11a83dSAlexander Duyck  *
764fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
765fd11a83dSAlexander Duyck  */
7669451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
7679451980aSAlexander Duyck 				 gfp_t gfp_mask)
768fd11a83dSAlexander Duyck {
7693226b158SEric Dumazet 	struct napi_alloc_cache *nc;
770fd11a83dSAlexander Duyck 	struct sk_buff *skb;
771dbae2b06SPaolo Abeni 	bool pfmemalloc;
7729451980aSAlexander Duyck 	void *data;
773fd11a83dSAlexander Duyck 
774ee2640dfSEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
7759451980aSAlexander Duyck 	len += NET_SKB_PAD + NET_IP_ALIGN;
776fd11a83dSAlexander Duyck 
7773226b158SEric Dumazet 	/* If requested length is either too small or too big,
7783226b158SEric Dumazet 	 * we use kmalloc() for skb->head allocation.
779dbae2b06SPaolo Abeni 	 * When the small frag allocator is available, prefer it over kmalloc
780dbae2b06SPaolo Abeni 	 * for small fragments
7813226b158SEric Dumazet 	 */
782dbae2b06SPaolo Abeni 	if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) ||
7833226b158SEric Dumazet 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
784d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
785cfb8ec65SAlexander Lobakin 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
786cfb8ec65SAlexander Lobakin 				  NUMA_NO_NODE);
787a080e7bdSAlexander Duyck 		if (!skb)
788a080e7bdSAlexander Duyck 			goto skb_fail;
789a080e7bdSAlexander Duyck 		goto skb_success;
790a080e7bdSAlexander Duyck 	}
7919451980aSAlexander Duyck 
7923226b158SEric Dumazet 	nc = this_cpu_ptr(&napi_alloc_cache);
7939451980aSAlexander Duyck 
7949451980aSAlexander Duyck 	if (sk_memalloc_socks())
7959451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
7969451980aSAlexander Duyck 
797dbae2b06SPaolo Abeni 	if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
798dbae2b06SPaolo Abeni 		/* we are artificially inflating the allocation size, but
799dbae2b06SPaolo Abeni 		 * that is not as bad as it may look like, as:
800dbae2b06SPaolo Abeni 		 * - 'len' less than GRO_MAX_HEAD makes little sense
801dbae2b06SPaolo Abeni 		 * - On most systems, larger 'len' values lead to fragment
802dbae2b06SPaolo Abeni 		 *   size above 512 bytes
803dbae2b06SPaolo Abeni 		 * - kmalloc would use the kmalloc-1k slab for such values
804dbae2b06SPaolo Abeni 		 * - Builds with smaller GRO_MAX_HEAD will very likely do
805dbae2b06SPaolo Abeni 		 *   little networking, as that implies no WiFi and no
806dbae2b06SPaolo Abeni 		 *   tunnels support, and 32 bits arches.
807dbae2b06SPaolo Abeni 		 */
808dbae2b06SPaolo Abeni 		len = SZ_1K;
809dbae2b06SPaolo Abeni 
810dbae2b06SPaolo Abeni 		data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
811dbae2b06SPaolo Abeni 		pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
812dbae2b06SPaolo Abeni 	} else {
813115f1a5cSEric Dumazet 		len = SKB_HEAD_ALIGN(len);
814dbae2b06SPaolo Abeni 
8158c2dd3e4SAlexander Duyck 		data = page_frag_alloc(&nc->page, len, gfp_mask);
816dbae2b06SPaolo Abeni 		pfmemalloc = nc->page.pfmemalloc;
817dbae2b06SPaolo Abeni 	}
818dbae2b06SPaolo Abeni 
8199451980aSAlexander Duyck 	if (unlikely(!data))
8209451980aSAlexander Duyck 		return NULL;
8219451980aSAlexander Duyck 
822cfb8ec65SAlexander Lobakin 	skb = __napi_build_skb(data, len);
8239451980aSAlexander Duyck 	if (unlikely(!skb)) {
824181edb2bSAlexander Duyck 		skb_free_frag(data);
8259451980aSAlexander Duyck 		return NULL;
8269451980aSAlexander Duyck 	}
8279451980aSAlexander Duyck 
828dbae2b06SPaolo Abeni 	if (pfmemalloc)
8299451980aSAlexander Duyck 		skb->pfmemalloc = 1;
8309451980aSAlexander Duyck 	skb->head_frag = 1;
8319451980aSAlexander Duyck 
832a080e7bdSAlexander Duyck skb_success:
833fd11a83dSAlexander Duyck 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
834fd11a83dSAlexander Duyck 	skb->dev = napi->dev;
835fd11a83dSAlexander Duyck 
836a080e7bdSAlexander Duyck skb_fail:
837fd11a83dSAlexander Duyck 	return skb;
838fd11a83dSAlexander Duyck }
839fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
840fd11a83dSAlexander Duyck 
841654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
84250269e19SEric Dumazet 		     int size, unsigned int truesize)
843654bed16SPeter Zijlstra {
844654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
845654bed16SPeter Zijlstra 	skb->len += size;
846654bed16SPeter Zijlstra 	skb->data_len += size;
84750269e19SEric Dumazet 	skb->truesize += truesize;
848654bed16SPeter Zijlstra }
849654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
850654bed16SPeter Zijlstra 
851f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
852f8e617e1SJason Wang 			  unsigned int truesize)
853f8e617e1SJason Wang {
854f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
855f8e617e1SJason Wang 
856f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
857f8e617e1SJason Wang 	skb->len += size;
858f8e617e1SJason Wang 	skb->data_len += size;
859f8e617e1SJason Wang 	skb->truesize += truesize;
860f8e617e1SJason Wang }
861f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
862f8e617e1SJason Wang 
86327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
8641da177e4SLinus Torvalds {
865bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
86627b437c8SHerbert Xu 	*listp = NULL;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
86927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
87027b437c8SHerbert Xu {
87127b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
87227b437c8SHerbert Xu }
87327b437c8SHerbert Xu 
8741da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	struct sk_buff *list;
8771da177e4SLinus Torvalds 
878fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
8791da177e4SLinus Torvalds 		skb_get(list);
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
88275eaf63eSAlexander Lobakin #if IS_ENABLED(CONFIG_PAGE_POOL)
88375eaf63eSAlexander Lobakin bool napi_pp_put_page(struct page *page, bool napi_safe)
88475eaf63eSAlexander Lobakin {
8855b899c33SAlexander Lobakin 	bool allow_direct = false;
88675eaf63eSAlexander Lobakin 	struct page_pool *pp;
88775eaf63eSAlexander Lobakin 
88875eaf63eSAlexander Lobakin 	page = compound_head(page);
88975eaf63eSAlexander Lobakin 
89075eaf63eSAlexander Lobakin 	/* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
89175eaf63eSAlexander Lobakin 	 * in order to preserve any existing bits, such as bit 0 for the
89275eaf63eSAlexander Lobakin 	 * head page of compound page and bit 1 for pfmemalloc page, so
89375eaf63eSAlexander Lobakin 	 * mask those bits for freeing side when doing below checking,
89475eaf63eSAlexander Lobakin 	 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
89575eaf63eSAlexander Lobakin 	 * to avoid recycling the pfmemalloc page.
89675eaf63eSAlexander Lobakin 	 */
89775eaf63eSAlexander Lobakin 	if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
89875eaf63eSAlexander Lobakin 		return false;
89975eaf63eSAlexander Lobakin 
90075eaf63eSAlexander Lobakin 	pp = page->pp;
90175eaf63eSAlexander Lobakin 
90275eaf63eSAlexander Lobakin 	/* Allow direct recycle if we have reasons to believe that we are
90375eaf63eSAlexander Lobakin 	 * in the same context as the consumer would run, so there's
90475eaf63eSAlexander Lobakin 	 * no possible race.
9054a36d018SAlexander Lobakin 	 * __page_pool_put_page() makes sure we're not in hardirq context
9064a36d018SAlexander Lobakin 	 * and interrupts are enabled prior to accessing the cache.
90775eaf63eSAlexander Lobakin 	 */
9084a36d018SAlexander Lobakin 	if (napi_safe || in_softirq()) {
9095b899c33SAlexander Lobakin 		const struct napi_struct *napi = READ_ONCE(pp->p.napi);
9105b899c33SAlexander Lobakin 
9115b899c33SAlexander Lobakin 		allow_direct = napi &&
91275eaf63eSAlexander Lobakin 			READ_ONCE(napi->list_owner) == smp_processor_id();
9135b899c33SAlexander Lobakin 	}
91475eaf63eSAlexander Lobakin 
91575eaf63eSAlexander Lobakin 	/* Driver set this to memory recycling info. Reset it on recycle.
91675eaf63eSAlexander Lobakin 	 * This will *not* work for NIC using a split-page memory model.
91775eaf63eSAlexander Lobakin 	 * The page will be returned to the pool here regardless of the
91875eaf63eSAlexander Lobakin 	 * 'flipped' fragment being in use or not.
91975eaf63eSAlexander Lobakin 	 */
92075eaf63eSAlexander Lobakin 	page_pool_put_full_page(pp, page, allow_direct);
92175eaf63eSAlexander Lobakin 
92275eaf63eSAlexander Lobakin 	return true;
92375eaf63eSAlexander Lobakin }
92475eaf63eSAlexander Lobakin EXPORT_SYMBOL(napi_pp_put_page);
92575eaf63eSAlexander Lobakin #endif
92675eaf63eSAlexander Lobakin 
927b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe)
9284727bab4SYunsheng Lin {
9294727bab4SYunsheng Lin 	if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
9304727bab4SYunsheng Lin 		return false;
93175eaf63eSAlexander Lobakin 	return napi_pp_put_page(virt_to_page(data), napi_safe);
9324727bab4SYunsheng Lin }
9334727bab4SYunsheng Lin 
934bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset)
935bf9f1baaSEric Dumazet {
936bf9f1baaSEric Dumazet 	if (end_offset == SKB_SMALL_HEAD_HEADROOM)
937bf9f1baaSEric Dumazet 		kmem_cache_free(skb_small_head_cache, head);
938bf9f1baaSEric Dumazet 	else
939bf9f1baaSEric Dumazet 		kfree(head);
940bf9f1baaSEric Dumazet }
941bf9f1baaSEric Dumazet 
942b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe)
943d3836f21SEric Dumazet {
944181edb2bSAlexander Duyck 	unsigned char *head = skb->head;
945181edb2bSAlexander Duyck 
9466a5bcd84SIlias Apalodimas 	if (skb->head_frag) {
947b07a2d97SJakub Kicinski 		if (skb_pp_recycle(skb, head, napi_safe))
9486a5bcd84SIlias Apalodimas 			return;
949181edb2bSAlexander Duyck 		skb_free_frag(head);
9506a5bcd84SIlias Apalodimas 	} else {
951bf9f1baaSEric Dumazet 		skb_kfree_head(head, skb_end_offset(skb));
952d3836f21SEric Dumazet 	}
9536a5bcd84SIlias Apalodimas }
954d3836f21SEric Dumazet 
955b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason,
956b07a2d97SJakub Kicinski 			     bool napi_safe)
9571da177e4SLinus Torvalds {
958ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
9591da177e4SLinus Torvalds 	int i;
960ff04a771SEric Dumazet 
961ff04a771SEric Dumazet 	if (skb->cloned &&
962ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
963ff04a771SEric Dumazet 			      &shinfo->dataref))
9642cc3aeb5SIlias Apalodimas 		goto exit;
965ff04a771SEric Dumazet 
966753f1ca4SPavel Begunkov 	if (skb_zcopy(skb)) {
967753f1ca4SPavel Begunkov 		bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
968753f1ca4SPavel Begunkov 
96970c43167SJonathan Lemon 		skb_zcopy_clear(skb, true);
970753f1ca4SPavel Begunkov 		if (skip_unref)
971753f1ca4SPavel Begunkov 			goto free_head;
972753f1ca4SPavel Begunkov 	}
97370c43167SJonathan Lemon 
974ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
9758c48eea3SJakub Kicinski 		napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe);
9761da177e4SLinus Torvalds 
977753f1ca4SPavel Begunkov free_head:
978ff04a771SEric Dumazet 	if (shinfo->frag_list)
979511a3edaSEric Dumazet 		kfree_skb_list_reason(shinfo->frag_list, reason);
9801da177e4SLinus Torvalds 
981b07a2d97SJakub Kicinski 	skb_free_head(skb, napi_safe);
9822cc3aeb5SIlias Apalodimas exit:
9832cc3aeb5SIlias Apalodimas 	/* When we clone an SKB we copy the reycling bit. The pp_recycle
9842cc3aeb5SIlias Apalodimas 	 * bit is only set on the head though, so in order to avoid races
9852cc3aeb5SIlias Apalodimas 	 * while trying to recycle fragments on __skb_frag_unref() we need
9862cc3aeb5SIlias Apalodimas 	 * to make one SKB responsible for triggering the recycle path.
9872cc3aeb5SIlias Apalodimas 	 * So disable the recycling bit if an SKB is cloned and we have
98858e61e41STom Rix 	 * additional references to the fragmented part of the SKB.
9892cc3aeb5SIlias Apalodimas 	 * Eventually the last SKB will have the recycling bit set and it's
9902cc3aeb5SIlias Apalodimas 	 * dataref set to 0, which will trigger the recycling
9912cc3aeb5SIlias Apalodimas 	 */
9922cc3aeb5SIlias Apalodimas 	skb->pp_recycle = 0;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
9961da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
9971da177e4SLinus Torvalds  */
9982d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
9991da177e4SLinus Torvalds {
1000d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
1001d179cd12SDavid S. Miller 
1002d179cd12SDavid S. Miller 	switch (skb->fclone) {
1003d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
1004025a785fSJakub Kicinski 		kmem_cache_free(skbuff_cache, skb);
10056ffe75ebSEric Dumazet 		return;
1006d179cd12SDavid S. Miller 
1007d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
1008d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
10096ffe75ebSEric Dumazet 
10106ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
10116ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
10126ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
10136ffe75ebSEric Dumazet 		 */
10142638595aSReshetova, Elena 		if (refcount_read(&fclones->fclone_ref) == 1)
10156ffe75ebSEric Dumazet 			goto fastpath;
1016d179cd12SDavid S. Miller 		break;
1017d179cd12SDavid S. Miller 
10186ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
1019d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
1020d179cd12SDavid S. Miller 		break;
10213ff50b79SStephen Hemminger 	}
10222638595aSReshetova, Elena 	if (!refcount_dec_and_test(&fclones->fclone_ref))
10236ffe75ebSEric Dumazet 		return;
10246ffe75ebSEric Dumazet fastpath:
10256ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10280a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb)
10291da177e4SLinus Torvalds {
1030adf30907SEric Dumazet 	skb_dst_drop(skb);
10311da177e4SLinus Torvalds 	if (skb->destructor) {
10327890e2f0SEric Dumazet 		DEBUG_NET_WARN_ON_ONCE(in_hardirq());
10331da177e4SLinus Torvalds 		skb->destructor(skb);
10341da177e4SLinus Torvalds 	}
1035a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1036cb9c6836SFlorian Westphal 	nf_conntrack_put(skb_nfct(skb));
10372fc72c7bSKOVACS Krisztian #endif
1038df5042f4SFlorian Westphal 	skb_ext_put(skb);
103904a4bb55SLennert Buytenhek }
104004a4bb55SLennert Buytenhek 
104104a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
1042b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason,
1043b07a2d97SJakub Kicinski 			    bool napi_safe)
104404a4bb55SLennert Buytenhek {
104504a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
1046a28b1b90SFlorian Westphal 	if (likely(skb->head))
1047b07a2d97SJakub Kicinski 		skb_release_data(skb, reason, napi_safe);
10482d4baff8SHerbert Xu }
10491da177e4SLinus Torvalds 
10502d4baff8SHerbert Xu /**
10512d4baff8SHerbert Xu  *	__kfree_skb - private function
10522d4baff8SHerbert Xu  *	@skb: buffer
10532d4baff8SHerbert Xu  *
10542d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
10552d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
10562d4baff8SHerbert Xu  *	always call kfree_skb
10572d4baff8SHerbert Xu  */
10582d4baff8SHerbert Xu 
10592d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
10602d4baff8SHerbert Xu {
1061b07a2d97SJakub Kicinski 	skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false);
10621da177e4SLinus Torvalds 	kfree_skbmem(skb);
10631da177e4SLinus Torvalds }
1064b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
10651da177e4SLinus Torvalds 
1066a4650da2SJesper Dangaard Brouer static __always_inline
1067a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1068a4650da2SJesper Dangaard Brouer {
1069a4650da2SJesper Dangaard Brouer 	if (unlikely(!skb_unref(skb)))
1070a4650da2SJesper Dangaard Brouer 		return false;
1071a4650da2SJesper Dangaard Brouer 
1072071c0fc6SJohannes Berg 	DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET ||
1073071c0fc6SJohannes Berg 			       u32_get_bits(reason,
1074071c0fc6SJohannes Berg 					    SKB_DROP_REASON_SUBSYS_MASK) >=
1075071c0fc6SJohannes Berg 				SKB_DROP_REASON_SUBSYS_NUM);
1076a4650da2SJesper Dangaard Brouer 
1077a4650da2SJesper Dangaard Brouer 	if (reason == SKB_CONSUMED)
1078dd1b5278SEric Dumazet 		trace_consume_skb(skb, __builtin_return_address(0));
1079a4650da2SJesper Dangaard Brouer 	else
1080a4650da2SJesper Dangaard Brouer 		trace_kfree_skb(skb, __builtin_return_address(0), reason);
1081a4650da2SJesper Dangaard Brouer 	return true;
1082a4650da2SJesper Dangaard Brouer }
1083a4650da2SJesper Dangaard Brouer 
10841da177e4SLinus Torvalds /**
1085c504e5c2SMenglong Dong  *	kfree_skb_reason - free an sk_buff with special reason
1086231d06aeSJörn Engel  *	@skb: buffer to free
1087c504e5c2SMenglong Dong  *	@reason: reason why this skb is dropped
1088231d06aeSJörn Engel  *
1089231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
1090c504e5c2SMenglong Dong  *	hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
1091c504e5c2SMenglong Dong  *	tracepoint.
1092231d06aeSJörn Engel  */
1093c205cc75SMenglong Dong void __fix_address
1094c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1095231d06aeSJörn Engel {
1096a4650da2SJesper Dangaard Brouer 	if (__kfree_skb_reason(skb, reason))
1097231d06aeSJörn Engel 		__kfree_skb(skb);
1098231d06aeSJörn Engel }
1099c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason);
1100231d06aeSJörn Engel 
1101eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE	16
1102eedade12SJesper Dangaard Brouer 
1103eedade12SJesper Dangaard Brouer struct skb_free_array {
1104eedade12SJesper Dangaard Brouer 	unsigned int skb_count;
1105eedade12SJesper Dangaard Brouer 	void *skb_array[KFREE_SKB_BULK_SIZE];
1106eedade12SJesper Dangaard Brouer };
1107eedade12SJesper Dangaard Brouer 
1108eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb,
1109eedade12SJesper Dangaard Brouer 			       struct skb_free_array *sa,
1110eedade12SJesper Dangaard Brouer 			       enum skb_drop_reason reason)
1111eedade12SJesper Dangaard Brouer {
1112eedade12SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
1113eedade12SJesper Dangaard Brouer 	if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
1114eedade12SJesper Dangaard Brouer 		__kfree_skb(skb);
1115eedade12SJesper Dangaard Brouer 		return;
1116eedade12SJesper Dangaard Brouer 	}
1117eedade12SJesper Dangaard Brouer 
1118b07a2d97SJakub Kicinski 	skb_release_all(skb, reason, false);
1119eedade12SJesper Dangaard Brouer 	sa->skb_array[sa->skb_count++] = skb;
1120eedade12SJesper Dangaard Brouer 
1121eedade12SJesper Dangaard Brouer 	if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) {
1122025a785fSJakub Kicinski 		kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE,
1123eedade12SJesper Dangaard Brouer 				     sa->skb_array);
1124eedade12SJesper Dangaard Brouer 		sa->skb_count = 0;
1125eedade12SJesper Dangaard Brouer 	}
1126eedade12SJesper Dangaard Brouer }
1127eedade12SJesper Dangaard Brouer 
1128a4650da2SJesper Dangaard Brouer void __fix_address
1129a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason)
1130bd8a7036SEric Dumazet {
1131eedade12SJesper Dangaard Brouer 	struct skb_free_array sa;
1132eedade12SJesper Dangaard Brouer 
1133eedade12SJesper Dangaard Brouer 	sa.skb_count = 0;
1134eedade12SJesper Dangaard Brouer 
1135bd8a7036SEric Dumazet 	while (segs) {
1136bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
1137bd8a7036SEric Dumazet 
11389dde0cd3SJesper Dangaard Brouer 		if (__kfree_skb_reason(segs, reason)) {
11399dde0cd3SJesper Dangaard Brouer 			skb_poison_list(segs);
1140eedade12SJesper Dangaard Brouer 			kfree_skb_add_bulk(segs, &sa, reason);
11419dde0cd3SJesper Dangaard Brouer 		}
1142eedade12SJesper Dangaard Brouer 
1143bd8a7036SEric Dumazet 		segs = next;
1144bd8a7036SEric Dumazet 	}
1145eedade12SJesper Dangaard Brouer 
1146eedade12SJesper Dangaard Brouer 	if (sa.skb_count)
1147025a785fSJakub Kicinski 		kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array);
1148bd8a7036SEric Dumazet }
1149215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason);
1150bd8a7036SEric Dumazet 
11516413139dSWillem de Bruijn /* Dump skb information and contents.
11526413139dSWillem de Bruijn  *
11536413139dSWillem de Bruijn  * Must only be called from net_ratelimit()-ed paths.
11546413139dSWillem de Bruijn  *
1155302af7c6SVladimir Oltean  * Dumps whole packets if full_pkt, only headers otherwise.
11566413139dSWillem de Bruijn  */
11576413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
11586413139dSWillem de Bruijn {
11596413139dSWillem de Bruijn 	struct skb_shared_info *sh = skb_shinfo(skb);
11606413139dSWillem de Bruijn 	struct net_device *dev = skb->dev;
11616413139dSWillem de Bruijn 	struct sock *sk = skb->sk;
11626413139dSWillem de Bruijn 	struct sk_buff *list_skb;
11636413139dSWillem de Bruijn 	bool has_mac, has_trans;
11646413139dSWillem de Bruijn 	int headroom, tailroom;
11656413139dSWillem de Bruijn 	int i, len, seg_len;
11666413139dSWillem de Bruijn 
11676413139dSWillem de Bruijn 	if (full_pkt)
11686413139dSWillem de Bruijn 		len = skb->len;
11696413139dSWillem de Bruijn 	else
11706413139dSWillem de Bruijn 		len = min_t(int, skb->len, MAX_HEADER + 128);
11716413139dSWillem de Bruijn 
11726413139dSWillem de Bruijn 	headroom = skb_headroom(skb);
11736413139dSWillem de Bruijn 	tailroom = skb_tailroom(skb);
11746413139dSWillem de Bruijn 
11756413139dSWillem de Bruijn 	has_mac = skb_mac_header_was_set(skb);
11766413139dSWillem de Bruijn 	has_trans = skb_transport_header_was_set(skb);
11776413139dSWillem de Bruijn 
11786413139dSWillem de Bruijn 	printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
11796413139dSWillem de Bruijn 	       "mac=(%d,%d) net=(%d,%d) trans=%d\n"
11806413139dSWillem de Bruijn 	       "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
11816413139dSWillem de Bruijn 	       "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
11826413139dSWillem de Bruijn 	       "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
11836413139dSWillem de Bruijn 	       level, skb->len, headroom, skb_headlen(skb), tailroom,
11846413139dSWillem de Bruijn 	       has_mac ? skb->mac_header : -1,
11856413139dSWillem de Bruijn 	       has_mac ? skb_mac_header_len(skb) : -1,
11866413139dSWillem de Bruijn 	       skb->network_header,
11876413139dSWillem de Bruijn 	       has_trans ? skb_network_header_len(skb) : -1,
11886413139dSWillem de Bruijn 	       has_trans ? skb->transport_header : -1,
11896413139dSWillem de Bruijn 	       sh->tx_flags, sh->nr_frags,
11906413139dSWillem de Bruijn 	       sh->gso_size, sh->gso_type, sh->gso_segs,
11916413139dSWillem de Bruijn 	       skb->csum, skb->ip_summed, skb->csum_complete_sw,
11926413139dSWillem de Bruijn 	       skb->csum_valid, skb->csum_level,
11936413139dSWillem de Bruijn 	       skb->hash, skb->sw_hash, skb->l4_hash,
11946413139dSWillem de Bruijn 	       ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
11956413139dSWillem de Bruijn 
11966413139dSWillem de Bruijn 	if (dev)
11978a03ef67SGal Pressman 		printk("%sdev name=%s feat=%pNF\n",
11986413139dSWillem de Bruijn 		       level, dev->name, &dev->features);
11996413139dSWillem de Bruijn 	if (sk)
1200db8051f3SQian Cai 		printk("%ssk family=%hu type=%u proto=%u\n",
12016413139dSWillem de Bruijn 		       level, sk->sk_family, sk->sk_type, sk->sk_protocol);
12026413139dSWillem de Bruijn 
12036413139dSWillem de Bruijn 	if (full_pkt && headroom)
12046413139dSWillem de Bruijn 		print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
12056413139dSWillem de Bruijn 			       16, 1, skb->head, headroom, false);
12066413139dSWillem de Bruijn 
12076413139dSWillem de Bruijn 	seg_len = min_t(int, skb_headlen(skb), len);
12086413139dSWillem de Bruijn 	if (seg_len)
12096413139dSWillem de Bruijn 		print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
12106413139dSWillem de Bruijn 			       16, 1, skb->data, seg_len, false);
12116413139dSWillem de Bruijn 	len -= seg_len;
12126413139dSWillem de Bruijn 
12136413139dSWillem de Bruijn 	if (full_pkt && tailroom)
12146413139dSWillem de Bruijn 		print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
12156413139dSWillem de Bruijn 			       16, 1, skb_tail_pointer(skb), tailroom, false);
12166413139dSWillem de Bruijn 
12176413139dSWillem de Bruijn 	for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
12186413139dSWillem de Bruijn 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
12196413139dSWillem de Bruijn 		u32 p_off, p_len, copied;
12206413139dSWillem de Bruijn 		struct page *p;
12216413139dSWillem de Bruijn 		u8 *vaddr;
12226413139dSWillem de Bruijn 
1223b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(frag, skb_frag_off(frag),
12246413139dSWillem de Bruijn 				      skb_frag_size(frag), p, p_off, p_len,
12256413139dSWillem de Bruijn 				      copied) {
12266413139dSWillem de Bruijn 			seg_len = min_t(int, p_len, len);
12276413139dSWillem de Bruijn 			vaddr = kmap_atomic(p);
12286413139dSWillem de Bruijn 			print_hex_dump(level, "skb frag:     ",
12296413139dSWillem de Bruijn 				       DUMP_PREFIX_OFFSET,
12306413139dSWillem de Bruijn 				       16, 1, vaddr + p_off, seg_len, false);
12316413139dSWillem de Bruijn 			kunmap_atomic(vaddr);
12326413139dSWillem de Bruijn 			len -= seg_len;
12336413139dSWillem de Bruijn 			if (!len)
12346413139dSWillem de Bruijn 				break;
12356413139dSWillem de Bruijn 		}
12366413139dSWillem de Bruijn 	}
12376413139dSWillem de Bruijn 
12386413139dSWillem de Bruijn 	if (full_pkt && skb_has_frag_list(skb)) {
12396413139dSWillem de Bruijn 		printk("skb fraglist:\n");
12406413139dSWillem de Bruijn 		skb_walk_frags(skb, list_skb)
12416413139dSWillem de Bruijn 			skb_dump(level, list_skb, true);
12426413139dSWillem de Bruijn 	}
12436413139dSWillem de Bruijn }
12446413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump);
12456413139dSWillem de Bruijn 
1246d1a203eaSStephen Hemminger /**
124725121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
124825121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
124925121173SMichael S. Tsirkin  *
125025121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
125125121173SMichael S. Tsirkin  *	skb must be freed afterwards.
125225121173SMichael S. Tsirkin  */
125325121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
125425121173SMichael S. Tsirkin {
1255753f1ca4SPavel Begunkov 	if (skb) {
1256753f1ca4SPavel Begunkov 		skb_zcopy_downgrade_managed(skb);
12571f8b977aSWillem de Bruijn 		skb_zcopy_clear(skb, true);
125825121173SMichael S. Tsirkin 	}
1259753f1ca4SPavel Begunkov }
126025121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
126125121173SMichael S. Tsirkin 
1262be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS
126325121173SMichael S. Tsirkin /**
1264ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
1265ead2ceb0SNeil Horman  *	@skb: buffer to free
1266ead2ceb0SNeil Horman  *
1267ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
1268ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
1269ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
1270ead2ceb0SNeil Horman  */
1271ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
1272ead2ceb0SNeil Horman {
12733889a803SPaolo Abeni 	if (!skb_unref(skb))
1274ead2ceb0SNeil Horman 		return;
12753889a803SPaolo Abeni 
1276dd1b5278SEric Dumazet 	trace_consume_skb(skb, __builtin_return_address(0));
1277ead2ceb0SNeil Horman 	__kfree_skb(skb);
1278ead2ceb0SNeil Horman }
1279ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
1280be769db2SHerbert Xu #endif
1281ead2ceb0SNeil Horman 
12820a463c78SPaolo Abeni /**
1283c1639be9SMauro Carvalho Chehab  *	__consume_stateless_skb - free an skbuff, assuming it is stateless
12840a463c78SPaolo Abeni  *	@skb: buffer to free
12850a463c78SPaolo Abeni  *
1286ca2c1418SPaolo Abeni  *	Alike consume_skb(), but this variant assumes that this is the last
1287ca2c1418SPaolo Abeni  *	skb reference and all the head states have been already dropped
12880a463c78SPaolo Abeni  */
1289ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb)
12900a463c78SPaolo Abeni {
1291dd1b5278SEric Dumazet 	trace_consume_skb(skb, __builtin_return_address(0));
1292b07a2d97SJakub Kicinski 	skb_release_data(skb, SKB_CONSUMED, false);
12930a463c78SPaolo Abeni 	kfree_skbmem(skb);
12940a463c78SPaolo Abeni }
12950a463c78SPaolo Abeni 
1296f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb)
1297795bb1c0SJesper Dangaard Brouer {
1298795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
1299f450d539SAlexander Lobakin 	u32 i;
1300795bb1c0SJesper Dangaard Brouer 
1301025a785fSJakub Kicinski 	kasan_poison_object_data(skbuff_cache, skb);
1302795bb1c0SJesper Dangaard Brouer 	nc->skb_cache[nc->skb_count++] = skb;
1303795bb1c0SJesper Dangaard Brouer 
1304795bb1c0SJesper Dangaard Brouer 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
1305f450d539SAlexander Lobakin 		for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
1306025a785fSJakub Kicinski 			kasan_unpoison_object_data(skbuff_cache,
1307f450d539SAlexander Lobakin 						   nc->skb_cache[i]);
1308f450d539SAlexander Lobakin 
1309025a785fSJakub Kicinski 		kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF,
1310f450d539SAlexander Lobakin 				     nc->skb_cache + NAPI_SKB_CACHE_HALF);
1311f450d539SAlexander Lobakin 		nc->skb_count = NAPI_SKB_CACHE_HALF;
1312795bb1c0SJesper Dangaard Brouer 	}
1313795bb1c0SJesper Dangaard Brouer }
1314f450d539SAlexander Lobakin 
13158fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason)
131615fad714SJesper Dangaard Brouer {
13178fa66e4aSJakub Kicinski 	skb_release_all(skb, reason, true);
13189243adfcSAlexander Lobakin 	napi_skb_cache_put(skb);
13199243adfcSAlexander Lobakin }
13209243adfcSAlexander Lobakin 
13219243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb)
13229243adfcSAlexander Lobakin {
13239efb4b5bSPaolo Abeni 	if (unlikely(skb->slow_gro)) {
13248550ff8dSPaul Blakey 		nf_reset_ct(skb);
13259243adfcSAlexander Lobakin 		skb_dst_drop(skb);
13269243adfcSAlexander Lobakin 		skb_ext_put(skb);
13275e10da53SPaolo Abeni 		skb_orphan(skb);
13289efb4b5bSPaolo Abeni 		skb->slow_gro = 0;
13299efb4b5bSPaolo Abeni 	}
1330f450d539SAlexander Lobakin 	napi_skb_cache_put(skb);
133115fad714SJesper Dangaard Brouer }
1332795bb1c0SJesper Dangaard Brouer 
1333795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
1334795bb1c0SJesper Dangaard Brouer {
1335885eb0a5SJesper Dangaard Brouer 	/* Zero budget indicate non-NAPI context called us, like netpoll */
1336795bb1c0SJesper Dangaard Brouer 	if (unlikely(!budget)) {
1337885eb0a5SJesper Dangaard Brouer 		dev_consume_skb_any(skb);
1338795bb1c0SJesper Dangaard Brouer 		return;
1339795bb1c0SJesper Dangaard Brouer 	}
1340795bb1c0SJesper Dangaard Brouer 
1341ee2640dfSEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
13426454eca8SYunsheng Lin 
13437608894eSPaolo Abeni 	if (!skb_unref(skb))
1344795bb1c0SJesper Dangaard Brouer 		return;
13457608894eSPaolo Abeni 
1346795bb1c0SJesper Dangaard Brouer 	/* if reaching here SKB is ready to free */
1347dd1b5278SEric Dumazet 	trace_consume_skb(skb, __builtin_return_address(0));
1348795bb1c0SJesper Dangaard Brouer 
1349795bb1c0SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
1350abbdb5a7SEric Dumazet 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
1351795bb1c0SJesper Dangaard Brouer 		__kfree_skb(skb);
1352795bb1c0SJesper Dangaard Brouer 		return;
1353795bb1c0SJesper Dangaard Brouer 	}
1354795bb1c0SJesper Dangaard Brouer 
1355b07a2d97SJakub Kicinski 	skb_release_all(skb, SKB_CONSUMED, !!budget);
1356f450d539SAlexander Lobakin 	napi_skb_cache_put(skb);
1357795bb1c0SJesper Dangaard Brouer }
1358795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
1359795bb1c0SJesper Dangaard Brouer 
136003f61041SKees Cook /* Make sure a field is contained by headers group */
1361b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
136203f61041SKees Cook 	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
136303f61041SKees Cook 		     offsetof(struct sk_buff, headers.field));	\
1364b1937227SEric Dumazet 
1365dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1366dec18810SHerbert Xu {
1367dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
1368b1937227SEric Dumazet 	/* We do not copy old->sk */
1369dec18810SHerbert Xu 	new->dev		= old->dev;
1370b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
13717fee226aSEric Dumazet 	skb_dst_copy(new, old);
1372df5042f4SFlorian Westphal 	__skb_ext_copy(new, old);
1373b1937227SEric Dumazet 	__nf_copy(new, old, false);
13746aa895b0SPatrick McHardy 
137503f61041SKees Cook 	/* Note : this field could be in the headers group.
1376b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
1377b1937227SEric Dumazet 	 */
1378b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
137906021292SEliezer Tamir 
138003f61041SKees Cook 	memcpy(&new->headers, &old->headers, sizeof(new->headers));
1381b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
1382b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
1383b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
1384b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
1385b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
1386b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
1387b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
1388b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
1389b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
1390b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
1391b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
1392b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
1393b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
1394b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
1395b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
1396b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
1397b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
1398b1937227SEric Dumazet #endif
1399e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
1400b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
140106021292SEliezer Tamir #endif
140268822bdfSEric Dumazet 	CHECK_SKB_FIELD(alloc_cpu);
14032bd82484SEric Dumazet #ifdef CONFIG_XPS
14042bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
14052bd82484SEric Dumazet #endif
1406b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
1407b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
1408b1937227SEric Dumazet #endif
1409b1937227SEric Dumazet 
1410dec18810SHerbert Xu }
1411dec18810SHerbert Xu 
141282c49a35SHerbert Xu /*
141382c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
141482c49a35SHerbert Xu  * __copy_skb_header above instead.
141582c49a35SHerbert Xu  */
1416e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
14171da177e4SLinus Torvalds {
14181da177e4SLinus Torvalds #define C(x) n->x = skb->x
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	n->next = n->prev = NULL;
14211da177e4SLinus Torvalds 	n->sk = NULL;
1422dec18810SHerbert Xu 	__copy_skb_header(n, skb);
1423dec18810SHerbert Xu 
14241da177e4SLinus Torvalds 	C(len);
14251da177e4SLinus Torvalds 	C(data_len);
14263e6b3b2eSAlexey Dobriyan 	C(mac_len);
1427334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
142802f1c89dSPaul Moore 	n->cloned = 1;
14291da177e4SLinus Torvalds 	n->nohdr = 0;
1430b13dda9fSEric Dumazet 	n->peeked = 0;
1431e78bfb07SStefano Brivio 	C(pfmemalloc);
14326a5bcd84SIlias Apalodimas 	C(pp_recycle);
14331da177e4SLinus Torvalds 	n->destructor = NULL;
14341da177e4SLinus Torvalds 	C(tail);
14351da177e4SLinus Torvalds 	C(end);
143602f1c89dSPaul Moore 	C(head);
1437d3836f21SEric Dumazet 	C(head_frag);
143802f1c89dSPaul Moore 	C(data);
143902f1c89dSPaul Moore 	C(truesize);
144063354797SReshetova, Elena 	refcount_set(&n->users, 1);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
14431da177e4SLinus Torvalds 	skb->cloned = 1;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	return n;
1446e0053ec0SHerbert Xu #undef C
1447e0053ec0SHerbert Xu }
1448e0053ec0SHerbert Xu 
1449e0053ec0SHerbert Xu /**
1450da29e4b4SJakub Kicinski  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1451da29e4b4SJakub Kicinski  * @first: first sk_buff of the msg
1452da29e4b4SJakub Kicinski  */
1453da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1454da29e4b4SJakub Kicinski {
1455da29e4b4SJakub Kicinski 	struct sk_buff *n;
1456da29e4b4SJakub Kicinski 
1457da29e4b4SJakub Kicinski 	n = alloc_skb(0, GFP_ATOMIC);
1458da29e4b4SJakub Kicinski 	if (!n)
1459da29e4b4SJakub Kicinski 		return NULL;
1460da29e4b4SJakub Kicinski 
1461da29e4b4SJakub Kicinski 	n->len = first->len;
1462da29e4b4SJakub Kicinski 	n->data_len = first->len;
1463da29e4b4SJakub Kicinski 	n->truesize = first->truesize;
1464da29e4b4SJakub Kicinski 
1465da29e4b4SJakub Kicinski 	skb_shinfo(n)->frag_list = first;
1466da29e4b4SJakub Kicinski 
1467da29e4b4SJakub Kicinski 	__copy_skb_header(n, first);
1468da29e4b4SJakub Kicinski 	n->destructor = NULL;
1469da29e4b4SJakub Kicinski 
1470da29e4b4SJakub Kicinski 	return n;
1471da29e4b4SJakub Kicinski }
1472da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1473da29e4b4SJakub Kicinski 
1474da29e4b4SJakub Kicinski /**
1475e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
1476e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
1477e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
1478e0053ec0SHerbert Xu  *
1479e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
1480e0053ec0SHerbert Xu  *	supplied by the user.
1481e0053ec0SHerbert Xu  *
1482e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
1483e0053ec0SHerbert Xu  */
1484e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1485e0053ec0SHerbert Xu {
1486b07a2d97SJakub Kicinski 	skb_release_all(dst, SKB_CONSUMED, false);
1487e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
1488e0053ec0SHerbert Xu }
1489e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
1490e0053ec0SHerbert Xu 
14916f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1492a91dbff5SWillem de Bruijn {
149314ade6baSWillem de Bruijn 	unsigned long max_pg, num_pg, new_pg, old_pg, rlim;
1494a91dbff5SWillem de Bruijn 	struct user_struct *user;
1495a91dbff5SWillem de Bruijn 
1496a91dbff5SWillem de Bruijn 	if (capable(CAP_IPC_LOCK) || !size)
1497a91dbff5SWillem de Bruijn 		return 0;
1498a91dbff5SWillem de Bruijn 
149914ade6baSWillem de Bruijn 	rlim = rlimit(RLIMIT_MEMLOCK);
150014ade6baSWillem de Bruijn 	if (rlim == RLIM_INFINITY)
150114ade6baSWillem de Bruijn 		return 0;
150214ade6baSWillem de Bruijn 
1503a91dbff5SWillem de Bruijn 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
150414ade6baSWillem de Bruijn 	max_pg = rlim >> PAGE_SHIFT;
1505a91dbff5SWillem de Bruijn 	user = mmp->user ? : current_user();
1506a91dbff5SWillem de Bruijn 
1507a91dbff5SWillem de Bruijn 	old_pg = atomic_long_read(&user->locked_vm);
150857fc05e8SEric Dumazet 	do {
1509a91dbff5SWillem de Bruijn 		new_pg = old_pg + num_pg;
1510a91dbff5SWillem de Bruijn 		if (new_pg > max_pg)
1511a91dbff5SWillem de Bruijn 			return -ENOBUFS;
151257fc05e8SEric Dumazet 	} while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg));
1513a91dbff5SWillem de Bruijn 
1514a91dbff5SWillem de Bruijn 	if (!mmp->user) {
1515a91dbff5SWillem de Bruijn 		mmp->user = get_uid(user);
1516a91dbff5SWillem de Bruijn 		mmp->num_pg = num_pg;
1517a91dbff5SWillem de Bruijn 	} else {
1518a91dbff5SWillem de Bruijn 		mmp->num_pg += num_pg;
1519a91dbff5SWillem de Bruijn 	}
1520a91dbff5SWillem de Bruijn 
1521a91dbff5SWillem de Bruijn 	return 0;
1522a91dbff5SWillem de Bruijn }
15236f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1524a91dbff5SWillem de Bruijn 
15256f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp)
1526a91dbff5SWillem de Bruijn {
1527a91dbff5SWillem de Bruijn 	if (mmp->user) {
1528a91dbff5SWillem de Bruijn 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1529a91dbff5SWillem de Bruijn 		free_uid(mmp->user);
1530a91dbff5SWillem de Bruijn 	}
1531a91dbff5SWillem de Bruijn }
15326f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1533a91dbff5SWillem de Bruijn 
1534c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
153552267790SWillem de Bruijn {
1536e7d2b510SPavel Begunkov 	struct ubuf_info_msgzc *uarg;
153752267790SWillem de Bruijn 	struct sk_buff *skb;
153852267790SWillem de Bruijn 
153952267790SWillem de Bruijn 	WARN_ON_ONCE(!in_task());
154052267790SWillem de Bruijn 
154152267790SWillem de Bruijn 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
154252267790SWillem de Bruijn 	if (!skb)
154352267790SWillem de Bruijn 		return NULL;
154452267790SWillem de Bruijn 
154552267790SWillem de Bruijn 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
154652267790SWillem de Bruijn 	uarg = (void *)skb->cb;
1547a91dbff5SWillem de Bruijn 	uarg->mmp.user = NULL;
1548a91dbff5SWillem de Bruijn 
1549a91dbff5SWillem de Bruijn 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
1550a91dbff5SWillem de Bruijn 		kfree_skb(skb);
1551a91dbff5SWillem de Bruijn 		return NULL;
1552a91dbff5SWillem de Bruijn 	}
155352267790SWillem de Bruijn 
1554e7d2b510SPavel Begunkov 	uarg->ubuf.callback = msg_zerocopy_callback;
15554ab6c99dSWillem de Bruijn 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
15564ab6c99dSWillem de Bruijn 	uarg->len = 1;
15574ab6c99dSWillem de Bruijn 	uarg->bytelen = size;
155852267790SWillem de Bruijn 	uarg->zerocopy = 1;
1559e7d2b510SPavel Begunkov 	uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
1560e7d2b510SPavel Begunkov 	refcount_set(&uarg->ubuf.refcnt, 1);
156152267790SWillem de Bruijn 	sock_hold(sk);
156252267790SWillem de Bruijn 
1563e7d2b510SPavel Begunkov 	return &uarg->ubuf;
156452267790SWillem de Bruijn }
156552267790SWillem de Bruijn 
1566e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg)
156752267790SWillem de Bruijn {
156852267790SWillem de Bruijn 	return container_of((void *)uarg, struct sk_buff, cb);
156952267790SWillem de Bruijn }
157052267790SWillem de Bruijn 
15718c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
15724ab6c99dSWillem de Bruijn 				       struct ubuf_info *uarg)
15734ab6c99dSWillem de Bruijn {
15744ab6c99dSWillem de Bruijn 	if (uarg) {
1575e7d2b510SPavel Begunkov 		struct ubuf_info_msgzc *uarg_zc;
15764ab6c99dSWillem de Bruijn 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
15774ab6c99dSWillem de Bruijn 		u32 bytelen, next;
15784ab6c99dSWillem de Bruijn 
15791b4b2b09SPavel Begunkov 		/* there might be non MSG_ZEROCOPY users */
15801b4b2b09SPavel Begunkov 		if (uarg->callback != msg_zerocopy_callback)
15811b4b2b09SPavel Begunkov 			return NULL;
15821b4b2b09SPavel Begunkov 
15834ab6c99dSWillem de Bruijn 		/* realloc only when socket is locked (TCP, UDP cork),
15844ab6c99dSWillem de Bruijn 		 * so uarg->len and sk_zckey access is serialized
15854ab6c99dSWillem de Bruijn 		 */
15864ab6c99dSWillem de Bruijn 		if (!sock_owned_by_user(sk)) {
15874ab6c99dSWillem de Bruijn 			WARN_ON_ONCE(1);
15884ab6c99dSWillem de Bruijn 			return NULL;
15894ab6c99dSWillem de Bruijn 		}
15904ab6c99dSWillem de Bruijn 
1591e7d2b510SPavel Begunkov 		uarg_zc = uarg_to_msgzc(uarg);
1592e7d2b510SPavel Begunkov 		bytelen = uarg_zc->bytelen + size;
1593e7d2b510SPavel Begunkov 		if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) {
15944ab6c99dSWillem de Bruijn 			/* TCP can create new skb to attach new uarg */
15954ab6c99dSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
15964ab6c99dSWillem de Bruijn 				goto new_alloc;
15974ab6c99dSWillem de Bruijn 			return NULL;
15984ab6c99dSWillem de Bruijn 		}
15994ab6c99dSWillem de Bruijn 
16004ab6c99dSWillem de Bruijn 		next = (u32)atomic_read(&sk->sk_zckey);
1601e7d2b510SPavel Begunkov 		if ((u32)(uarg_zc->id + uarg_zc->len) == next) {
1602e7d2b510SPavel Begunkov 			if (mm_account_pinned_pages(&uarg_zc->mmp, size))
1603a91dbff5SWillem de Bruijn 				return NULL;
1604e7d2b510SPavel Begunkov 			uarg_zc->len++;
1605e7d2b510SPavel Begunkov 			uarg_zc->bytelen = bytelen;
16064ab6c99dSWillem de Bruijn 			atomic_set(&sk->sk_zckey, ++next);
1607100f6d8eSWillem de Bruijn 
1608100f6d8eSWillem de Bruijn 			/* no extra ref when appending to datagram (MSG_MORE) */
1609100f6d8eSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
16108e044917SJonathan Lemon 				net_zcopy_get(uarg);
1611100f6d8eSWillem de Bruijn 
16124ab6c99dSWillem de Bruijn 			return uarg;
16134ab6c99dSWillem de Bruijn 		}
16144ab6c99dSWillem de Bruijn 	}
16154ab6c99dSWillem de Bruijn 
16164ab6c99dSWillem de Bruijn new_alloc:
16178c793822SJonathan Lemon 	return msg_zerocopy_alloc(sk, size);
16184ab6c99dSWillem de Bruijn }
16198c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
16204ab6c99dSWillem de Bruijn 
16214ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
16224ab6c99dSWillem de Bruijn {
16234ab6c99dSWillem de Bruijn 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
16244ab6c99dSWillem de Bruijn 	u32 old_lo, old_hi;
16254ab6c99dSWillem de Bruijn 	u64 sum_len;
16264ab6c99dSWillem de Bruijn 
16274ab6c99dSWillem de Bruijn 	old_lo = serr->ee.ee_info;
16284ab6c99dSWillem de Bruijn 	old_hi = serr->ee.ee_data;
16294ab6c99dSWillem de Bruijn 	sum_len = old_hi - old_lo + 1ULL + len;
16304ab6c99dSWillem de Bruijn 
16314ab6c99dSWillem de Bruijn 	if (sum_len >= (1ULL << 32))
16324ab6c99dSWillem de Bruijn 		return false;
16334ab6c99dSWillem de Bruijn 
16344ab6c99dSWillem de Bruijn 	if (lo != old_hi + 1)
16354ab6c99dSWillem de Bruijn 		return false;
16364ab6c99dSWillem de Bruijn 
16374ab6c99dSWillem de Bruijn 	serr->ee.ee_data += len;
16384ab6c99dSWillem de Bruijn 	return true;
16394ab6c99dSWillem de Bruijn }
16404ab6c99dSWillem de Bruijn 
1641e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg)
164252267790SWillem de Bruijn {
16434ab6c99dSWillem de Bruijn 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
164452267790SWillem de Bruijn 	struct sock_exterr_skb *serr;
164552267790SWillem de Bruijn 	struct sock *sk = skb->sk;
16464ab6c99dSWillem de Bruijn 	struct sk_buff_head *q;
16474ab6c99dSWillem de Bruijn 	unsigned long flags;
16483bdd5ee0SWillem de Bruijn 	bool is_zerocopy;
16494ab6c99dSWillem de Bruijn 	u32 lo, hi;
16504ab6c99dSWillem de Bruijn 	u16 len;
165152267790SWillem de Bruijn 
1652ccaffff1SWillem de Bruijn 	mm_unaccount_pinned_pages(&uarg->mmp);
1653ccaffff1SWillem de Bruijn 
16544ab6c99dSWillem de Bruijn 	/* if !len, there was only 1 call, and it was aborted
16554ab6c99dSWillem de Bruijn 	 * so do not queue a completion notification
16564ab6c99dSWillem de Bruijn 	 */
16574ab6c99dSWillem de Bruijn 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
165852267790SWillem de Bruijn 		goto release;
165952267790SWillem de Bruijn 
16604ab6c99dSWillem de Bruijn 	len = uarg->len;
16614ab6c99dSWillem de Bruijn 	lo = uarg->id;
16624ab6c99dSWillem de Bruijn 	hi = uarg->id + len - 1;
16633bdd5ee0SWillem de Bruijn 	is_zerocopy = uarg->zerocopy;
16644ab6c99dSWillem de Bruijn 
166552267790SWillem de Bruijn 	serr = SKB_EXT_ERR(skb);
166652267790SWillem de Bruijn 	memset(serr, 0, sizeof(*serr));
166752267790SWillem de Bruijn 	serr->ee.ee_errno = 0;
166852267790SWillem de Bruijn 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
16694ab6c99dSWillem de Bruijn 	serr->ee.ee_data = hi;
16704ab6c99dSWillem de Bruijn 	serr->ee.ee_info = lo;
16713bdd5ee0SWillem de Bruijn 	if (!is_zerocopy)
167252267790SWillem de Bruijn 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
167352267790SWillem de Bruijn 
16744ab6c99dSWillem de Bruijn 	q = &sk->sk_error_queue;
16754ab6c99dSWillem de Bruijn 	spin_lock_irqsave(&q->lock, flags);
16764ab6c99dSWillem de Bruijn 	tail = skb_peek_tail(q);
16774ab6c99dSWillem de Bruijn 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
16784ab6c99dSWillem de Bruijn 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
16794ab6c99dSWillem de Bruijn 		__skb_queue_tail(q, skb);
168052267790SWillem de Bruijn 		skb = NULL;
16814ab6c99dSWillem de Bruijn 	}
16824ab6c99dSWillem de Bruijn 	spin_unlock_irqrestore(&q->lock, flags);
168352267790SWillem de Bruijn 
1684e3ae2365SAlexander Aring 	sk_error_report(sk);
168552267790SWillem de Bruijn 
168652267790SWillem de Bruijn release:
168752267790SWillem de Bruijn 	consume_skb(skb);
168852267790SWillem de Bruijn 	sock_put(sk);
168952267790SWillem de Bruijn }
169075518851SJonathan Lemon 
16918c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
169236177832SJonathan Lemon 			   bool success)
169375518851SJonathan Lemon {
1694e7d2b510SPavel Begunkov 	struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg);
1695e7d2b510SPavel Begunkov 
1696e7d2b510SPavel Begunkov 	uarg_zc->zerocopy = uarg_zc->zerocopy & success;
169775518851SJonathan Lemon 
169875518851SJonathan Lemon 	if (refcount_dec_and_test(&uarg->refcnt))
1699e7d2b510SPavel Begunkov 		__msg_zerocopy_callback(uarg_zc);
170075518851SJonathan Lemon }
17018c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
170252267790SWillem de Bruijn 
17038c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
170452267790SWillem de Bruijn {
1705e7d2b510SPavel Begunkov 	struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk;
170652267790SWillem de Bruijn 
170752267790SWillem de Bruijn 	atomic_dec(&sk->sk_zckey);
1708e7d2b510SPavel Begunkov 	uarg_to_msgzc(uarg)->len--;
170952267790SWillem de Bruijn 
171052900d22SWillem de Bruijn 	if (have_uref)
17118c793822SJonathan Lemon 		msg_zerocopy_callback(NULL, uarg, true);
171252267790SWillem de Bruijn }
17138c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
171452267790SWillem de Bruijn 
171552267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
171652267790SWillem de Bruijn 			     struct msghdr *msg, int len,
171752267790SWillem de Bruijn 			     struct ubuf_info *uarg)
171852267790SWillem de Bruijn {
17194ab6c99dSWillem de Bruijn 	struct ubuf_info *orig_uarg = skb_zcopy(skb);
172052267790SWillem de Bruijn 	int err, orig_len = skb->len;
172152267790SWillem de Bruijn 
17224ab6c99dSWillem de Bruijn 	/* An skb can only point to one uarg. This edge case happens when
17234ab6c99dSWillem de Bruijn 	 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
17244ab6c99dSWillem de Bruijn 	 */
17254ab6c99dSWillem de Bruijn 	if (orig_uarg && uarg != orig_uarg)
17264ab6c99dSWillem de Bruijn 		return -EEXIST;
17274ab6c99dSWillem de Bruijn 
1728ebe73a28SDavid Ahern 	err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len);
172952267790SWillem de Bruijn 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
173054d43117SWillem de Bruijn 		struct sock *save_sk = skb->sk;
173154d43117SWillem de Bruijn 
173252267790SWillem de Bruijn 		/* Streams do not free skb on error. Reset to prev state. */
1733829b7bddSPavel Begunkov 		iov_iter_revert(&msg->msg_iter, skb->len - orig_len);
173454d43117SWillem de Bruijn 		skb->sk = sk;
173552267790SWillem de Bruijn 		___pskb_trim(skb, orig_len);
173654d43117SWillem de Bruijn 		skb->sk = save_sk;
173752267790SWillem de Bruijn 		return err;
173852267790SWillem de Bruijn 	}
173952267790SWillem de Bruijn 
174052900d22SWillem de Bruijn 	skb_zcopy_set(skb, uarg, NULL);
174152267790SWillem de Bruijn 	return skb->len - orig_len;
174252267790SWillem de Bruijn }
174352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
174452267790SWillem de Bruijn 
1745753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb)
1746753f1ca4SPavel Begunkov {
1747753f1ca4SPavel Begunkov 	int i;
1748753f1ca4SPavel Begunkov 
1749753f1ca4SPavel Begunkov 	skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS;
1750753f1ca4SPavel Begunkov 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1751753f1ca4SPavel Begunkov 		skb_frag_ref(skb, i);
1752753f1ca4SPavel Begunkov }
1753753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed);
1754753f1ca4SPavel Begunkov 
17551f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
175652267790SWillem de Bruijn 			      gfp_t gfp_mask)
175752267790SWillem de Bruijn {
175852267790SWillem de Bruijn 	if (skb_zcopy(orig)) {
175952267790SWillem de Bruijn 		if (skb_zcopy(nskb)) {
176052267790SWillem de Bruijn 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
176152267790SWillem de Bruijn 			if (!gfp_mask) {
176252267790SWillem de Bruijn 				WARN_ON_ONCE(1);
176352267790SWillem de Bruijn 				return -ENOMEM;
176452267790SWillem de Bruijn 			}
176552267790SWillem de Bruijn 			if (skb_uarg(nskb) == skb_uarg(orig))
176652267790SWillem de Bruijn 				return 0;
176752267790SWillem de Bruijn 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
176852267790SWillem de Bruijn 				return -EIO;
176952267790SWillem de Bruijn 		}
177052900d22SWillem de Bruijn 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
177152267790SWillem de Bruijn 	}
177252267790SWillem de Bruijn 	return 0;
177352267790SWillem de Bruijn }
177452267790SWillem de Bruijn 
17752c53040fSBen Hutchings /**
17762c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
177748c83012SMichael S. Tsirkin  *	@skb: the skb to modify
177848c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
177948c83012SMichael S. Tsirkin  *
178006b4feb3SJonathan Lemon  *	This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
178148c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
178248c83012SMichael S. Tsirkin  *	to userspace pages.
178348c83012SMichael S. Tsirkin  *
178448c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
178548c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
178648c83012SMichael S. Tsirkin  *
178748c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
178848c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
178948c83012SMichael S. Tsirkin  */
179048c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1791a6686f2fSShirley Ma {
1792a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
1793a6686f2fSShirley Ma 	struct page *page, *head = NULL;
17947e692df3SEric Dumazet 	int i, order, psize, new_frags;
17953ece7826SWillem de Bruijn 	u32 d_off;
1796a6686f2fSShirley Ma 
17973ece7826SWillem de Bruijn 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
17983ece7826SWillem de Bruijn 		return -EINVAL;
17993ece7826SWillem de Bruijn 
1800f72c4ac6SWillem de Bruijn 	if (!num_frags)
1801f72c4ac6SWillem de Bruijn 		goto release;
1802f72c4ac6SWillem de Bruijn 
18037e692df3SEric Dumazet 	/* We might have to allocate high order pages, so compute what minimum
18047e692df3SEric Dumazet 	 * page order is needed.
18057e692df3SEric Dumazet 	 */
18067e692df3SEric Dumazet 	order = 0;
18077e692df3SEric Dumazet 	while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb))
18087e692df3SEric Dumazet 		order++;
18097e692df3SEric Dumazet 	psize = (PAGE_SIZE << order);
18107e692df3SEric Dumazet 
18117e692df3SEric Dumazet 	new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order);
18123ece7826SWillem de Bruijn 	for (i = 0; i < new_frags; i++) {
18137e692df3SEric Dumazet 		page = alloc_pages(gfp_mask | __GFP_COMP, order);
1814a6686f2fSShirley Ma 		if (!page) {
1815a6686f2fSShirley Ma 			while (head) {
181640dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
1817a6686f2fSShirley Ma 				put_page(head);
1818a6686f2fSShirley Ma 				head = next;
1819a6686f2fSShirley Ma 			}
1820a6686f2fSShirley Ma 			return -ENOMEM;
1821a6686f2fSShirley Ma 		}
18223ece7826SWillem de Bruijn 		set_page_private(page, (unsigned long)head);
18233ece7826SWillem de Bruijn 		head = page;
18243ece7826SWillem de Bruijn 	}
18253ece7826SWillem de Bruijn 
18263ece7826SWillem de Bruijn 	page = head;
18273ece7826SWillem de Bruijn 	d_off = 0;
18283ece7826SWillem de Bruijn 	for (i = 0; i < num_frags; i++) {
18293ece7826SWillem de Bruijn 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
18303ece7826SWillem de Bruijn 		u32 p_off, p_len, copied;
18313ece7826SWillem de Bruijn 		struct page *p;
18323ece7826SWillem de Bruijn 		u8 *vaddr;
1833c613c209SWillem de Bruijn 
1834b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1835c613c209SWillem de Bruijn 				      p, p_off, p_len, copied) {
18363ece7826SWillem de Bruijn 			u32 copy, done = 0;
1837c613c209SWillem de Bruijn 			vaddr = kmap_atomic(p);
18383ece7826SWillem de Bruijn 
18393ece7826SWillem de Bruijn 			while (done < p_len) {
18407e692df3SEric Dumazet 				if (d_off == psize) {
18413ece7826SWillem de Bruijn 					d_off = 0;
18423ece7826SWillem de Bruijn 					page = (struct page *)page_private(page);
18433ece7826SWillem de Bruijn 				}
18447e692df3SEric Dumazet 				copy = min_t(u32, psize - d_off, p_len - done);
18453ece7826SWillem de Bruijn 				memcpy(page_address(page) + d_off,
18463ece7826SWillem de Bruijn 				       vaddr + p_off + done, copy);
18473ece7826SWillem de Bruijn 				done += copy;
18483ece7826SWillem de Bruijn 				d_off += copy;
18493ece7826SWillem de Bruijn 			}
185051c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1851c613c209SWillem de Bruijn 		}
1852a6686f2fSShirley Ma 	}
1853a6686f2fSShirley Ma 
1854a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
185502756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
1856a8605c60SIan Campbell 		skb_frag_unref(skb, i);
1857a6686f2fSShirley Ma 
1858a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
18593ece7826SWillem de Bruijn 	for (i = 0; i < new_frags - 1; i++) {
18607e692df3SEric Dumazet 		__skb_fill_page_desc(skb, i, head, 0, psize);
186140dadff2SSunghan Suh 		head = (struct page *)page_private(head);
1862a6686f2fSShirley Ma 	}
18633ece7826SWillem de Bruijn 	__skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
18643ece7826SWillem de Bruijn 	skb_shinfo(skb)->nr_frags = new_frags;
186548c83012SMichael S. Tsirkin 
1866b90ddd56SWillem de Bruijn release:
18671f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, false);
1868a6686f2fSShirley Ma 	return 0;
1869a6686f2fSShirley Ma }
1870dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1871a6686f2fSShirley Ma 
1872e0053ec0SHerbert Xu /**
1873e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
1874e0053ec0SHerbert Xu  *	@skb: buffer to clone
1875e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
1876e0053ec0SHerbert Xu  *
1877e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1878e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
1879e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
1880e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
1881e0053ec0SHerbert Xu  *
1882e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
1883e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
1884e0053ec0SHerbert Xu  */
1885e0053ec0SHerbert Xu 
1886e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1887e0053ec0SHerbert Xu {
1888d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
1889d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
1890d0bf4a9eSEric Dumazet 						       skb1);
18916ffe75ebSEric Dumazet 	struct sk_buff *n;
1892e0053ec0SHerbert Xu 
189370008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
1894a6686f2fSShirley Ma 		return NULL;
1895a6686f2fSShirley Ma 
1896e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
18972638595aSReshetova, Elena 	    refcount_read(&fclones->fclone_ref) == 1) {
18986ffe75ebSEric Dumazet 		n = &fclones->skb2;
18992638595aSReshetova, Elena 		refcount_set(&fclones->fclone_ref, 2);
1900c2dd4059SEric Dumazet 		n->fclone = SKB_FCLONE_CLONE;
1901e0053ec0SHerbert Xu 	} else {
1902c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
1903c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
1904c93bdd0eSMel Gorman 
1905025a785fSJakub Kicinski 		n = kmem_cache_alloc(skbuff_cache, gfp_mask);
1906e0053ec0SHerbert Xu 		if (!n)
1907e0053ec0SHerbert Xu 			return NULL;
1908fe55f6d5SVegard Nossum 
1909e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1910e0053ec0SHerbert Xu 	}
1911e0053ec0SHerbert Xu 
1912e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
19131da177e4SLinus Torvalds }
1914b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
19151da177e4SLinus Torvalds 
1916b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off)
1917f5b17294SPravin B Shelar {
1918030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
1919030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1920030737bcSEric Dumazet 		skb->csum_start += off;
1921f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1922f5b17294SPravin B Shelar 	skb->transport_header += off;
1923f5b17294SPravin B Shelar 	skb->network_header   += off;
1924f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1925f5b17294SPravin B Shelar 		skb->mac_header += off;
1926f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1927f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1928aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1929f5b17294SPravin B Shelar }
1930b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update);
1931f5b17294SPravin B Shelar 
193208303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
19331da177e4SLinus Torvalds {
1934dec18810SHerbert Xu 	__copy_skb_header(new, old);
1935dec18810SHerbert Xu 
19367967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
19377967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
19387967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
19391da177e4SLinus Torvalds }
194008303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header);
19411da177e4SLinus Torvalds 
1942c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1943c93bdd0eSMel Gorman {
1944c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1945c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1946c93bdd0eSMel Gorman 	return 0;
1947c93bdd0eSMel Gorman }
1948c93bdd0eSMel Gorman 
19491da177e4SLinus Torvalds /**
19501da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
19511da177e4SLinus Torvalds  *	@skb: buffer to copy
19521da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
19531da177e4SLinus Torvalds  *
19541da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
19551da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
19561da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
19571da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
19581da177e4SLinus Torvalds  *
19591da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
19601da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
19611da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
19621da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
19631da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
19641da177e4SLinus Torvalds  */
19651da177e4SLinus Torvalds 
1966dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
19671da177e4SLinus Torvalds {
19686602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1969ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1970c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1971c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
19726602cebbSEric Dumazet 
19731da177e4SLinus Torvalds 	if (!n)
19741da177e4SLinus Torvalds 		return NULL;
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	/* Set the data pointer */
19771da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
19781da177e4SLinus Torvalds 	/* Set the tail pointer and length */
19791da177e4SLinus Torvalds 	skb_put(n, skb->len);
19801da177e4SLinus Torvalds 
19819f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
19821da177e4SLinus Torvalds 
198308303c18SIlya Lesokhin 	skb_copy_header(n, skb);
19841da177e4SLinus Torvalds 	return n;
19851da177e4SLinus Torvalds }
1986b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds /**
1989bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
19901da177e4SLinus Torvalds  *	@skb: buffer to copy
1991117632e6SEric Dumazet  *	@headroom: headroom of new skb
19921da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1993bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1994bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1995bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
19961da177e4SLinus Torvalds  *
19971da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
19981da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
19991da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
20001da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
20011da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
20021da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
20031da177e4SLinus Torvalds  */
20041da177e4SLinus Torvalds 
2005bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
2006bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
20071da177e4SLinus Torvalds {
2008117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
2009bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
2010bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
20116602cebbSEric Dumazet 
20121da177e4SLinus Torvalds 	if (!n)
20131da177e4SLinus Torvalds 		goto out;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	/* Set the data pointer */
2016117632e6SEric Dumazet 	skb_reserve(n, headroom);
20171da177e4SLinus Torvalds 	/* Set the tail pointer and length */
20181da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
20191da177e4SLinus Torvalds 	/* Copy the bytes */
2020d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
20211da177e4SLinus Torvalds 
202225f484a6SHerbert Xu 	n->truesize += skb->data_len;
20231da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
20241da177e4SLinus Torvalds 	n->len	     = skb->len;
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
20271da177e4SLinus Torvalds 		int i;
20281da177e4SLinus Torvalds 
20291f8b977aSWillem de Bruijn 		if (skb_orphan_frags(skb, gfp_mask) ||
20301f8b977aSWillem de Bruijn 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
20311511022cSDan Carpenter 			kfree_skb(n);
20321511022cSDan Carpenter 			n = NULL;
2033a6686f2fSShirley Ma 			goto out;
2034a6686f2fSShirley Ma 		}
20351da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20361da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
2037ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
20381da177e4SLinus Torvalds 		}
20391da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 
204221dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
20431da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
20441da177e4SLinus Torvalds 		skb_clone_fraglist(n);
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
204708303c18SIlya Lesokhin 	skb_copy_header(n, skb);
20481da177e4SLinus Torvalds out:
20491da177e4SLinus Torvalds 	return n;
20501da177e4SLinus Torvalds }
2051bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds /**
20541da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
20551da177e4SLinus Torvalds  *	@skb: buffer to reallocate
20561da177e4SLinus Torvalds  *	@nhead: room to add at head
20571da177e4SLinus Torvalds  *	@ntail: room to add at tail
20581da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
20591da177e4SLinus Torvalds  *
2060bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
2061bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
20621da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
20631da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
20641da177e4SLinus Torvalds  *
20651da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
20661da177e4SLinus Torvalds  *	reloaded after call to this function.
20671da177e4SLinus Torvalds  */
20681da177e4SLinus Torvalds 
206986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
2070dd0fc66fSAl Viro 		     gfp_t gfp_mask)
20711da177e4SLinus Torvalds {
207212d6c1d3SKees Cook 	unsigned int osize = skb_end_offset(skb);
207312d6c1d3SKees Cook 	unsigned int size = osize + nhead + ntail;
20741da177e4SLinus Torvalds 	long off;
2075158f323bSEric Dumazet 	u8 *data;
207612d6c1d3SKees Cook 	int i;
20771da177e4SLinus Torvalds 
20784edd87adSHerbert Xu 	BUG_ON(nhead < 0);
20794edd87adSHerbert Xu 
20809f77fad3STim Hansen 	BUG_ON(skb_shared(skb));
20811da177e4SLinus Torvalds 
2082753f1ca4SPavel Begunkov 	skb_zcopy_downgrade_managed(skb);
2083753f1ca4SPavel Begunkov 
2084c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
2085c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
208612d6c1d3SKees Cook 
20875c0e820cSEric Dumazet 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
20881da177e4SLinus Torvalds 	if (!data)
20891da177e4SLinus Torvalds 		goto nodata;
209012d6c1d3SKees Cook 	size = SKB_WITH_OVERHEAD(size);
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
20936602cebbSEric Dumazet 	 * optimized for the cases when header is void.
20946602cebbSEric Dumazet 	 */
20956602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
20966602cebbSEric Dumazet 
20976602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
20986602cebbSEric Dumazet 	       skb_shinfo(skb),
2099fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
21001da177e4SLinus Torvalds 
21013e24591aSAlexander Duyck 	/*
21023e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
21033e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
21043e24591aSAlexander Duyck 	 * be since all we did is relocate the values
21053e24591aSAlexander Duyck 	 */
21063e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
210770008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
2108a6686f2fSShirley Ma 			goto nofrags;
21091f8b977aSWillem de Bruijn 		if (skb_zcopy(skb))
2110c1d1b437SEric Dumazet 			refcount_inc(&skb_uarg(skb)->refcnt);
21111da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2112ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
21131da177e4SLinus Torvalds 
211421dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
21151da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
21161da177e4SLinus Torvalds 
2117b07a2d97SJakub Kicinski 		skb_release_data(skb, SKB_CONSUMED, false);
21183e24591aSAlexander Duyck 	} else {
2119b07a2d97SJakub Kicinski 		skb_free_head(skb, false);
21201fd63041SEric Dumazet 	}
21211da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	skb->head     = data;
2124d3836f21SEric Dumazet 	skb->head_frag = 0;
21251da177e4SLinus Torvalds 	skb->data    += off;
2126763087daSEric Dumazet 
2127763087daSEric Dumazet 	skb_set_end_offset(skb, size);
21284305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
212956eb8882SPatrick McHardy 	off           = nhead;
213056eb8882SPatrick McHardy #endif
213127a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
2132b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
21331da177e4SLinus Torvalds 	skb->cloned   = 0;
2134334a8132SPatrick McHardy 	skb->hdr_len  = 0;
21351da177e4SLinus Torvalds 	skb->nohdr    = 0;
21361da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
2137158f323bSEric Dumazet 
2138de8f3a83SDaniel Borkmann 	skb_metadata_clear(skb);
2139de8f3a83SDaniel Borkmann 
2140158f323bSEric Dumazet 	/* It is not generally safe to change skb->truesize.
2141158f323bSEric Dumazet 	 * For the moment, we really care of rx path, or
2142158f323bSEric Dumazet 	 * when skb is orphaned (not attached to a socket).
2143158f323bSEric Dumazet 	 */
2144158f323bSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
2145158f323bSEric Dumazet 		skb->truesize += size - osize;
2146158f323bSEric Dumazet 
21471da177e4SLinus Torvalds 	return 0;
21481da177e4SLinus Torvalds 
2149a6686f2fSShirley Ma nofrags:
2150bf9f1baaSEric Dumazet 	skb_kfree_head(data, size);
21511da177e4SLinus Torvalds nodata:
21521da177e4SLinus Torvalds 	return -ENOMEM;
21531da177e4SLinus Torvalds }
2154b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
21591da177e4SLinus Torvalds {
21601da177e4SLinus Torvalds 	struct sk_buff *skb2;
21611da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	if (delta <= 0)
21641da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
21651da177e4SLinus Torvalds 	else {
21661da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
21671da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
21681da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
21691da177e4SLinus Torvalds 			kfree_skb(skb2);
21701da177e4SLinus Torvalds 			skb2 = NULL;
21711da177e4SLinus Torvalds 		}
21721da177e4SLinus Torvalds 	}
21731da177e4SLinus Torvalds 	return skb2;
21741da177e4SLinus Torvalds }
2175b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
21761da177e4SLinus Torvalds 
2177880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */
21782b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
21792b88cba5SEric Dumazet {
21802b88cba5SEric Dumazet 	unsigned int saved_end_offset, saved_truesize;
21812b88cba5SEric Dumazet 	struct skb_shared_info *shinfo;
21822b88cba5SEric Dumazet 	int res;
21832b88cba5SEric Dumazet 
21842b88cba5SEric Dumazet 	saved_end_offset = skb_end_offset(skb);
21852b88cba5SEric Dumazet 	saved_truesize = skb->truesize;
21862b88cba5SEric Dumazet 
21872b88cba5SEric Dumazet 	res = pskb_expand_head(skb, 0, 0, pri);
21882b88cba5SEric Dumazet 	if (res)
21892b88cba5SEric Dumazet 		return res;
21902b88cba5SEric Dumazet 
21912b88cba5SEric Dumazet 	skb->truesize = saved_truesize;
21922b88cba5SEric Dumazet 
21932b88cba5SEric Dumazet 	if (likely(skb_end_offset(skb) == saved_end_offset))
21942b88cba5SEric Dumazet 		return 0;
21952b88cba5SEric Dumazet 
2196880ce5f2SEric Dumazet 	/* We can not change skb->end if the original or new value
2197880ce5f2SEric Dumazet 	 * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head().
2198880ce5f2SEric Dumazet 	 */
2199880ce5f2SEric Dumazet 	if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM ||
2200880ce5f2SEric Dumazet 	    skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) {
2201880ce5f2SEric Dumazet 		/* We think this path should not be taken.
2202880ce5f2SEric Dumazet 		 * Add a temporary trace to warn us just in case.
2203880ce5f2SEric Dumazet 		 */
2204880ce5f2SEric Dumazet 		pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n",
2205880ce5f2SEric Dumazet 			    saved_end_offset, skb_end_offset(skb));
2206880ce5f2SEric Dumazet 		WARN_ON_ONCE(1);
2207880ce5f2SEric Dumazet 		return 0;
2208880ce5f2SEric Dumazet 	}
2209880ce5f2SEric Dumazet 
22102b88cba5SEric Dumazet 	shinfo = skb_shinfo(skb);
22112b88cba5SEric Dumazet 
22122b88cba5SEric Dumazet 	/* We are about to change back skb->end,
22132b88cba5SEric Dumazet 	 * we need to move skb_shinfo() to its new location.
22142b88cba5SEric Dumazet 	 */
22152b88cba5SEric Dumazet 	memmove(skb->head + saved_end_offset,
22162b88cba5SEric Dumazet 		shinfo,
22172b88cba5SEric Dumazet 		offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
22182b88cba5SEric Dumazet 
22192b88cba5SEric Dumazet 	skb_set_end_offset(skb, saved_end_offset);
22202b88cba5SEric Dumazet 
22212b88cba5SEric Dumazet 	return 0;
22222b88cba5SEric Dumazet }
22232b88cba5SEric Dumazet 
22241da177e4SLinus Torvalds /**
2225f1260ff1SVasily Averin  *	skb_expand_head - reallocate header of &sk_buff
2226f1260ff1SVasily Averin  *	@skb: buffer to reallocate
2227f1260ff1SVasily Averin  *	@headroom: needed headroom
2228f1260ff1SVasily Averin  *
2229f1260ff1SVasily Averin  *	Unlike skb_realloc_headroom, this one does not allocate a new skb
2230f1260ff1SVasily Averin  *	if possible; copies skb->sk to new skb as needed
2231f1260ff1SVasily Averin  *	and frees original skb in case of failures.
2232f1260ff1SVasily Averin  *
2233f1260ff1SVasily Averin  *	It expect increased headroom and generates warning otherwise.
2234f1260ff1SVasily Averin  */
2235f1260ff1SVasily Averin 
2236f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
2237f1260ff1SVasily Averin {
2238f1260ff1SVasily Averin 	int delta = headroom - skb_headroom(skb);
22397f678defSVasily Averin 	int osize = skb_end_offset(skb);
22407f678defSVasily Averin 	struct sock *sk = skb->sk;
2241f1260ff1SVasily Averin 
2242f1260ff1SVasily Averin 	if (WARN_ONCE(delta <= 0,
2243f1260ff1SVasily Averin 		      "%s is expecting an increase in the headroom", __func__))
2244f1260ff1SVasily Averin 		return skb;
2245f1260ff1SVasily Averin 
22467f678defSVasily Averin 	delta = SKB_DATA_ALIGN(delta);
22477f678defSVasily Averin 	/* pskb_expand_head() might crash, if skb is shared. */
22487f678defSVasily Averin 	if (skb_shared(skb) || !is_skb_wmem(skb)) {
2249f1260ff1SVasily Averin 		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2250f1260ff1SVasily Averin 
22517f678defSVasily Averin 		if (unlikely(!nskb))
22527f678defSVasily Averin 			goto fail;
22537f678defSVasily Averin 
22547f678defSVasily Averin 		if (sk)
22557f678defSVasily Averin 			skb_set_owner_w(nskb, sk);
2256f1260ff1SVasily Averin 		consume_skb(skb);
2257f1260ff1SVasily Averin 		skb = nskb;
2258f1260ff1SVasily Averin 	}
22597f678defSVasily Averin 	if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
22607f678defSVasily Averin 		goto fail;
22617f678defSVasily Averin 
22627f678defSVasily Averin 	if (sk && is_skb_wmem(skb)) {
22637f678defSVasily Averin 		delta = skb_end_offset(skb) - osize;
22647f678defSVasily Averin 		refcount_add(delta, &sk->sk_wmem_alloc);
22657f678defSVasily Averin 		skb->truesize += delta;
2266f1260ff1SVasily Averin 	}
2267f1260ff1SVasily Averin 	return skb;
22687f678defSVasily Averin 
22697f678defSVasily Averin fail:
22707f678defSVasily Averin 	kfree_skb(skb);
22717f678defSVasily Averin 	return NULL;
2272f1260ff1SVasily Averin }
2273f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head);
2274f1260ff1SVasily Averin 
2275f1260ff1SVasily Averin /**
22761da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
22771da177e4SLinus Torvalds  *	@skb: buffer to copy
22781da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
22791da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
22801da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
22811da177e4SLinus Torvalds  *
22821da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
22831da177e4SLinus Torvalds  *	allocate additional space.
22841da177e4SLinus Torvalds  *
22851da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
22861da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
22871da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
22881da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
22891da177e4SLinus Torvalds  *
22901da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
22911da177e4SLinus Torvalds  *	is called from an interrupt.
22921da177e4SLinus Torvalds  */
22931da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
229486a76cafSVictor Fusco 				int newheadroom, int newtailroom,
2295dd0fc66fSAl Viro 				gfp_t gfp_mask)
22961da177e4SLinus Torvalds {
22971da177e4SLinus Torvalds 	/*
22981da177e4SLinus Torvalds 	 *	Allocate the copy buffer
22991da177e4SLinus Torvalds 	 */
2300c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
2301c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
2302c93bdd0eSMel Gorman 					NUMA_NO_NODE);
2303efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
23041da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 	if (!n)
23071da177e4SLinus Torvalds 		return NULL;
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 	/* Set the tail pointer and length */
23121da177e4SLinus Torvalds 	skb_put(n, skb->len);
23131da177e4SLinus Torvalds 
2314efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
23151da177e4SLinus Torvalds 	head_copy_off = 0;
23161da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
23171da177e4SLinus Torvalds 		head_copy_len = newheadroom;
23181da177e4SLinus Torvalds 	else
23191da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	/* Copy the linear header and data. */
23229f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
23239f77fad3STim Hansen 			     skb->len + head_copy_len));
23241da177e4SLinus Torvalds 
232508303c18SIlya Lesokhin 	skb_copy_header(n, skb);
23261da177e4SLinus Torvalds 
2327030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
2328efd1e8d5SPatrick McHardy 
23291da177e4SLinus Torvalds 	return n;
23301da177e4SLinus Torvalds }
2331b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds /**
2334cd0a137aSFlorian Fainelli  *	__skb_pad		-	zero pad the tail of an skb
23351da177e4SLinus Torvalds  *	@skb: buffer to pad
23361da177e4SLinus Torvalds  *	@pad: space to pad
2337cd0a137aSFlorian Fainelli  *	@free_on_error: free buffer on error
23381da177e4SLinus Torvalds  *
23391da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
23401da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
23411da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
23421da177e4SLinus Torvalds  *
2343cd0a137aSFlorian Fainelli  *	May return error in out of memory cases. The skb is freed on error
2344cd0a137aSFlorian Fainelli  *	if @free_on_error is true.
23451da177e4SLinus Torvalds  */
23461da177e4SLinus Torvalds 
2347cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
23481da177e4SLinus Torvalds {
23495b057c6bSHerbert Xu 	int err;
23505b057c6bSHerbert Xu 	int ntail;
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
23535b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
23541da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
23555b057c6bSHerbert Xu 		return 0;
23561da177e4SLinus Torvalds 	}
23571da177e4SLinus Torvalds 
23584305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
23595b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
23605b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
23615b057c6bSHerbert Xu 		if (unlikely(err))
23625b057c6bSHerbert Xu 			goto free_skb;
23635b057c6bSHerbert Xu 	}
23645b057c6bSHerbert Xu 
23655b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
23665b057c6bSHerbert Xu 	 * to be audited.
23675b057c6bSHerbert Xu 	 */
23685b057c6bSHerbert Xu 	err = skb_linearize(skb);
23695b057c6bSHerbert Xu 	if (unlikely(err))
23705b057c6bSHerbert Xu 		goto free_skb;
23715b057c6bSHerbert Xu 
23725b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
23735b057c6bSHerbert Xu 	return 0;
23745b057c6bSHerbert Xu 
23755b057c6bSHerbert Xu free_skb:
2376cd0a137aSFlorian Fainelli 	if (free_on_error)
23771da177e4SLinus Torvalds 		kfree_skb(skb);
23785b057c6bSHerbert Xu 	return err;
23791da177e4SLinus Torvalds }
2380cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad);
23811da177e4SLinus Torvalds 
23820dde3e16SIlpo Järvinen /**
23830c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
23840c7ddf36SMathias Krause  *	@skb: start of the buffer to use
23850c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
23860c7ddf36SMathias Krause  *	@len: amount of data to add
23870c7ddf36SMathias Krause  *
23880c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
23890c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
23900c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
23910c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
23920c7ddf36SMathias Krause  *	returned.
23930c7ddf36SMathias Krause  */
23940c7ddf36SMathias Krause 
23954df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
23960c7ddf36SMathias Krause {
23970c7ddf36SMathias Krause 	if (tail != skb) {
23980c7ddf36SMathias Krause 		skb->data_len += len;
23990c7ddf36SMathias Krause 		skb->len += len;
24000c7ddf36SMathias Krause 	}
24010c7ddf36SMathias Krause 	return skb_put(tail, len);
24020c7ddf36SMathias Krause }
24030c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
24040c7ddf36SMathias Krause 
24050c7ddf36SMathias Krause /**
24060dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
24070dde3e16SIlpo Järvinen  *	@skb: buffer to use
24080dde3e16SIlpo Järvinen  *	@len: amount of data to add
24090dde3e16SIlpo Järvinen  *
24100dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
24110dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
24120dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
24130dde3e16SIlpo Järvinen  */
24144df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len)
24150dde3e16SIlpo Järvinen {
24164df864c1SJohannes Berg 	void *tmp = skb_tail_pointer(skb);
24170dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
24180dde3e16SIlpo Järvinen 	skb->tail += len;
24190dde3e16SIlpo Järvinen 	skb->len  += len;
24200dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
24210dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
24220dde3e16SIlpo Järvinen 	return tmp;
24230dde3e16SIlpo Järvinen }
24240dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
24250dde3e16SIlpo Järvinen 
24266be8ac2fSIlpo Järvinen /**
2427c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
2428c2aa270aSIlpo Järvinen  *	@skb: buffer to use
2429c2aa270aSIlpo Järvinen  *	@len: amount of data to add
2430c2aa270aSIlpo Järvinen  *
2431c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
2432c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
2433c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
2434c2aa270aSIlpo Järvinen  */
2435d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len)
2436c2aa270aSIlpo Järvinen {
2437c2aa270aSIlpo Järvinen 	skb->data -= len;
2438c2aa270aSIlpo Järvinen 	skb->len  += len;
2439c2aa270aSIlpo Järvinen 	if (unlikely(skb->data < skb->head))
2440c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
2441c2aa270aSIlpo Järvinen 	return skb->data;
2442c2aa270aSIlpo Järvinen }
2443c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
2444c2aa270aSIlpo Järvinen 
2445c2aa270aSIlpo Järvinen /**
24466be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
24476be8ac2fSIlpo Järvinen  *	@skb: buffer to use
24486be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
24496be8ac2fSIlpo Järvinen  *
24506be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
24516be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
24526be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
24536be8ac2fSIlpo Järvinen  *	the old data.
24546be8ac2fSIlpo Järvinen  */
2455af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len)
24566be8ac2fSIlpo Järvinen {
245747d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
24586be8ac2fSIlpo Järvinen }
24596be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
24606be8ac2fSIlpo Järvinen 
2461419ae74eSIlpo Järvinen /**
246213244cccSLuiz Augusto von Dentz  *	skb_pull_data - remove data from the start of a buffer returning its
246313244cccSLuiz Augusto von Dentz  *	original position.
246413244cccSLuiz Augusto von Dentz  *	@skb: buffer to use
246513244cccSLuiz Augusto von Dentz  *	@len: amount of data to remove
246613244cccSLuiz Augusto von Dentz  *
246713244cccSLuiz Augusto von Dentz  *	This function removes data from the start of a buffer, returning
246813244cccSLuiz Augusto von Dentz  *	the memory to the headroom. A pointer to the original data in the buffer
246913244cccSLuiz Augusto von Dentz  *	is returned after checking if there is enough data to pull. Once the
247013244cccSLuiz Augusto von Dentz  *	data has been pulled future pushes will overwrite the old data.
247113244cccSLuiz Augusto von Dentz  */
247213244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len)
247313244cccSLuiz Augusto von Dentz {
247413244cccSLuiz Augusto von Dentz 	void *data = skb->data;
247513244cccSLuiz Augusto von Dentz 
247613244cccSLuiz Augusto von Dentz 	if (skb->len < len)
247713244cccSLuiz Augusto von Dentz 		return NULL;
247813244cccSLuiz Augusto von Dentz 
247913244cccSLuiz Augusto von Dentz 	skb_pull(skb, len);
248013244cccSLuiz Augusto von Dentz 
248113244cccSLuiz Augusto von Dentz 	return data;
248213244cccSLuiz Augusto von Dentz }
248313244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data);
248413244cccSLuiz Augusto von Dentz 
248513244cccSLuiz Augusto von Dentz /**
2486419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
2487419ae74eSIlpo Järvinen  *	@skb: buffer to alter
2488419ae74eSIlpo Järvinen  *	@len: new length
2489419ae74eSIlpo Järvinen  *
2490419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
2491419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
2492419ae74eSIlpo Järvinen  *	The skb must be linear.
2493419ae74eSIlpo Järvinen  */
2494419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
2495419ae74eSIlpo Järvinen {
2496419ae74eSIlpo Järvinen 	if (skb->len > len)
2497419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
2498419ae74eSIlpo Järvinen }
2499419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
2500419ae74eSIlpo Järvinen 
25013cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
25021da177e4SLinus Torvalds  */
25031da177e4SLinus Torvalds 
25043cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
25051da177e4SLinus Torvalds {
250627b437c8SHerbert Xu 	struct sk_buff **fragp;
250727b437c8SHerbert Xu 	struct sk_buff *frag;
25081da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
25091da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
25101da177e4SLinus Torvalds 	int i;
251127b437c8SHerbert Xu 	int err;
251227b437c8SHerbert Xu 
251327b437c8SHerbert Xu 	if (skb_cloned(skb) &&
251427b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
251527b437c8SHerbert Xu 		return err;
25161da177e4SLinus Torvalds 
2517f4d26fb3SHerbert Xu 	i = 0;
2518f4d26fb3SHerbert Xu 	if (offset >= len)
2519f4d26fb3SHerbert Xu 		goto drop_pages;
2520f4d26fb3SHerbert Xu 
2521f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
25229e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
252327b437c8SHerbert Xu 
252427b437c8SHerbert Xu 		if (end < len) {
25251da177e4SLinus Torvalds 			offset = end;
252627b437c8SHerbert Xu 			continue;
25271da177e4SLinus Torvalds 		}
25281da177e4SLinus Torvalds 
25299e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
253027b437c8SHerbert Xu 
2531f4d26fb3SHerbert Xu drop_pages:
253227b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
253327b437c8SHerbert Xu 
253427b437c8SHerbert Xu 		for (; i < nfrags; i++)
2535ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
253627b437c8SHerbert Xu 
253721dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
253827b437c8SHerbert Xu 			skb_drop_fraglist(skb);
2539f4d26fb3SHerbert Xu 		goto done;
254027b437c8SHerbert Xu 	}
254127b437c8SHerbert Xu 
254227b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
254327b437c8SHerbert Xu 	     fragp = &frag->next) {
254427b437c8SHerbert Xu 		int end = offset + frag->len;
254527b437c8SHerbert Xu 
254627b437c8SHerbert Xu 		if (skb_shared(frag)) {
254727b437c8SHerbert Xu 			struct sk_buff *nfrag;
254827b437c8SHerbert Xu 
254927b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
255027b437c8SHerbert Xu 			if (unlikely(!nfrag))
255127b437c8SHerbert Xu 				return -ENOMEM;
255227b437c8SHerbert Xu 
255327b437c8SHerbert Xu 			nfrag->next = frag->next;
255485bb2a60SEric Dumazet 			consume_skb(frag);
255527b437c8SHerbert Xu 			frag = nfrag;
255627b437c8SHerbert Xu 			*fragp = frag;
255727b437c8SHerbert Xu 		}
255827b437c8SHerbert Xu 
255927b437c8SHerbert Xu 		if (end < len) {
256027b437c8SHerbert Xu 			offset = end;
256127b437c8SHerbert Xu 			continue;
256227b437c8SHerbert Xu 		}
256327b437c8SHerbert Xu 
256427b437c8SHerbert Xu 		if (end > len &&
256527b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
256627b437c8SHerbert Xu 			return err;
256727b437c8SHerbert Xu 
256827b437c8SHerbert Xu 		if (frag->next)
256927b437c8SHerbert Xu 			skb_drop_list(&frag->next);
257027b437c8SHerbert Xu 		break;
257127b437c8SHerbert Xu 	}
257227b437c8SHerbert Xu 
2573f4d26fb3SHerbert Xu done:
257427b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
25751da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
25761da177e4SLinus Torvalds 		skb->len       = len;
25771da177e4SLinus Torvalds 	} else {
25781da177e4SLinus Torvalds 		skb->len       = len;
25791da177e4SLinus Torvalds 		skb->data_len  = 0;
258027a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
25811da177e4SLinus Torvalds 	}
25821da177e4SLinus Torvalds 
2583c21b48ccSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
2584c21b48ccSEric Dumazet 		skb_condense(skb);
25851da177e4SLinus Torvalds 	return 0;
25861da177e4SLinus Torvalds }
2587b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
25881da177e4SLinus Torvalds 
258988078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly
259088078d98SEric Dumazet  */
259188078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
259288078d98SEric Dumazet {
259388078d98SEric Dumazet 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
259488078d98SEric Dumazet 		int delta = skb->len - len;
259588078d98SEric Dumazet 
2596d55bef50SDimitris Michailidis 		skb->csum = csum_block_sub(skb->csum,
2597d55bef50SDimitris Michailidis 					   skb_checksum(skb, len, delta, 0),
2598d55bef50SDimitris Michailidis 					   len);
259954970a2fSVasily Averin 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
260054970a2fSVasily Averin 		int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
260154970a2fSVasily Averin 		int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
260254970a2fSVasily Averin 
260354970a2fSVasily Averin 		if (offset + sizeof(__sum16) > hdlen)
260454970a2fSVasily Averin 			return -EINVAL;
260588078d98SEric Dumazet 	}
260688078d98SEric Dumazet 	return __pskb_trim(skb, len);
260788078d98SEric Dumazet }
260888078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow);
260988078d98SEric Dumazet 
26101da177e4SLinus Torvalds /**
26111da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
26121da177e4SLinus Torvalds  *	@skb: buffer to reallocate
26131da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
26141da177e4SLinus Torvalds  *
26151da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
26161da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
26171da177e4SLinus Torvalds  *	data from fragmented part.
26181da177e4SLinus Torvalds  *
26191da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
26201da177e4SLinus Torvalds  *
26211da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
26221da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
26231da177e4SLinus Torvalds  *
26241da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
26251da177e4SLinus Torvalds  *	reloaded after call to this function.
26261da177e4SLinus Torvalds  */
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
26291da177e4SLinus Torvalds  * when it is necessary.
26301da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
26311da177e4SLinus Torvalds  * 2. It may change skb pointers.
26321da177e4SLinus Torvalds  *
26331da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
26341da177e4SLinus Torvalds  */
2635af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta)
26361da177e4SLinus Torvalds {
26371da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
26381da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
26391da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
26401da177e4SLinus Torvalds 	 */
26414305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
26441da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
26451da177e4SLinus Torvalds 				     GFP_ATOMIC))
26461da177e4SLinus Torvalds 			return NULL;
26471da177e4SLinus Torvalds 	}
26481da177e4SLinus Torvalds 
26499f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
26509f77fad3STim Hansen 			     skb_tail_pointer(skb), delta));
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
26531da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
26541da177e4SLinus Torvalds 	 */
265521dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
26561da177e4SLinus Torvalds 		goto pull_pages;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
26591da177e4SLinus Torvalds 	eat = delta;
26601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26619e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
26629e903e08SEric Dumazet 
26639e903e08SEric Dumazet 		if (size >= eat)
26641da177e4SLinus Torvalds 			goto pull_pages;
26659e903e08SEric Dumazet 		eat -= size;
26661da177e4SLinus Torvalds 	}
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
266909001b03SWenhua Shi 	 * Certainly, it is possible to add an offset to skb data,
26701da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
26711da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
26721da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
26731da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
26741da177e4SLinus Torvalds 	 */
26751da177e4SLinus Torvalds 	if (eat) {
26761da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
26771da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
26781da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds 		do {
26811da177e4SLinus Torvalds 			if (list->len <= eat) {
26821da177e4SLinus Torvalds 				/* Eaten as whole. */
26831da177e4SLinus Torvalds 				eat -= list->len;
26841da177e4SLinus Torvalds 				list = list->next;
26851da177e4SLinus Torvalds 				insp = list;
26861da177e4SLinus Torvalds 			} else {
26871da177e4SLinus Torvalds 				/* Eaten partially. */
26882d7afdcbSSubash Abhinov Kasiviswanathan 				if (skb_is_gso(skb) && !list->head_frag &&
26892d7afdcbSSubash Abhinov Kasiviswanathan 				    skb_headlen(list))
26902d7afdcbSSubash Abhinov Kasiviswanathan 					skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 				if (skb_shared(list)) {
26931da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
26941da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
26951da177e4SLinus Torvalds 					if (!clone)
26961da177e4SLinus Torvalds 						return NULL;
26971da177e4SLinus Torvalds 					insp = list->next;
26981da177e4SLinus Torvalds 					list = clone;
26991da177e4SLinus Torvalds 				} else {
27001da177e4SLinus Torvalds 					/* This may be pulled without
27011da177e4SLinus Torvalds 					 * problems. */
27021da177e4SLinus Torvalds 					insp = list;
27031da177e4SLinus Torvalds 				}
27041da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
27051da177e4SLinus Torvalds 					kfree_skb(clone);
27061da177e4SLinus Torvalds 					return NULL;
27071da177e4SLinus Torvalds 				}
27081da177e4SLinus Torvalds 				break;
27091da177e4SLinus Torvalds 			}
27101da177e4SLinus Torvalds 		} while (eat);
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds 		/* Free pulled out fragments. */
27131da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
27141da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
2715ef527f96SEric Dumazet 			consume_skb(list);
27161da177e4SLinus Torvalds 		}
27171da177e4SLinus Torvalds 		/* And insert new clone at head. */
27181da177e4SLinus Torvalds 		if (clone) {
27191da177e4SLinus Torvalds 			clone->next = list;
27201da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
27211da177e4SLinus Torvalds 		}
27221da177e4SLinus Torvalds 	}
27231da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds pull_pages:
27261da177e4SLinus Torvalds 	eat = delta;
27271da177e4SLinus Torvalds 	k = 0;
27281da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27299e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
27309e903e08SEric Dumazet 
27319e903e08SEric Dumazet 		if (size <= eat) {
2732ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
27339e903e08SEric Dumazet 			eat -= size;
27341da177e4SLinus Torvalds 		} else {
2735b54c9d5bSJonathan Lemon 			skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2736b54c9d5bSJonathan Lemon 
2737b54c9d5bSJonathan Lemon 			*frag = skb_shinfo(skb)->frags[i];
27381da177e4SLinus Torvalds 			if (eat) {
2739b54c9d5bSJonathan Lemon 				skb_frag_off_add(frag, eat);
2740b54c9d5bSJonathan Lemon 				skb_frag_size_sub(frag, eat);
27413ccc6c6fSlinzhang 				if (!i)
27423ccc6c6fSlinzhang 					goto end;
27431da177e4SLinus Torvalds 				eat = 0;
27441da177e4SLinus Torvalds 			}
27451da177e4SLinus Torvalds 			k++;
27461da177e4SLinus Torvalds 		}
27471da177e4SLinus Torvalds 	}
27481da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
27491da177e4SLinus Torvalds 
27503ccc6c6fSlinzhang end:
27511da177e4SLinus Torvalds 	skb->tail     += delta;
27521da177e4SLinus Torvalds 	skb->data_len -= delta;
27531da177e4SLinus Torvalds 
27541f8b977aSWillem de Bruijn 	if (!skb->data_len)
27551f8b977aSWillem de Bruijn 		skb_zcopy_clear(skb, false);
27561f8b977aSWillem de Bruijn 
275727a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
27581da177e4SLinus Torvalds }
2759b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
27601da177e4SLinus Torvalds 
276122019b17SEric Dumazet /**
276222019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
276322019b17SEric Dumazet  *	@skb: source skb
276422019b17SEric Dumazet  *	@offset: offset in source
276522019b17SEric Dumazet  *	@to: destination buffer
276622019b17SEric Dumazet  *	@len: number of bytes to copy
276722019b17SEric Dumazet  *
276822019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
276922019b17SEric Dumazet  *	destination buffer.
277022019b17SEric Dumazet  *
277122019b17SEric Dumazet  *	CAUTION ! :
277222019b17SEric Dumazet  *		If its prototype is ever changed,
277322019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
277422019b17SEric Dumazet  *		since it is called from BPF assembly code.
277522019b17SEric Dumazet  */
27761da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
27771da177e4SLinus Torvalds {
27781a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2779fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2780fbb398a8SDavid S. Miller 	int i, copy;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
27831da177e4SLinus Torvalds 		goto fault;
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 	/* Copy header. */
27861a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
27871da177e4SLinus Torvalds 		if (copy > len)
27881da177e4SLinus Torvalds 			copy = len;
2789d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
27901da177e4SLinus Torvalds 		if ((len -= copy) == 0)
27911da177e4SLinus Torvalds 			return 0;
27921da177e4SLinus Torvalds 		offset += copy;
27931da177e4SLinus Torvalds 		to     += copy;
27941da177e4SLinus Torvalds 	}
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27971a028e50SDavid S. Miller 		int end;
279851c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
27991da177e4SLinus Torvalds 
2800547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28011a028e50SDavid S. Miller 
280251c56b00SEric Dumazet 		end = start + skb_frag_size(f);
28031da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2804c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2805c613c209SWillem de Bruijn 			struct page *p;
28061da177e4SLinus Torvalds 			u8 *vaddr;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 			if (copy > len)
28091da177e4SLinus Torvalds 				copy = len;
28101da177e4SLinus Torvalds 
2811c613c209SWillem de Bruijn 			skb_frag_foreach_page(f,
2812b54c9d5bSJonathan Lemon 					      skb_frag_off(f) + offset - start,
2813c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2814c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2815c613c209SWillem de Bruijn 				memcpy(to + copied, vaddr + p_off, p_len);
281651c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2817c613c209SWillem de Bruijn 			}
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 			if ((len -= copy) == 0)
28201da177e4SLinus Torvalds 				return 0;
28211da177e4SLinus Torvalds 			offset += copy;
28221da177e4SLinus Torvalds 			to     += copy;
28231da177e4SLinus Torvalds 		}
28241a028e50SDavid S. Miller 		start = end;
28251da177e4SLinus Torvalds 	}
28261da177e4SLinus Torvalds 
2827fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
28281a028e50SDavid S. Miller 		int end;
28291da177e4SLinus Torvalds 
2830547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28311a028e50SDavid S. Miller 
2832fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
28331da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
28341da177e4SLinus Torvalds 			if (copy > len)
28351da177e4SLinus Torvalds 				copy = len;
2836fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
28371da177e4SLinus Torvalds 				goto fault;
28381da177e4SLinus Torvalds 			if ((len -= copy) == 0)
28391da177e4SLinus Torvalds 				return 0;
28401da177e4SLinus Torvalds 			offset += copy;
28411da177e4SLinus Torvalds 			to     += copy;
28421da177e4SLinus Torvalds 		}
28431a028e50SDavid S. Miller 		start = end;
28441da177e4SLinus Torvalds 	}
2845a6686f2fSShirley Ma 
28461da177e4SLinus Torvalds 	if (!len)
28471da177e4SLinus Torvalds 		return 0;
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds fault:
28501da177e4SLinus Torvalds 	return -EFAULT;
28511da177e4SLinus Torvalds }
2852b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
28531da177e4SLinus Torvalds 
28549c55e01cSJens Axboe /*
28559c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
28569c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
28579c55e01cSJens Axboe  */
28589c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
28599c55e01cSJens Axboe {
28608b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
28618b9d3728SJarek Poplawski }
28629c55e01cSJens Axboe 
2863a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
28644fb66994SJarek Poplawski 				   unsigned int *offset,
286518aafc62SEric Dumazet 				   struct sock *sk)
28668b9d3728SJarek Poplawski {
28675640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
28688b9d3728SJarek Poplawski 
28695640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
28708b9d3728SJarek Poplawski 		return NULL;
28714fb66994SJarek Poplawski 
28725640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
28734fb66994SJarek Poplawski 
28745640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
28755640f768SEric Dumazet 	       page_address(page) + *offset, *len);
28765640f768SEric Dumazet 	*offset = pfrag->offset;
28775640f768SEric Dumazet 	pfrag->offset += *len;
28784fb66994SJarek Poplawski 
28795640f768SEric Dumazet 	return pfrag->page;
28809c55e01cSJens Axboe }
28819c55e01cSJens Axboe 
288241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
288341c73a0dSEric Dumazet 			     struct page *page,
288441c73a0dSEric Dumazet 			     unsigned int offset)
288541c73a0dSEric Dumazet {
288641c73a0dSEric Dumazet 	return	spd->nr_pages &&
288741c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
288841c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
288941c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
289041c73a0dSEric Dumazet }
289141c73a0dSEric Dumazet 
28929c55e01cSJens Axboe /*
28939c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
28949c55e01cSJens Axboe  */
2895a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
289635f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
28974fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
289818aafc62SEric Dumazet 			  bool linear,
28997a67e56fSJarek Poplawski 			  struct sock *sk)
29009c55e01cSJens Axboe {
290141c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2902a108d5f3SDavid S. Miller 		return true;
29039c55e01cSJens Axboe 
29048b9d3728SJarek Poplawski 	if (linear) {
290518aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
29068b9d3728SJarek Poplawski 		if (!page)
2907a108d5f3SDavid S. Miller 			return true;
290841c73a0dSEric Dumazet 	}
290941c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
291041c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
2911a108d5f3SDavid S. Miller 		return false;
291241c73a0dSEric Dumazet 	}
29138b9d3728SJarek Poplawski 	get_page(page);
29149c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
29154fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
29169c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
29179c55e01cSJens Axboe 	spd->nr_pages++;
29188b9d3728SJarek Poplawski 
2919a108d5f3SDavid S. Miller 	return false;
29209c55e01cSJens Axboe }
29219c55e01cSJens Axboe 
2922a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
29232870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
292418aafc62SEric Dumazet 			     unsigned int *len,
2925d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
292635f3d14dSJens Axboe 			     struct sock *sk,
292735f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
29289c55e01cSJens Axboe {
29292870c43dSOctavian Purdila 	if (!*len)
2930a108d5f3SDavid S. Miller 		return true;
29319c55e01cSJens Axboe 
29322870c43dSOctavian Purdila 	/* skip this segment if already processed */
29332870c43dSOctavian Purdila 	if (*off >= plen) {
29342870c43dSOctavian Purdila 		*off -= plen;
2935a108d5f3SDavid S. Miller 		return false;
29362870c43dSOctavian Purdila 	}
29372870c43dSOctavian Purdila 
29382870c43dSOctavian Purdila 	/* ignore any bits we already processed */
29399ca1b22dSEric Dumazet 	poff += *off;
29409ca1b22dSEric Dumazet 	plen -= *off;
29412870c43dSOctavian Purdila 	*off = 0;
29422870c43dSOctavian Purdila 
294318aafc62SEric Dumazet 	do {
294418aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
29452870c43dSOctavian Purdila 
294618aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
294718aafc62SEric Dumazet 				  linear, sk))
2948a108d5f3SDavid S. Miller 			return true;
294918aafc62SEric Dumazet 		poff += flen;
295018aafc62SEric Dumazet 		plen -= flen;
29512870c43dSOctavian Purdila 		*len -= flen;
295218aafc62SEric Dumazet 	} while (*len && plen);
29532870c43dSOctavian Purdila 
2954a108d5f3SDavid S. Miller 	return false;
2955db43a282SOctavian Purdila }
29569c55e01cSJens Axboe 
29579c55e01cSJens Axboe /*
2958a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
29592870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
29609c55e01cSJens Axboe  */
2961a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
296235f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
296335f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
29642870c43dSOctavian Purdila {
29652870c43dSOctavian Purdila 	int seg;
2966fa9835e5STom Herbert 	struct sk_buff *iter;
29679c55e01cSJens Axboe 
29681d0c0b32SEric Dumazet 	/* map the linear part :
29692996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
29702996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
29712996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
29729c55e01cSJens Axboe 	 */
29732870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
29742870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
29752870c43dSOctavian Purdila 			     skb_headlen(skb),
297618aafc62SEric Dumazet 			     offset, len, spd,
29773a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
29781d0c0b32SEric Dumazet 			     sk, pipe))
2979a108d5f3SDavid S. Miller 		return true;
29809c55e01cSJens Axboe 
29819c55e01cSJens Axboe 	/*
29829c55e01cSJens Axboe 	 * then map the fragments
29839c55e01cSJens Axboe 	 */
29849c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
29859c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
29869c55e01cSJens Axboe 
2987ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
2988b54c9d5bSJonathan Lemon 				     skb_frag_off(f), skb_frag_size(f),
298918aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
2990a108d5f3SDavid S. Miller 			return true;
29919c55e01cSJens Axboe 	}
29929c55e01cSJens Axboe 
2993fa9835e5STom Herbert 	skb_walk_frags(skb, iter) {
2994fa9835e5STom Herbert 		if (*offset >= iter->len) {
2995fa9835e5STom Herbert 			*offset -= iter->len;
2996fa9835e5STom Herbert 			continue;
2997fa9835e5STom Herbert 		}
2998fa9835e5STom Herbert 		/* __skb_splice_bits() only fails if the output has no room
2999fa9835e5STom Herbert 		 * left, so no point in going over the frag_list for the error
3000fa9835e5STom Herbert 		 * case.
3001fa9835e5STom Herbert 		 */
3002fa9835e5STom Herbert 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
3003fa9835e5STom Herbert 			return true;
3004fa9835e5STom Herbert 	}
3005fa9835e5STom Herbert 
3006a108d5f3SDavid S. Miller 	return false;
30079c55e01cSJens Axboe }
30089c55e01cSJens Axboe 
30099c55e01cSJens Axboe /*
30109c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
3011fa9835e5STom Herbert  * the fragments, and the frag list.
30129c55e01cSJens Axboe  */
3013a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
30149c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
301525869262SAl Viro 		    unsigned int flags)
30169c55e01cSJens Axboe {
301741c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
301841c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
30199c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
30209c55e01cSJens Axboe 		.pages = pages,
30219c55e01cSJens Axboe 		.partial = partial,
3022047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
302328a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
30249c55e01cSJens Axboe 		.spd_release = sock_spd_release,
30259c55e01cSJens Axboe 	};
302635f3d14dSJens Axboe 	int ret = 0;
302735f3d14dSJens Axboe 
3028fa9835e5STom Herbert 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
30299c55e01cSJens Axboe 
3030a60e3cc7SHannes Frederic Sowa 	if (spd.nr_pages)
303125869262SAl Viro 		ret = splice_to_pipe(pipe, &spd);
30329c55e01cSJens Axboe 
303335f3d14dSJens Axboe 	return ret;
30349c55e01cSJens Axboe }
30352b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
30369c55e01cSJens Axboe 
3037c729ed6fSDavid Howells static int sendmsg_locked(struct sock *sk, struct msghdr *msg)
3038c729ed6fSDavid Howells {
3039c729ed6fSDavid Howells 	struct socket *sock = sk->sk_socket;
3040c729ed6fSDavid Howells 	size_t size = msg_data_left(msg);
3041c729ed6fSDavid Howells 
3042c729ed6fSDavid Howells 	if (!sock)
3043c729ed6fSDavid Howells 		return -EINVAL;
3044c729ed6fSDavid Howells 
3045c729ed6fSDavid Howells 	if (!sock->ops->sendmsg_locked)
3046c729ed6fSDavid Howells 		return sock_no_sendmsg_locked(sk, msg, size);
3047c729ed6fSDavid Howells 
3048c729ed6fSDavid Howells 	return sock->ops->sendmsg_locked(sk, msg, size);
3049c729ed6fSDavid Howells }
3050c729ed6fSDavid Howells 
3051c729ed6fSDavid Howells static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg)
30520739cd28SCong Wang {
30530739cd28SCong Wang 	struct socket *sock = sk->sk_socket;
30540739cd28SCong Wang 
30550739cd28SCong Wang 	if (!sock)
30560739cd28SCong Wang 		return -EINVAL;
3057c729ed6fSDavid Howells 	return sock_sendmsg(sock, msg);
30580739cd28SCong Wang }
30590739cd28SCong Wang 
3060c729ed6fSDavid Howells typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg);
30610739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
3062c729ed6fSDavid Howells 			   int len, sendmsg_func sendmsg)
306320bf50deSTom Herbert {
306420bf50deSTom Herbert 	unsigned int orig_len = len;
306520bf50deSTom Herbert 	struct sk_buff *head = skb;
306620bf50deSTom Herbert 	unsigned short fragidx;
306720bf50deSTom Herbert 	int slen, ret;
306820bf50deSTom Herbert 
306920bf50deSTom Herbert do_frag_list:
307020bf50deSTom Herbert 
307120bf50deSTom Herbert 	/* Deal with head data */
307220bf50deSTom Herbert 	while (offset < skb_headlen(skb) && len) {
307320bf50deSTom Herbert 		struct kvec kv;
307420bf50deSTom Herbert 		struct msghdr msg;
307520bf50deSTom Herbert 
307620bf50deSTom Herbert 		slen = min_t(int, len, skb_headlen(skb) - offset);
307720bf50deSTom Herbert 		kv.iov_base = skb->data + offset;
3078db5980d8SJohn Fastabend 		kv.iov_len = slen;
307920bf50deSTom Herbert 		memset(&msg, 0, sizeof(msg));
3080bd95e678SJohn Fastabend 		msg.msg_flags = MSG_DONTWAIT;
308120bf50deSTom Herbert 
3082c729ed6fSDavid Howells 		iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen);
3083c729ed6fSDavid Howells 		ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3084c729ed6fSDavid Howells 				      sendmsg_unlocked, sk, &msg);
308520bf50deSTom Herbert 		if (ret <= 0)
308620bf50deSTom Herbert 			goto error;
308720bf50deSTom Herbert 
308820bf50deSTom Herbert 		offset += ret;
308920bf50deSTom Herbert 		len -= ret;
309020bf50deSTom Herbert 	}
309120bf50deSTom Herbert 
309220bf50deSTom Herbert 	/* All the data was skb head? */
309320bf50deSTom Herbert 	if (!len)
309420bf50deSTom Herbert 		goto out;
309520bf50deSTom Herbert 
309620bf50deSTom Herbert 	/* Make offset relative to start of frags */
309720bf50deSTom Herbert 	offset -= skb_headlen(skb);
309820bf50deSTom Herbert 
309920bf50deSTom Herbert 	/* Find where we are in frag list */
310020bf50deSTom Herbert 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
310120bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
310220bf50deSTom Herbert 
3103d8e18a51SMatthew Wilcox (Oracle) 		if (offset < skb_frag_size(frag))
310420bf50deSTom Herbert 			break;
310520bf50deSTom Herbert 
3106d8e18a51SMatthew Wilcox (Oracle) 		offset -= skb_frag_size(frag);
310720bf50deSTom Herbert 	}
310820bf50deSTom Herbert 
310920bf50deSTom Herbert 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
311020bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
311120bf50deSTom Herbert 
3112d8e18a51SMatthew Wilcox (Oracle) 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
311320bf50deSTom Herbert 
311420bf50deSTom Herbert 		while (slen) {
3115c729ed6fSDavid Howells 			struct bio_vec bvec;
3116c729ed6fSDavid Howells 			struct msghdr msg = {
3117c729ed6fSDavid Howells 				.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT,
3118c729ed6fSDavid Howells 			};
3119c729ed6fSDavid Howells 
3120c729ed6fSDavid Howells 			bvec_set_page(&bvec, skb_frag_page(frag), slen,
3121c729ed6fSDavid Howells 				      skb_frag_off(frag) + offset);
3122c729ed6fSDavid Howells 			iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
3123c729ed6fSDavid Howells 				      slen);
3124c729ed6fSDavid Howells 
3125c729ed6fSDavid Howells 			ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3126c729ed6fSDavid Howells 					      sendmsg_unlocked, sk, &msg);
312720bf50deSTom Herbert 			if (ret <= 0)
312820bf50deSTom Herbert 				goto error;
312920bf50deSTom Herbert 
313020bf50deSTom Herbert 			len -= ret;
313120bf50deSTom Herbert 			offset += ret;
313220bf50deSTom Herbert 			slen -= ret;
313320bf50deSTom Herbert 		}
313420bf50deSTom Herbert 
313520bf50deSTom Herbert 		offset = 0;
313620bf50deSTom Herbert 	}
313720bf50deSTom Herbert 
313820bf50deSTom Herbert 	if (len) {
313920bf50deSTom Herbert 		/* Process any frag lists */
314020bf50deSTom Herbert 
314120bf50deSTom Herbert 		if (skb == head) {
314220bf50deSTom Herbert 			if (skb_has_frag_list(skb)) {
314320bf50deSTom Herbert 				skb = skb_shinfo(skb)->frag_list;
314420bf50deSTom Herbert 				goto do_frag_list;
314520bf50deSTom Herbert 			}
314620bf50deSTom Herbert 		} else if (skb->next) {
314720bf50deSTom Herbert 			skb = skb->next;
314820bf50deSTom Herbert 			goto do_frag_list;
314920bf50deSTom Herbert 		}
315020bf50deSTom Herbert 	}
315120bf50deSTom Herbert 
315220bf50deSTom Herbert out:
315320bf50deSTom Herbert 	return orig_len - len;
315420bf50deSTom Herbert 
315520bf50deSTom Herbert error:
315620bf50deSTom Herbert 	return orig_len == len ? ret : orig_len - len;
315720bf50deSTom Herbert }
31580739cd28SCong Wang 
31590739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */
31600739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
31610739cd28SCong Wang 			 int len)
31620739cd28SCong Wang {
3163c729ed6fSDavid Howells 	return __skb_send_sock(sk, skb, offset, len, sendmsg_locked);
31640739cd28SCong Wang }
316520bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked);
316620bf50deSTom Herbert 
31670739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */
31680739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
31690739cd28SCong Wang {
3170c729ed6fSDavid Howells 	return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked);
31710739cd28SCong Wang }
31720739cd28SCong Wang 
3173357b40a1SHerbert Xu /**
3174357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
3175357b40a1SHerbert Xu  *	@skb: destination buffer
3176357b40a1SHerbert Xu  *	@offset: offset in destination
3177357b40a1SHerbert Xu  *	@from: source buffer
3178357b40a1SHerbert Xu  *	@len: number of bytes to copy
3179357b40a1SHerbert Xu  *
3180357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
3181357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
3182357b40a1SHerbert Xu  *	traversing fragment lists and such.
3183357b40a1SHerbert Xu  */
3184357b40a1SHerbert Xu 
31850c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
3186357b40a1SHerbert Xu {
31871a028e50SDavid S. Miller 	int start = skb_headlen(skb);
3188fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3189fbb398a8SDavid S. Miller 	int i, copy;
3190357b40a1SHerbert Xu 
3191357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
3192357b40a1SHerbert Xu 		goto fault;
3193357b40a1SHerbert Xu 
31941a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
3195357b40a1SHerbert Xu 		if (copy > len)
3196357b40a1SHerbert Xu 			copy = len;
319727d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
3198357b40a1SHerbert Xu 		if ((len -= copy) == 0)
3199357b40a1SHerbert Xu 			return 0;
3200357b40a1SHerbert Xu 		offset += copy;
3201357b40a1SHerbert Xu 		from += copy;
3202357b40a1SHerbert Xu 	}
3203357b40a1SHerbert Xu 
3204357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3205357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
32061a028e50SDavid S. Miller 		int end;
3207357b40a1SHerbert Xu 
3208547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32091a028e50SDavid S. Miller 
32109e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
3211357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
3212c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
3213c613c209SWillem de Bruijn 			struct page *p;
3214357b40a1SHerbert Xu 			u8 *vaddr;
3215357b40a1SHerbert Xu 
3216357b40a1SHerbert Xu 			if (copy > len)
3217357b40a1SHerbert Xu 				copy = len;
3218357b40a1SHerbert Xu 
3219c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
3220b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
3221c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
3222c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
3223c613c209SWillem de Bruijn 				memcpy(vaddr + p_off, from + copied, p_len);
322451c56b00SEric Dumazet 				kunmap_atomic(vaddr);
3225c613c209SWillem de Bruijn 			}
3226357b40a1SHerbert Xu 
3227357b40a1SHerbert Xu 			if ((len -= copy) == 0)
3228357b40a1SHerbert Xu 				return 0;
3229357b40a1SHerbert Xu 			offset += copy;
3230357b40a1SHerbert Xu 			from += copy;
3231357b40a1SHerbert Xu 		}
32321a028e50SDavid S. Miller 		start = end;
3233357b40a1SHerbert Xu 	}
3234357b40a1SHerbert Xu 
3235fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
32361a028e50SDavid S. Miller 		int end;
3237357b40a1SHerbert Xu 
3238547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32391a028e50SDavid S. Miller 
3240fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3241357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
3242357b40a1SHerbert Xu 			if (copy > len)
3243357b40a1SHerbert Xu 				copy = len;
3244fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
32451a028e50SDavid S. Miller 					   from, copy))
3246357b40a1SHerbert Xu 				goto fault;
3247357b40a1SHerbert Xu 			if ((len -= copy) == 0)
3248357b40a1SHerbert Xu 				return 0;
3249357b40a1SHerbert Xu 			offset += copy;
3250357b40a1SHerbert Xu 			from += copy;
3251357b40a1SHerbert Xu 		}
32521a028e50SDavid S. Miller 		start = end;
3253357b40a1SHerbert Xu 	}
3254357b40a1SHerbert Xu 	if (!len)
3255357b40a1SHerbert Xu 		return 0;
3256357b40a1SHerbert Xu 
3257357b40a1SHerbert Xu fault:
3258357b40a1SHerbert Xu 	return -EFAULT;
3259357b40a1SHerbert Xu }
3260357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
3261357b40a1SHerbert Xu 
32621da177e4SLinus Torvalds /* Checksum skb data. */
32632817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
32642817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
32651da177e4SLinus Torvalds {
32661a028e50SDavid S. Miller 	int start = skb_headlen(skb);
32671a028e50SDavid S. Miller 	int i, copy = start - offset;
3268fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
32691da177e4SLinus Torvalds 	int pos = 0;
32701da177e4SLinus Torvalds 
32711da177e4SLinus Torvalds 	/* Checksum header. */
32721da177e4SLinus Torvalds 	if (copy > 0) {
32731da177e4SLinus Torvalds 		if (copy > len)
32741da177e4SLinus Torvalds 			copy = len;
32752544af03SMatteo Croce 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
32762544af03SMatteo Croce 				       skb->data + offset, copy, csum);
32771da177e4SLinus Torvalds 		if ((len -= copy) == 0)
32781da177e4SLinus Torvalds 			return csum;
32791da177e4SLinus Torvalds 		offset += copy;
32801da177e4SLinus Torvalds 		pos	= copy;
32811da177e4SLinus Torvalds 	}
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
32841a028e50SDavid S. Miller 		int end;
328551c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
32861da177e4SLinus Torvalds 
3287547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32881a028e50SDavid S. Miller 
328951c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
32901da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
3291c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
3292c613c209SWillem de Bruijn 			struct page *p;
329344bb9363SAl Viro 			__wsum csum2;
32941da177e4SLinus Torvalds 			u8 *vaddr;
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 			if (copy > len)
32971da177e4SLinus Torvalds 				copy = len;
3298c613c209SWillem de Bruijn 
3299c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
3300b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
3301c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
3302c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
33032544af03SMatteo Croce 				csum2 = INDIRECT_CALL_1(ops->update,
33042544af03SMatteo Croce 							csum_partial_ext,
33052544af03SMatteo Croce 							vaddr + p_off, p_len, 0);
330651c56b00SEric Dumazet 				kunmap_atomic(vaddr);
33072544af03SMatteo Croce 				csum = INDIRECT_CALL_1(ops->combine,
33082544af03SMatteo Croce 						       csum_block_add_ext, csum,
33092544af03SMatteo Croce 						       csum2, pos, p_len);
3310c613c209SWillem de Bruijn 				pos += p_len;
3311c613c209SWillem de Bruijn 			}
3312c613c209SWillem de Bruijn 
33131da177e4SLinus Torvalds 			if (!(len -= copy))
33141da177e4SLinus Torvalds 				return csum;
33151da177e4SLinus Torvalds 			offset += copy;
33161da177e4SLinus Torvalds 		}
33171a028e50SDavid S. Miller 		start = end;
33181da177e4SLinus Torvalds 	}
33191da177e4SLinus Torvalds 
3320fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
33211a028e50SDavid S. Miller 		int end;
33221da177e4SLinus Torvalds 
3323547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33241a028e50SDavid S. Miller 
3325fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
33261da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
33275f92a738SAl Viro 			__wsum csum2;
33281da177e4SLinus Torvalds 			if (copy > len)
33291da177e4SLinus Torvalds 				copy = len;
33302817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
33312817a336SDaniel Borkmann 					       copy, 0, ops);
33322544af03SMatteo Croce 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
33332544af03SMatteo Croce 					       csum, csum2, pos, copy);
33341da177e4SLinus Torvalds 			if ((len -= copy) == 0)
33351da177e4SLinus Torvalds 				return csum;
33361da177e4SLinus Torvalds 			offset += copy;
33371da177e4SLinus Torvalds 			pos    += copy;
33381da177e4SLinus Torvalds 		}
33391a028e50SDavid S. Miller 		start = end;
33401da177e4SLinus Torvalds 	}
334109a62660SKris Katterjohn 	BUG_ON(len);
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds 	return csum;
33441da177e4SLinus Torvalds }
33452817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
33462817a336SDaniel Borkmann 
33472817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
33482817a336SDaniel Borkmann 		    int len, __wsum csum)
33492817a336SDaniel Borkmann {
33502817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
3351cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
33522817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
33532817a336SDaniel Borkmann 	};
33542817a336SDaniel Borkmann 
33552817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
33562817a336SDaniel Borkmann }
3357b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds /* Both of above in one bottle. */
33601da177e4SLinus Torvalds 
336181d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
33628d5930dfSAl Viro 				    u8 *to, int len)
33631da177e4SLinus Torvalds {
33641a028e50SDavid S. Miller 	int start = skb_headlen(skb);
33651a028e50SDavid S. Miller 	int i, copy = start - offset;
3366fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
33671da177e4SLinus Torvalds 	int pos = 0;
33688d5930dfSAl Viro 	__wsum csum = 0;
33691da177e4SLinus Torvalds 
33701da177e4SLinus Torvalds 	/* Copy header. */
33711da177e4SLinus Torvalds 	if (copy > 0) {
33721da177e4SLinus Torvalds 		if (copy > len)
33731da177e4SLinus Torvalds 			copy = len;
33741da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
3375cc44c17bSAl Viro 						 copy);
33761da177e4SLinus Torvalds 		if ((len -= copy) == 0)
33771da177e4SLinus Torvalds 			return csum;
33781da177e4SLinus Torvalds 		offset += copy;
33791da177e4SLinus Torvalds 		to     += copy;
33801da177e4SLinus Torvalds 		pos	= copy;
33811da177e4SLinus Torvalds 	}
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
33841a028e50SDavid S. Miller 		int end;
33851da177e4SLinus Torvalds 
3386547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33871a028e50SDavid S. Miller 
33889e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
33891da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
3390c613c209SWillem de Bruijn 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3391c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
3392c613c209SWillem de Bruijn 			struct page *p;
33935084205fSAl Viro 			__wsum csum2;
33941da177e4SLinus Torvalds 			u8 *vaddr;
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 			if (copy > len)
33971da177e4SLinus Torvalds 				copy = len;
3398c613c209SWillem de Bruijn 
3399c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
3400b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
3401c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
3402c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
3403c613c209SWillem de Bruijn 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
3404c613c209SWillem de Bruijn 								  to + copied,
3405cc44c17bSAl Viro 								  p_len);
340651c56b00SEric Dumazet 				kunmap_atomic(vaddr);
34071da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
3408c613c209SWillem de Bruijn 				pos += p_len;
3409c613c209SWillem de Bruijn 			}
3410c613c209SWillem de Bruijn 
34111da177e4SLinus Torvalds 			if (!(len -= copy))
34121da177e4SLinus Torvalds 				return csum;
34131da177e4SLinus Torvalds 			offset += copy;
34141da177e4SLinus Torvalds 			to     += copy;
34151da177e4SLinus Torvalds 		}
34161a028e50SDavid S. Miller 		start = end;
34171da177e4SLinus Torvalds 	}
34181da177e4SLinus Torvalds 
3419fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
342081d77662SAl Viro 		__wsum csum2;
34211a028e50SDavid S. Miller 		int end;
34221da177e4SLinus Torvalds 
3423547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
34241a028e50SDavid S. Miller 
3425fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
34261da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
34271da177e4SLinus Torvalds 			if (copy > len)
34281da177e4SLinus Torvalds 				copy = len;
3429fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
34301a028e50SDavid S. Miller 						       offset - start,
34318d5930dfSAl Viro 						       to, copy);
34321da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
34331da177e4SLinus Torvalds 			if ((len -= copy) == 0)
34341da177e4SLinus Torvalds 				return csum;
34351da177e4SLinus Torvalds 			offset += copy;
34361da177e4SLinus Torvalds 			to     += copy;
34371da177e4SLinus Torvalds 			pos    += copy;
34381da177e4SLinus Torvalds 		}
34391a028e50SDavid S. Miller 		start = end;
34401da177e4SLinus Torvalds 	}
344109a62660SKris Katterjohn 	BUG_ON(len);
34421da177e4SLinus Torvalds 	return csum;
34431da177e4SLinus Torvalds }
3444b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
34451da177e4SLinus Torvalds 
344649f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
344749f8e832SCong Wang {
344849f8e832SCong Wang 	__sum16 sum;
344949f8e832SCong Wang 
345049f8e832SCong Wang 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
345114641931SCong Wang 	/* See comments in __skb_checksum_complete(). */
345249f8e832SCong Wang 	if (likely(!sum)) {
345349f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
345449f8e832SCong Wang 		    !skb->csum_complete_sw)
34557fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
345649f8e832SCong Wang 	}
345749f8e832SCong Wang 	if (!skb_shared(skb))
345849f8e832SCong Wang 		skb->csum_valid = !sum;
345949f8e832SCong Wang 	return sum;
346049f8e832SCong Wang }
346149f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head);
346249f8e832SCong Wang 
346314641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum,
346414641931SCong Wang  * which has been changed from the hardware checksum, for example, by
346514641931SCong Wang  * __skb_checksum_validate_complete(). And, the original skb->csum must
346614641931SCong Wang  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
346714641931SCong Wang  *
346814641931SCong Wang  * It returns non-zero if the recomputed checksum is still invalid, otherwise
346914641931SCong Wang  * zero. The new checksum is stored back into skb->csum unless the skb is
347014641931SCong Wang  * shared.
347114641931SCong Wang  */
347249f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb)
347349f8e832SCong Wang {
347449f8e832SCong Wang 	__wsum csum;
347549f8e832SCong Wang 	__sum16 sum;
347649f8e832SCong Wang 
347749f8e832SCong Wang 	csum = skb_checksum(skb, 0, skb->len, 0);
347849f8e832SCong Wang 
347949f8e832SCong Wang 	sum = csum_fold(csum_add(skb->csum, csum));
348014641931SCong Wang 	/* This check is inverted, because we already knew the hardware
348114641931SCong Wang 	 * checksum is invalid before calling this function. So, if the
348214641931SCong Wang 	 * re-computed checksum is valid instead, then we have a mismatch
348314641931SCong Wang 	 * between the original skb->csum and skb_checksum(). This means either
348414641931SCong Wang 	 * the original hardware checksum is incorrect or we screw up skb->csum
348514641931SCong Wang 	 * when moving skb->data around.
348614641931SCong Wang 	 */
348749f8e832SCong Wang 	if (likely(!sum)) {
348849f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
348949f8e832SCong Wang 		    !skb->csum_complete_sw)
34907fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
349149f8e832SCong Wang 	}
349249f8e832SCong Wang 
349349f8e832SCong Wang 	if (!skb_shared(skb)) {
349449f8e832SCong Wang 		/* Save full packet checksum */
349549f8e832SCong Wang 		skb->csum = csum;
349649f8e832SCong Wang 		skb->ip_summed = CHECKSUM_COMPLETE;
349749f8e832SCong Wang 		skb->csum_complete_sw = 1;
349849f8e832SCong Wang 		skb->csum_valid = !sum;
349949f8e832SCong Wang 	}
350049f8e832SCong Wang 
350149f8e832SCong Wang 	return sum;
350249f8e832SCong Wang }
350349f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete);
350449f8e832SCong Wang 
35059617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
35069617813dSDavide Caratti {
35079617813dSDavide Caratti 	net_warn_ratelimited(
35089617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
35099617813dSDavide Caratti 		__func__);
35109617813dSDavide Caratti 	return 0;
35119617813dSDavide Caratti }
35129617813dSDavide Caratti 
35139617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
35149617813dSDavide Caratti 				       int offset, int len)
35159617813dSDavide Caratti {
35169617813dSDavide Caratti 	net_warn_ratelimited(
35179617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
35189617813dSDavide Caratti 		__func__);
35199617813dSDavide Caratti 	return 0;
35209617813dSDavide Caratti }
35219617813dSDavide Caratti 
35229617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = {
35239617813dSDavide Caratti 	.update  = warn_crc32c_csum_update,
35249617813dSDavide Caratti 	.combine = warn_crc32c_csum_combine,
35259617813dSDavide Caratti };
35269617813dSDavide Caratti 
35279617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
35289617813dSDavide Caratti 	&default_crc32c_ops;
35299617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub);
35309617813dSDavide Caratti 
3531af2806f8SThomas Graf  /**
3532af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3533af2806f8SThomas Graf  *	@from: source buffer
3534af2806f8SThomas Graf  *
3535af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
3536af2806f8SThomas Graf  *	into skb_zerocopy().
3537af2806f8SThomas Graf  */
3538af2806f8SThomas Graf unsigned int
3539af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
3540af2806f8SThomas Graf {
3541af2806f8SThomas Graf 	unsigned int hlen = 0;
3542af2806f8SThomas Graf 
3543af2806f8SThomas Graf 	if (!from->head_frag ||
3544af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
3545a17ad096SPravin B Shelar 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3546af2806f8SThomas Graf 		hlen = skb_headlen(from);
3547a17ad096SPravin B Shelar 		if (!hlen)
3548a17ad096SPravin B Shelar 			hlen = from->len;
3549a17ad096SPravin B Shelar 	}
3550af2806f8SThomas Graf 
3551af2806f8SThomas Graf 	if (skb_has_frag_list(from))
3552af2806f8SThomas Graf 		hlen = from->len;
3553af2806f8SThomas Graf 
3554af2806f8SThomas Graf 	return hlen;
3555af2806f8SThomas Graf }
3556af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3557af2806f8SThomas Graf 
3558af2806f8SThomas Graf /**
3559af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
3560af2806f8SThomas Graf  *	@to: destination buffer
35617fceb4deSMasanari Iida  *	@from: source buffer
3562af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
3563af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
3564af2806f8SThomas Graf  *
3565af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
3566af2806f8SThomas Graf  *	to the frags in the source buffer.
3567af2806f8SThomas Graf  *
3568af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3569af2806f8SThomas Graf  *	headroom in the `to` buffer.
357036d5fe6aSZoltan Kiss  *
357136d5fe6aSZoltan Kiss  *	Return value:
357236d5fe6aSZoltan Kiss  *	0: everything is OK
357336d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
357436d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
3575af2806f8SThomas Graf  */
357636d5fe6aSZoltan Kiss int
357736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3578af2806f8SThomas Graf {
3579af2806f8SThomas Graf 	int i, j = 0;
3580af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
358136d5fe6aSZoltan Kiss 	int ret;
3582af2806f8SThomas Graf 	struct page *page;
3583af2806f8SThomas Graf 	unsigned int offset;
3584af2806f8SThomas Graf 
3585af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
3586af2806f8SThomas Graf 
3587af2806f8SThomas Graf 	/* dont bother with small payloads */
358836d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
358936d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
3590af2806f8SThomas Graf 
3591af2806f8SThomas Graf 	if (hlen) {
359236d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
359336d5fe6aSZoltan Kiss 		if (unlikely(ret))
359436d5fe6aSZoltan Kiss 			return ret;
3595af2806f8SThomas Graf 		len -= hlen;
3596af2806f8SThomas Graf 	} else {
3597af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
3598af2806f8SThomas Graf 		if (plen) {
3599af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
3600af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
3601af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
3602af2806f8SThomas Graf 			get_page(page);
3603af2806f8SThomas Graf 			j = 1;
3604af2806f8SThomas Graf 			len -= plen;
3605af2806f8SThomas Graf 		}
3606af2806f8SThomas Graf 	}
3607af2806f8SThomas Graf 
3608ede57d58SRichard Gobert 	skb_len_add(to, len + plen);
3609af2806f8SThomas Graf 
361036d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
361136d5fe6aSZoltan Kiss 		skb_tx_error(from);
361236d5fe6aSZoltan Kiss 		return -ENOMEM;
361336d5fe6aSZoltan Kiss 	}
36141f8b977aSWillem de Bruijn 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
361536d5fe6aSZoltan Kiss 
3616af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3617d8e18a51SMatthew Wilcox (Oracle) 		int size;
3618d8e18a51SMatthew Wilcox (Oracle) 
3619af2806f8SThomas Graf 		if (!len)
3620af2806f8SThomas Graf 			break;
3621af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3622d8e18a51SMatthew Wilcox (Oracle) 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3623d8e18a51SMatthew Wilcox (Oracle) 					len);
3624d8e18a51SMatthew Wilcox (Oracle) 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3625d8e18a51SMatthew Wilcox (Oracle) 		len -= size;
3626af2806f8SThomas Graf 		skb_frag_ref(to, j);
3627af2806f8SThomas Graf 		j++;
3628af2806f8SThomas Graf 	}
3629af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
363036d5fe6aSZoltan Kiss 
363136d5fe6aSZoltan Kiss 	return 0;
3632af2806f8SThomas Graf }
3633af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
3634af2806f8SThomas Graf 
36351da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
36361da177e4SLinus Torvalds {
3637d3bc23e7SAl Viro 	__wsum csum;
36381da177e4SLinus Torvalds 	long csstart;
36391da177e4SLinus Torvalds 
364084fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
364155508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
36421da177e4SLinus Torvalds 	else
36431da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
36441da177e4SLinus Torvalds 
364509a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
36461da177e4SLinus Torvalds 
3647d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	csum = 0;
36501da177e4SLinus Torvalds 	if (csstart != skb->len)
36511da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
36528d5930dfSAl Viro 					      skb->len - csstart);
36531da177e4SLinus Torvalds 
365484fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3655ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
36561da177e4SLinus Torvalds 
3657d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
36581da177e4SLinus Torvalds 	}
36591da177e4SLinus Torvalds }
3660b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds /**
36631da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
36641da177e4SLinus Torvalds  *	@list: list to dequeue from
36651da177e4SLinus Torvalds  *
36661da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
36671da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
36681da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
36691da177e4SLinus Torvalds  */
36701da177e4SLinus Torvalds 
36711da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
36721da177e4SLinus Torvalds {
36731da177e4SLinus Torvalds 	unsigned long flags;
36741da177e4SLinus Torvalds 	struct sk_buff *result;
36751da177e4SLinus Torvalds 
36761da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
36771da177e4SLinus Torvalds 	result = __skb_dequeue(list);
36781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
36791da177e4SLinus Torvalds 	return result;
36801da177e4SLinus Torvalds }
3681b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
36821da177e4SLinus Torvalds 
36831da177e4SLinus Torvalds /**
36841da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
36851da177e4SLinus Torvalds  *	@list: list to dequeue from
36861da177e4SLinus Torvalds  *
36871da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
36881da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
36891da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
36901da177e4SLinus Torvalds  */
36911da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
36921da177e4SLinus Torvalds {
36931da177e4SLinus Torvalds 	unsigned long flags;
36941da177e4SLinus Torvalds 	struct sk_buff *result;
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
36971da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
36981da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
36991da177e4SLinus Torvalds 	return result;
37001da177e4SLinus Torvalds }
3701b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
37021da177e4SLinus Torvalds 
37031da177e4SLinus Torvalds /**
3704*4025d3e7SEric Dumazet  *	skb_queue_purge_reason - empty a list
37051da177e4SLinus Torvalds  *	@list: list to empty
3706*4025d3e7SEric Dumazet  *	@reason: drop reason
37071da177e4SLinus Torvalds  *
37081da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
37091da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
37101da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
37111da177e4SLinus Torvalds  */
3712*4025d3e7SEric Dumazet void skb_queue_purge_reason(struct sk_buff_head *list,
3713*4025d3e7SEric Dumazet 			    enum skb_drop_reason reason)
37141da177e4SLinus Torvalds {
37151da177e4SLinus Torvalds 	struct sk_buff *skb;
3716*4025d3e7SEric Dumazet 
37171da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
3718*4025d3e7SEric Dumazet 		kfree_skb_reason(skb, reason);
37191da177e4SLinus Torvalds }
3720*4025d3e7SEric Dumazet EXPORT_SYMBOL(skb_queue_purge_reason);
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds /**
37239f5afeaeSYaogong Wang  *	skb_rbtree_purge - empty a skb rbtree
37249f5afeaeSYaogong Wang  *	@root: root of the rbtree to empty
3725385114deSPeter Oskolkov  *	Return value: the sum of truesizes of all purged skbs.
37269f5afeaeSYaogong Wang  *
37279f5afeaeSYaogong Wang  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
37289f5afeaeSYaogong Wang  *	the list and one reference dropped. This function does not take
37299f5afeaeSYaogong Wang  *	any lock. Synchronization should be handled by the caller (e.g., TCP
37309f5afeaeSYaogong Wang  *	out-of-order queue is protected by the socket lock).
37319f5afeaeSYaogong Wang  */
3732385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root)
37339f5afeaeSYaogong Wang {
37347c90584cSEric Dumazet 	struct rb_node *p = rb_first(root);
3735385114deSPeter Oskolkov 	unsigned int sum = 0;
37369f5afeaeSYaogong Wang 
37377c90584cSEric Dumazet 	while (p) {
37387c90584cSEric Dumazet 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
37397c90584cSEric Dumazet 
37407c90584cSEric Dumazet 		p = rb_next(p);
37417c90584cSEric Dumazet 		rb_erase(&skb->rbnode, root);
3742385114deSPeter Oskolkov 		sum += skb->truesize;
37439f5afeaeSYaogong Wang 		kfree_skb(skb);
37447c90584cSEric Dumazet 	}
3745385114deSPeter Oskolkov 	return sum;
37469f5afeaeSYaogong Wang }
37479f5afeaeSYaogong Wang 
37489f5afeaeSYaogong Wang /**
37491da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
37501da177e4SLinus Torvalds  *	@list: list to use
37511da177e4SLinus Torvalds  *	@newsk: buffer to queue
37521da177e4SLinus Torvalds  *
37531da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
37541da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
37551da177e4SLinus Torvalds  *	safely.
37561da177e4SLinus Torvalds  *
37571da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
37581da177e4SLinus Torvalds  */
37591da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
37601da177e4SLinus Torvalds {
37611da177e4SLinus Torvalds 	unsigned long flags;
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
37641da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
37651da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
37661da177e4SLinus Torvalds }
3767b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
37681da177e4SLinus Torvalds 
37691da177e4SLinus Torvalds /**
37701da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
37711da177e4SLinus Torvalds  *	@list: list to use
37721da177e4SLinus Torvalds  *	@newsk: buffer to queue
37731da177e4SLinus Torvalds  *
37741da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
37751da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
37761da177e4SLinus Torvalds  *	safely.
37771da177e4SLinus Torvalds  *
37781da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
37791da177e4SLinus Torvalds  */
37801da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
37811da177e4SLinus Torvalds {
37821da177e4SLinus Torvalds 	unsigned long flags;
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
37851da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
37861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
37871da177e4SLinus Torvalds }
3788b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
37898728b834SDavid S. Miller 
37901da177e4SLinus Torvalds /**
37911da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
37921da177e4SLinus Torvalds  *	@skb: buffer to remove
37938728b834SDavid S. Miller  *	@list: list to use
37941da177e4SLinus Torvalds  *
37958728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
37968728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
37971da177e4SLinus Torvalds  *
37988728b834SDavid S. Miller  *	You must know what list the SKB is on.
37991da177e4SLinus Torvalds  */
38008728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
38011da177e4SLinus Torvalds {
38021da177e4SLinus Torvalds 	unsigned long flags;
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
38058728b834SDavid S. Miller 	__skb_unlink(skb, list);
38061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
38071da177e4SLinus Torvalds }
3808b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds /**
38111da177e4SLinus Torvalds  *	skb_append	-	append a buffer
38121da177e4SLinus Torvalds  *	@old: buffer to insert after
38131da177e4SLinus Torvalds  *	@newsk: buffer to insert
38148728b834SDavid S. Miller  *	@list: list to use
38151da177e4SLinus Torvalds  *
38161da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
38171da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
38181da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
38191da177e4SLinus Torvalds  */
38208728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
38211da177e4SLinus Torvalds {
38221da177e4SLinus Torvalds 	unsigned long flags;
38231da177e4SLinus Torvalds 
38248728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
38257de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
38268728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
38271da177e4SLinus Torvalds }
3828b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
38311da177e4SLinus Torvalds 					   struct sk_buff* skb1,
38321da177e4SLinus Torvalds 					   const u32 len, const int pos)
38331da177e4SLinus Torvalds {
38341da177e4SLinus Torvalds 	int i;
38351da177e4SLinus Torvalds 
3836d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3837d626f62bSArnaldo Carvalho de Melo 					 pos - len);
38381da177e4SLinus Torvalds 	/* And move data appendix as is. */
38391da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
38401da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
38411da177e4SLinus Torvalds 
38421da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
38431da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
38441da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
38451da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
38461da177e4SLinus Torvalds 	skb->data_len		   = 0;
38471da177e4SLinus Torvalds 	skb->len		   = len;
384827a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
38491da177e4SLinus Torvalds }
38501da177e4SLinus Torvalds 
38511da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
38521da177e4SLinus Torvalds 				       struct sk_buff* skb1,
38531da177e4SLinus Torvalds 				       const u32 len, int pos)
38541da177e4SLinus Torvalds {
38551da177e4SLinus Torvalds 	int i, k = 0;
38561da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
38591da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
38601da177e4SLinus Torvalds 	skb->len		  = len;
38611da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
38621da177e4SLinus Torvalds 
38631da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
38649e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds 		if (pos + size > len) {
38671da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
38681da177e4SLinus Torvalds 
38691da177e4SLinus Torvalds 			if (pos < len) {
38701da177e4SLinus Torvalds 				/* Split frag.
38711da177e4SLinus Torvalds 				 * We have two variants in this case:
38721da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
38731da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
38741da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
38751da177e4SLinus Torvalds 				 *    where splitting is expensive.
38761da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
38771da177e4SLinus Torvalds 				 */
3878ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
3879b54c9d5bSJonathan Lemon 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
38809e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
38819e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
38821da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
38831da177e4SLinus Torvalds 			}
38841da177e4SLinus Torvalds 			k++;
38851da177e4SLinus Torvalds 		} else
38861da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
38871da177e4SLinus Torvalds 		pos += size;
38881da177e4SLinus Torvalds 	}
38891da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
38901da177e4SLinus Torvalds }
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds /**
38931da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
38941da177e4SLinus Torvalds  * @skb: the buffer to split
38951da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
38961da177e4SLinus Torvalds  * @len: new length for skb
38971da177e4SLinus Torvalds  */
38981da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
38991da177e4SLinus Torvalds {
39001da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
39019b65b17dSTalal Ahmad 	const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY;
39021da177e4SLinus Torvalds 
3903753f1ca4SPavel Begunkov 	skb_zcopy_downgrade_managed(skb);
3904753f1ca4SPavel Begunkov 
39059b65b17dSTalal Ahmad 	skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags;
39061f8b977aSWillem de Bruijn 	skb_zerocopy_clone(skb1, skb, 0);
39071da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
39081da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
39091da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
39101da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
39111da177e4SLinus Torvalds }
3912b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
39131da177e4SLinus Torvalds 
39149f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
39159f782db3SIlpo Järvinen  *
39169f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
39179f782db3SIlpo Järvinen  */
3918832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
3919832d11c5SIlpo Järvinen {
3920c4777efaSEric Dumazet 	return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
3921832d11c5SIlpo Järvinen }
3922832d11c5SIlpo Järvinen 
3923832d11c5SIlpo Järvinen /**
3924832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
3925832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
3926832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
3927832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
3928832d11c5SIlpo Järvinen  *
3929832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
393020e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3931832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
3932832d11c5SIlpo Järvinen  *
3933832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
3934832d11c5SIlpo Järvinen  *
3935832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
3936832d11c5SIlpo Järvinen  * to have non-paged data as well.
3937832d11c5SIlpo Järvinen  *
3938832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
3939832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
3940832d11c5SIlpo Järvinen  */
3941832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3942832d11c5SIlpo Järvinen {
3943832d11c5SIlpo Järvinen 	int from, to, merge, todo;
3944d8e18a51SMatthew Wilcox (Oracle) 	skb_frag_t *fragfrom, *fragto;
3945832d11c5SIlpo Järvinen 
3946832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
3947f8071cdeSEric Dumazet 
3948f8071cdeSEric Dumazet 	if (skb_headlen(skb))
3949f8071cdeSEric Dumazet 		return 0;
39501f8b977aSWillem de Bruijn 	if (skb_zcopy(tgt) || skb_zcopy(skb))
39511f8b977aSWillem de Bruijn 		return 0;
3952832d11c5SIlpo Järvinen 
3953832d11c5SIlpo Järvinen 	todo = shiftlen;
3954832d11c5SIlpo Järvinen 	from = 0;
3955832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
3956832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
3957832d11c5SIlpo Järvinen 
3958832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
3959832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
3960832d11c5SIlpo Järvinen 	 */
3961832d11c5SIlpo Järvinen 	if (!to ||
3962ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3963b54c9d5bSJonathan Lemon 			      skb_frag_off(fragfrom))) {
3964832d11c5SIlpo Järvinen 		merge = -1;
3965832d11c5SIlpo Järvinen 	} else {
3966832d11c5SIlpo Järvinen 		merge = to - 1;
3967832d11c5SIlpo Järvinen 
39689e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
3969832d11c5SIlpo Järvinen 		if (todo < 0) {
3970832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
3971832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
3972832d11c5SIlpo Järvinen 				return 0;
3973832d11c5SIlpo Järvinen 
39749f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
39759f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
3976832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
3977832d11c5SIlpo Järvinen 
39789e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
39799e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
3980b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, shiftlen);
3981832d11c5SIlpo Järvinen 
3982832d11c5SIlpo Järvinen 			goto onlymerged;
3983832d11c5SIlpo Järvinen 		}
3984832d11c5SIlpo Järvinen 
3985832d11c5SIlpo Järvinen 		from++;
3986832d11c5SIlpo Järvinen 	}
3987832d11c5SIlpo Järvinen 
3988832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
3989832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
3990832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3991832d11c5SIlpo Järvinen 		return 0;
3992832d11c5SIlpo Järvinen 
3993832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3994832d11c5SIlpo Järvinen 		return 0;
3995832d11c5SIlpo Järvinen 
3996832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3997832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
3998832d11c5SIlpo Järvinen 			return 0;
3999832d11c5SIlpo Järvinen 
4000832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
4001832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
4002832d11c5SIlpo Järvinen 
40039e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
4004832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
40059e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
4006832d11c5SIlpo Järvinen 			from++;
4007832d11c5SIlpo Järvinen 			to++;
4008832d11c5SIlpo Järvinen 
4009832d11c5SIlpo Järvinen 		} else {
4010ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
4011b54c9d5bSJonathan Lemon 			skb_frag_page_copy(fragto, fragfrom);
4012b54c9d5bSJonathan Lemon 			skb_frag_off_copy(fragto, fragfrom);
40139e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
4014832d11c5SIlpo Järvinen 
4015b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, todo);
40169e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
4017832d11c5SIlpo Järvinen 			todo = 0;
4018832d11c5SIlpo Järvinen 
4019832d11c5SIlpo Järvinen 			to++;
4020832d11c5SIlpo Järvinen 			break;
4021832d11c5SIlpo Järvinen 		}
4022832d11c5SIlpo Järvinen 	}
4023832d11c5SIlpo Järvinen 
4024832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
4025832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
4026832d11c5SIlpo Järvinen 
4027832d11c5SIlpo Järvinen 	if (merge >= 0) {
4028832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
4029832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
4030832d11c5SIlpo Järvinen 
40319e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
40326a5bcd84SIlias Apalodimas 		__skb_frag_unref(fragfrom, skb->pp_recycle);
4033832d11c5SIlpo Järvinen 	}
4034832d11c5SIlpo Järvinen 
4035832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
4036832d11c5SIlpo Järvinen 	to = 0;
4037832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
4038832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
4039832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
4040832d11c5SIlpo Järvinen 
4041832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
4042832d11c5SIlpo Järvinen 
4043832d11c5SIlpo Järvinen onlymerged:
4044832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
4045832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
4046832d11c5SIlpo Järvinen 	 */
4047832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
4048832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
4049832d11c5SIlpo Järvinen 
4050ede57d58SRichard Gobert 	skb_len_add(skb, -shiftlen);
4051ede57d58SRichard Gobert 	skb_len_add(tgt, shiftlen);
4052832d11c5SIlpo Järvinen 
4053832d11c5SIlpo Järvinen 	return shiftlen;
4054832d11c5SIlpo Järvinen }
4055832d11c5SIlpo Järvinen 
4056677e90edSThomas Graf /**
4057677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
4058677e90edSThomas Graf  * @skb: the buffer to read
4059677e90edSThomas Graf  * @from: lower offset of data to be read
4060677e90edSThomas Graf  * @to: upper offset of data to be read
4061677e90edSThomas Graf  * @st: state variable
4062677e90edSThomas Graf  *
4063677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
4064677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
4065677e90edSThomas Graf  */
4066677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
4067677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
4068677e90edSThomas Graf {
4069677e90edSThomas Graf 	st->lower_offset = from;
4070677e90edSThomas Graf 	st->upper_offset = to;
4071677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
4072677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
4073677e90edSThomas Graf 	st->frag_data = NULL;
407497550f6fSWillem de Bruijn 	st->frag_off = 0;
4075677e90edSThomas Graf }
4076b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
4077677e90edSThomas Graf 
4078677e90edSThomas Graf /**
4079677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
4080677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
4081677e90edSThomas Graf  * @data: destination pointer for data to be returned
4082677e90edSThomas Graf  * @st: state variable
4083677e90edSThomas Graf  *
4084bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
4085677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
4086bc32383cSMathias Krause  * the head of the data block to @data and returns the length
4087677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
4088677e90edSThomas Graf  * offset has been reached.
4089677e90edSThomas Graf  *
4090677e90edSThomas Graf  * The caller is not required to consume all of the data
4091bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
4092677e90edSThomas Graf  * of bytes already consumed and the next call to
4093677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
4094677e90edSThomas Graf  *
409525985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
4096e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
4097677e90edSThomas Graf  *       reads of potentially non linear data.
4098677e90edSThomas Graf  *
4099bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
4100677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
4101677e90edSThomas Graf  *       a stack for this purpose.
4102677e90edSThomas Graf  */
4103677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
4104677e90edSThomas Graf 			  struct skb_seq_state *st)
4105677e90edSThomas Graf {
4106677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
4107677e90edSThomas Graf 	skb_frag_t *frag;
4108677e90edSThomas Graf 
4109aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
4110aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
4111aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
4112aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
4113aeb193eaSWedson Almeida Filho 		}
4114677e90edSThomas Graf 		return 0;
4115aeb193eaSWedson Almeida Filho 	}
4116677e90edSThomas Graf 
4117677e90edSThomas Graf next_skb:
411895e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
4119677e90edSThomas Graf 
4120995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
412195e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
4122677e90edSThomas Graf 		return block_limit - abs_offset;
4123677e90edSThomas Graf 	}
4124677e90edSThomas Graf 
4125677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
4126677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
4127677e90edSThomas Graf 
4128677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
412997550f6fSWillem de Bruijn 		unsigned int pg_idx, pg_off, pg_sz;
4130677e90edSThomas Graf 
413197550f6fSWillem de Bruijn 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
413297550f6fSWillem de Bruijn 
413397550f6fSWillem de Bruijn 		pg_idx = 0;
413497550f6fSWillem de Bruijn 		pg_off = skb_frag_off(frag);
413597550f6fSWillem de Bruijn 		pg_sz = skb_frag_size(frag);
413697550f6fSWillem de Bruijn 
413797550f6fSWillem de Bruijn 		if (skb_frag_must_loop(skb_frag_page(frag))) {
413897550f6fSWillem de Bruijn 			pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
413997550f6fSWillem de Bruijn 			pg_off = offset_in_page(pg_off + st->frag_off);
414097550f6fSWillem de Bruijn 			pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
414197550f6fSWillem de Bruijn 						    PAGE_SIZE - pg_off);
414297550f6fSWillem de Bruijn 		}
414397550f6fSWillem de Bruijn 
414497550f6fSWillem de Bruijn 		block_limit = pg_sz + st->stepped_offset;
4145677e90edSThomas Graf 		if (abs_offset < block_limit) {
4146677e90edSThomas Graf 			if (!st->frag_data)
414797550f6fSWillem de Bruijn 				st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
4148677e90edSThomas Graf 
414997550f6fSWillem de Bruijn 			*data = (u8 *)st->frag_data + pg_off +
4150677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
4151677e90edSThomas Graf 
4152677e90edSThomas Graf 			return block_limit - abs_offset;
4153677e90edSThomas Graf 		}
4154677e90edSThomas Graf 
4155677e90edSThomas Graf 		if (st->frag_data) {
415651c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
4157677e90edSThomas Graf 			st->frag_data = NULL;
4158677e90edSThomas Graf 		}
4159677e90edSThomas Graf 
416097550f6fSWillem de Bruijn 		st->stepped_offset += pg_sz;
416197550f6fSWillem de Bruijn 		st->frag_off += pg_sz;
416297550f6fSWillem de Bruijn 		if (st->frag_off == skb_frag_size(frag)) {
416397550f6fSWillem de Bruijn 			st->frag_off = 0;
4164677e90edSThomas Graf 			st->frag_idx++;
416597550f6fSWillem de Bruijn 		}
4166677e90edSThomas Graf 	}
4167677e90edSThomas Graf 
41685b5a60daSOlaf Kirch 	if (st->frag_data) {
416951c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
41705b5a60daSOlaf Kirch 		st->frag_data = NULL;
41715b5a60daSOlaf Kirch 	}
41725b5a60daSOlaf Kirch 
417321dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
4174677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
417595e3b24cSHerbert Xu 		st->frag_idx = 0;
4176677e90edSThomas Graf 		goto next_skb;
417771b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
417871b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
417971b3346dSShyam Iyer 		st->frag_idx = 0;
4180677e90edSThomas Graf 		goto next_skb;
4181677e90edSThomas Graf 	}
4182677e90edSThomas Graf 
4183677e90edSThomas Graf 	return 0;
4184677e90edSThomas Graf }
4185b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
4186677e90edSThomas Graf 
4187677e90edSThomas Graf /**
4188677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
4189677e90edSThomas Graf  * @st: state variable
4190677e90edSThomas Graf  *
4191677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
4192677e90edSThomas Graf  * returned 0.
4193677e90edSThomas Graf  */
4194677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
4195677e90edSThomas Graf {
4196677e90edSThomas Graf 	if (st->frag_data)
419751c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
4198677e90edSThomas Graf }
4199b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
4200677e90edSThomas Graf 
42013fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
42023fc7e8a6SThomas Graf 
42033fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
42043fc7e8a6SThomas Graf 					  struct ts_config *conf,
42053fc7e8a6SThomas Graf 					  struct ts_state *state)
42063fc7e8a6SThomas Graf {
42073fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
42083fc7e8a6SThomas Graf }
42093fc7e8a6SThomas Graf 
42103fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
42113fc7e8a6SThomas Graf {
42123fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
42133fc7e8a6SThomas Graf }
42143fc7e8a6SThomas Graf 
42153fc7e8a6SThomas Graf /**
42163fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
42173fc7e8a6SThomas Graf  * @skb: the buffer to look in
42183fc7e8a6SThomas Graf  * @from: search offset
42193fc7e8a6SThomas Graf  * @to: search limit
42203fc7e8a6SThomas Graf  * @config: textsearch configuration
42213fc7e8a6SThomas Graf  *
42223fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
42233fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
42243fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
42253fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
42263fc7e8a6SThomas Graf  */
42273fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
4228059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
42293fc7e8a6SThomas Graf {
4230059a2440SBojan Prtvar 	struct ts_state state;
4231f72b948dSPhil Oester 	unsigned int ret;
4232f72b948dSPhil Oester 
4233b228c9b0SWillem de Bruijn 	BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
4234b228c9b0SWillem de Bruijn 
42353fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
42363fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
42373fc7e8a6SThomas Graf 
4238059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
42393fc7e8a6SThomas Graf 
4240059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
4241f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
42423fc7e8a6SThomas Graf }
4243b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
42443fc7e8a6SThomas Graf 
4245be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
424696449f90SDavid Howells 			 int offset, size_t size, size_t max_frags)
4247be12a1feSHannes Frederic Sowa {
4248be12a1feSHannes Frederic Sowa 	int i = skb_shinfo(skb)->nr_frags;
4249be12a1feSHannes Frederic Sowa 
4250be12a1feSHannes Frederic Sowa 	if (skb_can_coalesce(skb, i, page, offset)) {
4251be12a1feSHannes Frederic Sowa 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
425296449f90SDavid Howells 	} else if (i < max_frags) {
4253753f1ca4SPavel Begunkov 		skb_zcopy_downgrade_managed(skb);
4254be12a1feSHannes Frederic Sowa 		get_page(page);
4255228ebc41SEric Dumazet 		skb_fill_page_desc_noacc(skb, i, page, offset, size);
4256be12a1feSHannes Frederic Sowa 	} else {
4257be12a1feSHannes Frederic Sowa 		return -EMSGSIZE;
4258be12a1feSHannes Frederic Sowa 	}
4259be12a1feSHannes Frederic Sowa 
4260be12a1feSHannes Frederic Sowa 	return 0;
4261be12a1feSHannes Frederic Sowa }
4262be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
4263be12a1feSHannes Frederic Sowa 
4264cbb042f9SHerbert Xu /**
4265cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
4266cbb042f9SHerbert Xu  *	@skb: buffer to update
4267cbb042f9SHerbert Xu  *	@len: length of data pulled
4268cbb042f9SHerbert Xu  *
4269cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
4270fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
427184fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
427284fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
427384fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
4274cbb042f9SHerbert Xu  */
4275af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
4276cbb042f9SHerbert Xu {
427731b33dfbSPravin B Shelar 	unsigned char *data = skb->data;
427831b33dfbSPravin B Shelar 
4279cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
428031b33dfbSPravin B Shelar 	__skb_pull(skb, len);
428131b33dfbSPravin B Shelar 	skb_postpull_rcsum(skb, data, len);
428231b33dfbSPravin B Shelar 	return skb->data;
4283cbb042f9SHerbert Xu }
4284f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
4285f94691acSArnaldo Carvalho de Melo 
428613acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
428713acc94eSYonghong Song {
428813acc94eSYonghong Song 	skb_frag_t head_frag;
428913acc94eSYonghong Song 	struct page *page;
429013acc94eSYonghong Song 
429113acc94eSYonghong Song 	page = virt_to_head_page(frag_skb->head);
4292b51f4113SYunsheng Lin 	skb_frag_fill_page_desc(&head_frag, page, frag_skb->data -
4293b51f4113SYunsheng Lin 				(unsigned char *)page_address(page),
4294b51f4113SYunsheng Lin 				skb_headlen(frag_skb));
429513acc94eSYonghong Song 	return head_frag;
429613acc94eSYonghong Song }
429713acc94eSYonghong Song 
42983a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb,
42993a1296a3SSteffen Klassert 				 netdev_features_t features,
43003a1296a3SSteffen Klassert 				 unsigned int offset)
43013a1296a3SSteffen Klassert {
43023a1296a3SSteffen Klassert 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
43033a1296a3SSteffen Klassert 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
43043a1296a3SSteffen Klassert 	unsigned int delta_truesize = 0;
43053a1296a3SSteffen Klassert 	unsigned int delta_len = 0;
43063a1296a3SSteffen Klassert 	struct sk_buff *tail = NULL;
430753475c5dSDongseok Yi 	struct sk_buff *nskb, *tmp;
4308cf3ab8d4SLina Wang 	int len_diff, err;
43093a1296a3SSteffen Klassert 
43103a1296a3SSteffen Klassert 	skb_push(skb, -skb_network_offset(skb) + offset);
43113a1296a3SSteffen Klassert 
4312c329b261SPaolo Abeni 	/* Ensure the head is writeable before touching the shared info */
4313c329b261SPaolo Abeni 	err = skb_unclone(skb, GFP_ATOMIC);
4314c329b261SPaolo Abeni 	if (err)
4315c329b261SPaolo Abeni 		goto err_linearize;
4316c329b261SPaolo Abeni 
43173a1296a3SSteffen Klassert 	skb_shinfo(skb)->frag_list = NULL;
43183a1296a3SSteffen Klassert 
4319876e8ca8SYan Zhai 	while (list_skb) {
43203a1296a3SSteffen Klassert 		nskb = list_skb;
43213a1296a3SSteffen Klassert 		list_skb = list_skb->next;
43223a1296a3SSteffen Klassert 
432353475c5dSDongseok Yi 		err = 0;
4324224102deSlena wang 		delta_truesize += nskb->truesize;
432553475c5dSDongseok Yi 		if (skb_shared(nskb)) {
432653475c5dSDongseok Yi 			tmp = skb_clone(nskb, GFP_ATOMIC);
432753475c5dSDongseok Yi 			if (tmp) {
432853475c5dSDongseok Yi 				consume_skb(nskb);
432953475c5dSDongseok Yi 				nskb = tmp;
433053475c5dSDongseok Yi 				err = skb_unclone(nskb, GFP_ATOMIC);
433153475c5dSDongseok Yi 			} else {
433253475c5dSDongseok Yi 				err = -ENOMEM;
433353475c5dSDongseok Yi 			}
433453475c5dSDongseok Yi 		}
433553475c5dSDongseok Yi 
43363a1296a3SSteffen Klassert 		if (!tail)
43373a1296a3SSteffen Klassert 			skb->next = nskb;
43383a1296a3SSteffen Klassert 		else
43393a1296a3SSteffen Klassert 			tail->next = nskb;
43403a1296a3SSteffen Klassert 
434153475c5dSDongseok Yi 		if (unlikely(err)) {
434253475c5dSDongseok Yi 			nskb->next = list_skb;
434353475c5dSDongseok Yi 			goto err_linearize;
434453475c5dSDongseok Yi 		}
434553475c5dSDongseok Yi 
43463a1296a3SSteffen Klassert 		tail = nskb;
43473a1296a3SSteffen Klassert 
43483a1296a3SSteffen Klassert 		delta_len += nskb->len;
43493a1296a3SSteffen Klassert 
43503a1296a3SSteffen Klassert 		skb_push(nskb, -skb_network_offset(nskb) + offset);
43513a1296a3SSteffen Klassert 
4352cf673ed0SFlorian Westphal 		skb_release_head_state(nskb);
4353cf3ab8d4SLina Wang 		len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
43543a1296a3SSteffen Klassert 		__copy_skb_header(nskb, skb);
43553a1296a3SSteffen Klassert 
43563a1296a3SSteffen Klassert 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
4357cf3ab8d4SLina Wang 		nskb->transport_header += len_diff;
43583a1296a3SSteffen Klassert 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
43593a1296a3SSteffen Klassert 						 nskb->data - tnl_hlen,
43603a1296a3SSteffen Klassert 						 offset + tnl_hlen);
43613a1296a3SSteffen Klassert 
43623a1296a3SSteffen Klassert 		if (skb_needs_linearize(nskb, features) &&
43633a1296a3SSteffen Klassert 		    __skb_linearize(nskb))
43643a1296a3SSteffen Klassert 			goto err_linearize;
4365876e8ca8SYan Zhai 	}
43663a1296a3SSteffen Klassert 
43673a1296a3SSteffen Klassert 	skb->truesize = skb->truesize - delta_truesize;
43683a1296a3SSteffen Klassert 	skb->data_len = skb->data_len - delta_len;
43693a1296a3SSteffen Klassert 	skb->len = skb->len - delta_len;
43703a1296a3SSteffen Klassert 
43713a1296a3SSteffen Klassert 	skb_gso_reset(skb);
43723a1296a3SSteffen Klassert 
43733a1296a3SSteffen Klassert 	skb->prev = tail;
43743a1296a3SSteffen Klassert 
43753a1296a3SSteffen Klassert 	if (skb_needs_linearize(skb, features) &&
43763a1296a3SSteffen Klassert 	    __skb_linearize(skb))
43773a1296a3SSteffen Klassert 		goto err_linearize;
43783a1296a3SSteffen Klassert 
43793a1296a3SSteffen Klassert 	skb_get(skb);
43803a1296a3SSteffen Klassert 
43813a1296a3SSteffen Klassert 	return skb;
43823a1296a3SSteffen Klassert 
43833a1296a3SSteffen Klassert err_linearize:
43843a1296a3SSteffen Klassert 	kfree_skb_list(skb->next);
43853a1296a3SSteffen Klassert 	skb->next = NULL;
43863a1296a3SSteffen Klassert 	return ERR_PTR(-ENOMEM);
43873a1296a3SSteffen Klassert }
43883a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list);
43893a1296a3SSteffen Klassert 
4390f4c50d99SHerbert Xu /**
4391f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
4392df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
4393576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
4394f4c50d99SHerbert Xu  *
4395f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
43964c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
43974c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
4398f4c50d99SHerbert Xu  */
4399df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
4400df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
4401f4c50d99SHerbert Xu {
4402f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
4403f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
44041a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4405df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
4406df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
4407df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
44081fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
4409f4c50d99SHerbert Xu 	unsigned int offset = doffset;
4410df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
4411802ab55aSAlexander Duyck 	unsigned int partial_segs = 0;
4412f4c50d99SHerbert Xu 	unsigned int headroom;
4413802ab55aSAlexander Duyck 	unsigned int len = head_skb->len;
4414ec5f0615SPravin B Shelar 	__be16 proto;
441536c98382SAlexander Duyck 	bool csum, sg;
4416df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
4417f4c50d99SHerbert Xu 	int err = -ENOMEM;
4418f4c50d99SHerbert Xu 	int i = 0;
4419f4c50d99SHerbert Xu 	int pos;
4420f4c50d99SHerbert Xu 
44219e4b7a99SJiri Benc 	if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
44229e4b7a99SJiri Benc 	    mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
44239e4b7a99SJiri Benc 		struct sk_buff *check_skb;
44249e4b7a99SJiri Benc 
44259e4b7a99SJiri Benc 		for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
44269e4b7a99SJiri Benc 			if (skb_headlen(check_skb) && !check_skb->head_frag) {
44279e4b7a99SJiri Benc 				/* gso_size is untrusted, and we have a frag_list with
44289e4b7a99SJiri Benc 				 * a linear non head_frag item.
44293dcbdb13SShmulik Ladkani 				 *
44309e4b7a99SJiri Benc 				 * If head_skb's headlen does not fit requested gso_size,
44319e4b7a99SJiri Benc 				 * it means that the frag_list members do NOT terminate
44329e4b7a99SJiri Benc 				 * on exact gso_size boundaries. Hence we cannot perform
44339e4b7a99SJiri Benc 				 * skb_frag_t page sharing. Therefore we must fallback to
44349e4b7a99SJiri Benc 				 * copying the frag_list skbs; we do so by disabling SG.
44353dcbdb13SShmulik Ladkani 				 */
44363dcbdb13SShmulik Ladkani 				features &= ~NETIF_F_SG;
44379e4b7a99SJiri Benc 				break;
44389e4b7a99SJiri Benc 			}
44399e4b7a99SJiri Benc 		}
44403dcbdb13SShmulik Ladkani 	}
44413dcbdb13SShmulik Ladkani 
44425882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
44432f631133SMiaohe Lin 	proto = skb_network_protocol(head_skb, NULL);
4444ec5f0615SPravin B Shelar 	if (unlikely(!proto))
4445ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
4446ec5f0615SPravin B Shelar 
444736c98382SAlexander Duyck 	sg = !!(features & NETIF_F_SG);
4448f245d079SAlexander Duyck 	csum = !!can_checksum_protocol(features, proto);
44497e2b10c1STom Herbert 
445007b26c94SSteffen Klassert 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
445107b26c94SSteffen Klassert 		if (!(features & NETIF_F_GSO_PARTIAL)) {
445207b26c94SSteffen Klassert 			struct sk_buff *iter;
445343170c4eSIlan Tayari 			unsigned int frag_len;
445407b26c94SSteffen Klassert 
445507b26c94SSteffen Klassert 			if (!list_skb ||
445607b26c94SSteffen Klassert 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
445707b26c94SSteffen Klassert 				goto normal;
445807b26c94SSteffen Klassert 
445943170c4eSIlan Tayari 			/* If we get here then all the required
446043170c4eSIlan Tayari 			 * GSO features except frag_list are supported.
446143170c4eSIlan Tayari 			 * Try to split the SKB to multiple GSO SKBs
446243170c4eSIlan Tayari 			 * with no frag_list.
446343170c4eSIlan Tayari 			 * Currently we can do that only when the buffers don't
446443170c4eSIlan Tayari 			 * have a linear part and all the buffers except
446543170c4eSIlan Tayari 			 * the last are of the same length.
446607b26c94SSteffen Klassert 			 */
446743170c4eSIlan Tayari 			frag_len = list_skb->len;
446807b26c94SSteffen Klassert 			skb_walk_frags(head_skb, iter) {
446943170c4eSIlan Tayari 				if (frag_len != iter->len && iter->next)
447043170c4eSIlan Tayari 					goto normal;
4471eaffadbbSIlan Tayari 				if (skb_headlen(iter) && !iter->head_frag)
447207b26c94SSteffen Klassert 					goto normal;
447307b26c94SSteffen Klassert 
447407b26c94SSteffen Klassert 				len -= iter->len;
447507b26c94SSteffen Klassert 			}
447643170c4eSIlan Tayari 
447743170c4eSIlan Tayari 			if (len != frag_len)
447843170c4eSIlan Tayari 				goto normal;
447907b26c94SSteffen Klassert 		}
448007b26c94SSteffen Klassert 
4481802ab55aSAlexander Duyck 		/* GSO partial only requires that we trim off any excess that
4482802ab55aSAlexander Duyck 		 * doesn't fit into an MSS sized block, so take care of that
4483802ab55aSAlexander Duyck 		 * now.
4484802ab55aSAlexander Duyck 		 */
4485802ab55aSAlexander Duyck 		partial_segs = len / mss;
4486d7fb5a80SAlexander Duyck 		if (partial_segs > 1)
4487802ab55aSAlexander Duyck 			mss *= partial_segs;
4488d7fb5a80SAlexander Duyck 		else
4489d7fb5a80SAlexander Duyck 			partial_segs = 0;
4490802ab55aSAlexander Duyck 	}
4491802ab55aSAlexander Duyck 
449207b26c94SSteffen Klassert normal:
4493df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
4494df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
4495f4c50d99SHerbert Xu 
4496f4c50d99SHerbert Xu 	do {
4497f4c50d99SHerbert Xu 		struct sk_buff *nskb;
44988cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
4499c8884eddSHerbert Xu 		int hsize;
4500f4c50d99SHerbert Xu 		int size;
4501f4c50d99SHerbert Xu 
45023953c46cSMarcelo Ricardo Leitner 		if (unlikely(mss == GSO_BY_FRAGS)) {
45033953c46cSMarcelo Ricardo Leitner 			len = list_skb->len;
45043953c46cSMarcelo Ricardo Leitner 		} else {
4505df5771ffSMichael S. Tsirkin 			len = head_skb->len - offset;
4506f4c50d99SHerbert Xu 			if (len > mss)
4507f4c50d99SHerbert Xu 				len = mss;
45083953c46cSMarcelo Ricardo Leitner 		}
4509f4c50d99SHerbert Xu 
4510df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
4511f4c50d99SHerbert Xu 
4512dbd50f23SXin Long 		if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
45131a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
45141a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
451589319d38SHerbert Xu 
45169d8506ccSHerbert Xu 			i = 0;
45171a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
45181a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
45191fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
45201a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
45219d8506ccSHerbert Xu 
45229d8506ccSHerbert Xu 			while (pos < offset + len) {
45239d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
45249d8506ccSHerbert Xu 
45254e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
45269d8506ccSHerbert Xu 				if (pos + size > offset + len)
45279d8506ccSHerbert Xu 					break;
45289d8506ccSHerbert Xu 
45299d8506ccSHerbert Xu 				i++;
45309d8506ccSHerbert Xu 				pos += size;
45314e1beba1SMichael S. Tsirkin 				frag++;
45329d8506ccSHerbert Xu 			}
45339d8506ccSHerbert Xu 
45341a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
45351a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
453689319d38SHerbert Xu 
4537f4c50d99SHerbert Xu 			if (unlikely(!nskb))
4538f4c50d99SHerbert Xu 				goto err;
4539f4c50d99SHerbert Xu 
45409d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
45419d8506ccSHerbert Xu 				kfree_skb(nskb);
45429d8506ccSHerbert Xu 				goto err;
45439d8506ccSHerbert Xu 			}
45449d8506ccSHerbert Xu 
4545ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
454689319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
454789319d38SHerbert Xu 				kfree_skb(nskb);
454889319d38SHerbert Xu 				goto err;
454989319d38SHerbert Xu 			}
455089319d38SHerbert Xu 
4551ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
455289319d38SHerbert Xu 			skb_release_head_state(nskb);
455389319d38SHerbert Xu 			__skb_push(nskb, doffset);
455489319d38SHerbert Xu 		} else {
455500b229f7SPaolo Abeni 			if (hsize < 0)
455600b229f7SPaolo Abeni 				hsize = 0;
4557dbd50f23SXin Long 			if (hsize > len || !sg)
4558dbd50f23SXin Long 				hsize = len;
4559dbd50f23SXin Long 
4560c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
4561df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4562c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
456389319d38SHerbert Xu 
456489319d38SHerbert Xu 			if (unlikely(!nskb))
456589319d38SHerbert Xu 				goto err;
456689319d38SHerbert Xu 
456789319d38SHerbert Xu 			skb_reserve(nskb, headroom);
456889319d38SHerbert Xu 			__skb_put(nskb, doffset);
456989319d38SHerbert Xu 		}
457089319d38SHerbert Xu 
4571f4c50d99SHerbert Xu 		if (segs)
4572f4c50d99SHerbert Xu 			tail->next = nskb;
4573f4c50d99SHerbert Xu 		else
4574f4c50d99SHerbert Xu 			segs = nskb;
4575f4c50d99SHerbert Xu 		tail = nskb;
4576f4c50d99SHerbert Xu 
4577df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
4578f4c50d99SHerbert Xu 
4579030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4580fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
458168c33163SPravin B Shelar 
4582df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
458368c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
458468c33163SPravin B Shelar 						 doffset + tnl_hlen);
458589319d38SHerbert Xu 
45869d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
45871cdbcb79SSimon Horman 			goto perform_csum_check;
458889319d38SHerbert Xu 
45897fbeffedSAlexander Duyck 		if (!sg) {
45901454c9faSYadu Kishore 			if (!csum) {
45917fbeffedSAlexander Duyck 				if (!nskb->remcsum_offload)
45926f85a124SHerbert Xu 					nskb->ip_summed = CHECKSUM_NONE;
459376443456SAlexander Duyck 				SKB_GSO_CB(nskb)->csum =
459476443456SAlexander Duyck 					skb_copy_and_csum_bits(head_skb, offset,
45951454c9faSYadu Kishore 							       skb_put(nskb,
45961454c9faSYadu Kishore 								       len),
45978d5930dfSAl Viro 							       len);
45987e2b10c1STom Herbert 				SKB_GSO_CB(nskb)->csum_start =
4599de843723STom Herbert 					skb_headroom(nskb) + doffset;
46001454c9faSYadu Kishore 			} else {
4601c624c58eSlily 				if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
4602c624c58eSlily 					goto err;
46031454c9faSYadu Kishore 			}
4604f4c50d99SHerbert Xu 			continue;
4605f4c50d99SHerbert Xu 		}
4606f4c50d99SHerbert Xu 
46078cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
4608f4c50d99SHerbert Xu 
4609df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
4610d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
4611f4c50d99SHerbert Xu 
461206b4feb3SJonathan Lemon 		skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
461306b4feb3SJonathan Lemon 					   SKBFL_SHARED_FRAG;
4614cef401deSEric Dumazet 
4615bf5c25d6SWillem de Bruijn 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4616bf5c25d6SWillem de Bruijn 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4617bf5c25d6SWillem de Bruijn 			goto err;
4618bf5c25d6SWillem de Bruijn 
46199d8506ccSHerbert Xu 		while (pos < offset + len) {
46209d8506ccSHerbert Xu 			if (i >= nfrags) {
46219d8506ccSHerbert Xu 				i = 0;
46221a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
46231a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
46241fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
462513acc94eSYonghong Song 				if (!skb_headlen(list_skb)) {
46269d8506ccSHerbert Xu 					BUG_ON(!nfrags);
462713acc94eSYonghong Song 				} else {
462813acc94eSYonghong Song 					BUG_ON(!list_skb->head_frag);
46299d8506ccSHerbert Xu 
463013acc94eSYonghong Song 					/* to make room for head_frag. */
463113acc94eSYonghong Song 					i--;
463213acc94eSYonghong Song 					frag--;
463313acc94eSYonghong Song 				}
4634bf5c25d6SWillem de Bruijn 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4635bf5c25d6SWillem de Bruijn 				    skb_zerocopy_clone(nskb, frag_skb,
4636bf5c25d6SWillem de Bruijn 						       GFP_ATOMIC))
4637bf5c25d6SWillem de Bruijn 					goto err;
4638bf5c25d6SWillem de Bruijn 
46391a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
46409d8506ccSHerbert Xu 			}
46419d8506ccSHerbert Xu 
46429d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
46439d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
46449d8506ccSHerbert Xu 				net_warn_ratelimited(
46459d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
46469d8506ccSHerbert Xu 					pos, mss);
4647ff907a11SEric Dumazet 				err = -EINVAL;
46489d8506ccSHerbert Xu 				goto err;
46499d8506ccSHerbert Xu 			}
46509d8506ccSHerbert Xu 
465113acc94eSYonghong Song 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
46528cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
46538cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
4654f4c50d99SHerbert Xu 
4655f4c50d99SHerbert Xu 			if (pos < offset) {
4656b54c9d5bSJonathan Lemon 				skb_frag_off_add(nskb_frag, offset - pos);
46578cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
4658f4c50d99SHerbert Xu 			}
4659f4c50d99SHerbert Xu 
466089319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
4661f4c50d99SHerbert Xu 
4662f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
4663f4c50d99SHerbert Xu 				i++;
46644e1beba1SMichael S. Tsirkin 				frag++;
4665f4c50d99SHerbert Xu 				pos += size;
4666f4c50d99SHerbert Xu 			} else {
46678cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
466889319d38SHerbert Xu 				goto skip_fraglist;
4669f4c50d99SHerbert Xu 			}
4670f4c50d99SHerbert Xu 
46718cb19905SMichael S. Tsirkin 			nskb_frag++;
4672f4c50d99SHerbert Xu 		}
4673f4c50d99SHerbert Xu 
467489319d38SHerbert Xu skip_fraglist:
4675f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
4676f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
4677f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
4678ec5f0615SPravin B Shelar 
46791cdbcb79SSimon Horman perform_csum_check:
46807fbeffedSAlexander Duyck 		if (!csum) {
4681ff907a11SEric Dumazet 			if (skb_has_shared_frag(nskb) &&
4682ff907a11SEric Dumazet 			    __skb_linearize(nskb))
4683ddff00d4SAlexander Duyck 				goto err;
4684ff907a11SEric Dumazet 
46857fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
4686ec5f0615SPravin B Shelar 				nskb->ip_summed = CHECKSUM_NONE;
468776443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
468876443456SAlexander Duyck 				skb_checksum(nskb, doffset,
468976443456SAlexander Duyck 					     nskb->len - doffset, 0);
46907e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
46917e2b10c1STom Herbert 				skb_headroom(nskb) + doffset;
4692ec5f0615SPravin B Shelar 		}
4693df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
4694f4c50d99SHerbert Xu 
4695bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
4696bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
4697bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
4698bec3cfdcSEric Dumazet 	 */
4699bec3cfdcSEric Dumazet 	segs->prev = tail;
4700432c856fSToshiaki Makita 
4701802ab55aSAlexander Duyck 	if (partial_segs) {
470207b26c94SSteffen Klassert 		struct sk_buff *iter;
4703802ab55aSAlexander Duyck 		int type = skb_shinfo(head_skb)->gso_type;
470407b26c94SSteffen Klassert 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4705802ab55aSAlexander Duyck 
4706802ab55aSAlexander Duyck 		/* Update type to add partial and then remove dodgy if set */
470707b26c94SSteffen Klassert 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4708802ab55aSAlexander Duyck 		type &= ~SKB_GSO_DODGY;
4709802ab55aSAlexander Duyck 
4710802ab55aSAlexander Duyck 		/* Update GSO info and prepare to start updating headers on
4711802ab55aSAlexander Duyck 		 * our way back down the stack of protocols.
4712802ab55aSAlexander Duyck 		 */
471307b26c94SSteffen Klassert 		for (iter = segs; iter; iter = iter->next) {
471407b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_size = gso_size;
471507b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_segs = partial_segs;
471607b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_type = type;
471707b26c94SSteffen Klassert 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
471807b26c94SSteffen Klassert 		}
471907b26c94SSteffen Klassert 
472007b26c94SSteffen Klassert 		if (tail->len - doffset <= gso_size)
472107b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_size = 0;
472207b26c94SSteffen Klassert 		else if (tail != segs)
472307b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4724802ab55aSAlexander Duyck 	}
4725802ab55aSAlexander Duyck 
4726432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
4727432c856fSToshiaki Makita 	 * using skb_set_owner_w().
4728432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
4729432c856fSToshiaki Makita 	 */
4730432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
4731432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
4732432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
4733432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
4734432c856fSToshiaki Makita 	}
4735f4c50d99SHerbert Xu 	return segs;
4736f4c50d99SHerbert Xu 
4737f4c50d99SHerbert Xu err:
4738289dccbeSEric Dumazet 	kfree_skb_list(segs);
4739f4c50d99SHerbert Xu 	return ERR_PTR(err);
4740f4c50d99SHerbert Xu }
4741f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
4742f4c50d99SHerbert Xu 
4743df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
4744df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE	8
4745df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4746df5042f4SFlorian Westphal 
4747df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = {
4748df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4749df5042f4SFlorian Westphal 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4750df5042f4SFlorian Westphal #endif
47514165079bSFlorian Westphal #ifdef CONFIG_XFRM
47524165079bSFlorian Westphal 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
47534165079bSFlorian Westphal #endif
475495a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
475595a7233cSPaul Blakey 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
475695a7233cSPaul Blakey #endif
47573ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
47583ee17bc7SMat Martineau 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
47593ee17bc7SMat Martineau #endif
476078476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS)
476178476d31SJeremy Kerr 	[SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
476278476d31SJeremy Kerr #endif
4763df5042f4SFlorian Westphal };
4764df5042f4SFlorian Westphal 
4765df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void)
4766df5042f4SFlorian Westphal {
4767df5042f4SFlorian Westphal 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4768df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4769df5042f4SFlorian Westphal 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4770df5042f4SFlorian Westphal #endif
47714165079bSFlorian Westphal #ifdef CONFIG_XFRM
47724165079bSFlorian Westphal 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
47734165079bSFlorian Westphal #endif
477495a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
477595a7233cSPaul Blakey 		skb_ext_type_len[TC_SKB_EXT] +
477695a7233cSPaul Blakey #endif
47773ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
47783ee17bc7SMat Martineau 		skb_ext_type_len[SKB_EXT_MPTCP] +
47793ee17bc7SMat Martineau #endif
478078476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS)
478178476d31SJeremy Kerr 		skb_ext_type_len[SKB_EXT_MCTP] +
478278476d31SJeremy Kerr #endif
4783df5042f4SFlorian Westphal 		0;
4784df5042f4SFlorian Westphal }
4785df5042f4SFlorian Westphal 
4786df5042f4SFlorian Westphal static void skb_extensions_init(void)
4787df5042f4SFlorian Westphal {
4788df5042f4SFlorian Westphal 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4789df5042f4SFlorian Westphal 	BUILD_BUG_ON(skb_ext_total_length() > 255);
4790df5042f4SFlorian Westphal 
4791df5042f4SFlorian Westphal 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4792df5042f4SFlorian Westphal 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4793df5042f4SFlorian Westphal 					     0,
4794df5042f4SFlorian Westphal 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4795df5042f4SFlorian Westphal 					     NULL);
4796df5042f4SFlorian Westphal }
4797df5042f4SFlorian Westphal #else
4798df5042f4SFlorian Westphal static void skb_extensions_init(void) {}
4799df5042f4SFlorian Westphal #endif
4800df5042f4SFlorian Westphal 
48010a064316SJesper Dangaard Brouer /* The SKB kmem_cache slab is critical for network performance.  Never
48020a064316SJesper Dangaard Brouer  * merge/alias the slab with similar sized objects.  This avoids fragmentation
48030a064316SJesper Dangaard Brouer  * that hurts performance of kmem_cache_{alloc,free}_bulk APIs.
48040a064316SJesper Dangaard Brouer  */
48050a064316SJesper Dangaard Brouer #ifndef CONFIG_SLUB_TINY
48060a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE	SLAB_NO_MERGE
48070a064316SJesper Dangaard Brouer #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */
48080a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE	0
48090a064316SJesper Dangaard Brouer #endif
48100a064316SJesper Dangaard Brouer 
48111da177e4SLinus Torvalds void __init skb_init(void)
48121da177e4SLinus Torvalds {
4813025a785fSJakub Kicinski 	skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache",
48141da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
48151da177e4SLinus Torvalds 					      0,
48160a064316SJesper Dangaard Brouer 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC|
48170a064316SJesper Dangaard Brouer 						FLAG_SKB_NO_MERGE,
481879a8a642SKees Cook 					      offsetof(struct sk_buff, cb),
481979a8a642SKees Cook 					      sizeof_field(struct sk_buff, cb),
482020c2df83SPaul Mundt 					      NULL);
4821d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4822d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
4823d179cd12SDavid S. Miller 						0,
4824e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
482520c2df83SPaul Mundt 						NULL);
48260b34d680SEric Dumazet 	/* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes.
48270b34d680SEric Dumazet 	 * struct skb_shared_info is located at the end of skb->head,
48280b34d680SEric Dumazet 	 * and should not be copied to/from user.
48290b34d680SEric Dumazet 	 */
48300b34d680SEric Dumazet 	skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head",
4831bf9f1baaSEric Dumazet 						SKB_SMALL_HEAD_CACHE_SIZE,
4832bf9f1baaSEric Dumazet 						0,
4833bf9f1baaSEric Dumazet 						SLAB_HWCACHE_ALIGN | SLAB_PANIC,
48340b34d680SEric Dumazet 						0,
48350b34d680SEric Dumazet 						SKB_SMALL_HEAD_HEADROOM,
4836bf9f1baaSEric Dumazet 						NULL);
4837df5042f4SFlorian Westphal 	skb_extensions_init();
48381da177e4SLinus Torvalds }
48391da177e4SLinus Torvalds 
484051c739d1SDavid S. Miller static int
484148a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
484248a1df65SJason A. Donenfeld 	       unsigned int recursion_level)
4843716ea3a7SDavid Howells {
48441a028e50SDavid S. Miller 	int start = skb_headlen(skb);
48451a028e50SDavid S. Miller 	int i, copy = start - offset;
4846fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
4847716ea3a7SDavid Howells 	int elt = 0;
4848716ea3a7SDavid Howells 
484948a1df65SJason A. Donenfeld 	if (unlikely(recursion_level >= 24))
485048a1df65SJason A. Donenfeld 		return -EMSGSIZE;
485148a1df65SJason A. Donenfeld 
4852716ea3a7SDavid Howells 	if (copy > 0) {
4853716ea3a7SDavid Howells 		if (copy > len)
4854716ea3a7SDavid Howells 			copy = len;
4855642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
4856716ea3a7SDavid Howells 		elt++;
4857716ea3a7SDavid Howells 		if ((len -= copy) == 0)
4858716ea3a7SDavid Howells 			return elt;
4859716ea3a7SDavid Howells 		offset += copy;
4860716ea3a7SDavid Howells 	}
4861716ea3a7SDavid Howells 
4862716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
48631a028e50SDavid S. Miller 		int end;
4864716ea3a7SDavid Howells 
4865547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
48661a028e50SDavid S. Miller 
48679e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4868716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
4869716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
487048a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
487148a1df65SJason A. Donenfeld 				return -EMSGSIZE;
4872716ea3a7SDavid Howells 
4873716ea3a7SDavid Howells 			if (copy > len)
4874716ea3a7SDavid Howells 				copy = len;
4875ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4876b54c9d5bSJonathan Lemon 				    skb_frag_off(frag) + offset - start);
4877716ea3a7SDavid Howells 			elt++;
4878716ea3a7SDavid Howells 			if (!(len -= copy))
4879716ea3a7SDavid Howells 				return elt;
4880716ea3a7SDavid Howells 			offset += copy;
4881716ea3a7SDavid Howells 		}
48821a028e50SDavid S. Miller 		start = end;
4883716ea3a7SDavid Howells 	}
4884716ea3a7SDavid Howells 
4885fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
488648a1df65SJason A. Donenfeld 		int end, ret;
4887716ea3a7SDavid Howells 
4888547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
48891a028e50SDavid S. Miller 
4890fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
4891716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
489248a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
489348a1df65SJason A. Donenfeld 				return -EMSGSIZE;
489448a1df65SJason A. Donenfeld 
4895716ea3a7SDavid Howells 			if (copy > len)
4896716ea3a7SDavid Howells 				copy = len;
489748a1df65SJason A. Donenfeld 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
489848a1df65SJason A. Donenfeld 					      copy, recursion_level + 1);
489948a1df65SJason A. Donenfeld 			if (unlikely(ret < 0))
490048a1df65SJason A. Donenfeld 				return ret;
490148a1df65SJason A. Donenfeld 			elt += ret;
4902716ea3a7SDavid Howells 			if ((len -= copy) == 0)
4903716ea3a7SDavid Howells 				return elt;
4904716ea3a7SDavid Howells 			offset += copy;
4905716ea3a7SDavid Howells 		}
49061a028e50SDavid S. Miller 		start = end;
4907716ea3a7SDavid Howells 	}
4908716ea3a7SDavid Howells 	BUG_ON(len);
4909716ea3a7SDavid Howells 	return elt;
4910716ea3a7SDavid Howells }
4911716ea3a7SDavid Howells 
491248a1df65SJason A. Donenfeld /**
491348a1df65SJason A. Donenfeld  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
491448a1df65SJason A. Donenfeld  *	@skb: Socket buffer containing the buffers to be mapped
491548a1df65SJason A. Donenfeld  *	@sg: The scatter-gather list to map into
491648a1df65SJason A. Donenfeld  *	@offset: The offset into the buffer's contents to start mapping
491748a1df65SJason A. Donenfeld  *	@len: Length of buffer space to be mapped
491848a1df65SJason A. Donenfeld  *
491948a1df65SJason A. Donenfeld  *	Fill the specified scatter-gather list with mappings/pointers into a
492048a1df65SJason A. Donenfeld  *	region of the buffer space attached to a socket buffer. Returns either
492148a1df65SJason A. Donenfeld  *	the number of scatterlist items used, or -EMSGSIZE if the contents
492248a1df65SJason A. Donenfeld  *	could not fit.
492348a1df65SJason A. Donenfeld  */
492448a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
492548a1df65SJason A. Donenfeld {
492648a1df65SJason A. Donenfeld 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
492748a1df65SJason A. Donenfeld 
492848a1df65SJason A. Donenfeld 	if (nsg <= 0)
492948a1df65SJason A. Donenfeld 		return nsg;
493048a1df65SJason A. Donenfeld 
493148a1df65SJason A. Donenfeld 	sg_mark_end(&sg[nsg - 1]);
493248a1df65SJason A. Donenfeld 
493348a1df65SJason A. Donenfeld 	return nsg;
493448a1df65SJason A. Donenfeld }
493548a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec);
493648a1df65SJason A. Donenfeld 
493725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
493825a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
493925a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
494025a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
494125a91d8dSFan Du  *
494225a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
494325a91d8dSFan Du  * 1. sg_init_table
494425a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
494525a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
494625a91d8dSFan Du  *
494725a91d8dSFan Du  * This is equivalent to:
494825a91d8dSFan Du  * 1. sg_init_table
494925a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
495025a91d8dSFan Du  * 3. sg_unmark_end
495125a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
495225a91d8dSFan Du  *
495325a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
495425a91d8dSFan Du  * is more preferable.
495525a91d8dSFan Du  */
495625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
495725a91d8dSFan Du 			int offset, int len)
495825a91d8dSFan Du {
495948a1df65SJason A. Donenfeld 	return __skb_to_sgvec(skb, sg, offset, len, 0);
496025a91d8dSFan Du }
496125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
496225a91d8dSFan Du 
496351c739d1SDavid S. Miller 
496451c739d1SDavid S. Miller 
4965716ea3a7SDavid Howells /**
4966716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4967716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
4968716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
4969716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4970716ea3a7SDavid Howells  *
4971716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
4972716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
4973716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
4974716ea3a7SDavid Howells  *
4975716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
4976716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
4977716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
4978716ea3a7SDavid Howells  *
4979716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
4980716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
4981716ea3a7SDavid Howells  */
4982716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4983716ea3a7SDavid Howells {
4984716ea3a7SDavid Howells 	int copyflag;
4985716ea3a7SDavid Howells 	int elt;
4986716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
4987716ea3a7SDavid Howells 
4988716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
4989716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
4990716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
4991716ea3a7SDavid Howells 	 */
4992716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4993c15fc199SMiaohe Lin 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4994716ea3a7SDavid Howells 		return -ENOMEM;
4995716ea3a7SDavid Howells 
4996716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
499721dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
4998716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
4999716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
5000716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
5001716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
5002716ea3a7SDavid Howells 
5003716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
5004716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
5005716ea3a7SDavid Howells 			return -ENOMEM;
5006716ea3a7SDavid Howells 
5007716ea3a7SDavid Howells 		/* Voila! */
5008716ea3a7SDavid Howells 		*trailer = skb;
5009716ea3a7SDavid Howells 		return 1;
5010716ea3a7SDavid Howells 	}
5011716ea3a7SDavid Howells 
5012716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
5013716ea3a7SDavid Howells 
5014716ea3a7SDavid Howells 	elt = 1;
5015716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
5016716ea3a7SDavid Howells 	copyflag = 0;
5017716ea3a7SDavid Howells 
5018716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
5019716ea3a7SDavid Howells 		int ntail = 0;
5020716ea3a7SDavid Howells 
5021716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
5022716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
5023716ea3a7SDavid Howells 		 * after it. */
5024716ea3a7SDavid Howells 
5025716ea3a7SDavid Howells 		if (skb_shared(skb1))
5026716ea3a7SDavid Howells 			copyflag = 1;
5027716ea3a7SDavid Howells 
5028716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
5029716ea3a7SDavid Howells 
5030716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
5031716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
503221dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
5033716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
5034716ea3a7SDavid Howells 				ntail = tailbits + 128;
5035716ea3a7SDavid Howells 		}
5036716ea3a7SDavid Howells 
5037716ea3a7SDavid Howells 		if (copyflag ||
5038716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
5039716ea3a7SDavid Howells 		    ntail ||
5040716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
504121dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
5042716ea3a7SDavid Howells 			struct sk_buff *skb2;
5043716ea3a7SDavid Howells 
5044716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
5045716ea3a7SDavid Howells 			if (ntail == 0)
5046716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
5047716ea3a7SDavid Howells 			else
5048716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
5049716ea3a7SDavid Howells 						       skb_headroom(skb1),
5050716ea3a7SDavid Howells 						       ntail,
5051716ea3a7SDavid Howells 						       GFP_ATOMIC);
5052716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
5053716ea3a7SDavid Howells 				return -ENOMEM;
5054716ea3a7SDavid Howells 
5055716ea3a7SDavid Howells 			if (skb1->sk)
5056716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
5057716ea3a7SDavid Howells 
5058716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
5059716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
5060716ea3a7SDavid Howells 
5061716ea3a7SDavid Howells 			skb2->next = skb1->next;
5062716ea3a7SDavid Howells 			*skb_p = skb2;
5063716ea3a7SDavid Howells 			kfree_skb(skb1);
5064716ea3a7SDavid Howells 			skb1 = skb2;
5065716ea3a7SDavid Howells 		}
5066716ea3a7SDavid Howells 		elt++;
5067716ea3a7SDavid Howells 		*trailer = skb1;
5068716ea3a7SDavid Howells 		skb_p = &skb1->next;
5069716ea3a7SDavid Howells 	}
5070716ea3a7SDavid Howells 
5071716ea3a7SDavid Howells 	return elt;
5072716ea3a7SDavid Howells }
5073b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
5074716ea3a7SDavid Howells 
5075b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
5076b1faf566SEric Dumazet {
5077b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
5078b1faf566SEric Dumazet 
5079b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
5080b1faf566SEric Dumazet }
5081b1faf566SEric Dumazet 
50828605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
50838605330aSSoheil Hassas Yeganeh {
50848605330aSSoheil Hassas Yeganeh 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
50858605330aSSoheil Hassas Yeganeh 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
50868605330aSSoheil Hassas Yeganeh 	 */
50878605330aSSoheil Hassas Yeganeh 	skb->pkt_type = PACKET_OUTGOING;
50888605330aSSoheil Hassas Yeganeh 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
50898605330aSSoheil Hassas Yeganeh }
50908605330aSSoheil Hassas Yeganeh 
5091b1faf566SEric Dumazet /*
5092b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
5093b1faf566SEric Dumazet  */
5094b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
5095b1faf566SEric Dumazet {
5096b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
5097ebb3b78dSEric Dumazet 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
5098b1faf566SEric Dumazet 		return -ENOMEM;
5099b1faf566SEric Dumazet 
5100b1faf566SEric Dumazet 	skb_orphan(skb);
5101b1faf566SEric Dumazet 	skb->sk = sk;
5102b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
5103b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
51048605330aSSoheil Hassas Yeganeh 	skb_set_err_queue(skb);
5105b1faf566SEric Dumazet 
5106abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
5107abb57ea4SEric Dumazet 	skb_dst_force(skb);
5108abb57ea4SEric Dumazet 
5109b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
5110b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
5111e3ae2365SAlexander Aring 		sk_error_report(sk);
5112b1faf566SEric Dumazet 	return 0;
5113b1faf566SEric Dumazet }
5114b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
5115b1faf566SEric Dumazet 
511683a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
511783a1a1a7SSoheil Hassas Yeganeh {
511883a1a1a7SSoheil Hassas Yeganeh 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
511983a1a1a7SSoheil Hassas Yeganeh 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
512083a1a1a7SSoheil Hassas Yeganeh }
512183a1a1a7SSoheil Hassas Yeganeh 
5122364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
5123364a9e93SWillem de Bruijn {
5124364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
512583a1a1a7SSoheil Hassas Yeganeh 	struct sk_buff *skb, *skb_next = NULL;
512683a1a1a7SSoheil Hassas Yeganeh 	bool icmp_next = false;
5127997d5c3fSEric Dumazet 	unsigned long flags;
5128364a9e93SWillem de Bruijn 
5129997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
5130364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
513138b25793SSoheil Hassas Yeganeh 	if (skb && (skb_next = skb_peek(q))) {
513283a1a1a7SSoheil Hassas Yeganeh 		icmp_next = is_icmp_err_skb(skb_next);
513338b25793SSoheil Hassas Yeganeh 		if (icmp_next)
5134985f7337SWillem de Bruijn 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
513538b25793SSoheil Hassas Yeganeh 	}
5136997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
5137364a9e93SWillem de Bruijn 
513883a1a1a7SSoheil Hassas Yeganeh 	if (is_icmp_err_skb(skb) && !icmp_next)
513983a1a1a7SSoheil Hassas Yeganeh 		sk->sk_err = 0;
514083a1a1a7SSoheil Hassas Yeganeh 
514183a1a1a7SSoheil Hassas Yeganeh 	if (skb_next)
5142e3ae2365SAlexander Aring 		sk_error_report(sk);
5143364a9e93SWillem de Bruijn 
5144364a9e93SWillem de Bruijn 	return skb;
5145364a9e93SWillem de Bruijn }
5146364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
5147364a9e93SWillem de Bruijn 
5148cab41c47SAlexander Duyck /**
5149cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
5150cab41c47SAlexander Duyck  * @skb: the skb to clone
5151cab41c47SAlexander Duyck  *
5152cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
5153cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
5154cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
5155cab41c47SAlexander Duyck  *
5156cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
5157cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
5158cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
5159cab41c47SAlexander Duyck  * the sk_error_queue.
5160cab41c47SAlexander Duyck  */
516162bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
516262bccb8cSAlexander Duyck {
516362bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
516462bccb8cSAlexander Duyck 	struct sk_buff *clone;
516562bccb8cSAlexander Duyck 
516641c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
516762bccb8cSAlexander Duyck 		return NULL;
516862bccb8cSAlexander Duyck 
516962bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
517062bccb8cSAlexander Duyck 	if (!clone) {
517162bccb8cSAlexander Duyck 		sock_put(sk);
517262bccb8cSAlexander Duyck 		return NULL;
517362bccb8cSAlexander Duyck 	}
517462bccb8cSAlexander Duyck 
517562bccb8cSAlexander Duyck 	clone->sk = sk;
517662bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
517762bccb8cSAlexander Duyck 
517862bccb8cSAlexander Duyck 	return clone;
517962bccb8cSAlexander Duyck }
518062bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
518162bccb8cSAlexander Duyck 
518237846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
518337846ef0SAlexander Duyck 					struct sock *sk,
51844ef1b286SSoheil Hassas Yeganeh 					int tstype,
51854ef1b286SSoheil Hassas Yeganeh 					bool opt_stats)
5186ac45f602SPatrick Ohly {
5187ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
5188ac45f602SPatrick Ohly 	int err;
5189ac45f602SPatrick Ohly 
51904ef1b286SSoheil Hassas Yeganeh 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
51914ef1b286SSoheil Hassas Yeganeh 
5192ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
5193ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
5194ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
5195ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
5196e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
51974ef1b286SSoheil Hassas Yeganeh 	serr->opt_stats = opt_stats;
51981862d620SWillem de Bruijn 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
51994ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
520009c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
520142f67eeaSEric Dumazet 		if (sk_is_tcp(sk))
5202a1cdec57SEric Dumazet 			serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
52034ed2d765SWillem de Bruijn 	}
520429030374SEric Dumazet 
5205ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
520629030374SEric Dumazet 
5207ac45f602SPatrick Ohly 	if (err)
5208ac45f602SPatrick Ohly 		kfree_skb(skb);
5209ac45f602SPatrick Ohly }
521037846ef0SAlexander Duyck 
5211b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
5212b245be1fSWillem de Bruijn {
5213b245be1fSWillem de Bruijn 	bool ret;
5214b245be1fSWillem de Bruijn 
5215d2154b0aSKuniyuki Iwashima 	if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
5216b245be1fSWillem de Bruijn 		return true;
5217b245be1fSWillem de Bruijn 
5218b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
5219b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
5220b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
5221b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
5222b245be1fSWillem de Bruijn 	return ret;
5223b245be1fSWillem de Bruijn }
5224b245be1fSWillem de Bruijn 
522537846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
522637846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
522737846ef0SAlexander Duyck {
522837846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
522937846ef0SAlexander Duyck 
5230b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
523135b99dffSWillem de Bruijn 		goto err;
5232b245be1fSWillem de Bruijn 
52339ac25fc0SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
52349ac25fc0SEric Dumazet 	 * but only if the socket refcount is not zero.
52359ac25fc0SEric Dumazet 	 */
523641c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
523737846ef0SAlexander Duyck 		*skb_hwtstamps(skb) = *hwtstamps;
52384ef1b286SSoheil Hassas Yeganeh 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
523937846ef0SAlexander Duyck 		sock_put(sk);
524035b99dffSWillem de Bruijn 		return;
524137846ef0SAlexander Duyck 	}
524235b99dffSWillem de Bruijn 
524335b99dffSWillem de Bruijn err:
524435b99dffSWillem de Bruijn 	kfree_skb(skb);
52459ac25fc0SEric Dumazet }
524637846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
524737846ef0SAlexander Duyck 
524837846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
5249e7ed11eeSYousuk Seung 		     const struct sk_buff *ack_skb,
525037846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
525137846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
525237846ef0SAlexander Duyck {
525337846ef0SAlexander Duyck 	struct sk_buff *skb;
52544ef1b286SSoheil Hassas Yeganeh 	bool tsonly, opt_stats = false;
525537846ef0SAlexander Duyck 
52563a8dd971SWillem de Bruijn 	if (!sk)
52573a8dd971SWillem de Bruijn 		return;
52583a8dd971SWillem de Bruijn 
5259b50a5c70SMiroslav Lichvar 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
5260b50a5c70SMiroslav Lichvar 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
5261b50a5c70SMiroslav Lichvar 		return;
5262b50a5c70SMiroslav Lichvar 
52633a8dd971SWillem de Bruijn 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
52643a8dd971SWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, tsonly))
526537846ef0SAlexander Duyck 		return;
526637846ef0SAlexander Duyck 
52671c885808SFrancis Yan 	if (tsonly) {
52681c885808SFrancis Yan #ifdef CONFIG_INET
52691c885808SFrancis Yan 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
527042f67eeaSEric Dumazet 		    sk_is_tcp(sk)) {
5271e7ed11eeSYousuk Seung 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
5272e7ed11eeSYousuk Seung 							     ack_skb);
52734ef1b286SSoheil Hassas Yeganeh 			opt_stats = true;
52744ef1b286SSoheil Hassas Yeganeh 		} else
52751c885808SFrancis Yan #endif
52761c885808SFrancis Yan 			skb = alloc_skb(0, GFP_ATOMIC);
52771c885808SFrancis Yan 	} else {
527837846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
527950749f2dSKuniyuki Iwashima 
52808a02fb71SPratyush Yadav 		if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) {
52818a02fb71SPratyush Yadav 			kfree_skb(skb);
528250749f2dSKuniyuki Iwashima 			return;
52831c885808SFrancis Yan 		}
52848a02fb71SPratyush Yadav 	}
528537846ef0SAlexander Duyck 	if (!skb)
528637846ef0SAlexander Duyck 		return;
528737846ef0SAlexander Duyck 
528849ca0d8bSWillem de Bruijn 	if (tsonly) {
5289fff88030SWillem de Bruijn 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
5290fff88030SWillem de Bruijn 					     SKBTX_ANY_TSTAMP;
529149ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
529249ca0d8bSWillem de Bruijn 	}
529349ca0d8bSWillem de Bruijn 
529449ca0d8bSWillem de Bruijn 	if (hwtstamps)
529549ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
529649ca0d8bSWillem de Bruijn 	else
5297d93376f5SMartin KaFai Lau 		__net_timestamp(skb);
529849ca0d8bSWillem de Bruijn 
52994ef1b286SSoheil Hassas Yeganeh 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
530037846ef0SAlexander Duyck }
5301e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
5302e7fd2885SWillem de Bruijn 
5303e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
5304e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
5305e7fd2885SWillem de Bruijn {
5306e7ed11eeSYousuk Seung 	return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
5307e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
5308e7fd2885SWillem de Bruijn }
5309ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
5310ac45f602SPatrick Ohly 
5311eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS
53126e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
53136e3e939fSJohannes Berg {
53146e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
53156e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
5316dd4f1072SEric Dumazet 	int err = 1;
53176e3e939fSJohannes Berg 
53186e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
53196e3e939fSJohannes Berg 	skb->wifi_acked = acked;
53206e3e939fSJohannes Berg 
53216e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
53226e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
53236e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
53246e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
53256e3e939fSJohannes Berg 
5326dd4f1072SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
5327dd4f1072SEric Dumazet 	 * but only if the socket refcount is not zero.
5328dd4f1072SEric Dumazet 	 */
532941c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
53306e3e939fSJohannes Berg 		err = sock_queue_err_skb(sk, skb);
5331dd4f1072SEric Dumazet 		sock_put(sk);
5332dd4f1072SEric Dumazet 	}
53336e3e939fSJohannes Berg 	if (err)
53346e3e939fSJohannes Berg 		kfree_skb(skb);
53356e3e939fSJohannes Berg }
53366e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5337eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */
53386e3e939fSJohannes Berg 
5339f35d9d8aSRusty Russell /**
5340f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
5341f35d9d8aSRusty Russell  * @skb: the skb to set
5342f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
5343f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
5344f35d9d8aSRusty Russell  *
5345f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
5346f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5347f35d9d8aSRusty Russell  *
5348f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
5349f35d9d8aSRusty Russell  * returns false you should drop the packet.
5350f35d9d8aSRusty Russell  */
5351f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5352f35d9d8aSRusty Russell {
535352b5d6f5SEric Dumazet 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
535452b5d6f5SEric Dumazet 	u32 csum_start = skb_headroom(skb) + (u32)start;
535552b5d6f5SEric Dumazet 
5356424f8416SEric Dumazet 	if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) {
535752b5d6f5SEric Dumazet 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
535852b5d6f5SEric Dumazet 				     start, off, skb_headroom(skb), skb_headlen(skb));
5359f35d9d8aSRusty Russell 		return false;
5360f35d9d8aSRusty Russell 	}
5361f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
536252b5d6f5SEric Dumazet 	skb->csum_start = csum_start;
5363f35d9d8aSRusty Russell 	skb->csum_offset = off;
5364424f8416SEric Dumazet 	skb->transport_header = csum_start;
5365f35d9d8aSRusty Russell 	return true;
5366f35d9d8aSRusty Russell }
5367b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5368f35d9d8aSRusty Russell 
5369ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5370ed1f50c3SPaul Durrant 			       unsigned int max)
5371ed1f50c3SPaul Durrant {
5372ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
5373ed1f50c3SPaul Durrant 		return 0;
5374ed1f50c3SPaul Durrant 
5375ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
5376ed1f50c3SPaul Durrant 	 * won't need to do it again.
5377ed1f50c3SPaul Durrant 	 */
5378ed1f50c3SPaul Durrant 	if (max > skb->len)
5379ed1f50c3SPaul Durrant 		max = skb->len;
5380ed1f50c3SPaul Durrant 
5381ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5382ed1f50c3SPaul Durrant 		return -ENOMEM;
5383ed1f50c3SPaul Durrant 
5384ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
5385ed1f50c3SPaul Durrant 		return -EPROTO;
5386ed1f50c3SPaul Durrant 
5387ed1f50c3SPaul Durrant 	return 0;
5388ed1f50c3SPaul Durrant }
5389ed1f50c3SPaul Durrant 
5390f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
5391f9708b43SJan Beulich 
5392f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5393f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
5394f9708b43SJan Beulich 				      unsigned int off)
5395f9708b43SJan Beulich {
5396f9708b43SJan Beulich 	int err;
5397f9708b43SJan Beulich 
5398161d1792SKees Cook 	switch (proto) {
5399f9708b43SJan Beulich 	case IPPROTO_TCP:
5400f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5401f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
5402f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
5403f9708b43SJan Beulich 						  offsetof(struct tcphdr,
5404f9708b43SJan Beulich 							   check)))
5405f9708b43SJan Beulich 			err = -EPROTO;
5406f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5407f9708b43SJan Beulich 
5408f9708b43SJan Beulich 	case IPPROTO_UDP:
5409f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5410f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
5411f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
5412f9708b43SJan Beulich 						  offsetof(struct udphdr,
5413f9708b43SJan Beulich 							   check)))
5414f9708b43SJan Beulich 			err = -EPROTO;
5415f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5416f9708b43SJan Beulich 	}
5417f9708b43SJan Beulich 
5418f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
5419f9708b43SJan Beulich }
5420f9708b43SJan Beulich 
5421ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
5422ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
5423ed1f50c3SPaul Durrant  */
5424ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
5425ed1f50c3SPaul Durrant 
5426f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5427ed1f50c3SPaul Durrant {
5428ed1f50c3SPaul Durrant 	unsigned int off;
5429ed1f50c3SPaul Durrant 	bool fragment;
5430f9708b43SJan Beulich 	__sum16 *csum;
5431ed1f50c3SPaul Durrant 	int err;
5432ed1f50c3SPaul Durrant 
5433ed1f50c3SPaul Durrant 	fragment = false;
5434ed1f50c3SPaul Durrant 
5435ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
5436ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
5437ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
5438ed1f50c3SPaul Durrant 	if (err < 0)
5439ed1f50c3SPaul Durrant 		goto out;
5440ed1f50c3SPaul Durrant 
544111f920d2SMiaohe Lin 	if (ip_is_fragment(ip_hdr(skb)))
5442ed1f50c3SPaul Durrant 		fragment = true;
5443ed1f50c3SPaul Durrant 
5444ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
5445ed1f50c3SPaul Durrant 
5446ed1f50c3SPaul Durrant 	err = -EPROTO;
5447ed1f50c3SPaul Durrant 
5448ed1f50c3SPaul Durrant 	if (fragment)
5449ed1f50c3SPaul Durrant 		goto out;
5450ed1f50c3SPaul Durrant 
5451f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5452f9708b43SJan Beulich 	if (IS_ERR(csum))
5453f9708b43SJan Beulich 		return PTR_ERR(csum);
5454ed1f50c3SPaul Durrant 
5455ed1f50c3SPaul Durrant 	if (recalculate)
5456f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5457ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
5458ed1f50c3SPaul Durrant 					   skb->len - off,
5459f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
5460ed1f50c3SPaul Durrant 	err = 0;
5461ed1f50c3SPaul Durrant 
5462ed1f50c3SPaul Durrant out:
5463ed1f50c3SPaul Durrant 	return err;
5464ed1f50c3SPaul Durrant }
5465ed1f50c3SPaul Durrant 
5466ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
5467ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
5468ed1f50c3SPaul Durrant  */
5469ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
5470ed1f50c3SPaul Durrant 
5471ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
5472ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
5473ed1f50c3SPaul Durrant 
5474ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5475ed1f50c3SPaul Durrant {
5476ed1f50c3SPaul Durrant 	int err;
5477ed1f50c3SPaul Durrant 	u8 nexthdr;
5478ed1f50c3SPaul Durrant 	unsigned int off;
5479ed1f50c3SPaul Durrant 	unsigned int len;
5480ed1f50c3SPaul Durrant 	bool fragment;
5481ed1f50c3SPaul Durrant 	bool done;
5482f9708b43SJan Beulich 	__sum16 *csum;
5483ed1f50c3SPaul Durrant 
5484ed1f50c3SPaul Durrant 	fragment = false;
5485ed1f50c3SPaul Durrant 	done = false;
5486ed1f50c3SPaul Durrant 
5487ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
5488ed1f50c3SPaul Durrant 
5489ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5490ed1f50c3SPaul Durrant 	if (err < 0)
5491ed1f50c3SPaul Durrant 		goto out;
5492ed1f50c3SPaul Durrant 
5493ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
5494ed1f50c3SPaul Durrant 
5495ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5496ed1f50c3SPaul Durrant 	while (off <= len && !done) {
5497ed1f50c3SPaul Durrant 		switch (nexthdr) {
5498ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
5499ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
5500ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
5501ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
5502ed1f50c3SPaul Durrant 
5503ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
5504ed1f50c3SPaul Durrant 						  off +
5505ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
5506ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
5507ed1f50c3SPaul Durrant 			if (err < 0)
5508ed1f50c3SPaul Durrant 				goto out;
5509ed1f50c3SPaul Durrant 
5510ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5511ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
5512ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
5513ed1f50c3SPaul Durrant 			break;
5514ed1f50c3SPaul Durrant 		}
5515ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
5516ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
5517ed1f50c3SPaul Durrant 
5518ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
5519ed1f50c3SPaul Durrant 						  off +
5520ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
5521ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
5522ed1f50c3SPaul Durrant 			if (err < 0)
5523ed1f50c3SPaul Durrant 				goto out;
5524ed1f50c3SPaul Durrant 
5525ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5526ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
5527ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
5528ed1f50c3SPaul Durrant 			break;
5529ed1f50c3SPaul Durrant 		}
5530ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
5531ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
5532ed1f50c3SPaul Durrant 
5533ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
5534ed1f50c3SPaul Durrant 						  off +
5535ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
5536ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
5537ed1f50c3SPaul Durrant 			if (err < 0)
5538ed1f50c3SPaul Durrant 				goto out;
5539ed1f50c3SPaul Durrant 
5540ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
5541ed1f50c3SPaul Durrant 
5542ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5543ed1f50c3SPaul Durrant 				fragment = true;
5544ed1f50c3SPaul Durrant 
5545ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
5546ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
5547ed1f50c3SPaul Durrant 			break;
5548ed1f50c3SPaul Durrant 		}
5549ed1f50c3SPaul Durrant 		default:
5550ed1f50c3SPaul Durrant 			done = true;
5551ed1f50c3SPaul Durrant 			break;
5552ed1f50c3SPaul Durrant 		}
5553ed1f50c3SPaul Durrant 	}
5554ed1f50c3SPaul Durrant 
5555ed1f50c3SPaul Durrant 	err = -EPROTO;
5556ed1f50c3SPaul Durrant 
5557ed1f50c3SPaul Durrant 	if (!done || fragment)
5558ed1f50c3SPaul Durrant 		goto out;
5559ed1f50c3SPaul Durrant 
5560f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
5561f9708b43SJan Beulich 	if (IS_ERR(csum))
5562f9708b43SJan Beulich 		return PTR_ERR(csum);
5563ed1f50c3SPaul Durrant 
5564ed1f50c3SPaul Durrant 	if (recalculate)
5565f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5566ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
5567f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
5568ed1f50c3SPaul Durrant 	err = 0;
5569ed1f50c3SPaul Durrant 
5570ed1f50c3SPaul Durrant out:
5571ed1f50c3SPaul Durrant 	return err;
5572ed1f50c3SPaul Durrant }
5573ed1f50c3SPaul Durrant 
5574ed1f50c3SPaul Durrant /**
5575ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
5576ed1f50c3SPaul Durrant  * @skb: the skb to set up
5577ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
5578ed1f50c3SPaul Durrant  */
5579ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5580ed1f50c3SPaul Durrant {
5581ed1f50c3SPaul Durrant 	int err;
5582ed1f50c3SPaul Durrant 
5583ed1f50c3SPaul Durrant 	switch (skb->protocol) {
5584ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
5585f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
5586ed1f50c3SPaul Durrant 		break;
5587ed1f50c3SPaul Durrant 
5588ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
5589ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
5590ed1f50c3SPaul Durrant 		break;
5591ed1f50c3SPaul Durrant 
5592ed1f50c3SPaul Durrant 	default:
5593ed1f50c3SPaul Durrant 		err = -EPROTO;
5594ed1f50c3SPaul Durrant 		break;
5595ed1f50c3SPaul Durrant 	}
5596ed1f50c3SPaul Durrant 
5597ed1f50c3SPaul Durrant 	return err;
5598ed1f50c3SPaul Durrant }
5599ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
5600ed1f50c3SPaul Durrant 
56019afd85c9SLinus Lüssing /**
56029afd85c9SLinus Lüssing  * skb_checksum_maybe_trim - maybe trims the given skb
56039afd85c9SLinus Lüssing  * @skb: the skb to check
56049afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
56059afd85c9SLinus Lüssing  *
56069afd85c9SLinus Lüssing  * Checks whether the given skb has data beyond the given transport length.
56079afd85c9SLinus Lüssing  * If so, returns a cloned skb trimmed to this transport length.
56089afd85c9SLinus Lüssing  * Otherwise returns the provided skb. Returns NULL in error cases
56099afd85c9SLinus Lüssing  * (e.g. transport_len exceeds skb length or out-of-memory).
56109afd85c9SLinus Lüssing  *
5611a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5612a516993fSLinus Lüssing  * differs from the provided skb.
56139afd85c9SLinus Lüssing  */
56149afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
56159afd85c9SLinus Lüssing 					       unsigned int transport_len)
56169afd85c9SLinus Lüssing {
56179afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
56189afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + transport_len;
56199afd85c9SLinus Lüssing 	int ret;
56209afd85c9SLinus Lüssing 
5621a516993fSLinus Lüssing 	if (skb->len < len)
56229afd85c9SLinus Lüssing 		return NULL;
5623a516993fSLinus Lüssing 	else if (skb->len == len)
56249afd85c9SLinus Lüssing 		return skb;
56259afd85c9SLinus Lüssing 
56269afd85c9SLinus Lüssing 	skb_chk = skb_clone(skb, GFP_ATOMIC);
56279afd85c9SLinus Lüssing 	if (!skb_chk)
56289afd85c9SLinus Lüssing 		return NULL;
56299afd85c9SLinus Lüssing 
56309afd85c9SLinus Lüssing 	ret = pskb_trim_rcsum(skb_chk, len);
56319afd85c9SLinus Lüssing 	if (ret) {
56329afd85c9SLinus Lüssing 		kfree_skb(skb_chk);
56339afd85c9SLinus Lüssing 		return NULL;
56349afd85c9SLinus Lüssing 	}
56359afd85c9SLinus Lüssing 
56369afd85c9SLinus Lüssing 	return skb_chk;
56379afd85c9SLinus Lüssing }
56389afd85c9SLinus Lüssing 
56399afd85c9SLinus Lüssing /**
56409afd85c9SLinus Lüssing  * skb_checksum_trimmed - validate checksum of an skb
56419afd85c9SLinus Lüssing  * @skb: the skb to check
56429afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
56439afd85c9SLinus Lüssing  * @skb_chkf: checksum function to use
56449afd85c9SLinus Lüssing  *
56459afd85c9SLinus Lüssing  * Applies the given checksum function skb_chkf to the provided skb.
56469afd85c9SLinus Lüssing  * Returns a checked and maybe trimmed skb. Returns NULL on error.
56479afd85c9SLinus Lüssing  *
56489afd85c9SLinus Lüssing  * If the skb has data beyond the given transport length, then a
56499afd85c9SLinus Lüssing  * trimmed & cloned skb is checked and returned.
56509afd85c9SLinus Lüssing  *
5651a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5652a516993fSLinus Lüssing  * differs from the provided skb.
56539afd85c9SLinus Lüssing  */
56549afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
56559afd85c9SLinus Lüssing 				     unsigned int transport_len,
56569afd85c9SLinus Lüssing 				     __sum16(*skb_chkf)(struct sk_buff *skb))
56579afd85c9SLinus Lüssing {
56589afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
56599afd85c9SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
5660fcba67c9SLinus Lüssing 	__sum16 ret;
56619afd85c9SLinus Lüssing 
56629afd85c9SLinus Lüssing 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
56639afd85c9SLinus Lüssing 	if (!skb_chk)
5664a516993fSLinus Lüssing 		goto err;
56659afd85c9SLinus Lüssing 
5666a516993fSLinus Lüssing 	if (!pskb_may_pull(skb_chk, offset))
5667a516993fSLinus Lüssing 		goto err;
56689afd85c9SLinus Lüssing 
56699b368814SLinus Lüssing 	skb_pull_rcsum(skb_chk, offset);
56709afd85c9SLinus Lüssing 	ret = skb_chkf(skb_chk);
56719b368814SLinus Lüssing 	skb_push_rcsum(skb_chk, offset);
56729afd85c9SLinus Lüssing 
5673a516993fSLinus Lüssing 	if (ret)
5674a516993fSLinus Lüssing 		goto err;
56759afd85c9SLinus Lüssing 
56769afd85c9SLinus Lüssing 	return skb_chk;
5677a516993fSLinus Lüssing 
5678a516993fSLinus Lüssing err:
5679a516993fSLinus Lüssing 	if (skb_chk && skb_chk != skb)
5680a516993fSLinus Lüssing 		kfree_skb(skb_chk);
5681a516993fSLinus Lüssing 
5682a516993fSLinus Lüssing 	return NULL;
5683a516993fSLinus Lüssing 
56849afd85c9SLinus Lüssing }
56859afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
56869afd85c9SLinus Lüssing 
56874497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
56884497b076SBen Hutchings {
5689e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5690e87cc472SJoe Perches 			     skb->dev->name);
56914497b076SBen Hutchings }
56924497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5693bad43ca8SEric Dumazet 
5694bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5695bad43ca8SEric Dumazet {
56963d861f66SEric Dumazet 	if (head_stolen) {
56973d861f66SEric Dumazet 		skb_release_head_state(skb);
5698025a785fSJakub Kicinski 		kmem_cache_free(skbuff_cache, skb);
56993d861f66SEric Dumazet 	} else {
5700bad43ca8SEric Dumazet 		__kfree_skb(skb);
5701bad43ca8SEric Dumazet 	}
57023d861f66SEric Dumazet }
5703bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
5704bad43ca8SEric Dumazet 
5705bad43ca8SEric Dumazet /**
5706bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
5707bad43ca8SEric Dumazet  * @to: prior buffer
5708bad43ca8SEric Dumazet  * @from: buffer to add
5709bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
5710c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
5711bad43ca8SEric Dumazet  */
5712bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5713bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
5714bad43ca8SEric Dumazet {
5715c818fa9eSEric Dumazet 	struct skb_shared_info *to_shinfo, *from_shinfo;
5716bad43ca8SEric Dumazet 	int i, delta, len = from->len;
5717bad43ca8SEric Dumazet 
5718bad43ca8SEric Dumazet 	*fragstolen = false;
5719bad43ca8SEric Dumazet 
5720bad43ca8SEric Dumazet 	if (skb_cloned(to))
5721bad43ca8SEric Dumazet 		return false;
5722bad43ca8SEric Dumazet 
57230646dc31SLiang Chen 	/* In general, avoid mixing page_pool and non-page_pool allocated
57240646dc31SLiang Chen 	 * pages within the same SKB. Additionally avoid dealing with clones
57250646dc31SLiang Chen 	 * with page_pool pages, in case the SKB is using page_pool fragment
57261effe8caSJean-Philippe Brucker 	 * references (PP_FLAG_PAGE_FRAG). Since we only take full page
57271effe8caSJean-Philippe Brucker 	 * references for cloned SKBs at the moment that would result in
57281effe8caSJean-Philippe Brucker 	 * inconsistent reference counts.
57290646dc31SLiang Chen 	 * In theory we could take full references if @from is cloned and
57300646dc31SLiang Chen 	 * !@to->pp_recycle but its tricky (due to potential race with
57310646dc31SLiang Chen 	 * the clone disappearing) and rare, so not worth dealing with.
57326a5bcd84SIlias Apalodimas 	 */
57330646dc31SLiang Chen 	if (to->pp_recycle != from->pp_recycle ||
57340646dc31SLiang Chen 	    (from->pp_recycle && skb_cloned(from)))
57356a5bcd84SIlias Apalodimas 		return false;
57366a5bcd84SIlias Apalodimas 
5737bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
5738e93a0435SEric Dumazet 		if (len)
5739bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5740bad43ca8SEric Dumazet 		*delta_truesize = 0;
5741bad43ca8SEric Dumazet 		return true;
5742bad43ca8SEric Dumazet 	}
5743bad43ca8SEric Dumazet 
5744c818fa9eSEric Dumazet 	to_shinfo = skb_shinfo(to);
5745c818fa9eSEric Dumazet 	from_shinfo = skb_shinfo(from);
5746c818fa9eSEric Dumazet 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5747bad43ca8SEric Dumazet 		return false;
57481f8b977aSWillem de Bruijn 	if (skb_zcopy(to) || skb_zcopy(from))
57491f8b977aSWillem de Bruijn 		return false;
5750bad43ca8SEric Dumazet 
5751bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
5752bad43ca8SEric Dumazet 		struct page *page;
5753bad43ca8SEric Dumazet 		unsigned int offset;
5754bad43ca8SEric Dumazet 
5755c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5756c818fa9eSEric Dumazet 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5757bad43ca8SEric Dumazet 			return false;
5758bad43ca8SEric Dumazet 
5759bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
5760bad43ca8SEric Dumazet 			return false;
5761bad43ca8SEric Dumazet 
5762bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5763bad43ca8SEric Dumazet 
5764bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
5765bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
5766bad43ca8SEric Dumazet 
5767c818fa9eSEric Dumazet 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5768bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
5769bad43ca8SEric Dumazet 		*fragstolen = true;
5770bad43ca8SEric Dumazet 	} else {
5771c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5772c818fa9eSEric Dumazet 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5773bad43ca8SEric Dumazet 			return false;
5774bad43ca8SEric Dumazet 
5775f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5776bad43ca8SEric Dumazet 	}
5777bad43ca8SEric Dumazet 
5778bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
5779bad43ca8SEric Dumazet 
5780c818fa9eSEric Dumazet 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5781c818fa9eSEric Dumazet 	       from_shinfo->frags,
5782c818fa9eSEric Dumazet 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5783c818fa9eSEric Dumazet 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5784bad43ca8SEric Dumazet 
5785bad43ca8SEric Dumazet 	if (!skb_cloned(from))
5786c818fa9eSEric Dumazet 		from_shinfo->nr_frags = 0;
5787bad43ca8SEric Dumazet 
57888ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
57898ea853fdSLi RongQing 	 * since we set nr_frags to 0.
57908ea853fdSLi RongQing 	 */
5791c818fa9eSEric Dumazet 	for (i = 0; i < from_shinfo->nr_frags; i++)
5792c818fa9eSEric Dumazet 		__skb_frag_ref(&from_shinfo->frags[i]);
5793bad43ca8SEric Dumazet 
5794bad43ca8SEric Dumazet 	to->truesize += delta;
5795bad43ca8SEric Dumazet 	to->len += len;
5796bad43ca8SEric Dumazet 	to->data_len += len;
5797bad43ca8SEric Dumazet 
5798bad43ca8SEric Dumazet 	*delta_truesize = delta;
5799bad43ca8SEric Dumazet 	return true;
5800bad43ca8SEric Dumazet }
5801bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
5802621e84d6SNicolas Dichtel 
5803621e84d6SNicolas Dichtel /**
58048b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
5805621e84d6SNicolas Dichtel  *
5806621e84d6SNicolas Dichtel  * @skb: buffer to clean
58078b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
5808621e84d6SNicolas Dichtel  *
58098b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
58108b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
58118b27f277SNicolas Dichtel  * operations.
58128b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
58138b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
58148b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
5815621e84d6SNicolas Dichtel  */
58168b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5817621e84d6SNicolas Dichtel {
5818621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
5819621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
582060ff7467SWANG Cong 	skb->ignore_df = 0;
5821621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
5822174e2381SFlorian Westphal 	skb_ext_reset(skb);
5823895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
5824621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
5825213dd74aSHerbert Xu 
58266f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV
58276f9a5069SPetr Machata 	skb->offload_fwd_mark = 0;
5828875e8939SIdo Schimmel 	skb->offload_l3_fwd_mark = 0;
58296f9a5069SPetr Machata #endif
58306f9a5069SPetr Machata 
5831213dd74aSHerbert Xu 	if (!xnet)
5832213dd74aSHerbert Xu 		return;
5833213dd74aSHerbert Xu 
58342b5ec1a5SYe Yin 	ipvs_reset(skb);
5835213dd74aSHerbert Xu 	skb->mark = 0;
5836de799101SMartin KaFai Lau 	skb_clear_tstamp(skb);
5837621e84d6SNicolas Dichtel }
5838621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
5839de960aa9SFlorian Westphal 
58400d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
58410d5501c1SVlad Yasevich {
5842d85e8be2SYuya Kusakabe 	int mac_len, meta_len;
5843d85e8be2SYuya Kusakabe 	void *meta;
58444bbb3e0eSToshiaki Makita 
58450d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
58460d5501c1SVlad Yasevich 		kfree_skb(skb);
58470d5501c1SVlad Yasevich 		return NULL;
58480d5501c1SVlad Yasevich 	}
58490d5501c1SVlad Yasevich 
58504bbb3e0eSToshiaki Makita 	mac_len = skb->data - skb_mac_header(skb);
5851ae474573SToshiaki Makita 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
58524bbb3e0eSToshiaki Makita 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
58534bbb3e0eSToshiaki Makita 			mac_len - VLAN_HLEN - ETH_TLEN);
5854ae474573SToshiaki Makita 	}
5855d85e8be2SYuya Kusakabe 
5856d85e8be2SYuya Kusakabe 	meta_len = skb_metadata_len(skb);
5857d85e8be2SYuya Kusakabe 	if (meta_len) {
5858d85e8be2SYuya Kusakabe 		meta = skb_metadata_end(skb) - meta_len;
5859d85e8be2SYuya Kusakabe 		memmove(meta + VLAN_HLEN, meta, meta_len);
5860d85e8be2SYuya Kusakabe 	}
5861d85e8be2SYuya Kusakabe 
58620d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
58630d5501c1SVlad Yasevich 	return skb;
58640d5501c1SVlad Yasevich }
58650d5501c1SVlad Yasevich 
58660d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
58670d5501c1SVlad Yasevich {
58680d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
58690d5501c1SVlad Yasevich 	u16 vlan_tci;
58700d5501c1SVlad Yasevich 
5871df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
58720d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
58730d5501c1SVlad Yasevich 		return skb;
58740d5501c1SVlad Yasevich 	}
58750d5501c1SVlad Yasevich 
58760d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
58770d5501c1SVlad Yasevich 	if (unlikely(!skb))
58780d5501c1SVlad Yasevich 		goto err_free;
587955eff0ebSMiaohe Lin 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
588055eff0ebSMiaohe Lin 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
58810d5501c1SVlad Yasevich 		goto err_free;
58820d5501c1SVlad Yasevich 
58830d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
58840d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
58850d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
58860d5501c1SVlad Yasevich 
58870d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
58880d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
58890d5501c1SVlad Yasevich 
58900d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
58910d5501c1SVlad Yasevich 	if (unlikely(!skb))
58920d5501c1SVlad Yasevich 		goto err_free;
58930d5501c1SVlad Yasevich 
58940d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
58958be33ecfSAlexander Lobakin 	if (!skb_transport_header_was_set(skb))
58960d5501c1SVlad Yasevich 		skb_reset_transport_header(skb);
58970d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
58980d5501c1SVlad Yasevich 
58990d5501c1SVlad Yasevich 	return skb;
59000d5501c1SVlad Yasevich 
59010d5501c1SVlad Yasevich err_free:
59020d5501c1SVlad Yasevich 	kfree_skb(skb);
59030d5501c1SVlad Yasevich 	return NULL;
59040d5501c1SVlad Yasevich }
59050d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
59062e4e4410SEric Dumazet 
590792ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
5908e2195121SJiri Pirko {
5909e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
5910e2195121SJiri Pirko 		return -ENOMEM;
5911e2195121SJiri Pirko 
5912e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5913e2195121SJiri Pirko 		return 0;
5914e2195121SJiri Pirko 
5915e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5916e2195121SJiri Pirko }
5917e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
5918e2195121SJiri Pirko 
5919bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
5920bfca4c52SShmulik Ladkani  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5921bfca4c52SShmulik Ladkani  */
5922bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
592393515d53SJiri Pirko {
5924b6a79208SShmulik Ladkani 	int offset = skb->data - skb_mac_header(skb);
592593515d53SJiri Pirko 	int err;
592693515d53SJiri Pirko 
5927b6a79208SShmulik Ladkani 	if (WARN_ONCE(offset,
5928b6a79208SShmulik Ladkani 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5929b6a79208SShmulik Ladkani 		      offset)) {
5930b6a79208SShmulik Ladkani 		return -EINVAL;
5931b6a79208SShmulik Ladkani 	}
5932b6a79208SShmulik Ladkani 
593393515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
593493515d53SJiri Pirko 	if (unlikely(err))
5935b6a79208SShmulik Ladkani 		return err;
593693515d53SJiri Pirko 
593793515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
593893515d53SJiri Pirko 
59390bcf2e4aSVladimir Oltean 	vlan_remove_tag(skb, vlan_tci);
594093515d53SJiri Pirko 
594193515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
594293515d53SJiri Pirko 
594393515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
594493515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
594593515d53SJiri Pirko 
594693515d53SJiri Pirko 	skb_reset_mac_len(skb);
594793515d53SJiri Pirko 
594893515d53SJiri Pirko 	return err;
594993515d53SJiri Pirko }
5950bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
595193515d53SJiri Pirko 
5952b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
5953b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5954b6a79208SShmulik Ladkani  */
595593515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
595693515d53SJiri Pirko {
595793515d53SJiri Pirko 	u16 vlan_tci;
595893515d53SJiri Pirko 	__be16 vlan_proto;
595993515d53SJiri Pirko 	int err;
596093515d53SJiri Pirko 
5961df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
5962b1817524SMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
596393515d53SJiri Pirko 	} else {
5964ecf4ee41SShmulik Ladkani 		if (unlikely(!eth_type_vlan(skb->protocol)))
596593515d53SJiri Pirko 			return 0;
596693515d53SJiri Pirko 
596793515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
596893515d53SJiri Pirko 		if (err)
596993515d53SJiri Pirko 			return err;
597093515d53SJiri Pirko 	}
597193515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
5972ecf4ee41SShmulik Ladkani 	if (likely(!eth_type_vlan(skb->protocol)))
597393515d53SJiri Pirko 		return 0;
597493515d53SJiri Pirko 
597593515d53SJiri Pirko 	vlan_proto = skb->protocol;
597693515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
597793515d53SJiri Pirko 	if (unlikely(err))
597893515d53SJiri Pirko 		return err;
597993515d53SJiri Pirko 
598093515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
598193515d53SJiri Pirko 	return 0;
598293515d53SJiri Pirko }
598393515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
598493515d53SJiri Pirko 
5985b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5986b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5987b6a79208SShmulik Ladkani  */
598893515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
598993515d53SJiri Pirko {
5990df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
5991b6a79208SShmulik Ladkani 		int offset = skb->data - skb_mac_header(skb);
599293515d53SJiri Pirko 		int err;
599393515d53SJiri Pirko 
5994b6a79208SShmulik Ladkani 		if (WARN_ONCE(offset,
5995b6a79208SShmulik Ladkani 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5996b6a79208SShmulik Ladkani 			      offset)) {
5997b6a79208SShmulik Ladkani 			return -EINVAL;
5998b6a79208SShmulik Ladkani 		}
5999b6a79208SShmulik Ladkani 
600093515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
6001df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
6002b6a79208SShmulik Ladkani 		if (err)
600393515d53SJiri Pirko 			return err;
60049241e2dfSDaniel Borkmann 
600593515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
600693515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
600793515d53SJiri Pirko 
60086b83d28aSDaniel Borkmann 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
600993515d53SJiri Pirko 	}
601093515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
601193515d53SJiri Pirko 	return 0;
601293515d53SJiri Pirko }
601393515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
601493515d53SJiri Pirko 
601519fbcb36SGuillaume Nault /**
601619fbcb36SGuillaume Nault  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
601719fbcb36SGuillaume Nault  *
601819fbcb36SGuillaume Nault  * @skb: Socket buffer to modify
601919fbcb36SGuillaume Nault  *
602019fbcb36SGuillaume Nault  * Drop the Ethernet header of @skb.
602119fbcb36SGuillaume Nault  *
602219fbcb36SGuillaume Nault  * Expects that skb->data points to the mac header and that no VLAN tags are
602319fbcb36SGuillaume Nault  * present.
602419fbcb36SGuillaume Nault  *
602519fbcb36SGuillaume Nault  * Returns 0 on success, -errno otherwise.
602619fbcb36SGuillaume Nault  */
602719fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb)
602819fbcb36SGuillaume Nault {
602919fbcb36SGuillaume Nault 	if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
603019fbcb36SGuillaume Nault 	    skb_network_offset(skb) < ETH_HLEN)
603119fbcb36SGuillaume Nault 		return -EPROTO;
603219fbcb36SGuillaume Nault 
603319fbcb36SGuillaume Nault 	skb_pull_rcsum(skb, ETH_HLEN);
603419fbcb36SGuillaume Nault 	skb_reset_mac_header(skb);
603519fbcb36SGuillaume Nault 	skb_reset_mac_len(skb);
603619fbcb36SGuillaume Nault 
603719fbcb36SGuillaume Nault 	return 0;
603819fbcb36SGuillaume Nault }
603919fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop);
604019fbcb36SGuillaume Nault 
604119fbcb36SGuillaume Nault /**
604219fbcb36SGuillaume Nault  * skb_eth_push() - Add a new Ethernet header at the head of a packet
604319fbcb36SGuillaume Nault  *
604419fbcb36SGuillaume Nault  * @skb: Socket buffer to modify
604519fbcb36SGuillaume Nault  * @dst: Destination MAC address of the new header
604619fbcb36SGuillaume Nault  * @src: Source MAC address of the new header
604719fbcb36SGuillaume Nault  *
604819fbcb36SGuillaume Nault  * Prepend @skb with a new Ethernet header.
604919fbcb36SGuillaume Nault  *
605019fbcb36SGuillaume Nault  * Expects that skb->data points to the mac header, which must be empty.
605119fbcb36SGuillaume Nault  *
605219fbcb36SGuillaume Nault  * Returns 0 on success, -errno otherwise.
605319fbcb36SGuillaume Nault  */
605419fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
605519fbcb36SGuillaume Nault 		 const unsigned char *src)
605619fbcb36SGuillaume Nault {
605719fbcb36SGuillaume Nault 	struct ethhdr *eth;
605819fbcb36SGuillaume Nault 	int err;
605919fbcb36SGuillaume Nault 
606019fbcb36SGuillaume Nault 	if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
606119fbcb36SGuillaume Nault 		return -EPROTO;
606219fbcb36SGuillaume Nault 
606319fbcb36SGuillaume Nault 	err = skb_cow_head(skb, sizeof(*eth));
606419fbcb36SGuillaume Nault 	if (err < 0)
606519fbcb36SGuillaume Nault 		return err;
606619fbcb36SGuillaume Nault 
606719fbcb36SGuillaume Nault 	skb_push(skb, sizeof(*eth));
606819fbcb36SGuillaume Nault 	skb_reset_mac_header(skb);
606919fbcb36SGuillaume Nault 	skb_reset_mac_len(skb);
607019fbcb36SGuillaume Nault 
607119fbcb36SGuillaume Nault 	eth = eth_hdr(skb);
607219fbcb36SGuillaume Nault 	ether_addr_copy(eth->h_dest, dst);
607319fbcb36SGuillaume Nault 	ether_addr_copy(eth->h_source, src);
607419fbcb36SGuillaume Nault 	eth->h_proto = skb->protocol;
607519fbcb36SGuillaume Nault 
607619fbcb36SGuillaume Nault 	skb_postpush_rcsum(skb, eth, sizeof(*eth));
607719fbcb36SGuillaume Nault 
607819fbcb36SGuillaume Nault 	return 0;
607919fbcb36SGuillaume Nault }
608019fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push);
608119fbcb36SGuillaume Nault 
60828822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */
60838822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
60848822e270SJohn Hurley 			     __be16 ethertype)
60858822e270SJohn Hurley {
60868822e270SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
60878822e270SJohn Hurley 		__be16 diff[] = { ~hdr->h_proto, ethertype };
60888822e270SJohn Hurley 
60898822e270SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
60908822e270SJohn Hurley 	}
60918822e270SJohn Hurley 
60928822e270SJohn Hurley 	hdr->h_proto = ethertype;
60938822e270SJohn Hurley }
60948822e270SJohn Hurley 
60958822e270SJohn Hurley /**
6096e7dbfed1SMartin Varghese  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
6097e7dbfed1SMartin Varghese  *                   the packet
60988822e270SJohn Hurley  *
60998822e270SJohn Hurley  * @skb: buffer
61008822e270SJohn Hurley  * @mpls_lse: MPLS label stack entry to push
61018822e270SJohn Hurley  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
6102fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
6103e7dbfed1SMartin Varghese  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
6104e7dbfed1SMartin Varghese  *            ethernet
61058822e270SJohn Hurley  *
61068822e270SJohn Hurley  * Expects skb->data at mac header.
61078822e270SJohn Hurley  *
61088822e270SJohn Hurley  * Returns 0 on success, -errno otherwise.
61098822e270SJohn Hurley  */
6110fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
6111d04ac224SMartin Varghese 		  int mac_len, bool ethernet)
61128822e270SJohn Hurley {
61138822e270SJohn Hurley 	struct mpls_shim_hdr *lse;
61148822e270SJohn Hurley 	int err;
61158822e270SJohn Hurley 
61168822e270SJohn Hurley 	if (unlikely(!eth_p_mpls(mpls_proto)))
61178822e270SJohn Hurley 		return -EINVAL;
61188822e270SJohn Hurley 
61198822e270SJohn Hurley 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
61208822e270SJohn Hurley 	if (skb->encapsulation)
61218822e270SJohn Hurley 		return -EINVAL;
61228822e270SJohn Hurley 
61238822e270SJohn Hurley 	err = skb_cow_head(skb, MPLS_HLEN);
61248822e270SJohn Hurley 	if (unlikely(err))
61258822e270SJohn Hurley 		return err;
61268822e270SJohn Hurley 
61278822e270SJohn Hurley 	if (!skb->inner_protocol) {
6128e7dbfed1SMartin Varghese 		skb_set_inner_network_header(skb, skb_network_offset(skb));
61298822e270SJohn Hurley 		skb_set_inner_protocol(skb, skb->protocol);
61308822e270SJohn Hurley 	}
61318822e270SJohn Hurley 
61328822e270SJohn Hurley 	skb_push(skb, MPLS_HLEN);
61338822e270SJohn Hurley 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
6134fa4e0f88SDavide Caratti 		mac_len);
61358822e270SJohn Hurley 	skb_reset_mac_header(skb);
6136fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
6137e7dbfed1SMartin Varghese 	skb_reset_mac_len(skb);
61388822e270SJohn Hurley 
61398822e270SJohn Hurley 	lse = mpls_hdr(skb);
61408822e270SJohn Hurley 	lse->label_stack_entry = mpls_lse;
61418822e270SJohn Hurley 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
61428822e270SJohn Hurley 
61434296adc3SGuillaume Nault 	if (ethernet && mac_len >= ETH_HLEN)
61448822e270SJohn Hurley 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
61458822e270SJohn Hurley 	skb->protocol = mpls_proto;
61468822e270SJohn Hurley 
61478822e270SJohn Hurley 	return 0;
61488822e270SJohn Hurley }
61498822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push);
61508822e270SJohn Hurley 
61512e4e4410SEric Dumazet /**
6152ed246ceeSJohn Hurley  * skb_mpls_pop() - pop the outermost MPLS header
6153ed246ceeSJohn Hurley  *
6154ed246ceeSJohn Hurley  * @skb: buffer
6155ed246ceeSJohn Hurley  * @next_proto: ethertype of header after popped MPLS header
6156fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
615776f99f98SMartin Varghese  * @ethernet: flag to indicate if the packet is ethernet
6158ed246ceeSJohn Hurley  *
6159ed246ceeSJohn Hurley  * Expects skb->data at mac header.
6160ed246ceeSJohn Hurley  *
6161ed246ceeSJohn Hurley  * Returns 0 on success, -errno otherwise.
6162ed246ceeSJohn Hurley  */
6163040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
6164040b5cfbSMartin Varghese 		 bool ethernet)
6165ed246ceeSJohn Hurley {
6166ed246ceeSJohn Hurley 	int err;
6167ed246ceeSJohn Hurley 
6168ed246ceeSJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
6169dedc5a08SDavide Caratti 		return 0;
6170ed246ceeSJohn Hurley 
6171fa4e0f88SDavide Caratti 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
6172ed246ceeSJohn Hurley 	if (unlikely(err))
6173ed246ceeSJohn Hurley 		return err;
6174ed246ceeSJohn Hurley 
6175ed246ceeSJohn Hurley 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
6176ed246ceeSJohn Hurley 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
6177fa4e0f88SDavide Caratti 		mac_len);
6178ed246ceeSJohn Hurley 
6179ed246ceeSJohn Hurley 	__skb_pull(skb, MPLS_HLEN);
6180ed246ceeSJohn Hurley 	skb_reset_mac_header(skb);
6181fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
6182ed246ceeSJohn Hurley 
61834296adc3SGuillaume Nault 	if (ethernet && mac_len >= ETH_HLEN) {
6184ed246ceeSJohn Hurley 		struct ethhdr *hdr;
6185ed246ceeSJohn Hurley 
6186ed246ceeSJohn Hurley 		/* use mpls_hdr() to get ethertype to account for VLANs. */
6187ed246ceeSJohn Hurley 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6188ed246ceeSJohn Hurley 		skb_mod_eth_type(skb, hdr, next_proto);
6189ed246ceeSJohn Hurley 	}
6190ed246ceeSJohn Hurley 	skb->protocol = next_proto;
6191ed246ceeSJohn Hurley 
6192ed246ceeSJohn Hurley 	return 0;
6193ed246ceeSJohn Hurley }
6194ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop);
6195ed246ceeSJohn Hurley 
6196ed246ceeSJohn Hurley /**
6197d27cf5c5SJohn Hurley  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6198d27cf5c5SJohn Hurley  *
6199d27cf5c5SJohn Hurley  * @skb: buffer
6200d27cf5c5SJohn Hurley  * @mpls_lse: new MPLS label stack entry to update to
6201d27cf5c5SJohn Hurley  *
6202d27cf5c5SJohn Hurley  * Expects skb->data at mac header.
6203d27cf5c5SJohn Hurley  *
6204d27cf5c5SJohn Hurley  * Returns 0 on success, -errno otherwise.
6205d27cf5c5SJohn Hurley  */
6206d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6207d27cf5c5SJohn Hurley {
6208d27cf5c5SJohn Hurley 	int err;
6209d27cf5c5SJohn Hurley 
6210d27cf5c5SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
6211d27cf5c5SJohn Hurley 		return -EINVAL;
6212d27cf5c5SJohn Hurley 
6213d27cf5c5SJohn Hurley 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6214d27cf5c5SJohn Hurley 	if (unlikely(err))
6215d27cf5c5SJohn Hurley 		return err;
6216d27cf5c5SJohn Hurley 
6217d27cf5c5SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
6218d27cf5c5SJohn Hurley 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6219d27cf5c5SJohn Hurley 
6220d27cf5c5SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6221d27cf5c5SJohn Hurley 	}
6222d27cf5c5SJohn Hurley 
6223d27cf5c5SJohn Hurley 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
6224d27cf5c5SJohn Hurley 
6225d27cf5c5SJohn Hurley 	return 0;
6226d27cf5c5SJohn Hurley }
6227d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6228d27cf5c5SJohn Hurley 
6229d27cf5c5SJohn Hurley /**
62302a2ea508SJohn Hurley  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
62312a2ea508SJohn Hurley  *
62322a2ea508SJohn Hurley  * @skb: buffer
62332a2ea508SJohn Hurley  *
62342a2ea508SJohn Hurley  * Expects skb->data at mac header.
62352a2ea508SJohn Hurley  *
62362a2ea508SJohn Hurley  * Returns 0 on success, -errno otherwise.
62372a2ea508SJohn Hurley  */
62382a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb)
62392a2ea508SJohn Hurley {
62402a2ea508SJohn Hurley 	u32 lse;
62412a2ea508SJohn Hurley 	u8 ttl;
62422a2ea508SJohn Hurley 
62432a2ea508SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
62442a2ea508SJohn Hurley 		return -EINVAL;
62452a2ea508SJohn Hurley 
624613de4ed9SDavide Caratti 	if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
624713de4ed9SDavide Caratti 		return -ENOMEM;
624813de4ed9SDavide Caratti 
62492a2ea508SJohn Hurley 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
62502a2ea508SJohn Hurley 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
62512a2ea508SJohn Hurley 	if (!--ttl)
62522a2ea508SJohn Hurley 		return -EINVAL;
62532a2ea508SJohn Hurley 
62542a2ea508SJohn Hurley 	lse &= ~MPLS_LS_TTL_MASK;
62552a2ea508SJohn Hurley 	lse |= ttl << MPLS_LS_TTL_SHIFT;
62562a2ea508SJohn Hurley 
62572a2ea508SJohn Hurley 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
62582a2ea508SJohn Hurley }
62592a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
62602a2ea508SJohn Hurley 
62612a2ea508SJohn Hurley /**
62622e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
62632e4e4410SEric Dumazet  *
6264de3f0d0eSMasanari Iida  * @header_len: size of linear part
6265de3f0d0eSMasanari Iida  * @data_len: needed length in frags
626609c2c907SEric Dumazet  * @order: max page order desired.
6267de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
6268de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
62692e4e4410SEric Dumazet  *
62702e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
62712e4e4410SEric Dumazet  */
62722e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
62732e4e4410SEric Dumazet 				     unsigned long data_len,
627409c2c907SEric Dumazet 				     int order,
62752e4e4410SEric Dumazet 				     int *errcode,
62762e4e4410SEric Dumazet 				     gfp_t gfp_mask)
62772e4e4410SEric Dumazet {
62782e4e4410SEric Dumazet 	unsigned long chunk;
62792e4e4410SEric Dumazet 	struct sk_buff *skb;
62802e4e4410SEric Dumazet 	struct page *page;
628109c2c907SEric Dumazet 	int nr_frags = 0;
62822e4e4410SEric Dumazet 
62832e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
628409c2c907SEric Dumazet 	if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order)))
62852e4e4410SEric Dumazet 		return NULL;
62862e4e4410SEric Dumazet 
62872e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
6288f8c468e8SDavid Rientjes 	skb = alloc_skb(header_len, gfp_mask);
62892e4e4410SEric Dumazet 	if (!skb)
62902e4e4410SEric Dumazet 		return NULL;
62912e4e4410SEric Dumazet 
629209c2c907SEric Dumazet 	while (data_len) {
629309c2c907SEric Dumazet 		if (nr_frags == MAX_SKB_FRAGS - 1)
629409c2c907SEric Dumazet 			goto failure;
629509c2c907SEric Dumazet 		while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
629609c2c907SEric Dumazet 			order--;
62972e4e4410SEric Dumazet 
629809c2c907SEric Dumazet 		if (order) {
6299d0164adcSMel Gorman 			page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
63002e4e4410SEric Dumazet 					   __GFP_COMP |
6301d14b56f5SMichal Hocko 					   __GFP_NOWARN,
63022e4e4410SEric Dumazet 					   order);
630309c2c907SEric Dumazet 			if (!page) {
63042e4e4410SEric Dumazet 				order--;
630509c2c907SEric Dumazet 				continue;
63062e4e4410SEric Dumazet 			}
630709c2c907SEric Dumazet 		} else {
63082e4e4410SEric Dumazet 			page = alloc_page(gfp_mask);
63092e4e4410SEric Dumazet 			if (!page)
63102e4e4410SEric Dumazet 				goto failure;
631109c2c907SEric Dumazet 		}
63122e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
63132e4e4410SEric Dumazet 			      PAGE_SIZE << order);
631409c2c907SEric Dumazet 		skb_fill_page_desc(skb, nr_frags, page, 0, chunk);
631509c2c907SEric Dumazet 		nr_frags++;
631609c2c907SEric Dumazet 		skb->truesize += (PAGE_SIZE << order);
63172e4e4410SEric Dumazet 		data_len -= chunk;
63182e4e4410SEric Dumazet 	}
63192e4e4410SEric Dumazet 	return skb;
63202e4e4410SEric Dumazet 
63212e4e4410SEric Dumazet failure:
63222e4e4410SEric Dumazet 	kfree_skb(skb);
63232e4e4410SEric Dumazet 	return NULL;
63242e4e4410SEric Dumazet }
63252e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
63266fa01ccdSSowmini Varadhan 
63276fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
63286fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
63296fa01ccdSSowmini Varadhan 				    const int headlen, gfp_t gfp_mask)
63306fa01ccdSSowmini Varadhan {
63316fa01ccdSSowmini Varadhan 	int i;
633212d6c1d3SKees Cook 	unsigned int size = skb_end_offset(skb);
63336fa01ccdSSowmini Varadhan 	int new_hlen = headlen - off;
63346fa01ccdSSowmini Varadhan 	u8 *data;
63356fa01ccdSSowmini Varadhan 
63366fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
63376fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
633812d6c1d3SKees Cook 
63395c0e820cSEric Dumazet 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
63406fa01ccdSSowmini Varadhan 	if (!data)
63416fa01ccdSSowmini Varadhan 		return -ENOMEM;
634212d6c1d3SKees Cook 	size = SKB_WITH_OVERHEAD(size);
63436fa01ccdSSowmini Varadhan 
63446fa01ccdSSowmini Varadhan 	/* Copy real data, and all frags */
63456fa01ccdSSowmini Varadhan 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
63466fa01ccdSSowmini Varadhan 	skb->len -= off;
63476fa01ccdSSowmini Varadhan 
63486fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
63496fa01ccdSSowmini Varadhan 	       skb_shinfo(skb),
63506fa01ccdSSowmini Varadhan 	       offsetof(struct skb_shared_info,
63516fa01ccdSSowmini Varadhan 			frags[skb_shinfo(skb)->nr_frags]));
63526fa01ccdSSowmini Varadhan 	if (skb_cloned(skb)) {
63536fa01ccdSSowmini Varadhan 		/* drop the old head gracefully */
63546fa01ccdSSowmini Varadhan 		if (skb_orphan_frags(skb, gfp_mask)) {
6355bf9f1baaSEric Dumazet 			skb_kfree_head(data, size);
63566fa01ccdSSowmini Varadhan 			return -ENOMEM;
63576fa01ccdSSowmini Varadhan 		}
63586fa01ccdSSowmini Varadhan 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
63596fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
63606fa01ccdSSowmini Varadhan 		if (skb_has_frag_list(skb))
63616fa01ccdSSowmini Varadhan 			skb_clone_fraglist(skb);
6362b07a2d97SJakub Kicinski 		skb_release_data(skb, SKB_CONSUMED, false);
63636fa01ccdSSowmini Varadhan 	} else {
63646fa01ccdSSowmini Varadhan 		/* we can reuse existing recount- all we did was
63656fa01ccdSSowmini Varadhan 		 * relocate values
63666fa01ccdSSowmini Varadhan 		 */
6367b07a2d97SJakub Kicinski 		skb_free_head(skb, false);
63686fa01ccdSSowmini Varadhan 	}
63696fa01ccdSSowmini Varadhan 
63706fa01ccdSSowmini Varadhan 	skb->head = data;
63716fa01ccdSSowmini Varadhan 	skb->data = data;
63726fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
6373763087daSEric Dumazet 	skb_set_end_offset(skb, size);
63746fa01ccdSSowmini Varadhan 	skb_set_tail_pointer(skb, skb_headlen(skb));
63756fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
63766fa01ccdSSowmini Varadhan 	skb->cloned = 0;
63776fa01ccdSSowmini Varadhan 	skb->hdr_len = 0;
63786fa01ccdSSowmini Varadhan 	skb->nohdr = 0;
63796fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
63806fa01ccdSSowmini Varadhan 
63816fa01ccdSSowmini Varadhan 	return 0;
63826fa01ccdSSowmini Varadhan }
63836fa01ccdSSowmini Varadhan 
63846fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
63856fa01ccdSSowmini Varadhan 
63866fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
63876fa01ccdSSowmini Varadhan  * pskb_carve()
63886fa01ccdSSowmini Varadhan  */
63896fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
63906fa01ccdSSowmini Varadhan 				struct skb_shared_info *shinfo, int eat,
63916fa01ccdSSowmini Varadhan 				gfp_t gfp_mask)
63926fa01ccdSSowmini Varadhan {
63936fa01ccdSSowmini Varadhan 	struct sk_buff *list = shinfo->frag_list;
63946fa01ccdSSowmini Varadhan 	struct sk_buff *clone = NULL;
63956fa01ccdSSowmini Varadhan 	struct sk_buff *insp = NULL;
63966fa01ccdSSowmini Varadhan 
63976fa01ccdSSowmini Varadhan 	do {
63986fa01ccdSSowmini Varadhan 		if (!list) {
63996fa01ccdSSowmini Varadhan 			pr_err("Not enough bytes to eat. Want %d\n", eat);
64006fa01ccdSSowmini Varadhan 			return -EFAULT;
64016fa01ccdSSowmini Varadhan 		}
64026fa01ccdSSowmini Varadhan 		if (list->len <= eat) {
64036fa01ccdSSowmini Varadhan 			/* Eaten as whole. */
64046fa01ccdSSowmini Varadhan 			eat -= list->len;
64056fa01ccdSSowmini Varadhan 			list = list->next;
64066fa01ccdSSowmini Varadhan 			insp = list;
64076fa01ccdSSowmini Varadhan 		} else {
64086fa01ccdSSowmini Varadhan 			/* Eaten partially. */
64096fa01ccdSSowmini Varadhan 			if (skb_shared(list)) {
64106fa01ccdSSowmini Varadhan 				clone = skb_clone(list, gfp_mask);
64116fa01ccdSSowmini Varadhan 				if (!clone)
64126fa01ccdSSowmini Varadhan 					return -ENOMEM;
64136fa01ccdSSowmini Varadhan 				insp = list->next;
64146fa01ccdSSowmini Varadhan 				list = clone;
64156fa01ccdSSowmini Varadhan 			} else {
64166fa01ccdSSowmini Varadhan 				/* This may be pulled without problems. */
64176fa01ccdSSowmini Varadhan 				insp = list;
64186fa01ccdSSowmini Varadhan 			}
64196fa01ccdSSowmini Varadhan 			if (pskb_carve(list, eat, gfp_mask) < 0) {
64206fa01ccdSSowmini Varadhan 				kfree_skb(clone);
64216fa01ccdSSowmini Varadhan 				return -ENOMEM;
64226fa01ccdSSowmini Varadhan 			}
64236fa01ccdSSowmini Varadhan 			break;
64246fa01ccdSSowmini Varadhan 		}
64256fa01ccdSSowmini Varadhan 	} while (eat);
64266fa01ccdSSowmini Varadhan 
64276fa01ccdSSowmini Varadhan 	/* Free pulled out fragments. */
64286fa01ccdSSowmini Varadhan 	while ((list = shinfo->frag_list) != insp) {
64296fa01ccdSSowmini Varadhan 		shinfo->frag_list = list->next;
6430ef527f96SEric Dumazet 		consume_skb(list);
64316fa01ccdSSowmini Varadhan 	}
64326fa01ccdSSowmini Varadhan 	/* And insert new clone at head. */
64336fa01ccdSSowmini Varadhan 	if (clone) {
64346fa01ccdSSowmini Varadhan 		clone->next = list;
64356fa01ccdSSowmini Varadhan 		shinfo->frag_list = clone;
64366fa01ccdSSowmini Varadhan 	}
64376fa01ccdSSowmini Varadhan 	return 0;
64386fa01ccdSSowmini Varadhan }
64396fa01ccdSSowmini Varadhan 
64406fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
64416fa01ccdSSowmini Varadhan  * non-linear part of skb
64426fa01ccdSSowmini Varadhan  */
64436fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
64446fa01ccdSSowmini Varadhan 				       int pos, gfp_t gfp_mask)
64456fa01ccdSSowmini Varadhan {
64466fa01ccdSSowmini Varadhan 	int i, k = 0;
644712d6c1d3SKees Cook 	unsigned int size = skb_end_offset(skb);
64486fa01ccdSSowmini Varadhan 	u8 *data;
64496fa01ccdSSowmini Varadhan 	const int nfrags = skb_shinfo(skb)->nr_frags;
64506fa01ccdSSowmini Varadhan 	struct skb_shared_info *shinfo;
64516fa01ccdSSowmini Varadhan 
64526fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
64536fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
645412d6c1d3SKees Cook 
64555c0e820cSEric Dumazet 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
64566fa01ccdSSowmini Varadhan 	if (!data)
64576fa01ccdSSowmini Varadhan 		return -ENOMEM;
645812d6c1d3SKees Cook 	size = SKB_WITH_OVERHEAD(size);
64596fa01ccdSSowmini Varadhan 
64606fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
6461e3ec1e8cSMiaohe Lin 	       skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
64626fa01ccdSSowmini Varadhan 	if (skb_orphan_frags(skb, gfp_mask)) {
6463bf9f1baaSEric Dumazet 		skb_kfree_head(data, size);
64646fa01ccdSSowmini Varadhan 		return -ENOMEM;
64656fa01ccdSSowmini Varadhan 	}
64666fa01ccdSSowmini Varadhan 	shinfo = (struct skb_shared_info *)(data + size);
64676fa01ccdSSowmini Varadhan 	for (i = 0; i < nfrags; i++) {
64686fa01ccdSSowmini Varadhan 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
64696fa01ccdSSowmini Varadhan 
64706fa01ccdSSowmini Varadhan 		if (pos + fsize > off) {
64716fa01ccdSSowmini Varadhan 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
64726fa01ccdSSowmini Varadhan 
64736fa01ccdSSowmini Varadhan 			if (pos < off) {
64746fa01ccdSSowmini Varadhan 				/* Split frag.
64756fa01ccdSSowmini Varadhan 				 * We have two variants in this case:
64766fa01ccdSSowmini Varadhan 				 * 1. Move all the frag to the second
64776fa01ccdSSowmini Varadhan 				 *    part, if it is possible. F.e.
64786fa01ccdSSowmini Varadhan 				 *    this approach is mandatory for TUX,
64796fa01ccdSSowmini Varadhan 				 *    where splitting is expensive.
64806fa01ccdSSowmini Varadhan 				 * 2. Split is accurately. We make this.
64816fa01ccdSSowmini Varadhan 				 */
6482b54c9d5bSJonathan Lemon 				skb_frag_off_add(&shinfo->frags[0], off - pos);
64836fa01ccdSSowmini Varadhan 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
64846fa01ccdSSowmini Varadhan 			}
64856fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
64866fa01ccdSSowmini Varadhan 			k++;
64876fa01ccdSSowmini Varadhan 		}
64886fa01ccdSSowmini Varadhan 		pos += fsize;
64896fa01ccdSSowmini Varadhan 	}
64906fa01ccdSSowmini Varadhan 	shinfo->nr_frags = k;
64916fa01ccdSSowmini Varadhan 	if (skb_has_frag_list(skb))
64926fa01ccdSSowmini Varadhan 		skb_clone_fraglist(skb);
64936fa01ccdSSowmini Varadhan 
64946fa01ccdSSowmini Varadhan 	/* split line is in frag list */
6495eabe8618SMiaohe Lin 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6496eabe8618SMiaohe Lin 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6497eabe8618SMiaohe Lin 		if (skb_has_frag_list(skb))
6498eabe8618SMiaohe Lin 			kfree_skb_list(skb_shinfo(skb)->frag_list);
6499bf9f1baaSEric Dumazet 		skb_kfree_head(data, size);
6500eabe8618SMiaohe Lin 		return -ENOMEM;
65016fa01ccdSSowmini Varadhan 	}
6502b07a2d97SJakub Kicinski 	skb_release_data(skb, SKB_CONSUMED, false);
65036fa01ccdSSowmini Varadhan 
65046fa01ccdSSowmini Varadhan 	skb->head = data;
65056fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
65066fa01ccdSSowmini Varadhan 	skb->data = data;
6507763087daSEric Dumazet 	skb_set_end_offset(skb, size);
65086fa01ccdSSowmini Varadhan 	skb_reset_tail_pointer(skb);
65096fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
65106fa01ccdSSowmini Varadhan 	skb->cloned   = 0;
65116fa01ccdSSowmini Varadhan 	skb->hdr_len  = 0;
65126fa01ccdSSowmini Varadhan 	skb->nohdr    = 0;
65136fa01ccdSSowmini Varadhan 	skb->len -= off;
65146fa01ccdSSowmini Varadhan 	skb->data_len = skb->len;
65156fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
65166fa01ccdSSowmini Varadhan 	return 0;
65176fa01ccdSSowmini Varadhan }
65186fa01ccdSSowmini Varadhan 
65196fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
65206fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
65216fa01ccdSSowmini Varadhan {
65226fa01ccdSSowmini Varadhan 	int headlen = skb_headlen(skb);
65236fa01ccdSSowmini Varadhan 
65246fa01ccdSSowmini Varadhan 	if (len < headlen)
65256fa01ccdSSowmini Varadhan 		return pskb_carve_inside_header(skb, len, headlen, gfp);
65266fa01ccdSSowmini Varadhan 	else
65276fa01ccdSSowmini Varadhan 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
65286fa01ccdSSowmini Varadhan }
65296fa01ccdSSowmini Varadhan 
65306fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
65316fa01ccdSSowmini Varadhan  * a new skb
65326fa01ccdSSowmini Varadhan  */
65336fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
65346fa01ccdSSowmini Varadhan 			     int to_copy, gfp_t gfp)
65356fa01ccdSSowmini Varadhan {
65366fa01ccdSSowmini Varadhan 	struct sk_buff  *clone = skb_clone(skb, gfp);
65376fa01ccdSSowmini Varadhan 
65386fa01ccdSSowmini Varadhan 	if (!clone)
65396fa01ccdSSowmini Varadhan 		return NULL;
65406fa01ccdSSowmini Varadhan 
65416fa01ccdSSowmini Varadhan 	if (pskb_carve(clone, off, gfp) < 0 ||
65426fa01ccdSSowmini Varadhan 	    pskb_trim(clone, to_copy)) {
65436fa01ccdSSowmini Varadhan 		kfree_skb(clone);
65446fa01ccdSSowmini Varadhan 		return NULL;
65456fa01ccdSSowmini Varadhan 	}
65466fa01ccdSSowmini Varadhan 	return clone;
65476fa01ccdSSowmini Varadhan }
65486fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
6549c8c8b127SEric Dumazet 
6550c8c8b127SEric Dumazet /**
6551c8c8b127SEric Dumazet  * skb_condense - try to get rid of fragments/frag_list if possible
6552c8c8b127SEric Dumazet  * @skb: buffer
6553c8c8b127SEric Dumazet  *
6554c8c8b127SEric Dumazet  * Can be used to save memory before skb is added to a busy queue.
6555c8c8b127SEric Dumazet  * If packet has bytes in frags and enough tail room in skb->head,
6556c8c8b127SEric Dumazet  * pull all of them, so that we can free the frags right now and adjust
6557c8c8b127SEric Dumazet  * truesize.
6558c8c8b127SEric Dumazet  * Notes:
6559c8c8b127SEric Dumazet  *	We do not reallocate skb->head thus can not fail.
6560c8c8b127SEric Dumazet  *	Caller must re-evaluate skb->truesize if needed.
6561c8c8b127SEric Dumazet  */
6562c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
6563c8c8b127SEric Dumazet {
65643174fed9SEric Dumazet 	if (skb->data_len) {
65653174fed9SEric Dumazet 		if (skb->data_len > skb->end - skb->tail ||
6566c8c8b127SEric Dumazet 		    skb_cloned(skb))
6567c8c8b127SEric Dumazet 			return;
6568c8c8b127SEric Dumazet 
6569c8c8b127SEric Dumazet 		/* Nice, we can free page frag(s) right now */
6570c8c8b127SEric Dumazet 		__pskb_pull_tail(skb, skb->data_len);
65713174fed9SEric Dumazet 	}
65723174fed9SEric Dumazet 	/* At this point, skb->truesize might be over estimated,
65733174fed9SEric Dumazet 	 * because skb had a fragment, and fragments do not tell
65743174fed9SEric Dumazet 	 * their truesize.
65753174fed9SEric Dumazet 	 * When we pulled its content into skb->head, fragment
65763174fed9SEric Dumazet 	 * was freed, but __pskb_pull_tail() could not possibly
65773174fed9SEric Dumazet 	 * adjust skb->truesize, not knowing the frag truesize.
6578c8c8b127SEric Dumazet 	 */
6579c8c8b127SEric Dumazet 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6580c8c8b127SEric Dumazet }
6581d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense);
6582df5042f4SFlorian Westphal 
6583df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
6584df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6585df5042f4SFlorian Westphal {
6586df5042f4SFlorian Westphal 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6587df5042f4SFlorian Westphal }
6588df5042f4SFlorian Westphal 
65898b69a803SPaolo Abeni /**
65908b69a803SPaolo Abeni  * __skb_ext_alloc - allocate a new skb extensions storage
65918b69a803SPaolo Abeni  *
65924930f483SFlorian Westphal  * @flags: See kmalloc().
65934930f483SFlorian Westphal  *
65948b69a803SPaolo Abeni  * Returns the newly allocated pointer. The pointer can later attached to a
65958b69a803SPaolo Abeni  * skb via __skb_ext_set().
65968b69a803SPaolo Abeni  * Note: caller must handle the skb_ext as an opaque data.
65978b69a803SPaolo Abeni  */
65984930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags)
6599df5042f4SFlorian Westphal {
66004930f483SFlorian Westphal 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6601df5042f4SFlorian Westphal 
6602df5042f4SFlorian Westphal 	if (new) {
6603df5042f4SFlorian Westphal 		memset(new->offset, 0, sizeof(new->offset));
6604df5042f4SFlorian Westphal 		refcount_set(&new->refcnt, 1);
6605df5042f4SFlorian Westphal 	}
6606df5042f4SFlorian Westphal 
6607df5042f4SFlorian Westphal 	return new;
6608df5042f4SFlorian Westphal }
6609df5042f4SFlorian Westphal 
66104165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
66114165079bSFlorian Westphal 					 unsigned int old_active)
6612df5042f4SFlorian Westphal {
6613df5042f4SFlorian Westphal 	struct skb_ext *new;
6614df5042f4SFlorian Westphal 
6615df5042f4SFlorian Westphal 	if (refcount_read(&old->refcnt) == 1)
6616df5042f4SFlorian Westphal 		return old;
6617df5042f4SFlorian Westphal 
6618df5042f4SFlorian Westphal 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6619df5042f4SFlorian Westphal 	if (!new)
6620df5042f4SFlorian Westphal 		return NULL;
6621df5042f4SFlorian Westphal 
6622df5042f4SFlorian Westphal 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6623df5042f4SFlorian Westphal 	refcount_set(&new->refcnt, 1);
6624df5042f4SFlorian Westphal 
66254165079bSFlorian Westphal #ifdef CONFIG_XFRM
66264165079bSFlorian Westphal 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
66274165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
66284165079bSFlorian Westphal 		unsigned int i;
66294165079bSFlorian Westphal 
66304165079bSFlorian Westphal 		for (i = 0; i < sp->len; i++)
66314165079bSFlorian Westphal 			xfrm_state_hold(sp->xvec[i]);
66324165079bSFlorian Westphal 	}
66334165079bSFlorian Westphal #endif
6634df5042f4SFlorian Westphal 	__skb_ext_put(old);
6635df5042f4SFlorian Westphal 	return new;
6636df5042f4SFlorian Westphal }
6637df5042f4SFlorian Westphal 
6638df5042f4SFlorian Westphal /**
66398b69a803SPaolo Abeni  * __skb_ext_set - attach the specified extension storage to this skb
66408b69a803SPaolo Abeni  * @skb: buffer
66418b69a803SPaolo Abeni  * @id: extension id
66428b69a803SPaolo Abeni  * @ext: extension storage previously allocated via __skb_ext_alloc()
66438b69a803SPaolo Abeni  *
66448b69a803SPaolo Abeni  * Existing extensions, if any, are cleared.
66458b69a803SPaolo Abeni  *
66468b69a803SPaolo Abeni  * Returns the pointer to the extension.
66478b69a803SPaolo Abeni  */
66488b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
66498b69a803SPaolo Abeni 		    struct skb_ext *ext)
66508b69a803SPaolo Abeni {
66518b69a803SPaolo Abeni 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
66528b69a803SPaolo Abeni 
66538b69a803SPaolo Abeni 	skb_ext_put(skb);
66548b69a803SPaolo Abeni 	newlen = newoff + skb_ext_type_len[id];
66558b69a803SPaolo Abeni 	ext->chunks = newlen;
66568b69a803SPaolo Abeni 	ext->offset[id] = newoff;
66578b69a803SPaolo Abeni 	skb->extensions = ext;
66588b69a803SPaolo Abeni 	skb->active_extensions = 1 << id;
66598b69a803SPaolo Abeni 	return skb_ext_get_ptr(ext, id);
66608b69a803SPaolo Abeni }
66618b69a803SPaolo Abeni 
66628b69a803SPaolo Abeni /**
6663df5042f4SFlorian Westphal  * skb_ext_add - allocate space for given extension, COW if needed
6664df5042f4SFlorian Westphal  * @skb: buffer
6665df5042f4SFlorian Westphal  * @id: extension to allocate space for
6666df5042f4SFlorian Westphal  *
6667df5042f4SFlorian Westphal  * Allocates enough space for the given extension.
6668df5042f4SFlorian Westphal  * If the extension is already present, a pointer to that extension
6669df5042f4SFlorian Westphal  * is returned.
6670df5042f4SFlorian Westphal  *
6671df5042f4SFlorian Westphal  * If the skb was cloned, COW applies and the returned memory can be
6672df5042f4SFlorian Westphal  * modified without changing the extension space of clones buffers.
6673df5042f4SFlorian Westphal  *
6674df5042f4SFlorian Westphal  * Returns pointer to the extension or NULL on allocation failure.
6675df5042f4SFlorian Westphal  */
6676df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6677df5042f4SFlorian Westphal {
6678df5042f4SFlorian Westphal 	struct skb_ext *new, *old = NULL;
6679df5042f4SFlorian Westphal 	unsigned int newlen, newoff;
6680df5042f4SFlorian Westphal 
6681df5042f4SFlorian Westphal 	if (skb->active_extensions) {
6682df5042f4SFlorian Westphal 		old = skb->extensions;
6683df5042f4SFlorian Westphal 
66844165079bSFlorian Westphal 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6685df5042f4SFlorian Westphal 		if (!new)
6686df5042f4SFlorian Westphal 			return NULL;
6687df5042f4SFlorian Westphal 
6688682ec859SPaolo Abeni 		if (__skb_ext_exist(new, id))
6689df5042f4SFlorian Westphal 			goto set_active;
6690df5042f4SFlorian Westphal 
6691e94e50bdSPaolo Abeni 		newoff = new->chunks;
6692df5042f4SFlorian Westphal 	} else {
6693df5042f4SFlorian Westphal 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6694df5042f4SFlorian Westphal 
66954930f483SFlorian Westphal 		new = __skb_ext_alloc(GFP_ATOMIC);
6696df5042f4SFlorian Westphal 		if (!new)
6697df5042f4SFlorian Westphal 			return NULL;
6698df5042f4SFlorian Westphal 	}
6699df5042f4SFlorian Westphal 
6700df5042f4SFlorian Westphal 	newlen = newoff + skb_ext_type_len[id];
6701df5042f4SFlorian Westphal 	new->chunks = newlen;
6702df5042f4SFlorian Westphal 	new->offset[id] = newoff;
6703df5042f4SFlorian Westphal set_active:
6704b0999f38SPaolo Abeni 	skb->slow_gro = 1;
6705682ec859SPaolo Abeni 	skb->extensions = new;
6706df5042f4SFlorian Westphal 	skb->active_extensions |= 1 << id;
6707df5042f4SFlorian Westphal 	return skb_ext_get_ptr(new, id);
6708df5042f4SFlorian Westphal }
6709df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add);
6710df5042f4SFlorian Westphal 
67114165079bSFlorian Westphal #ifdef CONFIG_XFRM
67124165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp)
67134165079bSFlorian Westphal {
67144165079bSFlorian Westphal 	unsigned int i;
67154165079bSFlorian Westphal 
67164165079bSFlorian Westphal 	for (i = 0; i < sp->len; i++)
67174165079bSFlorian Westphal 		xfrm_state_put(sp->xvec[i]);
67184165079bSFlorian Westphal }
67194165079bSFlorian Westphal #endif
67204165079bSFlorian Westphal 
672178476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS
672278476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow)
672378476d31SJeremy Kerr {
672478476d31SJeremy Kerr 	if (flow->key)
672578476d31SJeremy Kerr 		mctp_key_unref(flow->key);
672678476d31SJeremy Kerr }
672778476d31SJeremy Kerr #endif
672878476d31SJeremy Kerr 
6729df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6730df5042f4SFlorian Westphal {
6731df5042f4SFlorian Westphal 	struct skb_ext *ext = skb->extensions;
6732df5042f4SFlorian Westphal 
6733df5042f4SFlorian Westphal 	skb->active_extensions &= ~(1 << id);
6734df5042f4SFlorian Westphal 	if (skb->active_extensions == 0) {
6735df5042f4SFlorian Westphal 		skb->extensions = NULL;
6736df5042f4SFlorian Westphal 		__skb_ext_put(ext);
67374165079bSFlorian Westphal #ifdef CONFIG_XFRM
67384165079bSFlorian Westphal 	} else if (id == SKB_EXT_SEC_PATH &&
67394165079bSFlorian Westphal 		   refcount_read(&ext->refcnt) == 1) {
67404165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
67414165079bSFlorian Westphal 
67424165079bSFlorian Westphal 		skb_ext_put_sp(sp);
67434165079bSFlorian Westphal 		sp->len = 0;
67444165079bSFlorian Westphal #endif
6745df5042f4SFlorian Westphal 	}
6746df5042f4SFlorian Westphal }
6747df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del);
6748df5042f4SFlorian Westphal 
6749df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext)
6750df5042f4SFlorian Westphal {
6751df5042f4SFlorian Westphal 	/* If this is last clone, nothing can increment
6752df5042f4SFlorian Westphal 	 * it after check passes.  Avoids one atomic op.
6753df5042f4SFlorian Westphal 	 */
6754df5042f4SFlorian Westphal 	if (refcount_read(&ext->refcnt) == 1)
6755df5042f4SFlorian Westphal 		goto free_now;
6756df5042f4SFlorian Westphal 
6757df5042f4SFlorian Westphal 	if (!refcount_dec_and_test(&ext->refcnt))
6758df5042f4SFlorian Westphal 		return;
6759df5042f4SFlorian Westphal free_now:
67604165079bSFlorian Westphal #ifdef CONFIG_XFRM
67614165079bSFlorian Westphal 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
67624165079bSFlorian Westphal 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
67634165079bSFlorian Westphal #endif
676478476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS
676578476d31SJeremy Kerr 	if (__skb_ext_exist(ext, SKB_EXT_MCTP))
676678476d31SJeremy Kerr 		skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
676778476d31SJeremy Kerr #endif
67684165079bSFlorian Westphal 
6769df5042f4SFlorian Westphal 	kmem_cache_free(skbuff_ext_cache, ext);
6770df5042f4SFlorian Westphal }
6771df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put);
6772df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */
677368822bdfSEric Dumazet 
677468822bdfSEric Dumazet /**
677568822bdfSEric Dumazet  * skb_attempt_defer_free - queue skb for remote freeing
677668822bdfSEric Dumazet  * @skb: buffer
677768822bdfSEric Dumazet  *
677868822bdfSEric Dumazet  * Put @skb in a per-cpu list, using the cpu which
677968822bdfSEric Dumazet  * allocated the skb/pages to reduce false sharing
678068822bdfSEric Dumazet  * and memory zone spinlock contention.
678168822bdfSEric Dumazet  */
678268822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb)
678368822bdfSEric Dumazet {
678468822bdfSEric Dumazet 	int cpu = skb->alloc_cpu;
678568822bdfSEric Dumazet 	struct softnet_data *sd;
678639564c3fSEric Dumazet 	unsigned int defer_max;
678768822bdfSEric Dumazet 	bool kick;
678868822bdfSEric Dumazet 
678968822bdfSEric Dumazet 	if (WARN_ON_ONCE(cpu >= nr_cpu_ids) ||
679068822bdfSEric Dumazet 	    !cpu_online(cpu) ||
679168822bdfSEric Dumazet 	    cpu == raw_smp_processor_id()) {
679239564c3fSEric Dumazet nodefer:	__kfree_skb(skb);
679368822bdfSEric Dumazet 		return;
679468822bdfSEric Dumazet 	}
679568822bdfSEric Dumazet 
6796e8e1ce84SEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
6797e8e1ce84SEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(skb->destructor);
6798e8e1ce84SEric Dumazet 
679968822bdfSEric Dumazet 	sd = &per_cpu(softnet_data, cpu);
680039564c3fSEric Dumazet 	defer_max = READ_ONCE(sysctl_skb_defer_max);
680139564c3fSEric Dumazet 	if (READ_ONCE(sd->defer_count) >= defer_max)
680239564c3fSEric Dumazet 		goto nodefer;
680339564c3fSEric Dumazet 
6804931e93bdSEric Dumazet 	spin_lock_bh(&sd->defer_lock);
6805c09b0cd2SJakub Kicinski 	/* Send an IPI every time queue reaches half capacity. */
6806c09b0cd2SJakub Kicinski 	kick = sd->defer_count == (defer_max >> 1);
6807c09b0cd2SJakub Kicinski 	/* Paired with the READ_ONCE() few lines above */
6808c09b0cd2SJakub Kicinski 	WRITE_ONCE(sd->defer_count, sd->defer_count + 1);
6809c09b0cd2SJakub Kicinski 
681068822bdfSEric Dumazet 	skb->next = sd->defer_list;
681168822bdfSEric Dumazet 	/* Paired with READ_ONCE() in skb_defer_free_flush() */
681268822bdfSEric Dumazet 	WRITE_ONCE(sd->defer_list, skb);
6813931e93bdSEric Dumazet 	spin_unlock_bh(&sd->defer_lock);
681468822bdfSEric Dumazet 
681568822bdfSEric Dumazet 	/* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU
681668822bdfSEric Dumazet 	 * if we are unlucky enough (this seems very unlikely).
681768822bdfSEric Dumazet 	 */
681897e719a8SEric Dumazet 	if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1))
681968822bdfSEric Dumazet 		smp_call_function_single_async(cpu, &sd->defer_csd);
682068822bdfSEric Dumazet }
68212e910b95SDavid Howells 
68222e910b95SDavid Howells static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
68232e910b95SDavid Howells 				 size_t offset, size_t len)
68242e910b95SDavid Howells {
68252e910b95SDavid Howells 	const char *kaddr;
68262e910b95SDavid Howells 	__wsum csum;
68272e910b95SDavid Howells 
68282e910b95SDavid Howells 	kaddr = kmap_local_page(page);
68292e910b95SDavid Howells 	csum = csum_partial(kaddr + offset, len, 0);
68302e910b95SDavid Howells 	kunmap_local(kaddr);
68312e910b95SDavid Howells 	skb->csum = csum_block_add(skb->csum, csum, skb->len);
68322e910b95SDavid Howells }
68332e910b95SDavid Howells 
68342e910b95SDavid Howells /**
68352e910b95SDavid Howells  * skb_splice_from_iter - Splice (or copy) pages to skbuff
68362e910b95SDavid Howells  * @skb: The buffer to add pages to
68372e910b95SDavid Howells  * @iter: Iterator representing the pages to be added
68382e910b95SDavid Howells  * @maxsize: Maximum amount of pages to be added
68392e910b95SDavid Howells  * @gfp: Allocation flags
68402e910b95SDavid Howells  *
68412e910b95SDavid Howells  * This is a common helper function for supporting MSG_SPLICE_PAGES.  It
68422e910b95SDavid Howells  * extracts pages from an iterator and adds them to the socket buffer if
68432e910b95SDavid Howells  * possible, copying them to fragments if not possible (such as if they're slab
68442e910b95SDavid Howells  * pages).
68452e910b95SDavid Howells  *
68462e910b95SDavid Howells  * Returns the amount of data spliced/copied or -EMSGSIZE if there's
68472e910b95SDavid Howells  * insufficient space in the buffer to transfer anything.
68482e910b95SDavid Howells  */
68492e910b95SDavid Howells ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
68502e910b95SDavid Howells 			     ssize_t maxsize, gfp_t gfp)
68512e910b95SDavid Howells {
68522e910b95SDavid Howells 	size_t frag_limit = READ_ONCE(sysctl_max_skb_frags);
68532e910b95SDavid Howells 	struct page *pages[8], **ppages = pages;
68542e910b95SDavid Howells 	ssize_t spliced = 0, ret = 0;
68552e910b95SDavid Howells 	unsigned int i;
68562e910b95SDavid Howells 
68572e910b95SDavid Howells 	while (iter->count > 0) {
6858ef1bc119SDan Carpenter 		ssize_t space, nr, len;
6859ef1bc119SDan Carpenter 		size_t off;
68602e910b95SDavid Howells 
68612e910b95SDavid Howells 		ret = -EMSGSIZE;
68622e910b95SDavid Howells 		space = frag_limit - skb_shinfo(skb)->nr_frags;
68632e910b95SDavid Howells 		if (space < 0)
68642e910b95SDavid Howells 			break;
68652e910b95SDavid Howells 
68662e910b95SDavid Howells 		/* We might be able to coalesce without increasing nr_frags */
68672e910b95SDavid Howells 		nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages));
68682e910b95SDavid Howells 
68692e910b95SDavid Howells 		len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off);
68702e910b95SDavid Howells 		if (len <= 0) {
68712e910b95SDavid Howells 			ret = len ?: -EIO;
68722e910b95SDavid Howells 			break;
68732e910b95SDavid Howells 		}
68742e910b95SDavid Howells 
68752e910b95SDavid Howells 		i = 0;
68762e910b95SDavid Howells 		do {
68772e910b95SDavid Howells 			struct page *page = pages[i++];
68782e910b95SDavid Howells 			size_t part = min_t(size_t, PAGE_SIZE - off, len);
68792e910b95SDavid Howells 
68802e910b95SDavid Howells 			ret = -EIO;
68812e910b95SDavid Howells 			if (WARN_ON_ONCE(!sendpage_ok(page)))
68822e910b95SDavid Howells 				goto out;
68832e910b95SDavid Howells 
68842e910b95SDavid Howells 			ret = skb_append_pagefrags(skb, page, off, part,
68852e910b95SDavid Howells 						   frag_limit);
68862e910b95SDavid Howells 			if (ret < 0) {
68872e910b95SDavid Howells 				iov_iter_revert(iter, len);
68882e910b95SDavid Howells 				goto out;
68892e910b95SDavid Howells 			}
68902e910b95SDavid Howells 
68912e910b95SDavid Howells 			if (skb->ip_summed == CHECKSUM_NONE)
68922e910b95SDavid Howells 				skb_splice_csum_page(skb, page, off, part);
68932e910b95SDavid Howells 
68942e910b95SDavid Howells 			off = 0;
68952e910b95SDavid Howells 			spliced += part;
68962e910b95SDavid Howells 			maxsize -= part;
68972e910b95SDavid Howells 			len -= part;
68982e910b95SDavid Howells 		} while (len > 0);
68992e910b95SDavid Howells 
69002e910b95SDavid Howells 		if (maxsize <= 0)
69012e910b95SDavid Howells 			break;
69022e910b95SDavid Howells 	}
69032e910b95SDavid Howells 
69042e910b95SDavid Howells out:
69052e910b95SDavid Howells 	skb_len_add(skb, spliced);
69062e910b95SDavid Howells 	return spliced ?: ret;
69072e910b95SDavid Howells }
69082e910b95SDavid Howells EXPORT_SYMBOL(skb_splice_from_iter);
6909