xref: /openbmc/linux/net/core/skbuff.c (revision be769db2)
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 	}
2521da177e4SLinus Torvalds out:
2531da177e4SLinus Torvalds 	return skb;
2541da177e4SLinus Torvalds nodata:
2558798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2561da177e4SLinus Torvalds 	skb = NULL;
2571da177e4SLinus Torvalds 	goto out;
2581da177e4SLinus Torvalds }
259b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2601da177e4SLinus Torvalds 
261ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */
262ba0509b6SJesper Dangaard Brouer static struct sk_buff *__build_skb_around(struct sk_buff *skb,
263ba0509b6SJesper Dangaard Brouer 					  void *data, unsigned int frag_size)
264ba0509b6SJesper Dangaard Brouer {
265ba0509b6SJesper Dangaard Brouer 	struct skb_shared_info *shinfo;
266ba0509b6SJesper Dangaard Brouer 	unsigned int size = frag_size ? : ksize(data);
267ba0509b6SJesper Dangaard Brouer 
268ba0509b6SJesper Dangaard Brouer 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
269ba0509b6SJesper Dangaard Brouer 
270ba0509b6SJesper Dangaard Brouer 	/* Assumes caller memset cleared SKB */
271ba0509b6SJesper Dangaard Brouer 	skb->truesize = SKB_TRUESIZE(size);
272ba0509b6SJesper Dangaard Brouer 	refcount_set(&skb->users, 1);
273ba0509b6SJesper Dangaard Brouer 	skb->head = data;
274ba0509b6SJesper Dangaard Brouer 	skb->data = data;
275ba0509b6SJesper Dangaard Brouer 	skb_reset_tail_pointer(skb);
276ba0509b6SJesper Dangaard Brouer 	skb->end = skb->tail + size;
277ba0509b6SJesper Dangaard Brouer 	skb->mac_header = (typeof(skb->mac_header))~0U;
278ba0509b6SJesper Dangaard Brouer 	skb->transport_header = (typeof(skb->transport_header))~0U;
279ba0509b6SJesper Dangaard Brouer 
280ba0509b6SJesper Dangaard Brouer 	/* make sure we initialize shinfo sequentially */
281ba0509b6SJesper Dangaard Brouer 	shinfo = skb_shinfo(skb);
282ba0509b6SJesper Dangaard Brouer 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
283ba0509b6SJesper Dangaard Brouer 	atomic_set(&shinfo->dataref, 1);
284ba0509b6SJesper Dangaard Brouer 
285ba0509b6SJesper Dangaard Brouer 	return skb;
286ba0509b6SJesper Dangaard Brouer }
287ba0509b6SJesper Dangaard Brouer 
2881da177e4SLinus Torvalds /**
2892ea2f62cSEric Dumazet  * __build_skb - build a network buffer
290b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
2912ea2f62cSEric Dumazet  * @frag_size: size of data, or 0 if head was kmalloced
292b2b5ce9dSEric Dumazet  *
293b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
294deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
2952ea2f62cSEric Dumazet  * @frag_size is 0, otherwise data should come from the page allocator
2962ea2f62cSEric Dumazet  *  or vmalloc()
297b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
298b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
299b2b5ce9dSEric Dumazet  * Notes :
300b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
301b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
302b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
303b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
304b2b5ce9dSEric Dumazet  *  before giving packet to stack.
305b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
306b2b5ce9dSEric Dumazet  */
3072ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size)
308b2b5ce9dSEric Dumazet {
309b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
310b2b5ce9dSEric Dumazet 
311b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
312ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
313b2b5ce9dSEric Dumazet 		return NULL;
314b2b5ce9dSEric Dumazet 
315b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
316b2b5ce9dSEric Dumazet 
317ba0509b6SJesper Dangaard Brouer 	return __build_skb_around(skb, data, frag_size);
318b2b5ce9dSEric Dumazet }
3192ea2f62cSEric Dumazet 
3202ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically
3212ea2f62cSEric Dumazet  * takes care of skb->head and skb->pfmemalloc
3222ea2f62cSEric Dumazet  * This means that if @frag_size is not zero, then @data must be backed
3232ea2f62cSEric Dumazet  * by a page fragment, not kmalloc() or vmalloc()
3242ea2f62cSEric Dumazet  */
3252ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
3262ea2f62cSEric Dumazet {
3272ea2f62cSEric Dumazet 	struct sk_buff *skb = __build_skb(data, frag_size);
3282ea2f62cSEric Dumazet 
3292ea2f62cSEric Dumazet 	if (skb && frag_size) {
3302ea2f62cSEric Dumazet 		skb->head_frag = 1;
3312f064f34SMichal Hocko 		if (page_is_pfmemalloc(virt_to_head_page(data)))
3322ea2f62cSEric Dumazet 			skb->pfmemalloc = 1;
3332ea2f62cSEric Dumazet 	}
3342ea2f62cSEric Dumazet 	return skb;
3352ea2f62cSEric Dumazet }
336b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
337b2b5ce9dSEric Dumazet 
338ba0509b6SJesper Dangaard Brouer /**
339ba0509b6SJesper Dangaard Brouer  * build_skb_around - build a network buffer around provided skb
340ba0509b6SJesper Dangaard Brouer  * @skb: sk_buff provide by caller, must be memset cleared
341ba0509b6SJesper Dangaard Brouer  * @data: data buffer provided by caller
342ba0509b6SJesper Dangaard Brouer  * @frag_size: size of data, or 0 if head was kmalloced
343ba0509b6SJesper Dangaard Brouer  */
344ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb,
345ba0509b6SJesper Dangaard Brouer 				 void *data, unsigned int frag_size)
346ba0509b6SJesper Dangaard Brouer {
347ba0509b6SJesper Dangaard Brouer 	if (unlikely(!skb))
348ba0509b6SJesper Dangaard Brouer 		return NULL;
349ba0509b6SJesper Dangaard Brouer 
350ba0509b6SJesper Dangaard Brouer 	skb = __build_skb_around(skb, data, frag_size);
351ba0509b6SJesper Dangaard Brouer 
352ba0509b6SJesper Dangaard Brouer 	if (skb && frag_size) {
353ba0509b6SJesper Dangaard Brouer 		skb->head_frag = 1;
354ba0509b6SJesper Dangaard Brouer 		if (page_is_pfmemalloc(virt_to_head_page(data)))
355ba0509b6SJesper Dangaard Brouer 			skb->pfmemalloc = 1;
356ba0509b6SJesper Dangaard Brouer 	}
357ba0509b6SJesper Dangaard Brouer 	return skb;
358ba0509b6SJesper Dangaard Brouer }
359ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around);
360ba0509b6SJesper Dangaard Brouer 
361795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE	64
362795bb1c0SJesper Dangaard Brouer 
363795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache {
364795bb1c0SJesper Dangaard Brouer 	struct page_frag_cache page;
365e0d7924aSAlexey Dobriyan 	unsigned int skb_count;
366795bb1c0SJesper Dangaard Brouer 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
367795bb1c0SJesper Dangaard Brouer };
368795bb1c0SJesper Dangaard Brouer 
369b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
370795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
371ffde7328SAlexander Duyck 
372ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
373ffde7328SAlexander Duyck {
374795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
3759451980aSAlexander Duyck 
3768c2dd3e4SAlexander Duyck 	return page_frag_alloc(&nc->page, fragsz, gfp_mask);
377ffde7328SAlexander Duyck }
378ffde7328SAlexander Duyck 
379ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
380ffde7328SAlexander Duyck {
3813bed3cc4SAlexander Duyck 	fragsz = SKB_DATA_ALIGN(fragsz);
3823bed3cc4SAlexander Duyck 
383453f85d4SMel Gorman 	return __napi_alloc_frag(fragsz, GFP_ATOMIC);
384ffde7328SAlexander Duyck }
385ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
386ffde7328SAlexander Duyck 
3876f532612SEric Dumazet /**
3887ba7aeabSSebastian Andrzej Siewior  * netdev_alloc_frag - allocate a page fragment
3897ba7aeabSSebastian Andrzej Siewior  * @fragsz: fragment size
3907ba7aeabSSebastian Andrzej Siewior  *
3917ba7aeabSSebastian Andrzej Siewior  * Allocates a frag from a page for receive buffer.
3927ba7aeabSSebastian Andrzej Siewior  * Uses GFP_ATOMIC allocations.
3937ba7aeabSSebastian Andrzej Siewior  */
3947ba7aeabSSebastian Andrzej Siewior void *netdev_alloc_frag(unsigned int fragsz)
3957ba7aeabSSebastian Andrzej Siewior {
3967ba7aeabSSebastian Andrzej Siewior 	struct page_frag_cache *nc;
3977ba7aeabSSebastian Andrzej Siewior 	void *data;
3987ba7aeabSSebastian Andrzej Siewior 
3997ba7aeabSSebastian Andrzej Siewior 	fragsz = SKB_DATA_ALIGN(fragsz);
4007ba7aeabSSebastian Andrzej Siewior 	if (in_irq() || irqs_disabled()) {
4017ba7aeabSSebastian Andrzej Siewior 		nc = this_cpu_ptr(&netdev_alloc_cache);
4027ba7aeabSSebastian Andrzej Siewior 		data = page_frag_alloc(nc, fragsz, GFP_ATOMIC);
4037ba7aeabSSebastian Andrzej Siewior 	} else {
4047ba7aeabSSebastian Andrzej Siewior 		local_bh_disable();
4057ba7aeabSSebastian Andrzej Siewior 		data = __napi_alloc_frag(fragsz, GFP_ATOMIC);
4067ba7aeabSSebastian Andrzej Siewior 		local_bh_enable();
4077ba7aeabSSebastian Andrzej Siewior 	}
4087ba7aeabSSebastian Andrzej Siewior 	return data;
4097ba7aeabSSebastian Andrzej Siewior }
4107ba7aeabSSebastian Andrzej Siewior EXPORT_SYMBOL(netdev_alloc_frag);
4117ba7aeabSSebastian Andrzej Siewior 
4127ba7aeabSSebastian Andrzej Siewior /**
413fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
414fd11a83dSAlexander Duyck  *	@dev: network device to receive on
415d7499160SMasanari Iida  *	@len: length to allocate
416fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
417fd11a83dSAlexander Duyck  *
418fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
419fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
420fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
421fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
422fd11a83dSAlexander Duyck  *
423fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
424fd11a83dSAlexander Duyck  */
4259451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
4269451980aSAlexander Duyck 				   gfp_t gfp_mask)
427fd11a83dSAlexander Duyck {
428b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
429fd11a83dSAlexander Duyck 	struct sk_buff *skb;
4309451980aSAlexander Duyck 	bool pfmemalloc;
4319451980aSAlexander Duyck 	void *data;
432fd11a83dSAlexander Duyck 
4339451980aSAlexander Duyck 	len += NET_SKB_PAD;
434fd11a83dSAlexander Duyck 
4359451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
436d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
437a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
438a080e7bdSAlexander Duyck 		if (!skb)
439a080e7bdSAlexander Duyck 			goto skb_fail;
440a080e7bdSAlexander Duyck 		goto skb_success;
441a080e7bdSAlexander Duyck 	}
4429451980aSAlexander Duyck 
4439451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4449451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
4459451980aSAlexander Duyck 
4469451980aSAlexander Duyck 	if (sk_memalloc_socks())
4479451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
4489451980aSAlexander Duyck 
44992dcabd7SSebastian Andrzej Siewior 	if (in_irq() || irqs_disabled()) {
4509451980aSAlexander Duyck 		nc = this_cpu_ptr(&netdev_alloc_cache);
4518c2dd3e4SAlexander Duyck 		data = page_frag_alloc(nc, len, gfp_mask);
4529451980aSAlexander Duyck 		pfmemalloc = nc->pfmemalloc;
45392dcabd7SSebastian Andrzej Siewior 	} else {
45492dcabd7SSebastian Andrzej Siewior 		local_bh_disable();
45592dcabd7SSebastian Andrzej Siewior 		nc = this_cpu_ptr(&napi_alloc_cache.page);
45692dcabd7SSebastian Andrzej Siewior 		data = page_frag_alloc(nc, len, gfp_mask);
45792dcabd7SSebastian Andrzej Siewior 		pfmemalloc = nc->pfmemalloc;
45892dcabd7SSebastian Andrzej Siewior 		local_bh_enable();
45992dcabd7SSebastian Andrzej Siewior 	}
4609451980aSAlexander Duyck 
4619451980aSAlexander Duyck 	if (unlikely(!data))
4629451980aSAlexander Duyck 		return NULL;
4639451980aSAlexander Duyck 
4649451980aSAlexander Duyck 	skb = __build_skb(data, len);
4659451980aSAlexander Duyck 	if (unlikely(!skb)) {
466181edb2bSAlexander Duyck 		skb_free_frag(data);
4679451980aSAlexander Duyck 		return NULL;
4689451980aSAlexander Duyck 	}
4699451980aSAlexander Duyck 
4709451980aSAlexander Duyck 	if (pfmemalloc)
4719451980aSAlexander Duyck 		skb->pfmemalloc = 1;
4729451980aSAlexander Duyck 	skb->head_frag = 1;
4739451980aSAlexander Duyck 
474a080e7bdSAlexander Duyck skb_success:
4758af27456SChristoph Hellwig 	skb_reserve(skb, NET_SKB_PAD);
4767b2e497aSChristoph Hellwig 	skb->dev = dev;
477fd11a83dSAlexander Duyck 
478a080e7bdSAlexander Duyck skb_fail:
4798af27456SChristoph Hellwig 	return skb;
4808af27456SChristoph Hellwig }
481b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4821da177e4SLinus Torvalds 
483fd11a83dSAlexander Duyck /**
484fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
485fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
486d7499160SMasanari Iida  *	@len: length to allocate
487fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
488fd11a83dSAlexander Duyck  *
489fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
490fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
491fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
492fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
493fd11a83dSAlexander Duyck  *
494fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
495fd11a83dSAlexander Duyck  */
4969451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
4979451980aSAlexander Duyck 				 gfp_t gfp_mask)
498fd11a83dSAlexander Duyck {
499795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
500fd11a83dSAlexander Duyck 	struct sk_buff *skb;
5019451980aSAlexander Duyck 	void *data;
502fd11a83dSAlexander Duyck 
5039451980aSAlexander Duyck 	len += NET_SKB_PAD + NET_IP_ALIGN;
504fd11a83dSAlexander Duyck 
5059451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
506d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
507a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
508a080e7bdSAlexander Duyck 		if (!skb)
509a080e7bdSAlexander Duyck 			goto skb_fail;
510a080e7bdSAlexander Duyck 		goto skb_success;
511a080e7bdSAlexander Duyck 	}
5129451980aSAlexander Duyck 
5139451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5149451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
5159451980aSAlexander Duyck 
5169451980aSAlexander Duyck 	if (sk_memalloc_socks())
5179451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
5189451980aSAlexander Duyck 
5198c2dd3e4SAlexander Duyck 	data = page_frag_alloc(&nc->page, len, gfp_mask);
5209451980aSAlexander Duyck 	if (unlikely(!data))
5219451980aSAlexander Duyck 		return NULL;
5229451980aSAlexander Duyck 
5239451980aSAlexander Duyck 	skb = __build_skb(data, len);
5249451980aSAlexander Duyck 	if (unlikely(!skb)) {
525181edb2bSAlexander Duyck 		skb_free_frag(data);
5269451980aSAlexander Duyck 		return NULL;
5279451980aSAlexander Duyck 	}
5289451980aSAlexander Duyck 
529795bb1c0SJesper Dangaard Brouer 	if (nc->page.pfmemalloc)
5309451980aSAlexander Duyck 		skb->pfmemalloc = 1;
5319451980aSAlexander Duyck 	skb->head_frag = 1;
5329451980aSAlexander Duyck 
533a080e7bdSAlexander Duyck skb_success:
534fd11a83dSAlexander Duyck 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
535fd11a83dSAlexander Duyck 	skb->dev = napi->dev;
536fd11a83dSAlexander Duyck 
537a080e7bdSAlexander Duyck skb_fail:
538fd11a83dSAlexander Duyck 	return skb;
539fd11a83dSAlexander Duyck }
540fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
541fd11a83dSAlexander Duyck 
542654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
54350269e19SEric Dumazet 		     int size, unsigned int truesize)
544654bed16SPeter Zijlstra {
545654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
546654bed16SPeter Zijlstra 	skb->len += size;
547654bed16SPeter Zijlstra 	skb->data_len += size;
54850269e19SEric Dumazet 	skb->truesize += truesize;
549654bed16SPeter Zijlstra }
550654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
551654bed16SPeter Zijlstra 
552f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
553f8e617e1SJason Wang 			  unsigned int truesize)
554f8e617e1SJason Wang {
555f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
556f8e617e1SJason Wang 
557f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
558f8e617e1SJason Wang 	skb->len += size;
559f8e617e1SJason Wang 	skb->data_len += size;
560f8e617e1SJason Wang 	skb->truesize += truesize;
561f8e617e1SJason Wang }
562f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
563f8e617e1SJason Wang 
56427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5651da177e4SLinus Torvalds {
566bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
56727b437c8SHerbert Xu 	*listp = NULL;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
57127b437c8SHerbert Xu {
57227b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
57327b437c8SHerbert Xu }
57427b437c8SHerbert Xu 
5751da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	struct sk_buff *list;
5781da177e4SLinus Torvalds 
579fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5801da177e4SLinus Torvalds 		skb_get(list);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
583d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
584d3836f21SEric Dumazet {
585181edb2bSAlexander Duyck 	unsigned char *head = skb->head;
586181edb2bSAlexander Duyck 
587d3836f21SEric Dumazet 	if (skb->head_frag)
588181edb2bSAlexander Duyck 		skb_free_frag(head);
589d3836f21SEric Dumazet 	else
590181edb2bSAlexander Duyck 		kfree(head);
591d3836f21SEric Dumazet }
592d3836f21SEric Dumazet 
5935bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
5941da177e4SLinus Torvalds {
595ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
5961da177e4SLinus Torvalds 	int i;
597ff04a771SEric Dumazet 
598ff04a771SEric Dumazet 	if (skb->cloned &&
599ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
600ff04a771SEric Dumazet 			      &shinfo->dataref))
601ff04a771SEric Dumazet 		return;
602ff04a771SEric Dumazet 
603ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
604ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
6051da177e4SLinus Torvalds 
606ff04a771SEric Dumazet 	if (shinfo->frag_list)
607ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
6081da177e4SLinus Torvalds 
6091f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
610d3836f21SEric Dumazet 	skb_free_head(skb);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /*
6141da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
6151da177e4SLinus Torvalds  */
6162d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
6171da177e4SLinus Torvalds {
618d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
619d179cd12SDavid S. Miller 
620d179cd12SDavid S. Miller 	switch (skb->fclone) {
621d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
6221da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
6236ffe75ebSEric Dumazet 		return;
624d179cd12SDavid S. Miller 
625d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
626d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6276ffe75ebSEric Dumazet 
6286ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
6296ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
6306ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
6316ffe75ebSEric Dumazet 		 */
6322638595aSReshetova, Elena 		if (refcount_read(&fclones->fclone_ref) == 1)
6336ffe75ebSEric Dumazet 			goto fastpath;
634d179cd12SDavid S. Miller 		break;
635d179cd12SDavid S. Miller 
6366ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
637d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
638d179cd12SDavid S. Miller 		break;
6393ff50b79SStephen Hemminger 	}
6402638595aSReshetova, Elena 	if (!refcount_dec_and_test(&fclones->fclone_ref))
6416ffe75ebSEric Dumazet 		return;
6426ffe75ebSEric Dumazet fastpath:
6436ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6460a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb)
6471da177e4SLinus Torvalds {
648adf30907SEric Dumazet 	skb_dst_drop(skb);
6491da177e4SLinus Torvalds 	if (skb->destructor) {
6509c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6511da177e4SLinus Torvalds 		skb->destructor(skb);
6521da177e4SLinus Torvalds 	}
653a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
654cb9c6836SFlorian Westphal 	nf_conntrack_put(skb_nfct(skb));
6552fc72c7bSKOVACS Krisztian #endif
656df5042f4SFlorian Westphal 	skb_ext_put(skb);
65704a4bb55SLennert Buytenhek }
65804a4bb55SLennert Buytenhek 
65904a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
66004a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
66104a4bb55SLennert Buytenhek {
66204a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
663a28b1b90SFlorian Westphal 	if (likely(skb->head))
6642d4baff8SHerbert Xu 		skb_release_data(skb);
6652d4baff8SHerbert Xu }
6661da177e4SLinus Torvalds 
6672d4baff8SHerbert Xu /**
6682d4baff8SHerbert Xu  *	__kfree_skb - private function
6692d4baff8SHerbert Xu  *	@skb: buffer
6702d4baff8SHerbert Xu  *
6712d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6722d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6732d4baff8SHerbert Xu  *	always call kfree_skb
6742d4baff8SHerbert Xu  */
6752d4baff8SHerbert Xu 
6762d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6772d4baff8SHerbert Xu {
6782d4baff8SHerbert Xu 	skb_release_all(skb);
6791da177e4SLinus Torvalds 	kfree_skbmem(skb);
6801da177e4SLinus Torvalds }
681b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /**
684231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
685231d06aeSJörn Engel  *	@skb: buffer to free
686231d06aeSJörn Engel  *
687231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
688231d06aeSJörn Engel  *	hit zero.
689231d06aeSJörn Engel  */
690231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
691231d06aeSJörn Engel {
6923889a803SPaolo Abeni 	if (!skb_unref(skb))
693231d06aeSJörn Engel 		return;
6943889a803SPaolo Abeni 
695ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
696231d06aeSJörn Engel 	__kfree_skb(skb);
697231d06aeSJörn Engel }
698b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
699231d06aeSJörn Engel 
700bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
701bd8a7036SEric Dumazet {
702bd8a7036SEric Dumazet 	while (segs) {
703bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
704bd8a7036SEric Dumazet 
705bd8a7036SEric Dumazet 		kfree_skb(segs);
706bd8a7036SEric Dumazet 		segs = next;
707bd8a7036SEric Dumazet 	}
708bd8a7036SEric Dumazet }
709bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
710bd8a7036SEric Dumazet 
7116413139dSWillem de Bruijn /* Dump skb information and contents.
7126413139dSWillem de Bruijn  *
7136413139dSWillem de Bruijn  * Must only be called from net_ratelimit()-ed paths.
7146413139dSWillem de Bruijn  *
7156413139dSWillem de Bruijn  * Dumps up to can_dump_full whole packets if full_pkt, headers otherwise.
7166413139dSWillem de Bruijn  */
7176413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
7186413139dSWillem de Bruijn {
7196413139dSWillem de Bruijn 	static atomic_t can_dump_full = ATOMIC_INIT(5);
7206413139dSWillem de Bruijn 	struct skb_shared_info *sh = skb_shinfo(skb);
7216413139dSWillem de Bruijn 	struct net_device *dev = skb->dev;
7226413139dSWillem de Bruijn 	struct sock *sk = skb->sk;
7236413139dSWillem de Bruijn 	struct sk_buff *list_skb;
7246413139dSWillem de Bruijn 	bool has_mac, has_trans;
7256413139dSWillem de Bruijn 	int headroom, tailroom;
7266413139dSWillem de Bruijn 	int i, len, seg_len;
7276413139dSWillem de Bruijn 
7286413139dSWillem de Bruijn 	if (full_pkt)
7296413139dSWillem de Bruijn 		full_pkt = atomic_dec_if_positive(&can_dump_full) >= 0;
7306413139dSWillem de Bruijn 
7316413139dSWillem de Bruijn 	if (full_pkt)
7326413139dSWillem de Bruijn 		len = skb->len;
7336413139dSWillem de Bruijn 	else
7346413139dSWillem de Bruijn 		len = min_t(int, skb->len, MAX_HEADER + 128);
7356413139dSWillem de Bruijn 
7366413139dSWillem de Bruijn 	headroom = skb_headroom(skb);
7376413139dSWillem de Bruijn 	tailroom = skb_tailroom(skb);
7386413139dSWillem de Bruijn 
7396413139dSWillem de Bruijn 	has_mac = skb_mac_header_was_set(skb);
7406413139dSWillem de Bruijn 	has_trans = skb_transport_header_was_set(skb);
7416413139dSWillem de Bruijn 
7426413139dSWillem de Bruijn 	printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
7436413139dSWillem de Bruijn 	       "mac=(%d,%d) net=(%d,%d) trans=%d\n"
7446413139dSWillem de Bruijn 	       "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
7456413139dSWillem de Bruijn 	       "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
7466413139dSWillem de Bruijn 	       "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
7476413139dSWillem de Bruijn 	       level, skb->len, headroom, skb_headlen(skb), tailroom,
7486413139dSWillem de Bruijn 	       has_mac ? skb->mac_header : -1,
7496413139dSWillem de Bruijn 	       has_mac ? skb_mac_header_len(skb) : -1,
7506413139dSWillem de Bruijn 	       skb->network_header,
7516413139dSWillem de Bruijn 	       has_trans ? skb_network_header_len(skb) : -1,
7526413139dSWillem de Bruijn 	       has_trans ? skb->transport_header : -1,
7536413139dSWillem de Bruijn 	       sh->tx_flags, sh->nr_frags,
7546413139dSWillem de Bruijn 	       sh->gso_size, sh->gso_type, sh->gso_segs,
7556413139dSWillem de Bruijn 	       skb->csum, skb->ip_summed, skb->csum_complete_sw,
7566413139dSWillem de Bruijn 	       skb->csum_valid, skb->csum_level,
7576413139dSWillem de Bruijn 	       skb->hash, skb->sw_hash, skb->l4_hash,
7586413139dSWillem de Bruijn 	       ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
7596413139dSWillem de Bruijn 
7606413139dSWillem de Bruijn 	if (dev)
7616413139dSWillem de Bruijn 		printk("%sdev name=%s feat=0x%pNF\n",
7626413139dSWillem de Bruijn 		       level, dev->name, &dev->features);
7636413139dSWillem de Bruijn 	if (sk)
764db8051f3SQian Cai 		printk("%ssk family=%hu type=%u proto=%u\n",
7656413139dSWillem de Bruijn 		       level, sk->sk_family, sk->sk_type, sk->sk_protocol);
7666413139dSWillem de Bruijn 
7676413139dSWillem de Bruijn 	if (full_pkt && headroom)
7686413139dSWillem de Bruijn 		print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
7696413139dSWillem de Bruijn 			       16, 1, skb->head, headroom, false);
7706413139dSWillem de Bruijn 
7716413139dSWillem de Bruijn 	seg_len = min_t(int, skb_headlen(skb), len);
7726413139dSWillem de Bruijn 	if (seg_len)
7736413139dSWillem de Bruijn 		print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
7746413139dSWillem de Bruijn 			       16, 1, skb->data, seg_len, false);
7756413139dSWillem de Bruijn 	len -= seg_len;
7766413139dSWillem de Bruijn 
7776413139dSWillem de Bruijn 	if (full_pkt && tailroom)
7786413139dSWillem de Bruijn 		print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
7796413139dSWillem de Bruijn 			       16, 1, skb_tail_pointer(skb), tailroom, false);
7806413139dSWillem de Bruijn 
7816413139dSWillem de Bruijn 	for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
7826413139dSWillem de Bruijn 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7836413139dSWillem de Bruijn 		u32 p_off, p_len, copied;
7846413139dSWillem de Bruijn 		struct page *p;
7856413139dSWillem de Bruijn 		u8 *vaddr;
7866413139dSWillem de Bruijn 
787b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(frag, skb_frag_off(frag),
7886413139dSWillem de Bruijn 				      skb_frag_size(frag), p, p_off, p_len,
7896413139dSWillem de Bruijn 				      copied) {
7906413139dSWillem de Bruijn 			seg_len = min_t(int, p_len, len);
7916413139dSWillem de Bruijn 			vaddr = kmap_atomic(p);
7926413139dSWillem de Bruijn 			print_hex_dump(level, "skb frag:     ",
7936413139dSWillem de Bruijn 				       DUMP_PREFIX_OFFSET,
7946413139dSWillem de Bruijn 				       16, 1, vaddr + p_off, seg_len, false);
7956413139dSWillem de Bruijn 			kunmap_atomic(vaddr);
7966413139dSWillem de Bruijn 			len -= seg_len;
7976413139dSWillem de Bruijn 			if (!len)
7986413139dSWillem de Bruijn 				break;
7996413139dSWillem de Bruijn 		}
8006413139dSWillem de Bruijn 	}
8016413139dSWillem de Bruijn 
8026413139dSWillem de Bruijn 	if (full_pkt && skb_has_frag_list(skb)) {
8036413139dSWillem de Bruijn 		printk("skb fraglist:\n");
8046413139dSWillem de Bruijn 		skb_walk_frags(skb, list_skb)
8056413139dSWillem de Bruijn 			skb_dump(level, list_skb, true);
8066413139dSWillem de Bruijn 	}
8076413139dSWillem de Bruijn }
8086413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump);
8096413139dSWillem de Bruijn 
810d1a203eaSStephen Hemminger /**
81125121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
81225121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
81325121173SMichael S. Tsirkin  *
81425121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
81525121173SMichael S. Tsirkin  *	skb must be freed afterwards.
81625121173SMichael S. Tsirkin  */
81725121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
81825121173SMichael S. Tsirkin {
8191f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
82025121173SMichael S. Tsirkin }
82125121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
82225121173SMichael S. Tsirkin 
823be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS
82425121173SMichael S. Tsirkin /**
825ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
826ead2ceb0SNeil Horman  *	@skb: buffer to free
827ead2ceb0SNeil Horman  *
828ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
829ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
830ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
831ead2ceb0SNeil Horman  */
832ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
833ead2ceb0SNeil Horman {
8343889a803SPaolo Abeni 	if (!skb_unref(skb))
835ead2ceb0SNeil Horman 		return;
8363889a803SPaolo Abeni 
83707dc22e7SKoki Sanagi 	trace_consume_skb(skb);
838ead2ceb0SNeil Horman 	__kfree_skb(skb);
839ead2ceb0SNeil Horman }
840ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
841be769db2SHerbert Xu #endif
842ead2ceb0SNeil Horman 
8430a463c78SPaolo Abeni /**
8440a463c78SPaolo Abeni  *	consume_stateless_skb - free an skbuff, assuming it is stateless
8450a463c78SPaolo Abeni  *	@skb: buffer to free
8460a463c78SPaolo Abeni  *
847ca2c1418SPaolo Abeni  *	Alike consume_skb(), but this variant assumes that this is the last
848ca2c1418SPaolo Abeni  *	skb reference and all the head states have been already dropped
8490a463c78SPaolo Abeni  */
850ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb)
8510a463c78SPaolo Abeni {
8520a463c78SPaolo Abeni 	trace_consume_skb(skb);
8530a463c78SPaolo Abeni 	skb_release_data(skb);
8540a463c78SPaolo Abeni 	kfree_skbmem(skb);
8550a463c78SPaolo Abeni }
8560a463c78SPaolo Abeni 
857795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void)
858795bb1c0SJesper Dangaard Brouer {
859795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
860795bb1c0SJesper Dangaard Brouer 
861795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if containing objects */
862795bb1c0SJesper Dangaard Brouer 	if (nc->skb_count) {
863795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
864795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
865795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
866795bb1c0SJesper Dangaard Brouer 	}
867795bb1c0SJesper Dangaard Brouer }
868795bb1c0SJesper Dangaard Brouer 
86915fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb)
870795bb1c0SJesper Dangaard Brouer {
871795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
872795bb1c0SJesper Dangaard Brouer 
873795bb1c0SJesper Dangaard Brouer 	/* drop skb->head and call any destructors for packet */
874795bb1c0SJesper Dangaard Brouer 	skb_release_all(skb);
875795bb1c0SJesper Dangaard Brouer 
876795bb1c0SJesper Dangaard Brouer 	/* record skb to CPU local list */
877795bb1c0SJesper Dangaard Brouer 	nc->skb_cache[nc->skb_count++] = skb;
878795bb1c0SJesper Dangaard Brouer 
879795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB
880795bb1c0SJesper Dangaard Brouer 	/* SLUB writes into objects when freeing */
881795bb1c0SJesper Dangaard Brouer 	prefetchw(skb);
882795bb1c0SJesper Dangaard Brouer #endif
883795bb1c0SJesper Dangaard Brouer 
884795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if it is filled */
885795bb1c0SJesper Dangaard Brouer 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
886795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
887795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
888795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
889795bb1c0SJesper Dangaard Brouer 	}
890795bb1c0SJesper Dangaard Brouer }
89115fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb)
89215fad714SJesper Dangaard Brouer {
89315fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
89415fad714SJesper Dangaard Brouer }
895795bb1c0SJesper Dangaard Brouer 
896795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
897795bb1c0SJesper Dangaard Brouer {
898795bb1c0SJesper Dangaard Brouer 	if (unlikely(!skb))
899795bb1c0SJesper Dangaard Brouer 		return;
900795bb1c0SJesper Dangaard Brouer 
901885eb0a5SJesper Dangaard Brouer 	/* Zero budget indicate non-NAPI context called us, like netpoll */
902795bb1c0SJesper Dangaard Brouer 	if (unlikely(!budget)) {
903885eb0a5SJesper Dangaard Brouer 		dev_consume_skb_any(skb);
904795bb1c0SJesper Dangaard Brouer 		return;
905795bb1c0SJesper Dangaard Brouer 	}
906795bb1c0SJesper Dangaard Brouer 
9077608894eSPaolo Abeni 	if (!skb_unref(skb))
908795bb1c0SJesper Dangaard Brouer 		return;
9097608894eSPaolo Abeni 
910795bb1c0SJesper Dangaard Brouer 	/* if reaching here SKB is ready to free */
911795bb1c0SJesper Dangaard Brouer 	trace_consume_skb(skb);
912795bb1c0SJesper Dangaard Brouer 
913795bb1c0SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
914abbdb5a7SEric Dumazet 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
915795bb1c0SJesper Dangaard Brouer 		__kfree_skb(skb);
916795bb1c0SJesper Dangaard Brouer 		return;
917795bb1c0SJesper Dangaard Brouer 	}
918795bb1c0SJesper Dangaard Brouer 
91915fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
920795bb1c0SJesper Dangaard Brouer }
921795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
922795bb1c0SJesper Dangaard Brouer 
923b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
924b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
925b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
926b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
927b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
928b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
929b1937227SEric Dumazet 
930dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
931dec18810SHerbert Xu {
932dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
933b1937227SEric Dumazet 	/* We do not copy old->sk */
934dec18810SHerbert Xu 	new->dev		= old->dev;
935b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
9367fee226aSEric Dumazet 	skb_dst_copy(new, old);
937df5042f4SFlorian Westphal 	__skb_ext_copy(new, old);
938b1937227SEric Dumazet 	__nf_copy(new, old, false);
9396aa895b0SPatrick McHardy 
940b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
941b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
942b1937227SEric Dumazet 	 */
943b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
94406021292SEliezer Tamir 
945b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
946b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
947b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
948b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
949b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
950b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
951b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
952b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
953b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
954b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
955b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
956b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
957b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
958b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
959b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
960b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
961b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
962b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
963b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
964b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
965b1937227SEric Dumazet #endif
966e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
967b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
96806021292SEliezer Tamir #endif
9692bd82484SEric Dumazet #ifdef CONFIG_XPS
9702bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
9712bd82484SEric Dumazet #endif
972b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
973b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
974b1937227SEric Dumazet #endif
975b1937227SEric Dumazet 
976dec18810SHerbert Xu }
977dec18810SHerbert Xu 
97882c49a35SHerbert Xu /*
97982c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
98082c49a35SHerbert Xu  * __copy_skb_header above instead.
98182c49a35SHerbert Xu  */
982e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds #define C(x) n->x = skb->x
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	n->next = n->prev = NULL;
9871da177e4SLinus Torvalds 	n->sk = NULL;
988dec18810SHerbert Xu 	__copy_skb_header(n, skb);
989dec18810SHerbert Xu 
9901da177e4SLinus Torvalds 	C(len);
9911da177e4SLinus Torvalds 	C(data_len);
9923e6b3b2eSAlexey Dobriyan 	C(mac_len);
993334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
99402f1c89dSPaul Moore 	n->cloned = 1;
9951da177e4SLinus Torvalds 	n->nohdr = 0;
996b13dda9fSEric Dumazet 	n->peeked = 0;
997e78bfb07SStefano Brivio 	C(pfmemalloc);
9981da177e4SLinus Torvalds 	n->destructor = NULL;
9991da177e4SLinus Torvalds 	C(tail);
10001da177e4SLinus Torvalds 	C(end);
100102f1c89dSPaul Moore 	C(head);
1002d3836f21SEric Dumazet 	C(head_frag);
100302f1c89dSPaul Moore 	C(data);
100402f1c89dSPaul Moore 	C(truesize);
100563354797SReshetova, Elena 	refcount_set(&n->users, 1);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
10081da177e4SLinus Torvalds 	skb->cloned = 1;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	return n;
1011e0053ec0SHerbert Xu #undef C
1012e0053ec0SHerbert Xu }
1013e0053ec0SHerbert Xu 
1014e0053ec0SHerbert Xu /**
1015da29e4b4SJakub Kicinski  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1016da29e4b4SJakub Kicinski  * @first: first sk_buff of the msg
1017da29e4b4SJakub Kicinski  */
1018da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1019da29e4b4SJakub Kicinski {
1020da29e4b4SJakub Kicinski 	struct sk_buff *n;
1021da29e4b4SJakub Kicinski 
1022da29e4b4SJakub Kicinski 	n = alloc_skb(0, GFP_ATOMIC);
1023da29e4b4SJakub Kicinski 	if (!n)
1024da29e4b4SJakub Kicinski 		return NULL;
1025da29e4b4SJakub Kicinski 
1026da29e4b4SJakub Kicinski 	n->len = first->len;
1027da29e4b4SJakub Kicinski 	n->data_len = first->len;
1028da29e4b4SJakub Kicinski 	n->truesize = first->truesize;
1029da29e4b4SJakub Kicinski 
1030da29e4b4SJakub Kicinski 	skb_shinfo(n)->frag_list = first;
1031da29e4b4SJakub Kicinski 
1032da29e4b4SJakub Kicinski 	__copy_skb_header(n, first);
1033da29e4b4SJakub Kicinski 	n->destructor = NULL;
1034da29e4b4SJakub Kicinski 
1035da29e4b4SJakub Kicinski 	return n;
1036da29e4b4SJakub Kicinski }
1037da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1038da29e4b4SJakub Kicinski 
1039da29e4b4SJakub Kicinski /**
1040e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
1041e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
1042e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
1043e0053ec0SHerbert Xu  *
1044e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
1045e0053ec0SHerbert Xu  *	supplied by the user.
1046e0053ec0SHerbert Xu  *
1047e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
1048e0053ec0SHerbert Xu  */
1049e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1050e0053ec0SHerbert Xu {
10512d4baff8SHerbert Xu 	skb_release_all(dst);
1052e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
1053e0053ec0SHerbert Xu }
1054e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
1055e0053ec0SHerbert Xu 
10566f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1057a91dbff5SWillem de Bruijn {
1058a91dbff5SWillem de Bruijn 	unsigned long max_pg, num_pg, new_pg, old_pg;
1059a91dbff5SWillem de Bruijn 	struct user_struct *user;
1060a91dbff5SWillem de Bruijn 
1061a91dbff5SWillem de Bruijn 	if (capable(CAP_IPC_LOCK) || !size)
1062a91dbff5SWillem de Bruijn 		return 0;
1063a91dbff5SWillem de Bruijn 
1064a91dbff5SWillem de Bruijn 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
1065a91dbff5SWillem de Bruijn 	max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1066a91dbff5SWillem de Bruijn 	user = mmp->user ? : current_user();
1067a91dbff5SWillem de Bruijn 
1068a91dbff5SWillem de Bruijn 	do {
1069a91dbff5SWillem de Bruijn 		old_pg = atomic_long_read(&user->locked_vm);
1070a91dbff5SWillem de Bruijn 		new_pg = old_pg + num_pg;
1071a91dbff5SWillem de Bruijn 		if (new_pg > max_pg)
1072a91dbff5SWillem de Bruijn 			return -ENOBUFS;
1073a91dbff5SWillem de Bruijn 	} while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1074a91dbff5SWillem de Bruijn 		 old_pg);
1075a91dbff5SWillem de Bruijn 
1076a91dbff5SWillem de Bruijn 	if (!mmp->user) {
1077a91dbff5SWillem de Bruijn 		mmp->user = get_uid(user);
1078a91dbff5SWillem de Bruijn 		mmp->num_pg = num_pg;
1079a91dbff5SWillem de Bruijn 	} else {
1080a91dbff5SWillem de Bruijn 		mmp->num_pg += num_pg;
1081a91dbff5SWillem de Bruijn 	}
1082a91dbff5SWillem de Bruijn 
1083a91dbff5SWillem de Bruijn 	return 0;
1084a91dbff5SWillem de Bruijn }
10856f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1086a91dbff5SWillem de Bruijn 
10876f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp)
1088a91dbff5SWillem de Bruijn {
1089a91dbff5SWillem de Bruijn 	if (mmp->user) {
1090a91dbff5SWillem de Bruijn 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1091a91dbff5SWillem de Bruijn 		free_uid(mmp->user);
1092a91dbff5SWillem de Bruijn 	}
1093a91dbff5SWillem de Bruijn }
10946f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1095a91dbff5SWillem de Bruijn 
109652267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
109752267790SWillem de Bruijn {
109852267790SWillem de Bruijn 	struct ubuf_info *uarg;
109952267790SWillem de Bruijn 	struct sk_buff *skb;
110052267790SWillem de Bruijn 
110152267790SWillem de Bruijn 	WARN_ON_ONCE(!in_task());
110252267790SWillem de Bruijn 
110352267790SWillem de Bruijn 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
110452267790SWillem de Bruijn 	if (!skb)
110552267790SWillem de Bruijn 		return NULL;
110652267790SWillem de Bruijn 
110752267790SWillem de Bruijn 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
110852267790SWillem de Bruijn 	uarg = (void *)skb->cb;
1109a91dbff5SWillem de Bruijn 	uarg->mmp.user = NULL;
1110a91dbff5SWillem de Bruijn 
1111a91dbff5SWillem de Bruijn 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
1112a91dbff5SWillem de Bruijn 		kfree_skb(skb);
1113a91dbff5SWillem de Bruijn 		return NULL;
1114a91dbff5SWillem de Bruijn 	}
111552267790SWillem de Bruijn 
111652267790SWillem de Bruijn 	uarg->callback = sock_zerocopy_callback;
11174ab6c99dSWillem de Bruijn 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
11184ab6c99dSWillem de Bruijn 	uarg->len = 1;
11194ab6c99dSWillem de Bruijn 	uarg->bytelen = size;
112052267790SWillem de Bruijn 	uarg->zerocopy = 1;
1121c1d1b437SEric Dumazet 	refcount_set(&uarg->refcnt, 1);
112252267790SWillem de Bruijn 	sock_hold(sk);
112352267790SWillem de Bruijn 
112452267790SWillem de Bruijn 	return uarg;
112552267790SWillem de Bruijn }
112652267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
112752267790SWillem de Bruijn 
112852267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
112952267790SWillem de Bruijn {
113052267790SWillem de Bruijn 	return container_of((void *)uarg, struct sk_buff, cb);
113152267790SWillem de Bruijn }
113252267790SWillem de Bruijn 
11334ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
11344ab6c99dSWillem de Bruijn 					struct ubuf_info *uarg)
11354ab6c99dSWillem de Bruijn {
11364ab6c99dSWillem de Bruijn 	if (uarg) {
11374ab6c99dSWillem de Bruijn 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
11384ab6c99dSWillem de Bruijn 		u32 bytelen, next;
11394ab6c99dSWillem de Bruijn 
11404ab6c99dSWillem de Bruijn 		/* realloc only when socket is locked (TCP, UDP cork),
11414ab6c99dSWillem de Bruijn 		 * so uarg->len and sk_zckey access is serialized
11424ab6c99dSWillem de Bruijn 		 */
11434ab6c99dSWillem de Bruijn 		if (!sock_owned_by_user(sk)) {
11444ab6c99dSWillem de Bruijn 			WARN_ON_ONCE(1);
11454ab6c99dSWillem de Bruijn 			return NULL;
11464ab6c99dSWillem de Bruijn 		}
11474ab6c99dSWillem de Bruijn 
11484ab6c99dSWillem de Bruijn 		bytelen = uarg->bytelen + size;
11494ab6c99dSWillem de Bruijn 		if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
11504ab6c99dSWillem de Bruijn 			/* TCP can create new skb to attach new uarg */
11514ab6c99dSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
11524ab6c99dSWillem de Bruijn 				goto new_alloc;
11534ab6c99dSWillem de Bruijn 			return NULL;
11544ab6c99dSWillem de Bruijn 		}
11554ab6c99dSWillem de Bruijn 
11564ab6c99dSWillem de Bruijn 		next = (u32)atomic_read(&sk->sk_zckey);
11574ab6c99dSWillem de Bruijn 		if ((u32)(uarg->id + uarg->len) == next) {
1158a91dbff5SWillem de Bruijn 			if (mm_account_pinned_pages(&uarg->mmp, size))
1159a91dbff5SWillem de Bruijn 				return NULL;
11604ab6c99dSWillem de Bruijn 			uarg->len++;
11614ab6c99dSWillem de Bruijn 			uarg->bytelen = bytelen;
11624ab6c99dSWillem de Bruijn 			atomic_set(&sk->sk_zckey, ++next);
1163100f6d8eSWillem de Bruijn 
1164100f6d8eSWillem de Bruijn 			/* no extra ref when appending to datagram (MSG_MORE) */
1165100f6d8eSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
1166db5bce32SEric Dumazet 				sock_zerocopy_get(uarg);
1167100f6d8eSWillem de Bruijn 
11684ab6c99dSWillem de Bruijn 			return uarg;
11694ab6c99dSWillem de Bruijn 		}
11704ab6c99dSWillem de Bruijn 	}
11714ab6c99dSWillem de Bruijn 
11724ab6c99dSWillem de Bruijn new_alloc:
11734ab6c99dSWillem de Bruijn 	return sock_zerocopy_alloc(sk, size);
11744ab6c99dSWillem de Bruijn }
11754ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
11764ab6c99dSWillem de Bruijn 
11774ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
11784ab6c99dSWillem de Bruijn {
11794ab6c99dSWillem de Bruijn 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
11804ab6c99dSWillem de Bruijn 	u32 old_lo, old_hi;
11814ab6c99dSWillem de Bruijn 	u64 sum_len;
11824ab6c99dSWillem de Bruijn 
11834ab6c99dSWillem de Bruijn 	old_lo = serr->ee.ee_info;
11844ab6c99dSWillem de Bruijn 	old_hi = serr->ee.ee_data;
11854ab6c99dSWillem de Bruijn 	sum_len = old_hi - old_lo + 1ULL + len;
11864ab6c99dSWillem de Bruijn 
11874ab6c99dSWillem de Bruijn 	if (sum_len >= (1ULL << 32))
11884ab6c99dSWillem de Bruijn 		return false;
11894ab6c99dSWillem de Bruijn 
11904ab6c99dSWillem de Bruijn 	if (lo != old_hi + 1)
11914ab6c99dSWillem de Bruijn 		return false;
11924ab6c99dSWillem de Bruijn 
11934ab6c99dSWillem de Bruijn 	serr->ee.ee_data += len;
11944ab6c99dSWillem de Bruijn 	return true;
11954ab6c99dSWillem de Bruijn }
11964ab6c99dSWillem de Bruijn 
119752267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
119852267790SWillem de Bruijn {
11994ab6c99dSWillem de Bruijn 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
120052267790SWillem de Bruijn 	struct sock_exterr_skb *serr;
120152267790SWillem de Bruijn 	struct sock *sk = skb->sk;
12024ab6c99dSWillem de Bruijn 	struct sk_buff_head *q;
12034ab6c99dSWillem de Bruijn 	unsigned long flags;
12044ab6c99dSWillem de Bruijn 	u32 lo, hi;
12054ab6c99dSWillem de Bruijn 	u16 len;
120652267790SWillem de Bruijn 
1207ccaffff1SWillem de Bruijn 	mm_unaccount_pinned_pages(&uarg->mmp);
1208ccaffff1SWillem de Bruijn 
12094ab6c99dSWillem de Bruijn 	/* if !len, there was only 1 call, and it was aborted
12104ab6c99dSWillem de Bruijn 	 * so do not queue a completion notification
12114ab6c99dSWillem de Bruijn 	 */
12124ab6c99dSWillem de Bruijn 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
121352267790SWillem de Bruijn 		goto release;
121452267790SWillem de Bruijn 
12154ab6c99dSWillem de Bruijn 	len = uarg->len;
12164ab6c99dSWillem de Bruijn 	lo = uarg->id;
12174ab6c99dSWillem de Bruijn 	hi = uarg->id + len - 1;
12184ab6c99dSWillem de Bruijn 
121952267790SWillem de Bruijn 	serr = SKB_EXT_ERR(skb);
122052267790SWillem de Bruijn 	memset(serr, 0, sizeof(*serr));
122152267790SWillem de Bruijn 	serr->ee.ee_errno = 0;
122252267790SWillem de Bruijn 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
12234ab6c99dSWillem de Bruijn 	serr->ee.ee_data = hi;
12244ab6c99dSWillem de Bruijn 	serr->ee.ee_info = lo;
122552267790SWillem de Bruijn 	if (!success)
122652267790SWillem de Bruijn 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
122752267790SWillem de Bruijn 
12284ab6c99dSWillem de Bruijn 	q = &sk->sk_error_queue;
12294ab6c99dSWillem de Bruijn 	spin_lock_irqsave(&q->lock, flags);
12304ab6c99dSWillem de Bruijn 	tail = skb_peek_tail(q);
12314ab6c99dSWillem de Bruijn 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
12324ab6c99dSWillem de Bruijn 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
12334ab6c99dSWillem de Bruijn 		__skb_queue_tail(q, skb);
123452267790SWillem de Bruijn 		skb = NULL;
12354ab6c99dSWillem de Bruijn 	}
12364ab6c99dSWillem de Bruijn 	spin_unlock_irqrestore(&q->lock, flags);
123752267790SWillem de Bruijn 
123852267790SWillem de Bruijn 	sk->sk_error_report(sk);
123952267790SWillem de Bruijn 
124052267790SWillem de Bruijn release:
124152267790SWillem de Bruijn 	consume_skb(skb);
124252267790SWillem de Bruijn 	sock_put(sk);
124352267790SWillem de Bruijn }
124452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
124552267790SWillem de Bruijn 
124652267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg)
124752267790SWillem de Bruijn {
1248c1d1b437SEric Dumazet 	if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
124952267790SWillem de Bruijn 		if (uarg->callback)
125052267790SWillem de Bruijn 			uarg->callback(uarg, uarg->zerocopy);
125152267790SWillem de Bruijn 		else
125252267790SWillem de Bruijn 			consume_skb(skb_from_uarg(uarg));
125352267790SWillem de Bruijn 	}
125452267790SWillem de Bruijn }
125552267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put);
125652267790SWillem de Bruijn 
125752900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
125852267790SWillem de Bruijn {
125952267790SWillem de Bruijn 	if (uarg) {
126052267790SWillem de Bruijn 		struct sock *sk = skb_from_uarg(uarg)->sk;
126152267790SWillem de Bruijn 
126252267790SWillem de Bruijn 		atomic_dec(&sk->sk_zckey);
12634ab6c99dSWillem de Bruijn 		uarg->len--;
126452267790SWillem de Bruijn 
126552900d22SWillem de Bruijn 		if (have_uref)
126652267790SWillem de Bruijn 			sock_zerocopy_put(uarg);
126752267790SWillem de Bruijn 	}
126852267790SWillem de Bruijn }
126952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
127052267790SWillem de Bruijn 
1271b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1272b5947e5dSWillem de Bruijn {
1273b5947e5dSWillem de Bruijn 	return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1274b5947e5dSWillem de Bruijn }
1275b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1276b5947e5dSWillem de Bruijn 
127752267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
127852267790SWillem de Bruijn 			     struct msghdr *msg, int len,
127952267790SWillem de Bruijn 			     struct ubuf_info *uarg)
128052267790SWillem de Bruijn {
12814ab6c99dSWillem de Bruijn 	struct ubuf_info *orig_uarg = skb_zcopy(skb);
128252267790SWillem de Bruijn 	struct iov_iter orig_iter = msg->msg_iter;
128352267790SWillem de Bruijn 	int err, orig_len = skb->len;
128452267790SWillem de Bruijn 
12854ab6c99dSWillem de Bruijn 	/* An skb can only point to one uarg. This edge case happens when
12864ab6c99dSWillem de Bruijn 	 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
12874ab6c99dSWillem de Bruijn 	 */
12884ab6c99dSWillem de Bruijn 	if (orig_uarg && uarg != orig_uarg)
12894ab6c99dSWillem de Bruijn 		return -EEXIST;
12904ab6c99dSWillem de Bruijn 
129152267790SWillem de Bruijn 	err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
129252267790SWillem de Bruijn 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
129354d43117SWillem de Bruijn 		struct sock *save_sk = skb->sk;
129454d43117SWillem de Bruijn 
129552267790SWillem de Bruijn 		/* Streams do not free skb on error. Reset to prev state. */
129652267790SWillem de Bruijn 		msg->msg_iter = orig_iter;
129754d43117SWillem de Bruijn 		skb->sk = sk;
129852267790SWillem de Bruijn 		___pskb_trim(skb, orig_len);
129954d43117SWillem de Bruijn 		skb->sk = save_sk;
130052267790SWillem de Bruijn 		return err;
130152267790SWillem de Bruijn 	}
130252267790SWillem de Bruijn 
130352900d22SWillem de Bruijn 	skb_zcopy_set(skb, uarg, NULL);
130452267790SWillem de Bruijn 	return skb->len - orig_len;
130552267790SWillem de Bruijn }
130652267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
130752267790SWillem de Bruijn 
13081f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
130952267790SWillem de Bruijn 			      gfp_t gfp_mask)
131052267790SWillem de Bruijn {
131152267790SWillem de Bruijn 	if (skb_zcopy(orig)) {
131252267790SWillem de Bruijn 		if (skb_zcopy(nskb)) {
131352267790SWillem de Bruijn 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
131452267790SWillem de Bruijn 			if (!gfp_mask) {
131552267790SWillem de Bruijn 				WARN_ON_ONCE(1);
131652267790SWillem de Bruijn 				return -ENOMEM;
131752267790SWillem de Bruijn 			}
131852267790SWillem de Bruijn 			if (skb_uarg(nskb) == skb_uarg(orig))
131952267790SWillem de Bruijn 				return 0;
132052267790SWillem de Bruijn 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
132152267790SWillem de Bruijn 				return -EIO;
132252267790SWillem de Bruijn 		}
132352900d22SWillem de Bruijn 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
132452267790SWillem de Bruijn 	}
132552267790SWillem de Bruijn 	return 0;
132652267790SWillem de Bruijn }
132752267790SWillem de Bruijn 
13282c53040fSBen Hutchings /**
13292c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
133048c83012SMichael S. Tsirkin  *	@skb: the skb to modify
133148c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
133248c83012SMichael S. Tsirkin  *
133348c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
133448c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
133548c83012SMichael S. Tsirkin  *	to userspace pages.
133648c83012SMichael S. Tsirkin  *
133748c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
133848c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
133948c83012SMichael S. Tsirkin  *
134048c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
134148c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
134248c83012SMichael S. Tsirkin  */
134348c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1344a6686f2fSShirley Ma {
1345a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
1346a6686f2fSShirley Ma 	struct page *page, *head = NULL;
13473ece7826SWillem de Bruijn 	int i, new_frags;
13483ece7826SWillem de Bruijn 	u32 d_off;
1349a6686f2fSShirley Ma 
13503ece7826SWillem de Bruijn 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
13513ece7826SWillem de Bruijn 		return -EINVAL;
13523ece7826SWillem de Bruijn 
1353f72c4ac6SWillem de Bruijn 	if (!num_frags)
1354f72c4ac6SWillem de Bruijn 		goto release;
1355f72c4ac6SWillem de Bruijn 
13563ece7826SWillem de Bruijn 	new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
13573ece7826SWillem de Bruijn 	for (i = 0; i < new_frags; i++) {
135802756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
1359a6686f2fSShirley Ma 		if (!page) {
1360a6686f2fSShirley Ma 			while (head) {
136140dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
1362a6686f2fSShirley Ma 				put_page(head);
1363a6686f2fSShirley Ma 				head = next;
1364a6686f2fSShirley Ma 			}
1365a6686f2fSShirley Ma 			return -ENOMEM;
1366a6686f2fSShirley Ma 		}
13673ece7826SWillem de Bruijn 		set_page_private(page, (unsigned long)head);
13683ece7826SWillem de Bruijn 		head = page;
13693ece7826SWillem de Bruijn 	}
13703ece7826SWillem de Bruijn 
13713ece7826SWillem de Bruijn 	page = head;
13723ece7826SWillem de Bruijn 	d_off = 0;
13733ece7826SWillem de Bruijn 	for (i = 0; i < num_frags; i++) {
13743ece7826SWillem de Bruijn 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
13753ece7826SWillem de Bruijn 		u32 p_off, p_len, copied;
13763ece7826SWillem de Bruijn 		struct page *p;
13773ece7826SWillem de Bruijn 		u8 *vaddr;
1378c613c209SWillem de Bruijn 
1379b54c9d5bSJonathan Lemon 		skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1380c613c209SWillem de Bruijn 				      p, p_off, p_len, copied) {
13813ece7826SWillem de Bruijn 			u32 copy, done = 0;
1382c613c209SWillem de Bruijn 			vaddr = kmap_atomic(p);
13833ece7826SWillem de Bruijn 
13843ece7826SWillem de Bruijn 			while (done < p_len) {
13853ece7826SWillem de Bruijn 				if (d_off == PAGE_SIZE) {
13863ece7826SWillem de Bruijn 					d_off = 0;
13873ece7826SWillem de Bruijn 					page = (struct page *)page_private(page);
13883ece7826SWillem de Bruijn 				}
13893ece7826SWillem de Bruijn 				copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
13903ece7826SWillem de Bruijn 				memcpy(page_address(page) + d_off,
13913ece7826SWillem de Bruijn 				       vaddr + p_off + done, copy);
13923ece7826SWillem de Bruijn 				done += copy;
13933ece7826SWillem de Bruijn 				d_off += copy;
13943ece7826SWillem de Bruijn 			}
139551c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1396c613c209SWillem de Bruijn 		}
1397a6686f2fSShirley Ma 	}
1398a6686f2fSShirley Ma 
1399a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
140002756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
1401a8605c60SIan Campbell 		skb_frag_unref(skb, i);
1402a6686f2fSShirley Ma 
1403a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
14043ece7826SWillem de Bruijn 	for (i = 0; i < new_frags - 1; i++) {
14053ece7826SWillem de Bruijn 		__skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
140640dadff2SSunghan Suh 		head = (struct page *)page_private(head);
1407a6686f2fSShirley Ma 	}
14083ece7826SWillem de Bruijn 	__skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
14093ece7826SWillem de Bruijn 	skb_shinfo(skb)->nr_frags = new_frags;
141048c83012SMichael S. Tsirkin 
1411b90ddd56SWillem de Bruijn release:
14121f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, false);
1413a6686f2fSShirley Ma 	return 0;
1414a6686f2fSShirley Ma }
1415dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1416a6686f2fSShirley Ma 
1417e0053ec0SHerbert Xu /**
1418e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
1419e0053ec0SHerbert Xu  *	@skb: buffer to clone
1420e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
1421e0053ec0SHerbert Xu  *
1422e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1423e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
1424e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
1425e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
1426e0053ec0SHerbert Xu  *
1427e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
1428e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
1429e0053ec0SHerbert Xu  */
1430e0053ec0SHerbert Xu 
1431e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1432e0053ec0SHerbert Xu {
1433d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
1434d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
1435d0bf4a9eSEric Dumazet 						       skb1);
14366ffe75ebSEric Dumazet 	struct sk_buff *n;
1437e0053ec0SHerbert Xu 
143870008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
1439a6686f2fSShirley Ma 		return NULL;
1440a6686f2fSShirley Ma 
1441e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
14422638595aSReshetova, Elena 	    refcount_read(&fclones->fclone_ref) == 1) {
14436ffe75ebSEric Dumazet 		n = &fclones->skb2;
14442638595aSReshetova, Elena 		refcount_set(&fclones->fclone_ref, 2);
1445e0053ec0SHerbert Xu 	} else {
1446c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
1447c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
1448c93bdd0eSMel Gorman 
1449e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1450e0053ec0SHerbert Xu 		if (!n)
1451e0053ec0SHerbert Xu 			return NULL;
1452fe55f6d5SVegard Nossum 
1453e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1454e0053ec0SHerbert Xu 	}
1455e0053ec0SHerbert Xu 
1456e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
14571da177e4SLinus Torvalds }
1458b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
14591da177e4SLinus Torvalds 
1460b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off)
1461f5b17294SPravin B Shelar {
1462030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
1463030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1464030737bcSEric Dumazet 		skb->csum_start += off;
1465f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1466f5b17294SPravin B Shelar 	skb->transport_header += off;
1467f5b17294SPravin B Shelar 	skb->network_header   += off;
1468f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1469f5b17294SPravin B Shelar 		skb->mac_header += off;
1470f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1471f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1472aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1473f5b17294SPravin B Shelar }
1474b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update);
1475f5b17294SPravin B Shelar 
147608303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
14771da177e4SLinus Torvalds {
1478dec18810SHerbert Xu 	__copy_skb_header(new, old);
1479dec18810SHerbert Xu 
14807967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
14817967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
14827967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
14831da177e4SLinus Torvalds }
148408303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header);
14851da177e4SLinus Torvalds 
1486c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1487c93bdd0eSMel Gorman {
1488c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1489c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1490c93bdd0eSMel Gorman 	return 0;
1491c93bdd0eSMel Gorman }
1492c93bdd0eSMel Gorman 
14931da177e4SLinus Torvalds /**
14941da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
14951da177e4SLinus Torvalds  *	@skb: buffer to copy
14961da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
14971da177e4SLinus Torvalds  *
14981da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
14991da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
15001da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
15011da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
15021da177e4SLinus Torvalds  *
15031da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
15041da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
15051da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
15061da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
15071da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
15081da177e4SLinus Torvalds  */
15091da177e4SLinus Torvalds 
1510dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
15111da177e4SLinus Torvalds {
15126602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1513ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1514c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1515c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
15166602cebbSEric Dumazet 
15171da177e4SLinus Torvalds 	if (!n)
15181da177e4SLinus Torvalds 		return NULL;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	/* Set the data pointer */
15211da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
15221da177e4SLinus Torvalds 	/* Set the tail pointer and length */
15231da177e4SLinus Torvalds 	skb_put(n, skb->len);
15241da177e4SLinus Torvalds 
15259f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
15261da177e4SLinus Torvalds 
152708303c18SIlya Lesokhin 	skb_copy_header(n, skb);
15281da177e4SLinus Torvalds 	return n;
15291da177e4SLinus Torvalds }
1530b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /**
1533bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
15341da177e4SLinus Torvalds  *	@skb: buffer to copy
1535117632e6SEric Dumazet  *	@headroom: headroom of new skb
15361da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1537bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1538bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1539bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
15401da177e4SLinus Torvalds  *
15411da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
15421da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
15431da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
15441da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
15451da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
15461da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds 
1549bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1550bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
15511da177e4SLinus Torvalds {
1552117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1553bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1554bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
15556602cebbSEric Dumazet 
15561da177e4SLinus Torvalds 	if (!n)
15571da177e4SLinus Torvalds 		goto out;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	/* Set the data pointer */
1560117632e6SEric Dumazet 	skb_reserve(n, headroom);
15611da177e4SLinus Torvalds 	/* Set the tail pointer and length */
15621da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
15631da177e4SLinus Torvalds 	/* Copy the bytes */
1564d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
15651da177e4SLinus Torvalds 
156625f484a6SHerbert Xu 	n->truesize += skb->data_len;
15671da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
15681da177e4SLinus Torvalds 	n->len	     = skb->len;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
15711da177e4SLinus Torvalds 		int i;
15721da177e4SLinus Torvalds 
15731f8b977aSWillem de Bruijn 		if (skb_orphan_frags(skb, gfp_mask) ||
15741f8b977aSWillem de Bruijn 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
15751511022cSDan Carpenter 			kfree_skb(n);
15761511022cSDan Carpenter 			n = NULL;
1577a6686f2fSShirley Ma 			goto out;
1578a6686f2fSShirley Ma 		}
15791da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15801da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1581ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
15821da177e4SLinus Torvalds 		}
15831da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds 
158621dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
15871da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
15881da177e4SLinus Torvalds 		skb_clone_fraglist(n);
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 
159108303c18SIlya Lesokhin 	skb_copy_header(n, skb);
15921da177e4SLinus Torvalds out:
15931da177e4SLinus Torvalds 	return n;
15941da177e4SLinus Torvalds }
1595bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds /**
15981da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
15991da177e4SLinus Torvalds  *	@skb: buffer to reallocate
16001da177e4SLinus Torvalds  *	@nhead: room to add at head
16011da177e4SLinus Torvalds  *	@ntail: room to add at tail
16021da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
16031da177e4SLinus Torvalds  *
1604bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1605bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
16061da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
16071da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
16081da177e4SLinus Torvalds  *
16091da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
16101da177e4SLinus Torvalds  *	reloaded after call to this function.
16111da177e4SLinus Torvalds  */
16121da177e4SLinus Torvalds 
161386a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1614dd0fc66fSAl Viro 		     gfp_t gfp_mask)
16151da177e4SLinus Torvalds {
1616158f323bSEric Dumazet 	int i, osize = skb_end_offset(skb);
1617158f323bSEric Dumazet 	int size = osize + nhead + ntail;
16181da177e4SLinus Torvalds 	long off;
1619158f323bSEric Dumazet 	u8 *data;
16201da177e4SLinus Torvalds 
16214edd87adSHerbert Xu 	BUG_ON(nhead < 0);
16224edd87adSHerbert Xu 
16239f77fad3STim Hansen 	BUG_ON(skb_shared(skb));
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
16261da177e4SLinus Torvalds 
1627c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1628c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1629c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1630c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
16311da177e4SLinus Torvalds 	if (!data)
16321da177e4SLinus Torvalds 		goto nodata;
163387151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
16366602cebbSEric Dumazet 	 * optimized for the cases when header is void.
16376602cebbSEric Dumazet 	 */
16386602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
16396602cebbSEric Dumazet 
16406602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
16416602cebbSEric Dumazet 	       skb_shinfo(skb),
1642fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
16431da177e4SLinus Torvalds 
16443e24591aSAlexander Duyck 	/*
16453e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
16463e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
16473e24591aSAlexander Duyck 	 * be since all we did is relocate the values
16483e24591aSAlexander Duyck 	 */
16493e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
165070008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1651a6686f2fSShirley Ma 			goto nofrags;
16521f8b977aSWillem de Bruijn 		if (skb_zcopy(skb))
1653c1d1b437SEric Dumazet 			refcount_inc(&skb_uarg(skb)->refcnt);
16541da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1655ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
16561da177e4SLinus Torvalds 
165721dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
16581da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 		skb_release_data(skb);
16613e24591aSAlexander Duyck 	} else {
16623e24591aSAlexander Duyck 		skb_free_head(skb);
16631fd63041SEric Dumazet 	}
16641da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	skb->head     = data;
1667d3836f21SEric Dumazet 	skb->head_frag = 0;
16681da177e4SLinus Torvalds 	skb->data    += off;
16694305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
16704305b541SArnaldo Carvalho de Melo 	skb->end      = size;
167156eb8882SPatrick McHardy 	off           = nhead;
16724305b541SArnaldo Carvalho de Melo #else
16734305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
167456eb8882SPatrick McHardy #endif
167527a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1676b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
16771da177e4SLinus Torvalds 	skb->cloned   = 0;
1678334a8132SPatrick McHardy 	skb->hdr_len  = 0;
16791da177e4SLinus Torvalds 	skb->nohdr    = 0;
16801da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1681158f323bSEric Dumazet 
1682de8f3a83SDaniel Borkmann 	skb_metadata_clear(skb);
1683de8f3a83SDaniel Borkmann 
1684158f323bSEric Dumazet 	/* It is not generally safe to change skb->truesize.
1685158f323bSEric Dumazet 	 * For the moment, we really care of rx path, or
1686158f323bSEric Dumazet 	 * when skb is orphaned (not attached to a socket).
1687158f323bSEric Dumazet 	 */
1688158f323bSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
1689158f323bSEric Dumazet 		skb->truesize += size - osize;
1690158f323bSEric Dumazet 
16911da177e4SLinus Torvalds 	return 0;
16921da177e4SLinus Torvalds 
1693a6686f2fSShirley Ma nofrags:
1694a6686f2fSShirley Ma 	kfree(data);
16951da177e4SLinus Torvalds nodata:
16961da177e4SLinus Torvalds 	return -ENOMEM;
16971da177e4SLinus Torvalds }
1698b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	struct sk_buff *skb2;
17051da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	if (delta <= 0)
17081da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
17091da177e4SLinus Torvalds 	else {
17101da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
17111da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
17121da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
17131da177e4SLinus Torvalds 			kfree_skb(skb2);
17141da177e4SLinus Torvalds 			skb2 = NULL;
17151da177e4SLinus Torvalds 		}
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds 	return skb2;
17181da177e4SLinus Torvalds }
1719b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds /**
17221da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
17231da177e4SLinus Torvalds  *	@skb: buffer to copy
17241da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
17251da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
17261da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
17271da177e4SLinus Torvalds  *
17281da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
17291da177e4SLinus Torvalds  *	allocate additional space.
17301da177e4SLinus Torvalds  *
17311da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
17321da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
17331da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
17341da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
17351da177e4SLinus Torvalds  *
17361da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
17371da177e4SLinus Torvalds  *	is called from an interrupt.
17381da177e4SLinus Torvalds  */
17391da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
174086a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1741dd0fc66fSAl Viro 				gfp_t gfp_mask)
17421da177e4SLinus Torvalds {
17431da177e4SLinus Torvalds 	/*
17441da177e4SLinus Torvalds 	 *	Allocate the copy buffer
17451da177e4SLinus Torvalds 	 */
1746c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1747c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1748c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1749efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
17501da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	if (!n)
17531da177e4SLinus Torvalds 		return NULL;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	/* Set the tail pointer and length */
17581da177e4SLinus Torvalds 	skb_put(n, skb->len);
17591da177e4SLinus Torvalds 
1760efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
17611da177e4SLinus Torvalds 	head_copy_off = 0;
17621da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
17631da177e4SLinus Torvalds 		head_copy_len = newheadroom;
17641da177e4SLinus Torvalds 	else
17651da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	/* Copy the linear header and data. */
17689f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
17699f77fad3STim Hansen 			     skb->len + head_copy_len));
17701da177e4SLinus Torvalds 
177108303c18SIlya Lesokhin 	skb_copy_header(n, skb);
17721da177e4SLinus Torvalds 
1773030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1774efd1e8d5SPatrick McHardy 
17751da177e4SLinus Torvalds 	return n;
17761da177e4SLinus Torvalds }
1777b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds /**
1780cd0a137aSFlorian Fainelli  *	__skb_pad		-	zero pad the tail of an skb
17811da177e4SLinus Torvalds  *	@skb: buffer to pad
17821da177e4SLinus Torvalds  *	@pad: space to pad
1783cd0a137aSFlorian Fainelli  *	@free_on_error: free buffer on error
17841da177e4SLinus Torvalds  *
17851da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
17861da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
17871da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
17881da177e4SLinus Torvalds  *
1789cd0a137aSFlorian Fainelli  *	May return error in out of memory cases. The skb is freed on error
1790cd0a137aSFlorian Fainelli  *	if @free_on_error is true.
17911da177e4SLinus Torvalds  */
17921da177e4SLinus Torvalds 
1793cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
17941da177e4SLinus Torvalds {
17955b057c6bSHerbert Xu 	int err;
17965b057c6bSHerbert Xu 	int ntail;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
17995b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
18001da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
18015b057c6bSHerbert Xu 		return 0;
18021da177e4SLinus Torvalds 	}
18031da177e4SLinus Torvalds 
18044305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
18055b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
18065b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
18075b057c6bSHerbert Xu 		if (unlikely(err))
18085b057c6bSHerbert Xu 			goto free_skb;
18095b057c6bSHerbert Xu 	}
18105b057c6bSHerbert Xu 
18115b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
18125b057c6bSHerbert Xu 	 * to be audited.
18135b057c6bSHerbert Xu 	 */
18145b057c6bSHerbert Xu 	err = skb_linearize(skb);
18155b057c6bSHerbert Xu 	if (unlikely(err))
18165b057c6bSHerbert Xu 		goto free_skb;
18175b057c6bSHerbert Xu 
18185b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
18195b057c6bSHerbert Xu 	return 0;
18205b057c6bSHerbert Xu 
18215b057c6bSHerbert Xu free_skb:
1822cd0a137aSFlorian Fainelli 	if (free_on_error)
18231da177e4SLinus Torvalds 		kfree_skb(skb);
18245b057c6bSHerbert Xu 	return err;
18251da177e4SLinus Torvalds }
1826cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad);
18271da177e4SLinus Torvalds 
18280dde3e16SIlpo Järvinen /**
18290c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
18300c7ddf36SMathias Krause  *	@skb: start of the buffer to use
18310c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
18320c7ddf36SMathias Krause  *	@len: amount of data to add
18330c7ddf36SMathias Krause  *
18340c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
18350c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
18360c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
18370c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
18380c7ddf36SMathias Krause  *	returned.
18390c7ddf36SMathias Krause  */
18400c7ddf36SMathias Krause 
18414df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
18420c7ddf36SMathias Krause {
18430c7ddf36SMathias Krause 	if (tail != skb) {
18440c7ddf36SMathias Krause 		skb->data_len += len;
18450c7ddf36SMathias Krause 		skb->len += len;
18460c7ddf36SMathias Krause 	}
18470c7ddf36SMathias Krause 	return skb_put(tail, len);
18480c7ddf36SMathias Krause }
18490c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
18500c7ddf36SMathias Krause 
18510c7ddf36SMathias Krause /**
18520dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
18530dde3e16SIlpo Järvinen  *	@skb: buffer to use
18540dde3e16SIlpo Järvinen  *	@len: amount of data to add
18550dde3e16SIlpo Järvinen  *
18560dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
18570dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
18580dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
18590dde3e16SIlpo Järvinen  */
18604df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len)
18610dde3e16SIlpo Järvinen {
18624df864c1SJohannes Berg 	void *tmp = skb_tail_pointer(skb);
18630dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
18640dde3e16SIlpo Järvinen 	skb->tail += len;
18650dde3e16SIlpo Järvinen 	skb->len  += len;
18660dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
18670dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
18680dde3e16SIlpo Järvinen 	return tmp;
18690dde3e16SIlpo Järvinen }
18700dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
18710dde3e16SIlpo Järvinen 
18726be8ac2fSIlpo Järvinen /**
1873c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1874c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1875c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1876c2aa270aSIlpo Järvinen  *
1877c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1878c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1879c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1880c2aa270aSIlpo Järvinen  */
1881d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len)
1882c2aa270aSIlpo Järvinen {
1883c2aa270aSIlpo Järvinen 	skb->data -= len;
1884c2aa270aSIlpo Järvinen 	skb->len  += len;
1885c2aa270aSIlpo Järvinen 	if (unlikely(skb->data < skb->head))
1886c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1887c2aa270aSIlpo Järvinen 	return skb->data;
1888c2aa270aSIlpo Järvinen }
1889c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1890c2aa270aSIlpo Järvinen 
1891c2aa270aSIlpo Järvinen /**
18926be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
18936be8ac2fSIlpo Järvinen  *	@skb: buffer to use
18946be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
18956be8ac2fSIlpo Järvinen  *
18966be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
18976be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
18986be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
18996be8ac2fSIlpo Järvinen  *	the old data.
19006be8ac2fSIlpo Järvinen  */
1901af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len)
19026be8ac2fSIlpo Järvinen {
190347d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
19046be8ac2fSIlpo Järvinen }
19056be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
19066be8ac2fSIlpo Järvinen 
1907419ae74eSIlpo Järvinen /**
1908419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1909419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1910419ae74eSIlpo Järvinen  *	@len: new length
1911419ae74eSIlpo Järvinen  *
1912419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1913419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1914419ae74eSIlpo Järvinen  *	The skb must be linear.
1915419ae74eSIlpo Järvinen  */
1916419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1917419ae74eSIlpo Järvinen {
1918419ae74eSIlpo Järvinen 	if (skb->len > len)
1919419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1920419ae74eSIlpo Järvinen }
1921419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1922419ae74eSIlpo Järvinen 
19233cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
19241da177e4SLinus Torvalds  */
19251da177e4SLinus Torvalds 
19263cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
19271da177e4SLinus Torvalds {
192827b437c8SHerbert Xu 	struct sk_buff **fragp;
192927b437c8SHerbert Xu 	struct sk_buff *frag;
19301da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
19311da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
19321da177e4SLinus Torvalds 	int i;
193327b437c8SHerbert Xu 	int err;
193427b437c8SHerbert Xu 
193527b437c8SHerbert Xu 	if (skb_cloned(skb) &&
193627b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
193727b437c8SHerbert Xu 		return err;
19381da177e4SLinus Torvalds 
1939f4d26fb3SHerbert Xu 	i = 0;
1940f4d26fb3SHerbert Xu 	if (offset >= len)
1941f4d26fb3SHerbert Xu 		goto drop_pages;
1942f4d26fb3SHerbert Xu 
1943f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
19449e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
194527b437c8SHerbert Xu 
194627b437c8SHerbert Xu 		if (end < len) {
19471da177e4SLinus Torvalds 			offset = end;
194827b437c8SHerbert Xu 			continue;
19491da177e4SLinus Torvalds 		}
19501da177e4SLinus Torvalds 
19519e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
195227b437c8SHerbert Xu 
1953f4d26fb3SHerbert Xu drop_pages:
195427b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
195527b437c8SHerbert Xu 
195627b437c8SHerbert Xu 		for (; i < nfrags; i++)
1957ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
195827b437c8SHerbert Xu 
195921dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
196027b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1961f4d26fb3SHerbert Xu 		goto done;
196227b437c8SHerbert Xu 	}
196327b437c8SHerbert Xu 
196427b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
196527b437c8SHerbert Xu 	     fragp = &frag->next) {
196627b437c8SHerbert Xu 		int end = offset + frag->len;
196727b437c8SHerbert Xu 
196827b437c8SHerbert Xu 		if (skb_shared(frag)) {
196927b437c8SHerbert Xu 			struct sk_buff *nfrag;
197027b437c8SHerbert Xu 
197127b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
197227b437c8SHerbert Xu 			if (unlikely(!nfrag))
197327b437c8SHerbert Xu 				return -ENOMEM;
197427b437c8SHerbert Xu 
197527b437c8SHerbert Xu 			nfrag->next = frag->next;
197685bb2a60SEric Dumazet 			consume_skb(frag);
197727b437c8SHerbert Xu 			frag = nfrag;
197827b437c8SHerbert Xu 			*fragp = frag;
197927b437c8SHerbert Xu 		}
198027b437c8SHerbert Xu 
198127b437c8SHerbert Xu 		if (end < len) {
198227b437c8SHerbert Xu 			offset = end;
198327b437c8SHerbert Xu 			continue;
198427b437c8SHerbert Xu 		}
198527b437c8SHerbert Xu 
198627b437c8SHerbert Xu 		if (end > len &&
198727b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
198827b437c8SHerbert Xu 			return err;
198927b437c8SHerbert Xu 
199027b437c8SHerbert Xu 		if (frag->next)
199127b437c8SHerbert Xu 			skb_drop_list(&frag->next);
199227b437c8SHerbert Xu 		break;
199327b437c8SHerbert Xu 	}
199427b437c8SHerbert Xu 
1995f4d26fb3SHerbert Xu done:
199627b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
19971da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
19981da177e4SLinus Torvalds 		skb->len       = len;
19991da177e4SLinus Torvalds 	} else {
20001da177e4SLinus Torvalds 		skb->len       = len;
20011da177e4SLinus Torvalds 		skb->data_len  = 0;
200227a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
20031da177e4SLinus Torvalds 	}
20041da177e4SLinus Torvalds 
2005c21b48ccSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
2006c21b48ccSEric Dumazet 		skb_condense(skb);
20071da177e4SLinus Torvalds 	return 0;
20081da177e4SLinus Torvalds }
2009b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
20101da177e4SLinus Torvalds 
201188078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly
201288078d98SEric Dumazet  */
201388078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
201488078d98SEric Dumazet {
201588078d98SEric Dumazet 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
201688078d98SEric Dumazet 		int delta = skb->len - len;
201788078d98SEric Dumazet 
2018d55bef50SDimitris Michailidis 		skb->csum = csum_block_sub(skb->csum,
2019d55bef50SDimitris Michailidis 					   skb_checksum(skb, len, delta, 0),
2020d55bef50SDimitris Michailidis 					   len);
202188078d98SEric Dumazet 	}
202288078d98SEric Dumazet 	return __pskb_trim(skb, len);
202388078d98SEric Dumazet }
202488078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow);
202588078d98SEric Dumazet 
20261da177e4SLinus Torvalds /**
20271da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
20281da177e4SLinus Torvalds  *	@skb: buffer to reallocate
20291da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
20301da177e4SLinus Torvalds  *
20311da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
20321da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
20331da177e4SLinus Torvalds  *	data from fragmented part.
20341da177e4SLinus Torvalds  *
20351da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
20361da177e4SLinus Torvalds  *
20371da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
20381da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
20391da177e4SLinus Torvalds  *
20401da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
20411da177e4SLinus Torvalds  *	reloaded after call to this function.
20421da177e4SLinus Torvalds  */
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
20451da177e4SLinus Torvalds  * when it is necessary.
20461da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
20471da177e4SLinus Torvalds  * 2. It may change skb pointers.
20481da177e4SLinus Torvalds  *
20491da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
20501da177e4SLinus Torvalds  */
2051af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
20541da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
20551da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
20561da177e4SLinus Torvalds 	 */
20574305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
20601da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
20611da177e4SLinus Torvalds 				     GFP_ATOMIC))
20621da177e4SLinus Torvalds 			return NULL;
20631da177e4SLinus Torvalds 	}
20641da177e4SLinus Torvalds 
20659f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
20669f77fad3STim Hansen 			     skb_tail_pointer(skb), delta));
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
20691da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
20701da177e4SLinus Torvalds 	 */
207121dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
20721da177e4SLinus Torvalds 		goto pull_pages;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
20751da177e4SLinus Torvalds 	eat = delta;
20761da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20779e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
20789e903e08SEric Dumazet 
20799e903e08SEric Dumazet 		if (size >= eat)
20801da177e4SLinus Torvalds 			goto pull_pages;
20819e903e08SEric Dumazet 		eat -= size;
20821da177e4SLinus Torvalds 	}
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
208509001b03SWenhua Shi 	 * Certainly, it is possible to add an offset to skb data,
20861da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
20871da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
20881da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
20891da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
20901da177e4SLinus Torvalds 	 */
20911da177e4SLinus Torvalds 	if (eat) {
20921da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
20931da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
20941da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 		do {
20971da177e4SLinus Torvalds 			if (list->len <= eat) {
20981da177e4SLinus Torvalds 				/* Eaten as whole. */
20991da177e4SLinus Torvalds 				eat -= list->len;
21001da177e4SLinus Torvalds 				list = list->next;
21011da177e4SLinus Torvalds 				insp = list;
21021da177e4SLinus Torvalds 			} else {
21031da177e4SLinus Torvalds 				/* Eaten partially. */
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 				if (skb_shared(list)) {
21061da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
21071da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
21081da177e4SLinus Torvalds 					if (!clone)
21091da177e4SLinus Torvalds 						return NULL;
21101da177e4SLinus Torvalds 					insp = list->next;
21111da177e4SLinus Torvalds 					list = clone;
21121da177e4SLinus Torvalds 				} else {
21131da177e4SLinus Torvalds 					/* This may be pulled without
21141da177e4SLinus Torvalds 					 * problems. */
21151da177e4SLinus Torvalds 					insp = list;
21161da177e4SLinus Torvalds 				}
21171da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
21181da177e4SLinus Torvalds 					kfree_skb(clone);
21191da177e4SLinus Torvalds 					return NULL;
21201da177e4SLinus Torvalds 				}
21211da177e4SLinus Torvalds 				break;
21221da177e4SLinus Torvalds 			}
21231da177e4SLinus Torvalds 		} while (eat);
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 		/* Free pulled out fragments. */
21261da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
21271da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
21281da177e4SLinus Torvalds 			kfree_skb(list);
21291da177e4SLinus Torvalds 		}
21301da177e4SLinus Torvalds 		/* And insert new clone at head. */
21311da177e4SLinus Torvalds 		if (clone) {
21321da177e4SLinus Torvalds 			clone->next = list;
21331da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
21341da177e4SLinus Torvalds 		}
21351da177e4SLinus Torvalds 	}
21361da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds pull_pages:
21391da177e4SLinus Torvalds 	eat = delta;
21401da177e4SLinus Torvalds 	k = 0;
21411da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
21429e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
21439e903e08SEric Dumazet 
21449e903e08SEric Dumazet 		if (size <= eat) {
2145ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
21469e903e08SEric Dumazet 			eat -= size;
21471da177e4SLinus Torvalds 		} else {
2148b54c9d5bSJonathan Lemon 			skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2149b54c9d5bSJonathan Lemon 
2150b54c9d5bSJonathan Lemon 			*frag = skb_shinfo(skb)->frags[i];
21511da177e4SLinus Torvalds 			if (eat) {
2152b54c9d5bSJonathan Lemon 				skb_frag_off_add(frag, eat);
2153b54c9d5bSJonathan Lemon 				skb_frag_size_sub(frag, eat);
21543ccc6c6fSlinzhang 				if (!i)
21553ccc6c6fSlinzhang 					goto end;
21561da177e4SLinus Torvalds 				eat = 0;
21571da177e4SLinus Torvalds 			}
21581da177e4SLinus Torvalds 			k++;
21591da177e4SLinus Torvalds 		}
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
21621da177e4SLinus Torvalds 
21633ccc6c6fSlinzhang end:
21641da177e4SLinus Torvalds 	skb->tail     += delta;
21651da177e4SLinus Torvalds 	skb->data_len -= delta;
21661da177e4SLinus Torvalds 
21671f8b977aSWillem de Bruijn 	if (!skb->data_len)
21681f8b977aSWillem de Bruijn 		skb_zcopy_clear(skb, false);
21691f8b977aSWillem de Bruijn 
217027a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
21711da177e4SLinus Torvalds }
2172b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
21731da177e4SLinus Torvalds 
217422019b17SEric Dumazet /**
217522019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
217622019b17SEric Dumazet  *	@skb: source skb
217722019b17SEric Dumazet  *	@offset: offset in source
217822019b17SEric Dumazet  *	@to: destination buffer
217922019b17SEric Dumazet  *	@len: number of bytes to copy
218022019b17SEric Dumazet  *
218122019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
218222019b17SEric Dumazet  *	destination buffer.
218322019b17SEric Dumazet  *
218422019b17SEric Dumazet  *	CAUTION ! :
218522019b17SEric Dumazet  *		If its prototype is ever changed,
218622019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
218722019b17SEric Dumazet  *		since it is called from BPF assembly code.
218822019b17SEric Dumazet  */
21891da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
21901da177e4SLinus Torvalds {
21911a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2192fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2193fbb398a8SDavid S. Miller 	int i, copy;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
21961da177e4SLinus Torvalds 		goto fault;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	/* Copy header. */
21991a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
22001da177e4SLinus Torvalds 		if (copy > len)
22011da177e4SLinus Torvalds 			copy = len;
2202d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
22031da177e4SLinus Torvalds 		if ((len -= copy) == 0)
22041da177e4SLinus Torvalds 			return 0;
22051da177e4SLinus Torvalds 		offset += copy;
22061da177e4SLinus Torvalds 		to     += copy;
22071da177e4SLinus Torvalds 	}
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
22101a028e50SDavid S. Miller 		int end;
221151c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
22121da177e4SLinus Torvalds 
2213547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
22141a028e50SDavid S. Miller 
221551c56b00SEric Dumazet 		end = start + skb_frag_size(f);
22161da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2217c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2218c613c209SWillem de Bruijn 			struct page *p;
22191da177e4SLinus Torvalds 			u8 *vaddr;
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 			if (copy > len)
22221da177e4SLinus Torvalds 				copy = len;
22231da177e4SLinus Torvalds 
2224c613c209SWillem de Bruijn 			skb_frag_foreach_page(f,
2225b54c9d5bSJonathan Lemon 					      skb_frag_off(f) + offset - start,
2226c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2227c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2228c613c209SWillem de Bruijn 				memcpy(to + copied, vaddr + p_off, p_len);
222951c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2230c613c209SWillem de Bruijn 			}
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22331da177e4SLinus Torvalds 				return 0;
22341da177e4SLinus Torvalds 			offset += copy;
22351da177e4SLinus Torvalds 			to     += copy;
22361da177e4SLinus Torvalds 		}
22371a028e50SDavid S. Miller 		start = end;
22381da177e4SLinus Torvalds 	}
22391da177e4SLinus Torvalds 
2240fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
22411a028e50SDavid S. Miller 		int end;
22421da177e4SLinus Torvalds 
2243547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
22441a028e50SDavid S. Miller 
2245fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
22461da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
22471da177e4SLinus Torvalds 			if (copy > len)
22481da177e4SLinus Torvalds 				copy = len;
2249fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
22501da177e4SLinus Torvalds 				goto fault;
22511da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22521da177e4SLinus Torvalds 				return 0;
22531da177e4SLinus Torvalds 			offset += copy;
22541da177e4SLinus Torvalds 			to     += copy;
22551da177e4SLinus Torvalds 		}
22561a028e50SDavid S. Miller 		start = end;
22571da177e4SLinus Torvalds 	}
2258a6686f2fSShirley Ma 
22591da177e4SLinus Torvalds 	if (!len)
22601da177e4SLinus Torvalds 		return 0;
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds fault:
22631da177e4SLinus Torvalds 	return -EFAULT;
22641da177e4SLinus Torvalds }
2265b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
22661da177e4SLinus Torvalds 
22679c55e01cSJens Axboe /*
22689c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
22699c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
22709c55e01cSJens Axboe  */
22719c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
22729c55e01cSJens Axboe {
22738b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
22748b9d3728SJarek Poplawski }
22759c55e01cSJens Axboe 
2276a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
22774fb66994SJarek Poplawski 				   unsigned int *offset,
227818aafc62SEric Dumazet 				   struct sock *sk)
22798b9d3728SJarek Poplawski {
22805640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
22818b9d3728SJarek Poplawski 
22825640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
22838b9d3728SJarek Poplawski 		return NULL;
22844fb66994SJarek Poplawski 
22855640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
22864fb66994SJarek Poplawski 
22875640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
22885640f768SEric Dumazet 	       page_address(page) + *offset, *len);
22895640f768SEric Dumazet 	*offset = pfrag->offset;
22905640f768SEric Dumazet 	pfrag->offset += *len;
22914fb66994SJarek Poplawski 
22925640f768SEric Dumazet 	return pfrag->page;
22939c55e01cSJens Axboe }
22949c55e01cSJens Axboe 
229541c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
229641c73a0dSEric Dumazet 			     struct page *page,
229741c73a0dSEric Dumazet 			     unsigned int offset)
229841c73a0dSEric Dumazet {
229941c73a0dSEric Dumazet 	return	spd->nr_pages &&
230041c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
230141c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
230241c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
230341c73a0dSEric Dumazet }
230441c73a0dSEric Dumazet 
23059c55e01cSJens Axboe /*
23069c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
23079c55e01cSJens Axboe  */
2308a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
230935f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
23104fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
231118aafc62SEric Dumazet 			  bool linear,
23127a67e56fSJarek Poplawski 			  struct sock *sk)
23139c55e01cSJens Axboe {
231441c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2315a108d5f3SDavid S. Miller 		return true;
23169c55e01cSJens Axboe 
23178b9d3728SJarek Poplawski 	if (linear) {
231818aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
23198b9d3728SJarek Poplawski 		if (!page)
2320a108d5f3SDavid S. Miller 			return true;
232141c73a0dSEric Dumazet 	}
232241c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
232341c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
2324a108d5f3SDavid S. Miller 		return false;
232541c73a0dSEric Dumazet 	}
23268b9d3728SJarek Poplawski 	get_page(page);
23279c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
23284fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
23299c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
23309c55e01cSJens Axboe 	spd->nr_pages++;
23318b9d3728SJarek Poplawski 
2332a108d5f3SDavid S. Miller 	return false;
23339c55e01cSJens Axboe }
23349c55e01cSJens Axboe 
2335a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
23362870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
233718aafc62SEric Dumazet 			     unsigned int *len,
2338d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
233935f3d14dSJens Axboe 			     struct sock *sk,
234035f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
23419c55e01cSJens Axboe {
23422870c43dSOctavian Purdila 	if (!*len)
2343a108d5f3SDavid S. Miller 		return true;
23449c55e01cSJens Axboe 
23452870c43dSOctavian Purdila 	/* skip this segment if already processed */
23462870c43dSOctavian Purdila 	if (*off >= plen) {
23472870c43dSOctavian Purdila 		*off -= plen;
2348a108d5f3SDavid S. Miller 		return false;
23492870c43dSOctavian Purdila 	}
23502870c43dSOctavian Purdila 
23512870c43dSOctavian Purdila 	/* ignore any bits we already processed */
23529ca1b22dSEric Dumazet 	poff += *off;
23539ca1b22dSEric Dumazet 	plen -= *off;
23542870c43dSOctavian Purdila 	*off = 0;
23552870c43dSOctavian Purdila 
235618aafc62SEric Dumazet 	do {
235718aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
23582870c43dSOctavian Purdila 
235918aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
236018aafc62SEric Dumazet 				  linear, sk))
2361a108d5f3SDavid S. Miller 			return true;
236218aafc62SEric Dumazet 		poff += flen;
236318aafc62SEric Dumazet 		plen -= flen;
23642870c43dSOctavian Purdila 		*len -= flen;
236518aafc62SEric Dumazet 	} while (*len && plen);
23662870c43dSOctavian Purdila 
2367a108d5f3SDavid S. Miller 	return false;
2368db43a282SOctavian Purdila }
23699c55e01cSJens Axboe 
23709c55e01cSJens Axboe /*
2371a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
23722870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
23739c55e01cSJens Axboe  */
2374a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
237535f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
237635f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
23772870c43dSOctavian Purdila {
23782870c43dSOctavian Purdila 	int seg;
2379fa9835e5STom Herbert 	struct sk_buff *iter;
23809c55e01cSJens Axboe 
23811d0c0b32SEric Dumazet 	/* map the linear part :
23822996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
23832996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
23842996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
23859c55e01cSJens Axboe 	 */
23862870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
23872870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
23882870c43dSOctavian Purdila 			     skb_headlen(skb),
238918aafc62SEric Dumazet 			     offset, len, spd,
23903a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
23911d0c0b32SEric Dumazet 			     sk, pipe))
2392a108d5f3SDavid S. Miller 		return true;
23939c55e01cSJens Axboe 
23949c55e01cSJens Axboe 	/*
23959c55e01cSJens Axboe 	 * then map the fragments
23969c55e01cSJens Axboe 	 */
23979c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
23989c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
23999c55e01cSJens Axboe 
2400ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
2401b54c9d5bSJonathan Lemon 				     skb_frag_off(f), skb_frag_size(f),
240218aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
2403a108d5f3SDavid S. Miller 			return true;
24049c55e01cSJens Axboe 	}
24059c55e01cSJens Axboe 
2406fa9835e5STom Herbert 	skb_walk_frags(skb, iter) {
2407fa9835e5STom Herbert 		if (*offset >= iter->len) {
2408fa9835e5STom Herbert 			*offset -= iter->len;
2409fa9835e5STom Herbert 			continue;
2410fa9835e5STom Herbert 		}
2411fa9835e5STom Herbert 		/* __skb_splice_bits() only fails if the output has no room
2412fa9835e5STom Herbert 		 * left, so no point in going over the frag_list for the error
2413fa9835e5STom Herbert 		 * case.
2414fa9835e5STom Herbert 		 */
2415fa9835e5STom Herbert 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2416fa9835e5STom Herbert 			return true;
2417fa9835e5STom Herbert 	}
2418fa9835e5STom Herbert 
2419a108d5f3SDavid S. Miller 	return false;
24209c55e01cSJens Axboe }
24219c55e01cSJens Axboe 
24229c55e01cSJens Axboe /*
24239c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
2424fa9835e5STom Herbert  * the fragments, and the frag list.
24259c55e01cSJens Axboe  */
2426a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
24279c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
242825869262SAl Viro 		    unsigned int flags)
24299c55e01cSJens Axboe {
243041c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
243141c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
24329c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
24339c55e01cSJens Axboe 		.pages = pages,
24349c55e01cSJens Axboe 		.partial = partial,
2435047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
243628a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
24379c55e01cSJens Axboe 		.spd_release = sock_spd_release,
24389c55e01cSJens Axboe 	};
243935f3d14dSJens Axboe 	int ret = 0;
244035f3d14dSJens Axboe 
2441fa9835e5STom Herbert 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
24429c55e01cSJens Axboe 
2443a60e3cc7SHannes Frederic Sowa 	if (spd.nr_pages)
244425869262SAl Viro 		ret = splice_to_pipe(pipe, &spd);
24459c55e01cSJens Axboe 
244635f3d14dSJens Axboe 	return ret;
24479c55e01cSJens Axboe }
24482b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
24499c55e01cSJens Axboe 
245020bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */
245120bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
245220bf50deSTom Herbert 			 int len)
245320bf50deSTom Herbert {
245420bf50deSTom Herbert 	unsigned int orig_len = len;
245520bf50deSTom Herbert 	struct sk_buff *head = skb;
245620bf50deSTom Herbert 	unsigned short fragidx;
245720bf50deSTom Herbert 	int slen, ret;
245820bf50deSTom Herbert 
245920bf50deSTom Herbert do_frag_list:
246020bf50deSTom Herbert 
246120bf50deSTom Herbert 	/* Deal with head data */
246220bf50deSTom Herbert 	while (offset < skb_headlen(skb) && len) {
246320bf50deSTom Herbert 		struct kvec kv;
246420bf50deSTom Herbert 		struct msghdr msg;
246520bf50deSTom Herbert 
246620bf50deSTom Herbert 		slen = min_t(int, len, skb_headlen(skb) - offset);
246720bf50deSTom Herbert 		kv.iov_base = skb->data + offset;
2468db5980d8SJohn Fastabend 		kv.iov_len = slen;
246920bf50deSTom Herbert 		memset(&msg, 0, sizeof(msg));
2470bd95e678SJohn Fastabend 		msg.msg_flags = MSG_DONTWAIT;
247120bf50deSTom Herbert 
247220bf50deSTom Herbert 		ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
247320bf50deSTom Herbert 		if (ret <= 0)
247420bf50deSTom Herbert 			goto error;
247520bf50deSTom Herbert 
247620bf50deSTom Herbert 		offset += ret;
247720bf50deSTom Herbert 		len -= ret;
247820bf50deSTom Herbert 	}
247920bf50deSTom Herbert 
248020bf50deSTom Herbert 	/* All the data was skb head? */
248120bf50deSTom Herbert 	if (!len)
248220bf50deSTom Herbert 		goto out;
248320bf50deSTom Herbert 
248420bf50deSTom Herbert 	/* Make offset relative to start of frags */
248520bf50deSTom Herbert 	offset -= skb_headlen(skb);
248620bf50deSTom Herbert 
248720bf50deSTom Herbert 	/* Find where we are in frag list */
248820bf50deSTom Herbert 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
248920bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
249020bf50deSTom Herbert 
2491d8e18a51SMatthew Wilcox (Oracle) 		if (offset < skb_frag_size(frag))
249220bf50deSTom Herbert 			break;
249320bf50deSTom Herbert 
2494d8e18a51SMatthew Wilcox (Oracle) 		offset -= skb_frag_size(frag);
249520bf50deSTom Herbert 	}
249620bf50deSTom Herbert 
249720bf50deSTom Herbert 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
249820bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
249920bf50deSTom Herbert 
2500d8e18a51SMatthew Wilcox (Oracle) 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
250120bf50deSTom Herbert 
250220bf50deSTom Herbert 		while (slen) {
2503d8e18a51SMatthew Wilcox (Oracle) 			ret = kernel_sendpage_locked(sk, skb_frag_page(frag),
2504b54c9d5bSJonathan Lemon 						     skb_frag_off(frag) + offset,
250520bf50deSTom Herbert 						     slen, MSG_DONTWAIT);
250620bf50deSTom Herbert 			if (ret <= 0)
250720bf50deSTom Herbert 				goto error;
250820bf50deSTom Herbert 
250920bf50deSTom Herbert 			len -= ret;
251020bf50deSTom Herbert 			offset += ret;
251120bf50deSTom Herbert 			slen -= ret;
251220bf50deSTom Herbert 		}
251320bf50deSTom Herbert 
251420bf50deSTom Herbert 		offset = 0;
251520bf50deSTom Herbert 	}
251620bf50deSTom Herbert 
251720bf50deSTom Herbert 	if (len) {
251820bf50deSTom Herbert 		/* Process any frag lists */
251920bf50deSTom Herbert 
252020bf50deSTom Herbert 		if (skb == head) {
252120bf50deSTom Herbert 			if (skb_has_frag_list(skb)) {
252220bf50deSTom Herbert 				skb = skb_shinfo(skb)->frag_list;
252320bf50deSTom Herbert 				goto do_frag_list;
252420bf50deSTom Herbert 			}
252520bf50deSTom Herbert 		} else if (skb->next) {
252620bf50deSTom Herbert 			skb = skb->next;
252720bf50deSTom Herbert 			goto do_frag_list;
252820bf50deSTom Herbert 		}
252920bf50deSTom Herbert 	}
253020bf50deSTom Herbert 
253120bf50deSTom Herbert out:
253220bf50deSTom Herbert 	return orig_len - len;
253320bf50deSTom Herbert 
253420bf50deSTom Herbert error:
253520bf50deSTom Herbert 	return orig_len == len ? ret : orig_len - len;
253620bf50deSTom Herbert }
253720bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked);
253820bf50deSTom Herbert 
2539357b40a1SHerbert Xu /**
2540357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
2541357b40a1SHerbert Xu  *	@skb: destination buffer
2542357b40a1SHerbert Xu  *	@offset: offset in destination
2543357b40a1SHerbert Xu  *	@from: source buffer
2544357b40a1SHerbert Xu  *	@len: number of bytes to copy
2545357b40a1SHerbert Xu  *
2546357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
2547357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
2548357b40a1SHerbert Xu  *	traversing fragment lists and such.
2549357b40a1SHerbert Xu  */
2550357b40a1SHerbert Xu 
25510c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2552357b40a1SHerbert Xu {
25531a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2554fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2555fbb398a8SDavid S. Miller 	int i, copy;
2556357b40a1SHerbert Xu 
2557357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
2558357b40a1SHerbert Xu 		goto fault;
2559357b40a1SHerbert Xu 
25601a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
2561357b40a1SHerbert Xu 		if (copy > len)
2562357b40a1SHerbert Xu 			copy = len;
256327d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2564357b40a1SHerbert Xu 		if ((len -= copy) == 0)
2565357b40a1SHerbert Xu 			return 0;
2566357b40a1SHerbert Xu 		offset += copy;
2567357b40a1SHerbert Xu 		from += copy;
2568357b40a1SHerbert Xu 	}
2569357b40a1SHerbert Xu 
2570357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2571357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
25721a028e50SDavid S. Miller 		int end;
2573357b40a1SHerbert Xu 
2574547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
25751a028e50SDavid S. Miller 
25769e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
2577357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2578c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2579c613c209SWillem de Bruijn 			struct page *p;
2580357b40a1SHerbert Xu 			u8 *vaddr;
2581357b40a1SHerbert Xu 
2582357b40a1SHerbert Xu 			if (copy > len)
2583357b40a1SHerbert Xu 				copy = len;
2584357b40a1SHerbert Xu 
2585c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2586b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2587c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2588c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2589c613c209SWillem de Bruijn 				memcpy(vaddr + p_off, from + copied, p_len);
259051c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2591c613c209SWillem de Bruijn 			}
2592357b40a1SHerbert Xu 
2593357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2594357b40a1SHerbert Xu 				return 0;
2595357b40a1SHerbert Xu 			offset += copy;
2596357b40a1SHerbert Xu 			from += copy;
2597357b40a1SHerbert Xu 		}
25981a028e50SDavid S. Miller 		start = end;
2599357b40a1SHerbert Xu 	}
2600357b40a1SHerbert Xu 
2601fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
26021a028e50SDavid S. Miller 		int end;
2603357b40a1SHerbert Xu 
2604547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26051a028e50SDavid S. Miller 
2606fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2607357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2608357b40a1SHerbert Xu 			if (copy > len)
2609357b40a1SHerbert Xu 				copy = len;
2610fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
26111a028e50SDavid S. Miller 					   from, copy))
2612357b40a1SHerbert Xu 				goto fault;
2613357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2614357b40a1SHerbert Xu 				return 0;
2615357b40a1SHerbert Xu 			offset += copy;
2616357b40a1SHerbert Xu 			from += copy;
2617357b40a1SHerbert Xu 		}
26181a028e50SDavid S. Miller 		start = end;
2619357b40a1SHerbert Xu 	}
2620357b40a1SHerbert Xu 	if (!len)
2621357b40a1SHerbert Xu 		return 0;
2622357b40a1SHerbert Xu 
2623357b40a1SHerbert Xu fault:
2624357b40a1SHerbert Xu 	return -EFAULT;
2625357b40a1SHerbert Xu }
2626357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
2627357b40a1SHerbert Xu 
26281da177e4SLinus Torvalds /* Checksum skb data. */
26292817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
26302817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
26311da177e4SLinus Torvalds {
26321a028e50SDavid S. Miller 	int start = skb_headlen(skb);
26331a028e50SDavid S. Miller 	int i, copy = start - offset;
2634fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
26351da177e4SLinus Torvalds 	int pos = 0;
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	/* Checksum header. */
26381da177e4SLinus Torvalds 	if (copy > 0) {
26391da177e4SLinus Torvalds 		if (copy > len)
26401da177e4SLinus Torvalds 			copy = len;
26412544af03SMatteo Croce 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
26422544af03SMatteo Croce 				       skb->data + offset, copy, csum);
26431da177e4SLinus Torvalds 		if ((len -= copy) == 0)
26441da177e4SLinus Torvalds 			return csum;
26451da177e4SLinus Torvalds 		offset += copy;
26461da177e4SLinus Torvalds 		pos	= copy;
26471da177e4SLinus Torvalds 	}
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26501a028e50SDavid S. Miller 		int end;
265151c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
26521da177e4SLinus Torvalds 
2653547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26541a028e50SDavid S. Miller 
265551c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
26561da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2657c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2658c613c209SWillem de Bruijn 			struct page *p;
265944bb9363SAl Viro 			__wsum csum2;
26601da177e4SLinus Torvalds 			u8 *vaddr;
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 			if (copy > len)
26631da177e4SLinus Torvalds 				copy = len;
2664c613c209SWillem de Bruijn 
2665c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2666b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2667c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2668c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
26692544af03SMatteo Croce 				csum2 = INDIRECT_CALL_1(ops->update,
26702544af03SMatteo Croce 							csum_partial_ext,
26712544af03SMatteo Croce 							vaddr + p_off, p_len, 0);
267251c56b00SEric Dumazet 				kunmap_atomic(vaddr);
26732544af03SMatteo Croce 				csum = INDIRECT_CALL_1(ops->combine,
26742544af03SMatteo Croce 						       csum_block_add_ext, csum,
26752544af03SMatteo Croce 						       csum2, pos, p_len);
2676c613c209SWillem de Bruijn 				pos += p_len;
2677c613c209SWillem de Bruijn 			}
2678c613c209SWillem de Bruijn 
26791da177e4SLinus Torvalds 			if (!(len -= copy))
26801da177e4SLinus Torvalds 				return csum;
26811da177e4SLinus Torvalds 			offset += copy;
26821da177e4SLinus Torvalds 		}
26831a028e50SDavid S. Miller 		start = end;
26841da177e4SLinus Torvalds 	}
26851da177e4SLinus Torvalds 
2686fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
26871a028e50SDavid S. Miller 		int end;
26881da177e4SLinus Torvalds 
2689547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26901a028e50SDavid S. Miller 
2691fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
26921da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
26935f92a738SAl Viro 			__wsum csum2;
26941da177e4SLinus Torvalds 			if (copy > len)
26951da177e4SLinus Torvalds 				copy = len;
26962817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
26972817a336SDaniel Borkmann 					       copy, 0, ops);
26982544af03SMatteo Croce 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
26992544af03SMatteo Croce 					       csum, csum2, pos, copy);
27001da177e4SLinus Torvalds 			if ((len -= copy) == 0)
27011da177e4SLinus Torvalds 				return csum;
27021da177e4SLinus Torvalds 			offset += copy;
27031da177e4SLinus Torvalds 			pos    += copy;
27041da177e4SLinus Torvalds 		}
27051a028e50SDavid S. Miller 		start = end;
27061da177e4SLinus Torvalds 	}
270709a62660SKris Katterjohn 	BUG_ON(len);
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds 	return csum;
27101da177e4SLinus Torvalds }
27112817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
27122817a336SDaniel Borkmann 
27132817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
27142817a336SDaniel Borkmann 		    int len, __wsum csum)
27152817a336SDaniel Borkmann {
27162817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2717cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
27182817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
27192817a336SDaniel Borkmann 	};
27202817a336SDaniel Borkmann 
27212817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
27222817a336SDaniel Borkmann }
2723b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds /* Both of above in one bottle. */
27261da177e4SLinus Torvalds 
272781d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
272881d77662SAl Viro 				    u8 *to, int len, __wsum csum)
27291da177e4SLinus Torvalds {
27301a028e50SDavid S. Miller 	int start = skb_headlen(skb);
27311a028e50SDavid S. Miller 	int i, copy = start - offset;
2732fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
27331da177e4SLinus Torvalds 	int pos = 0;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 	/* Copy header. */
27361da177e4SLinus Torvalds 	if (copy > 0) {
27371da177e4SLinus Torvalds 		if (copy > len)
27381da177e4SLinus Torvalds 			copy = len;
27391da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
27401da177e4SLinus Torvalds 						 copy, csum);
27411da177e4SLinus Torvalds 		if ((len -= copy) == 0)
27421da177e4SLinus Torvalds 			return csum;
27431da177e4SLinus Torvalds 		offset += copy;
27441da177e4SLinus Torvalds 		to     += copy;
27451da177e4SLinus Torvalds 		pos	= copy;
27461da177e4SLinus Torvalds 	}
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27491a028e50SDavid S. Miller 		int end;
27501da177e4SLinus Torvalds 
2751547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
27521a028e50SDavid S. Miller 
27539e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
27541da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2755c613c209SWillem de Bruijn 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2756c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2757c613c209SWillem de Bruijn 			struct page *p;
27585084205fSAl Viro 			__wsum csum2;
27591da177e4SLinus Torvalds 			u8 *vaddr;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 			if (copy > len)
27621da177e4SLinus Torvalds 				copy = len;
2763c613c209SWillem de Bruijn 
2764c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2765b54c9d5bSJonathan Lemon 					      skb_frag_off(frag) + offset - start,
2766c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2767c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2768c613c209SWillem de Bruijn 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2769c613c209SWillem de Bruijn 								  to + copied,
2770c613c209SWillem de Bruijn 								  p_len, 0);
277151c56b00SEric Dumazet 				kunmap_atomic(vaddr);
27721da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
2773c613c209SWillem de Bruijn 				pos += p_len;
2774c613c209SWillem de Bruijn 			}
2775c613c209SWillem de Bruijn 
27761da177e4SLinus Torvalds 			if (!(len -= copy))
27771da177e4SLinus Torvalds 				return csum;
27781da177e4SLinus Torvalds 			offset += copy;
27791da177e4SLinus Torvalds 			to     += copy;
27801da177e4SLinus Torvalds 		}
27811a028e50SDavid S. Miller 		start = end;
27821da177e4SLinus Torvalds 	}
27831da177e4SLinus Torvalds 
2784fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
278581d77662SAl Viro 		__wsum csum2;
27861a028e50SDavid S. Miller 		int end;
27871da177e4SLinus Torvalds 
2788547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
27891a028e50SDavid S. Miller 
2790fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
27911da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
27921da177e4SLinus Torvalds 			if (copy > len)
27931da177e4SLinus Torvalds 				copy = len;
2794fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
27951a028e50SDavid S. Miller 						       offset - start,
27961da177e4SLinus Torvalds 						       to, copy, 0);
27971da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
27981da177e4SLinus Torvalds 			if ((len -= copy) == 0)
27991da177e4SLinus Torvalds 				return csum;
28001da177e4SLinus Torvalds 			offset += copy;
28011da177e4SLinus Torvalds 			to     += copy;
28021da177e4SLinus Torvalds 			pos    += copy;
28031da177e4SLinus Torvalds 		}
28041a028e50SDavid S. Miller 		start = end;
28051da177e4SLinus Torvalds 	}
280609a62660SKris Katterjohn 	BUG_ON(len);
28071da177e4SLinus Torvalds 	return csum;
28081da177e4SLinus Torvalds }
2809b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
28101da177e4SLinus Torvalds 
281149f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
281249f8e832SCong Wang {
281349f8e832SCong Wang 	__sum16 sum;
281449f8e832SCong Wang 
281549f8e832SCong Wang 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
281614641931SCong Wang 	/* See comments in __skb_checksum_complete(). */
281749f8e832SCong Wang 	if (likely(!sum)) {
281849f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
281949f8e832SCong Wang 		    !skb->csum_complete_sw)
28207fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
282149f8e832SCong Wang 	}
282249f8e832SCong Wang 	if (!skb_shared(skb))
282349f8e832SCong Wang 		skb->csum_valid = !sum;
282449f8e832SCong Wang 	return sum;
282549f8e832SCong Wang }
282649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head);
282749f8e832SCong Wang 
282814641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum,
282914641931SCong Wang  * which has been changed from the hardware checksum, for example, by
283014641931SCong Wang  * __skb_checksum_validate_complete(). And, the original skb->csum must
283114641931SCong Wang  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
283214641931SCong Wang  *
283314641931SCong Wang  * It returns non-zero if the recomputed checksum is still invalid, otherwise
283414641931SCong Wang  * zero. The new checksum is stored back into skb->csum unless the skb is
283514641931SCong Wang  * shared.
283614641931SCong Wang  */
283749f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb)
283849f8e832SCong Wang {
283949f8e832SCong Wang 	__wsum csum;
284049f8e832SCong Wang 	__sum16 sum;
284149f8e832SCong Wang 
284249f8e832SCong Wang 	csum = skb_checksum(skb, 0, skb->len, 0);
284349f8e832SCong Wang 
284449f8e832SCong Wang 	sum = csum_fold(csum_add(skb->csum, csum));
284514641931SCong Wang 	/* This check is inverted, because we already knew the hardware
284614641931SCong Wang 	 * checksum is invalid before calling this function. So, if the
284714641931SCong Wang 	 * re-computed checksum is valid instead, then we have a mismatch
284814641931SCong Wang 	 * between the original skb->csum and skb_checksum(). This means either
284914641931SCong Wang 	 * the original hardware checksum is incorrect or we screw up skb->csum
285014641931SCong Wang 	 * when moving skb->data around.
285114641931SCong Wang 	 */
285249f8e832SCong Wang 	if (likely(!sum)) {
285349f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
285449f8e832SCong Wang 		    !skb->csum_complete_sw)
28557fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
285649f8e832SCong Wang 	}
285749f8e832SCong Wang 
285849f8e832SCong Wang 	if (!skb_shared(skb)) {
285949f8e832SCong Wang 		/* Save full packet checksum */
286049f8e832SCong Wang 		skb->csum = csum;
286149f8e832SCong Wang 		skb->ip_summed = CHECKSUM_COMPLETE;
286249f8e832SCong Wang 		skb->csum_complete_sw = 1;
286349f8e832SCong Wang 		skb->csum_valid = !sum;
286449f8e832SCong Wang 	}
286549f8e832SCong Wang 
286649f8e832SCong Wang 	return sum;
286749f8e832SCong Wang }
286849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete);
286949f8e832SCong Wang 
28709617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
28719617813dSDavide Caratti {
28729617813dSDavide Caratti 	net_warn_ratelimited(
28739617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
28749617813dSDavide Caratti 		__func__);
28759617813dSDavide Caratti 	return 0;
28769617813dSDavide Caratti }
28779617813dSDavide Caratti 
28789617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
28799617813dSDavide Caratti 				       int offset, int len)
28809617813dSDavide Caratti {
28819617813dSDavide Caratti 	net_warn_ratelimited(
28829617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
28839617813dSDavide Caratti 		__func__);
28849617813dSDavide Caratti 	return 0;
28859617813dSDavide Caratti }
28869617813dSDavide Caratti 
28879617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = {
28889617813dSDavide Caratti 	.update  = warn_crc32c_csum_update,
28899617813dSDavide Caratti 	.combine = warn_crc32c_csum_combine,
28909617813dSDavide Caratti };
28919617813dSDavide Caratti 
28929617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
28939617813dSDavide Caratti 	&default_crc32c_ops;
28949617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub);
28959617813dSDavide Caratti 
2896af2806f8SThomas Graf  /**
2897af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2898af2806f8SThomas Graf  *	@from: source buffer
2899af2806f8SThomas Graf  *
2900af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2901af2806f8SThomas Graf  *	into skb_zerocopy().
2902af2806f8SThomas Graf  */
2903af2806f8SThomas Graf unsigned int
2904af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2905af2806f8SThomas Graf {
2906af2806f8SThomas Graf 	unsigned int hlen = 0;
2907af2806f8SThomas Graf 
2908af2806f8SThomas Graf 	if (!from->head_frag ||
2909af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2910af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2911af2806f8SThomas Graf 		hlen = skb_headlen(from);
2912af2806f8SThomas Graf 
2913af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2914af2806f8SThomas Graf 		hlen = from->len;
2915af2806f8SThomas Graf 
2916af2806f8SThomas Graf 	return hlen;
2917af2806f8SThomas Graf }
2918af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2919af2806f8SThomas Graf 
2920af2806f8SThomas Graf /**
2921af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2922af2806f8SThomas Graf  *	@to: destination buffer
29237fceb4deSMasanari Iida  *	@from: source buffer
2924af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2925af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2926af2806f8SThomas Graf  *
2927af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2928af2806f8SThomas Graf  *	to the frags in the source buffer.
2929af2806f8SThomas Graf  *
2930af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2931af2806f8SThomas Graf  *	headroom in the `to` buffer.
293236d5fe6aSZoltan Kiss  *
293336d5fe6aSZoltan Kiss  *	Return value:
293436d5fe6aSZoltan Kiss  *	0: everything is OK
293536d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
293636d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2937af2806f8SThomas Graf  */
293836d5fe6aSZoltan Kiss int
293936d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2940af2806f8SThomas Graf {
2941af2806f8SThomas Graf 	int i, j = 0;
2942af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
294336d5fe6aSZoltan Kiss 	int ret;
2944af2806f8SThomas Graf 	struct page *page;
2945af2806f8SThomas Graf 	unsigned int offset;
2946af2806f8SThomas Graf 
2947af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2948af2806f8SThomas Graf 
2949af2806f8SThomas Graf 	/* dont bother with small payloads */
295036d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
295136d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2952af2806f8SThomas Graf 
2953af2806f8SThomas Graf 	if (hlen) {
295436d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
295536d5fe6aSZoltan Kiss 		if (unlikely(ret))
295636d5fe6aSZoltan Kiss 			return ret;
2957af2806f8SThomas Graf 		len -= hlen;
2958af2806f8SThomas Graf 	} else {
2959af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2960af2806f8SThomas Graf 		if (plen) {
2961af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2962af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2963af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2964af2806f8SThomas Graf 			get_page(page);
2965af2806f8SThomas Graf 			j = 1;
2966af2806f8SThomas Graf 			len -= plen;
2967af2806f8SThomas Graf 		}
2968af2806f8SThomas Graf 	}
2969af2806f8SThomas Graf 
2970af2806f8SThomas Graf 	to->truesize += len + plen;
2971af2806f8SThomas Graf 	to->len += len + plen;
2972af2806f8SThomas Graf 	to->data_len += len + plen;
2973af2806f8SThomas Graf 
297436d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
297536d5fe6aSZoltan Kiss 		skb_tx_error(from);
297636d5fe6aSZoltan Kiss 		return -ENOMEM;
297736d5fe6aSZoltan Kiss 	}
29781f8b977aSWillem de Bruijn 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
297936d5fe6aSZoltan Kiss 
2980af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2981d8e18a51SMatthew Wilcox (Oracle) 		int size;
2982d8e18a51SMatthew Wilcox (Oracle) 
2983af2806f8SThomas Graf 		if (!len)
2984af2806f8SThomas Graf 			break;
2985af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2986d8e18a51SMatthew Wilcox (Oracle) 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
2987d8e18a51SMatthew Wilcox (Oracle) 					len);
2988d8e18a51SMatthew Wilcox (Oracle) 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
2989d8e18a51SMatthew Wilcox (Oracle) 		len -= size;
2990af2806f8SThomas Graf 		skb_frag_ref(to, j);
2991af2806f8SThomas Graf 		j++;
2992af2806f8SThomas Graf 	}
2993af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
299436d5fe6aSZoltan Kiss 
299536d5fe6aSZoltan Kiss 	return 0;
2996af2806f8SThomas Graf }
2997af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2998af2806f8SThomas Graf 
29991da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
30001da177e4SLinus Torvalds {
3001d3bc23e7SAl Viro 	__wsum csum;
30021da177e4SLinus Torvalds 	long csstart;
30031da177e4SLinus Torvalds 
300484fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
300555508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
30061da177e4SLinus Torvalds 	else
30071da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
30081da177e4SLinus Torvalds 
300909a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
30101da177e4SLinus Torvalds 
3011d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 	csum = 0;
30141da177e4SLinus Torvalds 	if (csstart != skb->len)
30151da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
30161da177e4SLinus Torvalds 					      skb->len - csstart, 0);
30171da177e4SLinus Torvalds 
301884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3019ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
30201da177e4SLinus Torvalds 
3021d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
30221da177e4SLinus Torvalds 	}
30231da177e4SLinus Torvalds }
3024b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds /**
30271da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
30281da177e4SLinus Torvalds  *	@list: list to dequeue from
30291da177e4SLinus Torvalds  *
30301da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
30311da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
30321da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
30331da177e4SLinus Torvalds  */
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
30361da177e4SLinus Torvalds {
30371da177e4SLinus Torvalds 	unsigned long flags;
30381da177e4SLinus Torvalds 	struct sk_buff *result;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30411da177e4SLinus Torvalds 	result = __skb_dequeue(list);
30421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30431da177e4SLinus Torvalds 	return result;
30441da177e4SLinus Torvalds }
3045b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds /**
30481da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
30491da177e4SLinus Torvalds  *	@list: list to dequeue from
30501da177e4SLinus Torvalds  *
30511da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
30521da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
30531da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
30541da177e4SLinus Torvalds  */
30551da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
30561da177e4SLinus Torvalds {
30571da177e4SLinus Torvalds 	unsigned long flags;
30581da177e4SLinus Torvalds 	struct sk_buff *result;
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30611da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
30621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30631da177e4SLinus Torvalds 	return result;
30641da177e4SLinus Torvalds }
3065b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds /**
30681da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
30691da177e4SLinus Torvalds  *	@list: list to empty
30701da177e4SLinus Torvalds  *
30711da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
30721da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
30731da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
30741da177e4SLinus Torvalds  */
30751da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
30761da177e4SLinus Torvalds {
30771da177e4SLinus Torvalds 	struct sk_buff *skb;
30781da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
30791da177e4SLinus Torvalds 		kfree_skb(skb);
30801da177e4SLinus Torvalds }
3081b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds /**
30849f5afeaeSYaogong Wang  *	skb_rbtree_purge - empty a skb rbtree
30859f5afeaeSYaogong Wang  *	@root: root of the rbtree to empty
3086385114deSPeter Oskolkov  *	Return value: the sum of truesizes of all purged skbs.
30879f5afeaeSYaogong Wang  *
30889f5afeaeSYaogong Wang  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
30899f5afeaeSYaogong Wang  *	the list and one reference dropped. This function does not take
30909f5afeaeSYaogong Wang  *	any lock. Synchronization should be handled by the caller (e.g., TCP
30919f5afeaeSYaogong Wang  *	out-of-order queue is protected by the socket lock).
30929f5afeaeSYaogong Wang  */
3093385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root)
30949f5afeaeSYaogong Wang {
30957c90584cSEric Dumazet 	struct rb_node *p = rb_first(root);
3096385114deSPeter Oskolkov 	unsigned int sum = 0;
30979f5afeaeSYaogong Wang 
30987c90584cSEric Dumazet 	while (p) {
30997c90584cSEric Dumazet 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
31007c90584cSEric Dumazet 
31017c90584cSEric Dumazet 		p = rb_next(p);
31027c90584cSEric Dumazet 		rb_erase(&skb->rbnode, root);
3103385114deSPeter Oskolkov 		sum += skb->truesize;
31049f5afeaeSYaogong Wang 		kfree_skb(skb);
31057c90584cSEric Dumazet 	}
3106385114deSPeter Oskolkov 	return sum;
31079f5afeaeSYaogong Wang }
31089f5afeaeSYaogong Wang 
31099f5afeaeSYaogong Wang /**
31101da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
31111da177e4SLinus Torvalds  *	@list: list to use
31121da177e4SLinus Torvalds  *	@newsk: buffer to queue
31131da177e4SLinus Torvalds  *
31141da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
31151da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
31161da177e4SLinus Torvalds  *	safely.
31171da177e4SLinus Torvalds  *
31181da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31191da177e4SLinus Torvalds  */
31201da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
31211da177e4SLinus Torvalds {
31221da177e4SLinus Torvalds 	unsigned long flags;
31231da177e4SLinus Torvalds 
31241da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31251da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
31261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31271da177e4SLinus Torvalds }
3128b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds /**
31311da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
31321da177e4SLinus Torvalds  *	@list: list to use
31331da177e4SLinus Torvalds  *	@newsk: buffer to queue
31341da177e4SLinus Torvalds  *
31351da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
31361da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
31371da177e4SLinus Torvalds  *	safely.
31381da177e4SLinus Torvalds  *
31391da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31401da177e4SLinus Torvalds  */
31411da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
31421da177e4SLinus Torvalds {
31431da177e4SLinus Torvalds 	unsigned long flags;
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31461da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
31471da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31481da177e4SLinus Torvalds }
3149b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
31508728b834SDavid S. Miller 
31511da177e4SLinus Torvalds /**
31521da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
31531da177e4SLinus Torvalds  *	@skb: buffer to remove
31548728b834SDavid S. Miller  *	@list: list to use
31551da177e4SLinus Torvalds  *
31568728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
31578728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
31581da177e4SLinus Torvalds  *
31598728b834SDavid S. Miller  *	You must know what list the SKB is on.
31601da177e4SLinus Torvalds  */
31618728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
31621da177e4SLinus Torvalds {
31631da177e4SLinus Torvalds 	unsigned long flags;
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
31668728b834SDavid S. Miller 	__skb_unlink(skb, list);
31671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
31681da177e4SLinus Torvalds }
3169b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds /**
31721da177e4SLinus Torvalds  *	skb_append	-	append a buffer
31731da177e4SLinus Torvalds  *	@old: buffer to insert after
31741da177e4SLinus Torvalds  *	@newsk: buffer to insert
31758728b834SDavid S. Miller  *	@list: list to use
31761da177e4SLinus Torvalds  *
31771da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
31781da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
31791da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
31801da177e4SLinus Torvalds  */
31818728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
31821da177e4SLinus Torvalds {
31831da177e4SLinus Torvalds 	unsigned long flags;
31841da177e4SLinus Torvalds 
31858728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
31867de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
31878728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
31881da177e4SLinus Torvalds }
3189b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
31921da177e4SLinus Torvalds 					   struct sk_buff* skb1,
31931da177e4SLinus Torvalds 					   const u32 len, const int pos)
31941da177e4SLinus Torvalds {
31951da177e4SLinus Torvalds 	int i;
31961da177e4SLinus Torvalds 
3197d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3198d626f62bSArnaldo Carvalho de Melo 					 pos - len);
31991da177e4SLinus Torvalds 	/* And move data appendix as is. */
32001da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
32011da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
32041da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
32051da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
32061da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
32071da177e4SLinus Torvalds 	skb->data_len		   = 0;
32081da177e4SLinus Torvalds 	skb->len		   = len;
320927a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
32101da177e4SLinus Torvalds }
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
32131da177e4SLinus Torvalds 				       struct sk_buff* skb1,
32141da177e4SLinus Torvalds 				       const u32 len, int pos)
32151da177e4SLinus Torvalds {
32161da177e4SLinus Torvalds 	int i, k = 0;
32171da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
32201da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
32211da177e4SLinus Torvalds 	skb->len		  = len;
32221da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
32259e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 		if (pos + size > len) {
32281da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 			if (pos < len) {
32311da177e4SLinus Torvalds 				/* Split frag.
32321da177e4SLinus Torvalds 				 * We have two variants in this case:
32331da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
32341da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
32351da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
32361da177e4SLinus Torvalds 				 *    where splitting is expensive.
32371da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
32381da177e4SLinus Torvalds 				 */
3239ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
3240b54c9d5bSJonathan Lemon 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
32419e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
32429e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
32431da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
32441da177e4SLinus Torvalds 			}
32451da177e4SLinus Torvalds 			k++;
32461da177e4SLinus Torvalds 		} else
32471da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
32481da177e4SLinus Torvalds 		pos += size;
32491da177e4SLinus Torvalds 	}
32501da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
32511da177e4SLinus Torvalds }
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds /**
32541da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
32551da177e4SLinus Torvalds  * @skb: the buffer to split
32561da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
32571da177e4SLinus Torvalds  * @len: new length for skb
32581da177e4SLinus Torvalds  */
32591da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
32601da177e4SLinus Torvalds {
32611da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
32621da177e4SLinus Torvalds 
3263fff88030SWillem de Bruijn 	skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3264fff88030SWillem de Bruijn 				      SKBTX_SHARED_FRAG;
32651f8b977aSWillem de Bruijn 	skb_zerocopy_clone(skb1, skb, 0);
32661da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
32671da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
32681da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
32691da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
32701da177e4SLinus Torvalds }
3271b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
32721da177e4SLinus Torvalds 
32739f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
32749f782db3SIlpo Järvinen  *
32759f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
32769f782db3SIlpo Järvinen  */
3277832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
3278832d11c5SIlpo Järvinen {
32790ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3280832d11c5SIlpo Järvinen }
3281832d11c5SIlpo Järvinen 
3282832d11c5SIlpo Järvinen /**
3283832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
3284832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
3285832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
3286832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
3287832d11c5SIlpo Järvinen  *
3288832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
328920e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3290832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
3291832d11c5SIlpo Järvinen  *
3292832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
3293832d11c5SIlpo Järvinen  *
3294832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
3295832d11c5SIlpo Järvinen  * to have non-paged data as well.
3296832d11c5SIlpo Järvinen  *
3297832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
3298832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
3299832d11c5SIlpo Järvinen  */
3300832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3301832d11c5SIlpo Järvinen {
3302832d11c5SIlpo Järvinen 	int from, to, merge, todo;
3303d8e18a51SMatthew Wilcox (Oracle) 	skb_frag_t *fragfrom, *fragto;
3304832d11c5SIlpo Järvinen 
3305832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
3306f8071cdeSEric Dumazet 
3307f8071cdeSEric Dumazet 	if (skb_headlen(skb))
3308f8071cdeSEric Dumazet 		return 0;
33091f8b977aSWillem de Bruijn 	if (skb_zcopy(tgt) || skb_zcopy(skb))
33101f8b977aSWillem de Bruijn 		return 0;
3311832d11c5SIlpo Järvinen 
3312832d11c5SIlpo Järvinen 	todo = shiftlen;
3313832d11c5SIlpo Järvinen 	from = 0;
3314832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
3315832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
3316832d11c5SIlpo Järvinen 
3317832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
3318832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
3319832d11c5SIlpo Järvinen 	 */
3320832d11c5SIlpo Järvinen 	if (!to ||
3321ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3322b54c9d5bSJonathan Lemon 			      skb_frag_off(fragfrom))) {
3323832d11c5SIlpo Järvinen 		merge = -1;
3324832d11c5SIlpo Järvinen 	} else {
3325832d11c5SIlpo Järvinen 		merge = to - 1;
3326832d11c5SIlpo Järvinen 
33279e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
3328832d11c5SIlpo Järvinen 		if (todo < 0) {
3329832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
3330832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
3331832d11c5SIlpo Järvinen 				return 0;
3332832d11c5SIlpo Järvinen 
33339f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
33349f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
3335832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
3336832d11c5SIlpo Järvinen 
33379e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
33389e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
3339b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, shiftlen);
3340832d11c5SIlpo Järvinen 
3341832d11c5SIlpo Järvinen 			goto onlymerged;
3342832d11c5SIlpo Järvinen 		}
3343832d11c5SIlpo Järvinen 
3344832d11c5SIlpo Järvinen 		from++;
3345832d11c5SIlpo Järvinen 	}
3346832d11c5SIlpo Järvinen 
3347832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
3348832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
3349832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3350832d11c5SIlpo Järvinen 		return 0;
3351832d11c5SIlpo Järvinen 
3352832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3353832d11c5SIlpo Järvinen 		return 0;
3354832d11c5SIlpo Järvinen 
3355832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3356832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
3357832d11c5SIlpo Järvinen 			return 0;
3358832d11c5SIlpo Järvinen 
3359832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
3360832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
3361832d11c5SIlpo Järvinen 
33629e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
3363832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
33649e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
3365832d11c5SIlpo Järvinen 			from++;
3366832d11c5SIlpo Järvinen 			to++;
3367832d11c5SIlpo Järvinen 
3368832d11c5SIlpo Järvinen 		} else {
3369ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
3370b54c9d5bSJonathan Lemon 			skb_frag_page_copy(fragto, fragfrom);
3371b54c9d5bSJonathan Lemon 			skb_frag_off_copy(fragto, fragfrom);
33729e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
3373832d11c5SIlpo Järvinen 
3374b54c9d5bSJonathan Lemon 			skb_frag_off_add(fragfrom, todo);
33759e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
3376832d11c5SIlpo Järvinen 			todo = 0;
3377832d11c5SIlpo Järvinen 
3378832d11c5SIlpo Järvinen 			to++;
3379832d11c5SIlpo Järvinen 			break;
3380832d11c5SIlpo Järvinen 		}
3381832d11c5SIlpo Järvinen 	}
3382832d11c5SIlpo Järvinen 
3383832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
3384832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
3385832d11c5SIlpo Järvinen 
3386832d11c5SIlpo Järvinen 	if (merge >= 0) {
3387832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
3388832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
3389832d11c5SIlpo Järvinen 
33909e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3391ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
3392832d11c5SIlpo Järvinen 	}
3393832d11c5SIlpo Järvinen 
3394832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
3395832d11c5SIlpo Järvinen 	to = 0;
3396832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
3397832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3398832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
3399832d11c5SIlpo Järvinen 
3400832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3401832d11c5SIlpo Järvinen 
3402832d11c5SIlpo Järvinen onlymerged:
3403832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
3404832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
3405832d11c5SIlpo Järvinen 	 */
3406832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
3407832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
3408832d11c5SIlpo Järvinen 
3409832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
3410832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
3411832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
3412832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
3413832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
3414832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
3415832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
3416832d11c5SIlpo Järvinen 
3417832d11c5SIlpo Järvinen 	return shiftlen;
3418832d11c5SIlpo Järvinen }
3419832d11c5SIlpo Järvinen 
3420677e90edSThomas Graf /**
3421677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
3422677e90edSThomas Graf  * @skb: the buffer to read
3423677e90edSThomas Graf  * @from: lower offset of data to be read
3424677e90edSThomas Graf  * @to: upper offset of data to be read
3425677e90edSThomas Graf  * @st: state variable
3426677e90edSThomas Graf  *
3427677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
3428677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
3429677e90edSThomas Graf  */
3430677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3431677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
3432677e90edSThomas Graf {
3433677e90edSThomas Graf 	st->lower_offset = from;
3434677e90edSThomas Graf 	st->upper_offset = to;
3435677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
3436677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
3437677e90edSThomas Graf 	st->frag_data = NULL;
3438677e90edSThomas Graf }
3439b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
3440677e90edSThomas Graf 
3441677e90edSThomas Graf /**
3442677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
3443677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
3444677e90edSThomas Graf  * @data: destination pointer for data to be returned
3445677e90edSThomas Graf  * @st: state variable
3446677e90edSThomas Graf  *
3447bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
3448677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
3449bc32383cSMathias Krause  * the head of the data block to @data and returns the length
3450677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
3451677e90edSThomas Graf  * offset has been reached.
3452677e90edSThomas Graf  *
3453677e90edSThomas Graf  * The caller is not required to consume all of the data
3454bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
3455677e90edSThomas Graf  * of bytes already consumed and the next call to
3456677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
3457677e90edSThomas Graf  *
345825985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
3459e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
3460677e90edSThomas Graf  *       reads of potentially non linear data.
3461677e90edSThomas Graf  *
3462bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
3463677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
3464677e90edSThomas Graf  *       a stack for this purpose.
3465677e90edSThomas Graf  */
3466677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3467677e90edSThomas Graf 			  struct skb_seq_state *st)
3468677e90edSThomas Graf {
3469677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3470677e90edSThomas Graf 	skb_frag_t *frag;
3471677e90edSThomas Graf 
3472aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
3473aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
3474aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
3475aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
3476aeb193eaSWedson Almeida Filho 		}
3477677e90edSThomas Graf 		return 0;
3478aeb193eaSWedson Almeida Filho 	}
3479677e90edSThomas Graf 
3480677e90edSThomas Graf next_skb:
348195e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3482677e90edSThomas Graf 
3483995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
348495e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3485677e90edSThomas Graf 		return block_limit - abs_offset;
3486677e90edSThomas Graf 	}
3487677e90edSThomas Graf 
3488677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
3489677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
3490677e90edSThomas Graf 
3491677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3492677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
34939e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
3494677e90edSThomas Graf 
3495677e90edSThomas Graf 		if (abs_offset < block_limit) {
3496677e90edSThomas Graf 			if (!st->frag_data)
349751c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
3498677e90edSThomas Graf 
3499b54c9d5bSJonathan Lemon 			*data = (u8 *) st->frag_data + skb_frag_off(frag) +
3500677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
3501677e90edSThomas Graf 
3502677e90edSThomas Graf 			return block_limit - abs_offset;
3503677e90edSThomas Graf 		}
3504677e90edSThomas Graf 
3505677e90edSThomas Graf 		if (st->frag_data) {
350651c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
3507677e90edSThomas Graf 			st->frag_data = NULL;
3508677e90edSThomas Graf 		}
3509677e90edSThomas Graf 
3510677e90edSThomas Graf 		st->frag_idx++;
35119e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
3512677e90edSThomas Graf 	}
3513677e90edSThomas Graf 
35145b5a60daSOlaf Kirch 	if (st->frag_data) {
351551c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
35165b5a60daSOlaf Kirch 		st->frag_data = NULL;
35175b5a60daSOlaf Kirch 	}
35185b5a60daSOlaf Kirch 
351921dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3520677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
352195e3b24cSHerbert Xu 		st->frag_idx = 0;
3522677e90edSThomas Graf 		goto next_skb;
352371b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
352471b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
352571b3346dSShyam Iyer 		st->frag_idx = 0;
3526677e90edSThomas Graf 		goto next_skb;
3527677e90edSThomas Graf 	}
3528677e90edSThomas Graf 
3529677e90edSThomas Graf 	return 0;
3530677e90edSThomas Graf }
3531b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
3532677e90edSThomas Graf 
3533677e90edSThomas Graf /**
3534677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
3535677e90edSThomas Graf  * @st: state variable
3536677e90edSThomas Graf  *
3537677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
3538677e90edSThomas Graf  * returned 0.
3539677e90edSThomas Graf  */
3540677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
3541677e90edSThomas Graf {
3542677e90edSThomas Graf 	if (st->frag_data)
354351c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
3544677e90edSThomas Graf }
3545b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
3546677e90edSThomas Graf 
35473fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
35483fc7e8a6SThomas Graf 
35493fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
35503fc7e8a6SThomas Graf 					  struct ts_config *conf,
35513fc7e8a6SThomas Graf 					  struct ts_state *state)
35523fc7e8a6SThomas Graf {
35533fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
35543fc7e8a6SThomas Graf }
35553fc7e8a6SThomas Graf 
35563fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
35573fc7e8a6SThomas Graf {
35583fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
35593fc7e8a6SThomas Graf }
35603fc7e8a6SThomas Graf 
35613fc7e8a6SThomas Graf /**
35623fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
35633fc7e8a6SThomas Graf  * @skb: the buffer to look in
35643fc7e8a6SThomas Graf  * @from: search offset
35653fc7e8a6SThomas Graf  * @to: search limit
35663fc7e8a6SThomas Graf  * @config: textsearch configuration
35673fc7e8a6SThomas Graf  *
35683fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
35693fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
35703fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
35713fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
35723fc7e8a6SThomas Graf  */
35733fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3574059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
35753fc7e8a6SThomas Graf {
3576059a2440SBojan Prtvar 	struct ts_state state;
3577f72b948dSPhil Oester 	unsigned int ret;
3578f72b948dSPhil Oester 
35793fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
35803fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
35813fc7e8a6SThomas Graf 
3582059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
35833fc7e8a6SThomas Graf 
3584059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
3585f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
35863fc7e8a6SThomas Graf }
3587b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
35883fc7e8a6SThomas Graf 
3589be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3590be12a1feSHannes Frederic Sowa 			 int offset, size_t size)
3591be12a1feSHannes Frederic Sowa {
3592be12a1feSHannes Frederic Sowa 	int i = skb_shinfo(skb)->nr_frags;
3593be12a1feSHannes Frederic Sowa 
3594be12a1feSHannes Frederic Sowa 	if (skb_can_coalesce(skb, i, page, offset)) {
3595be12a1feSHannes Frederic Sowa 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3596be12a1feSHannes Frederic Sowa 	} else if (i < MAX_SKB_FRAGS) {
3597be12a1feSHannes Frederic Sowa 		get_page(page);
3598be12a1feSHannes Frederic Sowa 		skb_fill_page_desc(skb, i, page, offset, size);
3599be12a1feSHannes Frederic Sowa 	} else {
3600be12a1feSHannes Frederic Sowa 		return -EMSGSIZE;
3601be12a1feSHannes Frederic Sowa 	}
3602be12a1feSHannes Frederic Sowa 
3603be12a1feSHannes Frederic Sowa 	return 0;
3604be12a1feSHannes Frederic Sowa }
3605be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3606be12a1feSHannes Frederic Sowa 
3607cbb042f9SHerbert Xu /**
3608cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
3609cbb042f9SHerbert Xu  *	@skb: buffer to update
3610cbb042f9SHerbert Xu  *	@len: length of data pulled
3611cbb042f9SHerbert Xu  *
3612cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
3613fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
361484fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
361584fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
361684fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
3617cbb042f9SHerbert Xu  */
3618af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3619cbb042f9SHerbert Xu {
362031b33dfbSPravin B Shelar 	unsigned char *data = skb->data;
362131b33dfbSPravin B Shelar 
3622cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
362331b33dfbSPravin B Shelar 	__skb_pull(skb, len);
362431b33dfbSPravin B Shelar 	skb_postpull_rcsum(skb, data, len);
362531b33dfbSPravin B Shelar 	return skb->data;
3626cbb042f9SHerbert Xu }
3627f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3628f94691acSArnaldo Carvalho de Melo 
362913acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
363013acc94eSYonghong Song {
363113acc94eSYonghong Song 	skb_frag_t head_frag;
363213acc94eSYonghong Song 	struct page *page;
363313acc94eSYonghong Song 
363413acc94eSYonghong Song 	page = virt_to_head_page(frag_skb->head);
3635d8e18a51SMatthew Wilcox (Oracle) 	__skb_frag_set_page(&head_frag, page);
3636b54c9d5bSJonathan Lemon 	skb_frag_off_set(&head_frag, frag_skb->data -
3637b54c9d5bSJonathan Lemon 			 (unsigned char *)page_address(page));
3638d8e18a51SMatthew Wilcox (Oracle) 	skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
363913acc94eSYonghong Song 	return head_frag;
364013acc94eSYonghong Song }
364113acc94eSYonghong Song 
36423a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb,
36433a1296a3SSteffen Klassert 				 netdev_features_t features,
36443a1296a3SSteffen Klassert 				 unsigned int offset)
36453a1296a3SSteffen Klassert {
36463a1296a3SSteffen Klassert 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
36473a1296a3SSteffen Klassert 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
36483a1296a3SSteffen Klassert 	unsigned int delta_truesize = 0;
36493a1296a3SSteffen Klassert 	unsigned int delta_len = 0;
36503a1296a3SSteffen Klassert 	struct sk_buff *tail = NULL;
36513a1296a3SSteffen Klassert 	struct sk_buff *nskb;
36523a1296a3SSteffen Klassert 
36533a1296a3SSteffen Klassert 	skb_push(skb, -skb_network_offset(skb) + offset);
36543a1296a3SSteffen Klassert 
36553a1296a3SSteffen Klassert 	skb_shinfo(skb)->frag_list = NULL;
36563a1296a3SSteffen Klassert 
36573a1296a3SSteffen Klassert 	do {
36583a1296a3SSteffen Klassert 		nskb = list_skb;
36593a1296a3SSteffen Klassert 		list_skb = list_skb->next;
36603a1296a3SSteffen Klassert 
36613a1296a3SSteffen Klassert 		if (!tail)
36623a1296a3SSteffen Klassert 			skb->next = nskb;
36633a1296a3SSteffen Klassert 		else
36643a1296a3SSteffen Klassert 			tail->next = nskb;
36653a1296a3SSteffen Klassert 
36663a1296a3SSteffen Klassert 		tail = nskb;
36673a1296a3SSteffen Klassert 
36683a1296a3SSteffen Klassert 		delta_len += nskb->len;
36693a1296a3SSteffen Klassert 		delta_truesize += nskb->truesize;
36703a1296a3SSteffen Klassert 
36713a1296a3SSteffen Klassert 		skb_push(nskb, -skb_network_offset(nskb) + offset);
36723a1296a3SSteffen Klassert 
3673cf673ed0SFlorian Westphal 		skb_release_head_state(nskb);
36743a1296a3SSteffen Klassert 		 __copy_skb_header(nskb, skb);
36753a1296a3SSteffen Klassert 
36763a1296a3SSteffen Klassert 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
36773a1296a3SSteffen Klassert 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
36783a1296a3SSteffen Klassert 						 nskb->data - tnl_hlen,
36793a1296a3SSteffen Klassert 						 offset + tnl_hlen);
36803a1296a3SSteffen Klassert 
36813a1296a3SSteffen Klassert 		if (skb_needs_linearize(nskb, features) &&
36823a1296a3SSteffen Klassert 		    __skb_linearize(nskb))
36833a1296a3SSteffen Klassert 			goto err_linearize;
36843a1296a3SSteffen Klassert 
36853a1296a3SSteffen Klassert 	} while (list_skb);
36863a1296a3SSteffen Klassert 
36873a1296a3SSteffen Klassert 	skb->truesize = skb->truesize - delta_truesize;
36883a1296a3SSteffen Klassert 	skb->data_len = skb->data_len - delta_len;
36893a1296a3SSteffen Klassert 	skb->len = skb->len - delta_len;
36903a1296a3SSteffen Klassert 
36913a1296a3SSteffen Klassert 	skb_gso_reset(skb);
36923a1296a3SSteffen Klassert 
36933a1296a3SSteffen Klassert 	skb->prev = tail;
36943a1296a3SSteffen Klassert 
36953a1296a3SSteffen Klassert 	if (skb_needs_linearize(skb, features) &&
36963a1296a3SSteffen Klassert 	    __skb_linearize(skb))
36973a1296a3SSteffen Klassert 		goto err_linearize;
36983a1296a3SSteffen Klassert 
36993a1296a3SSteffen Klassert 	skb_get(skb);
37003a1296a3SSteffen Klassert 
37013a1296a3SSteffen Klassert 	return skb;
37023a1296a3SSteffen Klassert 
37033a1296a3SSteffen Klassert err_linearize:
37043a1296a3SSteffen Klassert 	kfree_skb_list(skb->next);
37053a1296a3SSteffen Klassert 	skb->next = NULL;
37063a1296a3SSteffen Klassert 	return ERR_PTR(-ENOMEM);
37073a1296a3SSteffen Klassert }
37083a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list);
37093a1296a3SSteffen Klassert 
37103a1296a3SSteffen Klassert int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
37113a1296a3SSteffen Klassert {
37123a1296a3SSteffen Klassert 	if (unlikely(p->len + skb->len >= 65536))
37133a1296a3SSteffen Klassert 		return -E2BIG;
37143a1296a3SSteffen Klassert 
37153a1296a3SSteffen Klassert 	if (NAPI_GRO_CB(p)->last == p)
37163a1296a3SSteffen Klassert 		skb_shinfo(p)->frag_list = skb;
37173a1296a3SSteffen Klassert 	else
37183a1296a3SSteffen Klassert 		NAPI_GRO_CB(p)->last->next = skb;
37193a1296a3SSteffen Klassert 
37203a1296a3SSteffen Klassert 	skb_pull(skb, skb_gro_offset(skb));
37213a1296a3SSteffen Klassert 
37223a1296a3SSteffen Klassert 	NAPI_GRO_CB(p)->last = skb;
37233a1296a3SSteffen Klassert 	NAPI_GRO_CB(p)->count++;
37243a1296a3SSteffen Klassert 	p->data_len += skb->len;
37253a1296a3SSteffen Klassert 	p->truesize += skb->truesize;
37263a1296a3SSteffen Klassert 	p->len += skb->len;
37273a1296a3SSteffen Klassert 
37283a1296a3SSteffen Klassert 	NAPI_GRO_CB(skb)->same_flow = 1;
37293a1296a3SSteffen Klassert 
37303a1296a3SSteffen Klassert 	return 0;
37313a1296a3SSteffen Klassert }
37323a1296a3SSteffen Klassert 
3733f4c50d99SHerbert Xu /**
3734f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
3735df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
3736576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
3737f4c50d99SHerbert Xu  *
3738f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
37394c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
37404c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
3741f4c50d99SHerbert Xu  */
3742df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
3743df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
3744f4c50d99SHerbert Xu {
3745f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
3746f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
37471a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3748df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3749df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3750df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
37511fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
3752f4c50d99SHerbert Xu 	unsigned int offset = doffset;
3753df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3754802ab55aSAlexander Duyck 	unsigned int partial_segs = 0;
3755f4c50d99SHerbert Xu 	unsigned int headroom;
3756802ab55aSAlexander Duyck 	unsigned int len = head_skb->len;
3757ec5f0615SPravin B Shelar 	__be16 proto;
375836c98382SAlexander Duyck 	bool csum, sg;
3759df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3760f4c50d99SHerbert Xu 	int err = -ENOMEM;
3761f4c50d99SHerbert Xu 	int i = 0;
3762f4c50d99SHerbert Xu 	int pos;
3763f4c50d99SHerbert Xu 
37643dcbdb13SShmulik Ladkani 	if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
37653dcbdb13SShmulik Ladkani 	    (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
37663dcbdb13SShmulik Ladkani 		/* gso_size is untrusted, and we have a frag_list with a linear
37673dcbdb13SShmulik Ladkani 		 * non head_frag head.
37683dcbdb13SShmulik Ladkani 		 *
37693dcbdb13SShmulik Ladkani 		 * (we assume checking the first list_skb member suffices;
37703dcbdb13SShmulik Ladkani 		 * i.e if either of the list_skb members have non head_frag
37713dcbdb13SShmulik Ladkani 		 * head, then the first one has too).
37723dcbdb13SShmulik Ladkani 		 *
37733dcbdb13SShmulik Ladkani 		 * If head_skb's headlen does not fit requested gso_size, it
37743dcbdb13SShmulik Ladkani 		 * means that the frag_list members do NOT terminate on exact
37753dcbdb13SShmulik Ladkani 		 * gso_size boundaries. Hence we cannot perform skb_frag_t page
37763dcbdb13SShmulik Ladkani 		 * sharing. Therefore we must fallback to copying the frag_list
37773dcbdb13SShmulik Ladkani 		 * skbs; we do so by disabling SG.
37783dcbdb13SShmulik Ladkani 		 */
37793dcbdb13SShmulik Ladkani 		if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
37803dcbdb13SShmulik Ladkani 			features &= ~NETIF_F_SG;
37813dcbdb13SShmulik Ladkani 	}
37823dcbdb13SShmulik Ladkani 
37835882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
37842f631133SMiaohe Lin 	proto = skb_network_protocol(head_skb, NULL);
3785ec5f0615SPravin B Shelar 	if (unlikely(!proto))
3786ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
3787ec5f0615SPravin B Shelar 
378836c98382SAlexander Duyck 	sg = !!(features & NETIF_F_SG);
3789f245d079SAlexander Duyck 	csum = !!can_checksum_protocol(features, proto);
37907e2b10c1STom Herbert 
379107b26c94SSteffen Klassert 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
379207b26c94SSteffen Klassert 		if (!(features & NETIF_F_GSO_PARTIAL)) {
379307b26c94SSteffen Klassert 			struct sk_buff *iter;
379443170c4eSIlan Tayari 			unsigned int frag_len;
379507b26c94SSteffen Klassert 
379607b26c94SSteffen Klassert 			if (!list_skb ||
379707b26c94SSteffen Klassert 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
379807b26c94SSteffen Klassert 				goto normal;
379907b26c94SSteffen Klassert 
380043170c4eSIlan Tayari 			/* If we get here then all the required
380143170c4eSIlan Tayari 			 * GSO features except frag_list are supported.
380243170c4eSIlan Tayari 			 * Try to split the SKB to multiple GSO SKBs
380343170c4eSIlan Tayari 			 * with no frag_list.
380443170c4eSIlan Tayari 			 * Currently we can do that only when the buffers don't
380543170c4eSIlan Tayari 			 * have a linear part and all the buffers except
380643170c4eSIlan Tayari 			 * the last are of the same length.
380707b26c94SSteffen Klassert 			 */
380843170c4eSIlan Tayari 			frag_len = list_skb->len;
380907b26c94SSteffen Klassert 			skb_walk_frags(head_skb, iter) {
381043170c4eSIlan Tayari 				if (frag_len != iter->len && iter->next)
381143170c4eSIlan Tayari 					goto normal;
3812eaffadbbSIlan Tayari 				if (skb_headlen(iter) && !iter->head_frag)
381307b26c94SSteffen Klassert 					goto normal;
381407b26c94SSteffen Klassert 
381507b26c94SSteffen Klassert 				len -= iter->len;
381607b26c94SSteffen Klassert 			}
381743170c4eSIlan Tayari 
381843170c4eSIlan Tayari 			if (len != frag_len)
381943170c4eSIlan Tayari 				goto normal;
382007b26c94SSteffen Klassert 		}
382107b26c94SSteffen Klassert 
3822802ab55aSAlexander Duyck 		/* GSO partial only requires that we trim off any excess that
3823802ab55aSAlexander Duyck 		 * doesn't fit into an MSS sized block, so take care of that
3824802ab55aSAlexander Duyck 		 * now.
3825802ab55aSAlexander Duyck 		 */
3826802ab55aSAlexander Duyck 		partial_segs = len / mss;
3827d7fb5a80SAlexander Duyck 		if (partial_segs > 1)
3828802ab55aSAlexander Duyck 			mss *= partial_segs;
3829d7fb5a80SAlexander Duyck 		else
3830d7fb5a80SAlexander Duyck 			partial_segs = 0;
3831802ab55aSAlexander Duyck 	}
3832802ab55aSAlexander Duyck 
383307b26c94SSteffen Klassert normal:
3834df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
3835df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
3836f4c50d99SHerbert Xu 
3837f4c50d99SHerbert Xu 	do {
3838f4c50d99SHerbert Xu 		struct sk_buff *nskb;
38398cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
3840c8884eddSHerbert Xu 		int hsize;
3841f4c50d99SHerbert Xu 		int size;
3842f4c50d99SHerbert Xu 
38433953c46cSMarcelo Ricardo Leitner 		if (unlikely(mss == GSO_BY_FRAGS)) {
38443953c46cSMarcelo Ricardo Leitner 			len = list_skb->len;
38453953c46cSMarcelo Ricardo Leitner 		} else {
3846df5771ffSMichael S. Tsirkin 			len = head_skb->len - offset;
3847f4c50d99SHerbert Xu 			if (len > mss)
3848f4c50d99SHerbert Xu 				len = mss;
38493953c46cSMarcelo Ricardo Leitner 		}
3850f4c50d99SHerbert Xu 
3851df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
3852f4c50d99SHerbert Xu 		if (hsize < 0)
3853f4c50d99SHerbert Xu 			hsize = 0;
3854c8884eddSHerbert Xu 		if (hsize > len || !sg)
3855c8884eddSHerbert Xu 			hsize = len;
3856f4c50d99SHerbert Xu 
38571a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
38581a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
38591a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
386089319d38SHerbert Xu 
38619d8506ccSHerbert Xu 			i = 0;
38621a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
38631a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
38641fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
38651a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
38669d8506ccSHerbert Xu 
38679d8506ccSHerbert Xu 			while (pos < offset + len) {
38689d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
38699d8506ccSHerbert Xu 
38704e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
38719d8506ccSHerbert Xu 				if (pos + size > offset + len)
38729d8506ccSHerbert Xu 					break;
38739d8506ccSHerbert Xu 
38749d8506ccSHerbert Xu 				i++;
38759d8506ccSHerbert Xu 				pos += size;
38764e1beba1SMichael S. Tsirkin 				frag++;
38779d8506ccSHerbert Xu 			}
38789d8506ccSHerbert Xu 
38791a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
38801a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
388189319d38SHerbert Xu 
3882f4c50d99SHerbert Xu 			if (unlikely(!nskb))
3883f4c50d99SHerbert Xu 				goto err;
3884f4c50d99SHerbert Xu 
38859d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
38869d8506ccSHerbert Xu 				kfree_skb(nskb);
38879d8506ccSHerbert Xu 				goto err;
38889d8506ccSHerbert Xu 			}
38899d8506ccSHerbert Xu 
3890ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
389189319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
389289319d38SHerbert Xu 				kfree_skb(nskb);
389389319d38SHerbert Xu 				goto err;
389489319d38SHerbert Xu 			}
389589319d38SHerbert Xu 
3896ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
389789319d38SHerbert Xu 			skb_release_head_state(nskb);
389889319d38SHerbert Xu 			__skb_push(nskb, doffset);
389989319d38SHerbert Xu 		} else {
3900c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3901df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3902c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
390389319d38SHerbert Xu 
390489319d38SHerbert Xu 			if (unlikely(!nskb))
390589319d38SHerbert Xu 				goto err;
390689319d38SHerbert Xu 
390789319d38SHerbert Xu 			skb_reserve(nskb, headroom);
390889319d38SHerbert Xu 			__skb_put(nskb, doffset);
390989319d38SHerbert Xu 		}
391089319d38SHerbert Xu 
3911f4c50d99SHerbert Xu 		if (segs)
3912f4c50d99SHerbert Xu 			tail->next = nskb;
3913f4c50d99SHerbert Xu 		else
3914f4c50d99SHerbert Xu 			segs = nskb;
3915f4c50d99SHerbert Xu 		tail = nskb;
3916f4c50d99SHerbert Xu 
3917df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3918f4c50d99SHerbert Xu 
3919030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3920fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
392168c33163SPravin B Shelar 
3922df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
392368c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
392468c33163SPravin B Shelar 						 doffset + tnl_hlen);
392589319d38SHerbert Xu 
39269d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
39271cdbcb79SSimon Horman 			goto perform_csum_check;
392889319d38SHerbert Xu 
39297fbeffedSAlexander Duyck 		if (!sg) {
39301454c9faSYadu Kishore 			if (!csum) {
39317fbeffedSAlexander Duyck 				if (!nskb->remcsum_offload)
39326f85a124SHerbert Xu 					nskb->ip_summed = CHECKSUM_NONE;
393376443456SAlexander Duyck 				SKB_GSO_CB(nskb)->csum =
393476443456SAlexander Duyck 					skb_copy_and_csum_bits(head_skb, offset,
39351454c9faSYadu Kishore 							       skb_put(nskb,
39361454c9faSYadu Kishore 								       len),
3937f4c50d99SHerbert Xu 							       len, 0);
39387e2b10c1STom Herbert 				SKB_GSO_CB(nskb)->csum_start =
3939de843723STom Herbert 					skb_headroom(nskb) + doffset;
39401454c9faSYadu Kishore 			} else {
39411454c9faSYadu Kishore 				skb_copy_bits(head_skb, offset,
39421454c9faSYadu Kishore 					      skb_put(nskb, len),
39431454c9faSYadu Kishore 					      len);
39441454c9faSYadu Kishore 			}
3945f4c50d99SHerbert Xu 			continue;
3946f4c50d99SHerbert Xu 		}
3947f4c50d99SHerbert Xu 
39488cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3949f4c50d99SHerbert Xu 
3950df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3951d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3952f4c50d99SHerbert Xu 
3953fff88030SWillem de Bruijn 		skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3954df5771ffSMichael S. Tsirkin 					      SKBTX_SHARED_FRAG;
3955cef401deSEric Dumazet 
3956bf5c25d6SWillem de Bruijn 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3957bf5c25d6SWillem de Bruijn 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3958bf5c25d6SWillem de Bruijn 			goto err;
3959bf5c25d6SWillem de Bruijn 
39609d8506ccSHerbert Xu 		while (pos < offset + len) {
39619d8506ccSHerbert Xu 			if (i >= nfrags) {
39629d8506ccSHerbert Xu 				i = 0;
39631a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
39641a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
39651fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
396613acc94eSYonghong Song 				if (!skb_headlen(list_skb)) {
39679d8506ccSHerbert Xu 					BUG_ON(!nfrags);
396813acc94eSYonghong Song 				} else {
396913acc94eSYonghong Song 					BUG_ON(!list_skb->head_frag);
39709d8506ccSHerbert Xu 
397113acc94eSYonghong Song 					/* to make room for head_frag. */
397213acc94eSYonghong Song 					i--;
397313acc94eSYonghong Song 					frag--;
397413acc94eSYonghong Song 				}
3975bf5c25d6SWillem de Bruijn 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3976bf5c25d6SWillem de Bruijn 				    skb_zerocopy_clone(nskb, frag_skb,
3977bf5c25d6SWillem de Bruijn 						       GFP_ATOMIC))
3978bf5c25d6SWillem de Bruijn 					goto err;
3979bf5c25d6SWillem de Bruijn 
39801a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
39819d8506ccSHerbert Xu 			}
39829d8506ccSHerbert Xu 
39839d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
39849d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
39859d8506ccSHerbert Xu 				net_warn_ratelimited(
39869d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
39879d8506ccSHerbert Xu 					pos, mss);
3988ff907a11SEric Dumazet 				err = -EINVAL;
39899d8506ccSHerbert Xu 				goto err;
39909d8506ccSHerbert Xu 			}
39919d8506ccSHerbert Xu 
399213acc94eSYonghong Song 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
39938cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
39948cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3995f4c50d99SHerbert Xu 
3996f4c50d99SHerbert Xu 			if (pos < offset) {
3997b54c9d5bSJonathan Lemon 				skb_frag_off_add(nskb_frag, offset - pos);
39988cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3999f4c50d99SHerbert Xu 			}
4000f4c50d99SHerbert Xu 
400189319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
4002f4c50d99SHerbert Xu 
4003f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
4004f4c50d99SHerbert Xu 				i++;
40054e1beba1SMichael S. Tsirkin 				frag++;
4006f4c50d99SHerbert Xu 				pos += size;
4007f4c50d99SHerbert Xu 			} else {
40088cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
400989319d38SHerbert Xu 				goto skip_fraglist;
4010f4c50d99SHerbert Xu 			}
4011f4c50d99SHerbert Xu 
40128cb19905SMichael S. Tsirkin 			nskb_frag++;
4013f4c50d99SHerbert Xu 		}
4014f4c50d99SHerbert Xu 
401589319d38SHerbert Xu skip_fraglist:
4016f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
4017f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
4018f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
4019ec5f0615SPravin B Shelar 
40201cdbcb79SSimon Horman perform_csum_check:
40217fbeffedSAlexander Duyck 		if (!csum) {
4022ff907a11SEric Dumazet 			if (skb_has_shared_frag(nskb) &&
4023ff907a11SEric Dumazet 			    __skb_linearize(nskb))
4024ddff00d4SAlexander Duyck 				goto err;
4025ff907a11SEric Dumazet 
40267fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
4027ec5f0615SPravin B Shelar 				nskb->ip_summed = CHECKSUM_NONE;
402876443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
402976443456SAlexander Duyck 				skb_checksum(nskb, doffset,
403076443456SAlexander Duyck 					     nskb->len - doffset, 0);
40317e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
40327e2b10c1STom Herbert 				skb_headroom(nskb) + doffset;
4033ec5f0615SPravin B Shelar 		}
4034df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
4035f4c50d99SHerbert Xu 
4036bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
4037bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
4038bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
4039bec3cfdcSEric Dumazet 	 */
4040bec3cfdcSEric Dumazet 	segs->prev = tail;
4041432c856fSToshiaki Makita 
4042802ab55aSAlexander Duyck 	if (partial_segs) {
404307b26c94SSteffen Klassert 		struct sk_buff *iter;
4044802ab55aSAlexander Duyck 		int type = skb_shinfo(head_skb)->gso_type;
404507b26c94SSteffen Klassert 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4046802ab55aSAlexander Duyck 
4047802ab55aSAlexander Duyck 		/* Update type to add partial and then remove dodgy if set */
404807b26c94SSteffen Klassert 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4049802ab55aSAlexander Duyck 		type &= ~SKB_GSO_DODGY;
4050802ab55aSAlexander Duyck 
4051802ab55aSAlexander Duyck 		/* Update GSO info and prepare to start updating headers on
4052802ab55aSAlexander Duyck 		 * our way back down the stack of protocols.
4053802ab55aSAlexander Duyck 		 */
405407b26c94SSteffen Klassert 		for (iter = segs; iter; iter = iter->next) {
405507b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_size = gso_size;
405607b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_segs = partial_segs;
405707b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_type = type;
405807b26c94SSteffen Klassert 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
405907b26c94SSteffen Klassert 		}
406007b26c94SSteffen Klassert 
406107b26c94SSteffen Klassert 		if (tail->len - doffset <= gso_size)
406207b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_size = 0;
406307b26c94SSteffen Klassert 		else if (tail != segs)
406407b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4065802ab55aSAlexander Duyck 	}
4066802ab55aSAlexander Duyck 
4067432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
4068432c856fSToshiaki Makita 	 * using skb_set_owner_w().
4069432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
4070432c856fSToshiaki Makita 	 */
4071432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
4072432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
4073432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
4074432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
4075432c856fSToshiaki Makita 	}
4076f4c50d99SHerbert Xu 	return segs;
4077f4c50d99SHerbert Xu 
4078f4c50d99SHerbert Xu err:
4079289dccbeSEric Dumazet 	kfree_skb_list(segs);
4080f4c50d99SHerbert Xu 	return ERR_PTR(err);
4081f4c50d99SHerbert Xu }
4082f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
4083f4c50d99SHerbert Xu 
4084d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
408571d93b39SHerbert Xu {
40868a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
408767147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
408867147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
40898a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
4090715dc1f3SEric Dumazet 	unsigned int delta_truesize;
4091d4546c25SDavid Miller 	struct sk_buff *lp;
409271d93b39SHerbert Xu 
40930ab03f35SSteffen Klassert 	if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
409471d93b39SHerbert Xu 		return -E2BIG;
409571d93b39SHerbert Xu 
409629e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
40978a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
40988a29111cSEric Dumazet 
40998a29111cSEric Dumazet 	if (headlen <= offset) {
410042da6994SHerbert Xu 		skb_frag_t *frag;
410166e92fcfSHerbert Xu 		skb_frag_t *frag2;
41029aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
41039aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
410442da6994SHerbert Xu 
410566e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
41068a29111cSEric Dumazet 			goto merge;
410781705ad1SHerbert Xu 
41088a29111cSEric Dumazet 		offset -= headlen;
41099aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
41109aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
4111f5572068SHerbert Xu 
41129aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
41139aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
411466e92fcfSHerbert Xu 		do {
411566e92fcfSHerbert Xu 			*--frag = *--frag2;
411666e92fcfSHerbert Xu 		} while (--i);
411766e92fcfSHerbert Xu 
4118b54c9d5bSJonathan Lemon 		skb_frag_off_add(frag, offset);
41199e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
412066e92fcfSHerbert Xu 
4121715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
4122ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
4123ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
4124715dc1f3SEric Dumazet 
4125f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
4126f5572068SHerbert Xu 		skb->len -= skb->data_len;
4127f5572068SHerbert Xu 		skb->data_len = 0;
4128f5572068SHerbert Xu 
4129715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
41305d38a079SHerbert Xu 		goto done;
4131d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
4132d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
4133d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
4134d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
4135d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
4136d7e8883cSEric Dumazet 		unsigned int first_offset;
4137d7e8883cSEric Dumazet 
4138d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
41398a29111cSEric Dumazet 			goto merge;
4140d7e8883cSEric Dumazet 
4141d7e8883cSEric Dumazet 		first_offset = skb->data -
4142d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
4143d7e8883cSEric Dumazet 			       offset;
4144d7e8883cSEric Dumazet 
4145d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4146d7e8883cSEric Dumazet 
4147d8e18a51SMatthew Wilcox (Oracle) 		__skb_frag_set_page(frag, page);
4148b54c9d5bSJonathan Lemon 		skb_frag_off_set(frag, first_offset);
4149d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
4150d7e8883cSEric Dumazet 
4151d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4152d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
4153d7e8883cSEric Dumazet 
4154715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4155d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4156d7e8883cSEric Dumazet 		goto done;
41578a29111cSEric Dumazet 	}
415871d93b39SHerbert Xu 
415971d93b39SHerbert Xu merge:
4160715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
416167147ba9SHerbert Xu 	if (offset > headlen) {
4162d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
4163d1dc7abfSMichal Schmidt 
4164b54c9d5bSJonathan Lemon 		skb_frag_off_add(&skbinfo->frags[0], eat);
41659e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
4166d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
4167d1dc7abfSMichal Schmidt 		skb->len -= eat;
416867147ba9SHerbert Xu 		offset = headlen;
416956035022SHerbert Xu 	}
417056035022SHerbert Xu 
417167147ba9SHerbert Xu 	__skb_pull(skb, offset);
417256035022SHerbert Xu 
417329e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
41748a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
41758a29111cSEric Dumazet 	else
4176c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
4177c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
4178f4a775d1SEric Dumazet 	__skb_header_release(skb);
41798a29111cSEric Dumazet 	lp = p;
418071d93b39SHerbert Xu 
41815d38a079SHerbert Xu done:
41825d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
418337fe4732SHerbert Xu 	p->data_len += len;
4184715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
418537fe4732SHerbert Xu 	p->len += len;
41868a29111cSEric Dumazet 	if (lp != p) {
41878a29111cSEric Dumazet 		lp->data_len += len;
41888a29111cSEric Dumazet 		lp->truesize += delta_truesize;
41898a29111cSEric Dumazet 		lp->len += len;
41908a29111cSEric Dumazet 	}
419171d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
419271d93b39SHerbert Xu 	return 0;
419371d93b39SHerbert Xu }
419471d93b39SHerbert Xu 
4195df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
4196df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE	8
4197df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4198df5042f4SFlorian Westphal 
4199df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = {
4200df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4201df5042f4SFlorian Westphal 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4202df5042f4SFlorian Westphal #endif
42034165079bSFlorian Westphal #ifdef CONFIG_XFRM
42044165079bSFlorian Westphal 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
42054165079bSFlorian Westphal #endif
420695a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
420795a7233cSPaul Blakey 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
420895a7233cSPaul Blakey #endif
42093ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
42103ee17bc7SMat Martineau 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
42113ee17bc7SMat Martineau #endif
4212df5042f4SFlorian Westphal };
4213df5042f4SFlorian Westphal 
4214df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void)
4215df5042f4SFlorian Westphal {
4216df5042f4SFlorian Westphal 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4217df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4218df5042f4SFlorian Westphal 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4219df5042f4SFlorian Westphal #endif
42204165079bSFlorian Westphal #ifdef CONFIG_XFRM
42214165079bSFlorian Westphal 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
42224165079bSFlorian Westphal #endif
422395a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
422495a7233cSPaul Blakey 		skb_ext_type_len[TC_SKB_EXT] +
422595a7233cSPaul Blakey #endif
42263ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
42273ee17bc7SMat Martineau 		skb_ext_type_len[SKB_EXT_MPTCP] +
42283ee17bc7SMat Martineau #endif
4229df5042f4SFlorian Westphal 		0;
4230df5042f4SFlorian Westphal }
4231df5042f4SFlorian Westphal 
4232df5042f4SFlorian Westphal static void skb_extensions_init(void)
4233df5042f4SFlorian Westphal {
4234df5042f4SFlorian Westphal 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4235df5042f4SFlorian Westphal 	BUILD_BUG_ON(skb_ext_total_length() > 255);
4236df5042f4SFlorian Westphal 
4237df5042f4SFlorian Westphal 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4238df5042f4SFlorian Westphal 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4239df5042f4SFlorian Westphal 					     0,
4240df5042f4SFlorian Westphal 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4241df5042f4SFlorian Westphal 					     NULL);
4242df5042f4SFlorian Westphal }
4243df5042f4SFlorian Westphal #else
4244df5042f4SFlorian Westphal static void skb_extensions_init(void) {}
4245df5042f4SFlorian Westphal #endif
4246df5042f4SFlorian Westphal 
42471da177e4SLinus Torvalds void __init skb_init(void)
42481da177e4SLinus Torvalds {
424979a8a642SKees Cook 	skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
42501da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
42511da177e4SLinus Torvalds 					      0,
4252e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
425379a8a642SKees Cook 					      offsetof(struct sk_buff, cb),
425479a8a642SKees Cook 					      sizeof_field(struct sk_buff, cb),
425520c2df83SPaul Mundt 					      NULL);
4256d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4257d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
4258d179cd12SDavid S. Miller 						0,
4259e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
426020c2df83SPaul Mundt 						NULL);
4261df5042f4SFlorian Westphal 	skb_extensions_init();
42621da177e4SLinus Torvalds }
42631da177e4SLinus Torvalds 
426451c739d1SDavid S. Miller static int
426548a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
426648a1df65SJason A. Donenfeld 	       unsigned int recursion_level)
4267716ea3a7SDavid Howells {
42681a028e50SDavid S. Miller 	int start = skb_headlen(skb);
42691a028e50SDavid S. Miller 	int i, copy = start - offset;
4270fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
4271716ea3a7SDavid Howells 	int elt = 0;
4272716ea3a7SDavid Howells 
427348a1df65SJason A. Donenfeld 	if (unlikely(recursion_level >= 24))
427448a1df65SJason A. Donenfeld 		return -EMSGSIZE;
427548a1df65SJason A. Donenfeld 
4276716ea3a7SDavid Howells 	if (copy > 0) {
4277716ea3a7SDavid Howells 		if (copy > len)
4278716ea3a7SDavid Howells 			copy = len;
4279642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
4280716ea3a7SDavid Howells 		elt++;
4281716ea3a7SDavid Howells 		if ((len -= copy) == 0)
4282716ea3a7SDavid Howells 			return elt;
4283716ea3a7SDavid Howells 		offset += copy;
4284716ea3a7SDavid Howells 	}
4285716ea3a7SDavid Howells 
4286716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
42871a028e50SDavid S. Miller 		int end;
4288716ea3a7SDavid Howells 
4289547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
42901a028e50SDavid S. Miller 
42919e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4292716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
4293716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
429448a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
429548a1df65SJason A. Donenfeld 				return -EMSGSIZE;
4296716ea3a7SDavid Howells 
4297716ea3a7SDavid Howells 			if (copy > len)
4298716ea3a7SDavid Howells 				copy = len;
4299ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4300b54c9d5bSJonathan Lemon 				    skb_frag_off(frag) + offset - start);
4301716ea3a7SDavid Howells 			elt++;
4302716ea3a7SDavid Howells 			if (!(len -= copy))
4303716ea3a7SDavid Howells 				return elt;
4304716ea3a7SDavid Howells 			offset += copy;
4305716ea3a7SDavid Howells 		}
43061a028e50SDavid S. Miller 		start = end;
4307716ea3a7SDavid Howells 	}
4308716ea3a7SDavid Howells 
4309fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
431048a1df65SJason A. Donenfeld 		int end, ret;
4311716ea3a7SDavid Howells 
4312547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
43131a028e50SDavid S. Miller 
4314fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
4315716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
431648a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
431748a1df65SJason A. Donenfeld 				return -EMSGSIZE;
431848a1df65SJason A. Donenfeld 
4319716ea3a7SDavid Howells 			if (copy > len)
4320716ea3a7SDavid Howells 				copy = len;
432148a1df65SJason A. Donenfeld 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
432248a1df65SJason A. Donenfeld 					      copy, recursion_level + 1);
432348a1df65SJason A. Donenfeld 			if (unlikely(ret < 0))
432448a1df65SJason A. Donenfeld 				return ret;
432548a1df65SJason A. Donenfeld 			elt += ret;
4326716ea3a7SDavid Howells 			if ((len -= copy) == 0)
4327716ea3a7SDavid Howells 				return elt;
4328716ea3a7SDavid Howells 			offset += copy;
4329716ea3a7SDavid Howells 		}
43301a028e50SDavid S. Miller 		start = end;
4331716ea3a7SDavid Howells 	}
4332716ea3a7SDavid Howells 	BUG_ON(len);
4333716ea3a7SDavid Howells 	return elt;
4334716ea3a7SDavid Howells }
4335716ea3a7SDavid Howells 
433648a1df65SJason A. Donenfeld /**
433748a1df65SJason A. Donenfeld  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
433848a1df65SJason A. Donenfeld  *	@skb: Socket buffer containing the buffers to be mapped
433948a1df65SJason A. Donenfeld  *	@sg: The scatter-gather list to map into
434048a1df65SJason A. Donenfeld  *	@offset: The offset into the buffer's contents to start mapping
434148a1df65SJason A. Donenfeld  *	@len: Length of buffer space to be mapped
434248a1df65SJason A. Donenfeld  *
434348a1df65SJason A. Donenfeld  *	Fill the specified scatter-gather list with mappings/pointers into a
434448a1df65SJason A. Donenfeld  *	region of the buffer space attached to a socket buffer. Returns either
434548a1df65SJason A. Donenfeld  *	the number of scatterlist items used, or -EMSGSIZE if the contents
434648a1df65SJason A. Donenfeld  *	could not fit.
434748a1df65SJason A. Donenfeld  */
434848a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
434948a1df65SJason A. Donenfeld {
435048a1df65SJason A. Donenfeld 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
435148a1df65SJason A. Donenfeld 
435248a1df65SJason A. Donenfeld 	if (nsg <= 0)
435348a1df65SJason A. Donenfeld 		return nsg;
435448a1df65SJason A. Donenfeld 
435548a1df65SJason A. Donenfeld 	sg_mark_end(&sg[nsg - 1]);
435648a1df65SJason A. Donenfeld 
435748a1df65SJason A. Donenfeld 	return nsg;
435848a1df65SJason A. Donenfeld }
435948a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec);
436048a1df65SJason A. Donenfeld 
436125a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
436225a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
436325a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
436425a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
436525a91d8dSFan Du  *
436625a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
436725a91d8dSFan Du  * 1. sg_init_table
436825a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
436925a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
437025a91d8dSFan Du  *
437125a91d8dSFan Du  * This is equivalent to:
437225a91d8dSFan Du  * 1. sg_init_table
437325a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
437425a91d8dSFan Du  * 3. sg_unmark_end
437525a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
437625a91d8dSFan Du  *
437725a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
437825a91d8dSFan Du  * is more preferable.
437925a91d8dSFan Du  */
438025a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
438125a91d8dSFan Du 			int offset, int len)
438225a91d8dSFan Du {
438348a1df65SJason A. Donenfeld 	return __skb_to_sgvec(skb, sg, offset, len, 0);
438425a91d8dSFan Du }
438525a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
438625a91d8dSFan Du 
438751c739d1SDavid S. Miller 
438851c739d1SDavid S. Miller 
4389716ea3a7SDavid Howells /**
4390716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4391716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
4392716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
4393716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4394716ea3a7SDavid Howells  *
4395716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
4396716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
4397716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
4398716ea3a7SDavid Howells  *
4399716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
4400716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
4401716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
4402716ea3a7SDavid Howells  *
4403716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
4404716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
4405716ea3a7SDavid Howells  */
4406716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4407716ea3a7SDavid Howells {
4408716ea3a7SDavid Howells 	int copyflag;
4409716ea3a7SDavid Howells 	int elt;
4410716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
4411716ea3a7SDavid Howells 
4412716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
4413716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
4414716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
4415716ea3a7SDavid Howells 	 */
4416716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4417c15fc199SMiaohe Lin 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4418716ea3a7SDavid Howells 		return -ENOMEM;
4419716ea3a7SDavid Howells 
4420716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
442121dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
4422716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
4423716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
4424716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
4425716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
4426716ea3a7SDavid Howells 
4427716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
4428716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4429716ea3a7SDavid Howells 			return -ENOMEM;
4430716ea3a7SDavid Howells 
4431716ea3a7SDavid Howells 		/* Voila! */
4432716ea3a7SDavid Howells 		*trailer = skb;
4433716ea3a7SDavid Howells 		return 1;
4434716ea3a7SDavid Howells 	}
4435716ea3a7SDavid Howells 
4436716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
4437716ea3a7SDavid Howells 
4438716ea3a7SDavid Howells 	elt = 1;
4439716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
4440716ea3a7SDavid Howells 	copyflag = 0;
4441716ea3a7SDavid Howells 
4442716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
4443716ea3a7SDavid Howells 		int ntail = 0;
4444716ea3a7SDavid Howells 
4445716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
4446716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
4447716ea3a7SDavid Howells 		 * after it. */
4448716ea3a7SDavid Howells 
4449716ea3a7SDavid Howells 		if (skb_shared(skb1))
4450716ea3a7SDavid Howells 			copyflag = 1;
4451716ea3a7SDavid Howells 
4452716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
4453716ea3a7SDavid Howells 
4454716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
4455716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
445621dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
4457716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
4458716ea3a7SDavid Howells 				ntail = tailbits + 128;
4459716ea3a7SDavid Howells 		}
4460716ea3a7SDavid Howells 
4461716ea3a7SDavid Howells 		if (copyflag ||
4462716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
4463716ea3a7SDavid Howells 		    ntail ||
4464716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
446521dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
4466716ea3a7SDavid Howells 			struct sk_buff *skb2;
4467716ea3a7SDavid Howells 
4468716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
4469716ea3a7SDavid Howells 			if (ntail == 0)
4470716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
4471716ea3a7SDavid Howells 			else
4472716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
4473716ea3a7SDavid Howells 						       skb_headroom(skb1),
4474716ea3a7SDavid Howells 						       ntail,
4475716ea3a7SDavid Howells 						       GFP_ATOMIC);
4476716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
4477716ea3a7SDavid Howells 				return -ENOMEM;
4478716ea3a7SDavid Howells 
4479716ea3a7SDavid Howells 			if (skb1->sk)
4480716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
4481716ea3a7SDavid Howells 
4482716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
4483716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
4484716ea3a7SDavid Howells 
4485716ea3a7SDavid Howells 			skb2->next = skb1->next;
4486716ea3a7SDavid Howells 			*skb_p = skb2;
4487716ea3a7SDavid Howells 			kfree_skb(skb1);
4488716ea3a7SDavid Howells 			skb1 = skb2;
4489716ea3a7SDavid Howells 		}
4490716ea3a7SDavid Howells 		elt++;
4491716ea3a7SDavid Howells 		*trailer = skb1;
4492716ea3a7SDavid Howells 		skb_p = &skb1->next;
4493716ea3a7SDavid Howells 	}
4494716ea3a7SDavid Howells 
4495716ea3a7SDavid Howells 	return elt;
4496716ea3a7SDavid Howells }
4497b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
4498716ea3a7SDavid Howells 
4499b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
4500b1faf566SEric Dumazet {
4501b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
4502b1faf566SEric Dumazet 
4503b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4504b1faf566SEric Dumazet }
4505b1faf566SEric Dumazet 
45068605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
45078605330aSSoheil Hassas Yeganeh {
45088605330aSSoheil Hassas Yeganeh 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
45098605330aSSoheil Hassas Yeganeh 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
45108605330aSSoheil Hassas Yeganeh 	 */
45118605330aSSoheil Hassas Yeganeh 	skb->pkt_type = PACKET_OUTGOING;
45128605330aSSoheil Hassas Yeganeh 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
45138605330aSSoheil Hassas Yeganeh }
45148605330aSSoheil Hassas Yeganeh 
4515b1faf566SEric Dumazet /*
4516b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4517b1faf566SEric Dumazet  */
4518b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4519b1faf566SEric Dumazet {
4520b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4521ebb3b78dSEric Dumazet 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4522b1faf566SEric Dumazet 		return -ENOMEM;
4523b1faf566SEric Dumazet 
4524b1faf566SEric Dumazet 	skb_orphan(skb);
4525b1faf566SEric Dumazet 	skb->sk = sk;
4526b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
4527b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
45288605330aSSoheil Hassas Yeganeh 	skb_set_err_queue(skb);
4529b1faf566SEric Dumazet 
4530abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
4531abb57ea4SEric Dumazet 	skb_dst_force(skb);
4532abb57ea4SEric Dumazet 
4533b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
4534b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
45356e5d58fdSVinicius Costa Gomes 		sk->sk_error_report(sk);
4536b1faf566SEric Dumazet 	return 0;
4537b1faf566SEric Dumazet }
4538b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
4539b1faf566SEric Dumazet 
454083a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
454183a1a1a7SSoheil Hassas Yeganeh {
454283a1a1a7SSoheil Hassas Yeganeh 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
454383a1a1a7SSoheil Hassas Yeganeh 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
454483a1a1a7SSoheil Hassas Yeganeh }
454583a1a1a7SSoheil Hassas Yeganeh 
4546364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4547364a9e93SWillem de Bruijn {
4548364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
454983a1a1a7SSoheil Hassas Yeganeh 	struct sk_buff *skb, *skb_next = NULL;
455083a1a1a7SSoheil Hassas Yeganeh 	bool icmp_next = false;
4551997d5c3fSEric Dumazet 	unsigned long flags;
4552364a9e93SWillem de Bruijn 
4553997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
4554364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
455538b25793SSoheil Hassas Yeganeh 	if (skb && (skb_next = skb_peek(q))) {
455683a1a1a7SSoheil Hassas Yeganeh 		icmp_next = is_icmp_err_skb(skb_next);
455738b25793SSoheil Hassas Yeganeh 		if (icmp_next)
455838b25793SSoheil Hassas Yeganeh 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
455938b25793SSoheil Hassas Yeganeh 	}
4560997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
4561364a9e93SWillem de Bruijn 
456283a1a1a7SSoheil Hassas Yeganeh 	if (is_icmp_err_skb(skb) && !icmp_next)
456383a1a1a7SSoheil Hassas Yeganeh 		sk->sk_err = 0;
456483a1a1a7SSoheil Hassas Yeganeh 
456583a1a1a7SSoheil Hassas Yeganeh 	if (skb_next)
4566364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
4567364a9e93SWillem de Bruijn 
4568364a9e93SWillem de Bruijn 	return skb;
4569364a9e93SWillem de Bruijn }
4570364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
4571364a9e93SWillem de Bruijn 
4572cab41c47SAlexander Duyck /**
4573cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
4574cab41c47SAlexander Duyck  * @skb: the skb to clone
4575cab41c47SAlexander Duyck  *
4576cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
4577cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
4578cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
4579cab41c47SAlexander Duyck  *
4580cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
4581cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
4582cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
4583cab41c47SAlexander Duyck  * the sk_error_queue.
4584cab41c47SAlexander Duyck  */
458562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
458662bccb8cSAlexander Duyck {
458762bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
458862bccb8cSAlexander Duyck 	struct sk_buff *clone;
458962bccb8cSAlexander Duyck 
459041c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
459162bccb8cSAlexander Duyck 		return NULL;
459262bccb8cSAlexander Duyck 
459362bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
459462bccb8cSAlexander Duyck 	if (!clone) {
459562bccb8cSAlexander Duyck 		sock_put(sk);
459662bccb8cSAlexander Duyck 		return NULL;
459762bccb8cSAlexander Duyck 	}
459862bccb8cSAlexander Duyck 
459962bccb8cSAlexander Duyck 	clone->sk = sk;
460062bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
460162bccb8cSAlexander Duyck 
460262bccb8cSAlexander Duyck 	return clone;
460362bccb8cSAlexander Duyck }
460462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
460562bccb8cSAlexander Duyck 
460637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
460737846ef0SAlexander Duyck 					struct sock *sk,
46084ef1b286SSoheil Hassas Yeganeh 					int tstype,
46094ef1b286SSoheil Hassas Yeganeh 					bool opt_stats)
4610ac45f602SPatrick Ohly {
4611ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
4612ac45f602SPatrick Ohly 	int err;
4613ac45f602SPatrick Ohly 
46144ef1b286SSoheil Hassas Yeganeh 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
46154ef1b286SSoheil Hassas Yeganeh 
4616ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
4617ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
4618ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
4619ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4620e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
46214ef1b286SSoheil Hassas Yeganeh 	serr->opt_stats = opt_stats;
46221862d620SWillem de Bruijn 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
46234ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
462409c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
4625ac5cc977SWANG Cong 		if (sk->sk_protocol == IPPROTO_TCP &&
4626ac5cc977SWANG Cong 		    sk->sk_type == SOCK_STREAM)
46274ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
46284ed2d765SWillem de Bruijn 	}
462929030374SEric Dumazet 
4630ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
463129030374SEric Dumazet 
4632ac45f602SPatrick Ohly 	if (err)
4633ac45f602SPatrick Ohly 		kfree_skb(skb);
4634ac45f602SPatrick Ohly }
463537846ef0SAlexander Duyck 
4636b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4637b245be1fSWillem de Bruijn {
4638b245be1fSWillem de Bruijn 	bool ret;
4639b245be1fSWillem de Bruijn 
4640b245be1fSWillem de Bruijn 	if (likely(sysctl_tstamp_allow_data || tsonly))
4641b245be1fSWillem de Bruijn 		return true;
4642b245be1fSWillem de Bruijn 
4643b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
4644b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
4645b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4646b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
4647b245be1fSWillem de Bruijn 	return ret;
4648b245be1fSWillem de Bruijn }
4649b245be1fSWillem de Bruijn 
465037846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
465137846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
465237846ef0SAlexander Duyck {
465337846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
465437846ef0SAlexander Duyck 
4655b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
465635b99dffSWillem de Bruijn 		goto err;
4657b245be1fSWillem de Bruijn 
46589ac25fc0SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
46599ac25fc0SEric Dumazet 	 * but only if the socket refcount is not zero.
46609ac25fc0SEric Dumazet 	 */
466141c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
466237846ef0SAlexander Duyck 		*skb_hwtstamps(skb) = *hwtstamps;
46634ef1b286SSoheil Hassas Yeganeh 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
466437846ef0SAlexander Duyck 		sock_put(sk);
466535b99dffSWillem de Bruijn 		return;
466637846ef0SAlexander Duyck 	}
466735b99dffSWillem de Bruijn 
466835b99dffSWillem de Bruijn err:
466935b99dffSWillem de Bruijn 	kfree_skb(skb);
46709ac25fc0SEric Dumazet }
467137846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
467237846ef0SAlexander Duyck 
467337846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
467437846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
467537846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
467637846ef0SAlexander Duyck {
467737846ef0SAlexander Duyck 	struct sk_buff *skb;
46784ef1b286SSoheil Hassas Yeganeh 	bool tsonly, opt_stats = false;
467937846ef0SAlexander Duyck 
46803a8dd971SWillem de Bruijn 	if (!sk)
46813a8dd971SWillem de Bruijn 		return;
46823a8dd971SWillem de Bruijn 
4683b50a5c70SMiroslav Lichvar 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4684b50a5c70SMiroslav Lichvar 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4685b50a5c70SMiroslav Lichvar 		return;
4686b50a5c70SMiroslav Lichvar 
46873a8dd971SWillem de Bruijn 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
46883a8dd971SWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, tsonly))
468937846ef0SAlexander Duyck 		return;
469037846ef0SAlexander Duyck 
46911c885808SFrancis Yan 	if (tsonly) {
46921c885808SFrancis Yan #ifdef CONFIG_INET
46931c885808SFrancis Yan 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
46941c885808SFrancis Yan 		    sk->sk_protocol == IPPROTO_TCP &&
46954ef1b286SSoheil Hassas Yeganeh 		    sk->sk_type == SOCK_STREAM) {
469648040793SYousuk Seung 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb);
46974ef1b286SSoheil Hassas Yeganeh 			opt_stats = true;
46984ef1b286SSoheil Hassas Yeganeh 		} else
46991c885808SFrancis Yan #endif
47001c885808SFrancis Yan 			skb = alloc_skb(0, GFP_ATOMIC);
47011c885808SFrancis Yan 	} else {
470237846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
47031c885808SFrancis Yan 	}
470437846ef0SAlexander Duyck 	if (!skb)
470537846ef0SAlexander Duyck 		return;
470637846ef0SAlexander Duyck 
470749ca0d8bSWillem de Bruijn 	if (tsonly) {
4708fff88030SWillem de Bruijn 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4709fff88030SWillem de Bruijn 					     SKBTX_ANY_TSTAMP;
471049ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
471149ca0d8bSWillem de Bruijn 	}
471249ca0d8bSWillem de Bruijn 
471349ca0d8bSWillem de Bruijn 	if (hwtstamps)
471449ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
471549ca0d8bSWillem de Bruijn 	else
471649ca0d8bSWillem de Bruijn 		skb->tstamp = ktime_get_real();
471749ca0d8bSWillem de Bruijn 
47184ef1b286SSoheil Hassas Yeganeh 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
471937846ef0SAlexander Duyck }
4720e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4721e7fd2885SWillem de Bruijn 
4722e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
4723e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
4724e7fd2885SWillem de Bruijn {
4725e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4726e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
4727e7fd2885SWillem de Bruijn }
4728ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4729ac45f602SPatrick Ohly 
47306e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
47316e3e939fSJohannes Berg {
47326e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
47336e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
4734dd4f1072SEric Dumazet 	int err = 1;
47356e3e939fSJohannes Berg 
47366e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
47376e3e939fSJohannes Berg 	skb->wifi_acked = acked;
47386e3e939fSJohannes Berg 
47396e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
47406e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
47416e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
47426e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
47436e3e939fSJohannes Berg 
4744dd4f1072SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
4745dd4f1072SEric Dumazet 	 * but only if the socket refcount is not zero.
4746dd4f1072SEric Dumazet 	 */
474741c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
47486e3e939fSJohannes Berg 		err = sock_queue_err_skb(sk, skb);
4749dd4f1072SEric Dumazet 		sock_put(sk);
4750dd4f1072SEric Dumazet 	}
47516e3e939fSJohannes Berg 	if (err)
47526e3e939fSJohannes Berg 		kfree_skb(skb);
47536e3e939fSJohannes Berg }
47546e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
47556e3e939fSJohannes Berg 
4756f35d9d8aSRusty Russell /**
4757f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
4758f35d9d8aSRusty Russell  * @skb: the skb to set
4759f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
4760f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
4761f35d9d8aSRusty Russell  *
4762f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
4763f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4764f35d9d8aSRusty Russell  *
4765f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
4766f35d9d8aSRusty Russell  * returns false you should drop the packet.
4767f35d9d8aSRusty Russell  */
4768f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4769f35d9d8aSRusty Russell {
477052b5d6f5SEric Dumazet 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
477152b5d6f5SEric Dumazet 	u32 csum_start = skb_headroom(skb) + (u32)start;
477252b5d6f5SEric Dumazet 
477352b5d6f5SEric Dumazet 	if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
477452b5d6f5SEric Dumazet 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
477552b5d6f5SEric Dumazet 				     start, off, skb_headroom(skb), skb_headlen(skb));
4776f35d9d8aSRusty Russell 		return false;
4777f35d9d8aSRusty Russell 	}
4778f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
477952b5d6f5SEric Dumazet 	skb->csum_start = csum_start;
4780f35d9d8aSRusty Russell 	skb->csum_offset = off;
4781e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
4782f35d9d8aSRusty Russell 	return true;
4783f35d9d8aSRusty Russell }
4784b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4785f35d9d8aSRusty Russell 
4786ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4787ed1f50c3SPaul Durrant 			       unsigned int max)
4788ed1f50c3SPaul Durrant {
4789ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
4790ed1f50c3SPaul Durrant 		return 0;
4791ed1f50c3SPaul Durrant 
4792ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
4793ed1f50c3SPaul Durrant 	 * won't need to do it again.
4794ed1f50c3SPaul Durrant 	 */
4795ed1f50c3SPaul Durrant 	if (max > skb->len)
4796ed1f50c3SPaul Durrant 		max = skb->len;
4797ed1f50c3SPaul Durrant 
4798ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4799ed1f50c3SPaul Durrant 		return -ENOMEM;
4800ed1f50c3SPaul Durrant 
4801ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
4802ed1f50c3SPaul Durrant 		return -EPROTO;
4803ed1f50c3SPaul Durrant 
4804ed1f50c3SPaul Durrant 	return 0;
4805ed1f50c3SPaul Durrant }
4806ed1f50c3SPaul Durrant 
4807f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
4808f9708b43SJan Beulich 
4809f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4810f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
4811f9708b43SJan Beulich 				      unsigned int off)
4812f9708b43SJan Beulich {
4813f9708b43SJan Beulich 	int err;
4814f9708b43SJan Beulich 
4815161d1792SKees Cook 	switch (proto) {
4816f9708b43SJan Beulich 	case IPPROTO_TCP:
4817f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4818f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
4819f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4820f9708b43SJan Beulich 						  offsetof(struct tcphdr,
4821f9708b43SJan Beulich 							   check)))
4822f9708b43SJan Beulich 			err = -EPROTO;
4823f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4824f9708b43SJan Beulich 
4825f9708b43SJan Beulich 	case IPPROTO_UDP:
4826f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4827f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
4828f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4829f9708b43SJan Beulich 						  offsetof(struct udphdr,
4830f9708b43SJan Beulich 							   check)))
4831f9708b43SJan Beulich 			err = -EPROTO;
4832f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4833f9708b43SJan Beulich 	}
4834f9708b43SJan Beulich 
4835f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
4836f9708b43SJan Beulich }
4837f9708b43SJan Beulich 
4838ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4839ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
4840ed1f50c3SPaul Durrant  */
4841ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
4842ed1f50c3SPaul Durrant 
4843f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4844ed1f50c3SPaul Durrant {
4845ed1f50c3SPaul Durrant 	unsigned int off;
4846ed1f50c3SPaul Durrant 	bool fragment;
4847f9708b43SJan Beulich 	__sum16 *csum;
4848ed1f50c3SPaul Durrant 	int err;
4849ed1f50c3SPaul Durrant 
4850ed1f50c3SPaul Durrant 	fragment = false;
4851ed1f50c3SPaul Durrant 
4852ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
4853ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
4854ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
4855ed1f50c3SPaul Durrant 	if (err < 0)
4856ed1f50c3SPaul Durrant 		goto out;
4857ed1f50c3SPaul Durrant 
485811f920d2SMiaohe Lin 	if (ip_is_fragment(ip_hdr(skb)))
4859ed1f50c3SPaul Durrant 		fragment = true;
4860ed1f50c3SPaul Durrant 
4861ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
4862ed1f50c3SPaul Durrant 
4863ed1f50c3SPaul Durrant 	err = -EPROTO;
4864ed1f50c3SPaul Durrant 
4865ed1f50c3SPaul Durrant 	if (fragment)
4866ed1f50c3SPaul Durrant 		goto out;
4867ed1f50c3SPaul Durrant 
4868f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4869f9708b43SJan Beulich 	if (IS_ERR(csum))
4870f9708b43SJan Beulich 		return PTR_ERR(csum);
4871ed1f50c3SPaul Durrant 
4872ed1f50c3SPaul Durrant 	if (recalculate)
4873f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4874ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
4875ed1f50c3SPaul Durrant 					   skb->len - off,
4876f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
4877ed1f50c3SPaul Durrant 	err = 0;
4878ed1f50c3SPaul Durrant 
4879ed1f50c3SPaul Durrant out:
4880ed1f50c3SPaul Durrant 	return err;
4881ed1f50c3SPaul Durrant }
4882ed1f50c3SPaul Durrant 
4883ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4884ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
4885ed1f50c3SPaul Durrant  */
4886ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
4887ed1f50c3SPaul Durrant 
4888ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
4889ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
4890ed1f50c3SPaul Durrant 
4891ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4892ed1f50c3SPaul Durrant {
4893ed1f50c3SPaul Durrant 	int err;
4894ed1f50c3SPaul Durrant 	u8 nexthdr;
4895ed1f50c3SPaul Durrant 	unsigned int off;
4896ed1f50c3SPaul Durrant 	unsigned int len;
4897ed1f50c3SPaul Durrant 	bool fragment;
4898ed1f50c3SPaul Durrant 	bool done;
4899f9708b43SJan Beulich 	__sum16 *csum;
4900ed1f50c3SPaul Durrant 
4901ed1f50c3SPaul Durrant 	fragment = false;
4902ed1f50c3SPaul Durrant 	done = false;
4903ed1f50c3SPaul Durrant 
4904ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
4905ed1f50c3SPaul Durrant 
4906ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4907ed1f50c3SPaul Durrant 	if (err < 0)
4908ed1f50c3SPaul Durrant 		goto out;
4909ed1f50c3SPaul Durrant 
4910ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
4911ed1f50c3SPaul Durrant 
4912ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4913ed1f50c3SPaul Durrant 	while (off <= len && !done) {
4914ed1f50c3SPaul Durrant 		switch (nexthdr) {
4915ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
4916ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
4917ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
4918ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
4919ed1f50c3SPaul Durrant 
4920ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4921ed1f50c3SPaul Durrant 						  off +
4922ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
4923ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4924ed1f50c3SPaul Durrant 			if (err < 0)
4925ed1f50c3SPaul Durrant 				goto out;
4926ed1f50c3SPaul Durrant 
4927ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4928ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4929ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
4930ed1f50c3SPaul Durrant 			break;
4931ed1f50c3SPaul Durrant 		}
4932ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
4933ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
4934ed1f50c3SPaul Durrant 
4935ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4936ed1f50c3SPaul Durrant 						  off +
4937ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
4938ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4939ed1f50c3SPaul Durrant 			if (err < 0)
4940ed1f50c3SPaul Durrant 				goto out;
4941ed1f50c3SPaul Durrant 
4942ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4943ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4944ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
4945ed1f50c3SPaul Durrant 			break;
4946ed1f50c3SPaul Durrant 		}
4947ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
4948ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
4949ed1f50c3SPaul Durrant 
4950ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4951ed1f50c3SPaul Durrant 						  off +
4952ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
4953ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4954ed1f50c3SPaul Durrant 			if (err < 0)
4955ed1f50c3SPaul Durrant 				goto out;
4956ed1f50c3SPaul Durrant 
4957ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
4958ed1f50c3SPaul Durrant 
4959ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4960ed1f50c3SPaul Durrant 				fragment = true;
4961ed1f50c3SPaul Durrant 
4962ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4963ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
4964ed1f50c3SPaul Durrant 			break;
4965ed1f50c3SPaul Durrant 		}
4966ed1f50c3SPaul Durrant 		default:
4967ed1f50c3SPaul Durrant 			done = true;
4968ed1f50c3SPaul Durrant 			break;
4969ed1f50c3SPaul Durrant 		}
4970ed1f50c3SPaul Durrant 	}
4971ed1f50c3SPaul Durrant 
4972ed1f50c3SPaul Durrant 	err = -EPROTO;
4973ed1f50c3SPaul Durrant 
4974ed1f50c3SPaul Durrant 	if (!done || fragment)
4975ed1f50c3SPaul Durrant 		goto out;
4976ed1f50c3SPaul Durrant 
4977f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
4978f9708b43SJan Beulich 	if (IS_ERR(csum))
4979f9708b43SJan Beulich 		return PTR_ERR(csum);
4980ed1f50c3SPaul Durrant 
4981ed1f50c3SPaul Durrant 	if (recalculate)
4982f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4983ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
4984f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
4985ed1f50c3SPaul Durrant 	err = 0;
4986ed1f50c3SPaul Durrant 
4987ed1f50c3SPaul Durrant out:
4988ed1f50c3SPaul Durrant 	return err;
4989ed1f50c3SPaul Durrant }
4990ed1f50c3SPaul Durrant 
4991ed1f50c3SPaul Durrant /**
4992ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
4993ed1f50c3SPaul Durrant  * @skb: the skb to set up
4994ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
4995ed1f50c3SPaul Durrant  */
4996ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4997ed1f50c3SPaul Durrant {
4998ed1f50c3SPaul Durrant 	int err;
4999ed1f50c3SPaul Durrant 
5000ed1f50c3SPaul Durrant 	switch (skb->protocol) {
5001ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
5002f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
5003ed1f50c3SPaul Durrant 		break;
5004ed1f50c3SPaul Durrant 
5005ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
5006ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
5007ed1f50c3SPaul Durrant 		break;
5008ed1f50c3SPaul Durrant 
5009ed1f50c3SPaul Durrant 	default:
5010ed1f50c3SPaul Durrant 		err = -EPROTO;
5011ed1f50c3SPaul Durrant 		break;
5012ed1f50c3SPaul Durrant 	}
5013ed1f50c3SPaul Durrant 
5014ed1f50c3SPaul Durrant 	return err;
5015ed1f50c3SPaul Durrant }
5016ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
5017ed1f50c3SPaul Durrant 
50189afd85c9SLinus Lüssing /**
50199afd85c9SLinus Lüssing  * skb_checksum_maybe_trim - maybe trims the given skb
50209afd85c9SLinus Lüssing  * @skb: the skb to check
50219afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
50229afd85c9SLinus Lüssing  *
50239afd85c9SLinus Lüssing  * Checks whether the given skb has data beyond the given transport length.
50249afd85c9SLinus Lüssing  * If so, returns a cloned skb trimmed to this transport length.
50259afd85c9SLinus Lüssing  * Otherwise returns the provided skb. Returns NULL in error cases
50269afd85c9SLinus Lüssing  * (e.g. transport_len exceeds skb length or out-of-memory).
50279afd85c9SLinus Lüssing  *
5028a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5029a516993fSLinus Lüssing  * differs from the provided skb.
50309afd85c9SLinus Lüssing  */
50319afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
50329afd85c9SLinus Lüssing 					       unsigned int transport_len)
50339afd85c9SLinus Lüssing {
50349afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
50359afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + transport_len;
50369afd85c9SLinus Lüssing 	int ret;
50379afd85c9SLinus Lüssing 
5038a516993fSLinus Lüssing 	if (skb->len < len)
50399afd85c9SLinus Lüssing 		return NULL;
5040a516993fSLinus Lüssing 	else if (skb->len == len)
50419afd85c9SLinus Lüssing 		return skb;
50429afd85c9SLinus Lüssing 
50439afd85c9SLinus Lüssing 	skb_chk = skb_clone(skb, GFP_ATOMIC);
50449afd85c9SLinus Lüssing 	if (!skb_chk)
50459afd85c9SLinus Lüssing 		return NULL;
50469afd85c9SLinus Lüssing 
50479afd85c9SLinus Lüssing 	ret = pskb_trim_rcsum(skb_chk, len);
50489afd85c9SLinus Lüssing 	if (ret) {
50499afd85c9SLinus Lüssing 		kfree_skb(skb_chk);
50509afd85c9SLinus Lüssing 		return NULL;
50519afd85c9SLinus Lüssing 	}
50529afd85c9SLinus Lüssing 
50539afd85c9SLinus Lüssing 	return skb_chk;
50549afd85c9SLinus Lüssing }
50559afd85c9SLinus Lüssing 
50569afd85c9SLinus Lüssing /**
50579afd85c9SLinus Lüssing  * skb_checksum_trimmed - validate checksum of an skb
50589afd85c9SLinus Lüssing  * @skb: the skb to check
50599afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
50609afd85c9SLinus Lüssing  * @skb_chkf: checksum function to use
50619afd85c9SLinus Lüssing  *
50629afd85c9SLinus Lüssing  * Applies the given checksum function skb_chkf to the provided skb.
50639afd85c9SLinus Lüssing  * Returns a checked and maybe trimmed skb. Returns NULL on error.
50649afd85c9SLinus Lüssing  *
50659afd85c9SLinus Lüssing  * If the skb has data beyond the given transport length, then a
50669afd85c9SLinus Lüssing  * trimmed & cloned skb is checked and returned.
50679afd85c9SLinus Lüssing  *
5068a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
5069a516993fSLinus Lüssing  * differs from the provided skb.
50709afd85c9SLinus Lüssing  */
50719afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
50729afd85c9SLinus Lüssing 				     unsigned int transport_len,
50739afd85c9SLinus Lüssing 				     __sum16(*skb_chkf)(struct sk_buff *skb))
50749afd85c9SLinus Lüssing {
50759afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
50769afd85c9SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
5077fcba67c9SLinus Lüssing 	__sum16 ret;
50789afd85c9SLinus Lüssing 
50799afd85c9SLinus Lüssing 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
50809afd85c9SLinus Lüssing 	if (!skb_chk)
5081a516993fSLinus Lüssing 		goto err;
50829afd85c9SLinus Lüssing 
5083a516993fSLinus Lüssing 	if (!pskb_may_pull(skb_chk, offset))
5084a516993fSLinus Lüssing 		goto err;
50859afd85c9SLinus Lüssing 
50869b368814SLinus Lüssing 	skb_pull_rcsum(skb_chk, offset);
50879afd85c9SLinus Lüssing 	ret = skb_chkf(skb_chk);
50889b368814SLinus Lüssing 	skb_push_rcsum(skb_chk, offset);
50899afd85c9SLinus Lüssing 
5090a516993fSLinus Lüssing 	if (ret)
5091a516993fSLinus Lüssing 		goto err;
50929afd85c9SLinus Lüssing 
50939afd85c9SLinus Lüssing 	return skb_chk;
5094a516993fSLinus Lüssing 
5095a516993fSLinus Lüssing err:
5096a516993fSLinus Lüssing 	if (skb_chk && skb_chk != skb)
5097a516993fSLinus Lüssing 		kfree_skb(skb_chk);
5098a516993fSLinus Lüssing 
5099a516993fSLinus Lüssing 	return NULL;
5100a516993fSLinus Lüssing 
51019afd85c9SLinus Lüssing }
51029afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
51039afd85c9SLinus Lüssing 
51044497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
51054497b076SBen Hutchings {
5106e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5107e87cc472SJoe Perches 			     skb->dev->name);
51084497b076SBen Hutchings }
51094497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5110bad43ca8SEric Dumazet 
5111bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5112bad43ca8SEric Dumazet {
51133d861f66SEric Dumazet 	if (head_stolen) {
51143d861f66SEric Dumazet 		skb_release_head_state(skb);
5115bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
51163d861f66SEric Dumazet 	} else {
5117bad43ca8SEric Dumazet 		__kfree_skb(skb);
5118bad43ca8SEric Dumazet 	}
51193d861f66SEric Dumazet }
5120bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
5121bad43ca8SEric Dumazet 
5122bad43ca8SEric Dumazet /**
5123bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
5124bad43ca8SEric Dumazet  * @to: prior buffer
5125bad43ca8SEric Dumazet  * @from: buffer to add
5126bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
5127c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
5128bad43ca8SEric Dumazet  */
5129bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5130bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
5131bad43ca8SEric Dumazet {
5132c818fa9eSEric Dumazet 	struct skb_shared_info *to_shinfo, *from_shinfo;
5133bad43ca8SEric Dumazet 	int i, delta, len = from->len;
5134bad43ca8SEric Dumazet 
5135bad43ca8SEric Dumazet 	*fragstolen = false;
5136bad43ca8SEric Dumazet 
5137bad43ca8SEric Dumazet 	if (skb_cloned(to))
5138bad43ca8SEric Dumazet 		return false;
5139bad43ca8SEric Dumazet 
5140bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
5141e93a0435SEric Dumazet 		if (len)
5142bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5143bad43ca8SEric Dumazet 		*delta_truesize = 0;
5144bad43ca8SEric Dumazet 		return true;
5145bad43ca8SEric Dumazet 	}
5146bad43ca8SEric Dumazet 
5147c818fa9eSEric Dumazet 	to_shinfo = skb_shinfo(to);
5148c818fa9eSEric Dumazet 	from_shinfo = skb_shinfo(from);
5149c818fa9eSEric Dumazet 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5150bad43ca8SEric Dumazet 		return false;
51511f8b977aSWillem de Bruijn 	if (skb_zcopy(to) || skb_zcopy(from))
51521f8b977aSWillem de Bruijn 		return false;
5153bad43ca8SEric Dumazet 
5154bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
5155bad43ca8SEric Dumazet 		struct page *page;
5156bad43ca8SEric Dumazet 		unsigned int offset;
5157bad43ca8SEric Dumazet 
5158c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5159c818fa9eSEric Dumazet 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5160bad43ca8SEric Dumazet 			return false;
5161bad43ca8SEric Dumazet 
5162bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
5163bad43ca8SEric Dumazet 			return false;
5164bad43ca8SEric Dumazet 
5165bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5166bad43ca8SEric Dumazet 
5167bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
5168bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
5169bad43ca8SEric Dumazet 
5170c818fa9eSEric Dumazet 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5171bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
5172bad43ca8SEric Dumazet 		*fragstolen = true;
5173bad43ca8SEric Dumazet 	} else {
5174c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
5175c818fa9eSEric Dumazet 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5176bad43ca8SEric Dumazet 			return false;
5177bad43ca8SEric Dumazet 
5178f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5179bad43ca8SEric Dumazet 	}
5180bad43ca8SEric Dumazet 
5181bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
5182bad43ca8SEric Dumazet 
5183c818fa9eSEric Dumazet 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5184c818fa9eSEric Dumazet 	       from_shinfo->frags,
5185c818fa9eSEric Dumazet 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5186c818fa9eSEric Dumazet 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5187bad43ca8SEric Dumazet 
5188bad43ca8SEric Dumazet 	if (!skb_cloned(from))
5189c818fa9eSEric Dumazet 		from_shinfo->nr_frags = 0;
5190bad43ca8SEric Dumazet 
51918ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
51928ea853fdSLi RongQing 	 * since we set nr_frags to 0.
51938ea853fdSLi RongQing 	 */
5194c818fa9eSEric Dumazet 	for (i = 0; i < from_shinfo->nr_frags; i++)
5195c818fa9eSEric Dumazet 		__skb_frag_ref(&from_shinfo->frags[i]);
5196bad43ca8SEric Dumazet 
5197bad43ca8SEric Dumazet 	to->truesize += delta;
5198bad43ca8SEric Dumazet 	to->len += len;
5199bad43ca8SEric Dumazet 	to->data_len += len;
5200bad43ca8SEric Dumazet 
5201bad43ca8SEric Dumazet 	*delta_truesize = delta;
5202bad43ca8SEric Dumazet 	return true;
5203bad43ca8SEric Dumazet }
5204bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
5205621e84d6SNicolas Dichtel 
5206621e84d6SNicolas Dichtel /**
52078b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
5208621e84d6SNicolas Dichtel  *
5209621e84d6SNicolas Dichtel  * @skb: buffer to clean
52108b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
5211621e84d6SNicolas Dichtel  *
52128b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
52138b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
52148b27f277SNicolas Dichtel  * operations.
52158b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
52168b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
52178b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
5218621e84d6SNicolas Dichtel  */
52198b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5220621e84d6SNicolas Dichtel {
5221621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
5222621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
522360ff7467SWANG Cong 	skb->ignore_df = 0;
5224621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
5225174e2381SFlorian Westphal 	skb_ext_reset(skb);
5226895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
5227621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
5228213dd74aSHerbert Xu 
52296f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV
52306f9a5069SPetr Machata 	skb->offload_fwd_mark = 0;
5231875e8939SIdo Schimmel 	skb->offload_l3_fwd_mark = 0;
52326f9a5069SPetr Machata #endif
52336f9a5069SPetr Machata 
5234213dd74aSHerbert Xu 	if (!xnet)
5235213dd74aSHerbert Xu 		return;
5236213dd74aSHerbert Xu 
52372b5ec1a5SYe Yin 	ipvs_reset(skb);
5238213dd74aSHerbert Xu 	skb->mark = 0;
5239c47d8c2fSJesus Sanchez-Palencia 	skb->tstamp = 0;
5240621e84d6SNicolas Dichtel }
5241621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
5242de960aa9SFlorian Westphal 
5243de960aa9SFlorian Westphal /**
5244de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5245de960aa9SFlorian Westphal  *
5246de960aa9SFlorian Westphal  * @skb: GSO skb
5247de960aa9SFlorian Westphal  *
5248de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
5249de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
5250de960aa9SFlorian Westphal  *
5251de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5252de960aa9SFlorian Westphal  */
5253a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5254de960aa9SFlorian Westphal {
5255de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5256f993bc25SFlorian Westphal 	unsigned int thlen = 0;
5257f993bc25SFlorian Westphal 
5258f993bc25SFlorian Westphal 	if (skb->encapsulation) {
5259f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
5260f993bc25SFlorian Westphal 			skb_transport_header(skb);
5261de960aa9SFlorian Westphal 
5262de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5263f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
5264f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5265f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
52661dd27cdeSDaniel Axtens 	} else if (unlikely(skb_is_gso_sctp(skb))) {
526790017accSMarcelo Ricardo Leitner 		thlen = sizeof(struct sctphdr);
5268ee80d1ebSWillem de Bruijn 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5269ee80d1ebSWillem de Bruijn 		thlen = sizeof(struct udphdr);
5270f993bc25SFlorian Westphal 	}
52716d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
52726d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
52736d39d589SFlorian Westphal 	 * accounted for.
52746d39d589SFlorian Westphal 	 */
5275f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
5276de960aa9SFlorian Westphal }
5277a4a77718SDaniel Axtens 
5278a4a77718SDaniel Axtens /**
5279a4a77718SDaniel Axtens  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5280a4a77718SDaniel Axtens  *
5281a4a77718SDaniel Axtens  * @skb: GSO skb
5282a4a77718SDaniel Axtens  *
5283a4a77718SDaniel Axtens  * skb_gso_network_seglen is used to determine the real size of the
5284a4a77718SDaniel Axtens  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5285a4a77718SDaniel Axtens  *
5286a4a77718SDaniel Axtens  * The MAC/L2 header is not accounted for.
5287a4a77718SDaniel Axtens  */
5288a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5289a4a77718SDaniel Axtens {
5290a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) -
5291a4a77718SDaniel Axtens 			       skb_network_header(skb);
5292a4a77718SDaniel Axtens 
5293a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5294a4a77718SDaniel Axtens }
5295a4a77718SDaniel Axtens 
5296a4a77718SDaniel Axtens /**
5297a4a77718SDaniel Axtens  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5298a4a77718SDaniel Axtens  *
5299a4a77718SDaniel Axtens  * @skb: GSO skb
5300a4a77718SDaniel Axtens  *
5301a4a77718SDaniel Axtens  * skb_gso_mac_seglen is used to determine the real size of the
5302a4a77718SDaniel Axtens  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5303a4a77718SDaniel Axtens  * headers (TCP/UDP).
5304a4a77718SDaniel Axtens  */
5305a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5306a4a77718SDaniel Axtens {
5307a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5308a4a77718SDaniel Axtens 
5309a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5310a4a77718SDaniel Axtens }
53110d5501c1SVlad Yasevich 
5312ae7ef81eSMarcelo Ricardo Leitner /**
53132b16f048SDaniel Axtens  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
53142b16f048SDaniel Axtens  *
53152b16f048SDaniel Axtens  * There are a couple of instances where we have a GSO skb, and we
53162b16f048SDaniel Axtens  * want to determine what size it would be after it is segmented.
53172b16f048SDaniel Axtens  *
53182b16f048SDaniel Axtens  * We might want to check:
53192b16f048SDaniel Axtens  * -    L3+L4+payload size (e.g. IP forwarding)
53202b16f048SDaniel Axtens  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
53212b16f048SDaniel Axtens  *
53222b16f048SDaniel Axtens  * This is a helper to do that correctly considering GSO_BY_FRAGS.
53232b16f048SDaniel Axtens  *
532449682bfaSMathieu Malaterre  * @skb: GSO skb
532549682bfaSMathieu Malaterre  *
53262b16f048SDaniel Axtens  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
53272b16f048SDaniel Axtens  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
53282b16f048SDaniel Axtens  *
53292b16f048SDaniel Axtens  * @max_len: The maximum permissible length.
53302b16f048SDaniel Axtens  *
53312b16f048SDaniel Axtens  * Returns true if the segmented length <= max length.
53322b16f048SDaniel Axtens  */
53332b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb,
53342b16f048SDaniel Axtens 				      unsigned int seg_len,
53352b16f048SDaniel Axtens 				      unsigned int max_len) {
53362b16f048SDaniel Axtens 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
53372b16f048SDaniel Axtens 	const struct sk_buff *iter;
53382b16f048SDaniel Axtens 
53392b16f048SDaniel Axtens 	if (shinfo->gso_size != GSO_BY_FRAGS)
53402b16f048SDaniel Axtens 		return seg_len <= max_len;
53412b16f048SDaniel Axtens 
53422b16f048SDaniel Axtens 	/* Undo this so we can re-use header sizes */
53432b16f048SDaniel Axtens 	seg_len -= GSO_BY_FRAGS;
53442b16f048SDaniel Axtens 
53452b16f048SDaniel Axtens 	skb_walk_frags(skb, iter) {
53462b16f048SDaniel Axtens 		if (seg_len + skb_headlen(iter) > max_len)
53472b16f048SDaniel Axtens 			return false;
53482b16f048SDaniel Axtens 	}
53492b16f048SDaniel Axtens 
53502b16f048SDaniel Axtens 	return true;
53512b16f048SDaniel Axtens }
53522b16f048SDaniel Axtens 
53532b16f048SDaniel Axtens /**
5354779b7931SDaniel Axtens  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5355ae7ef81eSMarcelo Ricardo Leitner  *
5356ae7ef81eSMarcelo Ricardo Leitner  * @skb: GSO skb
535776f21b99SDavid S. Miller  * @mtu: MTU to validate against
5358ae7ef81eSMarcelo Ricardo Leitner  *
5359779b7931SDaniel Axtens  * skb_gso_validate_network_len validates if a given skb will fit a
5360779b7931SDaniel Axtens  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5361779b7931SDaniel Axtens  * payload.
5362ae7ef81eSMarcelo Ricardo Leitner  */
5363779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5364ae7ef81eSMarcelo Ricardo Leitner {
53652b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5366ae7ef81eSMarcelo Ricardo Leitner }
5367779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5368ae7ef81eSMarcelo Ricardo Leitner 
53692b16f048SDaniel Axtens /**
53702b16f048SDaniel Axtens  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
53712b16f048SDaniel Axtens  *
53722b16f048SDaniel Axtens  * @skb: GSO skb
53732b16f048SDaniel Axtens  * @len: length to validate against
53742b16f048SDaniel Axtens  *
53752b16f048SDaniel Axtens  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
53762b16f048SDaniel Axtens  * length once split, including L2, L3 and L4 headers and the payload.
53772b16f048SDaniel Axtens  */
53782b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
53792b16f048SDaniel Axtens {
53802b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
53812b16f048SDaniel Axtens }
53822b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
53832b16f048SDaniel Axtens 
53840d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
53850d5501c1SVlad Yasevich {
5386d85e8be2SYuya Kusakabe 	int mac_len, meta_len;
5387d85e8be2SYuya Kusakabe 	void *meta;
53884bbb3e0eSToshiaki Makita 
53890d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
53900d5501c1SVlad Yasevich 		kfree_skb(skb);
53910d5501c1SVlad Yasevich 		return NULL;
53920d5501c1SVlad Yasevich 	}
53930d5501c1SVlad Yasevich 
53944bbb3e0eSToshiaki Makita 	mac_len = skb->data - skb_mac_header(skb);
5395ae474573SToshiaki Makita 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
53964bbb3e0eSToshiaki Makita 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
53974bbb3e0eSToshiaki Makita 			mac_len - VLAN_HLEN - ETH_TLEN);
5398ae474573SToshiaki Makita 	}
5399d85e8be2SYuya Kusakabe 
5400d85e8be2SYuya Kusakabe 	meta_len = skb_metadata_len(skb);
5401d85e8be2SYuya Kusakabe 	if (meta_len) {
5402d85e8be2SYuya Kusakabe 		meta = skb_metadata_end(skb) - meta_len;
5403d85e8be2SYuya Kusakabe 		memmove(meta + VLAN_HLEN, meta, meta_len);
5404d85e8be2SYuya Kusakabe 	}
5405d85e8be2SYuya Kusakabe 
54060d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
54070d5501c1SVlad Yasevich 	return skb;
54080d5501c1SVlad Yasevich }
54090d5501c1SVlad Yasevich 
54100d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
54110d5501c1SVlad Yasevich {
54120d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
54130d5501c1SVlad Yasevich 	u16 vlan_tci;
54140d5501c1SVlad Yasevich 
5415df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
54160d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
54170d5501c1SVlad Yasevich 		return skb;
54180d5501c1SVlad Yasevich 	}
54190d5501c1SVlad Yasevich 
54200d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
54210d5501c1SVlad Yasevich 	if (unlikely(!skb))
54220d5501c1SVlad Yasevich 		goto err_free;
542355eff0ebSMiaohe Lin 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
542455eff0ebSMiaohe Lin 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
54250d5501c1SVlad Yasevich 		goto err_free;
54260d5501c1SVlad Yasevich 
54270d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
54280d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
54290d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
54300d5501c1SVlad Yasevich 
54310d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
54320d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
54330d5501c1SVlad Yasevich 
54340d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
54350d5501c1SVlad Yasevich 	if (unlikely(!skb))
54360d5501c1SVlad Yasevich 		goto err_free;
54370d5501c1SVlad Yasevich 
54380d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
54390d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
54400d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
54410d5501c1SVlad Yasevich 
54420d5501c1SVlad Yasevich 	return skb;
54430d5501c1SVlad Yasevich 
54440d5501c1SVlad Yasevich err_free:
54450d5501c1SVlad Yasevich 	kfree_skb(skb);
54460d5501c1SVlad Yasevich 	return NULL;
54470d5501c1SVlad Yasevich }
54480d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
54492e4e4410SEric Dumazet 
5450e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
5451e2195121SJiri Pirko {
5452e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
5453e2195121SJiri Pirko 		return -ENOMEM;
5454e2195121SJiri Pirko 
5455e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5456e2195121SJiri Pirko 		return 0;
5457e2195121SJiri Pirko 
5458e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5459e2195121SJiri Pirko }
5460e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
5461e2195121SJiri Pirko 
5462bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
5463bfca4c52SShmulik Ladkani  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5464bfca4c52SShmulik Ladkani  */
5465bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
546693515d53SJiri Pirko {
546793515d53SJiri Pirko 	struct vlan_hdr *vhdr;
5468b6a79208SShmulik Ladkani 	int offset = skb->data - skb_mac_header(skb);
546993515d53SJiri Pirko 	int err;
547093515d53SJiri Pirko 
5471b6a79208SShmulik Ladkani 	if (WARN_ONCE(offset,
5472b6a79208SShmulik Ladkani 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5473b6a79208SShmulik Ladkani 		      offset)) {
5474b6a79208SShmulik Ladkani 		return -EINVAL;
5475b6a79208SShmulik Ladkani 	}
5476b6a79208SShmulik Ladkani 
547793515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
547893515d53SJiri Pirko 	if (unlikely(err))
5479b6a79208SShmulik Ladkani 		return err;
548093515d53SJiri Pirko 
548193515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
548293515d53SJiri Pirko 
548393515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
548493515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
548593515d53SJiri Pirko 
548693515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
548793515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
548893515d53SJiri Pirko 
548993515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
549093515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
549193515d53SJiri Pirko 
549293515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
549393515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
549493515d53SJiri Pirko 
549593515d53SJiri Pirko 	skb_reset_mac_len(skb);
549693515d53SJiri Pirko 
549793515d53SJiri Pirko 	return err;
549893515d53SJiri Pirko }
5499bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
550093515d53SJiri Pirko 
5501b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
5502b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5503b6a79208SShmulik Ladkani  */
550493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
550593515d53SJiri Pirko {
550693515d53SJiri Pirko 	u16 vlan_tci;
550793515d53SJiri Pirko 	__be16 vlan_proto;
550893515d53SJiri Pirko 	int err;
550993515d53SJiri Pirko 
5510df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
5511b1817524SMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
551293515d53SJiri Pirko 	} else {
5513ecf4ee41SShmulik Ladkani 		if (unlikely(!eth_type_vlan(skb->protocol)))
551493515d53SJiri Pirko 			return 0;
551593515d53SJiri Pirko 
551693515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
551793515d53SJiri Pirko 		if (err)
551893515d53SJiri Pirko 			return err;
551993515d53SJiri Pirko 	}
552093515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
5521ecf4ee41SShmulik Ladkani 	if (likely(!eth_type_vlan(skb->protocol)))
552293515d53SJiri Pirko 		return 0;
552393515d53SJiri Pirko 
552493515d53SJiri Pirko 	vlan_proto = skb->protocol;
552593515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
552693515d53SJiri Pirko 	if (unlikely(err))
552793515d53SJiri Pirko 		return err;
552893515d53SJiri Pirko 
552993515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
553093515d53SJiri Pirko 	return 0;
553193515d53SJiri Pirko }
553293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
553393515d53SJiri Pirko 
5534b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5535b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5536b6a79208SShmulik Ladkani  */
553793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
553893515d53SJiri Pirko {
5539df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
5540b6a79208SShmulik Ladkani 		int offset = skb->data - skb_mac_header(skb);
554193515d53SJiri Pirko 		int err;
554293515d53SJiri Pirko 
5543b6a79208SShmulik Ladkani 		if (WARN_ONCE(offset,
5544b6a79208SShmulik Ladkani 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5545b6a79208SShmulik Ladkani 			      offset)) {
5546b6a79208SShmulik Ladkani 			return -EINVAL;
5547b6a79208SShmulik Ladkani 		}
5548b6a79208SShmulik Ladkani 
554993515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
5550df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
5551b6a79208SShmulik Ladkani 		if (err)
555293515d53SJiri Pirko 			return err;
55539241e2dfSDaniel Borkmann 
555493515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
555593515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
555693515d53SJiri Pirko 
55576b83d28aSDaniel Borkmann 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
555893515d53SJiri Pirko 	}
555993515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
556093515d53SJiri Pirko 	return 0;
556193515d53SJiri Pirko }
556293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
556393515d53SJiri Pirko 
55648822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */
55658822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
55668822e270SJohn Hurley 			     __be16 ethertype)
55678822e270SJohn Hurley {
55688822e270SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
55698822e270SJohn Hurley 		__be16 diff[] = { ~hdr->h_proto, ethertype };
55708822e270SJohn Hurley 
55718822e270SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
55728822e270SJohn Hurley 	}
55738822e270SJohn Hurley 
55748822e270SJohn Hurley 	hdr->h_proto = ethertype;
55758822e270SJohn Hurley }
55768822e270SJohn Hurley 
55778822e270SJohn Hurley /**
5578e7dbfed1SMartin Varghese  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5579e7dbfed1SMartin Varghese  *                   the packet
55808822e270SJohn Hurley  *
55818822e270SJohn Hurley  * @skb: buffer
55828822e270SJohn Hurley  * @mpls_lse: MPLS label stack entry to push
55838822e270SJohn Hurley  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
5584fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
5585e7dbfed1SMartin Varghese  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5586e7dbfed1SMartin Varghese  *            ethernet
55878822e270SJohn Hurley  *
55888822e270SJohn Hurley  * Expects skb->data at mac header.
55898822e270SJohn Hurley  *
55908822e270SJohn Hurley  * Returns 0 on success, -errno otherwise.
55918822e270SJohn Hurley  */
5592fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
5593d04ac224SMartin Varghese 		  int mac_len, bool ethernet)
55948822e270SJohn Hurley {
55958822e270SJohn Hurley 	struct mpls_shim_hdr *lse;
55968822e270SJohn Hurley 	int err;
55978822e270SJohn Hurley 
55988822e270SJohn Hurley 	if (unlikely(!eth_p_mpls(mpls_proto)))
55998822e270SJohn Hurley 		return -EINVAL;
56008822e270SJohn Hurley 
56018822e270SJohn Hurley 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
56028822e270SJohn Hurley 	if (skb->encapsulation)
56038822e270SJohn Hurley 		return -EINVAL;
56048822e270SJohn Hurley 
56058822e270SJohn Hurley 	err = skb_cow_head(skb, MPLS_HLEN);
56068822e270SJohn Hurley 	if (unlikely(err))
56078822e270SJohn Hurley 		return err;
56088822e270SJohn Hurley 
56098822e270SJohn Hurley 	if (!skb->inner_protocol) {
5610e7dbfed1SMartin Varghese 		skb_set_inner_network_header(skb, skb_network_offset(skb));
56118822e270SJohn Hurley 		skb_set_inner_protocol(skb, skb->protocol);
56128822e270SJohn Hurley 	}
56138822e270SJohn Hurley 
56148822e270SJohn Hurley 	skb_push(skb, MPLS_HLEN);
56158822e270SJohn Hurley 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
5616fa4e0f88SDavide Caratti 		mac_len);
56178822e270SJohn Hurley 	skb_reset_mac_header(skb);
5618fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
5619e7dbfed1SMartin Varghese 	skb_reset_mac_len(skb);
56208822e270SJohn Hurley 
56218822e270SJohn Hurley 	lse = mpls_hdr(skb);
56228822e270SJohn Hurley 	lse->label_stack_entry = mpls_lse;
56238822e270SJohn Hurley 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
56248822e270SJohn Hurley 
5625d04ac224SMartin Varghese 	if (ethernet)
56268822e270SJohn Hurley 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
56278822e270SJohn Hurley 	skb->protocol = mpls_proto;
56288822e270SJohn Hurley 
56298822e270SJohn Hurley 	return 0;
56308822e270SJohn Hurley }
56318822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push);
56328822e270SJohn Hurley 
56332e4e4410SEric Dumazet /**
5634ed246ceeSJohn Hurley  * skb_mpls_pop() - pop the outermost MPLS header
5635ed246ceeSJohn Hurley  *
5636ed246ceeSJohn Hurley  * @skb: buffer
5637ed246ceeSJohn Hurley  * @next_proto: ethertype of header after popped MPLS header
5638fa4e0f88SDavide Caratti  * @mac_len: length of the MAC header
563976f99f98SMartin Varghese  * @ethernet: flag to indicate if the packet is ethernet
5640ed246ceeSJohn Hurley  *
5641ed246ceeSJohn Hurley  * Expects skb->data at mac header.
5642ed246ceeSJohn Hurley  *
5643ed246ceeSJohn Hurley  * Returns 0 on success, -errno otherwise.
5644ed246ceeSJohn Hurley  */
5645040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5646040b5cfbSMartin Varghese 		 bool ethernet)
5647ed246ceeSJohn Hurley {
5648ed246ceeSJohn Hurley 	int err;
5649ed246ceeSJohn Hurley 
5650ed246ceeSJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
5651dedc5a08SDavide Caratti 		return 0;
5652ed246ceeSJohn Hurley 
5653fa4e0f88SDavide Caratti 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
5654ed246ceeSJohn Hurley 	if (unlikely(err))
5655ed246ceeSJohn Hurley 		return err;
5656ed246ceeSJohn Hurley 
5657ed246ceeSJohn Hurley 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5658ed246ceeSJohn Hurley 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
5659fa4e0f88SDavide Caratti 		mac_len);
5660ed246ceeSJohn Hurley 
5661ed246ceeSJohn Hurley 	__skb_pull(skb, MPLS_HLEN);
5662ed246ceeSJohn Hurley 	skb_reset_mac_header(skb);
5663fa4e0f88SDavide Caratti 	skb_set_network_header(skb, mac_len);
5664ed246ceeSJohn Hurley 
5665040b5cfbSMartin Varghese 	if (ethernet) {
5666ed246ceeSJohn Hurley 		struct ethhdr *hdr;
5667ed246ceeSJohn Hurley 
5668ed246ceeSJohn Hurley 		/* use mpls_hdr() to get ethertype to account for VLANs. */
5669ed246ceeSJohn Hurley 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5670ed246ceeSJohn Hurley 		skb_mod_eth_type(skb, hdr, next_proto);
5671ed246ceeSJohn Hurley 	}
5672ed246ceeSJohn Hurley 	skb->protocol = next_proto;
5673ed246ceeSJohn Hurley 
5674ed246ceeSJohn Hurley 	return 0;
5675ed246ceeSJohn Hurley }
5676ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop);
5677ed246ceeSJohn Hurley 
5678ed246ceeSJohn Hurley /**
5679d27cf5c5SJohn Hurley  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5680d27cf5c5SJohn Hurley  *
5681d27cf5c5SJohn Hurley  * @skb: buffer
5682d27cf5c5SJohn Hurley  * @mpls_lse: new MPLS label stack entry to update to
5683d27cf5c5SJohn Hurley  *
5684d27cf5c5SJohn Hurley  * Expects skb->data at mac header.
5685d27cf5c5SJohn Hurley  *
5686d27cf5c5SJohn Hurley  * Returns 0 on success, -errno otherwise.
5687d27cf5c5SJohn Hurley  */
5688d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5689d27cf5c5SJohn Hurley {
5690d27cf5c5SJohn Hurley 	int err;
5691d27cf5c5SJohn Hurley 
5692d27cf5c5SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
5693d27cf5c5SJohn Hurley 		return -EINVAL;
5694d27cf5c5SJohn Hurley 
5695d27cf5c5SJohn Hurley 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5696d27cf5c5SJohn Hurley 	if (unlikely(err))
5697d27cf5c5SJohn Hurley 		return err;
5698d27cf5c5SJohn Hurley 
5699d27cf5c5SJohn Hurley 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
5700d27cf5c5SJohn Hurley 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5701d27cf5c5SJohn Hurley 
5702d27cf5c5SJohn Hurley 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5703d27cf5c5SJohn Hurley 	}
5704d27cf5c5SJohn Hurley 
5705d27cf5c5SJohn Hurley 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
5706d27cf5c5SJohn Hurley 
5707d27cf5c5SJohn Hurley 	return 0;
5708d27cf5c5SJohn Hurley }
5709d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5710d27cf5c5SJohn Hurley 
5711d27cf5c5SJohn Hurley /**
57122a2ea508SJohn Hurley  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
57132a2ea508SJohn Hurley  *
57142a2ea508SJohn Hurley  * @skb: buffer
57152a2ea508SJohn Hurley  *
57162a2ea508SJohn Hurley  * Expects skb->data at mac header.
57172a2ea508SJohn Hurley  *
57182a2ea508SJohn Hurley  * Returns 0 on success, -errno otherwise.
57192a2ea508SJohn Hurley  */
57202a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb)
57212a2ea508SJohn Hurley {
57222a2ea508SJohn Hurley 	u32 lse;
57232a2ea508SJohn Hurley 	u8 ttl;
57242a2ea508SJohn Hurley 
57252a2ea508SJohn Hurley 	if (unlikely(!eth_p_mpls(skb->protocol)))
57262a2ea508SJohn Hurley 		return -EINVAL;
57272a2ea508SJohn Hurley 
57282a2ea508SJohn Hurley 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
57292a2ea508SJohn Hurley 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
57302a2ea508SJohn Hurley 	if (!--ttl)
57312a2ea508SJohn Hurley 		return -EINVAL;
57322a2ea508SJohn Hurley 
57332a2ea508SJohn Hurley 	lse &= ~MPLS_LS_TTL_MASK;
57342a2ea508SJohn Hurley 	lse |= ttl << MPLS_LS_TTL_SHIFT;
57352a2ea508SJohn Hurley 
57362a2ea508SJohn Hurley 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
57372a2ea508SJohn Hurley }
57382a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
57392a2ea508SJohn Hurley 
57402a2ea508SJohn Hurley /**
57412e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
57422e4e4410SEric Dumazet  *
5743de3f0d0eSMasanari Iida  * @header_len: size of linear part
5744de3f0d0eSMasanari Iida  * @data_len: needed length in frags
5745de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
5746de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
5747de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
57482e4e4410SEric Dumazet  *
57492e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
57502e4e4410SEric Dumazet  */
57512e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
57522e4e4410SEric Dumazet 				     unsigned long data_len,
57532e4e4410SEric Dumazet 				     int max_page_order,
57542e4e4410SEric Dumazet 				     int *errcode,
57552e4e4410SEric Dumazet 				     gfp_t gfp_mask)
57562e4e4410SEric Dumazet {
57572e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
57582e4e4410SEric Dumazet 	unsigned long chunk;
57592e4e4410SEric Dumazet 	struct sk_buff *skb;
57602e4e4410SEric Dumazet 	struct page *page;
57612e4e4410SEric Dumazet 	int i;
57622e4e4410SEric Dumazet 
57632e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
57642e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
57652e4e4410SEric Dumazet 	 * high order pages...
57662e4e4410SEric Dumazet 	 */
57672e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
57682e4e4410SEric Dumazet 		return NULL;
57692e4e4410SEric Dumazet 
57702e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
5771f8c468e8SDavid Rientjes 	skb = alloc_skb(header_len, gfp_mask);
57722e4e4410SEric Dumazet 	if (!skb)
57732e4e4410SEric Dumazet 		return NULL;
57742e4e4410SEric Dumazet 
57752e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
57762e4e4410SEric Dumazet 
57772e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
57782e4e4410SEric Dumazet 		int order = max_page_order;
57792e4e4410SEric Dumazet 
57802e4e4410SEric Dumazet 		while (order) {
57812e4e4410SEric Dumazet 			if (npages >= 1 << order) {
5782d0164adcSMel Gorman 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
57832e4e4410SEric Dumazet 						   __GFP_COMP |
5784d14b56f5SMichal Hocko 						   __GFP_NOWARN,
57852e4e4410SEric Dumazet 						   order);
57862e4e4410SEric Dumazet 				if (page)
57872e4e4410SEric Dumazet 					goto fill_page;
57882e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
57892e4e4410SEric Dumazet 				order = 1;
57902e4e4410SEric Dumazet 				max_page_order = 0;
57912e4e4410SEric Dumazet 			}
57922e4e4410SEric Dumazet 			order--;
57932e4e4410SEric Dumazet 		}
57942e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
57952e4e4410SEric Dumazet 		if (!page)
57962e4e4410SEric Dumazet 			goto failure;
57972e4e4410SEric Dumazet fill_page:
57982e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
57992e4e4410SEric Dumazet 			      PAGE_SIZE << order);
58002e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
58012e4e4410SEric Dumazet 		data_len -= chunk;
58022e4e4410SEric Dumazet 		npages -= 1 << order;
58032e4e4410SEric Dumazet 	}
58042e4e4410SEric Dumazet 	return skb;
58052e4e4410SEric Dumazet 
58062e4e4410SEric Dumazet failure:
58072e4e4410SEric Dumazet 	kfree_skb(skb);
58082e4e4410SEric Dumazet 	return NULL;
58092e4e4410SEric Dumazet }
58102e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
58116fa01ccdSSowmini Varadhan 
58126fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
58136fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
58146fa01ccdSSowmini Varadhan 				    const int headlen, gfp_t gfp_mask)
58156fa01ccdSSowmini Varadhan {
58166fa01ccdSSowmini Varadhan 	int i;
58176fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
58186fa01ccdSSowmini Varadhan 	int new_hlen = headlen - off;
58196fa01ccdSSowmini Varadhan 	u8 *data;
58206fa01ccdSSowmini Varadhan 
58216fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
58226fa01ccdSSowmini Varadhan 
58236fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
58246fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
58256fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
58266fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
58276fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
58286fa01ccdSSowmini Varadhan 	if (!data)
58296fa01ccdSSowmini Varadhan 		return -ENOMEM;
58306fa01ccdSSowmini Varadhan 
58316fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
58326fa01ccdSSowmini Varadhan 
58336fa01ccdSSowmini Varadhan 	/* Copy real data, and all frags */
58346fa01ccdSSowmini Varadhan 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
58356fa01ccdSSowmini Varadhan 	skb->len -= off;
58366fa01ccdSSowmini Varadhan 
58376fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
58386fa01ccdSSowmini Varadhan 	       skb_shinfo(skb),
58396fa01ccdSSowmini Varadhan 	       offsetof(struct skb_shared_info,
58406fa01ccdSSowmini Varadhan 			frags[skb_shinfo(skb)->nr_frags]));
58416fa01ccdSSowmini Varadhan 	if (skb_cloned(skb)) {
58426fa01ccdSSowmini Varadhan 		/* drop the old head gracefully */
58436fa01ccdSSowmini Varadhan 		if (skb_orphan_frags(skb, gfp_mask)) {
58446fa01ccdSSowmini Varadhan 			kfree(data);
58456fa01ccdSSowmini Varadhan 			return -ENOMEM;
58466fa01ccdSSowmini Varadhan 		}
58476fa01ccdSSowmini Varadhan 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
58486fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
58496fa01ccdSSowmini Varadhan 		if (skb_has_frag_list(skb))
58506fa01ccdSSowmini Varadhan 			skb_clone_fraglist(skb);
58516fa01ccdSSowmini Varadhan 		skb_release_data(skb);
58526fa01ccdSSowmini Varadhan 	} else {
58536fa01ccdSSowmini Varadhan 		/* we can reuse existing recount- all we did was
58546fa01ccdSSowmini Varadhan 		 * relocate values
58556fa01ccdSSowmini Varadhan 		 */
58566fa01ccdSSowmini Varadhan 		skb_free_head(skb);
58576fa01ccdSSowmini Varadhan 	}
58586fa01ccdSSowmini Varadhan 
58596fa01ccdSSowmini Varadhan 	skb->head = data;
58606fa01ccdSSowmini Varadhan 	skb->data = data;
58616fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
58626fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
58636fa01ccdSSowmini Varadhan 	skb->end = size;
58646fa01ccdSSowmini Varadhan #else
58656fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
58666fa01ccdSSowmini Varadhan #endif
58676fa01ccdSSowmini Varadhan 	skb_set_tail_pointer(skb, skb_headlen(skb));
58686fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
58696fa01ccdSSowmini Varadhan 	skb->cloned = 0;
58706fa01ccdSSowmini Varadhan 	skb->hdr_len = 0;
58716fa01ccdSSowmini Varadhan 	skb->nohdr = 0;
58726fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
58736fa01ccdSSowmini Varadhan 
58746fa01ccdSSowmini Varadhan 	return 0;
58756fa01ccdSSowmini Varadhan }
58766fa01ccdSSowmini Varadhan 
58776fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
58786fa01ccdSSowmini Varadhan 
58796fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
58806fa01ccdSSowmini Varadhan  * pskb_carve()
58816fa01ccdSSowmini Varadhan  */
58826fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
58836fa01ccdSSowmini Varadhan 				struct skb_shared_info *shinfo, int eat,
58846fa01ccdSSowmini Varadhan 				gfp_t gfp_mask)
58856fa01ccdSSowmini Varadhan {
58866fa01ccdSSowmini Varadhan 	struct sk_buff *list = shinfo->frag_list;
58876fa01ccdSSowmini Varadhan 	struct sk_buff *clone = NULL;
58886fa01ccdSSowmini Varadhan 	struct sk_buff *insp = NULL;
58896fa01ccdSSowmini Varadhan 
58906fa01ccdSSowmini Varadhan 	do {
58916fa01ccdSSowmini Varadhan 		if (!list) {
58926fa01ccdSSowmini Varadhan 			pr_err("Not enough bytes to eat. Want %d\n", eat);
58936fa01ccdSSowmini Varadhan 			return -EFAULT;
58946fa01ccdSSowmini Varadhan 		}
58956fa01ccdSSowmini Varadhan 		if (list->len <= eat) {
58966fa01ccdSSowmini Varadhan 			/* Eaten as whole. */
58976fa01ccdSSowmini Varadhan 			eat -= list->len;
58986fa01ccdSSowmini Varadhan 			list = list->next;
58996fa01ccdSSowmini Varadhan 			insp = list;
59006fa01ccdSSowmini Varadhan 		} else {
59016fa01ccdSSowmini Varadhan 			/* Eaten partially. */
59026fa01ccdSSowmini Varadhan 			if (skb_shared(list)) {
59036fa01ccdSSowmini Varadhan 				clone = skb_clone(list, gfp_mask);
59046fa01ccdSSowmini Varadhan 				if (!clone)
59056fa01ccdSSowmini Varadhan 					return -ENOMEM;
59066fa01ccdSSowmini Varadhan 				insp = list->next;
59076fa01ccdSSowmini Varadhan 				list = clone;
59086fa01ccdSSowmini Varadhan 			} else {
59096fa01ccdSSowmini Varadhan 				/* This may be pulled without problems. */
59106fa01ccdSSowmini Varadhan 				insp = list;
59116fa01ccdSSowmini Varadhan 			}
59126fa01ccdSSowmini Varadhan 			if (pskb_carve(list, eat, gfp_mask) < 0) {
59136fa01ccdSSowmini Varadhan 				kfree_skb(clone);
59146fa01ccdSSowmini Varadhan 				return -ENOMEM;
59156fa01ccdSSowmini Varadhan 			}
59166fa01ccdSSowmini Varadhan 			break;
59176fa01ccdSSowmini Varadhan 		}
59186fa01ccdSSowmini Varadhan 	} while (eat);
59196fa01ccdSSowmini Varadhan 
59206fa01ccdSSowmini Varadhan 	/* Free pulled out fragments. */
59216fa01ccdSSowmini Varadhan 	while ((list = shinfo->frag_list) != insp) {
59226fa01ccdSSowmini Varadhan 		shinfo->frag_list = list->next;
59236fa01ccdSSowmini Varadhan 		kfree_skb(list);
59246fa01ccdSSowmini Varadhan 	}
59256fa01ccdSSowmini Varadhan 	/* And insert new clone at head. */
59266fa01ccdSSowmini Varadhan 	if (clone) {
59276fa01ccdSSowmini Varadhan 		clone->next = list;
59286fa01ccdSSowmini Varadhan 		shinfo->frag_list = clone;
59296fa01ccdSSowmini Varadhan 	}
59306fa01ccdSSowmini Varadhan 	return 0;
59316fa01ccdSSowmini Varadhan }
59326fa01ccdSSowmini Varadhan 
59336fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
59346fa01ccdSSowmini Varadhan  * non-linear part of skb
59356fa01ccdSSowmini Varadhan  */
59366fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
59376fa01ccdSSowmini Varadhan 				       int pos, gfp_t gfp_mask)
59386fa01ccdSSowmini Varadhan {
59396fa01ccdSSowmini Varadhan 	int i, k = 0;
59406fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
59416fa01ccdSSowmini Varadhan 	u8 *data;
59426fa01ccdSSowmini Varadhan 	const int nfrags = skb_shinfo(skb)->nr_frags;
59436fa01ccdSSowmini Varadhan 	struct skb_shared_info *shinfo;
59446fa01ccdSSowmini Varadhan 
59456fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
59466fa01ccdSSowmini Varadhan 
59476fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
59486fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
59496fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
59506fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
59516fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
59526fa01ccdSSowmini Varadhan 	if (!data)
59536fa01ccdSSowmini Varadhan 		return -ENOMEM;
59546fa01ccdSSowmini Varadhan 
59556fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
59566fa01ccdSSowmini Varadhan 
59576fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
59586fa01ccdSSowmini Varadhan 	       skb_shinfo(skb), offsetof(struct skb_shared_info,
59596fa01ccdSSowmini Varadhan 					 frags[skb_shinfo(skb)->nr_frags]));
59606fa01ccdSSowmini Varadhan 	if (skb_orphan_frags(skb, gfp_mask)) {
59616fa01ccdSSowmini Varadhan 		kfree(data);
59626fa01ccdSSowmini Varadhan 		return -ENOMEM;
59636fa01ccdSSowmini Varadhan 	}
59646fa01ccdSSowmini Varadhan 	shinfo = (struct skb_shared_info *)(data + size);
59656fa01ccdSSowmini Varadhan 	for (i = 0; i < nfrags; i++) {
59666fa01ccdSSowmini Varadhan 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
59676fa01ccdSSowmini Varadhan 
59686fa01ccdSSowmini Varadhan 		if (pos + fsize > off) {
59696fa01ccdSSowmini Varadhan 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
59706fa01ccdSSowmini Varadhan 
59716fa01ccdSSowmini Varadhan 			if (pos < off) {
59726fa01ccdSSowmini Varadhan 				/* Split frag.
59736fa01ccdSSowmini Varadhan 				 * We have two variants in this case:
59746fa01ccdSSowmini Varadhan 				 * 1. Move all the frag to the second
59756fa01ccdSSowmini Varadhan 				 *    part, if it is possible. F.e.
59766fa01ccdSSowmini Varadhan 				 *    this approach is mandatory for TUX,
59776fa01ccdSSowmini Varadhan 				 *    where splitting is expensive.
59786fa01ccdSSowmini Varadhan 				 * 2. Split is accurately. We make this.
59796fa01ccdSSowmini Varadhan 				 */
5980b54c9d5bSJonathan Lemon 				skb_frag_off_add(&shinfo->frags[0], off - pos);
59816fa01ccdSSowmini Varadhan 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
59826fa01ccdSSowmini Varadhan 			}
59836fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
59846fa01ccdSSowmini Varadhan 			k++;
59856fa01ccdSSowmini Varadhan 		}
59866fa01ccdSSowmini Varadhan 		pos += fsize;
59876fa01ccdSSowmini Varadhan 	}
59886fa01ccdSSowmini Varadhan 	shinfo->nr_frags = k;
59896fa01ccdSSowmini Varadhan 	if (skb_has_frag_list(skb))
59906fa01ccdSSowmini Varadhan 		skb_clone_fraglist(skb);
59916fa01ccdSSowmini Varadhan 
59926fa01ccdSSowmini Varadhan 	/* split line is in frag list */
5993eabe8618SMiaohe Lin 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
5994eabe8618SMiaohe Lin 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
5995eabe8618SMiaohe Lin 		if (skb_has_frag_list(skb))
5996eabe8618SMiaohe Lin 			kfree_skb_list(skb_shinfo(skb)->frag_list);
5997eabe8618SMiaohe Lin 		kfree(data);
5998eabe8618SMiaohe Lin 		return -ENOMEM;
59996fa01ccdSSowmini Varadhan 	}
60006fa01ccdSSowmini Varadhan 	skb_release_data(skb);
60016fa01ccdSSowmini Varadhan 
60026fa01ccdSSowmini Varadhan 	skb->head = data;
60036fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
60046fa01ccdSSowmini Varadhan 	skb->data = data;
60056fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
60066fa01ccdSSowmini Varadhan 	skb->end = size;
60076fa01ccdSSowmini Varadhan #else
60086fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
60096fa01ccdSSowmini Varadhan #endif
60106fa01ccdSSowmini Varadhan 	skb_reset_tail_pointer(skb);
60116fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
60126fa01ccdSSowmini Varadhan 	skb->cloned   = 0;
60136fa01ccdSSowmini Varadhan 	skb->hdr_len  = 0;
60146fa01ccdSSowmini Varadhan 	skb->nohdr    = 0;
60156fa01ccdSSowmini Varadhan 	skb->len -= off;
60166fa01ccdSSowmini Varadhan 	skb->data_len = skb->len;
60176fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
60186fa01ccdSSowmini Varadhan 	return 0;
60196fa01ccdSSowmini Varadhan }
60206fa01ccdSSowmini Varadhan 
60216fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
60226fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
60236fa01ccdSSowmini Varadhan {
60246fa01ccdSSowmini Varadhan 	int headlen = skb_headlen(skb);
60256fa01ccdSSowmini Varadhan 
60266fa01ccdSSowmini Varadhan 	if (len < headlen)
60276fa01ccdSSowmini Varadhan 		return pskb_carve_inside_header(skb, len, headlen, gfp);
60286fa01ccdSSowmini Varadhan 	else
60296fa01ccdSSowmini Varadhan 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
60306fa01ccdSSowmini Varadhan }
60316fa01ccdSSowmini Varadhan 
60326fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
60336fa01ccdSSowmini Varadhan  * a new skb
60346fa01ccdSSowmini Varadhan  */
60356fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
60366fa01ccdSSowmini Varadhan 			     int to_copy, gfp_t gfp)
60376fa01ccdSSowmini Varadhan {
60386fa01ccdSSowmini Varadhan 	struct sk_buff  *clone = skb_clone(skb, gfp);
60396fa01ccdSSowmini Varadhan 
60406fa01ccdSSowmini Varadhan 	if (!clone)
60416fa01ccdSSowmini Varadhan 		return NULL;
60426fa01ccdSSowmini Varadhan 
60436fa01ccdSSowmini Varadhan 	if (pskb_carve(clone, off, gfp) < 0 ||
60446fa01ccdSSowmini Varadhan 	    pskb_trim(clone, to_copy)) {
60456fa01ccdSSowmini Varadhan 		kfree_skb(clone);
60466fa01ccdSSowmini Varadhan 		return NULL;
60476fa01ccdSSowmini Varadhan 	}
60486fa01ccdSSowmini Varadhan 	return clone;
60496fa01ccdSSowmini Varadhan }
60506fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
6051c8c8b127SEric Dumazet 
6052c8c8b127SEric Dumazet /**
6053c8c8b127SEric Dumazet  * skb_condense - try to get rid of fragments/frag_list if possible
6054c8c8b127SEric Dumazet  * @skb: buffer
6055c8c8b127SEric Dumazet  *
6056c8c8b127SEric Dumazet  * Can be used to save memory before skb is added to a busy queue.
6057c8c8b127SEric Dumazet  * If packet has bytes in frags and enough tail room in skb->head,
6058c8c8b127SEric Dumazet  * pull all of them, so that we can free the frags right now and adjust
6059c8c8b127SEric Dumazet  * truesize.
6060c8c8b127SEric Dumazet  * Notes:
6061c8c8b127SEric Dumazet  *	We do not reallocate skb->head thus can not fail.
6062c8c8b127SEric Dumazet  *	Caller must re-evaluate skb->truesize if needed.
6063c8c8b127SEric Dumazet  */
6064c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
6065c8c8b127SEric Dumazet {
60663174fed9SEric Dumazet 	if (skb->data_len) {
60673174fed9SEric Dumazet 		if (skb->data_len > skb->end - skb->tail ||
6068c8c8b127SEric Dumazet 		    skb_cloned(skb))
6069c8c8b127SEric Dumazet 			return;
6070c8c8b127SEric Dumazet 
6071c8c8b127SEric Dumazet 		/* Nice, we can free page frag(s) right now */
6072c8c8b127SEric Dumazet 		__pskb_pull_tail(skb, skb->data_len);
60733174fed9SEric Dumazet 	}
60743174fed9SEric Dumazet 	/* At this point, skb->truesize might be over estimated,
60753174fed9SEric Dumazet 	 * because skb had a fragment, and fragments do not tell
60763174fed9SEric Dumazet 	 * their truesize.
60773174fed9SEric Dumazet 	 * When we pulled its content into skb->head, fragment
60783174fed9SEric Dumazet 	 * was freed, but __pskb_pull_tail() could not possibly
60793174fed9SEric Dumazet 	 * adjust skb->truesize, not knowing the frag truesize.
6080c8c8b127SEric Dumazet 	 */
6081c8c8b127SEric Dumazet 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6082c8c8b127SEric Dumazet }
6083df5042f4SFlorian Westphal 
6084df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
6085df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6086df5042f4SFlorian Westphal {
6087df5042f4SFlorian Westphal 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6088df5042f4SFlorian Westphal }
6089df5042f4SFlorian Westphal 
60908b69a803SPaolo Abeni /**
60918b69a803SPaolo Abeni  * __skb_ext_alloc - allocate a new skb extensions storage
60928b69a803SPaolo Abeni  *
60934930f483SFlorian Westphal  * @flags: See kmalloc().
60944930f483SFlorian Westphal  *
60958b69a803SPaolo Abeni  * Returns the newly allocated pointer. The pointer can later attached to a
60968b69a803SPaolo Abeni  * skb via __skb_ext_set().
60978b69a803SPaolo Abeni  * Note: caller must handle the skb_ext as an opaque data.
60988b69a803SPaolo Abeni  */
60994930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags)
6100df5042f4SFlorian Westphal {
61014930f483SFlorian Westphal 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6102df5042f4SFlorian Westphal 
6103df5042f4SFlorian Westphal 	if (new) {
6104df5042f4SFlorian Westphal 		memset(new->offset, 0, sizeof(new->offset));
6105df5042f4SFlorian Westphal 		refcount_set(&new->refcnt, 1);
6106df5042f4SFlorian Westphal 	}
6107df5042f4SFlorian Westphal 
6108df5042f4SFlorian Westphal 	return new;
6109df5042f4SFlorian Westphal }
6110df5042f4SFlorian Westphal 
61114165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
61124165079bSFlorian Westphal 					 unsigned int old_active)
6113df5042f4SFlorian Westphal {
6114df5042f4SFlorian Westphal 	struct skb_ext *new;
6115df5042f4SFlorian Westphal 
6116df5042f4SFlorian Westphal 	if (refcount_read(&old->refcnt) == 1)
6117df5042f4SFlorian Westphal 		return old;
6118df5042f4SFlorian Westphal 
6119df5042f4SFlorian Westphal 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6120df5042f4SFlorian Westphal 	if (!new)
6121df5042f4SFlorian Westphal 		return NULL;
6122df5042f4SFlorian Westphal 
6123df5042f4SFlorian Westphal 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6124df5042f4SFlorian Westphal 	refcount_set(&new->refcnt, 1);
6125df5042f4SFlorian Westphal 
61264165079bSFlorian Westphal #ifdef CONFIG_XFRM
61274165079bSFlorian Westphal 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
61284165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
61294165079bSFlorian Westphal 		unsigned int i;
61304165079bSFlorian Westphal 
61314165079bSFlorian Westphal 		for (i = 0; i < sp->len; i++)
61324165079bSFlorian Westphal 			xfrm_state_hold(sp->xvec[i]);
61334165079bSFlorian Westphal 	}
61344165079bSFlorian Westphal #endif
6135df5042f4SFlorian Westphal 	__skb_ext_put(old);
6136df5042f4SFlorian Westphal 	return new;
6137df5042f4SFlorian Westphal }
6138df5042f4SFlorian Westphal 
6139df5042f4SFlorian Westphal /**
61408b69a803SPaolo Abeni  * __skb_ext_set - attach the specified extension storage to this skb
61418b69a803SPaolo Abeni  * @skb: buffer
61428b69a803SPaolo Abeni  * @id: extension id
61438b69a803SPaolo Abeni  * @ext: extension storage previously allocated via __skb_ext_alloc()
61448b69a803SPaolo Abeni  *
61458b69a803SPaolo Abeni  * Existing extensions, if any, are cleared.
61468b69a803SPaolo Abeni  *
61478b69a803SPaolo Abeni  * Returns the pointer to the extension.
61488b69a803SPaolo Abeni  */
61498b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
61508b69a803SPaolo Abeni 		    struct skb_ext *ext)
61518b69a803SPaolo Abeni {
61528b69a803SPaolo Abeni 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
61538b69a803SPaolo Abeni 
61548b69a803SPaolo Abeni 	skb_ext_put(skb);
61558b69a803SPaolo Abeni 	newlen = newoff + skb_ext_type_len[id];
61568b69a803SPaolo Abeni 	ext->chunks = newlen;
61578b69a803SPaolo Abeni 	ext->offset[id] = newoff;
61588b69a803SPaolo Abeni 	skb->extensions = ext;
61598b69a803SPaolo Abeni 	skb->active_extensions = 1 << id;
61608b69a803SPaolo Abeni 	return skb_ext_get_ptr(ext, id);
61618b69a803SPaolo Abeni }
61628b69a803SPaolo Abeni 
61638b69a803SPaolo Abeni /**
6164df5042f4SFlorian Westphal  * skb_ext_add - allocate space for given extension, COW if needed
6165df5042f4SFlorian Westphal  * @skb: buffer
6166df5042f4SFlorian Westphal  * @id: extension to allocate space for
6167df5042f4SFlorian Westphal  *
6168df5042f4SFlorian Westphal  * Allocates enough space for the given extension.
6169df5042f4SFlorian Westphal  * If the extension is already present, a pointer to that extension
6170df5042f4SFlorian Westphal  * is returned.
6171df5042f4SFlorian Westphal  *
6172df5042f4SFlorian Westphal  * If the skb was cloned, COW applies and the returned memory can be
6173df5042f4SFlorian Westphal  * modified without changing the extension space of clones buffers.
6174df5042f4SFlorian Westphal  *
6175df5042f4SFlorian Westphal  * Returns pointer to the extension or NULL on allocation failure.
6176df5042f4SFlorian Westphal  */
6177df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6178df5042f4SFlorian Westphal {
6179df5042f4SFlorian Westphal 	struct skb_ext *new, *old = NULL;
6180df5042f4SFlorian Westphal 	unsigned int newlen, newoff;
6181df5042f4SFlorian Westphal 
6182df5042f4SFlorian Westphal 	if (skb->active_extensions) {
6183df5042f4SFlorian Westphal 		old = skb->extensions;
6184df5042f4SFlorian Westphal 
61854165079bSFlorian Westphal 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6186df5042f4SFlorian Westphal 		if (!new)
6187df5042f4SFlorian Westphal 			return NULL;
6188df5042f4SFlorian Westphal 
6189682ec859SPaolo Abeni 		if (__skb_ext_exist(new, id))
6190df5042f4SFlorian Westphal 			goto set_active;
6191df5042f4SFlorian Westphal 
6192e94e50bdSPaolo Abeni 		newoff = new->chunks;
6193df5042f4SFlorian Westphal 	} else {
6194df5042f4SFlorian Westphal 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6195df5042f4SFlorian Westphal 
61964930f483SFlorian Westphal 		new = __skb_ext_alloc(GFP_ATOMIC);
6197df5042f4SFlorian Westphal 		if (!new)
6198df5042f4SFlorian Westphal 			return NULL;
6199df5042f4SFlorian Westphal 	}
6200df5042f4SFlorian Westphal 
6201df5042f4SFlorian Westphal 	newlen = newoff + skb_ext_type_len[id];
6202df5042f4SFlorian Westphal 	new->chunks = newlen;
6203df5042f4SFlorian Westphal 	new->offset[id] = newoff;
6204df5042f4SFlorian Westphal set_active:
6205682ec859SPaolo Abeni 	skb->extensions = new;
6206df5042f4SFlorian Westphal 	skb->active_extensions |= 1 << id;
6207df5042f4SFlorian Westphal 	return skb_ext_get_ptr(new, id);
6208df5042f4SFlorian Westphal }
6209df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add);
6210df5042f4SFlorian Westphal 
62114165079bSFlorian Westphal #ifdef CONFIG_XFRM
62124165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp)
62134165079bSFlorian Westphal {
62144165079bSFlorian Westphal 	unsigned int i;
62154165079bSFlorian Westphal 
62164165079bSFlorian Westphal 	for (i = 0; i < sp->len; i++)
62174165079bSFlorian Westphal 		xfrm_state_put(sp->xvec[i]);
62184165079bSFlorian Westphal }
62194165079bSFlorian Westphal #endif
62204165079bSFlorian Westphal 
6221df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6222df5042f4SFlorian Westphal {
6223df5042f4SFlorian Westphal 	struct skb_ext *ext = skb->extensions;
6224df5042f4SFlorian Westphal 
6225df5042f4SFlorian Westphal 	skb->active_extensions &= ~(1 << id);
6226df5042f4SFlorian Westphal 	if (skb->active_extensions == 0) {
6227df5042f4SFlorian Westphal 		skb->extensions = NULL;
6228df5042f4SFlorian Westphal 		__skb_ext_put(ext);
62294165079bSFlorian Westphal #ifdef CONFIG_XFRM
62304165079bSFlorian Westphal 	} else if (id == SKB_EXT_SEC_PATH &&
62314165079bSFlorian Westphal 		   refcount_read(&ext->refcnt) == 1) {
62324165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
62334165079bSFlorian Westphal 
62344165079bSFlorian Westphal 		skb_ext_put_sp(sp);
62354165079bSFlorian Westphal 		sp->len = 0;
62364165079bSFlorian Westphal #endif
6237df5042f4SFlorian Westphal 	}
6238df5042f4SFlorian Westphal }
6239df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del);
6240df5042f4SFlorian Westphal 
6241df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext)
6242df5042f4SFlorian Westphal {
6243df5042f4SFlorian Westphal 	/* If this is last clone, nothing can increment
6244df5042f4SFlorian Westphal 	 * it after check passes.  Avoids one atomic op.
6245df5042f4SFlorian Westphal 	 */
6246df5042f4SFlorian Westphal 	if (refcount_read(&ext->refcnt) == 1)
6247df5042f4SFlorian Westphal 		goto free_now;
6248df5042f4SFlorian Westphal 
6249df5042f4SFlorian Westphal 	if (!refcount_dec_and_test(&ext->refcnt))
6250df5042f4SFlorian Westphal 		return;
6251df5042f4SFlorian Westphal free_now:
62524165079bSFlorian Westphal #ifdef CONFIG_XFRM
62534165079bSFlorian Westphal 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
62544165079bSFlorian Westphal 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
62554165079bSFlorian Westphal #endif
62564165079bSFlorian Westphal 
6257df5042f4SFlorian Westphal 	kmem_cache_free(skbuff_ext_cache, ext);
6258df5042f4SFlorian Westphal }
6259df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put);
6260df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */
6261