xref: /openbmc/linux/net/core/skbuff.c (revision 100f6d8e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Fixes:
81da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
91da177e4SLinus Torvalds  *					balancer bugs.
101da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
111da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
121da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
131da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
141da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
151da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
171da177e4SLinus Torvalds  *					only put in the headers
181da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
191da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
201da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
211da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	NOTE:
241da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
251da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
261da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
271da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
301da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
311da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
321da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40e005d193SJoe Perches 
411da177e4SLinus Torvalds #include <linux/module.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/mm.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/in.h>
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
49de960aa9SFlorian Westphal #include <linux/tcp.h>
50de960aa9SFlorian Westphal #include <linux/udp.h>
5190017accSMarcelo Ricardo Leitner #include <linux/sctp.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
541da177e4SLinus Torvalds #include <net/pkt_sched.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds #include <linux/string.h>
571da177e4SLinus Torvalds #include <linux/skbuff.h>
589c55e01cSJens Axboe #include <linux/splice.h>
591da177e4SLinus Torvalds #include <linux/cache.h>
601da177e4SLinus Torvalds #include <linux/rtnetlink.h>
611da177e4SLinus Torvalds #include <linux/init.h>
62716ea3a7SDavid Howells #include <linux/scatterlist.h>
63ac45f602SPatrick Ohly #include <linux/errqueue.h>
64268bb0ceSLinus Torvalds #include <linux/prefetch.h>
650d5501c1SVlad Yasevich #include <linux/if_vlan.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <net/protocol.h>
681da177e4SLinus Torvalds #include <net/dst.h>
691da177e4SLinus Torvalds #include <net/sock.h>
701da177e4SLinus Torvalds #include <net/checksum.h>
71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
721da177e4SLinus Torvalds #include <net/xfrm.h>
731da177e4SLinus Torvalds 
747c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
75ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
7651c56b00SEric Dumazet #include <linux/highmem.h>
77b245be1fSWillem de Bruijn #include <linux/capability.h>
78b245be1fSWillem de Bruijn #include <linux/user_namespace.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 {
105e005d193SJoe Perches 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p 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 *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
373ffde7328SAlexander Duyck {
374b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
375ffde7328SAlexander Duyck 	unsigned long flags;
376ffde7328SAlexander Duyck 	void *data;
377ffde7328SAlexander Duyck 
378ffde7328SAlexander Duyck 	local_irq_save(flags);
3799451980aSAlexander Duyck 	nc = this_cpu_ptr(&netdev_alloc_cache);
3808c2dd3e4SAlexander Duyck 	data = page_frag_alloc(nc, fragsz, gfp_mask);
3816f532612SEric Dumazet 	local_irq_restore(flags);
3826f532612SEric Dumazet 	return data;
3836f532612SEric Dumazet }
384c93bdd0eSMel Gorman 
385c93bdd0eSMel Gorman /**
386c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
387c93bdd0eSMel Gorman  * @fragsz: fragment size
388c93bdd0eSMel Gorman  *
389c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
390c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
391c93bdd0eSMel Gorman  */
392c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
393c93bdd0eSMel Gorman {
3943bed3cc4SAlexander Duyck 	fragsz = SKB_DATA_ALIGN(fragsz);
3953bed3cc4SAlexander Duyck 
396453f85d4SMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
397c93bdd0eSMel Gorman }
3986f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
3996f532612SEric Dumazet 
400ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
401ffde7328SAlexander Duyck {
402795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
4039451980aSAlexander Duyck 
4048c2dd3e4SAlexander Duyck 	return page_frag_alloc(&nc->page, fragsz, gfp_mask);
405ffde7328SAlexander Duyck }
406ffde7328SAlexander Duyck 
407ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
408ffde7328SAlexander Duyck {
4093bed3cc4SAlexander Duyck 	fragsz = SKB_DATA_ALIGN(fragsz);
4103bed3cc4SAlexander Duyck 
411453f85d4SMel Gorman 	return __napi_alloc_frag(fragsz, GFP_ATOMIC);
412ffde7328SAlexander Duyck }
413ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
414ffde7328SAlexander Duyck 
4156f532612SEric Dumazet /**
416fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
417fd11a83dSAlexander Duyck  *	@dev: network device to receive on
418d7499160SMasanari Iida  *	@len: length to allocate
419fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
420fd11a83dSAlexander Duyck  *
421fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
422fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
423fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
424fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
425fd11a83dSAlexander Duyck  *
426fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
427fd11a83dSAlexander Duyck  */
4289451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
4299451980aSAlexander Duyck 				   gfp_t gfp_mask)
430fd11a83dSAlexander Duyck {
431b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
4329451980aSAlexander Duyck 	unsigned long flags;
433fd11a83dSAlexander Duyck 	struct sk_buff *skb;
4349451980aSAlexander Duyck 	bool pfmemalloc;
4359451980aSAlexander Duyck 	void *data;
436fd11a83dSAlexander Duyck 
4379451980aSAlexander Duyck 	len += NET_SKB_PAD;
438fd11a83dSAlexander Duyck 
4399451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
440d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
441a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
442a080e7bdSAlexander Duyck 		if (!skb)
443a080e7bdSAlexander Duyck 			goto skb_fail;
444a080e7bdSAlexander Duyck 		goto skb_success;
445a080e7bdSAlexander Duyck 	}
4469451980aSAlexander Duyck 
4479451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4489451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
4499451980aSAlexander Duyck 
4509451980aSAlexander Duyck 	if (sk_memalloc_socks())
4519451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
4529451980aSAlexander Duyck 
4539451980aSAlexander Duyck 	local_irq_save(flags);
4549451980aSAlexander Duyck 
4559451980aSAlexander Duyck 	nc = this_cpu_ptr(&netdev_alloc_cache);
4568c2dd3e4SAlexander Duyck 	data = page_frag_alloc(nc, len, gfp_mask);
4579451980aSAlexander Duyck 	pfmemalloc = nc->pfmemalloc;
4589451980aSAlexander Duyck 
4599451980aSAlexander Duyck 	local_irq_restore(flags);
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 	/* use OR instead of assignment to avoid clearing of bits in mask */
4719451980aSAlexander Duyck 	if (pfmemalloc)
4729451980aSAlexander Duyck 		skb->pfmemalloc = 1;
4739451980aSAlexander Duyck 	skb->head_frag = 1;
4749451980aSAlexander Duyck 
475a080e7bdSAlexander Duyck skb_success:
4768af27456SChristoph Hellwig 	skb_reserve(skb, NET_SKB_PAD);
4777b2e497aSChristoph Hellwig 	skb->dev = dev;
478fd11a83dSAlexander Duyck 
479a080e7bdSAlexander Duyck skb_fail:
4808af27456SChristoph Hellwig 	return skb;
4818af27456SChristoph Hellwig }
482b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4831da177e4SLinus Torvalds 
484fd11a83dSAlexander Duyck /**
485fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
486fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
487d7499160SMasanari Iida  *	@len: length to allocate
488fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
489fd11a83dSAlexander Duyck  *
490fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
491fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
492fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
493fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
494fd11a83dSAlexander Duyck  *
495fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
496fd11a83dSAlexander Duyck  */
4979451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
4989451980aSAlexander Duyck 				 gfp_t gfp_mask)
499fd11a83dSAlexander Duyck {
500795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
501fd11a83dSAlexander Duyck 	struct sk_buff *skb;
5029451980aSAlexander Duyck 	void *data;
503fd11a83dSAlexander Duyck 
5049451980aSAlexander Duyck 	len += NET_SKB_PAD + NET_IP_ALIGN;
505fd11a83dSAlexander Duyck 
5069451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
507d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
508a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
509a080e7bdSAlexander Duyck 		if (!skb)
510a080e7bdSAlexander Duyck 			goto skb_fail;
511a080e7bdSAlexander Duyck 		goto skb_success;
512a080e7bdSAlexander Duyck 	}
5139451980aSAlexander Duyck 
5149451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5159451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
5169451980aSAlexander Duyck 
5179451980aSAlexander Duyck 	if (sk_memalloc_socks())
5189451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
5199451980aSAlexander Duyck 
5208c2dd3e4SAlexander Duyck 	data = page_frag_alloc(&nc->page, len, gfp_mask);
5219451980aSAlexander Duyck 	if (unlikely(!data))
5229451980aSAlexander Duyck 		return NULL;
5239451980aSAlexander Duyck 
5249451980aSAlexander Duyck 	skb = __build_skb(data, len);
5259451980aSAlexander Duyck 	if (unlikely(!skb)) {
526181edb2bSAlexander Duyck 		skb_free_frag(data);
5279451980aSAlexander Duyck 		return NULL;
5289451980aSAlexander Duyck 	}
5299451980aSAlexander Duyck 
5309451980aSAlexander Duyck 	/* use OR instead of assignment to avoid clearing of bits in mask */
531795bb1c0SJesper Dangaard Brouer 	if (nc->page.pfmemalloc)
5329451980aSAlexander Duyck 		skb->pfmemalloc = 1;
5339451980aSAlexander Duyck 	skb->head_frag = 1;
5349451980aSAlexander Duyck 
535a080e7bdSAlexander Duyck skb_success:
536fd11a83dSAlexander Duyck 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
537fd11a83dSAlexander Duyck 	skb->dev = napi->dev;
538fd11a83dSAlexander Duyck 
539a080e7bdSAlexander Duyck skb_fail:
540fd11a83dSAlexander Duyck 	return skb;
541fd11a83dSAlexander Duyck }
542fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
543fd11a83dSAlexander Duyck 
544654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
54550269e19SEric Dumazet 		     int size, unsigned int truesize)
546654bed16SPeter Zijlstra {
547654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
548654bed16SPeter Zijlstra 	skb->len += size;
549654bed16SPeter Zijlstra 	skb->data_len += size;
55050269e19SEric Dumazet 	skb->truesize += truesize;
551654bed16SPeter Zijlstra }
552654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
553654bed16SPeter Zijlstra 
554f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
555f8e617e1SJason Wang 			  unsigned int truesize)
556f8e617e1SJason Wang {
557f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
558f8e617e1SJason Wang 
559f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
560f8e617e1SJason Wang 	skb->len += size;
561f8e617e1SJason Wang 	skb->data_len += size;
562f8e617e1SJason Wang 	skb->truesize += truesize;
563f8e617e1SJason Wang }
564f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
565f8e617e1SJason Wang 
56627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5671da177e4SLinus Torvalds {
568bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
56927b437c8SHerbert Xu 	*listp = NULL;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
57227b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
57327b437c8SHerbert Xu {
57427b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
57527b437c8SHerbert Xu }
57627b437c8SHerbert Xu 
5771da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct sk_buff *list;
5801da177e4SLinus Torvalds 
581fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5821da177e4SLinus Torvalds 		skb_get(list);
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
586d3836f21SEric Dumazet {
587181edb2bSAlexander Duyck 	unsigned char *head = skb->head;
588181edb2bSAlexander Duyck 
589d3836f21SEric Dumazet 	if (skb->head_frag)
590181edb2bSAlexander Duyck 		skb_free_frag(head);
591d3836f21SEric Dumazet 	else
592181edb2bSAlexander Duyck 		kfree(head);
593d3836f21SEric Dumazet }
594d3836f21SEric Dumazet 
5955bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
5961da177e4SLinus Torvalds {
597ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
5981da177e4SLinus Torvalds 	int i;
599ff04a771SEric Dumazet 
600ff04a771SEric Dumazet 	if (skb->cloned &&
601ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
602ff04a771SEric Dumazet 			      &shinfo->dataref))
603ff04a771SEric Dumazet 		return;
604ff04a771SEric Dumazet 
605ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
606ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
6071da177e4SLinus Torvalds 
608ff04a771SEric Dumazet 	if (shinfo->frag_list)
609ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
6101da177e4SLinus Torvalds 
6111f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
612d3836f21SEric Dumazet 	skb_free_head(skb);
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /*
6161da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
6171da177e4SLinus Torvalds  */
6182d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
6191da177e4SLinus Torvalds {
620d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
621d179cd12SDavid S. Miller 
622d179cd12SDavid S. Miller 	switch (skb->fclone) {
623d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
6241da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
6256ffe75ebSEric Dumazet 		return;
626d179cd12SDavid S. Miller 
627d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
628d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6296ffe75ebSEric Dumazet 
6306ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
6316ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
6326ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
6336ffe75ebSEric Dumazet 		 */
6342638595aSReshetova, Elena 		if (refcount_read(&fclones->fclone_ref) == 1)
6356ffe75ebSEric Dumazet 			goto fastpath;
636d179cd12SDavid S. Miller 		break;
637d179cd12SDavid S. Miller 
6386ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
639d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
640d179cd12SDavid S. Miller 		break;
6413ff50b79SStephen Hemminger 	}
6422638595aSReshetova, Elena 	if (!refcount_dec_and_test(&fclones->fclone_ref))
6436ffe75ebSEric Dumazet 		return;
6446ffe75ebSEric Dumazet fastpath:
6456ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6480a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb)
6491da177e4SLinus Torvalds {
650adf30907SEric Dumazet 	skb_dst_drop(skb);
6511da177e4SLinus Torvalds 	if (skb->destructor) {
6529c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6531da177e4SLinus Torvalds 		skb->destructor(skb);
6541da177e4SLinus Torvalds 	}
655a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
656cb9c6836SFlorian Westphal 	nf_conntrack_put(skb_nfct(skb));
6572fc72c7bSKOVACS Krisztian #endif
658df5042f4SFlorian Westphal 	skb_ext_put(skb);
65904a4bb55SLennert Buytenhek }
66004a4bb55SLennert Buytenhek 
66104a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
66204a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
66304a4bb55SLennert Buytenhek {
66404a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
665a28b1b90SFlorian Westphal 	if (likely(skb->head))
6662d4baff8SHerbert Xu 		skb_release_data(skb);
6672d4baff8SHerbert Xu }
6681da177e4SLinus Torvalds 
6692d4baff8SHerbert Xu /**
6702d4baff8SHerbert Xu  *	__kfree_skb - private function
6712d4baff8SHerbert Xu  *	@skb: buffer
6722d4baff8SHerbert Xu  *
6732d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6742d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6752d4baff8SHerbert Xu  *	always call kfree_skb
6762d4baff8SHerbert Xu  */
6772d4baff8SHerbert Xu 
6782d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6792d4baff8SHerbert Xu {
6802d4baff8SHerbert Xu 	skb_release_all(skb);
6811da177e4SLinus Torvalds 	kfree_skbmem(skb);
6821da177e4SLinus Torvalds }
683b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /**
686231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
687231d06aeSJörn Engel  *	@skb: buffer to free
688231d06aeSJörn Engel  *
689231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
690231d06aeSJörn Engel  *	hit zero.
691231d06aeSJörn Engel  */
692231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
693231d06aeSJörn Engel {
6943889a803SPaolo Abeni 	if (!skb_unref(skb))
695231d06aeSJörn Engel 		return;
6963889a803SPaolo Abeni 
697ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
698231d06aeSJörn Engel 	__kfree_skb(skb);
699231d06aeSJörn Engel }
700b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
701231d06aeSJörn Engel 
702bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
703bd8a7036SEric Dumazet {
704bd8a7036SEric Dumazet 	while (segs) {
705bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
706bd8a7036SEric Dumazet 
707bd8a7036SEric Dumazet 		kfree_skb(segs);
708bd8a7036SEric Dumazet 		segs = next;
709bd8a7036SEric Dumazet 	}
710bd8a7036SEric Dumazet }
711bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
712bd8a7036SEric Dumazet 
713d1a203eaSStephen Hemminger /**
71425121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
71525121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
71625121173SMichael S. Tsirkin  *
71725121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
71825121173SMichael S. Tsirkin  *	skb must be freed afterwards.
71925121173SMichael S. Tsirkin  */
72025121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
72125121173SMichael S. Tsirkin {
7221f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, true);
72325121173SMichael S. Tsirkin }
72425121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
72525121173SMichael S. Tsirkin 
72625121173SMichael S. Tsirkin /**
727ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
728ead2ceb0SNeil Horman  *	@skb: buffer to free
729ead2ceb0SNeil Horman  *
730ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
731ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
732ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
733ead2ceb0SNeil Horman  */
734ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
735ead2ceb0SNeil Horman {
7363889a803SPaolo Abeni 	if (!skb_unref(skb))
737ead2ceb0SNeil Horman 		return;
7383889a803SPaolo Abeni 
73907dc22e7SKoki Sanagi 	trace_consume_skb(skb);
740ead2ceb0SNeil Horman 	__kfree_skb(skb);
741ead2ceb0SNeil Horman }
742ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
743ead2ceb0SNeil Horman 
7440a463c78SPaolo Abeni /**
7450a463c78SPaolo Abeni  *	consume_stateless_skb - free an skbuff, assuming it is stateless
7460a463c78SPaolo Abeni  *	@skb: buffer to free
7470a463c78SPaolo Abeni  *
748ca2c1418SPaolo Abeni  *	Alike consume_skb(), but this variant assumes that this is the last
749ca2c1418SPaolo Abeni  *	skb reference and all the head states have been already dropped
7500a463c78SPaolo Abeni  */
751ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb)
7520a463c78SPaolo Abeni {
7530a463c78SPaolo Abeni 	trace_consume_skb(skb);
7540a463c78SPaolo Abeni 	skb_release_data(skb);
7550a463c78SPaolo Abeni 	kfree_skbmem(skb);
7560a463c78SPaolo Abeni }
7570a463c78SPaolo Abeni 
758795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void)
759795bb1c0SJesper Dangaard Brouer {
760795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
761795bb1c0SJesper Dangaard Brouer 
762795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if containing objects */
763795bb1c0SJesper Dangaard Brouer 	if (nc->skb_count) {
764795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
765795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
766795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
767795bb1c0SJesper Dangaard Brouer 	}
768795bb1c0SJesper Dangaard Brouer }
769795bb1c0SJesper Dangaard Brouer 
77015fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb)
771795bb1c0SJesper Dangaard Brouer {
772795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
773795bb1c0SJesper Dangaard Brouer 
774795bb1c0SJesper Dangaard Brouer 	/* drop skb->head and call any destructors for packet */
775795bb1c0SJesper Dangaard Brouer 	skb_release_all(skb);
776795bb1c0SJesper Dangaard Brouer 
777795bb1c0SJesper Dangaard Brouer 	/* record skb to CPU local list */
778795bb1c0SJesper Dangaard Brouer 	nc->skb_cache[nc->skb_count++] = skb;
779795bb1c0SJesper Dangaard Brouer 
780795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB
781795bb1c0SJesper Dangaard Brouer 	/* SLUB writes into objects when freeing */
782795bb1c0SJesper Dangaard Brouer 	prefetchw(skb);
783795bb1c0SJesper Dangaard Brouer #endif
784795bb1c0SJesper Dangaard Brouer 
785795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if it is filled */
786795bb1c0SJesper Dangaard Brouer 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
787795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
788795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
789795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
790795bb1c0SJesper Dangaard Brouer 	}
791795bb1c0SJesper Dangaard Brouer }
79215fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb)
79315fad714SJesper Dangaard Brouer {
79415fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
79515fad714SJesper Dangaard Brouer }
796795bb1c0SJesper Dangaard Brouer 
797795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
798795bb1c0SJesper Dangaard Brouer {
799795bb1c0SJesper Dangaard Brouer 	if (unlikely(!skb))
800795bb1c0SJesper Dangaard Brouer 		return;
801795bb1c0SJesper Dangaard Brouer 
802885eb0a5SJesper Dangaard Brouer 	/* Zero budget indicate non-NAPI context called us, like netpoll */
803795bb1c0SJesper Dangaard Brouer 	if (unlikely(!budget)) {
804885eb0a5SJesper Dangaard Brouer 		dev_consume_skb_any(skb);
805795bb1c0SJesper Dangaard Brouer 		return;
806795bb1c0SJesper Dangaard Brouer 	}
807795bb1c0SJesper Dangaard Brouer 
8087608894eSPaolo Abeni 	if (!skb_unref(skb))
809795bb1c0SJesper Dangaard Brouer 		return;
8107608894eSPaolo Abeni 
811795bb1c0SJesper Dangaard Brouer 	/* if reaching here SKB is ready to free */
812795bb1c0SJesper Dangaard Brouer 	trace_consume_skb(skb);
813795bb1c0SJesper Dangaard Brouer 
814795bb1c0SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
815abbdb5a7SEric Dumazet 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
816795bb1c0SJesper Dangaard Brouer 		__kfree_skb(skb);
817795bb1c0SJesper Dangaard Brouer 		return;
818795bb1c0SJesper Dangaard Brouer 	}
819795bb1c0SJesper Dangaard Brouer 
82015fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
821795bb1c0SJesper Dangaard Brouer }
822795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
823795bb1c0SJesper Dangaard Brouer 
824b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
825b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
826b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
827b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
828b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
829b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
830b1937227SEric Dumazet 
831dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
832dec18810SHerbert Xu {
833dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
834b1937227SEric Dumazet 	/* We do not copy old->sk */
835dec18810SHerbert Xu 	new->dev		= old->dev;
836b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
8377fee226aSEric Dumazet 	skb_dst_copy(new, old);
838df5042f4SFlorian Westphal 	__skb_ext_copy(new, old);
839b1937227SEric Dumazet 	__nf_copy(new, old, false);
8406aa895b0SPatrick McHardy 
841b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
842b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
843b1937227SEric Dumazet 	 */
844b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
84506021292SEliezer Tamir 
846b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
847b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
848b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
849b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
850b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
851b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
852b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
853b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
854b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
855b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
856b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
857b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
858b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
859b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
860b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
861b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
862b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
863b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
864b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
865b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
866b1937227SEric Dumazet #endif
867e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
868b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
86906021292SEliezer Tamir #endif
8702bd82484SEric Dumazet #ifdef CONFIG_XPS
8712bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
8722bd82484SEric Dumazet #endif
873b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
874b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
875b1937227SEric Dumazet #endif
876b1937227SEric Dumazet 
877dec18810SHerbert Xu }
878dec18810SHerbert Xu 
87982c49a35SHerbert Xu /*
88082c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
88182c49a35SHerbert Xu  * __copy_skb_header above instead.
88282c49a35SHerbert Xu  */
883e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
8841da177e4SLinus Torvalds {
8851da177e4SLinus Torvalds #define C(x) n->x = skb->x
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	n->next = n->prev = NULL;
8881da177e4SLinus Torvalds 	n->sk = NULL;
889dec18810SHerbert Xu 	__copy_skb_header(n, skb);
890dec18810SHerbert Xu 
8911da177e4SLinus Torvalds 	C(len);
8921da177e4SLinus Torvalds 	C(data_len);
8933e6b3b2eSAlexey Dobriyan 	C(mac_len);
894334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
89502f1c89dSPaul Moore 	n->cloned = 1;
8961da177e4SLinus Torvalds 	n->nohdr = 0;
897b13dda9fSEric Dumazet 	n->peeked = 0;
898e78bfb07SStefano Brivio 	C(pfmemalloc);
8991da177e4SLinus Torvalds 	n->destructor = NULL;
9001da177e4SLinus Torvalds 	C(tail);
9011da177e4SLinus Torvalds 	C(end);
90202f1c89dSPaul Moore 	C(head);
903d3836f21SEric Dumazet 	C(head_frag);
90402f1c89dSPaul Moore 	C(data);
90502f1c89dSPaul Moore 	C(truesize);
90663354797SReshetova, Elena 	refcount_set(&n->users, 1);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
9091da177e4SLinus Torvalds 	skb->cloned = 1;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	return n;
912e0053ec0SHerbert Xu #undef C
913e0053ec0SHerbert Xu }
914e0053ec0SHerbert Xu 
915e0053ec0SHerbert Xu /**
916e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
917e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
918e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
919e0053ec0SHerbert Xu  *
920e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
921e0053ec0SHerbert Xu  *	supplied by the user.
922e0053ec0SHerbert Xu  *
923e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
924e0053ec0SHerbert Xu  */
925e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
926e0053ec0SHerbert Xu {
9272d4baff8SHerbert Xu 	skb_release_all(dst);
928e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
929e0053ec0SHerbert Xu }
930e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
931e0053ec0SHerbert Xu 
9326f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
933a91dbff5SWillem de Bruijn {
934a91dbff5SWillem de Bruijn 	unsigned long max_pg, num_pg, new_pg, old_pg;
935a91dbff5SWillem de Bruijn 	struct user_struct *user;
936a91dbff5SWillem de Bruijn 
937a91dbff5SWillem de Bruijn 	if (capable(CAP_IPC_LOCK) || !size)
938a91dbff5SWillem de Bruijn 		return 0;
939a91dbff5SWillem de Bruijn 
940a91dbff5SWillem de Bruijn 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
941a91dbff5SWillem de Bruijn 	max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
942a91dbff5SWillem de Bruijn 	user = mmp->user ? : current_user();
943a91dbff5SWillem de Bruijn 
944a91dbff5SWillem de Bruijn 	do {
945a91dbff5SWillem de Bruijn 		old_pg = atomic_long_read(&user->locked_vm);
946a91dbff5SWillem de Bruijn 		new_pg = old_pg + num_pg;
947a91dbff5SWillem de Bruijn 		if (new_pg > max_pg)
948a91dbff5SWillem de Bruijn 			return -ENOBUFS;
949a91dbff5SWillem de Bruijn 	} while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
950a91dbff5SWillem de Bruijn 		 old_pg);
951a91dbff5SWillem de Bruijn 
952a91dbff5SWillem de Bruijn 	if (!mmp->user) {
953a91dbff5SWillem de Bruijn 		mmp->user = get_uid(user);
954a91dbff5SWillem de Bruijn 		mmp->num_pg = num_pg;
955a91dbff5SWillem de Bruijn 	} else {
956a91dbff5SWillem de Bruijn 		mmp->num_pg += num_pg;
957a91dbff5SWillem de Bruijn 	}
958a91dbff5SWillem de Bruijn 
959a91dbff5SWillem de Bruijn 	return 0;
960a91dbff5SWillem de Bruijn }
9616f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
962a91dbff5SWillem de Bruijn 
9636f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp)
964a91dbff5SWillem de Bruijn {
965a91dbff5SWillem de Bruijn 	if (mmp->user) {
966a91dbff5SWillem de Bruijn 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
967a91dbff5SWillem de Bruijn 		free_uid(mmp->user);
968a91dbff5SWillem de Bruijn 	}
969a91dbff5SWillem de Bruijn }
9706f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
971a91dbff5SWillem de Bruijn 
97252267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
97352267790SWillem de Bruijn {
97452267790SWillem de Bruijn 	struct ubuf_info *uarg;
97552267790SWillem de Bruijn 	struct sk_buff *skb;
97652267790SWillem de Bruijn 
97752267790SWillem de Bruijn 	WARN_ON_ONCE(!in_task());
97852267790SWillem de Bruijn 
97952267790SWillem de Bruijn 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
98052267790SWillem de Bruijn 	if (!skb)
98152267790SWillem de Bruijn 		return NULL;
98252267790SWillem de Bruijn 
98352267790SWillem de Bruijn 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
98452267790SWillem de Bruijn 	uarg = (void *)skb->cb;
985a91dbff5SWillem de Bruijn 	uarg->mmp.user = NULL;
986a91dbff5SWillem de Bruijn 
987a91dbff5SWillem de Bruijn 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
988a91dbff5SWillem de Bruijn 		kfree_skb(skb);
989a91dbff5SWillem de Bruijn 		return NULL;
990a91dbff5SWillem de Bruijn 	}
99152267790SWillem de Bruijn 
99252267790SWillem de Bruijn 	uarg->callback = sock_zerocopy_callback;
9934ab6c99dSWillem de Bruijn 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
9944ab6c99dSWillem de Bruijn 	uarg->len = 1;
9954ab6c99dSWillem de Bruijn 	uarg->bytelen = size;
99652267790SWillem de Bruijn 	uarg->zerocopy = 1;
997c1d1b437SEric Dumazet 	refcount_set(&uarg->refcnt, 1);
99852267790SWillem de Bruijn 	sock_hold(sk);
99952267790SWillem de Bruijn 
100052267790SWillem de Bruijn 	return uarg;
100152267790SWillem de Bruijn }
100252267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
100352267790SWillem de Bruijn 
100452267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
100552267790SWillem de Bruijn {
100652267790SWillem de Bruijn 	return container_of((void *)uarg, struct sk_buff, cb);
100752267790SWillem de Bruijn }
100852267790SWillem de Bruijn 
10094ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
10104ab6c99dSWillem de Bruijn 					struct ubuf_info *uarg)
10114ab6c99dSWillem de Bruijn {
10124ab6c99dSWillem de Bruijn 	if (uarg) {
10134ab6c99dSWillem de Bruijn 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
10144ab6c99dSWillem de Bruijn 		u32 bytelen, next;
10154ab6c99dSWillem de Bruijn 
10164ab6c99dSWillem de Bruijn 		/* realloc only when socket is locked (TCP, UDP cork),
10174ab6c99dSWillem de Bruijn 		 * so uarg->len and sk_zckey access is serialized
10184ab6c99dSWillem de Bruijn 		 */
10194ab6c99dSWillem de Bruijn 		if (!sock_owned_by_user(sk)) {
10204ab6c99dSWillem de Bruijn 			WARN_ON_ONCE(1);
10214ab6c99dSWillem de Bruijn 			return NULL;
10224ab6c99dSWillem de Bruijn 		}
10234ab6c99dSWillem de Bruijn 
10244ab6c99dSWillem de Bruijn 		bytelen = uarg->bytelen + size;
10254ab6c99dSWillem de Bruijn 		if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
10264ab6c99dSWillem de Bruijn 			/* TCP can create new skb to attach new uarg */
10274ab6c99dSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
10284ab6c99dSWillem de Bruijn 				goto new_alloc;
10294ab6c99dSWillem de Bruijn 			return NULL;
10304ab6c99dSWillem de Bruijn 		}
10314ab6c99dSWillem de Bruijn 
10324ab6c99dSWillem de Bruijn 		next = (u32)atomic_read(&sk->sk_zckey);
10334ab6c99dSWillem de Bruijn 		if ((u32)(uarg->id + uarg->len) == next) {
1034a91dbff5SWillem de Bruijn 			if (mm_account_pinned_pages(&uarg->mmp, size))
1035a91dbff5SWillem de Bruijn 				return NULL;
10364ab6c99dSWillem de Bruijn 			uarg->len++;
10374ab6c99dSWillem de Bruijn 			uarg->bytelen = bytelen;
10384ab6c99dSWillem de Bruijn 			atomic_set(&sk->sk_zckey, ++next);
1039100f6d8eSWillem de Bruijn 
1040100f6d8eSWillem de Bruijn 			/* no extra ref when appending to datagram (MSG_MORE) */
1041100f6d8eSWillem de Bruijn 			if (sk->sk_type == SOCK_STREAM)
1042db5bce32SEric Dumazet 				sock_zerocopy_get(uarg);
1043100f6d8eSWillem de Bruijn 
10444ab6c99dSWillem de Bruijn 			return uarg;
10454ab6c99dSWillem de Bruijn 		}
10464ab6c99dSWillem de Bruijn 	}
10474ab6c99dSWillem de Bruijn 
10484ab6c99dSWillem de Bruijn new_alloc:
10494ab6c99dSWillem de Bruijn 	return sock_zerocopy_alloc(sk, size);
10504ab6c99dSWillem de Bruijn }
10514ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
10524ab6c99dSWillem de Bruijn 
10534ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
10544ab6c99dSWillem de Bruijn {
10554ab6c99dSWillem de Bruijn 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
10564ab6c99dSWillem de Bruijn 	u32 old_lo, old_hi;
10574ab6c99dSWillem de Bruijn 	u64 sum_len;
10584ab6c99dSWillem de Bruijn 
10594ab6c99dSWillem de Bruijn 	old_lo = serr->ee.ee_info;
10604ab6c99dSWillem de Bruijn 	old_hi = serr->ee.ee_data;
10614ab6c99dSWillem de Bruijn 	sum_len = old_hi - old_lo + 1ULL + len;
10624ab6c99dSWillem de Bruijn 
10634ab6c99dSWillem de Bruijn 	if (sum_len >= (1ULL << 32))
10644ab6c99dSWillem de Bruijn 		return false;
10654ab6c99dSWillem de Bruijn 
10664ab6c99dSWillem de Bruijn 	if (lo != old_hi + 1)
10674ab6c99dSWillem de Bruijn 		return false;
10684ab6c99dSWillem de Bruijn 
10694ab6c99dSWillem de Bruijn 	serr->ee.ee_data += len;
10704ab6c99dSWillem de Bruijn 	return true;
10714ab6c99dSWillem de Bruijn }
10724ab6c99dSWillem de Bruijn 
107352267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
107452267790SWillem de Bruijn {
10754ab6c99dSWillem de Bruijn 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
107652267790SWillem de Bruijn 	struct sock_exterr_skb *serr;
107752267790SWillem de Bruijn 	struct sock *sk = skb->sk;
10784ab6c99dSWillem de Bruijn 	struct sk_buff_head *q;
10794ab6c99dSWillem de Bruijn 	unsigned long flags;
10804ab6c99dSWillem de Bruijn 	u32 lo, hi;
10814ab6c99dSWillem de Bruijn 	u16 len;
108252267790SWillem de Bruijn 
1083ccaffff1SWillem de Bruijn 	mm_unaccount_pinned_pages(&uarg->mmp);
1084ccaffff1SWillem de Bruijn 
10854ab6c99dSWillem de Bruijn 	/* if !len, there was only 1 call, and it was aborted
10864ab6c99dSWillem de Bruijn 	 * so do not queue a completion notification
10874ab6c99dSWillem de Bruijn 	 */
10884ab6c99dSWillem de Bruijn 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
108952267790SWillem de Bruijn 		goto release;
109052267790SWillem de Bruijn 
10914ab6c99dSWillem de Bruijn 	len = uarg->len;
10924ab6c99dSWillem de Bruijn 	lo = uarg->id;
10934ab6c99dSWillem de Bruijn 	hi = uarg->id + len - 1;
10944ab6c99dSWillem de Bruijn 
109552267790SWillem de Bruijn 	serr = SKB_EXT_ERR(skb);
109652267790SWillem de Bruijn 	memset(serr, 0, sizeof(*serr));
109752267790SWillem de Bruijn 	serr->ee.ee_errno = 0;
109852267790SWillem de Bruijn 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
10994ab6c99dSWillem de Bruijn 	serr->ee.ee_data = hi;
11004ab6c99dSWillem de Bruijn 	serr->ee.ee_info = lo;
110152267790SWillem de Bruijn 	if (!success)
110252267790SWillem de Bruijn 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
110352267790SWillem de Bruijn 
11044ab6c99dSWillem de Bruijn 	q = &sk->sk_error_queue;
11054ab6c99dSWillem de Bruijn 	spin_lock_irqsave(&q->lock, flags);
11064ab6c99dSWillem de Bruijn 	tail = skb_peek_tail(q);
11074ab6c99dSWillem de Bruijn 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
11084ab6c99dSWillem de Bruijn 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
11094ab6c99dSWillem de Bruijn 		__skb_queue_tail(q, skb);
111052267790SWillem de Bruijn 		skb = NULL;
11114ab6c99dSWillem de Bruijn 	}
11124ab6c99dSWillem de Bruijn 	spin_unlock_irqrestore(&q->lock, flags);
111352267790SWillem de Bruijn 
111452267790SWillem de Bruijn 	sk->sk_error_report(sk);
111552267790SWillem de Bruijn 
111652267790SWillem de Bruijn release:
111752267790SWillem de Bruijn 	consume_skb(skb);
111852267790SWillem de Bruijn 	sock_put(sk);
111952267790SWillem de Bruijn }
112052267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
112152267790SWillem de Bruijn 
112252267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg)
112352267790SWillem de Bruijn {
1124c1d1b437SEric Dumazet 	if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
112552267790SWillem de Bruijn 		if (uarg->callback)
112652267790SWillem de Bruijn 			uarg->callback(uarg, uarg->zerocopy);
112752267790SWillem de Bruijn 		else
112852267790SWillem de Bruijn 			consume_skb(skb_from_uarg(uarg));
112952267790SWillem de Bruijn 	}
113052267790SWillem de Bruijn }
113152267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put);
113252267790SWillem de Bruijn 
113352900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
113452267790SWillem de Bruijn {
113552267790SWillem de Bruijn 	if (uarg) {
113652267790SWillem de Bruijn 		struct sock *sk = skb_from_uarg(uarg)->sk;
113752267790SWillem de Bruijn 
113852267790SWillem de Bruijn 		atomic_dec(&sk->sk_zckey);
11394ab6c99dSWillem de Bruijn 		uarg->len--;
114052267790SWillem de Bruijn 
114152900d22SWillem de Bruijn 		if (have_uref)
114252267790SWillem de Bruijn 			sock_zerocopy_put(uarg);
114352267790SWillem de Bruijn 	}
114452267790SWillem de Bruijn }
114552267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
114652267790SWillem de Bruijn 
1147b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1148b5947e5dSWillem de Bruijn {
1149b5947e5dSWillem de Bruijn 	return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1150b5947e5dSWillem de Bruijn }
1151b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1152b5947e5dSWillem de Bruijn 
115352267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
115452267790SWillem de Bruijn 			     struct msghdr *msg, int len,
115552267790SWillem de Bruijn 			     struct ubuf_info *uarg)
115652267790SWillem de Bruijn {
11574ab6c99dSWillem de Bruijn 	struct ubuf_info *orig_uarg = skb_zcopy(skb);
115852267790SWillem de Bruijn 	struct iov_iter orig_iter = msg->msg_iter;
115952267790SWillem de Bruijn 	int err, orig_len = skb->len;
116052267790SWillem de Bruijn 
11614ab6c99dSWillem de Bruijn 	/* An skb can only point to one uarg. This edge case happens when
11624ab6c99dSWillem de Bruijn 	 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
11634ab6c99dSWillem de Bruijn 	 */
11644ab6c99dSWillem de Bruijn 	if (orig_uarg && uarg != orig_uarg)
11654ab6c99dSWillem de Bruijn 		return -EEXIST;
11664ab6c99dSWillem de Bruijn 
116752267790SWillem de Bruijn 	err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
116852267790SWillem de Bruijn 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
116954d43117SWillem de Bruijn 		struct sock *save_sk = skb->sk;
117054d43117SWillem de Bruijn 
117152267790SWillem de Bruijn 		/* Streams do not free skb on error. Reset to prev state. */
117252267790SWillem de Bruijn 		msg->msg_iter = orig_iter;
117354d43117SWillem de Bruijn 		skb->sk = sk;
117452267790SWillem de Bruijn 		___pskb_trim(skb, orig_len);
117554d43117SWillem de Bruijn 		skb->sk = save_sk;
117652267790SWillem de Bruijn 		return err;
117752267790SWillem de Bruijn 	}
117852267790SWillem de Bruijn 
117952900d22SWillem de Bruijn 	skb_zcopy_set(skb, uarg, NULL);
118052267790SWillem de Bruijn 	return skb->len - orig_len;
118152267790SWillem de Bruijn }
118252267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
118352267790SWillem de Bruijn 
11841f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
118552267790SWillem de Bruijn 			      gfp_t gfp_mask)
118652267790SWillem de Bruijn {
118752267790SWillem de Bruijn 	if (skb_zcopy(orig)) {
118852267790SWillem de Bruijn 		if (skb_zcopy(nskb)) {
118952267790SWillem de Bruijn 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
119052267790SWillem de Bruijn 			if (!gfp_mask) {
119152267790SWillem de Bruijn 				WARN_ON_ONCE(1);
119252267790SWillem de Bruijn 				return -ENOMEM;
119352267790SWillem de Bruijn 			}
119452267790SWillem de Bruijn 			if (skb_uarg(nskb) == skb_uarg(orig))
119552267790SWillem de Bruijn 				return 0;
119652267790SWillem de Bruijn 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
119752267790SWillem de Bruijn 				return -EIO;
119852267790SWillem de Bruijn 		}
119952900d22SWillem de Bruijn 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
120052267790SWillem de Bruijn 	}
120152267790SWillem de Bruijn 	return 0;
120252267790SWillem de Bruijn }
120352267790SWillem de Bruijn 
12042c53040fSBen Hutchings /**
12052c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
120648c83012SMichael S. Tsirkin  *	@skb: the skb to modify
120748c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
120848c83012SMichael S. Tsirkin  *
120948c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
121048c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
121148c83012SMichael S. Tsirkin  *	to userspace pages.
121248c83012SMichael S. Tsirkin  *
121348c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
121448c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
121548c83012SMichael S. Tsirkin  *
121648c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
121748c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
121848c83012SMichael S. Tsirkin  */
121948c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1220a6686f2fSShirley Ma {
1221a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
1222a6686f2fSShirley Ma 	struct page *page, *head = NULL;
12233ece7826SWillem de Bruijn 	int i, new_frags;
12243ece7826SWillem de Bruijn 	u32 d_off;
1225a6686f2fSShirley Ma 
12263ece7826SWillem de Bruijn 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
12273ece7826SWillem de Bruijn 		return -EINVAL;
12283ece7826SWillem de Bruijn 
1229f72c4ac6SWillem de Bruijn 	if (!num_frags)
1230f72c4ac6SWillem de Bruijn 		goto release;
1231f72c4ac6SWillem de Bruijn 
12323ece7826SWillem de Bruijn 	new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
12333ece7826SWillem de Bruijn 	for (i = 0; i < new_frags; i++) {
123402756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
1235a6686f2fSShirley Ma 		if (!page) {
1236a6686f2fSShirley Ma 			while (head) {
123740dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
1238a6686f2fSShirley Ma 				put_page(head);
1239a6686f2fSShirley Ma 				head = next;
1240a6686f2fSShirley Ma 			}
1241a6686f2fSShirley Ma 			return -ENOMEM;
1242a6686f2fSShirley Ma 		}
12433ece7826SWillem de Bruijn 		set_page_private(page, (unsigned long)head);
12443ece7826SWillem de Bruijn 		head = page;
12453ece7826SWillem de Bruijn 	}
12463ece7826SWillem de Bruijn 
12473ece7826SWillem de Bruijn 	page = head;
12483ece7826SWillem de Bruijn 	d_off = 0;
12493ece7826SWillem de Bruijn 	for (i = 0; i < num_frags; i++) {
12503ece7826SWillem de Bruijn 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
12513ece7826SWillem de Bruijn 		u32 p_off, p_len, copied;
12523ece7826SWillem de Bruijn 		struct page *p;
12533ece7826SWillem de Bruijn 		u8 *vaddr;
1254c613c209SWillem de Bruijn 
1255c613c209SWillem de Bruijn 		skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1256c613c209SWillem de Bruijn 				      p, p_off, p_len, copied) {
12573ece7826SWillem de Bruijn 			u32 copy, done = 0;
1258c613c209SWillem de Bruijn 			vaddr = kmap_atomic(p);
12593ece7826SWillem de Bruijn 
12603ece7826SWillem de Bruijn 			while (done < p_len) {
12613ece7826SWillem de Bruijn 				if (d_off == PAGE_SIZE) {
12623ece7826SWillem de Bruijn 					d_off = 0;
12633ece7826SWillem de Bruijn 					page = (struct page *)page_private(page);
12643ece7826SWillem de Bruijn 				}
12653ece7826SWillem de Bruijn 				copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
12663ece7826SWillem de Bruijn 				memcpy(page_address(page) + d_off,
12673ece7826SWillem de Bruijn 				       vaddr + p_off + done, copy);
12683ece7826SWillem de Bruijn 				done += copy;
12693ece7826SWillem de Bruijn 				d_off += copy;
12703ece7826SWillem de Bruijn 			}
127151c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1272c613c209SWillem de Bruijn 		}
1273a6686f2fSShirley Ma 	}
1274a6686f2fSShirley Ma 
1275a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
127602756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
1277a8605c60SIan Campbell 		skb_frag_unref(skb, i);
1278a6686f2fSShirley Ma 
1279a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
12803ece7826SWillem de Bruijn 	for (i = 0; i < new_frags - 1; i++) {
12813ece7826SWillem de Bruijn 		__skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
128240dadff2SSunghan Suh 		head = (struct page *)page_private(head);
1283a6686f2fSShirley Ma 	}
12843ece7826SWillem de Bruijn 	__skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
12853ece7826SWillem de Bruijn 	skb_shinfo(skb)->nr_frags = new_frags;
128648c83012SMichael S. Tsirkin 
1287b90ddd56SWillem de Bruijn release:
12881f8b977aSWillem de Bruijn 	skb_zcopy_clear(skb, false);
1289a6686f2fSShirley Ma 	return 0;
1290a6686f2fSShirley Ma }
1291dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1292a6686f2fSShirley Ma 
1293e0053ec0SHerbert Xu /**
1294e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
1295e0053ec0SHerbert Xu  *	@skb: buffer to clone
1296e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
1297e0053ec0SHerbert Xu  *
1298e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1299e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
1300e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
1301e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
1302e0053ec0SHerbert Xu  *
1303e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
1304e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
1305e0053ec0SHerbert Xu  */
1306e0053ec0SHerbert Xu 
1307e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1308e0053ec0SHerbert Xu {
1309d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
1310d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
1311d0bf4a9eSEric Dumazet 						       skb1);
13126ffe75ebSEric Dumazet 	struct sk_buff *n;
1313e0053ec0SHerbert Xu 
131470008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
1315a6686f2fSShirley Ma 		return NULL;
1316a6686f2fSShirley Ma 
1317e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
13182638595aSReshetova, Elena 	    refcount_read(&fclones->fclone_ref) == 1) {
13196ffe75ebSEric Dumazet 		n = &fclones->skb2;
13202638595aSReshetova, Elena 		refcount_set(&fclones->fclone_ref, 2);
1321e0053ec0SHerbert Xu 	} else {
1322c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
1323c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
1324c93bdd0eSMel Gorman 
1325e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1326e0053ec0SHerbert Xu 		if (!n)
1327e0053ec0SHerbert Xu 			return NULL;
1328fe55f6d5SVegard Nossum 
1329e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1330e0053ec0SHerbert Xu 	}
1331e0053ec0SHerbert Xu 
1332e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
13331da177e4SLinus Torvalds }
1334b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
13351da177e4SLinus Torvalds 
1336b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off)
1337f5b17294SPravin B Shelar {
1338030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
1339030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1340030737bcSEric Dumazet 		skb->csum_start += off;
1341f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1342f5b17294SPravin B Shelar 	skb->transport_header += off;
1343f5b17294SPravin B Shelar 	skb->network_header   += off;
1344f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1345f5b17294SPravin B Shelar 		skb->mac_header += off;
1346f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1347f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1348aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1349f5b17294SPravin B Shelar }
1350b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update);
1351f5b17294SPravin B Shelar 
135208303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
13531da177e4SLinus Torvalds {
1354dec18810SHerbert Xu 	__copy_skb_header(new, old);
1355dec18810SHerbert Xu 
13567967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
13577967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
13587967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
13591da177e4SLinus Torvalds }
136008303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header);
13611da177e4SLinus Torvalds 
1362c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1363c93bdd0eSMel Gorman {
1364c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1365c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1366c93bdd0eSMel Gorman 	return 0;
1367c93bdd0eSMel Gorman }
1368c93bdd0eSMel Gorman 
13691da177e4SLinus Torvalds /**
13701da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
13711da177e4SLinus Torvalds  *	@skb: buffer to copy
13721da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
13731da177e4SLinus Torvalds  *
13741da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
13751da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
13761da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
13771da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
13781da177e4SLinus Torvalds  *
13791da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
13801da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
13811da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
13821da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
13831da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
13841da177e4SLinus Torvalds  */
13851da177e4SLinus Torvalds 
1386dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
13871da177e4SLinus Torvalds {
13886602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1389ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1390c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1391c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
13926602cebbSEric Dumazet 
13931da177e4SLinus Torvalds 	if (!n)
13941da177e4SLinus Torvalds 		return NULL;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	/* Set the data pointer */
13971da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
13981da177e4SLinus Torvalds 	/* Set the tail pointer and length */
13991da177e4SLinus Torvalds 	skb_put(n, skb->len);
14001da177e4SLinus Torvalds 
14019f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
14021da177e4SLinus Torvalds 
140308303c18SIlya Lesokhin 	skb_copy_header(n, skb);
14041da177e4SLinus Torvalds 	return n;
14051da177e4SLinus Torvalds }
1406b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds /**
1409bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
14101da177e4SLinus Torvalds  *	@skb: buffer to copy
1411117632e6SEric Dumazet  *	@headroom: headroom of new skb
14121da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1413bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1414bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1415bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
14161da177e4SLinus Torvalds  *
14171da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
14181da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
14191da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
14201da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
14211da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
14221da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
14231da177e4SLinus Torvalds  */
14241da177e4SLinus Torvalds 
1425bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1426bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
14271da177e4SLinus Torvalds {
1428117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1429bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1430bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
14316602cebbSEric Dumazet 
14321da177e4SLinus Torvalds 	if (!n)
14331da177e4SLinus Torvalds 		goto out;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/* Set the data pointer */
1436117632e6SEric Dumazet 	skb_reserve(n, headroom);
14371da177e4SLinus Torvalds 	/* Set the tail pointer and length */
14381da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
14391da177e4SLinus Torvalds 	/* Copy the bytes */
1440d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
14411da177e4SLinus Torvalds 
144225f484a6SHerbert Xu 	n->truesize += skb->data_len;
14431da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
14441da177e4SLinus Torvalds 	n->len	     = skb->len;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
14471da177e4SLinus Torvalds 		int i;
14481da177e4SLinus Torvalds 
14491f8b977aSWillem de Bruijn 		if (skb_orphan_frags(skb, gfp_mask) ||
14501f8b977aSWillem de Bruijn 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
14511511022cSDan Carpenter 			kfree_skb(n);
14521511022cSDan Carpenter 			n = NULL;
1453a6686f2fSShirley Ma 			goto out;
1454a6686f2fSShirley Ma 		}
14551da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
14561da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1457ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
14581da177e4SLinus Torvalds 		}
14591da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
14601da177e4SLinus Torvalds 	}
14611da177e4SLinus Torvalds 
146221dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
14631da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
14641da177e4SLinus Torvalds 		skb_clone_fraglist(n);
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 
146708303c18SIlya Lesokhin 	skb_copy_header(n, skb);
14681da177e4SLinus Torvalds out:
14691da177e4SLinus Torvalds 	return n;
14701da177e4SLinus Torvalds }
1471bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds /**
14741da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
14751da177e4SLinus Torvalds  *	@skb: buffer to reallocate
14761da177e4SLinus Torvalds  *	@nhead: room to add at head
14771da177e4SLinus Torvalds  *	@ntail: room to add at tail
14781da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
14791da177e4SLinus Torvalds  *
1480bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1481bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
14821da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
14831da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
14841da177e4SLinus Torvalds  *
14851da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
14861da177e4SLinus Torvalds  *	reloaded after call to this function.
14871da177e4SLinus Torvalds  */
14881da177e4SLinus Torvalds 
148986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1490dd0fc66fSAl Viro 		     gfp_t gfp_mask)
14911da177e4SLinus Torvalds {
1492158f323bSEric Dumazet 	int i, osize = skb_end_offset(skb);
1493158f323bSEric Dumazet 	int size = osize + nhead + ntail;
14941da177e4SLinus Torvalds 	long off;
1495158f323bSEric Dumazet 	u8 *data;
14961da177e4SLinus Torvalds 
14974edd87adSHerbert Xu 	BUG_ON(nhead < 0);
14984edd87adSHerbert Xu 
14999f77fad3STim Hansen 	BUG_ON(skb_shared(skb));
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
15021da177e4SLinus Torvalds 
1503c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1504c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1505c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1506c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
15071da177e4SLinus Torvalds 	if (!data)
15081da177e4SLinus Torvalds 		goto nodata;
150987151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
15126602cebbSEric Dumazet 	 * optimized for the cases when header is void.
15136602cebbSEric Dumazet 	 */
15146602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
15156602cebbSEric Dumazet 
15166602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
15176602cebbSEric Dumazet 	       skb_shinfo(skb),
1518fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
15191da177e4SLinus Torvalds 
15203e24591aSAlexander Duyck 	/*
15213e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
15223e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
15233e24591aSAlexander Duyck 	 * be since all we did is relocate the values
15243e24591aSAlexander Duyck 	 */
15253e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
152670008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1527a6686f2fSShirley Ma 			goto nofrags;
15281f8b977aSWillem de Bruijn 		if (skb_zcopy(skb))
1529c1d1b437SEric Dumazet 			refcount_inc(&skb_uarg(skb)->refcnt);
15301da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1531ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
15321da177e4SLinus Torvalds 
153321dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
15341da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 		skb_release_data(skb);
15373e24591aSAlexander Duyck 	} else {
15383e24591aSAlexander Duyck 		skb_free_head(skb);
15391fd63041SEric Dumazet 	}
15401da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	skb->head     = data;
1543d3836f21SEric Dumazet 	skb->head_frag = 0;
15441da177e4SLinus Torvalds 	skb->data    += off;
15454305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
15464305b541SArnaldo Carvalho de Melo 	skb->end      = size;
154756eb8882SPatrick McHardy 	off           = nhead;
15484305b541SArnaldo Carvalho de Melo #else
15494305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
155056eb8882SPatrick McHardy #endif
155127a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1552b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
15531da177e4SLinus Torvalds 	skb->cloned   = 0;
1554334a8132SPatrick McHardy 	skb->hdr_len  = 0;
15551da177e4SLinus Torvalds 	skb->nohdr    = 0;
15561da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1557158f323bSEric Dumazet 
1558de8f3a83SDaniel Borkmann 	skb_metadata_clear(skb);
1559de8f3a83SDaniel Borkmann 
1560158f323bSEric Dumazet 	/* It is not generally safe to change skb->truesize.
1561158f323bSEric Dumazet 	 * For the moment, we really care of rx path, or
1562158f323bSEric Dumazet 	 * when skb is orphaned (not attached to a socket).
1563158f323bSEric Dumazet 	 */
1564158f323bSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
1565158f323bSEric Dumazet 		skb->truesize += size - osize;
1566158f323bSEric Dumazet 
15671da177e4SLinus Torvalds 	return 0;
15681da177e4SLinus Torvalds 
1569a6686f2fSShirley Ma nofrags:
1570a6686f2fSShirley Ma 	kfree(data);
15711da177e4SLinus Torvalds nodata:
15721da177e4SLinus Torvalds 	return -ENOMEM;
15731da177e4SLinus Torvalds }
1574b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	struct sk_buff *skb2;
15811da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	if (delta <= 0)
15841da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
15851da177e4SLinus Torvalds 	else {
15861da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
15871da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
15881da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
15891da177e4SLinus Torvalds 			kfree_skb(skb2);
15901da177e4SLinus Torvalds 			skb2 = NULL;
15911da177e4SLinus Torvalds 		}
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 	return skb2;
15941da177e4SLinus Torvalds }
1595b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds /**
15981da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
15991da177e4SLinus Torvalds  *	@skb: buffer to copy
16001da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
16011da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
16021da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
16031da177e4SLinus Torvalds  *
16041da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
16051da177e4SLinus Torvalds  *	allocate additional space.
16061da177e4SLinus Torvalds  *
16071da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
16081da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
16091da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
16101da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
16111da177e4SLinus Torvalds  *
16121da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
16131da177e4SLinus Torvalds  *	is called from an interrupt.
16141da177e4SLinus Torvalds  */
16151da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
161686a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1617dd0fc66fSAl Viro 				gfp_t gfp_mask)
16181da177e4SLinus Torvalds {
16191da177e4SLinus Torvalds 	/*
16201da177e4SLinus Torvalds 	 *	Allocate the copy buffer
16211da177e4SLinus Torvalds 	 */
1622c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1623c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1624c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1625efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
16261da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	if (!n)
16291da177e4SLinus Torvalds 		return NULL;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	/* Set the tail pointer and length */
16341da177e4SLinus Torvalds 	skb_put(n, skb->len);
16351da177e4SLinus Torvalds 
1636efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
16371da177e4SLinus Torvalds 	head_copy_off = 0;
16381da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
16391da177e4SLinus Torvalds 		head_copy_len = newheadroom;
16401da177e4SLinus Torvalds 	else
16411da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	/* Copy the linear header and data. */
16449f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
16459f77fad3STim Hansen 			     skb->len + head_copy_len));
16461da177e4SLinus Torvalds 
164708303c18SIlya Lesokhin 	skb_copy_header(n, skb);
16481da177e4SLinus Torvalds 
1649030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1650efd1e8d5SPatrick McHardy 
16511da177e4SLinus Torvalds 	return n;
16521da177e4SLinus Torvalds }
1653b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /**
1656cd0a137aSFlorian Fainelli  *	__skb_pad		-	zero pad the tail of an skb
16571da177e4SLinus Torvalds  *	@skb: buffer to pad
16581da177e4SLinus Torvalds  *	@pad: space to pad
1659cd0a137aSFlorian Fainelli  *	@free_on_error: free buffer on error
16601da177e4SLinus Torvalds  *
16611da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
16621da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
16631da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
16641da177e4SLinus Torvalds  *
1665cd0a137aSFlorian Fainelli  *	May return error in out of memory cases. The skb is freed on error
1666cd0a137aSFlorian Fainelli  *	if @free_on_error is true.
16671da177e4SLinus Torvalds  */
16681da177e4SLinus Torvalds 
1669cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
16701da177e4SLinus Torvalds {
16715b057c6bSHerbert Xu 	int err;
16725b057c6bSHerbert Xu 	int ntail;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
16755b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
16761da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
16775b057c6bSHerbert Xu 		return 0;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16804305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
16815b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
16825b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
16835b057c6bSHerbert Xu 		if (unlikely(err))
16845b057c6bSHerbert Xu 			goto free_skb;
16855b057c6bSHerbert Xu 	}
16865b057c6bSHerbert Xu 
16875b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
16885b057c6bSHerbert Xu 	 * to be audited.
16895b057c6bSHerbert Xu 	 */
16905b057c6bSHerbert Xu 	err = skb_linearize(skb);
16915b057c6bSHerbert Xu 	if (unlikely(err))
16925b057c6bSHerbert Xu 		goto free_skb;
16935b057c6bSHerbert Xu 
16945b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
16955b057c6bSHerbert Xu 	return 0;
16965b057c6bSHerbert Xu 
16975b057c6bSHerbert Xu free_skb:
1698cd0a137aSFlorian Fainelli 	if (free_on_error)
16991da177e4SLinus Torvalds 		kfree_skb(skb);
17005b057c6bSHerbert Xu 	return err;
17011da177e4SLinus Torvalds }
1702cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad);
17031da177e4SLinus Torvalds 
17040dde3e16SIlpo Järvinen /**
17050c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
17060c7ddf36SMathias Krause  *	@skb: start of the buffer to use
17070c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
17080c7ddf36SMathias Krause  *	@len: amount of data to add
17090c7ddf36SMathias Krause  *
17100c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
17110c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
17120c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
17130c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
17140c7ddf36SMathias Krause  *	returned.
17150c7ddf36SMathias Krause  */
17160c7ddf36SMathias Krause 
17174df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
17180c7ddf36SMathias Krause {
17190c7ddf36SMathias Krause 	if (tail != skb) {
17200c7ddf36SMathias Krause 		skb->data_len += len;
17210c7ddf36SMathias Krause 		skb->len += len;
17220c7ddf36SMathias Krause 	}
17230c7ddf36SMathias Krause 	return skb_put(tail, len);
17240c7ddf36SMathias Krause }
17250c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
17260c7ddf36SMathias Krause 
17270c7ddf36SMathias Krause /**
17280dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
17290dde3e16SIlpo Järvinen  *	@skb: buffer to use
17300dde3e16SIlpo Järvinen  *	@len: amount of data to add
17310dde3e16SIlpo Järvinen  *
17320dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
17330dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
17340dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
17350dde3e16SIlpo Järvinen  */
17364df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len)
17370dde3e16SIlpo Järvinen {
17384df864c1SJohannes Berg 	void *tmp = skb_tail_pointer(skb);
17390dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
17400dde3e16SIlpo Järvinen 	skb->tail += len;
17410dde3e16SIlpo Järvinen 	skb->len  += len;
17420dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
17430dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
17440dde3e16SIlpo Järvinen 	return tmp;
17450dde3e16SIlpo Järvinen }
17460dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
17470dde3e16SIlpo Järvinen 
17486be8ac2fSIlpo Järvinen /**
1749c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1750c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1751c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1752c2aa270aSIlpo Järvinen  *
1753c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1754c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1755c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1756c2aa270aSIlpo Järvinen  */
1757d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len)
1758c2aa270aSIlpo Järvinen {
1759c2aa270aSIlpo Järvinen 	skb->data -= len;
1760c2aa270aSIlpo Järvinen 	skb->len  += len;
1761c2aa270aSIlpo Järvinen 	if (unlikely(skb->data < skb->head))
1762c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1763c2aa270aSIlpo Järvinen 	return skb->data;
1764c2aa270aSIlpo Järvinen }
1765c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1766c2aa270aSIlpo Järvinen 
1767c2aa270aSIlpo Järvinen /**
17686be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
17696be8ac2fSIlpo Järvinen  *	@skb: buffer to use
17706be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
17716be8ac2fSIlpo Järvinen  *
17726be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
17736be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
17746be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
17756be8ac2fSIlpo Järvinen  *	the old data.
17766be8ac2fSIlpo Järvinen  */
1777af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len)
17786be8ac2fSIlpo Järvinen {
177947d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
17806be8ac2fSIlpo Järvinen }
17816be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
17826be8ac2fSIlpo Järvinen 
1783419ae74eSIlpo Järvinen /**
1784419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1785419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1786419ae74eSIlpo Järvinen  *	@len: new length
1787419ae74eSIlpo Järvinen  *
1788419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1789419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1790419ae74eSIlpo Järvinen  *	The skb must be linear.
1791419ae74eSIlpo Järvinen  */
1792419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1793419ae74eSIlpo Järvinen {
1794419ae74eSIlpo Järvinen 	if (skb->len > len)
1795419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1796419ae74eSIlpo Järvinen }
1797419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1798419ae74eSIlpo Järvinen 
17993cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
18001da177e4SLinus Torvalds  */
18011da177e4SLinus Torvalds 
18023cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
18031da177e4SLinus Torvalds {
180427b437c8SHerbert Xu 	struct sk_buff **fragp;
180527b437c8SHerbert Xu 	struct sk_buff *frag;
18061da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
18071da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
18081da177e4SLinus Torvalds 	int i;
180927b437c8SHerbert Xu 	int err;
181027b437c8SHerbert Xu 
181127b437c8SHerbert Xu 	if (skb_cloned(skb) &&
181227b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
181327b437c8SHerbert Xu 		return err;
18141da177e4SLinus Torvalds 
1815f4d26fb3SHerbert Xu 	i = 0;
1816f4d26fb3SHerbert Xu 	if (offset >= len)
1817f4d26fb3SHerbert Xu 		goto drop_pages;
1818f4d26fb3SHerbert Xu 
1819f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
18209e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
182127b437c8SHerbert Xu 
182227b437c8SHerbert Xu 		if (end < len) {
18231da177e4SLinus Torvalds 			offset = end;
182427b437c8SHerbert Xu 			continue;
18251da177e4SLinus Torvalds 		}
18261da177e4SLinus Torvalds 
18279e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
182827b437c8SHerbert Xu 
1829f4d26fb3SHerbert Xu drop_pages:
183027b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
183127b437c8SHerbert Xu 
183227b437c8SHerbert Xu 		for (; i < nfrags; i++)
1833ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
183427b437c8SHerbert Xu 
183521dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
183627b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1837f4d26fb3SHerbert Xu 		goto done;
183827b437c8SHerbert Xu 	}
183927b437c8SHerbert Xu 
184027b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
184127b437c8SHerbert Xu 	     fragp = &frag->next) {
184227b437c8SHerbert Xu 		int end = offset + frag->len;
184327b437c8SHerbert Xu 
184427b437c8SHerbert Xu 		if (skb_shared(frag)) {
184527b437c8SHerbert Xu 			struct sk_buff *nfrag;
184627b437c8SHerbert Xu 
184727b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
184827b437c8SHerbert Xu 			if (unlikely(!nfrag))
184927b437c8SHerbert Xu 				return -ENOMEM;
185027b437c8SHerbert Xu 
185127b437c8SHerbert Xu 			nfrag->next = frag->next;
185285bb2a60SEric Dumazet 			consume_skb(frag);
185327b437c8SHerbert Xu 			frag = nfrag;
185427b437c8SHerbert Xu 			*fragp = frag;
185527b437c8SHerbert Xu 		}
185627b437c8SHerbert Xu 
185727b437c8SHerbert Xu 		if (end < len) {
185827b437c8SHerbert Xu 			offset = end;
185927b437c8SHerbert Xu 			continue;
186027b437c8SHerbert Xu 		}
186127b437c8SHerbert Xu 
186227b437c8SHerbert Xu 		if (end > len &&
186327b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
186427b437c8SHerbert Xu 			return err;
186527b437c8SHerbert Xu 
186627b437c8SHerbert Xu 		if (frag->next)
186727b437c8SHerbert Xu 			skb_drop_list(&frag->next);
186827b437c8SHerbert Xu 		break;
186927b437c8SHerbert Xu 	}
187027b437c8SHerbert Xu 
1871f4d26fb3SHerbert Xu done:
187227b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
18731da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
18741da177e4SLinus Torvalds 		skb->len       = len;
18751da177e4SLinus Torvalds 	} else {
18761da177e4SLinus Torvalds 		skb->len       = len;
18771da177e4SLinus Torvalds 		skb->data_len  = 0;
187827a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 
1881c21b48ccSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
1882c21b48ccSEric Dumazet 		skb_condense(skb);
18831da177e4SLinus Torvalds 	return 0;
18841da177e4SLinus Torvalds }
1885b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
18861da177e4SLinus Torvalds 
188788078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly
188888078d98SEric Dumazet  */
188988078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
189088078d98SEric Dumazet {
189188078d98SEric Dumazet 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
189288078d98SEric Dumazet 		int delta = skb->len - len;
189388078d98SEric Dumazet 
1894d55bef50SDimitris Michailidis 		skb->csum = csum_block_sub(skb->csum,
1895d55bef50SDimitris Michailidis 					   skb_checksum(skb, len, delta, 0),
1896d55bef50SDimitris Michailidis 					   len);
189788078d98SEric Dumazet 	}
189888078d98SEric Dumazet 	return __pskb_trim(skb, len);
189988078d98SEric Dumazet }
190088078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow);
190188078d98SEric Dumazet 
19021da177e4SLinus Torvalds /**
19031da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
19041da177e4SLinus Torvalds  *	@skb: buffer to reallocate
19051da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
19061da177e4SLinus Torvalds  *
19071da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
19081da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
19091da177e4SLinus Torvalds  *	data from fragmented part.
19101da177e4SLinus Torvalds  *
19111da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
19121da177e4SLinus Torvalds  *
19131da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
19141da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
19171da177e4SLinus Torvalds  *	reloaded after call to this function.
19181da177e4SLinus Torvalds  */
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
19211da177e4SLinus Torvalds  * when it is necessary.
19221da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
19231da177e4SLinus Torvalds  * 2. It may change skb pointers.
19241da177e4SLinus Torvalds  *
19251da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
19261da177e4SLinus Torvalds  */
1927af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta)
19281da177e4SLinus Torvalds {
19291da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
19301da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
19311da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
19321da177e4SLinus Torvalds 	 */
19334305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
19361da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
19371da177e4SLinus Torvalds 				     GFP_ATOMIC))
19381da177e4SLinus Torvalds 			return NULL;
19391da177e4SLinus Torvalds 	}
19401da177e4SLinus Torvalds 
19419f77fad3STim Hansen 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
19429f77fad3STim Hansen 			     skb_tail_pointer(skb), delta));
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
19451da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
19461da177e4SLinus Torvalds 	 */
194721dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
19481da177e4SLinus Torvalds 		goto pull_pages;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
19511da177e4SLinus Torvalds 	eat = delta;
19521da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
19539e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
19549e903e08SEric Dumazet 
19559e903e08SEric Dumazet 		if (size >= eat)
19561da177e4SLinus Torvalds 			goto pull_pages;
19579e903e08SEric Dumazet 		eat -= size;
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
196109001b03SWenhua Shi 	 * Certainly, it is possible to add an offset to skb data,
19621da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
19631da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
19641da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
19651da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
19661da177e4SLinus Torvalds 	 */
19671da177e4SLinus Torvalds 	if (eat) {
19681da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
19691da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
19701da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 		do {
19731da177e4SLinus Torvalds 			if (list->len <= eat) {
19741da177e4SLinus Torvalds 				/* Eaten as whole. */
19751da177e4SLinus Torvalds 				eat -= list->len;
19761da177e4SLinus Torvalds 				list = list->next;
19771da177e4SLinus Torvalds 				insp = list;
19781da177e4SLinus Torvalds 			} else {
19791da177e4SLinus Torvalds 				/* Eaten partially. */
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 				if (skb_shared(list)) {
19821da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
19831da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
19841da177e4SLinus Torvalds 					if (!clone)
19851da177e4SLinus Torvalds 						return NULL;
19861da177e4SLinus Torvalds 					insp = list->next;
19871da177e4SLinus Torvalds 					list = clone;
19881da177e4SLinus Torvalds 				} else {
19891da177e4SLinus Torvalds 					/* This may be pulled without
19901da177e4SLinus Torvalds 					 * problems. */
19911da177e4SLinus Torvalds 					insp = list;
19921da177e4SLinus Torvalds 				}
19931da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
19941da177e4SLinus Torvalds 					kfree_skb(clone);
19951da177e4SLinus Torvalds 					return NULL;
19961da177e4SLinus Torvalds 				}
19971da177e4SLinus Torvalds 				break;
19981da177e4SLinus Torvalds 			}
19991da177e4SLinus Torvalds 		} while (eat);
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 		/* Free pulled out fragments. */
20021da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
20031da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
20041da177e4SLinus Torvalds 			kfree_skb(list);
20051da177e4SLinus Torvalds 		}
20061da177e4SLinus Torvalds 		/* And insert new clone at head. */
20071da177e4SLinus Torvalds 		if (clone) {
20081da177e4SLinus Torvalds 			clone->next = list;
20091da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
20101da177e4SLinus Torvalds 		}
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds pull_pages:
20151da177e4SLinus Torvalds 	eat = delta;
20161da177e4SLinus Torvalds 	k = 0;
20171da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20189e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
20199e903e08SEric Dumazet 
20209e903e08SEric Dumazet 		if (size <= eat) {
2021ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
20229e903e08SEric Dumazet 			eat -= size;
20231da177e4SLinus Torvalds 		} else {
20241da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
20251da177e4SLinus Torvalds 			if (eat) {
20261da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
20279e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
20283ccc6c6fSlinzhang 				if (!i)
20293ccc6c6fSlinzhang 					goto end;
20301da177e4SLinus Torvalds 				eat = 0;
20311da177e4SLinus Torvalds 			}
20321da177e4SLinus Torvalds 			k++;
20331da177e4SLinus Torvalds 		}
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
20361da177e4SLinus Torvalds 
20373ccc6c6fSlinzhang end:
20381da177e4SLinus Torvalds 	skb->tail     += delta;
20391da177e4SLinus Torvalds 	skb->data_len -= delta;
20401da177e4SLinus Torvalds 
20411f8b977aSWillem de Bruijn 	if (!skb->data_len)
20421f8b977aSWillem de Bruijn 		skb_zcopy_clear(skb, false);
20431f8b977aSWillem de Bruijn 
204427a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
20451da177e4SLinus Torvalds }
2046b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
20471da177e4SLinus Torvalds 
204822019b17SEric Dumazet /**
204922019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
205022019b17SEric Dumazet  *	@skb: source skb
205122019b17SEric Dumazet  *	@offset: offset in source
205222019b17SEric Dumazet  *	@to: destination buffer
205322019b17SEric Dumazet  *	@len: number of bytes to copy
205422019b17SEric Dumazet  *
205522019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
205622019b17SEric Dumazet  *	destination buffer.
205722019b17SEric Dumazet  *
205822019b17SEric Dumazet  *	CAUTION ! :
205922019b17SEric Dumazet  *		If its prototype is ever changed,
206022019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
206122019b17SEric Dumazet  *		since it is called from BPF assembly code.
206222019b17SEric Dumazet  */
20631da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
20641da177e4SLinus Torvalds {
20651a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2066fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2067fbb398a8SDavid S. Miller 	int i, copy;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
20701da177e4SLinus Torvalds 		goto fault;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	/* Copy header. */
20731a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
20741da177e4SLinus Torvalds 		if (copy > len)
20751da177e4SLinus Torvalds 			copy = len;
2076d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
20771da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20781da177e4SLinus Torvalds 			return 0;
20791da177e4SLinus Torvalds 		offset += copy;
20801da177e4SLinus Torvalds 		to     += copy;
20811da177e4SLinus Torvalds 	}
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20841a028e50SDavid S. Miller 		int end;
208551c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
20861da177e4SLinus Torvalds 
2087547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20881a028e50SDavid S. Miller 
208951c56b00SEric Dumazet 		end = start + skb_frag_size(f);
20901da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2091c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2092c613c209SWillem de Bruijn 			struct page *p;
20931da177e4SLinus Torvalds 			u8 *vaddr;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 			if (copy > len)
20961da177e4SLinus Torvalds 				copy = len;
20971da177e4SLinus Torvalds 
2098c613c209SWillem de Bruijn 			skb_frag_foreach_page(f,
2099c613c209SWillem de Bruijn 					      f->page_offset + offset - start,
2100c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2101c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2102c613c209SWillem de Bruijn 				memcpy(to + copied, vaddr + p_off, p_len);
210351c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2104c613c209SWillem de Bruijn 			}
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21071da177e4SLinus Torvalds 				return 0;
21081da177e4SLinus Torvalds 			offset += copy;
21091da177e4SLinus Torvalds 			to     += copy;
21101da177e4SLinus Torvalds 		}
21111a028e50SDavid S. Miller 		start = end;
21121da177e4SLinus Torvalds 	}
21131da177e4SLinus Torvalds 
2114fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
21151a028e50SDavid S. Miller 		int end;
21161da177e4SLinus Torvalds 
2117547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21181a028e50SDavid S. Miller 
2119fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
21201da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21211da177e4SLinus Torvalds 			if (copy > len)
21221da177e4SLinus Torvalds 				copy = len;
2123fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
21241da177e4SLinus Torvalds 				goto fault;
21251da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21261da177e4SLinus Torvalds 				return 0;
21271da177e4SLinus Torvalds 			offset += copy;
21281da177e4SLinus Torvalds 			to     += copy;
21291da177e4SLinus Torvalds 		}
21301a028e50SDavid S. Miller 		start = end;
21311da177e4SLinus Torvalds 	}
2132a6686f2fSShirley Ma 
21331da177e4SLinus Torvalds 	if (!len)
21341da177e4SLinus Torvalds 		return 0;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds fault:
21371da177e4SLinus Torvalds 	return -EFAULT;
21381da177e4SLinus Torvalds }
2139b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
21401da177e4SLinus Torvalds 
21419c55e01cSJens Axboe /*
21429c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
21439c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
21449c55e01cSJens Axboe  */
21459c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
21469c55e01cSJens Axboe {
21478b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
21488b9d3728SJarek Poplawski }
21499c55e01cSJens Axboe 
2150a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
21514fb66994SJarek Poplawski 				   unsigned int *offset,
215218aafc62SEric Dumazet 				   struct sock *sk)
21538b9d3728SJarek Poplawski {
21545640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
21558b9d3728SJarek Poplawski 
21565640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
21578b9d3728SJarek Poplawski 		return NULL;
21584fb66994SJarek Poplawski 
21595640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
21604fb66994SJarek Poplawski 
21615640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
21625640f768SEric Dumazet 	       page_address(page) + *offset, *len);
21635640f768SEric Dumazet 	*offset = pfrag->offset;
21645640f768SEric Dumazet 	pfrag->offset += *len;
21654fb66994SJarek Poplawski 
21665640f768SEric Dumazet 	return pfrag->page;
21679c55e01cSJens Axboe }
21689c55e01cSJens Axboe 
216941c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
217041c73a0dSEric Dumazet 			     struct page *page,
217141c73a0dSEric Dumazet 			     unsigned int offset)
217241c73a0dSEric Dumazet {
217341c73a0dSEric Dumazet 	return	spd->nr_pages &&
217441c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
217541c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
217641c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
217741c73a0dSEric Dumazet }
217841c73a0dSEric Dumazet 
21799c55e01cSJens Axboe /*
21809c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
21819c55e01cSJens Axboe  */
2182a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
218335f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
21844fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
218518aafc62SEric Dumazet 			  bool linear,
21867a67e56fSJarek Poplawski 			  struct sock *sk)
21879c55e01cSJens Axboe {
218841c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2189a108d5f3SDavid S. Miller 		return true;
21909c55e01cSJens Axboe 
21918b9d3728SJarek Poplawski 	if (linear) {
219218aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
21938b9d3728SJarek Poplawski 		if (!page)
2194a108d5f3SDavid S. Miller 			return true;
219541c73a0dSEric Dumazet 	}
219641c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
219741c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
2198a108d5f3SDavid S. Miller 		return false;
219941c73a0dSEric Dumazet 	}
22008b9d3728SJarek Poplawski 	get_page(page);
22019c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
22024fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
22039c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
22049c55e01cSJens Axboe 	spd->nr_pages++;
22058b9d3728SJarek Poplawski 
2206a108d5f3SDavid S. Miller 	return false;
22079c55e01cSJens Axboe }
22089c55e01cSJens Axboe 
2209a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
22102870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
221118aafc62SEric Dumazet 			     unsigned int *len,
2212d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
221335f3d14dSJens Axboe 			     struct sock *sk,
221435f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
22159c55e01cSJens Axboe {
22162870c43dSOctavian Purdila 	if (!*len)
2217a108d5f3SDavid S. Miller 		return true;
22189c55e01cSJens Axboe 
22192870c43dSOctavian Purdila 	/* skip this segment if already processed */
22202870c43dSOctavian Purdila 	if (*off >= plen) {
22212870c43dSOctavian Purdila 		*off -= plen;
2222a108d5f3SDavid S. Miller 		return false;
22232870c43dSOctavian Purdila 	}
22242870c43dSOctavian Purdila 
22252870c43dSOctavian Purdila 	/* ignore any bits we already processed */
22269ca1b22dSEric Dumazet 	poff += *off;
22279ca1b22dSEric Dumazet 	plen -= *off;
22282870c43dSOctavian Purdila 	*off = 0;
22292870c43dSOctavian Purdila 
223018aafc62SEric Dumazet 	do {
223118aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
22322870c43dSOctavian Purdila 
223318aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
223418aafc62SEric Dumazet 				  linear, sk))
2235a108d5f3SDavid S. Miller 			return true;
223618aafc62SEric Dumazet 		poff += flen;
223718aafc62SEric Dumazet 		plen -= flen;
22382870c43dSOctavian Purdila 		*len -= flen;
223918aafc62SEric Dumazet 	} while (*len && plen);
22402870c43dSOctavian Purdila 
2241a108d5f3SDavid S. Miller 	return false;
2242db43a282SOctavian Purdila }
22439c55e01cSJens Axboe 
22449c55e01cSJens Axboe /*
2245a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
22462870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
22479c55e01cSJens Axboe  */
2248a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
224935f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
225035f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
22512870c43dSOctavian Purdila {
22522870c43dSOctavian Purdila 	int seg;
2253fa9835e5STom Herbert 	struct sk_buff *iter;
22549c55e01cSJens Axboe 
22551d0c0b32SEric Dumazet 	/* map the linear part :
22562996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
22572996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
22582996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
22599c55e01cSJens Axboe 	 */
22602870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
22612870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
22622870c43dSOctavian Purdila 			     skb_headlen(skb),
226318aafc62SEric Dumazet 			     offset, len, spd,
22643a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
22651d0c0b32SEric Dumazet 			     sk, pipe))
2266a108d5f3SDavid S. Miller 		return true;
22679c55e01cSJens Axboe 
22689c55e01cSJens Axboe 	/*
22699c55e01cSJens Axboe 	 * then map the fragments
22709c55e01cSJens Axboe 	 */
22719c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
22729c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
22739c55e01cSJens Axboe 
2274ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
22759e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
227618aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
2277a108d5f3SDavid S. Miller 			return true;
22789c55e01cSJens Axboe 	}
22799c55e01cSJens Axboe 
2280fa9835e5STom Herbert 	skb_walk_frags(skb, iter) {
2281fa9835e5STom Herbert 		if (*offset >= iter->len) {
2282fa9835e5STom Herbert 			*offset -= iter->len;
2283fa9835e5STom Herbert 			continue;
2284fa9835e5STom Herbert 		}
2285fa9835e5STom Herbert 		/* __skb_splice_bits() only fails if the output has no room
2286fa9835e5STom Herbert 		 * left, so no point in going over the frag_list for the error
2287fa9835e5STom Herbert 		 * case.
2288fa9835e5STom Herbert 		 */
2289fa9835e5STom Herbert 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2290fa9835e5STom Herbert 			return true;
2291fa9835e5STom Herbert 	}
2292fa9835e5STom Herbert 
2293a108d5f3SDavid S. Miller 	return false;
22949c55e01cSJens Axboe }
22959c55e01cSJens Axboe 
22969c55e01cSJens Axboe /*
22979c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
2298fa9835e5STom Herbert  * the fragments, and the frag list.
22999c55e01cSJens Axboe  */
2300a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
23019c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
230225869262SAl Viro 		    unsigned int flags)
23039c55e01cSJens Axboe {
230441c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
230541c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
23069c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
23079c55e01cSJens Axboe 		.pages = pages,
23089c55e01cSJens Axboe 		.partial = partial,
2309047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
231028a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
23119c55e01cSJens Axboe 		.spd_release = sock_spd_release,
23129c55e01cSJens Axboe 	};
231335f3d14dSJens Axboe 	int ret = 0;
231435f3d14dSJens Axboe 
2315fa9835e5STom Herbert 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
23169c55e01cSJens Axboe 
2317a60e3cc7SHannes Frederic Sowa 	if (spd.nr_pages)
231825869262SAl Viro 		ret = splice_to_pipe(pipe, &spd);
23199c55e01cSJens Axboe 
232035f3d14dSJens Axboe 	return ret;
23219c55e01cSJens Axboe }
23222b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
23239c55e01cSJens Axboe 
232420bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */
232520bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
232620bf50deSTom Herbert 			 int len)
232720bf50deSTom Herbert {
232820bf50deSTom Herbert 	unsigned int orig_len = len;
232920bf50deSTom Herbert 	struct sk_buff *head = skb;
233020bf50deSTom Herbert 	unsigned short fragidx;
233120bf50deSTom Herbert 	int slen, ret;
233220bf50deSTom Herbert 
233320bf50deSTom Herbert do_frag_list:
233420bf50deSTom Herbert 
233520bf50deSTom Herbert 	/* Deal with head data */
233620bf50deSTom Herbert 	while (offset < skb_headlen(skb) && len) {
233720bf50deSTom Herbert 		struct kvec kv;
233820bf50deSTom Herbert 		struct msghdr msg;
233920bf50deSTom Herbert 
234020bf50deSTom Herbert 		slen = min_t(int, len, skb_headlen(skb) - offset);
234120bf50deSTom Herbert 		kv.iov_base = skb->data + offset;
2342db5980d8SJohn Fastabend 		kv.iov_len = slen;
234320bf50deSTom Herbert 		memset(&msg, 0, sizeof(msg));
234420bf50deSTom Herbert 
234520bf50deSTom Herbert 		ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
234620bf50deSTom Herbert 		if (ret <= 0)
234720bf50deSTom Herbert 			goto error;
234820bf50deSTom Herbert 
234920bf50deSTom Herbert 		offset += ret;
235020bf50deSTom Herbert 		len -= ret;
235120bf50deSTom Herbert 	}
235220bf50deSTom Herbert 
235320bf50deSTom Herbert 	/* All the data was skb head? */
235420bf50deSTom Herbert 	if (!len)
235520bf50deSTom Herbert 		goto out;
235620bf50deSTom Herbert 
235720bf50deSTom Herbert 	/* Make offset relative to start of frags */
235820bf50deSTom Herbert 	offset -= skb_headlen(skb);
235920bf50deSTom Herbert 
236020bf50deSTom Herbert 	/* Find where we are in frag list */
236120bf50deSTom Herbert 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
236220bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
236320bf50deSTom Herbert 
236420bf50deSTom Herbert 		if (offset < frag->size)
236520bf50deSTom Herbert 			break;
236620bf50deSTom Herbert 
236720bf50deSTom Herbert 		offset -= frag->size;
236820bf50deSTom Herbert 	}
236920bf50deSTom Herbert 
237020bf50deSTom Herbert 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
237120bf50deSTom Herbert 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
237220bf50deSTom Herbert 
237320bf50deSTom Herbert 		slen = min_t(size_t, len, frag->size - offset);
237420bf50deSTom Herbert 
237520bf50deSTom Herbert 		while (slen) {
237620bf50deSTom Herbert 			ret = kernel_sendpage_locked(sk, frag->page.p,
237720bf50deSTom Herbert 						     frag->page_offset + offset,
237820bf50deSTom Herbert 						     slen, MSG_DONTWAIT);
237920bf50deSTom Herbert 			if (ret <= 0)
238020bf50deSTom Herbert 				goto error;
238120bf50deSTom Herbert 
238220bf50deSTom Herbert 			len -= ret;
238320bf50deSTom Herbert 			offset += ret;
238420bf50deSTom Herbert 			slen -= ret;
238520bf50deSTom Herbert 		}
238620bf50deSTom Herbert 
238720bf50deSTom Herbert 		offset = 0;
238820bf50deSTom Herbert 	}
238920bf50deSTom Herbert 
239020bf50deSTom Herbert 	if (len) {
239120bf50deSTom Herbert 		/* Process any frag lists */
239220bf50deSTom Herbert 
239320bf50deSTom Herbert 		if (skb == head) {
239420bf50deSTom Herbert 			if (skb_has_frag_list(skb)) {
239520bf50deSTom Herbert 				skb = skb_shinfo(skb)->frag_list;
239620bf50deSTom Herbert 				goto do_frag_list;
239720bf50deSTom Herbert 			}
239820bf50deSTom Herbert 		} else if (skb->next) {
239920bf50deSTom Herbert 			skb = skb->next;
240020bf50deSTom Herbert 			goto do_frag_list;
240120bf50deSTom Herbert 		}
240220bf50deSTom Herbert 	}
240320bf50deSTom Herbert 
240420bf50deSTom Herbert out:
240520bf50deSTom Herbert 	return orig_len - len;
240620bf50deSTom Herbert 
240720bf50deSTom Herbert error:
240820bf50deSTom Herbert 	return orig_len == len ? ret : orig_len - len;
240920bf50deSTom Herbert }
241020bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked);
241120bf50deSTom Herbert 
2412357b40a1SHerbert Xu /**
2413357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
2414357b40a1SHerbert Xu  *	@skb: destination buffer
2415357b40a1SHerbert Xu  *	@offset: offset in destination
2416357b40a1SHerbert Xu  *	@from: source buffer
2417357b40a1SHerbert Xu  *	@len: number of bytes to copy
2418357b40a1SHerbert Xu  *
2419357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
2420357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
2421357b40a1SHerbert Xu  *	traversing fragment lists and such.
2422357b40a1SHerbert Xu  */
2423357b40a1SHerbert Xu 
24240c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2425357b40a1SHerbert Xu {
24261a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2427fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2428fbb398a8SDavid S. Miller 	int i, copy;
2429357b40a1SHerbert Xu 
2430357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
2431357b40a1SHerbert Xu 		goto fault;
2432357b40a1SHerbert Xu 
24331a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
2434357b40a1SHerbert Xu 		if (copy > len)
2435357b40a1SHerbert Xu 			copy = len;
243627d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2437357b40a1SHerbert Xu 		if ((len -= copy) == 0)
2438357b40a1SHerbert Xu 			return 0;
2439357b40a1SHerbert Xu 		offset += copy;
2440357b40a1SHerbert Xu 		from += copy;
2441357b40a1SHerbert Xu 	}
2442357b40a1SHerbert Xu 
2443357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2444357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
24451a028e50SDavid S. Miller 		int end;
2446357b40a1SHerbert Xu 
2447547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
24481a028e50SDavid S. Miller 
24499e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
2450357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2451c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2452c613c209SWillem de Bruijn 			struct page *p;
2453357b40a1SHerbert Xu 			u8 *vaddr;
2454357b40a1SHerbert Xu 
2455357b40a1SHerbert Xu 			if (copy > len)
2456357b40a1SHerbert Xu 				copy = len;
2457357b40a1SHerbert Xu 
2458c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2459c613c209SWillem de Bruijn 					      frag->page_offset + offset - start,
2460c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2461c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2462c613c209SWillem de Bruijn 				memcpy(vaddr + p_off, from + copied, p_len);
246351c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2464c613c209SWillem de Bruijn 			}
2465357b40a1SHerbert Xu 
2466357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2467357b40a1SHerbert Xu 				return 0;
2468357b40a1SHerbert Xu 			offset += copy;
2469357b40a1SHerbert Xu 			from += copy;
2470357b40a1SHerbert Xu 		}
24711a028e50SDavid S. Miller 		start = end;
2472357b40a1SHerbert Xu 	}
2473357b40a1SHerbert Xu 
2474fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
24751a028e50SDavid S. Miller 		int end;
2476357b40a1SHerbert Xu 
2477547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
24781a028e50SDavid S. Miller 
2479fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2480357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2481357b40a1SHerbert Xu 			if (copy > len)
2482357b40a1SHerbert Xu 				copy = len;
2483fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
24841a028e50SDavid S. Miller 					   from, copy))
2485357b40a1SHerbert Xu 				goto fault;
2486357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2487357b40a1SHerbert Xu 				return 0;
2488357b40a1SHerbert Xu 			offset += copy;
2489357b40a1SHerbert Xu 			from += copy;
2490357b40a1SHerbert Xu 		}
24911a028e50SDavid S. Miller 		start = end;
2492357b40a1SHerbert Xu 	}
2493357b40a1SHerbert Xu 	if (!len)
2494357b40a1SHerbert Xu 		return 0;
2495357b40a1SHerbert Xu 
2496357b40a1SHerbert Xu fault:
2497357b40a1SHerbert Xu 	return -EFAULT;
2498357b40a1SHerbert Xu }
2499357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
2500357b40a1SHerbert Xu 
25011da177e4SLinus Torvalds /* Checksum skb data. */
25022817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
25032817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
25041da177e4SLinus Torvalds {
25051a028e50SDavid S. Miller 	int start = skb_headlen(skb);
25061a028e50SDavid S. Miller 	int i, copy = start - offset;
2507fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
25081da177e4SLinus Torvalds 	int pos = 0;
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	/* Checksum header. */
25111da177e4SLinus Torvalds 	if (copy > 0) {
25121da177e4SLinus Torvalds 		if (copy > len)
25131da177e4SLinus Torvalds 			copy = len;
25142817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
25151da177e4SLinus Torvalds 		if ((len -= copy) == 0)
25161da177e4SLinus Torvalds 			return csum;
25171da177e4SLinus Torvalds 		offset += copy;
25181da177e4SLinus Torvalds 		pos	= copy;
25191da177e4SLinus Torvalds 	}
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
25221a028e50SDavid S. Miller 		int end;
252351c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
25241da177e4SLinus Torvalds 
2525547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
25261a028e50SDavid S. Miller 
252751c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
25281da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2529c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2530c613c209SWillem de Bruijn 			struct page *p;
253144bb9363SAl Viro 			__wsum csum2;
25321da177e4SLinus Torvalds 			u8 *vaddr;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 			if (copy > len)
25351da177e4SLinus Torvalds 				copy = len;
2536c613c209SWillem de Bruijn 
2537c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2538c613c209SWillem de Bruijn 					      frag->page_offset + offset - start,
2539c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2540c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2541c613c209SWillem de Bruijn 				csum2 = ops->update(vaddr + p_off, p_len, 0);
254251c56b00SEric Dumazet 				kunmap_atomic(vaddr);
2543c613c209SWillem de Bruijn 				csum = ops->combine(csum, csum2, pos, p_len);
2544c613c209SWillem de Bruijn 				pos += p_len;
2545c613c209SWillem de Bruijn 			}
2546c613c209SWillem de Bruijn 
25471da177e4SLinus Torvalds 			if (!(len -= copy))
25481da177e4SLinus Torvalds 				return csum;
25491da177e4SLinus Torvalds 			offset += copy;
25501da177e4SLinus Torvalds 		}
25511a028e50SDavid S. Miller 		start = end;
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 
2554fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
25551a028e50SDavid S. Miller 		int end;
25561da177e4SLinus Torvalds 
2557547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
25581a028e50SDavid S. Miller 
2559fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
25601da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
25615f92a738SAl Viro 			__wsum csum2;
25621da177e4SLinus Torvalds 			if (copy > len)
25631da177e4SLinus Torvalds 				copy = len;
25642817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
25652817a336SDaniel Borkmann 					       copy, 0, ops);
25662817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
25671da177e4SLinus Torvalds 			if ((len -= copy) == 0)
25681da177e4SLinus Torvalds 				return csum;
25691da177e4SLinus Torvalds 			offset += copy;
25701da177e4SLinus Torvalds 			pos    += copy;
25711da177e4SLinus Torvalds 		}
25721a028e50SDavid S. Miller 		start = end;
25731da177e4SLinus Torvalds 	}
257409a62660SKris Katterjohn 	BUG_ON(len);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	return csum;
25771da177e4SLinus Torvalds }
25782817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
25792817a336SDaniel Borkmann 
25802817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
25812817a336SDaniel Borkmann 		    int len, __wsum csum)
25822817a336SDaniel Borkmann {
25832817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2584cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
25852817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
25862817a336SDaniel Borkmann 	};
25872817a336SDaniel Borkmann 
25882817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
25892817a336SDaniel Borkmann }
2590b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds /* Both of above in one bottle. */
25931da177e4SLinus Torvalds 
259481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
259581d77662SAl Viro 				    u8 *to, int len, __wsum csum)
25961da177e4SLinus Torvalds {
25971a028e50SDavid S. Miller 	int start = skb_headlen(skb);
25981a028e50SDavid S. Miller 	int i, copy = start - offset;
2599fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
26001da177e4SLinus Torvalds 	int pos = 0;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	/* Copy header. */
26031da177e4SLinus Torvalds 	if (copy > 0) {
26041da177e4SLinus Torvalds 		if (copy > len)
26051da177e4SLinus Torvalds 			copy = len;
26061da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
26071da177e4SLinus Torvalds 						 copy, csum);
26081da177e4SLinus Torvalds 		if ((len -= copy) == 0)
26091da177e4SLinus Torvalds 			return csum;
26101da177e4SLinus Torvalds 		offset += copy;
26111da177e4SLinus Torvalds 		to     += copy;
26121da177e4SLinus Torvalds 		pos	= copy;
26131da177e4SLinus Torvalds 	}
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26161a028e50SDavid S. Miller 		int end;
26171da177e4SLinus Torvalds 
2618547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26191a028e50SDavid S. Miller 
26209e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
26211da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
2622c613c209SWillem de Bruijn 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2623c613c209SWillem de Bruijn 			u32 p_off, p_len, copied;
2624c613c209SWillem de Bruijn 			struct page *p;
26255084205fSAl Viro 			__wsum csum2;
26261da177e4SLinus Torvalds 			u8 *vaddr;
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds 			if (copy > len)
26291da177e4SLinus Torvalds 				copy = len;
2630c613c209SWillem de Bruijn 
2631c613c209SWillem de Bruijn 			skb_frag_foreach_page(frag,
2632c613c209SWillem de Bruijn 					      frag->page_offset + offset - start,
2633c613c209SWillem de Bruijn 					      copy, p, p_off, p_len, copied) {
2634c613c209SWillem de Bruijn 				vaddr = kmap_atomic(p);
2635c613c209SWillem de Bruijn 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2636c613c209SWillem de Bruijn 								  to + copied,
2637c613c209SWillem de Bruijn 								  p_len, 0);
263851c56b00SEric Dumazet 				kunmap_atomic(vaddr);
26391da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
2640c613c209SWillem de Bruijn 				pos += p_len;
2641c613c209SWillem de Bruijn 			}
2642c613c209SWillem de Bruijn 
26431da177e4SLinus Torvalds 			if (!(len -= copy))
26441da177e4SLinus Torvalds 				return csum;
26451da177e4SLinus Torvalds 			offset += copy;
26461da177e4SLinus Torvalds 			to     += copy;
26471da177e4SLinus Torvalds 		}
26481a028e50SDavid S. Miller 		start = end;
26491da177e4SLinus Torvalds 	}
26501da177e4SLinus Torvalds 
2651fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
265281d77662SAl Viro 		__wsum csum2;
26531a028e50SDavid S. Miller 		int end;
26541da177e4SLinus Torvalds 
2655547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26561a028e50SDavid S. Miller 
2657fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
26581da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
26591da177e4SLinus Torvalds 			if (copy > len)
26601da177e4SLinus Torvalds 				copy = len;
2661fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
26621a028e50SDavid S. Miller 						       offset - start,
26631da177e4SLinus Torvalds 						       to, copy, 0);
26641da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
26651da177e4SLinus Torvalds 			if ((len -= copy) == 0)
26661da177e4SLinus Torvalds 				return csum;
26671da177e4SLinus Torvalds 			offset += copy;
26681da177e4SLinus Torvalds 			to     += copy;
26691da177e4SLinus Torvalds 			pos    += copy;
26701da177e4SLinus Torvalds 		}
26711a028e50SDavid S. Miller 		start = end;
26721da177e4SLinus Torvalds 	}
267309a62660SKris Katterjohn 	BUG_ON(len);
26741da177e4SLinus Torvalds 	return csum;
26751da177e4SLinus Torvalds }
2676b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
26771da177e4SLinus Torvalds 
267849f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
267949f8e832SCong Wang {
268049f8e832SCong Wang 	__sum16 sum;
268149f8e832SCong Wang 
268249f8e832SCong Wang 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
268314641931SCong Wang 	/* See comments in __skb_checksum_complete(). */
268449f8e832SCong Wang 	if (likely(!sum)) {
268549f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
268649f8e832SCong Wang 		    !skb->csum_complete_sw)
26877fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
268849f8e832SCong Wang 	}
268949f8e832SCong Wang 	if (!skb_shared(skb))
269049f8e832SCong Wang 		skb->csum_valid = !sum;
269149f8e832SCong Wang 	return sum;
269249f8e832SCong Wang }
269349f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head);
269449f8e832SCong Wang 
269514641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum,
269614641931SCong Wang  * which has been changed from the hardware checksum, for example, by
269714641931SCong Wang  * __skb_checksum_validate_complete(). And, the original skb->csum must
269814641931SCong Wang  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
269914641931SCong Wang  *
270014641931SCong Wang  * It returns non-zero if the recomputed checksum is still invalid, otherwise
270114641931SCong Wang  * zero. The new checksum is stored back into skb->csum unless the skb is
270214641931SCong Wang  * shared.
270314641931SCong Wang  */
270449f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb)
270549f8e832SCong Wang {
270649f8e832SCong Wang 	__wsum csum;
270749f8e832SCong Wang 	__sum16 sum;
270849f8e832SCong Wang 
270949f8e832SCong Wang 	csum = skb_checksum(skb, 0, skb->len, 0);
271049f8e832SCong Wang 
271149f8e832SCong Wang 	sum = csum_fold(csum_add(skb->csum, csum));
271214641931SCong Wang 	/* This check is inverted, because we already knew the hardware
271314641931SCong Wang 	 * checksum is invalid before calling this function. So, if the
271414641931SCong Wang 	 * re-computed checksum is valid instead, then we have a mismatch
271514641931SCong Wang 	 * between the original skb->csum and skb_checksum(). This means either
271614641931SCong Wang 	 * the original hardware checksum is incorrect or we screw up skb->csum
271714641931SCong Wang 	 * when moving skb->data around.
271814641931SCong Wang 	 */
271949f8e832SCong Wang 	if (likely(!sum)) {
272049f8e832SCong Wang 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
272149f8e832SCong Wang 		    !skb->csum_complete_sw)
27227fe50ac8SCong Wang 			netdev_rx_csum_fault(skb->dev, skb);
272349f8e832SCong Wang 	}
272449f8e832SCong Wang 
272549f8e832SCong Wang 	if (!skb_shared(skb)) {
272649f8e832SCong Wang 		/* Save full packet checksum */
272749f8e832SCong Wang 		skb->csum = csum;
272849f8e832SCong Wang 		skb->ip_summed = CHECKSUM_COMPLETE;
272949f8e832SCong Wang 		skb->csum_complete_sw = 1;
273049f8e832SCong Wang 		skb->csum_valid = !sum;
273149f8e832SCong Wang 	}
273249f8e832SCong Wang 
273349f8e832SCong Wang 	return sum;
273449f8e832SCong Wang }
273549f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete);
273649f8e832SCong Wang 
27379617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
27389617813dSDavide Caratti {
27399617813dSDavide Caratti 	net_warn_ratelimited(
27409617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
27419617813dSDavide Caratti 		__func__);
27429617813dSDavide Caratti 	return 0;
27439617813dSDavide Caratti }
27449617813dSDavide Caratti 
27459617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
27469617813dSDavide Caratti 				       int offset, int len)
27479617813dSDavide Caratti {
27489617813dSDavide Caratti 	net_warn_ratelimited(
27499617813dSDavide Caratti 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
27509617813dSDavide Caratti 		__func__);
27519617813dSDavide Caratti 	return 0;
27529617813dSDavide Caratti }
27539617813dSDavide Caratti 
27549617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = {
27559617813dSDavide Caratti 	.update  = warn_crc32c_csum_update,
27569617813dSDavide Caratti 	.combine = warn_crc32c_csum_combine,
27579617813dSDavide Caratti };
27589617813dSDavide Caratti 
27599617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
27609617813dSDavide Caratti 	&default_crc32c_ops;
27619617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub);
27629617813dSDavide Caratti 
2763af2806f8SThomas Graf  /**
2764af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2765af2806f8SThomas Graf  *	@from: source buffer
2766af2806f8SThomas Graf  *
2767af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2768af2806f8SThomas Graf  *	into skb_zerocopy().
2769af2806f8SThomas Graf  */
2770af2806f8SThomas Graf unsigned int
2771af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2772af2806f8SThomas Graf {
2773af2806f8SThomas Graf 	unsigned int hlen = 0;
2774af2806f8SThomas Graf 
2775af2806f8SThomas Graf 	if (!from->head_frag ||
2776af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2777af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2778af2806f8SThomas Graf 		hlen = skb_headlen(from);
2779af2806f8SThomas Graf 
2780af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2781af2806f8SThomas Graf 		hlen = from->len;
2782af2806f8SThomas Graf 
2783af2806f8SThomas Graf 	return hlen;
2784af2806f8SThomas Graf }
2785af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2786af2806f8SThomas Graf 
2787af2806f8SThomas Graf /**
2788af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2789af2806f8SThomas Graf  *	@to: destination buffer
27907fceb4deSMasanari Iida  *	@from: source buffer
2791af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2792af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2793af2806f8SThomas Graf  *
2794af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2795af2806f8SThomas Graf  *	to the frags in the source buffer.
2796af2806f8SThomas Graf  *
2797af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2798af2806f8SThomas Graf  *	headroom in the `to` buffer.
279936d5fe6aSZoltan Kiss  *
280036d5fe6aSZoltan Kiss  *	Return value:
280136d5fe6aSZoltan Kiss  *	0: everything is OK
280236d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
280336d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2804af2806f8SThomas Graf  */
280536d5fe6aSZoltan Kiss int
280636d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2807af2806f8SThomas Graf {
2808af2806f8SThomas Graf 	int i, j = 0;
2809af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
281036d5fe6aSZoltan Kiss 	int ret;
2811af2806f8SThomas Graf 	struct page *page;
2812af2806f8SThomas Graf 	unsigned int offset;
2813af2806f8SThomas Graf 
2814af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2815af2806f8SThomas Graf 
2816af2806f8SThomas Graf 	/* dont bother with small payloads */
281736d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
281836d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2819af2806f8SThomas Graf 
2820af2806f8SThomas Graf 	if (hlen) {
282136d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
282236d5fe6aSZoltan Kiss 		if (unlikely(ret))
282336d5fe6aSZoltan Kiss 			return ret;
2824af2806f8SThomas Graf 		len -= hlen;
2825af2806f8SThomas Graf 	} else {
2826af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2827af2806f8SThomas Graf 		if (plen) {
2828af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2829af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2830af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2831af2806f8SThomas Graf 			get_page(page);
2832af2806f8SThomas Graf 			j = 1;
2833af2806f8SThomas Graf 			len -= plen;
2834af2806f8SThomas Graf 		}
2835af2806f8SThomas Graf 	}
2836af2806f8SThomas Graf 
2837af2806f8SThomas Graf 	to->truesize += len + plen;
2838af2806f8SThomas Graf 	to->len += len + plen;
2839af2806f8SThomas Graf 	to->data_len += len + plen;
2840af2806f8SThomas Graf 
284136d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
284236d5fe6aSZoltan Kiss 		skb_tx_error(from);
284336d5fe6aSZoltan Kiss 		return -ENOMEM;
284436d5fe6aSZoltan Kiss 	}
28451f8b977aSWillem de Bruijn 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
284636d5fe6aSZoltan Kiss 
2847af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2848af2806f8SThomas Graf 		if (!len)
2849af2806f8SThomas Graf 			break;
2850af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2851af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2852af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2853af2806f8SThomas Graf 		skb_frag_ref(to, j);
2854af2806f8SThomas Graf 		j++;
2855af2806f8SThomas Graf 	}
2856af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
285736d5fe6aSZoltan Kiss 
285836d5fe6aSZoltan Kiss 	return 0;
2859af2806f8SThomas Graf }
2860af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2861af2806f8SThomas Graf 
28621da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
28631da177e4SLinus Torvalds {
2864d3bc23e7SAl Viro 	__wsum csum;
28651da177e4SLinus Torvalds 	long csstart;
28661da177e4SLinus Torvalds 
286784fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
286855508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
28691da177e4SLinus Torvalds 	else
28701da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
28711da177e4SLinus Torvalds 
287209a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
28731da177e4SLinus Torvalds 
2874d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	csum = 0;
28771da177e4SLinus Torvalds 	if (csstart != skb->len)
28781da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
28791da177e4SLinus Torvalds 					      skb->len - csstart, 0);
28801da177e4SLinus Torvalds 
288184fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2882ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
28831da177e4SLinus Torvalds 
2884d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
28851da177e4SLinus Torvalds 	}
28861da177e4SLinus Torvalds }
2887b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds /**
28901da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
28911da177e4SLinus Torvalds  *	@list: list to dequeue from
28921da177e4SLinus Torvalds  *
28931da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
28941da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
28951da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
28961da177e4SLinus Torvalds  */
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
28991da177e4SLinus Torvalds {
29001da177e4SLinus Torvalds 	unsigned long flags;
29011da177e4SLinus Torvalds 	struct sk_buff *result;
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
29041da177e4SLinus Torvalds 	result = __skb_dequeue(list);
29051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
29061da177e4SLinus Torvalds 	return result;
29071da177e4SLinus Torvalds }
2908b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds /**
29111da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
29121da177e4SLinus Torvalds  *	@list: list to dequeue from
29131da177e4SLinus Torvalds  *
29141da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
29151da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
29161da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
29171da177e4SLinus Torvalds  */
29181da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
29191da177e4SLinus Torvalds {
29201da177e4SLinus Torvalds 	unsigned long flags;
29211da177e4SLinus Torvalds 	struct sk_buff *result;
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
29241da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
29251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
29261da177e4SLinus Torvalds 	return result;
29271da177e4SLinus Torvalds }
2928b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds /**
29311da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
29321da177e4SLinus Torvalds  *	@list: list to empty
29331da177e4SLinus Torvalds  *
29341da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
29351da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
29361da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
29371da177e4SLinus Torvalds  */
29381da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
29391da177e4SLinus Torvalds {
29401da177e4SLinus Torvalds 	struct sk_buff *skb;
29411da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
29421da177e4SLinus Torvalds 		kfree_skb(skb);
29431da177e4SLinus Torvalds }
2944b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds /**
29479f5afeaeSYaogong Wang  *	skb_rbtree_purge - empty a skb rbtree
29489f5afeaeSYaogong Wang  *	@root: root of the rbtree to empty
2949385114deSPeter Oskolkov  *	Return value: the sum of truesizes of all purged skbs.
29509f5afeaeSYaogong Wang  *
29519f5afeaeSYaogong Wang  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
29529f5afeaeSYaogong Wang  *	the list and one reference dropped. This function does not take
29539f5afeaeSYaogong Wang  *	any lock. Synchronization should be handled by the caller (e.g., TCP
29549f5afeaeSYaogong Wang  *	out-of-order queue is protected by the socket lock).
29559f5afeaeSYaogong Wang  */
2956385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root)
29579f5afeaeSYaogong Wang {
29587c90584cSEric Dumazet 	struct rb_node *p = rb_first(root);
2959385114deSPeter Oskolkov 	unsigned int sum = 0;
29609f5afeaeSYaogong Wang 
29617c90584cSEric Dumazet 	while (p) {
29627c90584cSEric Dumazet 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
29637c90584cSEric Dumazet 
29647c90584cSEric Dumazet 		p = rb_next(p);
29657c90584cSEric Dumazet 		rb_erase(&skb->rbnode, root);
2966385114deSPeter Oskolkov 		sum += skb->truesize;
29679f5afeaeSYaogong Wang 		kfree_skb(skb);
29687c90584cSEric Dumazet 	}
2969385114deSPeter Oskolkov 	return sum;
29709f5afeaeSYaogong Wang }
29719f5afeaeSYaogong Wang 
29729f5afeaeSYaogong Wang /**
29731da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
29741da177e4SLinus Torvalds  *	@list: list to use
29751da177e4SLinus Torvalds  *	@newsk: buffer to queue
29761da177e4SLinus Torvalds  *
29771da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
29781da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
29791da177e4SLinus Torvalds  *	safely.
29801da177e4SLinus Torvalds  *
29811da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
29821da177e4SLinus Torvalds  */
29831da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds 	unsigned long flags;
29861da177e4SLinus Torvalds 
29871da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
29881da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
29891da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
29901da177e4SLinus Torvalds }
2991b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds /**
29941da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
29951da177e4SLinus Torvalds  *	@list: list to use
29961da177e4SLinus Torvalds  *	@newsk: buffer to queue
29971da177e4SLinus Torvalds  *
29981da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
29991da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
30001da177e4SLinus Torvalds  *	safely.
30011da177e4SLinus Torvalds  *
30021da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
30031da177e4SLinus Torvalds  */
30041da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	unsigned long flags;
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30091da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
30101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30111da177e4SLinus Torvalds }
3012b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
30138728b834SDavid S. Miller 
30141da177e4SLinus Torvalds /**
30151da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
30161da177e4SLinus Torvalds  *	@skb: buffer to remove
30178728b834SDavid S. Miller  *	@list: list to use
30181da177e4SLinus Torvalds  *
30198728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
30208728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
30211da177e4SLinus Torvalds  *
30228728b834SDavid S. Miller  *	You must know what list the SKB is on.
30231da177e4SLinus Torvalds  */
30248728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
30251da177e4SLinus Torvalds {
30261da177e4SLinus Torvalds 	unsigned long flags;
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
30298728b834SDavid S. Miller 	__skb_unlink(skb, list);
30301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
30311da177e4SLinus Torvalds }
3032b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds /**
30351da177e4SLinus Torvalds  *	skb_append	-	append a buffer
30361da177e4SLinus Torvalds  *	@old: buffer to insert after
30371da177e4SLinus Torvalds  *	@newsk: buffer to insert
30388728b834SDavid S. Miller  *	@list: list to use
30391da177e4SLinus Torvalds  *
30401da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
30411da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
30421da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
30431da177e4SLinus Torvalds  */
30448728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
30451da177e4SLinus Torvalds {
30461da177e4SLinus Torvalds 	unsigned long flags;
30471da177e4SLinus Torvalds 
30488728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
30497de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
30508728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
30511da177e4SLinus Torvalds }
3052b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
30551da177e4SLinus Torvalds 					   struct sk_buff* skb1,
30561da177e4SLinus Torvalds 					   const u32 len, const int pos)
30571da177e4SLinus Torvalds {
30581da177e4SLinus Torvalds 	int i;
30591da177e4SLinus Torvalds 
3060d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3061d626f62bSArnaldo Carvalho de Melo 					 pos - len);
30621da177e4SLinus Torvalds 	/* And move data appendix as is. */
30631da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
30641da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
30671da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
30681da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
30691da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
30701da177e4SLinus Torvalds 	skb->data_len		   = 0;
30711da177e4SLinus Torvalds 	skb->len		   = len;
307227a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
30731da177e4SLinus Torvalds }
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
30761da177e4SLinus Torvalds 				       struct sk_buff* skb1,
30771da177e4SLinus Torvalds 				       const u32 len, int pos)
30781da177e4SLinus Torvalds {
30791da177e4SLinus Torvalds 	int i, k = 0;
30801da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
30831da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
30841da177e4SLinus Torvalds 	skb->len		  = len;
30851da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
30889e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 		if (pos + size > len) {
30911da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds 			if (pos < len) {
30941da177e4SLinus Torvalds 				/* Split frag.
30951da177e4SLinus Torvalds 				 * We have two variants in this case:
30961da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
30971da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
30981da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
30991da177e4SLinus Torvalds 				 *    where splitting is expensive.
31001da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
31011da177e4SLinus Torvalds 				 */
3102ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
31031da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
31049e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
31059e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
31061da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
31071da177e4SLinus Torvalds 			}
31081da177e4SLinus Torvalds 			k++;
31091da177e4SLinus Torvalds 		} else
31101da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
31111da177e4SLinus Torvalds 		pos += size;
31121da177e4SLinus Torvalds 	}
31131da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
31141da177e4SLinus Torvalds }
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds /**
31171da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
31181da177e4SLinus Torvalds  * @skb: the buffer to split
31191da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
31201da177e4SLinus Torvalds  * @len: new length for skb
31211da177e4SLinus Torvalds  */
31221da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
31231da177e4SLinus Torvalds {
31241da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
31251da177e4SLinus Torvalds 
3126fff88030SWillem de Bruijn 	skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3127fff88030SWillem de Bruijn 				      SKBTX_SHARED_FRAG;
31281f8b977aSWillem de Bruijn 	skb_zerocopy_clone(skb1, skb, 0);
31291da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
31301da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
31311da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
31321da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
31331da177e4SLinus Torvalds }
3134b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
31351da177e4SLinus Torvalds 
31369f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
31379f782db3SIlpo Järvinen  *
31389f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
31399f782db3SIlpo Järvinen  */
3140832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
3141832d11c5SIlpo Järvinen {
31420ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3143832d11c5SIlpo Järvinen }
3144832d11c5SIlpo Järvinen 
3145832d11c5SIlpo Järvinen /**
3146832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
3147832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
3148832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
3149832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
3150832d11c5SIlpo Järvinen  *
3151832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
315220e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3153832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
3154832d11c5SIlpo Järvinen  *
3155832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
3156832d11c5SIlpo Järvinen  *
3157832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
3158832d11c5SIlpo Järvinen  * to have non-paged data as well.
3159832d11c5SIlpo Järvinen  *
3160832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
3161832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
3162832d11c5SIlpo Järvinen  */
3163832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3164832d11c5SIlpo Järvinen {
3165832d11c5SIlpo Järvinen 	int from, to, merge, todo;
3166832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
3167832d11c5SIlpo Järvinen 
3168832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
3169f8071cdeSEric Dumazet 
3170f8071cdeSEric Dumazet 	if (skb_headlen(skb))
3171f8071cdeSEric Dumazet 		return 0;
31721f8b977aSWillem de Bruijn 	if (skb_zcopy(tgt) || skb_zcopy(skb))
31731f8b977aSWillem de Bruijn 		return 0;
3174832d11c5SIlpo Järvinen 
3175832d11c5SIlpo Järvinen 	todo = shiftlen;
3176832d11c5SIlpo Järvinen 	from = 0;
3177832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
3178832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
3179832d11c5SIlpo Järvinen 
3180832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
3181832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
3182832d11c5SIlpo Järvinen 	 */
3183832d11c5SIlpo Järvinen 	if (!to ||
3184ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3185ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
3186832d11c5SIlpo Järvinen 		merge = -1;
3187832d11c5SIlpo Järvinen 	} else {
3188832d11c5SIlpo Järvinen 		merge = to - 1;
3189832d11c5SIlpo Järvinen 
31909e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
3191832d11c5SIlpo Järvinen 		if (todo < 0) {
3192832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
3193832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
3194832d11c5SIlpo Järvinen 				return 0;
3195832d11c5SIlpo Järvinen 
31969f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
31979f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
3198832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
3199832d11c5SIlpo Järvinen 
32009e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
32019e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
3202832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
3203832d11c5SIlpo Järvinen 
3204832d11c5SIlpo Järvinen 			goto onlymerged;
3205832d11c5SIlpo Järvinen 		}
3206832d11c5SIlpo Järvinen 
3207832d11c5SIlpo Järvinen 		from++;
3208832d11c5SIlpo Järvinen 	}
3209832d11c5SIlpo Järvinen 
3210832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
3211832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
3212832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3213832d11c5SIlpo Järvinen 		return 0;
3214832d11c5SIlpo Järvinen 
3215832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3216832d11c5SIlpo Järvinen 		return 0;
3217832d11c5SIlpo Järvinen 
3218832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3219832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
3220832d11c5SIlpo Järvinen 			return 0;
3221832d11c5SIlpo Järvinen 
3222832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
3223832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
3224832d11c5SIlpo Järvinen 
32259e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
3226832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
32279e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
3228832d11c5SIlpo Järvinen 			from++;
3229832d11c5SIlpo Järvinen 			to++;
3230832d11c5SIlpo Järvinen 
3231832d11c5SIlpo Järvinen 		} else {
3232ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
3233832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
3234832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
32359e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
3236832d11c5SIlpo Järvinen 
3237832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
32389e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
3239832d11c5SIlpo Järvinen 			todo = 0;
3240832d11c5SIlpo Järvinen 
3241832d11c5SIlpo Järvinen 			to++;
3242832d11c5SIlpo Järvinen 			break;
3243832d11c5SIlpo Järvinen 		}
3244832d11c5SIlpo Järvinen 	}
3245832d11c5SIlpo Järvinen 
3246832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
3247832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
3248832d11c5SIlpo Järvinen 
3249832d11c5SIlpo Järvinen 	if (merge >= 0) {
3250832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
3251832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
3252832d11c5SIlpo Järvinen 
32539e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3254ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
3255832d11c5SIlpo Järvinen 	}
3256832d11c5SIlpo Järvinen 
3257832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
3258832d11c5SIlpo Järvinen 	to = 0;
3259832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
3260832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3261832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
3262832d11c5SIlpo Järvinen 
3263832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3264832d11c5SIlpo Järvinen 
3265832d11c5SIlpo Järvinen onlymerged:
3266832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
3267832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
3268832d11c5SIlpo Järvinen 	 */
3269832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
3270832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
3271832d11c5SIlpo Järvinen 
3272832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
3273832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
3274832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
3275832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
3276832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
3277832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
3278832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
3279832d11c5SIlpo Järvinen 
3280832d11c5SIlpo Järvinen 	return shiftlen;
3281832d11c5SIlpo Järvinen }
3282832d11c5SIlpo Järvinen 
3283677e90edSThomas Graf /**
3284677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
3285677e90edSThomas Graf  * @skb: the buffer to read
3286677e90edSThomas Graf  * @from: lower offset of data to be read
3287677e90edSThomas Graf  * @to: upper offset of data to be read
3288677e90edSThomas Graf  * @st: state variable
3289677e90edSThomas Graf  *
3290677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
3291677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
3292677e90edSThomas Graf  */
3293677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3294677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
3295677e90edSThomas Graf {
3296677e90edSThomas Graf 	st->lower_offset = from;
3297677e90edSThomas Graf 	st->upper_offset = to;
3298677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
3299677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
3300677e90edSThomas Graf 	st->frag_data = NULL;
3301677e90edSThomas Graf }
3302b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
3303677e90edSThomas Graf 
3304677e90edSThomas Graf /**
3305677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
3306677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
3307677e90edSThomas Graf  * @data: destination pointer for data to be returned
3308677e90edSThomas Graf  * @st: state variable
3309677e90edSThomas Graf  *
3310bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
3311677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
3312bc32383cSMathias Krause  * the head of the data block to @data and returns the length
3313677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
3314677e90edSThomas Graf  * offset has been reached.
3315677e90edSThomas Graf  *
3316677e90edSThomas Graf  * The caller is not required to consume all of the data
3317bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
3318677e90edSThomas Graf  * of bytes already consumed and the next call to
3319677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
3320677e90edSThomas Graf  *
332125985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
3322e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
3323677e90edSThomas Graf  *       reads of potentially non linear data.
3324677e90edSThomas Graf  *
3325bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
3326677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
3327677e90edSThomas Graf  *       a stack for this purpose.
3328677e90edSThomas Graf  */
3329677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3330677e90edSThomas Graf 			  struct skb_seq_state *st)
3331677e90edSThomas Graf {
3332677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3333677e90edSThomas Graf 	skb_frag_t *frag;
3334677e90edSThomas Graf 
3335aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
3336aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
3337aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
3338aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
3339aeb193eaSWedson Almeida Filho 		}
3340677e90edSThomas Graf 		return 0;
3341aeb193eaSWedson Almeida Filho 	}
3342677e90edSThomas Graf 
3343677e90edSThomas Graf next_skb:
334495e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3345677e90edSThomas Graf 
3346995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
334795e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3348677e90edSThomas Graf 		return block_limit - abs_offset;
3349677e90edSThomas Graf 	}
3350677e90edSThomas Graf 
3351677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
3352677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
3353677e90edSThomas Graf 
3354677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3355677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
33569e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
3357677e90edSThomas Graf 
3358677e90edSThomas Graf 		if (abs_offset < block_limit) {
3359677e90edSThomas Graf 			if (!st->frag_data)
336051c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
3361677e90edSThomas Graf 
3362677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
3363677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
3364677e90edSThomas Graf 
3365677e90edSThomas Graf 			return block_limit - abs_offset;
3366677e90edSThomas Graf 		}
3367677e90edSThomas Graf 
3368677e90edSThomas Graf 		if (st->frag_data) {
336951c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
3370677e90edSThomas Graf 			st->frag_data = NULL;
3371677e90edSThomas Graf 		}
3372677e90edSThomas Graf 
3373677e90edSThomas Graf 		st->frag_idx++;
33749e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
3375677e90edSThomas Graf 	}
3376677e90edSThomas Graf 
33775b5a60daSOlaf Kirch 	if (st->frag_data) {
337851c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
33795b5a60daSOlaf Kirch 		st->frag_data = NULL;
33805b5a60daSOlaf Kirch 	}
33815b5a60daSOlaf Kirch 
338221dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3383677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
338495e3b24cSHerbert Xu 		st->frag_idx = 0;
3385677e90edSThomas Graf 		goto next_skb;
338671b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
338771b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
338871b3346dSShyam Iyer 		st->frag_idx = 0;
3389677e90edSThomas Graf 		goto next_skb;
3390677e90edSThomas Graf 	}
3391677e90edSThomas Graf 
3392677e90edSThomas Graf 	return 0;
3393677e90edSThomas Graf }
3394b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
3395677e90edSThomas Graf 
3396677e90edSThomas Graf /**
3397677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
3398677e90edSThomas Graf  * @st: state variable
3399677e90edSThomas Graf  *
3400677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
3401677e90edSThomas Graf  * returned 0.
3402677e90edSThomas Graf  */
3403677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
3404677e90edSThomas Graf {
3405677e90edSThomas Graf 	if (st->frag_data)
340651c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
3407677e90edSThomas Graf }
3408b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
3409677e90edSThomas Graf 
34103fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
34113fc7e8a6SThomas Graf 
34123fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
34133fc7e8a6SThomas Graf 					  struct ts_config *conf,
34143fc7e8a6SThomas Graf 					  struct ts_state *state)
34153fc7e8a6SThomas Graf {
34163fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
34173fc7e8a6SThomas Graf }
34183fc7e8a6SThomas Graf 
34193fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
34203fc7e8a6SThomas Graf {
34213fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
34223fc7e8a6SThomas Graf }
34233fc7e8a6SThomas Graf 
34243fc7e8a6SThomas Graf /**
34253fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
34263fc7e8a6SThomas Graf  * @skb: the buffer to look in
34273fc7e8a6SThomas Graf  * @from: search offset
34283fc7e8a6SThomas Graf  * @to: search limit
34293fc7e8a6SThomas Graf  * @config: textsearch configuration
34303fc7e8a6SThomas Graf  *
34313fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
34323fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
34333fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
34343fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
34353fc7e8a6SThomas Graf  */
34363fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3437059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
34383fc7e8a6SThomas Graf {
3439059a2440SBojan Prtvar 	struct ts_state state;
3440f72b948dSPhil Oester 	unsigned int ret;
3441f72b948dSPhil Oester 
34423fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
34433fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
34443fc7e8a6SThomas Graf 
3445059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
34463fc7e8a6SThomas Graf 
3447059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
3448f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
34493fc7e8a6SThomas Graf }
3450b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
34513fc7e8a6SThomas Graf 
3452be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3453be12a1feSHannes Frederic Sowa 			 int offset, size_t size)
3454be12a1feSHannes Frederic Sowa {
3455be12a1feSHannes Frederic Sowa 	int i = skb_shinfo(skb)->nr_frags;
3456be12a1feSHannes Frederic Sowa 
3457be12a1feSHannes Frederic Sowa 	if (skb_can_coalesce(skb, i, page, offset)) {
3458be12a1feSHannes Frederic Sowa 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3459be12a1feSHannes Frederic Sowa 	} else if (i < MAX_SKB_FRAGS) {
3460be12a1feSHannes Frederic Sowa 		get_page(page);
3461be12a1feSHannes Frederic Sowa 		skb_fill_page_desc(skb, i, page, offset, size);
3462be12a1feSHannes Frederic Sowa 	} else {
3463be12a1feSHannes Frederic Sowa 		return -EMSGSIZE;
3464be12a1feSHannes Frederic Sowa 	}
3465be12a1feSHannes Frederic Sowa 
3466be12a1feSHannes Frederic Sowa 	return 0;
3467be12a1feSHannes Frederic Sowa }
3468be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3469be12a1feSHannes Frederic Sowa 
3470cbb042f9SHerbert Xu /**
3471cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
3472cbb042f9SHerbert Xu  *	@skb: buffer to update
3473cbb042f9SHerbert Xu  *	@len: length of data pulled
3474cbb042f9SHerbert Xu  *
3475cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
3476fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
347784fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
347884fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
347984fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
3480cbb042f9SHerbert Xu  */
3481af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3482cbb042f9SHerbert Xu {
348331b33dfbSPravin B Shelar 	unsigned char *data = skb->data;
348431b33dfbSPravin B Shelar 
3485cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
348631b33dfbSPravin B Shelar 	__skb_pull(skb, len);
348731b33dfbSPravin B Shelar 	skb_postpull_rcsum(skb, data, len);
348831b33dfbSPravin B Shelar 	return skb->data;
3489cbb042f9SHerbert Xu }
3490f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3491f94691acSArnaldo Carvalho de Melo 
349213acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
349313acc94eSYonghong Song {
349413acc94eSYonghong Song 	skb_frag_t head_frag;
349513acc94eSYonghong Song 	struct page *page;
349613acc94eSYonghong Song 
349713acc94eSYonghong Song 	page = virt_to_head_page(frag_skb->head);
349813acc94eSYonghong Song 	head_frag.page.p = page;
349913acc94eSYonghong Song 	head_frag.page_offset = frag_skb->data -
350013acc94eSYonghong Song 		(unsigned char *)page_address(page);
350113acc94eSYonghong Song 	head_frag.size = skb_headlen(frag_skb);
350213acc94eSYonghong Song 	return head_frag;
350313acc94eSYonghong Song }
350413acc94eSYonghong Song 
3505f4c50d99SHerbert Xu /**
3506f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
3507df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
3508576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
3509f4c50d99SHerbert Xu  *
3510f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
35114c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
35124c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
3513f4c50d99SHerbert Xu  */
3514df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
3515df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
3516f4c50d99SHerbert Xu {
3517f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
3518f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
35191a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3520df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3521df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3522df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
35231fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
3524f4c50d99SHerbert Xu 	unsigned int offset = doffset;
3525df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3526802ab55aSAlexander Duyck 	unsigned int partial_segs = 0;
3527f4c50d99SHerbert Xu 	unsigned int headroom;
3528802ab55aSAlexander Duyck 	unsigned int len = head_skb->len;
3529ec5f0615SPravin B Shelar 	__be16 proto;
353036c98382SAlexander Duyck 	bool csum, sg;
3531df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3532f4c50d99SHerbert Xu 	int err = -ENOMEM;
3533f4c50d99SHerbert Xu 	int i = 0;
3534f4c50d99SHerbert Xu 	int pos;
353553d6471cSVlad Yasevich 	int dummy;
3536f4c50d99SHerbert Xu 
35375882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
353853d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
3539ec5f0615SPravin B Shelar 	if (unlikely(!proto))
3540ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
3541ec5f0615SPravin B Shelar 
354236c98382SAlexander Duyck 	sg = !!(features & NETIF_F_SG);
3543f245d079SAlexander Duyck 	csum = !!can_checksum_protocol(features, proto);
35447e2b10c1STom Herbert 
354507b26c94SSteffen Klassert 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
354607b26c94SSteffen Klassert 		if (!(features & NETIF_F_GSO_PARTIAL)) {
354707b26c94SSteffen Klassert 			struct sk_buff *iter;
354843170c4eSIlan Tayari 			unsigned int frag_len;
354907b26c94SSteffen Klassert 
355007b26c94SSteffen Klassert 			if (!list_skb ||
355107b26c94SSteffen Klassert 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
355207b26c94SSteffen Klassert 				goto normal;
355307b26c94SSteffen Klassert 
355443170c4eSIlan Tayari 			/* If we get here then all the required
355543170c4eSIlan Tayari 			 * GSO features except frag_list are supported.
355643170c4eSIlan Tayari 			 * Try to split the SKB to multiple GSO SKBs
355743170c4eSIlan Tayari 			 * with no frag_list.
355843170c4eSIlan Tayari 			 * Currently we can do that only when the buffers don't
355943170c4eSIlan Tayari 			 * have a linear part and all the buffers except
356043170c4eSIlan Tayari 			 * the last are of the same length.
356107b26c94SSteffen Klassert 			 */
356243170c4eSIlan Tayari 			frag_len = list_skb->len;
356307b26c94SSteffen Klassert 			skb_walk_frags(head_skb, iter) {
356443170c4eSIlan Tayari 				if (frag_len != iter->len && iter->next)
356543170c4eSIlan Tayari 					goto normal;
3566eaffadbbSIlan Tayari 				if (skb_headlen(iter) && !iter->head_frag)
356707b26c94SSteffen Klassert 					goto normal;
356807b26c94SSteffen Klassert 
356907b26c94SSteffen Klassert 				len -= iter->len;
357007b26c94SSteffen Klassert 			}
357143170c4eSIlan Tayari 
357243170c4eSIlan Tayari 			if (len != frag_len)
357343170c4eSIlan Tayari 				goto normal;
357407b26c94SSteffen Klassert 		}
357507b26c94SSteffen Klassert 
3576802ab55aSAlexander Duyck 		/* GSO partial only requires that we trim off any excess that
3577802ab55aSAlexander Duyck 		 * doesn't fit into an MSS sized block, so take care of that
3578802ab55aSAlexander Duyck 		 * now.
3579802ab55aSAlexander Duyck 		 */
3580802ab55aSAlexander Duyck 		partial_segs = len / mss;
3581d7fb5a80SAlexander Duyck 		if (partial_segs > 1)
3582802ab55aSAlexander Duyck 			mss *= partial_segs;
3583d7fb5a80SAlexander Duyck 		else
3584d7fb5a80SAlexander Duyck 			partial_segs = 0;
3585802ab55aSAlexander Duyck 	}
3586802ab55aSAlexander Duyck 
358707b26c94SSteffen Klassert normal:
3588df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
3589df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
3590f4c50d99SHerbert Xu 
3591f4c50d99SHerbert Xu 	do {
3592f4c50d99SHerbert Xu 		struct sk_buff *nskb;
35938cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
3594c8884eddSHerbert Xu 		int hsize;
3595f4c50d99SHerbert Xu 		int size;
3596f4c50d99SHerbert Xu 
35973953c46cSMarcelo Ricardo Leitner 		if (unlikely(mss == GSO_BY_FRAGS)) {
35983953c46cSMarcelo Ricardo Leitner 			len = list_skb->len;
35993953c46cSMarcelo Ricardo Leitner 		} else {
3600df5771ffSMichael S. Tsirkin 			len = head_skb->len - offset;
3601f4c50d99SHerbert Xu 			if (len > mss)
3602f4c50d99SHerbert Xu 				len = mss;
36033953c46cSMarcelo Ricardo Leitner 		}
3604f4c50d99SHerbert Xu 
3605df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
3606f4c50d99SHerbert Xu 		if (hsize < 0)
3607f4c50d99SHerbert Xu 			hsize = 0;
3608c8884eddSHerbert Xu 		if (hsize > len || !sg)
3609c8884eddSHerbert Xu 			hsize = len;
3610f4c50d99SHerbert Xu 
36111a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
36121a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
36131a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
361489319d38SHerbert Xu 
36159d8506ccSHerbert Xu 			i = 0;
36161a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
36171a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
36181fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
36191a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
36209d8506ccSHerbert Xu 
36219d8506ccSHerbert Xu 			while (pos < offset + len) {
36229d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
36239d8506ccSHerbert Xu 
36244e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
36259d8506ccSHerbert Xu 				if (pos + size > offset + len)
36269d8506ccSHerbert Xu 					break;
36279d8506ccSHerbert Xu 
36289d8506ccSHerbert Xu 				i++;
36299d8506ccSHerbert Xu 				pos += size;
36304e1beba1SMichael S. Tsirkin 				frag++;
36319d8506ccSHerbert Xu 			}
36329d8506ccSHerbert Xu 
36331a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
36341a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
363589319d38SHerbert Xu 
3636f4c50d99SHerbert Xu 			if (unlikely(!nskb))
3637f4c50d99SHerbert Xu 				goto err;
3638f4c50d99SHerbert Xu 
36399d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
36409d8506ccSHerbert Xu 				kfree_skb(nskb);
36419d8506ccSHerbert Xu 				goto err;
36429d8506ccSHerbert Xu 			}
36439d8506ccSHerbert Xu 
3644ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
364589319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
364689319d38SHerbert Xu 				kfree_skb(nskb);
364789319d38SHerbert Xu 				goto err;
364889319d38SHerbert Xu 			}
364989319d38SHerbert Xu 
3650ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
365189319d38SHerbert Xu 			skb_release_head_state(nskb);
365289319d38SHerbert Xu 			__skb_push(nskb, doffset);
365389319d38SHerbert Xu 		} else {
3654c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3655df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3656c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
365789319d38SHerbert Xu 
365889319d38SHerbert Xu 			if (unlikely(!nskb))
365989319d38SHerbert Xu 				goto err;
366089319d38SHerbert Xu 
366189319d38SHerbert Xu 			skb_reserve(nskb, headroom);
366289319d38SHerbert Xu 			__skb_put(nskb, doffset);
366389319d38SHerbert Xu 		}
366489319d38SHerbert Xu 
3665f4c50d99SHerbert Xu 		if (segs)
3666f4c50d99SHerbert Xu 			tail->next = nskb;
3667f4c50d99SHerbert Xu 		else
3668f4c50d99SHerbert Xu 			segs = nskb;
3669f4c50d99SHerbert Xu 		tail = nskb;
3670f4c50d99SHerbert Xu 
3671df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3672f4c50d99SHerbert Xu 
3673030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3674fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
367568c33163SPravin B Shelar 
3676df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
367768c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
367868c33163SPravin B Shelar 						 doffset + tnl_hlen);
367989319d38SHerbert Xu 
36809d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
36811cdbcb79SSimon Horman 			goto perform_csum_check;
368289319d38SHerbert Xu 
36837fbeffedSAlexander Duyck 		if (!sg) {
36847fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
36856f85a124SHerbert Xu 				nskb->ip_summed = CHECKSUM_NONE;
368676443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
368776443456SAlexander Duyck 				skb_copy_and_csum_bits(head_skb, offset,
3688f4c50d99SHerbert Xu 						       skb_put(nskb, len),
3689f4c50d99SHerbert Xu 						       len, 0);
36907e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
3691de843723STom Herbert 				skb_headroom(nskb) + doffset;
3692f4c50d99SHerbert Xu 			continue;
3693f4c50d99SHerbert Xu 		}
3694f4c50d99SHerbert Xu 
36958cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3696f4c50d99SHerbert Xu 
3697df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3698d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3699f4c50d99SHerbert Xu 
3700fff88030SWillem de Bruijn 		skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3701df5771ffSMichael S. Tsirkin 					      SKBTX_SHARED_FRAG;
3702cef401deSEric Dumazet 
3703bf5c25d6SWillem de Bruijn 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3704bf5c25d6SWillem de Bruijn 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3705bf5c25d6SWillem de Bruijn 			goto err;
3706bf5c25d6SWillem de Bruijn 
37079d8506ccSHerbert Xu 		while (pos < offset + len) {
37089d8506ccSHerbert Xu 			if (i >= nfrags) {
37099d8506ccSHerbert Xu 				i = 0;
37101a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
37111a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
37121fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
371313acc94eSYonghong Song 				if (!skb_headlen(list_skb)) {
37149d8506ccSHerbert Xu 					BUG_ON(!nfrags);
371513acc94eSYonghong Song 				} else {
371613acc94eSYonghong Song 					BUG_ON(!list_skb->head_frag);
37179d8506ccSHerbert Xu 
371813acc94eSYonghong Song 					/* to make room for head_frag. */
371913acc94eSYonghong Song 					i--;
372013acc94eSYonghong Song 					frag--;
372113acc94eSYonghong Song 				}
3722bf5c25d6SWillem de Bruijn 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3723bf5c25d6SWillem de Bruijn 				    skb_zerocopy_clone(nskb, frag_skb,
3724bf5c25d6SWillem de Bruijn 						       GFP_ATOMIC))
3725bf5c25d6SWillem de Bruijn 					goto err;
3726bf5c25d6SWillem de Bruijn 
37271a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
37289d8506ccSHerbert Xu 			}
37299d8506ccSHerbert Xu 
37309d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
37319d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
37329d8506ccSHerbert Xu 				net_warn_ratelimited(
37339d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
37349d8506ccSHerbert Xu 					pos, mss);
3735ff907a11SEric Dumazet 				err = -EINVAL;
37369d8506ccSHerbert Xu 				goto err;
37379d8506ccSHerbert Xu 			}
37389d8506ccSHerbert Xu 
373913acc94eSYonghong Song 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
37408cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
37418cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3742f4c50d99SHerbert Xu 
3743f4c50d99SHerbert Xu 			if (pos < offset) {
37448cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
37458cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3746f4c50d99SHerbert Xu 			}
3747f4c50d99SHerbert Xu 
374889319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3749f4c50d99SHerbert Xu 
3750f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3751f4c50d99SHerbert Xu 				i++;
37524e1beba1SMichael S. Tsirkin 				frag++;
3753f4c50d99SHerbert Xu 				pos += size;
3754f4c50d99SHerbert Xu 			} else {
37558cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
375689319d38SHerbert Xu 				goto skip_fraglist;
3757f4c50d99SHerbert Xu 			}
3758f4c50d99SHerbert Xu 
37598cb19905SMichael S. Tsirkin 			nskb_frag++;
3760f4c50d99SHerbert Xu 		}
3761f4c50d99SHerbert Xu 
376289319d38SHerbert Xu skip_fraglist:
3763f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3764f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3765f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3766ec5f0615SPravin B Shelar 
37671cdbcb79SSimon Horman perform_csum_check:
37687fbeffedSAlexander Duyck 		if (!csum) {
3769ff907a11SEric Dumazet 			if (skb_has_shared_frag(nskb) &&
3770ff907a11SEric Dumazet 			    __skb_linearize(nskb))
3771ddff00d4SAlexander Duyck 				goto err;
3772ff907a11SEric Dumazet 
37737fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
3774ec5f0615SPravin B Shelar 				nskb->ip_summed = CHECKSUM_NONE;
377576443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
377676443456SAlexander Duyck 				skb_checksum(nskb, doffset,
377776443456SAlexander Duyck 					     nskb->len - doffset, 0);
37787e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
37797e2b10c1STom Herbert 				skb_headroom(nskb) + doffset;
3780ec5f0615SPravin B Shelar 		}
3781df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3782f4c50d99SHerbert Xu 
3783bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
3784bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
3785bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
3786bec3cfdcSEric Dumazet 	 */
3787bec3cfdcSEric Dumazet 	segs->prev = tail;
3788432c856fSToshiaki Makita 
3789802ab55aSAlexander Duyck 	if (partial_segs) {
379007b26c94SSteffen Klassert 		struct sk_buff *iter;
3791802ab55aSAlexander Duyck 		int type = skb_shinfo(head_skb)->gso_type;
379207b26c94SSteffen Klassert 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3793802ab55aSAlexander Duyck 
3794802ab55aSAlexander Duyck 		/* Update type to add partial and then remove dodgy if set */
379507b26c94SSteffen Klassert 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3796802ab55aSAlexander Duyck 		type &= ~SKB_GSO_DODGY;
3797802ab55aSAlexander Duyck 
3798802ab55aSAlexander Duyck 		/* Update GSO info and prepare to start updating headers on
3799802ab55aSAlexander Duyck 		 * our way back down the stack of protocols.
3800802ab55aSAlexander Duyck 		 */
380107b26c94SSteffen Klassert 		for (iter = segs; iter; iter = iter->next) {
380207b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_size = gso_size;
380307b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_segs = partial_segs;
380407b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_type = type;
380507b26c94SSteffen Klassert 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
380607b26c94SSteffen Klassert 		}
380707b26c94SSteffen Klassert 
380807b26c94SSteffen Klassert 		if (tail->len - doffset <= gso_size)
380907b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_size = 0;
381007b26c94SSteffen Klassert 		else if (tail != segs)
381107b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3812802ab55aSAlexander Duyck 	}
3813802ab55aSAlexander Duyck 
3814432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
3815432c856fSToshiaki Makita 	 * using skb_set_owner_w().
3816432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
3817432c856fSToshiaki Makita 	 */
3818432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
3819432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
3820432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
3821432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
3822432c856fSToshiaki Makita 	}
3823f4c50d99SHerbert Xu 	return segs;
3824f4c50d99SHerbert Xu 
3825f4c50d99SHerbert Xu err:
3826289dccbeSEric Dumazet 	kfree_skb_list(segs);
3827f4c50d99SHerbert Xu 	return ERR_PTR(err);
3828f4c50d99SHerbert Xu }
3829f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3830f4c50d99SHerbert Xu 
3831d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
383271d93b39SHerbert Xu {
38338a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
383467147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
383567147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
38368a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
3837715dc1f3SEric Dumazet 	unsigned int delta_truesize;
3838d4546c25SDavid Miller 	struct sk_buff *lp;
383971d93b39SHerbert Xu 
38400ab03f35SSteffen Klassert 	if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
384171d93b39SHerbert Xu 		return -E2BIG;
384271d93b39SHerbert Xu 
384329e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
38448a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
38458a29111cSEric Dumazet 
38468a29111cSEric Dumazet 	if (headlen <= offset) {
384742da6994SHerbert Xu 		skb_frag_t *frag;
384866e92fcfSHerbert Xu 		skb_frag_t *frag2;
38499aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
38509aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
385142da6994SHerbert Xu 
385266e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
38538a29111cSEric Dumazet 			goto merge;
385481705ad1SHerbert Xu 
38558a29111cSEric Dumazet 		offset -= headlen;
38569aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
38579aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3858f5572068SHerbert Xu 
38599aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
38609aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
386166e92fcfSHerbert Xu 		do {
386266e92fcfSHerbert Xu 			*--frag = *--frag2;
386366e92fcfSHerbert Xu 		} while (--i);
386466e92fcfSHerbert Xu 
386566e92fcfSHerbert Xu 		frag->page_offset += offset;
38669e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
386766e92fcfSHerbert Xu 
3868715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3869ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3870ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3871715dc1f3SEric Dumazet 
3872f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3873f5572068SHerbert Xu 		skb->len -= skb->data_len;
3874f5572068SHerbert Xu 		skb->data_len = 0;
3875f5572068SHerbert Xu 
3876715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
38775d38a079SHerbert Xu 		goto done;
3878d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3879d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3880d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3881d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3882d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3883d7e8883cSEric Dumazet 		unsigned int first_offset;
3884d7e8883cSEric Dumazet 
3885d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
38868a29111cSEric Dumazet 			goto merge;
3887d7e8883cSEric Dumazet 
3888d7e8883cSEric Dumazet 		first_offset = skb->data -
3889d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3890d7e8883cSEric Dumazet 			       offset;
3891d7e8883cSEric Dumazet 
3892d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3893d7e8883cSEric Dumazet 
3894d7e8883cSEric Dumazet 		frag->page.p	  = page;
3895d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3896d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3897d7e8883cSEric Dumazet 
3898d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3899d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3900d7e8883cSEric Dumazet 
3901715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3902d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3903d7e8883cSEric Dumazet 		goto done;
39048a29111cSEric Dumazet 	}
390571d93b39SHerbert Xu 
390671d93b39SHerbert Xu merge:
3907715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
390867147ba9SHerbert Xu 	if (offset > headlen) {
3909d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3910d1dc7abfSMichal Schmidt 
3911d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
39129e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3913d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3914d1dc7abfSMichal Schmidt 		skb->len -= eat;
391567147ba9SHerbert Xu 		offset = headlen;
391656035022SHerbert Xu 	}
391756035022SHerbert Xu 
391867147ba9SHerbert Xu 	__skb_pull(skb, offset);
391956035022SHerbert Xu 
392029e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
39218a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
39228a29111cSEric Dumazet 	else
3923c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3924c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
3925f4a775d1SEric Dumazet 	__skb_header_release(skb);
39268a29111cSEric Dumazet 	lp = p;
392771d93b39SHerbert Xu 
39285d38a079SHerbert Xu done:
39295d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
393037fe4732SHerbert Xu 	p->data_len += len;
3931715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
393237fe4732SHerbert Xu 	p->len += len;
39338a29111cSEric Dumazet 	if (lp != p) {
39348a29111cSEric Dumazet 		lp->data_len += len;
39358a29111cSEric Dumazet 		lp->truesize += delta_truesize;
39368a29111cSEric Dumazet 		lp->len += len;
39378a29111cSEric Dumazet 	}
393871d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
393971d93b39SHerbert Xu 	return 0;
394071d93b39SHerbert Xu }
394157c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive);
394271d93b39SHerbert Xu 
3943df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
3944df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE	8
3945df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3946df5042f4SFlorian Westphal 
3947df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = {
3948df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3949df5042f4SFlorian Westphal 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3950df5042f4SFlorian Westphal #endif
39514165079bSFlorian Westphal #ifdef CONFIG_XFRM
39524165079bSFlorian Westphal 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
39534165079bSFlorian Westphal #endif
3954df5042f4SFlorian Westphal };
3955df5042f4SFlorian Westphal 
3956df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void)
3957df5042f4SFlorian Westphal {
3958df5042f4SFlorian Westphal 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3959df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3960df5042f4SFlorian Westphal 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3961df5042f4SFlorian Westphal #endif
39624165079bSFlorian Westphal #ifdef CONFIG_XFRM
39634165079bSFlorian Westphal 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
39644165079bSFlorian Westphal #endif
3965df5042f4SFlorian Westphal 		0;
3966df5042f4SFlorian Westphal }
3967df5042f4SFlorian Westphal 
3968df5042f4SFlorian Westphal static void skb_extensions_init(void)
3969df5042f4SFlorian Westphal {
3970df5042f4SFlorian Westphal 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3971df5042f4SFlorian Westphal 	BUILD_BUG_ON(skb_ext_total_length() > 255);
3972df5042f4SFlorian Westphal 
3973df5042f4SFlorian Westphal 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3974df5042f4SFlorian Westphal 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3975df5042f4SFlorian Westphal 					     0,
3976df5042f4SFlorian Westphal 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3977df5042f4SFlorian Westphal 					     NULL);
3978df5042f4SFlorian Westphal }
3979df5042f4SFlorian Westphal #else
3980df5042f4SFlorian Westphal static void skb_extensions_init(void) {}
3981df5042f4SFlorian Westphal #endif
3982df5042f4SFlorian Westphal 
39831da177e4SLinus Torvalds void __init skb_init(void)
39841da177e4SLinus Torvalds {
398579a8a642SKees Cook 	skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
39861da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
39871da177e4SLinus Torvalds 					      0,
3988e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
398979a8a642SKees Cook 					      offsetof(struct sk_buff, cb),
399079a8a642SKees Cook 					      sizeof_field(struct sk_buff, cb),
399120c2df83SPaul Mundt 					      NULL);
3992d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3993d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
3994d179cd12SDavid S. Miller 						0,
3995e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
399620c2df83SPaul Mundt 						NULL);
3997df5042f4SFlorian Westphal 	skb_extensions_init();
39981da177e4SLinus Torvalds }
39991da177e4SLinus Torvalds 
400051c739d1SDavid S. Miller static int
400148a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
400248a1df65SJason A. Donenfeld 	       unsigned int recursion_level)
4003716ea3a7SDavid Howells {
40041a028e50SDavid S. Miller 	int start = skb_headlen(skb);
40051a028e50SDavid S. Miller 	int i, copy = start - offset;
4006fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
4007716ea3a7SDavid Howells 	int elt = 0;
4008716ea3a7SDavid Howells 
400948a1df65SJason A. Donenfeld 	if (unlikely(recursion_level >= 24))
401048a1df65SJason A. Donenfeld 		return -EMSGSIZE;
401148a1df65SJason A. Donenfeld 
4012716ea3a7SDavid Howells 	if (copy > 0) {
4013716ea3a7SDavid Howells 		if (copy > len)
4014716ea3a7SDavid Howells 			copy = len;
4015642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
4016716ea3a7SDavid Howells 		elt++;
4017716ea3a7SDavid Howells 		if ((len -= copy) == 0)
4018716ea3a7SDavid Howells 			return elt;
4019716ea3a7SDavid Howells 		offset += copy;
4020716ea3a7SDavid Howells 	}
4021716ea3a7SDavid Howells 
4022716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
40231a028e50SDavid S. Miller 		int end;
4024716ea3a7SDavid Howells 
4025547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
40261a028e50SDavid S. Miller 
40279e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4028716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
4029716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
403048a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
403148a1df65SJason A. Donenfeld 				return -EMSGSIZE;
4032716ea3a7SDavid Howells 
4033716ea3a7SDavid Howells 			if (copy > len)
4034716ea3a7SDavid Howells 				copy = len;
4035ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4036642f1490SJens Axboe 					frag->page_offset+offset-start);
4037716ea3a7SDavid Howells 			elt++;
4038716ea3a7SDavid Howells 			if (!(len -= copy))
4039716ea3a7SDavid Howells 				return elt;
4040716ea3a7SDavid Howells 			offset += copy;
4041716ea3a7SDavid Howells 		}
40421a028e50SDavid S. Miller 		start = end;
4043716ea3a7SDavid Howells 	}
4044716ea3a7SDavid Howells 
4045fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
404648a1df65SJason A. Donenfeld 		int end, ret;
4047716ea3a7SDavid Howells 
4048547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
40491a028e50SDavid S. Miller 
4050fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
4051716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
405248a1df65SJason A. Donenfeld 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
405348a1df65SJason A. Donenfeld 				return -EMSGSIZE;
405448a1df65SJason A. Donenfeld 
4055716ea3a7SDavid Howells 			if (copy > len)
4056716ea3a7SDavid Howells 				copy = len;
405748a1df65SJason A. Donenfeld 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
405848a1df65SJason A. Donenfeld 					      copy, recursion_level + 1);
405948a1df65SJason A. Donenfeld 			if (unlikely(ret < 0))
406048a1df65SJason A. Donenfeld 				return ret;
406148a1df65SJason A. Donenfeld 			elt += ret;
4062716ea3a7SDavid Howells 			if ((len -= copy) == 0)
4063716ea3a7SDavid Howells 				return elt;
4064716ea3a7SDavid Howells 			offset += copy;
4065716ea3a7SDavid Howells 		}
40661a028e50SDavid S. Miller 		start = end;
4067716ea3a7SDavid Howells 	}
4068716ea3a7SDavid Howells 	BUG_ON(len);
4069716ea3a7SDavid Howells 	return elt;
4070716ea3a7SDavid Howells }
4071716ea3a7SDavid Howells 
407248a1df65SJason A. Donenfeld /**
407348a1df65SJason A. Donenfeld  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
407448a1df65SJason A. Donenfeld  *	@skb: Socket buffer containing the buffers to be mapped
407548a1df65SJason A. Donenfeld  *	@sg: The scatter-gather list to map into
407648a1df65SJason A. Donenfeld  *	@offset: The offset into the buffer's contents to start mapping
407748a1df65SJason A. Donenfeld  *	@len: Length of buffer space to be mapped
407848a1df65SJason A. Donenfeld  *
407948a1df65SJason A. Donenfeld  *	Fill the specified scatter-gather list with mappings/pointers into a
408048a1df65SJason A. Donenfeld  *	region of the buffer space attached to a socket buffer. Returns either
408148a1df65SJason A. Donenfeld  *	the number of scatterlist items used, or -EMSGSIZE if the contents
408248a1df65SJason A. Donenfeld  *	could not fit.
408348a1df65SJason A. Donenfeld  */
408448a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
408548a1df65SJason A. Donenfeld {
408648a1df65SJason A. Donenfeld 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
408748a1df65SJason A. Donenfeld 
408848a1df65SJason A. Donenfeld 	if (nsg <= 0)
408948a1df65SJason A. Donenfeld 		return nsg;
409048a1df65SJason A. Donenfeld 
409148a1df65SJason A. Donenfeld 	sg_mark_end(&sg[nsg - 1]);
409248a1df65SJason A. Donenfeld 
409348a1df65SJason A. Donenfeld 	return nsg;
409448a1df65SJason A. Donenfeld }
409548a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec);
409648a1df65SJason A. Donenfeld 
409725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
409825a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
409925a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
410025a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
410125a91d8dSFan Du  *
410225a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
410325a91d8dSFan Du  * 1. sg_init_table
410425a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
410525a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
410625a91d8dSFan Du  *
410725a91d8dSFan Du  * This is equivalent to:
410825a91d8dSFan Du  * 1. sg_init_table
410925a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
411025a91d8dSFan Du  * 3. sg_unmark_end
411125a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
411225a91d8dSFan Du  *
411325a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
411425a91d8dSFan Du  * is more preferable.
411525a91d8dSFan Du  */
411625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
411725a91d8dSFan Du 			int offset, int len)
411825a91d8dSFan Du {
411948a1df65SJason A. Donenfeld 	return __skb_to_sgvec(skb, sg, offset, len, 0);
412025a91d8dSFan Du }
412125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
412225a91d8dSFan Du 
412351c739d1SDavid S. Miller 
412451c739d1SDavid S. Miller 
4125716ea3a7SDavid Howells /**
4126716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4127716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
4128716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
4129716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4130716ea3a7SDavid Howells  *
4131716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
4132716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
4133716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
4134716ea3a7SDavid Howells  *
4135716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
4136716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
4137716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
4138716ea3a7SDavid Howells  *
4139716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
4140716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
4141716ea3a7SDavid Howells  */
4142716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4143716ea3a7SDavid Howells {
4144716ea3a7SDavid Howells 	int copyflag;
4145716ea3a7SDavid Howells 	int elt;
4146716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
4147716ea3a7SDavid Howells 
4148716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
4149716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
4150716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
4151716ea3a7SDavid Howells 	 */
4152716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4153716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4154716ea3a7SDavid Howells 		return -ENOMEM;
4155716ea3a7SDavid Howells 
4156716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
415721dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
4158716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
4159716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
4160716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
4161716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
4162716ea3a7SDavid Howells 
4163716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
4164716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4165716ea3a7SDavid Howells 			return -ENOMEM;
4166716ea3a7SDavid Howells 
4167716ea3a7SDavid Howells 		/* Voila! */
4168716ea3a7SDavid Howells 		*trailer = skb;
4169716ea3a7SDavid Howells 		return 1;
4170716ea3a7SDavid Howells 	}
4171716ea3a7SDavid Howells 
4172716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
4173716ea3a7SDavid Howells 
4174716ea3a7SDavid Howells 	elt = 1;
4175716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
4176716ea3a7SDavid Howells 	copyflag = 0;
4177716ea3a7SDavid Howells 
4178716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
4179716ea3a7SDavid Howells 		int ntail = 0;
4180716ea3a7SDavid Howells 
4181716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
4182716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
4183716ea3a7SDavid Howells 		 * after it. */
4184716ea3a7SDavid Howells 
4185716ea3a7SDavid Howells 		if (skb_shared(skb1))
4186716ea3a7SDavid Howells 			copyflag = 1;
4187716ea3a7SDavid Howells 
4188716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
4189716ea3a7SDavid Howells 
4190716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
4191716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
419221dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
4193716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
4194716ea3a7SDavid Howells 				ntail = tailbits + 128;
4195716ea3a7SDavid Howells 		}
4196716ea3a7SDavid Howells 
4197716ea3a7SDavid Howells 		if (copyflag ||
4198716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
4199716ea3a7SDavid Howells 		    ntail ||
4200716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
420121dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
4202716ea3a7SDavid Howells 			struct sk_buff *skb2;
4203716ea3a7SDavid Howells 
4204716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
4205716ea3a7SDavid Howells 			if (ntail == 0)
4206716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
4207716ea3a7SDavid Howells 			else
4208716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
4209716ea3a7SDavid Howells 						       skb_headroom(skb1),
4210716ea3a7SDavid Howells 						       ntail,
4211716ea3a7SDavid Howells 						       GFP_ATOMIC);
4212716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
4213716ea3a7SDavid Howells 				return -ENOMEM;
4214716ea3a7SDavid Howells 
4215716ea3a7SDavid Howells 			if (skb1->sk)
4216716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
4217716ea3a7SDavid Howells 
4218716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
4219716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
4220716ea3a7SDavid Howells 
4221716ea3a7SDavid Howells 			skb2->next = skb1->next;
4222716ea3a7SDavid Howells 			*skb_p = skb2;
4223716ea3a7SDavid Howells 			kfree_skb(skb1);
4224716ea3a7SDavid Howells 			skb1 = skb2;
4225716ea3a7SDavid Howells 		}
4226716ea3a7SDavid Howells 		elt++;
4227716ea3a7SDavid Howells 		*trailer = skb1;
4228716ea3a7SDavid Howells 		skb_p = &skb1->next;
4229716ea3a7SDavid Howells 	}
4230716ea3a7SDavid Howells 
4231716ea3a7SDavid Howells 	return elt;
4232716ea3a7SDavid Howells }
4233b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
4234716ea3a7SDavid Howells 
4235b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
4236b1faf566SEric Dumazet {
4237b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
4238b1faf566SEric Dumazet 
4239b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4240b1faf566SEric Dumazet }
4241b1faf566SEric Dumazet 
42428605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
42438605330aSSoheil Hassas Yeganeh {
42448605330aSSoheil Hassas Yeganeh 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
42458605330aSSoheil Hassas Yeganeh 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
42468605330aSSoheil Hassas Yeganeh 	 */
42478605330aSSoheil Hassas Yeganeh 	skb->pkt_type = PACKET_OUTGOING;
42488605330aSSoheil Hassas Yeganeh 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
42498605330aSSoheil Hassas Yeganeh }
42508605330aSSoheil Hassas Yeganeh 
4251b1faf566SEric Dumazet /*
4252b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4253b1faf566SEric Dumazet  */
4254b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4255b1faf566SEric Dumazet {
4256b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
425795c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
4258b1faf566SEric Dumazet 		return -ENOMEM;
4259b1faf566SEric Dumazet 
4260b1faf566SEric Dumazet 	skb_orphan(skb);
4261b1faf566SEric Dumazet 	skb->sk = sk;
4262b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
4263b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
42648605330aSSoheil Hassas Yeganeh 	skb_set_err_queue(skb);
4265b1faf566SEric Dumazet 
4266abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
4267abb57ea4SEric Dumazet 	skb_dst_force(skb);
4268abb57ea4SEric Dumazet 
4269b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
4270b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
42716e5d58fdSVinicius Costa Gomes 		sk->sk_error_report(sk);
4272b1faf566SEric Dumazet 	return 0;
4273b1faf566SEric Dumazet }
4274b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
4275b1faf566SEric Dumazet 
427683a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
427783a1a1a7SSoheil Hassas Yeganeh {
427883a1a1a7SSoheil Hassas Yeganeh 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
427983a1a1a7SSoheil Hassas Yeganeh 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
428083a1a1a7SSoheil Hassas Yeganeh }
428183a1a1a7SSoheil Hassas Yeganeh 
4282364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4283364a9e93SWillem de Bruijn {
4284364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
428583a1a1a7SSoheil Hassas Yeganeh 	struct sk_buff *skb, *skb_next = NULL;
428683a1a1a7SSoheil Hassas Yeganeh 	bool icmp_next = false;
4287997d5c3fSEric Dumazet 	unsigned long flags;
4288364a9e93SWillem de Bruijn 
4289997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
4290364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
429138b25793SSoheil Hassas Yeganeh 	if (skb && (skb_next = skb_peek(q))) {
429283a1a1a7SSoheil Hassas Yeganeh 		icmp_next = is_icmp_err_skb(skb_next);
429338b25793SSoheil Hassas Yeganeh 		if (icmp_next)
429438b25793SSoheil Hassas Yeganeh 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
429538b25793SSoheil Hassas Yeganeh 	}
4296997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
4297364a9e93SWillem de Bruijn 
429883a1a1a7SSoheil Hassas Yeganeh 	if (is_icmp_err_skb(skb) && !icmp_next)
429983a1a1a7SSoheil Hassas Yeganeh 		sk->sk_err = 0;
430083a1a1a7SSoheil Hassas Yeganeh 
430183a1a1a7SSoheil Hassas Yeganeh 	if (skb_next)
4302364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
4303364a9e93SWillem de Bruijn 
4304364a9e93SWillem de Bruijn 	return skb;
4305364a9e93SWillem de Bruijn }
4306364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
4307364a9e93SWillem de Bruijn 
4308cab41c47SAlexander Duyck /**
4309cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
4310cab41c47SAlexander Duyck  * @skb: the skb to clone
4311cab41c47SAlexander Duyck  *
4312cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
4313cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
4314cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
4315cab41c47SAlexander Duyck  *
4316cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
4317cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
4318cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
4319cab41c47SAlexander Duyck  * the sk_error_queue.
4320cab41c47SAlexander Duyck  */
432162bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
432262bccb8cSAlexander Duyck {
432362bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
432462bccb8cSAlexander Duyck 	struct sk_buff *clone;
432562bccb8cSAlexander Duyck 
432641c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
432762bccb8cSAlexander Duyck 		return NULL;
432862bccb8cSAlexander Duyck 
432962bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
433062bccb8cSAlexander Duyck 	if (!clone) {
433162bccb8cSAlexander Duyck 		sock_put(sk);
433262bccb8cSAlexander Duyck 		return NULL;
433362bccb8cSAlexander Duyck 	}
433462bccb8cSAlexander Duyck 
433562bccb8cSAlexander Duyck 	clone->sk = sk;
433662bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
433762bccb8cSAlexander Duyck 
433862bccb8cSAlexander Duyck 	return clone;
433962bccb8cSAlexander Duyck }
434062bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
434162bccb8cSAlexander Duyck 
434237846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
434337846ef0SAlexander Duyck 					struct sock *sk,
43444ef1b286SSoheil Hassas Yeganeh 					int tstype,
43454ef1b286SSoheil Hassas Yeganeh 					bool opt_stats)
4346ac45f602SPatrick Ohly {
4347ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
4348ac45f602SPatrick Ohly 	int err;
4349ac45f602SPatrick Ohly 
43504ef1b286SSoheil Hassas Yeganeh 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
43514ef1b286SSoheil Hassas Yeganeh 
4352ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
4353ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
4354ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
4355ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4356e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
43574ef1b286SSoheil Hassas Yeganeh 	serr->opt_stats = opt_stats;
43581862d620SWillem de Bruijn 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
43594ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
436009c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
4361ac5cc977SWANG Cong 		if (sk->sk_protocol == IPPROTO_TCP &&
4362ac5cc977SWANG Cong 		    sk->sk_type == SOCK_STREAM)
43634ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
43644ed2d765SWillem de Bruijn 	}
436529030374SEric Dumazet 
4366ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
436729030374SEric Dumazet 
4368ac45f602SPatrick Ohly 	if (err)
4369ac45f602SPatrick Ohly 		kfree_skb(skb);
4370ac45f602SPatrick Ohly }
437137846ef0SAlexander Duyck 
4372b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4373b245be1fSWillem de Bruijn {
4374b245be1fSWillem de Bruijn 	bool ret;
4375b245be1fSWillem de Bruijn 
4376b245be1fSWillem de Bruijn 	if (likely(sysctl_tstamp_allow_data || tsonly))
4377b245be1fSWillem de Bruijn 		return true;
4378b245be1fSWillem de Bruijn 
4379b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
4380b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
4381b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4382b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
4383b245be1fSWillem de Bruijn 	return ret;
4384b245be1fSWillem de Bruijn }
4385b245be1fSWillem de Bruijn 
438637846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
438737846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
438837846ef0SAlexander Duyck {
438937846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
439037846ef0SAlexander Duyck 
4391b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
439235b99dffSWillem de Bruijn 		goto err;
4393b245be1fSWillem de Bruijn 
43949ac25fc0SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
43959ac25fc0SEric Dumazet 	 * but only if the socket refcount is not zero.
43969ac25fc0SEric Dumazet 	 */
439741c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
439837846ef0SAlexander Duyck 		*skb_hwtstamps(skb) = *hwtstamps;
43994ef1b286SSoheil Hassas Yeganeh 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
440037846ef0SAlexander Duyck 		sock_put(sk);
440135b99dffSWillem de Bruijn 		return;
440237846ef0SAlexander Duyck 	}
440335b99dffSWillem de Bruijn 
440435b99dffSWillem de Bruijn err:
440535b99dffSWillem de Bruijn 	kfree_skb(skb);
44069ac25fc0SEric Dumazet }
440737846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
440837846ef0SAlexander Duyck 
440937846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
441037846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
441137846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
441237846ef0SAlexander Duyck {
441337846ef0SAlexander Duyck 	struct sk_buff *skb;
44144ef1b286SSoheil Hassas Yeganeh 	bool tsonly, opt_stats = false;
441537846ef0SAlexander Duyck 
44163a8dd971SWillem de Bruijn 	if (!sk)
44173a8dd971SWillem de Bruijn 		return;
44183a8dd971SWillem de Bruijn 
4419b50a5c70SMiroslav Lichvar 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4420b50a5c70SMiroslav Lichvar 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4421b50a5c70SMiroslav Lichvar 		return;
4422b50a5c70SMiroslav Lichvar 
44233a8dd971SWillem de Bruijn 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
44243a8dd971SWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, tsonly))
442537846ef0SAlexander Duyck 		return;
442637846ef0SAlexander Duyck 
44271c885808SFrancis Yan 	if (tsonly) {
44281c885808SFrancis Yan #ifdef CONFIG_INET
44291c885808SFrancis Yan 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
44301c885808SFrancis Yan 		    sk->sk_protocol == IPPROTO_TCP &&
44314ef1b286SSoheil Hassas Yeganeh 		    sk->sk_type == SOCK_STREAM) {
44321c885808SFrancis Yan 			skb = tcp_get_timestamping_opt_stats(sk);
44334ef1b286SSoheil Hassas Yeganeh 			opt_stats = true;
44344ef1b286SSoheil Hassas Yeganeh 		} else
44351c885808SFrancis Yan #endif
44361c885808SFrancis Yan 			skb = alloc_skb(0, GFP_ATOMIC);
44371c885808SFrancis Yan 	} else {
443837846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
44391c885808SFrancis Yan 	}
444037846ef0SAlexander Duyck 	if (!skb)
444137846ef0SAlexander Duyck 		return;
444237846ef0SAlexander Duyck 
444349ca0d8bSWillem de Bruijn 	if (tsonly) {
4444fff88030SWillem de Bruijn 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4445fff88030SWillem de Bruijn 					     SKBTX_ANY_TSTAMP;
444649ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
444749ca0d8bSWillem de Bruijn 	}
444849ca0d8bSWillem de Bruijn 
444949ca0d8bSWillem de Bruijn 	if (hwtstamps)
445049ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
445149ca0d8bSWillem de Bruijn 	else
445249ca0d8bSWillem de Bruijn 		skb->tstamp = ktime_get_real();
445349ca0d8bSWillem de Bruijn 
44544ef1b286SSoheil Hassas Yeganeh 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
445537846ef0SAlexander Duyck }
4456e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4457e7fd2885SWillem de Bruijn 
4458e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
4459e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
4460e7fd2885SWillem de Bruijn {
4461e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4462e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
4463e7fd2885SWillem de Bruijn }
4464ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4465ac45f602SPatrick Ohly 
44666e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
44676e3e939fSJohannes Berg {
44686e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
44696e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
4470dd4f1072SEric Dumazet 	int err = 1;
44716e3e939fSJohannes Berg 
44726e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
44736e3e939fSJohannes Berg 	skb->wifi_acked = acked;
44746e3e939fSJohannes Berg 
44756e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
44766e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
44776e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
44786e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
44796e3e939fSJohannes Berg 
4480dd4f1072SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
4481dd4f1072SEric Dumazet 	 * but only if the socket refcount is not zero.
4482dd4f1072SEric Dumazet 	 */
448341c6d650SReshetova, Elena 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
44846e3e939fSJohannes Berg 		err = sock_queue_err_skb(sk, skb);
4485dd4f1072SEric Dumazet 		sock_put(sk);
4486dd4f1072SEric Dumazet 	}
44876e3e939fSJohannes Berg 	if (err)
44886e3e939fSJohannes Berg 		kfree_skb(skb);
44896e3e939fSJohannes Berg }
44906e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
44916e3e939fSJohannes Berg 
4492f35d9d8aSRusty Russell /**
4493f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
4494f35d9d8aSRusty Russell  * @skb: the skb to set
4495f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
4496f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
4497f35d9d8aSRusty Russell  *
4498f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
4499f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4500f35d9d8aSRusty Russell  *
4501f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
4502f35d9d8aSRusty Russell  * returns false you should drop the packet.
4503f35d9d8aSRusty Russell  */
4504f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4505f35d9d8aSRusty Russell {
450652b5d6f5SEric Dumazet 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
450752b5d6f5SEric Dumazet 	u32 csum_start = skb_headroom(skb) + (u32)start;
450852b5d6f5SEric Dumazet 
450952b5d6f5SEric Dumazet 	if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
451052b5d6f5SEric Dumazet 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
451152b5d6f5SEric Dumazet 				     start, off, skb_headroom(skb), skb_headlen(skb));
4512f35d9d8aSRusty Russell 		return false;
4513f35d9d8aSRusty Russell 	}
4514f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
451552b5d6f5SEric Dumazet 	skb->csum_start = csum_start;
4516f35d9d8aSRusty Russell 	skb->csum_offset = off;
4517e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
4518f35d9d8aSRusty Russell 	return true;
4519f35d9d8aSRusty Russell }
4520b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4521f35d9d8aSRusty Russell 
4522ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4523ed1f50c3SPaul Durrant 			       unsigned int max)
4524ed1f50c3SPaul Durrant {
4525ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
4526ed1f50c3SPaul Durrant 		return 0;
4527ed1f50c3SPaul Durrant 
4528ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
4529ed1f50c3SPaul Durrant 	 * won't need to do it again.
4530ed1f50c3SPaul Durrant 	 */
4531ed1f50c3SPaul Durrant 	if (max > skb->len)
4532ed1f50c3SPaul Durrant 		max = skb->len;
4533ed1f50c3SPaul Durrant 
4534ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4535ed1f50c3SPaul Durrant 		return -ENOMEM;
4536ed1f50c3SPaul Durrant 
4537ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
4538ed1f50c3SPaul Durrant 		return -EPROTO;
4539ed1f50c3SPaul Durrant 
4540ed1f50c3SPaul Durrant 	return 0;
4541ed1f50c3SPaul Durrant }
4542ed1f50c3SPaul Durrant 
4543f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
4544f9708b43SJan Beulich 
4545f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4546f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
4547f9708b43SJan Beulich 				      unsigned int off)
4548f9708b43SJan Beulich {
4549f9708b43SJan Beulich 	switch (proto) {
4550f9708b43SJan Beulich 		int err;
4551f9708b43SJan Beulich 
4552f9708b43SJan Beulich 	case IPPROTO_TCP:
4553f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4554f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
4555f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4556f9708b43SJan Beulich 						  offsetof(struct tcphdr,
4557f9708b43SJan Beulich 							   check)))
4558f9708b43SJan Beulich 			err = -EPROTO;
4559f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4560f9708b43SJan Beulich 
4561f9708b43SJan Beulich 	case IPPROTO_UDP:
4562f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4563f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
4564f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4565f9708b43SJan Beulich 						  offsetof(struct udphdr,
4566f9708b43SJan Beulich 							   check)))
4567f9708b43SJan Beulich 			err = -EPROTO;
4568f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4569f9708b43SJan Beulich 	}
4570f9708b43SJan Beulich 
4571f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
4572f9708b43SJan Beulich }
4573f9708b43SJan Beulich 
4574ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4575ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
4576ed1f50c3SPaul Durrant  */
4577ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
4578ed1f50c3SPaul Durrant 
4579f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4580ed1f50c3SPaul Durrant {
4581ed1f50c3SPaul Durrant 	unsigned int off;
4582ed1f50c3SPaul Durrant 	bool fragment;
4583f9708b43SJan Beulich 	__sum16 *csum;
4584ed1f50c3SPaul Durrant 	int err;
4585ed1f50c3SPaul Durrant 
4586ed1f50c3SPaul Durrant 	fragment = false;
4587ed1f50c3SPaul Durrant 
4588ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
4589ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
4590ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
4591ed1f50c3SPaul Durrant 	if (err < 0)
4592ed1f50c3SPaul Durrant 		goto out;
4593ed1f50c3SPaul Durrant 
4594ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4595ed1f50c3SPaul Durrant 		fragment = true;
4596ed1f50c3SPaul Durrant 
4597ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
4598ed1f50c3SPaul Durrant 
4599ed1f50c3SPaul Durrant 	err = -EPROTO;
4600ed1f50c3SPaul Durrant 
4601ed1f50c3SPaul Durrant 	if (fragment)
4602ed1f50c3SPaul Durrant 		goto out;
4603ed1f50c3SPaul Durrant 
4604f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4605f9708b43SJan Beulich 	if (IS_ERR(csum))
4606f9708b43SJan Beulich 		return PTR_ERR(csum);
4607ed1f50c3SPaul Durrant 
4608ed1f50c3SPaul Durrant 	if (recalculate)
4609f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4610ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
4611ed1f50c3SPaul Durrant 					   skb->len - off,
4612f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
4613ed1f50c3SPaul Durrant 	err = 0;
4614ed1f50c3SPaul Durrant 
4615ed1f50c3SPaul Durrant out:
4616ed1f50c3SPaul Durrant 	return err;
4617ed1f50c3SPaul Durrant }
4618ed1f50c3SPaul Durrant 
4619ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4620ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
4621ed1f50c3SPaul Durrant  */
4622ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
4623ed1f50c3SPaul Durrant 
4624ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
4625ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
4626ed1f50c3SPaul Durrant 
4627ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4628ed1f50c3SPaul Durrant {
4629ed1f50c3SPaul Durrant 	int err;
4630ed1f50c3SPaul Durrant 	u8 nexthdr;
4631ed1f50c3SPaul Durrant 	unsigned int off;
4632ed1f50c3SPaul Durrant 	unsigned int len;
4633ed1f50c3SPaul Durrant 	bool fragment;
4634ed1f50c3SPaul Durrant 	bool done;
4635f9708b43SJan Beulich 	__sum16 *csum;
4636ed1f50c3SPaul Durrant 
4637ed1f50c3SPaul Durrant 	fragment = false;
4638ed1f50c3SPaul Durrant 	done = false;
4639ed1f50c3SPaul Durrant 
4640ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
4641ed1f50c3SPaul Durrant 
4642ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4643ed1f50c3SPaul Durrant 	if (err < 0)
4644ed1f50c3SPaul Durrant 		goto out;
4645ed1f50c3SPaul Durrant 
4646ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
4647ed1f50c3SPaul Durrant 
4648ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4649ed1f50c3SPaul Durrant 	while (off <= len && !done) {
4650ed1f50c3SPaul Durrant 		switch (nexthdr) {
4651ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
4652ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
4653ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
4654ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
4655ed1f50c3SPaul Durrant 
4656ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4657ed1f50c3SPaul Durrant 						  off +
4658ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
4659ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4660ed1f50c3SPaul Durrant 			if (err < 0)
4661ed1f50c3SPaul Durrant 				goto out;
4662ed1f50c3SPaul Durrant 
4663ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4664ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4665ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
4666ed1f50c3SPaul Durrant 			break;
4667ed1f50c3SPaul Durrant 		}
4668ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
4669ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
4670ed1f50c3SPaul Durrant 
4671ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4672ed1f50c3SPaul Durrant 						  off +
4673ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
4674ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4675ed1f50c3SPaul Durrant 			if (err < 0)
4676ed1f50c3SPaul Durrant 				goto out;
4677ed1f50c3SPaul Durrant 
4678ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4679ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4680ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
4681ed1f50c3SPaul Durrant 			break;
4682ed1f50c3SPaul Durrant 		}
4683ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
4684ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
4685ed1f50c3SPaul Durrant 
4686ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4687ed1f50c3SPaul Durrant 						  off +
4688ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
4689ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4690ed1f50c3SPaul Durrant 			if (err < 0)
4691ed1f50c3SPaul Durrant 				goto out;
4692ed1f50c3SPaul Durrant 
4693ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
4694ed1f50c3SPaul Durrant 
4695ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4696ed1f50c3SPaul Durrant 				fragment = true;
4697ed1f50c3SPaul Durrant 
4698ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4699ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
4700ed1f50c3SPaul Durrant 			break;
4701ed1f50c3SPaul Durrant 		}
4702ed1f50c3SPaul Durrant 		default:
4703ed1f50c3SPaul Durrant 			done = true;
4704ed1f50c3SPaul Durrant 			break;
4705ed1f50c3SPaul Durrant 		}
4706ed1f50c3SPaul Durrant 	}
4707ed1f50c3SPaul Durrant 
4708ed1f50c3SPaul Durrant 	err = -EPROTO;
4709ed1f50c3SPaul Durrant 
4710ed1f50c3SPaul Durrant 	if (!done || fragment)
4711ed1f50c3SPaul Durrant 		goto out;
4712ed1f50c3SPaul Durrant 
4713f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
4714f9708b43SJan Beulich 	if (IS_ERR(csum))
4715f9708b43SJan Beulich 		return PTR_ERR(csum);
4716ed1f50c3SPaul Durrant 
4717ed1f50c3SPaul Durrant 	if (recalculate)
4718f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4719ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
4720f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
4721ed1f50c3SPaul Durrant 	err = 0;
4722ed1f50c3SPaul Durrant 
4723ed1f50c3SPaul Durrant out:
4724ed1f50c3SPaul Durrant 	return err;
4725ed1f50c3SPaul Durrant }
4726ed1f50c3SPaul Durrant 
4727ed1f50c3SPaul Durrant /**
4728ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
4729ed1f50c3SPaul Durrant  * @skb: the skb to set up
4730ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
4731ed1f50c3SPaul Durrant  */
4732ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4733ed1f50c3SPaul Durrant {
4734ed1f50c3SPaul Durrant 	int err;
4735ed1f50c3SPaul Durrant 
4736ed1f50c3SPaul Durrant 	switch (skb->protocol) {
4737ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
4738f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
4739ed1f50c3SPaul Durrant 		break;
4740ed1f50c3SPaul Durrant 
4741ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
4742ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
4743ed1f50c3SPaul Durrant 		break;
4744ed1f50c3SPaul Durrant 
4745ed1f50c3SPaul Durrant 	default:
4746ed1f50c3SPaul Durrant 		err = -EPROTO;
4747ed1f50c3SPaul Durrant 		break;
4748ed1f50c3SPaul Durrant 	}
4749ed1f50c3SPaul Durrant 
4750ed1f50c3SPaul Durrant 	return err;
4751ed1f50c3SPaul Durrant }
4752ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
4753ed1f50c3SPaul Durrant 
47549afd85c9SLinus Lüssing /**
47559afd85c9SLinus Lüssing  * skb_checksum_maybe_trim - maybe trims the given skb
47569afd85c9SLinus Lüssing  * @skb: the skb to check
47579afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
47589afd85c9SLinus Lüssing  *
47599afd85c9SLinus Lüssing  * Checks whether the given skb has data beyond the given transport length.
47609afd85c9SLinus Lüssing  * If so, returns a cloned skb trimmed to this transport length.
47619afd85c9SLinus Lüssing  * Otherwise returns the provided skb. Returns NULL in error cases
47629afd85c9SLinus Lüssing  * (e.g. transport_len exceeds skb length or out-of-memory).
47639afd85c9SLinus Lüssing  *
4764a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
4765a516993fSLinus Lüssing  * differs from the provided skb.
47669afd85c9SLinus Lüssing  */
47679afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
47689afd85c9SLinus Lüssing 					       unsigned int transport_len)
47699afd85c9SLinus Lüssing {
47709afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
47719afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + transport_len;
47729afd85c9SLinus Lüssing 	int ret;
47739afd85c9SLinus Lüssing 
4774a516993fSLinus Lüssing 	if (skb->len < len)
47759afd85c9SLinus Lüssing 		return NULL;
4776a516993fSLinus Lüssing 	else if (skb->len == len)
47779afd85c9SLinus Lüssing 		return skb;
47789afd85c9SLinus Lüssing 
47799afd85c9SLinus Lüssing 	skb_chk = skb_clone(skb, GFP_ATOMIC);
47809afd85c9SLinus Lüssing 	if (!skb_chk)
47819afd85c9SLinus Lüssing 		return NULL;
47829afd85c9SLinus Lüssing 
47839afd85c9SLinus Lüssing 	ret = pskb_trim_rcsum(skb_chk, len);
47849afd85c9SLinus Lüssing 	if (ret) {
47859afd85c9SLinus Lüssing 		kfree_skb(skb_chk);
47869afd85c9SLinus Lüssing 		return NULL;
47879afd85c9SLinus Lüssing 	}
47889afd85c9SLinus Lüssing 
47899afd85c9SLinus Lüssing 	return skb_chk;
47909afd85c9SLinus Lüssing }
47919afd85c9SLinus Lüssing 
47929afd85c9SLinus Lüssing /**
47939afd85c9SLinus Lüssing  * skb_checksum_trimmed - validate checksum of an skb
47949afd85c9SLinus Lüssing  * @skb: the skb to check
47959afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
47969afd85c9SLinus Lüssing  * @skb_chkf: checksum function to use
47979afd85c9SLinus Lüssing  *
47989afd85c9SLinus Lüssing  * Applies the given checksum function skb_chkf to the provided skb.
47999afd85c9SLinus Lüssing  * Returns a checked and maybe trimmed skb. Returns NULL on error.
48009afd85c9SLinus Lüssing  *
48019afd85c9SLinus Lüssing  * If the skb has data beyond the given transport length, then a
48029afd85c9SLinus Lüssing  * trimmed & cloned skb is checked and returned.
48039afd85c9SLinus Lüssing  *
4804a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
4805a516993fSLinus Lüssing  * differs from the provided skb.
48069afd85c9SLinus Lüssing  */
48079afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
48089afd85c9SLinus Lüssing 				     unsigned int transport_len,
48099afd85c9SLinus Lüssing 				     __sum16(*skb_chkf)(struct sk_buff *skb))
48109afd85c9SLinus Lüssing {
48119afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
48129afd85c9SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
4813fcba67c9SLinus Lüssing 	__sum16 ret;
48149afd85c9SLinus Lüssing 
48159afd85c9SLinus Lüssing 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
48169afd85c9SLinus Lüssing 	if (!skb_chk)
4817a516993fSLinus Lüssing 		goto err;
48189afd85c9SLinus Lüssing 
4819a516993fSLinus Lüssing 	if (!pskb_may_pull(skb_chk, offset))
4820a516993fSLinus Lüssing 		goto err;
48219afd85c9SLinus Lüssing 
48229b368814SLinus Lüssing 	skb_pull_rcsum(skb_chk, offset);
48239afd85c9SLinus Lüssing 	ret = skb_chkf(skb_chk);
48249b368814SLinus Lüssing 	skb_push_rcsum(skb_chk, offset);
48259afd85c9SLinus Lüssing 
4826a516993fSLinus Lüssing 	if (ret)
4827a516993fSLinus Lüssing 		goto err;
48289afd85c9SLinus Lüssing 
48299afd85c9SLinus Lüssing 	return skb_chk;
4830a516993fSLinus Lüssing 
4831a516993fSLinus Lüssing err:
4832a516993fSLinus Lüssing 	if (skb_chk && skb_chk != skb)
4833a516993fSLinus Lüssing 		kfree_skb(skb_chk);
4834a516993fSLinus Lüssing 
4835a516993fSLinus Lüssing 	return NULL;
4836a516993fSLinus Lüssing 
48379afd85c9SLinus Lüssing }
48389afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
48399afd85c9SLinus Lüssing 
48404497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
48414497b076SBen Hutchings {
4842e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4843e87cc472SJoe Perches 			     skb->dev->name);
48444497b076SBen Hutchings }
48454497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4846bad43ca8SEric Dumazet 
4847bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4848bad43ca8SEric Dumazet {
48493d861f66SEric Dumazet 	if (head_stolen) {
48503d861f66SEric Dumazet 		skb_release_head_state(skb);
4851bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
48523d861f66SEric Dumazet 	} else {
4853bad43ca8SEric Dumazet 		__kfree_skb(skb);
4854bad43ca8SEric Dumazet 	}
48553d861f66SEric Dumazet }
4856bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
4857bad43ca8SEric Dumazet 
4858bad43ca8SEric Dumazet /**
4859bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
4860bad43ca8SEric Dumazet  * @to: prior buffer
4861bad43ca8SEric Dumazet  * @from: buffer to add
4862bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
4863c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
4864bad43ca8SEric Dumazet  */
4865bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4866bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
4867bad43ca8SEric Dumazet {
4868c818fa9eSEric Dumazet 	struct skb_shared_info *to_shinfo, *from_shinfo;
4869bad43ca8SEric Dumazet 	int i, delta, len = from->len;
4870bad43ca8SEric Dumazet 
4871bad43ca8SEric Dumazet 	*fragstolen = false;
4872bad43ca8SEric Dumazet 
4873bad43ca8SEric Dumazet 	if (skb_cloned(to))
4874bad43ca8SEric Dumazet 		return false;
4875bad43ca8SEric Dumazet 
4876bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
4877e93a0435SEric Dumazet 		if (len)
4878bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4879bad43ca8SEric Dumazet 		*delta_truesize = 0;
4880bad43ca8SEric Dumazet 		return true;
4881bad43ca8SEric Dumazet 	}
4882bad43ca8SEric Dumazet 
4883c818fa9eSEric Dumazet 	to_shinfo = skb_shinfo(to);
4884c818fa9eSEric Dumazet 	from_shinfo = skb_shinfo(from);
4885c818fa9eSEric Dumazet 	if (to_shinfo->frag_list || from_shinfo->frag_list)
4886bad43ca8SEric Dumazet 		return false;
48871f8b977aSWillem de Bruijn 	if (skb_zcopy(to) || skb_zcopy(from))
48881f8b977aSWillem de Bruijn 		return false;
4889bad43ca8SEric Dumazet 
4890bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
4891bad43ca8SEric Dumazet 		struct page *page;
4892bad43ca8SEric Dumazet 		unsigned int offset;
4893bad43ca8SEric Dumazet 
4894c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
4895c818fa9eSEric Dumazet 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
4896bad43ca8SEric Dumazet 			return false;
4897bad43ca8SEric Dumazet 
4898bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
4899bad43ca8SEric Dumazet 			return false;
4900bad43ca8SEric Dumazet 
4901bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4902bad43ca8SEric Dumazet 
4903bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
4904bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
4905bad43ca8SEric Dumazet 
4906c818fa9eSEric Dumazet 		skb_fill_page_desc(to, to_shinfo->nr_frags,
4907bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
4908bad43ca8SEric Dumazet 		*fragstolen = true;
4909bad43ca8SEric Dumazet 	} else {
4910c818fa9eSEric Dumazet 		if (to_shinfo->nr_frags +
4911c818fa9eSEric Dumazet 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
4912bad43ca8SEric Dumazet 			return false;
4913bad43ca8SEric Dumazet 
4914f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4915bad43ca8SEric Dumazet 	}
4916bad43ca8SEric Dumazet 
4917bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
4918bad43ca8SEric Dumazet 
4919c818fa9eSEric Dumazet 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4920c818fa9eSEric Dumazet 	       from_shinfo->frags,
4921c818fa9eSEric Dumazet 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
4922c818fa9eSEric Dumazet 	to_shinfo->nr_frags += from_shinfo->nr_frags;
4923bad43ca8SEric Dumazet 
4924bad43ca8SEric Dumazet 	if (!skb_cloned(from))
4925c818fa9eSEric Dumazet 		from_shinfo->nr_frags = 0;
4926bad43ca8SEric Dumazet 
49278ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
49288ea853fdSLi RongQing 	 * since we set nr_frags to 0.
49298ea853fdSLi RongQing 	 */
4930c818fa9eSEric Dumazet 	for (i = 0; i < from_shinfo->nr_frags; i++)
4931c818fa9eSEric Dumazet 		__skb_frag_ref(&from_shinfo->frags[i]);
4932bad43ca8SEric Dumazet 
4933bad43ca8SEric Dumazet 	to->truesize += delta;
4934bad43ca8SEric Dumazet 	to->len += len;
4935bad43ca8SEric Dumazet 	to->data_len += len;
4936bad43ca8SEric Dumazet 
4937bad43ca8SEric Dumazet 	*delta_truesize = delta;
4938bad43ca8SEric Dumazet 	return true;
4939bad43ca8SEric Dumazet }
4940bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
4941621e84d6SNicolas Dichtel 
4942621e84d6SNicolas Dichtel /**
49438b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
4944621e84d6SNicolas Dichtel  *
4945621e84d6SNicolas Dichtel  * @skb: buffer to clean
49468b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
4947621e84d6SNicolas Dichtel  *
49488b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
49498b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
49508b27f277SNicolas Dichtel  * operations.
49518b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
49528b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
49538b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
4954621e84d6SNicolas Dichtel  */
49558b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4956621e84d6SNicolas Dichtel {
4957621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
4958621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
495960ff7467SWANG Cong 	skb->ignore_df = 0;
4960621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
4961621e84d6SNicolas Dichtel 	secpath_reset(skb);
4962621e84d6SNicolas Dichtel 	nf_reset(skb);
4963621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
4964213dd74aSHerbert Xu 
49656f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV
49666f9a5069SPetr Machata 	skb->offload_fwd_mark = 0;
4967875e8939SIdo Schimmel 	skb->offload_l3_fwd_mark = 0;
49686f9a5069SPetr Machata #endif
49696f9a5069SPetr Machata 
4970213dd74aSHerbert Xu 	if (!xnet)
4971213dd74aSHerbert Xu 		return;
4972213dd74aSHerbert Xu 
49732b5ec1a5SYe Yin 	ipvs_reset(skb);
4974213dd74aSHerbert Xu 	skb->mark = 0;
4975c47d8c2fSJesus Sanchez-Palencia 	skb->tstamp = 0;
4976621e84d6SNicolas Dichtel }
4977621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
4978de960aa9SFlorian Westphal 
4979de960aa9SFlorian Westphal /**
4980de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4981de960aa9SFlorian Westphal  *
4982de960aa9SFlorian Westphal  * @skb: GSO skb
4983de960aa9SFlorian Westphal  *
4984de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
4985de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
4986de960aa9SFlorian Westphal  *
4987de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4988de960aa9SFlorian Westphal  */
4989a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4990de960aa9SFlorian Westphal {
4991de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4992f993bc25SFlorian Westphal 	unsigned int thlen = 0;
4993f993bc25SFlorian Westphal 
4994f993bc25SFlorian Westphal 	if (skb->encapsulation) {
4995f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
4996f993bc25SFlorian Westphal 			skb_transport_header(skb);
4997de960aa9SFlorian Westphal 
4998de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4999f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
5000f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5001f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
50021dd27cdeSDaniel Axtens 	} else if (unlikely(skb_is_gso_sctp(skb))) {
500390017accSMarcelo Ricardo Leitner 		thlen = sizeof(struct sctphdr);
5004ee80d1ebSWillem de Bruijn 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5005ee80d1ebSWillem de Bruijn 		thlen = sizeof(struct udphdr);
5006f993bc25SFlorian Westphal 	}
50076d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
50086d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
50096d39d589SFlorian Westphal 	 * accounted for.
50106d39d589SFlorian Westphal 	 */
5011f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
5012de960aa9SFlorian Westphal }
5013a4a77718SDaniel Axtens 
5014a4a77718SDaniel Axtens /**
5015a4a77718SDaniel Axtens  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5016a4a77718SDaniel Axtens  *
5017a4a77718SDaniel Axtens  * @skb: GSO skb
5018a4a77718SDaniel Axtens  *
5019a4a77718SDaniel Axtens  * skb_gso_network_seglen is used to determine the real size of the
5020a4a77718SDaniel Axtens  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5021a4a77718SDaniel Axtens  *
5022a4a77718SDaniel Axtens  * The MAC/L2 header is not accounted for.
5023a4a77718SDaniel Axtens  */
5024a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5025a4a77718SDaniel Axtens {
5026a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) -
5027a4a77718SDaniel Axtens 			       skb_network_header(skb);
5028a4a77718SDaniel Axtens 
5029a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5030a4a77718SDaniel Axtens }
5031a4a77718SDaniel Axtens 
5032a4a77718SDaniel Axtens /**
5033a4a77718SDaniel Axtens  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5034a4a77718SDaniel Axtens  *
5035a4a77718SDaniel Axtens  * @skb: GSO skb
5036a4a77718SDaniel Axtens  *
5037a4a77718SDaniel Axtens  * skb_gso_mac_seglen is used to determine the real size of the
5038a4a77718SDaniel Axtens  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5039a4a77718SDaniel Axtens  * headers (TCP/UDP).
5040a4a77718SDaniel Axtens  */
5041a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5042a4a77718SDaniel Axtens {
5043a4a77718SDaniel Axtens 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5044a4a77718SDaniel Axtens 
5045a4a77718SDaniel Axtens 	return hdr_len + skb_gso_transport_seglen(skb);
5046a4a77718SDaniel Axtens }
50470d5501c1SVlad Yasevich 
5048ae7ef81eSMarcelo Ricardo Leitner /**
50492b16f048SDaniel Axtens  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
50502b16f048SDaniel Axtens  *
50512b16f048SDaniel Axtens  * There are a couple of instances where we have a GSO skb, and we
50522b16f048SDaniel Axtens  * want to determine what size it would be after it is segmented.
50532b16f048SDaniel Axtens  *
50542b16f048SDaniel Axtens  * We might want to check:
50552b16f048SDaniel Axtens  * -    L3+L4+payload size (e.g. IP forwarding)
50562b16f048SDaniel Axtens  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
50572b16f048SDaniel Axtens  *
50582b16f048SDaniel Axtens  * This is a helper to do that correctly considering GSO_BY_FRAGS.
50592b16f048SDaniel Axtens  *
506049682bfaSMathieu Malaterre  * @skb: GSO skb
506149682bfaSMathieu Malaterre  *
50622b16f048SDaniel Axtens  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
50632b16f048SDaniel Axtens  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
50642b16f048SDaniel Axtens  *
50652b16f048SDaniel Axtens  * @max_len: The maximum permissible length.
50662b16f048SDaniel Axtens  *
50672b16f048SDaniel Axtens  * Returns true if the segmented length <= max length.
50682b16f048SDaniel Axtens  */
50692b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb,
50702b16f048SDaniel Axtens 				      unsigned int seg_len,
50712b16f048SDaniel Axtens 				      unsigned int max_len) {
50722b16f048SDaniel Axtens 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
50732b16f048SDaniel Axtens 	const struct sk_buff *iter;
50742b16f048SDaniel Axtens 
50752b16f048SDaniel Axtens 	if (shinfo->gso_size != GSO_BY_FRAGS)
50762b16f048SDaniel Axtens 		return seg_len <= max_len;
50772b16f048SDaniel Axtens 
50782b16f048SDaniel Axtens 	/* Undo this so we can re-use header sizes */
50792b16f048SDaniel Axtens 	seg_len -= GSO_BY_FRAGS;
50802b16f048SDaniel Axtens 
50812b16f048SDaniel Axtens 	skb_walk_frags(skb, iter) {
50822b16f048SDaniel Axtens 		if (seg_len + skb_headlen(iter) > max_len)
50832b16f048SDaniel Axtens 			return false;
50842b16f048SDaniel Axtens 	}
50852b16f048SDaniel Axtens 
50862b16f048SDaniel Axtens 	return true;
50872b16f048SDaniel Axtens }
50882b16f048SDaniel Axtens 
50892b16f048SDaniel Axtens /**
5090779b7931SDaniel Axtens  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5091ae7ef81eSMarcelo Ricardo Leitner  *
5092ae7ef81eSMarcelo Ricardo Leitner  * @skb: GSO skb
509376f21b99SDavid S. Miller  * @mtu: MTU to validate against
5094ae7ef81eSMarcelo Ricardo Leitner  *
5095779b7931SDaniel Axtens  * skb_gso_validate_network_len validates if a given skb will fit a
5096779b7931SDaniel Axtens  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5097779b7931SDaniel Axtens  * payload.
5098ae7ef81eSMarcelo Ricardo Leitner  */
5099779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5100ae7ef81eSMarcelo Ricardo Leitner {
51012b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5102ae7ef81eSMarcelo Ricardo Leitner }
5103779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5104ae7ef81eSMarcelo Ricardo Leitner 
51052b16f048SDaniel Axtens /**
51062b16f048SDaniel Axtens  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
51072b16f048SDaniel Axtens  *
51082b16f048SDaniel Axtens  * @skb: GSO skb
51092b16f048SDaniel Axtens  * @len: length to validate against
51102b16f048SDaniel Axtens  *
51112b16f048SDaniel Axtens  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
51122b16f048SDaniel Axtens  * length once split, including L2, L3 and L4 headers and the payload.
51132b16f048SDaniel Axtens  */
51142b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
51152b16f048SDaniel Axtens {
51162b16f048SDaniel Axtens 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
51172b16f048SDaniel Axtens }
51182b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
51192b16f048SDaniel Axtens 
51200d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
51210d5501c1SVlad Yasevich {
5122d85e8be2SYuya Kusakabe 	int mac_len, meta_len;
5123d85e8be2SYuya Kusakabe 	void *meta;
51244bbb3e0eSToshiaki Makita 
51250d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
51260d5501c1SVlad Yasevich 		kfree_skb(skb);
51270d5501c1SVlad Yasevich 		return NULL;
51280d5501c1SVlad Yasevich 	}
51290d5501c1SVlad Yasevich 
51304bbb3e0eSToshiaki Makita 	mac_len = skb->data - skb_mac_header(skb);
5131ae474573SToshiaki Makita 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
51324bbb3e0eSToshiaki Makita 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
51334bbb3e0eSToshiaki Makita 			mac_len - VLAN_HLEN - ETH_TLEN);
5134ae474573SToshiaki Makita 	}
5135d85e8be2SYuya Kusakabe 
5136d85e8be2SYuya Kusakabe 	meta_len = skb_metadata_len(skb);
5137d85e8be2SYuya Kusakabe 	if (meta_len) {
5138d85e8be2SYuya Kusakabe 		meta = skb_metadata_end(skb) - meta_len;
5139d85e8be2SYuya Kusakabe 		memmove(meta + VLAN_HLEN, meta, meta_len);
5140d85e8be2SYuya Kusakabe 	}
5141d85e8be2SYuya Kusakabe 
51420d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
51430d5501c1SVlad Yasevich 	return skb;
51440d5501c1SVlad Yasevich }
51450d5501c1SVlad Yasevich 
51460d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
51470d5501c1SVlad Yasevich {
51480d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
51490d5501c1SVlad Yasevich 	u16 vlan_tci;
51500d5501c1SVlad Yasevich 
5151df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
51520d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
51530d5501c1SVlad Yasevich 		return skb;
51540d5501c1SVlad Yasevich 	}
51550d5501c1SVlad Yasevich 
51560d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
51570d5501c1SVlad Yasevich 	if (unlikely(!skb))
51580d5501c1SVlad Yasevich 		goto err_free;
51590d5501c1SVlad Yasevich 
51600d5501c1SVlad Yasevich 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
51610d5501c1SVlad Yasevich 		goto err_free;
51620d5501c1SVlad Yasevich 
51630d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
51640d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
51650d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
51660d5501c1SVlad Yasevich 
51670d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
51680d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
51690d5501c1SVlad Yasevich 
51700d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
51710d5501c1SVlad Yasevich 	if (unlikely(!skb))
51720d5501c1SVlad Yasevich 		goto err_free;
51730d5501c1SVlad Yasevich 
51740d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
51750d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
51760d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
51770d5501c1SVlad Yasevich 
51780d5501c1SVlad Yasevich 	return skb;
51790d5501c1SVlad Yasevich 
51800d5501c1SVlad Yasevich err_free:
51810d5501c1SVlad Yasevich 	kfree_skb(skb);
51820d5501c1SVlad Yasevich 	return NULL;
51830d5501c1SVlad Yasevich }
51840d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
51852e4e4410SEric Dumazet 
5186e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
5187e2195121SJiri Pirko {
5188e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
5189e2195121SJiri Pirko 		return -ENOMEM;
5190e2195121SJiri Pirko 
5191e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5192e2195121SJiri Pirko 		return 0;
5193e2195121SJiri Pirko 
5194e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5195e2195121SJiri Pirko }
5196e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
5197e2195121SJiri Pirko 
5198bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
5199bfca4c52SShmulik Ladkani  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5200bfca4c52SShmulik Ladkani  */
5201bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
520293515d53SJiri Pirko {
520393515d53SJiri Pirko 	struct vlan_hdr *vhdr;
5204b6a79208SShmulik Ladkani 	int offset = skb->data - skb_mac_header(skb);
520593515d53SJiri Pirko 	int err;
520693515d53SJiri Pirko 
5207b6a79208SShmulik Ladkani 	if (WARN_ONCE(offset,
5208b6a79208SShmulik Ladkani 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5209b6a79208SShmulik Ladkani 		      offset)) {
5210b6a79208SShmulik Ladkani 		return -EINVAL;
5211b6a79208SShmulik Ladkani 	}
5212b6a79208SShmulik Ladkani 
521393515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
521493515d53SJiri Pirko 	if (unlikely(err))
5215b6a79208SShmulik Ladkani 		return err;
521693515d53SJiri Pirko 
521793515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
521893515d53SJiri Pirko 
521993515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
522093515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
522193515d53SJiri Pirko 
522293515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
522393515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
522493515d53SJiri Pirko 
522593515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
522693515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
522793515d53SJiri Pirko 
522893515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
522993515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
523093515d53SJiri Pirko 
523193515d53SJiri Pirko 	skb_reset_mac_len(skb);
523293515d53SJiri Pirko 
523393515d53SJiri Pirko 	return err;
523493515d53SJiri Pirko }
5235bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
523693515d53SJiri Pirko 
5237b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
5238b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5239b6a79208SShmulik Ladkani  */
524093515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
524193515d53SJiri Pirko {
524293515d53SJiri Pirko 	u16 vlan_tci;
524393515d53SJiri Pirko 	__be16 vlan_proto;
524493515d53SJiri Pirko 	int err;
524593515d53SJiri Pirko 
5246df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
5247b1817524SMichał Mirosław 		__vlan_hwaccel_clear_tag(skb);
524893515d53SJiri Pirko 	} else {
5249ecf4ee41SShmulik Ladkani 		if (unlikely(!eth_type_vlan(skb->protocol)))
525093515d53SJiri Pirko 			return 0;
525193515d53SJiri Pirko 
525293515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
525393515d53SJiri Pirko 		if (err)
525493515d53SJiri Pirko 			return err;
525593515d53SJiri Pirko 	}
525693515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
5257ecf4ee41SShmulik Ladkani 	if (likely(!eth_type_vlan(skb->protocol)))
525893515d53SJiri Pirko 		return 0;
525993515d53SJiri Pirko 
526093515d53SJiri Pirko 	vlan_proto = skb->protocol;
526193515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
526293515d53SJiri Pirko 	if (unlikely(err))
526393515d53SJiri Pirko 		return err;
526493515d53SJiri Pirko 
526593515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
526693515d53SJiri Pirko 	return 0;
526793515d53SJiri Pirko }
526893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
526993515d53SJiri Pirko 
5270b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5271b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
5272b6a79208SShmulik Ladkani  */
527393515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
527493515d53SJiri Pirko {
5275df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
5276b6a79208SShmulik Ladkani 		int offset = skb->data - skb_mac_header(skb);
527793515d53SJiri Pirko 		int err;
527893515d53SJiri Pirko 
5279b6a79208SShmulik Ladkani 		if (WARN_ONCE(offset,
5280b6a79208SShmulik Ladkani 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5281b6a79208SShmulik Ladkani 			      offset)) {
5282b6a79208SShmulik Ladkani 			return -EINVAL;
5283b6a79208SShmulik Ladkani 		}
5284b6a79208SShmulik Ladkani 
528593515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
5286df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
5287b6a79208SShmulik Ladkani 		if (err)
528893515d53SJiri Pirko 			return err;
52899241e2dfSDaniel Borkmann 
529093515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
529193515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
529293515d53SJiri Pirko 
52936b83d28aSDaniel Borkmann 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
529493515d53SJiri Pirko 	}
529593515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
529693515d53SJiri Pirko 	return 0;
529793515d53SJiri Pirko }
529893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
529993515d53SJiri Pirko 
53002e4e4410SEric Dumazet /**
53012e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
53022e4e4410SEric Dumazet  *
5303de3f0d0eSMasanari Iida  * @header_len: size of linear part
5304de3f0d0eSMasanari Iida  * @data_len: needed length in frags
5305de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
5306de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
5307de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
53082e4e4410SEric Dumazet  *
53092e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
53102e4e4410SEric Dumazet  */
53112e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
53122e4e4410SEric Dumazet 				     unsigned long data_len,
53132e4e4410SEric Dumazet 				     int max_page_order,
53142e4e4410SEric Dumazet 				     int *errcode,
53152e4e4410SEric Dumazet 				     gfp_t gfp_mask)
53162e4e4410SEric Dumazet {
53172e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
53182e4e4410SEric Dumazet 	unsigned long chunk;
53192e4e4410SEric Dumazet 	struct sk_buff *skb;
53202e4e4410SEric Dumazet 	struct page *page;
53212e4e4410SEric Dumazet 	int i;
53222e4e4410SEric Dumazet 
53232e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
53242e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
53252e4e4410SEric Dumazet 	 * high order pages...
53262e4e4410SEric Dumazet 	 */
53272e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
53282e4e4410SEric Dumazet 		return NULL;
53292e4e4410SEric Dumazet 
53302e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
5331f8c468e8SDavid Rientjes 	skb = alloc_skb(header_len, gfp_mask);
53322e4e4410SEric Dumazet 	if (!skb)
53332e4e4410SEric Dumazet 		return NULL;
53342e4e4410SEric Dumazet 
53352e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
53362e4e4410SEric Dumazet 
53372e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
53382e4e4410SEric Dumazet 		int order = max_page_order;
53392e4e4410SEric Dumazet 
53402e4e4410SEric Dumazet 		while (order) {
53412e4e4410SEric Dumazet 			if (npages >= 1 << order) {
5342d0164adcSMel Gorman 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
53432e4e4410SEric Dumazet 						   __GFP_COMP |
5344d14b56f5SMichal Hocko 						   __GFP_NOWARN,
53452e4e4410SEric Dumazet 						   order);
53462e4e4410SEric Dumazet 				if (page)
53472e4e4410SEric Dumazet 					goto fill_page;
53482e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
53492e4e4410SEric Dumazet 				order = 1;
53502e4e4410SEric Dumazet 				max_page_order = 0;
53512e4e4410SEric Dumazet 			}
53522e4e4410SEric Dumazet 			order--;
53532e4e4410SEric Dumazet 		}
53542e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
53552e4e4410SEric Dumazet 		if (!page)
53562e4e4410SEric Dumazet 			goto failure;
53572e4e4410SEric Dumazet fill_page:
53582e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
53592e4e4410SEric Dumazet 			      PAGE_SIZE << order);
53602e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
53612e4e4410SEric Dumazet 		data_len -= chunk;
53622e4e4410SEric Dumazet 		npages -= 1 << order;
53632e4e4410SEric Dumazet 	}
53642e4e4410SEric Dumazet 	return skb;
53652e4e4410SEric Dumazet 
53662e4e4410SEric Dumazet failure:
53672e4e4410SEric Dumazet 	kfree_skb(skb);
53682e4e4410SEric Dumazet 	return NULL;
53692e4e4410SEric Dumazet }
53702e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
53716fa01ccdSSowmini Varadhan 
53726fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
53736fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
53746fa01ccdSSowmini Varadhan 				    const int headlen, gfp_t gfp_mask)
53756fa01ccdSSowmini Varadhan {
53766fa01ccdSSowmini Varadhan 	int i;
53776fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
53786fa01ccdSSowmini Varadhan 	int new_hlen = headlen - off;
53796fa01ccdSSowmini Varadhan 	u8 *data;
53806fa01ccdSSowmini Varadhan 
53816fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
53826fa01ccdSSowmini Varadhan 
53836fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
53846fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
53856fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
53866fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
53876fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
53886fa01ccdSSowmini Varadhan 	if (!data)
53896fa01ccdSSowmini Varadhan 		return -ENOMEM;
53906fa01ccdSSowmini Varadhan 
53916fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
53926fa01ccdSSowmini Varadhan 
53936fa01ccdSSowmini Varadhan 	/* Copy real data, and all frags */
53946fa01ccdSSowmini Varadhan 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
53956fa01ccdSSowmini Varadhan 	skb->len -= off;
53966fa01ccdSSowmini Varadhan 
53976fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
53986fa01ccdSSowmini Varadhan 	       skb_shinfo(skb),
53996fa01ccdSSowmini Varadhan 	       offsetof(struct skb_shared_info,
54006fa01ccdSSowmini Varadhan 			frags[skb_shinfo(skb)->nr_frags]));
54016fa01ccdSSowmini Varadhan 	if (skb_cloned(skb)) {
54026fa01ccdSSowmini Varadhan 		/* drop the old head gracefully */
54036fa01ccdSSowmini Varadhan 		if (skb_orphan_frags(skb, gfp_mask)) {
54046fa01ccdSSowmini Varadhan 			kfree(data);
54056fa01ccdSSowmini Varadhan 			return -ENOMEM;
54066fa01ccdSSowmini Varadhan 		}
54076fa01ccdSSowmini Varadhan 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
54086fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
54096fa01ccdSSowmini Varadhan 		if (skb_has_frag_list(skb))
54106fa01ccdSSowmini Varadhan 			skb_clone_fraglist(skb);
54116fa01ccdSSowmini Varadhan 		skb_release_data(skb);
54126fa01ccdSSowmini Varadhan 	} else {
54136fa01ccdSSowmini Varadhan 		/* we can reuse existing recount- all we did was
54146fa01ccdSSowmini Varadhan 		 * relocate values
54156fa01ccdSSowmini Varadhan 		 */
54166fa01ccdSSowmini Varadhan 		skb_free_head(skb);
54176fa01ccdSSowmini Varadhan 	}
54186fa01ccdSSowmini Varadhan 
54196fa01ccdSSowmini Varadhan 	skb->head = data;
54206fa01ccdSSowmini Varadhan 	skb->data = data;
54216fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
54226fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
54236fa01ccdSSowmini Varadhan 	skb->end = size;
54246fa01ccdSSowmini Varadhan #else
54256fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
54266fa01ccdSSowmini Varadhan #endif
54276fa01ccdSSowmini Varadhan 	skb_set_tail_pointer(skb, skb_headlen(skb));
54286fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
54296fa01ccdSSowmini Varadhan 	skb->cloned = 0;
54306fa01ccdSSowmini Varadhan 	skb->hdr_len = 0;
54316fa01ccdSSowmini Varadhan 	skb->nohdr = 0;
54326fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
54336fa01ccdSSowmini Varadhan 
54346fa01ccdSSowmini Varadhan 	return 0;
54356fa01ccdSSowmini Varadhan }
54366fa01ccdSSowmini Varadhan 
54376fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
54386fa01ccdSSowmini Varadhan 
54396fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
54406fa01ccdSSowmini Varadhan  * pskb_carve()
54416fa01ccdSSowmini Varadhan  */
54426fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
54436fa01ccdSSowmini Varadhan 				struct skb_shared_info *shinfo, int eat,
54446fa01ccdSSowmini Varadhan 				gfp_t gfp_mask)
54456fa01ccdSSowmini Varadhan {
54466fa01ccdSSowmini Varadhan 	struct sk_buff *list = shinfo->frag_list;
54476fa01ccdSSowmini Varadhan 	struct sk_buff *clone = NULL;
54486fa01ccdSSowmini Varadhan 	struct sk_buff *insp = NULL;
54496fa01ccdSSowmini Varadhan 
54506fa01ccdSSowmini Varadhan 	do {
54516fa01ccdSSowmini Varadhan 		if (!list) {
54526fa01ccdSSowmini Varadhan 			pr_err("Not enough bytes to eat. Want %d\n", eat);
54536fa01ccdSSowmini Varadhan 			return -EFAULT;
54546fa01ccdSSowmini Varadhan 		}
54556fa01ccdSSowmini Varadhan 		if (list->len <= eat) {
54566fa01ccdSSowmini Varadhan 			/* Eaten as whole. */
54576fa01ccdSSowmini Varadhan 			eat -= list->len;
54586fa01ccdSSowmini Varadhan 			list = list->next;
54596fa01ccdSSowmini Varadhan 			insp = list;
54606fa01ccdSSowmini Varadhan 		} else {
54616fa01ccdSSowmini Varadhan 			/* Eaten partially. */
54626fa01ccdSSowmini Varadhan 			if (skb_shared(list)) {
54636fa01ccdSSowmini Varadhan 				clone = skb_clone(list, gfp_mask);
54646fa01ccdSSowmini Varadhan 				if (!clone)
54656fa01ccdSSowmini Varadhan 					return -ENOMEM;
54666fa01ccdSSowmini Varadhan 				insp = list->next;
54676fa01ccdSSowmini Varadhan 				list = clone;
54686fa01ccdSSowmini Varadhan 			} else {
54696fa01ccdSSowmini Varadhan 				/* This may be pulled without problems. */
54706fa01ccdSSowmini Varadhan 				insp = list;
54716fa01ccdSSowmini Varadhan 			}
54726fa01ccdSSowmini Varadhan 			if (pskb_carve(list, eat, gfp_mask) < 0) {
54736fa01ccdSSowmini Varadhan 				kfree_skb(clone);
54746fa01ccdSSowmini Varadhan 				return -ENOMEM;
54756fa01ccdSSowmini Varadhan 			}
54766fa01ccdSSowmini Varadhan 			break;
54776fa01ccdSSowmini Varadhan 		}
54786fa01ccdSSowmini Varadhan 	} while (eat);
54796fa01ccdSSowmini Varadhan 
54806fa01ccdSSowmini Varadhan 	/* Free pulled out fragments. */
54816fa01ccdSSowmini Varadhan 	while ((list = shinfo->frag_list) != insp) {
54826fa01ccdSSowmini Varadhan 		shinfo->frag_list = list->next;
54836fa01ccdSSowmini Varadhan 		kfree_skb(list);
54846fa01ccdSSowmini Varadhan 	}
54856fa01ccdSSowmini Varadhan 	/* And insert new clone at head. */
54866fa01ccdSSowmini Varadhan 	if (clone) {
54876fa01ccdSSowmini Varadhan 		clone->next = list;
54886fa01ccdSSowmini Varadhan 		shinfo->frag_list = clone;
54896fa01ccdSSowmini Varadhan 	}
54906fa01ccdSSowmini Varadhan 	return 0;
54916fa01ccdSSowmini Varadhan }
54926fa01ccdSSowmini Varadhan 
54936fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
54946fa01ccdSSowmini Varadhan  * non-linear part of skb
54956fa01ccdSSowmini Varadhan  */
54966fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
54976fa01ccdSSowmini Varadhan 				       int pos, gfp_t gfp_mask)
54986fa01ccdSSowmini Varadhan {
54996fa01ccdSSowmini Varadhan 	int i, k = 0;
55006fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
55016fa01ccdSSowmini Varadhan 	u8 *data;
55026fa01ccdSSowmini Varadhan 	const int nfrags = skb_shinfo(skb)->nr_frags;
55036fa01ccdSSowmini Varadhan 	struct skb_shared_info *shinfo;
55046fa01ccdSSowmini Varadhan 
55056fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
55066fa01ccdSSowmini Varadhan 
55076fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
55086fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
55096fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
55106fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
55116fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
55126fa01ccdSSowmini Varadhan 	if (!data)
55136fa01ccdSSowmini Varadhan 		return -ENOMEM;
55146fa01ccdSSowmini Varadhan 
55156fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
55166fa01ccdSSowmini Varadhan 
55176fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
55186fa01ccdSSowmini Varadhan 	       skb_shinfo(skb), offsetof(struct skb_shared_info,
55196fa01ccdSSowmini Varadhan 					 frags[skb_shinfo(skb)->nr_frags]));
55206fa01ccdSSowmini Varadhan 	if (skb_orphan_frags(skb, gfp_mask)) {
55216fa01ccdSSowmini Varadhan 		kfree(data);
55226fa01ccdSSowmini Varadhan 		return -ENOMEM;
55236fa01ccdSSowmini Varadhan 	}
55246fa01ccdSSowmini Varadhan 	shinfo = (struct skb_shared_info *)(data + size);
55256fa01ccdSSowmini Varadhan 	for (i = 0; i < nfrags; i++) {
55266fa01ccdSSowmini Varadhan 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
55276fa01ccdSSowmini Varadhan 
55286fa01ccdSSowmini Varadhan 		if (pos + fsize > off) {
55296fa01ccdSSowmini Varadhan 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
55306fa01ccdSSowmini Varadhan 
55316fa01ccdSSowmini Varadhan 			if (pos < off) {
55326fa01ccdSSowmini Varadhan 				/* Split frag.
55336fa01ccdSSowmini Varadhan 				 * We have two variants in this case:
55346fa01ccdSSowmini Varadhan 				 * 1. Move all the frag to the second
55356fa01ccdSSowmini Varadhan 				 *    part, if it is possible. F.e.
55366fa01ccdSSowmini Varadhan 				 *    this approach is mandatory for TUX,
55376fa01ccdSSowmini Varadhan 				 *    where splitting is expensive.
55386fa01ccdSSowmini Varadhan 				 * 2. Split is accurately. We make this.
55396fa01ccdSSowmini Varadhan 				 */
55406fa01ccdSSowmini Varadhan 				shinfo->frags[0].page_offset += off - pos;
55416fa01ccdSSowmini Varadhan 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
55426fa01ccdSSowmini Varadhan 			}
55436fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
55446fa01ccdSSowmini Varadhan 			k++;
55456fa01ccdSSowmini Varadhan 		}
55466fa01ccdSSowmini Varadhan 		pos += fsize;
55476fa01ccdSSowmini Varadhan 	}
55486fa01ccdSSowmini Varadhan 	shinfo->nr_frags = k;
55496fa01ccdSSowmini Varadhan 	if (skb_has_frag_list(skb))
55506fa01ccdSSowmini Varadhan 		skb_clone_fraglist(skb);
55516fa01ccdSSowmini Varadhan 
55526fa01ccdSSowmini Varadhan 	if (k == 0) {
55536fa01ccdSSowmini Varadhan 		/* split line is in frag list */
55546fa01ccdSSowmini Varadhan 		pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
55556fa01ccdSSowmini Varadhan 	}
55566fa01ccdSSowmini Varadhan 	skb_release_data(skb);
55576fa01ccdSSowmini Varadhan 
55586fa01ccdSSowmini Varadhan 	skb->head = data;
55596fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
55606fa01ccdSSowmini Varadhan 	skb->data = data;
55616fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
55626fa01ccdSSowmini Varadhan 	skb->end = size;
55636fa01ccdSSowmini Varadhan #else
55646fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
55656fa01ccdSSowmini Varadhan #endif
55666fa01ccdSSowmini Varadhan 	skb_reset_tail_pointer(skb);
55676fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
55686fa01ccdSSowmini Varadhan 	skb->cloned   = 0;
55696fa01ccdSSowmini Varadhan 	skb->hdr_len  = 0;
55706fa01ccdSSowmini Varadhan 	skb->nohdr    = 0;
55716fa01ccdSSowmini Varadhan 	skb->len -= off;
55726fa01ccdSSowmini Varadhan 	skb->data_len = skb->len;
55736fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
55746fa01ccdSSowmini Varadhan 	return 0;
55756fa01ccdSSowmini Varadhan }
55766fa01ccdSSowmini Varadhan 
55776fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
55786fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
55796fa01ccdSSowmini Varadhan {
55806fa01ccdSSowmini Varadhan 	int headlen = skb_headlen(skb);
55816fa01ccdSSowmini Varadhan 
55826fa01ccdSSowmini Varadhan 	if (len < headlen)
55836fa01ccdSSowmini Varadhan 		return pskb_carve_inside_header(skb, len, headlen, gfp);
55846fa01ccdSSowmini Varadhan 	else
55856fa01ccdSSowmini Varadhan 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
55866fa01ccdSSowmini Varadhan }
55876fa01ccdSSowmini Varadhan 
55886fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
55896fa01ccdSSowmini Varadhan  * a new skb
55906fa01ccdSSowmini Varadhan  */
55916fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
55926fa01ccdSSowmini Varadhan 			     int to_copy, gfp_t gfp)
55936fa01ccdSSowmini Varadhan {
55946fa01ccdSSowmini Varadhan 	struct sk_buff  *clone = skb_clone(skb, gfp);
55956fa01ccdSSowmini Varadhan 
55966fa01ccdSSowmini Varadhan 	if (!clone)
55976fa01ccdSSowmini Varadhan 		return NULL;
55986fa01ccdSSowmini Varadhan 
55996fa01ccdSSowmini Varadhan 	if (pskb_carve(clone, off, gfp) < 0 ||
56006fa01ccdSSowmini Varadhan 	    pskb_trim(clone, to_copy)) {
56016fa01ccdSSowmini Varadhan 		kfree_skb(clone);
56026fa01ccdSSowmini Varadhan 		return NULL;
56036fa01ccdSSowmini Varadhan 	}
56046fa01ccdSSowmini Varadhan 	return clone;
56056fa01ccdSSowmini Varadhan }
56066fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
5607c8c8b127SEric Dumazet 
5608c8c8b127SEric Dumazet /**
5609c8c8b127SEric Dumazet  * skb_condense - try to get rid of fragments/frag_list if possible
5610c8c8b127SEric Dumazet  * @skb: buffer
5611c8c8b127SEric Dumazet  *
5612c8c8b127SEric Dumazet  * Can be used to save memory before skb is added to a busy queue.
5613c8c8b127SEric Dumazet  * If packet has bytes in frags and enough tail room in skb->head,
5614c8c8b127SEric Dumazet  * pull all of them, so that we can free the frags right now and adjust
5615c8c8b127SEric Dumazet  * truesize.
5616c8c8b127SEric Dumazet  * Notes:
5617c8c8b127SEric Dumazet  *	We do not reallocate skb->head thus can not fail.
5618c8c8b127SEric Dumazet  *	Caller must re-evaluate skb->truesize if needed.
5619c8c8b127SEric Dumazet  */
5620c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
5621c8c8b127SEric Dumazet {
56223174fed9SEric Dumazet 	if (skb->data_len) {
56233174fed9SEric Dumazet 		if (skb->data_len > skb->end - skb->tail ||
5624c8c8b127SEric Dumazet 		    skb_cloned(skb))
5625c8c8b127SEric Dumazet 			return;
5626c8c8b127SEric Dumazet 
5627c8c8b127SEric Dumazet 		/* Nice, we can free page frag(s) right now */
5628c8c8b127SEric Dumazet 		__pskb_pull_tail(skb, skb->data_len);
56293174fed9SEric Dumazet 	}
56303174fed9SEric Dumazet 	/* At this point, skb->truesize might be over estimated,
56313174fed9SEric Dumazet 	 * because skb had a fragment, and fragments do not tell
56323174fed9SEric Dumazet 	 * their truesize.
56333174fed9SEric Dumazet 	 * When we pulled its content into skb->head, fragment
56343174fed9SEric Dumazet 	 * was freed, but __pskb_pull_tail() could not possibly
56353174fed9SEric Dumazet 	 * adjust skb->truesize, not knowing the frag truesize.
5636c8c8b127SEric Dumazet 	 */
5637c8c8b127SEric Dumazet 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5638c8c8b127SEric Dumazet }
5639df5042f4SFlorian Westphal 
5640df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
5641df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5642df5042f4SFlorian Westphal {
5643df5042f4SFlorian Westphal 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5644df5042f4SFlorian Westphal }
5645df5042f4SFlorian Westphal 
5646df5042f4SFlorian Westphal static struct skb_ext *skb_ext_alloc(void)
5647df5042f4SFlorian Westphal {
5648df5042f4SFlorian Westphal 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5649df5042f4SFlorian Westphal 
5650df5042f4SFlorian Westphal 	if (new) {
5651df5042f4SFlorian Westphal 		memset(new->offset, 0, sizeof(new->offset));
5652df5042f4SFlorian Westphal 		refcount_set(&new->refcnt, 1);
5653df5042f4SFlorian Westphal 	}
5654df5042f4SFlorian Westphal 
5655df5042f4SFlorian Westphal 	return new;
5656df5042f4SFlorian Westphal }
5657df5042f4SFlorian Westphal 
56584165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
56594165079bSFlorian Westphal 					 unsigned int old_active)
5660df5042f4SFlorian Westphal {
5661df5042f4SFlorian Westphal 	struct skb_ext *new;
5662df5042f4SFlorian Westphal 
5663df5042f4SFlorian Westphal 	if (refcount_read(&old->refcnt) == 1)
5664df5042f4SFlorian Westphal 		return old;
5665df5042f4SFlorian Westphal 
5666df5042f4SFlorian Westphal 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5667df5042f4SFlorian Westphal 	if (!new)
5668df5042f4SFlorian Westphal 		return NULL;
5669df5042f4SFlorian Westphal 
5670df5042f4SFlorian Westphal 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5671df5042f4SFlorian Westphal 	refcount_set(&new->refcnt, 1);
5672df5042f4SFlorian Westphal 
56734165079bSFlorian Westphal #ifdef CONFIG_XFRM
56744165079bSFlorian Westphal 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
56754165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
56764165079bSFlorian Westphal 		unsigned int i;
56774165079bSFlorian Westphal 
56784165079bSFlorian Westphal 		for (i = 0; i < sp->len; i++)
56794165079bSFlorian Westphal 			xfrm_state_hold(sp->xvec[i]);
56804165079bSFlorian Westphal 	}
56814165079bSFlorian Westphal #endif
5682df5042f4SFlorian Westphal 	__skb_ext_put(old);
5683df5042f4SFlorian Westphal 	return new;
5684df5042f4SFlorian Westphal }
5685df5042f4SFlorian Westphal 
5686df5042f4SFlorian Westphal /**
5687df5042f4SFlorian Westphal  * skb_ext_add - allocate space for given extension, COW if needed
5688df5042f4SFlorian Westphal  * @skb: buffer
5689df5042f4SFlorian Westphal  * @id: extension to allocate space for
5690df5042f4SFlorian Westphal  *
5691df5042f4SFlorian Westphal  * Allocates enough space for the given extension.
5692df5042f4SFlorian Westphal  * If the extension is already present, a pointer to that extension
5693df5042f4SFlorian Westphal  * is returned.
5694df5042f4SFlorian Westphal  *
5695df5042f4SFlorian Westphal  * If the skb was cloned, COW applies and the returned memory can be
5696df5042f4SFlorian Westphal  * modified without changing the extension space of clones buffers.
5697df5042f4SFlorian Westphal  *
5698df5042f4SFlorian Westphal  * Returns pointer to the extension or NULL on allocation failure.
5699df5042f4SFlorian Westphal  */
5700df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5701df5042f4SFlorian Westphal {
5702df5042f4SFlorian Westphal 	struct skb_ext *new, *old = NULL;
5703df5042f4SFlorian Westphal 	unsigned int newlen, newoff;
5704df5042f4SFlorian Westphal 
5705df5042f4SFlorian Westphal 	if (skb->active_extensions) {
5706df5042f4SFlorian Westphal 		old = skb->extensions;
5707df5042f4SFlorian Westphal 
57084165079bSFlorian Westphal 		new = skb_ext_maybe_cow(old, skb->active_extensions);
5709df5042f4SFlorian Westphal 		if (!new)
5710df5042f4SFlorian Westphal 			return NULL;
5711df5042f4SFlorian Westphal 
5712682ec859SPaolo Abeni 		if (__skb_ext_exist(new, id))
5713df5042f4SFlorian Westphal 			goto set_active;
5714df5042f4SFlorian Westphal 
5715e94e50bdSPaolo Abeni 		newoff = new->chunks;
5716df5042f4SFlorian Westphal 	} else {
5717df5042f4SFlorian Westphal 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
5718df5042f4SFlorian Westphal 
5719df5042f4SFlorian Westphal 		new = skb_ext_alloc();
5720df5042f4SFlorian Westphal 		if (!new)
5721df5042f4SFlorian Westphal 			return NULL;
5722df5042f4SFlorian Westphal 	}
5723df5042f4SFlorian Westphal 
5724df5042f4SFlorian Westphal 	newlen = newoff + skb_ext_type_len[id];
5725df5042f4SFlorian Westphal 	new->chunks = newlen;
5726df5042f4SFlorian Westphal 	new->offset[id] = newoff;
5727df5042f4SFlorian Westphal set_active:
5728682ec859SPaolo Abeni 	skb->extensions = new;
5729df5042f4SFlorian Westphal 	skb->active_extensions |= 1 << id;
5730df5042f4SFlorian Westphal 	return skb_ext_get_ptr(new, id);
5731df5042f4SFlorian Westphal }
5732df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add);
5733df5042f4SFlorian Westphal 
57344165079bSFlorian Westphal #ifdef CONFIG_XFRM
57354165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp)
57364165079bSFlorian Westphal {
57374165079bSFlorian Westphal 	unsigned int i;
57384165079bSFlorian Westphal 
57394165079bSFlorian Westphal 	for (i = 0; i < sp->len; i++)
57404165079bSFlorian Westphal 		xfrm_state_put(sp->xvec[i]);
57414165079bSFlorian Westphal }
57424165079bSFlorian Westphal #endif
57434165079bSFlorian Westphal 
5744df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5745df5042f4SFlorian Westphal {
5746df5042f4SFlorian Westphal 	struct skb_ext *ext = skb->extensions;
5747df5042f4SFlorian Westphal 
5748df5042f4SFlorian Westphal 	skb->active_extensions &= ~(1 << id);
5749df5042f4SFlorian Westphal 	if (skb->active_extensions == 0) {
5750df5042f4SFlorian Westphal 		skb->extensions = NULL;
5751df5042f4SFlorian Westphal 		__skb_ext_put(ext);
57524165079bSFlorian Westphal #ifdef CONFIG_XFRM
57534165079bSFlorian Westphal 	} else if (id == SKB_EXT_SEC_PATH &&
57544165079bSFlorian Westphal 		   refcount_read(&ext->refcnt) == 1) {
57554165079bSFlorian Westphal 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
57564165079bSFlorian Westphal 
57574165079bSFlorian Westphal 		skb_ext_put_sp(sp);
57584165079bSFlorian Westphal 		sp->len = 0;
57594165079bSFlorian Westphal #endif
5760df5042f4SFlorian Westphal 	}
5761df5042f4SFlorian Westphal }
5762df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del);
5763df5042f4SFlorian Westphal 
5764df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext)
5765df5042f4SFlorian Westphal {
5766df5042f4SFlorian Westphal 	/* If this is last clone, nothing can increment
5767df5042f4SFlorian Westphal 	 * it after check passes.  Avoids one atomic op.
5768df5042f4SFlorian Westphal 	 */
5769df5042f4SFlorian Westphal 	if (refcount_read(&ext->refcnt) == 1)
5770df5042f4SFlorian Westphal 		goto free_now;
5771df5042f4SFlorian Westphal 
5772df5042f4SFlorian Westphal 	if (!refcount_dec_and_test(&ext->refcnt))
5773df5042f4SFlorian Westphal 		return;
5774df5042f4SFlorian Westphal free_now:
57754165079bSFlorian Westphal #ifdef CONFIG_XFRM
57764165079bSFlorian Westphal 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
57774165079bSFlorian Westphal 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
57784165079bSFlorian Westphal #endif
57794165079bSFlorian Westphal 
5780df5042f4SFlorian Westphal 	kmem_cache_free(skbuff_ext_cache, ext);
5781df5042f4SFlorian Westphal }
5782df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put);
5783df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */
5784