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>
82 struct kmem_cache *skbuff_head_cache __ro_after_init;
83 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
84 #ifdef CONFIG_SKB_EXTENSIONS
85 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
87 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
88 EXPORT_SYMBOL(sysctl_max_skb_frags);
91 * skb_panic - private function for out-of-line support
95 * @msg: skb_over_panic or skb_under_panic
97 * Out-of-line support for skb_put() and skb_push().
98 * Called via the wrapper skb_over_panic() or skb_under_panic().
99 * Keep out of line to prevent kernel bloat.
100 * __builtin_return_address is not used because it is not always reliable.
102 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
105 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
106 msg, addr, skb->len, sz, skb->head, skb->data,
107 (unsigned long)skb->tail, (unsigned long)skb->end,
108 skb->dev ? skb->dev->name : "<NULL>");
112 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114 skb_panic(skb, sz, addr, __func__);
117 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
119 skb_panic(skb, sz, addr, __func__);
123 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
124 * the caller if emergency pfmemalloc reserves are being used. If it is and
125 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
126 * may be used. Otherwise, the packet data may be discarded until enough
129 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
130 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
132 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
133 unsigned long ip, bool *pfmemalloc)
136 bool ret_pfmemalloc = false;
139 * Try a regular allocation, when that fails and we're not entitled
140 * to the reserves, fail.
142 obj = kmalloc_node_track_caller(size,
143 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
145 if (obj || !(gfp_pfmemalloc_allowed(flags)))
148 /* Try again but now we are using pfmemalloc reserves */
149 ret_pfmemalloc = true;
150 obj = kmalloc_node_track_caller(size, flags, node);
154 *pfmemalloc = ret_pfmemalloc;
159 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
160 * 'private' fields and also do memory statistics to find all the
166 * __alloc_skb - allocate a network buffer
167 * @size: size to allocate
168 * @gfp_mask: allocation mask
169 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
170 * instead of head cache and allocate a cloned (child) skb.
171 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
172 * allocations in case the data is required for writeback
173 * @node: numa node to allocate memory on
175 * Allocate a new &sk_buff. The returned buffer has no headroom and a
176 * tail room of at least size bytes. The object has a reference count
177 * of one. The return is the buffer. On a failure the return is %NULL.
179 * Buffers may only be allocated from interrupts using a @gfp_mask of
182 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
185 struct kmem_cache *cache;
186 struct skb_shared_info *shinfo;
191 cache = (flags & SKB_ALLOC_FCLONE)
192 ? skbuff_fclone_cache : skbuff_head_cache;
194 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
195 gfp_mask |= __GFP_MEMALLOC;
198 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
203 /* We do our best to align skb_shared_info on a separate cache
204 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
205 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
206 * Both skb->head and skb_shared_info are cache line aligned.
208 size = SKB_DATA_ALIGN(size);
209 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
210 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
213 /* kmalloc(size) might give us more room than requested.
214 * Put skb_shared_info exactly at the end of allocated zone,
215 * to allow max possible filling before reallocation.
217 size = SKB_WITH_OVERHEAD(ksize(data));
218 prefetchw(data + size);
221 * Only clear those fields we need to clear, not those that we will
222 * actually initialise below. Hence, don't put any more fields after
223 * the tail pointer in struct sk_buff!
225 memset(skb, 0, offsetof(struct sk_buff, tail));
226 /* Account for allocated memory : skb + skb->head */
227 skb->truesize = SKB_TRUESIZE(size);
228 skb->pfmemalloc = pfmemalloc;
229 refcount_set(&skb->users, 1);
232 skb_reset_tail_pointer(skb);
233 skb->end = skb->tail + size;
234 skb->mac_header = (typeof(skb->mac_header))~0U;
235 skb->transport_header = (typeof(skb->transport_header))~0U;
237 /* make sure we initialize shinfo sequentially */
238 shinfo = skb_shinfo(skb);
239 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
240 atomic_set(&shinfo->dataref, 1);
242 if (flags & SKB_ALLOC_FCLONE) {
243 struct sk_buff_fclones *fclones;
245 fclones = container_of(skb, struct sk_buff_fclones, skb1);
247 skb->fclone = SKB_FCLONE_ORIG;
248 refcount_set(&fclones->fclone_ref, 1);
250 fclones->skb2.fclone = SKB_FCLONE_CLONE;
255 kmem_cache_free(cache, skb);
259 EXPORT_SYMBOL(__alloc_skb);
262 * __build_skb - build a network buffer
263 * @data: data buffer provided by caller
264 * @frag_size: size of data, or 0 if head was kmalloced
266 * Allocate a new &sk_buff. Caller provides space holding head and
267 * skb_shared_info. @data must have been allocated by kmalloc() only if
268 * @frag_size is 0, otherwise data should come from the page allocator
270 * The return is the new skb buffer.
271 * On a failure the return is %NULL, and @data is not freed.
273 * Before IO, driver allocates only data buffer where NIC put incoming frame
274 * Driver should add room at head (NET_SKB_PAD) and
275 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
276 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
277 * before giving packet to stack.
278 * RX rings only contains data buffers, not full skbs.
280 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
282 struct skb_shared_info *shinfo;
284 unsigned int size = frag_size ? : ksize(data);
286 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
290 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
292 memset(skb, 0, offsetof(struct sk_buff, tail));
293 skb->truesize = SKB_TRUESIZE(size);
294 refcount_set(&skb->users, 1);
297 skb_reset_tail_pointer(skb);
298 skb->end = skb->tail + size;
299 skb->mac_header = (typeof(skb->mac_header))~0U;
300 skb->transport_header = (typeof(skb->transport_header))~0U;
302 /* make sure we initialize shinfo sequentially */
303 shinfo = skb_shinfo(skb);
304 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
305 atomic_set(&shinfo->dataref, 1);
310 /* build_skb() is wrapper over __build_skb(), that specifically
311 * takes care of skb->head and skb->pfmemalloc
312 * This means that if @frag_size is not zero, then @data must be backed
313 * by a page fragment, not kmalloc() or vmalloc()
315 struct sk_buff *build_skb(void *data, unsigned int frag_size)
317 struct sk_buff *skb = __build_skb(data, frag_size);
319 if (skb && frag_size) {
321 if (page_is_pfmemalloc(virt_to_head_page(data)))
326 EXPORT_SYMBOL(build_skb);
328 #define NAPI_SKB_CACHE_SIZE 64
330 struct napi_alloc_cache {
331 struct page_frag_cache page;
332 unsigned int skb_count;
333 void *skb_cache[NAPI_SKB_CACHE_SIZE];
336 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
337 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
339 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
341 struct page_frag_cache *nc;
345 local_irq_save(flags);
346 nc = this_cpu_ptr(&netdev_alloc_cache);
347 data = page_frag_alloc(nc, fragsz, gfp_mask);
348 local_irq_restore(flags);
353 * netdev_alloc_frag - allocate a page fragment
354 * @fragsz: fragment size
356 * Allocates a frag from a page for receive buffer.
357 * Uses GFP_ATOMIC allocations.
359 void *netdev_alloc_frag(unsigned int fragsz)
361 fragsz = SKB_DATA_ALIGN(fragsz);
363 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
365 EXPORT_SYMBOL(netdev_alloc_frag);
367 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
369 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
371 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
374 void *napi_alloc_frag(unsigned int fragsz)
376 fragsz = SKB_DATA_ALIGN(fragsz);
378 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
380 EXPORT_SYMBOL(napi_alloc_frag);
383 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
384 * @dev: network device to receive on
385 * @len: length to allocate
386 * @gfp_mask: get_free_pages mask, passed to alloc_skb
388 * Allocate a new &sk_buff and assign it a usage count of one. The
389 * buffer has NET_SKB_PAD headroom built in. Users should allocate
390 * the headroom they think they need without accounting for the
391 * built in space. The built in space is used for optimisations.
393 * %NULL is returned if there is no free memory.
395 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
398 struct page_frag_cache *nc;
406 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
407 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
408 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
414 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
415 len = SKB_DATA_ALIGN(len);
417 if (sk_memalloc_socks())
418 gfp_mask |= __GFP_MEMALLOC;
420 local_irq_save(flags);
422 nc = this_cpu_ptr(&netdev_alloc_cache);
423 data = page_frag_alloc(nc, len, gfp_mask);
424 pfmemalloc = nc->pfmemalloc;
426 local_irq_restore(flags);
431 skb = __build_skb(data, len);
432 if (unlikely(!skb)) {
437 /* use OR instead of assignment to avoid clearing of bits in mask */
443 skb_reserve(skb, NET_SKB_PAD);
449 EXPORT_SYMBOL(__netdev_alloc_skb);
452 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
453 * @napi: napi instance this buffer was allocated for
454 * @len: length to allocate
455 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
457 * Allocate a new sk_buff for use in NAPI receive. This buffer will
458 * attempt to allocate the head from a special reserved region used
459 * only for NAPI Rx allocation. By doing this we can save several
460 * CPU cycles by avoiding having to disable and re-enable IRQs.
462 * %NULL is returned if there is no free memory.
464 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
467 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
471 len += NET_SKB_PAD + NET_IP_ALIGN;
473 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
474 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
475 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
481 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
482 len = SKB_DATA_ALIGN(len);
484 if (sk_memalloc_socks())
485 gfp_mask |= __GFP_MEMALLOC;
487 data = page_frag_alloc(&nc->page, len, gfp_mask);
491 skb = __build_skb(data, len);
492 if (unlikely(!skb)) {
497 /* use OR instead of assignment to avoid clearing of bits in mask */
498 if (nc->page.pfmemalloc)
503 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
504 skb->dev = napi->dev;
509 EXPORT_SYMBOL(__napi_alloc_skb);
511 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
512 int size, unsigned int truesize)
514 skb_fill_page_desc(skb, i, page, off, size);
516 skb->data_len += size;
517 skb->truesize += truesize;
519 EXPORT_SYMBOL(skb_add_rx_frag);
521 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
522 unsigned int truesize)
524 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
526 skb_frag_size_add(frag, size);
528 skb->data_len += size;
529 skb->truesize += truesize;
531 EXPORT_SYMBOL(skb_coalesce_rx_frag);
533 static void skb_drop_list(struct sk_buff **listp)
535 kfree_skb_list(*listp);
539 static inline void skb_drop_fraglist(struct sk_buff *skb)
541 skb_drop_list(&skb_shinfo(skb)->frag_list);
544 static void skb_clone_fraglist(struct sk_buff *skb)
546 struct sk_buff *list;
548 skb_walk_frags(skb, list)
552 static void skb_free_head(struct sk_buff *skb)
554 unsigned char *head = skb->head;
562 static void skb_release_data(struct sk_buff *skb)
564 struct skb_shared_info *shinfo = skb_shinfo(skb);
568 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
572 for (i = 0; i < shinfo->nr_frags; i++)
573 __skb_frag_unref(&shinfo->frags[i]);
575 if (shinfo->frag_list)
576 kfree_skb_list(shinfo->frag_list);
578 skb_zcopy_clear(skb, true);
583 * Free an skbuff by memory without cleaning the state.
585 static void kfree_skbmem(struct sk_buff *skb)
587 struct sk_buff_fclones *fclones;
589 switch (skb->fclone) {
590 case SKB_FCLONE_UNAVAILABLE:
591 kmem_cache_free(skbuff_head_cache, skb);
594 case SKB_FCLONE_ORIG:
595 fclones = container_of(skb, struct sk_buff_fclones, skb1);
597 /* We usually free the clone (TX completion) before original skb
598 * This test would have no chance to be true for the clone,
599 * while here, branch prediction will be good.
601 if (refcount_read(&fclones->fclone_ref) == 1)
605 default: /* SKB_FCLONE_CLONE */
606 fclones = container_of(skb, struct sk_buff_fclones, skb2);
609 if (!refcount_dec_and_test(&fclones->fclone_ref))
612 kmem_cache_free(skbuff_fclone_cache, fclones);
615 void skb_release_head_state(struct sk_buff *skb)
618 if (skb->destructor) {
620 skb->destructor(skb);
622 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
623 nf_conntrack_put(skb_nfct(skb));
628 /* Free everything but the sk_buff shell. */
629 static void skb_release_all(struct sk_buff *skb)
631 skb_release_head_state(skb);
632 if (likely(skb->head))
633 skb_release_data(skb);
637 * __kfree_skb - private function
640 * Free an sk_buff. Release anything attached to the buffer.
641 * Clean the state. This is an internal helper function. Users should
642 * always call kfree_skb
645 void __kfree_skb(struct sk_buff *skb)
647 skb_release_all(skb);
650 EXPORT_SYMBOL(__kfree_skb);
653 * kfree_skb - free an sk_buff
654 * @skb: buffer to free
656 * Drop a reference to the buffer and free it if the usage count has
659 void kfree_skb(struct sk_buff *skb)
664 trace_kfree_skb(skb, __builtin_return_address(0));
667 EXPORT_SYMBOL(kfree_skb);
669 void kfree_skb_list(struct sk_buff *segs)
672 struct sk_buff *next = segs->next;
678 EXPORT_SYMBOL(kfree_skb_list);
681 * skb_tx_error - report an sk_buff xmit error
682 * @skb: buffer that triggered an error
684 * Report xmit error if a device callback is tracking this skb.
685 * skb must be freed afterwards.
687 void skb_tx_error(struct sk_buff *skb)
689 skb_zcopy_clear(skb, true);
691 EXPORT_SYMBOL(skb_tx_error);
694 * consume_skb - free an skbuff
695 * @skb: buffer to free
697 * Drop a ref to the buffer and free it if the usage count has hit zero
698 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
699 * is being dropped after a failure and notes that
701 void consume_skb(struct sk_buff *skb)
706 trace_consume_skb(skb);
709 EXPORT_SYMBOL(consume_skb);
712 * consume_stateless_skb - free an skbuff, assuming it is stateless
713 * @skb: buffer to free
715 * Alike consume_skb(), but this variant assumes that this is the last
716 * skb reference and all the head states have been already dropped
718 void __consume_stateless_skb(struct sk_buff *skb)
720 trace_consume_skb(skb);
721 skb_release_data(skb);
725 void __kfree_skb_flush(void)
727 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
729 /* flush skb_cache if containing objects */
731 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
737 static inline void _kfree_skb_defer(struct sk_buff *skb)
739 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
741 /* drop skb->head and call any destructors for packet */
742 skb_release_all(skb);
744 /* record skb to CPU local list */
745 nc->skb_cache[nc->skb_count++] = skb;
748 /* SLUB writes into objects when freeing */
752 /* flush skb_cache if it is filled */
753 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
754 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
759 void __kfree_skb_defer(struct sk_buff *skb)
761 _kfree_skb_defer(skb);
764 void napi_consume_skb(struct sk_buff *skb, int budget)
769 /* Zero budget indicate non-NAPI context called us, like netpoll */
770 if (unlikely(!budget)) {
771 dev_consume_skb_any(skb);
778 /* if reaching here SKB is ready to free */
779 trace_consume_skb(skb);
781 /* if SKB is a clone, don't handle this case */
782 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
787 _kfree_skb_defer(skb);
789 EXPORT_SYMBOL(napi_consume_skb);
791 /* Make sure a field is enclosed inside headers_start/headers_end section */
792 #define CHECK_SKB_FIELD(field) \
793 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
794 offsetof(struct sk_buff, headers_start)); \
795 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
796 offsetof(struct sk_buff, headers_end)); \
798 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
800 new->tstamp = old->tstamp;
801 /* We do not copy old->sk */
803 memcpy(new->cb, old->cb, sizeof(old->cb));
804 skb_dst_copy(new, old);
805 __skb_ext_copy(new, old);
806 __nf_copy(new, old, false);
808 /* Note : this field could be in headers_start/headers_end section
809 * It is not yet because we do not want to have a 16 bit hole
811 new->queue_mapping = old->queue_mapping;
813 memcpy(&new->headers_start, &old->headers_start,
814 offsetof(struct sk_buff, headers_end) -
815 offsetof(struct sk_buff, headers_start));
816 CHECK_SKB_FIELD(protocol);
817 CHECK_SKB_FIELD(csum);
818 CHECK_SKB_FIELD(hash);
819 CHECK_SKB_FIELD(priority);
820 CHECK_SKB_FIELD(skb_iif);
821 CHECK_SKB_FIELD(vlan_proto);
822 CHECK_SKB_FIELD(vlan_tci);
823 CHECK_SKB_FIELD(transport_header);
824 CHECK_SKB_FIELD(network_header);
825 CHECK_SKB_FIELD(mac_header);
826 CHECK_SKB_FIELD(inner_protocol);
827 CHECK_SKB_FIELD(inner_transport_header);
828 CHECK_SKB_FIELD(inner_network_header);
829 CHECK_SKB_FIELD(inner_mac_header);
830 CHECK_SKB_FIELD(mark);
831 #ifdef CONFIG_NETWORK_SECMARK
832 CHECK_SKB_FIELD(secmark);
834 #ifdef CONFIG_NET_RX_BUSY_POLL
835 CHECK_SKB_FIELD(napi_id);
838 CHECK_SKB_FIELD(sender_cpu);
840 #ifdef CONFIG_NET_SCHED
841 CHECK_SKB_FIELD(tc_index);
847 * You should not add any new code to this function. Add it to
848 * __copy_skb_header above instead.
850 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
852 #define C(x) n->x = skb->x
854 n->next = n->prev = NULL;
856 __copy_skb_header(n, skb);
861 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
866 n->destructor = NULL;
873 refcount_set(&n->users, 1);
875 atomic_inc(&(skb_shinfo(skb)->dataref));
883 * skb_morph - morph one skb into another
884 * @dst: the skb to receive the contents
885 * @src: the skb to supply the contents
887 * This is identical to skb_clone except that the target skb is
888 * supplied by the user.
890 * The target skb is returned upon exit.
892 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
894 skb_release_all(dst);
895 return __skb_clone(dst, src);
897 EXPORT_SYMBOL_GPL(skb_morph);
899 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
901 unsigned long max_pg, num_pg, new_pg, old_pg;
902 struct user_struct *user;
904 if (capable(CAP_IPC_LOCK) || !size)
907 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
908 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
909 user = mmp->user ? : current_user();
912 old_pg = atomic_long_read(&user->locked_vm);
913 new_pg = old_pg + num_pg;
916 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
920 mmp->user = get_uid(user);
921 mmp->num_pg = num_pg;
923 mmp->num_pg += num_pg;
928 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
930 void mm_unaccount_pinned_pages(struct mmpin *mmp)
933 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
937 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
939 struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
941 struct ubuf_info *uarg;
944 WARN_ON_ONCE(!in_task());
946 skb = sock_omalloc(sk, 0, GFP_KERNEL);
950 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
951 uarg = (void *)skb->cb;
952 uarg->mmp.user = NULL;
954 if (mm_account_pinned_pages(&uarg->mmp, size)) {
959 uarg->callback = sock_zerocopy_callback;
960 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
962 uarg->bytelen = size;
964 refcount_set(&uarg->refcnt, 1);
969 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
971 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
973 return container_of((void *)uarg, struct sk_buff, cb);
976 struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
977 struct ubuf_info *uarg)
980 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
983 /* realloc only when socket is locked (TCP, UDP cork),
984 * so uarg->len and sk_zckey access is serialized
986 if (!sock_owned_by_user(sk)) {
991 bytelen = uarg->bytelen + size;
992 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
993 /* TCP can create new skb to attach new uarg */
994 if (sk->sk_type == SOCK_STREAM)
999 next = (u32)atomic_read(&sk->sk_zckey);
1000 if ((u32)(uarg->id + uarg->len) == next) {
1001 if (mm_account_pinned_pages(&uarg->mmp, size))
1004 uarg->bytelen = bytelen;
1005 atomic_set(&sk->sk_zckey, ++next);
1006 sock_zerocopy_get(uarg);
1012 return sock_zerocopy_alloc(sk, size);
1014 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1016 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1018 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1022 old_lo = serr->ee.ee_info;
1023 old_hi = serr->ee.ee_data;
1024 sum_len = old_hi - old_lo + 1ULL + len;
1026 if (sum_len >= (1ULL << 32))
1029 if (lo != old_hi + 1)
1032 serr->ee.ee_data += len;
1036 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1038 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1039 struct sock_exterr_skb *serr;
1040 struct sock *sk = skb->sk;
1041 struct sk_buff_head *q;
1042 unsigned long flags;
1046 mm_unaccount_pinned_pages(&uarg->mmp);
1048 /* if !len, there was only 1 call, and it was aborted
1049 * so do not queue a completion notification
1051 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1056 hi = uarg->id + len - 1;
1058 serr = SKB_EXT_ERR(skb);
1059 memset(serr, 0, sizeof(*serr));
1060 serr->ee.ee_errno = 0;
1061 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1062 serr->ee.ee_data = hi;
1063 serr->ee.ee_info = lo;
1065 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1067 q = &sk->sk_error_queue;
1068 spin_lock_irqsave(&q->lock, flags);
1069 tail = skb_peek_tail(q);
1070 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1071 !skb_zerocopy_notify_extend(tail, lo, len)) {
1072 __skb_queue_tail(q, skb);
1075 spin_unlock_irqrestore(&q->lock, flags);
1077 sk->sk_error_report(sk);
1083 EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1085 void sock_zerocopy_put(struct ubuf_info *uarg)
1087 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
1089 uarg->callback(uarg, uarg->zerocopy);
1091 consume_skb(skb_from_uarg(uarg));
1094 EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1096 void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1099 struct sock *sk = skb_from_uarg(uarg)->sk;
1101 atomic_dec(&sk->sk_zckey);
1105 sock_zerocopy_put(uarg);
1108 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1110 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1112 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1114 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1116 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1117 struct msghdr *msg, int len,
1118 struct ubuf_info *uarg)
1120 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1121 struct iov_iter orig_iter = msg->msg_iter;
1122 int err, orig_len = skb->len;
1124 /* An skb can only point to one uarg. This edge case happens when
1125 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1127 if (orig_uarg && uarg != orig_uarg)
1130 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1131 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1132 struct sock *save_sk = skb->sk;
1134 /* Streams do not free skb on error. Reset to prev state. */
1135 msg->msg_iter = orig_iter;
1137 ___pskb_trim(skb, orig_len);
1142 skb_zcopy_set(skb, uarg, NULL);
1143 return skb->len - orig_len;
1145 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1147 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1150 if (skb_zcopy(orig)) {
1151 if (skb_zcopy(nskb)) {
1152 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1157 if (skb_uarg(nskb) == skb_uarg(orig))
1159 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1162 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1168 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1169 * @skb: the skb to modify
1170 * @gfp_mask: allocation priority
1172 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1173 * It will copy all frags into kernel and drop the reference
1174 * to userspace pages.
1176 * If this function is called from an interrupt gfp_mask() must be
1179 * Returns 0 on success or a negative error code on failure
1180 * to allocate kernel memory to copy to.
1182 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1184 int num_frags = skb_shinfo(skb)->nr_frags;
1185 struct page *page, *head = NULL;
1189 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1195 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1196 for (i = 0; i < new_frags; i++) {
1197 page = alloc_page(gfp_mask);
1200 struct page *next = (struct page *)page_private(head);
1206 set_page_private(page, (unsigned long)head);
1212 for (i = 0; i < num_frags; i++) {
1213 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1214 u32 p_off, p_len, copied;
1218 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1219 p, p_off, p_len, copied) {
1221 vaddr = kmap_atomic(p);
1223 while (done < p_len) {
1224 if (d_off == PAGE_SIZE) {
1226 page = (struct page *)page_private(page);
1228 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1229 memcpy(page_address(page) + d_off,
1230 vaddr + p_off + done, copy);
1234 kunmap_atomic(vaddr);
1238 /* skb frags release userspace buffers */
1239 for (i = 0; i < num_frags; i++)
1240 skb_frag_unref(skb, i);
1242 /* skb frags point to kernel buffers */
1243 for (i = 0; i < new_frags - 1; i++) {
1244 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1245 head = (struct page *)page_private(head);
1247 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1248 skb_shinfo(skb)->nr_frags = new_frags;
1251 skb_zcopy_clear(skb, false);
1254 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1257 * skb_clone - duplicate an sk_buff
1258 * @skb: buffer to clone
1259 * @gfp_mask: allocation priority
1261 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1262 * copies share the same packet data but not structure. The new
1263 * buffer has a reference count of 1. If the allocation fails the
1264 * function returns %NULL otherwise the new buffer is returned.
1266 * If this function is called from an interrupt gfp_mask() must be
1270 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1272 struct sk_buff_fclones *fclones = container_of(skb,
1273 struct sk_buff_fclones,
1277 if (skb_orphan_frags(skb, gfp_mask))
1280 if (skb->fclone == SKB_FCLONE_ORIG &&
1281 refcount_read(&fclones->fclone_ref) == 1) {
1283 refcount_set(&fclones->fclone_ref, 2);
1285 if (skb_pfmemalloc(skb))
1286 gfp_mask |= __GFP_MEMALLOC;
1288 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1292 n->fclone = SKB_FCLONE_UNAVAILABLE;
1295 return __skb_clone(n, skb);
1297 EXPORT_SYMBOL(skb_clone);
1299 void skb_headers_offset_update(struct sk_buff *skb, int off)
1301 /* Only adjust this if it actually is csum_start rather than csum */
1302 if (skb->ip_summed == CHECKSUM_PARTIAL)
1303 skb->csum_start += off;
1304 /* {transport,network,mac}_header and tail are relative to skb->head */
1305 skb->transport_header += off;
1306 skb->network_header += off;
1307 if (skb_mac_header_was_set(skb))
1308 skb->mac_header += off;
1309 skb->inner_transport_header += off;
1310 skb->inner_network_header += off;
1311 skb->inner_mac_header += off;
1313 EXPORT_SYMBOL(skb_headers_offset_update);
1315 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1317 __copy_skb_header(new, old);
1319 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1320 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1321 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1323 EXPORT_SYMBOL(skb_copy_header);
1325 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1327 if (skb_pfmemalloc(skb))
1328 return SKB_ALLOC_RX;
1333 * skb_copy - create private copy of an sk_buff
1334 * @skb: buffer to copy
1335 * @gfp_mask: allocation priority
1337 * Make a copy of both an &sk_buff and its data. This is used when the
1338 * caller wishes to modify the data and needs a private copy of the
1339 * data to alter. Returns %NULL on failure or the pointer to the buffer
1340 * on success. The returned buffer has a reference count of 1.
1342 * As by-product this function converts non-linear &sk_buff to linear
1343 * one, so that &sk_buff becomes completely private and caller is allowed
1344 * to modify all the data of returned buffer. This means that this
1345 * function is not recommended for use in circumstances when only
1346 * header is going to be modified. Use pskb_copy() instead.
1349 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1351 int headerlen = skb_headroom(skb);
1352 unsigned int size = skb_end_offset(skb) + skb->data_len;
1353 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1354 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1359 /* Set the data pointer */
1360 skb_reserve(n, headerlen);
1361 /* Set the tail pointer and length */
1362 skb_put(n, skb->len);
1364 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1366 skb_copy_header(n, skb);
1369 EXPORT_SYMBOL(skb_copy);
1372 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1373 * @skb: buffer to copy
1374 * @headroom: headroom of new skb
1375 * @gfp_mask: allocation priority
1376 * @fclone: if true allocate the copy of the skb from the fclone
1377 * cache instead of the head cache; it is recommended to set this
1378 * to true for the cases where the copy will likely be cloned
1380 * Make a copy of both an &sk_buff and part of its data, located
1381 * in header. Fragmented data remain shared. This is used when
1382 * the caller wishes to modify only header of &sk_buff and needs
1383 * private copy of the header to alter. Returns %NULL on failure
1384 * or the pointer to the buffer on success.
1385 * The returned buffer has a reference count of 1.
1388 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1389 gfp_t gfp_mask, bool fclone)
1391 unsigned int size = skb_headlen(skb) + headroom;
1392 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1393 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1398 /* Set the data pointer */
1399 skb_reserve(n, headroom);
1400 /* Set the tail pointer and length */
1401 skb_put(n, skb_headlen(skb));
1402 /* Copy the bytes */
1403 skb_copy_from_linear_data(skb, n->data, n->len);
1405 n->truesize += skb->data_len;
1406 n->data_len = skb->data_len;
1409 if (skb_shinfo(skb)->nr_frags) {
1412 if (skb_orphan_frags(skb, gfp_mask) ||
1413 skb_zerocopy_clone(n, skb, gfp_mask)) {
1418 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1419 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1420 skb_frag_ref(skb, i);
1422 skb_shinfo(n)->nr_frags = i;
1425 if (skb_has_frag_list(skb)) {
1426 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1427 skb_clone_fraglist(n);
1430 skb_copy_header(n, skb);
1434 EXPORT_SYMBOL(__pskb_copy_fclone);
1437 * pskb_expand_head - reallocate header of &sk_buff
1438 * @skb: buffer to reallocate
1439 * @nhead: room to add at head
1440 * @ntail: room to add at tail
1441 * @gfp_mask: allocation priority
1443 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1444 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1445 * reference count of 1. Returns zero in the case of success or error,
1446 * if expansion failed. In the last case, &sk_buff is not changed.
1448 * All the pointers pointing into skb header may change and must be
1449 * reloaded after call to this function.
1452 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1455 int i, osize = skb_end_offset(skb);
1456 int size = osize + nhead + ntail;
1462 BUG_ON(skb_shared(skb));
1464 size = SKB_DATA_ALIGN(size);
1466 if (skb_pfmemalloc(skb))
1467 gfp_mask |= __GFP_MEMALLOC;
1468 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1469 gfp_mask, NUMA_NO_NODE, NULL);
1472 size = SKB_WITH_OVERHEAD(ksize(data));
1474 /* Copy only real data... and, alas, header. This should be
1475 * optimized for the cases when header is void.
1477 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1479 memcpy((struct skb_shared_info *)(data + size),
1481 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1484 * if shinfo is shared we must drop the old head gracefully, but if it
1485 * is not we can just drop the old head and let the existing refcount
1486 * be since all we did is relocate the values
1488 if (skb_cloned(skb)) {
1489 if (skb_orphan_frags(skb, gfp_mask))
1492 refcount_inc(&skb_uarg(skb)->refcnt);
1493 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1494 skb_frag_ref(skb, i);
1496 if (skb_has_frag_list(skb))
1497 skb_clone_fraglist(skb);
1499 skb_release_data(skb);
1503 off = (data + nhead) - skb->head;
1508 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1512 skb->end = skb->head + size;
1515 skb_headers_offset_update(skb, nhead);
1519 atomic_set(&skb_shinfo(skb)->dataref, 1);
1521 skb_metadata_clear(skb);
1523 /* It is not generally safe to change skb->truesize.
1524 * For the moment, we really care of rx path, or
1525 * when skb is orphaned (not attached to a socket).
1527 if (!skb->sk || skb->destructor == sock_edemux)
1528 skb->truesize += size - osize;
1537 EXPORT_SYMBOL(pskb_expand_head);
1539 /* Make private copy of skb with writable head and some headroom */
1541 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1543 struct sk_buff *skb2;
1544 int delta = headroom - skb_headroom(skb);
1547 skb2 = pskb_copy(skb, GFP_ATOMIC);
1549 skb2 = skb_clone(skb, GFP_ATOMIC);
1550 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1558 EXPORT_SYMBOL(skb_realloc_headroom);
1561 * skb_copy_expand - copy and expand sk_buff
1562 * @skb: buffer to copy
1563 * @newheadroom: new free bytes at head
1564 * @newtailroom: new free bytes at tail
1565 * @gfp_mask: allocation priority
1567 * Make a copy of both an &sk_buff and its data and while doing so
1568 * allocate additional space.
1570 * This is used when the caller wishes to modify the data and needs a
1571 * private copy of the data to alter as well as more space for new fields.
1572 * Returns %NULL on failure or the pointer to the buffer
1573 * on success. The returned buffer has a reference count of 1.
1575 * You must pass %GFP_ATOMIC as the allocation priority if this function
1576 * is called from an interrupt.
1578 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1579 int newheadroom, int newtailroom,
1583 * Allocate the copy buffer
1585 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1586 gfp_mask, skb_alloc_rx_flag(skb),
1588 int oldheadroom = skb_headroom(skb);
1589 int head_copy_len, head_copy_off;
1594 skb_reserve(n, newheadroom);
1596 /* Set the tail pointer and length */
1597 skb_put(n, skb->len);
1599 head_copy_len = oldheadroom;
1601 if (newheadroom <= head_copy_len)
1602 head_copy_len = newheadroom;
1604 head_copy_off = newheadroom - head_copy_len;
1606 /* Copy the linear header and data. */
1607 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1608 skb->len + head_copy_len));
1610 skb_copy_header(n, skb);
1612 skb_headers_offset_update(n, newheadroom - oldheadroom);
1616 EXPORT_SYMBOL(skb_copy_expand);
1619 * __skb_pad - zero pad the tail of an skb
1620 * @skb: buffer to pad
1621 * @pad: space to pad
1622 * @free_on_error: free buffer on error
1624 * Ensure that a buffer is followed by a padding area that is zero
1625 * filled. Used by network drivers which may DMA or transfer data
1626 * beyond the buffer end onto the wire.
1628 * May return error in out of memory cases. The skb is freed on error
1629 * if @free_on_error is true.
1632 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1637 /* If the skbuff is non linear tailroom is always zero.. */
1638 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1639 memset(skb->data+skb->len, 0, pad);
1643 ntail = skb->data_len + pad - (skb->end - skb->tail);
1644 if (likely(skb_cloned(skb) || ntail > 0)) {
1645 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1650 /* FIXME: The use of this function with non-linear skb's really needs
1653 err = skb_linearize(skb);
1657 memset(skb->data + skb->len, 0, pad);
1665 EXPORT_SYMBOL(__skb_pad);
1668 * pskb_put - add data to the tail of a potentially fragmented buffer
1669 * @skb: start of the buffer to use
1670 * @tail: tail fragment of the buffer to use
1671 * @len: amount of data to add
1673 * This function extends the used data area of the potentially
1674 * fragmented buffer. @tail must be the last fragment of @skb -- or
1675 * @skb itself. If this would exceed the total buffer size the kernel
1676 * will panic. A pointer to the first byte of the extra data is
1680 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1683 skb->data_len += len;
1686 return skb_put(tail, len);
1688 EXPORT_SYMBOL_GPL(pskb_put);
1691 * skb_put - add data to a buffer
1692 * @skb: buffer to use
1693 * @len: amount of data to add
1695 * This function extends the used data area of the buffer. If this would
1696 * exceed the total buffer size the kernel will panic. A pointer to the
1697 * first byte of the extra data is returned.
1699 void *skb_put(struct sk_buff *skb, unsigned int len)
1701 void *tmp = skb_tail_pointer(skb);
1702 SKB_LINEAR_ASSERT(skb);
1705 if (unlikely(skb->tail > skb->end))
1706 skb_over_panic(skb, len, __builtin_return_address(0));
1709 EXPORT_SYMBOL(skb_put);
1712 * skb_push - add data to the start of a buffer
1713 * @skb: buffer to use
1714 * @len: amount of data to add
1716 * This function extends the used data area of the buffer at the buffer
1717 * start. If this would exceed the total buffer headroom the kernel will
1718 * panic. A pointer to the first byte of the extra data is returned.
1720 void *skb_push(struct sk_buff *skb, unsigned int len)
1724 if (unlikely(skb->data < skb->head))
1725 skb_under_panic(skb, len, __builtin_return_address(0));
1728 EXPORT_SYMBOL(skb_push);
1731 * skb_pull - remove data from the start of a buffer
1732 * @skb: buffer to use
1733 * @len: amount of data to remove
1735 * This function removes data from the start of a buffer, returning
1736 * the memory to the headroom. A pointer to the next data in the buffer
1737 * is returned. Once the data has been pulled future pushes will overwrite
1740 void *skb_pull(struct sk_buff *skb, unsigned int len)
1742 return skb_pull_inline(skb, len);
1744 EXPORT_SYMBOL(skb_pull);
1747 * skb_trim - remove end from a buffer
1748 * @skb: buffer to alter
1751 * Cut the length of a buffer down by removing data from the tail. If
1752 * the buffer is already under the length specified it is not modified.
1753 * The skb must be linear.
1755 void skb_trim(struct sk_buff *skb, unsigned int len)
1758 __skb_trim(skb, len);
1760 EXPORT_SYMBOL(skb_trim);
1762 /* Trims skb to length len. It can change skb pointers.
1765 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1767 struct sk_buff **fragp;
1768 struct sk_buff *frag;
1769 int offset = skb_headlen(skb);
1770 int nfrags = skb_shinfo(skb)->nr_frags;
1774 if (skb_cloned(skb) &&
1775 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1782 for (; i < nfrags; i++) {
1783 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1790 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1793 skb_shinfo(skb)->nr_frags = i;
1795 for (; i < nfrags; i++)
1796 skb_frag_unref(skb, i);
1798 if (skb_has_frag_list(skb))
1799 skb_drop_fraglist(skb);
1803 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1804 fragp = &frag->next) {
1805 int end = offset + frag->len;
1807 if (skb_shared(frag)) {
1808 struct sk_buff *nfrag;
1810 nfrag = skb_clone(frag, GFP_ATOMIC);
1811 if (unlikely(!nfrag))
1814 nfrag->next = frag->next;
1826 unlikely((err = pskb_trim(frag, len - offset))))
1830 skb_drop_list(&frag->next);
1835 if (len > skb_headlen(skb)) {
1836 skb->data_len -= skb->len - len;
1841 skb_set_tail_pointer(skb, len);
1844 if (!skb->sk || skb->destructor == sock_edemux)
1848 EXPORT_SYMBOL(___pskb_trim);
1850 /* Note : use pskb_trim_rcsum() instead of calling this directly
1852 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1854 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1855 int delta = skb->len - len;
1857 skb->csum = csum_block_sub(skb->csum,
1858 skb_checksum(skb, len, delta, 0),
1861 return __pskb_trim(skb, len);
1863 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1866 * __pskb_pull_tail - advance tail of skb header
1867 * @skb: buffer to reallocate
1868 * @delta: number of bytes to advance tail
1870 * The function makes a sense only on a fragmented &sk_buff,
1871 * it expands header moving its tail forward and copying necessary
1872 * data from fragmented part.
1874 * &sk_buff MUST have reference count of 1.
1876 * Returns %NULL (and &sk_buff does not change) if pull failed
1877 * or value of new tail of skb in the case of success.
1879 * All the pointers pointing into skb header may change and must be
1880 * reloaded after call to this function.
1883 /* Moves tail of skb head forward, copying data from fragmented part,
1884 * when it is necessary.
1885 * 1. It may fail due to malloc failure.
1886 * 2. It may change skb pointers.
1888 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1890 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1892 /* If skb has not enough free space at tail, get new one
1893 * plus 128 bytes for future expansions. If we have enough
1894 * room at tail, reallocate without expansion only if skb is cloned.
1896 int i, k, eat = (skb->tail + delta) - skb->end;
1898 if (eat > 0 || skb_cloned(skb)) {
1899 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1904 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1905 skb_tail_pointer(skb), delta));
1907 /* Optimization: no fragments, no reasons to preestimate
1908 * size of pulled pages. Superb.
1910 if (!skb_has_frag_list(skb))
1913 /* Estimate size of pulled pages. */
1915 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1916 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1923 /* If we need update frag list, we are in troubles.
1924 * Certainly, it is possible to add an offset to skb data,
1925 * but taking into account that pulling is expected to
1926 * be very rare operation, it is worth to fight against
1927 * further bloating skb head and crucify ourselves here instead.
1928 * Pure masohism, indeed. 8)8)
1931 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1932 struct sk_buff *clone = NULL;
1933 struct sk_buff *insp = NULL;
1936 if (list->len <= eat) {
1937 /* Eaten as whole. */
1942 /* Eaten partially. */
1944 if (skb_shared(list)) {
1945 /* Sucks! We need to fork list. :-( */
1946 clone = skb_clone(list, GFP_ATOMIC);
1952 /* This may be pulled without
1956 if (!pskb_pull(list, eat)) {
1964 /* Free pulled out fragments. */
1965 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1966 skb_shinfo(skb)->frag_list = list->next;
1969 /* And insert new clone at head. */
1972 skb_shinfo(skb)->frag_list = clone;
1975 /* Success! Now we may commit changes to skb data. */
1980 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1981 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1984 skb_frag_unref(skb, i);
1987 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1989 skb_shinfo(skb)->frags[k].page_offset += eat;
1990 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1998 skb_shinfo(skb)->nr_frags = k;
2002 skb->data_len -= delta;
2005 skb_zcopy_clear(skb, false);
2007 return skb_tail_pointer(skb);
2009 EXPORT_SYMBOL(__pskb_pull_tail);
2012 * skb_copy_bits - copy bits from skb to kernel buffer
2014 * @offset: offset in source
2015 * @to: destination buffer
2016 * @len: number of bytes to copy
2018 * Copy the specified number of bytes from the source skb to the
2019 * destination buffer.
2022 * If its prototype is ever changed,
2023 * check arch/{*}/net/{*}.S files,
2024 * since it is called from BPF assembly code.
2026 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2028 int start = skb_headlen(skb);
2029 struct sk_buff *frag_iter;
2032 if (offset > (int)skb->len - len)
2036 if ((copy = start - offset) > 0) {
2039 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2040 if ((len -= copy) == 0)
2046 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2048 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2050 WARN_ON(start > offset + len);
2052 end = start + skb_frag_size(f);
2053 if ((copy = end - offset) > 0) {
2054 u32 p_off, p_len, copied;
2061 skb_frag_foreach_page(f,
2062 f->page_offset + offset - start,
2063 copy, p, p_off, p_len, copied) {
2064 vaddr = kmap_atomic(p);
2065 memcpy(to + copied, vaddr + p_off, p_len);
2066 kunmap_atomic(vaddr);
2069 if ((len -= copy) == 0)
2077 skb_walk_frags(skb, frag_iter) {
2080 WARN_ON(start > offset + len);
2082 end = start + frag_iter->len;
2083 if ((copy = end - offset) > 0) {
2086 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2088 if ((len -= copy) == 0)
2102 EXPORT_SYMBOL(skb_copy_bits);
2105 * Callback from splice_to_pipe(), if we need to release some pages
2106 * at the end of the spd in case we error'ed out in filling the pipe.
2108 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2110 put_page(spd->pages[i]);
2113 static struct page *linear_to_page(struct page *page, unsigned int *len,
2114 unsigned int *offset,
2117 struct page_frag *pfrag = sk_page_frag(sk);
2119 if (!sk_page_frag_refill(sk, pfrag))
2122 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2124 memcpy(page_address(pfrag->page) + pfrag->offset,
2125 page_address(page) + *offset, *len);
2126 *offset = pfrag->offset;
2127 pfrag->offset += *len;
2132 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2134 unsigned int offset)
2136 return spd->nr_pages &&
2137 spd->pages[spd->nr_pages - 1] == page &&
2138 (spd->partial[spd->nr_pages - 1].offset +
2139 spd->partial[spd->nr_pages - 1].len == offset);
2143 * Fill page/offset/length into spd, if it can hold more pages.
2145 static bool spd_fill_page(struct splice_pipe_desc *spd,
2146 struct pipe_inode_info *pipe, struct page *page,
2147 unsigned int *len, unsigned int offset,
2151 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2155 page = linear_to_page(page, len, &offset, sk);
2159 if (spd_can_coalesce(spd, page, offset)) {
2160 spd->partial[spd->nr_pages - 1].len += *len;
2164 spd->pages[spd->nr_pages] = page;
2165 spd->partial[spd->nr_pages].len = *len;
2166 spd->partial[spd->nr_pages].offset = offset;
2172 static bool __splice_segment(struct page *page, unsigned int poff,
2173 unsigned int plen, unsigned int *off,
2175 struct splice_pipe_desc *spd, bool linear,
2177 struct pipe_inode_info *pipe)
2182 /* skip this segment if already processed */
2188 /* ignore any bits we already processed */
2194 unsigned int flen = min(*len, plen);
2196 if (spd_fill_page(spd, pipe, page, &flen, poff,
2202 } while (*len && plen);
2208 * Map linear and fragment data from the skb to spd. It reports true if the
2209 * pipe is full or if we already spliced the requested length.
2211 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2212 unsigned int *offset, unsigned int *len,
2213 struct splice_pipe_desc *spd, struct sock *sk)
2216 struct sk_buff *iter;
2218 /* map the linear part :
2219 * If skb->head_frag is set, this 'linear' part is backed by a
2220 * fragment, and if the head is not shared with any clones then
2221 * we can avoid a copy since we own the head portion of this page.
2223 if (__splice_segment(virt_to_page(skb->data),
2224 (unsigned long) skb->data & (PAGE_SIZE - 1),
2227 skb_head_is_locked(skb),
2232 * then map the fragments
2234 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2235 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2237 if (__splice_segment(skb_frag_page(f),
2238 f->page_offset, skb_frag_size(f),
2239 offset, len, spd, false, sk, pipe))
2243 skb_walk_frags(skb, iter) {
2244 if (*offset >= iter->len) {
2245 *offset -= iter->len;
2248 /* __skb_splice_bits() only fails if the output has no room
2249 * left, so no point in going over the frag_list for the error
2252 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2260 * Map data from the skb to a pipe. Should handle both the linear part,
2261 * the fragments, and the frag list.
2263 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2264 struct pipe_inode_info *pipe, unsigned int tlen,
2267 struct partial_page partial[MAX_SKB_FRAGS];
2268 struct page *pages[MAX_SKB_FRAGS];
2269 struct splice_pipe_desc spd = {
2272 .nr_pages_max = MAX_SKB_FRAGS,
2273 .ops = &nosteal_pipe_buf_ops,
2274 .spd_release = sock_spd_release,
2278 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2281 ret = splice_to_pipe(pipe, &spd);
2285 EXPORT_SYMBOL_GPL(skb_splice_bits);
2287 /* Send skb data on a socket. Socket must be locked. */
2288 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2291 unsigned int orig_len = len;
2292 struct sk_buff *head = skb;
2293 unsigned short fragidx;
2298 /* Deal with head data */
2299 while (offset < skb_headlen(skb) && len) {
2303 slen = min_t(int, len, skb_headlen(skb) - offset);
2304 kv.iov_base = skb->data + offset;
2306 memset(&msg, 0, sizeof(msg));
2308 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2316 /* All the data was skb head? */
2320 /* Make offset relative to start of frags */
2321 offset -= skb_headlen(skb);
2323 /* Find where we are in frag list */
2324 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2325 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2327 if (offset < frag->size)
2330 offset -= frag->size;
2333 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2334 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2336 slen = min_t(size_t, len, frag->size - offset);
2339 ret = kernel_sendpage_locked(sk, frag->page.p,
2340 frag->page_offset + offset,
2341 slen, MSG_DONTWAIT);
2354 /* Process any frag lists */
2357 if (skb_has_frag_list(skb)) {
2358 skb = skb_shinfo(skb)->frag_list;
2361 } else if (skb->next) {
2368 return orig_len - len;
2371 return orig_len == len ? ret : orig_len - len;
2373 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2376 * skb_store_bits - store bits from kernel buffer to skb
2377 * @skb: destination buffer
2378 * @offset: offset in destination
2379 * @from: source buffer
2380 * @len: number of bytes to copy
2382 * Copy the specified number of bytes from the source buffer to the
2383 * destination skb. This function handles all the messy bits of
2384 * traversing fragment lists and such.
2387 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2389 int start = skb_headlen(skb);
2390 struct sk_buff *frag_iter;
2393 if (offset > (int)skb->len - len)
2396 if ((copy = start - offset) > 0) {
2399 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2400 if ((len -= copy) == 0)
2406 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2407 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2410 WARN_ON(start > offset + len);
2412 end = start + skb_frag_size(frag);
2413 if ((copy = end - offset) > 0) {
2414 u32 p_off, p_len, copied;
2421 skb_frag_foreach_page(frag,
2422 frag->page_offset + offset - start,
2423 copy, p, p_off, p_len, copied) {
2424 vaddr = kmap_atomic(p);
2425 memcpy(vaddr + p_off, from + copied, p_len);
2426 kunmap_atomic(vaddr);
2429 if ((len -= copy) == 0)
2437 skb_walk_frags(skb, frag_iter) {
2440 WARN_ON(start > offset + len);
2442 end = start + frag_iter->len;
2443 if ((copy = end - offset) > 0) {
2446 if (skb_store_bits(frag_iter, offset - start,
2449 if ((len -= copy) == 0)
2462 EXPORT_SYMBOL(skb_store_bits);
2464 /* Checksum skb data. */
2465 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2466 __wsum csum, const struct skb_checksum_ops *ops)
2468 int start = skb_headlen(skb);
2469 int i, copy = start - offset;
2470 struct sk_buff *frag_iter;
2473 /* Checksum header. */
2477 csum = ops->update(skb->data + offset, copy, csum);
2478 if ((len -= copy) == 0)
2484 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2486 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2488 WARN_ON(start > offset + len);
2490 end = start + skb_frag_size(frag);
2491 if ((copy = end - offset) > 0) {
2492 u32 p_off, p_len, copied;
2500 skb_frag_foreach_page(frag,
2501 frag->page_offset + offset - start,
2502 copy, p, p_off, p_len, copied) {
2503 vaddr = kmap_atomic(p);
2504 csum2 = ops->update(vaddr + p_off, p_len, 0);
2505 kunmap_atomic(vaddr);
2506 csum = ops->combine(csum, csum2, pos, p_len);
2517 skb_walk_frags(skb, frag_iter) {
2520 WARN_ON(start > offset + len);
2522 end = start + frag_iter->len;
2523 if ((copy = end - offset) > 0) {
2527 csum2 = __skb_checksum(frag_iter, offset - start,
2529 csum = ops->combine(csum, csum2, pos, copy);
2530 if ((len -= copy) == 0)
2541 EXPORT_SYMBOL(__skb_checksum);
2543 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2544 int len, __wsum csum)
2546 const struct skb_checksum_ops ops = {
2547 .update = csum_partial_ext,
2548 .combine = csum_block_add_ext,
2551 return __skb_checksum(skb, offset, len, csum, &ops);
2553 EXPORT_SYMBOL(skb_checksum);
2555 /* Both of above in one bottle. */
2557 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2558 u8 *to, int len, __wsum csum)
2560 int start = skb_headlen(skb);
2561 int i, copy = start - offset;
2562 struct sk_buff *frag_iter;
2569 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2571 if ((len -= copy) == 0)
2578 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2581 WARN_ON(start > offset + len);
2583 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2584 if ((copy = end - offset) > 0) {
2585 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2586 u32 p_off, p_len, copied;
2594 skb_frag_foreach_page(frag,
2595 frag->page_offset + offset - start,
2596 copy, p, p_off, p_len, copied) {
2597 vaddr = kmap_atomic(p);
2598 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2601 kunmap_atomic(vaddr);
2602 csum = csum_block_add(csum, csum2, pos);
2614 skb_walk_frags(skb, frag_iter) {
2618 WARN_ON(start > offset + len);
2620 end = start + frag_iter->len;
2621 if ((copy = end - offset) > 0) {
2624 csum2 = skb_copy_and_csum_bits(frag_iter,
2627 csum = csum_block_add(csum, csum2, pos);
2628 if ((len -= copy) == 0)
2639 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2641 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2645 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
2646 /* See comments in __skb_checksum_complete(). */
2648 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2649 !skb->csum_complete_sw)
2650 netdev_rx_csum_fault(skb->dev, skb);
2652 if (!skb_shared(skb))
2653 skb->csum_valid = !sum;
2656 EXPORT_SYMBOL(__skb_checksum_complete_head);
2658 /* This function assumes skb->csum already holds pseudo header's checksum,
2659 * which has been changed from the hardware checksum, for example, by
2660 * __skb_checksum_validate_complete(). And, the original skb->csum must
2661 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2663 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2664 * zero. The new checksum is stored back into skb->csum unless the skb is
2667 __sum16 __skb_checksum_complete(struct sk_buff *skb)
2672 csum = skb_checksum(skb, 0, skb->len, 0);
2674 sum = csum_fold(csum_add(skb->csum, csum));
2675 /* This check is inverted, because we already knew the hardware
2676 * checksum is invalid before calling this function. So, if the
2677 * re-computed checksum is valid instead, then we have a mismatch
2678 * between the original skb->csum and skb_checksum(). This means either
2679 * the original hardware checksum is incorrect or we screw up skb->csum
2680 * when moving skb->data around.
2683 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2684 !skb->csum_complete_sw)
2685 netdev_rx_csum_fault(skb->dev, skb);
2688 if (!skb_shared(skb)) {
2689 /* Save full packet checksum */
2691 skb->ip_summed = CHECKSUM_COMPLETE;
2692 skb->csum_complete_sw = 1;
2693 skb->csum_valid = !sum;
2698 EXPORT_SYMBOL(__skb_checksum_complete);
2700 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2702 net_warn_ratelimited(
2703 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2708 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2709 int offset, int len)
2711 net_warn_ratelimited(
2712 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2717 static const struct skb_checksum_ops default_crc32c_ops = {
2718 .update = warn_crc32c_csum_update,
2719 .combine = warn_crc32c_csum_combine,
2722 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2723 &default_crc32c_ops;
2724 EXPORT_SYMBOL(crc32c_csum_stub);
2727 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2728 * @from: source buffer
2730 * Calculates the amount of linear headroom needed in the 'to' skb passed
2731 * into skb_zerocopy().
2734 skb_zerocopy_headlen(const struct sk_buff *from)
2736 unsigned int hlen = 0;
2738 if (!from->head_frag ||
2739 skb_headlen(from) < L1_CACHE_BYTES ||
2740 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2741 hlen = skb_headlen(from);
2743 if (skb_has_frag_list(from))
2748 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2751 * skb_zerocopy - Zero copy skb to skb
2752 * @to: destination buffer
2753 * @from: source buffer
2754 * @len: number of bytes to copy from source buffer
2755 * @hlen: size of linear headroom in destination buffer
2757 * Copies up to `len` bytes from `from` to `to` by creating references
2758 * to the frags in the source buffer.
2760 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2761 * headroom in the `to` buffer.
2764 * 0: everything is OK
2765 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2766 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2769 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2772 int plen = 0; /* length of skb->head fragment */
2775 unsigned int offset;
2777 BUG_ON(!from->head_frag && !hlen);
2779 /* dont bother with small payloads */
2780 if (len <= skb_tailroom(to))
2781 return skb_copy_bits(from, 0, skb_put(to, len), len);
2784 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2789 plen = min_t(int, skb_headlen(from), len);
2791 page = virt_to_head_page(from->head);
2792 offset = from->data - (unsigned char *)page_address(page);
2793 __skb_fill_page_desc(to, 0, page, offset, plen);
2800 to->truesize += len + plen;
2801 to->len += len + plen;
2802 to->data_len += len + plen;
2804 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2808 skb_zerocopy_clone(to, from, GFP_ATOMIC);
2810 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2813 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2814 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2815 len -= skb_shinfo(to)->frags[j].size;
2816 skb_frag_ref(to, j);
2819 skb_shinfo(to)->nr_frags = j;
2823 EXPORT_SYMBOL_GPL(skb_zerocopy);
2825 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2830 if (skb->ip_summed == CHECKSUM_PARTIAL)
2831 csstart = skb_checksum_start_offset(skb);
2833 csstart = skb_headlen(skb);
2835 BUG_ON(csstart > skb_headlen(skb));
2837 skb_copy_from_linear_data(skb, to, csstart);
2840 if (csstart != skb->len)
2841 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2842 skb->len - csstart, 0);
2844 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2845 long csstuff = csstart + skb->csum_offset;
2847 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2850 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2853 * skb_dequeue - remove from the head of the queue
2854 * @list: list to dequeue from
2856 * Remove the head of the list. The list lock is taken so the function
2857 * may be used safely with other locking list functions. The head item is
2858 * returned or %NULL if the list is empty.
2861 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2863 unsigned long flags;
2864 struct sk_buff *result;
2866 spin_lock_irqsave(&list->lock, flags);
2867 result = __skb_dequeue(list);
2868 spin_unlock_irqrestore(&list->lock, flags);
2871 EXPORT_SYMBOL(skb_dequeue);
2874 * skb_dequeue_tail - remove from the tail of the queue
2875 * @list: list to dequeue from
2877 * Remove the tail of the list. The list lock is taken so the function
2878 * may be used safely with other locking list functions. The tail item is
2879 * returned or %NULL if the list is empty.
2881 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2883 unsigned long flags;
2884 struct sk_buff *result;
2886 spin_lock_irqsave(&list->lock, flags);
2887 result = __skb_dequeue_tail(list);
2888 spin_unlock_irqrestore(&list->lock, flags);
2891 EXPORT_SYMBOL(skb_dequeue_tail);
2894 * skb_queue_purge - empty a list
2895 * @list: list to empty
2897 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2898 * the list and one reference dropped. This function takes the list
2899 * lock and is atomic with respect to other list locking functions.
2901 void skb_queue_purge(struct sk_buff_head *list)
2903 struct sk_buff *skb;
2904 while ((skb = skb_dequeue(list)) != NULL)
2907 EXPORT_SYMBOL(skb_queue_purge);
2910 * skb_rbtree_purge - empty a skb rbtree
2911 * @root: root of the rbtree to empty
2912 * Return value: the sum of truesizes of all purged skbs.
2914 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2915 * the list and one reference dropped. This function does not take
2916 * any lock. Synchronization should be handled by the caller (e.g., TCP
2917 * out-of-order queue is protected by the socket lock).
2919 unsigned int skb_rbtree_purge(struct rb_root *root)
2921 struct rb_node *p = rb_first(root);
2922 unsigned int sum = 0;
2925 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2928 rb_erase(&skb->rbnode, root);
2929 sum += skb->truesize;
2936 * skb_queue_head - queue a buffer at the list head
2937 * @list: list to use
2938 * @newsk: buffer to queue
2940 * Queue a buffer at the start of the list. This function takes the
2941 * list lock and can be used safely with other locking &sk_buff functions
2944 * A buffer cannot be placed on two lists at the same time.
2946 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2948 unsigned long flags;
2950 spin_lock_irqsave(&list->lock, flags);
2951 __skb_queue_head(list, newsk);
2952 spin_unlock_irqrestore(&list->lock, flags);
2954 EXPORT_SYMBOL(skb_queue_head);
2957 * skb_queue_tail - queue a buffer at the list tail
2958 * @list: list to use
2959 * @newsk: buffer to queue
2961 * Queue a buffer at the tail of the list. This function takes the
2962 * list lock and can be used safely with other locking &sk_buff functions
2965 * A buffer cannot be placed on two lists at the same time.
2967 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2969 unsigned long flags;
2971 spin_lock_irqsave(&list->lock, flags);
2972 __skb_queue_tail(list, newsk);
2973 spin_unlock_irqrestore(&list->lock, flags);
2975 EXPORT_SYMBOL(skb_queue_tail);
2978 * skb_unlink - remove a buffer from a list
2979 * @skb: buffer to remove
2980 * @list: list to use
2982 * Remove a packet from a list. The list locks are taken and this
2983 * function is atomic with respect to other list locked calls
2985 * You must know what list the SKB is on.
2987 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2989 unsigned long flags;
2991 spin_lock_irqsave(&list->lock, flags);
2992 __skb_unlink(skb, list);
2993 spin_unlock_irqrestore(&list->lock, flags);
2995 EXPORT_SYMBOL(skb_unlink);
2998 * skb_append - append a buffer
2999 * @old: buffer to insert after
3000 * @newsk: buffer to insert
3001 * @list: list to use
3003 * Place a packet after a given packet in a list. The list locks are taken
3004 * and this function is atomic with respect to other list locked calls.
3005 * A buffer cannot be placed on two lists at the same time.
3007 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3009 unsigned long flags;
3011 spin_lock_irqsave(&list->lock, flags);
3012 __skb_queue_after(list, old, newsk);
3013 spin_unlock_irqrestore(&list->lock, flags);
3015 EXPORT_SYMBOL(skb_append);
3017 static inline void skb_split_inside_header(struct sk_buff *skb,
3018 struct sk_buff* skb1,
3019 const u32 len, const int pos)
3023 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3025 /* And move data appendix as is. */
3026 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3027 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3029 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3030 skb_shinfo(skb)->nr_frags = 0;
3031 skb1->data_len = skb->data_len;
3032 skb1->len += skb1->data_len;
3035 skb_set_tail_pointer(skb, len);
3038 static inline void skb_split_no_header(struct sk_buff *skb,
3039 struct sk_buff* skb1,
3040 const u32 len, int pos)
3043 const int nfrags = skb_shinfo(skb)->nr_frags;
3045 skb_shinfo(skb)->nr_frags = 0;
3046 skb1->len = skb1->data_len = skb->len - len;
3048 skb->data_len = len - pos;
3050 for (i = 0; i < nfrags; i++) {
3051 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3053 if (pos + size > len) {
3054 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3058 * We have two variants in this case:
3059 * 1. Move all the frag to the second
3060 * part, if it is possible. F.e.
3061 * this approach is mandatory for TUX,
3062 * where splitting is expensive.
3063 * 2. Split is accurately. We make this.
3065 skb_frag_ref(skb, i);
3066 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
3067 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3068 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3069 skb_shinfo(skb)->nr_frags++;
3073 skb_shinfo(skb)->nr_frags++;
3076 skb_shinfo(skb1)->nr_frags = k;
3080 * skb_split - Split fragmented skb to two parts at length len.
3081 * @skb: the buffer to split
3082 * @skb1: the buffer to receive the second part
3083 * @len: new length for skb
3085 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3087 int pos = skb_headlen(skb);
3089 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3091 skb_zerocopy_clone(skb1, skb, 0);
3092 if (len < pos) /* Split line is inside header. */
3093 skb_split_inside_header(skb, skb1, len, pos);
3094 else /* Second chunk has no header, nothing to copy. */
3095 skb_split_no_header(skb, skb1, len, pos);
3097 EXPORT_SYMBOL(skb_split);
3099 /* Shifting from/to a cloned skb is a no-go.
3101 * Caller cannot keep skb_shinfo related pointers past calling here!
3103 static int skb_prepare_for_shift(struct sk_buff *skb)
3105 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3109 * skb_shift - Shifts paged data partially from skb to another
3110 * @tgt: buffer into which tail data gets added
3111 * @skb: buffer from which the paged data comes from
3112 * @shiftlen: shift up to this many bytes
3114 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3115 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3116 * It's up to caller to free skb if everything was shifted.
3118 * If @tgt runs out of frags, the whole operation is aborted.
3120 * Skb cannot include anything else but paged data while tgt is allowed
3121 * to have non-paged data as well.
3123 * TODO: full sized shift could be optimized but that would need
3124 * specialized skb free'er to handle frags without up-to-date nr_frags.
3126 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3128 int from, to, merge, todo;
3129 struct skb_frag_struct *fragfrom, *fragto;
3131 BUG_ON(shiftlen > skb->len);
3133 if (skb_headlen(skb))
3135 if (skb_zcopy(tgt) || skb_zcopy(skb))
3140 to = skb_shinfo(tgt)->nr_frags;
3141 fragfrom = &skb_shinfo(skb)->frags[from];
3143 /* Actual merge is delayed until the point when we know we can
3144 * commit all, so that we don't have to undo partial changes
3147 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3148 fragfrom->page_offset)) {
3153 todo -= skb_frag_size(fragfrom);
3155 if (skb_prepare_for_shift(skb) ||
3156 skb_prepare_for_shift(tgt))
3159 /* All previous frag pointers might be stale! */
3160 fragfrom = &skb_shinfo(skb)->frags[from];
3161 fragto = &skb_shinfo(tgt)->frags[merge];
3163 skb_frag_size_add(fragto, shiftlen);
3164 skb_frag_size_sub(fragfrom, shiftlen);
3165 fragfrom->page_offset += shiftlen;
3173 /* Skip full, not-fitting skb to avoid expensive operations */
3174 if ((shiftlen == skb->len) &&
3175 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3178 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3181 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3182 if (to == MAX_SKB_FRAGS)
3185 fragfrom = &skb_shinfo(skb)->frags[from];
3186 fragto = &skb_shinfo(tgt)->frags[to];
3188 if (todo >= skb_frag_size(fragfrom)) {
3189 *fragto = *fragfrom;
3190 todo -= skb_frag_size(fragfrom);
3195 __skb_frag_ref(fragfrom);
3196 fragto->page = fragfrom->page;
3197 fragto->page_offset = fragfrom->page_offset;
3198 skb_frag_size_set(fragto, todo);
3200 fragfrom->page_offset += todo;
3201 skb_frag_size_sub(fragfrom, todo);
3209 /* Ready to "commit" this state change to tgt */
3210 skb_shinfo(tgt)->nr_frags = to;
3213 fragfrom = &skb_shinfo(skb)->frags[0];
3214 fragto = &skb_shinfo(tgt)->frags[merge];
3216 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3217 __skb_frag_unref(fragfrom);
3220 /* Reposition in the original skb */
3222 while (from < skb_shinfo(skb)->nr_frags)
3223 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3224 skb_shinfo(skb)->nr_frags = to;
3226 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3229 /* Most likely the tgt won't ever need its checksum anymore, skb on
3230 * the other hand might need it if it needs to be resent
3232 tgt->ip_summed = CHECKSUM_PARTIAL;
3233 skb->ip_summed = CHECKSUM_PARTIAL;
3235 /* Yak, is it really working this way? Some helper please? */
3236 skb->len -= shiftlen;
3237 skb->data_len -= shiftlen;
3238 skb->truesize -= shiftlen;
3239 tgt->len += shiftlen;
3240 tgt->data_len += shiftlen;
3241 tgt->truesize += shiftlen;
3247 * skb_prepare_seq_read - Prepare a sequential read of skb data
3248 * @skb: the buffer to read
3249 * @from: lower offset of data to be read
3250 * @to: upper offset of data to be read
3251 * @st: state variable
3253 * Initializes the specified state variable. Must be called before
3254 * invoking skb_seq_read() for the first time.
3256 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3257 unsigned int to, struct skb_seq_state *st)
3259 st->lower_offset = from;
3260 st->upper_offset = to;
3261 st->root_skb = st->cur_skb = skb;
3262 st->frag_idx = st->stepped_offset = 0;
3263 st->frag_data = NULL;
3265 EXPORT_SYMBOL(skb_prepare_seq_read);
3268 * skb_seq_read - Sequentially read skb data
3269 * @consumed: number of bytes consumed by the caller so far
3270 * @data: destination pointer for data to be returned
3271 * @st: state variable
3273 * Reads a block of skb data at @consumed relative to the
3274 * lower offset specified to skb_prepare_seq_read(). Assigns
3275 * the head of the data block to @data and returns the length
3276 * of the block or 0 if the end of the skb data or the upper
3277 * offset has been reached.
3279 * The caller is not required to consume all of the data
3280 * returned, i.e. @consumed is typically set to the number
3281 * of bytes already consumed and the next call to
3282 * skb_seq_read() will return the remaining part of the block.
3284 * Note 1: The size of each block of data returned can be arbitrary,
3285 * this limitation is the cost for zerocopy sequential
3286 * reads of potentially non linear data.
3288 * Note 2: Fragment lists within fragments are not implemented
3289 * at the moment, state->root_skb could be replaced with
3290 * a stack for this purpose.
3292 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3293 struct skb_seq_state *st)
3295 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3298 if (unlikely(abs_offset >= st->upper_offset)) {
3299 if (st->frag_data) {
3300 kunmap_atomic(st->frag_data);
3301 st->frag_data = NULL;
3307 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3309 if (abs_offset < block_limit && !st->frag_data) {
3310 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3311 return block_limit - abs_offset;
3314 if (st->frag_idx == 0 && !st->frag_data)
3315 st->stepped_offset += skb_headlen(st->cur_skb);
3317 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3318 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3319 block_limit = skb_frag_size(frag) + st->stepped_offset;
3321 if (abs_offset < block_limit) {
3323 st->frag_data = kmap_atomic(skb_frag_page(frag));
3325 *data = (u8 *) st->frag_data + frag->page_offset +
3326 (abs_offset - st->stepped_offset);
3328 return block_limit - abs_offset;
3331 if (st->frag_data) {
3332 kunmap_atomic(st->frag_data);
3333 st->frag_data = NULL;
3337 st->stepped_offset += skb_frag_size(frag);
3340 if (st->frag_data) {
3341 kunmap_atomic(st->frag_data);
3342 st->frag_data = NULL;
3345 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3346 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3349 } else if (st->cur_skb->next) {
3350 st->cur_skb = st->cur_skb->next;
3357 EXPORT_SYMBOL(skb_seq_read);
3360 * skb_abort_seq_read - Abort a sequential read of skb data
3361 * @st: state variable
3363 * Must be called if skb_seq_read() was not called until it
3366 void skb_abort_seq_read(struct skb_seq_state *st)
3369 kunmap_atomic(st->frag_data);
3371 EXPORT_SYMBOL(skb_abort_seq_read);
3373 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3375 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3376 struct ts_config *conf,
3377 struct ts_state *state)
3379 return skb_seq_read(offset, text, TS_SKB_CB(state));
3382 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3384 skb_abort_seq_read(TS_SKB_CB(state));
3388 * skb_find_text - Find a text pattern in skb data
3389 * @skb: the buffer to look in
3390 * @from: search offset
3392 * @config: textsearch configuration
3394 * Finds a pattern in the skb data according to the specified
3395 * textsearch configuration. Use textsearch_next() to retrieve
3396 * subsequent occurrences of the pattern. Returns the offset
3397 * to the first occurrence or UINT_MAX if no match was found.
3399 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3400 unsigned int to, struct ts_config *config)
3402 struct ts_state state;
3405 config->get_next_block = skb_ts_get_next_block;
3406 config->finish = skb_ts_finish;
3408 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3410 ret = textsearch_find(config, &state);
3411 return (ret <= to - from ? ret : UINT_MAX);
3413 EXPORT_SYMBOL(skb_find_text);
3415 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3416 int offset, size_t size)
3418 int i = skb_shinfo(skb)->nr_frags;
3420 if (skb_can_coalesce(skb, i, page, offset)) {
3421 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3422 } else if (i < MAX_SKB_FRAGS) {
3424 skb_fill_page_desc(skb, i, page, offset, size);
3431 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3434 * skb_pull_rcsum - pull skb and update receive checksum
3435 * @skb: buffer to update
3436 * @len: length of data pulled
3438 * This function performs an skb_pull on the packet and updates
3439 * the CHECKSUM_COMPLETE checksum. It should be used on
3440 * receive path processing instead of skb_pull unless you know
3441 * that the checksum difference is zero (e.g., a valid IP header)
3442 * or you are setting ip_summed to CHECKSUM_NONE.
3444 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3446 unsigned char *data = skb->data;
3448 BUG_ON(len > skb->len);
3449 __skb_pull(skb, len);
3450 skb_postpull_rcsum(skb, data, len);
3453 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3455 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3457 skb_frag_t head_frag;
3460 page = virt_to_head_page(frag_skb->head);
3461 head_frag.page.p = page;
3462 head_frag.page_offset = frag_skb->data -
3463 (unsigned char *)page_address(page);
3464 head_frag.size = skb_headlen(frag_skb);
3469 * skb_segment - Perform protocol segmentation on skb.
3470 * @head_skb: buffer to segment
3471 * @features: features for the output path (see dev->features)
3473 * This function performs segmentation on the given skb. It returns
3474 * a pointer to the first in a list of new skbs for the segments.
3475 * In case of error it returns ERR_PTR(err).
3477 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3478 netdev_features_t features)
3480 struct sk_buff *segs = NULL;
3481 struct sk_buff *tail = NULL;
3482 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3483 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3484 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3485 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3486 struct sk_buff *frag_skb = head_skb;
3487 unsigned int offset = doffset;
3488 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3489 unsigned int partial_segs = 0;
3490 unsigned int headroom;
3491 unsigned int len = head_skb->len;
3494 int nfrags = skb_shinfo(head_skb)->nr_frags;
3500 __skb_push(head_skb, doffset);
3501 proto = skb_network_protocol(head_skb, &dummy);
3502 if (unlikely(!proto))
3503 return ERR_PTR(-EINVAL);
3505 sg = !!(features & NETIF_F_SG);
3506 csum = !!can_checksum_protocol(features, proto);
3508 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3509 if (!(features & NETIF_F_GSO_PARTIAL)) {
3510 struct sk_buff *iter;
3511 unsigned int frag_len;
3514 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3517 /* If we get here then all the required
3518 * GSO features except frag_list are supported.
3519 * Try to split the SKB to multiple GSO SKBs
3520 * with no frag_list.
3521 * Currently we can do that only when the buffers don't
3522 * have a linear part and all the buffers except
3523 * the last are of the same length.
3525 frag_len = list_skb->len;
3526 skb_walk_frags(head_skb, iter) {
3527 if (frag_len != iter->len && iter->next)
3529 if (skb_headlen(iter) && !iter->head_frag)
3535 if (len != frag_len)
3539 /* GSO partial only requires that we trim off any excess that
3540 * doesn't fit into an MSS sized block, so take care of that
3543 partial_segs = len / mss;
3544 if (partial_segs > 1)
3545 mss *= partial_segs;
3551 headroom = skb_headroom(head_skb);
3552 pos = skb_headlen(head_skb);
3555 struct sk_buff *nskb;
3556 skb_frag_t *nskb_frag;
3560 if (unlikely(mss == GSO_BY_FRAGS)) {
3561 len = list_skb->len;
3563 len = head_skb->len - offset;
3568 hsize = skb_headlen(head_skb) - offset;
3571 if (hsize > len || !sg)
3574 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3575 (skb_headlen(list_skb) == len || sg)) {
3576 BUG_ON(skb_headlen(list_skb) > len);
3579 nfrags = skb_shinfo(list_skb)->nr_frags;
3580 frag = skb_shinfo(list_skb)->frags;
3581 frag_skb = list_skb;
3582 pos += skb_headlen(list_skb);
3584 while (pos < offset + len) {
3585 BUG_ON(i >= nfrags);
3587 size = skb_frag_size(frag);
3588 if (pos + size > offset + len)
3596 nskb = skb_clone(list_skb, GFP_ATOMIC);
3597 list_skb = list_skb->next;
3599 if (unlikely(!nskb))
3602 if (unlikely(pskb_trim(nskb, len))) {
3607 hsize = skb_end_offset(nskb);
3608 if (skb_cow_head(nskb, doffset + headroom)) {
3613 nskb->truesize += skb_end_offset(nskb) - hsize;
3614 skb_release_head_state(nskb);
3615 __skb_push(nskb, doffset);
3617 nskb = __alloc_skb(hsize + doffset + headroom,
3618 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3621 if (unlikely(!nskb))
3624 skb_reserve(nskb, headroom);
3625 __skb_put(nskb, doffset);
3634 __copy_skb_header(nskb, head_skb);
3636 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3637 skb_reset_mac_len(nskb);
3639 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3640 nskb->data - tnl_hlen,
3641 doffset + tnl_hlen);
3643 if (nskb->len == len + doffset)
3644 goto perform_csum_check;
3647 if (!nskb->remcsum_offload)
3648 nskb->ip_summed = CHECKSUM_NONE;
3649 SKB_GSO_CB(nskb)->csum =
3650 skb_copy_and_csum_bits(head_skb, offset,
3653 SKB_GSO_CB(nskb)->csum_start =
3654 skb_headroom(nskb) + doffset;
3658 nskb_frag = skb_shinfo(nskb)->frags;
3660 skb_copy_from_linear_data_offset(head_skb, offset,
3661 skb_put(nskb, hsize), hsize);
3663 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3666 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3667 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3670 while (pos < offset + len) {
3673 nfrags = skb_shinfo(list_skb)->nr_frags;
3674 frag = skb_shinfo(list_skb)->frags;
3675 frag_skb = list_skb;
3676 if (!skb_headlen(list_skb)) {
3679 BUG_ON(!list_skb->head_frag);
3681 /* to make room for head_frag. */
3685 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3686 skb_zerocopy_clone(nskb, frag_skb,
3690 list_skb = list_skb->next;
3693 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3695 net_warn_ratelimited(
3696 "skb_segment: too many frags: %u %u\n",
3702 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
3703 __skb_frag_ref(nskb_frag);
3704 size = skb_frag_size(nskb_frag);
3707 nskb_frag->page_offset += offset - pos;
3708 skb_frag_size_sub(nskb_frag, offset - pos);
3711 skb_shinfo(nskb)->nr_frags++;
3713 if (pos + size <= offset + len) {
3718 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3726 nskb->data_len = len - hsize;
3727 nskb->len += nskb->data_len;
3728 nskb->truesize += nskb->data_len;
3732 if (skb_has_shared_frag(nskb) &&
3733 __skb_linearize(nskb))
3736 if (!nskb->remcsum_offload)
3737 nskb->ip_summed = CHECKSUM_NONE;
3738 SKB_GSO_CB(nskb)->csum =
3739 skb_checksum(nskb, doffset,
3740 nskb->len - doffset, 0);
3741 SKB_GSO_CB(nskb)->csum_start =
3742 skb_headroom(nskb) + doffset;
3744 } while ((offset += len) < head_skb->len);
3746 /* Some callers want to get the end of the list.
3747 * Put it in segs->prev to avoid walking the list.
3748 * (see validate_xmit_skb_list() for example)
3753 struct sk_buff *iter;
3754 int type = skb_shinfo(head_skb)->gso_type;
3755 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3757 /* Update type to add partial and then remove dodgy if set */
3758 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3759 type &= ~SKB_GSO_DODGY;
3761 /* Update GSO info and prepare to start updating headers on
3762 * our way back down the stack of protocols.
3764 for (iter = segs; iter; iter = iter->next) {
3765 skb_shinfo(iter)->gso_size = gso_size;
3766 skb_shinfo(iter)->gso_segs = partial_segs;
3767 skb_shinfo(iter)->gso_type = type;
3768 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3771 if (tail->len - doffset <= gso_size)
3772 skb_shinfo(tail)->gso_size = 0;
3773 else if (tail != segs)
3774 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3777 /* Following permits correct backpressure, for protocols
3778 * using skb_set_owner_w().
3779 * Idea is to tranfert ownership from head_skb to last segment.
3781 if (head_skb->destructor == sock_wfree) {
3782 swap(tail->truesize, head_skb->truesize);
3783 swap(tail->destructor, head_skb->destructor);
3784 swap(tail->sk, head_skb->sk);
3789 kfree_skb_list(segs);
3790 return ERR_PTR(err);
3792 EXPORT_SYMBOL_GPL(skb_segment);
3794 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
3796 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3797 unsigned int offset = skb_gro_offset(skb);
3798 unsigned int headlen = skb_headlen(skb);
3799 unsigned int len = skb_gro_len(skb);
3800 unsigned int delta_truesize;
3803 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
3806 lp = NAPI_GRO_CB(p)->last;
3807 pinfo = skb_shinfo(lp);
3809 if (headlen <= offset) {
3812 int i = skbinfo->nr_frags;
3813 int nr_frags = pinfo->nr_frags + i;
3815 if (nr_frags > MAX_SKB_FRAGS)
3819 pinfo->nr_frags = nr_frags;
3820 skbinfo->nr_frags = 0;
3822 frag = pinfo->frags + nr_frags;
3823 frag2 = skbinfo->frags + i;
3828 frag->page_offset += offset;
3829 skb_frag_size_sub(frag, offset);
3831 /* all fragments truesize : remove (head size + sk_buff) */
3832 delta_truesize = skb->truesize -
3833 SKB_TRUESIZE(skb_end_offset(skb));
3835 skb->truesize -= skb->data_len;
3836 skb->len -= skb->data_len;
3839 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3841 } else if (skb->head_frag) {
3842 int nr_frags = pinfo->nr_frags;
3843 skb_frag_t *frag = pinfo->frags + nr_frags;
3844 struct page *page = virt_to_head_page(skb->head);
3845 unsigned int first_size = headlen - offset;
3846 unsigned int first_offset;
3848 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3851 first_offset = skb->data -
3852 (unsigned char *)page_address(page) +
3855 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3857 frag->page.p = page;
3858 frag->page_offset = first_offset;
3859 skb_frag_size_set(frag, first_size);
3861 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3862 /* We dont need to clear skbinfo->nr_frags here */
3864 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3865 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3870 delta_truesize = skb->truesize;
3871 if (offset > headlen) {
3872 unsigned int eat = offset - headlen;
3874 skbinfo->frags[0].page_offset += eat;
3875 skb_frag_size_sub(&skbinfo->frags[0], eat);
3876 skb->data_len -= eat;
3881 __skb_pull(skb, offset);
3883 if (NAPI_GRO_CB(p)->last == p)
3884 skb_shinfo(p)->frag_list = skb;
3886 NAPI_GRO_CB(p)->last->next = skb;
3887 NAPI_GRO_CB(p)->last = skb;
3888 __skb_header_release(skb);
3892 NAPI_GRO_CB(p)->count++;
3894 p->truesize += delta_truesize;
3897 lp->data_len += len;
3898 lp->truesize += delta_truesize;
3901 NAPI_GRO_CB(skb)->same_flow = 1;
3904 EXPORT_SYMBOL_GPL(skb_gro_receive);
3906 #ifdef CONFIG_SKB_EXTENSIONS
3907 #define SKB_EXT_ALIGN_VALUE 8
3908 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3910 static const u8 skb_ext_type_len[] = {
3911 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3912 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3915 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3919 static __always_inline unsigned int skb_ext_total_length(void)
3921 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3922 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3923 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3926 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3931 static void skb_extensions_init(void)
3933 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3934 BUILD_BUG_ON(skb_ext_total_length() > 255);
3936 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3937 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3939 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3943 static void skb_extensions_init(void) {}
3946 void __init skb_init(void)
3948 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
3949 sizeof(struct sk_buff),
3951 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3952 offsetof(struct sk_buff, cb),
3953 sizeof_field(struct sk_buff, cb),
3955 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3956 sizeof(struct sk_buff_fclones),
3958 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3960 skb_extensions_init();
3964 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3965 unsigned int recursion_level)
3967 int start = skb_headlen(skb);
3968 int i, copy = start - offset;
3969 struct sk_buff *frag_iter;
3972 if (unlikely(recursion_level >= 24))
3978 sg_set_buf(sg, skb->data + offset, copy);
3980 if ((len -= copy) == 0)
3985 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3988 WARN_ON(start > offset + len);
3990 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3991 if ((copy = end - offset) > 0) {
3992 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3993 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3998 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3999 frag->page_offset+offset-start);
4008 skb_walk_frags(skb, frag_iter) {
4011 WARN_ON(start > offset + len);
4013 end = start + frag_iter->len;
4014 if ((copy = end - offset) > 0) {
4015 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4020 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4021 copy, recursion_level + 1);
4022 if (unlikely(ret < 0))
4025 if ((len -= copy) == 0)
4036 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4037 * @skb: Socket buffer containing the buffers to be mapped
4038 * @sg: The scatter-gather list to map into
4039 * @offset: The offset into the buffer's contents to start mapping
4040 * @len: Length of buffer space to be mapped
4042 * Fill the specified scatter-gather list with mappings/pointers into a
4043 * region of the buffer space attached to a socket buffer. Returns either
4044 * the number of scatterlist items used, or -EMSGSIZE if the contents
4047 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4049 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4054 sg_mark_end(&sg[nsg - 1]);
4058 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4060 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4061 * sglist without mark the sg which contain last skb data as the end.
4062 * So the caller can mannipulate sg list as will when padding new data after
4063 * the first call without calling sg_unmark_end to expend sg list.
4065 * Scenario to use skb_to_sgvec_nomark:
4067 * 2. skb_to_sgvec_nomark(payload1)
4068 * 3. skb_to_sgvec_nomark(payload2)
4070 * This is equivalent to:
4072 * 2. skb_to_sgvec(payload1)
4074 * 4. skb_to_sgvec(payload2)
4076 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4077 * is more preferable.
4079 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4080 int offset, int len)
4082 return __skb_to_sgvec(skb, sg, offset, len, 0);
4084 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4089 * skb_cow_data - Check that a socket buffer's data buffers are writable
4090 * @skb: The socket buffer to check.
4091 * @tailbits: Amount of trailing space to be added
4092 * @trailer: Returned pointer to the skb where the @tailbits space begins
4094 * Make sure that the data buffers attached to a socket buffer are
4095 * writable. If they are not, private copies are made of the data buffers
4096 * and the socket buffer is set to use these instead.
4098 * If @tailbits is given, make sure that there is space to write @tailbits
4099 * bytes of data beyond current end of socket buffer. @trailer will be
4100 * set to point to the skb in which this space begins.
4102 * The number of scatterlist elements required to completely map the
4103 * COW'd and extended socket buffer will be returned.
4105 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4109 struct sk_buff *skb1, **skb_p;
4111 /* If skb is cloned or its head is paged, reallocate
4112 * head pulling out all the pages (pages are considered not writable
4113 * at the moment even if they are anonymous).
4115 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4116 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4119 /* Easy case. Most of packets will go this way. */
4120 if (!skb_has_frag_list(skb)) {
4121 /* A little of trouble, not enough of space for trailer.
4122 * This should not happen, when stack is tuned to generate
4123 * good frames. OK, on miss we reallocate and reserve even more
4124 * space, 128 bytes is fair. */
4126 if (skb_tailroom(skb) < tailbits &&
4127 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4135 /* Misery. We are in troubles, going to mincer fragments... */
4138 skb_p = &skb_shinfo(skb)->frag_list;
4141 while ((skb1 = *skb_p) != NULL) {
4144 /* The fragment is partially pulled by someone,
4145 * this can happen on input. Copy it and everything
4148 if (skb_shared(skb1))
4151 /* If the skb is the last, worry about trailer. */
4153 if (skb1->next == NULL && tailbits) {
4154 if (skb_shinfo(skb1)->nr_frags ||
4155 skb_has_frag_list(skb1) ||
4156 skb_tailroom(skb1) < tailbits)
4157 ntail = tailbits + 128;
4163 skb_shinfo(skb1)->nr_frags ||
4164 skb_has_frag_list(skb1)) {
4165 struct sk_buff *skb2;
4167 /* Fuck, we are miserable poor guys... */
4169 skb2 = skb_copy(skb1, GFP_ATOMIC);
4171 skb2 = skb_copy_expand(skb1,
4175 if (unlikely(skb2 == NULL))
4179 skb_set_owner_w(skb2, skb1->sk);
4181 /* Looking around. Are we still alive?
4182 * OK, link new skb, drop old one */
4184 skb2->next = skb1->next;
4191 skb_p = &skb1->next;
4196 EXPORT_SYMBOL_GPL(skb_cow_data);
4198 static void sock_rmem_free(struct sk_buff *skb)
4200 struct sock *sk = skb->sk;
4202 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4205 static void skb_set_err_queue(struct sk_buff *skb)
4207 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4208 * So, it is safe to (mis)use it to mark skbs on the error queue.
4210 skb->pkt_type = PACKET_OUTGOING;
4211 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4215 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4217 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4219 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4220 (unsigned int)sk->sk_rcvbuf)
4225 skb->destructor = sock_rmem_free;
4226 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4227 skb_set_err_queue(skb);
4229 /* before exiting rcu section, make sure dst is refcounted */
4232 skb_queue_tail(&sk->sk_error_queue, skb);
4233 if (!sock_flag(sk, SOCK_DEAD))
4234 sk->sk_error_report(sk);
4237 EXPORT_SYMBOL(sock_queue_err_skb);
4239 static bool is_icmp_err_skb(const struct sk_buff *skb)
4241 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4242 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4245 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4247 struct sk_buff_head *q = &sk->sk_error_queue;
4248 struct sk_buff *skb, *skb_next = NULL;
4249 bool icmp_next = false;
4250 unsigned long flags;
4252 spin_lock_irqsave(&q->lock, flags);
4253 skb = __skb_dequeue(q);
4254 if (skb && (skb_next = skb_peek(q))) {
4255 icmp_next = is_icmp_err_skb(skb_next);
4257 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4259 spin_unlock_irqrestore(&q->lock, flags);
4261 if (is_icmp_err_skb(skb) && !icmp_next)
4265 sk->sk_error_report(sk);
4269 EXPORT_SYMBOL(sock_dequeue_err_skb);
4272 * skb_clone_sk - create clone of skb, and take reference to socket
4273 * @skb: the skb to clone
4275 * This function creates a clone of a buffer that holds a reference on
4276 * sk_refcnt. Buffers created via this function are meant to be
4277 * returned using sock_queue_err_skb, or free via kfree_skb.
4279 * When passing buffers allocated with this function to sock_queue_err_skb
4280 * it is necessary to wrap the call with sock_hold/sock_put in order to
4281 * prevent the socket from being released prior to being enqueued on
4282 * the sk_error_queue.
4284 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4286 struct sock *sk = skb->sk;
4287 struct sk_buff *clone;
4289 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4292 clone = skb_clone(skb, GFP_ATOMIC);
4299 clone->destructor = sock_efree;
4303 EXPORT_SYMBOL(skb_clone_sk);
4305 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4310 struct sock_exterr_skb *serr;
4313 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4315 serr = SKB_EXT_ERR(skb);
4316 memset(serr, 0, sizeof(*serr));
4317 serr->ee.ee_errno = ENOMSG;
4318 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4319 serr->ee.ee_info = tstype;
4320 serr->opt_stats = opt_stats;
4321 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4322 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4323 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4324 if (sk->sk_protocol == IPPROTO_TCP &&
4325 sk->sk_type == SOCK_STREAM)
4326 serr->ee.ee_data -= sk->sk_tskey;
4329 err = sock_queue_err_skb(sk, skb);
4335 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4339 if (likely(sysctl_tstamp_allow_data || tsonly))
4342 read_lock_bh(&sk->sk_callback_lock);
4343 ret = sk->sk_socket && sk->sk_socket->file &&
4344 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4345 read_unlock_bh(&sk->sk_callback_lock);
4349 void skb_complete_tx_timestamp(struct sk_buff *skb,
4350 struct skb_shared_hwtstamps *hwtstamps)
4352 struct sock *sk = skb->sk;
4354 if (!skb_may_tx_timestamp(sk, false))
4357 /* Take a reference to prevent skb_orphan() from freeing the socket,
4358 * but only if the socket refcount is not zero.
4360 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4361 *skb_hwtstamps(skb) = *hwtstamps;
4362 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4370 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4372 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4373 struct skb_shared_hwtstamps *hwtstamps,
4374 struct sock *sk, int tstype)
4376 struct sk_buff *skb;
4377 bool tsonly, opt_stats = false;
4382 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4383 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4386 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4387 if (!skb_may_tx_timestamp(sk, tsonly))
4392 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4393 sk->sk_protocol == IPPROTO_TCP &&
4394 sk->sk_type == SOCK_STREAM) {
4395 skb = tcp_get_timestamping_opt_stats(sk);
4399 skb = alloc_skb(0, GFP_ATOMIC);
4401 skb = skb_clone(orig_skb, GFP_ATOMIC);
4407 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4409 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4413 *skb_hwtstamps(skb) = *hwtstamps;
4415 skb->tstamp = ktime_get_real();
4417 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4419 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4421 void skb_tstamp_tx(struct sk_buff *orig_skb,
4422 struct skb_shared_hwtstamps *hwtstamps)
4424 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4427 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4429 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4431 struct sock *sk = skb->sk;
4432 struct sock_exterr_skb *serr;
4435 skb->wifi_acked_valid = 1;
4436 skb->wifi_acked = acked;
4438 serr = SKB_EXT_ERR(skb);
4439 memset(serr, 0, sizeof(*serr));
4440 serr->ee.ee_errno = ENOMSG;
4441 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4443 /* Take a reference to prevent skb_orphan() from freeing the socket,
4444 * but only if the socket refcount is not zero.
4446 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4447 err = sock_queue_err_skb(sk, skb);
4453 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4456 * skb_partial_csum_set - set up and verify partial csum values for packet
4457 * @skb: the skb to set
4458 * @start: the number of bytes after skb->data to start checksumming.
4459 * @off: the offset from start to place the checksum.
4461 * For untrusted partially-checksummed packets, we need to make sure the values
4462 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4464 * This function checks and sets those values and skb->ip_summed: if this
4465 * returns false you should drop the packet.
4467 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4469 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4470 u32 csum_start = skb_headroom(skb) + (u32)start;
4472 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4473 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4474 start, off, skb_headroom(skb), skb_headlen(skb));
4477 skb->ip_summed = CHECKSUM_PARTIAL;
4478 skb->csum_start = csum_start;
4479 skb->csum_offset = off;
4480 skb_set_transport_header(skb, start);
4483 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4485 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4488 if (skb_headlen(skb) >= len)
4491 /* If we need to pullup then pullup to the max, so we
4492 * won't need to do it again.
4497 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4500 if (skb_headlen(skb) < len)
4506 #define MAX_TCP_HDR_LEN (15 * 4)
4508 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4509 typeof(IPPROTO_IP) proto,
4516 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4517 off + MAX_TCP_HDR_LEN);
4518 if (!err && !skb_partial_csum_set(skb, off,
4519 offsetof(struct tcphdr,
4522 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4525 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4526 off + sizeof(struct udphdr));
4527 if (!err && !skb_partial_csum_set(skb, off,
4528 offsetof(struct udphdr,
4531 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4534 return ERR_PTR(-EPROTO);
4537 /* This value should be large enough to cover a tagged ethernet header plus
4538 * maximally sized IP and TCP or UDP headers.
4540 #define MAX_IP_HDR_LEN 128
4542 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4551 err = skb_maybe_pull_tail(skb,
4552 sizeof(struct iphdr),
4557 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4560 off = ip_hdrlen(skb);
4567 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4569 return PTR_ERR(csum);
4572 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4575 ip_hdr(skb)->protocol, 0);
4582 /* This value should be large enough to cover a tagged ethernet header plus
4583 * an IPv6 header, all options, and a maximal TCP or UDP header.
4585 #define MAX_IPV6_HDR_LEN 256
4587 #define OPT_HDR(type, skb, off) \
4588 (type *)(skb_network_header(skb) + (off))
4590 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4603 off = sizeof(struct ipv6hdr);
4605 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4609 nexthdr = ipv6_hdr(skb)->nexthdr;
4611 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4612 while (off <= len && !done) {
4614 case IPPROTO_DSTOPTS:
4615 case IPPROTO_HOPOPTS:
4616 case IPPROTO_ROUTING: {
4617 struct ipv6_opt_hdr *hp;
4619 err = skb_maybe_pull_tail(skb,
4621 sizeof(struct ipv6_opt_hdr),
4626 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4627 nexthdr = hp->nexthdr;
4628 off += ipv6_optlen(hp);
4632 struct ip_auth_hdr *hp;
4634 err = skb_maybe_pull_tail(skb,
4636 sizeof(struct ip_auth_hdr),
4641 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4642 nexthdr = hp->nexthdr;
4643 off += ipv6_authlen(hp);
4646 case IPPROTO_FRAGMENT: {
4647 struct frag_hdr *hp;
4649 err = skb_maybe_pull_tail(skb,
4651 sizeof(struct frag_hdr),
4656 hp = OPT_HDR(struct frag_hdr, skb, off);
4658 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4661 nexthdr = hp->nexthdr;
4662 off += sizeof(struct frag_hdr);
4673 if (!done || fragment)
4676 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4678 return PTR_ERR(csum);
4681 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4682 &ipv6_hdr(skb)->daddr,
4683 skb->len - off, nexthdr, 0);
4691 * skb_checksum_setup - set up partial checksum offset
4692 * @skb: the skb to set up
4693 * @recalculate: if true the pseudo-header checksum will be recalculated
4695 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4699 switch (skb->protocol) {
4700 case htons(ETH_P_IP):
4701 err = skb_checksum_setup_ipv4(skb, recalculate);
4704 case htons(ETH_P_IPV6):
4705 err = skb_checksum_setup_ipv6(skb, recalculate);
4715 EXPORT_SYMBOL(skb_checksum_setup);
4718 * skb_checksum_maybe_trim - maybe trims the given skb
4719 * @skb: the skb to check
4720 * @transport_len: the data length beyond the network header
4722 * Checks whether the given skb has data beyond the given transport length.
4723 * If so, returns a cloned skb trimmed to this transport length.
4724 * Otherwise returns the provided skb. Returns NULL in error cases
4725 * (e.g. transport_len exceeds skb length or out-of-memory).
4727 * Caller needs to set the skb transport header and free any returned skb if it
4728 * differs from the provided skb.
4730 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4731 unsigned int transport_len)
4733 struct sk_buff *skb_chk;
4734 unsigned int len = skb_transport_offset(skb) + transport_len;
4739 else if (skb->len == len)
4742 skb_chk = skb_clone(skb, GFP_ATOMIC);
4746 ret = pskb_trim_rcsum(skb_chk, len);
4756 * skb_checksum_trimmed - validate checksum of an skb
4757 * @skb: the skb to check
4758 * @transport_len: the data length beyond the network header
4759 * @skb_chkf: checksum function to use
4761 * Applies the given checksum function skb_chkf to the provided skb.
4762 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4764 * If the skb has data beyond the given transport length, then a
4765 * trimmed & cloned skb is checked and returned.
4767 * Caller needs to set the skb transport header and free any returned skb if it
4768 * differs from the provided skb.
4770 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4771 unsigned int transport_len,
4772 __sum16(*skb_chkf)(struct sk_buff *skb))
4774 struct sk_buff *skb_chk;
4775 unsigned int offset = skb_transport_offset(skb);
4778 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4782 if (!pskb_may_pull(skb_chk, offset))
4785 skb_pull_rcsum(skb_chk, offset);
4786 ret = skb_chkf(skb_chk);
4787 skb_push_rcsum(skb_chk, offset);
4795 if (skb_chk && skb_chk != skb)
4801 EXPORT_SYMBOL(skb_checksum_trimmed);
4803 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4805 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4808 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4810 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4813 skb_release_head_state(skb);
4814 kmem_cache_free(skbuff_head_cache, skb);
4819 EXPORT_SYMBOL(kfree_skb_partial);
4822 * skb_try_coalesce - try to merge skb to prior one
4824 * @from: buffer to add
4825 * @fragstolen: pointer to boolean
4826 * @delta_truesize: how much more was allocated than was requested
4828 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4829 bool *fragstolen, int *delta_truesize)
4831 struct skb_shared_info *to_shinfo, *from_shinfo;
4832 int i, delta, len = from->len;
4834 *fragstolen = false;
4839 if (len <= skb_tailroom(to)) {
4841 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4842 *delta_truesize = 0;
4846 to_shinfo = skb_shinfo(to);
4847 from_shinfo = skb_shinfo(from);
4848 if (to_shinfo->frag_list || from_shinfo->frag_list)
4850 if (skb_zcopy(to) || skb_zcopy(from))
4853 if (skb_headlen(from) != 0) {
4855 unsigned int offset;
4857 if (to_shinfo->nr_frags +
4858 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
4861 if (skb_head_is_locked(from))
4864 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4866 page = virt_to_head_page(from->head);
4867 offset = from->data - (unsigned char *)page_address(page);
4869 skb_fill_page_desc(to, to_shinfo->nr_frags,
4870 page, offset, skb_headlen(from));
4873 if (to_shinfo->nr_frags +
4874 from_shinfo->nr_frags > MAX_SKB_FRAGS)
4877 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4880 WARN_ON_ONCE(delta < len);
4882 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4884 from_shinfo->nr_frags * sizeof(skb_frag_t));
4885 to_shinfo->nr_frags += from_shinfo->nr_frags;
4887 if (!skb_cloned(from))
4888 from_shinfo->nr_frags = 0;
4890 /* if the skb is not cloned this does nothing
4891 * since we set nr_frags to 0.
4893 for (i = 0; i < from_shinfo->nr_frags; i++)
4894 __skb_frag_ref(&from_shinfo->frags[i]);
4896 to->truesize += delta;
4898 to->data_len += len;
4900 *delta_truesize = delta;
4903 EXPORT_SYMBOL(skb_try_coalesce);
4906 * skb_scrub_packet - scrub an skb
4908 * @skb: buffer to clean
4909 * @xnet: packet is crossing netns
4911 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4912 * into/from a tunnel. Some information have to be cleared during these
4914 * skb_scrub_packet can also be used to clean a skb before injecting it in
4915 * another namespace (@xnet == true). We have to clear all information in the
4916 * skb that could impact namespace isolation.
4918 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4920 skb->pkt_type = PACKET_HOST;
4926 nf_reset_trace(skb);
4928 #ifdef CONFIG_NET_SWITCHDEV
4929 skb->offload_fwd_mark = 0;
4930 skb->offload_l3_fwd_mark = 0;
4940 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4943 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4947 * skb_gso_transport_seglen is used to determine the real size of the
4948 * individual segments, including Layer4 headers (TCP/UDP).
4950 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4952 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4954 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4955 unsigned int thlen = 0;
4957 if (skb->encapsulation) {
4958 thlen = skb_inner_transport_header(skb) -
4959 skb_transport_header(skb);
4961 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4962 thlen += inner_tcp_hdrlen(skb);
4963 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4964 thlen = tcp_hdrlen(skb);
4965 } else if (unlikely(skb_is_gso_sctp(skb))) {
4966 thlen = sizeof(struct sctphdr);
4967 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
4968 thlen = sizeof(struct udphdr);
4970 /* UFO sets gso_size to the size of the fragmentation
4971 * payload, i.e. the size of the L4 (UDP) header is already
4974 return thlen + shinfo->gso_size;
4978 * skb_gso_network_seglen - Return length of individual segments of a gso packet
4982 * skb_gso_network_seglen is used to determine the real size of the
4983 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
4985 * The MAC/L2 header is not accounted for.
4987 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
4989 unsigned int hdr_len = skb_transport_header(skb) -
4990 skb_network_header(skb);
4992 return hdr_len + skb_gso_transport_seglen(skb);
4996 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5000 * skb_gso_mac_seglen is used to determine the real size of the
5001 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5002 * headers (TCP/UDP).
5004 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5006 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5008 return hdr_len + skb_gso_transport_seglen(skb);
5012 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5014 * There are a couple of instances where we have a GSO skb, and we
5015 * want to determine what size it would be after it is segmented.
5017 * We might want to check:
5018 * - L3+L4+payload size (e.g. IP forwarding)
5019 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5021 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5025 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5026 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5028 * @max_len: The maximum permissible length.
5030 * Returns true if the segmented length <= max length.
5032 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5033 unsigned int seg_len,
5034 unsigned int max_len) {
5035 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5036 const struct sk_buff *iter;
5038 if (shinfo->gso_size != GSO_BY_FRAGS)
5039 return seg_len <= max_len;
5041 /* Undo this so we can re-use header sizes */
5042 seg_len -= GSO_BY_FRAGS;
5044 skb_walk_frags(skb, iter) {
5045 if (seg_len + skb_headlen(iter) > max_len)
5053 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5056 * @mtu: MTU to validate against
5058 * skb_gso_validate_network_len validates if a given skb will fit a
5059 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5062 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5064 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5066 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5069 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5072 * @len: length to validate against
5074 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5075 * length once split, including L2, L3 and L4 headers and the payload.
5077 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5079 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5081 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5083 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5087 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5092 mac_len = skb->data - skb_mac_header(skb);
5093 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5094 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5095 mac_len - VLAN_HLEN - ETH_TLEN);
5097 skb->mac_header += VLAN_HLEN;
5101 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5103 struct vlan_hdr *vhdr;
5106 if (unlikely(skb_vlan_tag_present(skb))) {
5107 /* vlan_tci is already set-up so leave this for another time */
5111 skb = skb_share_check(skb, GFP_ATOMIC);
5115 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5118 vhdr = (struct vlan_hdr *)skb->data;
5119 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5120 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5122 skb_pull_rcsum(skb, VLAN_HLEN);
5123 vlan_set_encap_proto(skb, vhdr);
5125 skb = skb_reorder_vlan_header(skb);
5129 skb_reset_network_header(skb);
5130 skb_reset_transport_header(skb);
5131 skb_reset_mac_len(skb);
5139 EXPORT_SYMBOL(skb_vlan_untag);
5141 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5143 if (!pskb_may_pull(skb, write_len))
5146 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5149 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5151 EXPORT_SYMBOL(skb_ensure_writable);
5153 /* remove VLAN header from packet and update csum accordingly.
5154 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5156 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5158 struct vlan_hdr *vhdr;
5159 int offset = skb->data - skb_mac_header(skb);
5162 if (WARN_ONCE(offset,
5163 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5168 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5172 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5174 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5175 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5177 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5178 __skb_pull(skb, VLAN_HLEN);
5180 vlan_set_encap_proto(skb, vhdr);
5181 skb->mac_header += VLAN_HLEN;
5183 if (skb_network_offset(skb) < ETH_HLEN)
5184 skb_set_network_header(skb, ETH_HLEN);
5186 skb_reset_mac_len(skb);
5190 EXPORT_SYMBOL(__skb_vlan_pop);
5192 /* Pop a vlan tag either from hwaccel or from payload.
5193 * Expects skb->data at mac header.
5195 int skb_vlan_pop(struct sk_buff *skb)
5201 if (likely(skb_vlan_tag_present(skb))) {
5202 __vlan_hwaccel_clear_tag(skb);
5204 if (unlikely(!eth_type_vlan(skb->protocol)))
5207 err = __skb_vlan_pop(skb, &vlan_tci);
5211 /* move next vlan tag to hw accel tag */
5212 if (likely(!eth_type_vlan(skb->protocol)))
5215 vlan_proto = skb->protocol;
5216 err = __skb_vlan_pop(skb, &vlan_tci);
5220 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5223 EXPORT_SYMBOL(skb_vlan_pop);
5225 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5226 * Expects skb->data at mac header.
5228 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5230 if (skb_vlan_tag_present(skb)) {
5231 int offset = skb->data - skb_mac_header(skb);
5234 if (WARN_ONCE(offset,
5235 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5240 err = __vlan_insert_tag(skb, skb->vlan_proto,
5241 skb_vlan_tag_get(skb));
5245 skb->protocol = skb->vlan_proto;
5246 skb->mac_len += VLAN_HLEN;
5248 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5250 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5253 EXPORT_SYMBOL(skb_vlan_push);
5256 * alloc_skb_with_frags - allocate skb with page frags
5258 * @header_len: size of linear part
5259 * @data_len: needed length in frags
5260 * @max_page_order: max page order desired.
5261 * @errcode: pointer to error code if any
5262 * @gfp_mask: allocation mask
5264 * This can be used to allocate a paged skb, given a maximal order for frags.
5266 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5267 unsigned long data_len,
5272 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5273 unsigned long chunk;
5274 struct sk_buff *skb;
5278 *errcode = -EMSGSIZE;
5279 /* Note this test could be relaxed, if we succeed to allocate
5280 * high order pages...
5282 if (npages > MAX_SKB_FRAGS)
5285 *errcode = -ENOBUFS;
5286 skb = alloc_skb(header_len, gfp_mask);
5290 skb->truesize += npages << PAGE_SHIFT;
5292 for (i = 0; npages > 0; i++) {
5293 int order = max_page_order;
5296 if (npages >= 1 << order) {
5297 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
5303 /* Do not retry other high order allocations */
5309 page = alloc_page(gfp_mask);
5313 chunk = min_t(unsigned long, data_len,
5314 PAGE_SIZE << order);
5315 skb_fill_page_desc(skb, i, page, 0, chunk);
5317 npages -= 1 << order;
5325 EXPORT_SYMBOL(alloc_skb_with_frags);
5327 /* carve out the first off bytes from skb when off < headlen */
5328 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5329 const int headlen, gfp_t gfp_mask)
5332 int size = skb_end_offset(skb);
5333 int new_hlen = headlen - off;
5336 size = SKB_DATA_ALIGN(size);
5338 if (skb_pfmemalloc(skb))
5339 gfp_mask |= __GFP_MEMALLOC;
5340 data = kmalloc_reserve(size +
5341 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5342 gfp_mask, NUMA_NO_NODE, NULL);
5346 size = SKB_WITH_OVERHEAD(ksize(data));
5348 /* Copy real data, and all frags */
5349 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5352 memcpy((struct skb_shared_info *)(data + size),
5354 offsetof(struct skb_shared_info,
5355 frags[skb_shinfo(skb)->nr_frags]));
5356 if (skb_cloned(skb)) {
5357 /* drop the old head gracefully */
5358 if (skb_orphan_frags(skb, gfp_mask)) {
5362 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5363 skb_frag_ref(skb, i);
5364 if (skb_has_frag_list(skb))
5365 skb_clone_fraglist(skb);
5366 skb_release_data(skb);
5368 /* we can reuse existing recount- all we did was
5377 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5380 skb->end = skb->head + size;
5382 skb_set_tail_pointer(skb, skb_headlen(skb));
5383 skb_headers_offset_update(skb, 0);
5387 atomic_set(&skb_shinfo(skb)->dataref, 1);
5392 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5394 /* carve out the first eat bytes from skb's frag_list. May recurse into
5397 static int pskb_carve_frag_list(struct sk_buff *skb,
5398 struct skb_shared_info *shinfo, int eat,
5401 struct sk_buff *list = shinfo->frag_list;
5402 struct sk_buff *clone = NULL;
5403 struct sk_buff *insp = NULL;
5407 pr_err("Not enough bytes to eat. Want %d\n", eat);
5410 if (list->len <= eat) {
5411 /* Eaten as whole. */
5416 /* Eaten partially. */
5417 if (skb_shared(list)) {
5418 clone = skb_clone(list, gfp_mask);
5424 /* This may be pulled without problems. */
5427 if (pskb_carve(list, eat, gfp_mask) < 0) {
5435 /* Free pulled out fragments. */
5436 while ((list = shinfo->frag_list) != insp) {
5437 shinfo->frag_list = list->next;
5440 /* And insert new clone at head. */
5443 shinfo->frag_list = clone;
5448 /* carve off first len bytes from skb. Split line (off) is in the
5449 * non-linear part of skb
5451 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5452 int pos, gfp_t gfp_mask)
5455 int size = skb_end_offset(skb);
5457 const int nfrags = skb_shinfo(skb)->nr_frags;
5458 struct skb_shared_info *shinfo;
5460 size = SKB_DATA_ALIGN(size);
5462 if (skb_pfmemalloc(skb))
5463 gfp_mask |= __GFP_MEMALLOC;
5464 data = kmalloc_reserve(size +
5465 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5466 gfp_mask, NUMA_NO_NODE, NULL);
5470 size = SKB_WITH_OVERHEAD(ksize(data));
5472 memcpy((struct skb_shared_info *)(data + size),
5473 skb_shinfo(skb), offsetof(struct skb_shared_info,
5474 frags[skb_shinfo(skb)->nr_frags]));
5475 if (skb_orphan_frags(skb, gfp_mask)) {
5479 shinfo = (struct skb_shared_info *)(data + size);
5480 for (i = 0; i < nfrags; i++) {
5481 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5483 if (pos + fsize > off) {
5484 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5488 * We have two variants in this case:
5489 * 1. Move all the frag to the second
5490 * part, if it is possible. F.e.
5491 * this approach is mandatory for TUX,
5492 * where splitting is expensive.
5493 * 2. Split is accurately. We make this.
5495 shinfo->frags[0].page_offset += off - pos;
5496 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5498 skb_frag_ref(skb, i);
5503 shinfo->nr_frags = k;
5504 if (skb_has_frag_list(skb))
5505 skb_clone_fraglist(skb);
5508 /* split line is in frag list */
5509 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5511 skb_release_data(skb);
5516 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5519 skb->end = skb->head + size;
5521 skb_reset_tail_pointer(skb);
5522 skb_headers_offset_update(skb, 0);
5527 skb->data_len = skb->len;
5528 atomic_set(&skb_shinfo(skb)->dataref, 1);
5532 /* remove len bytes from the beginning of the skb */
5533 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5535 int headlen = skb_headlen(skb);
5538 return pskb_carve_inside_header(skb, len, headlen, gfp);
5540 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5543 /* Extract to_copy bytes starting at off from skb, and return this in
5546 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5547 int to_copy, gfp_t gfp)
5549 struct sk_buff *clone = skb_clone(skb, gfp);
5554 if (pskb_carve(clone, off, gfp) < 0 ||
5555 pskb_trim(clone, to_copy)) {
5561 EXPORT_SYMBOL(pskb_extract);
5564 * skb_condense - try to get rid of fragments/frag_list if possible
5567 * Can be used to save memory before skb is added to a busy queue.
5568 * If packet has bytes in frags and enough tail room in skb->head,
5569 * pull all of them, so that we can free the frags right now and adjust
5572 * We do not reallocate skb->head thus can not fail.
5573 * Caller must re-evaluate skb->truesize if needed.
5575 void skb_condense(struct sk_buff *skb)
5577 if (skb->data_len) {
5578 if (skb->data_len > skb->end - skb->tail ||
5582 /* Nice, we can free page frag(s) right now */
5583 __pskb_pull_tail(skb, skb->data_len);
5585 /* At this point, skb->truesize might be over estimated,
5586 * because skb had a fragment, and fragments do not tell
5588 * When we pulled its content into skb->head, fragment
5589 * was freed, but __pskb_pull_tail() could not possibly
5590 * adjust skb->truesize, not knowing the frag truesize.
5592 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5595 #ifdef CONFIG_SKB_EXTENSIONS
5596 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5598 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5601 static struct skb_ext *skb_ext_alloc(void)
5603 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5606 memset(new->offset, 0, sizeof(new->offset));
5607 refcount_set(&new->refcnt, 1);
5613 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5614 unsigned int old_active)
5616 struct skb_ext *new;
5618 if (refcount_read(&old->refcnt) == 1)
5621 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5625 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5626 refcount_set(&new->refcnt, 1);
5629 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5630 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5633 for (i = 0; i < sp->len; i++)
5634 xfrm_state_hold(sp->xvec[i]);
5642 * skb_ext_add - allocate space for given extension, COW if needed
5644 * @id: extension to allocate space for
5646 * Allocates enough space for the given extension.
5647 * If the extension is already present, a pointer to that extension
5650 * If the skb was cloned, COW applies and the returned memory can be
5651 * modified without changing the extension space of clones buffers.
5653 * Returns pointer to the extension or NULL on allocation failure.
5655 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5657 struct skb_ext *new, *old = NULL;
5658 unsigned int newlen, newoff;
5660 if (skb->active_extensions) {
5661 old = skb->extensions;
5663 new = skb_ext_maybe_cow(old, skb->active_extensions);
5667 if (__skb_ext_exist(new, id))
5670 newoff = new->chunks;
5672 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5674 new = skb_ext_alloc();
5679 newlen = newoff + skb_ext_type_len[id];
5680 new->chunks = newlen;
5681 new->offset[id] = newoff;
5683 skb->extensions = new;
5684 skb->active_extensions |= 1 << id;
5685 return skb_ext_get_ptr(new, id);
5687 EXPORT_SYMBOL(skb_ext_add);
5690 static void skb_ext_put_sp(struct sec_path *sp)
5694 for (i = 0; i < sp->len; i++)
5695 xfrm_state_put(sp->xvec[i]);
5699 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5701 struct skb_ext *ext = skb->extensions;
5703 skb->active_extensions &= ~(1 << id);
5704 if (skb->active_extensions == 0) {
5705 skb->extensions = NULL;
5708 } else if (id == SKB_EXT_SEC_PATH &&
5709 refcount_read(&ext->refcnt) == 1) {
5710 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5717 EXPORT_SYMBOL(__skb_ext_del);
5719 void __skb_ext_put(struct skb_ext *ext)
5721 /* If this is last clone, nothing can increment
5722 * it after check passes. Avoids one atomic op.
5724 if (refcount_read(&ext->refcnt) == 1)
5727 if (!refcount_dec_and_test(&ext->refcnt))
5731 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5732 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5735 kmem_cache_free(skbuff_ext_cache, ext);
5737 EXPORT_SYMBOL(__skb_ext_put);
5738 #endif /* CONFIG_SKB_EXTENSIONS */