1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines having to do with the 'struct sk_buff' memory handlers.
5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
9 * Alan Cox : Fixed the worst of the load
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
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).
32 * The functions in this file will not compile correctly with gcc 2.4.x
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
41 #include <linux/interrupt.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>
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/splice.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
59 #include <linux/errqueue.h>
60 #include <linux/prefetch.h>
61 #include <linux/if_vlan.h>
62 #include <linux/mpls.h>
63 #include <linux/kcov.h>
65 #include <net/protocol.h>
68 #include <net/checksum.h>
69 #include <net/ip6_checksum.h>
72 #include <net/mptcp.h>
73 #include <net/page_pool.h>
75 #include <linux/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
80 #include <linux/indirect_call_wrapper.h>
83 #include "sock_destructor.h"
85 struct kmem_cache *skbuff_head_cache __ro_after_init;
86 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
87 #ifdef CONFIG_SKB_EXTENSIONS
88 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
90 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
91 EXPORT_SYMBOL(sysctl_max_skb_frags);
94 * skb_panic - private function for out-of-line support
98 * @msg: skb_over_panic or skb_under_panic
100 * Out-of-line support for skb_put() and skb_push().
101 * Called via the wrapper skb_over_panic() or skb_under_panic().
102 * Keep out of line to prevent kernel bloat.
103 * __builtin_return_address is not used because it is not always reliable.
105 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
108 pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
109 msg, addr, skb->len, sz, skb->head, skb->data,
110 (unsigned long)skb->tail, (unsigned long)skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
115 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
117 skb_panic(skb, sz, addr, __func__);
120 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
122 skb_panic(skb, sz, addr, __func__);
125 #define NAPI_SKB_CACHE_SIZE 64
126 #define NAPI_SKB_CACHE_BULK 16
127 #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2)
129 struct napi_alloc_cache {
130 struct page_frag_cache page;
131 unsigned int skb_count;
132 void *skb_cache[NAPI_SKB_CACHE_SIZE];
135 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
136 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
138 static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask,
139 unsigned int align_mask)
141 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
143 return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask);
146 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
148 fragsz = SKB_DATA_ALIGN(fragsz);
150 return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
152 EXPORT_SYMBOL(__napi_alloc_frag_align);
154 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
156 struct page_frag_cache *nc;
159 fragsz = SKB_DATA_ALIGN(fragsz);
160 if (in_hardirq() || irqs_disabled()) {
161 nc = this_cpu_ptr(&netdev_alloc_cache);
162 data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
165 data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
170 EXPORT_SYMBOL(__netdev_alloc_frag_align);
172 static struct sk_buff *napi_skb_cache_get(void)
174 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
177 if (unlikely(!nc->skb_count))
178 nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache,
182 if (unlikely(!nc->skb_count))
185 skb = nc->skb_cache[--nc->skb_count];
186 kasan_unpoison_object_data(skbuff_head_cache, skb);
191 /* Caller must provide SKB that is memset cleared */
192 static void __build_skb_around(struct sk_buff *skb, void *data,
193 unsigned int frag_size)
195 struct skb_shared_info *shinfo;
196 unsigned int size = frag_size ? : ksize(data);
198 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
200 /* Assumes caller memset cleared SKB */
201 skb->truesize = SKB_TRUESIZE(size);
202 refcount_set(&skb->users, 1);
205 skb_reset_tail_pointer(skb);
206 skb_set_end_offset(skb, size);
207 skb->mac_header = (typeof(skb->mac_header))~0U;
208 skb->transport_header = (typeof(skb->transport_header))~0U;
210 /* make sure we initialize shinfo sequentially */
211 shinfo = skb_shinfo(skb);
212 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
213 atomic_set(&shinfo->dataref, 1);
215 skb_set_kcov_handle(skb, kcov_common_handle());
219 * __build_skb - build a network buffer
220 * @data: data buffer provided by caller
221 * @frag_size: size of data, or 0 if head was kmalloced
223 * Allocate a new &sk_buff. Caller provides space holding head and
224 * skb_shared_info. @data must have been allocated by kmalloc() only if
225 * @frag_size is 0, otherwise data should come from the page allocator
227 * The return is the new skb buffer.
228 * On a failure the return is %NULL, and @data is not freed.
230 * Before IO, driver allocates only data buffer where NIC put incoming frame
231 * Driver should add room at head (NET_SKB_PAD) and
232 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
233 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
234 * before giving packet to stack.
235 * RX rings only contains data buffers, not full skbs.
237 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
241 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
245 memset(skb, 0, offsetof(struct sk_buff, tail));
246 __build_skb_around(skb, data, frag_size);
251 /* build_skb() is wrapper over __build_skb(), that specifically
252 * takes care of skb->head and skb->pfmemalloc
253 * This means that if @frag_size is not zero, then @data must be backed
254 * by a page fragment, not kmalloc() or vmalloc()
256 struct sk_buff *build_skb(void *data, unsigned int frag_size)
258 struct sk_buff *skb = __build_skb(data, frag_size);
260 if (skb && frag_size) {
262 if (page_is_pfmemalloc(virt_to_head_page(data)))
267 EXPORT_SYMBOL(build_skb);
270 * build_skb_around - build a network buffer around provided skb
271 * @skb: sk_buff provide by caller, must be memset cleared
272 * @data: data buffer provided by caller
273 * @frag_size: size of data, or 0 if head was kmalloced
275 struct sk_buff *build_skb_around(struct sk_buff *skb,
276 void *data, unsigned int frag_size)
281 __build_skb_around(skb, data, frag_size);
285 if (page_is_pfmemalloc(virt_to_head_page(data)))
290 EXPORT_SYMBOL(build_skb_around);
293 * __napi_build_skb - build a network buffer
294 * @data: data buffer provided by caller
295 * @frag_size: size of data, or 0 if head was kmalloced
297 * Version of __build_skb() that uses NAPI percpu caches to obtain
298 * skbuff_head instead of inplace allocation.
300 * Returns a new &sk_buff on success, %NULL on allocation failure.
302 static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
306 skb = napi_skb_cache_get();
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 __build_skb_around(skb, data, frag_size);
317 * napi_build_skb - build a network buffer
318 * @data: data buffer provided by caller
319 * @frag_size: size of data, or 0 if head was kmalloced
321 * Version of __napi_build_skb() that takes care of skb->head_frag
322 * and skb->pfmemalloc when the data is a page or page fragment.
324 * Returns a new &sk_buff on success, %NULL on allocation failure.
326 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
328 struct sk_buff *skb = __napi_build_skb(data, frag_size);
330 if (likely(skb) && frag_size) {
332 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
337 EXPORT_SYMBOL(napi_build_skb);
340 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
341 * the caller if emergency pfmemalloc reserves are being used. If it is and
342 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
343 * may be used. Otherwise, the packet data may be discarded until enough
346 static void *kmalloc_reserve(size_t size, gfp_t flags, int node,
350 bool ret_pfmemalloc = false;
353 * Try a regular allocation, when that fails and we're not entitled
354 * to the reserves, fail.
356 obj = kmalloc_node_track_caller(size,
357 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
359 if (obj || !(gfp_pfmemalloc_allowed(flags)))
362 /* Try again but now we are using pfmemalloc reserves */
363 ret_pfmemalloc = true;
364 obj = kmalloc_node_track_caller(size, flags, node);
368 *pfmemalloc = ret_pfmemalloc;
373 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
374 * 'private' fields and also do memory statistics to find all the
380 * __alloc_skb - allocate a network buffer
381 * @size: size to allocate
382 * @gfp_mask: allocation mask
383 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
384 * instead of head cache and allocate a cloned (child) skb.
385 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
386 * allocations in case the data is required for writeback
387 * @node: numa node to allocate memory on
389 * Allocate a new &sk_buff. The returned buffer has no headroom and a
390 * tail room of at least size bytes. The object has a reference count
391 * of one. The return is the buffer. On a failure the return is %NULL.
393 * Buffers may only be allocated from interrupts using a @gfp_mask of
396 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
399 struct kmem_cache *cache;
404 cache = (flags & SKB_ALLOC_FCLONE)
405 ? skbuff_fclone_cache : skbuff_head_cache;
407 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
408 gfp_mask |= __GFP_MEMALLOC;
411 if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
412 likely(node == NUMA_NO_NODE || node == numa_mem_id()))
413 skb = napi_skb_cache_get();
415 skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
420 /* We do our best to align skb_shared_info on a separate cache
421 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
422 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
423 * Both skb->head and skb_shared_info are cache line aligned.
425 size = SKB_DATA_ALIGN(size);
426 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
427 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
430 /* kmalloc(size) might give us more room than requested.
431 * Put skb_shared_info exactly at the end of allocated zone,
432 * to allow max possible filling before reallocation.
434 size = SKB_WITH_OVERHEAD(ksize(data));
435 prefetchw(data + size);
438 * Only clear those fields we need to clear, not those that we will
439 * actually initialise below. Hence, don't put any more fields after
440 * the tail pointer in struct sk_buff!
442 memset(skb, 0, offsetof(struct sk_buff, tail));
443 __build_skb_around(skb, data, 0);
444 skb->pfmemalloc = pfmemalloc;
446 if (flags & SKB_ALLOC_FCLONE) {
447 struct sk_buff_fclones *fclones;
449 fclones = container_of(skb, struct sk_buff_fclones, skb1);
451 skb->fclone = SKB_FCLONE_ORIG;
452 refcount_set(&fclones->fclone_ref, 1);
454 fclones->skb2.fclone = SKB_FCLONE_CLONE;
460 kmem_cache_free(cache, skb);
463 EXPORT_SYMBOL(__alloc_skb);
466 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
467 * @dev: network device to receive on
468 * @len: length to allocate
469 * @gfp_mask: get_free_pages mask, passed to alloc_skb
471 * Allocate a new &sk_buff and assign it a usage count of one. The
472 * buffer has NET_SKB_PAD headroom built in. Users should allocate
473 * the headroom they think they need without accounting for the
474 * built in space. The built in space is used for optimisations.
476 * %NULL is returned if there is no free memory.
478 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
481 struct page_frag_cache *nc;
488 /* If requested length is either too small or too big,
489 * we use kmalloc() for skb->head allocation.
491 if (len <= SKB_WITH_OVERHEAD(1024) ||
492 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
493 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
494 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
500 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
501 len = SKB_DATA_ALIGN(len);
503 if (sk_memalloc_socks())
504 gfp_mask |= __GFP_MEMALLOC;
506 if (in_hardirq() || irqs_disabled()) {
507 nc = this_cpu_ptr(&netdev_alloc_cache);
508 data = page_frag_alloc(nc, len, gfp_mask);
509 pfmemalloc = nc->pfmemalloc;
512 nc = this_cpu_ptr(&napi_alloc_cache.page);
513 data = page_frag_alloc(nc, len, gfp_mask);
514 pfmemalloc = nc->pfmemalloc;
521 skb = __build_skb(data, len);
522 if (unlikely(!skb)) {
532 skb_reserve(skb, NET_SKB_PAD);
538 EXPORT_SYMBOL(__netdev_alloc_skb);
541 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
542 * @napi: napi instance this buffer was allocated for
543 * @len: length to allocate
544 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
546 * Allocate a new sk_buff for use in NAPI receive. This buffer will
547 * attempt to allocate the head from a special reserved region used
548 * only for NAPI Rx allocation. By doing this we can save several
549 * CPU cycles by avoiding having to disable and re-enable IRQs.
551 * %NULL is returned if there is no free memory.
553 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
556 struct napi_alloc_cache *nc;
560 len += NET_SKB_PAD + NET_IP_ALIGN;
562 /* If requested length is either too small or too big,
563 * we use kmalloc() for skb->head allocation.
565 if (len <= SKB_WITH_OVERHEAD(1024) ||
566 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
567 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
568 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
575 nc = this_cpu_ptr(&napi_alloc_cache);
576 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
577 len = SKB_DATA_ALIGN(len);
579 if (sk_memalloc_socks())
580 gfp_mask |= __GFP_MEMALLOC;
582 data = page_frag_alloc(&nc->page, len, gfp_mask);
586 skb = __napi_build_skb(data, len);
587 if (unlikely(!skb)) {
592 if (nc->page.pfmemalloc)
597 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
598 skb->dev = napi->dev;
603 EXPORT_SYMBOL(__napi_alloc_skb);
605 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
606 int size, unsigned int truesize)
608 skb_fill_page_desc(skb, i, page, off, size);
610 skb->data_len += size;
611 skb->truesize += truesize;
613 EXPORT_SYMBOL(skb_add_rx_frag);
615 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
616 unsigned int truesize)
618 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
620 skb_frag_size_add(frag, size);
622 skb->data_len += size;
623 skb->truesize += truesize;
625 EXPORT_SYMBOL(skb_coalesce_rx_frag);
627 static void skb_drop_list(struct sk_buff **listp)
629 kfree_skb_list(*listp);
633 static inline void skb_drop_fraglist(struct sk_buff *skb)
635 skb_drop_list(&skb_shinfo(skb)->frag_list);
638 static void skb_clone_fraglist(struct sk_buff *skb)
640 struct sk_buff *list;
642 skb_walk_frags(skb, list)
646 static void skb_free_head(struct sk_buff *skb)
648 unsigned char *head = skb->head;
650 if (skb->head_frag) {
651 if (skb_pp_recycle(skb, head))
659 static void skb_release_data(struct sk_buff *skb)
661 struct skb_shared_info *shinfo = skb_shinfo(skb);
665 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
669 skb_zcopy_clear(skb, true);
671 for (i = 0; i < shinfo->nr_frags; i++)
672 __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
674 if (shinfo->frag_list)
675 kfree_skb_list(shinfo->frag_list);
679 /* When we clone an SKB we copy the reycling bit. The pp_recycle
680 * bit is only set on the head though, so in order to avoid races
681 * while trying to recycle fragments on __skb_frag_unref() we need
682 * to make one SKB responsible for triggering the recycle path.
683 * So disable the recycling bit if an SKB is cloned and we have
684 * additional references to to the fragmented part of the SKB.
685 * Eventually the last SKB will have the recycling bit set and it's
686 * dataref set to 0, which will trigger the recycling
692 * Free an skbuff by memory without cleaning the state.
694 static void kfree_skbmem(struct sk_buff *skb)
696 struct sk_buff_fclones *fclones;
698 switch (skb->fclone) {
699 case SKB_FCLONE_UNAVAILABLE:
700 kmem_cache_free(skbuff_head_cache, skb);
703 case SKB_FCLONE_ORIG:
704 fclones = container_of(skb, struct sk_buff_fclones, skb1);
706 /* We usually free the clone (TX completion) before original skb
707 * This test would have no chance to be true for the clone,
708 * while here, branch prediction will be good.
710 if (refcount_read(&fclones->fclone_ref) == 1)
714 default: /* SKB_FCLONE_CLONE */
715 fclones = container_of(skb, struct sk_buff_fclones, skb2);
718 if (!refcount_dec_and_test(&fclones->fclone_ref))
721 kmem_cache_free(skbuff_fclone_cache, fclones);
724 void skb_release_head_state(struct sk_buff *skb)
727 if (skb->destructor) {
728 WARN_ON(in_hardirq());
729 skb->destructor(skb);
731 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
732 nf_conntrack_put(skb_nfct(skb));
737 /* Free everything but the sk_buff shell. */
738 static void skb_release_all(struct sk_buff *skb)
740 skb_release_head_state(skb);
741 if (likely(skb->head))
742 skb_release_data(skb);
746 * __kfree_skb - private function
749 * Free an sk_buff. Release anything attached to the buffer.
750 * Clean the state. This is an internal helper function. Users should
751 * always call kfree_skb
754 void __kfree_skb(struct sk_buff *skb)
756 skb_release_all(skb);
759 EXPORT_SYMBOL(__kfree_skb);
762 * kfree_skb_reason - free an sk_buff with special reason
763 * @skb: buffer to free
764 * @reason: reason why this skb is dropped
766 * Drop a reference to the buffer and free it if the usage count has
767 * hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
770 void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
775 trace_kfree_skb(skb, __builtin_return_address(0), reason);
778 EXPORT_SYMBOL(kfree_skb_reason);
780 void kfree_skb_list(struct sk_buff *segs)
783 struct sk_buff *next = segs->next;
789 EXPORT_SYMBOL(kfree_skb_list);
791 /* Dump skb information and contents.
793 * Must only be called from net_ratelimit()-ed paths.
795 * Dumps whole packets if full_pkt, only headers otherwise.
797 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
799 struct skb_shared_info *sh = skb_shinfo(skb);
800 struct net_device *dev = skb->dev;
801 struct sock *sk = skb->sk;
802 struct sk_buff *list_skb;
803 bool has_mac, has_trans;
804 int headroom, tailroom;
810 len = min_t(int, skb->len, MAX_HEADER + 128);
812 headroom = skb_headroom(skb);
813 tailroom = skb_tailroom(skb);
815 has_mac = skb_mac_header_was_set(skb);
816 has_trans = skb_transport_header_was_set(skb);
818 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
819 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
820 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
821 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
822 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
823 level, skb->len, headroom, skb_headlen(skb), tailroom,
824 has_mac ? skb->mac_header : -1,
825 has_mac ? skb_mac_header_len(skb) : -1,
827 has_trans ? skb_network_header_len(skb) : -1,
828 has_trans ? skb->transport_header : -1,
829 sh->tx_flags, sh->nr_frags,
830 sh->gso_size, sh->gso_type, sh->gso_segs,
831 skb->csum, skb->ip_summed, skb->csum_complete_sw,
832 skb->csum_valid, skb->csum_level,
833 skb->hash, skb->sw_hash, skb->l4_hash,
834 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
837 printk("%sdev name=%s feat=%pNF\n",
838 level, dev->name, &dev->features);
840 printk("%ssk family=%hu type=%u proto=%u\n",
841 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
843 if (full_pkt && headroom)
844 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
845 16, 1, skb->head, headroom, false);
847 seg_len = min_t(int, skb_headlen(skb), len);
849 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
850 16, 1, skb->data, seg_len, false);
853 if (full_pkt && tailroom)
854 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
855 16, 1, skb_tail_pointer(skb), tailroom, false);
857 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
858 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
859 u32 p_off, p_len, copied;
863 skb_frag_foreach_page(frag, skb_frag_off(frag),
864 skb_frag_size(frag), p, p_off, p_len,
866 seg_len = min_t(int, p_len, len);
867 vaddr = kmap_atomic(p);
868 print_hex_dump(level, "skb frag: ",
870 16, 1, vaddr + p_off, seg_len, false);
871 kunmap_atomic(vaddr);
878 if (full_pkt && skb_has_frag_list(skb)) {
879 printk("skb fraglist:\n");
880 skb_walk_frags(skb, list_skb)
881 skb_dump(level, list_skb, true);
884 EXPORT_SYMBOL(skb_dump);
887 * skb_tx_error - report an sk_buff xmit error
888 * @skb: buffer that triggered an error
890 * Report xmit error if a device callback is tracking this skb.
891 * skb must be freed afterwards.
893 void skb_tx_error(struct sk_buff *skb)
895 skb_zcopy_clear(skb, true);
897 EXPORT_SYMBOL(skb_tx_error);
899 #ifdef CONFIG_TRACEPOINTS
901 * consume_skb - free an skbuff
902 * @skb: buffer to free
904 * Drop a ref to the buffer and free it if the usage count has hit zero
905 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
906 * is being dropped after a failure and notes that
908 void consume_skb(struct sk_buff *skb)
913 trace_consume_skb(skb);
916 EXPORT_SYMBOL(consume_skb);
920 * __consume_stateless_skb - free an skbuff, assuming it is stateless
921 * @skb: buffer to free
923 * Alike consume_skb(), but this variant assumes that this is the last
924 * skb reference and all the head states have been already dropped
926 void __consume_stateless_skb(struct sk_buff *skb)
928 trace_consume_skb(skb);
929 skb_release_data(skb);
933 static void napi_skb_cache_put(struct sk_buff *skb)
935 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
938 kasan_poison_object_data(skbuff_head_cache, skb);
939 nc->skb_cache[nc->skb_count++] = skb;
941 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
942 for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
943 kasan_unpoison_object_data(skbuff_head_cache,
946 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF,
947 nc->skb_cache + NAPI_SKB_CACHE_HALF);
948 nc->skb_count = NAPI_SKB_CACHE_HALF;
952 void __kfree_skb_defer(struct sk_buff *skb)
954 skb_release_all(skb);
955 napi_skb_cache_put(skb);
958 void napi_skb_free_stolen_head(struct sk_buff *skb)
960 if (unlikely(skb->slow_gro)) {
967 napi_skb_cache_put(skb);
970 void napi_consume_skb(struct sk_buff *skb, int budget)
972 /* Zero budget indicate non-NAPI context called us, like netpoll */
973 if (unlikely(!budget)) {
974 dev_consume_skb_any(skb);
978 lockdep_assert_in_softirq();
983 /* if reaching here SKB is ready to free */
984 trace_consume_skb(skb);
986 /* if SKB is a clone, don't handle this case */
987 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
992 skb_release_all(skb);
993 napi_skb_cache_put(skb);
995 EXPORT_SYMBOL(napi_consume_skb);
997 /* Make sure a field is enclosed inside headers_start/headers_end section */
998 #define CHECK_SKB_FIELD(field) \
999 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
1000 offsetof(struct sk_buff, headers_start)); \
1001 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
1002 offsetof(struct sk_buff, headers_end)); \
1004 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1006 new->tstamp = old->tstamp;
1007 /* We do not copy old->sk */
1008 new->dev = old->dev;
1009 memcpy(new->cb, old->cb, sizeof(old->cb));
1010 skb_dst_copy(new, old);
1011 __skb_ext_copy(new, old);
1012 __nf_copy(new, old, false);
1014 /* Note : this field could be in headers_start/headers_end section
1015 * It is not yet because we do not want to have a 16 bit hole
1017 new->queue_mapping = old->queue_mapping;
1019 memcpy(&new->headers_start, &old->headers_start,
1020 offsetof(struct sk_buff, headers_end) -
1021 offsetof(struct sk_buff, headers_start));
1022 CHECK_SKB_FIELD(protocol);
1023 CHECK_SKB_FIELD(csum);
1024 CHECK_SKB_FIELD(hash);
1025 CHECK_SKB_FIELD(priority);
1026 CHECK_SKB_FIELD(skb_iif);
1027 CHECK_SKB_FIELD(vlan_proto);
1028 CHECK_SKB_FIELD(vlan_tci);
1029 CHECK_SKB_FIELD(transport_header);
1030 CHECK_SKB_FIELD(network_header);
1031 CHECK_SKB_FIELD(mac_header);
1032 CHECK_SKB_FIELD(inner_protocol);
1033 CHECK_SKB_FIELD(inner_transport_header);
1034 CHECK_SKB_FIELD(inner_network_header);
1035 CHECK_SKB_FIELD(inner_mac_header);
1036 CHECK_SKB_FIELD(mark);
1037 #ifdef CONFIG_NETWORK_SECMARK
1038 CHECK_SKB_FIELD(secmark);
1040 #ifdef CONFIG_NET_RX_BUSY_POLL
1041 CHECK_SKB_FIELD(napi_id);
1044 CHECK_SKB_FIELD(sender_cpu);
1046 #ifdef CONFIG_NET_SCHED
1047 CHECK_SKB_FIELD(tc_index);
1053 * You should not add any new code to this function. Add it to
1054 * __copy_skb_header above instead.
1056 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
1058 #define C(x) n->x = skb->x
1060 n->next = n->prev = NULL;
1062 __copy_skb_header(n, skb);
1067 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
1073 n->destructor = NULL;
1080 refcount_set(&n->users, 1);
1082 atomic_inc(&(skb_shinfo(skb)->dataref));
1090 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1091 * @first: first sk_buff of the msg
1093 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1097 n = alloc_skb(0, GFP_ATOMIC);
1101 n->len = first->len;
1102 n->data_len = first->len;
1103 n->truesize = first->truesize;
1105 skb_shinfo(n)->frag_list = first;
1107 __copy_skb_header(n, first);
1108 n->destructor = NULL;
1112 EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1115 * skb_morph - morph one skb into another
1116 * @dst: the skb to receive the contents
1117 * @src: the skb to supply the contents
1119 * This is identical to skb_clone except that the target skb is
1120 * supplied by the user.
1122 * The target skb is returned upon exit.
1124 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1126 skb_release_all(dst);
1127 return __skb_clone(dst, src);
1129 EXPORT_SYMBOL_GPL(skb_morph);
1131 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1133 unsigned long max_pg, num_pg, new_pg, old_pg;
1134 struct user_struct *user;
1136 if (capable(CAP_IPC_LOCK) || !size)
1139 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1140 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1141 user = mmp->user ? : current_user();
1144 old_pg = atomic_long_read(&user->locked_vm);
1145 new_pg = old_pg + num_pg;
1146 if (new_pg > max_pg)
1148 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1152 mmp->user = get_uid(user);
1153 mmp->num_pg = num_pg;
1155 mmp->num_pg += num_pg;
1160 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1162 void mm_unaccount_pinned_pages(struct mmpin *mmp)
1165 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1166 free_uid(mmp->user);
1169 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1171 struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
1173 struct ubuf_info *uarg;
1174 struct sk_buff *skb;
1176 WARN_ON_ONCE(!in_task());
1178 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1182 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1183 uarg = (void *)skb->cb;
1184 uarg->mmp.user = NULL;
1186 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1191 uarg->callback = msg_zerocopy_callback;
1192 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1194 uarg->bytelen = size;
1196 uarg->flags = SKBFL_ZEROCOPY_FRAG;
1197 refcount_set(&uarg->refcnt, 1);
1202 EXPORT_SYMBOL_GPL(msg_zerocopy_alloc);
1204 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1206 return container_of((void *)uarg, struct sk_buff, cb);
1209 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1210 struct ubuf_info *uarg)
1213 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1216 /* realloc only when socket is locked (TCP, UDP cork),
1217 * so uarg->len and sk_zckey access is serialized
1219 if (!sock_owned_by_user(sk)) {
1224 bytelen = uarg->bytelen + size;
1225 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1226 /* TCP can create new skb to attach new uarg */
1227 if (sk->sk_type == SOCK_STREAM)
1232 next = (u32)atomic_read(&sk->sk_zckey);
1233 if ((u32)(uarg->id + uarg->len) == next) {
1234 if (mm_account_pinned_pages(&uarg->mmp, size))
1237 uarg->bytelen = bytelen;
1238 atomic_set(&sk->sk_zckey, ++next);
1240 /* no extra ref when appending to datagram (MSG_MORE) */
1241 if (sk->sk_type == SOCK_STREAM)
1242 net_zcopy_get(uarg);
1249 return msg_zerocopy_alloc(sk, size);
1251 EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
1253 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1255 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1259 old_lo = serr->ee.ee_info;
1260 old_hi = serr->ee.ee_data;
1261 sum_len = old_hi - old_lo + 1ULL + len;
1263 if (sum_len >= (1ULL << 32))
1266 if (lo != old_hi + 1)
1269 serr->ee.ee_data += len;
1273 static void __msg_zerocopy_callback(struct ubuf_info *uarg)
1275 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1276 struct sock_exterr_skb *serr;
1277 struct sock *sk = skb->sk;
1278 struct sk_buff_head *q;
1279 unsigned long flags;
1284 mm_unaccount_pinned_pages(&uarg->mmp);
1286 /* if !len, there was only 1 call, and it was aborted
1287 * so do not queue a completion notification
1289 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1294 hi = uarg->id + len - 1;
1295 is_zerocopy = uarg->zerocopy;
1297 serr = SKB_EXT_ERR(skb);
1298 memset(serr, 0, sizeof(*serr));
1299 serr->ee.ee_errno = 0;
1300 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1301 serr->ee.ee_data = hi;
1302 serr->ee.ee_info = lo;
1304 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1306 q = &sk->sk_error_queue;
1307 spin_lock_irqsave(&q->lock, flags);
1308 tail = skb_peek_tail(q);
1309 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1310 !skb_zerocopy_notify_extend(tail, lo, len)) {
1311 __skb_queue_tail(q, skb);
1314 spin_unlock_irqrestore(&q->lock, flags);
1316 sk_error_report(sk);
1323 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
1326 uarg->zerocopy = uarg->zerocopy & success;
1328 if (refcount_dec_and_test(&uarg->refcnt))
1329 __msg_zerocopy_callback(uarg);
1331 EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
1333 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1335 struct sock *sk = skb_from_uarg(uarg)->sk;
1337 atomic_dec(&sk->sk_zckey);
1341 msg_zerocopy_callback(NULL, uarg, true);
1343 EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
1345 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1347 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1349 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1351 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1352 struct msghdr *msg, int len,
1353 struct ubuf_info *uarg)
1355 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1356 struct iov_iter orig_iter = msg->msg_iter;
1357 int err, orig_len = skb->len;
1359 /* An skb can only point to one uarg. This edge case happens when
1360 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1362 if (orig_uarg && uarg != orig_uarg)
1365 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1366 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1367 struct sock *save_sk = skb->sk;
1369 /* Streams do not free skb on error. Reset to prev state. */
1370 msg->msg_iter = orig_iter;
1372 ___pskb_trim(skb, orig_len);
1377 skb_zcopy_set(skb, uarg, NULL);
1378 return skb->len - orig_len;
1380 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1382 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1385 if (skb_zcopy(orig)) {
1386 if (skb_zcopy(nskb)) {
1387 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1392 if (skb_uarg(nskb) == skb_uarg(orig))
1394 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1397 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1403 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1404 * @skb: the skb to modify
1405 * @gfp_mask: allocation priority
1407 * This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
1408 * It will copy all frags into kernel and drop the reference
1409 * to userspace pages.
1411 * If this function is called from an interrupt gfp_mask() must be
1414 * Returns 0 on success or a negative error code on failure
1415 * to allocate kernel memory to copy to.
1417 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1419 int num_frags = skb_shinfo(skb)->nr_frags;
1420 struct page *page, *head = NULL;
1424 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1430 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1431 for (i = 0; i < new_frags; i++) {
1432 page = alloc_page(gfp_mask);
1435 struct page *next = (struct page *)page_private(head);
1441 set_page_private(page, (unsigned long)head);
1447 for (i = 0; i < num_frags; i++) {
1448 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1449 u32 p_off, p_len, copied;
1453 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1454 p, p_off, p_len, copied) {
1456 vaddr = kmap_atomic(p);
1458 while (done < p_len) {
1459 if (d_off == PAGE_SIZE) {
1461 page = (struct page *)page_private(page);
1463 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1464 memcpy(page_address(page) + d_off,
1465 vaddr + p_off + done, copy);
1469 kunmap_atomic(vaddr);
1473 /* skb frags release userspace buffers */
1474 for (i = 0; i < num_frags; i++)
1475 skb_frag_unref(skb, i);
1477 /* skb frags point to kernel buffers */
1478 for (i = 0; i < new_frags - 1; i++) {
1479 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1480 head = (struct page *)page_private(head);
1482 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1483 skb_shinfo(skb)->nr_frags = new_frags;
1486 skb_zcopy_clear(skb, false);
1489 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1492 * skb_clone - duplicate an sk_buff
1493 * @skb: buffer to clone
1494 * @gfp_mask: allocation priority
1496 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1497 * copies share the same packet data but not structure. The new
1498 * buffer has a reference count of 1. If the allocation fails the
1499 * function returns %NULL otherwise the new buffer is returned.
1501 * If this function is called from an interrupt gfp_mask() must be
1505 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1507 struct sk_buff_fclones *fclones = container_of(skb,
1508 struct sk_buff_fclones,
1512 if (skb_orphan_frags(skb, gfp_mask))
1515 if (skb->fclone == SKB_FCLONE_ORIG &&
1516 refcount_read(&fclones->fclone_ref) == 1) {
1518 refcount_set(&fclones->fclone_ref, 2);
1520 if (skb_pfmemalloc(skb))
1521 gfp_mask |= __GFP_MEMALLOC;
1523 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1527 n->fclone = SKB_FCLONE_UNAVAILABLE;
1530 return __skb_clone(n, skb);
1532 EXPORT_SYMBOL(skb_clone);
1534 void skb_headers_offset_update(struct sk_buff *skb, int off)
1536 /* Only adjust this if it actually is csum_start rather than csum */
1537 if (skb->ip_summed == CHECKSUM_PARTIAL)
1538 skb->csum_start += off;
1539 /* {transport,network,mac}_header and tail are relative to skb->head */
1540 skb->transport_header += off;
1541 skb->network_header += off;
1542 if (skb_mac_header_was_set(skb))
1543 skb->mac_header += off;
1544 skb->inner_transport_header += off;
1545 skb->inner_network_header += off;
1546 skb->inner_mac_header += off;
1548 EXPORT_SYMBOL(skb_headers_offset_update);
1550 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1552 __copy_skb_header(new, old);
1554 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1555 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1556 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1558 EXPORT_SYMBOL(skb_copy_header);
1560 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1562 if (skb_pfmemalloc(skb))
1563 return SKB_ALLOC_RX;
1568 * skb_copy - create private copy of an sk_buff
1569 * @skb: buffer to copy
1570 * @gfp_mask: allocation priority
1572 * Make a copy of both an &sk_buff and its data. This is used when the
1573 * caller wishes to modify the data and needs a private copy of the
1574 * data to alter. Returns %NULL on failure or the pointer to the buffer
1575 * on success. The returned buffer has a reference count of 1.
1577 * As by-product this function converts non-linear &sk_buff to linear
1578 * one, so that &sk_buff becomes completely private and caller is allowed
1579 * to modify all the data of returned buffer. This means that this
1580 * function is not recommended for use in circumstances when only
1581 * header is going to be modified. Use pskb_copy() instead.
1584 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1586 int headerlen = skb_headroom(skb);
1587 unsigned int size = skb_end_offset(skb) + skb->data_len;
1588 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1589 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1594 /* Set the data pointer */
1595 skb_reserve(n, headerlen);
1596 /* Set the tail pointer and length */
1597 skb_put(n, skb->len);
1599 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1601 skb_copy_header(n, skb);
1604 EXPORT_SYMBOL(skb_copy);
1607 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1608 * @skb: buffer to copy
1609 * @headroom: headroom of new skb
1610 * @gfp_mask: allocation priority
1611 * @fclone: if true allocate the copy of the skb from the fclone
1612 * cache instead of the head cache; it is recommended to set this
1613 * to true for the cases where the copy will likely be cloned
1615 * Make a copy of both an &sk_buff and part of its data, located
1616 * in header. Fragmented data remain shared. This is used when
1617 * the caller wishes to modify only header of &sk_buff and needs
1618 * private copy of the header to alter. Returns %NULL on failure
1619 * or the pointer to the buffer on success.
1620 * The returned buffer has a reference count of 1.
1623 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1624 gfp_t gfp_mask, bool fclone)
1626 unsigned int size = skb_headlen(skb) + headroom;
1627 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1628 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1633 /* Set the data pointer */
1634 skb_reserve(n, headroom);
1635 /* Set the tail pointer and length */
1636 skb_put(n, skb_headlen(skb));
1637 /* Copy the bytes */
1638 skb_copy_from_linear_data(skb, n->data, n->len);
1640 n->truesize += skb->data_len;
1641 n->data_len = skb->data_len;
1644 if (skb_shinfo(skb)->nr_frags) {
1647 if (skb_orphan_frags(skb, gfp_mask) ||
1648 skb_zerocopy_clone(n, skb, gfp_mask)) {
1653 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1654 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1655 skb_frag_ref(skb, i);
1657 skb_shinfo(n)->nr_frags = i;
1660 if (skb_has_frag_list(skb)) {
1661 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1662 skb_clone_fraglist(n);
1665 skb_copy_header(n, skb);
1669 EXPORT_SYMBOL(__pskb_copy_fclone);
1672 * pskb_expand_head - reallocate header of &sk_buff
1673 * @skb: buffer to reallocate
1674 * @nhead: room to add at head
1675 * @ntail: room to add at tail
1676 * @gfp_mask: allocation priority
1678 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1679 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1680 * reference count of 1. Returns zero in the case of success or error,
1681 * if expansion failed. In the last case, &sk_buff is not changed.
1683 * All the pointers pointing into skb header may change and must be
1684 * reloaded after call to this function.
1687 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1690 int i, osize = skb_end_offset(skb);
1691 int size = osize + nhead + ntail;
1697 BUG_ON(skb_shared(skb));
1699 size = SKB_DATA_ALIGN(size);
1701 if (skb_pfmemalloc(skb))
1702 gfp_mask |= __GFP_MEMALLOC;
1703 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1704 gfp_mask, NUMA_NO_NODE, NULL);
1707 size = SKB_WITH_OVERHEAD(ksize(data));
1709 /* Copy only real data... and, alas, header. This should be
1710 * optimized for the cases when header is void.
1712 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1714 memcpy((struct skb_shared_info *)(data + size),
1716 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1719 * if shinfo is shared we must drop the old head gracefully, but if it
1720 * is not we can just drop the old head and let the existing refcount
1721 * be since all we did is relocate the values
1723 if (skb_cloned(skb)) {
1724 if (skb_orphan_frags(skb, gfp_mask))
1727 refcount_inc(&skb_uarg(skb)->refcnt);
1728 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1729 skb_frag_ref(skb, i);
1731 if (skb_has_frag_list(skb))
1732 skb_clone_fraglist(skb);
1734 skb_release_data(skb);
1738 off = (data + nhead) - skb->head;
1744 skb_set_end_offset(skb, size);
1745 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1749 skb_headers_offset_update(skb, nhead);
1753 atomic_set(&skb_shinfo(skb)->dataref, 1);
1755 skb_metadata_clear(skb);
1757 /* It is not generally safe to change skb->truesize.
1758 * For the moment, we really care of rx path, or
1759 * when skb is orphaned (not attached to a socket).
1761 if (!skb->sk || skb->destructor == sock_edemux)
1762 skb->truesize += size - osize;
1771 EXPORT_SYMBOL(pskb_expand_head);
1773 /* Make private copy of skb with writable head and some headroom */
1775 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1777 struct sk_buff *skb2;
1778 int delta = headroom - skb_headroom(skb);
1781 skb2 = pskb_copy(skb, GFP_ATOMIC);
1783 skb2 = skb_clone(skb, GFP_ATOMIC);
1784 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1792 EXPORT_SYMBOL(skb_realloc_headroom);
1794 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
1796 unsigned int saved_end_offset, saved_truesize;
1797 struct skb_shared_info *shinfo;
1800 saved_end_offset = skb_end_offset(skb);
1801 saved_truesize = skb->truesize;
1803 res = pskb_expand_head(skb, 0, 0, pri);
1807 skb->truesize = saved_truesize;
1809 if (likely(skb_end_offset(skb) == saved_end_offset))
1812 shinfo = skb_shinfo(skb);
1814 /* We are about to change back skb->end,
1815 * we need to move skb_shinfo() to its new location.
1817 memmove(skb->head + saved_end_offset,
1819 offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
1821 skb_set_end_offset(skb, saved_end_offset);
1827 * skb_expand_head - reallocate header of &sk_buff
1828 * @skb: buffer to reallocate
1829 * @headroom: needed headroom
1831 * Unlike skb_realloc_headroom, this one does not allocate a new skb
1832 * if possible; copies skb->sk to new skb as needed
1833 * and frees original skb in case of failures.
1835 * It expect increased headroom and generates warning otherwise.
1838 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
1840 int delta = headroom - skb_headroom(skb);
1841 int osize = skb_end_offset(skb);
1842 struct sock *sk = skb->sk;
1844 if (WARN_ONCE(delta <= 0,
1845 "%s is expecting an increase in the headroom", __func__))
1848 delta = SKB_DATA_ALIGN(delta);
1849 /* pskb_expand_head() might crash, if skb is shared. */
1850 if (skb_shared(skb) || !is_skb_wmem(skb)) {
1851 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1853 if (unlikely(!nskb))
1857 skb_set_owner_w(nskb, sk);
1861 if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
1864 if (sk && is_skb_wmem(skb)) {
1865 delta = skb_end_offset(skb) - osize;
1866 refcount_add(delta, &sk->sk_wmem_alloc);
1867 skb->truesize += delta;
1875 EXPORT_SYMBOL(skb_expand_head);
1878 * skb_copy_expand - copy and expand sk_buff
1879 * @skb: buffer to copy
1880 * @newheadroom: new free bytes at head
1881 * @newtailroom: new free bytes at tail
1882 * @gfp_mask: allocation priority
1884 * Make a copy of both an &sk_buff and its data and while doing so
1885 * allocate additional space.
1887 * This is used when the caller wishes to modify the data and needs a
1888 * private copy of the data to alter as well as more space for new fields.
1889 * Returns %NULL on failure or the pointer to the buffer
1890 * on success. The returned buffer has a reference count of 1.
1892 * You must pass %GFP_ATOMIC as the allocation priority if this function
1893 * is called from an interrupt.
1895 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1896 int newheadroom, int newtailroom,
1900 * Allocate the copy buffer
1902 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1903 gfp_mask, skb_alloc_rx_flag(skb),
1905 int oldheadroom = skb_headroom(skb);
1906 int head_copy_len, head_copy_off;
1911 skb_reserve(n, newheadroom);
1913 /* Set the tail pointer and length */
1914 skb_put(n, skb->len);
1916 head_copy_len = oldheadroom;
1918 if (newheadroom <= head_copy_len)
1919 head_copy_len = newheadroom;
1921 head_copy_off = newheadroom - head_copy_len;
1923 /* Copy the linear header and data. */
1924 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1925 skb->len + head_copy_len));
1927 skb_copy_header(n, skb);
1929 skb_headers_offset_update(n, newheadroom - oldheadroom);
1933 EXPORT_SYMBOL(skb_copy_expand);
1936 * __skb_pad - zero pad the tail of an skb
1937 * @skb: buffer to pad
1938 * @pad: space to pad
1939 * @free_on_error: free buffer on error
1941 * Ensure that a buffer is followed by a padding area that is zero
1942 * filled. Used by network drivers which may DMA or transfer data
1943 * beyond the buffer end onto the wire.
1945 * May return error in out of memory cases. The skb is freed on error
1946 * if @free_on_error is true.
1949 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1954 /* If the skbuff is non linear tailroom is always zero.. */
1955 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1956 memset(skb->data+skb->len, 0, pad);
1960 ntail = skb->data_len + pad - (skb->end - skb->tail);
1961 if (likely(skb_cloned(skb) || ntail > 0)) {
1962 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1967 /* FIXME: The use of this function with non-linear skb's really needs
1970 err = skb_linearize(skb);
1974 memset(skb->data + skb->len, 0, pad);
1982 EXPORT_SYMBOL(__skb_pad);
1985 * pskb_put - add data to the tail of a potentially fragmented buffer
1986 * @skb: start of the buffer to use
1987 * @tail: tail fragment of the buffer to use
1988 * @len: amount of data to add
1990 * This function extends the used data area of the potentially
1991 * fragmented buffer. @tail must be the last fragment of @skb -- or
1992 * @skb itself. If this would exceed the total buffer size the kernel
1993 * will panic. A pointer to the first byte of the extra data is
1997 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
2000 skb->data_len += len;
2003 return skb_put(tail, len);
2005 EXPORT_SYMBOL_GPL(pskb_put);
2008 * skb_put - add data to a buffer
2009 * @skb: buffer to use
2010 * @len: amount of data to add
2012 * This function extends the used data area of the buffer. If this would
2013 * exceed the total buffer size the kernel will panic. A pointer to the
2014 * first byte of the extra data is returned.
2016 void *skb_put(struct sk_buff *skb, unsigned int len)
2018 void *tmp = skb_tail_pointer(skb);
2019 SKB_LINEAR_ASSERT(skb);
2022 if (unlikely(skb->tail > skb->end))
2023 skb_over_panic(skb, len, __builtin_return_address(0));
2026 EXPORT_SYMBOL(skb_put);
2029 * skb_push - add data to the start of a buffer
2030 * @skb: buffer to use
2031 * @len: amount of data to add
2033 * This function extends the used data area of the buffer at the buffer
2034 * start. If this would exceed the total buffer headroom the kernel will
2035 * panic. A pointer to the first byte of the extra data is returned.
2037 void *skb_push(struct sk_buff *skb, unsigned int len)
2041 if (unlikely(skb->data < skb->head))
2042 skb_under_panic(skb, len, __builtin_return_address(0));
2045 EXPORT_SYMBOL(skb_push);
2048 * skb_pull - remove data from the start of a buffer
2049 * @skb: buffer to use
2050 * @len: amount of data to remove
2052 * This function removes data from the start of a buffer, returning
2053 * the memory to the headroom. A pointer to the next data in the buffer
2054 * is returned. Once the data has been pulled future pushes will overwrite
2057 void *skb_pull(struct sk_buff *skb, unsigned int len)
2059 return skb_pull_inline(skb, len);
2061 EXPORT_SYMBOL(skb_pull);
2064 * skb_trim - remove end from a buffer
2065 * @skb: buffer to alter
2068 * Cut the length of a buffer down by removing data from the tail. If
2069 * the buffer is already under the length specified it is not modified.
2070 * The skb must be linear.
2072 void skb_trim(struct sk_buff *skb, unsigned int len)
2075 __skb_trim(skb, len);
2077 EXPORT_SYMBOL(skb_trim);
2079 /* Trims skb to length len. It can change skb pointers.
2082 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
2084 struct sk_buff **fragp;
2085 struct sk_buff *frag;
2086 int offset = skb_headlen(skb);
2087 int nfrags = skb_shinfo(skb)->nr_frags;
2091 if (skb_cloned(skb) &&
2092 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2099 for (; i < nfrags; i++) {
2100 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2107 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
2110 skb_shinfo(skb)->nr_frags = i;
2112 for (; i < nfrags; i++)
2113 skb_frag_unref(skb, i);
2115 if (skb_has_frag_list(skb))
2116 skb_drop_fraglist(skb);
2120 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2121 fragp = &frag->next) {
2122 int end = offset + frag->len;
2124 if (skb_shared(frag)) {
2125 struct sk_buff *nfrag;
2127 nfrag = skb_clone(frag, GFP_ATOMIC);
2128 if (unlikely(!nfrag))
2131 nfrag->next = frag->next;
2143 unlikely((err = pskb_trim(frag, len - offset))))
2147 skb_drop_list(&frag->next);
2152 if (len > skb_headlen(skb)) {
2153 skb->data_len -= skb->len - len;
2158 skb_set_tail_pointer(skb, len);
2161 if (!skb->sk || skb->destructor == sock_edemux)
2165 EXPORT_SYMBOL(___pskb_trim);
2167 /* Note : use pskb_trim_rcsum() instead of calling this directly
2169 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2171 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2172 int delta = skb->len - len;
2174 skb->csum = csum_block_sub(skb->csum,
2175 skb_checksum(skb, len, delta, 0),
2177 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2178 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2179 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2181 if (offset + sizeof(__sum16) > hdlen)
2184 return __pskb_trim(skb, len);
2186 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2189 * __pskb_pull_tail - advance tail of skb header
2190 * @skb: buffer to reallocate
2191 * @delta: number of bytes to advance tail
2193 * The function makes a sense only on a fragmented &sk_buff,
2194 * it expands header moving its tail forward and copying necessary
2195 * data from fragmented part.
2197 * &sk_buff MUST have reference count of 1.
2199 * Returns %NULL (and &sk_buff does not change) if pull failed
2200 * or value of new tail of skb in the case of success.
2202 * All the pointers pointing into skb header may change and must be
2203 * reloaded after call to this function.
2206 /* Moves tail of skb head forward, copying data from fragmented part,
2207 * when it is necessary.
2208 * 1. It may fail due to malloc failure.
2209 * 2. It may change skb pointers.
2211 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2213 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
2215 /* If skb has not enough free space at tail, get new one
2216 * plus 128 bytes for future expansions. If we have enough
2217 * room at tail, reallocate without expansion only if skb is cloned.
2219 int i, k, eat = (skb->tail + delta) - skb->end;
2221 if (eat > 0 || skb_cloned(skb)) {
2222 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2227 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2228 skb_tail_pointer(skb), delta));
2230 /* Optimization: no fragments, no reasons to preestimate
2231 * size of pulled pages. Superb.
2233 if (!skb_has_frag_list(skb))
2236 /* Estimate size of pulled pages. */
2238 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2239 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2246 /* If we need update frag list, we are in troubles.
2247 * Certainly, it is possible to add an offset to skb data,
2248 * but taking into account that pulling is expected to
2249 * be very rare operation, it is worth to fight against
2250 * further bloating skb head and crucify ourselves here instead.
2251 * Pure masohism, indeed. 8)8)
2254 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2255 struct sk_buff *clone = NULL;
2256 struct sk_buff *insp = NULL;
2259 if (list->len <= eat) {
2260 /* Eaten as whole. */
2265 /* Eaten partially. */
2266 if (skb_is_gso(skb) && !list->head_frag &&
2268 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2270 if (skb_shared(list)) {
2271 /* Sucks! We need to fork list. :-( */
2272 clone = skb_clone(list, GFP_ATOMIC);
2278 /* This may be pulled without
2282 if (!pskb_pull(list, eat)) {
2290 /* Free pulled out fragments. */
2291 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2292 skb_shinfo(skb)->frag_list = list->next;
2295 /* And insert new clone at head. */
2298 skb_shinfo(skb)->frag_list = clone;
2301 /* Success! Now we may commit changes to skb data. */
2306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2307 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2310 skb_frag_unref(skb, i);
2313 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2315 *frag = skb_shinfo(skb)->frags[i];
2317 skb_frag_off_add(frag, eat);
2318 skb_frag_size_sub(frag, eat);
2326 skb_shinfo(skb)->nr_frags = k;
2330 skb->data_len -= delta;
2333 skb_zcopy_clear(skb, false);
2335 return skb_tail_pointer(skb);
2337 EXPORT_SYMBOL(__pskb_pull_tail);
2340 * skb_copy_bits - copy bits from skb to kernel buffer
2342 * @offset: offset in source
2343 * @to: destination buffer
2344 * @len: number of bytes to copy
2346 * Copy the specified number of bytes from the source skb to the
2347 * destination buffer.
2350 * If its prototype is ever changed,
2351 * check arch/{*}/net/{*}.S files,
2352 * since it is called from BPF assembly code.
2354 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2356 int start = skb_headlen(skb);
2357 struct sk_buff *frag_iter;
2360 if (offset > (int)skb->len - len)
2364 if ((copy = start - offset) > 0) {
2367 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2368 if ((len -= copy) == 0)
2374 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2376 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2378 WARN_ON(start > offset + len);
2380 end = start + skb_frag_size(f);
2381 if ((copy = end - offset) > 0) {
2382 u32 p_off, p_len, copied;
2389 skb_frag_foreach_page(f,
2390 skb_frag_off(f) + offset - start,
2391 copy, p, p_off, p_len, copied) {
2392 vaddr = kmap_atomic(p);
2393 memcpy(to + copied, vaddr + p_off, p_len);
2394 kunmap_atomic(vaddr);
2397 if ((len -= copy) == 0)
2405 skb_walk_frags(skb, frag_iter) {
2408 WARN_ON(start > offset + len);
2410 end = start + frag_iter->len;
2411 if ((copy = end - offset) > 0) {
2414 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2416 if ((len -= copy) == 0)
2430 EXPORT_SYMBOL(skb_copy_bits);
2433 * Callback from splice_to_pipe(), if we need to release some pages
2434 * at the end of the spd in case we error'ed out in filling the pipe.
2436 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2438 put_page(spd->pages[i]);
2441 static struct page *linear_to_page(struct page *page, unsigned int *len,
2442 unsigned int *offset,
2445 struct page_frag *pfrag = sk_page_frag(sk);
2447 if (!sk_page_frag_refill(sk, pfrag))
2450 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2452 memcpy(page_address(pfrag->page) + pfrag->offset,
2453 page_address(page) + *offset, *len);
2454 *offset = pfrag->offset;
2455 pfrag->offset += *len;
2460 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2462 unsigned int offset)
2464 return spd->nr_pages &&
2465 spd->pages[spd->nr_pages - 1] == page &&
2466 (spd->partial[spd->nr_pages - 1].offset +
2467 spd->partial[spd->nr_pages - 1].len == offset);
2471 * Fill page/offset/length into spd, if it can hold more pages.
2473 static bool spd_fill_page(struct splice_pipe_desc *spd,
2474 struct pipe_inode_info *pipe, struct page *page,
2475 unsigned int *len, unsigned int offset,
2479 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2483 page = linear_to_page(page, len, &offset, sk);
2487 if (spd_can_coalesce(spd, page, offset)) {
2488 spd->partial[spd->nr_pages - 1].len += *len;
2492 spd->pages[spd->nr_pages] = page;
2493 spd->partial[spd->nr_pages].len = *len;
2494 spd->partial[spd->nr_pages].offset = offset;
2500 static bool __splice_segment(struct page *page, unsigned int poff,
2501 unsigned int plen, unsigned int *off,
2503 struct splice_pipe_desc *spd, bool linear,
2505 struct pipe_inode_info *pipe)
2510 /* skip this segment if already processed */
2516 /* ignore any bits we already processed */
2522 unsigned int flen = min(*len, plen);
2524 if (spd_fill_page(spd, pipe, page, &flen, poff,
2530 } while (*len && plen);
2536 * Map linear and fragment data from the skb to spd. It reports true if the
2537 * pipe is full or if we already spliced the requested length.
2539 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2540 unsigned int *offset, unsigned int *len,
2541 struct splice_pipe_desc *spd, struct sock *sk)
2544 struct sk_buff *iter;
2546 /* map the linear part :
2547 * If skb->head_frag is set, this 'linear' part is backed by a
2548 * fragment, and if the head is not shared with any clones then
2549 * we can avoid a copy since we own the head portion of this page.
2551 if (__splice_segment(virt_to_page(skb->data),
2552 (unsigned long) skb->data & (PAGE_SIZE - 1),
2555 skb_head_is_locked(skb),
2560 * then map the fragments
2562 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2563 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2565 if (__splice_segment(skb_frag_page(f),
2566 skb_frag_off(f), skb_frag_size(f),
2567 offset, len, spd, false, sk, pipe))
2571 skb_walk_frags(skb, iter) {
2572 if (*offset >= iter->len) {
2573 *offset -= iter->len;
2576 /* __skb_splice_bits() only fails if the output has no room
2577 * left, so no point in going over the frag_list for the error
2580 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2588 * Map data from the skb to a pipe. Should handle both the linear part,
2589 * the fragments, and the frag list.
2591 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2592 struct pipe_inode_info *pipe, unsigned int tlen,
2595 struct partial_page partial[MAX_SKB_FRAGS];
2596 struct page *pages[MAX_SKB_FRAGS];
2597 struct splice_pipe_desc spd = {
2600 .nr_pages_max = MAX_SKB_FRAGS,
2601 .ops = &nosteal_pipe_buf_ops,
2602 .spd_release = sock_spd_release,
2606 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2609 ret = splice_to_pipe(pipe, &spd);
2613 EXPORT_SYMBOL_GPL(skb_splice_bits);
2615 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2616 struct kvec *vec, size_t num, size_t size)
2618 struct socket *sock = sk->sk_socket;
2622 return kernel_sendmsg(sock, msg, vec, num, size);
2625 static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2626 size_t size, int flags)
2628 struct socket *sock = sk->sk_socket;
2632 return kernel_sendpage(sock, page, offset, size, flags);
2635 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2636 struct kvec *vec, size_t num, size_t size);
2637 typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2638 size_t size, int flags);
2639 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2640 int len, sendmsg_func sendmsg, sendpage_func sendpage)
2642 unsigned int orig_len = len;
2643 struct sk_buff *head = skb;
2644 unsigned short fragidx;
2649 /* Deal with head data */
2650 while (offset < skb_headlen(skb) && len) {
2654 slen = min_t(int, len, skb_headlen(skb) - offset);
2655 kv.iov_base = skb->data + offset;
2657 memset(&msg, 0, sizeof(msg));
2658 msg.msg_flags = MSG_DONTWAIT;
2660 ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2661 sendmsg_unlocked, sk, &msg, &kv, 1, slen);
2669 /* All the data was skb head? */
2673 /* Make offset relative to start of frags */
2674 offset -= skb_headlen(skb);
2676 /* Find where we are in frag list */
2677 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2678 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2680 if (offset < skb_frag_size(frag))
2683 offset -= skb_frag_size(frag);
2686 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2687 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2689 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
2692 ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2693 sendpage_unlocked, sk,
2694 skb_frag_page(frag),
2695 skb_frag_off(frag) + offset,
2696 slen, MSG_DONTWAIT);
2709 /* Process any frag lists */
2712 if (skb_has_frag_list(skb)) {
2713 skb = skb_shinfo(skb)->frag_list;
2716 } else if (skb->next) {
2723 return orig_len - len;
2726 return orig_len == len ? ret : orig_len - len;
2729 /* Send skb data on a socket. Socket must be locked. */
2730 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2733 return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2734 kernel_sendpage_locked);
2736 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2738 /* Send skb data on a socket. Socket must be unlocked. */
2739 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2741 return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
2746 * skb_store_bits - store bits from kernel buffer to skb
2747 * @skb: destination buffer
2748 * @offset: offset in destination
2749 * @from: source buffer
2750 * @len: number of bytes to copy
2752 * Copy the specified number of bytes from the source buffer to the
2753 * destination skb. This function handles all the messy bits of
2754 * traversing fragment lists and such.
2757 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2759 int start = skb_headlen(skb);
2760 struct sk_buff *frag_iter;
2763 if (offset > (int)skb->len - len)
2766 if ((copy = start - offset) > 0) {
2769 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2770 if ((len -= copy) == 0)
2776 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2777 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2780 WARN_ON(start > offset + len);
2782 end = start + skb_frag_size(frag);
2783 if ((copy = end - offset) > 0) {
2784 u32 p_off, p_len, copied;
2791 skb_frag_foreach_page(frag,
2792 skb_frag_off(frag) + offset - start,
2793 copy, p, p_off, p_len, copied) {
2794 vaddr = kmap_atomic(p);
2795 memcpy(vaddr + p_off, from + copied, p_len);
2796 kunmap_atomic(vaddr);
2799 if ((len -= copy) == 0)
2807 skb_walk_frags(skb, frag_iter) {
2810 WARN_ON(start > offset + len);
2812 end = start + frag_iter->len;
2813 if ((copy = end - offset) > 0) {
2816 if (skb_store_bits(frag_iter, offset - start,
2819 if ((len -= copy) == 0)
2832 EXPORT_SYMBOL(skb_store_bits);
2834 /* Checksum skb data. */
2835 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2836 __wsum csum, const struct skb_checksum_ops *ops)
2838 int start = skb_headlen(skb);
2839 int i, copy = start - offset;
2840 struct sk_buff *frag_iter;
2843 /* Checksum header. */
2847 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2848 skb->data + offset, copy, csum);
2849 if ((len -= copy) == 0)
2855 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2857 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2859 WARN_ON(start > offset + len);
2861 end = start + skb_frag_size(frag);
2862 if ((copy = end - offset) > 0) {
2863 u32 p_off, p_len, copied;
2871 skb_frag_foreach_page(frag,
2872 skb_frag_off(frag) + offset - start,
2873 copy, p, p_off, p_len, copied) {
2874 vaddr = kmap_atomic(p);
2875 csum2 = INDIRECT_CALL_1(ops->update,
2877 vaddr + p_off, p_len, 0);
2878 kunmap_atomic(vaddr);
2879 csum = INDIRECT_CALL_1(ops->combine,
2880 csum_block_add_ext, csum,
2892 skb_walk_frags(skb, frag_iter) {
2895 WARN_ON(start > offset + len);
2897 end = start + frag_iter->len;
2898 if ((copy = end - offset) > 0) {
2902 csum2 = __skb_checksum(frag_iter, offset - start,
2904 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2905 csum, csum2, pos, copy);
2906 if ((len -= copy) == 0)
2917 EXPORT_SYMBOL(__skb_checksum);
2919 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2920 int len, __wsum csum)
2922 const struct skb_checksum_ops ops = {
2923 .update = csum_partial_ext,
2924 .combine = csum_block_add_ext,
2927 return __skb_checksum(skb, offset, len, csum, &ops);
2929 EXPORT_SYMBOL(skb_checksum);
2931 /* Both of above in one bottle. */
2933 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2936 int start = skb_headlen(skb);
2937 int i, copy = start - offset;
2938 struct sk_buff *frag_iter;
2946 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2948 if ((len -= copy) == 0)
2955 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2958 WARN_ON(start > offset + len);
2960 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2961 if ((copy = end - offset) > 0) {
2962 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2963 u32 p_off, p_len, copied;
2971 skb_frag_foreach_page(frag,
2972 skb_frag_off(frag) + offset - start,
2973 copy, p, p_off, p_len, copied) {
2974 vaddr = kmap_atomic(p);
2975 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2978 kunmap_atomic(vaddr);
2979 csum = csum_block_add(csum, csum2, pos);
2991 skb_walk_frags(skb, frag_iter) {
2995 WARN_ON(start > offset + len);
2997 end = start + frag_iter->len;
2998 if ((copy = end - offset) > 0) {
3001 csum2 = skb_copy_and_csum_bits(frag_iter,
3004 csum = csum_block_add(csum, csum2, pos);
3005 if ((len -= copy) == 0)
3016 EXPORT_SYMBOL(skb_copy_and_csum_bits);
3018 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
3022 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
3023 /* See comments in __skb_checksum_complete(). */
3025 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3026 !skb->csum_complete_sw)
3027 netdev_rx_csum_fault(skb->dev, skb);
3029 if (!skb_shared(skb))
3030 skb->csum_valid = !sum;
3033 EXPORT_SYMBOL(__skb_checksum_complete_head);
3035 /* This function assumes skb->csum already holds pseudo header's checksum,
3036 * which has been changed from the hardware checksum, for example, by
3037 * __skb_checksum_validate_complete(). And, the original skb->csum must
3038 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
3040 * It returns non-zero if the recomputed checksum is still invalid, otherwise
3041 * zero. The new checksum is stored back into skb->csum unless the skb is
3044 __sum16 __skb_checksum_complete(struct sk_buff *skb)
3049 csum = skb_checksum(skb, 0, skb->len, 0);
3051 sum = csum_fold(csum_add(skb->csum, csum));
3052 /* This check is inverted, because we already knew the hardware
3053 * checksum is invalid before calling this function. So, if the
3054 * re-computed checksum is valid instead, then we have a mismatch
3055 * between the original skb->csum and skb_checksum(). This means either
3056 * the original hardware checksum is incorrect or we screw up skb->csum
3057 * when moving skb->data around.
3060 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3061 !skb->csum_complete_sw)
3062 netdev_rx_csum_fault(skb->dev, skb);
3065 if (!skb_shared(skb)) {
3066 /* Save full packet checksum */
3068 skb->ip_summed = CHECKSUM_COMPLETE;
3069 skb->csum_complete_sw = 1;
3070 skb->csum_valid = !sum;
3075 EXPORT_SYMBOL(__skb_checksum_complete);
3077 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
3079 net_warn_ratelimited(
3080 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3085 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
3086 int offset, int len)
3088 net_warn_ratelimited(
3089 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3094 static const struct skb_checksum_ops default_crc32c_ops = {
3095 .update = warn_crc32c_csum_update,
3096 .combine = warn_crc32c_csum_combine,
3099 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3100 &default_crc32c_ops;
3101 EXPORT_SYMBOL(crc32c_csum_stub);
3104 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3105 * @from: source buffer
3107 * Calculates the amount of linear headroom needed in the 'to' skb passed
3108 * into skb_zerocopy().
3111 skb_zerocopy_headlen(const struct sk_buff *from)
3113 unsigned int hlen = 0;
3115 if (!from->head_frag ||
3116 skb_headlen(from) < L1_CACHE_BYTES ||
3117 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3118 hlen = skb_headlen(from);
3123 if (skb_has_frag_list(from))
3128 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3131 * skb_zerocopy - Zero copy skb to skb
3132 * @to: destination buffer
3133 * @from: source buffer
3134 * @len: number of bytes to copy from source buffer
3135 * @hlen: size of linear headroom in destination buffer
3137 * Copies up to `len` bytes from `from` to `to` by creating references
3138 * to the frags in the source buffer.
3140 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3141 * headroom in the `to` buffer.
3144 * 0: everything is OK
3145 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
3146 * -EFAULT: skb_copy_bits() found some problem with skb geometry
3149 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3152 int plen = 0; /* length of skb->head fragment */
3155 unsigned int offset;
3157 BUG_ON(!from->head_frag && !hlen);
3159 /* dont bother with small payloads */
3160 if (len <= skb_tailroom(to))
3161 return skb_copy_bits(from, 0, skb_put(to, len), len);
3164 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3169 plen = min_t(int, skb_headlen(from), len);
3171 page = virt_to_head_page(from->head);
3172 offset = from->data - (unsigned char *)page_address(page);
3173 __skb_fill_page_desc(to, 0, page, offset, plen);
3180 to->truesize += len + plen;
3181 to->len += len + plen;
3182 to->data_len += len + plen;
3184 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3188 skb_zerocopy_clone(to, from, GFP_ATOMIC);
3190 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3195 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3196 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3198 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3200 skb_frag_ref(to, j);
3203 skb_shinfo(to)->nr_frags = j;
3207 EXPORT_SYMBOL_GPL(skb_zerocopy);
3209 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3214 if (skb->ip_summed == CHECKSUM_PARTIAL)
3215 csstart = skb_checksum_start_offset(skb);
3217 csstart = skb_headlen(skb);
3219 BUG_ON(csstart > skb_headlen(skb));
3221 skb_copy_from_linear_data(skb, to, csstart);
3224 if (csstart != skb->len)
3225 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3226 skb->len - csstart);
3228 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3229 long csstuff = csstart + skb->csum_offset;
3231 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
3234 EXPORT_SYMBOL(skb_copy_and_csum_dev);
3237 * skb_dequeue - remove from the head of the queue
3238 * @list: list to dequeue from
3240 * Remove the head of the list. The list lock is taken so the function
3241 * may be used safely with other locking list functions. The head item is
3242 * returned or %NULL if the list is empty.
3245 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3247 unsigned long flags;
3248 struct sk_buff *result;
3250 spin_lock_irqsave(&list->lock, flags);
3251 result = __skb_dequeue(list);
3252 spin_unlock_irqrestore(&list->lock, flags);
3255 EXPORT_SYMBOL(skb_dequeue);
3258 * skb_dequeue_tail - remove from the tail of the queue
3259 * @list: list to dequeue from
3261 * Remove the tail of the list. The list lock is taken so the function
3262 * may be used safely with other locking list functions. The tail item is
3263 * returned or %NULL if the list is empty.
3265 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3267 unsigned long flags;
3268 struct sk_buff *result;
3270 spin_lock_irqsave(&list->lock, flags);
3271 result = __skb_dequeue_tail(list);
3272 spin_unlock_irqrestore(&list->lock, flags);
3275 EXPORT_SYMBOL(skb_dequeue_tail);
3278 * skb_queue_purge - empty a list
3279 * @list: list to empty
3281 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3282 * the list and one reference dropped. This function takes the list
3283 * lock and is atomic with respect to other list locking functions.
3285 void skb_queue_purge(struct sk_buff_head *list)
3287 struct sk_buff *skb;
3288 while ((skb = skb_dequeue(list)) != NULL)
3291 EXPORT_SYMBOL(skb_queue_purge);
3294 * skb_rbtree_purge - empty a skb rbtree
3295 * @root: root of the rbtree to empty
3296 * Return value: the sum of truesizes of all purged skbs.
3298 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3299 * the list and one reference dropped. This function does not take
3300 * any lock. Synchronization should be handled by the caller (e.g., TCP
3301 * out-of-order queue is protected by the socket lock).
3303 unsigned int skb_rbtree_purge(struct rb_root *root)
3305 struct rb_node *p = rb_first(root);
3306 unsigned int sum = 0;
3309 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3312 rb_erase(&skb->rbnode, root);
3313 sum += skb->truesize;
3320 * skb_queue_head - queue a buffer at the list head
3321 * @list: list to use
3322 * @newsk: buffer to queue
3324 * Queue a buffer at the start of the list. This function takes the
3325 * list lock and can be used safely with other locking &sk_buff functions
3328 * A buffer cannot be placed on two lists at the same time.
3330 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3332 unsigned long flags;
3334 spin_lock_irqsave(&list->lock, flags);
3335 __skb_queue_head(list, newsk);
3336 spin_unlock_irqrestore(&list->lock, flags);
3338 EXPORT_SYMBOL(skb_queue_head);
3341 * skb_queue_tail - queue a buffer at the list tail
3342 * @list: list to use
3343 * @newsk: buffer to queue
3345 * Queue a buffer at the tail of the list. This function takes the
3346 * list lock and can be used safely with other locking &sk_buff functions
3349 * A buffer cannot be placed on two lists at the same time.
3351 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3353 unsigned long flags;
3355 spin_lock_irqsave(&list->lock, flags);
3356 __skb_queue_tail(list, newsk);
3357 spin_unlock_irqrestore(&list->lock, flags);
3359 EXPORT_SYMBOL(skb_queue_tail);
3362 * skb_unlink - remove a buffer from a list
3363 * @skb: buffer to remove
3364 * @list: list to use
3366 * Remove a packet from a list. The list locks are taken and this
3367 * function is atomic with respect to other list locked calls
3369 * You must know what list the SKB is on.
3371 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3373 unsigned long flags;
3375 spin_lock_irqsave(&list->lock, flags);
3376 __skb_unlink(skb, list);
3377 spin_unlock_irqrestore(&list->lock, flags);
3379 EXPORT_SYMBOL(skb_unlink);
3382 * skb_append - append a buffer
3383 * @old: buffer to insert after
3384 * @newsk: buffer to insert
3385 * @list: list to use
3387 * Place a packet after a given packet in a list. The list locks are taken
3388 * and this function is atomic with respect to other list locked calls.
3389 * A buffer cannot be placed on two lists at the same time.
3391 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3393 unsigned long flags;
3395 spin_lock_irqsave(&list->lock, flags);
3396 __skb_queue_after(list, old, newsk);
3397 spin_unlock_irqrestore(&list->lock, flags);
3399 EXPORT_SYMBOL(skb_append);
3401 static inline void skb_split_inside_header(struct sk_buff *skb,
3402 struct sk_buff* skb1,
3403 const u32 len, const int pos)
3407 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3409 /* And move data appendix as is. */
3410 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3411 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3413 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3414 skb_shinfo(skb)->nr_frags = 0;
3415 skb1->data_len = skb->data_len;
3416 skb1->len += skb1->data_len;
3419 skb_set_tail_pointer(skb, len);
3422 static inline void skb_split_no_header(struct sk_buff *skb,
3423 struct sk_buff* skb1,
3424 const u32 len, int pos)
3427 const int nfrags = skb_shinfo(skb)->nr_frags;
3429 skb_shinfo(skb)->nr_frags = 0;
3430 skb1->len = skb1->data_len = skb->len - len;
3432 skb->data_len = len - pos;
3434 for (i = 0; i < nfrags; i++) {
3435 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3437 if (pos + size > len) {
3438 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3442 * We have two variants in this case:
3443 * 1. Move all the frag to the second
3444 * part, if it is possible. F.e.
3445 * this approach is mandatory for TUX,
3446 * where splitting is expensive.
3447 * 2. Split is accurately. We make this.
3449 skb_frag_ref(skb, i);
3450 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
3451 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3452 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3453 skb_shinfo(skb)->nr_frags++;
3457 skb_shinfo(skb)->nr_frags++;
3460 skb_shinfo(skb1)->nr_frags = k;
3464 * skb_split - Split fragmented skb to two parts at length len.
3465 * @skb: the buffer to split
3466 * @skb1: the buffer to receive the second part
3467 * @len: new length for skb
3469 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3471 int pos = skb_headlen(skb);
3473 skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3474 skb_zerocopy_clone(skb1, skb, 0);
3475 if (len < pos) /* Split line is inside header. */
3476 skb_split_inside_header(skb, skb1, len, pos);
3477 else /* Second chunk has no header, nothing to copy. */
3478 skb_split_no_header(skb, skb1, len, pos);
3480 EXPORT_SYMBOL(skb_split);
3482 /* Shifting from/to a cloned skb is a no-go.
3484 * Caller cannot keep skb_shinfo related pointers past calling here!
3486 static int skb_prepare_for_shift(struct sk_buff *skb)
3488 return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
3492 * skb_shift - Shifts paged data partially from skb to another
3493 * @tgt: buffer into which tail data gets added
3494 * @skb: buffer from which the paged data comes from
3495 * @shiftlen: shift up to this many bytes
3497 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3498 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3499 * It's up to caller to free skb if everything was shifted.
3501 * If @tgt runs out of frags, the whole operation is aborted.
3503 * Skb cannot include anything else but paged data while tgt is allowed
3504 * to have non-paged data as well.
3506 * TODO: full sized shift could be optimized but that would need
3507 * specialized skb free'er to handle frags without up-to-date nr_frags.
3509 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3511 int from, to, merge, todo;
3512 skb_frag_t *fragfrom, *fragto;
3514 BUG_ON(shiftlen > skb->len);
3516 if (skb_headlen(skb))
3518 if (skb_zcopy(tgt) || skb_zcopy(skb))
3523 to = skb_shinfo(tgt)->nr_frags;
3524 fragfrom = &skb_shinfo(skb)->frags[from];
3526 /* Actual merge is delayed until the point when we know we can
3527 * commit all, so that we don't have to undo partial changes
3530 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3531 skb_frag_off(fragfrom))) {
3536 todo -= skb_frag_size(fragfrom);
3538 if (skb_prepare_for_shift(skb) ||
3539 skb_prepare_for_shift(tgt))
3542 /* All previous frag pointers might be stale! */
3543 fragfrom = &skb_shinfo(skb)->frags[from];
3544 fragto = &skb_shinfo(tgt)->frags[merge];
3546 skb_frag_size_add(fragto, shiftlen);
3547 skb_frag_size_sub(fragfrom, shiftlen);
3548 skb_frag_off_add(fragfrom, shiftlen);
3556 /* Skip full, not-fitting skb to avoid expensive operations */
3557 if ((shiftlen == skb->len) &&
3558 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3561 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3564 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3565 if (to == MAX_SKB_FRAGS)
3568 fragfrom = &skb_shinfo(skb)->frags[from];
3569 fragto = &skb_shinfo(tgt)->frags[to];
3571 if (todo >= skb_frag_size(fragfrom)) {
3572 *fragto = *fragfrom;
3573 todo -= skb_frag_size(fragfrom);
3578 __skb_frag_ref(fragfrom);
3579 skb_frag_page_copy(fragto, fragfrom);
3580 skb_frag_off_copy(fragto, fragfrom);
3581 skb_frag_size_set(fragto, todo);
3583 skb_frag_off_add(fragfrom, todo);
3584 skb_frag_size_sub(fragfrom, todo);
3592 /* Ready to "commit" this state change to tgt */
3593 skb_shinfo(tgt)->nr_frags = to;
3596 fragfrom = &skb_shinfo(skb)->frags[0];
3597 fragto = &skb_shinfo(tgt)->frags[merge];
3599 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3600 __skb_frag_unref(fragfrom, skb->pp_recycle);
3603 /* Reposition in the original skb */
3605 while (from < skb_shinfo(skb)->nr_frags)
3606 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3607 skb_shinfo(skb)->nr_frags = to;
3609 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3612 /* Most likely the tgt won't ever need its checksum anymore, skb on
3613 * the other hand might need it if it needs to be resent
3615 tgt->ip_summed = CHECKSUM_PARTIAL;
3616 skb->ip_summed = CHECKSUM_PARTIAL;
3618 /* Yak, is it really working this way? Some helper please? */
3619 skb->len -= shiftlen;
3620 skb->data_len -= shiftlen;
3621 skb->truesize -= shiftlen;
3622 tgt->len += shiftlen;
3623 tgt->data_len += shiftlen;
3624 tgt->truesize += shiftlen;
3630 * skb_prepare_seq_read - Prepare a sequential read of skb data
3631 * @skb: the buffer to read
3632 * @from: lower offset of data to be read
3633 * @to: upper offset of data to be read
3634 * @st: state variable
3636 * Initializes the specified state variable. Must be called before
3637 * invoking skb_seq_read() for the first time.
3639 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3640 unsigned int to, struct skb_seq_state *st)
3642 st->lower_offset = from;
3643 st->upper_offset = to;
3644 st->root_skb = st->cur_skb = skb;
3645 st->frag_idx = st->stepped_offset = 0;
3646 st->frag_data = NULL;
3649 EXPORT_SYMBOL(skb_prepare_seq_read);
3652 * skb_seq_read - Sequentially read skb data
3653 * @consumed: number of bytes consumed by the caller so far
3654 * @data: destination pointer for data to be returned
3655 * @st: state variable
3657 * Reads a block of skb data at @consumed relative to the
3658 * lower offset specified to skb_prepare_seq_read(). Assigns
3659 * the head of the data block to @data and returns the length
3660 * of the block or 0 if the end of the skb data or the upper
3661 * offset has been reached.
3663 * The caller is not required to consume all of the data
3664 * returned, i.e. @consumed is typically set to the number
3665 * of bytes already consumed and the next call to
3666 * skb_seq_read() will return the remaining part of the block.
3668 * Note 1: The size of each block of data returned can be arbitrary,
3669 * this limitation is the cost for zerocopy sequential
3670 * reads of potentially non linear data.
3672 * Note 2: Fragment lists within fragments are not implemented
3673 * at the moment, state->root_skb could be replaced with
3674 * a stack for this purpose.
3676 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3677 struct skb_seq_state *st)
3679 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3682 if (unlikely(abs_offset >= st->upper_offset)) {
3683 if (st->frag_data) {
3684 kunmap_atomic(st->frag_data);
3685 st->frag_data = NULL;
3691 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3693 if (abs_offset < block_limit && !st->frag_data) {
3694 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3695 return block_limit - abs_offset;
3698 if (st->frag_idx == 0 && !st->frag_data)
3699 st->stepped_offset += skb_headlen(st->cur_skb);
3701 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3702 unsigned int pg_idx, pg_off, pg_sz;
3704 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3707 pg_off = skb_frag_off(frag);
3708 pg_sz = skb_frag_size(frag);
3710 if (skb_frag_must_loop(skb_frag_page(frag))) {
3711 pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3712 pg_off = offset_in_page(pg_off + st->frag_off);
3713 pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3714 PAGE_SIZE - pg_off);
3717 block_limit = pg_sz + st->stepped_offset;
3718 if (abs_offset < block_limit) {
3720 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
3722 *data = (u8 *)st->frag_data + pg_off +
3723 (abs_offset - st->stepped_offset);
3725 return block_limit - abs_offset;
3728 if (st->frag_data) {
3729 kunmap_atomic(st->frag_data);
3730 st->frag_data = NULL;
3733 st->stepped_offset += pg_sz;
3734 st->frag_off += pg_sz;
3735 if (st->frag_off == skb_frag_size(frag)) {
3741 if (st->frag_data) {
3742 kunmap_atomic(st->frag_data);
3743 st->frag_data = NULL;
3746 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3747 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3750 } else if (st->cur_skb->next) {
3751 st->cur_skb = st->cur_skb->next;
3758 EXPORT_SYMBOL(skb_seq_read);
3761 * skb_abort_seq_read - Abort a sequential read of skb data
3762 * @st: state variable
3764 * Must be called if skb_seq_read() was not called until it
3767 void skb_abort_seq_read(struct skb_seq_state *st)
3770 kunmap_atomic(st->frag_data);
3772 EXPORT_SYMBOL(skb_abort_seq_read);
3774 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3776 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3777 struct ts_config *conf,
3778 struct ts_state *state)
3780 return skb_seq_read(offset, text, TS_SKB_CB(state));
3783 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3785 skb_abort_seq_read(TS_SKB_CB(state));
3789 * skb_find_text - Find a text pattern in skb data
3790 * @skb: the buffer to look in
3791 * @from: search offset
3793 * @config: textsearch configuration
3795 * Finds a pattern in the skb data according to the specified
3796 * textsearch configuration. Use textsearch_next() to retrieve
3797 * subsequent occurrences of the pattern. Returns the offset
3798 * to the first occurrence or UINT_MAX if no match was found.
3800 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3801 unsigned int to, struct ts_config *config)
3803 struct ts_state state;
3806 BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
3808 config->get_next_block = skb_ts_get_next_block;
3809 config->finish = skb_ts_finish;
3811 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3813 ret = textsearch_find(config, &state);
3814 return (ret <= to - from ? ret : UINT_MAX);
3816 EXPORT_SYMBOL(skb_find_text);
3818 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3819 int offset, size_t size)
3821 int i = skb_shinfo(skb)->nr_frags;
3823 if (skb_can_coalesce(skb, i, page, offset)) {
3824 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3825 } else if (i < MAX_SKB_FRAGS) {
3827 skb_fill_page_desc_noacc(skb, i, page, offset, size);
3834 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3837 * skb_pull_rcsum - pull skb and update receive checksum
3838 * @skb: buffer to update
3839 * @len: length of data pulled
3841 * This function performs an skb_pull on the packet and updates
3842 * the CHECKSUM_COMPLETE checksum. It should be used on
3843 * receive path processing instead of skb_pull unless you know
3844 * that the checksum difference is zero (e.g., a valid IP header)
3845 * or you are setting ip_summed to CHECKSUM_NONE.
3847 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3849 unsigned char *data = skb->data;
3851 BUG_ON(len > skb->len);
3852 __skb_pull(skb, len);
3853 skb_postpull_rcsum(skb, data, len);
3856 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3858 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3860 skb_frag_t head_frag;
3863 page = virt_to_head_page(frag_skb->head);
3864 __skb_frag_set_page(&head_frag, page);
3865 skb_frag_off_set(&head_frag, frag_skb->data -
3866 (unsigned char *)page_address(page));
3867 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
3871 struct sk_buff *skb_segment_list(struct sk_buff *skb,
3872 netdev_features_t features,
3873 unsigned int offset)
3875 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3876 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3877 unsigned int delta_truesize = 0;
3878 unsigned int delta_len = 0;
3879 struct sk_buff *tail = NULL;
3880 struct sk_buff *nskb, *tmp;
3883 skb_push(skb, -skb_network_offset(skb) + offset);
3885 /* Ensure the head is writeable before touching the shared info */
3886 err = skb_unclone(skb, GFP_ATOMIC);
3890 skb_shinfo(skb)->frag_list = NULL;
3894 list_skb = list_skb->next;
3897 delta_truesize += nskb->truesize;
3898 if (skb_shared(nskb)) {
3899 tmp = skb_clone(nskb, GFP_ATOMIC);
3903 err = skb_unclone(nskb, GFP_ATOMIC);
3914 if (unlikely(err)) {
3915 nskb->next = list_skb;
3921 delta_len += nskb->len;
3923 skb_push(nskb, -skb_network_offset(nskb) + offset);
3925 skb_release_head_state(nskb);
3926 len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
3927 __copy_skb_header(nskb, skb);
3929 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3930 nskb->transport_header += len_diff;
3931 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3932 nskb->data - tnl_hlen,
3935 if (skb_needs_linearize(nskb, features) &&
3936 __skb_linearize(nskb))
3940 skb->truesize = skb->truesize - delta_truesize;
3941 skb->data_len = skb->data_len - delta_len;
3942 skb->len = skb->len - delta_len;
3948 if (skb_needs_linearize(skb, features) &&
3949 __skb_linearize(skb))
3957 kfree_skb_list(skb->next);
3959 return ERR_PTR(-ENOMEM);
3961 EXPORT_SYMBOL_GPL(skb_segment_list);
3963 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3965 if (unlikely(p->len + skb->len >= 65536))
3968 if (NAPI_GRO_CB(p)->last == p)
3969 skb_shinfo(p)->frag_list = skb;
3971 NAPI_GRO_CB(p)->last->next = skb;
3973 skb_pull(skb, skb_gro_offset(skb));
3975 NAPI_GRO_CB(p)->last = skb;
3976 NAPI_GRO_CB(p)->count++;
3977 p->data_len += skb->len;
3979 /* sk owenrship - if any - completely transferred to the aggregated packet */
3980 skb->destructor = NULL;
3981 p->truesize += skb->truesize;
3984 NAPI_GRO_CB(skb)->same_flow = 1;
3990 * skb_segment - Perform protocol segmentation on skb.
3991 * @head_skb: buffer to segment
3992 * @features: features for the output path (see dev->features)
3994 * This function performs segmentation on the given skb. It returns
3995 * a pointer to the first in a list of new skbs for the segments.
3996 * In case of error it returns ERR_PTR(err).
3998 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3999 netdev_features_t features)
4001 struct sk_buff *segs = NULL;
4002 struct sk_buff *tail = NULL;
4003 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4004 unsigned int mss = skb_shinfo(head_skb)->gso_size;
4005 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4006 unsigned int offset = doffset;
4007 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
4008 unsigned int partial_segs = 0;
4009 unsigned int headroom;
4010 unsigned int len = head_skb->len;
4011 struct sk_buff *frag_skb;
4019 if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
4020 mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
4021 struct sk_buff *check_skb;
4023 for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
4024 if (skb_headlen(check_skb) && !check_skb->head_frag) {
4025 /* gso_size is untrusted, and we have a frag_list with
4026 * a linear non head_frag item.
4028 * If head_skb's headlen does not fit requested gso_size,
4029 * it means that the frag_list members do NOT terminate
4030 * on exact gso_size boundaries. Hence we cannot perform
4031 * skb_frag_t page sharing. Therefore we must fallback to
4032 * copying the frag_list skbs; we do so by disabling SG.
4034 features &= ~NETIF_F_SG;
4040 __skb_push(head_skb, doffset);
4041 proto = skb_network_protocol(head_skb, NULL);
4042 if (unlikely(!proto))
4043 return ERR_PTR(-EINVAL);
4045 sg = !!(features & NETIF_F_SG);
4046 csum = !!can_checksum_protocol(features, proto);
4048 if (sg && csum && (mss != GSO_BY_FRAGS)) {
4049 if (!(features & NETIF_F_GSO_PARTIAL)) {
4050 struct sk_buff *iter;
4051 unsigned int frag_len;
4054 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
4057 /* If we get here then all the required
4058 * GSO features except frag_list are supported.
4059 * Try to split the SKB to multiple GSO SKBs
4060 * with no frag_list.
4061 * Currently we can do that only when the buffers don't
4062 * have a linear part and all the buffers except
4063 * the last are of the same length.
4065 frag_len = list_skb->len;
4066 skb_walk_frags(head_skb, iter) {
4067 if (frag_len != iter->len && iter->next)
4069 if (skb_headlen(iter) && !iter->head_frag)
4075 if (len != frag_len)
4079 /* GSO partial only requires that we trim off any excess that
4080 * doesn't fit into an MSS sized block, so take care of that
4083 partial_segs = len / mss;
4084 if (partial_segs > 1)
4085 mss *= partial_segs;
4091 headroom = skb_headroom(head_skb);
4092 pos = skb_headlen(head_skb);
4094 if (skb_orphan_frags(head_skb, GFP_ATOMIC))
4095 return ERR_PTR(-ENOMEM);
4097 nfrags = skb_shinfo(head_skb)->nr_frags;
4098 frag = skb_shinfo(head_skb)->frags;
4099 frag_skb = head_skb;
4102 struct sk_buff *nskb;
4103 skb_frag_t *nskb_frag;
4107 if (unlikely(mss == GSO_BY_FRAGS)) {
4108 len = list_skb->len;
4110 len = head_skb->len - offset;
4115 hsize = skb_headlen(head_skb) - offset;
4117 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
4118 (skb_headlen(list_skb) == len || sg)) {
4119 BUG_ON(skb_headlen(list_skb) > len);
4121 nskb = skb_clone(list_skb, GFP_ATOMIC);
4122 if (unlikely(!nskb))
4126 nfrags = skb_shinfo(list_skb)->nr_frags;
4127 frag = skb_shinfo(list_skb)->frags;
4128 frag_skb = list_skb;
4129 pos += skb_headlen(list_skb);
4131 while (pos < offset + len) {
4132 BUG_ON(i >= nfrags);
4134 size = skb_frag_size(frag);
4135 if (pos + size > offset + len)
4143 list_skb = list_skb->next;
4145 if (unlikely(pskb_trim(nskb, len))) {
4150 hsize = skb_end_offset(nskb);
4151 if (skb_cow_head(nskb, doffset + headroom)) {
4156 nskb->truesize += skb_end_offset(nskb) - hsize;
4157 skb_release_head_state(nskb);
4158 __skb_push(nskb, doffset);
4162 if (hsize > len || !sg)
4165 nskb = __alloc_skb(hsize + doffset + headroom,
4166 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4169 if (unlikely(!nskb))
4172 skb_reserve(nskb, headroom);
4173 __skb_put(nskb, doffset);
4182 __copy_skb_header(nskb, head_skb);
4184 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4185 skb_reset_mac_len(nskb);
4187 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
4188 nskb->data - tnl_hlen,
4189 doffset + tnl_hlen);
4191 if (nskb->len == len + doffset)
4192 goto perform_csum_check;
4196 if (!nskb->remcsum_offload)
4197 nskb->ip_summed = CHECKSUM_NONE;
4198 SKB_GSO_CB(nskb)->csum =
4199 skb_copy_and_csum_bits(head_skb, offset,
4203 SKB_GSO_CB(nskb)->csum_start =
4204 skb_headroom(nskb) + doffset;
4206 if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
4212 nskb_frag = skb_shinfo(nskb)->frags;
4214 skb_copy_from_linear_data_offset(head_skb, offset,
4215 skb_put(nskb, hsize), hsize);
4217 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4220 if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4223 while (pos < offset + len) {
4225 if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
4226 skb_zerocopy_clone(nskb, list_skb,
4231 nfrags = skb_shinfo(list_skb)->nr_frags;
4232 frag = skb_shinfo(list_skb)->frags;
4233 frag_skb = list_skb;
4234 if (!skb_headlen(list_skb)) {
4237 BUG_ON(!list_skb->head_frag);
4239 /* to make room for head_frag. */
4244 list_skb = list_skb->next;
4247 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4249 net_warn_ratelimited(
4250 "skb_segment: too many frags: %u %u\n",
4256 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4257 __skb_frag_ref(nskb_frag);
4258 size = skb_frag_size(nskb_frag);
4261 skb_frag_off_add(nskb_frag, offset - pos);
4262 skb_frag_size_sub(nskb_frag, offset - pos);
4265 skb_shinfo(nskb)->nr_frags++;
4267 if (pos + size <= offset + len) {
4272 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
4280 nskb->data_len = len - hsize;
4281 nskb->len += nskb->data_len;
4282 nskb->truesize += nskb->data_len;
4286 if (skb_has_shared_frag(nskb) &&
4287 __skb_linearize(nskb))
4290 if (!nskb->remcsum_offload)
4291 nskb->ip_summed = CHECKSUM_NONE;
4292 SKB_GSO_CB(nskb)->csum =
4293 skb_checksum(nskb, doffset,
4294 nskb->len - doffset, 0);
4295 SKB_GSO_CB(nskb)->csum_start =
4296 skb_headroom(nskb) + doffset;
4298 } while ((offset += len) < head_skb->len);
4300 /* Some callers want to get the end of the list.
4301 * Put it in segs->prev to avoid walking the list.
4302 * (see validate_xmit_skb_list() for example)
4307 struct sk_buff *iter;
4308 int type = skb_shinfo(head_skb)->gso_type;
4309 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4311 /* Update type to add partial and then remove dodgy if set */
4312 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4313 type &= ~SKB_GSO_DODGY;
4315 /* Update GSO info and prepare to start updating headers on
4316 * our way back down the stack of protocols.
4318 for (iter = segs; iter; iter = iter->next) {
4319 skb_shinfo(iter)->gso_size = gso_size;
4320 skb_shinfo(iter)->gso_segs = partial_segs;
4321 skb_shinfo(iter)->gso_type = type;
4322 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4325 if (tail->len - doffset <= gso_size)
4326 skb_shinfo(tail)->gso_size = 0;
4327 else if (tail != segs)
4328 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4331 /* Following permits correct backpressure, for protocols
4332 * using skb_set_owner_w().
4333 * Idea is to tranfert ownership from head_skb to last segment.
4335 if (head_skb->destructor == sock_wfree) {
4336 swap(tail->truesize, head_skb->truesize);
4337 swap(tail->destructor, head_skb->destructor);
4338 swap(tail->sk, head_skb->sk);
4343 kfree_skb_list(segs);
4344 return ERR_PTR(err);
4346 EXPORT_SYMBOL_GPL(skb_segment);
4348 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
4350 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
4351 unsigned int offset = skb_gro_offset(skb);
4352 unsigned int headlen = skb_headlen(skb);
4353 unsigned int len = skb_gro_len(skb);
4354 unsigned int delta_truesize;
4355 unsigned int new_truesize;
4358 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
4361 lp = NAPI_GRO_CB(p)->last;
4362 pinfo = skb_shinfo(lp);
4364 if (headlen <= offset) {
4367 int i = skbinfo->nr_frags;
4368 int nr_frags = pinfo->nr_frags + i;
4370 if (nr_frags > MAX_SKB_FRAGS)
4374 pinfo->nr_frags = nr_frags;
4375 skbinfo->nr_frags = 0;
4377 frag = pinfo->frags + nr_frags;
4378 frag2 = skbinfo->frags + i;
4383 skb_frag_off_add(frag, offset);
4384 skb_frag_size_sub(frag, offset);
4386 /* all fragments truesize : remove (head size + sk_buff) */
4387 new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
4388 delta_truesize = skb->truesize - new_truesize;
4390 skb->truesize = new_truesize;
4391 skb->len -= skb->data_len;
4394 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
4396 } else if (skb->head_frag) {
4397 int nr_frags = pinfo->nr_frags;
4398 skb_frag_t *frag = pinfo->frags + nr_frags;
4399 struct page *page = virt_to_head_page(skb->head);
4400 unsigned int first_size = headlen - offset;
4401 unsigned int first_offset;
4403 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
4406 first_offset = skb->data -
4407 (unsigned char *)page_address(page) +
4410 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4412 __skb_frag_set_page(frag, page);
4413 skb_frag_off_set(frag, first_offset);
4414 skb_frag_size_set(frag, first_size);
4416 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4417 /* We dont need to clear skbinfo->nr_frags here */
4419 new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
4420 delta_truesize = skb->truesize - new_truesize;
4421 skb->truesize = new_truesize;
4422 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4427 /* sk owenrship - if any - completely transferred to the aggregated packet */
4428 skb->destructor = NULL;
4429 delta_truesize = skb->truesize;
4430 if (offset > headlen) {
4431 unsigned int eat = offset - headlen;
4433 skb_frag_off_add(&skbinfo->frags[0], eat);
4434 skb_frag_size_sub(&skbinfo->frags[0], eat);
4435 skb->data_len -= eat;
4440 __skb_pull(skb, offset);
4442 if (NAPI_GRO_CB(p)->last == p)
4443 skb_shinfo(p)->frag_list = skb;
4445 NAPI_GRO_CB(p)->last->next = skb;
4446 NAPI_GRO_CB(p)->last = skb;
4447 __skb_header_release(skb);
4451 NAPI_GRO_CB(p)->count++;
4453 p->truesize += delta_truesize;
4456 lp->data_len += len;
4457 lp->truesize += delta_truesize;
4460 NAPI_GRO_CB(skb)->same_flow = 1;
4464 #ifdef CONFIG_SKB_EXTENSIONS
4465 #define SKB_EXT_ALIGN_VALUE 8
4466 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4468 static const u8 skb_ext_type_len[] = {
4469 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4470 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4473 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4475 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4476 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4478 #if IS_ENABLED(CONFIG_MPTCP)
4479 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4483 static __always_inline unsigned int skb_ext_total_length(void)
4485 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4486 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4487 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4490 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4492 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4493 skb_ext_type_len[TC_SKB_EXT] +
4495 #if IS_ENABLED(CONFIG_MPTCP)
4496 skb_ext_type_len[SKB_EXT_MPTCP] +
4501 static void skb_extensions_init(void)
4503 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4504 BUILD_BUG_ON(skb_ext_total_length() > 255);
4506 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4507 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4509 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4513 static void skb_extensions_init(void) {}
4516 void __init skb_init(void)
4518 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
4519 sizeof(struct sk_buff),
4521 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4522 offsetof(struct sk_buff, cb),
4523 sizeof_field(struct sk_buff, cb),
4525 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4526 sizeof(struct sk_buff_fclones),
4528 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4530 skb_extensions_init();
4534 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4535 unsigned int recursion_level)
4537 int start = skb_headlen(skb);
4538 int i, copy = start - offset;
4539 struct sk_buff *frag_iter;
4542 if (unlikely(recursion_level >= 24))
4548 sg_set_buf(sg, skb->data + offset, copy);
4550 if ((len -= copy) == 0)
4555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4558 WARN_ON(start > offset + len);
4560 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4561 if ((copy = end - offset) > 0) {
4562 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4563 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4568 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4569 skb_frag_off(frag) + offset - start);
4578 skb_walk_frags(skb, frag_iter) {
4581 WARN_ON(start > offset + len);
4583 end = start + frag_iter->len;
4584 if ((copy = end - offset) > 0) {
4585 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4590 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4591 copy, recursion_level + 1);
4592 if (unlikely(ret < 0))
4595 if ((len -= copy) == 0)
4606 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4607 * @skb: Socket buffer containing the buffers to be mapped
4608 * @sg: The scatter-gather list to map into
4609 * @offset: The offset into the buffer's contents to start mapping
4610 * @len: Length of buffer space to be mapped
4612 * Fill the specified scatter-gather list with mappings/pointers into a
4613 * region of the buffer space attached to a socket buffer. Returns either
4614 * the number of scatterlist items used, or -EMSGSIZE if the contents
4617 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4619 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4624 sg_mark_end(&sg[nsg - 1]);
4628 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4630 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4631 * sglist without mark the sg which contain last skb data as the end.
4632 * So the caller can mannipulate sg list as will when padding new data after
4633 * the first call without calling sg_unmark_end to expend sg list.
4635 * Scenario to use skb_to_sgvec_nomark:
4637 * 2. skb_to_sgvec_nomark(payload1)
4638 * 3. skb_to_sgvec_nomark(payload2)
4640 * This is equivalent to:
4642 * 2. skb_to_sgvec(payload1)
4644 * 4. skb_to_sgvec(payload2)
4646 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4647 * is more preferable.
4649 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4650 int offset, int len)
4652 return __skb_to_sgvec(skb, sg, offset, len, 0);
4654 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4659 * skb_cow_data - Check that a socket buffer's data buffers are writable
4660 * @skb: The socket buffer to check.
4661 * @tailbits: Amount of trailing space to be added
4662 * @trailer: Returned pointer to the skb where the @tailbits space begins
4664 * Make sure that the data buffers attached to a socket buffer are
4665 * writable. If they are not, private copies are made of the data buffers
4666 * and the socket buffer is set to use these instead.
4668 * If @tailbits is given, make sure that there is space to write @tailbits
4669 * bytes of data beyond current end of socket buffer. @trailer will be
4670 * set to point to the skb in which this space begins.
4672 * The number of scatterlist elements required to completely map the
4673 * COW'd and extended socket buffer will be returned.
4675 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4679 struct sk_buff *skb1, **skb_p;
4681 /* If skb is cloned or its head is paged, reallocate
4682 * head pulling out all the pages (pages are considered not writable
4683 * at the moment even if they are anonymous).
4685 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4686 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4689 /* Easy case. Most of packets will go this way. */
4690 if (!skb_has_frag_list(skb)) {
4691 /* A little of trouble, not enough of space for trailer.
4692 * This should not happen, when stack is tuned to generate
4693 * good frames. OK, on miss we reallocate and reserve even more
4694 * space, 128 bytes is fair. */
4696 if (skb_tailroom(skb) < tailbits &&
4697 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4705 /* Misery. We are in troubles, going to mincer fragments... */
4708 skb_p = &skb_shinfo(skb)->frag_list;
4711 while ((skb1 = *skb_p) != NULL) {
4714 /* The fragment is partially pulled by someone,
4715 * this can happen on input. Copy it and everything
4718 if (skb_shared(skb1))
4721 /* If the skb is the last, worry about trailer. */
4723 if (skb1->next == NULL && tailbits) {
4724 if (skb_shinfo(skb1)->nr_frags ||
4725 skb_has_frag_list(skb1) ||
4726 skb_tailroom(skb1) < tailbits)
4727 ntail = tailbits + 128;
4733 skb_shinfo(skb1)->nr_frags ||
4734 skb_has_frag_list(skb1)) {
4735 struct sk_buff *skb2;
4737 /* Fuck, we are miserable poor guys... */
4739 skb2 = skb_copy(skb1, GFP_ATOMIC);
4741 skb2 = skb_copy_expand(skb1,
4745 if (unlikely(skb2 == NULL))
4749 skb_set_owner_w(skb2, skb1->sk);
4751 /* Looking around. Are we still alive?
4752 * OK, link new skb, drop old one */
4754 skb2->next = skb1->next;
4761 skb_p = &skb1->next;
4766 EXPORT_SYMBOL_GPL(skb_cow_data);
4768 static void sock_rmem_free(struct sk_buff *skb)
4770 struct sock *sk = skb->sk;
4772 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4775 static void skb_set_err_queue(struct sk_buff *skb)
4777 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4778 * So, it is safe to (mis)use it to mark skbs on the error queue.
4780 skb->pkt_type = PACKET_OUTGOING;
4781 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4785 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4787 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4789 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4790 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4795 skb->destructor = sock_rmem_free;
4796 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4797 skb_set_err_queue(skb);
4799 /* before exiting rcu section, make sure dst is refcounted */
4802 skb_queue_tail(&sk->sk_error_queue, skb);
4803 if (!sock_flag(sk, SOCK_DEAD))
4804 sk_error_report(sk);
4807 EXPORT_SYMBOL(sock_queue_err_skb);
4809 static bool is_icmp_err_skb(const struct sk_buff *skb)
4811 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4812 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4815 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4817 struct sk_buff_head *q = &sk->sk_error_queue;
4818 struct sk_buff *skb, *skb_next = NULL;
4819 bool icmp_next = false;
4820 unsigned long flags;
4822 spin_lock_irqsave(&q->lock, flags);
4823 skb = __skb_dequeue(q);
4824 if (skb && (skb_next = skb_peek(q))) {
4825 icmp_next = is_icmp_err_skb(skb_next);
4827 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
4829 spin_unlock_irqrestore(&q->lock, flags);
4831 if (is_icmp_err_skb(skb) && !icmp_next)
4835 sk_error_report(sk);
4839 EXPORT_SYMBOL(sock_dequeue_err_skb);
4842 * skb_clone_sk - create clone of skb, and take reference to socket
4843 * @skb: the skb to clone
4845 * This function creates a clone of a buffer that holds a reference on
4846 * sk_refcnt. Buffers created via this function are meant to be
4847 * returned using sock_queue_err_skb, or free via kfree_skb.
4849 * When passing buffers allocated with this function to sock_queue_err_skb
4850 * it is necessary to wrap the call with sock_hold/sock_put in order to
4851 * prevent the socket from being released prior to being enqueued on
4852 * the sk_error_queue.
4854 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4856 struct sock *sk = skb->sk;
4857 struct sk_buff *clone;
4859 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4862 clone = skb_clone(skb, GFP_ATOMIC);
4869 clone->destructor = sock_efree;
4873 EXPORT_SYMBOL(skb_clone_sk);
4875 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4880 struct sock_exterr_skb *serr;
4883 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4885 serr = SKB_EXT_ERR(skb);
4886 memset(serr, 0, sizeof(*serr));
4887 serr->ee.ee_errno = ENOMSG;
4888 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4889 serr->ee.ee_info = tstype;
4890 serr->opt_stats = opt_stats;
4891 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4892 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4893 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4894 if (sk->sk_protocol == IPPROTO_TCP &&
4895 sk->sk_type == SOCK_STREAM)
4896 serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
4899 err = sock_queue_err_skb(sk, skb);
4905 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4909 if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
4912 read_lock_bh(&sk->sk_callback_lock);
4913 ret = sk->sk_socket && sk->sk_socket->file &&
4914 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4915 read_unlock_bh(&sk->sk_callback_lock);
4919 void skb_complete_tx_timestamp(struct sk_buff *skb,
4920 struct skb_shared_hwtstamps *hwtstamps)
4922 struct sock *sk = skb->sk;
4924 if (!skb_may_tx_timestamp(sk, false))
4927 /* Take a reference to prevent skb_orphan() from freeing the socket,
4928 * but only if the socket refcount is not zero.
4930 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4931 *skb_hwtstamps(skb) = *hwtstamps;
4932 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4940 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4942 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4943 const struct sk_buff *ack_skb,
4944 struct skb_shared_hwtstamps *hwtstamps,
4945 struct sock *sk, int tstype)
4947 struct sk_buff *skb;
4948 bool tsonly, opt_stats = false;
4953 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4954 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4957 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4958 if (!skb_may_tx_timestamp(sk, tsonly))
4963 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4964 sk->sk_protocol == IPPROTO_TCP &&
4965 sk->sk_type == SOCK_STREAM) {
4966 skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
4971 skb = alloc_skb(0, GFP_ATOMIC);
4973 skb = skb_clone(orig_skb, GFP_ATOMIC);
4975 if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) {
4984 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4986 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4990 *skb_hwtstamps(skb) = *hwtstamps;
4992 skb->tstamp = ktime_get_real();
4994 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4996 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4998 void skb_tstamp_tx(struct sk_buff *orig_skb,
4999 struct skb_shared_hwtstamps *hwtstamps)
5001 return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
5004 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
5006 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
5008 struct sock *sk = skb->sk;
5009 struct sock_exterr_skb *serr;
5012 skb->wifi_acked_valid = 1;
5013 skb->wifi_acked = acked;
5015 serr = SKB_EXT_ERR(skb);
5016 memset(serr, 0, sizeof(*serr));
5017 serr->ee.ee_errno = ENOMSG;
5018 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
5020 /* Take a reference to prevent skb_orphan() from freeing the socket,
5021 * but only if the socket refcount is not zero.
5023 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5024 err = sock_queue_err_skb(sk, skb);
5030 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5033 * skb_partial_csum_set - set up and verify partial csum values for packet
5034 * @skb: the skb to set
5035 * @start: the number of bytes after skb->data to start checksumming.
5036 * @off: the offset from start to place the checksum.
5038 * For untrusted partially-checksummed packets, we need to make sure the values
5039 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5041 * This function checks and sets those values and skb->ip_summed: if this
5042 * returns false you should drop the packet.
5044 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5046 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
5047 u32 csum_start = skb_headroom(skb) + (u32)start;
5049 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
5050 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
5051 start, off, skb_headroom(skb), skb_headlen(skb));
5054 skb->ip_summed = CHECKSUM_PARTIAL;
5055 skb->csum_start = csum_start;
5056 skb->csum_offset = off;
5057 skb_set_transport_header(skb, start);
5060 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5062 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5065 if (skb_headlen(skb) >= len)
5068 /* If we need to pullup then pullup to the max, so we
5069 * won't need to do it again.
5074 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5077 if (skb_headlen(skb) < len)
5083 #define MAX_TCP_HDR_LEN (15 * 4)
5085 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5086 typeof(IPPROTO_IP) proto,
5093 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5094 off + MAX_TCP_HDR_LEN);
5095 if (!err && !skb_partial_csum_set(skb, off,
5096 offsetof(struct tcphdr,
5099 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5102 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5103 off + sizeof(struct udphdr));
5104 if (!err && !skb_partial_csum_set(skb, off,
5105 offsetof(struct udphdr,
5108 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5111 return ERR_PTR(-EPROTO);
5114 /* This value should be large enough to cover a tagged ethernet header plus
5115 * maximally sized IP and TCP or UDP headers.
5117 #define MAX_IP_HDR_LEN 128
5119 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5128 err = skb_maybe_pull_tail(skb,
5129 sizeof(struct iphdr),
5134 if (ip_is_fragment(ip_hdr(skb)))
5137 off = ip_hdrlen(skb);
5144 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5146 return PTR_ERR(csum);
5149 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5152 ip_hdr(skb)->protocol, 0);
5159 /* This value should be large enough to cover a tagged ethernet header plus
5160 * an IPv6 header, all options, and a maximal TCP or UDP header.
5162 #define MAX_IPV6_HDR_LEN 256
5164 #define OPT_HDR(type, skb, off) \
5165 (type *)(skb_network_header(skb) + (off))
5167 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5180 off = sizeof(struct ipv6hdr);
5182 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5186 nexthdr = ipv6_hdr(skb)->nexthdr;
5188 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5189 while (off <= len && !done) {
5191 case IPPROTO_DSTOPTS:
5192 case IPPROTO_HOPOPTS:
5193 case IPPROTO_ROUTING: {
5194 struct ipv6_opt_hdr *hp;
5196 err = skb_maybe_pull_tail(skb,
5198 sizeof(struct ipv6_opt_hdr),
5203 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5204 nexthdr = hp->nexthdr;
5205 off += ipv6_optlen(hp);
5209 struct ip_auth_hdr *hp;
5211 err = skb_maybe_pull_tail(skb,
5213 sizeof(struct ip_auth_hdr),
5218 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5219 nexthdr = hp->nexthdr;
5220 off += ipv6_authlen(hp);
5223 case IPPROTO_FRAGMENT: {
5224 struct frag_hdr *hp;
5226 err = skb_maybe_pull_tail(skb,
5228 sizeof(struct frag_hdr),
5233 hp = OPT_HDR(struct frag_hdr, skb, off);
5235 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5238 nexthdr = hp->nexthdr;
5239 off += sizeof(struct frag_hdr);
5250 if (!done || fragment)
5253 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5255 return PTR_ERR(csum);
5258 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5259 &ipv6_hdr(skb)->daddr,
5260 skb->len - off, nexthdr, 0);
5268 * skb_checksum_setup - set up partial checksum offset
5269 * @skb: the skb to set up
5270 * @recalculate: if true the pseudo-header checksum will be recalculated
5272 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5276 switch (skb->protocol) {
5277 case htons(ETH_P_IP):
5278 err = skb_checksum_setup_ipv4(skb, recalculate);
5281 case htons(ETH_P_IPV6):
5282 err = skb_checksum_setup_ipv6(skb, recalculate);
5292 EXPORT_SYMBOL(skb_checksum_setup);
5295 * skb_checksum_maybe_trim - maybe trims the given skb
5296 * @skb: the skb to check
5297 * @transport_len: the data length beyond the network header
5299 * Checks whether the given skb has data beyond the given transport length.
5300 * If so, returns a cloned skb trimmed to this transport length.
5301 * Otherwise returns the provided skb. Returns NULL in error cases
5302 * (e.g. transport_len exceeds skb length or out-of-memory).
5304 * Caller needs to set the skb transport header and free any returned skb if it
5305 * differs from the provided skb.
5307 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5308 unsigned int transport_len)
5310 struct sk_buff *skb_chk;
5311 unsigned int len = skb_transport_offset(skb) + transport_len;
5316 else if (skb->len == len)
5319 skb_chk = skb_clone(skb, GFP_ATOMIC);
5323 ret = pskb_trim_rcsum(skb_chk, len);
5333 * skb_checksum_trimmed - validate checksum of an skb
5334 * @skb: the skb to check
5335 * @transport_len: the data length beyond the network header
5336 * @skb_chkf: checksum function to use
5338 * Applies the given checksum function skb_chkf to the provided skb.
5339 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5341 * If the skb has data beyond the given transport length, then a
5342 * trimmed & cloned skb is checked and returned.
5344 * Caller needs to set the skb transport header and free any returned skb if it
5345 * differs from the provided skb.
5347 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5348 unsigned int transport_len,
5349 __sum16(*skb_chkf)(struct sk_buff *skb))
5351 struct sk_buff *skb_chk;
5352 unsigned int offset = skb_transport_offset(skb);
5355 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5359 if (!pskb_may_pull(skb_chk, offset))
5362 skb_pull_rcsum(skb_chk, offset);
5363 ret = skb_chkf(skb_chk);
5364 skb_push_rcsum(skb_chk, offset);
5372 if (skb_chk && skb_chk != skb)
5378 EXPORT_SYMBOL(skb_checksum_trimmed);
5380 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5382 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5385 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5387 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5390 skb_release_head_state(skb);
5391 kmem_cache_free(skbuff_head_cache, skb);
5396 EXPORT_SYMBOL(kfree_skb_partial);
5399 * skb_try_coalesce - try to merge skb to prior one
5401 * @from: buffer to add
5402 * @fragstolen: pointer to boolean
5403 * @delta_truesize: how much more was allocated than was requested
5405 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5406 bool *fragstolen, int *delta_truesize)
5408 struct skb_shared_info *to_shinfo, *from_shinfo;
5409 int i, delta, len = from->len;
5411 *fragstolen = false;
5416 /* In general, avoid mixing page_pool and non-page_pool allocated
5417 * pages within the same SKB. Additionally avoid dealing with clones
5418 * with page_pool pages, in case the SKB is using page_pool fragment
5419 * references (PP_FLAG_PAGE_FRAG). Since we only take full page
5420 * references for cloned SKBs at the moment that would result in
5421 * inconsistent reference counts.
5422 * In theory we could take full references if @from is cloned and
5423 * !@to->pp_recycle but its tricky (due to potential race with
5424 * the clone disappearing) and rare, so not worth dealing with.
5426 if (to->pp_recycle != from->pp_recycle ||
5427 (from->pp_recycle && skb_cloned(from)))
5430 if (len <= skb_tailroom(to)) {
5432 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5433 *delta_truesize = 0;
5437 to_shinfo = skb_shinfo(to);
5438 from_shinfo = skb_shinfo(from);
5439 if (to_shinfo->frag_list || from_shinfo->frag_list)
5441 if (skb_zcopy(to) || skb_zcopy(from))
5444 if (skb_headlen(from) != 0) {
5446 unsigned int offset;
5448 if (to_shinfo->nr_frags +
5449 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5452 if (skb_head_is_locked(from))
5455 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5457 page = virt_to_head_page(from->head);
5458 offset = from->data - (unsigned char *)page_address(page);
5460 skb_fill_page_desc(to, to_shinfo->nr_frags,
5461 page, offset, skb_headlen(from));
5464 if (to_shinfo->nr_frags +
5465 from_shinfo->nr_frags > MAX_SKB_FRAGS)
5468 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5471 WARN_ON_ONCE(delta < len);
5473 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5475 from_shinfo->nr_frags * sizeof(skb_frag_t));
5476 to_shinfo->nr_frags += from_shinfo->nr_frags;
5478 if (!skb_cloned(from))
5479 from_shinfo->nr_frags = 0;
5481 /* if the skb is not cloned this does nothing
5482 * since we set nr_frags to 0.
5484 for (i = 0; i < from_shinfo->nr_frags; i++)
5485 __skb_frag_ref(&from_shinfo->frags[i]);
5487 to->truesize += delta;
5489 to->data_len += len;
5491 *delta_truesize = delta;
5494 EXPORT_SYMBOL(skb_try_coalesce);
5497 * skb_scrub_packet - scrub an skb
5499 * @skb: buffer to clean
5500 * @xnet: packet is crossing netns
5502 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5503 * into/from a tunnel. Some information have to be cleared during these
5505 * skb_scrub_packet can also be used to clean a skb before injecting it in
5506 * another namespace (@xnet == true). We have to clear all information in the
5507 * skb that could impact namespace isolation.
5509 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5511 skb->pkt_type = PACKET_HOST;
5517 nf_reset_trace(skb);
5519 #ifdef CONFIG_NET_SWITCHDEV
5520 skb->offload_fwd_mark = 0;
5521 skb->offload_l3_fwd_mark = 0;
5531 EXPORT_SYMBOL_GPL(skb_scrub_packet);
5534 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5538 * skb_gso_transport_seglen is used to determine the real size of the
5539 * individual segments, including Layer4 headers (TCP/UDP).
5541 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5543 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5545 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5546 unsigned int thlen = 0;
5548 if (skb->encapsulation) {
5549 thlen = skb_inner_transport_header(skb) -
5550 skb_transport_header(skb);
5552 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5553 thlen += inner_tcp_hdrlen(skb);
5554 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5555 thlen = tcp_hdrlen(skb);
5556 } else if (unlikely(skb_is_gso_sctp(skb))) {
5557 thlen = sizeof(struct sctphdr);
5558 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5559 thlen = sizeof(struct udphdr);
5561 /* UFO sets gso_size to the size of the fragmentation
5562 * payload, i.e. the size of the L4 (UDP) header is already
5565 return thlen + shinfo->gso_size;
5569 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5573 * skb_gso_network_seglen is used to determine the real size of the
5574 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5576 * The MAC/L2 header is not accounted for.
5578 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5580 unsigned int hdr_len = skb_transport_header(skb) -
5581 skb_network_header(skb);
5583 return hdr_len + skb_gso_transport_seglen(skb);
5587 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5591 * skb_gso_mac_seglen is used to determine the real size of the
5592 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5593 * headers (TCP/UDP).
5595 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5597 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5599 return hdr_len + skb_gso_transport_seglen(skb);
5603 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5605 * There are a couple of instances where we have a GSO skb, and we
5606 * want to determine what size it would be after it is segmented.
5608 * We might want to check:
5609 * - L3+L4+payload size (e.g. IP forwarding)
5610 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5612 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5616 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5617 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5619 * @max_len: The maximum permissible length.
5621 * Returns true if the segmented length <= max length.
5623 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5624 unsigned int seg_len,
5625 unsigned int max_len) {
5626 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5627 const struct sk_buff *iter;
5629 if (shinfo->gso_size != GSO_BY_FRAGS)
5630 return seg_len <= max_len;
5632 /* Undo this so we can re-use header sizes */
5633 seg_len -= GSO_BY_FRAGS;
5635 skb_walk_frags(skb, iter) {
5636 if (seg_len + skb_headlen(iter) > max_len)
5644 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5647 * @mtu: MTU to validate against
5649 * skb_gso_validate_network_len validates if a given skb will fit a
5650 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5653 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5655 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5657 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5660 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5663 * @len: length to validate against
5665 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5666 * length once split, including L2, L3 and L4 headers and the payload.
5668 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5670 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5672 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5674 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5676 int mac_len, meta_len;
5679 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5684 mac_len = skb->data - skb_mac_header(skb);
5685 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5686 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5687 mac_len - VLAN_HLEN - ETH_TLEN);
5690 meta_len = skb_metadata_len(skb);
5692 meta = skb_metadata_end(skb) - meta_len;
5693 memmove(meta + VLAN_HLEN, meta, meta_len);
5696 skb->mac_header += VLAN_HLEN;
5700 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5702 struct vlan_hdr *vhdr;
5705 if (unlikely(skb_vlan_tag_present(skb))) {
5706 /* vlan_tci is already set-up so leave this for another time */
5710 skb = skb_share_check(skb, GFP_ATOMIC);
5713 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5714 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
5717 vhdr = (struct vlan_hdr *)skb->data;
5718 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5719 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5721 skb_pull_rcsum(skb, VLAN_HLEN);
5722 vlan_set_encap_proto(skb, vhdr);
5724 skb = skb_reorder_vlan_header(skb);
5728 skb_reset_network_header(skb);
5729 if (!skb_transport_header_was_set(skb))
5730 skb_reset_transport_header(skb);
5731 skb_reset_mac_len(skb);
5739 EXPORT_SYMBOL(skb_vlan_untag);
5741 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5743 if (!pskb_may_pull(skb, write_len))
5746 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5749 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5751 EXPORT_SYMBOL(skb_ensure_writable);
5753 /* remove VLAN header from packet and update csum accordingly.
5754 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5756 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5758 struct vlan_hdr *vhdr;
5759 int offset = skb->data - skb_mac_header(skb);
5762 if (WARN_ONCE(offset,
5763 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5768 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5772 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5774 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5775 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5777 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5778 __skb_pull(skb, VLAN_HLEN);
5780 vlan_set_encap_proto(skb, vhdr);
5781 skb->mac_header += VLAN_HLEN;
5783 if (skb_network_offset(skb) < ETH_HLEN)
5784 skb_set_network_header(skb, ETH_HLEN);
5786 skb_reset_mac_len(skb);
5790 EXPORT_SYMBOL(__skb_vlan_pop);
5792 /* Pop a vlan tag either from hwaccel or from payload.
5793 * Expects skb->data at mac header.
5795 int skb_vlan_pop(struct sk_buff *skb)
5801 if (likely(skb_vlan_tag_present(skb))) {
5802 __vlan_hwaccel_clear_tag(skb);
5804 if (unlikely(!eth_type_vlan(skb->protocol)))
5807 err = __skb_vlan_pop(skb, &vlan_tci);
5811 /* move next vlan tag to hw accel tag */
5812 if (likely(!eth_type_vlan(skb->protocol)))
5815 vlan_proto = skb->protocol;
5816 err = __skb_vlan_pop(skb, &vlan_tci);
5820 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5823 EXPORT_SYMBOL(skb_vlan_pop);
5825 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5826 * Expects skb->data at mac header.
5828 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5830 if (skb_vlan_tag_present(skb)) {
5831 int offset = skb->data - skb_mac_header(skb);
5834 if (WARN_ONCE(offset,
5835 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5840 err = __vlan_insert_tag(skb, skb->vlan_proto,
5841 skb_vlan_tag_get(skb));
5845 skb->protocol = skb->vlan_proto;
5846 skb->mac_len += VLAN_HLEN;
5848 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5850 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5853 EXPORT_SYMBOL(skb_vlan_push);
5856 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5858 * @skb: Socket buffer to modify
5860 * Drop the Ethernet header of @skb.
5862 * Expects that skb->data points to the mac header and that no VLAN tags are
5865 * Returns 0 on success, -errno otherwise.
5867 int skb_eth_pop(struct sk_buff *skb)
5869 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5870 skb_network_offset(skb) < ETH_HLEN)
5873 skb_pull_rcsum(skb, ETH_HLEN);
5874 skb_reset_mac_header(skb);
5875 skb_reset_mac_len(skb);
5879 EXPORT_SYMBOL(skb_eth_pop);
5882 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5884 * @skb: Socket buffer to modify
5885 * @dst: Destination MAC address of the new header
5886 * @src: Source MAC address of the new header
5888 * Prepend @skb with a new Ethernet header.
5890 * Expects that skb->data points to the mac header, which must be empty.
5892 * Returns 0 on success, -errno otherwise.
5894 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5895 const unsigned char *src)
5900 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5903 err = skb_cow_head(skb, sizeof(*eth));
5907 skb_push(skb, sizeof(*eth));
5908 skb_reset_mac_header(skb);
5909 skb_reset_mac_len(skb);
5912 ether_addr_copy(eth->h_dest, dst);
5913 ether_addr_copy(eth->h_source, src);
5914 eth->h_proto = skb->protocol;
5916 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5920 EXPORT_SYMBOL(skb_eth_push);
5922 /* Update the ethertype of hdr and the skb csum value if required. */
5923 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5926 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5927 __be16 diff[] = { ~hdr->h_proto, ethertype };
5929 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5932 hdr->h_proto = ethertype;
5936 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5940 * @mpls_lse: MPLS label stack entry to push
5941 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
5942 * @mac_len: length of the MAC header
5943 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5946 * Expects skb->data at mac header.
5948 * Returns 0 on success, -errno otherwise.
5950 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
5951 int mac_len, bool ethernet)
5953 struct mpls_shim_hdr *lse;
5956 if (unlikely(!eth_p_mpls(mpls_proto)))
5959 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5960 if (skb->encapsulation)
5963 err = skb_cow_head(skb, MPLS_HLEN);
5967 if (!skb->inner_protocol) {
5968 skb_set_inner_network_header(skb, skb_network_offset(skb));
5969 skb_set_inner_protocol(skb, skb->protocol);
5972 skb_push(skb, MPLS_HLEN);
5973 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
5975 skb_reset_mac_header(skb);
5976 skb_set_network_header(skb, mac_len);
5977 skb_reset_mac_len(skb);
5979 lse = mpls_hdr(skb);
5980 lse->label_stack_entry = mpls_lse;
5981 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5983 if (ethernet && mac_len >= ETH_HLEN)
5984 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5985 skb->protocol = mpls_proto;
5989 EXPORT_SYMBOL_GPL(skb_mpls_push);
5992 * skb_mpls_pop() - pop the outermost MPLS header
5995 * @next_proto: ethertype of header after popped MPLS header
5996 * @mac_len: length of the MAC header
5997 * @ethernet: flag to indicate if the packet is ethernet
5999 * Expects skb->data at mac header.
6001 * Returns 0 on success, -errno otherwise.
6003 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
6008 if (unlikely(!eth_p_mpls(skb->protocol)))
6011 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
6015 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
6016 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
6019 __skb_pull(skb, MPLS_HLEN);
6020 skb_reset_mac_header(skb);
6021 skb_set_network_header(skb, mac_len);
6023 if (ethernet && mac_len >= ETH_HLEN) {
6026 /* use mpls_hdr() to get ethertype to account for VLANs. */
6027 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6028 skb_mod_eth_type(skb, hdr, next_proto);
6030 skb->protocol = next_proto;
6034 EXPORT_SYMBOL_GPL(skb_mpls_pop);
6037 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6040 * @mpls_lse: new MPLS label stack entry to update to
6042 * Expects skb->data at mac header.
6044 * Returns 0 on success, -errno otherwise.
6046 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6050 if (unlikely(!eth_p_mpls(skb->protocol)))
6053 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6057 if (skb->ip_summed == CHECKSUM_COMPLETE) {
6058 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6060 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6063 mpls_hdr(skb)->label_stack_entry = mpls_lse;
6067 EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6070 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
6074 * Expects skb->data at mac header.
6076 * Returns 0 on success, -errno otherwise.
6078 int skb_mpls_dec_ttl(struct sk_buff *skb)
6083 if (unlikely(!eth_p_mpls(skb->protocol)))
6086 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
6089 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
6090 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
6094 lse &= ~MPLS_LS_TTL_MASK;
6095 lse |= ttl << MPLS_LS_TTL_SHIFT;
6097 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
6099 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
6102 * alloc_skb_with_frags - allocate skb with page frags
6104 * @header_len: size of linear part
6105 * @data_len: needed length in frags
6106 * @max_page_order: max page order desired.
6107 * @errcode: pointer to error code if any
6108 * @gfp_mask: allocation mask
6110 * This can be used to allocate a paged skb, given a maximal order for frags.
6112 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6113 unsigned long data_len,
6118 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
6119 unsigned long chunk;
6120 struct sk_buff *skb;
6124 *errcode = -EMSGSIZE;
6125 /* Note this test could be relaxed, if we succeed to allocate
6126 * high order pages...
6128 if (npages > MAX_SKB_FRAGS)
6131 *errcode = -ENOBUFS;
6132 skb = alloc_skb(header_len, gfp_mask);
6136 skb->truesize += npages << PAGE_SHIFT;
6138 for (i = 0; npages > 0; i++) {
6139 int order = max_page_order;
6142 if (npages >= 1 << order) {
6143 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
6149 /* Do not retry other high order allocations */
6155 page = alloc_page(gfp_mask);
6159 chunk = min_t(unsigned long, data_len,
6160 PAGE_SIZE << order);
6161 skb_fill_page_desc(skb, i, page, 0, chunk);
6163 npages -= 1 << order;
6171 EXPORT_SYMBOL(alloc_skb_with_frags);
6173 /* carve out the first off bytes from skb when off < headlen */
6174 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6175 const int headlen, gfp_t gfp_mask)
6178 int size = skb_end_offset(skb);
6179 int new_hlen = headlen - off;
6182 size = SKB_DATA_ALIGN(size);
6184 if (skb_pfmemalloc(skb))
6185 gfp_mask |= __GFP_MEMALLOC;
6186 data = kmalloc_reserve(size +
6187 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6188 gfp_mask, NUMA_NO_NODE, NULL);
6192 size = SKB_WITH_OVERHEAD(ksize(data));
6194 /* Copy real data, and all frags */
6195 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6198 memcpy((struct skb_shared_info *)(data + size),
6200 offsetof(struct skb_shared_info,
6201 frags[skb_shinfo(skb)->nr_frags]));
6202 if (skb_cloned(skb)) {
6203 /* drop the old head gracefully */
6204 if (skb_orphan_frags(skb, gfp_mask)) {
6208 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6209 skb_frag_ref(skb, i);
6210 if (skb_has_frag_list(skb))
6211 skb_clone_fraglist(skb);
6212 skb_release_data(skb);
6214 /* we can reuse existing recount- all we did was
6223 skb_set_end_offset(skb, size);
6224 skb_set_tail_pointer(skb, skb_headlen(skb));
6225 skb_headers_offset_update(skb, 0);
6229 atomic_set(&skb_shinfo(skb)->dataref, 1);
6234 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6236 /* carve out the first eat bytes from skb's frag_list. May recurse into
6239 static int pskb_carve_frag_list(struct sk_buff *skb,
6240 struct skb_shared_info *shinfo, int eat,
6243 struct sk_buff *list = shinfo->frag_list;
6244 struct sk_buff *clone = NULL;
6245 struct sk_buff *insp = NULL;
6249 pr_err("Not enough bytes to eat. Want %d\n", eat);
6252 if (list->len <= eat) {
6253 /* Eaten as whole. */
6258 /* Eaten partially. */
6259 if (skb_shared(list)) {
6260 clone = skb_clone(list, gfp_mask);
6266 /* This may be pulled without problems. */
6269 if (pskb_carve(list, eat, gfp_mask) < 0) {
6277 /* Free pulled out fragments. */
6278 while ((list = shinfo->frag_list) != insp) {
6279 shinfo->frag_list = list->next;
6282 /* And insert new clone at head. */
6285 shinfo->frag_list = clone;
6290 /* carve off first len bytes from skb. Split line (off) is in the
6291 * non-linear part of skb
6293 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6294 int pos, gfp_t gfp_mask)
6297 int size = skb_end_offset(skb);
6299 const int nfrags = skb_shinfo(skb)->nr_frags;
6300 struct skb_shared_info *shinfo;
6302 size = SKB_DATA_ALIGN(size);
6304 if (skb_pfmemalloc(skb))
6305 gfp_mask |= __GFP_MEMALLOC;
6306 data = kmalloc_reserve(size +
6307 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6308 gfp_mask, NUMA_NO_NODE, NULL);
6312 size = SKB_WITH_OVERHEAD(ksize(data));
6314 memcpy((struct skb_shared_info *)(data + size),
6315 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
6316 if (skb_orphan_frags(skb, gfp_mask)) {
6320 shinfo = (struct skb_shared_info *)(data + size);
6321 for (i = 0; i < nfrags; i++) {
6322 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6324 if (pos + fsize > off) {
6325 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6329 * We have two variants in this case:
6330 * 1. Move all the frag to the second
6331 * part, if it is possible. F.e.
6332 * this approach is mandatory for TUX,
6333 * where splitting is expensive.
6334 * 2. Split is accurately. We make this.
6336 skb_frag_off_add(&shinfo->frags[0], off - pos);
6337 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6339 skb_frag_ref(skb, i);
6344 shinfo->nr_frags = k;
6345 if (skb_has_frag_list(skb))
6346 skb_clone_fraglist(skb);
6348 /* split line is in frag list */
6349 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6350 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6351 if (skb_has_frag_list(skb))
6352 kfree_skb_list(skb_shinfo(skb)->frag_list);
6356 skb_release_data(skb);
6361 skb_set_end_offset(skb, size);
6362 skb_reset_tail_pointer(skb);
6363 skb_headers_offset_update(skb, 0);
6368 skb->data_len = skb->len;
6369 atomic_set(&skb_shinfo(skb)->dataref, 1);
6373 /* remove len bytes from the beginning of the skb */
6374 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6376 int headlen = skb_headlen(skb);
6379 return pskb_carve_inside_header(skb, len, headlen, gfp);
6381 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6384 /* Extract to_copy bytes starting at off from skb, and return this in
6387 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6388 int to_copy, gfp_t gfp)
6390 struct sk_buff *clone = skb_clone(skb, gfp);
6395 if (pskb_carve(clone, off, gfp) < 0 ||
6396 pskb_trim(clone, to_copy)) {
6402 EXPORT_SYMBOL(pskb_extract);
6405 * skb_condense - try to get rid of fragments/frag_list if possible
6408 * Can be used to save memory before skb is added to a busy queue.
6409 * If packet has bytes in frags and enough tail room in skb->head,
6410 * pull all of them, so that we can free the frags right now and adjust
6413 * We do not reallocate skb->head thus can not fail.
6414 * Caller must re-evaluate skb->truesize if needed.
6416 void skb_condense(struct sk_buff *skb)
6418 if (skb->data_len) {
6419 if (skb->data_len > skb->end - skb->tail ||
6423 /* Nice, we can free page frag(s) right now */
6424 __pskb_pull_tail(skb, skb->data_len);
6426 /* At this point, skb->truesize might be over estimated,
6427 * because skb had a fragment, and fragments do not tell
6429 * When we pulled its content into skb->head, fragment
6430 * was freed, but __pskb_pull_tail() could not possibly
6431 * adjust skb->truesize, not knowing the frag truesize.
6433 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6436 #ifdef CONFIG_SKB_EXTENSIONS
6437 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6439 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6443 * __skb_ext_alloc - allocate a new skb extensions storage
6445 * @flags: See kmalloc().
6447 * Returns the newly allocated pointer. The pointer can later attached to a
6448 * skb via __skb_ext_set().
6449 * Note: caller must handle the skb_ext as an opaque data.
6451 struct skb_ext *__skb_ext_alloc(gfp_t flags)
6453 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6456 memset(new->offset, 0, sizeof(new->offset));
6457 refcount_set(&new->refcnt, 1);
6463 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6464 unsigned int old_active)
6466 struct skb_ext *new;
6468 if (refcount_read(&old->refcnt) == 1)
6471 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6475 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6476 refcount_set(&new->refcnt, 1);
6479 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6480 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6483 for (i = 0; i < sp->len; i++)
6484 xfrm_state_hold(sp->xvec[i]);
6492 * __skb_ext_set - attach the specified extension storage to this skb
6495 * @ext: extension storage previously allocated via __skb_ext_alloc()
6497 * Existing extensions, if any, are cleared.
6499 * Returns the pointer to the extension.
6501 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6502 struct skb_ext *ext)
6504 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6507 newlen = newoff + skb_ext_type_len[id];
6508 ext->chunks = newlen;
6509 ext->offset[id] = newoff;
6510 skb->extensions = ext;
6511 skb->active_extensions = 1 << id;
6512 return skb_ext_get_ptr(ext, id);
6516 * skb_ext_add - allocate space for given extension, COW if needed
6518 * @id: extension to allocate space for
6520 * Allocates enough space for the given extension.
6521 * If the extension is already present, a pointer to that extension
6524 * If the skb was cloned, COW applies and the returned memory can be
6525 * modified without changing the extension space of clones buffers.
6527 * Returns pointer to the extension or NULL on allocation failure.
6529 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6531 struct skb_ext *new, *old = NULL;
6532 unsigned int newlen, newoff;
6534 if (skb->active_extensions) {
6535 old = skb->extensions;
6537 new = skb_ext_maybe_cow(old, skb->active_extensions);
6541 if (__skb_ext_exist(new, id))
6544 newoff = new->chunks;
6546 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6548 new = __skb_ext_alloc(GFP_ATOMIC);
6553 newlen = newoff + skb_ext_type_len[id];
6554 new->chunks = newlen;
6555 new->offset[id] = newoff;
6558 skb->extensions = new;
6559 skb->active_extensions |= 1 << id;
6560 return skb_ext_get_ptr(new, id);
6562 EXPORT_SYMBOL(skb_ext_add);
6565 static void skb_ext_put_sp(struct sec_path *sp)
6569 for (i = 0; i < sp->len; i++)
6570 xfrm_state_put(sp->xvec[i]);
6574 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6576 struct skb_ext *ext = skb->extensions;
6578 skb->active_extensions &= ~(1 << id);
6579 if (skb->active_extensions == 0) {
6580 skb->extensions = NULL;
6583 } else if (id == SKB_EXT_SEC_PATH &&
6584 refcount_read(&ext->refcnt) == 1) {
6585 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6592 EXPORT_SYMBOL(__skb_ext_del);
6594 void __skb_ext_put(struct skb_ext *ext)
6596 /* If this is last clone, nothing can increment
6597 * it after check passes. Avoids one atomic op.
6599 if (refcount_read(&ext->refcnt) == 1)
6602 if (!refcount_dec_and_test(&ext->refcnt))
6606 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6607 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6610 kmem_cache_free(skbuff_ext_cache, ext);
6612 EXPORT_SYMBOL(__skb_ext_put);
6613 #endif /* CONFIG_SKB_EXTENSIONS */