xref: /linux/net/core/skbuff.c (revision b2db6f4ace72e71fa09b8d1354f8ac9854140d74)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Routines having to do with the 'struct sk_buff' memory handlers.
4  *
5  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
6  *			Florian La Roche <rzsfl@rz.uni-sb.de>
7  *
8  *	Fixes:
9  *		Alan Cox	:	Fixed the worst of the load
10  *					balancer bugs.
11  *		Dave Platt	:	Interrupt stacking fix.
12  *	Richard Kooijman	:	Timestamp fixes.
13  *		Alan Cox	:	Changed buffer format.
14  *		Alan Cox	:	destructor hook for AF_UNIX etc.
15  *		Linus Torvalds	:	Better skb_clone.
16  *		Alan Cox	:	Added skb_copy.
17  *		Alan Cox	:	Added all the changed routines Linus
18  *					only put in the headers
19  *		Ray VanTassle	:	Fixed --skb->lock in free
20  *		Alan Cox	:	skb_copy copy arp field
21  *		Andi Kleen	:	slabified it.
22  *		Robert Olsson	:	Removed skb_head_pool
23  *
24  *	NOTE:
25  *		The __skb_ routines should be called with interrupts
26  *	disabled, or you better be *real* sure that the operation is atomic
27  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
28  *	or via disabling bottom half handlers, etc).
29  */
30 
31 /*
32  *	The functions in this file will not compile correctly with gcc 2.4.x
33  */
34 
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/in.h>
43 #include <linux/inet.h>
44 #include <linux/slab.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 #include <linux/sctp.h>
48 #include <linux/netdevice.h>
49 #ifdef CONFIG_NET_CLS_ACT
50 #include <net/pkt_sched.h>
51 #endif
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/skbuff_ref.h>
55 #include <linux/splice.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/scatterlist.h>
60 #include <linux/errqueue.h>
61 #include <linux/prefetch.h>
62 #include <linux/bitfield.h>
63 #include <linux/if_vlan.h>
64 #include <linux/mpls.h>
65 #include <linux/kcov.h>
66 #include <linux/iov_iter.h>
67 
68 #include <net/protocol.h>
69 #include <net/dst.h>
70 #include <net/sock.h>
71 #include <net/checksum.h>
72 #include <net/gso.h>
73 #include <net/hotdata.h>
74 #include <net/ip6_checksum.h>
75 #include <net/xfrm.h>
76 #include <net/mpls.h>
77 #include <net/mptcp.h>
78 #include <net/mctp.h>
79 #include <net/page_pool/helpers.h>
80 #include <net/dropreason.h>
81 
82 #include <linux/uaccess.h>
83 #include <trace/events/skb.h>
84 #include <linux/highmem.h>
85 #include <linux/capability.h>
86 #include <linux/user_namespace.h>
87 #include <linux/indirect_call_wrapper.h>
88 #include <linux/textsearch.h>
89 
90 #include "dev.h"
91 #include "sock_destructor.h"
92 
93 #ifdef CONFIG_SKB_EXTENSIONS
94 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
95 #endif
96 
97 #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER)
98 
99 /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two.
100  * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique
101  * size, and we can differentiate heads from skb_small_head_cache
102  * vs system slabs by looking at their size (skb_end_offset()).
103  */
104 #define SKB_SMALL_HEAD_CACHE_SIZE					\
105 	(is_power_of_2(SKB_SMALL_HEAD_SIZE) ?			\
106 		(SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) :	\
107 		SKB_SMALL_HEAD_SIZE)
108 
109 #define SKB_SMALL_HEAD_HEADROOM						\
110 	SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)
111 
112 /* kcm_write_msgs() relies on casting paged frags to bio_vec to use
113  * iov_iter_bvec(). These static asserts ensure the cast is valid is long as the
114  * netmem is a page.
115  */
116 static_assert(offsetof(struct bio_vec, bv_page) ==
117 	      offsetof(skb_frag_t, netmem));
118 static_assert(sizeof_field(struct bio_vec, bv_page) ==
119 	      sizeof_field(skb_frag_t, netmem));
120 
121 static_assert(offsetof(struct bio_vec, bv_len) == offsetof(skb_frag_t, len));
122 static_assert(sizeof_field(struct bio_vec, bv_len) ==
123 	      sizeof_field(skb_frag_t, len));
124 
125 static_assert(offsetof(struct bio_vec, bv_offset) ==
126 	      offsetof(skb_frag_t, offset));
127 static_assert(sizeof_field(struct bio_vec, bv_offset) ==
128 	      sizeof_field(skb_frag_t, offset));
129 
130 #undef FN
131 #define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
132 static const char * const drop_reasons[] = {
133 	[SKB_CONSUMED] = "CONSUMED",
134 	DEFINE_DROP_REASON(FN, FN)
135 };
136 
137 static const struct drop_reason_list drop_reasons_core = {
138 	.reasons = drop_reasons,
139 	.n_reasons = ARRAY_SIZE(drop_reasons),
140 };
141 
142 const struct drop_reason_list __rcu *
143 drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = {
144 	[SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core),
145 };
146 EXPORT_SYMBOL(drop_reasons_by_subsys);
147 
148 /**
149  * drop_reasons_register_subsys - register another drop reason subsystem
150  * @subsys: the subsystem to register, must not be the core
151  * @list: the list of drop reasons within the subsystem, must point to
152  *	a statically initialized list
153  */
154 void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,
155 				  const struct drop_reason_list *list)
156 {
157 	if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
158 		 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
159 		 "invalid subsystem %d\n", subsys))
160 		return;
161 
162 	/* must point to statically allocated memory, so INIT is OK */
163 	RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list);
164 }
165 EXPORT_SYMBOL_GPL(drop_reasons_register_subsys);
166 
167 /**
168  * drop_reasons_unregister_subsys - unregister a drop reason subsystem
169  * @subsys: the subsystem to remove, must not be the core
170  *
171  * Note: This will synchronize_rcu() to ensure no users when it returns.
172  */
173 void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)
174 {
175 	if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
176 		 subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
177 		 "invalid subsystem %d\n", subsys))
178 		return;
179 
180 	RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL);
181 
182 	synchronize_rcu();
183 }
184 EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys);
185 
186 /**
187  *	skb_panic - private function for out-of-line support
188  *	@skb:	buffer
189  *	@sz:	size
190  *	@addr:	address
191  *	@msg:	skb_over_panic or skb_under_panic
192  *
193  *	Out-of-line support for skb_put() and skb_push().
194  *	Called via the wrapper skb_over_panic() or skb_under_panic().
195  *	Keep out of line to prevent kernel bloat.
196  *	__builtin_return_address is not used because it is not always reliable.
197  */
198 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
199 		      const char msg[])
200 {
201 	pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
202 		 msg, addr, skb->len, sz, skb->head, skb->data,
203 		 (unsigned long)skb->tail, (unsigned long)skb->end,
204 		 skb->dev ? skb->dev->name : "<NULL>");
205 	BUG();
206 }
207 
208 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
209 {
210 	skb_panic(skb, sz, addr, __func__);
211 }
212 
213 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
214 {
215 	skb_panic(skb, sz, addr, __func__);
216 }
217 
218 #define NAPI_SKB_CACHE_SIZE	64
219 #define NAPI_SKB_CACHE_BULK	16
220 #define NAPI_SKB_CACHE_HALF	(NAPI_SKB_CACHE_SIZE / 2)
221 
222 #if PAGE_SIZE == SZ_4K
223 
224 #define NAPI_HAS_SMALL_PAGE_FRAG	1
225 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	((nc).pfmemalloc)
226 
227 /* specialized page frag allocator using a single order 0 page
228  * and slicing it into 1K sized fragment. Constrained to systems
229  * with a very limited amount of 1K fragments fitting a single
230  * page - to avoid excessive truesize underestimation
231  */
232 
233 struct page_frag_1k {
234 	void *va;
235 	u16 offset;
236 	bool pfmemalloc;
237 };
238 
239 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
240 {
241 	struct page *page;
242 	int offset;
243 
244 	offset = nc->offset - SZ_1K;
245 	if (likely(offset >= 0))
246 		goto use_frag;
247 
248 	page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
249 	if (!page)
250 		return NULL;
251 
252 	nc->va = page_address(page);
253 	nc->pfmemalloc = page_is_pfmemalloc(page);
254 	offset = PAGE_SIZE - SZ_1K;
255 	page_ref_add(page, offset / SZ_1K);
256 
257 use_frag:
258 	nc->offset = offset;
259 	return nc->va + offset;
260 }
261 #else
262 
263 /* the small page is actually unused in this build; add dummy helpers
264  * to please the compiler and avoid later preprocessor's conditionals
265  */
266 #define NAPI_HAS_SMALL_PAGE_FRAG	0
267 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	false
268 
269 struct page_frag_1k {
270 };
271 
272 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
273 {
274 	return NULL;
275 }
276 
277 #endif
278 
279 struct napi_alloc_cache {
280 	local_lock_t bh_lock;
281 	struct page_frag_cache page;
282 	struct page_frag_1k page_small;
283 	unsigned int skb_count;
284 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
285 };
286 
287 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
288 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache) = {
289 	.bh_lock = INIT_LOCAL_LOCK(bh_lock),
290 };
291 
292 /* Double check that napi_get_frags() allocates skbs with
293  * skb->head being backed by slab, not a page fragment.
294  * This is to make sure bug fixed in 3226b158e67c
295  * ("net: avoid 32 x truesize under-estimation for tiny skbs")
296  * does not accidentally come back.
297  */
298 void napi_get_frags_check(struct napi_struct *napi)
299 {
300 	struct sk_buff *skb;
301 
302 	local_bh_disable();
303 	skb = napi_get_frags(napi);
304 	WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
305 	napi_free_frags(napi);
306 	local_bh_enable();
307 }
308 
309 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
310 {
311 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
312 	void *data;
313 
314 	fragsz = SKB_DATA_ALIGN(fragsz);
315 
316 	local_lock_nested_bh(&napi_alloc_cache.bh_lock);
317 	data = __page_frag_alloc_align(&nc->page, fragsz,
318 				       GFP_ATOMIC | __GFP_NOWARN, align_mask);
319 	local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
320 	return data;
321 
322 }
323 EXPORT_SYMBOL(__napi_alloc_frag_align);
324 
325 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
326 {
327 	void *data;
328 
329 	if (in_hardirq() || irqs_disabled()) {
330 		struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
331 
332 		fragsz = SKB_DATA_ALIGN(fragsz);
333 		data = __page_frag_alloc_align(nc, fragsz,
334 					       GFP_ATOMIC | __GFP_NOWARN,
335 					       align_mask);
336 	} else {
337 		local_bh_disable();
338 		data = __napi_alloc_frag_align(fragsz, align_mask);
339 		local_bh_enable();
340 	}
341 	return data;
342 }
343 EXPORT_SYMBOL(__netdev_alloc_frag_align);
344 
345 static struct sk_buff *napi_skb_cache_get(void)
346 {
347 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
348 	struct sk_buff *skb;
349 
350 	local_lock_nested_bh(&napi_alloc_cache.bh_lock);
351 	if (unlikely(!nc->skb_count)) {
352 		nc->skb_count = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
353 						      GFP_ATOMIC | __GFP_NOWARN,
354 						      NAPI_SKB_CACHE_BULK,
355 						      nc->skb_cache);
356 		if (unlikely(!nc->skb_count)) {
357 			local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
358 			return NULL;
359 		}
360 	}
361 
362 	skb = nc->skb_cache[--nc->skb_count];
363 	local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
364 	kasan_mempool_unpoison_object(skb, kmem_cache_size(net_hotdata.skbuff_cache));
365 
366 	return skb;
367 }
368 
369 static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
370 					 unsigned int size)
371 {
372 	struct skb_shared_info *shinfo;
373 
374 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
375 
376 	/* Assumes caller memset cleared SKB */
377 	skb->truesize = SKB_TRUESIZE(size);
378 	refcount_set(&skb->users, 1);
379 	skb->head = data;
380 	skb->data = data;
381 	skb_reset_tail_pointer(skb);
382 	skb_set_end_offset(skb, size);
383 	skb->mac_header = (typeof(skb->mac_header))~0U;
384 	skb->transport_header = (typeof(skb->transport_header))~0U;
385 	skb->alloc_cpu = raw_smp_processor_id();
386 	/* make sure we initialize shinfo sequentially */
387 	shinfo = skb_shinfo(skb);
388 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
389 	atomic_set(&shinfo->dataref, 1);
390 
391 	skb_set_kcov_handle(skb, kcov_common_handle());
392 }
393 
394 static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
395 				     unsigned int *size)
396 {
397 	void *resized;
398 
399 	/* Must find the allocation size (and grow it to match). */
400 	*size = ksize(data);
401 	/* krealloc() will immediately return "data" when
402 	 * "ksize(data)" is requested: it is the existing upper
403 	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
404 	 * that this "new" pointer needs to be passed back to the
405 	 * caller for use so the __alloc_size hinting will be
406 	 * tracked correctly.
407 	 */
408 	resized = krealloc(data, *size, GFP_ATOMIC);
409 	WARN_ON_ONCE(resized != data);
410 	return resized;
411 }
412 
413 /* build_skb() variant which can operate on slab buffers.
414  * Note that this should be used sparingly as slab buffers
415  * cannot be combined efficiently by GRO!
416  */
417 struct sk_buff *slab_build_skb(void *data)
418 {
419 	struct sk_buff *skb;
420 	unsigned int size;
421 
422 	skb = kmem_cache_alloc(net_hotdata.skbuff_cache,
423 			       GFP_ATOMIC | __GFP_NOWARN);
424 	if (unlikely(!skb))
425 		return NULL;
426 
427 	memset(skb, 0, offsetof(struct sk_buff, tail));
428 	data = __slab_build_skb(skb, data, &size);
429 	__finalize_skb_around(skb, data, size);
430 
431 	return skb;
432 }
433 EXPORT_SYMBOL(slab_build_skb);
434 
435 /* Caller must provide SKB that is memset cleared */
436 static void __build_skb_around(struct sk_buff *skb, void *data,
437 			       unsigned int frag_size)
438 {
439 	unsigned int size = frag_size;
440 
441 	/* frag_size == 0 is considered deprecated now. Callers
442 	 * using slab buffer should use slab_build_skb() instead.
443 	 */
444 	if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
445 		data = __slab_build_skb(skb, data, &size);
446 
447 	__finalize_skb_around(skb, data, size);
448 }
449 
450 /**
451  * __build_skb - build a network buffer
452  * @data: data buffer provided by caller
453  * @frag_size: size of data (must not be 0)
454  *
455  * Allocate a new &sk_buff. Caller provides space holding head and
456  * skb_shared_info. @data must have been allocated from the page
457  * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
458  * allocation is deprecated, and callers should use slab_build_skb()
459  * instead.)
460  * The return is the new skb buffer.
461  * On a failure the return is %NULL, and @data is not freed.
462  * Notes :
463  *  Before IO, driver allocates only data buffer where NIC put incoming frame
464  *  Driver should add room at head (NET_SKB_PAD) and
465  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
466  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
467  *  before giving packet to stack.
468  *  RX rings only contains data buffers, not full skbs.
469  */
470 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
471 {
472 	struct sk_buff *skb;
473 
474 	skb = kmem_cache_alloc(net_hotdata.skbuff_cache,
475 			       GFP_ATOMIC | __GFP_NOWARN);
476 	if (unlikely(!skb))
477 		return NULL;
478 
479 	memset(skb, 0, offsetof(struct sk_buff, tail));
480 	__build_skb_around(skb, data, frag_size);
481 
482 	return skb;
483 }
484 
485 /* build_skb() is wrapper over __build_skb(), that specifically
486  * takes care of skb->head and skb->pfmemalloc
487  */
488 struct sk_buff *build_skb(void *data, unsigned int frag_size)
489 {
490 	struct sk_buff *skb = __build_skb(data, frag_size);
491 
492 	if (likely(skb && frag_size)) {
493 		skb->head_frag = 1;
494 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
495 	}
496 	return skb;
497 }
498 EXPORT_SYMBOL(build_skb);
499 
500 /**
501  * build_skb_around - build a network buffer around provided skb
502  * @skb: sk_buff provide by caller, must be memset cleared
503  * @data: data buffer provided by caller
504  * @frag_size: size of data
505  */
506 struct sk_buff *build_skb_around(struct sk_buff *skb,
507 				 void *data, unsigned int frag_size)
508 {
509 	if (unlikely(!skb))
510 		return NULL;
511 
512 	__build_skb_around(skb, data, frag_size);
513 
514 	if (frag_size) {
515 		skb->head_frag = 1;
516 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
517 	}
518 	return skb;
519 }
520 EXPORT_SYMBOL(build_skb_around);
521 
522 /**
523  * __napi_build_skb - build a network buffer
524  * @data: data buffer provided by caller
525  * @frag_size: size of data
526  *
527  * Version of __build_skb() that uses NAPI percpu caches to obtain
528  * skbuff_head instead of inplace allocation.
529  *
530  * Returns a new &sk_buff on success, %NULL on allocation failure.
531  */
532 static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
533 {
534 	struct sk_buff *skb;
535 
536 	skb = napi_skb_cache_get();
537 	if (unlikely(!skb))
538 		return NULL;
539 
540 	memset(skb, 0, offsetof(struct sk_buff, tail));
541 	__build_skb_around(skb, data, frag_size);
542 
543 	return skb;
544 }
545 
546 /**
547  * napi_build_skb - build a network buffer
548  * @data: data buffer provided by caller
549  * @frag_size: size of data
550  *
551  * Version of __napi_build_skb() that takes care of skb->head_frag
552  * and skb->pfmemalloc when the data is a page or page fragment.
553  *
554  * Returns a new &sk_buff on success, %NULL on allocation failure.
555  */
556 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
557 {
558 	struct sk_buff *skb = __napi_build_skb(data, frag_size);
559 
560 	if (likely(skb) && frag_size) {
561 		skb->head_frag = 1;
562 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
563 	}
564 
565 	return skb;
566 }
567 EXPORT_SYMBOL(napi_build_skb);
568 
569 /*
570  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
571  * the caller if emergency pfmemalloc reserves are being used. If it is and
572  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
573  * may be used. Otherwise, the packet data may be discarded until enough
574  * memory is free
575  */
576 static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
577 			     bool *pfmemalloc)
578 {
579 	bool ret_pfmemalloc = false;
580 	size_t obj_size;
581 	void *obj;
582 
583 	obj_size = SKB_HEAD_ALIGN(*size);
584 	if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
585 	    !(flags & KMALLOC_NOT_NORMAL_BITS)) {
586 		obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache,
587 				flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
588 				node);
589 		*size = SKB_SMALL_HEAD_CACHE_SIZE;
590 		if (obj || !(gfp_pfmemalloc_allowed(flags)))
591 			goto out;
592 		/* Try again but now we are using pfmemalloc reserves */
593 		ret_pfmemalloc = true;
594 		obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache, flags, node);
595 		goto out;
596 	}
597 
598 	obj_size = kmalloc_size_roundup(obj_size);
599 	/* The following cast might truncate high-order bits of obj_size, this
600 	 * is harmless because kmalloc(obj_size >= 2^32) will fail anyway.
601 	 */
602 	*size = (unsigned int)obj_size;
603 
604 	/*
605 	 * Try a regular allocation, when that fails and we're not entitled
606 	 * to the reserves, fail.
607 	 */
608 	obj = kmalloc_node_track_caller(obj_size,
609 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
610 					node);
611 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
612 		goto out;
613 
614 	/* Try again but now we are using pfmemalloc reserves */
615 	ret_pfmemalloc = true;
616 	obj = kmalloc_node_track_caller(obj_size, flags, node);
617 
618 out:
619 	if (pfmemalloc)
620 		*pfmemalloc = ret_pfmemalloc;
621 
622 	return obj;
623 }
624 
625 /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
626  *	'private' fields and also do memory statistics to find all the
627  *	[BEEP] leaks.
628  *
629  */
630 
631 /**
632  *	__alloc_skb	-	allocate a network buffer
633  *	@size: size to allocate
634  *	@gfp_mask: allocation mask
635  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
636  *		instead of head cache and allocate a cloned (child) skb.
637  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
638  *		allocations in case the data is required for writeback
639  *	@node: numa node to allocate memory on
640  *
641  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
642  *	tail room of at least size bytes. The object has a reference count
643  *	of one. The return is the buffer. On a failure the return is %NULL.
644  *
645  *	Buffers may only be allocated from interrupts using a @gfp_mask of
646  *	%GFP_ATOMIC.
647  */
648 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
649 			    int flags, int node)
650 {
651 	struct kmem_cache *cache;
652 	struct sk_buff *skb;
653 	bool pfmemalloc;
654 	u8 *data;
655 
656 	cache = (flags & SKB_ALLOC_FCLONE)
657 		? net_hotdata.skbuff_fclone_cache : net_hotdata.skbuff_cache;
658 
659 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
660 		gfp_mask |= __GFP_MEMALLOC;
661 
662 	/* Get the HEAD */
663 	if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
664 	    likely(node == NUMA_NO_NODE || node == numa_mem_id()))
665 		skb = napi_skb_cache_get();
666 	else
667 		skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
668 	if (unlikely(!skb))
669 		return NULL;
670 	prefetchw(skb);
671 
672 	/* We do our best to align skb_shared_info on a separate cache
673 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
674 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
675 	 * Both skb->head and skb_shared_info are cache line aligned.
676 	 */
677 	data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
678 	if (unlikely(!data))
679 		goto nodata;
680 	/* kmalloc_size_roundup() might give us more room than requested.
681 	 * Put skb_shared_info exactly at the end of allocated zone,
682 	 * to allow max possible filling before reallocation.
683 	 */
684 	prefetchw(data + SKB_WITH_OVERHEAD(size));
685 
686 	/*
687 	 * Only clear those fields we need to clear, not those that we will
688 	 * actually initialise below. Hence, don't put any more fields after
689 	 * the tail pointer in struct sk_buff!
690 	 */
691 	memset(skb, 0, offsetof(struct sk_buff, tail));
692 	__build_skb_around(skb, data, size);
693 	skb->pfmemalloc = pfmemalloc;
694 
695 	if (flags & SKB_ALLOC_FCLONE) {
696 		struct sk_buff_fclones *fclones;
697 
698 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
699 
700 		skb->fclone = SKB_FCLONE_ORIG;
701 		refcount_set(&fclones->fclone_ref, 1);
702 	}
703 
704 	return skb;
705 
706 nodata:
707 	kmem_cache_free(cache, skb);
708 	return NULL;
709 }
710 EXPORT_SYMBOL(__alloc_skb);
711 
712 /**
713  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
714  *	@dev: network device to receive on
715  *	@len: length to allocate
716  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
717  *
718  *	Allocate a new &sk_buff and assign it a usage count of one. The
719  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
720  *	the headroom they think they need without accounting for the
721  *	built in space. The built in space is used for optimisations.
722  *
723  *	%NULL is returned if there is no free memory.
724  */
725 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
726 				   gfp_t gfp_mask)
727 {
728 	struct page_frag_cache *nc;
729 	struct sk_buff *skb;
730 	bool pfmemalloc;
731 	void *data;
732 
733 	len += NET_SKB_PAD;
734 
735 	/* If requested length is either too small or too big,
736 	 * we use kmalloc() for skb->head allocation.
737 	 */
738 	if (len <= SKB_WITH_OVERHEAD(1024) ||
739 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
740 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
741 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
742 		if (!skb)
743 			goto skb_fail;
744 		goto skb_success;
745 	}
746 
747 	len = SKB_HEAD_ALIGN(len);
748 
749 	if (sk_memalloc_socks())
750 		gfp_mask |= __GFP_MEMALLOC;
751 
752 	if (in_hardirq() || irqs_disabled()) {
753 		nc = this_cpu_ptr(&netdev_alloc_cache);
754 		data = page_frag_alloc(nc, len, gfp_mask);
755 		pfmemalloc = nc->pfmemalloc;
756 	} else {
757 		local_bh_disable();
758 		local_lock_nested_bh(&napi_alloc_cache.bh_lock);
759 
760 		nc = this_cpu_ptr(&napi_alloc_cache.page);
761 		data = page_frag_alloc(nc, len, gfp_mask);
762 		pfmemalloc = nc->pfmemalloc;
763 
764 		local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
765 		local_bh_enable();
766 	}
767 
768 	if (unlikely(!data))
769 		return NULL;
770 
771 	skb = __build_skb(data, len);
772 	if (unlikely(!skb)) {
773 		skb_free_frag(data);
774 		return NULL;
775 	}
776 
777 	if (pfmemalloc)
778 		skb->pfmemalloc = 1;
779 	skb->head_frag = 1;
780 
781 skb_success:
782 	skb_reserve(skb, NET_SKB_PAD);
783 	skb->dev = dev;
784 
785 skb_fail:
786 	return skb;
787 }
788 EXPORT_SYMBOL(__netdev_alloc_skb);
789 
790 /**
791  *	napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
792  *	@napi: napi instance this buffer was allocated for
793  *	@len: length to allocate
794  *
795  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
796  *	attempt to allocate the head from a special reserved region used
797  *	only for NAPI Rx allocation.  By doing this we can save several
798  *	CPU cycles by avoiding having to disable and re-enable IRQs.
799  *
800  *	%NULL is returned if there is no free memory.
801  */
802 struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
803 {
804 	gfp_t gfp_mask = GFP_ATOMIC | __GFP_NOWARN;
805 	struct napi_alloc_cache *nc;
806 	struct sk_buff *skb;
807 	bool pfmemalloc;
808 	void *data;
809 
810 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
811 	len += NET_SKB_PAD + NET_IP_ALIGN;
812 
813 	/* If requested length is either too small or too big,
814 	 * we use kmalloc() for skb->head allocation.
815 	 * When the small frag allocator is available, prefer it over kmalloc
816 	 * for small fragments
817 	 */
818 	if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) ||
819 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
820 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
821 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
822 				  NUMA_NO_NODE);
823 		if (!skb)
824 			goto skb_fail;
825 		goto skb_success;
826 	}
827 
828 	if (sk_memalloc_socks())
829 		gfp_mask |= __GFP_MEMALLOC;
830 
831 	local_lock_nested_bh(&napi_alloc_cache.bh_lock);
832 	nc = this_cpu_ptr(&napi_alloc_cache);
833 	if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
834 		/* we are artificially inflating the allocation size, but
835 		 * that is not as bad as it may look like, as:
836 		 * - 'len' less than GRO_MAX_HEAD makes little sense
837 		 * - On most systems, larger 'len' values lead to fragment
838 		 *   size above 512 bytes
839 		 * - kmalloc would use the kmalloc-1k slab for such values
840 		 * - Builds with smaller GRO_MAX_HEAD will very likely do
841 		 *   little networking, as that implies no WiFi and no
842 		 *   tunnels support, and 32 bits arches.
843 		 */
844 		len = SZ_1K;
845 
846 		data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
847 		pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
848 	} else {
849 		len = SKB_HEAD_ALIGN(len);
850 
851 		data = page_frag_alloc(&nc->page, len, gfp_mask);
852 		pfmemalloc = nc->page.pfmemalloc;
853 	}
854 	local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
855 
856 	if (unlikely(!data))
857 		return NULL;
858 
859 	skb = __napi_build_skb(data, len);
860 	if (unlikely(!skb)) {
861 		skb_free_frag(data);
862 		return NULL;
863 	}
864 
865 	if (pfmemalloc)
866 		skb->pfmemalloc = 1;
867 	skb->head_frag = 1;
868 
869 skb_success:
870 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
871 	skb->dev = napi->dev;
872 
873 skb_fail:
874 	return skb;
875 }
876 EXPORT_SYMBOL(napi_alloc_skb);
877 
878 void skb_add_rx_frag_netmem(struct sk_buff *skb, int i, netmem_ref netmem,
879 			    int off, int size, unsigned int truesize)
880 {
881 	DEBUG_NET_WARN_ON_ONCE(size > truesize);
882 
883 	skb_fill_netmem_desc(skb, i, netmem, off, size);
884 	skb->len += size;
885 	skb->data_len += size;
886 	skb->truesize += truesize;
887 }
888 EXPORT_SYMBOL(skb_add_rx_frag_netmem);
889 
890 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
891 			  unsigned int truesize)
892 {
893 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
894 
895 	DEBUG_NET_WARN_ON_ONCE(size > truesize);
896 
897 	skb_frag_size_add(frag, size);
898 	skb->len += size;
899 	skb->data_len += size;
900 	skb->truesize += truesize;
901 }
902 EXPORT_SYMBOL(skb_coalesce_rx_frag);
903 
904 static void skb_drop_list(struct sk_buff **listp)
905 {
906 	kfree_skb_list(*listp);
907 	*listp = NULL;
908 }
909 
910 static inline void skb_drop_fraglist(struct sk_buff *skb)
911 {
912 	skb_drop_list(&skb_shinfo(skb)->frag_list);
913 }
914 
915 static void skb_clone_fraglist(struct sk_buff *skb)
916 {
917 	struct sk_buff *list;
918 
919 	skb_walk_frags(skb, list)
920 		skb_get(list);
921 }
922 
923 static bool is_pp_page(struct page *page)
924 {
925 	return (page->pp_magic & ~0x3UL) == PP_SIGNATURE;
926 }
927 
928 int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
929 		    unsigned int headroom)
930 {
931 #if IS_ENABLED(CONFIG_PAGE_POOL)
932 	u32 size, truesize, len, max_head_size, off;
933 	struct sk_buff *skb = *pskb, *nskb;
934 	int err, i, head_off;
935 	void *data;
936 
937 	/* XDP does not support fraglist so we need to linearize
938 	 * the skb.
939 	 */
940 	if (skb_has_frag_list(skb))
941 		return -EOPNOTSUPP;
942 
943 	max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE - headroom);
944 	if (skb->len > max_head_size + MAX_SKB_FRAGS * PAGE_SIZE)
945 		return -ENOMEM;
946 
947 	size = min_t(u32, skb->len, max_head_size);
948 	truesize = SKB_HEAD_ALIGN(size) + headroom;
949 	data = page_pool_dev_alloc_va(pool, &truesize);
950 	if (!data)
951 		return -ENOMEM;
952 
953 	nskb = napi_build_skb(data, truesize);
954 	if (!nskb) {
955 		page_pool_free_va(pool, data, true);
956 		return -ENOMEM;
957 	}
958 
959 	skb_reserve(nskb, headroom);
960 	skb_copy_header(nskb, skb);
961 	skb_mark_for_recycle(nskb);
962 
963 	err = skb_copy_bits(skb, 0, nskb->data, size);
964 	if (err) {
965 		consume_skb(nskb);
966 		return err;
967 	}
968 	skb_put(nskb, size);
969 
970 	head_off = skb_headroom(nskb) - skb_headroom(skb);
971 	skb_headers_offset_update(nskb, head_off);
972 
973 	off = size;
974 	len = skb->len - off;
975 	for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
976 		struct page *page;
977 		u32 page_off;
978 
979 		size = min_t(u32, len, PAGE_SIZE);
980 		truesize = size;
981 
982 		page = page_pool_dev_alloc(pool, &page_off, &truesize);
983 		if (!page) {
984 			consume_skb(nskb);
985 			return -ENOMEM;
986 		}
987 
988 		skb_add_rx_frag(nskb, i, page, page_off, size, truesize);
989 		err = skb_copy_bits(skb, off, page_address(page) + page_off,
990 				    size);
991 		if (err) {
992 			consume_skb(nskb);
993 			return err;
994 		}
995 
996 		len -= size;
997 		off += size;
998 	}
999 
1000 	consume_skb(skb);
1001 	*pskb = nskb;
1002 
1003 	return 0;
1004 #else
1005 	return -EOPNOTSUPP;
1006 #endif
1007 }
1008 EXPORT_SYMBOL(skb_pp_cow_data);
1009 
1010 int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb,
1011 			 struct bpf_prog *prog)
1012 {
1013 	if (!prog->aux->xdp_has_frags)
1014 		return -EINVAL;
1015 
1016 	return skb_pp_cow_data(pool, pskb, XDP_PACKET_HEADROOM);
1017 }
1018 EXPORT_SYMBOL(skb_cow_data_for_xdp);
1019 
1020 #if IS_ENABLED(CONFIG_PAGE_POOL)
1021 bool napi_pp_put_page(netmem_ref netmem)
1022 {
1023 	struct page *page = netmem_to_page(netmem);
1024 
1025 	page = compound_head(page);
1026 
1027 	/* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
1028 	 * in order to preserve any existing bits, such as bit 0 for the
1029 	 * head page of compound page and bit 1 for pfmemalloc page, so
1030 	 * mask those bits for freeing side when doing below checking,
1031 	 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
1032 	 * to avoid recycling the pfmemalloc page.
1033 	 */
1034 	if (unlikely(!is_pp_page(page)))
1035 		return false;
1036 
1037 	page_pool_put_full_netmem(page->pp, page_to_netmem(page), false);
1038 
1039 	return true;
1040 }
1041 EXPORT_SYMBOL(napi_pp_put_page);
1042 #endif
1043 
1044 static bool skb_pp_recycle(struct sk_buff *skb, void *data)
1045 {
1046 	if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
1047 		return false;
1048 	return napi_pp_put_page(page_to_netmem(virt_to_page(data)));
1049 }
1050 
1051 /**
1052  * skb_pp_frag_ref() - Increase fragment references of a page pool aware skb
1053  * @skb:	page pool aware skb
1054  *
1055  * Increase the fragment reference count (pp_ref_count) of a skb. This is
1056  * intended to gain fragment references only for page pool aware skbs,
1057  * i.e. when skb->pp_recycle is true, and not for fragments in a
1058  * non-pp-recycling skb. It has a fallback to increase references on normal
1059  * pages, as page pool aware skbs may also have normal page fragments.
1060  */
1061 static int skb_pp_frag_ref(struct sk_buff *skb)
1062 {
1063 	struct skb_shared_info *shinfo;
1064 	struct page *head_page;
1065 	int i;
1066 
1067 	if (!skb->pp_recycle)
1068 		return -EINVAL;
1069 
1070 	shinfo = skb_shinfo(skb);
1071 
1072 	for (i = 0; i < shinfo->nr_frags; i++) {
1073 		head_page = compound_head(skb_frag_page(&shinfo->frags[i]));
1074 		if (likely(is_pp_page(head_page)))
1075 			page_pool_ref_page(head_page);
1076 		else
1077 			page_ref_inc(head_page);
1078 	}
1079 	return 0;
1080 }
1081 
1082 static void skb_kfree_head(void *head, unsigned int end_offset)
1083 {
1084 	if (end_offset == SKB_SMALL_HEAD_HEADROOM)
1085 		kmem_cache_free(net_hotdata.skb_small_head_cache, head);
1086 	else
1087 		kfree(head);
1088 }
1089 
1090 static void skb_free_head(struct sk_buff *skb)
1091 {
1092 	unsigned char *head = skb->head;
1093 
1094 	if (skb->head_frag) {
1095 		if (skb_pp_recycle(skb, head))
1096 			return;
1097 		skb_free_frag(head);
1098 	} else {
1099 		skb_kfree_head(head, skb_end_offset(skb));
1100 	}
1101 }
1102 
1103 static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason)
1104 {
1105 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1106 	int i;
1107 
1108 	if (!skb_data_unref(skb, shinfo))
1109 		goto exit;
1110 
1111 	if (skb_zcopy(skb)) {
1112 		bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
1113 
1114 		skb_zcopy_clear(skb, true);
1115 		if (skip_unref)
1116 			goto free_head;
1117 	}
1118 
1119 	for (i = 0; i < shinfo->nr_frags; i++)
1120 		__skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
1121 
1122 free_head:
1123 	if (shinfo->frag_list)
1124 		kfree_skb_list_reason(shinfo->frag_list, reason);
1125 
1126 	skb_free_head(skb);
1127 exit:
1128 	/* When we clone an SKB we copy the reycling bit. The pp_recycle
1129 	 * bit is only set on the head though, so in order to avoid races
1130 	 * while trying to recycle fragments on __skb_frag_unref() we need
1131 	 * to make one SKB responsible for triggering the recycle path.
1132 	 * So disable the recycling bit if an SKB is cloned and we have
1133 	 * additional references to the fragmented part of the SKB.
1134 	 * Eventually the last SKB will have the recycling bit set and it's
1135 	 * dataref set to 0, which will trigger the recycling
1136 	 */
1137 	skb->pp_recycle = 0;
1138 }
1139 
1140 /*
1141  *	Free an skbuff by memory without cleaning the state.
1142  */
1143 static void kfree_skbmem(struct sk_buff *skb)
1144 {
1145 	struct sk_buff_fclones *fclones;
1146 
1147 	switch (skb->fclone) {
1148 	case SKB_FCLONE_UNAVAILABLE:
1149 		kmem_cache_free(net_hotdata.skbuff_cache, skb);
1150 		return;
1151 
1152 	case SKB_FCLONE_ORIG:
1153 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
1154 
1155 		/* We usually free the clone (TX completion) before original skb
1156 		 * This test would have no chance to be true for the clone,
1157 		 * while here, branch prediction will be good.
1158 		 */
1159 		if (refcount_read(&fclones->fclone_ref) == 1)
1160 			goto fastpath;
1161 		break;
1162 
1163 	default: /* SKB_FCLONE_CLONE */
1164 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
1165 		break;
1166 	}
1167 	if (!refcount_dec_and_test(&fclones->fclone_ref))
1168 		return;
1169 fastpath:
1170 	kmem_cache_free(net_hotdata.skbuff_fclone_cache, fclones);
1171 }
1172 
1173 void skb_release_head_state(struct sk_buff *skb)
1174 {
1175 	skb_dst_drop(skb);
1176 	if (skb->destructor) {
1177 		DEBUG_NET_WARN_ON_ONCE(in_hardirq());
1178 		skb->destructor(skb);
1179 	}
1180 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1181 	nf_conntrack_put(skb_nfct(skb));
1182 #endif
1183 	skb_ext_put(skb);
1184 }
1185 
1186 /* Free everything but the sk_buff shell. */
1187 static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason)
1188 {
1189 	skb_release_head_state(skb);
1190 	if (likely(skb->head))
1191 		skb_release_data(skb, reason);
1192 }
1193 
1194 /**
1195  *	__kfree_skb - private function
1196  *	@skb: buffer
1197  *
1198  *	Free an sk_buff. Release anything attached to the buffer.
1199  *	Clean the state. This is an internal helper function. Users should
1200  *	always call kfree_skb
1201  */
1202 
1203 void __kfree_skb(struct sk_buff *skb)
1204 {
1205 	skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1206 	kfree_skbmem(skb);
1207 }
1208 EXPORT_SYMBOL(__kfree_skb);
1209 
1210 static __always_inline
1211 bool __sk_skb_reason_drop(struct sock *sk, struct sk_buff *skb,
1212 			  enum skb_drop_reason reason)
1213 {
1214 	if (unlikely(!skb_unref(skb)))
1215 		return false;
1216 
1217 	DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET ||
1218 			       u32_get_bits(reason,
1219 					    SKB_DROP_REASON_SUBSYS_MASK) >=
1220 				SKB_DROP_REASON_SUBSYS_NUM);
1221 
1222 	if (reason == SKB_CONSUMED)
1223 		trace_consume_skb(skb, __builtin_return_address(0));
1224 	else
1225 		trace_kfree_skb(skb, __builtin_return_address(0), reason, sk);
1226 	return true;
1227 }
1228 
1229 /**
1230  *	sk_skb_reason_drop - free an sk_buff with special reason
1231  *	@sk: the socket to receive @skb, or NULL if not applicable
1232  *	@skb: buffer to free
1233  *	@reason: reason why this skb is dropped
1234  *
1235  *	Drop a reference to the buffer and free it if the usage count has hit
1236  *	zero. Meanwhile, pass the receiving socket and drop reason to
1237  *	'kfree_skb' tracepoint.
1238  */
1239 void __fix_address
1240 sk_skb_reason_drop(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
1241 {
1242 	if (__sk_skb_reason_drop(sk, skb, reason))
1243 		__kfree_skb(skb);
1244 }
1245 EXPORT_SYMBOL(sk_skb_reason_drop);
1246 
1247 #define KFREE_SKB_BULK_SIZE	16
1248 
1249 struct skb_free_array {
1250 	unsigned int skb_count;
1251 	void *skb_array[KFREE_SKB_BULK_SIZE];
1252 };
1253 
1254 static void kfree_skb_add_bulk(struct sk_buff *skb,
1255 			       struct skb_free_array *sa,
1256 			       enum skb_drop_reason reason)
1257 {
1258 	/* if SKB is a clone, don't handle this case */
1259 	if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
1260 		__kfree_skb(skb);
1261 		return;
1262 	}
1263 
1264 	skb_release_all(skb, reason);
1265 	sa->skb_array[sa->skb_count++] = skb;
1266 
1267 	if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) {
1268 		kmem_cache_free_bulk(net_hotdata.skbuff_cache, KFREE_SKB_BULK_SIZE,
1269 				     sa->skb_array);
1270 		sa->skb_count = 0;
1271 	}
1272 }
1273 
1274 void __fix_address
1275 kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason)
1276 {
1277 	struct skb_free_array sa;
1278 
1279 	sa.skb_count = 0;
1280 
1281 	while (segs) {
1282 		struct sk_buff *next = segs->next;
1283 
1284 		if (__sk_skb_reason_drop(NULL, segs, reason)) {
1285 			skb_poison_list(segs);
1286 			kfree_skb_add_bulk(segs, &sa, reason);
1287 		}
1288 
1289 		segs = next;
1290 	}
1291 
1292 	if (sa.skb_count)
1293 		kmem_cache_free_bulk(net_hotdata.skbuff_cache, sa.skb_count, sa.skb_array);
1294 }
1295 EXPORT_SYMBOL(kfree_skb_list_reason);
1296 
1297 /* Dump skb information and contents.
1298  *
1299  * Must only be called from net_ratelimit()-ed paths.
1300  *
1301  * Dumps whole packets if full_pkt, only headers otherwise.
1302  */
1303 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
1304 {
1305 	struct skb_shared_info *sh = skb_shinfo(skb);
1306 	struct net_device *dev = skb->dev;
1307 	struct sock *sk = skb->sk;
1308 	struct sk_buff *list_skb;
1309 	bool has_mac, has_trans;
1310 	int headroom, tailroom;
1311 	int i, len, seg_len;
1312 
1313 	if (full_pkt)
1314 		len = skb->len;
1315 	else
1316 		len = min_t(int, skb->len, MAX_HEADER + 128);
1317 
1318 	headroom = skb_headroom(skb);
1319 	tailroom = skb_tailroom(skb);
1320 
1321 	has_mac = skb_mac_header_was_set(skb);
1322 	has_trans = skb_transport_header_was_set(skb);
1323 
1324 	printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
1325 	       "mac=(%d,%d) mac_len=%u net=(%d,%d) trans=%d\n"
1326 	       "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
1327 	       "csum(0x%x start=%u offset=%u ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
1328 	       "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n"
1329 	       "priority=0x%x mark=0x%x alloc_cpu=%u vlan_all=0x%x\n"
1330 	       "encapsulation=%d inner(proto=0x%04x, mac=%u, net=%u, trans=%u)\n",
1331 	       level, skb->len, headroom, skb_headlen(skb), tailroom,
1332 	       has_mac ? skb->mac_header : -1,
1333 	       has_mac ? skb_mac_header_len(skb) : -1,
1334 	       skb->mac_len,
1335 	       skb->network_header,
1336 	       has_trans ? skb_network_header_len(skb) : -1,
1337 	       has_trans ? skb->transport_header : -1,
1338 	       sh->tx_flags, sh->nr_frags,
1339 	       sh->gso_size, sh->gso_type, sh->gso_segs,
1340 	       skb->csum, skb->csum_start, skb->csum_offset, skb->ip_summed,
1341 	       skb->csum_complete_sw, skb->csum_valid, skb->csum_level,
1342 	       skb->hash, skb->sw_hash, skb->l4_hash,
1343 	       ntohs(skb->protocol), skb->pkt_type, skb->skb_iif,
1344 	       skb->priority, skb->mark, skb->alloc_cpu, skb->vlan_all,
1345 	       skb->encapsulation, skb->inner_protocol, skb->inner_mac_header,
1346 	       skb->inner_network_header, skb->inner_transport_header);
1347 
1348 	if (dev)
1349 		printk("%sdev name=%s feat=%pNF\n",
1350 		       level, dev->name, &dev->features);
1351 	if (sk)
1352 		printk("%ssk family=%hu type=%u proto=%u\n",
1353 		       level, sk->sk_family, sk->sk_type, sk->sk_protocol);
1354 
1355 	if (full_pkt && headroom)
1356 		print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
1357 			       16, 1, skb->head, headroom, false);
1358 
1359 	seg_len = min_t(int, skb_headlen(skb), len);
1360 	if (seg_len)
1361 		print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
1362 			       16, 1, skb->data, seg_len, false);
1363 	len -= seg_len;
1364 
1365 	if (full_pkt && tailroom)
1366 		print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
1367 			       16, 1, skb_tail_pointer(skb), tailroom, false);
1368 
1369 	for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
1370 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1371 		u32 p_off, p_len, copied;
1372 		struct page *p;
1373 		u8 *vaddr;
1374 
1375 		skb_frag_foreach_page(frag, skb_frag_off(frag),
1376 				      skb_frag_size(frag), p, p_off, p_len,
1377 				      copied) {
1378 			seg_len = min_t(int, p_len, len);
1379 			vaddr = kmap_atomic(p);
1380 			print_hex_dump(level, "skb frag:     ",
1381 				       DUMP_PREFIX_OFFSET,
1382 				       16, 1, vaddr + p_off, seg_len, false);
1383 			kunmap_atomic(vaddr);
1384 			len -= seg_len;
1385 			if (!len)
1386 				break;
1387 		}
1388 	}
1389 
1390 	if (full_pkt && skb_has_frag_list(skb)) {
1391 		printk("skb fraglist:\n");
1392 		skb_walk_frags(skb, list_skb)
1393 			skb_dump(level, list_skb, true);
1394 	}
1395 }
1396 EXPORT_SYMBOL(skb_dump);
1397 
1398 /**
1399  *	skb_tx_error - report an sk_buff xmit error
1400  *	@skb: buffer that triggered an error
1401  *
1402  *	Report xmit error if a device callback is tracking this skb.
1403  *	skb must be freed afterwards.
1404  */
1405 void skb_tx_error(struct sk_buff *skb)
1406 {
1407 	if (skb) {
1408 		skb_zcopy_downgrade_managed(skb);
1409 		skb_zcopy_clear(skb, true);
1410 	}
1411 }
1412 EXPORT_SYMBOL(skb_tx_error);
1413 
1414 #ifdef CONFIG_TRACEPOINTS
1415 /**
1416  *	consume_skb - free an skbuff
1417  *	@skb: buffer to free
1418  *
1419  *	Drop a ref to the buffer and free it if the usage count has hit zero
1420  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
1421  *	is being dropped after a failure and notes that
1422  */
1423 void consume_skb(struct sk_buff *skb)
1424 {
1425 	if (!skb_unref(skb))
1426 		return;
1427 
1428 	trace_consume_skb(skb, __builtin_return_address(0));
1429 	__kfree_skb(skb);
1430 }
1431 EXPORT_SYMBOL(consume_skb);
1432 #endif
1433 
1434 /**
1435  *	__consume_stateless_skb - free an skbuff, assuming it is stateless
1436  *	@skb: buffer to free
1437  *
1438  *	Alike consume_skb(), but this variant assumes that this is the last
1439  *	skb reference and all the head states have been already dropped
1440  */
1441 void __consume_stateless_skb(struct sk_buff *skb)
1442 {
1443 	trace_consume_skb(skb, __builtin_return_address(0));
1444 	skb_release_data(skb, SKB_CONSUMED);
1445 	kfree_skbmem(skb);
1446 }
1447 
1448 static void napi_skb_cache_put(struct sk_buff *skb)
1449 {
1450 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
1451 	u32 i;
1452 
1453 	if (!kasan_mempool_poison_object(skb))
1454 		return;
1455 
1456 	local_lock_nested_bh(&napi_alloc_cache.bh_lock);
1457 	nc->skb_cache[nc->skb_count++] = skb;
1458 
1459 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
1460 		for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
1461 			kasan_mempool_unpoison_object(nc->skb_cache[i],
1462 						kmem_cache_size(net_hotdata.skbuff_cache));
1463 
1464 		kmem_cache_free_bulk(net_hotdata.skbuff_cache, NAPI_SKB_CACHE_HALF,
1465 				     nc->skb_cache + NAPI_SKB_CACHE_HALF);
1466 		nc->skb_count = NAPI_SKB_CACHE_HALF;
1467 	}
1468 	local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
1469 }
1470 
1471 void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason)
1472 {
1473 	skb_release_all(skb, reason);
1474 	napi_skb_cache_put(skb);
1475 }
1476 
1477 void napi_skb_free_stolen_head(struct sk_buff *skb)
1478 {
1479 	if (unlikely(skb->slow_gro)) {
1480 		nf_reset_ct(skb);
1481 		skb_dst_drop(skb);
1482 		skb_ext_put(skb);
1483 		skb_orphan(skb);
1484 		skb->slow_gro = 0;
1485 	}
1486 	napi_skb_cache_put(skb);
1487 }
1488 
1489 void napi_consume_skb(struct sk_buff *skb, int budget)
1490 {
1491 	/* Zero budget indicate non-NAPI context called us, like netpoll */
1492 	if (unlikely(!budget)) {
1493 		dev_consume_skb_any(skb);
1494 		return;
1495 	}
1496 
1497 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
1498 
1499 	if (!skb_unref(skb))
1500 		return;
1501 
1502 	/* if reaching here SKB is ready to free */
1503 	trace_consume_skb(skb, __builtin_return_address(0));
1504 
1505 	/* if SKB is a clone, don't handle this case */
1506 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
1507 		__kfree_skb(skb);
1508 		return;
1509 	}
1510 
1511 	skb_release_all(skb, SKB_CONSUMED);
1512 	napi_skb_cache_put(skb);
1513 }
1514 EXPORT_SYMBOL(napi_consume_skb);
1515 
1516 /* Make sure a field is contained by headers group */
1517 #define CHECK_SKB_FIELD(field) \
1518 	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
1519 		     offsetof(struct sk_buff, headers.field));	\
1520 
1521 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1522 {
1523 	new->tstamp		= old->tstamp;
1524 	/* We do not copy old->sk */
1525 	new->dev		= old->dev;
1526 	memcpy(new->cb, old->cb, sizeof(old->cb));
1527 	skb_dst_copy(new, old);
1528 	__skb_ext_copy(new, old);
1529 	__nf_copy(new, old, false);
1530 
1531 	/* Note : this field could be in the headers group.
1532 	 * It is not yet because we do not want to have a 16 bit hole
1533 	 */
1534 	new->queue_mapping = old->queue_mapping;
1535 
1536 	memcpy(&new->headers, &old->headers, sizeof(new->headers));
1537 	CHECK_SKB_FIELD(protocol);
1538 	CHECK_SKB_FIELD(csum);
1539 	CHECK_SKB_FIELD(hash);
1540 	CHECK_SKB_FIELD(priority);
1541 	CHECK_SKB_FIELD(skb_iif);
1542 	CHECK_SKB_FIELD(vlan_proto);
1543 	CHECK_SKB_FIELD(vlan_tci);
1544 	CHECK_SKB_FIELD(transport_header);
1545 	CHECK_SKB_FIELD(network_header);
1546 	CHECK_SKB_FIELD(mac_header);
1547 	CHECK_SKB_FIELD(inner_protocol);
1548 	CHECK_SKB_FIELD(inner_transport_header);
1549 	CHECK_SKB_FIELD(inner_network_header);
1550 	CHECK_SKB_FIELD(inner_mac_header);
1551 	CHECK_SKB_FIELD(mark);
1552 #ifdef CONFIG_NETWORK_SECMARK
1553 	CHECK_SKB_FIELD(secmark);
1554 #endif
1555 #ifdef CONFIG_NET_RX_BUSY_POLL
1556 	CHECK_SKB_FIELD(napi_id);
1557 #endif
1558 	CHECK_SKB_FIELD(alloc_cpu);
1559 #ifdef CONFIG_XPS
1560 	CHECK_SKB_FIELD(sender_cpu);
1561 #endif
1562 #ifdef CONFIG_NET_SCHED
1563 	CHECK_SKB_FIELD(tc_index);
1564 #endif
1565 
1566 }
1567 
1568 /*
1569  * You should not add any new code to this function.  Add it to
1570  * __copy_skb_header above instead.
1571  */
1572 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
1573 {
1574 #define C(x) n->x = skb->x
1575 
1576 	n->next = n->prev = NULL;
1577 	n->sk = NULL;
1578 	__copy_skb_header(n, skb);
1579 
1580 	C(len);
1581 	C(data_len);
1582 	C(mac_len);
1583 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
1584 	n->cloned = 1;
1585 	n->nohdr = 0;
1586 	n->peeked = 0;
1587 	C(pfmemalloc);
1588 	C(pp_recycle);
1589 	n->destructor = NULL;
1590 	C(tail);
1591 	C(end);
1592 	C(head);
1593 	C(head_frag);
1594 	C(data);
1595 	C(truesize);
1596 	refcount_set(&n->users, 1);
1597 
1598 	atomic_inc(&(skb_shinfo(skb)->dataref));
1599 	skb->cloned = 1;
1600 
1601 	return n;
1602 #undef C
1603 }
1604 
1605 /**
1606  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1607  * @first: first sk_buff of the msg
1608  */
1609 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1610 {
1611 	struct sk_buff *n;
1612 
1613 	n = alloc_skb(0, GFP_ATOMIC);
1614 	if (!n)
1615 		return NULL;
1616 
1617 	n->len = first->len;
1618 	n->data_len = first->len;
1619 	n->truesize = first->truesize;
1620 
1621 	skb_shinfo(n)->frag_list = first;
1622 
1623 	__copy_skb_header(n, first);
1624 	n->destructor = NULL;
1625 
1626 	return n;
1627 }
1628 EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1629 
1630 /**
1631  *	skb_morph	-	morph one skb into another
1632  *	@dst: the skb to receive the contents
1633  *	@src: the skb to supply the contents
1634  *
1635  *	This is identical to skb_clone except that the target skb is
1636  *	supplied by the user.
1637  *
1638  *	The target skb is returned upon exit.
1639  */
1640 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1641 {
1642 	skb_release_all(dst, SKB_CONSUMED);
1643 	return __skb_clone(dst, src);
1644 }
1645 EXPORT_SYMBOL_GPL(skb_morph);
1646 
1647 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1648 {
1649 	unsigned long max_pg, num_pg, new_pg, old_pg, rlim;
1650 	struct user_struct *user;
1651 
1652 	if (capable(CAP_IPC_LOCK) || !size)
1653 		return 0;
1654 
1655 	rlim = rlimit(RLIMIT_MEMLOCK);
1656 	if (rlim == RLIM_INFINITY)
1657 		return 0;
1658 
1659 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
1660 	max_pg = rlim >> PAGE_SHIFT;
1661 	user = mmp->user ? : current_user();
1662 
1663 	old_pg = atomic_long_read(&user->locked_vm);
1664 	do {
1665 		new_pg = old_pg + num_pg;
1666 		if (new_pg > max_pg)
1667 			return -ENOBUFS;
1668 	} while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg));
1669 
1670 	if (!mmp->user) {
1671 		mmp->user = get_uid(user);
1672 		mmp->num_pg = num_pg;
1673 	} else {
1674 		mmp->num_pg += num_pg;
1675 	}
1676 
1677 	return 0;
1678 }
1679 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1680 
1681 void mm_unaccount_pinned_pages(struct mmpin *mmp)
1682 {
1683 	if (mmp->user) {
1684 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1685 		free_uid(mmp->user);
1686 	}
1687 }
1688 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1689 
1690 static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
1691 {
1692 	struct ubuf_info_msgzc *uarg;
1693 	struct sk_buff *skb;
1694 
1695 	WARN_ON_ONCE(!in_task());
1696 
1697 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
1698 	if (!skb)
1699 		return NULL;
1700 
1701 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1702 	uarg = (void *)skb->cb;
1703 	uarg->mmp.user = NULL;
1704 
1705 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
1706 		kfree_skb(skb);
1707 		return NULL;
1708 	}
1709 
1710 	uarg->ubuf.ops = &msg_zerocopy_ubuf_ops;
1711 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1712 	uarg->len = 1;
1713 	uarg->bytelen = size;
1714 	uarg->zerocopy = 1;
1715 	uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
1716 	refcount_set(&uarg->ubuf.refcnt, 1);
1717 	sock_hold(sk);
1718 
1719 	return &uarg->ubuf;
1720 }
1721 
1722 static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg)
1723 {
1724 	return container_of((void *)uarg, struct sk_buff, cb);
1725 }
1726 
1727 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1728 				       struct ubuf_info *uarg)
1729 {
1730 	if (uarg) {
1731 		struct ubuf_info_msgzc *uarg_zc;
1732 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
1733 		u32 bytelen, next;
1734 
1735 		/* there might be non MSG_ZEROCOPY users */
1736 		if (uarg->ops != &msg_zerocopy_ubuf_ops)
1737 			return NULL;
1738 
1739 		/* realloc only when socket is locked (TCP, UDP cork),
1740 		 * so uarg->len and sk_zckey access is serialized
1741 		 */
1742 		if (!sock_owned_by_user(sk)) {
1743 			WARN_ON_ONCE(1);
1744 			return NULL;
1745 		}
1746 
1747 		uarg_zc = uarg_to_msgzc(uarg);
1748 		bytelen = uarg_zc->bytelen + size;
1749 		if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1750 			/* TCP can create new skb to attach new uarg */
1751 			if (sk->sk_type == SOCK_STREAM)
1752 				goto new_alloc;
1753 			return NULL;
1754 		}
1755 
1756 		next = (u32)atomic_read(&sk->sk_zckey);
1757 		if ((u32)(uarg_zc->id + uarg_zc->len) == next) {
1758 			if (mm_account_pinned_pages(&uarg_zc->mmp, size))
1759 				return NULL;
1760 			uarg_zc->len++;
1761 			uarg_zc->bytelen = bytelen;
1762 			atomic_set(&sk->sk_zckey, ++next);
1763 
1764 			/* no extra ref when appending to datagram (MSG_MORE) */
1765 			if (sk->sk_type == SOCK_STREAM)
1766 				net_zcopy_get(uarg);
1767 
1768 			return uarg;
1769 		}
1770 	}
1771 
1772 new_alloc:
1773 	return msg_zerocopy_alloc(sk, size);
1774 }
1775 EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
1776 
1777 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1778 {
1779 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1780 	u32 old_lo, old_hi;
1781 	u64 sum_len;
1782 
1783 	old_lo = serr->ee.ee_info;
1784 	old_hi = serr->ee.ee_data;
1785 	sum_len = old_hi - old_lo + 1ULL + len;
1786 
1787 	if (sum_len >= (1ULL << 32))
1788 		return false;
1789 
1790 	if (lo != old_hi + 1)
1791 		return false;
1792 
1793 	serr->ee.ee_data += len;
1794 	return true;
1795 }
1796 
1797 static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg)
1798 {
1799 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1800 	struct sock_exterr_skb *serr;
1801 	struct sock *sk = skb->sk;
1802 	struct sk_buff_head *q;
1803 	unsigned long flags;
1804 	bool is_zerocopy;
1805 	u32 lo, hi;
1806 	u16 len;
1807 
1808 	mm_unaccount_pinned_pages(&uarg->mmp);
1809 
1810 	/* if !len, there was only 1 call, and it was aborted
1811 	 * so do not queue a completion notification
1812 	 */
1813 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1814 		goto release;
1815 
1816 	len = uarg->len;
1817 	lo = uarg->id;
1818 	hi = uarg->id + len - 1;
1819 	is_zerocopy = uarg->zerocopy;
1820 
1821 	serr = SKB_EXT_ERR(skb);
1822 	memset(serr, 0, sizeof(*serr));
1823 	serr->ee.ee_errno = 0;
1824 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1825 	serr->ee.ee_data = hi;
1826 	serr->ee.ee_info = lo;
1827 	if (!is_zerocopy)
1828 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1829 
1830 	q = &sk->sk_error_queue;
1831 	spin_lock_irqsave(&q->lock, flags);
1832 	tail = skb_peek_tail(q);
1833 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1834 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
1835 		__skb_queue_tail(q, skb);
1836 		skb = NULL;
1837 	}
1838 	spin_unlock_irqrestore(&q->lock, flags);
1839 
1840 	sk_error_report(sk);
1841 
1842 release:
1843 	consume_skb(skb);
1844 	sock_put(sk);
1845 }
1846 
1847 static void msg_zerocopy_complete(struct sk_buff *skb, struct ubuf_info *uarg,
1848 				  bool success)
1849 {
1850 	struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg);
1851 
1852 	uarg_zc->zerocopy = uarg_zc->zerocopy & success;
1853 
1854 	if (refcount_dec_and_test(&uarg->refcnt))
1855 		__msg_zerocopy_callback(uarg_zc);
1856 }
1857 
1858 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1859 {
1860 	struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk;
1861 
1862 	atomic_dec(&sk->sk_zckey);
1863 	uarg_to_msgzc(uarg)->len--;
1864 
1865 	if (have_uref)
1866 		msg_zerocopy_complete(NULL, uarg, true);
1867 }
1868 EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
1869 
1870 const struct ubuf_info_ops msg_zerocopy_ubuf_ops = {
1871 	.complete = msg_zerocopy_complete,
1872 };
1873 EXPORT_SYMBOL_GPL(msg_zerocopy_ubuf_ops);
1874 
1875 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1876 			     struct msghdr *msg, int len,
1877 			     struct ubuf_info *uarg)
1878 {
1879 	int err, orig_len = skb->len;
1880 
1881 	if (uarg->ops->link_skb) {
1882 		err = uarg->ops->link_skb(skb, uarg);
1883 		if (err)
1884 			return err;
1885 	} else {
1886 		struct ubuf_info *orig_uarg = skb_zcopy(skb);
1887 
1888 		/* An skb can only point to one uarg. This edge case happens
1889 		 * when TCP appends to an skb, but zerocopy_realloc triggered
1890 		 * a new alloc.
1891 		 */
1892 		if (orig_uarg && uarg != orig_uarg)
1893 			return -EEXIST;
1894 	}
1895 
1896 	err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len);
1897 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1898 		struct sock *save_sk = skb->sk;
1899 
1900 		/* Streams do not free skb on error. Reset to prev state. */
1901 		iov_iter_revert(&msg->msg_iter, skb->len - orig_len);
1902 		skb->sk = sk;
1903 		___pskb_trim(skb, orig_len);
1904 		skb->sk = save_sk;
1905 		return err;
1906 	}
1907 
1908 	skb_zcopy_set(skb, uarg, NULL);
1909 	return skb->len - orig_len;
1910 }
1911 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1912 
1913 void __skb_zcopy_downgrade_managed(struct sk_buff *skb)
1914 {
1915 	int i;
1916 
1917 	skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS;
1918 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1919 		skb_frag_ref(skb, i);
1920 }
1921 EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed);
1922 
1923 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1924 			      gfp_t gfp_mask)
1925 {
1926 	if (skb_zcopy(orig)) {
1927 		if (skb_zcopy(nskb)) {
1928 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1929 			if (!gfp_mask) {
1930 				WARN_ON_ONCE(1);
1931 				return -ENOMEM;
1932 			}
1933 			if (skb_uarg(nskb) == skb_uarg(orig))
1934 				return 0;
1935 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1936 				return -EIO;
1937 		}
1938 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1939 	}
1940 	return 0;
1941 }
1942 
1943 /**
1944  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
1945  *	@skb: the skb to modify
1946  *	@gfp_mask: allocation priority
1947  *
1948  *	This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
1949  *	It will copy all frags into kernel and drop the reference
1950  *	to userspace pages.
1951  *
1952  *	If this function is called from an interrupt gfp_mask() must be
1953  *	%GFP_ATOMIC.
1954  *
1955  *	Returns 0 on success or a negative error code on failure
1956  *	to allocate kernel memory to copy to.
1957  */
1958 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1959 {
1960 	int num_frags = skb_shinfo(skb)->nr_frags;
1961 	struct page *page, *head = NULL;
1962 	int i, order, psize, new_frags;
1963 	u32 d_off;
1964 
1965 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1966 		return -EINVAL;
1967 
1968 	if (!num_frags)
1969 		goto release;
1970 
1971 	/* We might have to allocate high order pages, so compute what minimum
1972 	 * page order is needed.
1973 	 */
1974 	order = 0;
1975 	while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb))
1976 		order++;
1977 	psize = (PAGE_SIZE << order);
1978 
1979 	new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order);
1980 	for (i = 0; i < new_frags; i++) {
1981 		page = alloc_pages(gfp_mask | __GFP_COMP, order);
1982 		if (!page) {
1983 			while (head) {
1984 				struct page *next = (struct page *)page_private(head);
1985 				put_page(head);
1986 				head = next;
1987 			}
1988 			return -ENOMEM;
1989 		}
1990 		set_page_private(page, (unsigned long)head);
1991 		head = page;
1992 	}
1993 
1994 	page = head;
1995 	d_off = 0;
1996 	for (i = 0; i < num_frags; i++) {
1997 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1998 		u32 p_off, p_len, copied;
1999 		struct page *p;
2000 		u8 *vaddr;
2001 
2002 		skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
2003 				      p, p_off, p_len, copied) {
2004 			u32 copy, done = 0;
2005 			vaddr = kmap_atomic(p);
2006 
2007 			while (done < p_len) {
2008 				if (d_off == psize) {
2009 					d_off = 0;
2010 					page = (struct page *)page_private(page);
2011 				}
2012 				copy = min_t(u32, psize - d_off, p_len - done);
2013 				memcpy(page_address(page) + d_off,
2014 				       vaddr + p_off + done, copy);
2015 				done += copy;
2016 				d_off += copy;
2017 			}
2018 			kunmap_atomic(vaddr);
2019 		}
2020 	}
2021 
2022 	/* skb frags release userspace buffers */
2023 	for (i = 0; i < num_frags; i++)
2024 		skb_frag_unref(skb, i);
2025 
2026 	/* skb frags point to kernel buffers */
2027 	for (i = 0; i < new_frags - 1; i++) {
2028 		__skb_fill_netmem_desc(skb, i, page_to_netmem(head), 0, psize);
2029 		head = (struct page *)page_private(head);
2030 	}
2031 	__skb_fill_netmem_desc(skb, new_frags - 1, page_to_netmem(head), 0,
2032 			       d_off);
2033 	skb_shinfo(skb)->nr_frags = new_frags;
2034 
2035 release:
2036 	skb_zcopy_clear(skb, false);
2037 	return 0;
2038 }
2039 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
2040 
2041 /**
2042  *	skb_clone	-	duplicate an sk_buff
2043  *	@skb: buffer to clone
2044  *	@gfp_mask: allocation priority
2045  *
2046  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
2047  *	copies share the same packet data but not structure. The new
2048  *	buffer has a reference count of 1. If the allocation fails the
2049  *	function returns %NULL otherwise the new buffer is returned.
2050  *
2051  *	If this function is called from an interrupt gfp_mask() must be
2052  *	%GFP_ATOMIC.
2053  */
2054 
2055 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
2056 {
2057 	struct sk_buff_fclones *fclones = container_of(skb,
2058 						       struct sk_buff_fclones,
2059 						       skb1);
2060 	struct sk_buff *n;
2061 
2062 	if (skb_orphan_frags(skb, gfp_mask))
2063 		return NULL;
2064 
2065 	if (skb->fclone == SKB_FCLONE_ORIG &&
2066 	    refcount_read(&fclones->fclone_ref) == 1) {
2067 		n = &fclones->skb2;
2068 		refcount_set(&fclones->fclone_ref, 2);
2069 		n->fclone = SKB_FCLONE_CLONE;
2070 	} else {
2071 		if (skb_pfmemalloc(skb))
2072 			gfp_mask |= __GFP_MEMALLOC;
2073 
2074 		n = kmem_cache_alloc(net_hotdata.skbuff_cache, gfp_mask);
2075 		if (!n)
2076 			return NULL;
2077 
2078 		n->fclone = SKB_FCLONE_UNAVAILABLE;
2079 	}
2080 
2081 	return __skb_clone(n, skb);
2082 }
2083 EXPORT_SYMBOL(skb_clone);
2084 
2085 void skb_headers_offset_update(struct sk_buff *skb, int off)
2086 {
2087 	/* Only adjust this if it actually is csum_start rather than csum */
2088 	if (skb->ip_summed == CHECKSUM_PARTIAL)
2089 		skb->csum_start += off;
2090 	/* {transport,network,mac}_header and tail are relative to skb->head */
2091 	skb->transport_header += off;
2092 	skb->network_header   += off;
2093 	if (skb_mac_header_was_set(skb))
2094 		skb->mac_header += off;
2095 	skb->inner_transport_header += off;
2096 	skb->inner_network_header += off;
2097 	skb->inner_mac_header += off;
2098 }
2099 EXPORT_SYMBOL(skb_headers_offset_update);
2100 
2101 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
2102 {
2103 	__copy_skb_header(new, old);
2104 
2105 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
2106 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
2107 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
2108 }
2109 EXPORT_SYMBOL(skb_copy_header);
2110 
2111 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
2112 {
2113 	if (skb_pfmemalloc(skb))
2114 		return SKB_ALLOC_RX;
2115 	return 0;
2116 }
2117 
2118 /**
2119  *	skb_copy	-	create private copy of an sk_buff
2120  *	@skb: buffer to copy
2121  *	@gfp_mask: allocation priority
2122  *
2123  *	Make a copy of both an &sk_buff and its data. This is used when the
2124  *	caller wishes to modify the data and needs a private copy of the
2125  *	data to alter. Returns %NULL on failure or the pointer to the buffer
2126  *	on success. The returned buffer has a reference count of 1.
2127  *
2128  *	As by-product this function converts non-linear &sk_buff to linear
2129  *	one, so that &sk_buff becomes completely private and caller is allowed
2130  *	to modify all the data of returned buffer. This means that this
2131  *	function is not recommended for use in circumstances when only
2132  *	header is going to be modified. Use pskb_copy() instead.
2133  */
2134 
2135 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
2136 {
2137 	struct sk_buff *n;
2138 	unsigned int size;
2139 	int headerlen;
2140 
2141 	if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
2142 		return NULL;
2143 
2144 	headerlen = skb_headroom(skb);
2145 	size = skb_end_offset(skb) + skb->data_len;
2146 	n = __alloc_skb(size, gfp_mask,
2147 			skb_alloc_rx_flag(skb), NUMA_NO_NODE);
2148 	if (!n)
2149 		return NULL;
2150 
2151 	/* Set the data pointer */
2152 	skb_reserve(n, headerlen);
2153 	/* Set the tail pointer and length */
2154 	skb_put(n, skb->len);
2155 
2156 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
2157 
2158 	skb_copy_header(n, skb);
2159 	return n;
2160 }
2161 EXPORT_SYMBOL(skb_copy);
2162 
2163 /**
2164  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
2165  *	@skb: buffer to copy
2166  *	@headroom: headroom of new skb
2167  *	@gfp_mask: allocation priority
2168  *	@fclone: if true allocate the copy of the skb from the fclone
2169  *	cache instead of the head cache; it is recommended to set this
2170  *	to true for the cases where the copy will likely be cloned
2171  *
2172  *	Make a copy of both an &sk_buff and part of its data, located
2173  *	in header. Fragmented data remain shared. This is used when
2174  *	the caller wishes to modify only header of &sk_buff and needs
2175  *	private copy of the header to alter. Returns %NULL on failure
2176  *	or the pointer to the buffer on success.
2177  *	The returned buffer has a reference count of 1.
2178  */
2179 
2180 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
2181 				   gfp_t gfp_mask, bool fclone)
2182 {
2183 	unsigned int size = skb_headlen(skb) + headroom;
2184 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
2185 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
2186 
2187 	if (!n)
2188 		goto out;
2189 
2190 	/* Set the data pointer */
2191 	skb_reserve(n, headroom);
2192 	/* Set the tail pointer and length */
2193 	skb_put(n, skb_headlen(skb));
2194 	/* Copy the bytes */
2195 	skb_copy_from_linear_data(skb, n->data, n->len);
2196 
2197 	n->truesize += skb->data_len;
2198 	n->data_len  = skb->data_len;
2199 	n->len	     = skb->len;
2200 
2201 	if (skb_shinfo(skb)->nr_frags) {
2202 		int i;
2203 
2204 		if (skb_orphan_frags(skb, gfp_mask) ||
2205 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
2206 			kfree_skb(n);
2207 			n = NULL;
2208 			goto out;
2209 		}
2210 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2211 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
2212 			skb_frag_ref(skb, i);
2213 		}
2214 		skb_shinfo(n)->nr_frags = i;
2215 	}
2216 
2217 	if (skb_has_frag_list(skb)) {
2218 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
2219 		skb_clone_fraglist(n);
2220 	}
2221 
2222 	skb_copy_header(n, skb);
2223 out:
2224 	return n;
2225 }
2226 EXPORT_SYMBOL(__pskb_copy_fclone);
2227 
2228 /**
2229  *	pskb_expand_head - reallocate header of &sk_buff
2230  *	@skb: buffer to reallocate
2231  *	@nhead: room to add at head
2232  *	@ntail: room to add at tail
2233  *	@gfp_mask: allocation priority
2234  *
2235  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
2236  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
2237  *	reference count of 1. Returns zero in the case of success or error,
2238  *	if expansion failed. In the last case, &sk_buff is not changed.
2239  *
2240  *	All the pointers pointing into skb header may change and must be
2241  *	reloaded after call to this function.
2242  */
2243 
2244 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
2245 		     gfp_t gfp_mask)
2246 {
2247 	unsigned int osize = skb_end_offset(skb);
2248 	unsigned int size = osize + nhead + ntail;
2249 	long off;
2250 	u8 *data;
2251 	int i;
2252 
2253 	BUG_ON(nhead < 0);
2254 
2255 	BUG_ON(skb_shared(skb));
2256 
2257 	skb_zcopy_downgrade_managed(skb);
2258 
2259 	if (skb_pfmemalloc(skb))
2260 		gfp_mask |= __GFP_MEMALLOC;
2261 
2262 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
2263 	if (!data)
2264 		goto nodata;
2265 	size = SKB_WITH_OVERHEAD(size);
2266 
2267 	/* Copy only real data... and, alas, header. This should be
2268 	 * optimized for the cases when header is void.
2269 	 */
2270 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
2271 
2272 	memcpy((struct skb_shared_info *)(data + size),
2273 	       skb_shinfo(skb),
2274 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
2275 
2276 	/*
2277 	 * if shinfo is shared we must drop the old head gracefully, but if it
2278 	 * is not we can just drop the old head and let the existing refcount
2279 	 * be since all we did is relocate the values
2280 	 */
2281 	if (skb_cloned(skb)) {
2282 		if (skb_orphan_frags(skb, gfp_mask))
2283 			goto nofrags;
2284 		if (skb_zcopy(skb))
2285 			refcount_inc(&skb_uarg(skb)->refcnt);
2286 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2287 			skb_frag_ref(skb, i);
2288 
2289 		if (skb_has_frag_list(skb))
2290 			skb_clone_fraglist(skb);
2291 
2292 		skb_release_data(skb, SKB_CONSUMED);
2293 	} else {
2294 		skb_free_head(skb);
2295 	}
2296 	off = (data + nhead) - skb->head;
2297 
2298 	skb->head     = data;
2299 	skb->head_frag = 0;
2300 	skb->data    += off;
2301 
2302 	skb_set_end_offset(skb, size);
2303 #ifdef NET_SKBUFF_DATA_USES_OFFSET
2304 	off           = nhead;
2305 #endif
2306 	skb->tail	      += off;
2307 	skb_headers_offset_update(skb, nhead);
2308 	skb->cloned   = 0;
2309 	skb->hdr_len  = 0;
2310 	skb->nohdr    = 0;
2311 	atomic_set(&skb_shinfo(skb)->dataref, 1);
2312 
2313 	skb_metadata_clear(skb);
2314 
2315 	/* It is not generally safe to change skb->truesize.
2316 	 * For the moment, we really care of rx path, or
2317 	 * when skb is orphaned (not attached to a socket).
2318 	 */
2319 	if (!skb->sk || skb->destructor == sock_edemux)
2320 		skb->truesize += size - osize;
2321 
2322 	return 0;
2323 
2324 nofrags:
2325 	skb_kfree_head(data, size);
2326 nodata:
2327 	return -ENOMEM;
2328 }
2329 EXPORT_SYMBOL(pskb_expand_head);
2330 
2331 /* Make private copy of skb with writable head and some headroom */
2332 
2333 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
2334 {
2335 	struct sk_buff *skb2;
2336 	int delta = headroom - skb_headroom(skb);
2337 
2338 	if (delta <= 0)
2339 		skb2 = pskb_copy(skb, GFP_ATOMIC);
2340 	else {
2341 		skb2 = skb_clone(skb, GFP_ATOMIC);
2342 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
2343 					     GFP_ATOMIC)) {
2344 			kfree_skb(skb2);
2345 			skb2 = NULL;
2346 		}
2347 	}
2348 	return skb2;
2349 }
2350 EXPORT_SYMBOL(skb_realloc_headroom);
2351 
2352 /* Note: We plan to rework this in linux-6.4 */
2353 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
2354 {
2355 	unsigned int saved_end_offset, saved_truesize;
2356 	struct skb_shared_info *shinfo;
2357 	int res;
2358 
2359 	saved_end_offset = skb_end_offset(skb);
2360 	saved_truesize = skb->truesize;
2361 
2362 	res = pskb_expand_head(skb, 0, 0, pri);
2363 	if (res)
2364 		return res;
2365 
2366 	skb->truesize = saved_truesize;
2367 
2368 	if (likely(skb_end_offset(skb) == saved_end_offset))
2369 		return 0;
2370 
2371 	/* We can not change skb->end if the original or new value
2372 	 * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head().
2373 	 */
2374 	if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM ||
2375 	    skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) {
2376 		/* We think this path should not be taken.
2377 		 * Add a temporary trace to warn us just in case.
2378 		 */
2379 		pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n",
2380 			    saved_end_offset, skb_end_offset(skb));
2381 		WARN_ON_ONCE(1);
2382 		return 0;
2383 	}
2384 
2385 	shinfo = skb_shinfo(skb);
2386 
2387 	/* We are about to change back skb->end,
2388 	 * we need to move skb_shinfo() to its new location.
2389 	 */
2390 	memmove(skb->head + saved_end_offset,
2391 		shinfo,
2392 		offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
2393 
2394 	skb_set_end_offset(skb, saved_end_offset);
2395 
2396 	return 0;
2397 }
2398 
2399 /**
2400  *	skb_expand_head - reallocate header of &sk_buff
2401  *	@skb: buffer to reallocate
2402  *	@headroom: needed headroom
2403  *
2404  *	Unlike skb_realloc_headroom, this one does not allocate a new skb
2405  *	if possible; copies skb->sk to new skb as needed
2406  *	and frees original skb in case of failures.
2407  *
2408  *	It expect increased headroom and generates warning otherwise.
2409  */
2410 
2411 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
2412 {
2413 	int delta = headroom - skb_headroom(skb);
2414 	int osize = skb_end_offset(skb);
2415 	struct sock *sk = skb->sk;
2416 
2417 	if (WARN_ONCE(delta <= 0,
2418 		      "%s is expecting an increase in the headroom", __func__))
2419 		return skb;
2420 
2421 	delta = SKB_DATA_ALIGN(delta);
2422 	/* pskb_expand_head() might crash, if skb is shared. */
2423 	if (skb_shared(skb) || !is_skb_wmem(skb)) {
2424 		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2425 
2426 		if (unlikely(!nskb))
2427 			goto fail;
2428 
2429 		if (sk)
2430 			skb_set_owner_w(nskb, sk);
2431 		consume_skb(skb);
2432 		skb = nskb;
2433 	}
2434 	if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
2435 		goto fail;
2436 
2437 	if (sk && is_skb_wmem(skb)) {
2438 		delta = skb_end_offset(skb) - osize;
2439 		refcount_add(delta, &sk->sk_wmem_alloc);
2440 		skb->truesize += delta;
2441 	}
2442 	return skb;
2443 
2444 fail:
2445 	kfree_skb(skb);
2446 	return NULL;
2447 }
2448 EXPORT_SYMBOL(skb_expand_head);
2449 
2450 /**
2451  *	skb_copy_expand	-	copy and expand sk_buff
2452  *	@skb: buffer to copy
2453  *	@newheadroom: new free bytes at head
2454  *	@newtailroom: new free bytes at tail
2455  *	@gfp_mask: allocation priority
2456  *
2457  *	Make a copy of both an &sk_buff and its data and while doing so
2458  *	allocate additional space.
2459  *
2460  *	This is used when the caller wishes to modify the data and needs a
2461  *	private copy of the data to alter as well as more space for new fields.
2462  *	Returns %NULL on failure or the pointer to the buffer
2463  *	on success. The returned buffer has a reference count of 1.
2464  *
2465  *	You must pass %GFP_ATOMIC as the allocation priority if this function
2466  *	is called from an interrupt.
2467  */
2468 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
2469 				int newheadroom, int newtailroom,
2470 				gfp_t gfp_mask)
2471 {
2472 	/*
2473 	 *	Allocate the copy buffer
2474 	 */
2475 	int head_copy_len, head_copy_off;
2476 	struct sk_buff *n;
2477 	int oldheadroom;
2478 
2479 	if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
2480 		return NULL;
2481 
2482 	oldheadroom = skb_headroom(skb);
2483 	n = __alloc_skb(newheadroom + skb->len + newtailroom,
2484 			gfp_mask, skb_alloc_rx_flag(skb),
2485 			NUMA_NO_NODE);
2486 	if (!n)
2487 		return NULL;
2488 
2489 	skb_reserve(n, newheadroom);
2490 
2491 	/* Set the tail pointer and length */
2492 	skb_put(n, skb->len);
2493 
2494 	head_copy_len = oldheadroom;
2495 	head_copy_off = 0;
2496 	if (newheadroom <= head_copy_len)
2497 		head_copy_len = newheadroom;
2498 	else
2499 		head_copy_off = newheadroom - head_copy_len;
2500 
2501 	/* Copy the linear header and data. */
2502 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
2503 			     skb->len + head_copy_len));
2504 
2505 	skb_copy_header(n, skb);
2506 
2507 	skb_headers_offset_update(n, newheadroom - oldheadroom);
2508 
2509 	return n;
2510 }
2511 EXPORT_SYMBOL(skb_copy_expand);
2512 
2513 /**
2514  *	__skb_pad		-	zero pad the tail of an skb
2515  *	@skb: buffer to pad
2516  *	@pad: space to pad
2517  *	@free_on_error: free buffer on error
2518  *
2519  *	Ensure that a buffer is followed by a padding area that is zero
2520  *	filled. Used by network drivers which may DMA or transfer data
2521  *	beyond the buffer end onto the wire.
2522  *
2523  *	May return error in out of memory cases. The skb is freed on error
2524  *	if @free_on_error is true.
2525  */
2526 
2527 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
2528 {
2529 	int err;
2530 	int ntail;
2531 
2532 	/* If the skbuff is non linear tailroom is always zero.. */
2533 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
2534 		memset(skb->data+skb->len, 0, pad);
2535 		return 0;
2536 	}
2537 
2538 	ntail = skb->data_len + pad - (skb->end - skb->tail);
2539 	if (likely(skb_cloned(skb) || ntail > 0)) {
2540 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
2541 		if (unlikely(err))
2542 			goto free_skb;
2543 	}
2544 
2545 	/* FIXME: The use of this function with non-linear skb's really needs
2546 	 * to be audited.
2547 	 */
2548 	err = skb_linearize(skb);
2549 	if (unlikely(err))
2550 		goto free_skb;
2551 
2552 	memset(skb->data + skb->len, 0, pad);
2553 	return 0;
2554 
2555 free_skb:
2556 	if (free_on_error)
2557 		kfree_skb(skb);
2558 	return err;
2559 }
2560 EXPORT_SYMBOL(__skb_pad);
2561 
2562 /**
2563  *	pskb_put - add data to the tail of a potentially fragmented buffer
2564  *	@skb: start of the buffer to use
2565  *	@tail: tail fragment of the buffer to use
2566  *	@len: amount of data to add
2567  *
2568  *	This function extends the used data area of the potentially
2569  *	fragmented buffer. @tail must be the last fragment of @skb -- or
2570  *	@skb itself. If this would exceed the total buffer size the kernel
2571  *	will panic. A pointer to the first byte of the extra data is
2572  *	returned.
2573  */
2574 
2575 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
2576 {
2577 	if (tail != skb) {
2578 		skb->data_len += len;
2579 		skb->len += len;
2580 	}
2581 	return skb_put(tail, len);
2582 }
2583 EXPORT_SYMBOL_GPL(pskb_put);
2584 
2585 /**
2586  *	skb_put - add data to a buffer
2587  *	@skb: buffer to use
2588  *	@len: amount of data to add
2589  *
2590  *	This function extends the used data area of the buffer. If this would
2591  *	exceed the total buffer size the kernel will panic. A pointer to the
2592  *	first byte of the extra data is returned.
2593  */
2594 void *skb_put(struct sk_buff *skb, unsigned int len)
2595 {
2596 	void *tmp = skb_tail_pointer(skb);
2597 	SKB_LINEAR_ASSERT(skb);
2598 	skb->tail += len;
2599 	skb->len  += len;
2600 	if (unlikely(skb->tail > skb->end))
2601 		skb_over_panic(skb, len, __builtin_return_address(0));
2602 	return tmp;
2603 }
2604 EXPORT_SYMBOL(skb_put);
2605 
2606 /**
2607  *	skb_push - add data to the start of a buffer
2608  *	@skb: buffer to use
2609  *	@len: amount of data to add
2610  *
2611  *	This function extends the used data area of the buffer at the buffer
2612  *	start. If this would exceed the total buffer headroom the kernel will
2613  *	panic. A pointer to the first byte of the extra data is returned.
2614  */
2615 void *skb_push(struct sk_buff *skb, unsigned int len)
2616 {
2617 	skb->data -= len;
2618 	skb->len  += len;
2619 	if (unlikely(skb->data < skb->head))
2620 		skb_under_panic(skb, len, __builtin_return_address(0));
2621 	return skb->data;
2622 }
2623 EXPORT_SYMBOL(skb_push);
2624 
2625 /**
2626  *	skb_pull - remove data from the start of a buffer
2627  *	@skb: buffer to use
2628  *	@len: amount of data to remove
2629  *
2630  *	This function removes data from the start of a buffer, returning
2631  *	the memory to the headroom. A pointer to the next data in the buffer
2632  *	is returned. Once the data has been pulled future pushes will overwrite
2633  *	the old data.
2634  */
2635 void *skb_pull(struct sk_buff *skb, unsigned int len)
2636 {
2637 	return skb_pull_inline(skb, len);
2638 }
2639 EXPORT_SYMBOL(skb_pull);
2640 
2641 /**
2642  *	skb_pull_data - remove data from the start of a buffer returning its
2643  *	original position.
2644  *	@skb: buffer to use
2645  *	@len: amount of data to remove
2646  *
2647  *	This function removes data from the start of a buffer, returning
2648  *	the memory to the headroom. A pointer to the original data in the buffer
2649  *	is returned after checking if there is enough data to pull. Once the
2650  *	data has been pulled future pushes will overwrite the old data.
2651  */
2652 void *skb_pull_data(struct sk_buff *skb, size_t len)
2653 {
2654 	void *data = skb->data;
2655 
2656 	if (skb->len < len)
2657 		return NULL;
2658 
2659 	skb_pull(skb, len);
2660 
2661 	return data;
2662 }
2663 EXPORT_SYMBOL(skb_pull_data);
2664 
2665 /**
2666  *	skb_trim - remove end from a buffer
2667  *	@skb: buffer to alter
2668  *	@len: new length
2669  *
2670  *	Cut the length of a buffer down by removing data from the tail. If
2671  *	the buffer is already under the length specified it is not modified.
2672  *	The skb must be linear.
2673  */
2674 void skb_trim(struct sk_buff *skb, unsigned int len)
2675 {
2676 	if (skb->len > len)
2677 		__skb_trim(skb, len);
2678 }
2679 EXPORT_SYMBOL(skb_trim);
2680 
2681 /* Trims skb to length len. It can change skb pointers.
2682  */
2683 
2684 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
2685 {
2686 	struct sk_buff **fragp;
2687 	struct sk_buff *frag;
2688 	int offset = skb_headlen(skb);
2689 	int nfrags = skb_shinfo(skb)->nr_frags;
2690 	int i;
2691 	int err;
2692 
2693 	if (skb_cloned(skb) &&
2694 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2695 		return err;
2696 
2697 	i = 0;
2698 	if (offset >= len)
2699 		goto drop_pages;
2700 
2701 	for (; i < nfrags; i++) {
2702 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2703 
2704 		if (end < len) {
2705 			offset = end;
2706 			continue;
2707 		}
2708 
2709 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
2710 
2711 drop_pages:
2712 		skb_shinfo(skb)->nr_frags = i;
2713 
2714 		for (; i < nfrags; i++)
2715 			skb_frag_unref(skb, i);
2716 
2717 		if (skb_has_frag_list(skb))
2718 			skb_drop_fraglist(skb);
2719 		goto done;
2720 	}
2721 
2722 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2723 	     fragp = &frag->next) {
2724 		int end = offset + frag->len;
2725 
2726 		if (skb_shared(frag)) {
2727 			struct sk_buff *nfrag;
2728 
2729 			nfrag = skb_clone(frag, GFP_ATOMIC);
2730 			if (unlikely(!nfrag))
2731 				return -ENOMEM;
2732 
2733 			nfrag->next = frag->next;
2734 			consume_skb(frag);
2735 			frag = nfrag;
2736 			*fragp = frag;
2737 		}
2738 
2739 		if (end < len) {
2740 			offset = end;
2741 			continue;
2742 		}
2743 
2744 		if (end > len &&
2745 		    unlikely((err = pskb_trim(frag, len - offset))))
2746 			return err;
2747 
2748 		if (frag->next)
2749 			skb_drop_list(&frag->next);
2750 		break;
2751 	}
2752 
2753 done:
2754 	if (len > skb_headlen(skb)) {
2755 		skb->data_len -= skb->len - len;
2756 		skb->len       = len;
2757 	} else {
2758 		skb->len       = len;
2759 		skb->data_len  = 0;
2760 		skb_set_tail_pointer(skb, len);
2761 	}
2762 
2763 	if (!skb->sk || skb->destructor == sock_edemux)
2764 		skb_condense(skb);
2765 	return 0;
2766 }
2767 EXPORT_SYMBOL(___pskb_trim);
2768 
2769 /* Note : use pskb_trim_rcsum() instead of calling this directly
2770  */
2771 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2772 {
2773 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
2774 		int delta = skb->len - len;
2775 
2776 		skb->csum = csum_block_sub(skb->csum,
2777 					   skb_checksum(skb, len, delta, 0),
2778 					   len);
2779 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2780 		int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2781 		int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2782 
2783 		if (offset + sizeof(__sum16) > hdlen)
2784 			return -EINVAL;
2785 	}
2786 	return __pskb_trim(skb, len);
2787 }
2788 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2789 
2790 /**
2791  *	__pskb_pull_tail - advance tail of skb header
2792  *	@skb: buffer to reallocate
2793  *	@delta: number of bytes to advance tail
2794  *
2795  *	The function makes a sense only on a fragmented &sk_buff,
2796  *	it expands header moving its tail forward and copying necessary
2797  *	data from fragmented part.
2798  *
2799  *	&sk_buff MUST have reference count of 1.
2800  *
2801  *	Returns %NULL (and &sk_buff does not change) if pull failed
2802  *	or value of new tail of skb in the case of success.
2803  *
2804  *	All the pointers pointing into skb header may change and must be
2805  *	reloaded after call to this function.
2806  */
2807 
2808 /* Moves tail of skb head forward, copying data from fragmented part,
2809  * when it is necessary.
2810  * 1. It may fail due to malloc failure.
2811  * 2. It may change skb pointers.
2812  *
2813  * It is pretty complicated. Luckily, it is called only in exceptional cases.
2814  */
2815 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
2816 {
2817 	/* If skb has not enough free space at tail, get new one
2818 	 * plus 128 bytes for future expansions. If we have enough
2819 	 * room at tail, reallocate without expansion only if skb is cloned.
2820 	 */
2821 	int i, k, eat = (skb->tail + delta) - skb->end;
2822 
2823 	if (eat > 0 || skb_cloned(skb)) {
2824 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2825 				     GFP_ATOMIC))
2826 			return NULL;
2827 	}
2828 
2829 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2830 			     skb_tail_pointer(skb), delta));
2831 
2832 	/* Optimization: no fragments, no reasons to preestimate
2833 	 * size of pulled pages. Superb.
2834 	 */
2835 	if (!skb_has_frag_list(skb))
2836 		goto pull_pages;
2837 
2838 	/* Estimate size of pulled pages. */
2839 	eat = delta;
2840 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2841 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2842 
2843 		if (size >= eat)
2844 			goto pull_pages;
2845 		eat -= size;
2846 	}
2847 
2848 	/* If we need update frag list, we are in troubles.
2849 	 * Certainly, it is possible to add an offset to skb data,
2850 	 * but taking into account that pulling is expected to
2851 	 * be very rare operation, it is worth to fight against
2852 	 * further bloating skb head and crucify ourselves here instead.
2853 	 * Pure masohism, indeed. 8)8)
2854 	 */
2855 	if (eat) {
2856 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2857 		struct sk_buff *clone = NULL;
2858 		struct sk_buff *insp = NULL;
2859 
2860 		do {
2861 			if (list->len <= eat) {
2862 				/* Eaten as whole. */
2863 				eat -= list->len;
2864 				list = list->next;
2865 				insp = list;
2866 			} else {
2867 				/* Eaten partially. */
2868 				if (skb_is_gso(skb) && !list->head_frag &&
2869 				    skb_headlen(list))
2870 					skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2871 
2872 				if (skb_shared(list)) {
2873 					/* Sucks! We need to fork list. :-( */
2874 					clone = skb_clone(list, GFP_ATOMIC);
2875 					if (!clone)
2876 						return NULL;
2877 					insp = list->next;
2878 					list = clone;
2879 				} else {
2880 					/* This may be pulled without
2881 					 * problems. */
2882 					insp = list;
2883 				}
2884 				if (!pskb_pull(list, eat)) {
2885 					kfree_skb(clone);
2886 					return NULL;
2887 				}
2888 				break;
2889 			}
2890 		} while (eat);
2891 
2892 		/* Free pulled out fragments. */
2893 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
2894 			skb_shinfo(skb)->frag_list = list->next;
2895 			consume_skb(list);
2896 		}
2897 		/* And insert new clone at head. */
2898 		if (clone) {
2899 			clone->next = list;
2900 			skb_shinfo(skb)->frag_list = clone;
2901 		}
2902 	}
2903 	/* Success! Now we may commit changes to skb data. */
2904 
2905 pull_pages:
2906 	eat = delta;
2907 	k = 0;
2908 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2909 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2910 
2911 		if (size <= eat) {
2912 			skb_frag_unref(skb, i);
2913 			eat -= size;
2914 		} else {
2915 			skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2916 
2917 			*frag = skb_shinfo(skb)->frags[i];
2918 			if (eat) {
2919 				skb_frag_off_add(frag, eat);
2920 				skb_frag_size_sub(frag, eat);
2921 				if (!i)
2922 					goto end;
2923 				eat = 0;
2924 			}
2925 			k++;
2926 		}
2927 	}
2928 	skb_shinfo(skb)->nr_frags = k;
2929 
2930 end:
2931 	skb->tail     += delta;
2932 	skb->data_len -= delta;
2933 
2934 	if (!skb->data_len)
2935 		skb_zcopy_clear(skb, false);
2936 
2937 	return skb_tail_pointer(skb);
2938 }
2939 EXPORT_SYMBOL(__pskb_pull_tail);
2940 
2941 /**
2942  *	skb_copy_bits - copy bits from skb to kernel buffer
2943  *	@skb: source skb
2944  *	@offset: offset in source
2945  *	@to: destination buffer
2946  *	@len: number of bytes to copy
2947  *
2948  *	Copy the specified number of bytes from the source skb to the
2949  *	destination buffer.
2950  *
2951  *	CAUTION ! :
2952  *		If its prototype is ever changed,
2953  *		check arch/{*}/net/{*}.S files,
2954  *		since it is called from BPF assembly code.
2955  */
2956 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2957 {
2958 	int start = skb_headlen(skb);
2959 	struct sk_buff *frag_iter;
2960 	int i, copy;
2961 
2962 	if (offset > (int)skb->len - len)
2963 		goto fault;
2964 
2965 	/* Copy header. */
2966 	if ((copy = start - offset) > 0) {
2967 		if (copy > len)
2968 			copy = len;
2969 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
2970 		if ((len -= copy) == 0)
2971 			return 0;
2972 		offset += copy;
2973 		to     += copy;
2974 	}
2975 
2976 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2977 		int end;
2978 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2979 
2980 		WARN_ON(start > offset + len);
2981 
2982 		end = start + skb_frag_size(f);
2983 		if ((copy = end - offset) > 0) {
2984 			u32 p_off, p_len, copied;
2985 			struct page *p;
2986 			u8 *vaddr;
2987 
2988 			if (copy > len)
2989 				copy = len;
2990 
2991 			skb_frag_foreach_page(f,
2992 					      skb_frag_off(f) + offset - start,
2993 					      copy, p, p_off, p_len, copied) {
2994 				vaddr = kmap_atomic(p);
2995 				memcpy(to + copied, vaddr + p_off, p_len);
2996 				kunmap_atomic(vaddr);
2997 			}
2998 
2999 			if ((len -= copy) == 0)
3000 				return 0;
3001 			offset += copy;
3002 			to     += copy;
3003 		}
3004 		start = end;
3005 	}
3006 
3007 	skb_walk_frags(skb, frag_iter) {
3008 		int end;
3009 
3010 		WARN_ON(start > offset + len);
3011 
3012 		end = start + frag_iter->len;
3013 		if ((copy = end - offset) > 0) {
3014 			if (copy > len)
3015 				copy = len;
3016 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
3017 				goto fault;
3018 			if ((len -= copy) == 0)
3019 				return 0;
3020 			offset += copy;
3021 			to     += copy;
3022 		}
3023 		start = end;
3024 	}
3025 
3026 	if (!len)
3027 		return 0;
3028 
3029 fault:
3030 	return -EFAULT;
3031 }
3032 EXPORT_SYMBOL(skb_copy_bits);
3033 
3034 /*
3035  * Callback from splice_to_pipe(), if we need to release some pages
3036  * at the end of the spd in case we error'ed out in filling the pipe.
3037  */
3038 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3039 {
3040 	put_page(spd->pages[i]);
3041 }
3042 
3043 static struct page *linear_to_page(struct page *page, unsigned int *len,
3044 				   unsigned int *offset,
3045 				   struct sock *sk)
3046 {
3047 	struct page_frag *pfrag = sk_page_frag(sk);
3048 
3049 	if (!sk_page_frag_refill(sk, pfrag))
3050 		return NULL;
3051 
3052 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
3053 
3054 	memcpy(page_address(pfrag->page) + pfrag->offset,
3055 	       page_address(page) + *offset, *len);
3056 	*offset = pfrag->offset;
3057 	pfrag->offset += *len;
3058 
3059 	return pfrag->page;
3060 }
3061 
3062 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
3063 			     struct page *page,
3064 			     unsigned int offset)
3065 {
3066 	return	spd->nr_pages &&
3067 		spd->pages[spd->nr_pages - 1] == page &&
3068 		(spd->partial[spd->nr_pages - 1].offset +
3069 		 spd->partial[spd->nr_pages - 1].len == offset);
3070 }
3071 
3072 /*
3073  * Fill page/offset/length into spd, if it can hold more pages.
3074  */
3075 static bool spd_fill_page(struct splice_pipe_desc *spd,
3076 			  struct pipe_inode_info *pipe, struct page *page,
3077 			  unsigned int *len, unsigned int offset,
3078 			  bool linear,
3079 			  struct sock *sk)
3080 {
3081 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
3082 		return true;
3083 
3084 	if (linear) {
3085 		page = linear_to_page(page, len, &offset, sk);
3086 		if (!page)
3087 			return true;
3088 	}
3089 	if (spd_can_coalesce(spd, page, offset)) {
3090 		spd->partial[spd->nr_pages - 1].len += *len;
3091 		return false;
3092 	}
3093 	get_page(page);
3094 	spd->pages[spd->nr_pages] = page;
3095 	spd->partial[spd->nr_pages].len = *len;
3096 	spd->partial[spd->nr_pages].offset = offset;
3097 	spd->nr_pages++;
3098 
3099 	return false;
3100 }
3101 
3102 static bool __splice_segment(struct page *page, unsigned int poff,
3103 			     unsigned int plen, unsigned int *off,
3104 			     unsigned int *len,
3105 			     struct splice_pipe_desc *spd, bool linear,
3106 			     struct sock *sk,
3107 			     struct pipe_inode_info *pipe)
3108 {
3109 	if (!*len)
3110 		return true;
3111 
3112 	/* skip this segment if already processed */
3113 	if (*off >= plen) {
3114 		*off -= plen;
3115 		return false;
3116 	}
3117 
3118 	/* ignore any bits we already processed */
3119 	poff += *off;
3120 	plen -= *off;
3121 	*off = 0;
3122 
3123 	do {
3124 		unsigned int flen = min(*len, plen);
3125 
3126 		if (spd_fill_page(spd, pipe, page, &flen, poff,
3127 				  linear, sk))
3128 			return true;
3129 		poff += flen;
3130 		plen -= flen;
3131 		*len -= flen;
3132 	} while (*len && plen);
3133 
3134 	return false;
3135 }
3136 
3137 /*
3138  * Map linear and fragment data from the skb to spd. It reports true if the
3139  * pipe is full or if we already spliced the requested length.
3140  */
3141 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
3142 			      unsigned int *offset, unsigned int *len,
3143 			      struct splice_pipe_desc *spd, struct sock *sk)
3144 {
3145 	int seg;
3146 	struct sk_buff *iter;
3147 
3148 	/* map the linear part :
3149 	 * If skb->head_frag is set, this 'linear' part is backed by a
3150 	 * fragment, and if the head is not shared with any clones then
3151 	 * we can avoid a copy since we own the head portion of this page.
3152 	 */
3153 	if (__splice_segment(virt_to_page(skb->data),
3154 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
3155 			     skb_headlen(skb),
3156 			     offset, len, spd,
3157 			     skb_head_is_locked(skb),
3158 			     sk, pipe))
3159 		return true;
3160 
3161 	/*
3162 	 * then map the fragments
3163 	 */
3164 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
3165 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
3166 
3167 		if (__splice_segment(skb_frag_page(f),
3168 				     skb_frag_off(f), skb_frag_size(f),
3169 				     offset, len, spd, false, sk, pipe))
3170 			return true;
3171 	}
3172 
3173 	skb_walk_frags(skb, iter) {
3174 		if (*offset >= iter->len) {
3175 			*offset -= iter->len;
3176 			continue;
3177 		}
3178 		/* __skb_splice_bits() only fails if the output has no room
3179 		 * left, so no point in going over the frag_list for the error
3180 		 * case.
3181 		 */
3182 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
3183 			return true;
3184 	}
3185 
3186 	return false;
3187 }
3188 
3189 /*
3190  * Map data from the skb to a pipe. Should handle both the linear part,
3191  * the fragments, and the frag list.
3192  */
3193 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
3194 		    struct pipe_inode_info *pipe, unsigned int tlen,
3195 		    unsigned int flags)
3196 {
3197 	struct partial_page partial[MAX_SKB_FRAGS];
3198 	struct page *pages[MAX_SKB_FRAGS];
3199 	struct splice_pipe_desc spd = {
3200 		.pages = pages,
3201 		.partial = partial,
3202 		.nr_pages_max = MAX_SKB_FRAGS,
3203 		.ops = &nosteal_pipe_buf_ops,
3204 		.spd_release = sock_spd_release,
3205 	};
3206 	int ret = 0;
3207 
3208 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
3209 
3210 	if (spd.nr_pages)
3211 		ret = splice_to_pipe(pipe, &spd);
3212 
3213 	return ret;
3214 }
3215 EXPORT_SYMBOL_GPL(skb_splice_bits);
3216 
3217 static int sendmsg_locked(struct sock *sk, struct msghdr *msg)
3218 {
3219 	struct socket *sock = sk->sk_socket;
3220 	size_t size = msg_data_left(msg);
3221 
3222 	if (!sock)
3223 		return -EINVAL;
3224 
3225 	if (!sock->ops->sendmsg_locked)
3226 		return sock_no_sendmsg_locked(sk, msg, size);
3227 
3228 	return sock->ops->sendmsg_locked(sk, msg, size);
3229 }
3230 
3231 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg)
3232 {
3233 	struct socket *sock = sk->sk_socket;
3234 
3235 	if (!sock)
3236 		return -EINVAL;
3237 	return sock_sendmsg(sock, msg);
3238 }
3239 
3240 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg);
3241 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
3242 			   int len, sendmsg_func sendmsg)
3243 {
3244 	unsigned int orig_len = len;
3245 	struct sk_buff *head = skb;
3246 	unsigned short fragidx;
3247 	int slen, ret;
3248 
3249 do_frag_list:
3250 
3251 	/* Deal with head data */
3252 	while (offset < skb_headlen(skb) && len) {
3253 		struct kvec kv;
3254 		struct msghdr msg;
3255 
3256 		slen = min_t(int, len, skb_headlen(skb) - offset);
3257 		kv.iov_base = skb->data + offset;
3258 		kv.iov_len = slen;
3259 		memset(&msg, 0, sizeof(msg));
3260 		msg.msg_flags = MSG_DONTWAIT;
3261 
3262 		iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen);
3263 		ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3264 				      sendmsg_unlocked, sk, &msg);
3265 		if (ret <= 0)
3266 			goto error;
3267 
3268 		offset += ret;
3269 		len -= ret;
3270 	}
3271 
3272 	/* All the data was skb head? */
3273 	if (!len)
3274 		goto out;
3275 
3276 	/* Make offset relative to start of frags */
3277 	offset -= skb_headlen(skb);
3278 
3279 	/* Find where we are in frag list */
3280 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
3281 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
3282 
3283 		if (offset < skb_frag_size(frag))
3284 			break;
3285 
3286 		offset -= skb_frag_size(frag);
3287 	}
3288 
3289 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
3290 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
3291 
3292 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
3293 
3294 		while (slen) {
3295 			struct bio_vec bvec;
3296 			struct msghdr msg = {
3297 				.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT,
3298 			};
3299 
3300 			bvec_set_page(&bvec, skb_frag_page(frag), slen,
3301 				      skb_frag_off(frag) + offset);
3302 			iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
3303 				      slen);
3304 
3305 			ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3306 					      sendmsg_unlocked, sk, &msg);
3307 			if (ret <= 0)
3308 				goto error;
3309 
3310 			len -= ret;
3311 			offset += ret;
3312 			slen -= ret;
3313 		}
3314 
3315 		offset = 0;
3316 	}
3317 
3318 	if (len) {
3319 		/* Process any frag lists */
3320 
3321 		if (skb == head) {
3322 			if (skb_has_frag_list(skb)) {
3323 				skb = skb_shinfo(skb)->frag_list;
3324 				goto do_frag_list;
3325 			}
3326 		} else if (skb->next) {
3327 			skb = skb->next;
3328 			goto do_frag_list;
3329 		}
3330 	}
3331 
3332 out:
3333 	return orig_len - len;
3334 
3335 error:
3336 	return orig_len == len ? ret : orig_len - len;
3337 }
3338 
3339 /* Send skb data on a socket. Socket must be locked. */
3340 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
3341 			 int len)
3342 {
3343 	return __skb_send_sock(sk, skb, offset, len, sendmsg_locked);
3344 }
3345 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
3346 
3347 /* Send skb data on a socket. Socket must be unlocked. */
3348 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
3349 {
3350 	return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked);
3351 }
3352 
3353 /**
3354  *	skb_store_bits - store bits from kernel buffer to skb
3355  *	@skb: destination buffer
3356  *	@offset: offset in destination
3357  *	@from: source buffer
3358  *	@len: number of bytes to copy
3359  *
3360  *	Copy the specified number of bytes from the source buffer to the
3361  *	destination skb.  This function handles all the messy bits of
3362  *	traversing fragment lists and such.
3363  */
3364 
3365 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
3366 {
3367 	int start = skb_headlen(skb);
3368 	struct sk_buff *frag_iter;
3369 	int i, copy;
3370 
3371 	if (offset > (int)skb->len - len)
3372 		goto fault;
3373 
3374 	if ((copy = start - offset) > 0) {
3375 		if (copy > len)
3376 			copy = len;
3377 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
3378 		if ((len -= copy) == 0)
3379 			return 0;
3380 		offset += copy;
3381 		from += copy;
3382 	}
3383 
3384 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3385 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3386 		int end;
3387 
3388 		WARN_ON(start > offset + len);
3389 
3390 		end = start + skb_frag_size(frag);
3391 		if ((copy = end - offset) > 0) {
3392 			u32 p_off, p_len, copied;
3393 			struct page *p;
3394 			u8 *vaddr;
3395 
3396 			if (copy > len)
3397 				copy = len;
3398 
3399 			skb_frag_foreach_page(frag,
3400 					      skb_frag_off(frag) + offset - start,
3401 					      copy, p, p_off, p_len, copied) {
3402 				vaddr = kmap_atomic(p);
3403 				memcpy(vaddr + p_off, from + copied, p_len);
3404 				kunmap_atomic(vaddr);
3405 			}
3406 
3407 			if ((len -= copy) == 0)
3408 				return 0;
3409 			offset += copy;
3410 			from += copy;
3411 		}
3412 		start = end;
3413 	}
3414 
3415 	skb_walk_frags(skb, frag_iter) {
3416 		int end;
3417 
3418 		WARN_ON(start > offset + len);
3419 
3420 		end = start + frag_iter->len;
3421 		if ((copy = end - offset) > 0) {
3422 			if (copy > len)
3423 				copy = len;
3424 			if (skb_store_bits(frag_iter, offset - start,
3425 					   from, copy))
3426 				goto fault;
3427 			if ((len -= copy) == 0)
3428 				return 0;
3429 			offset += copy;
3430 			from += copy;
3431 		}
3432 		start = end;
3433 	}
3434 	if (!len)
3435 		return 0;
3436 
3437 fault:
3438 	return -EFAULT;
3439 }
3440 EXPORT_SYMBOL(skb_store_bits);
3441 
3442 /* Checksum skb data. */
3443 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
3444 		      __wsum csum, const struct skb_checksum_ops *ops)
3445 {
3446 	int start = skb_headlen(skb);
3447 	int i, copy = start - offset;
3448 	struct sk_buff *frag_iter;
3449 	int pos = 0;
3450 
3451 	/* Checksum header. */
3452 	if (copy > 0) {
3453 		if (copy > len)
3454 			copy = len;
3455 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
3456 				       skb->data + offset, copy, csum);
3457 		if ((len -= copy) == 0)
3458 			return csum;
3459 		offset += copy;
3460 		pos	= copy;
3461 	}
3462 
3463 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3464 		int end;
3465 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3466 
3467 		WARN_ON(start > offset + len);
3468 
3469 		end = start + skb_frag_size(frag);
3470 		if ((copy = end - offset) > 0) {
3471 			u32 p_off, p_len, copied;
3472 			struct page *p;
3473 			__wsum csum2;
3474 			u8 *vaddr;
3475 
3476 			if (copy > len)
3477 				copy = len;
3478 
3479 			skb_frag_foreach_page(frag,
3480 					      skb_frag_off(frag) + offset - start,
3481 					      copy, p, p_off, p_len, copied) {
3482 				vaddr = kmap_atomic(p);
3483 				csum2 = INDIRECT_CALL_1(ops->update,
3484 							csum_partial_ext,
3485 							vaddr + p_off, p_len, 0);
3486 				kunmap_atomic(vaddr);
3487 				csum = INDIRECT_CALL_1(ops->combine,
3488 						       csum_block_add_ext, csum,
3489 						       csum2, pos, p_len);
3490 				pos += p_len;
3491 			}
3492 
3493 			if (!(len -= copy))
3494 				return csum;
3495 			offset += copy;
3496 		}
3497 		start = end;
3498 	}
3499 
3500 	skb_walk_frags(skb, frag_iter) {
3501 		int end;
3502 
3503 		WARN_ON(start > offset + len);
3504 
3505 		end = start + frag_iter->len;
3506 		if ((copy = end - offset) > 0) {
3507 			__wsum csum2;
3508 			if (copy > len)
3509 				copy = len;
3510 			csum2 = __skb_checksum(frag_iter, offset - start,
3511 					       copy, 0, ops);
3512 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
3513 					       csum, csum2, pos, copy);
3514 			if ((len -= copy) == 0)
3515 				return csum;
3516 			offset += copy;
3517 			pos    += copy;
3518 		}
3519 		start = end;
3520 	}
3521 	BUG_ON(len);
3522 
3523 	return csum;
3524 }
3525 EXPORT_SYMBOL(__skb_checksum);
3526 
3527 __wsum skb_checksum(const struct sk_buff *skb, int offset,
3528 		    int len, __wsum csum)
3529 {
3530 	const struct skb_checksum_ops ops = {
3531 		.update  = csum_partial_ext,
3532 		.combine = csum_block_add_ext,
3533 	};
3534 
3535 	return __skb_checksum(skb, offset, len, csum, &ops);
3536 }
3537 EXPORT_SYMBOL(skb_checksum);
3538 
3539 /* Both of above in one bottle. */
3540 
3541 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
3542 				    u8 *to, int len)
3543 {
3544 	int start = skb_headlen(skb);
3545 	int i, copy = start - offset;
3546 	struct sk_buff *frag_iter;
3547 	int pos = 0;
3548 	__wsum csum = 0;
3549 
3550 	/* Copy header. */
3551 	if (copy > 0) {
3552 		if (copy > len)
3553 			copy = len;
3554 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
3555 						 copy);
3556 		if ((len -= copy) == 0)
3557 			return csum;
3558 		offset += copy;
3559 		to     += copy;
3560 		pos	= copy;
3561 	}
3562 
3563 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3564 		int end;
3565 
3566 		WARN_ON(start > offset + len);
3567 
3568 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3569 		if ((copy = end - offset) > 0) {
3570 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3571 			u32 p_off, p_len, copied;
3572 			struct page *p;
3573 			__wsum csum2;
3574 			u8 *vaddr;
3575 
3576 			if (copy > len)
3577 				copy = len;
3578 
3579 			skb_frag_foreach_page(frag,
3580 					      skb_frag_off(frag) + offset - start,
3581 					      copy, p, p_off, p_len, copied) {
3582 				vaddr = kmap_atomic(p);
3583 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
3584 								  to + copied,
3585 								  p_len);
3586 				kunmap_atomic(vaddr);
3587 				csum = csum_block_add(csum, csum2, pos);
3588 				pos += p_len;
3589 			}
3590 
3591 			if (!(len -= copy))
3592 				return csum;
3593 			offset += copy;
3594 			to     += copy;
3595 		}
3596 		start = end;
3597 	}
3598 
3599 	skb_walk_frags(skb, frag_iter) {
3600 		__wsum csum2;
3601 		int end;
3602 
3603 		WARN_ON(start > offset + len);
3604 
3605 		end = start + frag_iter->len;
3606 		if ((copy = end - offset) > 0) {
3607 			if (copy > len)
3608 				copy = len;
3609 			csum2 = skb_copy_and_csum_bits(frag_iter,
3610 						       offset - start,
3611 						       to, copy);
3612 			csum = csum_block_add(csum, csum2, pos);
3613 			if ((len -= copy) == 0)
3614 				return csum;
3615 			offset += copy;
3616 			to     += copy;
3617 			pos    += copy;
3618 		}
3619 		start = end;
3620 	}
3621 	BUG_ON(len);
3622 	return csum;
3623 }
3624 EXPORT_SYMBOL(skb_copy_and_csum_bits);
3625 
3626 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
3627 {
3628 	__sum16 sum;
3629 
3630 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
3631 	/* See comments in __skb_checksum_complete(). */
3632 	if (likely(!sum)) {
3633 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3634 		    !skb->csum_complete_sw)
3635 			netdev_rx_csum_fault(skb->dev, skb);
3636 	}
3637 	if (!skb_shared(skb))
3638 		skb->csum_valid = !sum;
3639 	return sum;
3640 }
3641 EXPORT_SYMBOL(__skb_checksum_complete_head);
3642 
3643 /* This function assumes skb->csum already holds pseudo header's checksum,
3644  * which has been changed from the hardware checksum, for example, by
3645  * __skb_checksum_validate_complete(). And, the original skb->csum must
3646  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
3647  *
3648  * It returns non-zero if the recomputed checksum is still invalid, otherwise
3649  * zero. The new checksum is stored back into skb->csum unless the skb is
3650  * shared.
3651  */
3652 __sum16 __skb_checksum_complete(struct sk_buff *skb)
3653 {
3654 	__wsum csum;
3655 	__sum16 sum;
3656 
3657 	csum = skb_checksum(skb, 0, skb->len, 0);
3658 
3659 	sum = csum_fold(csum_add(skb->csum, csum));
3660 	/* This check is inverted, because we already knew the hardware
3661 	 * checksum is invalid before calling this function. So, if the
3662 	 * re-computed checksum is valid instead, then we have a mismatch
3663 	 * between the original skb->csum and skb_checksum(). This means either
3664 	 * the original hardware checksum is incorrect or we screw up skb->csum
3665 	 * when moving skb->data around.
3666 	 */
3667 	if (likely(!sum)) {
3668 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3669 		    !skb->csum_complete_sw)
3670 			netdev_rx_csum_fault(skb->dev, skb);
3671 	}
3672 
3673 	if (!skb_shared(skb)) {
3674 		/* Save full packet checksum */
3675 		skb->csum = csum;
3676 		skb->ip_summed = CHECKSUM_COMPLETE;
3677 		skb->csum_complete_sw = 1;
3678 		skb->csum_valid = !sum;
3679 	}
3680 
3681 	return sum;
3682 }
3683 EXPORT_SYMBOL(__skb_checksum_complete);
3684 
3685 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
3686 {
3687 	net_warn_ratelimited(
3688 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
3689 		__func__);
3690 	return 0;
3691 }
3692 
3693 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
3694 				       int offset, int len)
3695 {
3696 	net_warn_ratelimited(
3697 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
3698 		__func__);
3699 	return 0;
3700 }
3701 
3702 static const struct skb_checksum_ops default_crc32c_ops = {
3703 	.update  = warn_crc32c_csum_update,
3704 	.combine = warn_crc32c_csum_combine,
3705 };
3706 
3707 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3708 	&default_crc32c_ops;
3709 EXPORT_SYMBOL(crc32c_csum_stub);
3710 
3711  /**
3712  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3713  *	@from: source buffer
3714  *
3715  *	Calculates the amount of linear headroom needed in the 'to' skb passed
3716  *	into skb_zerocopy().
3717  */
3718 unsigned int
3719 skb_zerocopy_headlen(const struct sk_buff *from)
3720 {
3721 	unsigned int hlen = 0;
3722 
3723 	if (!from->head_frag ||
3724 	    skb_headlen(from) < L1_CACHE_BYTES ||
3725 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3726 		hlen = skb_headlen(from);
3727 		if (!hlen)
3728 			hlen = from->len;
3729 	}
3730 
3731 	if (skb_has_frag_list(from))
3732 		hlen = from->len;
3733 
3734 	return hlen;
3735 }
3736 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3737 
3738 /**
3739  *	skb_zerocopy - Zero copy skb to skb
3740  *	@to: destination buffer
3741  *	@from: source buffer
3742  *	@len: number of bytes to copy from source buffer
3743  *	@hlen: size of linear headroom in destination buffer
3744  *
3745  *	Copies up to `len` bytes from `from` to `to` by creating references
3746  *	to the frags in the source buffer.
3747  *
3748  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3749  *	headroom in the `to` buffer.
3750  *
3751  *	Return value:
3752  *	0: everything is OK
3753  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
3754  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
3755  */
3756 int
3757 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3758 {
3759 	int i, j = 0;
3760 	int plen = 0; /* length of skb->head fragment */
3761 	int ret;
3762 	struct page *page;
3763 	unsigned int offset;
3764 
3765 	BUG_ON(!from->head_frag && !hlen);
3766 
3767 	/* dont bother with small payloads */
3768 	if (len <= skb_tailroom(to))
3769 		return skb_copy_bits(from, 0, skb_put(to, len), len);
3770 
3771 	if (hlen) {
3772 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3773 		if (unlikely(ret))
3774 			return ret;
3775 		len -= hlen;
3776 	} else {
3777 		plen = min_t(int, skb_headlen(from), len);
3778 		if (plen) {
3779 			page = virt_to_head_page(from->head);
3780 			offset = from->data - (unsigned char *)page_address(page);
3781 			__skb_fill_netmem_desc(to, 0, page_to_netmem(page),
3782 					       offset, plen);
3783 			get_page(page);
3784 			j = 1;
3785 			len -= plen;
3786 		}
3787 	}
3788 
3789 	skb_len_add(to, len + plen);
3790 
3791 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3792 		skb_tx_error(from);
3793 		return -ENOMEM;
3794 	}
3795 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
3796 
3797 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3798 		int size;
3799 
3800 		if (!len)
3801 			break;
3802 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3803 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3804 					len);
3805 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3806 		len -= size;
3807 		skb_frag_ref(to, j);
3808 		j++;
3809 	}
3810 	skb_shinfo(to)->nr_frags = j;
3811 
3812 	return 0;
3813 }
3814 EXPORT_SYMBOL_GPL(skb_zerocopy);
3815 
3816 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3817 {
3818 	__wsum csum;
3819 	long csstart;
3820 
3821 	if (skb->ip_summed == CHECKSUM_PARTIAL)
3822 		csstart = skb_checksum_start_offset(skb);
3823 	else
3824 		csstart = skb_headlen(skb);
3825 
3826 	BUG_ON(csstart > skb_headlen(skb));
3827 
3828 	skb_copy_from_linear_data(skb, to, csstart);
3829 
3830 	csum = 0;
3831 	if (csstart != skb->len)
3832 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3833 					      skb->len - csstart);
3834 
3835 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3836 		long csstuff = csstart + skb->csum_offset;
3837 
3838 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
3839 	}
3840 }
3841 EXPORT_SYMBOL(skb_copy_and_csum_dev);
3842 
3843 /**
3844  *	skb_dequeue - remove from the head of the queue
3845  *	@list: list to dequeue from
3846  *
3847  *	Remove the head of the list. The list lock is taken so the function
3848  *	may be used safely with other locking list functions. The head item is
3849  *	returned or %NULL if the list is empty.
3850  */
3851 
3852 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3853 {
3854 	unsigned long flags;
3855 	struct sk_buff *result;
3856 
3857 	spin_lock_irqsave(&list->lock, flags);
3858 	result = __skb_dequeue(list);
3859 	spin_unlock_irqrestore(&list->lock, flags);
3860 	return result;
3861 }
3862 EXPORT_SYMBOL(skb_dequeue);
3863 
3864 /**
3865  *	skb_dequeue_tail - remove from the tail of the queue
3866  *	@list: list to dequeue from
3867  *
3868  *	Remove the tail of the list. The list lock is taken so the function
3869  *	may be used safely with other locking list functions. The tail item is
3870  *	returned or %NULL if the list is empty.
3871  */
3872 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3873 {
3874 	unsigned long flags;
3875 	struct sk_buff *result;
3876 
3877 	spin_lock_irqsave(&list->lock, flags);
3878 	result = __skb_dequeue_tail(list);
3879 	spin_unlock_irqrestore(&list->lock, flags);
3880 	return result;
3881 }
3882 EXPORT_SYMBOL(skb_dequeue_tail);
3883 
3884 /**
3885  *	skb_queue_purge_reason - empty a list
3886  *	@list: list to empty
3887  *	@reason: drop reason
3888  *
3889  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
3890  *	the list and one reference dropped. This function takes the list
3891  *	lock and is atomic with respect to other list locking functions.
3892  */
3893 void skb_queue_purge_reason(struct sk_buff_head *list,
3894 			    enum skb_drop_reason reason)
3895 {
3896 	struct sk_buff_head tmp;
3897 	unsigned long flags;
3898 
3899 	if (skb_queue_empty_lockless(list))
3900 		return;
3901 
3902 	__skb_queue_head_init(&tmp);
3903 
3904 	spin_lock_irqsave(&list->lock, flags);
3905 	skb_queue_splice_init(list, &tmp);
3906 	spin_unlock_irqrestore(&list->lock, flags);
3907 
3908 	__skb_queue_purge_reason(&tmp, reason);
3909 }
3910 EXPORT_SYMBOL(skb_queue_purge_reason);
3911 
3912 /**
3913  *	skb_rbtree_purge - empty a skb rbtree
3914  *	@root: root of the rbtree to empty
3915  *	Return value: the sum of truesizes of all purged skbs.
3916  *
3917  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3918  *	the list and one reference dropped. This function does not take
3919  *	any lock. Synchronization should be handled by the caller (e.g., TCP
3920  *	out-of-order queue is protected by the socket lock).
3921  */
3922 unsigned int skb_rbtree_purge(struct rb_root *root)
3923 {
3924 	struct rb_node *p = rb_first(root);
3925 	unsigned int sum = 0;
3926 
3927 	while (p) {
3928 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3929 
3930 		p = rb_next(p);
3931 		rb_erase(&skb->rbnode, root);
3932 		sum += skb->truesize;
3933 		kfree_skb(skb);
3934 	}
3935 	return sum;
3936 }
3937 
3938 void skb_errqueue_purge(struct sk_buff_head *list)
3939 {
3940 	struct sk_buff *skb, *next;
3941 	struct sk_buff_head kill;
3942 	unsigned long flags;
3943 
3944 	__skb_queue_head_init(&kill);
3945 
3946 	spin_lock_irqsave(&list->lock, flags);
3947 	skb_queue_walk_safe(list, skb, next) {
3948 		if (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ZEROCOPY ||
3949 		    SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING)
3950 			continue;
3951 		__skb_unlink(skb, list);
3952 		__skb_queue_tail(&kill, skb);
3953 	}
3954 	spin_unlock_irqrestore(&list->lock, flags);
3955 	__skb_queue_purge(&kill);
3956 }
3957 EXPORT_SYMBOL(skb_errqueue_purge);
3958 
3959 /**
3960  *	skb_queue_head - queue a buffer at the list head
3961  *	@list: list to use
3962  *	@newsk: buffer to queue
3963  *
3964  *	Queue a buffer at the start of the list. This function takes the
3965  *	list lock and can be used safely with other locking &sk_buff functions
3966  *	safely.
3967  *
3968  *	A buffer cannot be placed on two lists at the same time.
3969  */
3970 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3971 {
3972 	unsigned long flags;
3973 
3974 	spin_lock_irqsave(&list->lock, flags);
3975 	__skb_queue_head(list, newsk);
3976 	spin_unlock_irqrestore(&list->lock, flags);
3977 }
3978 EXPORT_SYMBOL(skb_queue_head);
3979 
3980 /**
3981  *	skb_queue_tail - queue a buffer at the list tail
3982  *	@list: list to use
3983  *	@newsk: buffer to queue
3984  *
3985  *	Queue a buffer at the tail of the list. This function takes the
3986  *	list lock and can be used safely with other locking &sk_buff functions
3987  *	safely.
3988  *
3989  *	A buffer cannot be placed on two lists at the same time.
3990  */
3991 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3992 {
3993 	unsigned long flags;
3994 
3995 	spin_lock_irqsave(&list->lock, flags);
3996 	__skb_queue_tail(list, newsk);
3997 	spin_unlock_irqrestore(&list->lock, flags);
3998 }
3999 EXPORT_SYMBOL(skb_queue_tail);
4000 
4001 /**
4002  *	skb_unlink	-	remove a buffer from a list
4003  *	@skb: buffer to remove
4004  *	@list: list to use
4005  *
4006  *	Remove a packet from a list. The list locks are taken and this
4007  *	function is atomic with respect to other list locked calls
4008  *
4009  *	You must know what list the SKB is on.
4010  */
4011 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
4012 {
4013 	unsigned long flags;
4014 
4015 	spin_lock_irqsave(&list->lock, flags);
4016 	__skb_unlink(skb, list);
4017 	spin_unlock_irqrestore(&list->lock, flags);
4018 }
4019 EXPORT_SYMBOL(skb_unlink);
4020 
4021 /**
4022  *	skb_append	-	append a buffer
4023  *	@old: buffer to insert after
4024  *	@newsk: buffer to insert
4025  *	@list: list to use
4026  *
4027  *	Place a packet after a given packet in a list. The list locks are taken
4028  *	and this function is atomic with respect to other list locked calls.
4029  *	A buffer cannot be placed on two lists at the same time.
4030  */
4031 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
4032 {
4033 	unsigned long flags;
4034 
4035 	spin_lock_irqsave(&list->lock, flags);
4036 	__skb_queue_after(list, old, newsk);
4037 	spin_unlock_irqrestore(&list->lock, flags);
4038 }
4039 EXPORT_SYMBOL(skb_append);
4040 
4041 static inline void skb_split_inside_header(struct sk_buff *skb,
4042 					   struct sk_buff* skb1,
4043 					   const u32 len, const int pos)
4044 {
4045 	int i;
4046 
4047 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
4048 					 pos - len);
4049 	/* And move data appendix as is. */
4050 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4051 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
4052 
4053 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
4054 	skb_shinfo(skb)->nr_frags  = 0;
4055 	skb1->data_len		   = skb->data_len;
4056 	skb1->len		   += skb1->data_len;
4057 	skb->data_len		   = 0;
4058 	skb->len		   = len;
4059 	skb_set_tail_pointer(skb, len);
4060 }
4061 
4062 static inline void skb_split_no_header(struct sk_buff *skb,
4063 				       struct sk_buff* skb1,
4064 				       const u32 len, int pos)
4065 {
4066 	int i, k = 0;
4067 	const int nfrags = skb_shinfo(skb)->nr_frags;
4068 
4069 	skb_shinfo(skb)->nr_frags = 0;
4070 	skb1->len		  = skb1->data_len = skb->len - len;
4071 	skb->len		  = len;
4072 	skb->data_len		  = len - pos;
4073 
4074 	for (i = 0; i < nfrags; i++) {
4075 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4076 
4077 		if (pos + size > len) {
4078 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
4079 
4080 			if (pos < len) {
4081 				/* Split frag.
4082 				 * We have two variants in this case:
4083 				 * 1. Move all the frag to the second
4084 				 *    part, if it is possible. F.e.
4085 				 *    this approach is mandatory for TUX,
4086 				 *    where splitting is expensive.
4087 				 * 2. Split is accurately. We make this.
4088 				 */
4089 				skb_frag_ref(skb, i);
4090 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
4091 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
4092 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
4093 				skb_shinfo(skb)->nr_frags++;
4094 			}
4095 			k++;
4096 		} else
4097 			skb_shinfo(skb)->nr_frags++;
4098 		pos += size;
4099 	}
4100 	skb_shinfo(skb1)->nr_frags = k;
4101 }
4102 
4103 /**
4104  * skb_split - Split fragmented skb to two parts at length len.
4105  * @skb: the buffer to split
4106  * @skb1: the buffer to receive the second part
4107  * @len: new length for skb
4108  */
4109 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
4110 {
4111 	int pos = skb_headlen(skb);
4112 	const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY;
4113 
4114 	skb_zcopy_downgrade_managed(skb);
4115 
4116 	skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags;
4117 	skb_zerocopy_clone(skb1, skb, 0);
4118 	if (len < pos)	/* Split line is inside header. */
4119 		skb_split_inside_header(skb, skb1, len, pos);
4120 	else		/* Second chunk has no header, nothing to copy. */
4121 		skb_split_no_header(skb, skb1, len, pos);
4122 }
4123 EXPORT_SYMBOL(skb_split);
4124 
4125 /* Shifting from/to a cloned skb is a no-go.
4126  *
4127  * Caller cannot keep skb_shinfo related pointers past calling here!
4128  */
4129 static int skb_prepare_for_shift(struct sk_buff *skb)
4130 {
4131 	return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
4132 }
4133 
4134 /**
4135  * skb_shift - Shifts paged data partially from skb to another
4136  * @tgt: buffer into which tail data gets added
4137  * @skb: buffer from which the paged data comes from
4138  * @shiftlen: shift up to this many bytes
4139  *
4140  * Attempts to shift up to shiftlen worth of bytes, which may be less than
4141  * the length of the skb, from skb to tgt. Returns number bytes shifted.
4142  * It's up to caller to free skb if everything was shifted.
4143  *
4144  * If @tgt runs out of frags, the whole operation is aborted.
4145  *
4146  * Skb cannot include anything else but paged data while tgt is allowed
4147  * to have non-paged data as well.
4148  *
4149  * TODO: full sized shift could be optimized but that would need
4150  * specialized skb free'er to handle frags without up-to-date nr_frags.
4151  */
4152 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
4153 {
4154 	int from, to, merge, todo;
4155 	skb_frag_t *fragfrom, *fragto;
4156 
4157 	BUG_ON(shiftlen > skb->len);
4158 
4159 	if (skb_headlen(skb))
4160 		return 0;
4161 	if (skb_zcopy(tgt) || skb_zcopy(skb))
4162 		return 0;
4163 
4164 	DEBUG_NET_WARN_ON_ONCE(tgt->pp_recycle != skb->pp_recycle);
4165 	DEBUG_NET_WARN_ON_ONCE(skb_cmp_decrypted(tgt, skb));
4166 
4167 	todo = shiftlen;
4168 	from = 0;
4169 	to = skb_shinfo(tgt)->nr_frags;
4170 	fragfrom = &skb_shinfo(skb)->frags[from];
4171 
4172 	/* Actual merge is delayed until the point when we know we can
4173 	 * commit all, so that we don't have to undo partial changes
4174 	 */
4175 	if (!skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
4176 			      skb_frag_off(fragfrom))) {
4177 		merge = -1;
4178 	} else {
4179 		merge = to - 1;
4180 
4181 		todo -= skb_frag_size(fragfrom);
4182 		if (todo < 0) {
4183 			if (skb_prepare_for_shift(skb) ||
4184 			    skb_prepare_for_shift(tgt))
4185 				return 0;
4186 
4187 			/* All previous frag pointers might be stale! */
4188 			fragfrom = &skb_shinfo(skb)->frags[from];
4189 			fragto = &skb_shinfo(tgt)->frags[merge];
4190 
4191 			skb_frag_size_add(fragto, shiftlen);
4192 			skb_frag_size_sub(fragfrom, shiftlen);
4193 			skb_frag_off_add(fragfrom, shiftlen);
4194 
4195 			goto onlymerged;
4196 		}
4197 
4198 		from++;
4199 	}
4200 
4201 	/* Skip full, not-fitting skb to avoid expensive operations */
4202 	if ((shiftlen == skb->len) &&
4203 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
4204 		return 0;
4205 
4206 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
4207 		return 0;
4208 
4209 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
4210 		if (to == MAX_SKB_FRAGS)
4211 			return 0;
4212 
4213 		fragfrom = &skb_shinfo(skb)->frags[from];
4214 		fragto = &skb_shinfo(tgt)->frags[to];
4215 
4216 		if (todo >= skb_frag_size(fragfrom)) {
4217 			*fragto = *fragfrom;
4218 			todo -= skb_frag_size(fragfrom);
4219 			from++;
4220 			to++;
4221 
4222 		} else {
4223 			__skb_frag_ref(fragfrom);
4224 			skb_frag_page_copy(fragto, fragfrom);
4225 			skb_frag_off_copy(fragto, fragfrom);
4226 			skb_frag_size_set(fragto, todo);
4227 
4228 			skb_frag_off_add(fragfrom, todo);
4229 			skb_frag_size_sub(fragfrom, todo);
4230 			todo = 0;
4231 
4232 			to++;
4233 			break;
4234 		}
4235 	}
4236 
4237 	/* Ready to "commit" this state change to tgt */
4238 	skb_shinfo(tgt)->nr_frags = to;
4239 
4240 	if (merge >= 0) {
4241 		fragfrom = &skb_shinfo(skb)->frags[0];
4242 		fragto = &skb_shinfo(tgt)->frags[merge];
4243 
4244 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
4245 		__skb_frag_unref(fragfrom, skb->pp_recycle);
4246 	}
4247 
4248 	/* Reposition in the original skb */
4249 	to = 0;
4250 	while (from < skb_shinfo(skb)->nr_frags)
4251 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
4252 	skb_shinfo(skb)->nr_frags = to;
4253 
4254 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
4255 
4256 onlymerged:
4257 	/* Most likely the tgt won't ever need its checksum anymore, skb on
4258 	 * the other hand might need it if it needs to be resent
4259 	 */
4260 	tgt->ip_summed = CHECKSUM_PARTIAL;
4261 	skb->ip_summed = CHECKSUM_PARTIAL;
4262 
4263 	skb_len_add(skb, -shiftlen);
4264 	skb_len_add(tgt, shiftlen);
4265 
4266 	return shiftlen;
4267 }
4268 
4269 /**
4270  * skb_prepare_seq_read - Prepare a sequential read of skb data
4271  * @skb: the buffer to read
4272  * @from: lower offset of data to be read
4273  * @to: upper offset of data to be read
4274  * @st: state variable
4275  *
4276  * Initializes the specified state variable. Must be called before
4277  * invoking skb_seq_read() for the first time.
4278  */
4279 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
4280 			  unsigned int to, struct skb_seq_state *st)
4281 {
4282 	st->lower_offset = from;
4283 	st->upper_offset = to;
4284 	st->root_skb = st->cur_skb = skb;
4285 	st->frag_idx = st->stepped_offset = 0;
4286 	st->frag_data = NULL;
4287 	st->frag_off = 0;
4288 }
4289 EXPORT_SYMBOL(skb_prepare_seq_read);
4290 
4291 /**
4292  * skb_seq_read - Sequentially read skb data
4293  * @consumed: number of bytes consumed by the caller so far
4294  * @data: destination pointer for data to be returned
4295  * @st: state variable
4296  *
4297  * Reads a block of skb data at @consumed relative to the
4298  * lower offset specified to skb_prepare_seq_read(). Assigns
4299  * the head of the data block to @data and returns the length
4300  * of the block or 0 if the end of the skb data or the upper
4301  * offset has been reached.
4302  *
4303  * The caller is not required to consume all of the data
4304  * returned, i.e. @consumed is typically set to the number
4305  * of bytes already consumed and the next call to
4306  * skb_seq_read() will return the remaining part of the block.
4307  *
4308  * Note 1: The size of each block of data returned can be arbitrary,
4309  *       this limitation is the cost for zerocopy sequential
4310  *       reads of potentially non linear data.
4311  *
4312  * Note 2: Fragment lists within fragments are not implemented
4313  *       at the moment, state->root_skb could be replaced with
4314  *       a stack for this purpose.
4315  */
4316 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
4317 			  struct skb_seq_state *st)
4318 {
4319 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
4320 	skb_frag_t *frag;
4321 
4322 	if (unlikely(abs_offset >= st->upper_offset)) {
4323 		if (st->frag_data) {
4324 			kunmap_atomic(st->frag_data);
4325 			st->frag_data = NULL;
4326 		}
4327 		return 0;
4328 	}
4329 
4330 next_skb:
4331 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
4332 
4333 	if (abs_offset < block_limit && !st->frag_data) {
4334 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
4335 		return block_limit - abs_offset;
4336 	}
4337 
4338 	if (st->frag_idx == 0 && !st->frag_data)
4339 		st->stepped_offset += skb_headlen(st->cur_skb);
4340 
4341 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
4342 		unsigned int pg_idx, pg_off, pg_sz;
4343 
4344 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
4345 
4346 		pg_idx = 0;
4347 		pg_off = skb_frag_off(frag);
4348 		pg_sz = skb_frag_size(frag);
4349 
4350 		if (skb_frag_must_loop(skb_frag_page(frag))) {
4351 			pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
4352 			pg_off = offset_in_page(pg_off + st->frag_off);
4353 			pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
4354 						    PAGE_SIZE - pg_off);
4355 		}
4356 
4357 		block_limit = pg_sz + st->stepped_offset;
4358 		if (abs_offset < block_limit) {
4359 			if (!st->frag_data)
4360 				st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
4361 
4362 			*data = (u8 *)st->frag_data + pg_off +
4363 				(abs_offset - st->stepped_offset);
4364 
4365 			return block_limit - abs_offset;
4366 		}
4367 
4368 		if (st->frag_data) {
4369 			kunmap_atomic(st->frag_data);
4370 			st->frag_data = NULL;
4371 		}
4372 
4373 		st->stepped_offset += pg_sz;
4374 		st->frag_off += pg_sz;
4375 		if (st->frag_off == skb_frag_size(frag)) {
4376 			st->frag_off = 0;
4377 			st->frag_idx++;
4378 		}
4379 	}
4380 
4381 	if (st->frag_data) {
4382 		kunmap_atomic(st->frag_data);
4383 		st->frag_data = NULL;
4384 	}
4385 
4386 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
4387 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
4388 		st->frag_idx = 0;
4389 		goto next_skb;
4390 	} else if (st->cur_skb->next) {
4391 		st->cur_skb = st->cur_skb->next;
4392 		st->frag_idx = 0;
4393 		goto next_skb;
4394 	}
4395 
4396 	return 0;
4397 }
4398 EXPORT_SYMBOL(skb_seq_read);
4399 
4400 /**
4401  * skb_abort_seq_read - Abort a sequential read of skb data
4402  * @st: state variable
4403  *
4404  * Must be called if skb_seq_read() was not called until it
4405  * returned 0.
4406  */
4407 void skb_abort_seq_read(struct skb_seq_state *st)
4408 {
4409 	if (st->frag_data)
4410 		kunmap_atomic(st->frag_data);
4411 }
4412 EXPORT_SYMBOL(skb_abort_seq_read);
4413 
4414 #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
4415 
4416 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
4417 					  struct ts_config *conf,
4418 					  struct ts_state *state)
4419 {
4420 	return skb_seq_read(offset, text, TS_SKB_CB(state));
4421 }
4422 
4423 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
4424 {
4425 	skb_abort_seq_read(TS_SKB_CB(state));
4426 }
4427 
4428 /**
4429  * skb_find_text - Find a text pattern in skb data
4430  * @skb: the buffer to look in
4431  * @from: search offset
4432  * @to: search limit
4433  * @config: textsearch configuration
4434  *
4435  * Finds a pattern in the skb data according to the specified
4436  * textsearch configuration. Use textsearch_next() to retrieve
4437  * subsequent occurrences of the pattern. Returns the offset
4438  * to the first occurrence or UINT_MAX if no match was found.
4439  */
4440 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
4441 			   unsigned int to, struct ts_config *config)
4442 {
4443 	unsigned int patlen = config->ops->get_pattern_len(config);
4444 	struct ts_state state;
4445 	unsigned int ret;
4446 
4447 	BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
4448 
4449 	config->get_next_block = skb_ts_get_next_block;
4450 	config->finish = skb_ts_finish;
4451 
4452 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
4453 
4454 	ret = textsearch_find(config, &state);
4455 	return (ret + patlen <= to - from ? ret : UINT_MAX);
4456 }
4457 EXPORT_SYMBOL(skb_find_text);
4458 
4459 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
4460 			 int offset, size_t size, size_t max_frags)
4461 {
4462 	int i = skb_shinfo(skb)->nr_frags;
4463 
4464 	if (skb_can_coalesce(skb, i, page, offset)) {
4465 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
4466 	} else if (i < max_frags) {
4467 		skb_zcopy_downgrade_managed(skb);
4468 		get_page(page);
4469 		skb_fill_page_desc_noacc(skb, i, page, offset, size);
4470 	} else {
4471 		return -EMSGSIZE;
4472 	}
4473 
4474 	return 0;
4475 }
4476 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
4477 
4478 /**
4479  *	skb_pull_rcsum - pull skb and update receive checksum
4480  *	@skb: buffer to update
4481  *	@len: length of data pulled
4482  *
4483  *	This function performs an skb_pull on the packet and updates
4484  *	the CHECKSUM_COMPLETE checksum.  It should be used on
4485  *	receive path processing instead of skb_pull unless you know
4486  *	that the checksum difference is zero (e.g., a valid IP header)
4487  *	or you are setting ip_summed to CHECKSUM_NONE.
4488  */
4489 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
4490 {
4491 	unsigned char *data = skb->data;
4492 
4493 	BUG_ON(len > skb->len);
4494 	__skb_pull(skb, len);
4495 	skb_postpull_rcsum(skb, data, len);
4496 	return skb->data;
4497 }
4498 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
4499 
4500 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
4501 {
4502 	skb_frag_t head_frag;
4503 	struct page *page;
4504 
4505 	page = virt_to_head_page(frag_skb->head);
4506 	skb_frag_fill_page_desc(&head_frag, page, frag_skb->data -
4507 				(unsigned char *)page_address(page),
4508 				skb_headlen(frag_skb));
4509 	return head_frag;
4510 }
4511 
4512 struct sk_buff *skb_segment_list(struct sk_buff *skb,
4513 				 netdev_features_t features,
4514 				 unsigned int offset)
4515 {
4516 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
4517 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
4518 	unsigned int delta_truesize = 0;
4519 	unsigned int delta_len = 0;
4520 	struct sk_buff *tail = NULL;
4521 	struct sk_buff *nskb, *tmp;
4522 	int len_diff, err;
4523 
4524 	skb_push(skb, -skb_network_offset(skb) + offset);
4525 
4526 	/* Ensure the head is writeable before touching the shared info */
4527 	err = skb_unclone(skb, GFP_ATOMIC);
4528 	if (err)
4529 		goto err_linearize;
4530 
4531 	skb_shinfo(skb)->frag_list = NULL;
4532 
4533 	while (list_skb) {
4534 		nskb = list_skb;
4535 		list_skb = list_skb->next;
4536 
4537 		err = 0;
4538 		delta_truesize += nskb->truesize;
4539 		if (skb_shared(nskb)) {
4540 			tmp = skb_clone(nskb, GFP_ATOMIC);
4541 			if (tmp) {
4542 				consume_skb(nskb);
4543 				nskb = tmp;
4544 				err = skb_unclone(nskb, GFP_ATOMIC);
4545 			} else {
4546 				err = -ENOMEM;
4547 			}
4548 		}
4549 
4550 		if (!tail)
4551 			skb->next = nskb;
4552 		else
4553 			tail->next = nskb;
4554 
4555 		if (unlikely(err)) {
4556 			nskb->next = list_skb;
4557 			goto err_linearize;
4558 		}
4559 
4560 		tail = nskb;
4561 
4562 		delta_len += nskb->len;
4563 
4564 		skb_push(nskb, -skb_network_offset(nskb) + offset);
4565 
4566 		skb_release_head_state(nskb);
4567 		len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
4568 		__copy_skb_header(nskb, skb);
4569 
4570 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
4571 		nskb->transport_header += len_diff;
4572 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
4573 						 nskb->data - tnl_hlen,
4574 						 offset + tnl_hlen);
4575 
4576 		if (skb_needs_linearize(nskb, features) &&
4577 		    __skb_linearize(nskb))
4578 			goto err_linearize;
4579 	}
4580 
4581 	skb->truesize = skb->truesize - delta_truesize;
4582 	skb->data_len = skb->data_len - delta_len;
4583 	skb->len = skb->len - delta_len;
4584 
4585 	skb_gso_reset(skb);
4586 
4587 	skb->prev = tail;
4588 
4589 	if (skb_needs_linearize(skb, features) &&
4590 	    __skb_linearize(skb))
4591 		goto err_linearize;
4592 
4593 	skb_get(skb);
4594 
4595 	return skb;
4596 
4597 err_linearize:
4598 	kfree_skb_list(skb->next);
4599 	skb->next = NULL;
4600 	return ERR_PTR(-ENOMEM);
4601 }
4602 EXPORT_SYMBOL_GPL(skb_segment_list);
4603 
4604 /**
4605  *	skb_segment - Perform protocol segmentation on skb.
4606  *	@head_skb: buffer to segment
4607  *	@features: features for the output path (see dev->features)
4608  *
4609  *	This function performs segmentation on the given skb.  It returns
4610  *	a pointer to the first in a list of new skbs for the segments.
4611  *	In case of error it returns ERR_PTR(err).
4612  */
4613 struct sk_buff *skb_segment(struct sk_buff *head_skb,
4614 			    netdev_features_t features)
4615 {
4616 	struct sk_buff *segs = NULL;
4617 	struct sk_buff *tail = NULL;
4618 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4619 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
4620 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4621 	unsigned int offset = doffset;
4622 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
4623 	unsigned int partial_segs = 0;
4624 	unsigned int headroom;
4625 	unsigned int len = head_skb->len;
4626 	struct sk_buff *frag_skb;
4627 	skb_frag_t *frag;
4628 	__be16 proto;
4629 	bool csum, sg;
4630 	int err = -ENOMEM;
4631 	int i = 0;
4632 	int nfrags, pos;
4633 
4634 	if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
4635 	    mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
4636 		struct sk_buff *check_skb;
4637 
4638 		for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
4639 			if (skb_headlen(check_skb) && !check_skb->head_frag) {
4640 				/* gso_size is untrusted, and we have a frag_list with
4641 				 * a linear non head_frag item.
4642 				 *
4643 				 * If head_skb's headlen does not fit requested gso_size,
4644 				 * it means that the frag_list members do NOT terminate
4645 				 * on exact gso_size boundaries. Hence we cannot perform
4646 				 * skb_frag_t page sharing. Therefore we must fallback to
4647 				 * copying the frag_list skbs; we do so by disabling SG.
4648 				 */
4649 				features &= ~NETIF_F_SG;
4650 				break;
4651 			}
4652 		}
4653 	}
4654 
4655 	__skb_push(head_skb, doffset);
4656 	proto = skb_network_protocol(head_skb, NULL);
4657 	if (unlikely(!proto))
4658 		return ERR_PTR(-EINVAL);
4659 
4660 	sg = !!(features & NETIF_F_SG);
4661 	csum = !!can_checksum_protocol(features, proto);
4662 
4663 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
4664 		if (!(features & NETIF_F_GSO_PARTIAL)) {
4665 			struct sk_buff *iter;
4666 			unsigned int frag_len;
4667 
4668 			if (!list_skb ||
4669 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
4670 				goto normal;
4671 
4672 			/* If we get here then all the required
4673 			 * GSO features except frag_list are supported.
4674 			 * Try to split the SKB to multiple GSO SKBs
4675 			 * with no frag_list.
4676 			 * Currently we can do that only when the buffers don't
4677 			 * have a linear part and all the buffers except
4678 			 * the last are of the same length.
4679 			 */
4680 			frag_len = list_skb->len;
4681 			skb_walk_frags(head_skb, iter) {
4682 				if (frag_len != iter->len && iter->next)
4683 					goto normal;
4684 				if (skb_headlen(iter) && !iter->head_frag)
4685 					goto normal;
4686 
4687 				len -= iter->len;
4688 			}
4689 
4690 			if (len != frag_len)
4691 				goto normal;
4692 		}
4693 
4694 		/* GSO partial only requires that we trim off any excess that
4695 		 * doesn't fit into an MSS sized block, so take care of that
4696 		 * now.
4697 		 * Cap len to not accidentally hit GSO_BY_FRAGS.
4698 		 */
4699 		partial_segs = min(len, GSO_BY_FRAGS - 1) / mss;
4700 		if (partial_segs > 1)
4701 			mss *= partial_segs;
4702 		else
4703 			partial_segs = 0;
4704 	}
4705 
4706 normal:
4707 	headroom = skb_headroom(head_skb);
4708 	pos = skb_headlen(head_skb);
4709 
4710 	if (skb_orphan_frags(head_skb, GFP_ATOMIC))
4711 		return ERR_PTR(-ENOMEM);
4712 
4713 	nfrags = skb_shinfo(head_skb)->nr_frags;
4714 	frag = skb_shinfo(head_skb)->frags;
4715 	frag_skb = head_skb;
4716 
4717 	do {
4718 		struct sk_buff *nskb;
4719 		skb_frag_t *nskb_frag;
4720 		int hsize;
4721 		int size;
4722 
4723 		if (unlikely(mss == GSO_BY_FRAGS)) {
4724 			len = list_skb->len;
4725 		} else {
4726 			len = head_skb->len - offset;
4727 			if (len > mss)
4728 				len = mss;
4729 		}
4730 
4731 		hsize = skb_headlen(head_skb) - offset;
4732 
4733 		if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
4734 		    (skb_headlen(list_skb) == len || sg)) {
4735 			BUG_ON(skb_headlen(list_skb) > len);
4736 
4737 			nskb = skb_clone(list_skb, GFP_ATOMIC);
4738 			if (unlikely(!nskb))
4739 				goto err;
4740 
4741 			i = 0;
4742 			nfrags = skb_shinfo(list_skb)->nr_frags;
4743 			frag = skb_shinfo(list_skb)->frags;
4744 			frag_skb = list_skb;
4745 			pos += skb_headlen(list_skb);
4746 
4747 			while (pos < offset + len) {
4748 				BUG_ON(i >= nfrags);
4749 
4750 				size = skb_frag_size(frag);
4751 				if (pos + size > offset + len)
4752 					break;
4753 
4754 				i++;
4755 				pos += size;
4756 				frag++;
4757 			}
4758 
4759 			list_skb = list_skb->next;
4760 
4761 			if (unlikely(pskb_trim(nskb, len))) {
4762 				kfree_skb(nskb);
4763 				goto err;
4764 			}
4765 
4766 			hsize = skb_end_offset(nskb);
4767 			if (skb_cow_head(nskb, doffset + headroom)) {
4768 				kfree_skb(nskb);
4769 				goto err;
4770 			}
4771 
4772 			nskb->truesize += skb_end_offset(nskb) - hsize;
4773 			skb_release_head_state(nskb);
4774 			__skb_push(nskb, doffset);
4775 		} else {
4776 			if (hsize < 0)
4777 				hsize = 0;
4778 			if (hsize > len || !sg)
4779 				hsize = len;
4780 
4781 			nskb = __alloc_skb(hsize + doffset + headroom,
4782 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4783 					   NUMA_NO_NODE);
4784 
4785 			if (unlikely(!nskb))
4786 				goto err;
4787 
4788 			skb_reserve(nskb, headroom);
4789 			__skb_put(nskb, doffset);
4790 		}
4791 
4792 		if (segs)
4793 			tail->next = nskb;
4794 		else
4795 			segs = nskb;
4796 		tail = nskb;
4797 
4798 		__copy_skb_header(nskb, head_skb);
4799 
4800 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4801 		skb_reset_mac_len(nskb);
4802 
4803 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
4804 						 nskb->data - tnl_hlen,
4805 						 doffset + tnl_hlen);
4806 
4807 		if (nskb->len == len + doffset)
4808 			goto perform_csum_check;
4809 
4810 		if (!sg) {
4811 			if (!csum) {
4812 				if (!nskb->remcsum_offload)
4813 					nskb->ip_summed = CHECKSUM_NONE;
4814 				SKB_GSO_CB(nskb)->csum =
4815 					skb_copy_and_csum_bits(head_skb, offset,
4816 							       skb_put(nskb,
4817 								       len),
4818 							       len);
4819 				SKB_GSO_CB(nskb)->csum_start =
4820 					skb_headroom(nskb) + doffset;
4821 			} else {
4822 				if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
4823 					goto err;
4824 			}
4825 			continue;
4826 		}
4827 
4828 		nskb_frag = skb_shinfo(nskb)->frags;
4829 
4830 		skb_copy_from_linear_data_offset(head_skb, offset,
4831 						 skb_put(nskb, hsize), hsize);
4832 
4833 		skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4834 					   SKBFL_SHARED_FRAG;
4835 
4836 		if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4837 			goto err;
4838 
4839 		while (pos < offset + len) {
4840 			if (i >= nfrags) {
4841 				if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
4842 				    skb_zerocopy_clone(nskb, list_skb,
4843 						       GFP_ATOMIC))
4844 					goto err;
4845 
4846 				i = 0;
4847 				nfrags = skb_shinfo(list_skb)->nr_frags;
4848 				frag = skb_shinfo(list_skb)->frags;
4849 				frag_skb = list_skb;
4850 				if (!skb_headlen(list_skb)) {
4851 					BUG_ON(!nfrags);
4852 				} else {
4853 					BUG_ON(!list_skb->head_frag);
4854 
4855 					/* to make room for head_frag. */
4856 					i--;
4857 					frag--;
4858 				}
4859 
4860 				list_skb = list_skb->next;
4861 			}
4862 
4863 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
4864 				     MAX_SKB_FRAGS)) {
4865 				net_warn_ratelimited(
4866 					"skb_segment: too many frags: %u %u\n",
4867 					pos, mss);
4868 				err = -EINVAL;
4869 				goto err;
4870 			}
4871 
4872 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4873 			__skb_frag_ref(nskb_frag);
4874 			size = skb_frag_size(nskb_frag);
4875 
4876 			if (pos < offset) {
4877 				skb_frag_off_add(nskb_frag, offset - pos);
4878 				skb_frag_size_sub(nskb_frag, offset - pos);
4879 			}
4880 
4881 			skb_shinfo(nskb)->nr_frags++;
4882 
4883 			if (pos + size <= offset + len) {
4884 				i++;
4885 				frag++;
4886 				pos += size;
4887 			} else {
4888 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
4889 				goto skip_fraglist;
4890 			}
4891 
4892 			nskb_frag++;
4893 		}
4894 
4895 skip_fraglist:
4896 		nskb->data_len = len - hsize;
4897 		nskb->len += nskb->data_len;
4898 		nskb->truesize += nskb->data_len;
4899 
4900 perform_csum_check:
4901 		if (!csum) {
4902 			if (skb_has_shared_frag(nskb) &&
4903 			    __skb_linearize(nskb))
4904 				goto err;
4905 
4906 			if (!nskb->remcsum_offload)
4907 				nskb->ip_summed = CHECKSUM_NONE;
4908 			SKB_GSO_CB(nskb)->csum =
4909 				skb_checksum(nskb, doffset,
4910 					     nskb->len - doffset, 0);
4911 			SKB_GSO_CB(nskb)->csum_start =
4912 				skb_headroom(nskb) + doffset;
4913 		}
4914 	} while ((offset += len) < head_skb->len);
4915 
4916 	/* Some callers want to get the end of the list.
4917 	 * Put it in segs->prev to avoid walking the list.
4918 	 * (see validate_xmit_skb_list() for example)
4919 	 */
4920 	segs->prev = tail;
4921 
4922 	if (partial_segs) {
4923 		struct sk_buff *iter;
4924 		int type = skb_shinfo(head_skb)->gso_type;
4925 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4926 
4927 		/* Update type to add partial and then remove dodgy if set */
4928 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4929 		type &= ~SKB_GSO_DODGY;
4930 
4931 		/* Update GSO info and prepare to start updating headers on
4932 		 * our way back down the stack of protocols.
4933 		 */
4934 		for (iter = segs; iter; iter = iter->next) {
4935 			skb_shinfo(iter)->gso_size = gso_size;
4936 			skb_shinfo(iter)->gso_segs = partial_segs;
4937 			skb_shinfo(iter)->gso_type = type;
4938 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4939 		}
4940 
4941 		if (tail->len - doffset <= gso_size)
4942 			skb_shinfo(tail)->gso_size = 0;
4943 		else if (tail != segs)
4944 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4945 	}
4946 
4947 	/* Following permits correct backpressure, for protocols
4948 	 * using skb_set_owner_w().
4949 	 * Idea is to tranfert ownership from head_skb to last segment.
4950 	 */
4951 	if (head_skb->destructor == sock_wfree) {
4952 		swap(tail->truesize, head_skb->truesize);
4953 		swap(tail->destructor, head_skb->destructor);
4954 		swap(tail->sk, head_skb->sk);
4955 	}
4956 	return segs;
4957 
4958 err:
4959 	kfree_skb_list(segs);
4960 	return ERR_PTR(err);
4961 }
4962 EXPORT_SYMBOL_GPL(skb_segment);
4963 
4964 #ifdef CONFIG_SKB_EXTENSIONS
4965 #define SKB_EXT_ALIGN_VALUE	8
4966 #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4967 
4968 static const u8 skb_ext_type_len[] = {
4969 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4970 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4971 #endif
4972 #ifdef CONFIG_XFRM
4973 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4974 #endif
4975 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4976 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4977 #endif
4978 #if IS_ENABLED(CONFIG_MPTCP)
4979 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4980 #endif
4981 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4982 	[SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
4983 #endif
4984 };
4985 
4986 static __always_inline unsigned int skb_ext_total_length(void)
4987 {
4988 	unsigned int l = SKB_EXT_CHUNKSIZEOF(struct skb_ext);
4989 	int i;
4990 
4991 	for (i = 0; i < ARRAY_SIZE(skb_ext_type_len); i++)
4992 		l += skb_ext_type_len[i];
4993 
4994 	return l;
4995 }
4996 
4997 static void skb_extensions_init(void)
4998 {
4999 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
5000 #if !IS_ENABLED(CONFIG_KCOV_INSTRUMENT_ALL)
5001 	BUILD_BUG_ON(skb_ext_total_length() > 255);
5002 #endif
5003 
5004 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
5005 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
5006 					     0,
5007 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
5008 					     NULL);
5009 }
5010 #else
5011 static void skb_extensions_init(void) {}
5012 #endif
5013 
5014 /* The SKB kmem_cache slab is critical for network performance.  Never
5015  * merge/alias the slab with similar sized objects.  This avoids fragmentation
5016  * that hurts performance of kmem_cache_{alloc,free}_bulk APIs.
5017  */
5018 #ifndef CONFIG_SLUB_TINY
5019 #define FLAG_SKB_NO_MERGE	SLAB_NO_MERGE
5020 #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */
5021 #define FLAG_SKB_NO_MERGE	0
5022 #endif
5023 
5024 void __init skb_init(void)
5025 {
5026 	net_hotdata.skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache",
5027 					      sizeof(struct sk_buff),
5028 					      0,
5029 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC|
5030 						FLAG_SKB_NO_MERGE,
5031 					      offsetof(struct sk_buff, cb),
5032 					      sizeof_field(struct sk_buff, cb),
5033 					      NULL);
5034 	net_hotdata.skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
5035 						sizeof(struct sk_buff_fclones),
5036 						0,
5037 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
5038 						NULL);
5039 	/* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes.
5040 	 * struct skb_shared_info is located at the end of skb->head,
5041 	 * and should not be copied to/from user.
5042 	 */
5043 	net_hotdata.skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head",
5044 						SKB_SMALL_HEAD_CACHE_SIZE,
5045 						0,
5046 						SLAB_HWCACHE_ALIGN | SLAB_PANIC,
5047 						0,
5048 						SKB_SMALL_HEAD_HEADROOM,
5049 						NULL);
5050 	skb_extensions_init();
5051 }
5052 
5053 static int
5054 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
5055 	       unsigned int recursion_level)
5056 {
5057 	int start = skb_headlen(skb);
5058 	int i, copy = start - offset;
5059 	struct sk_buff *frag_iter;
5060 	int elt = 0;
5061 
5062 	if (unlikely(recursion_level >= 24))
5063 		return -EMSGSIZE;
5064 
5065 	if (copy > 0) {
5066 		if (copy > len)
5067 			copy = len;
5068 		sg_set_buf(sg, skb->data + offset, copy);
5069 		elt++;
5070 		if ((len -= copy) == 0)
5071 			return elt;
5072 		offset += copy;
5073 	}
5074 
5075 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5076 		int end;
5077 
5078 		WARN_ON(start > offset + len);
5079 
5080 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
5081 		if ((copy = end - offset) > 0) {
5082 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5083 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
5084 				return -EMSGSIZE;
5085 
5086 			if (copy > len)
5087 				copy = len;
5088 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
5089 				    skb_frag_off(frag) + offset - start);
5090 			elt++;
5091 			if (!(len -= copy))
5092 				return elt;
5093 			offset += copy;
5094 		}
5095 		start = end;
5096 	}
5097 
5098 	skb_walk_frags(skb, frag_iter) {
5099 		int end, ret;
5100 
5101 		WARN_ON(start > offset + len);
5102 
5103 		end = start + frag_iter->len;
5104 		if ((copy = end - offset) > 0) {
5105 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
5106 				return -EMSGSIZE;
5107 
5108 			if (copy > len)
5109 				copy = len;
5110 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
5111 					      copy, recursion_level + 1);
5112 			if (unlikely(ret < 0))
5113 				return ret;
5114 			elt += ret;
5115 			if ((len -= copy) == 0)
5116 				return elt;
5117 			offset += copy;
5118 		}
5119 		start = end;
5120 	}
5121 	BUG_ON(len);
5122 	return elt;
5123 }
5124 
5125 /**
5126  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
5127  *	@skb: Socket buffer containing the buffers to be mapped
5128  *	@sg: The scatter-gather list to map into
5129  *	@offset: The offset into the buffer's contents to start mapping
5130  *	@len: Length of buffer space to be mapped
5131  *
5132  *	Fill the specified scatter-gather list with mappings/pointers into a
5133  *	region of the buffer space attached to a socket buffer. Returns either
5134  *	the number of scatterlist items used, or -EMSGSIZE if the contents
5135  *	could not fit.
5136  */
5137 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
5138 {
5139 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
5140 
5141 	if (nsg <= 0)
5142 		return nsg;
5143 
5144 	sg_mark_end(&sg[nsg - 1]);
5145 
5146 	return nsg;
5147 }
5148 EXPORT_SYMBOL_GPL(skb_to_sgvec);
5149 
5150 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
5151  * sglist without mark the sg which contain last skb data as the end.
5152  * So the caller can mannipulate sg list as will when padding new data after
5153  * the first call without calling sg_unmark_end to expend sg list.
5154  *
5155  * Scenario to use skb_to_sgvec_nomark:
5156  * 1. sg_init_table
5157  * 2. skb_to_sgvec_nomark(payload1)
5158  * 3. skb_to_sgvec_nomark(payload2)
5159  *
5160  * This is equivalent to:
5161  * 1. sg_init_table
5162  * 2. skb_to_sgvec(payload1)
5163  * 3. sg_unmark_end
5164  * 4. skb_to_sgvec(payload2)
5165  *
5166  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
5167  * is more preferable.
5168  */
5169 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
5170 			int offset, int len)
5171 {
5172 	return __skb_to_sgvec(skb, sg, offset, len, 0);
5173 }
5174 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
5175 
5176 
5177 
5178 /**
5179  *	skb_cow_data - Check that a socket buffer's data buffers are writable
5180  *	@skb: The socket buffer to check.
5181  *	@tailbits: Amount of trailing space to be added
5182  *	@trailer: Returned pointer to the skb where the @tailbits space begins
5183  *
5184  *	Make sure that the data buffers attached to a socket buffer are
5185  *	writable. If they are not, private copies are made of the data buffers
5186  *	and the socket buffer is set to use these instead.
5187  *
5188  *	If @tailbits is given, make sure that there is space to write @tailbits
5189  *	bytes of data beyond current end of socket buffer.  @trailer will be
5190  *	set to point to the skb in which this space begins.
5191  *
5192  *	The number of scatterlist elements required to completely map the
5193  *	COW'd and extended socket buffer will be returned.
5194  */
5195 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
5196 {
5197 	int copyflag;
5198 	int elt;
5199 	struct sk_buff *skb1, **skb_p;
5200 
5201 	/* If skb is cloned or its head is paged, reallocate
5202 	 * head pulling out all the pages (pages are considered not writable
5203 	 * at the moment even if they are anonymous).
5204 	 */
5205 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
5206 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
5207 		return -ENOMEM;
5208 
5209 	/* Easy case. Most of packets will go this way. */
5210 	if (!skb_has_frag_list(skb)) {
5211 		/* A little of trouble, not enough of space for trailer.
5212 		 * This should not happen, when stack is tuned to generate
5213 		 * good frames. OK, on miss we reallocate and reserve even more
5214 		 * space, 128 bytes is fair. */
5215 
5216 		if (skb_tailroom(skb) < tailbits &&
5217 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
5218 			return -ENOMEM;
5219 
5220 		/* Voila! */
5221 		*trailer = skb;
5222 		return 1;
5223 	}
5224 
5225 	/* Misery. We are in troubles, going to mincer fragments... */
5226 
5227 	elt = 1;
5228 	skb_p = &skb_shinfo(skb)->frag_list;
5229 	copyflag = 0;
5230 
5231 	while ((skb1 = *skb_p) != NULL) {
5232 		int ntail = 0;
5233 
5234 		/* The fragment is partially pulled by someone,
5235 		 * this can happen on input. Copy it and everything
5236 		 * after it. */
5237 
5238 		if (skb_shared(skb1))
5239 			copyflag = 1;
5240 
5241 		/* If the skb is the last, worry about trailer. */
5242 
5243 		if (skb1->next == NULL && tailbits) {
5244 			if (skb_shinfo(skb1)->nr_frags ||
5245 			    skb_has_frag_list(skb1) ||
5246 			    skb_tailroom(skb1) < tailbits)
5247 				ntail = tailbits + 128;
5248 		}
5249 
5250 		if (copyflag ||
5251 		    skb_cloned(skb1) ||
5252 		    ntail ||
5253 		    skb_shinfo(skb1)->nr_frags ||
5254 		    skb_has_frag_list(skb1)) {
5255 			struct sk_buff *skb2;
5256 
5257 			/* Fuck, we are miserable poor guys... */
5258 			if (ntail == 0)
5259 				skb2 = skb_copy(skb1, GFP_ATOMIC);
5260 			else
5261 				skb2 = skb_copy_expand(skb1,
5262 						       skb_headroom(skb1),
5263 						       ntail,
5264 						       GFP_ATOMIC);
5265 			if (unlikely(skb2 == NULL))
5266 				return -ENOMEM;
5267 
5268 			if (skb1->sk)
5269 				skb_set_owner_w(skb2, skb1->sk);
5270 
5271 			/* Looking around. Are we still alive?
5272 			 * OK, link new skb, drop old one */
5273 
5274 			skb2->next = skb1->next;
5275 			*skb_p = skb2;
5276 			kfree_skb(skb1);
5277 			skb1 = skb2;
5278 		}
5279 		elt++;
5280 		*trailer = skb1;
5281 		skb_p = &skb1->next;
5282 	}
5283 
5284 	return elt;
5285 }
5286 EXPORT_SYMBOL_GPL(skb_cow_data);
5287 
5288 static void sock_rmem_free(struct sk_buff *skb)
5289 {
5290 	struct sock *sk = skb->sk;
5291 
5292 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
5293 }
5294 
5295 static void skb_set_err_queue(struct sk_buff *skb)
5296 {
5297 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
5298 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
5299 	 */
5300 	skb->pkt_type = PACKET_OUTGOING;
5301 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
5302 }
5303 
5304 /*
5305  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
5306  */
5307 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
5308 {
5309 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
5310 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
5311 		return -ENOMEM;
5312 
5313 	skb_orphan(skb);
5314 	skb->sk = sk;
5315 	skb->destructor = sock_rmem_free;
5316 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
5317 	skb_set_err_queue(skb);
5318 
5319 	/* before exiting rcu section, make sure dst is refcounted */
5320 	skb_dst_force(skb);
5321 
5322 	skb_queue_tail(&sk->sk_error_queue, skb);
5323 	if (!sock_flag(sk, SOCK_DEAD))
5324 		sk_error_report(sk);
5325 	return 0;
5326 }
5327 EXPORT_SYMBOL(sock_queue_err_skb);
5328 
5329 static bool is_icmp_err_skb(const struct sk_buff *skb)
5330 {
5331 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
5332 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
5333 }
5334 
5335 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
5336 {
5337 	struct sk_buff_head *q = &sk->sk_error_queue;
5338 	struct sk_buff *skb, *skb_next = NULL;
5339 	bool icmp_next = false;
5340 	unsigned long flags;
5341 
5342 	if (skb_queue_empty_lockless(q))
5343 		return NULL;
5344 
5345 	spin_lock_irqsave(&q->lock, flags);
5346 	skb = __skb_dequeue(q);
5347 	if (skb && (skb_next = skb_peek(q))) {
5348 		icmp_next = is_icmp_err_skb(skb_next);
5349 		if (icmp_next)
5350 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
5351 	}
5352 	spin_unlock_irqrestore(&q->lock, flags);
5353 
5354 	if (is_icmp_err_skb(skb) && !icmp_next)
5355 		sk->sk_err = 0;
5356 
5357 	if (skb_next)
5358 		sk_error_report(sk);
5359 
5360 	return skb;
5361 }
5362 EXPORT_SYMBOL(sock_dequeue_err_skb);
5363 
5364 /**
5365  * skb_clone_sk - create clone of skb, and take reference to socket
5366  * @skb: the skb to clone
5367  *
5368  * This function creates a clone of a buffer that holds a reference on
5369  * sk_refcnt.  Buffers created via this function are meant to be
5370  * returned using sock_queue_err_skb, or free via kfree_skb.
5371  *
5372  * When passing buffers allocated with this function to sock_queue_err_skb
5373  * it is necessary to wrap the call with sock_hold/sock_put in order to
5374  * prevent the socket from being released prior to being enqueued on
5375  * the sk_error_queue.
5376  */
5377 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
5378 {
5379 	struct sock *sk = skb->sk;
5380 	struct sk_buff *clone;
5381 
5382 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
5383 		return NULL;
5384 
5385 	clone = skb_clone(skb, GFP_ATOMIC);
5386 	if (!clone) {
5387 		sock_put(sk);
5388 		return NULL;
5389 	}
5390 
5391 	clone->sk = sk;
5392 	clone->destructor = sock_efree;
5393 
5394 	return clone;
5395 }
5396 EXPORT_SYMBOL(skb_clone_sk);
5397 
5398 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
5399 					struct sock *sk,
5400 					int tstype,
5401 					bool opt_stats)
5402 {
5403 	struct sock_exterr_skb *serr;
5404 	int err;
5405 
5406 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
5407 
5408 	serr = SKB_EXT_ERR(skb);
5409 	memset(serr, 0, sizeof(*serr));
5410 	serr->ee.ee_errno = ENOMSG;
5411 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
5412 	serr->ee.ee_info = tstype;
5413 	serr->opt_stats = opt_stats;
5414 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
5415 	if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) {
5416 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
5417 		if (sk_is_tcp(sk))
5418 			serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
5419 	}
5420 
5421 	err = sock_queue_err_skb(sk, skb);
5422 
5423 	if (err)
5424 		kfree_skb(skb);
5425 }
5426 
5427 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
5428 {
5429 	bool ret;
5430 
5431 	if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
5432 		return true;
5433 
5434 	read_lock_bh(&sk->sk_callback_lock);
5435 	ret = sk->sk_socket && sk->sk_socket->file &&
5436 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
5437 	read_unlock_bh(&sk->sk_callback_lock);
5438 	return ret;
5439 }
5440 
5441 void skb_complete_tx_timestamp(struct sk_buff *skb,
5442 			       struct skb_shared_hwtstamps *hwtstamps)
5443 {
5444 	struct sock *sk = skb->sk;
5445 
5446 	if (!skb_may_tx_timestamp(sk, false))
5447 		goto err;
5448 
5449 	/* Take a reference to prevent skb_orphan() from freeing the socket,
5450 	 * but only if the socket refcount is not zero.
5451 	 */
5452 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5453 		*skb_hwtstamps(skb) = *hwtstamps;
5454 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
5455 		sock_put(sk);
5456 		return;
5457 	}
5458 
5459 err:
5460 	kfree_skb(skb);
5461 }
5462 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
5463 
5464 void __skb_tstamp_tx(struct sk_buff *orig_skb,
5465 		     const struct sk_buff *ack_skb,
5466 		     struct skb_shared_hwtstamps *hwtstamps,
5467 		     struct sock *sk, int tstype)
5468 {
5469 	struct sk_buff *skb;
5470 	bool tsonly, opt_stats = false;
5471 	u32 tsflags;
5472 
5473 	if (!sk)
5474 		return;
5475 
5476 	tsflags = READ_ONCE(sk->sk_tsflags);
5477 	if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
5478 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
5479 		return;
5480 
5481 	tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
5482 	if (!skb_may_tx_timestamp(sk, tsonly))
5483 		return;
5484 
5485 	if (tsonly) {
5486 #ifdef CONFIG_INET
5487 		if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
5488 		    sk_is_tcp(sk)) {
5489 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
5490 							     ack_skb);
5491 			opt_stats = true;
5492 		} else
5493 #endif
5494 			skb = alloc_skb(0, GFP_ATOMIC);
5495 	} else {
5496 		skb = skb_clone(orig_skb, GFP_ATOMIC);
5497 
5498 		if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) {
5499 			kfree_skb(skb);
5500 			return;
5501 		}
5502 	}
5503 	if (!skb)
5504 		return;
5505 
5506 	if (tsonly) {
5507 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
5508 					     SKBTX_ANY_TSTAMP;
5509 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
5510 	}
5511 
5512 	if (hwtstamps)
5513 		*skb_hwtstamps(skb) = *hwtstamps;
5514 	else
5515 		__net_timestamp(skb);
5516 
5517 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
5518 }
5519 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
5520 
5521 void skb_tstamp_tx(struct sk_buff *orig_skb,
5522 		   struct skb_shared_hwtstamps *hwtstamps)
5523 {
5524 	return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
5525 			       SCM_TSTAMP_SND);
5526 }
5527 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
5528 
5529 #ifdef CONFIG_WIRELESS
5530 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
5531 {
5532 	struct sock *sk = skb->sk;
5533 	struct sock_exterr_skb *serr;
5534 	int err = 1;
5535 
5536 	skb->wifi_acked_valid = 1;
5537 	skb->wifi_acked = acked;
5538 
5539 	serr = SKB_EXT_ERR(skb);
5540 	memset(serr, 0, sizeof(*serr));
5541 	serr->ee.ee_errno = ENOMSG;
5542 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
5543 
5544 	/* Take a reference to prevent skb_orphan() from freeing the socket,
5545 	 * but only if the socket refcount is not zero.
5546 	 */
5547 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5548 		err = sock_queue_err_skb(sk, skb);
5549 		sock_put(sk);
5550 	}
5551 	if (err)
5552 		kfree_skb(skb);
5553 }
5554 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5555 #endif /* CONFIG_WIRELESS */
5556 
5557 /**
5558  * skb_partial_csum_set - set up and verify partial csum values for packet
5559  * @skb: the skb to set
5560  * @start: the number of bytes after skb->data to start checksumming.
5561  * @off: the offset from start to place the checksum.
5562  *
5563  * For untrusted partially-checksummed packets, we need to make sure the values
5564  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5565  *
5566  * This function checks and sets those values and skb->ip_summed: if this
5567  * returns false you should drop the packet.
5568  */
5569 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5570 {
5571 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
5572 	u32 csum_start = skb_headroom(skb) + (u32)start;
5573 
5574 	if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) {
5575 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
5576 				     start, off, skb_headroom(skb), skb_headlen(skb));
5577 		return false;
5578 	}
5579 	skb->ip_summed = CHECKSUM_PARTIAL;
5580 	skb->csum_start = csum_start;
5581 	skb->csum_offset = off;
5582 	skb->transport_header = csum_start;
5583 	return true;
5584 }
5585 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5586 
5587 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5588 			       unsigned int max)
5589 {
5590 	if (skb_headlen(skb) >= len)
5591 		return 0;
5592 
5593 	/* If we need to pullup then pullup to the max, so we
5594 	 * won't need to do it again.
5595 	 */
5596 	if (max > skb->len)
5597 		max = skb->len;
5598 
5599 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5600 		return -ENOMEM;
5601 
5602 	if (skb_headlen(skb) < len)
5603 		return -EPROTO;
5604 
5605 	return 0;
5606 }
5607 
5608 #define MAX_TCP_HDR_LEN (15 * 4)
5609 
5610 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5611 				      typeof(IPPROTO_IP) proto,
5612 				      unsigned int off)
5613 {
5614 	int err;
5615 
5616 	switch (proto) {
5617 	case IPPROTO_TCP:
5618 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5619 					  off + MAX_TCP_HDR_LEN);
5620 		if (!err && !skb_partial_csum_set(skb, off,
5621 						  offsetof(struct tcphdr,
5622 							   check)))
5623 			err = -EPROTO;
5624 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5625 
5626 	case IPPROTO_UDP:
5627 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5628 					  off + sizeof(struct udphdr));
5629 		if (!err && !skb_partial_csum_set(skb, off,
5630 						  offsetof(struct udphdr,
5631 							   check)))
5632 			err = -EPROTO;
5633 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5634 	}
5635 
5636 	return ERR_PTR(-EPROTO);
5637 }
5638 
5639 /* This value should be large enough to cover a tagged ethernet header plus
5640  * maximally sized IP and TCP or UDP headers.
5641  */
5642 #define MAX_IP_HDR_LEN 128
5643 
5644 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5645 {
5646 	unsigned int off;
5647 	bool fragment;
5648 	__sum16 *csum;
5649 	int err;
5650 
5651 	fragment = false;
5652 
5653 	err = skb_maybe_pull_tail(skb,
5654 				  sizeof(struct iphdr),
5655 				  MAX_IP_HDR_LEN);
5656 	if (err < 0)
5657 		goto out;
5658 
5659 	if (ip_is_fragment(ip_hdr(skb)))
5660 		fragment = true;
5661 
5662 	off = ip_hdrlen(skb);
5663 
5664 	err = -EPROTO;
5665 
5666 	if (fragment)
5667 		goto out;
5668 
5669 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5670 	if (IS_ERR(csum))
5671 		return PTR_ERR(csum);
5672 
5673 	if (recalculate)
5674 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5675 					   ip_hdr(skb)->daddr,
5676 					   skb->len - off,
5677 					   ip_hdr(skb)->protocol, 0);
5678 	err = 0;
5679 
5680 out:
5681 	return err;
5682 }
5683 
5684 /* This value should be large enough to cover a tagged ethernet header plus
5685  * an IPv6 header, all options, and a maximal TCP or UDP header.
5686  */
5687 #define MAX_IPV6_HDR_LEN 256
5688 
5689 #define OPT_HDR(type, skb, off) \
5690 	(type *)(skb_network_header(skb) + (off))
5691 
5692 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5693 {
5694 	int err;
5695 	u8 nexthdr;
5696 	unsigned int off;
5697 	unsigned int len;
5698 	bool fragment;
5699 	bool done;
5700 	__sum16 *csum;
5701 
5702 	fragment = false;
5703 	done = false;
5704 
5705 	off = sizeof(struct ipv6hdr);
5706 
5707 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5708 	if (err < 0)
5709 		goto out;
5710 
5711 	nexthdr = ipv6_hdr(skb)->nexthdr;
5712 
5713 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5714 	while (off <= len && !done) {
5715 		switch (nexthdr) {
5716 		case IPPROTO_DSTOPTS:
5717 		case IPPROTO_HOPOPTS:
5718 		case IPPROTO_ROUTING: {
5719 			struct ipv6_opt_hdr *hp;
5720 
5721 			err = skb_maybe_pull_tail(skb,
5722 						  off +
5723 						  sizeof(struct ipv6_opt_hdr),
5724 						  MAX_IPV6_HDR_LEN);
5725 			if (err < 0)
5726 				goto out;
5727 
5728 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5729 			nexthdr = hp->nexthdr;
5730 			off += ipv6_optlen(hp);
5731 			break;
5732 		}
5733 		case IPPROTO_AH: {
5734 			struct ip_auth_hdr *hp;
5735 
5736 			err = skb_maybe_pull_tail(skb,
5737 						  off +
5738 						  sizeof(struct ip_auth_hdr),
5739 						  MAX_IPV6_HDR_LEN);
5740 			if (err < 0)
5741 				goto out;
5742 
5743 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5744 			nexthdr = hp->nexthdr;
5745 			off += ipv6_authlen(hp);
5746 			break;
5747 		}
5748 		case IPPROTO_FRAGMENT: {
5749 			struct frag_hdr *hp;
5750 
5751 			err = skb_maybe_pull_tail(skb,
5752 						  off +
5753 						  sizeof(struct frag_hdr),
5754 						  MAX_IPV6_HDR_LEN);
5755 			if (err < 0)
5756 				goto out;
5757 
5758 			hp = OPT_HDR(struct frag_hdr, skb, off);
5759 
5760 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5761 				fragment = true;
5762 
5763 			nexthdr = hp->nexthdr;
5764 			off += sizeof(struct frag_hdr);
5765 			break;
5766 		}
5767 		default:
5768 			done = true;
5769 			break;
5770 		}
5771 	}
5772 
5773 	err = -EPROTO;
5774 
5775 	if (!done || fragment)
5776 		goto out;
5777 
5778 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
5779 	if (IS_ERR(csum))
5780 		return PTR_ERR(csum);
5781 
5782 	if (recalculate)
5783 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5784 					 &ipv6_hdr(skb)->daddr,
5785 					 skb->len - off, nexthdr, 0);
5786 	err = 0;
5787 
5788 out:
5789 	return err;
5790 }
5791 
5792 /**
5793  * skb_checksum_setup - set up partial checksum offset
5794  * @skb: the skb to set up
5795  * @recalculate: if true the pseudo-header checksum will be recalculated
5796  */
5797 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5798 {
5799 	int err;
5800 
5801 	switch (skb->protocol) {
5802 	case htons(ETH_P_IP):
5803 		err = skb_checksum_setup_ipv4(skb, recalculate);
5804 		break;
5805 
5806 	case htons(ETH_P_IPV6):
5807 		err = skb_checksum_setup_ipv6(skb, recalculate);
5808 		break;
5809 
5810 	default:
5811 		err = -EPROTO;
5812 		break;
5813 	}
5814 
5815 	return err;
5816 }
5817 EXPORT_SYMBOL(skb_checksum_setup);
5818 
5819 /**
5820  * skb_checksum_maybe_trim - maybe trims the given skb
5821  * @skb: the skb to check
5822  * @transport_len: the data length beyond the network header
5823  *
5824  * Checks whether the given skb has data beyond the given transport length.
5825  * If so, returns a cloned skb trimmed to this transport length.
5826  * Otherwise returns the provided skb. Returns NULL in error cases
5827  * (e.g. transport_len exceeds skb length or out-of-memory).
5828  *
5829  * Caller needs to set the skb transport header and free any returned skb if it
5830  * differs from the provided skb.
5831  */
5832 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5833 					       unsigned int transport_len)
5834 {
5835 	struct sk_buff *skb_chk;
5836 	unsigned int len = skb_transport_offset(skb) + transport_len;
5837 	int ret;
5838 
5839 	if (skb->len < len)
5840 		return NULL;
5841 	else if (skb->len == len)
5842 		return skb;
5843 
5844 	skb_chk = skb_clone(skb, GFP_ATOMIC);
5845 	if (!skb_chk)
5846 		return NULL;
5847 
5848 	ret = pskb_trim_rcsum(skb_chk, len);
5849 	if (ret) {
5850 		kfree_skb(skb_chk);
5851 		return NULL;
5852 	}
5853 
5854 	return skb_chk;
5855 }
5856 
5857 /**
5858  * skb_checksum_trimmed - validate checksum of an skb
5859  * @skb: the skb to check
5860  * @transport_len: the data length beyond the network header
5861  * @skb_chkf: checksum function to use
5862  *
5863  * Applies the given checksum function skb_chkf to the provided skb.
5864  * Returns a checked and maybe trimmed skb. Returns NULL on error.
5865  *
5866  * If the skb has data beyond the given transport length, then a
5867  * trimmed & cloned skb is checked and returned.
5868  *
5869  * Caller needs to set the skb transport header and free any returned skb if it
5870  * differs from the provided skb.
5871  */
5872 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5873 				     unsigned int transport_len,
5874 				     __sum16(*skb_chkf)(struct sk_buff *skb))
5875 {
5876 	struct sk_buff *skb_chk;
5877 	unsigned int offset = skb_transport_offset(skb);
5878 	__sum16 ret;
5879 
5880 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5881 	if (!skb_chk)
5882 		goto err;
5883 
5884 	if (!pskb_may_pull(skb_chk, offset))
5885 		goto err;
5886 
5887 	skb_pull_rcsum(skb_chk, offset);
5888 	ret = skb_chkf(skb_chk);
5889 	skb_push_rcsum(skb_chk, offset);
5890 
5891 	if (ret)
5892 		goto err;
5893 
5894 	return skb_chk;
5895 
5896 err:
5897 	if (skb_chk && skb_chk != skb)
5898 		kfree_skb(skb_chk);
5899 
5900 	return NULL;
5901 
5902 }
5903 EXPORT_SYMBOL(skb_checksum_trimmed);
5904 
5905 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5906 {
5907 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5908 			     skb->dev->name);
5909 }
5910 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5911 
5912 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5913 {
5914 	if (head_stolen) {
5915 		skb_release_head_state(skb);
5916 		kmem_cache_free(net_hotdata.skbuff_cache, skb);
5917 	} else {
5918 		__kfree_skb(skb);
5919 	}
5920 }
5921 EXPORT_SYMBOL(kfree_skb_partial);
5922 
5923 /**
5924  * skb_try_coalesce - try to merge skb to prior one
5925  * @to: prior buffer
5926  * @from: buffer to add
5927  * @fragstolen: pointer to boolean
5928  * @delta_truesize: how much more was allocated than was requested
5929  */
5930 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5931 		      bool *fragstolen, int *delta_truesize)
5932 {
5933 	struct skb_shared_info *to_shinfo, *from_shinfo;
5934 	int i, delta, len = from->len;
5935 
5936 	*fragstolen = false;
5937 
5938 	if (skb_cloned(to))
5939 		return false;
5940 
5941 	/* In general, avoid mixing page_pool and non-page_pool allocated
5942 	 * pages within the same SKB. In theory we could take full
5943 	 * references if @from is cloned and !@to->pp_recycle but its
5944 	 * tricky (due to potential race with the clone disappearing) and
5945 	 * rare, so not worth dealing with.
5946 	 */
5947 	if (to->pp_recycle != from->pp_recycle)
5948 		return false;
5949 
5950 	if (len <= skb_tailroom(to)) {
5951 		if (len)
5952 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5953 		*delta_truesize = 0;
5954 		return true;
5955 	}
5956 
5957 	to_shinfo = skb_shinfo(to);
5958 	from_shinfo = skb_shinfo(from);
5959 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5960 		return false;
5961 	if (skb_zcopy(to) || skb_zcopy(from))
5962 		return false;
5963 
5964 	if (skb_headlen(from) != 0) {
5965 		struct page *page;
5966 		unsigned int offset;
5967 
5968 		if (to_shinfo->nr_frags +
5969 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5970 			return false;
5971 
5972 		if (skb_head_is_locked(from))
5973 			return false;
5974 
5975 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5976 
5977 		page = virt_to_head_page(from->head);
5978 		offset = from->data - (unsigned char *)page_address(page);
5979 
5980 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5981 				   page, offset, skb_headlen(from));
5982 		*fragstolen = true;
5983 	} else {
5984 		if (to_shinfo->nr_frags +
5985 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5986 			return false;
5987 
5988 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5989 	}
5990 
5991 	WARN_ON_ONCE(delta < len);
5992 
5993 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5994 	       from_shinfo->frags,
5995 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5996 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5997 
5998 	if (!skb_cloned(from))
5999 		from_shinfo->nr_frags = 0;
6000 
6001 	/* if the skb is not cloned this does nothing
6002 	 * since we set nr_frags to 0.
6003 	 */
6004 	if (skb_pp_frag_ref(from)) {
6005 		for (i = 0; i < from_shinfo->nr_frags; i++)
6006 			__skb_frag_ref(&from_shinfo->frags[i]);
6007 	}
6008 
6009 	to->truesize += delta;
6010 	to->len += len;
6011 	to->data_len += len;
6012 
6013 	*delta_truesize = delta;
6014 	return true;
6015 }
6016 EXPORT_SYMBOL(skb_try_coalesce);
6017 
6018 /**
6019  * skb_scrub_packet - scrub an skb
6020  *
6021  * @skb: buffer to clean
6022  * @xnet: packet is crossing netns
6023  *
6024  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
6025  * into/from a tunnel. Some information have to be cleared during these
6026  * operations.
6027  * skb_scrub_packet can also be used to clean a skb before injecting it in
6028  * another namespace (@xnet == true). We have to clear all information in the
6029  * skb that could impact namespace isolation.
6030  */
6031 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
6032 {
6033 	skb->pkt_type = PACKET_HOST;
6034 	skb->skb_iif = 0;
6035 	skb->ignore_df = 0;
6036 	skb_dst_drop(skb);
6037 	skb_ext_reset(skb);
6038 	nf_reset_ct(skb);
6039 	nf_reset_trace(skb);
6040 
6041 #ifdef CONFIG_NET_SWITCHDEV
6042 	skb->offload_fwd_mark = 0;
6043 	skb->offload_l3_fwd_mark = 0;
6044 #endif
6045 
6046 	if (!xnet)
6047 		return;
6048 
6049 	ipvs_reset(skb);
6050 	skb->mark = 0;
6051 	skb_clear_tstamp(skb);
6052 }
6053 EXPORT_SYMBOL_GPL(skb_scrub_packet);
6054 
6055 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
6056 {
6057 	int mac_len, meta_len;
6058 	void *meta;
6059 
6060 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
6061 		kfree_skb(skb);
6062 		return NULL;
6063 	}
6064 
6065 	mac_len = skb->data - skb_mac_header(skb);
6066 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
6067 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
6068 			mac_len - VLAN_HLEN - ETH_TLEN);
6069 	}
6070 
6071 	meta_len = skb_metadata_len(skb);
6072 	if (meta_len) {
6073 		meta = skb_metadata_end(skb) - meta_len;
6074 		memmove(meta + VLAN_HLEN, meta, meta_len);
6075 	}
6076 
6077 	skb->mac_header += VLAN_HLEN;
6078 	return skb;
6079 }
6080 
6081 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
6082 {
6083 	struct vlan_hdr *vhdr;
6084 	u16 vlan_tci;
6085 
6086 	if (unlikely(skb_vlan_tag_present(skb))) {
6087 		/* vlan_tci is already set-up so leave this for another time */
6088 		return skb;
6089 	}
6090 
6091 	skb = skb_share_check(skb, GFP_ATOMIC);
6092 	if (unlikely(!skb))
6093 		goto err_free;
6094 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
6095 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
6096 		goto err_free;
6097 
6098 	vhdr = (struct vlan_hdr *)skb->data;
6099 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
6100 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
6101 
6102 	skb_pull_rcsum(skb, VLAN_HLEN);
6103 	vlan_set_encap_proto(skb, vhdr);
6104 
6105 	skb = skb_reorder_vlan_header(skb);
6106 	if (unlikely(!skb))
6107 		goto err_free;
6108 
6109 	skb_reset_network_header(skb);
6110 	if (!skb_transport_header_was_set(skb))
6111 		skb_reset_transport_header(skb);
6112 	skb_reset_mac_len(skb);
6113 
6114 	return skb;
6115 
6116 err_free:
6117 	kfree_skb(skb);
6118 	return NULL;
6119 }
6120 EXPORT_SYMBOL(skb_vlan_untag);
6121 
6122 int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
6123 {
6124 	if (!pskb_may_pull(skb, write_len))
6125 		return -ENOMEM;
6126 
6127 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
6128 		return 0;
6129 
6130 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
6131 }
6132 EXPORT_SYMBOL(skb_ensure_writable);
6133 
6134 int skb_ensure_writable_head_tail(struct sk_buff *skb, struct net_device *dev)
6135 {
6136 	int needed_headroom = dev->needed_headroom;
6137 	int needed_tailroom = dev->needed_tailroom;
6138 
6139 	/* For tail taggers, we need to pad short frames ourselves, to ensure
6140 	 * that the tail tag does not fail at its role of being at the end of
6141 	 * the packet, once the conduit interface pads the frame. Account for
6142 	 * that pad length here, and pad later.
6143 	 */
6144 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
6145 		needed_tailroom += ETH_ZLEN - skb->len;
6146 	/* skb_headroom() returns unsigned int... */
6147 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
6148 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
6149 
6150 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
6151 		/* No reallocation needed, yay! */
6152 		return 0;
6153 
6154 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
6155 				GFP_ATOMIC);
6156 }
6157 EXPORT_SYMBOL(skb_ensure_writable_head_tail);
6158 
6159 /* remove VLAN header from packet and update csum accordingly.
6160  * expects a non skb_vlan_tag_present skb with a vlan tag payload
6161  */
6162 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
6163 {
6164 	int offset = skb->data - skb_mac_header(skb);
6165 	int err;
6166 
6167 	if (WARN_ONCE(offset,
6168 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
6169 		      offset)) {
6170 		return -EINVAL;
6171 	}
6172 
6173 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
6174 	if (unlikely(err))
6175 		return err;
6176 
6177 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
6178 
6179 	vlan_remove_tag(skb, vlan_tci);
6180 
6181 	skb->mac_header += VLAN_HLEN;
6182 
6183 	if (skb_network_offset(skb) < ETH_HLEN)
6184 		skb_set_network_header(skb, ETH_HLEN);
6185 
6186 	skb_reset_mac_len(skb);
6187 
6188 	return err;
6189 }
6190 EXPORT_SYMBOL(__skb_vlan_pop);
6191 
6192 /* Pop a vlan tag either from hwaccel or from payload.
6193  * Expects skb->data at mac header.
6194  */
6195 int skb_vlan_pop(struct sk_buff *skb)
6196 {
6197 	u16 vlan_tci;
6198 	__be16 vlan_proto;
6199 	int err;
6200 
6201 	if (likely(skb_vlan_tag_present(skb))) {
6202 		__vlan_hwaccel_clear_tag(skb);
6203 	} else {
6204 		if (unlikely(!eth_type_vlan(skb->protocol)))
6205 			return 0;
6206 
6207 		err = __skb_vlan_pop(skb, &vlan_tci);
6208 		if (err)
6209 			return err;
6210 	}
6211 	/* move next vlan tag to hw accel tag */
6212 	if (likely(!eth_type_vlan(skb->protocol)))
6213 		return 0;
6214 
6215 	vlan_proto = skb->protocol;
6216 	err = __skb_vlan_pop(skb, &vlan_tci);
6217 	if (unlikely(err))
6218 		return err;
6219 
6220 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
6221 	return 0;
6222 }
6223 EXPORT_SYMBOL(skb_vlan_pop);
6224 
6225 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
6226  * Expects skb->data at mac header.
6227  */
6228 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
6229 {
6230 	if (skb_vlan_tag_present(skb)) {
6231 		int offset = skb->data - skb_mac_header(skb);
6232 		int err;
6233 
6234 		if (WARN_ONCE(offset,
6235 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
6236 			      offset)) {
6237 			return -EINVAL;
6238 		}
6239 
6240 		err = __vlan_insert_tag(skb, skb->vlan_proto,
6241 					skb_vlan_tag_get(skb));
6242 		if (err)
6243 			return err;
6244 
6245 		skb->protocol = skb->vlan_proto;
6246 		skb->mac_len += VLAN_HLEN;
6247 
6248 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
6249 	}
6250 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
6251 	return 0;
6252 }
6253 EXPORT_SYMBOL(skb_vlan_push);
6254 
6255 /**
6256  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
6257  *
6258  * @skb: Socket buffer to modify
6259  *
6260  * Drop the Ethernet header of @skb.
6261  *
6262  * Expects that skb->data points to the mac header and that no VLAN tags are
6263  * present.
6264  *
6265  * Returns 0 on success, -errno otherwise.
6266  */
6267 int skb_eth_pop(struct sk_buff *skb)
6268 {
6269 	if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
6270 	    skb_network_offset(skb) < ETH_HLEN)
6271 		return -EPROTO;
6272 
6273 	skb_pull_rcsum(skb, ETH_HLEN);
6274 	skb_reset_mac_header(skb);
6275 	skb_reset_mac_len(skb);
6276 
6277 	return 0;
6278 }
6279 EXPORT_SYMBOL(skb_eth_pop);
6280 
6281 /**
6282  * skb_eth_push() - Add a new Ethernet header at the head of a packet
6283  *
6284  * @skb: Socket buffer to modify
6285  * @dst: Destination MAC address of the new header
6286  * @src: Source MAC address of the new header
6287  *
6288  * Prepend @skb with a new Ethernet header.
6289  *
6290  * Expects that skb->data points to the mac header, which must be empty.
6291  *
6292  * Returns 0 on success, -errno otherwise.
6293  */
6294 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
6295 		 const unsigned char *src)
6296 {
6297 	struct ethhdr *eth;
6298 	int err;
6299 
6300 	if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
6301 		return -EPROTO;
6302 
6303 	err = skb_cow_head(skb, sizeof(*eth));
6304 	if (err < 0)
6305 		return err;
6306 
6307 	skb_push(skb, sizeof(*eth));
6308 	skb_reset_mac_header(skb);
6309 	skb_reset_mac_len(skb);
6310 
6311 	eth = eth_hdr(skb);
6312 	ether_addr_copy(eth->h_dest, dst);
6313 	ether_addr_copy(eth->h_source, src);
6314 	eth->h_proto = skb->protocol;
6315 
6316 	skb_postpush_rcsum(skb, eth, sizeof(*eth));
6317 
6318 	return 0;
6319 }
6320 EXPORT_SYMBOL(skb_eth_push);
6321 
6322 /* Update the ethertype of hdr and the skb csum value if required. */
6323 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
6324 			     __be16 ethertype)
6325 {
6326 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
6327 		__be16 diff[] = { ~hdr->h_proto, ethertype };
6328 
6329 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6330 	}
6331 
6332 	hdr->h_proto = ethertype;
6333 }
6334 
6335 /**
6336  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
6337  *                   the packet
6338  *
6339  * @skb: buffer
6340  * @mpls_lse: MPLS label stack entry to push
6341  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
6342  * @mac_len: length of the MAC header
6343  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
6344  *            ethernet
6345  *
6346  * Expects skb->data at mac header.
6347  *
6348  * Returns 0 on success, -errno otherwise.
6349  */
6350 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
6351 		  int mac_len, bool ethernet)
6352 {
6353 	struct mpls_shim_hdr *lse;
6354 	int err;
6355 
6356 	if (unlikely(!eth_p_mpls(mpls_proto)))
6357 		return -EINVAL;
6358 
6359 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
6360 	if (skb->encapsulation)
6361 		return -EINVAL;
6362 
6363 	err = skb_cow_head(skb, MPLS_HLEN);
6364 	if (unlikely(err))
6365 		return err;
6366 
6367 	if (!skb->inner_protocol) {
6368 		skb_set_inner_network_header(skb, skb_network_offset(skb));
6369 		skb_set_inner_protocol(skb, skb->protocol);
6370 	}
6371 
6372 	skb_push(skb, MPLS_HLEN);
6373 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
6374 		mac_len);
6375 	skb_reset_mac_header(skb);
6376 	skb_set_network_header(skb, mac_len);
6377 	skb_reset_mac_len(skb);
6378 
6379 	lse = mpls_hdr(skb);
6380 	lse->label_stack_entry = mpls_lse;
6381 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
6382 
6383 	if (ethernet && mac_len >= ETH_HLEN)
6384 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
6385 	skb->protocol = mpls_proto;
6386 
6387 	return 0;
6388 }
6389 EXPORT_SYMBOL_GPL(skb_mpls_push);
6390 
6391 /**
6392  * skb_mpls_pop() - pop the outermost MPLS header
6393  *
6394  * @skb: buffer
6395  * @next_proto: ethertype of header after popped MPLS header
6396  * @mac_len: length of the MAC header
6397  * @ethernet: flag to indicate if the packet is ethernet
6398  *
6399  * Expects skb->data at mac header.
6400  *
6401  * Returns 0 on success, -errno otherwise.
6402  */
6403 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
6404 		 bool ethernet)
6405 {
6406 	int err;
6407 
6408 	if (unlikely(!eth_p_mpls(skb->protocol)))
6409 		return 0;
6410 
6411 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
6412 	if (unlikely(err))
6413 		return err;
6414 
6415 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
6416 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
6417 		mac_len);
6418 
6419 	__skb_pull(skb, MPLS_HLEN);
6420 	skb_reset_mac_header(skb);
6421 	skb_set_network_header(skb, mac_len);
6422 
6423 	if (ethernet && mac_len >= ETH_HLEN) {
6424 		struct ethhdr *hdr;
6425 
6426 		/* use mpls_hdr() to get ethertype to account for VLANs. */
6427 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6428 		skb_mod_eth_type(skb, hdr, next_proto);
6429 	}
6430 	skb->protocol = next_proto;
6431 
6432 	return 0;
6433 }
6434 EXPORT_SYMBOL_GPL(skb_mpls_pop);
6435 
6436 /**
6437  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6438  *
6439  * @skb: buffer
6440  * @mpls_lse: new MPLS label stack entry to update to
6441  *
6442  * Expects skb->data at mac header.
6443  *
6444  * Returns 0 on success, -errno otherwise.
6445  */
6446 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6447 {
6448 	int err;
6449 
6450 	if (unlikely(!eth_p_mpls(skb->protocol)))
6451 		return -EINVAL;
6452 
6453 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6454 	if (unlikely(err))
6455 		return err;
6456 
6457 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
6458 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6459 
6460 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6461 	}
6462 
6463 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
6464 
6465 	return 0;
6466 }
6467 EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6468 
6469 /**
6470  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
6471  *
6472  * @skb: buffer
6473  *
6474  * Expects skb->data at mac header.
6475  *
6476  * Returns 0 on success, -errno otherwise.
6477  */
6478 int skb_mpls_dec_ttl(struct sk_buff *skb)
6479 {
6480 	u32 lse;
6481 	u8 ttl;
6482 
6483 	if (unlikely(!eth_p_mpls(skb->protocol)))
6484 		return -EINVAL;
6485 
6486 	if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
6487 		return -ENOMEM;
6488 
6489 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
6490 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
6491 	if (!--ttl)
6492 		return -EINVAL;
6493 
6494 	lse &= ~MPLS_LS_TTL_MASK;
6495 	lse |= ttl << MPLS_LS_TTL_SHIFT;
6496 
6497 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
6498 }
6499 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
6500 
6501 /**
6502  * alloc_skb_with_frags - allocate skb with page frags
6503  *
6504  * @header_len: size of linear part
6505  * @data_len: needed length in frags
6506  * @order: max page order desired.
6507  * @errcode: pointer to error code if any
6508  * @gfp_mask: allocation mask
6509  *
6510  * This can be used to allocate a paged skb, given a maximal order for frags.
6511  */
6512 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6513 				     unsigned long data_len,
6514 				     int order,
6515 				     int *errcode,
6516 				     gfp_t gfp_mask)
6517 {
6518 	unsigned long chunk;
6519 	struct sk_buff *skb;
6520 	struct page *page;
6521 	int nr_frags = 0;
6522 
6523 	*errcode = -EMSGSIZE;
6524 	if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order)))
6525 		return NULL;
6526 
6527 	*errcode = -ENOBUFS;
6528 	skb = alloc_skb(header_len, gfp_mask);
6529 	if (!skb)
6530 		return NULL;
6531 
6532 	while (data_len) {
6533 		if (nr_frags == MAX_SKB_FRAGS - 1)
6534 			goto failure;
6535 		while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
6536 			order--;
6537 
6538 		if (order) {
6539 			page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
6540 					   __GFP_COMP |
6541 					   __GFP_NOWARN,
6542 					   order);
6543 			if (!page) {
6544 				order--;
6545 				continue;
6546 			}
6547 		} else {
6548 			page = alloc_page(gfp_mask);
6549 			if (!page)
6550 				goto failure;
6551 		}
6552 		chunk = min_t(unsigned long, data_len,
6553 			      PAGE_SIZE << order);
6554 		skb_fill_page_desc(skb, nr_frags, page, 0, chunk);
6555 		nr_frags++;
6556 		skb->truesize += (PAGE_SIZE << order);
6557 		data_len -= chunk;
6558 	}
6559 	return skb;
6560 
6561 failure:
6562 	kfree_skb(skb);
6563 	return NULL;
6564 }
6565 EXPORT_SYMBOL(alloc_skb_with_frags);
6566 
6567 /* carve out the first off bytes from skb when off < headlen */
6568 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6569 				    const int headlen, gfp_t gfp_mask)
6570 {
6571 	int i;
6572 	unsigned int size = skb_end_offset(skb);
6573 	int new_hlen = headlen - off;
6574 	u8 *data;
6575 
6576 	if (skb_pfmemalloc(skb))
6577 		gfp_mask |= __GFP_MEMALLOC;
6578 
6579 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
6580 	if (!data)
6581 		return -ENOMEM;
6582 	size = SKB_WITH_OVERHEAD(size);
6583 
6584 	/* Copy real data, and all frags */
6585 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6586 	skb->len -= off;
6587 
6588 	memcpy((struct skb_shared_info *)(data + size),
6589 	       skb_shinfo(skb),
6590 	       offsetof(struct skb_shared_info,
6591 			frags[skb_shinfo(skb)->nr_frags]));
6592 	if (skb_cloned(skb)) {
6593 		/* drop the old head gracefully */
6594 		if (skb_orphan_frags(skb, gfp_mask)) {
6595 			skb_kfree_head(data, size);
6596 			return -ENOMEM;
6597 		}
6598 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6599 			skb_frag_ref(skb, i);
6600 		if (skb_has_frag_list(skb))
6601 			skb_clone_fraglist(skb);
6602 		skb_release_data(skb, SKB_CONSUMED);
6603 	} else {
6604 		/* we can reuse existing recount- all we did was
6605 		 * relocate values
6606 		 */
6607 		skb_free_head(skb);
6608 	}
6609 
6610 	skb->head = data;
6611 	skb->data = data;
6612 	skb->head_frag = 0;
6613 	skb_set_end_offset(skb, size);
6614 	skb_set_tail_pointer(skb, skb_headlen(skb));
6615 	skb_headers_offset_update(skb, 0);
6616 	skb->cloned = 0;
6617 	skb->hdr_len = 0;
6618 	skb->nohdr = 0;
6619 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6620 
6621 	return 0;
6622 }
6623 
6624 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6625 
6626 /* carve out the first eat bytes from skb's frag_list. May recurse into
6627  * pskb_carve()
6628  */
6629 static int pskb_carve_frag_list(struct sk_buff *skb,
6630 				struct skb_shared_info *shinfo, int eat,
6631 				gfp_t gfp_mask)
6632 {
6633 	struct sk_buff *list = shinfo->frag_list;
6634 	struct sk_buff *clone = NULL;
6635 	struct sk_buff *insp = NULL;
6636 
6637 	do {
6638 		if (!list) {
6639 			pr_err("Not enough bytes to eat. Want %d\n", eat);
6640 			return -EFAULT;
6641 		}
6642 		if (list->len <= eat) {
6643 			/* Eaten as whole. */
6644 			eat -= list->len;
6645 			list = list->next;
6646 			insp = list;
6647 		} else {
6648 			/* Eaten partially. */
6649 			if (skb_shared(list)) {
6650 				clone = skb_clone(list, gfp_mask);
6651 				if (!clone)
6652 					return -ENOMEM;
6653 				insp = list->next;
6654 				list = clone;
6655 			} else {
6656 				/* This may be pulled without problems. */
6657 				insp = list;
6658 			}
6659 			if (pskb_carve(list, eat, gfp_mask) < 0) {
6660 				kfree_skb(clone);
6661 				return -ENOMEM;
6662 			}
6663 			break;
6664 		}
6665 	} while (eat);
6666 
6667 	/* Free pulled out fragments. */
6668 	while ((list = shinfo->frag_list) != insp) {
6669 		shinfo->frag_list = list->next;
6670 		consume_skb(list);
6671 	}
6672 	/* And insert new clone at head. */
6673 	if (clone) {
6674 		clone->next = list;
6675 		shinfo->frag_list = clone;
6676 	}
6677 	return 0;
6678 }
6679 
6680 /* carve off first len bytes from skb. Split line (off) is in the
6681  * non-linear part of skb
6682  */
6683 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6684 				       int pos, gfp_t gfp_mask)
6685 {
6686 	int i, k = 0;
6687 	unsigned int size = skb_end_offset(skb);
6688 	u8 *data;
6689 	const int nfrags = skb_shinfo(skb)->nr_frags;
6690 	struct skb_shared_info *shinfo;
6691 
6692 	if (skb_pfmemalloc(skb))
6693 		gfp_mask |= __GFP_MEMALLOC;
6694 
6695 	data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
6696 	if (!data)
6697 		return -ENOMEM;
6698 	size = SKB_WITH_OVERHEAD(size);
6699 
6700 	memcpy((struct skb_shared_info *)(data + size),
6701 	       skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
6702 	if (skb_orphan_frags(skb, gfp_mask)) {
6703 		skb_kfree_head(data, size);
6704 		return -ENOMEM;
6705 	}
6706 	shinfo = (struct skb_shared_info *)(data + size);
6707 	for (i = 0; i < nfrags; i++) {
6708 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6709 
6710 		if (pos + fsize > off) {
6711 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6712 
6713 			if (pos < off) {
6714 				/* Split frag.
6715 				 * We have two variants in this case:
6716 				 * 1. Move all the frag to the second
6717 				 *    part, if it is possible. F.e.
6718 				 *    this approach is mandatory for TUX,
6719 				 *    where splitting is expensive.
6720 				 * 2. Split is accurately. We make this.
6721 				 */
6722 				skb_frag_off_add(&shinfo->frags[0], off - pos);
6723 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
6724 			}
6725 			skb_frag_ref(skb, i);
6726 			k++;
6727 		}
6728 		pos += fsize;
6729 	}
6730 	shinfo->nr_frags = k;
6731 	if (skb_has_frag_list(skb))
6732 		skb_clone_fraglist(skb);
6733 
6734 	/* split line is in frag list */
6735 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6736 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6737 		if (skb_has_frag_list(skb))
6738 			kfree_skb_list(skb_shinfo(skb)->frag_list);
6739 		skb_kfree_head(data, size);
6740 		return -ENOMEM;
6741 	}
6742 	skb_release_data(skb, SKB_CONSUMED);
6743 
6744 	skb->head = data;
6745 	skb->head_frag = 0;
6746 	skb->data = data;
6747 	skb_set_end_offset(skb, size);
6748 	skb_reset_tail_pointer(skb);
6749 	skb_headers_offset_update(skb, 0);
6750 	skb->cloned   = 0;
6751 	skb->hdr_len  = 0;
6752 	skb->nohdr    = 0;
6753 	skb->len -= off;
6754 	skb->data_len = skb->len;
6755 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6756 	return 0;
6757 }
6758 
6759 /* remove len bytes from the beginning of the skb */
6760 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6761 {
6762 	int headlen = skb_headlen(skb);
6763 
6764 	if (len < headlen)
6765 		return pskb_carve_inside_header(skb, len, headlen, gfp);
6766 	else
6767 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6768 }
6769 
6770 /* Extract to_copy bytes starting at off from skb, and return this in
6771  * a new skb
6772  */
6773 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6774 			     int to_copy, gfp_t gfp)
6775 {
6776 	struct sk_buff  *clone = skb_clone(skb, gfp);
6777 
6778 	if (!clone)
6779 		return NULL;
6780 
6781 	if (pskb_carve(clone, off, gfp) < 0 ||
6782 	    pskb_trim(clone, to_copy)) {
6783 		kfree_skb(clone);
6784 		return NULL;
6785 	}
6786 	return clone;
6787 }
6788 EXPORT_SYMBOL(pskb_extract);
6789 
6790 /**
6791  * skb_condense - try to get rid of fragments/frag_list if possible
6792  * @skb: buffer
6793  *
6794  * Can be used to save memory before skb is added to a busy queue.
6795  * If packet has bytes in frags and enough tail room in skb->head,
6796  * pull all of them, so that we can free the frags right now and adjust
6797  * truesize.
6798  * Notes:
6799  *	We do not reallocate skb->head thus can not fail.
6800  *	Caller must re-evaluate skb->truesize if needed.
6801  */
6802 void skb_condense(struct sk_buff *skb)
6803 {
6804 	if (skb->data_len) {
6805 		if (skb->data_len > skb->end - skb->tail ||
6806 		    skb_cloned(skb))
6807 			return;
6808 
6809 		/* Nice, we can free page frag(s) right now */
6810 		__pskb_pull_tail(skb, skb->data_len);
6811 	}
6812 	/* At this point, skb->truesize might be over estimated,
6813 	 * because skb had a fragment, and fragments do not tell
6814 	 * their truesize.
6815 	 * When we pulled its content into skb->head, fragment
6816 	 * was freed, but __pskb_pull_tail() could not possibly
6817 	 * adjust skb->truesize, not knowing the frag truesize.
6818 	 */
6819 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6820 }
6821 EXPORT_SYMBOL(skb_condense);
6822 
6823 #ifdef CONFIG_SKB_EXTENSIONS
6824 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6825 {
6826 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6827 }
6828 
6829 /**
6830  * __skb_ext_alloc - allocate a new skb extensions storage
6831  *
6832  * @flags: See kmalloc().
6833  *
6834  * Returns the newly allocated pointer. The pointer can later attached to a
6835  * skb via __skb_ext_set().
6836  * Note: caller must handle the skb_ext as an opaque data.
6837  */
6838 struct skb_ext *__skb_ext_alloc(gfp_t flags)
6839 {
6840 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6841 
6842 	if (new) {
6843 		memset(new->offset, 0, sizeof(new->offset));
6844 		refcount_set(&new->refcnt, 1);
6845 	}
6846 
6847 	return new;
6848 }
6849 
6850 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6851 					 unsigned int old_active)
6852 {
6853 	struct skb_ext *new;
6854 
6855 	if (refcount_read(&old->refcnt) == 1)
6856 		return old;
6857 
6858 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6859 	if (!new)
6860 		return NULL;
6861 
6862 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6863 	refcount_set(&new->refcnt, 1);
6864 
6865 #ifdef CONFIG_XFRM
6866 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6867 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6868 		unsigned int i;
6869 
6870 		for (i = 0; i < sp->len; i++)
6871 			xfrm_state_hold(sp->xvec[i]);
6872 	}
6873 #endif
6874 #ifdef CONFIG_MCTP_FLOWS
6875 	if (old_active & (1 << SKB_EXT_MCTP)) {
6876 		struct mctp_flow *flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
6877 
6878 		if (flow->key)
6879 			refcount_inc(&flow->key->refs);
6880 	}
6881 #endif
6882 	__skb_ext_put(old);
6883 	return new;
6884 }
6885 
6886 /**
6887  * __skb_ext_set - attach the specified extension storage to this skb
6888  * @skb: buffer
6889  * @id: extension id
6890  * @ext: extension storage previously allocated via __skb_ext_alloc()
6891  *
6892  * Existing extensions, if any, are cleared.
6893  *
6894  * Returns the pointer to the extension.
6895  */
6896 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6897 		    struct skb_ext *ext)
6898 {
6899 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6900 
6901 	skb_ext_put(skb);
6902 	newlen = newoff + skb_ext_type_len[id];
6903 	ext->chunks = newlen;
6904 	ext->offset[id] = newoff;
6905 	skb->extensions = ext;
6906 	skb->active_extensions = 1 << id;
6907 	return skb_ext_get_ptr(ext, id);
6908 }
6909 
6910 /**
6911  * skb_ext_add - allocate space for given extension, COW if needed
6912  * @skb: buffer
6913  * @id: extension to allocate space for
6914  *
6915  * Allocates enough space for the given extension.
6916  * If the extension is already present, a pointer to that extension
6917  * is returned.
6918  *
6919  * If the skb was cloned, COW applies and the returned memory can be
6920  * modified without changing the extension space of clones buffers.
6921  *
6922  * Returns pointer to the extension or NULL on allocation failure.
6923  */
6924 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6925 {
6926 	struct skb_ext *new, *old = NULL;
6927 	unsigned int newlen, newoff;
6928 
6929 	if (skb->active_extensions) {
6930 		old = skb->extensions;
6931 
6932 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6933 		if (!new)
6934 			return NULL;
6935 
6936 		if (__skb_ext_exist(new, id))
6937 			goto set_active;
6938 
6939 		newoff = new->chunks;
6940 	} else {
6941 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6942 
6943 		new = __skb_ext_alloc(GFP_ATOMIC);
6944 		if (!new)
6945 			return NULL;
6946 	}
6947 
6948 	newlen = newoff + skb_ext_type_len[id];
6949 	new->chunks = newlen;
6950 	new->offset[id] = newoff;
6951 set_active:
6952 	skb->slow_gro = 1;
6953 	skb->extensions = new;
6954 	skb->active_extensions |= 1 << id;
6955 	return skb_ext_get_ptr(new, id);
6956 }
6957 EXPORT_SYMBOL(skb_ext_add);
6958 
6959 #ifdef CONFIG_XFRM
6960 static void skb_ext_put_sp(struct sec_path *sp)
6961 {
6962 	unsigned int i;
6963 
6964 	for (i = 0; i < sp->len; i++)
6965 		xfrm_state_put(sp->xvec[i]);
6966 }
6967 #endif
6968 
6969 #ifdef CONFIG_MCTP_FLOWS
6970 static void skb_ext_put_mctp(struct mctp_flow *flow)
6971 {
6972 	if (flow->key)
6973 		mctp_key_unref(flow->key);
6974 }
6975 #endif
6976 
6977 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6978 {
6979 	struct skb_ext *ext = skb->extensions;
6980 
6981 	skb->active_extensions &= ~(1 << id);
6982 	if (skb->active_extensions == 0) {
6983 		skb->extensions = NULL;
6984 		__skb_ext_put(ext);
6985 #ifdef CONFIG_XFRM
6986 	} else if (id == SKB_EXT_SEC_PATH &&
6987 		   refcount_read(&ext->refcnt) == 1) {
6988 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6989 
6990 		skb_ext_put_sp(sp);
6991 		sp->len = 0;
6992 #endif
6993 	}
6994 }
6995 EXPORT_SYMBOL(__skb_ext_del);
6996 
6997 void __skb_ext_put(struct skb_ext *ext)
6998 {
6999 	/* If this is last clone, nothing can increment
7000 	 * it after check passes.  Avoids one atomic op.
7001 	 */
7002 	if (refcount_read(&ext->refcnt) == 1)
7003 		goto free_now;
7004 
7005 	if (!refcount_dec_and_test(&ext->refcnt))
7006 		return;
7007 free_now:
7008 #ifdef CONFIG_XFRM
7009 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
7010 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
7011 #endif
7012 #ifdef CONFIG_MCTP_FLOWS
7013 	if (__skb_ext_exist(ext, SKB_EXT_MCTP))
7014 		skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
7015 #endif
7016 
7017 	kmem_cache_free(skbuff_ext_cache, ext);
7018 }
7019 EXPORT_SYMBOL(__skb_ext_put);
7020 #endif /* CONFIG_SKB_EXTENSIONS */
7021 
7022 static void kfree_skb_napi_cache(struct sk_buff *skb)
7023 {
7024 	/* if SKB is a clone, don't handle this case */
7025 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
7026 		__kfree_skb(skb);
7027 		return;
7028 	}
7029 
7030 	local_bh_disable();
7031 	__napi_kfree_skb(skb, SKB_CONSUMED);
7032 	local_bh_enable();
7033 }
7034 
7035 /**
7036  * skb_attempt_defer_free - queue skb for remote freeing
7037  * @skb: buffer
7038  *
7039  * Put @skb in a per-cpu list, using the cpu which
7040  * allocated the skb/pages to reduce false sharing
7041  * and memory zone spinlock contention.
7042  */
7043 void skb_attempt_defer_free(struct sk_buff *skb)
7044 {
7045 	int cpu = skb->alloc_cpu;
7046 	struct softnet_data *sd;
7047 	unsigned int defer_max;
7048 	bool kick;
7049 
7050 	if (cpu == raw_smp_processor_id() ||
7051 	    WARN_ON_ONCE(cpu >= nr_cpu_ids) ||
7052 	    !cpu_online(cpu)) {
7053 nodefer:	kfree_skb_napi_cache(skb);
7054 		return;
7055 	}
7056 
7057 	DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
7058 	DEBUG_NET_WARN_ON_ONCE(skb->destructor);
7059 
7060 	sd = &per_cpu(softnet_data, cpu);
7061 	defer_max = READ_ONCE(net_hotdata.sysctl_skb_defer_max);
7062 	if (READ_ONCE(sd->defer_count) >= defer_max)
7063 		goto nodefer;
7064 
7065 	spin_lock_bh(&sd->defer_lock);
7066 	/* Send an IPI every time queue reaches half capacity. */
7067 	kick = sd->defer_count == (defer_max >> 1);
7068 	/* Paired with the READ_ONCE() few lines above */
7069 	WRITE_ONCE(sd->defer_count, sd->defer_count + 1);
7070 
7071 	skb->next = sd->defer_list;
7072 	/* Paired with READ_ONCE() in skb_defer_free_flush() */
7073 	WRITE_ONCE(sd->defer_list, skb);
7074 	spin_unlock_bh(&sd->defer_lock);
7075 
7076 	/* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU
7077 	 * if we are unlucky enough (this seems very unlikely).
7078 	 */
7079 	if (unlikely(kick))
7080 		kick_defer_list_purge(sd, cpu);
7081 }
7082 
7083 static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
7084 				 size_t offset, size_t len)
7085 {
7086 	const char *kaddr;
7087 	__wsum csum;
7088 
7089 	kaddr = kmap_local_page(page);
7090 	csum = csum_partial(kaddr + offset, len, 0);
7091 	kunmap_local(kaddr);
7092 	skb->csum = csum_block_add(skb->csum, csum, skb->len);
7093 }
7094 
7095 /**
7096  * skb_splice_from_iter - Splice (or copy) pages to skbuff
7097  * @skb: The buffer to add pages to
7098  * @iter: Iterator representing the pages to be added
7099  * @maxsize: Maximum amount of pages to be added
7100  * @gfp: Allocation flags
7101  *
7102  * This is a common helper function for supporting MSG_SPLICE_PAGES.  It
7103  * extracts pages from an iterator and adds them to the socket buffer if
7104  * possible, copying them to fragments if not possible (such as if they're slab
7105  * pages).
7106  *
7107  * Returns the amount of data spliced/copied or -EMSGSIZE if there's
7108  * insufficient space in the buffer to transfer anything.
7109  */
7110 ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
7111 			     ssize_t maxsize, gfp_t gfp)
7112 {
7113 	size_t frag_limit = READ_ONCE(net_hotdata.sysctl_max_skb_frags);
7114 	struct page *pages[8], **ppages = pages;
7115 	ssize_t spliced = 0, ret = 0;
7116 	unsigned int i;
7117 
7118 	while (iter->count > 0) {
7119 		ssize_t space, nr, len;
7120 		size_t off;
7121 
7122 		ret = -EMSGSIZE;
7123 		space = frag_limit - skb_shinfo(skb)->nr_frags;
7124 		if (space < 0)
7125 			break;
7126 
7127 		/* We might be able to coalesce without increasing nr_frags */
7128 		nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages));
7129 
7130 		len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off);
7131 		if (len <= 0) {
7132 			ret = len ?: -EIO;
7133 			break;
7134 		}
7135 
7136 		i = 0;
7137 		do {
7138 			struct page *page = pages[i++];
7139 			size_t part = min_t(size_t, PAGE_SIZE - off, len);
7140 
7141 			ret = -EIO;
7142 			if (WARN_ON_ONCE(!sendpage_ok(page)))
7143 				goto out;
7144 
7145 			ret = skb_append_pagefrags(skb, page, off, part,
7146 						   frag_limit);
7147 			if (ret < 0) {
7148 				iov_iter_revert(iter, len);
7149 				goto out;
7150 			}
7151 
7152 			if (skb->ip_summed == CHECKSUM_NONE)
7153 				skb_splice_csum_page(skb, page, off, part);
7154 
7155 			off = 0;
7156 			spliced += part;
7157 			maxsize -= part;
7158 			len -= part;
7159 		} while (len > 0);
7160 
7161 		if (maxsize <= 0)
7162 			break;
7163 	}
7164 
7165 out:
7166 	skb_len_add(skb, spliced);
7167 	return spliced ?: ret;
7168 }
7169 EXPORT_SYMBOL(skb_splice_from_iter);
7170 
7171 static __always_inline
7172 size_t memcpy_from_iter_csum(void *iter_from, size_t progress,
7173 			     size_t len, void *to, void *priv2)
7174 {
7175 	__wsum *csum = priv2;
7176 	__wsum next = csum_partial_copy_nocheck(iter_from, to + progress, len);
7177 
7178 	*csum = csum_block_add(*csum, next, progress);
7179 	return 0;
7180 }
7181 
7182 static __always_inline
7183 size_t copy_from_user_iter_csum(void __user *iter_from, size_t progress,
7184 				size_t len, void *to, void *priv2)
7185 {
7186 	__wsum next, *csum = priv2;
7187 
7188 	next = csum_and_copy_from_user(iter_from, to + progress, len);
7189 	*csum = csum_block_add(*csum, next, progress);
7190 	return next ? 0 : len;
7191 }
7192 
7193 bool csum_and_copy_from_iter_full(void *addr, size_t bytes,
7194 				  __wsum *csum, struct iov_iter *i)
7195 {
7196 	size_t copied;
7197 
7198 	if (WARN_ON_ONCE(!i->data_source))
7199 		return false;
7200 	copied = iterate_and_advance2(i, bytes, addr, csum,
7201 				      copy_from_user_iter_csum,
7202 				      memcpy_from_iter_csum);
7203 	if (likely(copied == bytes))
7204 		return true;
7205 	iov_iter_revert(i, copied);
7206 	return false;
7207 }
7208 EXPORT_SYMBOL(csum_and_copy_from_iter_full);
7209