2 * Routines having to do with the 'struct sk_buff' memory handlers.
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/tcp.h>
50 #include <linux/udp.h>
51 #include <linux/sctp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <linux/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __ro_after_init;
81 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
82 #ifdef CONFIG_SKB_EXTENSIONS
83 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
85 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
86 EXPORT_SYMBOL(sysctl_max_skb_frags);
89 * skb_panic - private function for out-of-line support
93 * @msg: skb_over_panic or skb_under_panic
95 * Out-of-line support for skb_put() and skb_push().
96 * Called via the wrapper skb_over_panic() or skb_under_panic().
97 * Keep out of line to prevent kernel bloat.
98 * __builtin_return_address is not used because it is not always reliable.
100 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
103 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
104 msg, addr, skb->len, sz, skb->head, skb->data,
105 (unsigned long)skb->tail, (unsigned long)skb->end,
106 skb->dev ? skb->dev->name : "<NULL>");
110 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
112 skb_panic(skb, sz, addr, __func__);
115 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
117 skb_panic(skb, sz, addr, __func__);
121 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
122 * the caller if emergency pfmemalloc reserves are being used. If it is and
123 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
124 * may be used. Otherwise, the packet data may be discarded until enough
127 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
128 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
130 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
131 unsigned long ip, bool *pfmemalloc)
134 bool ret_pfmemalloc = false;
137 * Try a regular allocation, when that fails and we're not entitled
138 * to the reserves, fail.
140 obj = kmalloc_node_track_caller(size,
141 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
143 if (obj || !(gfp_pfmemalloc_allowed(flags)))
146 /* Try again but now we are using pfmemalloc reserves */
147 ret_pfmemalloc = true;
148 obj = kmalloc_node_track_caller(size, flags, node);
152 *pfmemalloc = ret_pfmemalloc;
157 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
158 * 'private' fields and also do memory statistics to find all the
164 * __alloc_skb - allocate a network buffer
165 * @size: size to allocate
166 * @gfp_mask: allocation mask
167 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
168 * instead of head cache and allocate a cloned (child) skb.
169 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
170 * allocations in case the data is required for writeback
171 * @node: numa node to allocate memory on
173 * Allocate a new &sk_buff. The returned buffer has no headroom and a
174 * tail room of at least size bytes. The object has a reference count
175 * of one. The return is the buffer. On a failure the return is %NULL.
177 * Buffers may only be allocated from interrupts using a @gfp_mask of
180 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
183 struct kmem_cache *cache;
184 struct skb_shared_info *shinfo;
189 cache = (flags & SKB_ALLOC_FCLONE)
190 ? skbuff_fclone_cache : skbuff_head_cache;
192 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
193 gfp_mask |= __GFP_MEMALLOC;
196 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
201 /* We do our best to align skb_shared_info on a separate cache
202 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
203 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
204 * Both skb->head and skb_shared_info are cache line aligned.
206 size = SKB_DATA_ALIGN(size);
207 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
208 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
211 /* kmalloc(size) might give us more room than requested.
212 * Put skb_shared_info exactly at the end of allocated zone,
213 * to allow max possible filling before reallocation.
215 size = SKB_WITH_OVERHEAD(ksize(data));
216 prefetchw(data + size);
219 * Only clear those fields we need to clear, not those that we will
220 * actually initialise below. Hence, don't put any more fields after
221 * the tail pointer in struct sk_buff!
223 memset(skb, 0, offsetof(struct sk_buff, tail));
224 /* Account for allocated memory : skb + skb->head */
225 skb->truesize = SKB_TRUESIZE(size);
226 skb->pfmemalloc = pfmemalloc;
227 refcount_set(&skb->users, 1);
230 skb_reset_tail_pointer(skb);
231 skb->end = skb->tail + size;
232 skb->mac_header = (typeof(skb->mac_header))~0U;
233 skb->transport_header = (typeof(skb->transport_header))~0U;
235 /* make sure we initialize shinfo sequentially */
236 shinfo = skb_shinfo(skb);
237 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
238 atomic_set(&shinfo->dataref, 1);
240 if (flags & SKB_ALLOC_FCLONE) {
241 struct sk_buff_fclones *fclones;
243 fclones = container_of(skb, struct sk_buff_fclones, skb1);
245 skb->fclone = SKB_FCLONE_ORIG;
246 refcount_set(&fclones->fclone_ref, 1);
248 fclones->skb2.fclone = SKB_FCLONE_CLONE;
253 kmem_cache_free(cache, skb);
257 EXPORT_SYMBOL(__alloc_skb);
260 * __build_skb - build a network buffer
261 * @data: data buffer provided by caller
262 * @frag_size: size of data, or 0 if head was kmalloced
264 * Allocate a new &sk_buff. Caller provides space holding head and
265 * skb_shared_info. @data must have been allocated by kmalloc() only if
266 * @frag_size is 0, otherwise data should come from the page allocator
268 * The return is the new skb buffer.
269 * On a failure the return is %NULL, and @data is not freed.
271 * Before IO, driver allocates only data buffer where NIC put incoming frame
272 * Driver should add room at head (NET_SKB_PAD) and
273 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
274 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
275 * before giving packet to stack.
276 * RX rings only contains data buffers, not full skbs.
278 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
280 struct skb_shared_info *shinfo;
282 unsigned int size = frag_size ? : ksize(data);
284 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
288 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
290 memset(skb, 0, offsetof(struct sk_buff, tail));
291 skb->truesize = SKB_TRUESIZE(size);
292 refcount_set(&skb->users, 1);
295 skb_reset_tail_pointer(skb);
296 skb->end = skb->tail + size;
297 skb->mac_header = (typeof(skb->mac_header))~0U;
298 skb->transport_header = (typeof(skb->transport_header))~0U;
300 /* make sure we initialize shinfo sequentially */
301 shinfo = skb_shinfo(skb);
302 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
303 atomic_set(&shinfo->dataref, 1);
308 /* build_skb() is wrapper over __build_skb(), that specifically
309 * takes care of skb->head and skb->pfmemalloc
310 * This means that if @frag_size is not zero, then @data must be backed
311 * by a page fragment, not kmalloc() or vmalloc()
313 struct sk_buff *build_skb(void *data, unsigned int frag_size)
315 struct sk_buff *skb = __build_skb(data, frag_size);
317 if (skb && frag_size) {
319 if (page_is_pfmemalloc(virt_to_head_page(data)))
324 EXPORT_SYMBOL(build_skb);
326 #define NAPI_SKB_CACHE_SIZE 64
328 struct napi_alloc_cache {
329 struct page_frag_cache page;
330 unsigned int skb_count;
331 void *skb_cache[NAPI_SKB_CACHE_SIZE];
334 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
335 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
337 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
339 struct page_frag_cache *nc;
343 local_irq_save(flags);
344 nc = this_cpu_ptr(&netdev_alloc_cache);
345 data = page_frag_alloc(nc, fragsz, gfp_mask);
346 local_irq_restore(flags);
351 * netdev_alloc_frag - allocate a page fragment
352 * @fragsz: fragment size
354 * Allocates a frag from a page for receive buffer.
355 * Uses GFP_ATOMIC allocations.
357 void *netdev_alloc_frag(unsigned int fragsz)
359 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
361 EXPORT_SYMBOL(netdev_alloc_frag);
363 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
365 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
367 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
370 void *napi_alloc_frag(unsigned int fragsz)
372 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
374 EXPORT_SYMBOL(napi_alloc_frag);
377 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
378 * @dev: network device to receive on
379 * @len: length to allocate
380 * @gfp_mask: get_free_pages mask, passed to alloc_skb
382 * Allocate a new &sk_buff and assign it a usage count of one. The
383 * buffer has NET_SKB_PAD headroom built in. Users should allocate
384 * the headroom they think they need without accounting for the
385 * built in space. The built in space is used for optimisations.
387 * %NULL is returned if there is no free memory.
389 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
392 struct page_frag_cache *nc;
400 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
401 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
402 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
408 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
409 len = SKB_DATA_ALIGN(len);
411 if (sk_memalloc_socks())
412 gfp_mask |= __GFP_MEMALLOC;
414 local_irq_save(flags);
416 nc = this_cpu_ptr(&netdev_alloc_cache);
417 data = page_frag_alloc(nc, len, gfp_mask);
418 pfmemalloc = nc->pfmemalloc;
420 local_irq_restore(flags);
425 skb = __build_skb(data, len);
426 if (unlikely(!skb)) {
431 /* use OR instead of assignment to avoid clearing of bits in mask */
437 skb_reserve(skb, NET_SKB_PAD);
443 EXPORT_SYMBOL(__netdev_alloc_skb);
446 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
447 * @napi: napi instance this buffer was allocated for
448 * @len: length to allocate
449 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
451 * Allocate a new sk_buff for use in NAPI receive. This buffer will
452 * attempt to allocate the head from a special reserved region used
453 * only for NAPI Rx allocation. By doing this we can save several
454 * CPU cycles by avoiding having to disable and re-enable IRQs.
456 * %NULL is returned if there is no free memory.
458 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
461 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
465 len += NET_SKB_PAD + NET_IP_ALIGN;
467 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
468 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
469 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
475 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
476 len = SKB_DATA_ALIGN(len);
478 if (sk_memalloc_socks())
479 gfp_mask |= __GFP_MEMALLOC;
481 data = page_frag_alloc(&nc->page, len, gfp_mask);
485 skb = __build_skb(data, len);
486 if (unlikely(!skb)) {
491 /* use OR instead of assignment to avoid clearing of bits in mask */
492 if (nc->page.pfmemalloc)
497 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
498 skb->dev = napi->dev;
503 EXPORT_SYMBOL(__napi_alloc_skb);
505 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
506 int size, unsigned int truesize)
508 skb_fill_page_desc(skb, i, page, off, size);
510 skb->data_len += size;
511 skb->truesize += truesize;
513 EXPORT_SYMBOL(skb_add_rx_frag);
515 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
516 unsigned int truesize)
518 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
520 skb_frag_size_add(frag, size);
522 skb->data_len += size;
523 skb->truesize += truesize;
525 EXPORT_SYMBOL(skb_coalesce_rx_frag);
527 static void skb_drop_list(struct sk_buff **listp)
529 kfree_skb_list(*listp);
533 static inline void skb_drop_fraglist(struct sk_buff *skb)
535 skb_drop_list(&skb_shinfo(skb)->frag_list);
538 static void skb_clone_fraglist(struct sk_buff *skb)
540 struct sk_buff *list;
542 skb_walk_frags(skb, list)
546 static void skb_free_head(struct sk_buff *skb)
548 unsigned char *head = skb->head;
556 static void skb_release_data(struct sk_buff *skb)
558 struct skb_shared_info *shinfo = skb_shinfo(skb);
562 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
566 for (i = 0; i < shinfo->nr_frags; i++)
567 __skb_frag_unref(&shinfo->frags[i]);
569 if (shinfo->frag_list)
570 kfree_skb_list(shinfo->frag_list);
572 skb_zcopy_clear(skb, true);
577 * Free an skbuff by memory without cleaning the state.
579 static void kfree_skbmem(struct sk_buff *skb)
581 struct sk_buff_fclones *fclones;
583 switch (skb->fclone) {
584 case SKB_FCLONE_UNAVAILABLE:
585 kmem_cache_free(skbuff_head_cache, skb);
588 case SKB_FCLONE_ORIG:
589 fclones = container_of(skb, struct sk_buff_fclones, skb1);
591 /* We usually free the clone (TX completion) before original skb
592 * This test would have no chance to be true for the clone,
593 * while here, branch prediction will be good.
595 if (refcount_read(&fclones->fclone_ref) == 1)
599 default: /* SKB_FCLONE_CLONE */
600 fclones = container_of(skb, struct sk_buff_fclones, skb2);
603 if (!refcount_dec_and_test(&fclones->fclone_ref))
606 kmem_cache_free(skbuff_fclone_cache, fclones);
609 void skb_release_head_state(struct sk_buff *skb)
612 if (skb->destructor) {
614 skb->destructor(skb);
616 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
617 nf_conntrack_put(skb_nfct(skb));
622 /* Free everything but the sk_buff shell. */
623 static void skb_release_all(struct sk_buff *skb)
625 skb_release_head_state(skb);
626 if (likely(skb->head))
627 skb_release_data(skb);
631 * __kfree_skb - private function
634 * Free an sk_buff. Release anything attached to the buffer.
635 * Clean the state. This is an internal helper function. Users should
636 * always call kfree_skb
639 void __kfree_skb(struct sk_buff *skb)
641 skb_release_all(skb);
644 EXPORT_SYMBOL(__kfree_skb);
647 * kfree_skb - free an sk_buff
648 * @skb: buffer to free
650 * Drop a reference to the buffer and free it if the usage count has
653 void kfree_skb(struct sk_buff *skb)
658 trace_kfree_skb(skb, __builtin_return_address(0));
661 EXPORT_SYMBOL(kfree_skb);
663 void kfree_skb_list(struct sk_buff *segs)
666 struct sk_buff *next = segs->next;
672 EXPORT_SYMBOL(kfree_skb_list);
675 * skb_tx_error - report an sk_buff xmit error
676 * @skb: buffer that triggered an error
678 * Report xmit error if a device callback is tracking this skb.
679 * skb must be freed afterwards.
681 void skb_tx_error(struct sk_buff *skb)
683 skb_zcopy_clear(skb, true);
685 EXPORT_SYMBOL(skb_tx_error);
688 * consume_skb - free an skbuff
689 * @skb: buffer to free
691 * Drop a ref to the buffer and free it if the usage count has hit zero
692 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
693 * is being dropped after a failure and notes that
695 void consume_skb(struct sk_buff *skb)
700 trace_consume_skb(skb);
703 EXPORT_SYMBOL(consume_skb);
706 * consume_stateless_skb - free an skbuff, assuming it is stateless
707 * @skb: buffer to free
709 * Alike consume_skb(), but this variant assumes that this is the last
710 * skb reference and all the head states have been already dropped
712 void __consume_stateless_skb(struct sk_buff *skb)
714 trace_consume_skb(skb);
715 skb_release_data(skb);
719 void __kfree_skb_flush(void)
721 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
723 /* flush skb_cache if containing objects */
725 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
731 static inline void _kfree_skb_defer(struct sk_buff *skb)
733 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
735 /* drop skb->head and call any destructors for packet */
736 skb_release_all(skb);
738 /* record skb to CPU local list */
739 nc->skb_cache[nc->skb_count++] = skb;
742 /* SLUB writes into objects when freeing */
746 /* flush skb_cache if it is filled */
747 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
748 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
753 void __kfree_skb_defer(struct sk_buff *skb)
755 _kfree_skb_defer(skb);
758 void napi_consume_skb(struct sk_buff *skb, int budget)
763 /* Zero budget indicate non-NAPI context called us, like netpoll */
764 if (unlikely(!budget)) {
765 dev_consume_skb_any(skb);
772 /* if reaching here SKB is ready to free */
773 trace_consume_skb(skb);
775 /* if SKB is a clone, don't handle this case */
776 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
781 _kfree_skb_defer(skb);
783 EXPORT_SYMBOL(napi_consume_skb);
785 /* Make sure a field is enclosed inside headers_start/headers_end section */
786 #define CHECK_SKB_FIELD(field) \
787 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
788 offsetof(struct sk_buff, headers_start)); \
789 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
790 offsetof(struct sk_buff, headers_end)); \
792 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
794 new->tstamp = old->tstamp;
795 /* We do not copy old->sk */
797 memcpy(new->cb, old->cb, sizeof(old->cb));
798 skb_dst_copy(new, old);
799 __skb_ext_copy(new, old);
800 __nf_copy(new, old, false);
802 /* Note : this field could be in headers_start/headers_end section
803 * It is not yet because we do not want to have a 16 bit hole
805 new->queue_mapping = old->queue_mapping;
807 memcpy(&new->headers_start, &old->headers_start,
808 offsetof(struct sk_buff, headers_end) -
809 offsetof(struct sk_buff, headers_start));
810 CHECK_SKB_FIELD(protocol);
811 CHECK_SKB_FIELD(csum);
812 CHECK_SKB_FIELD(hash);
813 CHECK_SKB_FIELD(priority);
814 CHECK_SKB_FIELD(skb_iif);
815 CHECK_SKB_FIELD(vlan_proto);
816 CHECK_SKB_FIELD(vlan_tci);
817 CHECK_SKB_FIELD(transport_header);
818 CHECK_SKB_FIELD(network_header);
819 CHECK_SKB_FIELD(mac_header);
820 CHECK_SKB_FIELD(inner_protocol);
821 CHECK_SKB_FIELD(inner_transport_header);
822 CHECK_SKB_FIELD(inner_network_header);
823 CHECK_SKB_FIELD(inner_mac_header);
824 CHECK_SKB_FIELD(mark);
825 #ifdef CONFIG_NETWORK_SECMARK
826 CHECK_SKB_FIELD(secmark);
828 #ifdef CONFIG_NET_RX_BUSY_POLL
829 CHECK_SKB_FIELD(napi_id);
832 CHECK_SKB_FIELD(sender_cpu);
834 #ifdef CONFIG_NET_SCHED
835 CHECK_SKB_FIELD(tc_index);
841 * You should not add any new code to this function. Add it to
842 * __copy_skb_header above instead.
844 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
846 #define C(x) n->x = skb->x
848 n->next = n->prev = NULL;
850 __copy_skb_header(n, skb);
855 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
860 n->destructor = NULL;
867 refcount_set(&n->users, 1);
869 atomic_inc(&(skb_shinfo(skb)->dataref));
877 * skb_morph - morph one skb into another
878 * @dst: the skb to receive the contents
879 * @src: the skb to supply the contents
881 * This is identical to skb_clone except that the target skb is
882 * supplied by the user.
884 * The target skb is returned upon exit.
886 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
888 skb_release_all(dst);
889 return __skb_clone(dst, src);
891 EXPORT_SYMBOL_GPL(skb_morph);
893 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
895 unsigned long max_pg, num_pg, new_pg, old_pg;
896 struct user_struct *user;
898 if (capable(CAP_IPC_LOCK) || !size)
901 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
902 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
903 user = mmp->user ? : current_user();
906 old_pg = atomic_long_read(&user->locked_vm);
907 new_pg = old_pg + num_pg;
910 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
914 mmp->user = get_uid(user);
915 mmp->num_pg = num_pg;
917 mmp->num_pg += num_pg;
922 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
924 void mm_unaccount_pinned_pages(struct mmpin *mmp)
927 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
931 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
933 struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
935 struct ubuf_info *uarg;
938 WARN_ON_ONCE(!in_task());
940 skb = sock_omalloc(sk, 0, GFP_KERNEL);
944 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
945 uarg = (void *)skb->cb;
946 uarg->mmp.user = NULL;
948 if (mm_account_pinned_pages(&uarg->mmp, size)) {
953 uarg->callback = sock_zerocopy_callback;
954 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
956 uarg->bytelen = size;
958 refcount_set(&uarg->refcnt, 1);
963 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
965 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
967 return container_of((void *)uarg, struct sk_buff, cb);
970 struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
971 struct ubuf_info *uarg)
974 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
977 /* realloc only when socket is locked (TCP, UDP cork),
978 * so uarg->len and sk_zckey access is serialized
980 if (!sock_owned_by_user(sk)) {
985 bytelen = uarg->bytelen + size;
986 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
987 /* TCP can create new skb to attach new uarg */
988 if (sk->sk_type == SOCK_STREAM)
993 next = (u32)atomic_read(&sk->sk_zckey);
994 if ((u32)(uarg->id + uarg->len) == next) {
995 if (mm_account_pinned_pages(&uarg->mmp, size))
998 uarg->bytelen = bytelen;
999 atomic_set(&sk->sk_zckey, ++next);
1000 sock_zerocopy_get(uarg);
1006 return sock_zerocopy_alloc(sk, size);
1008 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1010 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1012 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1016 old_lo = serr->ee.ee_info;
1017 old_hi = serr->ee.ee_data;
1018 sum_len = old_hi - old_lo + 1ULL + len;
1020 if (sum_len >= (1ULL << 32))
1023 if (lo != old_hi + 1)
1026 serr->ee.ee_data += len;
1030 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1032 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1033 struct sock_exterr_skb *serr;
1034 struct sock *sk = skb->sk;
1035 struct sk_buff_head *q;
1036 unsigned long flags;
1040 mm_unaccount_pinned_pages(&uarg->mmp);
1042 /* if !len, there was only 1 call, and it was aborted
1043 * so do not queue a completion notification
1045 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1050 hi = uarg->id + len - 1;
1052 serr = SKB_EXT_ERR(skb);
1053 memset(serr, 0, sizeof(*serr));
1054 serr->ee.ee_errno = 0;
1055 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1056 serr->ee.ee_data = hi;
1057 serr->ee.ee_info = lo;
1059 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1061 q = &sk->sk_error_queue;
1062 spin_lock_irqsave(&q->lock, flags);
1063 tail = skb_peek_tail(q);
1064 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1065 !skb_zerocopy_notify_extend(tail, lo, len)) {
1066 __skb_queue_tail(q, skb);
1069 spin_unlock_irqrestore(&q->lock, flags);
1071 sk->sk_error_report(sk);
1077 EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1079 void sock_zerocopy_put(struct ubuf_info *uarg)
1081 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
1083 uarg->callback(uarg, uarg->zerocopy);
1085 consume_skb(skb_from_uarg(uarg));
1088 EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1090 void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1093 struct sock *sk = skb_from_uarg(uarg)->sk;
1095 atomic_dec(&sk->sk_zckey);
1099 sock_zerocopy_put(uarg);
1102 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1104 extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1105 struct iov_iter *from, size_t length);
1107 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1109 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1111 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1113 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1114 struct msghdr *msg, int len,
1115 struct ubuf_info *uarg)
1117 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1118 struct iov_iter orig_iter = msg->msg_iter;
1119 int err, orig_len = skb->len;
1121 /* An skb can only point to one uarg. This edge case happens when
1122 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1124 if (orig_uarg && uarg != orig_uarg)
1127 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1128 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1129 struct sock *save_sk = skb->sk;
1131 /* Streams do not free skb on error. Reset to prev state. */
1132 msg->msg_iter = orig_iter;
1134 ___pskb_trim(skb, orig_len);
1139 skb_zcopy_set(skb, uarg, NULL);
1140 return skb->len - orig_len;
1142 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1144 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1147 if (skb_zcopy(orig)) {
1148 if (skb_zcopy(nskb)) {
1149 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1154 if (skb_uarg(nskb) == skb_uarg(orig))
1156 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1159 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1165 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1166 * @skb: the skb to modify
1167 * @gfp_mask: allocation priority
1169 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1170 * It will copy all frags into kernel and drop the reference
1171 * to userspace pages.
1173 * If this function is called from an interrupt gfp_mask() must be
1176 * Returns 0 on success or a negative error code on failure
1177 * to allocate kernel memory to copy to.
1179 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1181 int num_frags = skb_shinfo(skb)->nr_frags;
1182 struct page *page, *head = NULL;
1186 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1192 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1193 for (i = 0; i < new_frags; i++) {
1194 page = alloc_page(gfp_mask);
1197 struct page *next = (struct page *)page_private(head);
1203 set_page_private(page, (unsigned long)head);
1209 for (i = 0; i < num_frags; i++) {
1210 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1211 u32 p_off, p_len, copied;
1215 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1216 p, p_off, p_len, copied) {
1218 vaddr = kmap_atomic(p);
1220 while (done < p_len) {
1221 if (d_off == PAGE_SIZE) {
1223 page = (struct page *)page_private(page);
1225 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1226 memcpy(page_address(page) + d_off,
1227 vaddr + p_off + done, copy);
1231 kunmap_atomic(vaddr);
1235 /* skb frags release userspace buffers */
1236 for (i = 0; i < num_frags; i++)
1237 skb_frag_unref(skb, i);
1239 /* skb frags point to kernel buffers */
1240 for (i = 0; i < new_frags - 1; i++) {
1241 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1242 head = (struct page *)page_private(head);
1244 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1245 skb_shinfo(skb)->nr_frags = new_frags;
1248 skb_zcopy_clear(skb, false);
1251 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1254 * skb_clone - duplicate an sk_buff
1255 * @skb: buffer to clone
1256 * @gfp_mask: allocation priority
1258 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1259 * copies share the same packet data but not structure. The new
1260 * buffer has a reference count of 1. If the allocation fails the
1261 * function returns %NULL otherwise the new buffer is returned.
1263 * If this function is called from an interrupt gfp_mask() must be
1267 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1269 struct sk_buff_fclones *fclones = container_of(skb,
1270 struct sk_buff_fclones,
1274 if (skb_orphan_frags(skb, gfp_mask))
1277 if (skb->fclone == SKB_FCLONE_ORIG &&
1278 refcount_read(&fclones->fclone_ref) == 1) {
1280 refcount_set(&fclones->fclone_ref, 2);
1282 if (skb_pfmemalloc(skb))
1283 gfp_mask |= __GFP_MEMALLOC;
1285 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1289 n->fclone = SKB_FCLONE_UNAVAILABLE;
1292 return __skb_clone(n, skb);
1294 EXPORT_SYMBOL(skb_clone);
1296 void skb_headers_offset_update(struct sk_buff *skb, int off)
1298 /* Only adjust this if it actually is csum_start rather than csum */
1299 if (skb->ip_summed == CHECKSUM_PARTIAL)
1300 skb->csum_start += off;
1301 /* {transport,network,mac}_header and tail are relative to skb->head */
1302 skb->transport_header += off;
1303 skb->network_header += off;
1304 if (skb_mac_header_was_set(skb))
1305 skb->mac_header += off;
1306 skb->inner_transport_header += off;
1307 skb->inner_network_header += off;
1308 skb->inner_mac_header += off;
1310 EXPORT_SYMBOL(skb_headers_offset_update);
1312 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1314 __copy_skb_header(new, old);
1316 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1317 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1318 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1320 EXPORT_SYMBOL(skb_copy_header);
1322 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1324 if (skb_pfmemalloc(skb))
1325 return SKB_ALLOC_RX;
1330 * skb_copy - create private copy of an sk_buff
1331 * @skb: buffer to copy
1332 * @gfp_mask: allocation priority
1334 * Make a copy of both an &sk_buff and its data. This is used when the
1335 * caller wishes to modify the data and needs a private copy of the
1336 * data to alter. Returns %NULL on failure or the pointer to the buffer
1337 * on success. The returned buffer has a reference count of 1.
1339 * As by-product this function converts non-linear &sk_buff to linear
1340 * one, so that &sk_buff becomes completely private and caller is allowed
1341 * to modify all the data of returned buffer. This means that this
1342 * function is not recommended for use in circumstances when only
1343 * header is going to be modified. Use pskb_copy() instead.
1346 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1348 int headerlen = skb_headroom(skb);
1349 unsigned int size = skb_end_offset(skb) + skb->data_len;
1350 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1351 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1356 /* Set the data pointer */
1357 skb_reserve(n, headerlen);
1358 /* Set the tail pointer and length */
1359 skb_put(n, skb->len);
1361 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1363 skb_copy_header(n, skb);
1366 EXPORT_SYMBOL(skb_copy);
1369 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1370 * @skb: buffer to copy
1371 * @headroom: headroom of new skb
1372 * @gfp_mask: allocation priority
1373 * @fclone: if true allocate the copy of the skb from the fclone
1374 * cache instead of the head cache; it is recommended to set this
1375 * to true for the cases where the copy will likely be cloned
1377 * Make a copy of both an &sk_buff and part of its data, located
1378 * in header. Fragmented data remain shared. This is used when
1379 * the caller wishes to modify only header of &sk_buff and needs
1380 * private copy of the header to alter. Returns %NULL on failure
1381 * or the pointer to the buffer on success.
1382 * The returned buffer has a reference count of 1.
1385 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1386 gfp_t gfp_mask, bool fclone)
1388 unsigned int size = skb_headlen(skb) + headroom;
1389 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1390 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1395 /* Set the data pointer */
1396 skb_reserve(n, headroom);
1397 /* Set the tail pointer and length */
1398 skb_put(n, skb_headlen(skb));
1399 /* Copy the bytes */
1400 skb_copy_from_linear_data(skb, n->data, n->len);
1402 n->truesize += skb->data_len;
1403 n->data_len = skb->data_len;
1406 if (skb_shinfo(skb)->nr_frags) {
1409 if (skb_orphan_frags(skb, gfp_mask) ||
1410 skb_zerocopy_clone(n, skb, gfp_mask)) {
1415 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1416 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1417 skb_frag_ref(skb, i);
1419 skb_shinfo(n)->nr_frags = i;
1422 if (skb_has_frag_list(skb)) {
1423 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1424 skb_clone_fraglist(n);
1427 skb_copy_header(n, skb);
1431 EXPORT_SYMBOL(__pskb_copy_fclone);
1434 * pskb_expand_head - reallocate header of &sk_buff
1435 * @skb: buffer to reallocate
1436 * @nhead: room to add at head
1437 * @ntail: room to add at tail
1438 * @gfp_mask: allocation priority
1440 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1441 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1442 * reference count of 1. Returns zero in the case of success or error,
1443 * if expansion failed. In the last case, &sk_buff is not changed.
1445 * All the pointers pointing into skb header may change and must be
1446 * reloaded after call to this function.
1449 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1452 int i, osize = skb_end_offset(skb);
1453 int size = osize + nhead + ntail;
1459 BUG_ON(skb_shared(skb));
1461 size = SKB_DATA_ALIGN(size);
1463 if (skb_pfmemalloc(skb))
1464 gfp_mask |= __GFP_MEMALLOC;
1465 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1466 gfp_mask, NUMA_NO_NODE, NULL);
1469 size = SKB_WITH_OVERHEAD(ksize(data));
1471 /* Copy only real data... and, alas, header. This should be
1472 * optimized for the cases when header is void.
1474 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1476 memcpy((struct skb_shared_info *)(data + size),
1478 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1481 * if shinfo is shared we must drop the old head gracefully, but if it
1482 * is not we can just drop the old head and let the existing refcount
1483 * be since all we did is relocate the values
1485 if (skb_cloned(skb)) {
1486 if (skb_orphan_frags(skb, gfp_mask))
1489 refcount_inc(&skb_uarg(skb)->refcnt);
1490 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1491 skb_frag_ref(skb, i);
1493 if (skb_has_frag_list(skb))
1494 skb_clone_fraglist(skb);
1496 skb_release_data(skb);
1500 off = (data + nhead) - skb->head;
1505 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1509 skb->end = skb->head + size;
1512 skb_headers_offset_update(skb, nhead);
1516 atomic_set(&skb_shinfo(skb)->dataref, 1);
1518 skb_metadata_clear(skb);
1520 /* It is not generally safe to change skb->truesize.
1521 * For the moment, we really care of rx path, or
1522 * when skb is orphaned (not attached to a socket).
1524 if (!skb->sk || skb->destructor == sock_edemux)
1525 skb->truesize += size - osize;
1534 EXPORT_SYMBOL(pskb_expand_head);
1536 /* Make private copy of skb with writable head and some headroom */
1538 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1540 struct sk_buff *skb2;
1541 int delta = headroom - skb_headroom(skb);
1544 skb2 = pskb_copy(skb, GFP_ATOMIC);
1546 skb2 = skb_clone(skb, GFP_ATOMIC);
1547 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1555 EXPORT_SYMBOL(skb_realloc_headroom);
1558 * skb_copy_expand - copy and expand sk_buff
1559 * @skb: buffer to copy
1560 * @newheadroom: new free bytes at head
1561 * @newtailroom: new free bytes at tail
1562 * @gfp_mask: allocation priority
1564 * Make a copy of both an &sk_buff and its data and while doing so
1565 * allocate additional space.
1567 * This is used when the caller wishes to modify the data and needs a
1568 * private copy of the data to alter as well as more space for new fields.
1569 * Returns %NULL on failure or the pointer to the buffer
1570 * on success. The returned buffer has a reference count of 1.
1572 * You must pass %GFP_ATOMIC as the allocation priority if this function
1573 * is called from an interrupt.
1575 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1576 int newheadroom, int newtailroom,
1580 * Allocate the copy buffer
1582 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1583 gfp_mask, skb_alloc_rx_flag(skb),
1585 int oldheadroom = skb_headroom(skb);
1586 int head_copy_len, head_copy_off;
1591 skb_reserve(n, newheadroom);
1593 /* Set the tail pointer and length */
1594 skb_put(n, skb->len);
1596 head_copy_len = oldheadroom;
1598 if (newheadroom <= head_copy_len)
1599 head_copy_len = newheadroom;
1601 head_copy_off = newheadroom - head_copy_len;
1603 /* Copy the linear header and data. */
1604 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1605 skb->len + head_copy_len));
1607 skb_copy_header(n, skb);
1609 skb_headers_offset_update(n, newheadroom - oldheadroom);
1613 EXPORT_SYMBOL(skb_copy_expand);
1616 * __skb_pad - zero pad the tail of an skb
1617 * @skb: buffer to pad
1618 * @pad: space to pad
1619 * @free_on_error: free buffer on error
1621 * Ensure that a buffer is followed by a padding area that is zero
1622 * filled. Used by network drivers which may DMA or transfer data
1623 * beyond the buffer end onto the wire.
1625 * May return error in out of memory cases. The skb is freed on error
1626 * if @free_on_error is true.
1629 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1634 /* If the skbuff is non linear tailroom is always zero.. */
1635 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1636 memset(skb->data+skb->len, 0, pad);
1640 ntail = skb->data_len + pad - (skb->end - skb->tail);
1641 if (likely(skb_cloned(skb) || ntail > 0)) {
1642 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1647 /* FIXME: The use of this function with non-linear skb's really needs
1650 err = skb_linearize(skb);
1654 memset(skb->data + skb->len, 0, pad);
1662 EXPORT_SYMBOL(__skb_pad);
1665 * pskb_put - add data to the tail of a potentially fragmented buffer
1666 * @skb: start of the buffer to use
1667 * @tail: tail fragment of the buffer to use
1668 * @len: amount of data to add
1670 * This function extends the used data area of the potentially
1671 * fragmented buffer. @tail must be the last fragment of @skb -- or
1672 * @skb itself. If this would exceed the total buffer size the kernel
1673 * will panic. A pointer to the first byte of the extra data is
1677 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1680 skb->data_len += len;
1683 return skb_put(tail, len);
1685 EXPORT_SYMBOL_GPL(pskb_put);
1688 * skb_put - add data to a buffer
1689 * @skb: buffer to use
1690 * @len: amount of data to add
1692 * This function extends the used data area of the buffer. If this would
1693 * exceed the total buffer size the kernel will panic. A pointer to the
1694 * first byte of the extra data is returned.
1696 void *skb_put(struct sk_buff *skb, unsigned int len)
1698 void *tmp = skb_tail_pointer(skb);
1699 SKB_LINEAR_ASSERT(skb);
1702 if (unlikely(skb->tail > skb->end))
1703 skb_over_panic(skb, len, __builtin_return_address(0));
1706 EXPORT_SYMBOL(skb_put);
1709 * skb_push - add data to the start of a buffer
1710 * @skb: buffer to use
1711 * @len: amount of data to add
1713 * This function extends the used data area of the buffer at the buffer
1714 * start. If this would exceed the total buffer headroom the kernel will
1715 * panic. A pointer to the first byte of the extra data is returned.
1717 void *skb_push(struct sk_buff *skb, unsigned int len)
1721 if (unlikely(skb->data < skb->head))
1722 skb_under_panic(skb, len, __builtin_return_address(0));
1725 EXPORT_SYMBOL(skb_push);
1728 * skb_pull - remove data from the start of a buffer
1729 * @skb: buffer to use
1730 * @len: amount of data to remove
1732 * This function removes data from the start of a buffer, returning
1733 * the memory to the headroom. A pointer to the next data in the buffer
1734 * is returned. Once the data has been pulled future pushes will overwrite
1737 void *skb_pull(struct sk_buff *skb, unsigned int len)
1739 return skb_pull_inline(skb, len);
1741 EXPORT_SYMBOL(skb_pull);
1744 * skb_trim - remove end from a buffer
1745 * @skb: buffer to alter
1748 * Cut the length of a buffer down by removing data from the tail. If
1749 * the buffer is already under the length specified it is not modified.
1750 * The skb must be linear.
1752 void skb_trim(struct sk_buff *skb, unsigned int len)
1755 __skb_trim(skb, len);
1757 EXPORT_SYMBOL(skb_trim);
1759 /* Trims skb to length len. It can change skb pointers.
1762 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1764 struct sk_buff **fragp;
1765 struct sk_buff *frag;
1766 int offset = skb_headlen(skb);
1767 int nfrags = skb_shinfo(skb)->nr_frags;
1771 if (skb_cloned(skb) &&
1772 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1779 for (; i < nfrags; i++) {
1780 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1787 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1790 skb_shinfo(skb)->nr_frags = i;
1792 for (; i < nfrags; i++)
1793 skb_frag_unref(skb, i);
1795 if (skb_has_frag_list(skb))
1796 skb_drop_fraglist(skb);
1800 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1801 fragp = &frag->next) {
1802 int end = offset + frag->len;
1804 if (skb_shared(frag)) {
1805 struct sk_buff *nfrag;
1807 nfrag = skb_clone(frag, GFP_ATOMIC);
1808 if (unlikely(!nfrag))
1811 nfrag->next = frag->next;
1823 unlikely((err = pskb_trim(frag, len - offset))))
1827 skb_drop_list(&frag->next);
1832 if (len > skb_headlen(skb)) {
1833 skb->data_len -= skb->len - len;
1838 skb_set_tail_pointer(skb, len);
1841 if (!skb->sk || skb->destructor == sock_edemux)
1845 EXPORT_SYMBOL(___pskb_trim);
1847 /* Note : use pskb_trim_rcsum() instead of calling this directly
1849 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1851 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1852 int delta = skb->len - len;
1854 skb->csum = csum_block_sub(skb->csum,
1855 skb_checksum(skb, len, delta, 0),
1858 return __pskb_trim(skb, len);
1860 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1863 * __pskb_pull_tail - advance tail of skb header
1864 * @skb: buffer to reallocate
1865 * @delta: number of bytes to advance tail
1867 * The function makes a sense only on a fragmented &sk_buff,
1868 * it expands header moving its tail forward and copying necessary
1869 * data from fragmented part.
1871 * &sk_buff MUST have reference count of 1.
1873 * Returns %NULL (and &sk_buff does not change) if pull failed
1874 * or value of new tail of skb in the case of success.
1876 * All the pointers pointing into skb header may change and must be
1877 * reloaded after call to this function.
1880 /* Moves tail of skb head forward, copying data from fragmented part,
1881 * when it is necessary.
1882 * 1. It may fail due to malloc failure.
1883 * 2. It may change skb pointers.
1885 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1887 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1889 /* If skb has not enough free space at tail, get new one
1890 * plus 128 bytes for future expansions. If we have enough
1891 * room at tail, reallocate without expansion only if skb is cloned.
1893 int i, k, eat = (skb->tail + delta) - skb->end;
1895 if (eat > 0 || skb_cloned(skb)) {
1896 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1901 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1902 skb_tail_pointer(skb), delta));
1904 /* Optimization: no fragments, no reasons to preestimate
1905 * size of pulled pages. Superb.
1907 if (!skb_has_frag_list(skb))
1910 /* Estimate size of pulled pages. */
1912 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1913 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1920 /* If we need update frag list, we are in troubles.
1921 * Certainly, it is possible to add an offset to skb data,
1922 * but taking into account that pulling is expected to
1923 * be very rare operation, it is worth to fight against
1924 * further bloating skb head and crucify ourselves here instead.
1925 * Pure masohism, indeed. 8)8)
1928 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1929 struct sk_buff *clone = NULL;
1930 struct sk_buff *insp = NULL;
1933 if (list->len <= eat) {
1934 /* Eaten as whole. */
1939 /* Eaten partially. */
1941 if (skb_shared(list)) {
1942 /* Sucks! We need to fork list. :-( */
1943 clone = skb_clone(list, GFP_ATOMIC);
1949 /* This may be pulled without
1953 if (!pskb_pull(list, eat)) {
1961 /* Free pulled out fragments. */
1962 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1963 skb_shinfo(skb)->frag_list = list->next;
1966 /* And insert new clone at head. */
1969 skb_shinfo(skb)->frag_list = clone;
1972 /* Success! Now we may commit changes to skb data. */
1977 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1978 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1981 skb_frag_unref(skb, i);
1984 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1986 skb_shinfo(skb)->frags[k].page_offset += eat;
1987 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1995 skb_shinfo(skb)->nr_frags = k;
1999 skb->data_len -= delta;
2002 skb_zcopy_clear(skb, false);
2004 return skb_tail_pointer(skb);
2006 EXPORT_SYMBOL(__pskb_pull_tail);
2009 * skb_copy_bits - copy bits from skb to kernel buffer
2011 * @offset: offset in source
2012 * @to: destination buffer
2013 * @len: number of bytes to copy
2015 * Copy the specified number of bytes from the source skb to the
2016 * destination buffer.
2019 * If its prototype is ever changed,
2020 * check arch/{*}/net/{*}.S files,
2021 * since it is called from BPF assembly code.
2023 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2025 int start = skb_headlen(skb);
2026 struct sk_buff *frag_iter;
2029 if (offset > (int)skb->len - len)
2033 if ((copy = start - offset) > 0) {
2036 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2037 if ((len -= copy) == 0)
2043 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2045 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2047 WARN_ON(start > offset + len);
2049 end = start + skb_frag_size(f);
2050 if ((copy = end - offset) > 0) {
2051 u32 p_off, p_len, copied;
2058 skb_frag_foreach_page(f,
2059 f->page_offset + offset - start,
2060 copy, p, p_off, p_len, copied) {
2061 vaddr = kmap_atomic(p);
2062 memcpy(to + copied, vaddr + p_off, p_len);
2063 kunmap_atomic(vaddr);
2066 if ((len -= copy) == 0)
2074 skb_walk_frags(skb, frag_iter) {
2077 WARN_ON(start > offset + len);
2079 end = start + frag_iter->len;
2080 if ((copy = end - offset) > 0) {
2083 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2085 if ((len -= copy) == 0)
2099 EXPORT_SYMBOL(skb_copy_bits);
2102 * Callback from splice_to_pipe(), if we need to release some pages
2103 * at the end of the spd in case we error'ed out in filling the pipe.
2105 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2107 put_page(spd->pages[i]);
2110 static struct page *linear_to_page(struct page *page, unsigned int *len,
2111 unsigned int *offset,
2114 struct page_frag *pfrag = sk_page_frag(sk);
2116 if (!sk_page_frag_refill(sk, pfrag))
2119 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2121 memcpy(page_address(pfrag->page) + pfrag->offset,
2122 page_address(page) + *offset, *len);
2123 *offset = pfrag->offset;
2124 pfrag->offset += *len;
2129 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2131 unsigned int offset)
2133 return spd->nr_pages &&
2134 spd->pages[spd->nr_pages - 1] == page &&
2135 (spd->partial[spd->nr_pages - 1].offset +
2136 spd->partial[spd->nr_pages - 1].len == offset);
2140 * Fill page/offset/length into spd, if it can hold more pages.
2142 static bool spd_fill_page(struct splice_pipe_desc *spd,
2143 struct pipe_inode_info *pipe, struct page *page,
2144 unsigned int *len, unsigned int offset,
2148 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2152 page = linear_to_page(page, len, &offset, sk);
2156 if (spd_can_coalesce(spd, page, offset)) {
2157 spd->partial[spd->nr_pages - 1].len += *len;
2161 spd->pages[spd->nr_pages] = page;
2162 spd->partial[spd->nr_pages].len = *len;
2163 spd->partial[spd->nr_pages].offset = offset;
2169 static bool __splice_segment(struct page *page, unsigned int poff,
2170 unsigned int plen, unsigned int *off,
2172 struct splice_pipe_desc *spd, bool linear,
2174 struct pipe_inode_info *pipe)
2179 /* skip this segment if already processed */
2185 /* ignore any bits we already processed */
2191 unsigned int flen = min(*len, plen);
2193 if (spd_fill_page(spd, pipe, page, &flen, poff,
2199 } while (*len && plen);
2205 * Map linear and fragment data from the skb to spd. It reports true if the
2206 * pipe is full or if we already spliced the requested length.
2208 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2209 unsigned int *offset, unsigned int *len,
2210 struct splice_pipe_desc *spd, struct sock *sk)
2213 struct sk_buff *iter;
2215 /* map the linear part :
2216 * If skb->head_frag is set, this 'linear' part is backed by a
2217 * fragment, and if the head is not shared with any clones then
2218 * we can avoid a copy since we own the head portion of this page.
2220 if (__splice_segment(virt_to_page(skb->data),
2221 (unsigned long) skb->data & (PAGE_SIZE - 1),
2224 skb_head_is_locked(skb),
2229 * then map the fragments
2231 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2232 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2234 if (__splice_segment(skb_frag_page(f),
2235 f->page_offset, skb_frag_size(f),
2236 offset, len, spd, false, sk, pipe))
2240 skb_walk_frags(skb, iter) {
2241 if (*offset >= iter->len) {
2242 *offset -= iter->len;
2245 /* __skb_splice_bits() only fails if the output has no room
2246 * left, so no point in going over the frag_list for the error
2249 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2257 * Map data from the skb to a pipe. Should handle both the linear part,
2258 * the fragments, and the frag list.
2260 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2261 struct pipe_inode_info *pipe, unsigned int tlen,
2264 struct partial_page partial[MAX_SKB_FRAGS];
2265 struct page *pages[MAX_SKB_FRAGS];
2266 struct splice_pipe_desc spd = {
2269 .nr_pages_max = MAX_SKB_FRAGS,
2270 .ops = &nosteal_pipe_buf_ops,
2271 .spd_release = sock_spd_release,
2275 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2278 ret = splice_to_pipe(pipe, &spd);
2282 EXPORT_SYMBOL_GPL(skb_splice_bits);
2284 /* Send skb data on a socket. Socket must be locked. */
2285 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2288 unsigned int orig_len = len;
2289 struct sk_buff *head = skb;
2290 unsigned short fragidx;
2295 /* Deal with head data */
2296 while (offset < skb_headlen(skb) && len) {
2300 slen = min_t(int, len, skb_headlen(skb) - offset);
2301 kv.iov_base = skb->data + offset;
2303 memset(&msg, 0, sizeof(msg));
2305 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2313 /* All the data was skb head? */
2317 /* Make offset relative to start of frags */
2318 offset -= skb_headlen(skb);
2320 /* Find where we are in frag list */
2321 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2322 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2324 if (offset < frag->size)
2327 offset -= frag->size;
2330 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2331 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2333 slen = min_t(size_t, len, frag->size - offset);
2336 ret = kernel_sendpage_locked(sk, frag->page.p,
2337 frag->page_offset + offset,
2338 slen, MSG_DONTWAIT);
2351 /* Process any frag lists */
2354 if (skb_has_frag_list(skb)) {
2355 skb = skb_shinfo(skb)->frag_list;
2358 } else if (skb->next) {
2365 return orig_len - len;
2368 return orig_len == len ? ret : orig_len - len;
2370 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2373 * skb_store_bits - store bits from kernel buffer to skb
2374 * @skb: destination buffer
2375 * @offset: offset in destination
2376 * @from: source buffer
2377 * @len: number of bytes to copy
2379 * Copy the specified number of bytes from the source buffer to the
2380 * destination skb. This function handles all the messy bits of
2381 * traversing fragment lists and such.
2384 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2386 int start = skb_headlen(skb);
2387 struct sk_buff *frag_iter;
2390 if (offset > (int)skb->len - len)
2393 if ((copy = start - offset) > 0) {
2396 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2397 if ((len -= copy) == 0)
2403 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2404 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2407 WARN_ON(start > offset + len);
2409 end = start + skb_frag_size(frag);
2410 if ((copy = end - offset) > 0) {
2411 u32 p_off, p_len, copied;
2418 skb_frag_foreach_page(frag,
2419 frag->page_offset + offset - start,
2420 copy, p, p_off, p_len, copied) {
2421 vaddr = kmap_atomic(p);
2422 memcpy(vaddr + p_off, from + copied, p_len);
2423 kunmap_atomic(vaddr);
2426 if ((len -= copy) == 0)
2434 skb_walk_frags(skb, frag_iter) {
2437 WARN_ON(start > offset + len);
2439 end = start + frag_iter->len;
2440 if ((copy = end - offset) > 0) {
2443 if (skb_store_bits(frag_iter, offset - start,
2446 if ((len -= copy) == 0)
2459 EXPORT_SYMBOL(skb_store_bits);
2461 /* Checksum skb data. */
2462 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2463 __wsum csum, const struct skb_checksum_ops *ops)
2465 int start = skb_headlen(skb);
2466 int i, copy = start - offset;
2467 struct sk_buff *frag_iter;
2470 /* Checksum header. */
2474 csum = ops->update(skb->data + offset, copy, csum);
2475 if ((len -= copy) == 0)
2481 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2483 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2485 WARN_ON(start > offset + len);
2487 end = start + skb_frag_size(frag);
2488 if ((copy = end - offset) > 0) {
2489 u32 p_off, p_len, copied;
2497 skb_frag_foreach_page(frag,
2498 frag->page_offset + offset - start,
2499 copy, p, p_off, p_len, copied) {
2500 vaddr = kmap_atomic(p);
2501 csum2 = ops->update(vaddr + p_off, p_len, 0);
2502 kunmap_atomic(vaddr);
2503 csum = ops->combine(csum, csum2, pos, p_len);
2514 skb_walk_frags(skb, frag_iter) {
2517 WARN_ON(start > offset + len);
2519 end = start + frag_iter->len;
2520 if ((copy = end - offset) > 0) {
2524 csum2 = __skb_checksum(frag_iter, offset - start,
2526 csum = ops->combine(csum, csum2, pos, copy);
2527 if ((len -= copy) == 0)
2538 EXPORT_SYMBOL(__skb_checksum);
2540 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2541 int len, __wsum csum)
2543 const struct skb_checksum_ops ops = {
2544 .update = csum_partial_ext,
2545 .combine = csum_block_add_ext,
2548 return __skb_checksum(skb, offset, len, csum, &ops);
2550 EXPORT_SYMBOL(skb_checksum);
2552 /* Both of above in one bottle. */
2554 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2555 u8 *to, int len, __wsum csum)
2557 int start = skb_headlen(skb);
2558 int i, copy = start - offset;
2559 struct sk_buff *frag_iter;
2566 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2568 if ((len -= copy) == 0)
2575 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2578 WARN_ON(start > offset + len);
2580 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2581 if ((copy = end - offset) > 0) {
2582 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2583 u32 p_off, p_len, copied;
2591 skb_frag_foreach_page(frag,
2592 frag->page_offset + offset - start,
2593 copy, p, p_off, p_len, copied) {
2594 vaddr = kmap_atomic(p);
2595 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2598 kunmap_atomic(vaddr);
2599 csum = csum_block_add(csum, csum2, pos);
2611 skb_walk_frags(skb, frag_iter) {
2615 WARN_ON(start > offset + len);
2617 end = start + frag_iter->len;
2618 if ((copy = end - offset) > 0) {
2621 csum2 = skb_copy_and_csum_bits(frag_iter,
2624 csum = csum_block_add(csum, csum2, pos);
2625 if ((len -= copy) == 0)
2636 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2638 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2642 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
2643 /* See comments in __skb_checksum_complete(). */
2645 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2646 !skb->csum_complete_sw)
2647 netdev_rx_csum_fault(skb->dev, skb);
2649 if (!skb_shared(skb))
2650 skb->csum_valid = !sum;
2653 EXPORT_SYMBOL(__skb_checksum_complete_head);
2655 /* This function assumes skb->csum already holds pseudo header's checksum,
2656 * which has been changed from the hardware checksum, for example, by
2657 * __skb_checksum_validate_complete(). And, the original skb->csum must
2658 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2660 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2661 * zero. The new checksum is stored back into skb->csum unless the skb is
2664 __sum16 __skb_checksum_complete(struct sk_buff *skb)
2669 csum = skb_checksum(skb, 0, skb->len, 0);
2671 sum = csum_fold(csum_add(skb->csum, csum));
2672 /* This check is inverted, because we already knew the hardware
2673 * checksum is invalid before calling this function. So, if the
2674 * re-computed checksum is valid instead, then we have a mismatch
2675 * between the original skb->csum and skb_checksum(). This means either
2676 * the original hardware checksum is incorrect or we screw up skb->csum
2677 * when moving skb->data around.
2680 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2681 !skb->csum_complete_sw)
2682 netdev_rx_csum_fault(skb->dev, skb);
2685 if (!skb_shared(skb)) {
2686 /* Save full packet checksum */
2688 skb->ip_summed = CHECKSUM_COMPLETE;
2689 skb->csum_complete_sw = 1;
2690 skb->csum_valid = !sum;
2695 EXPORT_SYMBOL(__skb_checksum_complete);
2697 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2699 net_warn_ratelimited(
2700 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2705 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2706 int offset, int len)
2708 net_warn_ratelimited(
2709 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2714 static const struct skb_checksum_ops default_crc32c_ops = {
2715 .update = warn_crc32c_csum_update,
2716 .combine = warn_crc32c_csum_combine,
2719 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2720 &default_crc32c_ops;
2721 EXPORT_SYMBOL(crc32c_csum_stub);
2724 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2725 * @from: source buffer
2727 * Calculates the amount of linear headroom needed in the 'to' skb passed
2728 * into skb_zerocopy().
2731 skb_zerocopy_headlen(const struct sk_buff *from)
2733 unsigned int hlen = 0;
2735 if (!from->head_frag ||
2736 skb_headlen(from) < L1_CACHE_BYTES ||
2737 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2738 hlen = skb_headlen(from);
2740 if (skb_has_frag_list(from))
2745 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2748 * skb_zerocopy - Zero copy skb to skb
2749 * @to: destination buffer
2750 * @from: source buffer
2751 * @len: number of bytes to copy from source buffer
2752 * @hlen: size of linear headroom in destination buffer
2754 * Copies up to `len` bytes from `from` to `to` by creating references
2755 * to the frags in the source buffer.
2757 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2758 * headroom in the `to` buffer.
2761 * 0: everything is OK
2762 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2763 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2766 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2769 int plen = 0; /* length of skb->head fragment */
2772 unsigned int offset;
2774 BUG_ON(!from->head_frag && !hlen);
2776 /* dont bother with small payloads */
2777 if (len <= skb_tailroom(to))
2778 return skb_copy_bits(from, 0, skb_put(to, len), len);
2781 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2786 plen = min_t(int, skb_headlen(from), len);
2788 page = virt_to_head_page(from->head);
2789 offset = from->data - (unsigned char *)page_address(page);
2790 __skb_fill_page_desc(to, 0, page, offset, plen);
2797 to->truesize += len + plen;
2798 to->len += len + plen;
2799 to->data_len += len + plen;
2801 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2805 skb_zerocopy_clone(to, from, GFP_ATOMIC);
2807 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2810 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2811 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2812 len -= skb_shinfo(to)->frags[j].size;
2813 skb_frag_ref(to, j);
2816 skb_shinfo(to)->nr_frags = j;
2820 EXPORT_SYMBOL_GPL(skb_zerocopy);
2822 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2827 if (skb->ip_summed == CHECKSUM_PARTIAL)
2828 csstart = skb_checksum_start_offset(skb);
2830 csstart = skb_headlen(skb);
2832 BUG_ON(csstart > skb_headlen(skb));
2834 skb_copy_from_linear_data(skb, to, csstart);
2837 if (csstart != skb->len)
2838 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2839 skb->len - csstart, 0);
2841 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2842 long csstuff = csstart + skb->csum_offset;
2844 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2847 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2850 * skb_dequeue - remove from the head of the queue
2851 * @list: list to dequeue from
2853 * Remove the head of the list. The list lock is taken so the function
2854 * may be used safely with other locking list functions. The head item is
2855 * returned or %NULL if the list is empty.
2858 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2860 unsigned long flags;
2861 struct sk_buff *result;
2863 spin_lock_irqsave(&list->lock, flags);
2864 result = __skb_dequeue(list);
2865 spin_unlock_irqrestore(&list->lock, flags);
2868 EXPORT_SYMBOL(skb_dequeue);
2871 * skb_dequeue_tail - remove from the tail of the queue
2872 * @list: list to dequeue from
2874 * Remove the tail of the list. The list lock is taken so the function
2875 * may be used safely with other locking list functions. The tail item is
2876 * returned or %NULL if the list is empty.
2878 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2880 unsigned long flags;
2881 struct sk_buff *result;
2883 spin_lock_irqsave(&list->lock, flags);
2884 result = __skb_dequeue_tail(list);
2885 spin_unlock_irqrestore(&list->lock, flags);
2888 EXPORT_SYMBOL(skb_dequeue_tail);
2891 * skb_queue_purge - empty a list
2892 * @list: list to empty
2894 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2895 * the list and one reference dropped. This function takes the list
2896 * lock and is atomic with respect to other list locking functions.
2898 void skb_queue_purge(struct sk_buff_head *list)
2900 struct sk_buff *skb;
2901 while ((skb = skb_dequeue(list)) != NULL)
2904 EXPORT_SYMBOL(skb_queue_purge);
2907 * skb_rbtree_purge - empty a skb rbtree
2908 * @root: root of the rbtree to empty
2909 * Return value: the sum of truesizes of all purged skbs.
2911 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2912 * the list and one reference dropped. This function does not take
2913 * any lock. Synchronization should be handled by the caller (e.g., TCP
2914 * out-of-order queue is protected by the socket lock).
2916 unsigned int skb_rbtree_purge(struct rb_root *root)
2918 struct rb_node *p = rb_first(root);
2919 unsigned int sum = 0;
2922 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2925 rb_erase(&skb->rbnode, root);
2926 sum += skb->truesize;
2933 * skb_queue_head - queue a buffer at the list head
2934 * @list: list to use
2935 * @newsk: buffer to queue
2937 * Queue a buffer at the start of the list. This function takes the
2938 * list lock and can be used safely with other locking &sk_buff functions
2941 * A buffer cannot be placed on two lists at the same time.
2943 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2945 unsigned long flags;
2947 spin_lock_irqsave(&list->lock, flags);
2948 __skb_queue_head(list, newsk);
2949 spin_unlock_irqrestore(&list->lock, flags);
2951 EXPORT_SYMBOL(skb_queue_head);
2954 * skb_queue_tail - queue a buffer at the list tail
2955 * @list: list to use
2956 * @newsk: buffer to queue
2958 * Queue a buffer at the tail of the list. This function takes the
2959 * list lock and can be used safely with other locking &sk_buff functions
2962 * A buffer cannot be placed on two lists at the same time.
2964 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2966 unsigned long flags;
2968 spin_lock_irqsave(&list->lock, flags);
2969 __skb_queue_tail(list, newsk);
2970 spin_unlock_irqrestore(&list->lock, flags);
2972 EXPORT_SYMBOL(skb_queue_tail);
2975 * skb_unlink - remove a buffer from a list
2976 * @skb: buffer to remove
2977 * @list: list to use
2979 * Remove a packet from a list. The list locks are taken and this
2980 * function is atomic with respect to other list locked calls
2982 * You must know what list the SKB is on.
2984 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2986 unsigned long flags;
2988 spin_lock_irqsave(&list->lock, flags);
2989 __skb_unlink(skb, list);
2990 spin_unlock_irqrestore(&list->lock, flags);
2992 EXPORT_SYMBOL(skb_unlink);
2995 * skb_append - append a buffer
2996 * @old: buffer to insert after
2997 * @newsk: buffer to insert
2998 * @list: list to use
3000 * Place a packet after a given packet in a list. The list locks are taken
3001 * and this function is atomic with respect to other list locked calls.
3002 * A buffer cannot be placed on two lists at the same time.
3004 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3006 unsigned long flags;
3008 spin_lock_irqsave(&list->lock, flags);
3009 __skb_queue_after(list, old, newsk);
3010 spin_unlock_irqrestore(&list->lock, flags);
3012 EXPORT_SYMBOL(skb_append);
3014 static inline void skb_split_inside_header(struct sk_buff *skb,
3015 struct sk_buff* skb1,
3016 const u32 len, const int pos)
3020 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3022 /* And move data appendix as is. */
3023 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3024 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3026 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3027 skb_shinfo(skb)->nr_frags = 0;
3028 skb1->data_len = skb->data_len;
3029 skb1->len += skb1->data_len;
3032 skb_set_tail_pointer(skb, len);
3035 static inline void skb_split_no_header(struct sk_buff *skb,
3036 struct sk_buff* skb1,
3037 const u32 len, int pos)
3040 const int nfrags = skb_shinfo(skb)->nr_frags;
3042 skb_shinfo(skb)->nr_frags = 0;
3043 skb1->len = skb1->data_len = skb->len - len;
3045 skb->data_len = len - pos;
3047 for (i = 0; i < nfrags; i++) {
3048 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3050 if (pos + size > len) {
3051 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3055 * We have two variants in this case:
3056 * 1. Move all the frag to the second
3057 * part, if it is possible. F.e.
3058 * this approach is mandatory for TUX,
3059 * where splitting is expensive.
3060 * 2. Split is accurately. We make this.
3062 skb_frag_ref(skb, i);
3063 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
3064 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3065 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3066 skb_shinfo(skb)->nr_frags++;
3070 skb_shinfo(skb)->nr_frags++;
3073 skb_shinfo(skb1)->nr_frags = k;
3077 * skb_split - Split fragmented skb to two parts at length len.
3078 * @skb: the buffer to split
3079 * @skb1: the buffer to receive the second part
3080 * @len: new length for skb
3082 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3084 int pos = skb_headlen(skb);
3086 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3088 skb_zerocopy_clone(skb1, skb, 0);
3089 if (len < pos) /* Split line is inside header. */
3090 skb_split_inside_header(skb, skb1, len, pos);
3091 else /* Second chunk has no header, nothing to copy. */
3092 skb_split_no_header(skb, skb1, len, pos);
3094 EXPORT_SYMBOL(skb_split);
3096 /* Shifting from/to a cloned skb is a no-go.
3098 * Caller cannot keep skb_shinfo related pointers past calling here!
3100 static int skb_prepare_for_shift(struct sk_buff *skb)
3102 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3106 * skb_shift - Shifts paged data partially from skb to another
3107 * @tgt: buffer into which tail data gets added
3108 * @skb: buffer from which the paged data comes from
3109 * @shiftlen: shift up to this many bytes
3111 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3112 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3113 * It's up to caller to free skb if everything was shifted.
3115 * If @tgt runs out of frags, the whole operation is aborted.
3117 * Skb cannot include anything else but paged data while tgt is allowed
3118 * to have non-paged data as well.
3120 * TODO: full sized shift could be optimized but that would need
3121 * specialized skb free'er to handle frags without up-to-date nr_frags.
3123 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3125 int from, to, merge, todo;
3126 struct skb_frag_struct *fragfrom, *fragto;
3128 BUG_ON(shiftlen > skb->len);
3130 if (skb_headlen(skb))
3132 if (skb_zcopy(tgt) || skb_zcopy(skb))
3137 to = skb_shinfo(tgt)->nr_frags;
3138 fragfrom = &skb_shinfo(skb)->frags[from];
3140 /* Actual merge is delayed until the point when we know we can
3141 * commit all, so that we don't have to undo partial changes
3144 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3145 fragfrom->page_offset)) {
3150 todo -= skb_frag_size(fragfrom);
3152 if (skb_prepare_for_shift(skb) ||
3153 skb_prepare_for_shift(tgt))
3156 /* All previous frag pointers might be stale! */
3157 fragfrom = &skb_shinfo(skb)->frags[from];
3158 fragto = &skb_shinfo(tgt)->frags[merge];
3160 skb_frag_size_add(fragto, shiftlen);
3161 skb_frag_size_sub(fragfrom, shiftlen);
3162 fragfrom->page_offset += shiftlen;
3170 /* Skip full, not-fitting skb to avoid expensive operations */
3171 if ((shiftlen == skb->len) &&
3172 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3175 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3178 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3179 if (to == MAX_SKB_FRAGS)
3182 fragfrom = &skb_shinfo(skb)->frags[from];
3183 fragto = &skb_shinfo(tgt)->frags[to];
3185 if (todo >= skb_frag_size(fragfrom)) {
3186 *fragto = *fragfrom;
3187 todo -= skb_frag_size(fragfrom);
3192 __skb_frag_ref(fragfrom);
3193 fragto->page = fragfrom->page;
3194 fragto->page_offset = fragfrom->page_offset;
3195 skb_frag_size_set(fragto, todo);
3197 fragfrom->page_offset += todo;
3198 skb_frag_size_sub(fragfrom, todo);
3206 /* Ready to "commit" this state change to tgt */
3207 skb_shinfo(tgt)->nr_frags = to;
3210 fragfrom = &skb_shinfo(skb)->frags[0];
3211 fragto = &skb_shinfo(tgt)->frags[merge];
3213 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3214 __skb_frag_unref(fragfrom);
3217 /* Reposition in the original skb */
3219 while (from < skb_shinfo(skb)->nr_frags)
3220 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3221 skb_shinfo(skb)->nr_frags = to;
3223 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3226 /* Most likely the tgt won't ever need its checksum anymore, skb on
3227 * the other hand might need it if it needs to be resent
3229 tgt->ip_summed = CHECKSUM_PARTIAL;
3230 skb->ip_summed = CHECKSUM_PARTIAL;
3232 /* Yak, is it really working this way? Some helper please? */
3233 skb->len -= shiftlen;
3234 skb->data_len -= shiftlen;
3235 skb->truesize -= shiftlen;
3236 tgt->len += shiftlen;
3237 tgt->data_len += shiftlen;
3238 tgt->truesize += shiftlen;
3244 * skb_prepare_seq_read - Prepare a sequential read of skb data
3245 * @skb: the buffer to read
3246 * @from: lower offset of data to be read
3247 * @to: upper offset of data to be read
3248 * @st: state variable
3250 * Initializes the specified state variable. Must be called before
3251 * invoking skb_seq_read() for the first time.
3253 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3254 unsigned int to, struct skb_seq_state *st)
3256 st->lower_offset = from;
3257 st->upper_offset = to;
3258 st->root_skb = st->cur_skb = skb;
3259 st->frag_idx = st->stepped_offset = 0;
3260 st->frag_data = NULL;
3262 EXPORT_SYMBOL(skb_prepare_seq_read);
3265 * skb_seq_read - Sequentially read skb data
3266 * @consumed: number of bytes consumed by the caller so far
3267 * @data: destination pointer for data to be returned
3268 * @st: state variable
3270 * Reads a block of skb data at @consumed relative to the
3271 * lower offset specified to skb_prepare_seq_read(). Assigns
3272 * the head of the data block to @data and returns the length
3273 * of the block or 0 if the end of the skb data or the upper
3274 * offset has been reached.
3276 * The caller is not required to consume all of the data
3277 * returned, i.e. @consumed is typically set to the number
3278 * of bytes already consumed and the next call to
3279 * skb_seq_read() will return the remaining part of the block.
3281 * Note 1: The size of each block of data returned can be arbitrary,
3282 * this limitation is the cost for zerocopy sequential
3283 * reads of potentially non linear data.
3285 * Note 2: Fragment lists within fragments are not implemented
3286 * at the moment, state->root_skb could be replaced with
3287 * a stack for this purpose.
3289 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3290 struct skb_seq_state *st)
3292 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3295 if (unlikely(abs_offset >= st->upper_offset)) {
3296 if (st->frag_data) {
3297 kunmap_atomic(st->frag_data);
3298 st->frag_data = NULL;
3304 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3306 if (abs_offset < block_limit && !st->frag_data) {
3307 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3308 return block_limit - abs_offset;
3311 if (st->frag_idx == 0 && !st->frag_data)
3312 st->stepped_offset += skb_headlen(st->cur_skb);
3314 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3315 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3316 block_limit = skb_frag_size(frag) + st->stepped_offset;
3318 if (abs_offset < block_limit) {
3320 st->frag_data = kmap_atomic(skb_frag_page(frag));
3322 *data = (u8 *) st->frag_data + frag->page_offset +
3323 (abs_offset - st->stepped_offset);
3325 return block_limit - abs_offset;
3328 if (st->frag_data) {
3329 kunmap_atomic(st->frag_data);
3330 st->frag_data = NULL;
3334 st->stepped_offset += skb_frag_size(frag);
3337 if (st->frag_data) {
3338 kunmap_atomic(st->frag_data);
3339 st->frag_data = NULL;
3342 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3343 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3346 } else if (st->cur_skb->next) {
3347 st->cur_skb = st->cur_skb->next;
3354 EXPORT_SYMBOL(skb_seq_read);
3357 * skb_abort_seq_read - Abort a sequential read of skb data
3358 * @st: state variable
3360 * Must be called if skb_seq_read() was not called until it
3363 void skb_abort_seq_read(struct skb_seq_state *st)
3366 kunmap_atomic(st->frag_data);
3368 EXPORT_SYMBOL(skb_abort_seq_read);
3370 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3372 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3373 struct ts_config *conf,
3374 struct ts_state *state)
3376 return skb_seq_read(offset, text, TS_SKB_CB(state));
3379 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3381 skb_abort_seq_read(TS_SKB_CB(state));
3385 * skb_find_text - Find a text pattern in skb data
3386 * @skb: the buffer to look in
3387 * @from: search offset
3389 * @config: textsearch configuration
3391 * Finds a pattern in the skb data according to the specified
3392 * textsearch configuration. Use textsearch_next() to retrieve
3393 * subsequent occurrences of the pattern. Returns the offset
3394 * to the first occurrence or UINT_MAX if no match was found.
3396 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3397 unsigned int to, struct ts_config *config)
3399 struct ts_state state;
3402 config->get_next_block = skb_ts_get_next_block;
3403 config->finish = skb_ts_finish;
3405 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3407 ret = textsearch_find(config, &state);
3408 return (ret <= to - from ? ret : UINT_MAX);
3410 EXPORT_SYMBOL(skb_find_text);
3412 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3413 int offset, size_t size)
3415 int i = skb_shinfo(skb)->nr_frags;
3417 if (skb_can_coalesce(skb, i, page, offset)) {
3418 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3419 } else if (i < MAX_SKB_FRAGS) {
3421 skb_fill_page_desc(skb, i, page, offset, size);
3428 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3431 * skb_pull_rcsum - pull skb and update receive checksum
3432 * @skb: buffer to update
3433 * @len: length of data pulled
3435 * This function performs an skb_pull on the packet and updates
3436 * the CHECKSUM_COMPLETE checksum. It should be used on
3437 * receive path processing instead of skb_pull unless you know
3438 * that the checksum difference is zero (e.g., a valid IP header)
3439 * or you are setting ip_summed to CHECKSUM_NONE.
3441 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3443 unsigned char *data = skb->data;
3445 BUG_ON(len > skb->len);
3446 __skb_pull(skb, len);
3447 skb_postpull_rcsum(skb, data, len);
3450 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3452 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3454 skb_frag_t head_frag;
3457 page = virt_to_head_page(frag_skb->head);
3458 head_frag.page.p = page;
3459 head_frag.page_offset = frag_skb->data -
3460 (unsigned char *)page_address(page);
3461 head_frag.size = skb_headlen(frag_skb);
3466 * skb_segment - Perform protocol segmentation on skb.
3467 * @head_skb: buffer to segment
3468 * @features: features for the output path (see dev->features)
3470 * This function performs segmentation on the given skb. It returns
3471 * a pointer to the first in a list of new skbs for the segments.
3472 * In case of error it returns ERR_PTR(err).
3474 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3475 netdev_features_t features)
3477 struct sk_buff *segs = NULL;
3478 struct sk_buff *tail = NULL;
3479 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3480 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3481 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3482 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3483 struct sk_buff *frag_skb = head_skb;
3484 unsigned int offset = doffset;
3485 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3486 unsigned int partial_segs = 0;
3487 unsigned int headroom;
3488 unsigned int len = head_skb->len;
3491 int nfrags = skb_shinfo(head_skb)->nr_frags;
3497 __skb_push(head_skb, doffset);
3498 proto = skb_network_protocol(head_skb, &dummy);
3499 if (unlikely(!proto))
3500 return ERR_PTR(-EINVAL);
3502 sg = !!(features & NETIF_F_SG);
3503 csum = !!can_checksum_protocol(features, proto);
3505 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3506 if (!(features & NETIF_F_GSO_PARTIAL)) {
3507 struct sk_buff *iter;
3508 unsigned int frag_len;
3511 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3514 /* If we get here then all the required
3515 * GSO features except frag_list are supported.
3516 * Try to split the SKB to multiple GSO SKBs
3517 * with no frag_list.
3518 * Currently we can do that only when the buffers don't
3519 * have a linear part and all the buffers except
3520 * the last are of the same length.
3522 frag_len = list_skb->len;
3523 skb_walk_frags(head_skb, iter) {
3524 if (frag_len != iter->len && iter->next)
3526 if (skb_headlen(iter) && !iter->head_frag)
3532 if (len != frag_len)
3536 /* GSO partial only requires that we trim off any excess that
3537 * doesn't fit into an MSS sized block, so take care of that
3540 partial_segs = len / mss;
3541 if (partial_segs > 1)
3542 mss *= partial_segs;
3548 headroom = skb_headroom(head_skb);
3549 pos = skb_headlen(head_skb);
3552 struct sk_buff *nskb;
3553 skb_frag_t *nskb_frag;
3557 if (unlikely(mss == GSO_BY_FRAGS)) {
3558 len = list_skb->len;
3560 len = head_skb->len - offset;
3565 hsize = skb_headlen(head_skb) - offset;
3568 if (hsize > len || !sg)
3571 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3572 (skb_headlen(list_skb) == len || sg)) {
3573 BUG_ON(skb_headlen(list_skb) > len);
3576 nfrags = skb_shinfo(list_skb)->nr_frags;
3577 frag = skb_shinfo(list_skb)->frags;
3578 frag_skb = list_skb;
3579 pos += skb_headlen(list_skb);
3581 while (pos < offset + len) {
3582 BUG_ON(i >= nfrags);
3584 size = skb_frag_size(frag);
3585 if (pos + size > offset + len)
3593 nskb = skb_clone(list_skb, GFP_ATOMIC);
3594 list_skb = list_skb->next;
3596 if (unlikely(!nskb))
3599 if (unlikely(pskb_trim(nskb, len))) {
3604 hsize = skb_end_offset(nskb);
3605 if (skb_cow_head(nskb, doffset + headroom)) {
3610 nskb->truesize += skb_end_offset(nskb) - hsize;
3611 skb_release_head_state(nskb);
3612 __skb_push(nskb, doffset);
3614 nskb = __alloc_skb(hsize + doffset + headroom,
3615 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3618 if (unlikely(!nskb))
3621 skb_reserve(nskb, headroom);
3622 __skb_put(nskb, doffset);
3631 __copy_skb_header(nskb, head_skb);
3633 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3634 skb_reset_mac_len(nskb);
3636 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3637 nskb->data - tnl_hlen,
3638 doffset + tnl_hlen);
3640 if (nskb->len == len + doffset)
3641 goto perform_csum_check;
3644 if (!nskb->remcsum_offload)
3645 nskb->ip_summed = CHECKSUM_NONE;
3646 SKB_GSO_CB(nskb)->csum =
3647 skb_copy_and_csum_bits(head_skb, offset,
3650 SKB_GSO_CB(nskb)->csum_start =
3651 skb_headroom(nskb) + doffset;
3655 nskb_frag = skb_shinfo(nskb)->frags;
3657 skb_copy_from_linear_data_offset(head_skb, offset,
3658 skb_put(nskb, hsize), hsize);
3660 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3663 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3664 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3667 while (pos < offset + len) {
3670 nfrags = skb_shinfo(list_skb)->nr_frags;
3671 frag = skb_shinfo(list_skb)->frags;
3672 frag_skb = list_skb;
3673 if (!skb_headlen(list_skb)) {
3676 BUG_ON(!list_skb->head_frag);
3678 /* to make room for head_frag. */
3682 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3683 skb_zerocopy_clone(nskb, frag_skb,
3687 list_skb = list_skb->next;
3690 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3692 net_warn_ratelimited(
3693 "skb_segment: too many frags: %u %u\n",
3699 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
3700 __skb_frag_ref(nskb_frag);
3701 size = skb_frag_size(nskb_frag);
3704 nskb_frag->page_offset += offset - pos;
3705 skb_frag_size_sub(nskb_frag, offset - pos);
3708 skb_shinfo(nskb)->nr_frags++;
3710 if (pos + size <= offset + len) {
3715 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3723 nskb->data_len = len - hsize;
3724 nskb->len += nskb->data_len;
3725 nskb->truesize += nskb->data_len;
3729 if (skb_has_shared_frag(nskb) &&
3730 __skb_linearize(nskb))
3733 if (!nskb->remcsum_offload)
3734 nskb->ip_summed = CHECKSUM_NONE;
3735 SKB_GSO_CB(nskb)->csum =
3736 skb_checksum(nskb, doffset,
3737 nskb->len - doffset, 0);
3738 SKB_GSO_CB(nskb)->csum_start =
3739 skb_headroom(nskb) + doffset;
3741 } while ((offset += len) < head_skb->len);
3743 /* Some callers want to get the end of the list.
3744 * Put it in segs->prev to avoid walking the list.
3745 * (see validate_xmit_skb_list() for example)
3750 struct sk_buff *iter;
3751 int type = skb_shinfo(head_skb)->gso_type;
3752 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3754 /* Update type to add partial and then remove dodgy if set */
3755 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3756 type &= ~SKB_GSO_DODGY;
3758 /* Update GSO info and prepare to start updating headers on
3759 * our way back down the stack of protocols.
3761 for (iter = segs; iter; iter = iter->next) {
3762 skb_shinfo(iter)->gso_size = gso_size;
3763 skb_shinfo(iter)->gso_segs = partial_segs;
3764 skb_shinfo(iter)->gso_type = type;
3765 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3768 if (tail->len - doffset <= gso_size)
3769 skb_shinfo(tail)->gso_size = 0;
3770 else if (tail != segs)
3771 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3774 /* Following permits correct backpressure, for protocols
3775 * using skb_set_owner_w().
3776 * Idea is to tranfert ownership from head_skb to last segment.
3778 if (head_skb->destructor == sock_wfree) {
3779 swap(tail->truesize, head_skb->truesize);
3780 swap(tail->destructor, head_skb->destructor);
3781 swap(tail->sk, head_skb->sk);
3786 kfree_skb_list(segs);
3787 return ERR_PTR(err);
3789 EXPORT_SYMBOL_GPL(skb_segment);
3791 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
3793 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3794 unsigned int offset = skb_gro_offset(skb);
3795 unsigned int headlen = skb_headlen(skb);
3796 unsigned int len = skb_gro_len(skb);
3797 unsigned int delta_truesize;
3800 if (unlikely(p->len + len >= 65536))
3803 lp = NAPI_GRO_CB(p)->last;
3804 pinfo = skb_shinfo(lp);
3806 if (headlen <= offset) {
3809 int i = skbinfo->nr_frags;
3810 int nr_frags = pinfo->nr_frags + i;
3812 if (nr_frags > MAX_SKB_FRAGS)
3816 pinfo->nr_frags = nr_frags;
3817 skbinfo->nr_frags = 0;
3819 frag = pinfo->frags + nr_frags;
3820 frag2 = skbinfo->frags + i;
3825 frag->page_offset += offset;
3826 skb_frag_size_sub(frag, offset);
3828 /* all fragments truesize : remove (head size + sk_buff) */
3829 delta_truesize = skb->truesize -
3830 SKB_TRUESIZE(skb_end_offset(skb));
3832 skb->truesize -= skb->data_len;
3833 skb->len -= skb->data_len;
3836 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3838 } else if (skb->head_frag) {
3839 int nr_frags = pinfo->nr_frags;
3840 skb_frag_t *frag = pinfo->frags + nr_frags;
3841 struct page *page = virt_to_head_page(skb->head);
3842 unsigned int first_size = headlen - offset;
3843 unsigned int first_offset;
3845 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3848 first_offset = skb->data -
3849 (unsigned char *)page_address(page) +
3852 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3854 frag->page.p = page;
3855 frag->page_offset = first_offset;
3856 skb_frag_size_set(frag, first_size);
3858 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3859 /* We dont need to clear skbinfo->nr_frags here */
3861 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3862 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3867 delta_truesize = skb->truesize;
3868 if (offset > headlen) {
3869 unsigned int eat = offset - headlen;
3871 skbinfo->frags[0].page_offset += eat;
3872 skb_frag_size_sub(&skbinfo->frags[0], eat);
3873 skb->data_len -= eat;
3878 __skb_pull(skb, offset);
3880 if (NAPI_GRO_CB(p)->last == p)
3881 skb_shinfo(p)->frag_list = skb;
3883 NAPI_GRO_CB(p)->last->next = skb;
3884 NAPI_GRO_CB(p)->last = skb;
3885 __skb_header_release(skb);
3889 NAPI_GRO_CB(p)->count++;
3891 p->truesize += delta_truesize;
3894 lp->data_len += len;
3895 lp->truesize += delta_truesize;
3898 NAPI_GRO_CB(skb)->same_flow = 1;
3901 EXPORT_SYMBOL_GPL(skb_gro_receive);
3903 #ifdef CONFIG_SKB_EXTENSIONS
3904 #define SKB_EXT_ALIGN_VALUE 8
3905 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3907 static const u8 skb_ext_type_len[] = {
3908 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3909 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3912 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3916 static __always_inline unsigned int skb_ext_total_length(void)
3918 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3919 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3920 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3923 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3928 static void skb_extensions_init(void)
3930 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3931 BUILD_BUG_ON(skb_ext_total_length() > 255);
3933 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3934 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3936 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3940 static void skb_extensions_init(void) {}
3943 void __init skb_init(void)
3945 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
3946 sizeof(struct sk_buff),
3948 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3949 offsetof(struct sk_buff, cb),
3950 sizeof_field(struct sk_buff, cb),
3952 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3953 sizeof(struct sk_buff_fclones),
3955 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3957 skb_extensions_init();
3961 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3962 unsigned int recursion_level)
3964 int start = skb_headlen(skb);
3965 int i, copy = start - offset;
3966 struct sk_buff *frag_iter;
3969 if (unlikely(recursion_level >= 24))
3975 sg_set_buf(sg, skb->data + offset, copy);
3977 if ((len -= copy) == 0)
3982 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3985 WARN_ON(start > offset + len);
3987 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3988 if ((copy = end - offset) > 0) {
3989 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3990 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3995 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3996 frag->page_offset+offset-start);
4005 skb_walk_frags(skb, frag_iter) {
4008 WARN_ON(start > offset + len);
4010 end = start + frag_iter->len;
4011 if ((copy = end - offset) > 0) {
4012 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4017 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4018 copy, recursion_level + 1);
4019 if (unlikely(ret < 0))
4022 if ((len -= copy) == 0)
4033 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4034 * @skb: Socket buffer containing the buffers to be mapped
4035 * @sg: The scatter-gather list to map into
4036 * @offset: The offset into the buffer's contents to start mapping
4037 * @len: Length of buffer space to be mapped
4039 * Fill the specified scatter-gather list with mappings/pointers into a
4040 * region of the buffer space attached to a socket buffer. Returns either
4041 * the number of scatterlist items used, or -EMSGSIZE if the contents
4044 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4046 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4051 sg_mark_end(&sg[nsg - 1]);
4055 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4057 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4058 * sglist without mark the sg which contain last skb data as the end.
4059 * So the caller can mannipulate sg list as will when padding new data after
4060 * the first call without calling sg_unmark_end to expend sg list.
4062 * Scenario to use skb_to_sgvec_nomark:
4064 * 2. skb_to_sgvec_nomark(payload1)
4065 * 3. skb_to_sgvec_nomark(payload2)
4067 * This is equivalent to:
4069 * 2. skb_to_sgvec(payload1)
4071 * 4. skb_to_sgvec(payload2)
4073 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4074 * is more preferable.
4076 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4077 int offset, int len)
4079 return __skb_to_sgvec(skb, sg, offset, len, 0);
4081 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4086 * skb_cow_data - Check that a socket buffer's data buffers are writable
4087 * @skb: The socket buffer to check.
4088 * @tailbits: Amount of trailing space to be added
4089 * @trailer: Returned pointer to the skb where the @tailbits space begins
4091 * Make sure that the data buffers attached to a socket buffer are
4092 * writable. If they are not, private copies are made of the data buffers
4093 * and the socket buffer is set to use these instead.
4095 * If @tailbits is given, make sure that there is space to write @tailbits
4096 * bytes of data beyond current end of socket buffer. @trailer will be
4097 * set to point to the skb in which this space begins.
4099 * The number of scatterlist elements required to completely map the
4100 * COW'd and extended socket buffer will be returned.
4102 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4106 struct sk_buff *skb1, **skb_p;
4108 /* If skb is cloned or its head is paged, reallocate
4109 * head pulling out all the pages (pages are considered not writable
4110 * at the moment even if they are anonymous).
4112 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4113 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4116 /* Easy case. Most of packets will go this way. */
4117 if (!skb_has_frag_list(skb)) {
4118 /* A little of trouble, not enough of space for trailer.
4119 * This should not happen, when stack is tuned to generate
4120 * good frames. OK, on miss we reallocate and reserve even more
4121 * space, 128 bytes is fair. */
4123 if (skb_tailroom(skb) < tailbits &&
4124 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4132 /* Misery. We are in troubles, going to mincer fragments... */
4135 skb_p = &skb_shinfo(skb)->frag_list;
4138 while ((skb1 = *skb_p) != NULL) {
4141 /* The fragment is partially pulled by someone,
4142 * this can happen on input. Copy it and everything
4145 if (skb_shared(skb1))
4148 /* If the skb is the last, worry about trailer. */
4150 if (skb1->next == NULL && tailbits) {
4151 if (skb_shinfo(skb1)->nr_frags ||
4152 skb_has_frag_list(skb1) ||
4153 skb_tailroom(skb1) < tailbits)
4154 ntail = tailbits + 128;
4160 skb_shinfo(skb1)->nr_frags ||
4161 skb_has_frag_list(skb1)) {
4162 struct sk_buff *skb2;
4164 /* Fuck, we are miserable poor guys... */
4166 skb2 = skb_copy(skb1, GFP_ATOMIC);
4168 skb2 = skb_copy_expand(skb1,
4172 if (unlikely(skb2 == NULL))
4176 skb_set_owner_w(skb2, skb1->sk);
4178 /* Looking around. Are we still alive?
4179 * OK, link new skb, drop old one */
4181 skb2->next = skb1->next;
4188 skb_p = &skb1->next;
4193 EXPORT_SYMBOL_GPL(skb_cow_data);
4195 static void sock_rmem_free(struct sk_buff *skb)
4197 struct sock *sk = skb->sk;
4199 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4202 static void skb_set_err_queue(struct sk_buff *skb)
4204 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4205 * So, it is safe to (mis)use it to mark skbs on the error queue.
4207 skb->pkt_type = PACKET_OUTGOING;
4208 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4212 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4214 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4216 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4217 (unsigned int)sk->sk_rcvbuf)
4222 skb->destructor = sock_rmem_free;
4223 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4224 skb_set_err_queue(skb);
4226 /* before exiting rcu section, make sure dst is refcounted */
4229 skb_queue_tail(&sk->sk_error_queue, skb);
4230 if (!sock_flag(sk, SOCK_DEAD))
4231 sk->sk_error_report(sk);
4234 EXPORT_SYMBOL(sock_queue_err_skb);
4236 static bool is_icmp_err_skb(const struct sk_buff *skb)
4238 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4239 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4242 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4244 struct sk_buff_head *q = &sk->sk_error_queue;
4245 struct sk_buff *skb, *skb_next = NULL;
4246 bool icmp_next = false;
4247 unsigned long flags;
4249 spin_lock_irqsave(&q->lock, flags);
4250 skb = __skb_dequeue(q);
4251 if (skb && (skb_next = skb_peek(q))) {
4252 icmp_next = is_icmp_err_skb(skb_next);
4254 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4256 spin_unlock_irqrestore(&q->lock, flags);
4258 if (is_icmp_err_skb(skb) && !icmp_next)
4262 sk->sk_error_report(sk);
4266 EXPORT_SYMBOL(sock_dequeue_err_skb);
4269 * skb_clone_sk - create clone of skb, and take reference to socket
4270 * @skb: the skb to clone
4272 * This function creates a clone of a buffer that holds a reference on
4273 * sk_refcnt. Buffers created via this function are meant to be
4274 * returned using sock_queue_err_skb, or free via kfree_skb.
4276 * When passing buffers allocated with this function to sock_queue_err_skb
4277 * it is necessary to wrap the call with sock_hold/sock_put in order to
4278 * prevent the socket from being released prior to being enqueued on
4279 * the sk_error_queue.
4281 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4283 struct sock *sk = skb->sk;
4284 struct sk_buff *clone;
4286 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4289 clone = skb_clone(skb, GFP_ATOMIC);
4296 clone->destructor = sock_efree;
4300 EXPORT_SYMBOL(skb_clone_sk);
4302 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4307 struct sock_exterr_skb *serr;
4310 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4312 serr = SKB_EXT_ERR(skb);
4313 memset(serr, 0, sizeof(*serr));
4314 serr->ee.ee_errno = ENOMSG;
4315 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4316 serr->ee.ee_info = tstype;
4317 serr->opt_stats = opt_stats;
4318 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4319 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4320 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4321 if (sk->sk_protocol == IPPROTO_TCP &&
4322 sk->sk_type == SOCK_STREAM)
4323 serr->ee.ee_data -= sk->sk_tskey;
4326 err = sock_queue_err_skb(sk, skb);
4332 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4336 if (likely(sysctl_tstamp_allow_data || tsonly))
4339 read_lock_bh(&sk->sk_callback_lock);
4340 ret = sk->sk_socket && sk->sk_socket->file &&
4341 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4342 read_unlock_bh(&sk->sk_callback_lock);
4346 void skb_complete_tx_timestamp(struct sk_buff *skb,
4347 struct skb_shared_hwtstamps *hwtstamps)
4349 struct sock *sk = skb->sk;
4351 if (!skb_may_tx_timestamp(sk, false))
4354 /* Take a reference to prevent skb_orphan() from freeing the socket,
4355 * but only if the socket refcount is not zero.
4357 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4358 *skb_hwtstamps(skb) = *hwtstamps;
4359 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4367 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4369 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4370 struct skb_shared_hwtstamps *hwtstamps,
4371 struct sock *sk, int tstype)
4373 struct sk_buff *skb;
4374 bool tsonly, opt_stats = false;
4379 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4380 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4383 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4384 if (!skb_may_tx_timestamp(sk, tsonly))
4389 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4390 sk->sk_protocol == IPPROTO_TCP &&
4391 sk->sk_type == SOCK_STREAM) {
4392 skb = tcp_get_timestamping_opt_stats(sk);
4396 skb = alloc_skb(0, GFP_ATOMIC);
4398 skb = skb_clone(orig_skb, GFP_ATOMIC);
4404 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4406 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4410 *skb_hwtstamps(skb) = *hwtstamps;
4412 skb->tstamp = ktime_get_real();
4414 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4416 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4418 void skb_tstamp_tx(struct sk_buff *orig_skb,
4419 struct skb_shared_hwtstamps *hwtstamps)
4421 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4424 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4426 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4428 struct sock *sk = skb->sk;
4429 struct sock_exterr_skb *serr;
4432 skb->wifi_acked_valid = 1;
4433 skb->wifi_acked = acked;
4435 serr = SKB_EXT_ERR(skb);
4436 memset(serr, 0, sizeof(*serr));
4437 serr->ee.ee_errno = ENOMSG;
4438 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4440 /* Take a reference to prevent skb_orphan() from freeing the socket,
4441 * but only if the socket refcount is not zero.
4443 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4444 err = sock_queue_err_skb(sk, skb);
4450 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4453 * skb_partial_csum_set - set up and verify partial csum values for packet
4454 * @skb: the skb to set
4455 * @start: the number of bytes after skb->data to start checksumming.
4456 * @off: the offset from start to place the checksum.
4458 * For untrusted partially-checksummed packets, we need to make sure the values
4459 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4461 * This function checks and sets those values and skb->ip_summed: if this
4462 * returns false you should drop the packet.
4464 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4466 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4467 u32 csum_start = skb_headroom(skb) + (u32)start;
4469 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4470 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4471 start, off, skb_headroom(skb), skb_headlen(skb));
4474 skb->ip_summed = CHECKSUM_PARTIAL;
4475 skb->csum_start = csum_start;
4476 skb->csum_offset = off;
4477 skb_set_transport_header(skb, start);
4480 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4482 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4485 if (skb_headlen(skb) >= len)
4488 /* If we need to pullup then pullup to the max, so we
4489 * won't need to do it again.
4494 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4497 if (skb_headlen(skb) < len)
4503 #define MAX_TCP_HDR_LEN (15 * 4)
4505 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4506 typeof(IPPROTO_IP) proto,
4513 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4514 off + MAX_TCP_HDR_LEN);
4515 if (!err && !skb_partial_csum_set(skb, off,
4516 offsetof(struct tcphdr,
4519 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4522 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4523 off + sizeof(struct udphdr));
4524 if (!err && !skb_partial_csum_set(skb, off,
4525 offsetof(struct udphdr,
4528 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4531 return ERR_PTR(-EPROTO);
4534 /* This value should be large enough to cover a tagged ethernet header plus
4535 * maximally sized IP and TCP or UDP headers.
4537 #define MAX_IP_HDR_LEN 128
4539 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4548 err = skb_maybe_pull_tail(skb,
4549 sizeof(struct iphdr),
4554 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4557 off = ip_hdrlen(skb);
4564 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4566 return PTR_ERR(csum);
4569 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4572 ip_hdr(skb)->protocol, 0);
4579 /* This value should be large enough to cover a tagged ethernet header plus
4580 * an IPv6 header, all options, and a maximal TCP or UDP header.
4582 #define MAX_IPV6_HDR_LEN 256
4584 #define OPT_HDR(type, skb, off) \
4585 (type *)(skb_network_header(skb) + (off))
4587 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4600 off = sizeof(struct ipv6hdr);
4602 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4606 nexthdr = ipv6_hdr(skb)->nexthdr;
4608 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4609 while (off <= len && !done) {
4611 case IPPROTO_DSTOPTS:
4612 case IPPROTO_HOPOPTS:
4613 case IPPROTO_ROUTING: {
4614 struct ipv6_opt_hdr *hp;
4616 err = skb_maybe_pull_tail(skb,
4618 sizeof(struct ipv6_opt_hdr),
4623 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4624 nexthdr = hp->nexthdr;
4625 off += ipv6_optlen(hp);
4629 struct ip_auth_hdr *hp;
4631 err = skb_maybe_pull_tail(skb,
4633 sizeof(struct ip_auth_hdr),
4638 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4639 nexthdr = hp->nexthdr;
4640 off += ipv6_authlen(hp);
4643 case IPPROTO_FRAGMENT: {
4644 struct frag_hdr *hp;
4646 err = skb_maybe_pull_tail(skb,
4648 sizeof(struct frag_hdr),
4653 hp = OPT_HDR(struct frag_hdr, skb, off);
4655 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4658 nexthdr = hp->nexthdr;
4659 off += sizeof(struct frag_hdr);
4670 if (!done || fragment)
4673 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4675 return PTR_ERR(csum);
4678 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4679 &ipv6_hdr(skb)->daddr,
4680 skb->len - off, nexthdr, 0);
4688 * skb_checksum_setup - set up partial checksum offset
4689 * @skb: the skb to set up
4690 * @recalculate: if true the pseudo-header checksum will be recalculated
4692 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4696 switch (skb->protocol) {
4697 case htons(ETH_P_IP):
4698 err = skb_checksum_setup_ipv4(skb, recalculate);
4701 case htons(ETH_P_IPV6):
4702 err = skb_checksum_setup_ipv6(skb, recalculate);
4712 EXPORT_SYMBOL(skb_checksum_setup);
4715 * skb_checksum_maybe_trim - maybe trims the given skb
4716 * @skb: the skb to check
4717 * @transport_len: the data length beyond the network header
4719 * Checks whether the given skb has data beyond the given transport length.
4720 * If so, returns a cloned skb trimmed to this transport length.
4721 * Otherwise returns the provided skb. Returns NULL in error cases
4722 * (e.g. transport_len exceeds skb length or out-of-memory).
4724 * Caller needs to set the skb transport header and free any returned skb if it
4725 * differs from the provided skb.
4727 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4728 unsigned int transport_len)
4730 struct sk_buff *skb_chk;
4731 unsigned int len = skb_transport_offset(skb) + transport_len;
4736 else if (skb->len == len)
4739 skb_chk = skb_clone(skb, GFP_ATOMIC);
4743 ret = pskb_trim_rcsum(skb_chk, len);
4753 * skb_checksum_trimmed - validate checksum of an skb
4754 * @skb: the skb to check
4755 * @transport_len: the data length beyond the network header
4756 * @skb_chkf: checksum function to use
4758 * Applies the given checksum function skb_chkf to the provided skb.
4759 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4761 * If the skb has data beyond the given transport length, then a
4762 * trimmed & cloned skb is checked and returned.
4764 * Caller needs to set the skb transport header and free any returned skb if it
4765 * differs from the provided skb.
4767 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4768 unsigned int transport_len,
4769 __sum16(*skb_chkf)(struct sk_buff *skb))
4771 struct sk_buff *skb_chk;
4772 unsigned int offset = skb_transport_offset(skb);
4775 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4779 if (!pskb_may_pull(skb_chk, offset))
4782 skb_pull_rcsum(skb_chk, offset);
4783 ret = skb_chkf(skb_chk);
4784 skb_push_rcsum(skb_chk, offset);
4792 if (skb_chk && skb_chk != skb)
4798 EXPORT_SYMBOL(skb_checksum_trimmed);
4800 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4802 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4805 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4807 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4810 skb_release_head_state(skb);
4811 kmem_cache_free(skbuff_head_cache, skb);
4816 EXPORT_SYMBOL(kfree_skb_partial);
4819 * skb_try_coalesce - try to merge skb to prior one
4821 * @from: buffer to add
4822 * @fragstolen: pointer to boolean
4823 * @delta_truesize: how much more was allocated than was requested
4825 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4826 bool *fragstolen, int *delta_truesize)
4828 struct skb_shared_info *to_shinfo, *from_shinfo;
4829 int i, delta, len = from->len;
4831 *fragstolen = false;
4836 if (len <= skb_tailroom(to)) {
4838 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4839 *delta_truesize = 0;
4843 to_shinfo = skb_shinfo(to);
4844 from_shinfo = skb_shinfo(from);
4845 if (to_shinfo->frag_list || from_shinfo->frag_list)
4847 if (skb_zcopy(to) || skb_zcopy(from))
4850 if (skb_headlen(from) != 0) {
4852 unsigned int offset;
4854 if (to_shinfo->nr_frags +
4855 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
4858 if (skb_head_is_locked(from))
4861 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4863 page = virt_to_head_page(from->head);
4864 offset = from->data - (unsigned char *)page_address(page);
4866 skb_fill_page_desc(to, to_shinfo->nr_frags,
4867 page, offset, skb_headlen(from));
4870 if (to_shinfo->nr_frags +
4871 from_shinfo->nr_frags > MAX_SKB_FRAGS)
4874 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4877 WARN_ON_ONCE(delta < len);
4879 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4881 from_shinfo->nr_frags * sizeof(skb_frag_t));
4882 to_shinfo->nr_frags += from_shinfo->nr_frags;
4884 if (!skb_cloned(from))
4885 from_shinfo->nr_frags = 0;
4887 /* if the skb is not cloned this does nothing
4888 * since we set nr_frags to 0.
4890 for (i = 0; i < from_shinfo->nr_frags; i++)
4891 __skb_frag_ref(&from_shinfo->frags[i]);
4893 to->truesize += delta;
4895 to->data_len += len;
4897 *delta_truesize = delta;
4900 EXPORT_SYMBOL(skb_try_coalesce);
4903 * skb_scrub_packet - scrub an skb
4905 * @skb: buffer to clean
4906 * @xnet: packet is crossing netns
4908 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4909 * into/from a tunnel. Some information have to be cleared during these
4911 * skb_scrub_packet can also be used to clean a skb before injecting it in
4912 * another namespace (@xnet == true). We have to clear all information in the
4913 * skb that could impact namespace isolation.
4915 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4917 skb->pkt_type = PACKET_HOST;
4923 nf_reset_trace(skb);
4925 #ifdef CONFIG_NET_SWITCHDEV
4926 skb->offload_fwd_mark = 0;
4927 skb->offload_l3_fwd_mark = 0;
4937 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4940 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4944 * skb_gso_transport_seglen is used to determine the real size of the
4945 * individual segments, including Layer4 headers (TCP/UDP).
4947 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4949 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4951 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4952 unsigned int thlen = 0;
4954 if (skb->encapsulation) {
4955 thlen = skb_inner_transport_header(skb) -
4956 skb_transport_header(skb);
4958 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4959 thlen += inner_tcp_hdrlen(skb);
4960 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4961 thlen = tcp_hdrlen(skb);
4962 } else if (unlikely(skb_is_gso_sctp(skb))) {
4963 thlen = sizeof(struct sctphdr);
4964 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
4965 thlen = sizeof(struct udphdr);
4967 /* UFO sets gso_size to the size of the fragmentation
4968 * payload, i.e. the size of the L4 (UDP) header is already
4971 return thlen + shinfo->gso_size;
4975 * skb_gso_network_seglen - Return length of individual segments of a gso packet
4979 * skb_gso_network_seglen is used to determine the real size of the
4980 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
4982 * The MAC/L2 header is not accounted for.
4984 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
4986 unsigned int hdr_len = skb_transport_header(skb) -
4987 skb_network_header(skb);
4989 return hdr_len + skb_gso_transport_seglen(skb);
4993 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
4997 * skb_gso_mac_seglen is used to determine the real size of the
4998 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
4999 * headers (TCP/UDP).
5001 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5003 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5005 return hdr_len + skb_gso_transport_seglen(skb);
5009 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5011 * There are a couple of instances where we have a GSO skb, and we
5012 * want to determine what size it would be after it is segmented.
5014 * We might want to check:
5015 * - L3+L4+payload size (e.g. IP forwarding)
5016 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5018 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5022 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5023 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5025 * @max_len: The maximum permissible length.
5027 * Returns true if the segmented length <= max length.
5029 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5030 unsigned int seg_len,
5031 unsigned int max_len) {
5032 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5033 const struct sk_buff *iter;
5035 if (shinfo->gso_size != GSO_BY_FRAGS)
5036 return seg_len <= max_len;
5038 /* Undo this so we can re-use header sizes */
5039 seg_len -= GSO_BY_FRAGS;
5041 skb_walk_frags(skb, iter) {
5042 if (seg_len + skb_headlen(iter) > max_len)
5050 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5053 * @mtu: MTU to validate against
5055 * skb_gso_validate_network_len validates if a given skb will fit a
5056 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5059 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5061 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5063 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5066 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5069 * @len: length to validate against
5071 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5072 * length once split, including L2, L3 and L4 headers and the payload.
5074 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5076 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5078 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5080 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5084 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5089 mac_len = skb->data - skb_mac_header(skb);
5090 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5091 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5092 mac_len - VLAN_HLEN - ETH_TLEN);
5094 skb->mac_header += VLAN_HLEN;
5098 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5100 struct vlan_hdr *vhdr;
5103 if (unlikely(skb_vlan_tag_present(skb))) {
5104 /* vlan_tci is already set-up so leave this for another time */
5108 skb = skb_share_check(skb, GFP_ATOMIC);
5112 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5115 vhdr = (struct vlan_hdr *)skb->data;
5116 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5117 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5119 skb_pull_rcsum(skb, VLAN_HLEN);
5120 vlan_set_encap_proto(skb, vhdr);
5122 skb = skb_reorder_vlan_header(skb);
5126 skb_reset_network_header(skb);
5127 skb_reset_transport_header(skb);
5128 skb_reset_mac_len(skb);
5136 EXPORT_SYMBOL(skb_vlan_untag);
5138 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5140 if (!pskb_may_pull(skb, write_len))
5143 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5146 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5148 EXPORT_SYMBOL(skb_ensure_writable);
5150 /* remove VLAN header from packet and update csum accordingly.
5151 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5153 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5155 struct vlan_hdr *vhdr;
5156 int offset = skb->data - skb_mac_header(skb);
5159 if (WARN_ONCE(offset,
5160 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5165 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5169 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5171 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5172 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5174 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5175 __skb_pull(skb, VLAN_HLEN);
5177 vlan_set_encap_proto(skb, vhdr);
5178 skb->mac_header += VLAN_HLEN;
5180 if (skb_network_offset(skb) < ETH_HLEN)
5181 skb_set_network_header(skb, ETH_HLEN);
5183 skb_reset_mac_len(skb);
5187 EXPORT_SYMBOL(__skb_vlan_pop);
5189 /* Pop a vlan tag either from hwaccel or from payload.
5190 * Expects skb->data at mac header.
5192 int skb_vlan_pop(struct sk_buff *skb)
5198 if (likely(skb_vlan_tag_present(skb))) {
5199 __vlan_hwaccel_clear_tag(skb);
5201 if (unlikely(!eth_type_vlan(skb->protocol)))
5204 err = __skb_vlan_pop(skb, &vlan_tci);
5208 /* move next vlan tag to hw accel tag */
5209 if (likely(!eth_type_vlan(skb->protocol)))
5212 vlan_proto = skb->protocol;
5213 err = __skb_vlan_pop(skb, &vlan_tci);
5217 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5220 EXPORT_SYMBOL(skb_vlan_pop);
5222 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5223 * Expects skb->data at mac header.
5225 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5227 if (skb_vlan_tag_present(skb)) {
5228 int offset = skb->data - skb_mac_header(skb);
5231 if (WARN_ONCE(offset,
5232 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5237 err = __vlan_insert_tag(skb, skb->vlan_proto,
5238 skb_vlan_tag_get(skb));
5242 skb->protocol = skb->vlan_proto;
5243 skb->mac_len += VLAN_HLEN;
5245 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5247 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5250 EXPORT_SYMBOL(skb_vlan_push);
5253 * alloc_skb_with_frags - allocate skb with page frags
5255 * @header_len: size of linear part
5256 * @data_len: needed length in frags
5257 * @max_page_order: max page order desired.
5258 * @errcode: pointer to error code if any
5259 * @gfp_mask: allocation mask
5261 * This can be used to allocate a paged skb, given a maximal order for frags.
5263 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5264 unsigned long data_len,
5269 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5270 unsigned long chunk;
5271 struct sk_buff *skb;
5275 *errcode = -EMSGSIZE;
5276 /* Note this test could be relaxed, if we succeed to allocate
5277 * high order pages...
5279 if (npages > MAX_SKB_FRAGS)
5282 *errcode = -ENOBUFS;
5283 skb = alloc_skb(header_len, gfp_mask);
5287 skb->truesize += npages << PAGE_SHIFT;
5289 for (i = 0; npages > 0; i++) {
5290 int order = max_page_order;
5293 if (npages >= 1 << order) {
5294 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
5300 /* Do not retry other high order allocations */
5306 page = alloc_page(gfp_mask);
5310 chunk = min_t(unsigned long, data_len,
5311 PAGE_SIZE << order);
5312 skb_fill_page_desc(skb, i, page, 0, chunk);
5314 npages -= 1 << order;
5322 EXPORT_SYMBOL(alloc_skb_with_frags);
5324 /* carve out the first off bytes from skb when off < headlen */
5325 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5326 const int headlen, gfp_t gfp_mask)
5329 int size = skb_end_offset(skb);
5330 int new_hlen = headlen - off;
5333 size = SKB_DATA_ALIGN(size);
5335 if (skb_pfmemalloc(skb))
5336 gfp_mask |= __GFP_MEMALLOC;
5337 data = kmalloc_reserve(size +
5338 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5339 gfp_mask, NUMA_NO_NODE, NULL);
5343 size = SKB_WITH_OVERHEAD(ksize(data));
5345 /* Copy real data, and all frags */
5346 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5349 memcpy((struct skb_shared_info *)(data + size),
5351 offsetof(struct skb_shared_info,
5352 frags[skb_shinfo(skb)->nr_frags]));
5353 if (skb_cloned(skb)) {
5354 /* drop the old head gracefully */
5355 if (skb_orphan_frags(skb, gfp_mask)) {
5359 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5360 skb_frag_ref(skb, i);
5361 if (skb_has_frag_list(skb))
5362 skb_clone_fraglist(skb);
5363 skb_release_data(skb);
5365 /* we can reuse existing recount- all we did was
5374 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5377 skb->end = skb->head + size;
5379 skb_set_tail_pointer(skb, skb_headlen(skb));
5380 skb_headers_offset_update(skb, 0);
5384 atomic_set(&skb_shinfo(skb)->dataref, 1);
5389 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5391 /* carve out the first eat bytes from skb's frag_list. May recurse into
5394 static int pskb_carve_frag_list(struct sk_buff *skb,
5395 struct skb_shared_info *shinfo, int eat,
5398 struct sk_buff *list = shinfo->frag_list;
5399 struct sk_buff *clone = NULL;
5400 struct sk_buff *insp = NULL;
5404 pr_err("Not enough bytes to eat. Want %d\n", eat);
5407 if (list->len <= eat) {
5408 /* Eaten as whole. */
5413 /* Eaten partially. */
5414 if (skb_shared(list)) {
5415 clone = skb_clone(list, gfp_mask);
5421 /* This may be pulled without problems. */
5424 if (pskb_carve(list, eat, gfp_mask) < 0) {
5432 /* Free pulled out fragments. */
5433 while ((list = shinfo->frag_list) != insp) {
5434 shinfo->frag_list = list->next;
5437 /* And insert new clone at head. */
5440 shinfo->frag_list = clone;
5445 /* carve off first len bytes from skb. Split line (off) is in the
5446 * non-linear part of skb
5448 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5449 int pos, gfp_t gfp_mask)
5452 int size = skb_end_offset(skb);
5454 const int nfrags = skb_shinfo(skb)->nr_frags;
5455 struct skb_shared_info *shinfo;
5457 size = SKB_DATA_ALIGN(size);
5459 if (skb_pfmemalloc(skb))
5460 gfp_mask |= __GFP_MEMALLOC;
5461 data = kmalloc_reserve(size +
5462 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5463 gfp_mask, NUMA_NO_NODE, NULL);
5467 size = SKB_WITH_OVERHEAD(ksize(data));
5469 memcpy((struct skb_shared_info *)(data + size),
5470 skb_shinfo(skb), offsetof(struct skb_shared_info,
5471 frags[skb_shinfo(skb)->nr_frags]));
5472 if (skb_orphan_frags(skb, gfp_mask)) {
5476 shinfo = (struct skb_shared_info *)(data + size);
5477 for (i = 0; i < nfrags; i++) {
5478 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5480 if (pos + fsize > off) {
5481 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5485 * We have two variants in this case:
5486 * 1. Move all the frag to the second
5487 * part, if it is possible. F.e.
5488 * this approach is mandatory for TUX,
5489 * where splitting is expensive.
5490 * 2. Split is accurately. We make this.
5492 shinfo->frags[0].page_offset += off - pos;
5493 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5495 skb_frag_ref(skb, i);
5500 shinfo->nr_frags = k;
5501 if (skb_has_frag_list(skb))
5502 skb_clone_fraglist(skb);
5505 /* split line is in frag list */
5506 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5508 skb_release_data(skb);
5513 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5516 skb->end = skb->head + size;
5518 skb_reset_tail_pointer(skb);
5519 skb_headers_offset_update(skb, 0);
5524 skb->data_len = skb->len;
5525 atomic_set(&skb_shinfo(skb)->dataref, 1);
5529 /* remove len bytes from the beginning of the skb */
5530 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5532 int headlen = skb_headlen(skb);
5535 return pskb_carve_inside_header(skb, len, headlen, gfp);
5537 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5540 /* Extract to_copy bytes starting at off from skb, and return this in
5543 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5544 int to_copy, gfp_t gfp)
5546 struct sk_buff *clone = skb_clone(skb, gfp);
5551 if (pskb_carve(clone, off, gfp) < 0 ||
5552 pskb_trim(clone, to_copy)) {
5558 EXPORT_SYMBOL(pskb_extract);
5561 * skb_condense - try to get rid of fragments/frag_list if possible
5564 * Can be used to save memory before skb is added to a busy queue.
5565 * If packet has bytes in frags and enough tail room in skb->head,
5566 * pull all of them, so that we can free the frags right now and adjust
5569 * We do not reallocate skb->head thus can not fail.
5570 * Caller must re-evaluate skb->truesize if needed.
5572 void skb_condense(struct sk_buff *skb)
5574 if (skb->data_len) {
5575 if (skb->data_len > skb->end - skb->tail ||
5579 /* Nice, we can free page frag(s) right now */
5580 __pskb_pull_tail(skb, skb->data_len);
5582 /* At this point, skb->truesize might be over estimated,
5583 * because skb had a fragment, and fragments do not tell
5585 * When we pulled its content into skb->head, fragment
5586 * was freed, but __pskb_pull_tail() could not possibly
5587 * adjust skb->truesize, not knowing the frag truesize.
5589 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5592 #ifdef CONFIG_SKB_EXTENSIONS
5593 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5595 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5598 static struct skb_ext *skb_ext_alloc(void)
5600 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5603 memset(new->offset, 0, sizeof(new->offset));
5604 refcount_set(&new->refcnt, 1);
5610 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5611 unsigned int old_active)
5613 struct skb_ext *new;
5615 if (refcount_read(&old->refcnt) == 1)
5618 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5622 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5623 refcount_set(&new->refcnt, 1);
5626 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5627 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5630 for (i = 0; i < sp->len; i++)
5631 xfrm_state_hold(sp->xvec[i]);
5639 * skb_ext_add - allocate space for given extension, COW if needed
5641 * @id: extension to allocate space for
5643 * Allocates enough space for the given extension.
5644 * If the extension is already present, a pointer to that extension
5647 * If the skb was cloned, COW applies and the returned memory can be
5648 * modified without changing the extension space of clones buffers.
5650 * Returns pointer to the extension or NULL on allocation failure.
5652 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5654 struct skb_ext *new, *old = NULL;
5655 unsigned int newlen, newoff;
5657 if (skb->active_extensions) {
5658 old = skb->extensions;
5660 new = skb_ext_maybe_cow(old, skb->active_extensions);
5664 if (__skb_ext_exist(new, id))
5667 newoff = new->chunks;
5669 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5671 new = skb_ext_alloc();
5676 newlen = newoff + skb_ext_type_len[id];
5677 new->chunks = newlen;
5678 new->offset[id] = newoff;
5680 skb->extensions = new;
5681 skb->active_extensions |= 1 << id;
5682 return skb_ext_get_ptr(new, id);
5684 EXPORT_SYMBOL(skb_ext_add);
5687 static void skb_ext_put_sp(struct sec_path *sp)
5691 for (i = 0; i < sp->len; i++)
5692 xfrm_state_put(sp->xvec[i]);
5696 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5698 struct skb_ext *ext = skb->extensions;
5700 skb->active_extensions &= ~(1 << id);
5701 if (skb->active_extensions == 0) {
5702 skb->extensions = NULL;
5705 } else if (id == SKB_EXT_SEC_PATH &&
5706 refcount_read(&ext->refcnt) == 1) {
5707 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5714 EXPORT_SYMBOL(__skb_ext_del);
5716 void __skb_ext_put(struct skb_ext *ext)
5718 /* If this is last clone, nothing can increment
5719 * it after check passes. Avoids one atomic op.
5721 if (refcount_read(&ext->refcnt) == 1)
5724 if (!refcount_dec_and_test(&ext->refcnt))
5728 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5729 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5732 kmem_cache_free(skbuff_ext_cache, ext);
5734 EXPORT_SYMBOL(__skb_ext_put);
5735 #endif /* CONFIG_SKB_EXTENSIONS */