xref: /openbmc/linux/net/core/skbuff.c (revision 3226b158)
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>
610d5501c1SVlad Yasevich #include <linux/if_vlan.h>
622a2ea508SJohn Hurley #include <linux/mpls.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #include <net/protocol.h>
651da177e4SLinus Torvalds #include <net/dst.h>
661da177e4SLinus Torvalds #include <net/sock.h>
671da177e4SLinus Torvalds #include <net/checksum.h>
68ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
691da177e4SLinus Torvalds #include <net/xfrm.h>
708822e270SJohn Hurley #include <net/mpls.h>
713ee17bc7SMat Martineau #include <net/mptcp.h>
721da177e4SLinus Torvalds 
737c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
74ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
7551c56b00SEric Dumazet #include <linux/highmem.h>
76b245be1fSWillem de Bruijn #include <linux/capability.h>
77b245be1fSWillem de Bruijn #include <linux/user_namespace.h>
782544af03SMatteo Croce #include <linux/indirect_call_wrapper.h>
79a1f8e7f7SAl Viro 
807b7ed885SBart Van Assche #include "datagram.h"
817b7ed885SBart Van Assche 
8208009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init;
8308009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
84df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
85df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init;
86df5042f4SFlorian Westphal #endif
875f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
885f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /**
91f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
921da177e4SLinus Torvalds  *	@skb:	buffer
931da177e4SLinus Torvalds  *	@sz:	size
94f05de73bSJean Sacren  *	@addr:	address
9599d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
961da177e4SLinus Torvalds  *
97f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
98f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
99f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
100f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
1011da177e4SLinus Torvalds  */
102f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
10399d5851eSJames Hogan 		      const char msg[])
1041da177e4SLinus Torvalds {
10541a46913SJesper Dangaard Brouer 	pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
10699d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
1074305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
10826095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
1091da177e4SLinus Torvalds 	BUG();
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
112f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1131da177e4SLinus Torvalds {
114f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
117f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
118f05de73bSJean Sacren {
119f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
120f05de73bSJean Sacren }
121c93bdd0eSMel Gorman 
122c93bdd0eSMel Gorman /*
123c93bdd0eSMel Gorman  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
124c93bdd0eSMel Gorman  * the caller if emergency pfmemalloc reserves are being used. If it is and
125c93bdd0eSMel Gorman  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
126c93bdd0eSMel Gorman  * may be used. Otherwise, the packet data may be discarded until enough
127c93bdd0eSMel Gorman  * memory is free
128c93bdd0eSMel Gorman  */
129c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
130c93bdd0eSMel Gorman 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
13161c5e88aSstephen hemminger 
13261c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
13361c5e88aSstephen hemminger 			       unsigned long ip, bool *pfmemalloc)
134c93bdd0eSMel Gorman {
135c93bdd0eSMel Gorman 	void *obj;
136c93bdd0eSMel Gorman 	bool ret_pfmemalloc = false;
137c93bdd0eSMel Gorman 
138c93bdd0eSMel Gorman 	/*
139c93bdd0eSMel Gorman 	 * Try a regular allocation, when that fails and we're not entitled
140c93bdd0eSMel Gorman 	 * to the reserves, fail.
141c93bdd0eSMel Gorman 	 */
142c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size,
143c93bdd0eSMel Gorman 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
144c93bdd0eSMel Gorman 					node);
145c93bdd0eSMel Gorman 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
146c93bdd0eSMel Gorman 		goto out;
147c93bdd0eSMel Gorman 
148c93bdd0eSMel Gorman 	/* Try again but now we are using pfmemalloc reserves */
149c93bdd0eSMel Gorman 	ret_pfmemalloc = true;
150c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size, flags, node);
151c93bdd0eSMel Gorman 
152c93bdd0eSMel Gorman out:
153c93bdd0eSMel Gorman 	if (pfmemalloc)
154c93bdd0eSMel Gorman 		*pfmemalloc = ret_pfmemalloc;
155c93bdd0eSMel Gorman 
156c93bdd0eSMel Gorman 	return obj;
157c93bdd0eSMel Gorman }
158c93bdd0eSMel Gorman 
1591da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1601da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1611da177e4SLinus Torvalds  *	[BEEP] leaks.
1621da177e4SLinus Torvalds  *
1631da177e4SLinus Torvalds  */
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /**
166d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1671da177e4SLinus Torvalds  *	@size: size to allocate
1681da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
169c93bdd0eSMel Gorman  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
170c93bdd0eSMel Gorman  *		instead of head cache and allocate a cloned (child) skb.
171c93bdd0eSMel Gorman  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
172c93bdd0eSMel Gorman  *		allocations in case the data is required for writeback
173b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
17694b6042cSBen Hutchings  *	tail room of at least size bytes. The object has a reference count
17794b6042cSBen Hutchings  *	of one. The return is the buffer. On a failure the return is %NULL.
1781da177e4SLinus Torvalds  *
1791da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1801da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1811da177e4SLinus Torvalds  */
182dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
183c93bdd0eSMel Gorman 			    int flags, int node)
1841da177e4SLinus Torvalds {
185e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1864947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1871da177e4SLinus Torvalds 	struct sk_buff *skb;
1881da177e4SLinus Torvalds 	u8 *data;
189c93bdd0eSMel Gorman 	bool pfmemalloc;
1901da177e4SLinus Torvalds 
191c93bdd0eSMel Gorman 	cache = (flags & SKB_ALLOC_FCLONE)
192c93bdd0eSMel Gorman 		? skbuff_fclone_cache : skbuff_head_cache;
193c93bdd0eSMel Gorman 
194c93bdd0eSMel Gorman 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
195c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1968798b3fbSHerbert Xu 
1971da177e4SLinus Torvalds 	/* Get the HEAD */
198b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1991da177e4SLinus Torvalds 	if (!skb)
2001da177e4SLinus Torvalds 		goto out;
201ec7d2f2cSEric Dumazet 	prefetchw(skb);
2021da177e4SLinus Torvalds 
20387fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
20487fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
20587fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
20687fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
20787fb4b7bSEric Dumazet 	 */
208bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
20987fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
210c93bdd0eSMel Gorman 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
2111da177e4SLinus Torvalds 	if (!data)
2121da177e4SLinus Torvalds 		goto nodata;
21387fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
21487fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
21587fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
21687fb4b7bSEric Dumazet 	 */
21787fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
218ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2191da177e4SLinus Torvalds 
220ca0605a7SArnaldo Carvalho de Melo 	/*
221c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
222c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
223c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
224ca0605a7SArnaldo Carvalho de Melo 	 */
225ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
22687fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
22787fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
228c93bdd0eSMel Gorman 	skb->pfmemalloc = pfmemalloc;
22963354797SReshetova, Elena 	refcount_set(&skb->users, 1);
2301da177e4SLinus Torvalds 	skb->head = data;
2311da177e4SLinus Torvalds 	skb->data = data;
23227a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2334305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
23435d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
23535d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
23619633e12SStephen Hemminger 
2374947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2384947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
239ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2404947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
2414947d3efSBenjamin LaHaise 
242c93bdd0eSMel Gorman 	if (flags & SKB_ALLOC_FCLONE) {
243d0bf4a9eSEric Dumazet 		struct sk_buff_fclones *fclones;
2441da177e4SLinus Torvalds 
245d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
246d0bf4a9eSEric Dumazet 
247d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
2482638595aSReshetova, Elena 		refcount_set(&fclones->fclone_ref, 1);
249d179cd12SDavid S. Miller 
2506ffe75ebSEric Dumazet 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
251d179cd12SDavid S. Miller 	}
2526370cc3bSAleksandr Nogikh 
2536370cc3bSAleksandr Nogikh 	skb_set_kcov_handle(skb, kcov_common_handle());
2546370cc3bSAleksandr Nogikh 
2551da177e4SLinus Torvalds out:
2561da177e4SLinus Torvalds 	return skb;
2571da177e4SLinus Torvalds nodata:
2588798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2591da177e4SLinus Torvalds 	skb = NULL;
2601da177e4SLinus Torvalds 	goto out;
2611da177e4SLinus Torvalds }
262b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2631da177e4SLinus Torvalds 
264ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */
265ba0509b6SJesper Dangaard Brouer static struct sk_buff *__build_skb_around(struct sk_buff *skb,
266ba0509b6SJesper Dangaard Brouer 					  void *data, unsigned int frag_size)
267ba0509b6SJesper Dangaard Brouer {
268ba0509b6SJesper Dangaard Brouer 	struct skb_shared_info *shinfo;
269ba0509b6SJesper Dangaard Brouer 	unsigned int size = frag_size ? : ksize(data);
270ba0509b6SJesper Dangaard Brouer 
271ba0509b6SJesper Dangaard Brouer 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
272ba0509b6SJesper Dangaard Brouer 
273ba0509b6SJesper Dangaard Brouer 	/* Assumes caller memset cleared SKB */
274ba0509b6SJesper Dangaard Brouer 	skb->truesize = SKB_TRUESIZE(size);
275ba0509b6SJesper Dangaard Brouer 	refcount_set(&skb->users, 1);
276ba0509b6SJesper Dangaard Brouer 	skb->head = data;
277ba0509b6SJesper Dangaard Brouer 	skb->data = data;
278ba0509b6SJesper Dangaard Brouer 	skb_reset_tail_pointer(skb);
279ba0509b6SJesper Dangaard Brouer 	skb->end = skb->tail + size;
280ba0509b6SJesper Dangaard Brouer 	skb->mac_header = (typeof(skb->mac_header))~0U;
281ba0509b6SJesper Dangaard Brouer 	skb->transport_header = (typeof(skb->transport_header))~0U;
282ba0509b6SJesper Dangaard Brouer 
283ba0509b6SJesper Dangaard Brouer 	/* make sure we initialize shinfo sequentially */
284ba0509b6SJesper Dangaard Brouer 	shinfo = skb_shinfo(skb);
285ba0509b6SJesper Dangaard Brouer 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
286ba0509b6SJesper Dangaard Brouer 	atomic_set(&shinfo->dataref, 1);
287ba0509b6SJesper Dangaard Brouer 
2886370cc3bSAleksandr Nogikh 	skb_set_kcov_handle(skb, kcov_common_handle());
2896370cc3bSAleksandr Nogikh 
290ba0509b6SJesper Dangaard Brouer 	return skb;
291ba0509b6SJesper Dangaard Brouer }
292ba0509b6SJesper Dangaard Brouer 
2931da177e4SLinus Torvalds /**
2942ea2f62cSEric Dumazet  * __build_skb - build a network buffer
295b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
2962ea2f62cSEric Dumazet  * @frag_size: size of data, or 0 if head was kmalloced
297b2b5ce9dSEric Dumazet  *
298b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
299deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
3002ea2f62cSEric Dumazet  * @frag_size is 0, otherwise data should come from the page allocator
3012ea2f62cSEric Dumazet  *  or vmalloc()
302b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
303b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
304b2b5ce9dSEric Dumazet  * Notes :
305b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
306b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
307b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
308b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
309b2b5ce9dSEric Dumazet  *  before giving packet to stack.
310b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
311b2b5ce9dSEric Dumazet  */
3122ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size)
313b2b5ce9dSEric Dumazet {
314b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
315b2b5ce9dSEric Dumazet 
316b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
317ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
318b2b5ce9dSEric Dumazet 		return NULL;
319b2b5ce9dSEric Dumazet 
320b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
321b2b5ce9dSEric Dumazet 
322ba0509b6SJesper Dangaard Brouer 	return __build_skb_around(skb, data, frag_size);
323b2b5ce9dSEric Dumazet }
3242ea2f62cSEric Dumazet 
3252ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically
3262ea2f62cSEric Dumazet  * takes care of skb->head and skb->pfmemalloc
3272ea2f62cSEric Dumazet  * This means that if @frag_size is not zero, then @data must be backed
3282ea2f62cSEric Dumazet  * by a page fragment, not kmalloc() or vmalloc()
3292ea2f62cSEric Dumazet  */
3302ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
3312ea2f62cSEric Dumazet {
3322ea2f62cSEric Dumazet 	struct sk_buff *skb = __build_skb(data, frag_size);
3332ea2f62cSEric Dumazet 
3342ea2f62cSEric Dumazet 	if (skb && frag_size) {
3352ea2f62cSEric Dumazet 		skb->head_frag = 1;
3362f064f34SMichal Hocko 		if (page_is_pfmemalloc(virt_to_head_page(data)))
3372ea2f62cSEric Dumazet 			skb->pfmemalloc = 1;
3382ea2f62cSEric Dumazet 	}
3392ea2f62cSEric Dumazet 	return skb;
3402ea2f62cSEric Dumazet }
341b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
342b2b5ce9dSEric Dumazet 
343ba0509b6SJesper Dangaard Brouer /**
344ba0509b6SJesper Dangaard Brouer  * build_skb_around - build a network buffer around provided skb
345ba0509b6SJesper Dangaard Brouer  * @skb: sk_buff provide by caller, must be memset cleared
346ba0509b6SJesper Dangaard Brouer  * @data: data buffer provided by caller
347ba0509b6SJesper Dangaard Brouer  * @frag_size: size of data, or 0 if head was kmalloced
348ba0509b6SJesper Dangaard Brouer  */
349ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb,
350ba0509b6SJesper Dangaard Brouer 				 void *data, unsigned int frag_size)
351ba0509b6SJesper Dangaard Brouer {
352ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
353ba0509b6SJesper Dangaard Brouer 		return NULL;
354ba0509b6SJesper Dangaard Brouer 
355ba0509b6SJesper Dangaard Brouer 	skb = __build_skb_around(skb, data, frag_size);
356ba0509b6SJesper Dangaard Brouer 
357ba0509b6SJesper Dangaard Brouer 	if (skb && frag_size) {
358ba0509b6SJesper Dangaard Brouer 		skb->head_frag = 1;
359ba0509b6SJesper Dangaard Brouer 		if (page_is_pfmemalloc(virt_to_head_page(data)))
360ba0509b6SJesper Dangaard Brouer 			skb->pfmemalloc = 1;
361ba0509b6SJesper Dangaard Brouer 	}
362ba0509b6SJesper Dangaard Brouer 	return skb;
363ba0509b6SJesper Dangaard Brouer }
364ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around);
365ba0509b6SJesper Dangaard Brouer 
366795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE	64
367795bb1c0SJesper Dangaard Brouer 
368795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache {
369795bb1c0SJesper Dangaard Brouer 	struct page_frag_cache page;
370e0d7924aSAlexey Dobriyan 	unsigned int skb_count;
371795bb1c0SJesper Dangaard Brouer 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
372795bb1c0SJesper Dangaard Brouer };
373795bb1c0SJesper Dangaard Brouer 
374b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
375795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
376ffde7328SAlexander Duyck 
377ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
378ffde7328SAlexander Duyck {
379795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
3809451980aSAlexander Duyck 
3818c2dd3e4SAlexander Duyck 	return page_frag_alloc(&nc->page, fragsz, gfp_mask);
382ffde7328SAlexander Duyck }
383ffde7328SAlexander Duyck 
384ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
385ffde7328SAlexander Duyck {
3863bed3cc4SAlexander Duyck 	fragsz = SKB_DATA_ALIGN(fragsz);
3873bed3cc4SAlexander Duyck 
388453f85d4SMel Gorman 	return __napi_alloc_frag(fragsz, GFP_ATOMIC);
389ffde7328SAlexander Duyck }
390ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
391ffde7328SAlexander Duyck 
3926f532612SEric Dumazet /**
3937ba7aeabSSebastian Andrzej Siewior  * netdev_alloc_frag - allocate a page fragment
3947ba7aeabSSebastian Andrzej Siewior  * @fragsz: fragment size
3957ba7aeabSSebastian Andrzej Siewior  *
3967ba7aeabSSebastian Andrzej Siewior  * Allocates a frag from a page for receive buffer.
3977ba7aeabSSebastian Andrzej Siewior  * Uses GFP_ATOMIC allocations.
3987ba7aeabSSebastian Andrzej Siewior  */
3997ba7aeabSSebastian Andrzej Siewior void *netdev_alloc_frag(unsigned int fragsz)
4007ba7aeabSSebastian Andrzej Siewior {
4017ba7aeabSSebastian Andrzej Siewior 	struct page_frag_cache *nc;
4027ba7aeabSSebastian Andrzej Siewior 	void *data;
4037ba7aeabSSebastian Andrzej Siewior 
4047ba7aeabSSebastian Andrzej Siewior 	fragsz = SKB_DATA_ALIGN(fragsz);
4057ba7aeabSSebastian Andrzej Siewior 	if (in_irq() || irqs_disabled()) {
4067ba7aeabSSebastian Andrzej Siewior 		nc = this_cpu_ptr(&netdev_alloc_cache);
4077ba7aeabSSebastian Andrzej Siewior 		data = page_frag_alloc(nc, fragsz, GFP_ATOMIC);
4087ba7aeabSSebastian Andrzej Siewior 	} else {
4097ba7aeabSSebastian Andrzej Siewior 		local_bh_disable();
4107ba7aeabSSebastian Andrzej Siewior 		data = __napi_alloc_frag(fragsz, GFP_ATOMIC);
4117ba7aeabSSebastian Andrzej Siewior 		local_bh_enable();
4127ba7aeabSSebastian Andrzej Siewior 	}
4137ba7aeabSSebastian Andrzej Siewior 	return data;
4147ba7aeabSSebastian Andrzej Siewior }
4157ba7aeabSSebastian Andrzej Siewior EXPORT_SYMBOL(netdev_alloc_frag);
4167ba7aeabSSebastian Andrzej Siewior 
4177ba7aeabSSebastian Andrzej Siewior /**
418fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
419fd11a83dSAlexander Duyck  *	@dev: network device to receive on
420d7499160SMasanari Iida  *	@len: length to allocate
421fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
422fd11a83dSAlexander Duyck  *
423fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
424fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
425fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
426fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
427fd11a83dSAlexander Duyck  *
428fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
429fd11a83dSAlexander Duyck  */
4309451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
4319451980aSAlexander Duyck 				   gfp_t gfp_mask)
432fd11a83dSAlexander Duyck {
433b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
434fd11a83dSAlexander Duyck 	struct sk_buff *skb;
4359451980aSAlexander Duyck 	bool pfmemalloc;
4369451980aSAlexander Duyck 	void *data;
437fd11a83dSAlexander Duyck 
4389451980aSAlexander Duyck 	len += NET_SKB_PAD;
439fd11a83dSAlexander Duyck 
4409451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
441d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
442a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
443a080e7bdSAlexander Duyck 		if (!skb)
444a080e7bdSAlexander Duyck 			goto skb_fail;
445a080e7bdSAlexander Duyck 		goto skb_success;
446a080e7bdSAlexander Duyck 	}
4479451980aSAlexander Duyck 
4489451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4499451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
4509451980aSAlexander Duyck 
4519451980aSAlexander Duyck 	if (sk_memalloc_socks())
4529451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
4539451980aSAlexander Duyck 
45492dcabd7SSebastian Andrzej Siewior 	if (in_irq() || irqs_disabled()) {
4559451980aSAlexander Duyck 		nc = this_cpu_ptr(&netdev_alloc_cache);
4568c2dd3e4SAlexander Duyck 		data = page_frag_alloc(nc, len, gfp_mask);
4579451980aSAlexander Duyck 		pfmemalloc = nc->pfmemalloc;
45892dcabd7SSebastian Andrzej Siewior 	} else {
45992dcabd7SSebastian Andrzej Siewior 		local_bh_disable();
46092dcabd7SSebastian Andrzej Siewior 		nc = this_cpu_ptr(&napi_alloc_cache.page);
46192dcabd7SSebastian Andrzej Siewior 		data = page_frag_alloc(nc, len, gfp_mask);
46292dcabd7SSebastian Andrzej Siewior 		pfmemalloc = nc->pfmemalloc;
46392dcabd7SSebastian Andrzej Siewior 		local_bh_enable();
46492dcabd7SSebastian Andrzej Siewior 	}
4659451980aSAlexander Duyck 
4669451980aSAlexander Duyck 	if (unlikely(!data))
4679451980aSAlexander Duyck 		return NULL;
4689451980aSAlexander Duyck 
4699451980aSAlexander Duyck 	skb = __build_skb(data, len);
4709451980aSAlexander Duyck 	if (unlikely(!skb)) {
471181edb2bSAlexander Duyck 		skb_free_frag(data);
4729451980aSAlexander Duyck 		return NULL;
4739451980aSAlexander Duyck 	}
4749451980aSAlexander Duyck 
4759451980aSAlexander Duyck 	if (pfmemalloc)
4769451980aSAlexander Duyck 		skb->pfmemalloc = 1;
4779451980aSAlexander Duyck 	skb->head_frag = 1;
4789451980aSAlexander Duyck 
479a080e7bdSAlexander Duyck skb_success:
4808af27456SChristoph Hellwig 	skb_reserve(skb, NET_SKB_PAD);
4817b2e497aSChristoph Hellwig 	skb->dev = dev;
482fd11a83dSAlexander Duyck 
483a080e7bdSAlexander Duyck skb_fail:
4848af27456SChristoph Hellwig 	return skb;
4858af27456SChristoph Hellwig }
486b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4871da177e4SLinus Torvalds 
488fd11a83dSAlexander Duyck /**
489fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
490fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
491d7499160SMasanari Iida  *	@len: length to allocate
492fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
493fd11a83dSAlexander Duyck  *
494fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
495fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
496fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
497fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
498fd11a83dSAlexander Duyck  *
499fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
500fd11a83dSAlexander Duyck  */
5019451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
5029451980aSAlexander Duyck 				 gfp_t gfp_mask)
503fd11a83dSAlexander Duyck {
504*3226b158SEric Dumazet 	struct napi_alloc_cache *nc;
505fd11a83dSAlexander Duyck 	struct sk_buff *skb;
5069451980aSAlexander Duyck 	void *data;
507fd11a83dSAlexander Duyck 
5089451980aSAlexander Duyck 	len += NET_SKB_PAD + NET_IP_ALIGN;
509fd11a83dSAlexander Duyck 
510*3226b158SEric Dumazet 	/* If requested length is either too small or too big,
511*3226b158SEric Dumazet 	 * we use kmalloc() for skb->head allocation.
512*3226b158SEric Dumazet 	 */
513*3226b158SEric Dumazet 	if (len <= SKB_WITH_OVERHEAD(1024) ||
514*3226b158SEric Dumazet 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
515d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
516a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
517a080e7bdSAlexander Duyck 		if (!skb)
518a080e7bdSAlexander Duyck 			goto skb_fail;
519a080e7bdSAlexander Duyck 		goto skb_success;
520a080e7bdSAlexander Duyck 	}
5219451980aSAlexander Duyck 
522*3226b158SEric Dumazet 	nc = this_cpu_ptr(&napi_alloc_cache);
5239451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5249451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
5259451980aSAlexander Duyck 
5269451980aSAlexander Duyck 	if (sk_memalloc_socks())
5279451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
5289451980aSAlexander Duyck 
5298c2dd3e4SAlexander Duyck 	data = page_frag_alloc(&nc->page, len, gfp_mask);
5309451980aSAlexander Duyck 	if (unlikely(!data))
5319451980aSAlexander Duyck 		return NULL;
5329451980aSAlexander Duyck 
5339451980aSAlexander Duyck 	skb = __build_skb(data, len);
5349451980aSAlexander Duyck 	if (unlikely(!skb)) {
535181edb2bSAlexander Duyck 		skb_free_frag(data);
5369451980aSAlexander Duyck 		return NULL;
5379451980aSAlexander Duyck 	}
5389451980aSAlexander Duyck 
539795bb1c0SJesper Dangaard Brouer 	if (nc->page.pfmemalloc)
5409451980aSAlexander Duyck 		skb->pfmemalloc = 1;
5419451980aSAlexander Duyck 	skb->head_frag = 1;
5429451980aSAlexander Duyck 
543a080e7bdSAlexander Duyck skb_success:
544fd11a83dSAlexander Duyck 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
545fd11a83dSAlexander Duyck 	skb->dev = napi->dev;
546fd11a83dSAlexander Duyck 
547a080e7bdSAlexander Duyck skb_fail:
548fd11a83dSAlexander Duyck 	return skb;
549fd11a83dSAlexander Duyck }
550fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
551fd11a83dSAlexander Duyck 
552654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
55350269e19SEric Dumazet 		     int size, unsigned int truesize)
554654bed16SPeter Zijlstra {
555654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
556654bed16SPeter Zijlstra 	skb->len += size;
557654bed16SPeter Zijlstra 	skb->data_len += size;
55850269e19SEric Dumazet 	skb->truesize += truesize;
559654bed16SPeter Zijlstra }
560654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
561654bed16SPeter Zijlstra 
562f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
563f8e617e1SJason Wang 			  unsigned int truesize)
564f8e617e1SJason Wang {
565f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
566f8e617e1SJason Wang 
567f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
568f8e617e1SJason Wang 	skb->len += size;
569f8e617e1SJason Wang 	skb->data_len += size;
570f8e617e1SJason Wang 	skb->truesize += truesize;
571f8e617e1SJason Wang }
572f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
573f8e617e1SJason Wang 
57427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5751da177e4SLinus Torvalds {
576bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
57727b437c8SHerbert Xu 	*listp = NULL;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
58027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
58127b437c8SHerbert Xu {
58227b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
58327b437c8SHerbert Xu }
58427b437c8SHerbert Xu 
5851da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	struct sk_buff *list;
5881da177e4SLinus Torvalds 
589fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5901da177e4SLinus Torvalds 		skb_get(list);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
593d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
594d3836f21SEric Dumazet {
595181edb2bSAlexander Duyck 	unsigned char *head = skb->head;
596181edb2bSAlexander Duyck 
597d3836f21SEric Dumazet 	if (skb->head_frag)
598181edb2bSAlexander Duyck 		skb_free_frag(head);
599d3836f21SEric Dumazet 	else
600181edb2bSAlexander Duyck 		kfree(head);
601d3836f21SEric Dumazet }
602d3836f21SEric Dumazet 
6035bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
6041da177e4SLinus Torvalds {
605ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
6061da177e4SLinus Torvalds 	int i;
607ff04a771SEric Dumazet 
608ff04a771SEric Dumazet 	if (skb->cloned &&
609ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
610ff04a771SEric Dumazet 			      &shinfo->dataref))
611ff04a771SEric Dumazet 		return;
612ff04a771SEric Dumazet 
613ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
614ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
6151da177e4SLinus Torvalds 
616ff04a771SEric Dumazet 	if (shinfo->frag_list)
617ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
6181da177e4SLinus Torvalds 
6191f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
620d3836f21SEric Dumazet 	skb_free_head(skb);
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /*
6241da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
6251da177e4SLinus Torvalds  */
6262d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
6271da177e4SLinus Torvalds {
628d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
629d179cd12SDavid S. Miller 
630d179cd12SDavid S. Miller 	switch (skb->fclone) {
631d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
6321da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
6336ffe75ebSEric Dumazet 		return;
634d179cd12SDavid S. Miller 
635d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
636d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6376ffe75ebSEric Dumazet 
6386ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
6396ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
6406ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
6416ffe75ebSEric Dumazet 		 */
6422638595aSReshetova, Elena 		if (refcount_read(&fclones->fclone_ref) == 1)
6436ffe75ebSEric Dumazet 			goto fastpath;
644d179cd12SDavid S. Miller 		break;
645d179cd12SDavid S. Miller 
6466ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
647d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
648d179cd12SDavid S. Miller 		break;
6493ff50b79SStephen Hemminger 	}
6502638595aSReshetova, Elena 	if (!refcount_dec_and_test(&fclones->fclone_ref))
6516ffe75ebSEric Dumazet 		return;
6526ffe75ebSEric Dumazet fastpath:
6536ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6560a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb)
6571da177e4SLinus Torvalds {
658adf30907SEric Dumazet 	skb_dst_drop(skb);
6591da177e4SLinus Torvalds 	if (skb->destructor) {
6609c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6611da177e4SLinus Torvalds 		skb->destructor(skb);
6621da177e4SLinus Torvalds 	}
663a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
664cb9c6836SFlorian Westphal 	nf_conntrack_put(skb_nfct(skb));
6652fc72c7bSKOVACS Krisztian #endif
666df5042f4SFlorian Westphal 	skb_ext_put(skb);
66704a4bb55SLennert Buytenhek }
66804a4bb55SLennert Buytenhek 
66904a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
67004a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
67104a4bb55SLennert Buytenhek {
67204a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
673a28b1b90SFlorian Westphal 	if (likely(skb->head))
6742d4baff8SHerbert Xu 		skb_release_data(skb);
6752d4baff8SHerbert Xu }
6761da177e4SLinus Torvalds 
6772d4baff8SHerbert Xu /**
6782d4baff8SHerbert Xu  *	__kfree_skb - private function
6792d4baff8SHerbert Xu  *	@skb: buffer
6802d4baff8SHerbert Xu  *
6812d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6822d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6832d4baff8SHerbert Xu  *	always call kfree_skb
6842d4baff8SHerbert Xu  */
6852d4baff8SHerbert Xu 
6862d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6872d4baff8SHerbert Xu {
6882d4baff8SHerbert Xu 	skb_release_all(skb);
6891da177e4SLinus Torvalds 	kfree_skbmem(skb);
6901da177e4SLinus Torvalds }
691b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /**
694231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
695231d06aeSJörn Engel  *	@skb: buffer to free
696231d06aeSJörn Engel  *
697231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
698231d06aeSJörn Engel  *	hit zero.
699231d06aeSJörn Engel  */
700231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
701231d06aeSJörn Engel {
7023889a803SPaolo Abeni 	if (!skb_unref(skb))
703231d06aeSJörn Engel 		return;
7043889a803SPaolo Abeni 
705ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
706231d06aeSJörn Engel 	__kfree_skb(skb);
707231d06aeSJörn Engel }
708b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
709231d06aeSJörn Engel 
710bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
711bd8a7036SEric Dumazet {
712bd8a7036SEric Dumazet 	while (segs) {
713bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
714bd8a7036SEric Dumazet 
715bd8a7036SEric Dumazet 		kfree_skb(segs);
716bd8a7036SEric Dumazet 		segs = next;
717bd8a7036SEric Dumazet 	}
718bd8a7036SEric Dumazet }
719bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
720bd8a7036SEric Dumazet 
7216413139dSWillem de Bruijn /* Dump skb information and contents.
7226413139dSWillem de Bruijn  *
7236413139dSWillem de Bruijn  * Must only be called from net_ratelimit()-ed paths.
7246413139dSWillem de Bruijn  *
725302af7c6SVladimir Oltean  * Dumps whole packets if full_pkt, only headers otherwise.
7266413139dSWillem de Bruijn  */
7276413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
7286413139dSWillem de Bruijn {
7296413139dSWillem de Bruijn 	struct skb_shared_info *sh = skb_shinfo(skb);
7306413139dSWillem de Bruijn 	struct net_device *dev = skb->dev;
7316413139dSWillem de Bruijn 	struct sock *sk = skb->sk;
7326413139dSWillem de Bruijn 	struct sk_buff *list_skb;
7336413139dSWillem de Bruijn 	bool has_mac, has_trans;
7346413139dSWillem de Bruijn 	int headroom, tailroom;
7356413139dSWillem de Bruijn 	int i, len, seg_len;
7366413139dSWillem de Bruijn 
7376413139dSWillem de Bruijn 	if (full_pkt)
7386413139dSWillem de Bruijn 		len = skb->len;
7396413139dSWillem de Bruijn 	else
7406413139dSWillem de Bruijn 		len = min_t(int, skb->len, MAX_HEADER + 128);
7416413139dSWillem de Bruijn 
7426413139dSWillem de Bruijn 	headroom = skb_headroom(skb);
7436413139dSWillem de Bruijn 	tailroom = skb_tailroom(skb);
7446413139dSWillem de Bruijn 
7456413139dSWillem de Bruijn 	has_mac = skb_mac_header_was_set(skb);
7466413139dSWillem de Bruijn 	has_trans = skb_transport_header_was_set(skb);
7476413139dSWillem de Bruijn 
7486413139dSWillem de Bruijn 	printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
7496413139dSWillem de Bruijn 	       "mac=(%d,%d) net=(%d,%d) trans=%d\n"
7506413139dSWillem de Bruijn 	       "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
7516413139dSWillem de Bruijn 	       "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
7526413139dSWillem de Bruijn 	       "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
7536413139dSWillem de Bruijn 	       level, skb->len, headroom, skb_headlen(skb), tailroom,
7546413139dSWillem de Bruijn 	       has_mac ? skb->mac_header : -1,
7556413139dSWillem de Bruijn 	       has_mac ? skb_mac_header_len(skb) : -1,
7566413139dSWillem de Bruijn 	       skb->network_header,
7576413139dSWillem de Bruijn 	       has_trans ? skb_network_header_len(skb) : -1,
7586413139dSWillem de Bruijn 	       has_trans ? skb->transport_header : -1,
7596413139dSWillem de Bruijn 	       sh->tx_flags, sh->nr_frags,
7606413139dSWillem de Bruijn 	       sh->gso_size, sh->gso_type, sh->gso_segs,
7616413139dSWillem de Bruijn 	       skb->csum, skb->ip_summed, skb->csum_complete_sw,
7626413139dSWillem de Bruijn 	       skb->csum_valid, skb->csum_level,
7636413139dSWillem de Bruijn 	       skb->hash, skb->sw_hash, skb->l4_hash,
7646413139dSWillem de Bruijn 	       ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
7656413139dSWillem de Bruijn 
7666413139dSWillem de Bruijn 	if (dev)
7676413139dSWillem de Bruijn 		printk("%sdev name=%s feat=0x%pNF\n",
7686413139dSWillem de Bruijn 		       level, dev->name, &dev->features);
7696413139dSWillem de Bruijn 	if (sk)
770db8051f3SQian Cai 		printk("%ssk family=%hu type=%u proto=%u\n",
7716413139dSWillem de Bruijn 		       level, sk->sk_family, sk->sk_type, sk->sk_protocol);
7726413139dSWillem de Bruijn 
7736413139dSWillem de Bruijn 	if (full_pkt && headroom)
7746413139dSWillem de Bruijn 		print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
7756413139dSWillem de Bruijn 			       16, 1, skb->head, headroom, false);
7766413139dSWillem de Bruijn 
7776413139dSWillem de Bruijn 	seg_len = min_t(int, skb_headlen(skb), len);
7786413139dSWillem de Bruijn 	if (seg_len)
7796413139dSWillem de Bruijn 		print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
7806413139dSWillem de Bruijn 			       16, 1, skb->data, seg_len, false);
7816413139dSWillem de Bruijn 	len -= seg_len;
7826413139dSWillem de Bruijn 
7836413139dSWillem de Bruijn 	if (full_pkt && tailroom)
7846413139dSWillem de Bruijn 		print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
7856413139dSWillem de Bruijn 			       16, 1, skb_tail_pointer(skb), tailroom, false);
7866413139dSWillem de Bruijn 
7876413139dSWillem de Bruijn 	for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
7886413139dSWillem de Bruijn 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7896413139dSWillem de Bruijn 		u32 p_off, p_len, copied;
7906413139dSWillem de Bruijn 		struct page *p;
7916413139dSWillem de Bruijn 		u8 *vaddr;
7926413139dSWillem de Bruijn 
793b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(frag, skb_frag_off(frag),
7946413139dSWillem de Bruijn 				      skb_frag_size(frag), p, p_off, p_len,
7956413139dSWillem de Bruijn 				      copied) {
7966413139dSWillem de Bruijn 			seg_len = min_t(int, p_len, len);
7976413139dSWillem de Bruijn 			vaddr = kmap_atomic(p);
7986413139dSWillem de Bruijn 			print_hex_dump(level, "skb frag:     ",
7996413139dSWillem de Bruijn 				       DUMP_PREFIX_OFFSET,
8006413139dSWillem de Bruijn 				       16, 1, vaddr + p_off, seg_len, false);
8016413139dSWillem de Bruijn 			kunmap_atomic(vaddr);
8026413139dSWillem de Bruijn 			len -= seg_len;
8036413139dSWillem de Bruijn 			if (!len)
8046413139dSWillem de Bruijn 				break;
8056413139dSWillem de Bruijn 		}
8066413139dSWillem de Bruijn 	}
8076413139dSWillem de Bruijn 
8086413139dSWillem de Bruijn 	if (full_pkt && skb_has_frag_list(skb)) {
8096413139dSWillem de Bruijn 		printk("skb fraglist:\n");
8106413139dSWillem de Bruijn 		skb_walk_frags(skb, list_skb)
8116413139dSWillem de Bruijn 			skb_dump(level, list_skb, true);
8126413139dSWillem de Bruijn 	}
8136413139dSWillem de Bruijn }
8146413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump);
8156413139dSWillem de Bruijn 
816d1a203eaSStephen Hemminger /**
81725121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
81825121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
81925121173SMichael S. Tsirkin  *
82025121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
82125121173SMichael S. Tsirkin  *	skb must be freed afterwards.
82225121173SMichael S. Tsirkin  */
82325121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
82425121173SMichael S. Tsirkin {
8251f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
82625121173SMichael S. Tsirkin }
82725121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
82825121173SMichael S. Tsirkin 
829be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS
83025121173SMichael S. Tsirkin /**
831ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
832ead2ceb0SNeil Horman  *	@skb: buffer to free
833ead2ceb0SNeil Horman  *
834ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
835ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
836ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
837ead2ceb0SNeil Horman  */
838ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
839ead2ceb0SNeil Horman {
8403889a803SPaolo Abeni 	if (!skb_unref(skb))
841ead2ceb0SNeil Horman 		return;
8423889a803SPaolo Abeni 
84307dc22e7SKoki Sanagi 	trace_consume_skb(skb);
844ead2ceb0SNeil Horman 	__kfree_skb(skb);
845ead2ceb0SNeil Horman }
846ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
847be769db2SHerbert Xu #endif
848ead2ceb0SNeil Horman 
8490a463c78SPaolo Abeni /**
850c1639be9SMauro Carvalho Chehab  *	__consume_stateless_skb - free an skbuff, assuming it is stateless
8510a463c78SPaolo Abeni  *	@skb: buffer to free
8520a463c78SPaolo Abeni  *
853ca2c1418SPaolo Abeni  *	Alike consume_skb(), but this variant assumes that this is the last
854ca2c1418SPaolo Abeni  *	skb reference and all the head states have been already dropped
8550a463c78SPaolo Abeni  */
856ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb)
8570a463c78SPaolo Abeni {
8580a463c78SPaolo Abeni 	trace_consume_skb(skb);
8590a463c78SPaolo Abeni 	skb_release_data(skb);
8600a463c78SPaolo Abeni 	kfree_skbmem(skb);
8610a463c78SPaolo Abeni }
8620a463c78SPaolo Abeni 
863795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void)
864795bb1c0SJesper Dangaard Brouer {
865795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
866795bb1c0SJesper Dangaard Brouer 
867795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if containing objects */
868795bb1c0SJesper Dangaard Brouer 	if (nc->skb_count) {
869795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
870795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
871795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
872795bb1c0SJesper Dangaard Brouer 	}
873795bb1c0SJesper Dangaard Brouer }
874795bb1c0SJesper Dangaard Brouer 
87515fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb)
876795bb1c0SJesper Dangaard Brouer {
877795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
878795bb1c0SJesper Dangaard Brouer 
879795bb1c0SJesper Dangaard Brouer 	/* drop skb->head and call any destructors for packet */
880795bb1c0SJesper Dangaard Brouer 	skb_release_all(skb);
881795bb1c0SJesper Dangaard Brouer 
882795bb1c0SJesper Dangaard Brouer 	/* record skb to CPU local list */
883795bb1c0SJesper Dangaard Brouer 	nc->skb_cache[nc->skb_count++] = skb;
884795bb1c0SJesper Dangaard Brouer 
885795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB
886795bb1c0SJesper Dangaard Brouer 	/* SLUB writes into objects when freeing */
887795bb1c0SJesper Dangaard Brouer 	prefetchw(skb);
888795bb1c0SJesper Dangaard Brouer #endif
889795bb1c0SJesper Dangaard Brouer 
890795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if it is filled */
891795bb1c0SJesper Dangaard Brouer 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
892795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
893795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
894795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
895795bb1c0SJesper Dangaard Brouer 	}
896795bb1c0SJesper Dangaard Brouer }
89715fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb)
89815fad714SJesper Dangaard Brouer {
89915fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
90015fad714SJesper Dangaard Brouer }
901795bb1c0SJesper Dangaard Brouer 
902795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
903795bb1c0SJesper Dangaard Brouer {
904885eb0a5SJesper Dangaard Brouer 	/* Zero budget indicate non-NAPI context called us, like netpoll */
905795bb1c0SJesper Dangaard Brouer 	if (unlikely(!budget)) {
906885eb0a5SJesper Dangaard Brouer 		dev_consume_skb_any(skb);
907795bb1c0SJesper Dangaard Brouer 		return;
908795bb1c0SJesper Dangaard Brouer 	}
909795bb1c0SJesper Dangaard Brouer 
9106454eca8SYunsheng Lin 	lockdep_assert_in_softirq();
9116454eca8SYunsheng Lin 
9127608894eSPaolo Abeni 	if (!skb_unref(skb))
913795bb1c0SJesper Dangaard Brouer 		return;
9147608894eSPaolo Abeni 
915795bb1c0SJesper Dangaard Brouer 	/* if reaching here SKB is ready to free */
916795bb1c0SJesper Dangaard Brouer 	trace_consume_skb(skb);
917795bb1c0SJesper Dangaard Brouer 
918795bb1c0SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
919abbdb5a7SEric Dumazet 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
920795bb1c0SJesper Dangaard Brouer 		__kfree_skb(skb);
921795bb1c0SJesper Dangaard Brouer 		return;
922795bb1c0SJesper Dangaard Brouer 	}
923795bb1c0SJesper Dangaard Brouer 
92415fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
925795bb1c0SJesper Dangaard Brouer }
926795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
927795bb1c0SJesper Dangaard Brouer 
928b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
929b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
930b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
931b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
932b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
933b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
934b1937227SEric Dumazet 
935dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
936dec18810SHerbert Xu {
937dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
938b1937227SEric Dumazet 	/* We do not copy old->sk */
939dec18810SHerbert Xu 	new->dev		= old->dev;
940b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
9417fee226aSEric Dumazet 	skb_dst_copy(new, old);
942df5042f4SFlorian Westphal 	__skb_ext_copy(new, old);
943b1937227SEric Dumazet 	__nf_copy(new, old, false);
9446aa895b0SPatrick McHardy 
945b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
946b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
947b1937227SEric Dumazet 	 */
948b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
94906021292SEliezer Tamir 
950b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
951b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
952b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
953b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
954b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
955b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
956b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
957b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
958b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
959b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
960b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
961b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
962b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
963b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
964b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
965b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
966b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
967b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
968b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
969b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
970b1937227SEric Dumazet #endif
971e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
972b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
97306021292SEliezer Tamir #endif
9742bd82484SEric Dumazet #ifdef CONFIG_XPS
9752bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
9762bd82484SEric Dumazet #endif
977b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
978b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
979b1937227SEric Dumazet #endif
980b1937227SEric Dumazet 
981dec18810SHerbert Xu }
982dec18810SHerbert Xu 
98382c49a35SHerbert Xu /*
98482c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
98582c49a35SHerbert Xu  * __copy_skb_header above instead.
98682c49a35SHerbert Xu  */
987e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds #define C(x) n->x = skb->x
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	n->next = n->prev = NULL;
9921da177e4SLinus Torvalds 	n->sk = NULL;
993dec18810SHerbert Xu 	__copy_skb_header(n, skb);
994dec18810SHerbert Xu 
9951da177e4SLinus Torvalds 	C(len);
9961da177e4SLinus Torvalds 	C(data_len);
9973e6b3b2eSAlexey Dobriyan 	C(mac_len);
998334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
99902f1c89dSPaul Moore 	n->cloned = 1;
10001da177e4SLinus Torvalds 	n->nohdr = 0;
1001b13dda9fSEric Dumazet 	n->peeked = 0;
1002e78bfb07SStefano Brivio 	C(pfmemalloc);
10031da177e4SLinus Torvalds 	n->destructor = NULL;
10041da177e4SLinus Torvalds 	C(tail);
10051da177e4SLinus Torvalds 	C(end);
100602f1c89dSPaul Moore 	C(head);
1007d3836f21SEric Dumazet 	C(head_frag);
100802f1c89dSPaul Moore 	C(data);
100902f1c89dSPaul Moore 	C(truesize);
101063354797SReshetova, Elena 	refcount_set(&n->users, 1);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
10131da177e4SLinus Torvalds 	skb->cloned = 1;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	return n;
1016e0053ec0SHerbert Xu #undef C
1017e0053ec0SHerbert Xu }
1018e0053ec0SHerbert Xu 
1019e0053ec0SHerbert Xu /**
1020da29e4b4SJakub Kicinski  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1021da29e4b4SJakub Kicinski  * @first: first sk_buff of the msg
1022da29e4b4SJakub Kicinski  */
1023da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1024da29e4b4SJakub Kicinski {
1025da29e4b4SJakub Kicinski 	struct sk_buff *n;
1026da29e4b4SJakub Kicinski 
1027da29e4b4SJakub Kicinski 	n = alloc_skb(0, GFP_ATOMIC);
1028da29e4b4SJakub Kicinski 	if (!n)
1029da29e4b4SJakub Kicinski 		return NULL;
1030da29e4b4SJakub Kicinski 
1031da29e4b4SJakub Kicinski 	n->len = first->len;
1032da29e4b4SJakub Kicinski 	n->data_len = first->len;
1033da29e4b4SJakub Kicinski 	n->truesize = first->truesize;
1034da29e4b4SJakub Kicinski 
1035da29e4b4SJakub Kicinski 	skb_shinfo(n)->frag_list = first;
1036da29e4b4SJakub Kicinski 
1037da29e4b4SJakub Kicinski 	__copy_skb_header(n, first);
1038da29e4b4SJakub Kicinski 	n->destructor = NULL;
1039da29e4b4SJakub Kicinski 
1040da29e4b4SJakub Kicinski 	return n;
1041da29e4b4SJakub Kicinski }
1042da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1043da29e4b4SJakub Kicinski 
1044da29e4b4SJakub Kicinski /**
1045e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
1046e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
1047e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
1048e0053ec0SHerbert Xu  *
1049e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
1050e0053ec0SHerbert Xu  *	supplied by the user.
1051e0053ec0SHerbert Xu  *
1052e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
1053e0053ec0SHerbert Xu  */
1054e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1055e0053ec0SHerbert Xu {
10562d4baff8SHerbert Xu 	skb_release_all(dst);
1057e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
1058e0053ec0SHerbert Xu }
1059e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
1060e0053ec0SHerbert Xu 
10616f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1062a91dbff5SWillem de Bruijn {
1063a91dbff5SWillem de Bruijn 	unsigned long max_pg, num_pg, new_pg, old_pg;
1064a91dbff5SWillem de Bruijn 	struct user_struct *user;
1065a91dbff5SWillem de Bruijn 
1066a91dbff5SWillem de Bruijn 	if (capable(CAP_IPC_LOCK) || !size)
1067a91dbff5SWillem de Bruijn 		return 0;
1068a91dbff5SWillem de Bruijn 
1069a91dbff5SWillem de Bruijn 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
1070a91dbff5SWillem de Bruijn 	max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1071a91dbff5SWillem de Bruijn 	user = mmp->user ? : current_user();
1072a91dbff5SWillem de Bruijn 
1073a91dbff5SWillem de Bruijn 	do {
1074a91dbff5SWillem de Bruijn 		old_pg = atomic_long_read(&user->locked_vm);
1075a91dbff5SWillem de Bruijn 		new_pg = old_pg + num_pg;
1076a91dbff5SWillem de Bruijn 		if (new_pg > max_pg)
1077a91dbff5SWillem de Bruijn 			return -ENOBUFS;
1078a91dbff5SWillem de Bruijn 	} while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1079a91dbff5SWillem de Bruijn 		 old_pg);
1080a91dbff5SWillem de Bruijn 
1081a91dbff5SWillem de Bruijn 	if (!mmp->user) {
1082a91dbff5SWillem de Bruijn 		mmp->user = get_uid(user);
1083a91dbff5SWillem de Bruijn 		mmp->num_pg = num_pg;
1084a91dbff5SWillem de Bruijn 	} else {
1085a91dbff5SWillem de Bruijn 		mmp->num_pg += num_pg;
1086a91dbff5SWillem de Bruijn 	}
1087a91dbff5SWillem de Bruijn 
1088a91dbff5SWillem de Bruijn 	return 0;
1089a91dbff5SWillem de Bruijn }
10906f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1091a91dbff5SWillem de Bruijn 
10926f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp)
1093a91dbff5SWillem de Bruijn {
1094a91dbff5SWillem de Bruijn 	if (mmp->user) {
1095a91dbff5SWillem de Bruijn 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1096a91dbff5SWillem de Bruijn 		free_uid(mmp->user);
1097a91dbff5SWillem de Bruijn 	}
1098a91dbff5SWillem de Bruijn }
10996f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1100a91dbff5SWillem de Bruijn 
110152267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
110252267790SWillem de Bruijn {
110352267790SWillem de Bruijn 	struct ubuf_info *uarg;
110452267790SWillem de Bruijn 	struct sk_buff *skb;
110552267790SWillem de Bruijn 
110652267790SWillem de Bruijn 	WARN_ON_ONCE(!in_task());
110752267790SWillem de Bruijn 
110852267790SWillem de Bruijn 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
110952267790SWillem de Bruijn 	if (!skb)
111052267790SWillem de Bruijn 		return NULL;
111152267790SWillem de Bruijn 
111252267790SWillem de Bruijn 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
111352267790SWillem de Bruijn 	uarg = (void *)skb->cb;
1114a91dbff5SWillem de Bruijn 	uarg->mmp.user = NULL;
1115a91dbff5SWillem de Bruijn 
1116a91dbff5SWillem de Bruijn 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
1117a91dbff5SWillem de Bruijn 		kfree_skb(skb);
1118a91dbff5SWillem de Bruijn 		return NULL;
1119a91dbff5SWillem de Bruijn 	}
112052267790SWillem de Bruijn 
112152267790SWillem de Bruijn 	uarg->callback = sock_zerocopy_callback;
11224ab6c99dSWillem de Bruijn 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
11234ab6c99dSWillem de Bruijn 	uarg->len = 1;
11244ab6c99dSWillem de Bruijn 	uarg->bytelen = size;
112552267790SWillem de Bruijn 	uarg->zerocopy = 1;
1126c1d1b437SEric Dumazet 	refcount_set(&uarg->refcnt, 1);
112752267790SWillem de Bruijn 	sock_hold(sk);
112852267790SWillem de Bruijn 
112952267790SWillem de Bruijn 	return uarg;
113052267790SWillem de Bruijn }
113152267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
113252267790SWillem de Bruijn 
113352267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
113452267790SWillem de Bruijn {
113552267790SWillem de Bruijn 	return container_of((void *)uarg, struct sk_buff, cb);
113652267790SWillem de Bruijn }
113752267790SWillem de Bruijn 
11384ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
11394ab6c99dSWillem de Bruijn 					struct ubuf_info *uarg)
11404ab6c99dSWillem de Bruijn {
11414ab6c99dSWillem de Bruijn 	if (uarg) {
11424ab6c99dSWillem de Bruijn 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
11434ab6c99dSWillem de Bruijn 		u32 bytelen, next;
11444ab6c99dSWillem de Bruijn 
11454ab6c99dSWillem de Bruijn 		/* realloc only when socket is locked (TCP, UDP cork),
11464ab6c99dSWillem de Bruijn 		 * so uarg->len and sk_zckey access is serialized
11474ab6c99dSWillem de Bruijn 		 */
11484ab6c99dSWillem de Bruijn 		if (!sock_owned_by_user(sk)) {
11494ab6c99dSWillem de Bruijn 			WARN_ON_ONCE(1);
11504ab6c99dSWillem de Bruijn 			return NULL;
11514ab6c99dSWillem de Bruijn 		}
11524ab6c99dSWillem de Bruijn 
11534ab6c99dSWillem de Bruijn 		bytelen = uarg->bytelen + size;
11544ab6c99dSWillem de Bruijn 		if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
11554ab6c99dSWillem de Bruijn 			/* TCP can create new skb to attach new uarg */
11564ab6c99dSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
11574ab6c99dSWillem de Bruijn 				goto new_alloc;
11584ab6c99dSWillem de Bruijn 			return NULL;
11594ab6c99dSWillem de Bruijn 		}
11604ab6c99dSWillem de Bruijn 
11614ab6c99dSWillem de Bruijn 		next = (u32)atomic_read(&sk->sk_zckey);
11624ab6c99dSWillem de Bruijn 		if ((u32)(uarg->id + uarg->len) == next) {
1163a91dbff5SWillem de Bruijn 			if (mm_account_pinned_pages(&uarg->mmp, size))
1164a91dbff5SWillem de Bruijn 				return NULL;
11654ab6c99dSWillem de Bruijn 			uarg->len++;
11664ab6c99dSWillem de Bruijn 			uarg->bytelen = bytelen;
11674ab6c99dSWillem de Bruijn 			atomic_set(&sk->sk_zckey, ++next);
1168100f6d8eSWillem de Bruijn 
1169100f6d8eSWillem de Bruijn 			/* no extra ref when appending to datagram (MSG_MORE) */
1170100f6d8eSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
1171db5bce32SEric Dumazet 				sock_zerocopy_get(uarg);
1172100f6d8eSWillem de Bruijn 
11734ab6c99dSWillem de Bruijn 			return uarg;
11744ab6c99dSWillem de Bruijn 		}
11754ab6c99dSWillem de Bruijn 	}
11764ab6c99dSWillem de Bruijn 
11774ab6c99dSWillem de Bruijn new_alloc:
11784ab6c99dSWillem de Bruijn 	return sock_zerocopy_alloc(sk, size);
11794ab6c99dSWillem de Bruijn }
11804ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
11814ab6c99dSWillem de Bruijn 
11824ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
11834ab6c99dSWillem de Bruijn {
11844ab6c99dSWillem de Bruijn 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
11854ab6c99dSWillem de Bruijn 	u32 old_lo, old_hi;
11864ab6c99dSWillem de Bruijn 	u64 sum_len;
11874ab6c99dSWillem de Bruijn 
11884ab6c99dSWillem de Bruijn 	old_lo = serr->ee.ee_info;
11894ab6c99dSWillem de Bruijn 	old_hi = serr->ee.ee_data;
11904ab6c99dSWillem de Bruijn 	sum_len = old_hi - old_lo + 1ULL + len;
11914ab6c99dSWillem de Bruijn 
11924ab6c99dSWillem de Bruijn 	if (sum_len >= (1ULL << 32))
11934ab6c99dSWillem de Bruijn 		return false;
11944ab6c99dSWillem de Bruijn 
11954ab6c99dSWillem de Bruijn 	if (lo != old_hi + 1)
11964ab6c99dSWillem de Bruijn 		return false;
11974ab6c99dSWillem de Bruijn 
11984ab6c99dSWillem de Bruijn 	serr->ee.ee_data += len;
11994ab6c99dSWillem de Bruijn 	return true;
12004ab6c99dSWillem de Bruijn }
12014ab6c99dSWillem de Bruijn 
120252267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
120352267790SWillem de Bruijn {
12044ab6c99dSWillem de Bruijn 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
120552267790SWillem de Bruijn 	struct sock_exterr_skb *serr;
120652267790SWillem de Bruijn 	struct sock *sk = skb->sk;
12074ab6c99dSWillem de Bruijn 	struct sk_buff_head *q;
12084ab6c99dSWillem de Bruijn 	unsigned long flags;
12094ab6c99dSWillem de Bruijn 	u32 lo, hi;
12104ab6c99dSWillem de Bruijn 	u16 len;
121152267790SWillem de Bruijn 
1212ccaffff1SWillem de Bruijn 	mm_unaccount_pinned_pages(&uarg->mmp);
1213ccaffff1SWillem de Bruijn 
12144ab6c99dSWillem de Bruijn 	/* if !len, there was only 1 call, and it was aborted
12154ab6c99dSWillem de Bruijn 	 * so do not queue a completion notification
12164ab6c99dSWillem de Bruijn 	 */
12174ab6c99dSWillem de Bruijn 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
121852267790SWillem de Bruijn 		goto release;
121952267790SWillem de Bruijn 
12204ab6c99dSWillem de Bruijn 	len = uarg->len;
12214ab6c99dSWillem de Bruijn 	lo = uarg->id;
12224ab6c99dSWillem de Bruijn 	hi = uarg->id + len - 1;
12234ab6c99dSWillem de Bruijn 
122452267790SWillem de Bruijn 	serr = SKB_EXT_ERR(skb);
122552267790SWillem de Bruijn 	memset(serr, 0, sizeof(*serr));
122652267790SWillem de Bruijn 	serr->ee.ee_errno = 0;
122752267790SWillem de Bruijn 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
12284ab6c99dSWillem de Bruijn 	serr->ee.ee_data = hi;
12294ab6c99dSWillem de Bruijn 	serr->ee.ee_info = lo;
123052267790SWillem de Bruijn 	if (!success)
123152267790SWillem de Bruijn 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
123252267790SWillem de Bruijn 
12334ab6c99dSWillem de Bruijn 	q = &sk->sk_error_queue;
12344ab6c99dSWillem de Bruijn 	spin_lock_irqsave(&q->lock, flags);
12354ab6c99dSWillem de Bruijn 	tail = skb_peek_tail(q);
12364ab6c99dSWillem de Bruijn 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
12374ab6c99dSWillem de Bruijn 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
12384ab6c99dSWillem de Bruijn 		__skb_queue_tail(q, skb);
123952267790SWillem de Bruijn 		skb = NULL;
12404ab6c99dSWillem de Bruijn 	}
12414ab6c99dSWillem de Bruijn 	spin_unlock_irqrestore(&q->lock, flags);
124252267790SWillem de Bruijn 
124352267790SWillem de Bruijn 	sk->sk_error_report(sk);
124452267790SWillem de Bruijn 
124552267790SWillem de Bruijn release:
124652267790SWillem de Bruijn 	consume_skb(skb);
124752267790SWillem de Bruijn 	sock_put(sk);
124852267790SWillem de Bruijn }
124952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
125052267790SWillem de Bruijn 
125152267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg)
125252267790SWillem de Bruijn {
1253c1d1b437SEric Dumazet 	if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
125452267790SWillem de Bruijn 		if (uarg->callback)
125552267790SWillem de Bruijn 			uarg->callback(uarg, uarg->zerocopy);
125652267790SWillem de Bruijn 		else
125752267790SWillem de Bruijn 			consume_skb(skb_from_uarg(uarg));
125852267790SWillem de Bruijn 	}
125952267790SWillem de Bruijn }
126052267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put);
126152267790SWillem de Bruijn 
126252900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
126352267790SWillem de Bruijn {
126452267790SWillem de Bruijn 	if (uarg) {
126552267790SWillem de Bruijn 		struct sock *sk = skb_from_uarg(uarg)->sk;
126652267790SWillem de Bruijn 
126752267790SWillem de Bruijn 		atomic_dec(&sk->sk_zckey);
12684ab6c99dSWillem de Bruijn 		uarg->len--;
126952267790SWillem de Bruijn 
127052900d22SWillem de Bruijn 		if (have_uref)
127152267790SWillem de Bruijn 			sock_zerocopy_put(uarg);
127252267790SWillem de Bruijn 	}
127352267790SWillem de Bruijn }
127452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
127552267790SWillem de Bruijn 
1276b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1277b5947e5dSWillem de Bruijn {
1278b5947e5dSWillem de Bruijn 	return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1279b5947e5dSWillem de Bruijn }
1280b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1281b5947e5dSWillem de Bruijn 
128252267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
128352267790SWillem de Bruijn 			     struct msghdr *msg, int len,
128452267790SWillem de Bruijn 			     struct ubuf_info *uarg)
128552267790SWillem de Bruijn {
12864ab6c99dSWillem de Bruijn 	struct ubuf_info *orig_uarg = skb_zcopy(skb);
128752267790SWillem de Bruijn 	struct iov_iter orig_iter = msg->msg_iter;
128852267790SWillem de Bruijn 	int err, orig_len = skb->len;
128952267790SWillem de Bruijn 
12904ab6c99dSWillem de Bruijn 	/* An skb can only point to one uarg. This edge case happens when
12914ab6c99dSWillem de Bruijn 	 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
12924ab6c99dSWillem de Bruijn 	 */
12934ab6c99dSWillem de Bruijn 	if (orig_uarg && uarg != orig_uarg)
12944ab6c99dSWillem de Bruijn 		return -EEXIST;
12954ab6c99dSWillem de Bruijn 
129652267790SWillem de Bruijn 	err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
129752267790SWillem de Bruijn 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
129854d43117SWillem de Bruijn 		struct sock *save_sk = skb->sk;
129954d43117SWillem de Bruijn 
130052267790SWillem de Bruijn 		/* Streams do not free skb on error. Reset to prev state. */
130152267790SWillem de Bruijn 		msg->msg_iter = orig_iter;
130254d43117SWillem de Bruijn 		skb->sk = sk;
130352267790SWillem de Bruijn 		___pskb_trim(skb, orig_len);
130454d43117SWillem de Bruijn 		skb->sk = save_sk;
130552267790SWillem de Bruijn 		return err;
130652267790SWillem de Bruijn 	}
130752267790SWillem de Bruijn 
130852900d22SWillem de Bruijn 	skb_zcopy_set(skb, uarg, NULL);
130952267790SWillem de Bruijn 	return skb->len - orig_len;
131052267790SWillem de Bruijn }
131152267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
131252267790SWillem de Bruijn 
13131f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
131452267790SWillem de Bruijn 			      gfp_t gfp_mask)
131552267790SWillem de Bruijn {
131652267790SWillem de Bruijn 	if (skb_zcopy(orig)) {
131752267790SWillem de Bruijn 		if (skb_zcopy(nskb)) {
131852267790SWillem de Bruijn 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
131952267790SWillem de Bruijn 			if (!gfp_mask) {
132052267790SWillem de Bruijn 				WARN_ON_ONCE(1);
132152267790SWillem de Bruijn 				return -ENOMEM;
132252267790SWillem de Bruijn 			}
132352267790SWillem de Bruijn 			if (skb_uarg(nskb) == skb_uarg(orig))
132452267790SWillem de Bruijn 				return 0;
132552267790SWillem de Bruijn 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
132652267790SWillem de Bruijn 				return -EIO;
132752267790SWillem de Bruijn 		}
132852900d22SWillem de Bruijn 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
132952267790SWillem de Bruijn 	}
133052267790SWillem de Bruijn 	return 0;
133152267790SWillem de Bruijn }
133252267790SWillem de Bruijn 
13332c53040fSBen Hutchings /**
13342c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
133548c83012SMichael S. Tsirkin  *	@skb: the skb to modify
133648c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
133748c83012SMichael S. Tsirkin  *
133848c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
133948c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
134048c83012SMichael S. Tsirkin  *	to userspace pages.
134148c83012SMichael S. Tsirkin  *
134248c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
134348c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
134448c83012SMichael S. Tsirkin  *
134548c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
134648c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
134748c83012SMichael S. Tsirkin  */
134848c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1349a6686f2fSShirley Ma {
1350a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
1351a6686f2fSShirley Ma 	struct page *page, *head = NULL;
13523ece7826SWillem de Bruijn 	int i, new_frags;
13533ece7826SWillem de Bruijn 	u32 d_off;
1354a6686f2fSShirley Ma 
13553ece7826SWillem de Bruijn 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
13563ece7826SWillem de Bruijn 		return -EINVAL;
13573ece7826SWillem de Bruijn 
1358f72c4ac6SWillem de Bruijn 	if (!num_frags)
1359f72c4ac6SWillem de Bruijn 		goto release;
1360f72c4ac6SWillem de Bruijn 
13613ece7826SWillem de Bruijn 	new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
13623ece7826SWillem de Bruijn 	for (i = 0; i < new_frags; i++) {
136302756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
1364a6686f2fSShirley Ma 		if (!page) {
1365a6686f2fSShirley Ma 			while (head) {
136640dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
1367a6686f2fSShirley Ma 				put_page(head);
1368a6686f2fSShirley Ma 				head = next;
1369a6686f2fSShirley Ma 			}
1370a6686f2fSShirley Ma 			return -ENOMEM;
1371a6686f2fSShirley Ma 		}
13723ece7826SWillem de Bruijn 		set_page_private(page, (unsigned long)head);
13733ece7826SWillem de Bruijn 		head = page;
13743ece7826SWillem de Bruijn 	}
13753ece7826SWillem de Bruijn 
13763ece7826SWillem de Bruijn 	page = head;
13773ece7826SWillem de Bruijn 	d_off = 0;
13783ece7826SWillem de Bruijn 	for (i = 0; i < num_frags; i++) {
13793ece7826SWillem de Bruijn 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
13803ece7826SWillem de Bruijn 		u32 p_off, p_len, copied;
13813ece7826SWillem de Bruijn 		struct page *p;
13823ece7826SWillem de Bruijn 		u8 *vaddr;
1383c613c209SWillem de Bruijn 
1384b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1385c613c209SWillem de Bruijn 				      p, p_off, p_len, copied) {
13863ece7826SWillem de Bruijn 			u32 copy, done = 0;
1387c613c209SWillem de Bruijn 			vaddr = kmap_atomic(p);
13883ece7826SWillem de Bruijn 
13893ece7826SWillem de Bruijn 			while (done < p_len) {
13903ece7826SWillem de Bruijn 				if (d_off == PAGE_SIZE) {
13913ece7826SWillem de Bruijn 					d_off = 0;
13923ece7826SWillem de Bruijn 					page = (struct page *)page_private(page);
13933ece7826SWillem de Bruijn 				}
13943ece7826SWillem de Bruijn 				copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
13953ece7826SWillem de Bruijn 				memcpy(page_address(page) + d_off,
13963ece7826SWillem de Bruijn 				       vaddr + p_off + done, copy);
13973ece7826SWillem de Bruijn 				done += copy;
13983ece7826SWillem de Bruijn 				d_off += copy;
13993ece7826SWillem de Bruijn 			}
140051c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1401c613c209SWillem de Bruijn 		}
1402a6686f2fSShirley Ma 	}
1403a6686f2fSShirley Ma 
1404a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
140502756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
1406a8605c60SIan Campbell 		skb_frag_unref(skb, i);
1407a6686f2fSShirley Ma 
1408a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
14093ece7826SWillem de Bruijn 	for (i = 0; i < new_frags - 1; i++) {
14103ece7826SWillem de Bruijn 		__skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
141140dadff2SSunghan Suh 		head = (struct page *)page_private(head);
1412a6686f2fSShirley Ma 	}
14133ece7826SWillem de Bruijn 	__skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
14143ece7826SWillem de Bruijn 	skb_shinfo(skb)->nr_frags = new_frags;
141548c83012SMichael S. Tsirkin 
1416b90ddd56SWillem de Bruijn release:
14171f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, false);
1418a6686f2fSShirley Ma 	return 0;
1419a6686f2fSShirley Ma }
1420dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1421a6686f2fSShirley Ma 
1422e0053ec0SHerbert Xu /**
1423e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
1424e0053ec0SHerbert Xu  *	@skb: buffer to clone
1425e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
1426e0053ec0SHerbert Xu  *
1427e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1428e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
1429e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
1430e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
1431e0053ec0SHerbert Xu  *
1432e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
1433e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
1434e0053ec0SHerbert Xu  */
1435e0053ec0SHerbert Xu 
1436e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1437e0053ec0SHerbert Xu {
1438d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
1439d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
1440d0bf4a9eSEric Dumazet 						       skb1);
14416ffe75ebSEric Dumazet 	struct sk_buff *n;
1442e0053ec0SHerbert Xu 
144370008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
1444a6686f2fSShirley Ma 		return NULL;
1445a6686f2fSShirley Ma 
1446e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
14472638595aSReshetova, Elena 	    refcount_read(&fclones->fclone_ref) == 1) {
14486ffe75ebSEric Dumazet 		n = &fclones->skb2;
14492638595aSReshetova, Elena 		refcount_set(&fclones->fclone_ref, 2);
1450e0053ec0SHerbert Xu 	} else {
1451c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
1452c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
1453c93bdd0eSMel Gorman 
1454e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1455e0053ec0SHerbert Xu 		if (!n)
1456e0053ec0SHerbert Xu 			return NULL;
1457fe55f6d5SVegard Nossum 
1458e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1459e0053ec0SHerbert Xu 	}
1460e0053ec0SHerbert Xu 
1461e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
14621da177e4SLinus Torvalds }
1463b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
14641da177e4SLinus Torvalds 
1465b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off)
1466f5b17294SPravin B Shelar {
1467030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
1468030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1469030737bcSEric Dumazet 		skb->csum_start += off;
1470f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1471f5b17294SPravin B Shelar 	skb->transport_header += off;
1472f5b17294SPravin B Shelar 	skb->network_header   += off;
1473f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1474f5b17294SPravin B Shelar 		skb->mac_header += off;
1475f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1476f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1477aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1478f5b17294SPravin B Shelar }
1479b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update);
1480f5b17294SPravin B Shelar 
148108303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
14821da177e4SLinus Torvalds {
1483dec18810SHerbert Xu 	__copy_skb_header(new, old);
1484dec18810SHerbert Xu 
14857967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
14867967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
14877967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
14881da177e4SLinus Torvalds }
148908303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header);
14901da177e4SLinus Torvalds 
1491c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1492c93bdd0eSMel Gorman {
1493c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1494c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1495c93bdd0eSMel Gorman 	return 0;
1496c93bdd0eSMel Gorman }
1497c93bdd0eSMel Gorman 
14981da177e4SLinus Torvalds /**
14991da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
15001da177e4SLinus Torvalds  *	@skb: buffer to copy
15011da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
15021da177e4SLinus Torvalds  *
15031da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
15041da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
15051da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
15061da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
15071da177e4SLinus Torvalds  *
15081da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
15091da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
15101da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
15111da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
15121da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
15131da177e4SLinus Torvalds  */
15141da177e4SLinus Torvalds 
1515dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
15161da177e4SLinus Torvalds {
15176602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1518ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1519c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1520c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
15216602cebbSEric Dumazet 
15221da177e4SLinus Torvalds 	if (!n)
15231da177e4SLinus Torvalds 		return NULL;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	/* Set the data pointer */
15261da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
15271da177e4SLinus Torvalds 	/* Set the tail pointer and length */
15281da177e4SLinus Torvalds 	skb_put(n, skb->len);
15291da177e4SLinus Torvalds 
15309f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
15311da177e4SLinus Torvalds 
153208303c18SIlya Lesokhin 	skb_copy_header(n, skb);
15331da177e4SLinus Torvalds 	return n;
15341da177e4SLinus Torvalds }
1535b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds /**
1538bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
15391da177e4SLinus Torvalds  *	@skb: buffer to copy
1540117632e6SEric Dumazet  *	@headroom: headroom of new skb
15411da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1542bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1543bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1544bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
15451da177e4SLinus Torvalds  *
15461da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
15471da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
15481da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
15491da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
15501da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
15511da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
15521da177e4SLinus Torvalds  */
15531da177e4SLinus Torvalds 
1554bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1555bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
15561da177e4SLinus Torvalds {
1557117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1558bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1559bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
15606602cebbSEric Dumazet 
15611da177e4SLinus Torvalds 	if (!n)
15621da177e4SLinus Torvalds 		goto out;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	/* Set the data pointer */
1565117632e6SEric Dumazet 	skb_reserve(n, headroom);
15661da177e4SLinus Torvalds 	/* Set the tail pointer and length */
15671da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
15681da177e4SLinus Torvalds 	/* Copy the bytes */
1569d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
15701da177e4SLinus Torvalds 
157125f484a6SHerbert Xu 	n->truesize += skb->data_len;
15721da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
15731da177e4SLinus Torvalds 	n->len	     = skb->len;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
15761da177e4SLinus Torvalds 		int i;
15771da177e4SLinus Torvalds 
15781f8b977aSWillem de Bruijn 		if (skb_orphan_frags(skb, gfp_mask) ||
15791f8b977aSWillem de Bruijn 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
15801511022cSDan Carpenter 			kfree_skb(n);
15811511022cSDan Carpenter 			n = NULL;
1582a6686f2fSShirley Ma 			goto out;
1583a6686f2fSShirley Ma 		}
15841da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15851da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1586ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
15871da177e4SLinus Torvalds 		}
15881da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 
159121dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
15921da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
15931da177e4SLinus Torvalds 		skb_clone_fraglist(n);
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
159608303c18SIlya Lesokhin 	skb_copy_header(n, skb);
15971da177e4SLinus Torvalds out:
15981da177e4SLinus Torvalds 	return n;
15991da177e4SLinus Torvalds }
1600bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds /**
16031da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
16041da177e4SLinus Torvalds  *	@skb: buffer to reallocate
16051da177e4SLinus Torvalds  *	@nhead: room to add at head
16061da177e4SLinus Torvalds  *	@ntail: room to add at tail
16071da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
16081da177e4SLinus Torvalds  *
1609bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1610bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
16111da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
16121da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
16131da177e4SLinus Torvalds  *
16141da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
16151da177e4SLinus Torvalds  *	reloaded after call to this function.
16161da177e4SLinus Torvalds  */
16171da177e4SLinus Torvalds 
161886a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1619dd0fc66fSAl Viro 		     gfp_t gfp_mask)
16201da177e4SLinus Torvalds {
1621158f323bSEric Dumazet 	int i, osize = skb_end_offset(skb);
1622158f323bSEric Dumazet 	int size = osize + nhead + ntail;
16231da177e4SLinus Torvalds 	long off;
1624158f323bSEric Dumazet 	u8 *data;
16251da177e4SLinus Torvalds 
16264edd87adSHerbert Xu 	BUG_ON(nhead < 0);
16274edd87adSHerbert Xu 
16289f77fad3STim Hansen 	BUG_ON(skb_shared(skb));
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
16311da177e4SLinus Torvalds 
1632c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1633c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1634c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1635c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
16361da177e4SLinus Torvalds 	if (!data)
16371da177e4SLinus Torvalds 		goto nodata;
163887151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
16416602cebbSEric Dumazet 	 * optimized for the cases when header is void.
16426602cebbSEric Dumazet 	 */
16436602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
16446602cebbSEric Dumazet 
16456602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
16466602cebbSEric Dumazet 	       skb_shinfo(skb),
1647fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
16481da177e4SLinus Torvalds 
16493e24591aSAlexander Duyck 	/*
16503e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
16513e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
16523e24591aSAlexander Duyck 	 * be since all we did is relocate the values
16533e24591aSAlexander Duyck 	 */
16543e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
165570008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1656a6686f2fSShirley Ma 			goto nofrags;
16571f8b977aSWillem de Bruijn 		if (skb_zcopy(skb))
1658c1d1b437SEric Dumazet 			refcount_inc(&skb_uarg(skb)->refcnt);
16591da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1660ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
16611da177e4SLinus Torvalds 
166221dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
16631da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 		skb_release_data(skb);
16663e24591aSAlexander Duyck 	} else {
16673e24591aSAlexander Duyck 		skb_free_head(skb);
16681fd63041SEric Dumazet 	}
16691da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	skb->head     = data;
1672d3836f21SEric Dumazet 	skb->head_frag = 0;
16731da177e4SLinus Torvalds 	skb->data    += off;
16744305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
16754305b541SArnaldo Carvalho de Melo 	skb->end      = size;
167656eb8882SPatrick McHardy 	off           = nhead;
16774305b541SArnaldo Carvalho de Melo #else
16784305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
167956eb8882SPatrick McHardy #endif
168027a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1681b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
16821da177e4SLinus Torvalds 	skb->cloned   = 0;
1683334a8132SPatrick McHardy 	skb->hdr_len  = 0;
16841da177e4SLinus Torvalds 	skb->nohdr    = 0;
16851da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1686158f323bSEric Dumazet 
1687de8f3a83SDaniel Borkmann 	skb_metadata_clear(skb);
1688de8f3a83SDaniel Borkmann 
1689158f323bSEric Dumazet 	/* It is not generally safe to change skb->truesize.
1690158f323bSEric Dumazet 	 * For the moment, we really care of rx path, or
1691158f323bSEric Dumazet 	 * when skb is orphaned (not attached to a socket).
1692158f323bSEric Dumazet 	 */
1693158f323bSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
1694158f323bSEric Dumazet 		skb->truesize += size - osize;
1695158f323bSEric Dumazet 
16961da177e4SLinus Torvalds 	return 0;
16971da177e4SLinus Torvalds 
1698a6686f2fSShirley Ma nofrags:
1699a6686f2fSShirley Ma 	kfree(data);
17001da177e4SLinus Torvalds nodata:
17011da177e4SLinus Torvalds 	return -ENOMEM;
17021da177e4SLinus Torvalds }
1703b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
17081da177e4SLinus Torvalds {
17091da177e4SLinus Torvalds 	struct sk_buff *skb2;
17101da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (delta <= 0)
17131da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
17141da177e4SLinus Torvalds 	else {
17151da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
17161da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
17171da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
17181da177e4SLinus Torvalds 			kfree_skb(skb2);
17191da177e4SLinus Torvalds 			skb2 = NULL;
17201da177e4SLinus Torvalds 		}
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 	return skb2;
17231da177e4SLinus Torvalds }
1724b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds /**
17271da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
17281da177e4SLinus Torvalds  *	@skb: buffer to copy
17291da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
17301da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
17311da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
17321da177e4SLinus Torvalds  *
17331da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
17341da177e4SLinus Torvalds  *	allocate additional space.
17351da177e4SLinus Torvalds  *
17361da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
17371da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
17381da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
17391da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
17401da177e4SLinus Torvalds  *
17411da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
17421da177e4SLinus Torvalds  *	is called from an interrupt.
17431da177e4SLinus Torvalds  */
17441da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
174586a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1746dd0fc66fSAl Viro 				gfp_t gfp_mask)
17471da177e4SLinus Torvalds {
17481da177e4SLinus Torvalds 	/*
17491da177e4SLinus Torvalds 	 *	Allocate the copy buffer
17501da177e4SLinus Torvalds 	 */
1751c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1752c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1753c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1754efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
17551da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	if (!n)
17581da177e4SLinus Torvalds 		return NULL;
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	/* Set the tail pointer and length */
17631da177e4SLinus Torvalds 	skb_put(n, skb->len);
17641da177e4SLinus Torvalds 
1765efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
17661da177e4SLinus Torvalds 	head_copy_off = 0;
17671da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
17681da177e4SLinus Torvalds 		head_copy_len = newheadroom;
17691da177e4SLinus Torvalds 	else
17701da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	/* Copy the linear header and data. */
17739f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
17749f77fad3STim Hansen 			     skb->len + head_copy_len));
17751da177e4SLinus Torvalds 
177608303c18SIlya Lesokhin 	skb_copy_header(n, skb);
17771da177e4SLinus Torvalds 
1778030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1779efd1e8d5SPatrick McHardy 
17801da177e4SLinus Torvalds 	return n;
17811da177e4SLinus Torvalds }
1782b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /**
1785cd0a137aSFlorian Fainelli  *	__skb_pad		-	zero pad the tail of an skb
17861da177e4SLinus Torvalds  *	@skb: buffer to pad
17871da177e4SLinus Torvalds  *	@pad: space to pad
1788cd0a137aSFlorian Fainelli  *	@free_on_error: free buffer on error
17891da177e4SLinus Torvalds  *
17901da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
17911da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
17921da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
17931da177e4SLinus Torvalds  *
1794cd0a137aSFlorian Fainelli  *	May return error in out of memory cases. The skb is freed on error
1795cd0a137aSFlorian Fainelli  *	if @free_on_error is true.
17961da177e4SLinus Torvalds  */
17971da177e4SLinus Torvalds 
1798cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
17991da177e4SLinus Torvalds {
18005b057c6bSHerbert Xu 	int err;
18015b057c6bSHerbert Xu 	int ntail;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
18045b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
18051da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
18065b057c6bSHerbert Xu 		return 0;
18071da177e4SLinus Torvalds 	}
18081da177e4SLinus Torvalds 
18094305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
18105b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
18115b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
18125b057c6bSHerbert Xu 		if (unlikely(err))
18135b057c6bSHerbert Xu 			goto free_skb;
18145b057c6bSHerbert Xu 	}
18155b057c6bSHerbert Xu 
18165b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
18175b057c6bSHerbert Xu 	 * to be audited.
18185b057c6bSHerbert Xu 	 */
18195b057c6bSHerbert Xu 	err = skb_linearize(skb);
18205b057c6bSHerbert Xu 	if (unlikely(err))
18215b057c6bSHerbert Xu 		goto free_skb;
18225b057c6bSHerbert Xu 
18235b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
18245b057c6bSHerbert Xu 	return 0;
18255b057c6bSHerbert Xu 
18265b057c6bSHerbert Xu free_skb:
1827cd0a137aSFlorian Fainelli 	if (free_on_error)
18281da177e4SLinus Torvalds 		kfree_skb(skb);
18295b057c6bSHerbert Xu 	return err;
18301da177e4SLinus Torvalds }
1831cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad);
18321da177e4SLinus Torvalds 
18330dde3e16SIlpo Järvinen /**
18340c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
18350c7ddf36SMathias Krause  *	@skb: start of the buffer to use
18360c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
18370c7ddf36SMathias Krause  *	@len: amount of data to add
18380c7ddf36SMathias Krause  *
18390c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
18400c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
18410c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
18420c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
18430c7ddf36SMathias Krause  *	returned.
18440c7ddf36SMathias Krause  */
18450c7ddf36SMathias Krause 
18464df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
18470c7ddf36SMathias Krause {
18480c7ddf36SMathias Krause 	if (tail != skb) {
18490c7ddf36SMathias Krause 		skb->data_len += len;
18500c7ddf36SMathias Krause 		skb->len += len;
18510c7ddf36SMathias Krause 	}
18520c7ddf36SMathias Krause 	return skb_put(tail, len);
18530c7ddf36SMathias Krause }
18540c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
18550c7ddf36SMathias Krause 
18560c7ddf36SMathias Krause /**
18570dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
18580dde3e16SIlpo Järvinen  *	@skb: buffer to use
18590dde3e16SIlpo Järvinen  *	@len: amount of data to add
18600dde3e16SIlpo Järvinen  *
18610dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
18620dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
18630dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
18640dde3e16SIlpo Järvinen  */
18654df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len)
18660dde3e16SIlpo Järvinen {
18674df864c1SJohannes Berg 	void *tmp = skb_tail_pointer(skb);
18680dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
18690dde3e16SIlpo Järvinen 	skb->tail += len;
18700dde3e16SIlpo Järvinen 	skb->len  += len;
18710dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
18720dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
18730dde3e16SIlpo Järvinen 	return tmp;
18740dde3e16SIlpo Järvinen }
18750dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
18760dde3e16SIlpo Järvinen 
18776be8ac2fSIlpo Järvinen /**
1878c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1879c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1880c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1881c2aa270aSIlpo Järvinen  *
1882c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1883c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1884c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1885c2aa270aSIlpo Järvinen  */
1886d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len)
1887c2aa270aSIlpo Järvinen {
1888c2aa270aSIlpo Järvinen 	skb->data -= len;
1889c2aa270aSIlpo Järvinen 	skb->len  += len;
1890c2aa270aSIlpo Järvinen 	if (unlikely(skb->data < skb->head))
1891c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1892c2aa270aSIlpo Järvinen 	return skb->data;
1893c2aa270aSIlpo Järvinen }
1894c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1895c2aa270aSIlpo Järvinen 
1896c2aa270aSIlpo Järvinen /**
18976be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
18986be8ac2fSIlpo Järvinen  *	@skb: buffer to use
18996be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
19006be8ac2fSIlpo Järvinen  *
19016be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
19026be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
19036be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
19046be8ac2fSIlpo Järvinen  *	the old data.
19056be8ac2fSIlpo Järvinen  */
1906af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len)
19076be8ac2fSIlpo Järvinen {
190847d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
19096be8ac2fSIlpo Järvinen }
19106be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
19116be8ac2fSIlpo Järvinen 
1912419ae74eSIlpo Järvinen /**
1913419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1914419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1915419ae74eSIlpo Järvinen  *	@len: new length
1916419ae74eSIlpo Järvinen  *
1917419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1918419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1919419ae74eSIlpo Järvinen  *	The skb must be linear.
1920419ae74eSIlpo Järvinen  */
1921419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1922419ae74eSIlpo Järvinen {
1923419ae74eSIlpo Järvinen 	if (skb->len > len)
1924419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1925419ae74eSIlpo Järvinen }
1926419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1927419ae74eSIlpo Järvinen 
19283cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
19291da177e4SLinus Torvalds  */
19301da177e4SLinus Torvalds 
19313cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
19321da177e4SLinus Torvalds {
193327b437c8SHerbert Xu 	struct sk_buff **fragp;
193427b437c8SHerbert Xu 	struct sk_buff *frag;
19351da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
19361da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
19371da177e4SLinus Torvalds 	int i;
193827b437c8SHerbert Xu 	int err;
193927b437c8SHerbert Xu 
194027b437c8SHerbert Xu 	if (skb_cloned(skb) &&
194127b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
194227b437c8SHerbert Xu 		return err;
19431da177e4SLinus Torvalds 
1944f4d26fb3SHerbert Xu 	i = 0;
1945f4d26fb3SHerbert Xu 	if (offset >= len)
1946f4d26fb3SHerbert Xu 		goto drop_pages;
1947f4d26fb3SHerbert Xu 
1948f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
19499e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
195027b437c8SHerbert Xu 
195127b437c8SHerbert Xu 		if (end < len) {
19521da177e4SLinus Torvalds 			offset = end;
195327b437c8SHerbert Xu 			continue;
19541da177e4SLinus Torvalds 		}
19551da177e4SLinus Torvalds 
19569e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
195727b437c8SHerbert Xu 
1958f4d26fb3SHerbert Xu drop_pages:
195927b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
196027b437c8SHerbert Xu 
196127b437c8SHerbert Xu 		for (; i < nfrags; i++)
1962ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
196327b437c8SHerbert Xu 
196421dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
196527b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1966f4d26fb3SHerbert Xu 		goto done;
196727b437c8SHerbert Xu 	}
196827b437c8SHerbert Xu 
196927b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
197027b437c8SHerbert Xu 	     fragp = &frag->next) {
197127b437c8SHerbert Xu 		int end = offset + frag->len;
197227b437c8SHerbert Xu 
197327b437c8SHerbert Xu 		if (skb_shared(frag)) {
197427b437c8SHerbert Xu 			struct sk_buff *nfrag;
197527b437c8SHerbert Xu 
197627b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
197727b437c8SHerbert Xu 			if (unlikely(!nfrag))
197827b437c8SHerbert Xu 				return -ENOMEM;
197927b437c8SHerbert Xu 
198027b437c8SHerbert Xu 			nfrag->next = frag->next;
198185bb2a60SEric Dumazet 			consume_skb(frag);
198227b437c8SHerbert Xu 			frag = nfrag;
198327b437c8SHerbert Xu 			*fragp = frag;
198427b437c8SHerbert Xu 		}
198527b437c8SHerbert Xu 
198627b437c8SHerbert Xu 		if (end < len) {
198727b437c8SHerbert Xu 			offset = end;
198827b437c8SHerbert Xu 			continue;
198927b437c8SHerbert Xu 		}
199027b437c8SHerbert Xu 
199127b437c8SHerbert Xu 		if (end > len &&
199227b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
199327b437c8SHerbert Xu 			return err;
199427b437c8SHerbert Xu 
199527b437c8SHerbert Xu 		if (frag->next)
199627b437c8SHerbert Xu 			skb_drop_list(&frag->next);
199727b437c8SHerbert Xu 		break;
199827b437c8SHerbert Xu 	}
199927b437c8SHerbert Xu 
2000f4d26fb3SHerbert Xu done:
200127b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
20021da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
20031da177e4SLinus Torvalds 		skb->len       = len;
20041da177e4SLinus Torvalds 	} else {
20051da177e4SLinus Torvalds 		skb->len       = len;
20061da177e4SLinus Torvalds 		skb->data_len  = 0;
200727a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
20081da177e4SLinus Torvalds 	}
20091da177e4SLinus Torvalds 
2010c21b48ccSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
2011c21b48ccSEric Dumazet 		skb_condense(skb);
20121da177e4SLinus Torvalds 	return 0;
20131da177e4SLinus Torvalds }
2014b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
20151da177e4SLinus Torvalds 
201688078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly
201788078d98SEric Dumazet  */
201888078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
201988078d98SEric Dumazet {
202088078d98SEric Dumazet 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
202188078d98SEric Dumazet 		int delta = skb->len - len;
202288078d98SEric Dumazet 
2023d55bef50SDimitris Michailidis 		skb->csum = csum_block_sub(skb->csum,
2024d55bef50SDimitris Michailidis 					   skb_checksum(skb, len, delta, 0),
2025d55bef50SDimitris Michailidis 					   len);
202654970a2fSVasily Averin 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
202754970a2fSVasily Averin 		int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
202854970a2fSVasily Averin 		int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
202954970a2fSVasily Averin 
203054970a2fSVasily Averin 		if (offset + sizeof(__sum16) > hdlen)
203154970a2fSVasily Averin 			return -EINVAL;
203288078d98SEric Dumazet 	}
203388078d98SEric Dumazet 	return __pskb_trim(skb, len);
203488078d98SEric Dumazet }
203588078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow);
203688078d98SEric Dumazet 
20371da177e4SLinus Torvalds /**
20381da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
20391da177e4SLinus Torvalds  *	@skb: buffer to reallocate
20401da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
20411da177e4SLinus Torvalds  *
20421da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
20431da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
20441da177e4SLinus Torvalds  *	data from fragmented part.
20451da177e4SLinus Torvalds  *
20461da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
20471da177e4SLinus Torvalds  *
20481da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
20491da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
20501da177e4SLinus Torvalds  *
20511da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
20521da177e4SLinus Torvalds  *	reloaded after call to this function.
20531da177e4SLinus Torvalds  */
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
20561da177e4SLinus Torvalds  * when it is necessary.
20571da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
20581da177e4SLinus Torvalds  * 2. It may change skb pointers.
20591da177e4SLinus Torvalds  *
20601da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
20611da177e4SLinus Torvalds  */
2062af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta)
20631da177e4SLinus Torvalds {
20641da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
20651da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
20661da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
20671da177e4SLinus Torvalds 	 */
20684305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
20711da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
20721da177e4SLinus Torvalds 				     GFP_ATOMIC))
20731da177e4SLinus Torvalds 			return NULL;
20741da177e4SLinus Torvalds 	}
20751da177e4SLinus Torvalds 
20769f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
20779f77fad3STim Hansen 			     skb_tail_pointer(skb), delta));
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
20801da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
20811da177e4SLinus Torvalds 	 */
208221dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
20831da177e4SLinus Torvalds 		goto pull_pages;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
20861da177e4SLinus Torvalds 	eat = delta;
20871da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20889e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
20899e903e08SEric Dumazet 
20909e903e08SEric Dumazet 		if (size >= eat)
20911da177e4SLinus Torvalds 			goto pull_pages;
20929e903e08SEric Dumazet 		eat -= size;
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
209609001b03SWenhua Shi 	 * Certainly, it is possible to add an offset to skb data,
20971da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
20981da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
20991da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
21001da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
21011da177e4SLinus Torvalds 	 */
21021da177e4SLinus Torvalds 	if (eat) {
21031da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
21041da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
21051da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 		do {
21081da177e4SLinus Torvalds 			if (list->len <= eat) {
21091da177e4SLinus Torvalds 				/* Eaten as whole. */
21101da177e4SLinus Torvalds 				eat -= list->len;
21111da177e4SLinus Torvalds 				list = list->next;
21121da177e4SLinus Torvalds 				insp = list;
21131da177e4SLinus Torvalds 			} else {
21141da177e4SLinus Torvalds 				/* Eaten partially. */
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 				if (skb_shared(list)) {
21171da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
21181da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
21191da177e4SLinus Torvalds 					if (!clone)
21201da177e4SLinus Torvalds 						return NULL;
21211da177e4SLinus Torvalds 					insp = list->next;
21221da177e4SLinus Torvalds 					list = clone;
21231da177e4SLinus Torvalds 				} else {
21241da177e4SLinus Torvalds 					/* This may be pulled without
21251da177e4SLinus Torvalds 					 * problems. */
21261da177e4SLinus Torvalds 					insp = list;
21271da177e4SLinus Torvalds 				}
21281da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
21291da177e4SLinus Torvalds 					kfree_skb(clone);
21301da177e4SLinus Torvalds 					return NULL;
21311da177e4SLinus Torvalds 				}
21321da177e4SLinus Torvalds 				break;
21331da177e4SLinus Torvalds 			}
21341da177e4SLinus Torvalds 		} while (eat);
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 		/* Free pulled out fragments. */
21371da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
21381da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
21391da177e4SLinus Torvalds 			kfree_skb(list);
21401da177e4SLinus Torvalds 		}
21411da177e4SLinus Torvalds 		/* And insert new clone at head. */
21421da177e4SLinus Torvalds 		if (clone) {
21431da177e4SLinus Torvalds 			clone->next = list;
21441da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
21451da177e4SLinus Torvalds 		}
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds pull_pages:
21501da177e4SLinus Torvalds 	eat = delta;
21511da177e4SLinus Torvalds 	k = 0;
21521da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
21539e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
21549e903e08SEric Dumazet 
21559e903e08SEric Dumazet 		if (size <= eat) {
2156ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
21579e903e08SEric Dumazet 			eat -= size;
21581da177e4SLinus Torvalds 		} else {
2159b54c9d5bSJonathan Lemon 			skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2160b54c9d5bSJonathan Lemon 
2161b54c9d5bSJonathan Lemon 			*frag = skb_shinfo(skb)->frags[i];
21621da177e4SLinus Torvalds 			if (eat) {
2163b54c9d5bSJonathan Lemon 				skb_frag_off_add(frag, eat);
2164b54c9d5bSJonathan Lemon 				skb_frag_size_sub(frag, eat);
21653ccc6c6fSlinzhang 				if (!i)
21663ccc6c6fSlinzhang 					goto end;
21671da177e4SLinus Torvalds 				eat = 0;
21681da177e4SLinus Torvalds 			}
21691da177e4SLinus Torvalds 			k++;
21701da177e4SLinus Torvalds 		}
21711da177e4SLinus Torvalds 	}
21721da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
21731da177e4SLinus Torvalds 
21743ccc6c6fSlinzhang end:
21751da177e4SLinus Torvalds 	skb->tail     += delta;
21761da177e4SLinus Torvalds 	skb->data_len -= delta;
21771da177e4SLinus Torvalds 
21781f8b977aSWillem de Bruijn 	if (!skb->data_len)
21791f8b977aSWillem de Bruijn 		skb_zcopy_clear(skb, false);
21801f8b977aSWillem de Bruijn 
218127a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
21821da177e4SLinus Torvalds }
2183b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
21841da177e4SLinus Torvalds 
218522019b17SEric Dumazet /**
218622019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
218722019b17SEric Dumazet  *	@skb: source skb
218822019b17SEric Dumazet  *	@offset: offset in source
218922019b17SEric Dumazet  *	@to: destination buffer
219022019b17SEric Dumazet  *	@len: number of bytes to copy
219122019b17SEric Dumazet  *
219222019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
219322019b17SEric Dumazet  *	destination buffer.
219422019b17SEric Dumazet  *
219522019b17SEric Dumazet  *	CAUTION ! :
219622019b17SEric Dumazet  *		If its prototype is ever changed,
219722019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
219822019b17SEric Dumazet  *		since it is called from BPF assembly code.
219922019b17SEric Dumazet  */
22001da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
22011da177e4SLinus Torvalds {
22021a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2203fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2204fbb398a8SDavid S. Miller 	int i, copy;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
22071da177e4SLinus Torvalds 		goto fault;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	/* Copy header. */
22101a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
22111da177e4SLinus Torvalds 		if (copy > len)
22121da177e4SLinus Torvalds 			copy = len;
2213d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
22141da177e4SLinus Torvalds 		if ((len -= copy) == 0)
22151da177e4SLinus Torvalds 			return 0;
22161da177e4SLinus Torvalds 		offset += copy;
22171da177e4SLinus Torvalds 		to     += copy;
22181da177e4SLinus Torvalds 	}
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
22211a028e50SDavid S. Miller 		int end;
222251c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
22231da177e4SLinus Torvalds 
2224547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
22251a028e50SDavid S. Miller 
222651c56b00SEric Dumazet 		end = start + skb_frag_size(f);
22271da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2228c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2229c613c209SWillem de Bruijn 			struct page *p;
22301da177e4SLinus Torvalds 			u8 *vaddr;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 			if (copy > len)
22331da177e4SLinus Torvalds 				copy = len;
22341da177e4SLinus Torvalds 
2235c613c209SWillem de Bruijn 			skb_frag_foreach_page(f,
2236b54c9d5bSJonathan Lemon 					      skb_frag_off(f) + offset - start,
2237c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2238c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2239c613c209SWillem de Bruijn 				memcpy(to + copied, vaddr + p_off, p_len);
224051c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2241c613c209SWillem de Bruijn 			}
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22441da177e4SLinus Torvalds 				return 0;
22451da177e4SLinus Torvalds 			offset += copy;
22461da177e4SLinus Torvalds 			to     += copy;
22471da177e4SLinus Torvalds 		}
22481a028e50SDavid S. Miller 		start = end;
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds 
2251fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
22521a028e50SDavid S. Miller 		int end;
22531da177e4SLinus Torvalds 
2254547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
22551a028e50SDavid S. Miller 
2256fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
22571da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
22581da177e4SLinus Torvalds 			if (copy > len)
22591da177e4SLinus Torvalds 				copy = len;
2260fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
22611da177e4SLinus Torvalds 				goto fault;
22621da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22631da177e4SLinus Torvalds 				return 0;
22641da177e4SLinus Torvalds 			offset += copy;
22651da177e4SLinus Torvalds 			to     += copy;
22661da177e4SLinus Torvalds 		}
22671a028e50SDavid S. Miller 		start = end;
22681da177e4SLinus Torvalds 	}
2269a6686f2fSShirley Ma 
22701da177e4SLinus Torvalds 	if (!len)
22711da177e4SLinus Torvalds 		return 0;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds fault:
22741da177e4SLinus Torvalds 	return -EFAULT;
22751da177e4SLinus Torvalds }
2276b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
22771da177e4SLinus Torvalds 
22789c55e01cSJens Axboe /*
22799c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
22809c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
22819c55e01cSJens Axboe  */
22829c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
22839c55e01cSJens Axboe {
22848b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
22858b9d3728SJarek Poplawski }
22869c55e01cSJens Axboe 
2287a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
22884fb66994SJarek Poplawski 				   unsigned int *offset,
228918aafc62SEric Dumazet 				   struct sock *sk)
22908b9d3728SJarek Poplawski {
22915640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
22928b9d3728SJarek Poplawski 
22935640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
22948b9d3728SJarek Poplawski 		return NULL;
22954fb66994SJarek Poplawski 
22965640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
22974fb66994SJarek Poplawski 
22985640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
22995640f768SEric Dumazet 	       page_address(page) + *offset, *len);
23005640f768SEric Dumazet 	*offset = pfrag->offset;
23015640f768SEric Dumazet 	pfrag->offset += *len;
23024fb66994SJarek Poplawski 
23035640f768SEric Dumazet 	return pfrag->page;
23049c55e01cSJens Axboe }
23059c55e01cSJens Axboe 
230641c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
230741c73a0dSEric Dumazet 			     struct page *page,
230841c73a0dSEric Dumazet 			     unsigned int offset)
230941c73a0dSEric Dumazet {
231041c73a0dSEric Dumazet 	return	spd->nr_pages &&
231141c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
231241c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
231341c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
231441c73a0dSEric Dumazet }
231541c73a0dSEric Dumazet 
23169c55e01cSJens Axboe /*
23179c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
23189c55e01cSJens Axboe  */
2319a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
232035f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
23214fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
232218aafc62SEric Dumazet 			  bool linear,
23237a67e56fSJarek Poplawski 			  struct sock *sk)
23249c55e01cSJens Axboe {
232541c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2326a108d5f3SDavid S. Miller 		return true;
23279c55e01cSJens Axboe 
23288b9d3728SJarek Poplawski 	if (linear) {
232918aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
23308b9d3728SJarek Poplawski 		if (!page)
2331a108d5f3SDavid S. Miller 			return true;
233241c73a0dSEric Dumazet 	}
233341c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
233441c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
2335a108d5f3SDavid S. Miller 		return false;
233641c73a0dSEric Dumazet 	}
23378b9d3728SJarek Poplawski 	get_page(page);
23389c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
23394fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
23409c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
23419c55e01cSJens Axboe 	spd->nr_pages++;
23428b9d3728SJarek Poplawski 
2343a108d5f3SDavid S. Miller 	return false;
23449c55e01cSJens Axboe }
23459c55e01cSJens Axboe 
2346a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
23472870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
234818aafc62SEric Dumazet 			     unsigned int *len,
2349d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
235035f3d14dSJens Axboe 			     struct sock *sk,
235135f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
23529c55e01cSJens Axboe {
23532870c43dSOctavian Purdila 	if (!*len)
2354a108d5f3SDavid S. Miller 		return true;
23559c55e01cSJens Axboe 
23562870c43dSOctavian Purdila 	/* skip this segment if already processed */
23572870c43dSOctavian Purdila 	if (*off >= plen) {
23582870c43dSOctavian Purdila 		*off -= plen;
2359a108d5f3SDavid S. Miller 		return false;
23602870c43dSOctavian Purdila 	}
23612870c43dSOctavian Purdila 
23622870c43dSOctavian Purdila 	/* ignore any bits we already processed */
23639ca1b22dSEric Dumazet 	poff += *off;
23649ca1b22dSEric Dumazet 	plen -= *off;
23652870c43dSOctavian Purdila 	*off = 0;
23662870c43dSOctavian Purdila 
236718aafc62SEric Dumazet 	do {
236818aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
23692870c43dSOctavian Purdila 
237018aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
237118aafc62SEric Dumazet 				  linear, sk))
2372a108d5f3SDavid S. Miller 			return true;
237318aafc62SEric Dumazet 		poff += flen;
237418aafc62SEric Dumazet 		plen -= flen;
23752870c43dSOctavian Purdila 		*len -= flen;
237618aafc62SEric Dumazet 	} while (*len && plen);
23772870c43dSOctavian Purdila 
2378a108d5f3SDavid S. Miller 	return false;
2379db43a282SOctavian Purdila }
23809c55e01cSJens Axboe 
23819c55e01cSJens Axboe /*
2382a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
23832870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
23849c55e01cSJens Axboe  */
2385a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
238635f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
238735f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
23882870c43dSOctavian Purdila {
23892870c43dSOctavian Purdila 	int seg;
2390fa9835e5STom Herbert 	struct sk_buff *iter;
23919c55e01cSJens Axboe 
23921d0c0b32SEric Dumazet 	/* map the linear part :
23932996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
23942996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
23952996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
23969c55e01cSJens Axboe 	 */
23972870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
23982870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
23992870c43dSOctavian Purdila 			     skb_headlen(skb),
240018aafc62SEric Dumazet 			     offset, len, spd,
24013a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
24021d0c0b32SEric Dumazet 			     sk, pipe))
2403a108d5f3SDavid S. Miller 		return true;
24049c55e01cSJens Axboe 
24059c55e01cSJens Axboe 	/*
24069c55e01cSJens Axboe 	 * then map the fragments
24079c55e01cSJens Axboe 	 */
24089c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
24099c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
24109c55e01cSJens Axboe 
2411ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
2412b54c9d5bSJonathan Lemon 				     skb_frag_off(f), skb_frag_size(f),
241318aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
2414a108d5f3SDavid S. Miller 			return true;
24159c55e01cSJens Axboe 	}
24169c55e01cSJens Axboe 
2417fa9835e5STom Herbert 	skb_walk_frags(skb, iter) {
2418fa9835e5STom Herbert 		if (*offset >= iter->len) {
2419fa9835e5STom Herbert 			*offset -= iter->len;
2420fa9835e5STom Herbert 			continue;
2421fa9835e5STom Herbert 		}
2422fa9835e5STom Herbert 		/* __skb_splice_bits() only fails if the output has no room
2423fa9835e5STom Herbert 		 * left, so no point in going over the frag_list for the error
2424fa9835e5STom Herbert 		 * case.
2425fa9835e5STom Herbert 		 */
2426fa9835e5STom Herbert 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2427fa9835e5STom Herbert 			return true;
2428fa9835e5STom Herbert 	}
2429fa9835e5STom Herbert 
2430a108d5f3SDavid S. Miller 	return false;
24319c55e01cSJens Axboe }
24329c55e01cSJens Axboe 
24339c55e01cSJens Axboe /*
24349c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
2435fa9835e5STom Herbert  * the fragments, and the frag list.
24369c55e01cSJens Axboe  */
2437a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
24389c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
243925869262SAl Viro 		    unsigned int flags)
24409c55e01cSJens Axboe {
244141c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
244241c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
24439c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
24449c55e01cSJens Axboe 		.pages = pages,
24459c55e01cSJens Axboe 		.partial = partial,
2446047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
244728a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
24489c55e01cSJens Axboe 		.spd_release = sock_spd_release,
24499c55e01cSJens Axboe 	};
245035f3d14dSJens Axboe 	int ret = 0;
245135f3d14dSJens Axboe 
2452fa9835e5STom Herbert 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
24539c55e01cSJens Axboe 
2454a60e3cc7SHannes Frederic Sowa 	if (spd.nr_pages)
245525869262SAl Viro 		ret = splice_to_pipe(pipe, &spd);
24569c55e01cSJens Axboe 
245735f3d14dSJens Axboe 	return ret;
24589c55e01cSJens Axboe }
24592b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
24609c55e01cSJens Axboe 
246120bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */
246220bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
246320bf50deSTom Herbert 			 int len)
246420bf50deSTom Herbert {
246520bf50deSTom Herbert 	unsigned int orig_len = len;
246620bf50deSTom Herbert 	struct sk_buff *head = skb;
246720bf50deSTom Herbert 	unsigned short fragidx;
246820bf50deSTom Herbert 	int slen, ret;
246920bf50deSTom Herbert 
247020bf50deSTom Herbert do_frag_list:
247120bf50deSTom Herbert 
247220bf50deSTom Herbert 	/* Deal with head data */
247320bf50deSTom Herbert 	while (offset < skb_headlen(skb) && len) {
247420bf50deSTom Herbert 		struct kvec kv;
247520bf50deSTom Herbert 		struct msghdr msg;
247620bf50deSTom Herbert 
247720bf50deSTom Herbert 		slen = min_t(int, len, skb_headlen(skb) - offset);
247820bf50deSTom Herbert 		kv.iov_base = skb->data + offset;
2479db5980d8SJohn Fastabend 		kv.iov_len = slen;
248020bf50deSTom Herbert 		memset(&msg, 0, sizeof(msg));
2481bd95e678SJohn Fastabend 		msg.msg_flags = MSG_DONTWAIT;
248220bf50deSTom Herbert 
248320bf50deSTom Herbert 		ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
248420bf50deSTom Herbert 		if (ret <= 0)
248520bf50deSTom Herbert 			goto error;
248620bf50deSTom Herbert 
248720bf50deSTom Herbert 		offset += ret;
248820bf50deSTom Herbert 		len -= ret;
248920bf50deSTom Herbert 	}
249020bf50deSTom Herbert 
249120bf50deSTom Herbert 	/* All the data was skb head? */
249220bf50deSTom Herbert 	if (!len)
249320bf50deSTom Herbert 		goto out;
249420bf50deSTom Herbert 
249520bf50deSTom Herbert 	/* Make offset relative to start of frags */
249620bf50deSTom Herbert 	offset -= skb_headlen(skb);
249720bf50deSTom Herbert 
249820bf50deSTom Herbert 	/* Find where we are in frag list */
249920bf50deSTom Herbert 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
250020bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
250120bf50deSTom Herbert 
2502d8e18a51SMatthew Wilcox (Oracle) 		if (offset < skb_frag_size(frag))
250320bf50deSTom Herbert 			break;
250420bf50deSTom Herbert 
2505d8e18a51SMatthew Wilcox (Oracle) 		offset -= skb_frag_size(frag);
250620bf50deSTom Herbert 	}
250720bf50deSTom Herbert 
250820bf50deSTom Herbert 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
250920bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
251020bf50deSTom Herbert 
2511d8e18a51SMatthew Wilcox (Oracle) 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
251220bf50deSTom Herbert 
251320bf50deSTom Herbert 		while (slen) {
2514d8e18a51SMatthew Wilcox (Oracle) 			ret = kernel_sendpage_locked(sk, skb_frag_page(frag),
2515b54c9d5bSJonathan Lemon 						     skb_frag_off(frag) + offset,
251620bf50deSTom Herbert 						     slen, MSG_DONTWAIT);
251720bf50deSTom Herbert 			if (ret <= 0)
251820bf50deSTom Herbert 				goto error;
251920bf50deSTom Herbert 
252020bf50deSTom Herbert 			len -= ret;
252120bf50deSTom Herbert 			offset += ret;
252220bf50deSTom Herbert 			slen -= ret;
252320bf50deSTom Herbert 		}
252420bf50deSTom Herbert 
252520bf50deSTom Herbert 		offset = 0;
252620bf50deSTom Herbert 	}
252720bf50deSTom Herbert 
252820bf50deSTom Herbert 	if (len) {
252920bf50deSTom Herbert 		/* Process any frag lists */
253020bf50deSTom Herbert 
253120bf50deSTom Herbert 		if (skb == head) {
253220bf50deSTom Herbert 			if (skb_has_frag_list(skb)) {
253320bf50deSTom Herbert 				skb = skb_shinfo(skb)->frag_list;
253420bf50deSTom Herbert 				goto do_frag_list;
253520bf50deSTom Herbert 			}
253620bf50deSTom Herbert 		} else if (skb->next) {
253720bf50deSTom Herbert 			skb = skb->next;
253820bf50deSTom Herbert 			goto do_frag_list;
253920bf50deSTom Herbert 		}
254020bf50deSTom Herbert 	}
254120bf50deSTom Herbert 
254220bf50deSTom Herbert out:
254320bf50deSTom Herbert 	return orig_len - len;
254420bf50deSTom Herbert 
254520bf50deSTom Herbert error:
254620bf50deSTom Herbert 	return orig_len == len ? ret : orig_len - len;
254720bf50deSTom Herbert }
254820bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked);
254920bf50deSTom Herbert 
2550357b40a1SHerbert Xu /**
2551357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
2552357b40a1SHerbert Xu  *	@skb: destination buffer
2553357b40a1SHerbert Xu  *	@offset: offset in destination
2554357b40a1SHerbert Xu  *	@from: source buffer
2555357b40a1SHerbert Xu  *	@len: number of bytes to copy
2556357b40a1SHerbert Xu  *
2557357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
2558357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
2559357b40a1SHerbert Xu  *	traversing fragment lists and such.
2560357b40a1SHerbert Xu  */
2561357b40a1SHerbert Xu 
25620c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2563357b40a1SHerbert Xu {
25641a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2565fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2566fbb398a8SDavid S. Miller 	int i, copy;
2567357b40a1SHerbert Xu 
2568357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
2569357b40a1SHerbert Xu 		goto fault;
2570357b40a1SHerbert Xu 
25711a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
2572357b40a1SHerbert Xu 		if (copy > len)
2573357b40a1SHerbert Xu 			copy = len;
257427d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2575357b40a1SHerbert Xu 		if ((len -= copy) == 0)
2576357b40a1SHerbert Xu 			return 0;
2577357b40a1SHerbert Xu 		offset += copy;
2578357b40a1SHerbert Xu 		from += copy;
2579357b40a1SHerbert Xu 	}
2580357b40a1SHerbert Xu 
2581357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2582357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
25831a028e50SDavid S. Miller 		int end;
2584357b40a1SHerbert Xu 
2585547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
25861a028e50SDavid S. Miller 
25879e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
2588357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2589c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2590c613c209SWillem de Bruijn 			struct page *p;
2591357b40a1SHerbert Xu 			u8 *vaddr;
2592357b40a1SHerbert Xu 
2593357b40a1SHerbert Xu 			if (copy > len)
2594357b40a1SHerbert Xu 				copy = len;
2595357b40a1SHerbert Xu 
2596c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2597b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2598c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2599c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2600c613c209SWillem de Bruijn 				memcpy(vaddr + p_off, from + copied, p_len);
260151c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2602c613c209SWillem de Bruijn 			}
2603357b40a1SHerbert Xu 
2604357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2605357b40a1SHerbert Xu 				return 0;
2606357b40a1SHerbert Xu 			offset += copy;
2607357b40a1SHerbert Xu 			from += copy;
2608357b40a1SHerbert Xu 		}
26091a028e50SDavid S. Miller 		start = end;
2610357b40a1SHerbert Xu 	}
2611357b40a1SHerbert Xu 
2612fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
26131a028e50SDavid S. Miller 		int end;
2614357b40a1SHerbert Xu 
2615547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26161a028e50SDavid S. Miller 
2617fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2618357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2619357b40a1SHerbert Xu 			if (copy > len)
2620357b40a1SHerbert Xu 				copy = len;
2621fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
26221a028e50SDavid S. Miller 					   from, copy))
2623357b40a1SHerbert Xu 				goto fault;
2624357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2625357b40a1SHerbert Xu 				return 0;
2626357b40a1SHerbert Xu 			offset += copy;
2627357b40a1SHerbert Xu 			from += copy;
2628357b40a1SHerbert Xu 		}
26291a028e50SDavid S. Miller 		start = end;
2630357b40a1SHerbert Xu 	}
2631357b40a1SHerbert Xu 	if (!len)
2632357b40a1SHerbert Xu 		return 0;
2633357b40a1SHerbert Xu 
2634357b40a1SHerbert Xu fault:
2635357b40a1SHerbert Xu 	return -EFAULT;
2636357b40a1SHerbert Xu }
2637357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
2638357b40a1SHerbert Xu 
26391da177e4SLinus Torvalds /* Checksum skb data. */
26402817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
26412817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
26421da177e4SLinus Torvalds {
26431a028e50SDavid S. Miller 	int start = skb_headlen(skb);
26441a028e50SDavid S. Miller 	int i, copy = start - offset;
2645fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
26461da177e4SLinus Torvalds 	int pos = 0;
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	/* Checksum header. */
26491da177e4SLinus Torvalds 	if (copy > 0) {
26501da177e4SLinus Torvalds 		if (copy > len)
26511da177e4SLinus Torvalds 			copy = len;
26522544af03SMatteo Croce 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
26532544af03SMatteo Croce 				       skb->data + offset, copy, csum);
26541da177e4SLinus Torvalds 		if ((len -= copy) == 0)
26551da177e4SLinus Torvalds 			return csum;
26561da177e4SLinus Torvalds 		offset += copy;
26571da177e4SLinus Torvalds 		pos	= copy;
26581da177e4SLinus Torvalds 	}
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26611a028e50SDavid S. Miller 		int end;
266251c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
26631da177e4SLinus Torvalds 
2664547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26651a028e50SDavid S. Miller 
266651c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
26671da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2668c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2669c613c209SWillem de Bruijn 			struct page *p;
267044bb9363SAl Viro 			__wsum csum2;
26711da177e4SLinus Torvalds 			u8 *vaddr;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 			if (copy > len)
26741da177e4SLinus Torvalds 				copy = len;
2675c613c209SWillem de Bruijn 
2676c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2677b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2678c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2679c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
26802544af03SMatteo Croce 				csum2 = INDIRECT_CALL_1(ops->update,
26812544af03SMatteo Croce 							csum_partial_ext,
26822544af03SMatteo Croce 							vaddr + p_off, p_len, 0);
268351c56b00SEric Dumazet 				kunmap_atomic(vaddr);
26842544af03SMatteo Croce 				csum = INDIRECT_CALL_1(ops->combine,
26852544af03SMatteo Croce 						       csum_block_add_ext, csum,
26862544af03SMatteo Croce 						       csum2, pos, p_len);
2687c613c209SWillem de Bruijn 				pos += p_len;
2688c613c209SWillem de Bruijn 			}
2689c613c209SWillem de Bruijn 
26901da177e4SLinus Torvalds 			if (!(len -= copy))
26911da177e4SLinus Torvalds 				return csum;
26921da177e4SLinus Torvalds 			offset += copy;
26931da177e4SLinus Torvalds 		}
26941a028e50SDavid S. Miller 		start = end;
26951da177e4SLinus Torvalds 	}
26961da177e4SLinus Torvalds 
2697fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
26981a028e50SDavid S. Miller 		int end;
26991da177e4SLinus Torvalds 
2700547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
27011a028e50SDavid S. Miller 
2702fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
27031da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
27045f92a738SAl Viro 			__wsum csum2;
27051da177e4SLinus Torvalds 			if (copy > len)
27061da177e4SLinus Torvalds 				copy = len;
27072817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
27082817a336SDaniel Borkmann 					       copy, 0, ops);
27092544af03SMatteo Croce 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
27102544af03SMatteo Croce 					       csum, csum2, pos, copy);
27111da177e4SLinus Torvalds 			if ((len -= copy) == 0)
27121da177e4SLinus Torvalds 				return csum;
27131da177e4SLinus Torvalds 			offset += copy;
27141da177e4SLinus Torvalds 			pos    += copy;
27151da177e4SLinus Torvalds 		}
27161a028e50SDavid S. Miller 		start = end;
27171da177e4SLinus Torvalds 	}
271809a62660SKris Katterjohn 	BUG_ON(len);
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	return csum;
27211da177e4SLinus Torvalds }
27222817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
27232817a336SDaniel Borkmann 
27242817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
27252817a336SDaniel Borkmann 		    int len, __wsum csum)
27262817a336SDaniel Borkmann {
27272817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2728cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
27292817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
27302817a336SDaniel Borkmann 	};
27312817a336SDaniel Borkmann 
27322817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
27332817a336SDaniel Borkmann }
2734b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds /* Both of above in one bottle. */
27371da177e4SLinus Torvalds 
273881d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
27398d5930dfSAl Viro 				    u8 *to, int len)
27401da177e4SLinus Torvalds {
27411a028e50SDavid S. Miller 	int start = skb_headlen(skb);
27421a028e50SDavid S. Miller 	int i, copy = start - offset;
2743fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
27441da177e4SLinus Torvalds 	int pos = 0;
27458d5930dfSAl Viro 	__wsum csum = 0;
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds 	/* Copy header. */
27481da177e4SLinus Torvalds 	if (copy > 0) {
27491da177e4SLinus Torvalds 		if (copy > len)
27501da177e4SLinus Torvalds 			copy = len;
27511da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
2752cc44c17bSAl Viro 						 copy);
27531da177e4SLinus Torvalds 		if ((len -= copy) == 0)
27541da177e4SLinus Torvalds 			return csum;
27551da177e4SLinus Torvalds 		offset += copy;
27561da177e4SLinus Torvalds 		to     += copy;
27571da177e4SLinus Torvalds 		pos	= copy;
27581da177e4SLinus Torvalds 	}
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27611a028e50SDavid S. Miller 		int end;
27621da177e4SLinus Torvalds 
2763547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
27641a028e50SDavid S. Miller 
27659e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
27661da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2767c613c209SWillem de Bruijn 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2768c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2769c613c209SWillem de Bruijn 			struct page *p;
27705084205fSAl Viro 			__wsum csum2;
27711da177e4SLinus Torvalds 			u8 *vaddr;
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 			if (copy > len)
27741da177e4SLinus Torvalds 				copy = len;
2775c613c209SWillem de Bruijn 
2776c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2777b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2778c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2779c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2780c613c209SWillem de Bruijn 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2781c613c209SWillem de Bruijn 								  to + copied,
2782cc44c17bSAl Viro 								  p_len);
278351c56b00SEric Dumazet 				kunmap_atomic(vaddr);
27841da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
2785c613c209SWillem de Bruijn 				pos += p_len;
2786c613c209SWillem de Bruijn 			}
2787c613c209SWillem de Bruijn 
27881da177e4SLinus Torvalds 			if (!(len -= copy))
27891da177e4SLinus Torvalds 				return csum;
27901da177e4SLinus Torvalds 			offset += copy;
27911da177e4SLinus Torvalds 			to     += copy;
27921da177e4SLinus Torvalds 		}
27931a028e50SDavid S. Miller 		start = end;
27941da177e4SLinus Torvalds 	}
27951da177e4SLinus Torvalds 
2796fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
279781d77662SAl Viro 		__wsum csum2;
27981a028e50SDavid S. Miller 		int end;
27991da177e4SLinus Torvalds 
2800547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28011a028e50SDavid S. Miller 
2802fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
28031da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
28041da177e4SLinus Torvalds 			if (copy > len)
28051da177e4SLinus Torvalds 				copy = len;
2806fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
28071a028e50SDavid S. Miller 						       offset - start,
28088d5930dfSAl Viro 						       to, copy);
28091da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
28101da177e4SLinus Torvalds 			if ((len -= copy) == 0)
28111da177e4SLinus Torvalds 				return csum;
28121da177e4SLinus Torvalds 			offset += copy;
28131da177e4SLinus Torvalds 			to     += copy;
28141da177e4SLinus Torvalds 			pos    += copy;
28151da177e4SLinus Torvalds 		}
28161a028e50SDavid S. Miller 		start = end;
28171da177e4SLinus Torvalds 	}
281809a62660SKris Katterjohn 	BUG_ON(len);
28191da177e4SLinus Torvalds 	return csum;
28201da177e4SLinus Torvalds }
2821b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
28221da177e4SLinus Torvalds 
282349f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
282449f8e832SCong Wang {
282549f8e832SCong Wang 	__sum16 sum;
282649f8e832SCong Wang 
282749f8e832SCong Wang 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
282814641931SCong Wang 	/* See comments in __skb_checksum_complete(). */
282949f8e832SCong Wang 	if (likely(!sum)) {
283049f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
283149f8e832SCong Wang 		    !skb->csum_complete_sw)
28327fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
283349f8e832SCong Wang 	}
283449f8e832SCong Wang 	if (!skb_shared(skb))
283549f8e832SCong Wang 		skb->csum_valid = !sum;
283649f8e832SCong Wang 	return sum;
283749f8e832SCong Wang }
283849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head);
283949f8e832SCong Wang 
284014641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum,
284114641931SCong Wang  * which has been changed from the hardware checksum, for example, by
284214641931SCong Wang  * __skb_checksum_validate_complete(). And, the original skb->csum must
284314641931SCong Wang  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
284414641931SCong Wang  *
284514641931SCong Wang  * It returns non-zero if the recomputed checksum is still invalid, otherwise
284614641931SCong Wang  * zero. The new checksum is stored back into skb->csum unless the skb is
284714641931SCong Wang  * shared.
284814641931SCong Wang  */
284949f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb)
285049f8e832SCong Wang {
285149f8e832SCong Wang 	__wsum csum;
285249f8e832SCong Wang 	__sum16 sum;
285349f8e832SCong Wang 
285449f8e832SCong Wang 	csum = skb_checksum(skb, 0, skb->len, 0);
285549f8e832SCong Wang 
285649f8e832SCong Wang 	sum = csum_fold(csum_add(skb->csum, csum));
285714641931SCong Wang 	/* This check is inverted, because we already knew the hardware
285814641931SCong Wang 	 * checksum is invalid before calling this function. So, if the
285914641931SCong Wang 	 * re-computed checksum is valid instead, then we have a mismatch
286014641931SCong Wang 	 * between the original skb->csum and skb_checksum(). This means either
286114641931SCong Wang 	 * the original hardware checksum is incorrect or we screw up skb->csum
286214641931SCong Wang 	 * when moving skb->data around.
286314641931SCong Wang 	 */
286449f8e832SCong Wang 	if (likely(!sum)) {
286549f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
286649f8e832SCong Wang 		    !skb->csum_complete_sw)
28677fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
286849f8e832SCong Wang 	}
286949f8e832SCong Wang 
287049f8e832SCong Wang 	if (!skb_shared(skb)) {
287149f8e832SCong Wang 		/* Save full packet checksum */
287249f8e832SCong Wang 		skb->csum = csum;
287349f8e832SCong Wang 		skb->ip_summed = CHECKSUM_COMPLETE;
287449f8e832SCong Wang 		skb->csum_complete_sw = 1;
287549f8e832SCong Wang 		skb->csum_valid = !sum;
287649f8e832SCong Wang 	}
287749f8e832SCong Wang 
287849f8e832SCong Wang 	return sum;
287949f8e832SCong Wang }
288049f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete);
288149f8e832SCong Wang 
28829617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
28839617813dSDavide Caratti {
28849617813dSDavide Caratti 	net_warn_ratelimited(
28859617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
28869617813dSDavide Caratti 		__func__);
28879617813dSDavide Caratti 	return 0;
28889617813dSDavide Caratti }
28899617813dSDavide Caratti 
28909617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
28919617813dSDavide Caratti 				       int offset, int len)
28929617813dSDavide Caratti {
28939617813dSDavide Caratti 	net_warn_ratelimited(
28949617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
28959617813dSDavide Caratti 		__func__);
28969617813dSDavide Caratti 	return 0;
28979617813dSDavide Caratti }
28989617813dSDavide Caratti 
28999617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = {
29009617813dSDavide Caratti 	.update  = warn_crc32c_csum_update,
29019617813dSDavide Caratti 	.combine = warn_crc32c_csum_combine,
29029617813dSDavide Caratti };
29039617813dSDavide Caratti 
29049617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
29059617813dSDavide Caratti 	&default_crc32c_ops;
29069617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub);
29079617813dSDavide Caratti 
2908af2806f8SThomas Graf  /**
2909af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2910af2806f8SThomas Graf  *	@from: source buffer
2911af2806f8SThomas Graf  *
2912af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2913af2806f8SThomas Graf  *	into skb_zerocopy().
2914af2806f8SThomas Graf  */
2915af2806f8SThomas Graf unsigned int
2916af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2917af2806f8SThomas Graf {
2918af2806f8SThomas Graf 	unsigned int hlen = 0;
2919af2806f8SThomas Graf 
2920af2806f8SThomas Graf 	if (!from->head_frag ||
2921af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2922af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2923af2806f8SThomas Graf 		hlen = skb_headlen(from);
2924af2806f8SThomas Graf 
2925af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2926af2806f8SThomas Graf 		hlen = from->len;
2927af2806f8SThomas Graf 
2928af2806f8SThomas Graf 	return hlen;
2929af2806f8SThomas Graf }
2930af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2931af2806f8SThomas Graf 
2932af2806f8SThomas Graf /**
2933af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2934af2806f8SThomas Graf  *	@to: destination buffer
29357fceb4deSMasanari Iida  *	@from: source buffer
2936af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2937af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2938af2806f8SThomas Graf  *
2939af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2940af2806f8SThomas Graf  *	to the frags in the source buffer.
2941af2806f8SThomas Graf  *
2942af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2943af2806f8SThomas Graf  *	headroom in the `to` buffer.
294436d5fe6aSZoltan Kiss  *
294536d5fe6aSZoltan Kiss  *	Return value:
294636d5fe6aSZoltan Kiss  *	0: everything is OK
294736d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
294836d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2949af2806f8SThomas Graf  */
295036d5fe6aSZoltan Kiss int
295136d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2952af2806f8SThomas Graf {
2953af2806f8SThomas Graf 	int i, j = 0;
2954af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
295536d5fe6aSZoltan Kiss 	int ret;
2956af2806f8SThomas Graf 	struct page *page;
2957af2806f8SThomas Graf 	unsigned int offset;
2958af2806f8SThomas Graf 
2959af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2960af2806f8SThomas Graf 
2961af2806f8SThomas Graf 	/* dont bother with small payloads */
296236d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
296336d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2964af2806f8SThomas Graf 
2965af2806f8SThomas Graf 	if (hlen) {
296636d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
296736d5fe6aSZoltan Kiss 		if (unlikely(ret))
296836d5fe6aSZoltan Kiss 			return ret;
2969af2806f8SThomas Graf 		len -= hlen;
2970af2806f8SThomas Graf 	} else {
2971af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2972af2806f8SThomas Graf 		if (plen) {
2973af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2974af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2975af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2976af2806f8SThomas Graf 			get_page(page);
2977af2806f8SThomas Graf 			j = 1;
2978af2806f8SThomas Graf 			len -= plen;
2979af2806f8SThomas Graf 		}
2980af2806f8SThomas Graf 	}
2981af2806f8SThomas Graf 
2982af2806f8SThomas Graf 	to->truesize += len + plen;
2983af2806f8SThomas Graf 	to->len += len + plen;
2984af2806f8SThomas Graf 	to->data_len += len + plen;
2985af2806f8SThomas Graf 
298636d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
298736d5fe6aSZoltan Kiss 		skb_tx_error(from);
298836d5fe6aSZoltan Kiss 		return -ENOMEM;
298936d5fe6aSZoltan Kiss 	}
29901f8b977aSWillem de Bruijn 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
299136d5fe6aSZoltan Kiss 
2992af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2993d8e18a51SMatthew Wilcox (Oracle) 		int size;
2994d8e18a51SMatthew Wilcox (Oracle) 
2995af2806f8SThomas Graf 		if (!len)
2996af2806f8SThomas Graf 			break;
2997af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2998d8e18a51SMatthew Wilcox (Oracle) 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
2999d8e18a51SMatthew Wilcox (Oracle) 					len);
3000d8e18a51SMatthew Wilcox (Oracle) 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3001d8e18a51SMatthew Wilcox (Oracle) 		len -= size;
3002af2806f8SThomas Graf 		skb_frag_ref(to, j);
3003af2806f8SThomas Graf 		j++;
3004af2806f8SThomas Graf 	}
3005af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
300636d5fe6aSZoltan Kiss 
300736d5fe6aSZoltan Kiss 	return 0;
3008af2806f8SThomas Graf }
3009af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
3010af2806f8SThomas Graf 
30111da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
30121da177e4SLinus Torvalds {
3013d3bc23e7SAl Viro 	__wsum csum;
30141da177e4SLinus Torvalds 	long csstart;
30151da177e4SLinus Torvalds 
301684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
301755508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
30181da177e4SLinus Torvalds 	else
30191da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
30201da177e4SLinus Torvalds 
302109a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
30221da177e4SLinus Torvalds 
3023d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds 	csum = 0;
30261da177e4SLinus Torvalds 	if (csstart != skb->len)
30271da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
30288d5930dfSAl Viro 					      skb->len - csstart);
30291da177e4SLinus Torvalds 
303084fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3031ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
30321da177e4SLinus Torvalds 
3033d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
30341da177e4SLinus Torvalds 	}
30351da177e4SLinus Torvalds }
3036b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds /**
30391da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
30401da177e4SLinus Torvalds  *	@list: list to dequeue from
30411da177e4SLinus Torvalds  *
30421da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
30431da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
30441da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
30451da177e4SLinus Torvalds  */
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
30481da177e4SLinus Torvalds {
30491da177e4SLinus Torvalds 	unsigned long flags;
30501da177e4SLinus Torvalds 	struct sk_buff *result;
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30531da177e4SLinus Torvalds 	result = __skb_dequeue(list);
30541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30551da177e4SLinus Torvalds 	return result;
30561da177e4SLinus Torvalds }
3057b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds /**
30601da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
30611da177e4SLinus Torvalds  *	@list: list to dequeue from
30621da177e4SLinus Torvalds  *
30631da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
30641da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
30651da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
30661da177e4SLinus Torvalds  */
30671da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
30681da177e4SLinus Torvalds {
30691da177e4SLinus Torvalds 	unsigned long flags;
30701da177e4SLinus Torvalds 	struct sk_buff *result;
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30731da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
30741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30751da177e4SLinus Torvalds 	return result;
30761da177e4SLinus Torvalds }
3077b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds /**
30801da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
30811da177e4SLinus Torvalds  *	@list: list to empty
30821da177e4SLinus Torvalds  *
30831da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
30841da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
30851da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
30861da177e4SLinus Torvalds  */
30871da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
30881da177e4SLinus Torvalds {
30891da177e4SLinus Torvalds 	struct sk_buff *skb;
30901da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
30911da177e4SLinus Torvalds 		kfree_skb(skb);
30921da177e4SLinus Torvalds }
3093b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds /**
30969f5afeaeSYaogong Wang  *	skb_rbtree_purge - empty a skb rbtree
30979f5afeaeSYaogong Wang  *	@root: root of the rbtree to empty
3098385114deSPeter Oskolkov  *	Return value: the sum of truesizes of all purged skbs.
30999f5afeaeSYaogong Wang  *
31009f5afeaeSYaogong Wang  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
31019f5afeaeSYaogong Wang  *	the list and one reference dropped. This function does not take
31029f5afeaeSYaogong Wang  *	any lock. Synchronization should be handled by the caller (e.g., TCP
31039f5afeaeSYaogong Wang  *	out-of-order queue is protected by the socket lock).
31049f5afeaeSYaogong Wang  */
3105385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root)
31069f5afeaeSYaogong Wang {
31077c90584cSEric Dumazet 	struct rb_node *p = rb_first(root);
3108385114deSPeter Oskolkov 	unsigned int sum = 0;
31099f5afeaeSYaogong Wang 
31107c90584cSEric Dumazet 	while (p) {
31117c90584cSEric Dumazet 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
31127c90584cSEric Dumazet 
31137c90584cSEric Dumazet 		p = rb_next(p);
31147c90584cSEric Dumazet 		rb_erase(&skb->rbnode, root);
3115385114deSPeter Oskolkov 		sum += skb->truesize;
31169f5afeaeSYaogong Wang 		kfree_skb(skb);
31177c90584cSEric Dumazet 	}
3118385114deSPeter Oskolkov 	return sum;
31199f5afeaeSYaogong Wang }
31209f5afeaeSYaogong Wang 
31219f5afeaeSYaogong Wang /**
31221da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
31231da177e4SLinus Torvalds  *	@list: list to use
31241da177e4SLinus Torvalds  *	@newsk: buffer to queue
31251da177e4SLinus Torvalds  *
31261da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
31271da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
31281da177e4SLinus Torvalds  *	safely.
31291da177e4SLinus Torvalds  *
31301da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31311da177e4SLinus Torvalds  */
31321da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
31331da177e4SLinus Torvalds {
31341da177e4SLinus Torvalds 	unsigned long flags;
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31371da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
31381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31391da177e4SLinus Torvalds }
3140b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds /**
31431da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
31441da177e4SLinus Torvalds  *	@list: list to use
31451da177e4SLinus Torvalds  *	@newsk: buffer to queue
31461da177e4SLinus Torvalds  *
31471da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
31481da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
31491da177e4SLinus Torvalds  *	safely.
31501da177e4SLinus Torvalds  *
31511da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31521da177e4SLinus Torvalds  */
31531da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
31541da177e4SLinus Torvalds {
31551da177e4SLinus Torvalds 	unsigned long flags;
31561da177e4SLinus Torvalds 
31571da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31581da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
31591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31601da177e4SLinus Torvalds }
3161b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
31628728b834SDavid S. Miller 
31631da177e4SLinus Torvalds /**
31641da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
31651da177e4SLinus Torvalds  *	@skb: buffer to remove
31668728b834SDavid S. Miller  *	@list: list to use
31671da177e4SLinus Torvalds  *
31688728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
31698728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
31701da177e4SLinus Torvalds  *
31718728b834SDavid S. Miller  *	You must know what list the SKB is on.
31721da177e4SLinus Torvalds  */
31738728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
31741da177e4SLinus Torvalds {
31751da177e4SLinus Torvalds 	unsigned long flags;
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31788728b834SDavid S. Miller 	__skb_unlink(skb, list);
31791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31801da177e4SLinus Torvalds }
3181b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds /**
31841da177e4SLinus Torvalds  *	skb_append	-	append a buffer
31851da177e4SLinus Torvalds  *	@old: buffer to insert after
31861da177e4SLinus Torvalds  *	@newsk: buffer to insert
31878728b834SDavid S. Miller  *	@list: list to use
31881da177e4SLinus Torvalds  *
31891da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
31901da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
31911da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31921da177e4SLinus Torvalds  */
31938728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
31941da177e4SLinus Torvalds {
31951da177e4SLinus Torvalds 	unsigned long flags;
31961da177e4SLinus Torvalds 
31978728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
31987de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
31998728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
32001da177e4SLinus Torvalds }
3201b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
32041da177e4SLinus Torvalds 					   struct sk_buff* skb1,
32051da177e4SLinus Torvalds 					   const u32 len, const int pos)
32061da177e4SLinus Torvalds {
32071da177e4SLinus Torvalds 	int i;
32081da177e4SLinus Torvalds 
3209d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3210d626f62bSArnaldo Carvalho de Melo 					 pos - len);
32111da177e4SLinus Torvalds 	/* And move data appendix as is. */
32121da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
32131da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
32161da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
32171da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
32181da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
32191da177e4SLinus Torvalds 	skb->data_len		   = 0;
32201da177e4SLinus Torvalds 	skb->len		   = len;
322127a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
32221da177e4SLinus Torvalds }
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
32251da177e4SLinus Torvalds 				       struct sk_buff* skb1,
32261da177e4SLinus Torvalds 				       const u32 len, int pos)
32271da177e4SLinus Torvalds {
32281da177e4SLinus Torvalds 	int i, k = 0;
32291da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
32321da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
32331da177e4SLinus Torvalds 	skb->len		  = len;
32341da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
32379e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 		if (pos + size > len) {
32401da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds 			if (pos < len) {
32431da177e4SLinus Torvalds 				/* Split frag.
32441da177e4SLinus Torvalds 				 * We have two variants in this case:
32451da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
32461da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
32471da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
32481da177e4SLinus Torvalds 				 *    where splitting is expensive.
32491da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
32501da177e4SLinus Torvalds 				 */
3251ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
3252b54c9d5bSJonathan Lemon 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
32539e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
32549e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
32551da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
32561da177e4SLinus Torvalds 			}
32571da177e4SLinus Torvalds 			k++;
32581da177e4SLinus Torvalds 		} else
32591da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
32601da177e4SLinus Torvalds 		pos += size;
32611da177e4SLinus Torvalds 	}
32621da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds /**
32661da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
32671da177e4SLinus Torvalds  * @skb: the buffer to split
32681da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
32691da177e4SLinus Torvalds  * @len: new length for skb
32701da177e4SLinus Torvalds  */
32711da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
32721da177e4SLinus Torvalds {
32731da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
32741da177e4SLinus Torvalds 
3275fff88030SWillem de Bruijn 	skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3276fff88030SWillem de Bruijn 				      SKBTX_SHARED_FRAG;
32771f8b977aSWillem de Bruijn 	skb_zerocopy_clone(skb1, skb, 0);
32781da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
32791da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
32801da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
32811da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
32821da177e4SLinus Torvalds }
3283b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
32841da177e4SLinus Torvalds 
32859f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
32869f782db3SIlpo Järvinen  *
32879f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
32889f782db3SIlpo Järvinen  */
3289832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
3290832d11c5SIlpo Järvinen {
32910ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3292832d11c5SIlpo Järvinen }
3293832d11c5SIlpo Järvinen 
3294832d11c5SIlpo Järvinen /**
3295832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
3296832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
3297832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
3298832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
3299832d11c5SIlpo Järvinen  *
3300832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
330120e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3302832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
3303832d11c5SIlpo Järvinen  *
3304832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
3305832d11c5SIlpo Järvinen  *
3306832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
3307832d11c5SIlpo Järvinen  * to have non-paged data as well.
3308832d11c5SIlpo Järvinen  *
3309832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
3310832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
3311832d11c5SIlpo Järvinen  */
3312832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3313832d11c5SIlpo Järvinen {
3314832d11c5SIlpo Järvinen 	int from, to, merge, todo;
3315d8e18a51SMatthew Wilcox (Oracle) 	skb_frag_t *fragfrom, *fragto;
3316832d11c5SIlpo Järvinen 
3317832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
3318f8071cdeSEric Dumazet 
3319f8071cdeSEric Dumazet 	if (skb_headlen(skb))
3320f8071cdeSEric Dumazet 		return 0;
33211f8b977aSWillem de Bruijn 	if (skb_zcopy(tgt) || skb_zcopy(skb))
33221f8b977aSWillem de Bruijn 		return 0;
3323832d11c5SIlpo Järvinen 
3324832d11c5SIlpo Järvinen 	todo = shiftlen;
3325832d11c5SIlpo Järvinen 	from = 0;
3326832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
3327832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
3328832d11c5SIlpo Järvinen 
3329832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
3330832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
3331832d11c5SIlpo Järvinen 	 */
3332832d11c5SIlpo Järvinen 	if (!to ||
3333ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3334b54c9d5bSJonathan Lemon 			      skb_frag_off(fragfrom))) {
3335832d11c5SIlpo Järvinen 		merge = -1;
3336832d11c5SIlpo Järvinen 	} else {
3337832d11c5SIlpo Järvinen 		merge = to - 1;
3338832d11c5SIlpo Järvinen 
33399e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
3340832d11c5SIlpo Järvinen 		if (todo < 0) {
3341832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
3342832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
3343832d11c5SIlpo Järvinen 				return 0;
3344832d11c5SIlpo Järvinen 
33459f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
33469f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
3347832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
3348832d11c5SIlpo Järvinen 
33499e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
33509e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
3351b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, shiftlen);
3352832d11c5SIlpo Järvinen 
3353832d11c5SIlpo Järvinen 			goto onlymerged;
3354832d11c5SIlpo Järvinen 		}
3355832d11c5SIlpo Järvinen 
3356832d11c5SIlpo Järvinen 		from++;
3357832d11c5SIlpo Järvinen 	}
3358832d11c5SIlpo Järvinen 
3359832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
3360832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
3361832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3362832d11c5SIlpo Järvinen 		return 0;
3363832d11c5SIlpo Järvinen 
3364832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3365832d11c5SIlpo Järvinen 		return 0;
3366832d11c5SIlpo Järvinen 
3367832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3368832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
3369832d11c5SIlpo Järvinen 			return 0;
3370832d11c5SIlpo Järvinen 
3371832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
3372832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
3373832d11c5SIlpo Järvinen 
33749e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
3375832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
33769e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
3377832d11c5SIlpo Järvinen 			from++;
3378832d11c5SIlpo Järvinen 			to++;
3379832d11c5SIlpo Järvinen 
3380832d11c5SIlpo Järvinen 		} else {
3381ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
3382b54c9d5bSJonathan Lemon 			skb_frag_page_copy(fragto, fragfrom);
3383b54c9d5bSJonathan Lemon 			skb_frag_off_copy(fragto, fragfrom);
33849e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
3385832d11c5SIlpo Järvinen 
3386b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, todo);
33879e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
3388832d11c5SIlpo Järvinen 			todo = 0;
3389832d11c5SIlpo Järvinen 
3390832d11c5SIlpo Järvinen 			to++;
3391832d11c5SIlpo Järvinen 			break;
3392832d11c5SIlpo Järvinen 		}
3393832d11c5SIlpo Järvinen 	}
3394832d11c5SIlpo Järvinen 
3395832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
3396832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
3397832d11c5SIlpo Järvinen 
3398832d11c5SIlpo Järvinen 	if (merge >= 0) {
3399832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
3400832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
3401832d11c5SIlpo Järvinen 
34029e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3403ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
3404832d11c5SIlpo Järvinen 	}
3405832d11c5SIlpo Järvinen 
3406832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
3407832d11c5SIlpo Järvinen 	to = 0;
3408832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
3409832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3410832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
3411832d11c5SIlpo Järvinen 
3412832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3413832d11c5SIlpo Järvinen 
3414832d11c5SIlpo Järvinen onlymerged:
3415832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
3416832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
3417832d11c5SIlpo Järvinen 	 */
3418832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
3419832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
3420832d11c5SIlpo Järvinen 
3421832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
3422832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
3423832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
3424832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
3425832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
3426832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
3427832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
3428832d11c5SIlpo Järvinen 
3429832d11c5SIlpo Järvinen 	return shiftlen;
3430832d11c5SIlpo Järvinen }
3431832d11c5SIlpo Järvinen 
3432677e90edSThomas Graf /**
3433677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
3434677e90edSThomas Graf  * @skb: the buffer to read
3435677e90edSThomas Graf  * @from: lower offset of data to be read
3436677e90edSThomas Graf  * @to: upper offset of data to be read
3437677e90edSThomas Graf  * @st: state variable
3438677e90edSThomas Graf  *
3439677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
3440677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
3441677e90edSThomas Graf  */
3442677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3443677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
3444677e90edSThomas Graf {
3445677e90edSThomas Graf 	st->lower_offset = from;
3446677e90edSThomas Graf 	st->upper_offset = to;
3447677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
3448677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
3449677e90edSThomas Graf 	st->frag_data = NULL;
345097550f6fSWillem de Bruijn 	st->frag_off = 0;
3451677e90edSThomas Graf }
3452b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
3453677e90edSThomas Graf 
3454677e90edSThomas Graf /**
3455677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
3456677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
3457677e90edSThomas Graf  * @data: destination pointer for data to be returned
3458677e90edSThomas Graf  * @st: state variable
3459677e90edSThomas Graf  *
3460bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
3461677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
3462bc32383cSMathias Krause  * the head of the data block to @data and returns the length
3463677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
3464677e90edSThomas Graf  * offset has been reached.
3465677e90edSThomas Graf  *
3466677e90edSThomas Graf  * The caller is not required to consume all of the data
3467bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
3468677e90edSThomas Graf  * of bytes already consumed and the next call to
3469677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
3470677e90edSThomas Graf  *
347125985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
3472e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
3473677e90edSThomas Graf  *       reads of potentially non linear data.
3474677e90edSThomas Graf  *
3475bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
3476677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
3477677e90edSThomas Graf  *       a stack for this purpose.
3478677e90edSThomas Graf  */
3479677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3480677e90edSThomas Graf 			  struct skb_seq_state *st)
3481677e90edSThomas Graf {
3482677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3483677e90edSThomas Graf 	skb_frag_t *frag;
3484677e90edSThomas Graf 
3485aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
3486aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
3487aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
3488aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
3489aeb193eaSWedson Almeida Filho 		}
3490677e90edSThomas Graf 		return 0;
3491aeb193eaSWedson Almeida Filho 	}
3492677e90edSThomas Graf 
3493677e90edSThomas Graf next_skb:
349495e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3495677e90edSThomas Graf 
3496995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
349795e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3498677e90edSThomas Graf 		return block_limit - abs_offset;
3499677e90edSThomas Graf 	}
3500677e90edSThomas Graf 
3501677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
3502677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
3503677e90edSThomas Graf 
3504677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
350597550f6fSWillem de Bruijn 		unsigned int pg_idx, pg_off, pg_sz;
3506677e90edSThomas Graf 
350797550f6fSWillem de Bruijn 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
350897550f6fSWillem de Bruijn 
350997550f6fSWillem de Bruijn 		pg_idx = 0;
351097550f6fSWillem de Bruijn 		pg_off = skb_frag_off(frag);
351197550f6fSWillem de Bruijn 		pg_sz = skb_frag_size(frag);
351297550f6fSWillem de Bruijn 
351397550f6fSWillem de Bruijn 		if (skb_frag_must_loop(skb_frag_page(frag))) {
351497550f6fSWillem de Bruijn 			pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
351597550f6fSWillem de Bruijn 			pg_off = offset_in_page(pg_off + st->frag_off);
351697550f6fSWillem de Bruijn 			pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
351797550f6fSWillem de Bruijn 						    PAGE_SIZE - pg_off);
351897550f6fSWillem de Bruijn 		}
351997550f6fSWillem de Bruijn 
352097550f6fSWillem de Bruijn 		block_limit = pg_sz + st->stepped_offset;
3521677e90edSThomas Graf 		if (abs_offset < block_limit) {
3522677e90edSThomas Graf 			if (!st->frag_data)
352397550f6fSWillem de Bruijn 				st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
3524677e90edSThomas Graf 
352597550f6fSWillem de Bruijn 			*data = (u8 *)st->frag_data + pg_off +
3526677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
3527677e90edSThomas Graf 
3528677e90edSThomas Graf 			return block_limit - abs_offset;
3529677e90edSThomas Graf 		}
3530677e90edSThomas Graf 
3531677e90edSThomas Graf 		if (st->frag_data) {
353251c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
3533677e90edSThomas Graf 			st->frag_data = NULL;
3534677e90edSThomas Graf 		}
3535677e90edSThomas Graf 
353697550f6fSWillem de Bruijn 		st->stepped_offset += pg_sz;
353797550f6fSWillem de Bruijn 		st->frag_off += pg_sz;
353897550f6fSWillem de Bruijn 		if (st->frag_off == skb_frag_size(frag)) {
353997550f6fSWillem de Bruijn 			st->frag_off = 0;
3540677e90edSThomas Graf 			st->frag_idx++;
354197550f6fSWillem de Bruijn 		}
3542677e90edSThomas Graf 	}
3543677e90edSThomas Graf 
35445b5a60daSOlaf Kirch 	if (st->frag_data) {
354551c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
35465b5a60daSOlaf Kirch 		st->frag_data = NULL;
35475b5a60daSOlaf Kirch 	}
35485b5a60daSOlaf Kirch 
354921dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3550677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
355195e3b24cSHerbert Xu 		st->frag_idx = 0;
3552677e90edSThomas Graf 		goto next_skb;
355371b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
355471b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
355571b3346dSShyam Iyer 		st->frag_idx = 0;
3556677e90edSThomas Graf 		goto next_skb;
3557677e90edSThomas Graf 	}
3558677e90edSThomas Graf 
3559677e90edSThomas Graf 	return 0;
3560677e90edSThomas Graf }
3561b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
3562677e90edSThomas Graf 
3563677e90edSThomas Graf /**
3564677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
3565677e90edSThomas Graf  * @st: state variable
3566677e90edSThomas Graf  *
3567677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
3568677e90edSThomas Graf  * returned 0.
3569677e90edSThomas Graf  */
3570677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
3571677e90edSThomas Graf {
3572677e90edSThomas Graf 	if (st->frag_data)
357351c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
3574677e90edSThomas Graf }
3575b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
3576677e90edSThomas Graf 
35773fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
35783fc7e8a6SThomas Graf 
35793fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
35803fc7e8a6SThomas Graf 					  struct ts_config *conf,
35813fc7e8a6SThomas Graf 					  struct ts_state *state)
35823fc7e8a6SThomas Graf {
35833fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
35843fc7e8a6SThomas Graf }
35853fc7e8a6SThomas Graf 
35863fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
35873fc7e8a6SThomas Graf {
35883fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
35893fc7e8a6SThomas Graf }
35903fc7e8a6SThomas Graf 
35913fc7e8a6SThomas Graf /**
35923fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
35933fc7e8a6SThomas Graf  * @skb: the buffer to look in
35943fc7e8a6SThomas Graf  * @from: search offset
35953fc7e8a6SThomas Graf  * @to: search limit
35963fc7e8a6SThomas Graf  * @config: textsearch configuration
35973fc7e8a6SThomas Graf  *
35983fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
35993fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
36003fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
36013fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
36023fc7e8a6SThomas Graf  */
36033fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3604059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
36053fc7e8a6SThomas Graf {
3606059a2440SBojan Prtvar 	struct ts_state state;
3607f72b948dSPhil Oester 	unsigned int ret;
3608f72b948dSPhil Oester 
36093fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
36103fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
36113fc7e8a6SThomas Graf 
3612059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
36133fc7e8a6SThomas Graf 
3614059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
3615f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
36163fc7e8a6SThomas Graf }
3617b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
36183fc7e8a6SThomas Graf 
3619be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3620be12a1feSHannes Frederic Sowa 			 int offset, size_t size)
3621be12a1feSHannes Frederic Sowa {
3622be12a1feSHannes Frederic Sowa 	int i = skb_shinfo(skb)->nr_frags;
3623be12a1feSHannes Frederic Sowa 
3624be12a1feSHannes Frederic Sowa 	if (skb_can_coalesce(skb, i, page, offset)) {
3625be12a1feSHannes Frederic Sowa 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3626be12a1feSHannes Frederic Sowa 	} else if (i < MAX_SKB_FRAGS) {
3627be12a1feSHannes Frederic Sowa 		get_page(page);
3628be12a1feSHannes Frederic Sowa 		skb_fill_page_desc(skb, i, page, offset, size);
3629be12a1feSHannes Frederic Sowa 	} else {
3630be12a1feSHannes Frederic Sowa 		return -EMSGSIZE;
3631be12a1feSHannes Frederic Sowa 	}
3632be12a1feSHannes Frederic Sowa 
3633be12a1feSHannes Frederic Sowa 	return 0;
3634be12a1feSHannes Frederic Sowa }
3635be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3636be12a1feSHannes Frederic Sowa 
3637cbb042f9SHerbert Xu /**
3638cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
3639cbb042f9SHerbert Xu  *	@skb: buffer to update
3640cbb042f9SHerbert Xu  *	@len: length of data pulled
3641cbb042f9SHerbert Xu  *
3642cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
3643fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
364484fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
364584fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
364684fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
3647cbb042f9SHerbert Xu  */
3648af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3649cbb042f9SHerbert Xu {
365031b33dfbSPravin B Shelar 	unsigned char *data = skb->data;
365131b33dfbSPravin B Shelar 
3652cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
365331b33dfbSPravin B Shelar 	__skb_pull(skb, len);
365431b33dfbSPravin B Shelar 	skb_postpull_rcsum(skb, data, len);
365531b33dfbSPravin B Shelar 	return skb->data;
3656cbb042f9SHerbert Xu }
3657f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3658f94691acSArnaldo Carvalho de Melo 
365913acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
366013acc94eSYonghong Song {
366113acc94eSYonghong Song 	skb_frag_t head_frag;
366213acc94eSYonghong Song 	struct page *page;
366313acc94eSYonghong Song 
366413acc94eSYonghong Song 	page = virt_to_head_page(frag_skb->head);
3665d8e18a51SMatthew Wilcox (Oracle) 	__skb_frag_set_page(&head_frag, page);
3666b54c9d5bSJonathan Lemon 	skb_frag_off_set(&head_frag, frag_skb->data -
3667b54c9d5bSJonathan Lemon 			 (unsigned char *)page_address(page));
3668d8e18a51SMatthew Wilcox (Oracle) 	skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
366913acc94eSYonghong Song 	return head_frag;
367013acc94eSYonghong Song }
367113acc94eSYonghong Song 
36723a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb,
36733a1296a3SSteffen Klassert 				 netdev_features_t features,
36743a1296a3SSteffen Klassert 				 unsigned int offset)
36753a1296a3SSteffen Klassert {
36763a1296a3SSteffen Klassert 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
36773a1296a3SSteffen Klassert 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
36783a1296a3SSteffen Klassert 	unsigned int delta_truesize = 0;
36793a1296a3SSteffen Klassert 	unsigned int delta_len = 0;
36803a1296a3SSteffen Klassert 	struct sk_buff *tail = NULL;
368153475c5dSDongseok Yi 	struct sk_buff *nskb, *tmp;
368253475c5dSDongseok Yi 	int err;
36833a1296a3SSteffen Klassert 
36843a1296a3SSteffen Klassert 	skb_push(skb, -skb_network_offset(skb) + offset);
36853a1296a3SSteffen Klassert 
36863a1296a3SSteffen Klassert 	skb_shinfo(skb)->frag_list = NULL;
36873a1296a3SSteffen Klassert 
36883a1296a3SSteffen Klassert 	do {
36893a1296a3SSteffen Klassert 		nskb = list_skb;
36903a1296a3SSteffen Klassert 		list_skb = list_skb->next;
36913a1296a3SSteffen Klassert 
369253475c5dSDongseok Yi 		err = 0;
369353475c5dSDongseok Yi 		if (skb_shared(nskb)) {
369453475c5dSDongseok Yi 			tmp = skb_clone(nskb, GFP_ATOMIC);
369553475c5dSDongseok Yi 			if (tmp) {
369653475c5dSDongseok Yi 				consume_skb(nskb);
369753475c5dSDongseok Yi 				nskb = tmp;
369853475c5dSDongseok Yi 				err = skb_unclone(nskb, GFP_ATOMIC);
369953475c5dSDongseok Yi 			} else {
370053475c5dSDongseok Yi 				err = -ENOMEM;
370153475c5dSDongseok Yi 			}
370253475c5dSDongseok Yi 		}
370353475c5dSDongseok Yi 
37043a1296a3SSteffen Klassert 		if (!tail)
37053a1296a3SSteffen Klassert 			skb->next = nskb;
37063a1296a3SSteffen Klassert 		else
37073a1296a3SSteffen Klassert 			tail->next = nskb;
37083a1296a3SSteffen Klassert 
370953475c5dSDongseok Yi 		if (unlikely(err)) {
371053475c5dSDongseok Yi 			nskb->next = list_skb;
371153475c5dSDongseok Yi 			goto err_linearize;
371253475c5dSDongseok Yi 		}
371353475c5dSDongseok Yi 
37143a1296a3SSteffen Klassert 		tail = nskb;
37153a1296a3SSteffen Klassert 
37163a1296a3SSteffen Klassert 		delta_len += nskb->len;
37173a1296a3SSteffen Klassert 		delta_truesize += nskb->truesize;
37183a1296a3SSteffen Klassert 
37193a1296a3SSteffen Klassert 		skb_push(nskb, -skb_network_offset(nskb) + offset);
37203a1296a3SSteffen Klassert 
3721cf673ed0SFlorian Westphal 		skb_release_head_state(nskb);
37223a1296a3SSteffen Klassert 		 __copy_skb_header(nskb, skb);
37233a1296a3SSteffen Klassert 
37243a1296a3SSteffen Klassert 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
37253a1296a3SSteffen Klassert 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
37263a1296a3SSteffen Klassert 						 nskb->data - tnl_hlen,
37273a1296a3SSteffen Klassert 						 offset + tnl_hlen);
37283a1296a3SSteffen Klassert 
37293a1296a3SSteffen Klassert 		if (skb_needs_linearize(nskb, features) &&
37303a1296a3SSteffen Klassert 		    __skb_linearize(nskb))
37313a1296a3SSteffen Klassert 			goto err_linearize;
37323a1296a3SSteffen Klassert 
37333a1296a3SSteffen Klassert 	} while (list_skb);
37343a1296a3SSteffen Klassert 
37353a1296a3SSteffen Klassert 	skb->truesize = skb->truesize - delta_truesize;
37363a1296a3SSteffen Klassert 	skb->data_len = skb->data_len - delta_len;
37373a1296a3SSteffen Klassert 	skb->len = skb->len - delta_len;
37383a1296a3SSteffen Klassert 
37393a1296a3SSteffen Klassert 	skb_gso_reset(skb);
37403a1296a3SSteffen Klassert 
37413a1296a3SSteffen Klassert 	skb->prev = tail;
37423a1296a3SSteffen Klassert 
37433a1296a3SSteffen Klassert 	if (skb_needs_linearize(skb, features) &&
37443a1296a3SSteffen Klassert 	    __skb_linearize(skb))
37453a1296a3SSteffen Klassert 		goto err_linearize;
37463a1296a3SSteffen Klassert 
37473a1296a3SSteffen Klassert 	skb_get(skb);
37483a1296a3SSteffen Klassert 
37493a1296a3SSteffen Klassert 	return skb;
37503a1296a3SSteffen Klassert 
37513a1296a3SSteffen Klassert err_linearize:
37523a1296a3SSteffen Klassert 	kfree_skb_list(skb->next);
37533a1296a3SSteffen Klassert 	skb->next = NULL;
37543a1296a3SSteffen Klassert 	return ERR_PTR(-ENOMEM);
37553a1296a3SSteffen Klassert }
37563a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list);
37573a1296a3SSteffen Klassert 
37583a1296a3SSteffen Klassert int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
37593a1296a3SSteffen Klassert {
37603a1296a3SSteffen Klassert 	if (unlikely(p->len + skb->len >= 65536))
37613a1296a3SSteffen Klassert 		return -E2BIG;
37623a1296a3SSteffen Klassert 
37633a1296a3SSteffen Klassert 	if (NAPI_GRO_CB(p)->last == p)
37643a1296a3SSteffen Klassert 		skb_shinfo(p)->frag_list = skb;
37653a1296a3SSteffen Klassert 	else
37663a1296a3SSteffen Klassert 		NAPI_GRO_CB(p)->last->next = skb;
37673a1296a3SSteffen Klassert 
37683a1296a3SSteffen Klassert 	skb_pull(skb, skb_gro_offset(skb));
37693a1296a3SSteffen Klassert 
37703a1296a3SSteffen Klassert 	NAPI_GRO_CB(p)->last = skb;
37713a1296a3SSteffen Klassert 	NAPI_GRO_CB(p)->count++;
37723a1296a3SSteffen Klassert 	p->data_len += skb->len;
37733a1296a3SSteffen Klassert 	p->truesize += skb->truesize;
37743a1296a3SSteffen Klassert 	p->len += skb->len;
37753a1296a3SSteffen Klassert 
37763a1296a3SSteffen Klassert 	NAPI_GRO_CB(skb)->same_flow = 1;
37773a1296a3SSteffen Klassert 
37783a1296a3SSteffen Klassert 	return 0;
37793a1296a3SSteffen Klassert }
37803a1296a3SSteffen Klassert 
3781f4c50d99SHerbert Xu /**
3782f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
3783df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
3784576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
3785f4c50d99SHerbert Xu  *
3786f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
37874c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
37884c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
3789f4c50d99SHerbert Xu  */
3790df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
3791df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
3792f4c50d99SHerbert Xu {
3793f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
3794f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
37951a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3796df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3797df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3798df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
37991fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
3800f4c50d99SHerbert Xu 	unsigned int offset = doffset;
3801df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3802802ab55aSAlexander Duyck 	unsigned int partial_segs = 0;
3803f4c50d99SHerbert Xu 	unsigned int headroom;
3804802ab55aSAlexander Duyck 	unsigned int len = head_skb->len;
3805ec5f0615SPravin B Shelar 	__be16 proto;
380636c98382SAlexander Duyck 	bool csum, sg;
3807df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3808f4c50d99SHerbert Xu 	int err = -ENOMEM;
3809f4c50d99SHerbert Xu 	int i = 0;
3810f4c50d99SHerbert Xu 	int pos;
3811f4c50d99SHerbert Xu 
38123dcbdb13SShmulik Ladkani 	if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
38133dcbdb13SShmulik Ladkani 	    (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
38143dcbdb13SShmulik Ladkani 		/* gso_size is untrusted, and we have a frag_list with a linear
38153dcbdb13SShmulik Ladkani 		 * non head_frag head.
38163dcbdb13SShmulik Ladkani 		 *
38173dcbdb13SShmulik Ladkani 		 * (we assume checking the first list_skb member suffices;
38183dcbdb13SShmulik Ladkani 		 * i.e if either of the list_skb members have non head_frag
38193dcbdb13SShmulik Ladkani 		 * head, then the first one has too).
38203dcbdb13SShmulik Ladkani 		 *
38213dcbdb13SShmulik Ladkani 		 * If head_skb's headlen does not fit requested gso_size, it
38223dcbdb13SShmulik Ladkani 		 * means that the frag_list members do NOT terminate on exact
38233dcbdb13SShmulik Ladkani 		 * gso_size boundaries. Hence we cannot perform skb_frag_t page
38243dcbdb13SShmulik Ladkani 		 * sharing. Therefore we must fallback to copying the frag_list
38253dcbdb13SShmulik Ladkani 		 * skbs; we do so by disabling SG.
38263dcbdb13SShmulik Ladkani 		 */
38273dcbdb13SShmulik Ladkani 		if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
38283dcbdb13SShmulik Ladkani 			features &= ~NETIF_F_SG;
38293dcbdb13SShmulik Ladkani 	}
38303dcbdb13SShmulik Ladkani 
38315882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
38322f631133SMiaohe Lin 	proto = skb_network_protocol(head_skb, NULL);
3833ec5f0615SPravin B Shelar 	if (unlikely(!proto))
3834ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
3835ec5f0615SPravin B Shelar 
383636c98382SAlexander Duyck 	sg = !!(features & NETIF_F_SG);
3837f245d079SAlexander Duyck 	csum = !!can_checksum_protocol(features, proto);
38387e2b10c1STom Herbert 
383907b26c94SSteffen Klassert 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
384007b26c94SSteffen Klassert 		if (!(features & NETIF_F_GSO_PARTIAL)) {
384107b26c94SSteffen Klassert 			struct sk_buff *iter;
384243170c4eSIlan Tayari 			unsigned int frag_len;
384307b26c94SSteffen Klassert 
384407b26c94SSteffen Klassert 			if (!list_skb ||
384507b26c94SSteffen Klassert 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
384607b26c94SSteffen Klassert 				goto normal;
384707b26c94SSteffen Klassert 
384843170c4eSIlan Tayari 			/* If we get here then all the required
384943170c4eSIlan Tayari 			 * GSO features except frag_list are supported.
385043170c4eSIlan Tayari 			 * Try to split the SKB to multiple GSO SKBs
385143170c4eSIlan Tayari 			 * with no frag_list.
385243170c4eSIlan Tayari 			 * Currently we can do that only when the buffers don't
385343170c4eSIlan Tayari 			 * have a linear part and all the buffers except
385443170c4eSIlan Tayari 			 * the last are of the same length.
385507b26c94SSteffen Klassert 			 */
385643170c4eSIlan Tayari 			frag_len = list_skb->len;
385707b26c94SSteffen Klassert 			skb_walk_frags(head_skb, iter) {
385843170c4eSIlan Tayari 				if (frag_len != iter->len && iter->next)
385943170c4eSIlan Tayari 					goto normal;
3860eaffadbbSIlan Tayari 				if (skb_headlen(iter) && !iter->head_frag)
386107b26c94SSteffen Klassert 					goto normal;
386207b26c94SSteffen Klassert 
386307b26c94SSteffen Klassert 				len -= iter->len;
386407b26c94SSteffen Klassert 			}
386543170c4eSIlan Tayari 
386643170c4eSIlan Tayari 			if (len != frag_len)
386743170c4eSIlan Tayari 				goto normal;
386807b26c94SSteffen Klassert 		}
386907b26c94SSteffen Klassert 
3870802ab55aSAlexander Duyck 		/* GSO partial only requires that we trim off any excess that
3871802ab55aSAlexander Duyck 		 * doesn't fit into an MSS sized block, so take care of that
3872802ab55aSAlexander Duyck 		 * now.
3873802ab55aSAlexander Duyck 		 */
3874802ab55aSAlexander Duyck 		partial_segs = len / mss;
3875d7fb5a80SAlexander Duyck 		if (partial_segs > 1)
3876802ab55aSAlexander Duyck 			mss *= partial_segs;
3877d7fb5a80SAlexander Duyck 		else
3878d7fb5a80SAlexander Duyck 			partial_segs = 0;
3879802ab55aSAlexander Duyck 	}
3880802ab55aSAlexander Duyck 
388107b26c94SSteffen Klassert normal:
3882df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
3883df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
3884f4c50d99SHerbert Xu 
3885f4c50d99SHerbert Xu 	do {
3886f4c50d99SHerbert Xu 		struct sk_buff *nskb;
38878cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
3888c8884eddSHerbert Xu 		int hsize;
3889f4c50d99SHerbert Xu 		int size;
3890f4c50d99SHerbert Xu 
38913953c46cSMarcelo Ricardo Leitner 		if (unlikely(mss == GSO_BY_FRAGS)) {
38923953c46cSMarcelo Ricardo Leitner 			len = list_skb->len;
38933953c46cSMarcelo Ricardo Leitner 		} else {
3894df5771ffSMichael S. Tsirkin 			len = head_skb->len - offset;
3895f4c50d99SHerbert Xu 			if (len > mss)
3896f4c50d99SHerbert Xu 				len = mss;
38973953c46cSMarcelo Ricardo Leitner 		}
3898f4c50d99SHerbert Xu 
3899df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
3900f4c50d99SHerbert Xu 		if (hsize < 0)
3901f4c50d99SHerbert Xu 			hsize = 0;
3902c8884eddSHerbert Xu 		if (hsize > len || !sg)
3903c8884eddSHerbert Xu 			hsize = len;
3904f4c50d99SHerbert Xu 
39051a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
39061a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
39071a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
390889319d38SHerbert Xu 
39099d8506ccSHerbert Xu 			i = 0;
39101a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
39111a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
39121fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
39131a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
39149d8506ccSHerbert Xu 
39159d8506ccSHerbert Xu 			while (pos < offset + len) {
39169d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
39179d8506ccSHerbert Xu 
39184e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
39199d8506ccSHerbert Xu 				if (pos + size > offset + len)
39209d8506ccSHerbert Xu 					break;
39219d8506ccSHerbert Xu 
39229d8506ccSHerbert Xu 				i++;
39239d8506ccSHerbert Xu 				pos += size;
39244e1beba1SMichael S. Tsirkin 				frag++;
39259d8506ccSHerbert Xu 			}
39269d8506ccSHerbert Xu 
39271a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
39281a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
392989319d38SHerbert Xu 
3930f4c50d99SHerbert Xu 			if (unlikely(!nskb))
3931f4c50d99SHerbert Xu 				goto err;
3932f4c50d99SHerbert Xu 
39339d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
39349d8506ccSHerbert Xu 				kfree_skb(nskb);
39359d8506ccSHerbert Xu 				goto err;
39369d8506ccSHerbert Xu 			}
39379d8506ccSHerbert Xu 
3938ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
393989319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
394089319d38SHerbert Xu 				kfree_skb(nskb);
394189319d38SHerbert Xu 				goto err;
394289319d38SHerbert Xu 			}
394389319d38SHerbert Xu 
3944ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
394589319d38SHerbert Xu 			skb_release_head_state(nskb);
394689319d38SHerbert Xu 			__skb_push(nskb, doffset);
394789319d38SHerbert Xu 		} else {
3948c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3949df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3950c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
395189319d38SHerbert Xu 
395289319d38SHerbert Xu 			if (unlikely(!nskb))
395389319d38SHerbert Xu 				goto err;
395489319d38SHerbert Xu 
395589319d38SHerbert Xu 			skb_reserve(nskb, headroom);
395689319d38SHerbert Xu 			__skb_put(nskb, doffset);
395789319d38SHerbert Xu 		}
395889319d38SHerbert Xu 
3959f4c50d99SHerbert Xu 		if (segs)
3960f4c50d99SHerbert Xu 			tail->next = nskb;
3961f4c50d99SHerbert Xu 		else
3962f4c50d99SHerbert Xu 			segs = nskb;
3963f4c50d99SHerbert Xu 		tail = nskb;
3964f4c50d99SHerbert Xu 
3965df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3966f4c50d99SHerbert Xu 
3967030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3968fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
396968c33163SPravin B Shelar 
3970df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
397168c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
397268c33163SPravin B Shelar 						 doffset + tnl_hlen);
397389319d38SHerbert Xu 
39749d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
39751cdbcb79SSimon Horman 			goto perform_csum_check;
397689319d38SHerbert Xu 
39777fbeffedSAlexander Duyck 		if (!sg) {
39781454c9faSYadu Kishore 			if (!csum) {
39797fbeffedSAlexander Duyck 				if (!nskb->remcsum_offload)
39806f85a124SHerbert Xu 					nskb->ip_summed = CHECKSUM_NONE;
398176443456SAlexander Duyck 				SKB_GSO_CB(nskb)->csum =
398276443456SAlexander Duyck 					skb_copy_and_csum_bits(head_skb, offset,
39831454c9faSYadu Kishore 							       skb_put(nskb,
39841454c9faSYadu Kishore 								       len),
39858d5930dfSAl Viro 							       len);
39867e2b10c1STom Herbert 				SKB_GSO_CB(nskb)->csum_start =
3987de843723STom Herbert 					skb_headroom(nskb) + doffset;
39881454c9faSYadu Kishore 			} else {
39891454c9faSYadu Kishore 				skb_copy_bits(head_skb, offset,
39901454c9faSYadu Kishore 					      skb_put(nskb, len),
39911454c9faSYadu Kishore 					      len);
39921454c9faSYadu Kishore 			}
3993f4c50d99SHerbert Xu 			continue;
3994f4c50d99SHerbert Xu 		}
3995f4c50d99SHerbert Xu 
39968cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3997f4c50d99SHerbert Xu 
3998df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3999d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
4000f4c50d99SHerbert Xu 
4001fff88030SWillem de Bruijn 		skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
4002df5771ffSMichael S. Tsirkin 					      SKBTX_SHARED_FRAG;
4003cef401deSEric Dumazet 
4004bf5c25d6SWillem de Bruijn 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4005bf5c25d6SWillem de Bruijn 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4006bf5c25d6SWillem de Bruijn 			goto err;
4007bf5c25d6SWillem de Bruijn 
40089d8506ccSHerbert Xu 		while (pos < offset + len) {
40099d8506ccSHerbert Xu 			if (i >= nfrags) {
40109d8506ccSHerbert Xu 				i = 0;
40111a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
40121a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
40131fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
401413acc94eSYonghong Song 				if (!skb_headlen(list_skb)) {
40159d8506ccSHerbert Xu 					BUG_ON(!nfrags);
401613acc94eSYonghong Song 				} else {
401713acc94eSYonghong Song 					BUG_ON(!list_skb->head_frag);
40189d8506ccSHerbert Xu 
401913acc94eSYonghong Song 					/* to make room for head_frag. */
402013acc94eSYonghong Song 					i--;
402113acc94eSYonghong Song 					frag--;
402213acc94eSYonghong Song 				}
4023bf5c25d6SWillem de Bruijn 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4024bf5c25d6SWillem de Bruijn 				    skb_zerocopy_clone(nskb, frag_skb,
4025bf5c25d6SWillem de Bruijn 						       GFP_ATOMIC))
4026bf5c25d6SWillem de Bruijn 					goto err;
4027bf5c25d6SWillem de Bruijn 
40281a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
40299d8506ccSHerbert Xu 			}
40309d8506ccSHerbert Xu 
40319d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
40329d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
40339d8506ccSHerbert Xu 				net_warn_ratelimited(
40349d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
40359d8506ccSHerbert Xu 					pos, mss);
4036ff907a11SEric Dumazet 				err = -EINVAL;
40379d8506ccSHerbert Xu 				goto err;
40389d8506ccSHerbert Xu 			}
40399d8506ccSHerbert Xu 
404013acc94eSYonghong Song 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
40418cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
40428cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
4043f4c50d99SHerbert Xu 
4044f4c50d99SHerbert Xu 			if (pos < offset) {
4045b54c9d5bSJonathan Lemon 				skb_frag_off_add(nskb_frag, offset - pos);
40468cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
4047f4c50d99SHerbert Xu 			}
4048f4c50d99SHerbert Xu 
404989319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
4050f4c50d99SHerbert Xu 
4051f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
4052f4c50d99SHerbert Xu 				i++;
40534e1beba1SMichael S. Tsirkin 				frag++;
4054f4c50d99SHerbert Xu 				pos += size;
4055f4c50d99SHerbert Xu 			} else {
40568cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
405789319d38SHerbert Xu 				goto skip_fraglist;
4058f4c50d99SHerbert Xu 			}
4059f4c50d99SHerbert Xu 
40608cb19905SMichael S. Tsirkin 			nskb_frag++;
4061f4c50d99SHerbert Xu 		}
4062f4c50d99SHerbert Xu 
406389319d38SHerbert Xu skip_fraglist:
4064f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
4065f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
4066f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
4067ec5f0615SPravin B Shelar 
40681cdbcb79SSimon Horman perform_csum_check:
40697fbeffedSAlexander Duyck 		if (!csum) {
4070ff907a11SEric Dumazet 			if (skb_has_shared_frag(nskb) &&
4071ff907a11SEric Dumazet 			    __skb_linearize(nskb))
4072ddff00d4SAlexander Duyck 				goto err;
4073ff907a11SEric Dumazet 
40747fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
4075ec5f0615SPravin B Shelar 				nskb->ip_summed = CHECKSUM_NONE;
407676443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
407776443456SAlexander Duyck 				skb_checksum(nskb, doffset,
407876443456SAlexander Duyck 					     nskb->len - doffset, 0);
40797e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
40807e2b10c1STom Herbert 				skb_headroom(nskb) + doffset;
4081ec5f0615SPravin B Shelar 		}
4082df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
4083f4c50d99SHerbert Xu 
4084bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
4085bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
4086bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
4087bec3cfdcSEric Dumazet 	 */
4088bec3cfdcSEric Dumazet 	segs->prev = tail;
4089432c856fSToshiaki Makita 
4090802ab55aSAlexander Duyck 	if (partial_segs) {
409107b26c94SSteffen Klassert 		struct sk_buff *iter;
4092802ab55aSAlexander Duyck 		int type = skb_shinfo(head_skb)->gso_type;
409307b26c94SSteffen Klassert 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4094802ab55aSAlexander Duyck 
4095802ab55aSAlexander Duyck 		/* Update type to add partial and then remove dodgy if set */
409607b26c94SSteffen Klassert 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4097802ab55aSAlexander Duyck 		type &= ~SKB_GSO_DODGY;
4098802ab55aSAlexander Duyck 
4099802ab55aSAlexander Duyck 		/* Update GSO info and prepare to start updating headers on
4100802ab55aSAlexander Duyck 		 * our way back down the stack of protocols.
4101802ab55aSAlexander Duyck 		 */
410207b26c94SSteffen Klassert 		for (iter = segs; iter; iter = iter->next) {
410307b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_size = gso_size;
410407b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_segs = partial_segs;
410507b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_type = type;
410607b26c94SSteffen Klassert 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
410707b26c94SSteffen Klassert 		}
410807b26c94SSteffen Klassert 
410907b26c94SSteffen Klassert 		if (tail->len - doffset <= gso_size)
411007b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_size = 0;
411107b26c94SSteffen Klassert 		else if (tail != segs)
411207b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4113802ab55aSAlexander Duyck 	}
4114802ab55aSAlexander Duyck 
4115432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
4116432c856fSToshiaki Makita 	 * using skb_set_owner_w().
4117432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
4118432c856fSToshiaki Makita 	 */
4119432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
4120432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
4121432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
4122432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
4123432c856fSToshiaki Makita 	}
4124f4c50d99SHerbert Xu 	return segs;
4125f4c50d99SHerbert Xu 
4126f4c50d99SHerbert Xu err:
4127289dccbeSEric Dumazet 	kfree_skb_list(segs);
4128f4c50d99SHerbert Xu 	return ERR_PTR(err);
4129f4c50d99SHerbert Xu }
4130f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
4131f4c50d99SHerbert Xu 
4132d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
413371d93b39SHerbert Xu {
41348a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
413567147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
413667147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
41378a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
4138715dc1f3SEric Dumazet 	unsigned int delta_truesize;
4139d4546c25SDavid Miller 	struct sk_buff *lp;
414071d93b39SHerbert Xu 
41410ab03f35SSteffen Klassert 	if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
414271d93b39SHerbert Xu 		return -E2BIG;
414371d93b39SHerbert Xu 
414429e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
41458a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
41468a29111cSEric Dumazet 
41478a29111cSEric Dumazet 	if (headlen <= offset) {
414842da6994SHerbert Xu 		skb_frag_t *frag;
414966e92fcfSHerbert Xu 		skb_frag_t *frag2;
41509aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
41519aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
415242da6994SHerbert Xu 
415366e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
41548a29111cSEric Dumazet 			goto merge;
415581705ad1SHerbert Xu 
41568a29111cSEric Dumazet 		offset -= headlen;
41579aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
41589aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
4159f5572068SHerbert Xu 
41609aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
41619aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
416266e92fcfSHerbert Xu 		do {
416366e92fcfSHerbert Xu 			*--frag = *--frag2;
416466e92fcfSHerbert Xu 		} while (--i);
416566e92fcfSHerbert Xu 
4166b54c9d5bSJonathan Lemon 		skb_frag_off_add(frag, offset);
41679e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
416866e92fcfSHerbert Xu 
4169715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
4170ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
4171ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
4172715dc1f3SEric Dumazet 
4173f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
4174f5572068SHerbert Xu 		skb->len -= skb->data_len;
4175f5572068SHerbert Xu 		skb->data_len = 0;
4176f5572068SHerbert Xu 
4177715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
41785d38a079SHerbert Xu 		goto done;
4179d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
4180d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
4181d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
4182d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
4183d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
4184d7e8883cSEric Dumazet 		unsigned int first_offset;
4185d7e8883cSEric Dumazet 
4186d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
41878a29111cSEric Dumazet 			goto merge;
4188d7e8883cSEric Dumazet 
4189d7e8883cSEric Dumazet 		first_offset = skb->data -
4190d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
4191d7e8883cSEric Dumazet 			       offset;
4192d7e8883cSEric Dumazet 
4193d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4194d7e8883cSEric Dumazet 
4195d8e18a51SMatthew Wilcox (Oracle) 		__skb_frag_set_page(frag, page);
4196b54c9d5bSJonathan Lemon 		skb_frag_off_set(frag, first_offset);
4197d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
4198d7e8883cSEric Dumazet 
4199d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4200d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
4201d7e8883cSEric Dumazet 
4202715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4203d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4204d7e8883cSEric Dumazet 		goto done;
42058a29111cSEric Dumazet 	}
420671d93b39SHerbert Xu 
420771d93b39SHerbert Xu merge:
4208715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
420967147ba9SHerbert Xu 	if (offset > headlen) {
4210d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
4211d1dc7abfSMichal Schmidt 
4212b54c9d5bSJonathan Lemon 		skb_frag_off_add(&skbinfo->frags[0], eat);
42139e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
4214d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
4215d1dc7abfSMichal Schmidt 		skb->len -= eat;
421667147ba9SHerbert Xu 		offset = headlen;
421756035022SHerbert Xu 	}
421856035022SHerbert Xu 
421967147ba9SHerbert Xu 	__skb_pull(skb, offset);
422056035022SHerbert Xu 
422129e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
42228a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
42238a29111cSEric Dumazet 	else
4224c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
4225c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
4226f4a775d1SEric Dumazet 	__skb_header_release(skb);
42278a29111cSEric Dumazet 	lp = p;
422871d93b39SHerbert Xu 
42295d38a079SHerbert Xu done:
42305d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
423137fe4732SHerbert Xu 	p->data_len += len;
4232715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
423337fe4732SHerbert Xu 	p->len += len;
42348a29111cSEric Dumazet 	if (lp != p) {
42358a29111cSEric Dumazet 		lp->data_len += len;
42368a29111cSEric Dumazet 		lp->truesize += delta_truesize;
42378a29111cSEric Dumazet 		lp->len += len;
42388a29111cSEric Dumazet 	}
423971d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
424071d93b39SHerbert Xu 	return 0;
424171d93b39SHerbert Xu }
424271d93b39SHerbert Xu 
4243df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
4244df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE	8
4245df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4246df5042f4SFlorian Westphal 
4247df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = {
4248df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4249df5042f4SFlorian Westphal 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4250df5042f4SFlorian Westphal #endif
42514165079bSFlorian Westphal #ifdef CONFIG_XFRM
42524165079bSFlorian Westphal 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
42534165079bSFlorian Westphal #endif
425495a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
425595a7233cSPaul Blakey 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
425695a7233cSPaul Blakey #endif
42573ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
42583ee17bc7SMat Martineau 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
42593ee17bc7SMat Martineau #endif
4260df5042f4SFlorian Westphal };
4261df5042f4SFlorian Westphal 
4262df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void)
4263df5042f4SFlorian Westphal {
4264df5042f4SFlorian Westphal 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4265df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4266df5042f4SFlorian Westphal 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4267df5042f4SFlorian Westphal #endif
42684165079bSFlorian Westphal #ifdef CONFIG_XFRM
42694165079bSFlorian Westphal 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
42704165079bSFlorian Westphal #endif
427195a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
427295a7233cSPaul Blakey 		skb_ext_type_len[TC_SKB_EXT] +
427395a7233cSPaul Blakey #endif
42743ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
42753ee17bc7SMat Martineau 		skb_ext_type_len[SKB_EXT_MPTCP] +
42763ee17bc7SMat Martineau #endif
4277df5042f4SFlorian Westphal 		0;
4278df5042f4SFlorian Westphal }
4279df5042f4SFlorian Westphal 
4280df5042f4SFlorian Westphal static void skb_extensions_init(void)
4281df5042f4SFlorian Westphal {
4282df5042f4SFlorian Westphal 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4283df5042f4SFlorian Westphal 	BUILD_BUG_ON(skb_ext_total_length() > 255);
4284df5042f4SFlorian Westphal 
4285df5042f4SFlorian Westphal 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4286df5042f4SFlorian Westphal 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4287df5042f4SFlorian Westphal 					     0,
4288df5042f4SFlorian Westphal 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4289df5042f4SFlorian Westphal 					     NULL);
4290df5042f4SFlorian Westphal }
4291df5042f4SFlorian Westphal #else
4292df5042f4SFlorian Westphal static void skb_extensions_init(void) {}
4293df5042f4SFlorian Westphal #endif
4294df5042f4SFlorian Westphal 
42951da177e4SLinus Torvalds void __init skb_init(void)
42961da177e4SLinus Torvalds {
429779a8a642SKees Cook 	skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
42981da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
42991da177e4SLinus Torvalds 					      0,
4300e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
430179a8a642SKees Cook 					      offsetof(struct sk_buff, cb),
430279a8a642SKees Cook 					      sizeof_field(struct sk_buff, cb),
430320c2df83SPaul Mundt 					      NULL);
4304d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4305d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
4306d179cd12SDavid S. Miller 						0,
4307e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
430820c2df83SPaul Mundt 						NULL);
4309df5042f4SFlorian Westphal 	skb_extensions_init();
43101da177e4SLinus Torvalds }
43111da177e4SLinus Torvalds 
431251c739d1SDavid S. Miller static int
431348a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
431448a1df65SJason A. Donenfeld 	       unsigned int recursion_level)
4315716ea3a7SDavid Howells {
43161a028e50SDavid S. Miller 	int start = skb_headlen(skb);
43171a028e50SDavid S. Miller 	int i, copy = start - offset;
4318fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
4319716ea3a7SDavid Howells 	int elt = 0;
4320716ea3a7SDavid Howells 
432148a1df65SJason A. Donenfeld 	if (unlikely(recursion_level >= 24))
432248a1df65SJason A. Donenfeld 		return -EMSGSIZE;
432348a1df65SJason A. Donenfeld 
4324716ea3a7SDavid Howells 	if (copy > 0) {
4325716ea3a7SDavid Howells 		if (copy > len)
4326716ea3a7SDavid Howells 			copy = len;
4327642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
4328716ea3a7SDavid Howells 		elt++;
4329716ea3a7SDavid Howells 		if ((len -= copy) == 0)
4330716ea3a7SDavid Howells 			return elt;
4331716ea3a7SDavid Howells 		offset += copy;
4332716ea3a7SDavid Howells 	}
4333716ea3a7SDavid Howells 
4334716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
43351a028e50SDavid S. Miller 		int end;
4336716ea3a7SDavid Howells 
4337547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
43381a028e50SDavid S. Miller 
43399e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4340716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
4341716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
434248a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
434348a1df65SJason A. Donenfeld 				return -EMSGSIZE;
4344716ea3a7SDavid Howells 
4345716ea3a7SDavid Howells 			if (copy > len)
4346716ea3a7SDavid Howells 				copy = len;
4347ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4348b54c9d5bSJonathan Lemon 				    skb_frag_off(frag) + offset - start);
4349716ea3a7SDavid Howells 			elt++;
4350716ea3a7SDavid Howells 			if (!(len -= copy))
4351716ea3a7SDavid Howells 				return elt;
4352716ea3a7SDavid Howells 			offset += copy;
4353716ea3a7SDavid Howells 		}
43541a028e50SDavid S. Miller 		start = end;
4355716ea3a7SDavid Howells 	}
4356716ea3a7SDavid Howells 
4357fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
435848a1df65SJason A. Donenfeld 		int end, ret;
4359716ea3a7SDavid Howells 
4360547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
43611a028e50SDavid S. Miller 
4362fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
4363716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
436448a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
436548a1df65SJason A. Donenfeld 				return -EMSGSIZE;
436648a1df65SJason A. Donenfeld 
4367716ea3a7SDavid Howells 			if (copy > len)
4368716ea3a7SDavid Howells 				copy = len;
436948a1df65SJason A. Donenfeld 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
437048a1df65SJason A. Donenfeld 					      copy, recursion_level + 1);
437148a1df65SJason A. Donenfeld 			if (unlikely(ret < 0))
437248a1df65SJason A. Donenfeld 				return ret;
437348a1df65SJason A. Donenfeld 			elt += ret;
4374716ea3a7SDavid Howells 			if ((len -= copy) == 0)
4375716ea3a7SDavid Howells 				return elt;
4376716ea3a7SDavid Howells 			offset += copy;
4377716ea3a7SDavid Howells 		}
43781a028e50SDavid S. Miller 		start = end;
4379716ea3a7SDavid Howells 	}
4380716ea3a7SDavid Howells 	BUG_ON(len);
4381716ea3a7SDavid Howells 	return elt;
4382716ea3a7SDavid Howells }
4383716ea3a7SDavid Howells 
438448a1df65SJason A. Donenfeld /**
438548a1df65SJason A. Donenfeld  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
438648a1df65SJason A. Donenfeld  *	@skb: Socket buffer containing the buffers to be mapped
438748a1df65SJason A. Donenfeld  *	@sg: The scatter-gather list to map into
438848a1df65SJason A. Donenfeld  *	@offset: The offset into the buffer's contents to start mapping
438948a1df65SJason A. Donenfeld  *	@len: Length of buffer space to be mapped
439048a1df65SJason A. Donenfeld  *
439148a1df65SJason A. Donenfeld  *	Fill the specified scatter-gather list with mappings/pointers into a
439248a1df65SJason A. Donenfeld  *	region of the buffer space attached to a socket buffer. Returns either
439348a1df65SJason A. Donenfeld  *	the number of scatterlist items used, or -EMSGSIZE if the contents
439448a1df65SJason A. Donenfeld  *	could not fit.
439548a1df65SJason A. Donenfeld  */
439648a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
439748a1df65SJason A. Donenfeld {
439848a1df65SJason A. Donenfeld 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
439948a1df65SJason A. Donenfeld 
440048a1df65SJason A. Donenfeld 	if (nsg <= 0)
440148a1df65SJason A. Donenfeld 		return nsg;
440248a1df65SJason A. Donenfeld 
440348a1df65SJason A. Donenfeld 	sg_mark_end(&sg[nsg - 1]);
440448a1df65SJason A. Donenfeld 
440548a1df65SJason A. Donenfeld 	return nsg;
440648a1df65SJason A. Donenfeld }
440748a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec);
440848a1df65SJason A. Donenfeld 
440925a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
441025a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
441125a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
441225a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
441325a91d8dSFan Du  *
441425a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
441525a91d8dSFan Du  * 1. sg_init_table
441625a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
441725a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
441825a91d8dSFan Du  *
441925a91d8dSFan Du  * This is equivalent to:
442025a91d8dSFan Du  * 1. sg_init_table
442125a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
442225a91d8dSFan Du  * 3. sg_unmark_end
442325a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
442425a91d8dSFan Du  *
442525a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
442625a91d8dSFan Du  * is more preferable.
442725a91d8dSFan Du  */
442825a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
442925a91d8dSFan Du 			int offset, int len)
443025a91d8dSFan Du {
443148a1df65SJason A. Donenfeld 	return __skb_to_sgvec(skb, sg, offset, len, 0);
443225a91d8dSFan Du }
443325a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
443425a91d8dSFan Du 
443551c739d1SDavid S. Miller 
443651c739d1SDavid S. Miller 
4437716ea3a7SDavid Howells /**
4438716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4439716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
4440716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
4441716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4442716ea3a7SDavid Howells  *
4443716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
4444716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
4445716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
4446716ea3a7SDavid Howells  *
4447716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
4448716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
4449716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
4450716ea3a7SDavid Howells  *
4451716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
4452716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
4453716ea3a7SDavid Howells  */
4454716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4455716ea3a7SDavid Howells {
4456716ea3a7SDavid Howells 	int copyflag;
4457716ea3a7SDavid Howells 	int elt;
4458716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
4459716ea3a7SDavid Howells 
4460716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
4461716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
4462716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
4463716ea3a7SDavid Howells 	 */
4464716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4465c15fc199SMiaohe Lin 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4466716ea3a7SDavid Howells 		return -ENOMEM;
4467716ea3a7SDavid Howells 
4468716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
446921dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
4470716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
4471716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
4472716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
4473716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
4474716ea3a7SDavid Howells 
4475716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
4476716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4477716ea3a7SDavid Howells 			return -ENOMEM;
4478716ea3a7SDavid Howells 
4479716ea3a7SDavid Howells 		/* Voila! */
4480716ea3a7SDavid Howells 		*trailer = skb;
4481716ea3a7SDavid Howells 		return 1;
4482716ea3a7SDavid Howells 	}
4483716ea3a7SDavid Howells 
4484716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
4485716ea3a7SDavid Howells 
4486716ea3a7SDavid Howells 	elt = 1;
4487716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
4488716ea3a7SDavid Howells 	copyflag = 0;
4489716ea3a7SDavid Howells 
4490716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
4491716ea3a7SDavid Howells 		int ntail = 0;
4492716ea3a7SDavid Howells 
4493716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
4494716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
4495716ea3a7SDavid Howells 		 * after it. */
4496716ea3a7SDavid Howells 
4497716ea3a7SDavid Howells 		if (skb_shared(skb1))
4498716ea3a7SDavid Howells 			copyflag = 1;
4499716ea3a7SDavid Howells 
4500716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
4501716ea3a7SDavid Howells 
4502716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
4503716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
450421dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
4505716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
4506716ea3a7SDavid Howells 				ntail = tailbits + 128;
4507716ea3a7SDavid Howells 		}
4508716ea3a7SDavid Howells 
4509716ea3a7SDavid Howells 		if (copyflag ||
4510716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
4511716ea3a7SDavid Howells 		    ntail ||
4512716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
451321dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
4514716ea3a7SDavid Howells 			struct sk_buff *skb2;
4515716ea3a7SDavid Howells 
4516716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
4517716ea3a7SDavid Howells 			if (ntail == 0)
4518716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
4519716ea3a7SDavid Howells 			else
4520716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
4521716ea3a7SDavid Howells 						       skb_headroom(skb1),
4522716ea3a7SDavid Howells 						       ntail,
4523716ea3a7SDavid Howells 						       GFP_ATOMIC);
4524716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
4525716ea3a7SDavid Howells 				return -ENOMEM;
4526716ea3a7SDavid Howells 
4527716ea3a7SDavid Howells 			if (skb1->sk)
4528716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
4529716ea3a7SDavid Howells 
4530716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
4531716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
4532716ea3a7SDavid Howells 
4533716ea3a7SDavid Howells 			skb2->next = skb1->next;
4534716ea3a7SDavid Howells 			*skb_p = skb2;
4535716ea3a7SDavid Howells 			kfree_skb(skb1);
4536716ea3a7SDavid Howells 			skb1 = skb2;
4537716ea3a7SDavid Howells 		}
4538716ea3a7SDavid Howells 		elt++;
4539716ea3a7SDavid Howells 		*trailer = skb1;
4540716ea3a7SDavid Howells 		skb_p = &skb1->next;
4541716ea3a7SDavid Howells 	}
4542716ea3a7SDavid Howells 
4543716ea3a7SDavid Howells 	return elt;
4544716ea3a7SDavid Howells }
4545b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
4546716ea3a7SDavid Howells 
4547b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
4548b1faf566SEric Dumazet {
4549b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
4550b1faf566SEric Dumazet 
4551b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4552b1faf566SEric Dumazet }
4553b1faf566SEric Dumazet 
45548605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
45558605330aSSoheil Hassas Yeganeh {
45568605330aSSoheil Hassas Yeganeh 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
45578605330aSSoheil Hassas Yeganeh 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
45588605330aSSoheil Hassas Yeganeh 	 */
45598605330aSSoheil Hassas Yeganeh 	skb->pkt_type = PACKET_OUTGOING;
45608605330aSSoheil Hassas Yeganeh 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
45618605330aSSoheil Hassas Yeganeh }
45628605330aSSoheil Hassas Yeganeh 
4563b1faf566SEric Dumazet /*
4564b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4565b1faf566SEric Dumazet  */
4566b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4567b1faf566SEric Dumazet {
4568b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4569ebb3b78dSEric Dumazet 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4570b1faf566SEric Dumazet 		return -ENOMEM;
4571b1faf566SEric Dumazet 
4572b1faf566SEric Dumazet 	skb_orphan(skb);
4573b1faf566SEric Dumazet 	skb->sk = sk;
4574b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
4575b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
45768605330aSSoheil Hassas Yeganeh 	skb_set_err_queue(skb);
4577b1faf566SEric Dumazet 
4578abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
4579abb57ea4SEric Dumazet 	skb_dst_force(skb);
4580abb57ea4SEric Dumazet 
4581b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
4582b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
45836e5d58fdSVinicius Costa Gomes 		sk->sk_error_report(sk);
4584b1faf566SEric Dumazet 	return 0;
4585b1faf566SEric Dumazet }
4586b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
4587b1faf566SEric Dumazet 
458883a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
458983a1a1a7SSoheil Hassas Yeganeh {
459083a1a1a7SSoheil Hassas Yeganeh 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
459183a1a1a7SSoheil Hassas Yeganeh 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
459283a1a1a7SSoheil Hassas Yeganeh }
459383a1a1a7SSoheil Hassas Yeganeh 
4594364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4595364a9e93SWillem de Bruijn {
4596364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
459783a1a1a7SSoheil Hassas Yeganeh 	struct sk_buff *skb, *skb_next = NULL;
459883a1a1a7SSoheil Hassas Yeganeh 	bool icmp_next = false;
4599997d5c3fSEric Dumazet 	unsigned long flags;
4600364a9e93SWillem de Bruijn 
4601997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
4602364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
460338b25793SSoheil Hassas Yeganeh 	if (skb && (skb_next = skb_peek(q))) {
460483a1a1a7SSoheil Hassas Yeganeh 		icmp_next = is_icmp_err_skb(skb_next);
460538b25793SSoheil Hassas Yeganeh 		if (icmp_next)
4606985f7337SWillem de Bruijn 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
460738b25793SSoheil Hassas Yeganeh 	}
4608997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
4609364a9e93SWillem de Bruijn 
461083a1a1a7SSoheil Hassas Yeganeh 	if (is_icmp_err_skb(skb) && !icmp_next)
461183a1a1a7SSoheil Hassas Yeganeh 		sk->sk_err = 0;
461283a1a1a7SSoheil Hassas Yeganeh 
461383a1a1a7SSoheil Hassas Yeganeh 	if (skb_next)
4614364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
4615364a9e93SWillem de Bruijn 
4616364a9e93SWillem de Bruijn 	return skb;
4617364a9e93SWillem de Bruijn }
4618364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
4619364a9e93SWillem de Bruijn 
4620cab41c47SAlexander Duyck /**
4621cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
4622cab41c47SAlexander Duyck  * @skb: the skb to clone
4623cab41c47SAlexander Duyck  *
4624cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
4625cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
4626cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
4627cab41c47SAlexander Duyck  *
4628cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
4629cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
4630cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
4631cab41c47SAlexander Duyck  * the sk_error_queue.
4632cab41c47SAlexander Duyck  */
463362bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
463462bccb8cSAlexander Duyck {
463562bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
463662bccb8cSAlexander Duyck 	struct sk_buff *clone;
463762bccb8cSAlexander Duyck 
463841c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
463962bccb8cSAlexander Duyck 		return NULL;
464062bccb8cSAlexander Duyck 
464162bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
464262bccb8cSAlexander Duyck 	if (!clone) {
464362bccb8cSAlexander Duyck 		sock_put(sk);
464462bccb8cSAlexander Duyck 		return NULL;
464562bccb8cSAlexander Duyck 	}
464662bccb8cSAlexander Duyck 
464762bccb8cSAlexander Duyck 	clone->sk = sk;
464862bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
464962bccb8cSAlexander Duyck 
465062bccb8cSAlexander Duyck 	return clone;
465162bccb8cSAlexander Duyck }
465262bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
465362bccb8cSAlexander Duyck 
465437846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
465537846ef0SAlexander Duyck 					struct sock *sk,
46564ef1b286SSoheil Hassas Yeganeh 					int tstype,
46574ef1b286SSoheil Hassas Yeganeh 					bool opt_stats)
4658ac45f602SPatrick Ohly {
4659ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
4660ac45f602SPatrick Ohly 	int err;
4661ac45f602SPatrick Ohly 
46624ef1b286SSoheil Hassas Yeganeh 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
46634ef1b286SSoheil Hassas Yeganeh 
4664ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
4665ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
4666ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
4667ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4668e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
46694ef1b286SSoheil Hassas Yeganeh 	serr->opt_stats = opt_stats;
46701862d620SWillem de Bruijn 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
46714ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
467209c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
4673ac5cc977SWANG Cong 		if (sk->sk_protocol == IPPROTO_TCP &&
4674ac5cc977SWANG Cong 		    sk->sk_type == SOCK_STREAM)
46754ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
46764ed2d765SWillem de Bruijn 	}
467729030374SEric Dumazet 
4678ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
467929030374SEric Dumazet 
4680ac45f602SPatrick Ohly 	if (err)
4681ac45f602SPatrick Ohly 		kfree_skb(skb);
4682ac45f602SPatrick Ohly }
468337846ef0SAlexander Duyck 
4684b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4685b245be1fSWillem de Bruijn {
4686b245be1fSWillem de Bruijn 	bool ret;
4687b245be1fSWillem de Bruijn 
4688b245be1fSWillem de Bruijn 	if (likely(sysctl_tstamp_allow_data || tsonly))
4689b245be1fSWillem de Bruijn 		return true;
4690b245be1fSWillem de Bruijn 
4691b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
4692b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
4693b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4694b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
4695b245be1fSWillem de Bruijn 	return ret;
4696b245be1fSWillem de Bruijn }
4697b245be1fSWillem de Bruijn 
469837846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
469937846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
470037846ef0SAlexander Duyck {
470137846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
470237846ef0SAlexander Duyck 
4703b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
470435b99dffSWillem de Bruijn 		goto err;
4705b245be1fSWillem de Bruijn 
47069ac25fc0SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
47079ac25fc0SEric Dumazet 	 * but only if the socket refcount is not zero.
47089ac25fc0SEric Dumazet 	 */
470941c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
471037846ef0SAlexander Duyck 		*skb_hwtstamps(skb) = *hwtstamps;
47114ef1b286SSoheil Hassas Yeganeh 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
471237846ef0SAlexander Duyck 		sock_put(sk);
471335b99dffSWillem de Bruijn 		return;
471437846ef0SAlexander Duyck 	}
471535b99dffSWillem de Bruijn 
471635b99dffSWillem de Bruijn err:
471735b99dffSWillem de Bruijn 	kfree_skb(skb);
47189ac25fc0SEric Dumazet }
471937846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
472037846ef0SAlexander Duyck 
472137846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
472237846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
472337846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
472437846ef0SAlexander Duyck {
472537846ef0SAlexander Duyck 	struct sk_buff *skb;
47264ef1b286SSoheil Hassas Yeganeh 	bool tsonly, opt_stats = false;
472737846ef0SAlexander Duyck 
47283a8dd971SWillem de Bruijn 	if (!sk)
47293a8dd971SWillem de Bruijn 		return;
47303a8dd971SWillem de Bruijn 
4731b50a5c70SMiroslav Lichvar 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4732b50a5c70SMiroslav Lichvar 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4733b50a5c70SMiroslav Lichvar 		return;
4734b50a5c70SMiroslav Lichvar 
47353a8dd971SWillem de Bruijn 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
47363a8dd971SWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, tsonly))
473737846ef0SAlexander Duyck 		return;
473837846ef0SAlexander Duyck 
47391c885808SFrancis Yan 	if (tsonly) {
47401c885808SFrancis Yan #ifdef CONFIG_INET
47411c885808SFrancis Yan 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
47421c885808SFrancis Yan 		    sk->sk_protocol == IPPROTO_TCP &&
47434ef1b286SSoheil Hassas Yeganeh 		    sk->sk_type == SOCK_STREAM) {
474448040793SYousuk Seung 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb);
47454ef1b286SSoheil Hassas Yeganeh 			opt_stats = true;
47464ef1b286SSoheil Hassas Yeganeh 		} else
47471c885808SFrancis Yan #endif
47481c885808SFrancis Yan 			skb = alloc_skb(0, GFP_ATOMIC);
47491c885808SFrancis Yan 	} else {
475037846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
47511c885808SFrancis Yan 	}
475237846ef0SAlexander Duyck 	if (!skb)
475337846ef0SAlexander Duyck 		return;
475437846ef0SAlexander Duyck 
475549ca0d8bSWillem de Bruijn 	if (tsonly) {
4756fff88030SWillem de Bruijn 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4757fff88030SWillem de Bruijn 					     SKBTX_ANY_TSTAMP;
475849ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
475949ca0d8bSWillem de Bruijn 	}
476049ca0d8bSWillem de Bruijn 
476149ca0d8bSWillem de Bruijn 	if (hwtstamps)
476249ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
476349ca0d8bSWillem de Bruijn 	else
476449ca0d8bSWillem de Bruijn 		skb->tstamp = ktime_get_real();
476549ca0d8bSWillem de Bruijn 
47664ef1b286SSoheil Hassas Yeganeh 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
476737846ef0SAlexander Duyck }
4768e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4769e7fd2885SWillem de Bruijn 
4770e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
4771e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
4772e7fd2885SWillem de Bruijn {
4773e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4774e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
4775e7fd2885SWillem de Bruijn }
4776ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4777ac45f602SPatrick Ohly 
47786e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
47796e3e939fSJohannes Berg {
47806e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
47816e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
4782dd4f1072SEric Dumazet 	int err = 1;
47836e3e939fSJohannes Berg 
47846e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
47856e3e939fSJohannes Berg 	skb->wifi_acked = acked;
47866e3e939fSJohannes Berg 
47876e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
47886e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
47896e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
47906e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
47916e3e939fSJohannes Berg 
4792dd4f1072SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
4793dd4f1072SEric Dumazet 	 * but only if the socket refcount is not zero.
4794dd4f1072SEric Dumazet 	 */
479541c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
47966e3e939fSJohannes Berg 		err = sock_queue_err_skb(sk, skb);
4797dd4f1072SEric Dumazet 		sock_put(sk);
4798dd4f1072SEric Dumazet 	}
47996e3e939fSJohannes Berg 	if (err)
48006e3e939fSJohannes Berg 		kfree_skb(skb);
48016e3e939fSJohannes Berg }
48026e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
48036e3e939fSJohannes Berg 
4804f35d9d8aSRusty Russell /**
4805f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
4806f35d9d8aSRusty Russell  * @skb: the skb to set
4807f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
4808f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
4809f35d9d8aSRusty Russell  *
4810f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
4811f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4812f35d9d8aSRusty Russell  *
4813f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
4814f35d9d8aSRusty Russell  * returns false you should drop the packet.
4815f35d9d8aSRusty Russell  */
4816f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4817f35d9d8aSRusty Russell {
481852b5d6f5SEric Dumazet 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
481952b5d6f5SEric Dumazet 	u32 csum_start = skb_headroom(skb) + (u32)start;
482052b5d6f5SEric Dumazet 
482152b5d6f5SEric Dumazet 	if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
482252b5d6f5SEric Dumazet 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
482352b5d6f5SEric Dumazet 				     start, off, skb_headroom(skb), skb_headlen(skb));
4824f35d9d8aSRusty Russell 		return false;
4825f35d9d8aSRusty Russell 	}
4826f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
482752b5d6f5SEric Dumazet 	skb->csum_start = csum_start;
4828f35d9d8aSRusty Russell 	skb->csum_offset = off;
4829e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
4830f35d9d8aSRusty Russell 	return true;
4831f35d9d8aSRusty Russell }
4832b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4833f35d9d8aSRusty Russell 
4834ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4835ed1f50c3SPaul Durrant 			       unsigned int max)
4836ed1f50c3SPaul Durrant {
4837ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
4838ed1f50c3SPaul Durrant 		return 0;
4839ed1f50c3SPaul Durrant 
4840ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
4841ed1f50c3SPaul Durrant 	 * won't need to do it again.
4842ed1f50c3SPaul Durrant 	 */
4843ed1f50c3SPaul Durrant 	if (max > skb->len)
4844ed1f50c3SPaul Durrant 		max = skb->len;
4845ed1f50c3SPaul Durrant 
4846ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4847ed1f50c3SPaul Durrant 		return -ENOMEM;
4848ed1f50c3SPaul Durrant 
4849ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
4850ed1f50c3SPaul Durrant 		return -EPROTO;
4851ed1f50c3SPaul Durrant 
4852ed1f50c3SPaul Durrant 	return 0;
4853ed1f50c3SPaul Durrant }
4854ed1f50c3SPaul Durrant 
4855f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
4856f9708b43SJan Beulich 
4857f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4858f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
4859f9708b43SJan Beulich 				      unsigned int off)
4860f9708b43SJan Beulich {
4861f9708b43SJan Beulich 	int err;
4862f9708b43SJan Beulich 
4863161d1792SKees Cook 	switch (proto) {
4864f9708b43SJan Beulich 	case IPPROTO_TCP:
4865f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4866f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
4867f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4868f9708b43SJan Beulich 						  offsetof(struct tcphdr,
4869f9708b43SJan Beulich 							   check)))
4870f9708b43SJan Beulich 			err = -EPROTO;
4871f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4872f9708b43SJan Beulich 
4873f9708b43SJan Beulich 	case IPPROTO_UDP:
4874f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4875f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
4876f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4877f9708b43SJan Beulich 						  offsetof(struct udphdr,
4878f9708b43SJan Beulich 							   check)))
4879f9708b43SJan Beulich 			err = -EPROTO;
4880f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4881f9708b43SJan Beulich 	}
4882f9708b43SJan Beulich 
4883f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
4884f9708b43SJan Beulich }
4885f9708b43SJan Beulich 
4886ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4887ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
4888ed1f50c3SPaul Durrant  */
4889ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
4890ed1f50c3SPaul Durrant 
4891f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4892ed1f50c3SPaul Durrant {
4893ed1f50c3SPaul Durrant 	unsigned int off;
4894ed1f50c3SPaul Durrant 	bool fragment;
4895f9708b43SJan Beulich 	__sum16 *csum;
4896ed1f50c3SPaul Durrant 	int err;
4897ed1f50c3SPaul Durrant 
4898ed1f50c3SPaul Durrant 	fragment = false;
4899ed1f50c3SPaul Durrant 
4900ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
4901ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
4902ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
4903ed1f50c3SPaul Durrant 	if (err < 0)
4904ed1f50c3SPaul Durrant 		goto out;
4905ed1f50c3SPaul Durrant 
490611f920d2SMiaohe Lin 	if (ip_is_fragment(ip_hdr(skb)))
4907ed1f50c3SPaul Durrant 		fragment = true;
4908ed1f50c3SPaul Durrant 
4909ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
4910ed1f50c3SPaul Durrant 
4911ed1f50c3SPaul Durrant 	err = -EPROTO;
4912ed1f50c3SPaul Durrant 
4913ed1f50c3SPaul Durrant 	if (fragment)
4914ed1f50c3SPaul Durrant 		goto out;
4915ed1f50c3SPaul Durrant 
4916f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4917f9708b43SJan Beulich 	if (IS_ERR(csum))
4918f9708b43SJan Beulich 		return PTR_ERR(csum);
4919ed1f50c3SPaul Durrant 
4920ed1f50c3SPaul Durrant 	if (recalculate)
4921f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4922ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
4923ed1f50c3SPaul Durrant 					   skb->len - off,
4924f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
4925ed1f50c3SPaul Durrant 	err = 0;
4926ed1f50c3SPaul Durrant 
4927ed1f50c3SPaul Durrant out:
4928ed1f50c3SPaul Durrant 	return err;
4929ed1f50c3SPaul Durrant }
4930ed1f50c3SPaul Durrant 
4931ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4932ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
4933ed1f50c3SPaul Durrant  */
4934ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
4935ed1f50c3SPaul Durrant 
4936ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
4937ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
4938ed1f50c3SPaul Durrant 
4939ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4940ed1f50c3SPaul Durrant {
4941ed1f50c3SPaul Durrant 	int err;
4942ed1f50c3SPaul Durrant 	u8 nexthdr;
4943ed1f50c3SPaul Durrant 	unsigned int off;
4944ed1f50c3SPaul Durrant 	unsigned int len;
4945ed1f50c3SPaul Durrant 	bool fragment;
4946ed1f50c3SPaul Durrant 	bool done;
4947f9708b43SJan Beulich 	__sum16 *csum;
4948ed1f50c3SPaul Durrant 
4949ed1f50c3SPaul Durrant 	fragment = false;
4950ed1f50c3SPaul Durrant 	done = false;
4951ed1f50c3SPaul Durrant 
4952ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
4953ed1f50c3SPaul Durrant 
4954ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4955ed1f50c3SPaul Durrant 	if (err < 0)
4956ed1f50c3SPaul Durrant 		goto out;
4957ed1f50c3SPaul Durrant 
4958ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
4959ed1f50c3SPaul Durrant 
4960ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4961ed1f50c3SPaul Durrant 	while (off <= len && !done) {
4962ed1f50c3SPaul Durrant 		switch (nexthdr) {
4963ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
4964ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
4965ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
4966ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
4967ed1f50c3SPaul Durrant 
4968ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4969ed1f50c3SPaul Durrant 						  off +
4970ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
4971ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4972ed1f50c3SPaul Durrant 			if (err < 0)
4973ed1f50c3SPaul Durrant 				goto out;
4974ed1f50c3SPaul Durrant 
4975ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4976ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4977ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
4978ed1f50c3SPaul Durrant 			break;
4979ed1f50c3SPaul Durrant 		}
4980ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
4981ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
4982ed1f50c3SPaul Durrant 
4983ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4984ed1f50c3SPaul Durrant 						  off +
4985ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
4986ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4987ed1f50c3SPaul Durrant 			if (err < 0)
4988ed1f50c3SPaul Durrant 				goto out;
4989ed1f50c3SPaul Durrant 
4990ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4991ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4992ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
4993ed1f50c3SPaul Durrant 			break;
4994ed1f50c3SPaul Durrant 		}
4995ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
4996ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
4997ed1f50c3SPaul Durrant 
4998ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4999ed1f50c3SPaul Durrant 						  off +
5000ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
5001ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
5002ed1f50c3SPaul Durrant 			if (err < 0)
5003ed1f50c3SPaul Durrant 				goto out;
5004ed1f50c3SPaul Durrant 
5005ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
5006ed1f50c3SPaul Durrant 
5007ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5008ed1f50c3SPaul Durrant 				fragment = true;
5009ed1f50c3SPaul Durrant 
5010ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
5011ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
5012ed1f50c3SPaul Durrant 			break;
5013ed1f50c3SPaul Durrant 		}
5014ed1f50c3SPaul Durrant 		default:
5015ed1f50c3SPaul Durrant 			done = true;
5016ed1f50c3SPaul Durrant 			break;
5017ed1f50c3SPaul Durrant 		}
5018ed1f50c3SPaul Durrant 	}
5019ed1f50c3SPaul Durrant 
5020ed1f50c3SPaul Durrant 	err = -EPROTO;
5021ed1f50c3SPaul Durrant 
5022ed1f50c3SPaul Durrant 	if (!done || fragment)
5023ed1f50c3SPaul Durrant 		goto out;
5024ed1f50c3SPaul Durrant 
5025f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
5026f9708b43SJan Beulich 	if (IS_ERR(csum))
5027f9708b43SJan Beulich 		return PTR_ERR(csum);
5028ed1f50c3SPaul Durrant 
5029ed1f50c3SPaul Durrant 	if (recalculate)
5030f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5031ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
5032f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
5033ed1f50c3SPaul Durrant 	err = 0;
5034ed1f50c3SPaul Durrant 
5035ed1f50c3SPaul Durrant out:
5036ed1f50c3SPaul Durrant 	return err;
5037ed1f50c3SPaul Durrant }
5038ed1f50c3SPaul Durrant 
5039ed1f50c3SPaul Durrant /**
5040ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
5041ed1f50c3SPaul Durrant  * @skb: the skb to set up
5042ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
5043ed1f50c3SPaul Durrant  */
5044ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5045ed1f50c3SPaul Durrant {
5046ed1f50c3SPaul Durrant 	int err;
5047ed1f50c3SPaul Durrant 
5048ed1f50c3SPaul Durrant 	switch (skb->protocol) {
5049ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
5050f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
5051ed1f50c3SPaul Durrant 		break;
5052ed1f50c3SPaul Durrant 
5053ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
5054ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
5055ed1f50c3SPaul Durrant 		break;
5056ed1f50c3SPaul Durrant 
5057ed1f50c3SPaul Durrant 	default:
5058ed1f50c3SPaul Durrant 		err = -EPROTO;
5059ed1f50c3SPaul Durrant 		break;
5060ed1f50c3SPaul Durrant 	}
5061ed1f50c3SPaul Durrant 
5062ed1f50c3SPaul Durrant 	return err;
5063ed1f50c3SPaul Durrant }
5064ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
5065ed1f50c3SPaul Durrant 
50669afd85c9SLinus Lüssing /**
50679afd85c9SLinus Lüssing  * skb_checksum_maybe_trim - maybe trims the given skb
50689afd85c9SLinus Lüssing  * @skb: the skb to check
50699afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
50709afd85c9SLinus Lüssing  *
50719afd85c9SLinus Lüssing  * Checks whether the given skb has data beyond the given transport length.
50729afd85c9SLinus Lüssing  * If so, returns a cloned skb trimmed to this transport length.
50739afd85c9SLinus Lüssing  * Otherwise returns the provided skb. Returns NULL in error cases
50749afd85c9SLinus Lüssing  * (e.g. transport_len exceeds skb length or out-of-memory).
50759afd85c9SLinus Lüssing  *
5076a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5077a516993fSLinus Lüssing  * differs from the provided skb.
50789afd85c9SLinus Lüssing  */
50799afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
50809afd85c9SLinus Lüssing 					       unsigned int transport_len)
50819afd85c9SLinus Lüssing {
50829afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
50839afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + transport_len;
50849afd85c9SLinus Lüssing 	int ret;
50859afd85c9SLinus Lüssing 
5086a516993fSLinus Lüssing 	if (skb->len < len)
50879afd85c9SLinus Lüssing 		return NULL;
5088a516993fSLinus Lüssing 	else if (skb->len == len)
50899afd85c9SLinus Lüssing 		return skb;
50909afd85c9SLinus Lüssing 
50919afd85c9SLinus Lüssing 	skb_chk = skb_clone(skb, GFP_ATOMIC);
50929afd85c9SLinus Lüssing 	if (!skb_chk)
50939afd85c9SLinus Lüssing 		return NULL;
50949afd85c9SLinus Lüssing 
50959afd85c9SLinus Lüssing 	ret = pskb_trim_rcsum(skb_chk, len);
50969afd85c9SLinus Lüssing 	if (ret) {
50979afd85c9SLinus Lüssing 		kfree_skb(skb_chk);
50989afd85c9SLinus Lüssing 		return NULL;
50999afd85c9SLinus Lüssing 	}
51009afd85c9SLinus Lüssing 
51019afd85c9SLinus Lüssing 	return skb_chk;
51029afd85c9SLinus Lüssing }
51039afd85c9SLinus Lüssing 
51049afd85c9SLinus Lüssing /**
51059afd85c9SLinus Lüssing  * skb_checksum_trimmed - validate checksum of an skb
51069afd85c9SLinus Lüssing  * @skb: the skb to check
51079afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
51089afd85c9SLinus Lüssing  * @skb_chkf: checksum function to use
51099afd85c9SLinus Lüssing  *
51109afd85c9SLinus Lüssing  * Applies the given checksum function skb_chkf to the provided skb.
51119afd85c9SLinus Lüssing  * Returns a checked and maybe trimmed skb. Returns NULL on error.
51129afd85c9SLinus Lüssing  *
51139afd85c9SLinus Lüssing  * If the skb has data beyond the given transport length, then a
51149afd85c9SLinus Lüssing  * trimmed & cloned skb is checked and returned.
51159afd85c9SLinus Lüssing  *
5116a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5117a516993fSLinus Lüssing  * differs from the provided skb.
51189afd85c9SLinus Lüssing  */
51199afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
51209afd85c9SLinus Lüssing 				     unsigned int transport_len,
51219afd85c9SLinus Lüssing 				     __sum16(*skb_chkf)(struct sk_buff *skb))
51229afd85c9SLinus Lüssing {
51239afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
51249afd85c9SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
5125fcba67c9SLinus Lüssing 	__sum16 ret;
51269afd85c9SLinus Lüssing 
51279afd85c9SLinus Lüssing 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
51289afd85c9SLinus Lüssing 	if (!skb_chk)
5129a516993fSLinus Lüssing 		goto err;
51309afd85c9SLinus Lüssing 
5131a516993fSLinus Lüssing 	if (!pskb_may_pull(skb_chk, offset))
5132a516993fSLinus Lüssing 		goto err;
51339afd85c9SLinus Lüssing 
51349b368814SLinus Lüssing 	skb_pull_rcsum(skb_chk, offset);
51359afd85c9SLinus Lüssing 	ret = skb_chkf(skb_chk);
51369b368814SLinus Lüssing 	skb_push_rcsum(skb_chk, offset);
51379afd85c9SLinus Lüssing 
5138a516993fSLinus Lüssing 	if (ret)
5139a516993fSLinus Lüssing 		goto err;
51409afd85c9SLinus Lüssing 
51419afd85c9SLinus Lüssing 	return skb_chk;
5142a516993fSLinus Lüssing 
5143a516993fSLinus Lüssing err:
5144a516993fSLinus Lüssing 	if (skb_chk && skb_chk != skb)
5145a516993fSLinus Lüssing 		kfree_skb(skb_chk);
5146a516993fSLinus Lüssing 
5147a516993fSLinus Lüssing 	return NULL;
5148a516993fSLinus Lüssing 
51499afd85c9SLinus Lüssing }
51509afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
51519afd85c9SLinus Lüssing 
51524497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
51534497b076SBen Hutchings {
5154e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5155e87cc472SJoe Perches 			     skb->dev->name);
51564497b076SBen Hutchings }
51574497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5158bad43ca8SEric Dumazet 
5159bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5160bad43ca8SEric Dumazet {
51613d861f66SEric Dumazet 	if (head_stolen) {
51623d861f66SEric Dumazet 		skb_release_head_state(skb);
5163bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
51643d861f66SEric Dumazet 	} else {
5165bad43ca8SEric Dumazet 		__kfree_skb(skb);
5166bad43ca8SEric Dumazet 	}
51673d861f66SEric Dumazet }
5168bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
5169bad43ca8SEric Dumazet 
5170bad43ca8SEric Dumazet /**
5171bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
5172bad43ca8SEric Dumazet  * @to: prior buffer
5173bad43ca8SEric Dumazet  * @from: buffer to add
5174bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
5175c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
5176bad43ca8SEric Dumazet  */
5177bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5178bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
5179bad43ca8SEric Dumazet {
5180c818fa9eSEric Dumazet 	struct skb_shared_info *to_shinfo, *from_shinfo;
5181bad43ca8SEric Dumazet 	int i, delta, len = from->len;
5182bad43ca8SEric Dumazet 
5183bad43ca8SEric Dumazet 	*fragstolen = false;
5184bad43ca8SEric Dumazet 
5185bad43ca8SEric Dumazet 	if (skb_cloned(to))
5186bad43ca8SEric Dumazet 		return false;
5187bad43ca8SEric Dumazet 
5188bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
5189e93a0435SEric Dumazet 		if (len)
5190bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5191bad43ca8SEric Dumazet 		*delta_truesize = 0;
5192bad43ca8SEric Dumazet 		return true;
5193bad43ca8SEric Dumazet 	}
5194bad43ca8SEric Dumazet 
5195c818fa9eSEric Dumazet 	to_shinfo = skb_shinfo(to);
5196c818fa9eSEric Dumazet 	from_shinfo = skb_shinfo(from);
5197c818fa9eSEric Dumazet 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5198bad43ca8SEric Dumazet 		return false;
51991f8b977aSWillem de Bruijn 	if (skb_zcopy(to) || skb_zcopy(from))
52001f8b977aSWillem de Bruijn 		return false;
5201bad43ca8SEric Dumazet 
5202bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
5203bad43ca8SEric Dumazet 		struct page *page;
5204bad43ca8SEric Dumazet 		unsigned int offset;
5205bad43ca8SEric Dumazet 
5206c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5207c818fa9eSEric Dumazet 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5208bad43ca8SEric Dumazet 			return false;
5209bad43ca8SEric Dumazet 
5210bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
5211bad43ca8SEric Dumazet 			return false;
5212bad43ca8SEric Dumazet 
5213bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5214bad43ca8SEric Dumazet 
5215bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
5216bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
5217bad43ca8SEric Dumazet 
5218c818fa9eSEric Dumazet 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5219bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
5220bad43ca8SEric Dumazet 		*fragstolen = true;
5221bad43ca8SEric Dumazet 	} else {
5222c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5223c818fa9eSEric Dumazet 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5224bad43ca8SEric Dumazet 			return false;
5225bad43ca8SEric Dumazet 
5226f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5227bad43ca8SEric Dumazet 	}
5228bad43ca8SEric Dumazet 
5229bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
5230bad43ca8SEric Dumazet 
5231c818fa9eSEric Dumazet 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5232c818fa9eSEric Dumazet 	       from_shinfo->frags,
5233c818fa9eSEric Dumazet 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5234c818fa9eSEric Dumazet 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5235bad43ca8SEric Dumazet 
5236bad43ca8SEric Dumazet 	if (!skb_cloned(from))
5237c818fa9eSEric Dumazet 		from_shinfo->nr_frags = 0;
5238bad43ca8SEric Dumazet 
52398ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
52408ea853fdSLi RongQing 	 * since we set nr_frags to 0.
52418ea853fdSLi RongQing 	 */
5242c818fa9eSEric Dumazet 	for (i = 0; i < from_shinfo->nr_frags; i++)
5243c818fa9eSEric Dumazet 		__skb_frag_ref(&from_shinfo->frags[i]);
5244bad43ca8SEric Dumazet 
5245bad43ca8SEric Dumazet 	to->truesize += delta;
5246bad43ca8SEric Dumazet 	to->len += len;
5247bad43ca8SEric Dumazet 	to->data_len += len;
5248bad43ca8SEric Dumazet 
5249bad43ca8SEric Dumazet 	*delta_truesize = delta;
5250bad43ca8SEric Dumazet 	return true;
5251bad43ca8SEric Dumazet }
5252bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
5253621e84d6SNicolas Dichtel 
5254621e84d6SNicolas Dichtel /**
52558b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
5256621e84d6SNicolas Dichtel  *
5257621e84d6SNicolas Dichtel  * @skb: buffer to clean
52588b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
5259621e84d6SNicolas Dichtel  *
52608b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
52618b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
52628b27f277SNicolas Dichtel  * operations.
52638b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
52648b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
52658b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
5266621e84d6SNicolas Dichtel  */
52678b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5268621e84d6SNicolas Dichtel {
5269621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
5270621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
527160ff7467SWANG Cong 	skb->ignore_df = 0;
5272621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
5273174e2381SFlorian Westphal 	skb_ext_reset(skb);
5274895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
5275621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
5276213dd74aSHerbert Xu 
52776f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV
52786f9a5069SPetr Machata 	skb->offload_fwd_mark = 0;
5279875e8939SIdo Schimmel 	skb->offload_l3_fwd_mark = 0;
52806f9a5069SPetr Machata #endif
52816f9a5069SPetr Machata 
5282213dd74aSHerbert Xu 	if (!xnet)
5283213dd74aSHerbert Xu 		return;
5284213dd74aSHerbert Xu 
52852b5ec1a5SYe Yin 	ipvs_reset(skb);
5286213dd74aSHerbert Xu 	skb->mark = 0;
5287c47d8c2fSJesus Sanchez-Palencia 	skb->tstamp = 0;
5288621e84d6SNicolas Dichtel }
5289621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
5290de960aa9SFlorian Westphal 
5291de960aa9SFlorian Westphal /**
5292de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5293de960aa9SFlorian Westphal  *
5294de960aa9SFlorian Westphal  * @skb: GSO skb
5295de960aa9SFlorian Westphal  *
5296de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
5297de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
5298de960aa9SFlorian Westphal  *
5299de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5300de960aa9SFlorian Westphal  */
5301a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5302de960aa9SFlorian Westphal {
5303de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5304f993bc25SFlorian Westphal 	unsigned int thlen = 0;
5305f993bc25SFlorian Westphal 
5306f993bc25SFlorian Westphal 	if (skb->encapsulation) {
5307f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
5308f993bc25SFlorian Westphal 			skb_transport_header(skb);
5309de960aa9SFlorian Westphal 
5310de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5311f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
5312f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5313f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
53141dd27cdeSDaniel Axtens 	} else if (unlikely(skb_is_gso_sctp(skb))) {
531590017accSMarcelo Ricardo Leitner 		thlen = sizeof(struct sctphdr);
5316ee80d1ebSWillem de Bruijn 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5317ee80d1ebSWillem de Bruijn 		thlen = sizeof(struct udphdr);
5318f993bc25SFlorian Westphal 	}
53196d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
53206d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
53216d39d589SFlorian Westphal 	 * accounted for.
53226d39d589SFlorian Westphal 	 */
5323f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
5324de960aa9SFlorian Westphal }
5325a4a77718SDaniel Axtens 
5326a4a77718SDaniel Axtens /**
5327a4a77718SDaniel Axtens  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5328a4a77718SDaniel Axtens  *
5329a4a77718SDaniel Axtens  * @skb: GSO skb
5330a4a77718SDaniel Axtens  *
5331a4a77718SDaniel Axtens  * skb_gso_network_seglen is used to determine the real size of the
5332a4a77718SDaniel Axtens  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5333a4a77718SDaniel Axtens  *
5334a4a77718SDaniel Axtens  * The MAC/L2 header is not accounted for.
5335a4a77718SDaniel Axtens  */
5336a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5337a4a77718SDaniel Axtens {
5338a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) -
5339a4a77718SDaniel Axtens 			       skb_network_header(skb);
5340a4a77718SDaniel Axtens 
5341a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5342a4a77718SDaniel Axtens }
5343a4a77718SDaniel Axtens 
5344a4a77718SDaniel Axtens /**
5345a4a77718SDaniel Axtens  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5346a4a77718SDaniel Axtens  *
5347a4a77718SDaniel Axtens  * @skb: GSO skb
5348a4a77718SDaniel Axtens  *
5349a4a77718SDaniel Axtens  * skb_gso_mac_seglen is used to determine the real size of the
5350a4a77718SDaniel Axtens  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5351a4a77718SDaniel Axtens  * headers (TCP/UDP).
5352a4a77718SDaniel Axtens  */
5353a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5354a4a77718SDaniel Axtens {
5355a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5356a4a77718SDaniel Axtens 
5357a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5358a4a77718SDaniel Axtens }
53590d5501c1SVlad Yasevich 
5360ae7ef81eSMarcelo Ricardo Leitner /**
53612b16f048SDaniel Axtens  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
53622b16f048SDaniel Axtens  *
53632b16f048SDaniel Axtens  * There are a couple of instances where we have a GSO skb, and we
53642b16f048SDaniel Axtens  * want to determine what size it would be after it is segmented.
53652b16f048SDaniel Axtens  *
53662b16f048SDaniel Axtens  * We might want to check:
53672b16f048SDaniel Axtens  * -    L3+L4+payload size (e.g. IP forwarding)
53682b16f048SDaniel Axtens  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
53692b16f048SDaniel Axtens  *
53702b16f048SDaniel Axtens  * This is a helper to do that correctly considering GSO_BY_FRAGS.
53712b16f048SDaniel Axtens  *
537249682bfaSMathieu Malaterre  * @skb: GSO skb
537349682bfaSMathieu Malaterre  *
53742b16f048SDaniel Axtens  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
53752b16f048SDaniel Axtens  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
53762b16f048SDaniel Axtens  *
53772b16f048SDaniel Axtens  * @max_len: The maximum permissible length.
53782b16f048SDaniel Axtens  *
53792b16f048SDaniel Axtens  * Returns true if the segmented length <= max length.
53802b16f048SDaniel Axtens  */
53812b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb,
53822b16f048SDaniel Axtens 				      unsigned int seg_len,
53832b16f048SDaniel Axtens 				      unsigned int max_len) {
53842b16f048SDaniel Axtens 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
53852b16f048SDaniel Axtens 	const struct sk_buff *iter;
53862b16f048SDaniel Axtens 
53872b16f048SDaniel Axtens 	if (shinfo->gso_size != GSO_BY_FRAGS)
53882b16f048SDaniel Axtens 		return seg_len <= max_len;
53892b16f048SDaniel Axtens 
53902b16f048SDaniel Axtens 	/* Undo this so we can re-use header sizes */
53912b16f048SDaniel Axtens 	seg_len -= GSO_BY_FRAGS;
53922b16f048SDaniel Axtens 
53932b16f048SDaniel Axtens 	skb_walk_frags(skb, iter) {
53942b16f048SDaniel Axtens 		if (seg_len + skb_headlen(iter) > max_len)
53952b16f048SDaniel Axtens 			return false;
53962b16f048SDaniel Axtens 	}
53972b16f048SDaniel Axtens 
53982b16f048SDaniel Axtens 	return true;
53992b16f048SDaniel Axtens }
54002b16f048SDaniel Axtens 
54012b16f048SDaniel Axtens /**
5402779b7931SDaniel Axtens  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5403ae7ef81eSMarcelo Ricardo Leitner  *
5404ae7ef81eSMarcelo Ricardo Leitner  * @skb: GSO skb
540576f21b99SDavid S. Miller  * @mtu: MTU to validate against
5406ae7ef81eSMarcelo Ricardo Leitner  *
5407779b7931SDaniel Axtens  * skb_gso_validate_network_len validates if a given skb will fit a
5408779b7931SDaniel Axtens  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5409779b7931SDaniel Axtens  * payload.
5410ae7ef81eSMarcelo Ricardo Leitner  */
5411779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5412ae7ef81eSMarcelo Ricardo Leitner {
54132b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5414ae7ef81eSMarcelo Ricardo Leitner }
5415779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5416ae7ef81eSMarcelo Ricardo Leitner 
54172b16f048SDaniel Axtens /**
54182b16f048SDaniel Axtens  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
54192b16f048SDaniel Axtens  *
54202b16f048SDaniel Axtens  * @skb: GSO skb
54212b16f048SDaniel Axtens  * @len: length to validate against
54222b16f048SDaniel Axtens  *
54232b16f048SDaniel Axtens  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
54242b16f048SDaniel Axtens  * length once split, including L2, L3 and L4 headers and the payload.
54252b16f048SDaniel Axtens  */
54262b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
54272b16f048SDaniel Axtens {
54282b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
54292b16f048SDaniel Axtens }
54302b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
54312b16f048SDaniel Axtens 
54320d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
54330d5501c1SVlad Yasevich {
5434d85e8be2SYuya Kusakabe 	int mac_len, meta_len;
5435d85e8be2SYuya Kusakabe 	void *meta;
54364bbb3e0eSToshiaki Makita 
54370d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
54380d5501c1SVlad Yasevich 		kfree_skb(skb);
54390d5501c1SVlad Yasevich 		return NULL;
54400d5501c1SVlad Yasevich 	}
54410d5501c1SVlad Yasevich 
54424bbb3e0eSToshiaki Makita 	mac_len = skb->data - skb_mac_header(skb);
5443ae474573SToshiaki Makita 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
54444bbb3e0eSToshiaki Makita 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
54454bbb3e0eSToshiaki Makita 			mac_len - VLAN_HLEN - ETH_TLEN);
5446ae474573SToshiaki Makita 	}
5447d85e8be2SYuya Kusakabe 
5448d85e8be2SYuya Kusakabe 	meta_len = skb_metadata_len(skb);
5449d85e8be2SYuya Kusakabe 	if (meta_len) {
5450d85e8be2SYuya Kusakabe 		meta = skb_metadata_end(skb) - meta_len;
5451d85e8be2SYuya Kusakabe 		memmove(meta + VLAN_HLEN, meta, meta_len);
5452d85e8be2SYuya Kusakabe 	}
5453d85e8be2SYuya Kusakabe 
54540d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
54550d5501c1SVlad Yasevich 	return skb;
54560d5501c1SVlad Yasevich }
54570d5501c1SVlad Yasevich 
54580d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
54590d5501c1SVlad Yasevich {
54600d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
54610d5501c1SVlad Yasevich 	u16 vlan_tci;
54620d5501c1SVlad Yasevich 
5463df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
54640d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
54650d5501c1SVlad Yasevich 		return skb;
54660d5501c1SVlad Yasevich 	}
54670d5501c1SVlad Yasevich 
54680d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
54690d5501c1SVlad Yasevich 	if (unlikely(!skb))
54700d5501c1SVlad Yasevich 		goto err_free;
547155eff0ebSMiaohe Lin 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
547255eff0ebSMiaohe Lin 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
54730d5501c1SVlad Yasevich 		goto err_free;
54740d5501c1SVlad Yasevich 
54750d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
54760d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
54770d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
54780d5501c1SVlad Yasevich 
54790d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
54800d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
54810d5501c1SVlad Yasevich 
54820d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
54830d5501c1SVlad Yasevich 	if (unlikely(!skb))
54840d5501c1SVlad Yasevich 		goto err_free;
54850d5501c1SVlad Yasevich 
54860d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
54878be33ecfSAlexander Lobakin 	if (!skb_transport_header_was_set(skb))
54880d5501c1SVlad Yasevich 		skb_reset_transport_header(skb);
54890d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
54900d5501c1SVlad Yasevich 
54910d5501c1SVlad Yasevich 	return skb;
54920d5501c1SVlad Yasevich 
54930d5501c1SVlad Yasevich err_free:
54940d5501c1SVlad Yasevich 	kfree_skb(skb);
54950d5501c1SVlad Yasevich 	return NULL;
54960d5501c1SVlad Yasevich }
54970d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
54982e4e4410SEric Dumazet 
5499e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
5500e2195121SJiri Pirko {
5501e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
5502e2195121SJiri Pirko 		return -ENOMEM;
5503e2195121SJiri Pirko 
5504e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5505e2195121SJiri Pirko 		return 0;
5506e2195121SJiri Pirko 
5507e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5508e2195121SJiri Pirko }
5509e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
5510e2195121SJiri Pirko 
5511bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
5512bfca4c52SShmulik Ladkani  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5513bfca4c52SShmulik Ladkani  */
5514bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
551593515d53SJiri Pirko {
551693515d53SJiri Pirko 	struct vlan_hdr *vhdr;
5517b6a79208SShmulik Ladkani 	int offset = skb->data - skb_mac_header(skb);
551893515d53SJiri Pirko 	int err;
551993515d53SJiri Pirko 
5520b6a79208SShmulik Ladkani 	if (WARN_ONCE(offset,
5521b6a79208SShmulik Ladkani 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5522b6a79208SShmulik Ladkani 		      offset)) {
5523b6a79208SShmulik Ladkani 		return -EINVAL;
5524b6a79208SShmulik Ladkani 	}
5525b6a79208SShmulik Ladkani 
552693515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
552793515d53SJiri Pirko 	if (unlikely(err))
5528b6a79208SShmulik Ladkani 		return err;
552993515d53SJiri Pirko 
553093515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
553193515d53SJiri Pirko 
553293515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
553393515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
553493515d53SJiri Pirko 
553593515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
553693515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
553793515d53SJiri Pirko 
553893515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
553993515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
554093515d53SJiri Pirko 
554193515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
554293515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
554393515d53SJiri Pirko 
554493515d53SJiri Pirko 	skb_reset_mac_len(skb);
554593515d53SJiri Pirko 
554693515d53SJiri Pirko 	return err;
554793515d53SJiri Pirko }
5548bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
554993515d53SJiri Pirko 
5550b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
5551b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5552b6a79208SShmulik Ladkani  */
555393515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
555493515d53SJiri Pirko {
555593515d53SJiri Pirko 	u16 vlan_tci;
555693515d53SJiri Pirko 	__be16 vlan_proto;
555793515d53SJiri Pirko 	int err;
555893515d53SJiri Pirko 
5559df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
5560b1817524SMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
556193515d53SJiri Pirko 	} else {
5562ecf4ee41SShmulik Ladkani 		if (unlikely(!eth_type_vlan(skb->protocol)))
556393515d53SJiri Pirko 			return 0;
556493515d53SJiri Pirko 
556593515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
556693515d53SJiri Pirko 		if (err)
556793515d53SJiri Pirko 			return err;
556893515d53SJiri Pirko 	}
556993515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
5570ecf4ee41SShmulik Ladkani 	if (likely(!eth_type_vlan(skb->protocol)))
557193515d53SJiri Pirko 		return 0;
557293515d53SJiri Pirko 
557393515d53SJiri Pirko 	vlan_proto = skb->protocol;
557493515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
557593515d53SJiri Pirko 	if (unlikely(err))
557693515d53SJiri Pirko 		return err;
557793515d53SJiri Pirko 
557893515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
557993515d53SJiri Pirko 	return 0;
558093515d53SJiri Pirko }
558193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
558293515d53SJiri Pirko 
5583b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5584b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5585b6a79208SShmulik Ladkani  */
558693515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
558793515d53SJiri Pirko {
5588df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
5589b6a79208SShmulik Ladkani 		int offset = skb->data - skb_mac_header(skb);
559093515d53SJiri Pirko 		int err;
559193515d53SJiri Pirko 
5592b6a79208SShmulik Ladkani 		if (WARN_ONCE(offset,
5593b6a79208SShmulik Ladkani 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5594b6a79208SShmulik Ladkani 			      offset)) {
5595b6a79208SShmulik Ladkani 			return -EINVAL;
5596b6a79208SShmulik Ladkani 		}
5597b6a79208SShmulik Ladkani 
559893515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
5599df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
5600b6a79208SShmulik Ladkani 		if (err)
560193515d53SJiri Pirko 			return err;
56029241e2dfSDaniel Borkmann 
560393515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
560493515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
560593515d53SJiri Pirko 
56066b83d28aSDaniel Borkmann 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
560793515d53SJiri Pirko 	}
560893515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
560993515d53SJiri Pirko 	return 0;
561093515d53SJiri Pirko }
561193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
561293515d53SJiri Pirko 
561319fbcb36SGuillaume Nault /**
561419fbcb36SGuillaume Nault  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
561519fbcb36SGuillaume Nault  *
561619fbcb36SGuillaume Nault  * @skb: Socket buffer to modify
561719fbcb36SGuillaume Nault  *
561819fbcb36SGuillaume Nault  * Drop the Ethernet header of @skb.
561919fbcb36SGuillaume Nault  *
562019fbcb36SGuillaume Nault  * Expects that skb->data points to the mac header and that no VLAN tags are
562119fbcb36SGuillaume Nault  * present.
562219fbcb36SGuillaume Nault  *
562319fbcb36SGuillaume Nault  * Returns 0 on success, -errno otherwise.
562419fbcb36SGuillaume Nault  */
562519fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb)
562619fbcb36SGuillaume Nault {
562719fbcb36SGuillaume Nault 	if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
562819fbcb36SGuillaume Nault 	    skb_network_offset(skb) < ETH_HLEN)
562919fbcb36SGuillaume Nault 		return -EPROTO;
563019fbcb36SGuillaume Nault 
563119fbcb36SGuillaume Nault 	skb_pull_rcsum(skb, ETH_HLEN);
563219fbcb36SGuillaume Nault 	skb_reset_mac_header(skb);
563319fbcb36SGuillaume Nault 	skb_reset_mac_len(skb);
563419fbcb36SGuillaume Nault 
563519fbcb36SGuillaume Nault 	return 0;
563619fbcb36SGuillaume Nault }
563719fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop);
563819fbcb36SGuillaume Nault 
563919fbcb36SGuillaume Nault /**
564019fbcb36SGuillaume Nault  * skb_eth_push() - Add a new Ethernet header at the head of a packet
564119fbcb36SGuillaume Nault  *
564219fbcb36SGuillaume Nault  * @skb: Socket buffer to modify
564319fbcb36SGuillaume Nault  * @dst: Destination MAC address of the new header
564419fbcb36SGuillaume Nault  * @src: Source MAC address of the new header
564519fbcb36SGuillaume Nault  *
564619fbcb36SGuillaume Nault  * Prepend @skb with a new Ethernet header.
564719fbcb36SGuillaume Nault  *
564819fbcb36SGuillaume Nault  * Expects that skb->data points to the mac header, which must be empty.
564919fbcb36SGuillaume Nault  *
565019fbcb36SGuillaume Nault  * Returns 0 on success, -errno otherwise.
565119fbcb36SGuillaume Nault  */
565219fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
565319fbcb36SGuillaume Nault 		 const unsigned char *src)
565419fbcb36SGuillaume Nault {
565519fbcb36SGuillaume Nault 	struct ethhdr *eth;
565619fbcb36SGuillaume Nault 	int err;
565719fbcb36SGuillaume Nault 
565819fbcb36SGuillaume Nault 	if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
565919fbcb36SGuillaume Nault 		return -EPROTO;
566019fbcb36SGuillaume Nault 
566119fbcb36SGuillaume Nault 	err = skb_cow_head(skb, sizeof(*eth));
566219fbcb36SGuillaume Nault 	if (err < 0)
566319fbcb36SGuillaume Nault 		return err;
566419fbcb36SGuillaume Nault 
566519fbcb36SGuillaume Nault 	skb_push(skb, sizeof(*eth));
566619fbcb36SGuillaume Nault 	skb_reset_mac_header(skb);
566719fbcb36SGuillaume Nault 	skb_reset_mac_len(skb);
566819fbcb36SGuillaume Nault 
566919fbcb36SGuillaume Nault 	eth = eth_hdr(skb);
567019fbcb36SGuillaume Nault 	ether_addr_copy(eth->h_dest, dst);
567119fbcb36SGuillaume Nault 	ether_addr_copy(eth->h_source, src);
567219fbcb36SGuillaume Nault 	eth->h_proto = skb->protocol;
567319fbcb36SGuillaume Nault 
567419fbcb36SGuillaume Nault 	skb_postpush_rcsum(skb, eth, sizeof(*eth));
567519fbcb36SGuillaume Nault 
567619fbcb36SGuillaume Nault 	return 0;
567719fbcb36SGuillaume Nault }
567819fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push);
567919fbcb36SGuillaume Nault 
56808822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */
56818822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
56828822e270SJohn Hurley 			     __be16 ethertype)
56838822e270SJohn Hurley {
56848822e270SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
56858822e270SJohn Hurley 		__be16 diff[] = { ~hdr->h_proto, ethertype };
56868822e270SJohn Hurley 
56878822e270SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
56888822e270SJohn Hurley 	}
56898822e270SJohn Hurley 
56908822e270SJohn Hurley 	hdr->h_proto = ethertype;
56918822e270SJohn Hurley }
56928822e270SJohn Hurley 
56938822e270SJohn Hurley /**
5694e7dbfed1SMartin Varghese  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5695e7dbfed1SMartin Varghese  *                   the packet
56968822e270SJohn Hurley  *
56978822e270SJohn Hurley  * @skb: buffer
56988822e270SJohn Hurley  * @mpls_lse: MPLS label stack entry to push
56998822e270SJohn Hurley  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
5700fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
5701e7dbfed1SMartin Varghese  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5702e7dbfed1SMartin Varghese  *            ethernet
57038822e270SJohn Hurley  *
57048822e270SJohn Hurley  * Expects skb->data at mac header.
57058822e270SJohn Hurley  *
57068822e270SJohn Hurley  * Returns 0 on success, -errno otherwise.
57078822e270SJohn Hurley  */
5708fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
5709d04ac224SMartin Varghese 		  int mac_len, bool ethernet)
57108822e270SJohn Hurley {
57118822e270SJohn Hurley 	struct mpls_shim_hdr *lse;
57128822e270SJohn Hurley 	int err;
57138822e270SJohn Hurley 
57148822e270SJohn Hurley 	if (unlikely(!eth_p_mpls(mpls_proto)))
57158822e270SJohn Hurley 		return -EINVAL;
57168822e270SJohn Hurley 
57178822e270SJohn Hurley 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
57188822e270SJohn Hurley 	if (skb->encapsulation)
57198822e270SJohn Hurley 		return -EINVAL;
57208822e270SJohn Hurley 
57218822e270SJohn Hurley 	err = skb_cow_head(skb, MPLS_HLEN);
57228822e270SJohn Hurley 	if (unlikely(err))
57238822e270SJohn Hurley 		return err;
57248822e270SJohn Hurley 
57258822e270SJohn Hurley 	if (!skb->inner_protocol) {
5726e7dbfed1SMartin Varghese 		skb_set_inner_network_header(skb, skb_network_offset(skb));
57278822e270SJohn Hurley 		skb_set_inner_protocol(skb, skb->protocol);
57288822e270SJohn Hurley 	}
57298822e270SJohn Hurley 
57308822e270SJohn Hurley 	skb_push(skb, MPLS_HLEN);
57318822e270SJohn Hurley 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
5732fa4e0f88SDavide Caratti 		mac_len);
57338822e270SJohn Hurley 	skb_reset_mac_header(skb);
5734fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
5735e7dbfed1SMartin Varghese 	skb_reset_mac_len(skb);
57368822e270SJohn Hurley 
57378822e270SJohn Hurley 	lse = mpls_hdr(skb);
57388822e270SJohn Hurley 	lse->label_stack_entry = mpls_lse;
57398822e270SJohn Hurley 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
57408822e270SJohn Hurley 
57414296adc3SGuillaume Nault 	if (ethernet && mac_len >= ETH_HLEN)
57428822e270SJohn Hurley 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
57438822e270SJohn Hurley 	skb->protocol = mpls_proto;
57448822e270SJohn Hurley 
57458822e270SJohn Hurley 	return 0;
57468822e270SJohn Hurley }
57478822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push);
57488822e270SJohn Hurley 
57492e4e4410SEric Dumazet /**
5750ed246ceeSJohn Hurley  * skb_mpls_pop() - pop the outermost MPLS header
5751ed246ceeSJohn Hurley  *
5752ed246ceeSJohn Hurley  * @skb: buffer
5753ed246ceeSJohn Hurley  * @next_proto: ethertype of header after popped MPLS header
5754fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
575576f99f98SMartin Varghese  * @ethernet: flag to indicate if the packet is ethernet
5756ed246ceeSJohn Hurley  *
5757ed246ceeSJohn Hurley  * Expects skb->data at mac header.
5758ed246ceeSJohn Hurley  *
5759ed246ceeSJohn Hurley  * Returns 0 on success, -errno otherwise.
5760ed246ceeSJohn Hurley  */
5761040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5762040b5cfbSMartin Varghese 		 bool ethernet)
5763ed246ceeSJohn Hurley {
5764ed246ceeSJohn Hurley 	int err;
5765ed246ceeSJohn Hurley 
5766ed246ceeSJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
5767dedc5a08SDavide Caratti 		return 0;
5768ed246ceeSJohn Hurley 
5769fa4e0f88SDavide Caratti 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
5770ed246ceeSJohn Hurley 	if (unlikely(err))
5771ed246ceeSJohn Hurley 		return err;
5772ed246ceeSJohn Hurley 
5773ed246ceeSJohn Hurley 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5774ed246ceeSJohn Hurley 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
5775fa4e0f88SDavide Caratti 		mac_len);
5776ed246ceeSJohn Hurley 
5777ed246ceeSJohn Hurley 	__skb_pull(skb, MPLS_HLEN);
5778ed246ceeSJohn Hurley 	skb_reset_mac_header(skb);
5779fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
5780ed246ceeSJohn Hurley 
57814296adc3SGuillaume Nault 	if (ethernet && mac_len >= ETH_HLEN) {
5782ed246ceeSJohn Hurley 		struct ethhdr *hdr;
5783ed246ceeSJohn Hurley 
5784ed246ceeSJohn Hurley 		/* use mpls_hdr() to get ethertype to account for VLANs. */
5785ed246ceeSJohn Hurley 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5786ed246ceeSJohn Hurley 		skb_mod_eth_type(skb, hdr, next_proto);
5787ed246ceeSJohn Hurley 	}
5788ed246ceeSJohn Hurley 	skb->protocol = next_proto;
5789ed246ceeSJohn Hurley 
5790ed246ceeSJohn Hurley 	return 0;
5791ed246ceeSJohn Hurley }
5792ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop);
5793ed246ceeSJohn Hurley 
5794ed246ceeSJohn Hurley /**
5795d27cf5c5SJohn Hurley  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5796d27cf5c5SJohn Hurley  *
5797d27cf5c5SJohn Hurley  * @skb: buffer
5798d27cf5c5SJohn Hurley  * @mpls_lse: new MPLS label stack entry to update to
5799d27cf5c5SJohn Hurley  *
5800d27cf5c5SJohn Hurley  * Expects skb->data at mac header.
5801d27cf5c5SJohn Hurley  *
5802d27cf5c5SJohn Hurley  * Returns 0 on success, -errno otherwise.
5803d27cf5c5SJohn Hurley  */
5804d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5805d27cf5c5SJohn Hurley {
5806d27cf5c5SJohn Hurley 	int err;
5807d27cf5c5SJohn Hurley 
5808d27cf5c5SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
5809d27cf5c5SJohn Hurley 		return -EINVAL;
5810d27cf5c5SJohn Hurley 
5811d27cf5c5SJohn Hurley 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5812d27cf5c5SJohn Hurley 	if (unlikely(err))
5813d27cf5c5SJohn Hurley 		return err;
5814d27cf5c5SJohn Hurley 
5815d27cf5c5SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
5816d27cf5c5SJohn Hurley 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5817d27cf5c5SJohn Hurley 
5818d27cf5c5SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5819d27cf5c5SJohn Hurley 	}
5820d27cf5c5SJohn Hurley 
5821d27cf5c5SJohn Hurley 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
5822d27cf5c5SJohn Hurley 
5823d27cf5c5SJohn Hurley 	return 0;
5824d27cf5c5SJohn Hurley }
5825d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5826d27cf5c5SJohn Hurley 
5827d27cf5c5SJohn Hurley /**
58282a2ea508SJohn Hurley  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
58292a2ea508SJohn Hurley  *
58302a2ea508SJohn Hurley  * @skb: buffer
58312a2ea508SJohn Hurley  *
58322a2ea508SJohn Hurley  * Expects skb->data at mac header.
58332a2ea508SJohn Hurley  *
58342a2ea508SJohn Hurley  * Returns 0 on success, -errno otherwise.
58352a2ea508SJohn Hurley  */
58362a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb)
58372a2ea508SJohn Hurley {
58382a2ea508SJohn Hurley 	u32 lse;
58392a2ea508SJohn Hurley 	u8 ttl;
58402a2ea508SJohn Hurley 
58412a2ea508SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
58422a2ea508SJohn Hurley 		return -EINVAL;
58432a2ea508SJohn Hurley 
584413de4ed9SDavide Caratti 	if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
584513de4ed9SDavide Caratti 		return -ENOMEM;
584613de4ed9SDavide Caratti 
58472a2ea508SJohn Hurley 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
58482a2ea508SJohn Hurley 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
58492a2ea508SJohn Hurley 	if (!--ttl)
58502a2ea508SJohn Hurley 		return -EINVAL;
58512a2ea508SJohn Hurley 
58522a2ea508SJohn Hurley 	lse &= ~MPLS_LS_TTL_MASK;
58532a2ea508SJohn Hurley 	lse |= ttl << MPLS_LS_TTL_SHIFT;
58542a2ea508SJohn Hurley 
58552a2ea508SJohn Hurley 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
58562a2ea508SJohn Hurley }
58572a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
58582a2ea508SJohn Hurley 
58592a2ea508SJohn Hurley /**
58602e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
58612e4e4410SEric Dumazet  *
5862de3f0d0eSMasanari Iida  * @header_len: size of linear part
5863de3f0d0eSMasanari Iida  * @data_len: needed length in frags
5864de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
5865de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
5866de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
58672e4e4410SEric Dumazet  *
58682e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
58692e4e4410SEric Dumazet  */
58702e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
58712e4e4410SEric Dumazet 				     unsigned long data_len,
58722e4e4410SEric Dumazet 				     int max_page_order,
58732e4e4410SEric Dumazet 				     int *errcode,
58742e4e4410SEric Dumazet 				     gfp_t gfp_mask)
58752e4e4410SEric Dumazet {
58762e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
58772e4e4410SEric Dumazet 	unsigned long chunk;
58782e4e4410SEric Dumazet 	struct sk_buff *skb;
58792e4e4410SEric Dumazet 	struct page *page;
58802e4e4410SEric Dumazet 	int i;
58812e4e4410SEric Dumazet 
58822e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
58832e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
58842e4e4410SEric Dumazet 	 * high order pages...
58852e4e4410SEric Dumazet 	 */
58862e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
58872e4e4410SEric Dumazet 		return NULL;
58882e4e4410SEric Dumazet 
58892e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
5890f8c468e8SDavid Rientjes 	skb = alloc_skb(header_len, gfp_mask);
58912e4e4410SEric Dumazet 	if (!skb)
58922e4e4410SEric Dumazet 		return NULL;
58932e4e4410SEric Dumazet 
58942e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
58952e4e4410SEric Dumazet 
58962e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
58972e4e4410SEric Dumazet 		int order = max_page_order;
58982e4e4410SEric Dumazet 
58992e4e4410SEric Dumazet 		while (order) {
59002e4e4410SEric Dumazet 			if (npages >= 1 << order) {
5901d0164adcSMel Gorman 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
59022e4e4410SEric Dumazet 						   __GFP_COMP |
5903d14b56f5SMichal Hocko 						   __GFP_NOWARN,
59042e4e4410SEric Dumazet 						   order);
59052e4e4410SEric Dumazet 				if (page)
59062e4e4410SEric Dumazet 					goto fill_page;
59072e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
59082e4e4410SEric Dumazet 				order = 1;
59092e4e4410SEric Dumazet 				max_page_order = 0;
59102e4e4410SEric Dumazet 			}
59112e4e4410SEric Dumazet 			order--;
59122e4e4410SEric Dumazet 		}
59132e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
59142e4e4410SEric Dumazet 		if (!page)
59152e4e4410SEric Dumazet 			goto failure;
59162e4e4410SEric Dumazet fill_page:
59172e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
59182e4e4410SEric Dumazet 			      PAGE_SIZE << order);
59192e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
59202e4e4410SEric Dumazet 		data_len -= chunk;
59212e4e4410SEric Dumazet 		npages -= 1 << order;
59222e4e4410SEric Dumazet 	}
59232e4e4410SEric Dumazet 	return skb;
59242e4e4410SEric Dumazet 
59252e4e4410SEric Dumazet failure:
59262e4e4410SEric Dumazet 	kfree_skb(skb);
59272e4e4410SEric Dumazet 	return NULL;
59282e4e4410SEric Dumazet }
59292e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
59306fa01ccdSSowmini Varadhan 
59316fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
59326fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
59336fa01ccdSSowmini Varadhan 				    const int headlen, gfp_t gfp_mask)
59346fa01ccdSSowmini Varadhan {
59356fa01ccdSSowmini Varadhan 	int i;
59366fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
59376fa01ccdSSowmini Varadhan 	int new_hlen = headlen - off;
59386fa01ccdSSowmini Varadhan 	u8 *data;
59396fa01ccdSSowmini Varadhan 
59406fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
59416fa01ccdSSowmini Varadhan 
59426fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
59436fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
59446fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
59456fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
59466fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
59476fa01ccdSSowmini Varadhan 	if (!data)
59486fa01ccdSSowmini Varadhan 		return -ENOMEM;
59496fa01ccdSSowmini Varadhan 
59506fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
59516fa01ccdSSowmini Varadhan 
59526fa01ccdSSowmini Varadhan 	/* Copy real data, and all frags */
59536fa01ccdSSowmini Varadhan 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
59546fa01ccdSSowmini Varadhan 	skb->len -= off;
59556fa01ccdSSowmini Varadhan 
59566fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
59576fa01ccdSSowmini Varadhan 	       skb_shinfo(skb),
59586fa01ccdSSowmini Varadhan 	       offsetof(struct skb_shared_info,
59596fa01ccdSSowmini Varadhan 			frags[skb_shinfo(skb)->nr_frags]));
59606fa01ccdSSowmini Varadhan 	if (skb_cloned(skb)) {
59616fa01ccdSSowmini Varadhan 		/* drop the old head gracefully */
59626fa01ccdSSowmini Varadhan 		if (skb_orphan_frags(skb, gfp_mask)) {
59636fa01ccdSSowmini Varadhan 			kfree(data);
59646fa01ccdSSowmini Varadhan 			return -ENOMEM;
59656fa01ccdSSowmini Varadhan 		}
59666fa01ccdSSowmini Varadhan 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
59676fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
59686fa01ccdSSowmini Varadhan 		if (skb_has_frag_list(skb))
59696fa01ccdSSowmini Varadhan 			skb_clone_fraglist(skb);
59706fa01ccdSSowmini Varadhan 		skb_release_data(skb);
59716fa01ccdSSowmini Varadhan 	} else {
59726fa01ccdSSowmini Varadhan 		/* we can reuse existing recount- all we did was
59736fa01ccdSSowmini Varadhan 		 * relocate values
59746fa01ccdSSowmini Varadhan 		 */
59756fa01ccdSSowmini Varadhan 		skb_free_head(skb);
59766fa01ccdSSowmini Varadhan 	}
59776fa01ccdSSowmini Varadhan 
59786fa01ccdSSowmini Varadhan 	skb->head = data;
59796fa01ccdSSowmini Varadhan 	skb->data = data;
59806fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
59816fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
59826fa01ccdSSowmini Varadhan 	skb->end = size;
59836fa01ccdSSowmini Varadhan #else
59846fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
59856fa01ccdSSowmini Varadhan #endif
59866fa01ccdSSowmini Varadhan 	skb_set_tail_pointer(skb, skb_headlen(skb));
59876fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
59886fa01ccdSSowmini Varadhan 	skb->cloned = 0;
59896fa01ccdSSowmini Varadhan 	skb->hdr_len = 0;
59906fa01ccdSSowmini Varadhan 	skb->nohdr = 0;
59916fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
59926fa01ccdSSowmini Varadhan 
59936fa01ccdSSowmini Varadhan 	return 0;
59946fa01ccdSSowmini Varadhan }
59956fa01ccdSSowmini Varadhan 
59966fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
59976fa01ccdSSowmini Varadhan 
59986fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
59996fa01ccdSSowmini Varadhan  * pskb_carve()
60006fa01ccdSSowmini Varadhan  */
60016fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
60026fa01ccdSSowmini Varadhan 				struct skb_shared_info *shinfo, int eat,
60036fa01ccdSSowmini Varadhan 				gfp_t gfp_mask)
60046fa01ccdSSowmini Varadhan {
60056fa01ccdSSowmini Varadhan 	struct sk_buff *list = shinfo->frag_list;
60066fa01ccdSSowmini Varadhan 	struct sk_buff *clone = NULL;
60076fa01ccdSSowmini Varadhan 	struct sk_buff *insp = NULL;
60086fa01ccdSSowmini Varadhan 
60096fa01ccdSSowmini Varadhan 	do {
60106fa01ccdSSowmini Varadhan 		if (!list) {
60116fa01ccdSSowmini Varadhan 			pr_err("Not enough bytes to eat. Want %d\n", eat);
60126fa01ccdSSowmini Varadhan 			return -EFAULT;
60136fa01ccdSSowmini Varadhan 		}
60146fa01ccdSSowmini Varadhan 		if (list->len <= eat) {
60156fa01ccdSSowmini Varadhan 			/* Eaten as whole. */
60166fa01ccdSSowmini Varadhan 			eat -= list->len;
60176fa01ccdSSowmini Varadhan 			list = list->next;
60186fa01ccdSSowmini Varadhan 			insp = list;
60196fa01ccdSSowmini Varadhan 		} else {
60206fa01ccdSSowmini Varadhan 			/* Eaten partially. */
60216fa01ccdSSowmini Varadhan 			if (skb_shared(list)) {
60226fa01ccdSSowmini Varadhan 				clone = skb_clone(list, gfp_mask);
60236fa01ccdSSowmini Varadhan 				if (!clone)
60246fa01ccdSSowmini Varadhan 					return -ENOMEM;
60256fa01ccdSSowmini Varadhan 				insp = list->next;
60266fa01ccdSSowmini Varadhan 				list = clone;
60276fa01ccdSSowmini Varadhan 			} else {
60286fa01ccdSSowmini Varadhan 				/* This may be pulled without problems. */
60296fa01ccdSSowmini Varadhan 				insp = list;
60306fa01ccdSSowmini Varadhan 			}
60316fa01ccdSSowmini Varadhan 			if (pskb_carve(list, eat, gfp_mask) < 0) {
60326fa01ccdSSowmini Varadhan 				kfree_skb(clone);
60336fa01ccdSSowmini Varadhan 				return -ENOMEM;
60346fa01ccdSSowmini Varadhan 			}
60356fa01ccdSSowmini Varadhan 			break;
60366fa01ccdSSowmini Varadhan 		}
60376fa01ccdSSowmini Varadhan 	} while (eat);
60386fa01ccdSSowmini Varadhan 
60396fa01ccdSSowmini Varadhan 	/* Free pulled out fragments. */
60406fa01ccdSSowmini Varadhan 	while ((list = shinfo->frag_list) != insp) {
60416fa01ccdSSowmini Varadhan 		shinfo->frag_list = list->next;
60426fa01ccdSSowmini Varadhan 		kfree_skb(list);
60436fa01ccdSSowmini Varadhan 	}
60446fa01ccdSSowmini Varadhan 	/* And insert new clone at head. */
60456fa01ccdSSowmini Varadhan 	if (clone) {
60466fa01ccdSSowmini Varadhan 		clone->next = list;
60476fa01ccdSSowmini Varadhan 		shinfo->frag_list = clone;
60486fa01ccdSSowmini Varadhan 	}
60496fa01ccdSSowmini Varadhan 	return 0;
60506fa01ccdSSowmini Varadhan }
60516fa01ccdSSowmini Varadhan 
60526fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
60536fa01ccdSSowmini Varadhan  * non-linear part of skb
60546fa01ccdSSowmini Varadhan  */
60556fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
60566fa01ccdSSowmini Varadhan 				       int pos, gfp_t gfp_mask)
60576fa01ccdSSowmini Varadhan {
60586fa01ccdSSowmini Varadhan 	int i, k = 0;
60596fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
60606fa01ccdSSowmini Varadhan 	u8 *data;
60616fa01ccdSSowmini Varadhan 	const int nfrags = skb_shinfo(skb)->nr_frags;
60626fa01ccdSSowmini Varadhan 	struct skb_shared_info *shinfo;
60636fa01ccdSSowmini Varadhan 
60646fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
60656fa01ccdSSowmini Varadhan 
60666fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
60676fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
60686fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
60696fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
60706fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
60716fa01ccdSSowmini Varadhan 	if (!data)
60726fa01ccdSSowmini Varadhan 		return -ENOMEM;
60736fa01ccdSSowmini Varadhan 
60746fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
60756fa01ccdSSowmini Varadhan 
60766fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
6077e3ec1e8cSMiaohe Lin 	       skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
60786fa01ccdSSowmini Varadhan 	if (skb_orphan_frags(skb, gfp_mask)) {
60796fa01ccdSSowmini Varadhan 		kfree(data);
60806fa01ccdSSowmini Varadhan 		return -ENOMEM;
60816fa01ccdSSowmini Varadhan 	}
60826fa01ccdSSowmini Varadhan 	shinfo = (struct skb_shared_info *)(data + size);
60836fa01ccdSSowmini Varadhan 	for (i = 0; i < nfrags; i++) {
60846fa01ccdSSowmini Varadhan 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
60856fa01ccdSSowmini Varadhan 
60866fa01ccdSSowmini Varadhan 		if (pos + fsize > off) {
60876fa01ccdSSowmini Varadhan 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
60886fa01ccdSSowmini Varadhan 
60896fa01ccdSSowmini Varadhan 			if (pos < off) {
60906fa01ccdSSowmini Varadhan 				/* Split frag.
60916fa01ccdSSowmini Varadhan 				 * We have two variants in this case:
60926fa01ccdSSowmini Varadhan 				 * 1. Move all the frag to the second
60936fa01ccdSSowmini Varadhan 				 *    part, if it is possible. F.e.
60946fa01ccdSSowmini Varadhan 				 *    this approach is mandatory for TUX,
60956fa01ccdSSowmini Varadhan 				 *    where splitting is expensive.
60966fa01ccdSSowmini Varadhan 				 * 2. Split is accurately. We make this.
60976fa01ccdSSowmini Varadhan 				 */
6098b54c9d5bSJonathan Lemon 				skb_frag_off_add(&shinfo->frags[0], off - pos);
60996fa01ccdSSowmini Varadhan 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
61006fa01ccdSSowmini Varadhan 			}
61016fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
61026fa01ccdSSowmini Varadhan 			k++;
61036fa01ccdSSowmini Varadhan 		}
61046fa01ccdSSowmini Varadhan 		pos += fsize;
61056fa01ccdSSowmini Varadhan 	}
61066fa01ccdSSowmini Varadhan 	shinfo->nr_frags = k;
61076fa01ccdSSowmini Varadhan 	if (skb_has_frag_list(skb))
61086fa01ccdSSowmini Varadhan 		skb_clone_fraglist(skb);
61096fa01ccdSSowmini Varadhan 
61106fa01ccdSSowmini Varadhan 	/* split line is in frag list */
6111eabe8618SMiaohe Lin 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6112eabe8618SMiaohe Lin 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6113eabe8618SMiaohe Lin 		if (skb_has_frag_list(skb))
6114eabe8618SMiaohe Lin 			kfree_skb_list(skb_shinfo(skb)->frag_list);
6115eabe8618SMiaohe Lin 		kfree(data);
6116eabe8618SMiaohe Lin 		return -ENOMEM;
61176fa01ccdSSowmini Varadhan 	}
61186fa01ccdSSowmini Varadhan 	skb_release_data(skb);
61196fa01ccdSSowmini Varadhan 
61206fa01ccdSSowmini Varadhan 	skb->head = data;
61216fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
61226fa01ccdSSowmini Varadhan 	skb->data = data;
61236fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
61246fa01ccdSSowmini Varadhan 	skb->end = size;
61256fa01ccdSSowmini Varadhan #else
61266fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
61276fa01ccdSSowmini Varadhan #endif
61286fa01ccdSSowmini Varadhan 	skb_reset_tail_pointer(skb);
61296fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
61306fa01ccdSSowmini Varadhan 	skb->cloned   = 0;
61316fa01ccdSSowmini Varadhan 	skb->hdr_len  = 0;
61326fa01ccdSSowmini Varadhan 	skb->nohdr    = 0;
61336fa01ccdSSowmini Varadhan 	skb->len -= off;
61346fa01ccdSSowmini Varadhan 	skb->data_len = skb->len;
61356fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
61366fa01ccdSSowmini Varadhan 	return 0;
61376fa01ccdSSowmini Varadhan }
61386fa01ccdSSowmini Varadhan 
61396fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
61406fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
61416fa01ccdSSowmini Varadhan {
61426fa01ccdSSowmini Varadhan 	int headlen = skb_headlen(skb);
61436fa01ccdSSowmini Varadhan 
61446fa01ccdSSowmini Varadhan 	if (len < headlen)
61456fa01ccdSSowmini Varadhan 		return pskb_carve_inside_header(skb, len, headlen, gfp);
61466fa01ccdSSowmini Varadhan 	else
61476fa01ccdSSowmini Varadhan 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
61486fa01ccdSSowmini Varadhan }
61496fa01ccdSSowmini Varadhan 
61506fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
61516fa01ccdSSowmini Varadhan  * a new skb
61526fa01ccdSSowmini Varadhan  */
61536fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
61546fa01ccdSSowmini Varadhan 			     int to_copy, gfp_t gfp)
61556fa01ccdSSowmini Varadhan {
61566fa01ccdSSowmini Varadhan 	struct sk_buff  *clone = skb_clone(skb, gfp);
61576fa01ccdSSowmini Varadhan 
61586fa01ccdSSowmini Varadhan 	if (!clone)
61596fa01ccdSSowmini Varadhan 		return NULL;
61606fa01ccdSSowmini Varadhan 
61616fa01ccdSSowmini Varadhan 	if (pskb_carve(clone, off, gfp) < 0 ||
61626fa01ccdSSowmini Varadhan 	    pskb_trim(clone, to_copy)) {
61636fa01ccdSSowmini Varadhan 		kfree_skb(clone);
61646fa01ccdSSowmini Varadhan 		return NULL;
61656fa01ccdSSowmini Varadhan 	}
61666fa01ccdSSowmini Varadhan 	return clone;
61676fa01ccdSSowmini Varadhan }
61686fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
6169c8c8b127SEric Dumazet 
6170c8c8b127SEric Dumazet /**
6171c8c8b127SEric Dumazet  * skb_condense - try to get rid of fragments/frag_list if possible
6172c8c8b127SEric Dumazet  * @skb: buffer
6173c8c8b127SEric Dumazet  *
6174c8c8b127SEric Dumazet  * Can be used to save memory before skb is added to a busy queue.
6175c8c8b127SEric Dumazet  * If packet has bytes in frags and enough tail room in skb->head,
6176c8c8b127SEric Dumazet  * pull all of them, so that we can free the frags right now and adjust
6177c8c8b127SEric Dumazet  * truesize.
6178c8c8b127SEric Dumazet  * Notes:
6179c8c8b127SEric Dumazet  *	We do not reallocate skb->head thus can not fail.
6180c8c8b127SEric Dumazet  *	Caller must re-evaluate skb->truesize if needed.
6181c8c8b127SEric Dumazet  */
6182c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
6183c8c8b127SEric Dumazet {
61843174fed9SEric Dumazet 	if (skb->data_len) {
61853174fed9SEric Dumazet 		if (skb->data_len > skb->end - skb->tail ||
6186c8c8b127SEric Dumazet 		    skb_cloned(skb))
6187c8c8b127SEric Dumazet 			return;
6188c8c8b127SEric Dumazet 
6189c8c8b127SEric Dumazet 		/* Nice, we can free page frag(s) right now */
6190c8c8b127SEric Dumazet 		__pskb_pull_tail(skb, skb->data_len);
61913174fed9SEric Dumazet 	}
61923174fed9SEric Dumazet 	/* At this point, skb->truesize might be over estimated,
61933174fed9SEric Dumazet 	 * because skb had a fragment, and fragments do not tell
61943174fed9SEric Dumazet 	 * their truesize.
61953174fed9SEric Dumazet 	 * When we pulled its content into skb->head, fragment
61963174fed9SEric Dumazet 	 * was freed, but __pskb_pull_tail() could not possibly
61973174fed9SEric Dumazet 	 * adjust skb->truesize, not knowing the frag truesize.
6198c8c8b127SEric Dumazet 	 */
6199c8c8b127SEric Dumazet 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6200c8c8b127SEric Dumazet }
6201df5042f4SFlorian Westphal 
6202df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
6203df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6204df5042f4SFlorian Westphal {
6205df5042f4SFlorian Westphal 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6206df5042f4SFlorian Westphal }
6207df5042f4SFlorian Westphal 
62088b69a803SPaolo Abeni /**
62098b69a803SPaolo Abeni  * __skb_ext_alloc - allocate a new skb extensions storage
62108b69a803SPaolo Abeni  *
62114930f483SFlorian Westphal  * @flags: See kmalloc().
62124930f483SFlorian Westphal  *
62138b69a803SPaolo Abeni  * Returns the newly allocated pointer. The pointer can later attached to a
62148b69a803SPaolo Abeni  * skb via __skb_ext_set().
62158b69a803SPaolo Abeni  * Note: caller must handle the skb_ext as an opaque data.
62168b69a803SPaolo Abeni  */
62174930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags)
6218df5042f4SFlorian Westphal {
62194930f483SFlorian Westphal 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6220df5042f4SFlorian Westphal 
6221df5042f4SFlorian Westphal 	if (new) {
6222df5042f4SFlorian Westphal 		memset(new->offset, 0, sizeof(new->offset));
6223df5042f4SFlorian Westphal 		refcount_set(&new->refcnt, 1);
6224df5042f4SFlorian Westphal 	}
6225df5042f4SFlorian Westphal 
6226df5042f4SFlorian Westphal 	return new;
6227df5042f4SFlorian Westphal }
6228df5042f4SFlorian Westphal 
62294165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
62304165079bSFlorian Westphal 					 unsigned int old_active)
6231df5042f4SFlorian Westphal {
6232df5042f4SFlorian Westphal 	struct skb_ext *new;
6233df5042f4SFlorian Westphal 
6234df5042f4SFlorian Westphal 	if (refcount_read(&old->refcnt) == 1)
6235df5042f4SFlorian Westphal 		return old;
6236df5042f4SFlorian Westphal 
6237df5042f4SFlorian Westphal 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6238df5042f4SFlorian Westphal 	if (!new)
6239df5042f4SFlorian Westphal 		return NULL;
6240df5042f4SFlorian Westphal 
6241df5042f4SFlorian Westphal 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6242df5042f4SFlorian Westphal 	refcount_set(&new->refcnt, 1);
6243df5042f4SFlorian Westphal 
62444165079bSFlorian Westphal #ifdef CONFIG_XFRM
62454165079bSFlorian Westphal 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
62464165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
62474165079bSFlorian Westphal 		unsigned int i;
62484165079bSFlorian Westphal 
62494165079bSFlorian Westphal 		for (i = 0; i < sp->len; i++)
62504165079bSFlorian Westphal 			xfrm_state_hold(sp->xvec[i]);
62514165079bSFlorian Westphal 	}
62524165079bSFlorian Westphal #endif
6253df5042f4SFlorian Westphal 	__skb_ext_put(old);
6254df5042f4SFlorian Westphal 	return new;
6255df5042f4SFlorian Westphal }
6256df5042f4SFlorian Westphal 
6257df5042f4SFlorian Westphal /**
62588b69a803SPaolo Abeni  * __skb_ext_set - attach the specified extension storage to this skb
62598b69a803SPaolo Abeni  * @skb: buffer
62608b69a803SPaolo Abeni  * @id: extension id
62618b69a803SPaolo Abeni  * @ext: extension storage previously allocated via __skb_ext_alloc()
62628b69a803SPaolo Abeni  *
62638b69a803SPaolo Abeni  * Existing extensions, if any, are cleared.
62648b69a803SPaolo Abeni  *
62658b69a803SPaolo Abeni  * Returns the pointer to the extension.
62668b69a803SPaolo Abeni  */
62678b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
62688b69a803SPaolo Abeni 		    struct skb_ext *ext)
62698b69a803SPaolo Abeni {
62708b69a803SPaolo Abeni 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
62718b69a803SPaolo Abeni 
62728b69a803SPaolo Abeni 	skb_ext_put(skb);
62738b69a803SPaolo Abeni 	newlen = newoff + skb_ext_type_len[id];
62748b69a803SPaolo Abeni 	ext->chunks = newlen;
62758b69a803SPaolo Abeni 	ext->offset[id] = newoff;
62768b69a803SPaolo Abeni 	skb->extensions = ext;
62778b69a803SPaolo Abeni 	skb->active_extensions = 1 << id;
62788b69a803SPaolo Abeni 	return skb_ext_get_ptr(ext, id);
62798b69a803SPaolo Abeni }
62808b69a803SPaolo Abeni 
62818b69a803SPaolo Abeni /**
6282df5042f4SFlorian Westphal  * skb_ext_add - allocate space for given extension, COW if needed
6283df5042f4SFlorian Westphal  * @skb: buffer
6284df5042f4SFlorian Westphal  * @id: extension to allocate space for
6285df5042f4SFlorian Westphal  *
6286df5042f4SFlorian Westphal  * Allocates enough space for the given extension.
6287df5042f4SFlorian Westphal  * If the extension is already present, a pointer to that extension
6288df5042f4SFlorian Westphal  * is returned.
6289df5042f4SFlorian Westphal  *
6290df5042f4SFlorian Westphal  * If the skb was cloned, COW applies and the returned memory can be
6291df5042f4SFlorian Westphal  * modified without changing the extension space of clones buffers.
6292df5042f4SFlorian Westphal  *
6293df5042f4SFlorian Westphal  * Returns pointer to the extension or NULL on allocation failure.
6294df5042f4SFlorian Westphal  */
6295df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6296df5042f4SFlorian Westphal {
6297df5042f4SFlorian Westphal 	struct skb_ext *new, *old = NULL;
6298df5042f4SFlorian Westphal 	unsigned int newlen, newoff;
6299df5042f4SFlorian Westphal 
6300df5042f4SFlorian Westphal 	if (skb->active_extensions) {
6301df5042f4SFlorian Westphal 		old = skb->extensions;
6302df5042f4SFlorian Westphal 
63034165079bSFlorian Westphal 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6304df5042f4SFlorian Westphal 		if (!new)
6305df5042f4SFlorian Westphal 			return NULL;
6306df5042f4SFlorian Westphal 
6307682ec859SPaolo Abeni 		if (__skb_ext_exist(new, id))
6308df5042f4SFlorian Westphal 			goto set_active;
6309df5042f4SFlorian Westphal 
6310e94e50bdSPaolo Abeni 		newoff = new->chunks;
6311df5042f4SFlorian Westphal 	} else {
6312df5042f4SFlorian Westphal 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6313df5042f4SFlorian Westphal 
63144930f483SFlorian Westphal 		new = __skb_ext_alloc(GFP_ATOMIC);
6315df5042f4SFlorian Westphal 		if (!new)
6316df5042f4SFlorian Westphal 			return NULL;
6317df5042f4SFlorian Westphal 	}
6318df5042f4SFlorian Westphal 
6319df5042f4SFlorian Westphal 	newlen = newoff + skb_ext_type_len[id];
6320df5042f4SFlorian Westphal 	new->chunks = newlen;
6321df5042f4SFlorian Westphal 	new->offset[id] = newoff;
6322df5042f4SFlorian Westphal set_active:
6323682ec859SPaolo Abeni 	skb->extensions = new;
6324df5042f4SFlorian Westphal 	skb->active_extensions |= 1 << id;
6325df5042f4SFlorian Westphal 	return skb_ext_get_ptr(new, id);
6326df5042f4SFlorian Westphal }
6327df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add);
6328df5042f4SFlorian Westphal 
63294165079bSFlorian Westphal #ifdef CONFIG_XFRM
63304165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp)
63314165079bSFlorian Westphal {
63324165079bSFlorian Westphal 	unsigned int i;
63334165079bSFlorian Westphal 
63344165079bSFlorian Westphal 	for (i = 0; i < sp->len; i++)
63354165079bSFlorian Westphal 		xfrm_state_put(sp->xvec[i]);
63364165079bSFlorian Westphal }
63374165079bSFlorian Westphal #endif
63384165079bSFlorian Westphal 
6339df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6340df5042f4SFlorian Westphal {
6341df5042f4SFlorian Westphal 	struct skb_ext *ext = skb->extensions;
6342df5042f4SFlorian Westphal 
6343df5042f4SFlorian Westphal 	skb->active_extensions &= ~(1 << id);
6344df5042f4SFlorian Westphal 	if (skb->active_extensions == 0) {
6345df5042f4SFlorian Westphal 		skb->extensions = NULL;
6346df5042f4SFlorian Westphal 		__skb_ext_put(ext);
63474165079bSFlorian Westphal #ifdef CONFIG_XFRM
63484165079bSFlorian Westphal 	} else if (id == SKB_EXT_SEC_PATH &&
63494165079bSFlorian Westphal 		   refcount_read(&ext->refcnt) == 1) {
63504165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
63514165079bSFlorian Westphal 
63524165079bSFlorian Westphal 		skb_ext_put_sp(sp);
63534165079bSFlorian Westphal 		sp->len = 0;
63544165079bSFlorian Westphal #endif
6355df5042f4SFlorian Westphal 	}
6356df5042f4SFlorian Westphal }
6357df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del);
6358df5042f4SFlorian Westphal 
6359df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext)
6360df5042f4SFlorian Westphal {
6361df5042f4SFlorian Westphal 	/* If this is last clone, nothing can increment
6362df5042f4SFlorian Westphal 	 * it after check passes.  Avoids one atomic op.
6363df5042f4SFlorian Westphal 	 */
6364df5042f4SFlorian Westphal 	if (refcount_read(&ext->refcnt) == 1)
6365df5042f4SFlorian Westphal 		goto free_now;
6366df5042f4SFlorian Westphal 
6367df5042f4SFlorian Westphal 	if (!refcount_dec_and_test(&ext->refcnt))
6368df5042f4SFlorian Westphal 		return;
6369df5042f4SFlorian Westphal free_now:
63704165079bSFlorian Westphal #ifdef CONFIG_XFRM
63714165079bSFlorian Westphal 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
63724165079bSFlorian Westphal 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
63734165079bSFlorian Westphal #endif
63744165079bSFlorian Westphal 
6375df5042f4SFlorian Westphal 	kmem_cache_free(skbuff_ext_cache, ext);
6376df5042f4SFlorian Westphal }
6377df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put);
6378df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */
6379