2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/tcp.h>
50 #include <linux/udp.h>
51 #include <linux/sctp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <linux/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83 EXPORT_SYMBOL(sysctl_max_skb_frags);
86 * skb_panic - private function for out-of-line support
90 * @msg: skb_over_panic or skb_under_panic
92 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 msg, addr, skb->len, sz, skb->head, skb->data,
102 (unsigned long)skb->tail, (unsigned long)skb->end,
103 skb->dev ? skb->dev->name : "<NULL>");
107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109 skb_panic(skb, sz, addr, __func__);
112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114 skb_panic(skb, sz, addr, __func__);
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 unsigned long ip, bool *pfmemalloc)
131 bool ret_pfmemalloc = false;
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
137 obj = kmalloc_node_track_caller(size,
138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 if (obj || !(gfp_pfmemalloc_allowed(flags)))
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc = true;
145 obj = kmalloc_node_track_caller(size, flags, node);
149 *pfmemalloc = ret_pfmemalloc;
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
161 * __alloc_skb - allocate a network buffer
162 * @size: size to allocate
163 * @gfp_mask: allocation mask
164 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
165 * instead of head cache and allocate a cloned (child) skb.
166 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
167 * allocations in case the data is required for writeback
168 * @node: numa node to allocate memory on
170 * Allocate a new &sk_buff. The returned buffer has no headroom and a
171 * tail room of at least size bytes. The object has a reference count
172 * of one. The return is the buffer. On a failure the return is %NULL.
174 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180 struct kmem_cache *cache;
181 struct skb_shared_info *shinfo;
186 cache = (flags & SKB_ALLOC_FCLONE)
187 ? skbuff_fclone_cache : skbuff_head_cache;
189 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
190 gfp_mask |= __GFP_MEMALLOC;
193 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
198 /* We do our best to align skb_shared_info on a separate cache
199 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
200 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
201 * Both skb->head and skb_shared_info are cache line aligned.
203 size = SKB_DATA_ALIGN(size);
204 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
205 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
208 /* kmalloc(size) might give us more room than requested.
209 * Put skb_shared_info exactly at the end of allocated zone,
210 * to allow max possible filling before reallocation.
212 size = SKB_WITH_OVERHEAD(ksize(data));
213 prefetchw(data + size);
216 * Only clear those fields we need to clear, not those that we will
217 * actually initialise below. Hence, don't put any more fields after
218 * the tail pointer in struct sk_buff!
220 memset(skb, 0, offsetof(struct sk_buff, tail));
221 /* Account for allocated memory : skb + skb->head */
222 skb->truesize = SKB_TRUESIZE(size);
223 skb->pfmemalloc = pfmemalloc;
224 refcount_set(&skb->users, 1);
227 skb_reset_tail_pointer(skb);
228 skb->end = skb->tail + size;
229 skb->mac_header = (typeof(skb->mac_header))~0U;
230 skb->transport_header = (typeof(skb->transport_header))~0U;
232 /* make sure we initialize shinfo sequentially */
233 shinfo = skb_shinfo(skb);
234 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
235 atomic_set(&shinfo->dataref, 1);
237 if (flags & SKB_ALLOC_FCLONE) {
238 struct sk_buff_fclones *fclones;
240 fclones = container_of(skb, struct sk_buff_fclones, skb1);
242 skb->fclone = SKB_FCLONE_ORIG;
243 refcount_set(&fclones->fclone_ref, 1);
245 fclones->skb2.fclone = SKB_FCLONE_CLONE;
250 kmem_cache_free(cache, skb);
254 EXPORT_SYMBOL(__alloc_skb);
257 * __build_skb - build a network buffer
258 * @data: data buffer provided by caller
259 * @frag_size: size of data, or 0 if head was kmalloced
261 * Allocate a new &sk_buff. Caller provides space holding head and
262 * skb_shared_info. @data must have been allocated by kmalloc() only if
263 * @frag_size is 0, otherwise data should come from the page allocator
265 * The return is the new skb buffer.
266 * On a failure the return is %NULL, and @data is not freed.
268 * Before IO, driver allocates only data buffer where NIC put incoming frame
269 * Driver should add room at head (NET_SKB_PAD) and
270 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
271 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
272 * before giving packet to stack.
273 * RX rings only contains data buffers, not full skbs.
275 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
277 struct skb_shared_info *shinfo;
279 unsigned int size = frag_size ? : ksize(data);
281 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
285 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
287 memset(skb, 0, offsetof(struct sk_buff, tail));
288 skb->truesize = SKB_TRUESIZE(size);
289 refcount_set(&skb->users, 1);
292 skb_reset_tail_pointer(skb);
293 skb->end = skb->tail + size;
294 skb->mac_header = (typeof(skb->mac_header))~0U;
295 skb->transport_header = (typeof(skb->transport_header))~0U;
297 /* make sure we initialize shinfo sequentially */
298 shinfo = skb_shinfo(skb);
299 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
300 atomic_set(&shinfo->dataref, 1);
305 /* build_skb() is wrapper over __build_skb(), that specifically
306 * takes care of skb->head and skb->pfmemalloc
307 * This means that if @frag_size is not zero, then @data must be backed
308 * by a page fragment, not kmalloc() or vmalloc()
310 struct sk_buff *build_skb(void *data, unsigned int frag_size)
312 struct sk_buff *skb = __build_skb(data, frag_size);
314 if (skb && frag_size) {
316 if (page_is_pfmemalloc(virt_to_head_page(data)))
321 EXPORT_SYMBOL(build_skb);
323 #define NAPI_SKB_CACHE_SIZE 64
325 struct napi_alloc_cache {
326 struct page_frag_cache page;
327 unsigned int skb_count;
328 void *skb_cache[NAPI_SKB_CACHE_SIZE];
331 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
332 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
334 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
336 struct page_frag_cache *nc;
340 local_irq_save(flags);
341 nc = this_cpu_ptr(&netdev_alloc_cache);
342 data = page_frag_alloc(nc, fragsz, gfp_mask);
343 local_irq_restore(flags);
348 * netdev_alloc_frag - allocate a page fragment
349 * @fragsz: fragment size
351 * Allocates a frag from a page for receive buffer.
352 * Uses GFP_ATOMIC allocations.
354 void *netdev_alloc_frag(unsigned int fragsz)
356 fragsz = SKB_DATA_ALIGN(fragsz);
358 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
360 EXPORT_SYMBOL(netdev_alloc_frag);
362 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
366 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
369 void *napi_alloc_frag(unsigned int fragsz)
371 fragsz = SKB_DATA_ALIGN(fragsz);
373 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
375 EXPORT_SYMBOL(napi_alloc_frag);
378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
380 * @len: length to allocate
381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
388 * %NULL is returned if there is no free memory.
390 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
393 struct page_frag_cache *nc;
401 /* If requested length is either too small or too big,
402 * we use kmalloc() for skb->head allocation.
404 if (len <= SKB_WITH_OVERHEAD(1024) ||
405 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
406 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
407 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
413 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
414 len = SKB_DATA_ALIGN(len);
416 if (sk_memalloc_socks())
417 gfp_mask |= __GFP_MEMALLOC;
419 local_irq_save(flags);
421 nc = this_cpu_ptr(&netdev_alloc_cache);
422 data = page_frag_alloc(nc, len, gfp_mask);
423 pfmemalloc = nc->pfmemalloc;
425 local_irq_restore(flags);
430 skb = __build_skb(data, len);
431 if (unlikely(!skb)) {
436 /* use OR instead of assignment to avoid clearing of bits in mask */
442 skb_reserve(skb, NET_SKB_PAD);
448 EXPORT_SYMBOL(__netdev_alloc_skb);
451 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
452 * @napi: napi instance this buffer was allocated for
453 * @len: length to allocate
454 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
456 * Allocate a new sk_buff for use in NAPI receive. This buffer will
457 * attempt to allocate the head from a special reserved region used
458 * only for NAPI Rx allocation. By doing this we can save several
459 * CPU cycles by avoiding having to disable and re-enable IRQs.
461 * %NULL is returned if there is no free memory.
463 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
466 struct napi_alloc_cache *nc;
470 len += NET_SKB_PAD + NET_IP_ALIGN;
472 /* If requested length is either too small or too big,
473 * we use kmalloc() for skb->head allocation.
475 if (len <= SKB_WITH_OVERHEAD(1024) ||
476 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
477 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
478 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
484 nc = this_cpu_ptr(&napi_alloc_cache);
485 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
486 len = SKB_DATA_ALIGN(len);
488 if (sk_memalloc_socks())
489 gfp_mask |= __GFP_MEMALLOC;
491 data = page_frag_alloc(&nc->page, len, gfp_mask);
495 skb = __build_skb(data, len);
496 if (unlikely(!skb)) {
501 /* use OR instead of assignment to avoid clearing of bits in mask */
502 if (nc->page.pfmemalloc)
507 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
508 skb->dev = napi->dev;
513 EXPORT_SYMBOL(__napi_alloc_skb);
515 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
516 int size, unsigned int truesize)
518 skb_fill_page_desc(skb, i, page, off, size);
520 skb->data_len += size;
521 skb->truesize += truesize;
523 EXPORT_SYMBOL(skb_add_rx_frag);
525 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
526 unsigned int truesize)
528 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
530 skb_frag_size_add(frag, size);
532 skb->data_len += size;
533 skb->truesize += truesize;
535 EXPORT_SYMBOL(skb_coalesce_rx_frag);
537 static void skb_drop_list(struct sk_buff **listp)
539 kfree_skb_list(*listp);
543 static inline void skb_drop_fraglist(struct sk_buff *skb)
545 skb_drop_list(&skb_shinfo(skb)->frag_list);
548 static void skb_clone_fraglist(struct sk_buff *skb)
550 struct sk_buff *list;
552 skb_walk_frags(skb, list)
556 static void skb_free_head(struct sk_buff *skb)
558 unsigned char *head = skb->head;
566 static void skb_release_data(struct sk_buff *skb)
568 struct skb_shared_info *shinfo = skb_shinfo(skb);
572 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
576 for (i = 0; i < shinfo->nr_frags; i++)
577 __skb_frag_unref(&shinfo->frags[i]);
579 if (shinfo->frag_list)
580 kfree_skb_list(shinfo->frag_list);
582 skb_zcopy_clear(skb, true);
587 * Free an skbuff by memory without cleaning the state.
589 static void kfree_skbmem(struct sk_buff *skb)
591 struct sk_buff_fclones *fclones;
593 switch (skb->fclone) {
594 case SKB_FCLONE_UNAVAILABLE:
595 kmem_cache_free(skbuff_head_cache, skb);
598 case SKB_FCLONE_ORIG:
599 fclones = container_of(skb, struct sk_buff_fclones, skb1);
601 /* We usually free the clone (TX completion) before original skb
602 * This test would have no chance to be true for the clone,
603 * while here, branch prediction will be good.
605 if (refcount_read(&fclones->fclone_ref) == 1)
609 default: /* SKB_FCLONE_CLONE */
610 fclones = container_of(skb, struct sk_buff_fclones, skb2);
613 if (!refcount_dec_and_test(&fclones->fclone_ref))
616 kmem_cache_free(skbuff_fclone_cache, fclones);
619 void skb_release_head_state(struct sk_buff *skb)
623 if (skb->destructor) {
625 skb->destructor(skb);
627 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
628 nf_conntrack_put(skb_nfct(skb));
630 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
631 nf_bridge_put(skb->nf_bridge);
635 /* Free everything but the sk_buff shell. */
636 static void skb_release_all(struct sk_buff *skb)
638 skb_release_head_state(skb);
639 if (likely(skb->head))
640 skb_release_data(skb);
644 * __kfree_skb - private function
647 * Free an sk_buff. Release anything attached to the buffer.
648 * Clean the state. This is an internal helper function. Users should
649 * always call kfree_skb
652 void __kfree_skb(struct sk_buff *skb)
654 skb_release_all(skb);
657 EXPORT_SYMBOL(__kfree_skb);
660 * kfree_skb - free an sk_buff
661 * @skb: buffer to free
663 * Drop a reference to the buffer and free it if the usage count has
666 void kfree_skb(struct sk_buff *skb)
671 trace_kfree_skb(skb, __builtin_return_address(0));
674 EXPORT_SYMBOL(kfree_skb);
676 void kfree_skb_list(struct sk_buff *segs)
679 struct sk_buff *next = segs->next;
685 EXPORT_SYMBOL(kfree_skb_list);
688 * skb_tx_error - report an sk_buff xmit error
689 * @skb: buffer that triggered an error
691 * Report xmit error if a device callback is tracking this skb.
692 * skb must be freed afterwards.
694 void skb_tx_error(struct sk_buff *skb)
696 skb_zcopy_clear(skb, true);
698 EXPORT_SYMBOL(skb_tx_error);
701 * consume_skb - free an skbuff
702 * @skb: buffer to free
704 * Drop a ref to the buffer and free it if the usage count has hit zero
705 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
706 * is being dropped after a failure and notes that
708 void consume_skb(struct sk_buff *skb)
713 trace_consume_skb(skb);
716 EXPORT_SYMBOL(consume_skb);
719 * consume_stateless_skb - free an skbuff, assuming it is stateless
720 * @skb: buffer to free
722 * Alike consume_skb(), but this variant assumes that this is the last
723 * skb reference and all the head states have been already dropped
725 void __consume_stateless_skb(struct sk_buff *skb)
727 trace_consume_skb(skb);
728 skb_release_data(skb);
732 void __kfree_skb_flush(void)
734 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
736 /* flush skb_cache if containing objects */
738 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
744 static inline void _kfree_skb_defer(struct sk_buff *skb)
746 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
748 /* drop skb->head and call any destructors for packet */
749 skb_release_all(skb);
751 /* record skb to CPU local list */
752 nc->skb_cache[nc->skb_count++] = skb;
755 /* SLUB writes into objects when freeing */
759 /* flush skb_cache if it is filled */
760 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
761 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
766 void __kfree_skb_defer(struct sk_buff *skb)
768 _kfree_skb_defer(skb);
771 void napi_consume_skb(struct sk_buff *skb, int budget)
776 /* Zero budget indicate non-NAPI context called us, like netpoll */
777 if (unlikely(!budget)) {
778 dev_consume_skb_any(skb);
785 /* if reaching here SKB is ready to free */
786 trace_consume_skb(skb);
788 /* if SKB is a clone, don't handle this case */
789 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
794 _kfree_skb_defer(skb);
796 EXPORT_SYMBOL(napi_consume_skb);
798 /* Make sure a field is enclosed inside headers_start/headers_end section */
799 #define CHECK_SKB_FIELD(field) \
800 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
801 offsetof(struct sk_buff, headers_start)); \
802 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
803 offsetof(struct sk_buff, headers_end)); \
805 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
807 new->tstamp = old->tstamp;
808 /* We do not copy old->sk */
810 memcpy(new->cb, old->cb, sizeof(old->cb));
811 skb_dst_copy(new, old);
813 new->sp = secpath_get(old->sp);
815 __nf_copy(new, old, false);
817 /* Note : this field could be in headers_start/headers_end section
818 * It is not yet because we do not want to have a 16 bit hole
820 new->queue_mapping = old->queue_mapping;
822 memcpy(&new->headers_start, &old->headers_start,
823 offsetof(struct sk_buff, headers_end) -
824 offsetof(struct sk_buff, headers_start));
825 CHECK_SKB_FIELD(protocol);
826 CHECK_SKB_FIELD(csum);
827 CHECK_SKB_FIELD(hash);
828 CHECK_SKB_FIELD(priority);
829 CHECK_SKB_FIELD(skb_iif);
830 CHECK_SKB_FIELD(vlan_proto);
831 CHECK_SKB_FIELD(vlan_tci);
832 CHECK_SKB_FIELD(transport_header);
833 CHECK_SKB_FIELD(network_header);
834 CHECK_SKB_FIELD(mac_header);
835 CHECK_SKB_FIELD(inner_protocol);
836 CHECK_SKB_FIELD(inner_transport_header);
837 CHECK_SKB_FIELD(inner_network_header);
838 CHECK_SKB_FIELD(inner_mac_header);
839 CHECK_SKB_FIELD(mark);
840 #ifdef CONFIG_NETWORK_SECMARK
841 CHECK_SKB_FIELD(secmark);
843 #ifdef CONFIG_NET_RX_BUSY_POLL
844 CHECK_SKB_FIELD(napi_id);
847 CHECK_SKB_FIELD(sender_cpu);
849 #ifdef CONFIG_NET_SCHED
850 CHECK_SKB_FIELD(tc_index);
856 * You should not add any new code to this function. Add it to
857 * __copy_skb_header above instead.
859 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
861 #define C(x) n->x = skb->x
863 n->next = n->prev = NULL;
865 __copy_skb_header(n, skb);
870 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
875 n->destructor = NULL;
882 refcount_set(&n->users, 1);
884 atomic_inc(&(skb_shinfo(skb)->dataref));
892 * skb_morph - morph one skb into another
893 * @dst: the skb to receive the contents
894 * @src: the skb to supply the contents
896 * This is identical to skb_clone except that the target skb is
897 * supplied by the user.
899 * The target skb is returned upon exit.
901 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
903 skb_release_all(dst);
904 return __skb_clone(dst, src);
906 EXPORT_SYMBOL_GPL(skb_morph);
908 static int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
910 unsigned long max_pg, num_pg, new_pg, old_pg;
911 struct user_struct *user;
913 if (capable(CAP_IPC_LOCK) || !size)
916 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
917 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
918 user = mmp->user ? : current_user();
921 old_pg = atomic_long_read(&user->locked_vm);
922 new_pg = old_pg + num_pg;
925 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
929 mmp->user = get_uid(user);
930 mmp->num_pg = num_pg;
932 mmp->num_pg += num_pg;
938 static void mm_unaccount_pinned_pages(struct mmpin *mmp)
941 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
946 struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
948 struct ubuf_info *uarg;
951 WARN_ON_ONCE(!in_task());
953 skb = sock_omalloc(sk, 0, GFP_KERNEL);
957 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
958 uarg = (void *)skb->cb;
959 uarg->mmp.user = NULL;
961 if (mm_account_pinned_pages(&uarg->mmp, size)) {
966 uarg->callback = sock_zerocopy_callback;
967 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
969 uarg->bytelen = size;
971 refcount_set(&uarg->refcnt, 1);
976 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
978 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
980 return container_of((void *)uarg, struct sk_buff, cb);
983 struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
984 struct ubuf_info *uarg)
987 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
990 /* realloc only when socket is locked (TCP, UDP cork),
991 * so uarg->len and sk_zckey access is serialized
993 if (!sock_owned_by_user(sk)) {
998 bytelen = uarg->bytelen + size;
999 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1000 /* TCP can create new skb to attach new uarg */
1001 if (sk->sk_type == SOCK_STREAM)
1006 next = (u32)atomic_read(&sk->sk_zckey);
1007 if ((u32)(uarg->id + uarg->len) == next) {
1008 if (mm_account_pinned_pages(&uarg->mmp, size))
1011 uarg->bytelen = bytelen;
1012 atomic_set(&sk->sk_zckey, ++next);
1013 sock_zerocopy_get(uarg);
1019 return sock_zerocopy_alloc(sk, size);
1021 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1023 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1025 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1029 old_lo = serr->ee.ee_info;
1030 old_hi = serr->ee.ee_data;
1031 sum_len = old_hi - old_lo + 1ULL + len;
1033 if (sum_len >= (1ULL << 32))
1036 if (lo != old_hi + 1)
1039 serr->ee.ee_data += len;
1043 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1045 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1046 struct sock_exterr_skb *serr;
1047 struct sock *sk = skb->sk;
1048 struct sk_buff_head *q;
1049 unsigned long flags;
1053 mm_unaccount_pinned_pages(&uarg->mmp);
1055 /* if !len, there was only 1 call, and it was aborted
1056 * so do not queue a completion notification
1058 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1063 hi = uarg->id + len - 1;
1065 serr = SKB_EXT_ERR(skb);
1066 memset(serr, 0, sizeof(*serr));
1067 serr->ee.ee_errno = 0;
1068 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1069 serr->ee.ee_data = hi;
1070 serr->ee.ee_info = lo;
1072 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1074 q = &sk->sk_error_queue;
1075 spin_lock_irqsave(&q->lock, flags);
1076 tail = skb_peek_tail(q);
1077 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1078 !skb_zerocopy_notify_extend(tail, lo, len)) {
1079 __skb_queue_tail(q, skb);
1082 spin_unlock_irqrestore(&q->lock, flags);
1084 sk->sk_error_report(sk);
1090 EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1092 void sock_zerocopy_put(struct ubuf_info *uarg)
1094 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
1096 uarg->callback(uarg, uarg->zerocopy);
1098 consume_skb(skb_from_uarg(uarg));
1101 EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1103 void sock_zerocopy_put_abort(struct ubuf_info *uarg)
1106 struct sock *sk = skb_from_uarg(uarg)->sk;
1108 atomic_dec(&sk->sk_zckey);
1111 sock_zerocopy_put(uarg);
1114 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1116 extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1117 struct iov_iter *from, size_t length);
1119 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1120 struct msghdr *msg, int len,
1121 struct ubuf_info *uarg)
1123 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1124 struct iov_iter orig_iter = msg->msg_iter;
1125 int err, orig_len = skb->len;
1127 /* An skb can only point to one uarg. This edge case happens when
1128 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1130 if (orig_uarg && uarg != orig_uarg)
1133 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1134 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1135 struct sock *save_sk = skb->sk;
1137 /* Streams do not free skb on error. Reset to prev state. */
1138 msg->msg_iter = orig_iter;
1140 ___pskb_trim(skb, orig_len);
1145 skb_zcopy_set(skb, uarg);
1146 return skb->len - orig_len;
1148 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1150 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1153 if (skb_zcopy(orig)) {
1154 if (skb_zcopy(nskb)) {
1155 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1160 if (skb_uarg(nskb) == skb_uarg(orig))
1162 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1165 skb_zcopy_set(nskb, skb_uarg(orig));
1171 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1172 * @skb: the skb to modify
1173 * @gfp_mask: allocation priority
1175 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1176 * It will copy all frags into kernel and drop the reference
1177 * to userspace pages.
1179 * If this function is called from an interrupt gfp_mask() must be
1182 * Returns 0 on success or a negative error code on failure
1183 * to allocate kernel memory to copy to.
1185 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1187 int num_frags = skb_shinfo(skb)->nr_frags;
1188 struct page *page, *head = NULL;
1192 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1198 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1199 for (i = 0; i < new_frags; i++) {
1200 page = alloc_page(gfp_mask);
1203 struct page *next = (struct page *)page_private(head);
1209 set_page_private(page, (unsigned long)head);
1215 for (i = 0; i < num_frags; i++) {
1216 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1217 u32 p_off, p_len, copied;
1221 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1222 p, p_off, p_len, copied) {
1224 vaddr = kmap_atomic(p);
1226 while (done < p_len) {
1227 if (d_off == PAGE_SIZE) {
1229 page = (struct page *)page_private(page);
1231 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1232 memcpy(page_address(page) + d_off,
1233 vaddr + p_off + done, copy);
1237 kunmap_atomic(vaddr);
1241 /* skb frags release userspace buffers */
1242 for (i = 0; i < num_frags; i++)
1243 skb_frag_unref(skb, i);
1245 /* skb frags point to kernel buffers */
1246 for (i = 0; i < new_frags - 1; i++) {
1247 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1248 head = (struct page *)page_private(head);
1250 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1251 skb_shinfo(skb)->nr_frags = new_frags;
1254 skb_zcopy_clear(skb, false);
1257 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1260 * skb_clone - duplicate an sk_buff
1261 * @skb: buffer to clone
1262 * @gfp_mask: allocation priority
1264 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1265 * copies share the same packet data but not structure. The new
1266 * buffer has a reference count of 1. If the allocation fails the
1267 * function returns %NULL otherwise the new buffer is returned.
1269 * If this function is called from an interrupt gfp_mask() must be
1273 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1275 struct sk_buff_fclones *fclones = container_of(skb,
1276 struct sk_buff_fclones,
1280 if (skb_orphan_frags(skb, gfp_mask))
1283 if (skb->fclone == SKB_FCLONE_ORIG &&
1284 refcount_read(&fclones->fclone_ref) == 1) {
1286 refcount_set(&fclones->fclone_ref, 2);
1288 if (skb_pfmemalloc(skb))
1289 gfp_mask |= __GFP_MEMALLOC;
1291 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1295 n->fclone = SKB_FCLONE_UNAVAILABLE;
1298 return __skb_clone(n, skb);
1300 EXPORT_SYMBOL(skb_clone);
1302 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1304 /* Only adjust this if it actually is csum_start rather than csum */
1305 if (skb->ip_summed == CHECKSUM_PARTIAL)
1306 skb->csum_start += off;
1307 /* {transport,network,mac}_header and tail are relative to skb->head */
1308 skb->transport_header += off;
1309 skb->network_header += off;
1310 if (skb_mac_header_was_set(skb))
1311 skb->mac_header += off;
1312 skb->inner_transport_header += off;
1313 skb->inner_network_header += off;
1314 skb->inner_mac_header += off;
1317 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1319 __copy_skb_header(new, old);
1321 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1322 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1323 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1325 EXPORT_SYMBOL(skb_copy_header);
1327 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1329 if (skb_pfmemalloc(skb))
1330 return SKB_ALLOC_RX;
1335 * skb_copy - create private copy of an sk_buff
1336 * @skb: buffer to copy
1337 * @gfp_mask: allocation priority
1339 * Make a copy of both an &sk_buff and its data. This is used when the
1340 * caller wishes to modify the data and needs a private copy of the
1341 * data to alter. Returns %NULL on failure or the pointer to the buffer
1342 * on success. The returned buffer has a reference count of 1.
1344 * As by-product this function converts non-linear &sk_buff to linear
1345 * one, so that &sk_buff becomes completely private and caller is allowed
1346 * to modify all the data of returned buffer. This means that this
1347 * function is not recommended for use in circumstances when only
1348 * header is going to be modified. Use pskb_copy() instead.
1351 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1353 int headerlen = skb_headroom(skb);
1354 unsigned int size = skb_end_offset(skb) + skb->data_len;
1355 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1356 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1361 /* Set the data pointer */
1362 skb_reserve(n, headerlen);
1363 /* Set the tail pointer and length */
1364 skb_put(n, skb->len);
1366 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1369 skb_copy_header(n, skb);
1372 EXPORT_SYMBOL(skb_copy);
1375 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1376 * @skb: buffer to copy
1377 * @headroom: headroom of new skb
1378 * @gfp_mask: allocation priority
1379 * @fclone: if true allocate the copy of the skb from the fclone
1380 * cache instead of the head cache; it is recommended to set this
1381 * to true for the cases where the copy will likely be cloned
1383 * Make a copy of both an &sk_buff and part of its data, located
1384 * in header. Fragmented data remain shared. This is used when
1385 * the caller wishes to modify only header of &sk_buff and needs
1386 * private copy of the header to alter. Returns %NULL on failure
1387 * or the pointer to the buffer on success.
1388 * The returned buffer has a reference count of 1.
1391 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1392 gfp_t gfp_mask, bool fclone)
1394 unsigned int size = skb_headlen(skb) + headroom;
1395 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1396 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1401 /* Set the data pointer */
1402 skb_reserve(n, headroom);
1403 /* Set the tail pointer and length */
1404 skb_put(n, skb_headlen(skb));
1405 /* Copy the bytes */
1406 skb_copy_from_linear_data(skb, n->data, n->len);
1408 n->truesize += skb->data_len;
1409 n->data_len = skb->data_len;
1412 if (skb_shinfo(skb)->nr_frags) {
1415 if (skb_orphan_frags(skb, gfp_mask) ||
1416 skb_zerocopy_clone(n, skb, gfp_mask)) {
1421 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1422 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1423 skb_frag_ref(skb, i);
1425 skb_shinfo(n)->nr_frags = i;
1428 if (skb_has_frag_list(skb)) {
1429 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1430 skb_clone_fraglist(n);
1433 skb_copy_header(n, skb);
1437 EXPORT_SYMBOL(__pskb_copy_fclone);
1440 * pskb_expand_head - reallocate header of &sk_buff
1441 * @skb: buffer to reallocate
1442 * @nhead: room to add at head
1443 * @ntail: room to add at tail
1444 * @gfp_mask: allocation priority
1446 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1447 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1448 * reference count of 1. Returns zero in the case of success or error,
1449 * if expansion failed. In the last case, &sk_buff is not changed.
1451 * All the pointers pointing into skb header may change and must be
1452 * reloaded after call to this function.
1455 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1458 int i, osize = skb_end_offset(skb);
1459 int size = osize + nhead + ntail;
1465 if (skb_shared(skb))
1468 size = SKB_DATA_ALIGN(size);
1470 if (skb_pfmemalloc(skb))
1471 gfp_mask |= __GFP_MEMALLOC;
1472 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1473 gfp_mask, NUMA_NO_NODE, NULL);
1476 size = SKB_WITH_OVERHEAD(ksize(data));
1478 /* Copy only real data... and, alas, header. This should be
1479 * optimized for the cases when header is void.
1481 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1483 memcpy((struct skb_shared_info *)(data + size),
1485 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1488 * if shinfo is shared we must drop the old head gracefully, but if it
1489 * is not we can just drop the old head and let the existing refcount
1490 * be since all we did is relocate the values
1492 if (skb_cloned(skb)) {
1493 if (skb_orphan_frags(skb, gfp_mask))
1496 refcount_inc(&skb_uarg(skb)->refcnt);
1497 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1498 skb_frag_ref(skb, i);
1500 if (skb_has_frag_list(skb))
1501 skb_clone_fraglist(skb);
1503 skb_release_data(skb);
1507 off = (data + nhead) - skb->head;
1512 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1516 skb->end = skb->head + size;
1519 skb_headers_offset_update(skb, nhead);
1523 atomic_set(&skb_shinfo(skb)->dataref, 1);
1525 /* It is not generally safe to change skb->truesize.
1526 * For the moment, we really care of rx path, or
1527 * when skb is orphaned (not attached to a socket).
1529 if (!skb->sk || skb->destructor == sock_edemux)
1530 skb->truesize += size - osize;
1539 EXPORT_SYMBOL(pskb_expand_head);
1541 /* Make private copy of skb with writable head and some headroom */
1543 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1545 struct sk_buff *skb2;
1546 int delta = headroom - skb_headroom(skb);
1549 skb2 = pskb_copy(skb, GFP_ATOMIC);
1551 skb2 = skb_clone(skb, GFP_ATOMIC);
1552 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1560 EXPORT_SYMBOL(skb_realloc_headroom);
1563 * skb_copy_expand - copy and expand sk_buff
1564 * @skb: buffer to copy
1565 * @newheadroom: new free bytes at head
1566 * @newtailroom: new free bytes at tail
1567 * @gfp_mask: allocation priority
1569 * Make a copy of both an &sk_buff and its data and while doing so
1570 * allocate additional space.
1572 * This is used when the caller wishes to modify the data and needs a
1573 * private copy of the data to alter as well as more space for new fields.
1574 * Returns %NULL on failure or the pointer to the buffer
1575 * on success. The returned buffer has a reference count of 1.
1577 * You must pass %GFP_ATOMIC as the allocation priority if this function
1578 * is called from an interrupt.
1580 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1581 int newheadroom, int newtailroom,
1585 * Allocate the copy buffer
1587 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1588 gfp_mask, skb_alloc_rx_flag(skb),
1590 int oldheadroom = skb_headroom(skb);
1591 int head_copy_len, head_copy_off;
1596 skb_reserve(n, newheadroom);
1598 /* Set the tail pointer and length */
1599 skb_put(n, skb->len);
1601 head_copy_len = oldheadroom;
1603 if (newheadroom <= head_copy_len)
1604 head_copy_len = newheadroom;
1606 head_copy_off = newheadroom - head_copy_len;
1608 /* Copy the linear header and data. */
1609 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1610 skb->len + head_copy_len))
1613 skb_copy_header(n, skb);
1615 skb_headers_offset_update(n, newheadroom - oldheadroom);
1619 EXPORT_SYMBOL(skb_copy_expand);
1622 * __skb_pad - zero pad the tail of an skb
1623 * @skb: buffer to pad
1624 * @pad: space to pad
1625 * @free_on_error: free buffer on error
1627 * Ensure that a buffer is followed by a padding area that is zero
1628 * filled. Used by network drivers which may DMA or transfer data
1629 * beyond the buffer end onto the wire.
1631 * May return error in out of memory cases. The skb is freed on error
1632 * if @free_on_error is true.
1635 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1640 /* If the skbuff is non linear tailroom is always zero.. */
1641 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1642 memset(skb->data+skb->len, 0, pad);
1646 ntail = skb->data_len + pad - (skb->end - skb->tail);
1647 if (likely(skb_cloned(skb) || ntail > 0)) {
1648 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1653 /* FIXME: The use of this function with non-linear skb's really needs
1656 err = skb_linearize(skb);
1660 memset(skb->data + skb->len, 0, pad);
1668 EXPORT_SYMBOL(__skb_pad);
1671 * pskb_put - add data to the tail of a potentially fragmented buffer
1672 * @skb: start of the buffer to use
1673 * @tail: tail fragment of the buffer to use
1674 * @len: amount of data to add
1676 * This function extends the used data area of the potentially
1677 * fragmented buffer. @tail must be the last fragment of @skb -- or
1678 * @skb itself. If this would exceed the total buffer size the kernel
1679 * will panic. A pointer to the first byte of the extra data is
1683 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1686 skb->data_len += len;
1689 return skb_put(tail, len);
1691 EXPORT_SYMBOL_GPL(pskb_put);
1694 * skb_put - add data to a buffer
1695 * @skb: buffer to use
1696 * @len: amount of data to add
1698 * This function extends the used data area of the buffer. If this would
1699 * exceed the total buffer size the kernel will panic. A pointer to the
1700 * first byte of the extra data is returned.
1702 void *skb_put(struct sk_buff *skb, unsigned int len)
1704 void *tmp = skb_tail_pointer(skb);
1705 SKB_LINEAR_ASSERT(skb);
1708 if (unlikely(skb->tail > skb->end))
1709 skb_over_panic(skb, len, __builtin_return_address(0));
1712 EXPORT_SYMBOL(skb_put);
1715 * skb_push - add data to the start of a buffer
1716 * @skb: buffer to use
1717 * @len: amount of data to add
1719 * This function extends the used data area of the buffer at the buffer
1720 * start. If this would exceed the total buffer headroom the kernel will
1721 * panic. A pointer to the first byte of the extra data is returned.
1723 void *skb_push(struct sk_buff *skb, unsigned int len)
1727 if (unlikely(skb->data<skb->head))
1728 skb_under_panic(skb, len, __builtin_return_address(0));
1731 EXPORT_SYMBOL(skb_push);
1734 * skb_pull - remove data from the start of a buffer
1735 * @skb: buffer to use
1736 * @len: amount of data to remove
1738 * This function removes data from the start of a buffer, returning
1739 * the memory to the headroom. A pointer to the next data in the buffer
1740 * is returned. Once the data has been pulled future pushes will overwrite
1743 void *skb_pull(struct sk_buff *skb, unsigned int len)
1745 return skb_pull_inline(skb, len);
1747 EXPORT_SYMBOL(skb_pull);
1750 * skb_trim - remove end from a buffer
1751 * @skb: buffer to alter
1754 * Cut the length of a buffer down by removing data from the tail. If
1755 * the buffer is already under the length specified it is not modified.
1756 * The skb must be linear.
1758 void skb_trim(struct sk_buff *skb, unsigned int len)
1761 __skb_trim(skb, len);
1763 EXPORT_SYMBOL(skb_trim);
1765 /* Trims skb to length len. It can change skb pointers.
1768 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1770 struct sk_buff **fragp;
1771 struct sk_buff *frag;
1772 int offset = skb_headlen(skb);
1773 int nfrags = skb_shinfo(skb)->nr_frags;
1777 if (skb_cloned(skb) &&
1778 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1785 for (; i < nfrags; i++) {
1786 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1793 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1796 skb_shinfo(skb)->nr_frags = i;
1798 for (; i < nfrags; i++)
1799 skb_frag_unref(skb, i);
1801 if (skb_has_frag_list(skb))
1802 skb_drop_fraglist(skb);
1806 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1807 fragp = &frag->next) {
1808 int end = offset + frag->len;
1810 if (skb_shared(frag)) {
1811 struct sk_buff *nfrag;
1813 nfrag = skb_clone(frag, GFP_ATOMIC);
1814 if (unlikely(!nfrag))
1817 nfrag->next = frag->next;
1829 unlikely((err = pskb_trim(frag, len - offset))))
1833 skb_drop_list(&frag->next);
1838 if (len > skb_headlen(skb)) {
1839 skb->data_len -= skb->len - len;
1844 skb_set_tail_pointer(skb, len);
1847 if (!skb->sk || skb->destructor == sock_edemux)
1851 EXPORT_SYMBOL(___pskb_trim);
1853 /* Note : use pskb_trim_rcsum() instead of calling this directly
1855 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1857 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1858 int delta = skb->len - len;
1860 skb->csum = csum_block_sub(skb->csum,
1861 skb_checksum(skb, len, delta, 0),
1863 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1864 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
1865 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
1867 if (offset + sizeof(__sum16) > hdlen)
1870 return __pskb_trim(skb, len);
1872 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1875 * __pskb_pull_tail - advance tail of skb header
1876 * @skb: buffer to reallocate
1877 * @delta: number of bytes to advance tail
1879 * The function makes a sense only on a fragmented &sk_buff,
1880 * it expands header moving its tail forward and copying necessary
1881 * data from fragmented part.
1883 * &sk_buff MUST have reference count of 1.
1885 * Returns %NULL (and &sk_buff does not change) if pull failed
1886 * or value of new tail of skb in the case of success.
1888 * All the pointers pointing into skb header may change and must be
1889 * reloaded after call to this function.
1892 /* Moves tail of skb head forward, copying data from fragmented part,
1893 * when it is necessary.
1894 * 1. It may fail due to malloc failure.
1895 * 2. It may change skb pointers.
1897 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1899 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1901 /* If skb has not enough free space at tail, get new one
1902 * plus 128 bytes for future expansions. If we have enough
1903 * room at tail, reallocate without expansion only if skb is cloned.
1905 int i, k, eat = (skb->tail + delta) - skb->end;
1907 if (eat > 0 || skb_cloned(skb)) {
1908 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1913 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1916 /* Optimization: no fragments, no reasons to preestimate
1917 * size of pulled pages. Superb.
1919 if (!skb_has_frag_list(skb))
1922 /* Estimate size of pulled pages. */
1924 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1925 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1932 /* If we need update frag list, we are in troubles.
1933 * Certainly, it is possible to add an offset to skb data,
1934 * but taking into account that pulling is expected to
1935 * be very rare operation, it is worth to fight against
1936 * further bloating skb head and crucify ourselves here instead.
1937 * Pure masohism, indeed. 8)8)
1940 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1941 struct sk_buff *clone = NULL;
1942 struct sk_buff *insp = NULL;
1947 if (list->len <= eat) {
1948 /* Eaten as whole. */
1953 /* Eaten partially. */
1954 if (skb_is_gso(skb) && !list->head_frag &&
1956 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1958 if (skb_shared(list)) {
1959 /* Sucks! We need to fork list. :-( */
1960 clone = skb_clone(list, GFP_ATOMIC);
1966 /* This may be pulled without
1970 if (!pskb_pull(list, eat)) {
1978 /* Free pulled out fragments. */
1979 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1980 skb_shinfo(skb)->frag_list = list->next;
1983 /* And insert new clone at head. */
1986 skb_shinfo(skb)->frag_list = clone;
1989 /* Success! Now we may commit changes to skb data. */
1994 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1995 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1998 skb_frag_unref(skb, i);
2001 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
2003 skb_shinfo(skb)->frags[k].page_offset += eat;
2004 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
2012 skb_shinfo(skb)->nr_frags = k;
2016 skb->data_len -= delta;
2019 skb_zcopy_clear(skb, false);
2021 return skb_tail_pointer(skb);
2023 EXPORT_SYMBOL(__pskb_pull_tail);
2026 * skb_copy_bits - copy bits from skb to kernel buffer
2028 * @offset: offset in source
2029 * @to: destination buffer
2030 * @len: number of bytes to copy
2032 * Copy the specified number of bytes from the source skb to the
2033 * destination buffer.
2036 * If its prototype is ever changed,
2037 * check arch/{*}/net/{*}.S files,
2038 * since it is called from BPF assembly code.
2040 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2042 int start = skb_headlen(skb);
2043 struct sk_buff *frag_iter;
2046 if (offset > (int)skb->len - len)
2050 if ((copy = start - offset) > 0) {
2053 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2054 if ((len -= copy) == 0)
2060 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2062 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2064 WARN_ON(start > offset + len);
2066 end = start + skb_frag_size(f);
2067 if ((copy = end - offset) > 0) {
2068 u32 p_off, p_len, copied;
2075 skb_frag_foreach_page(f,
2076 f->page_offset + offset - start,
2077 copy, p, p_off, p_len, copied) {
2078 vaddr = kmap_atomic(p);
2079 memcpy(to + copied, vaddr + p_off, p_len);
2080 kunmap_atomic(vaddr);
2083 if ((len -= copy) == 0)
2091 skb_walk_frags(skb, frag_iter) {
2094 WARN_ON(start > offset + len);
2096 end = start + frag_iter->len;
2097 if ((copy = end - offset) > 0) {
2100 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2102 if ((len -= copy) == 0)
2116 EXPORT_SYMBOL(skb_copy_bits);
2119 * Callback from splice_to_pipe(), if we need to release some pages
2120 * at the end of the spd in case we error'ed out in filling the pipe.
2122 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2124 put_page(spd->pages[i]);
2127 static struct page *linear_to_page(struct page *page, unsigned int *len,
2128 unsigned int *offset,
2131 struct page_frag *pfrag = sk_page_frag(sk);
2133 if (!sk_page_frag_refill(sk, pfrag))
2136 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2138 memcpy(page_address(pfrag->page) + pfrag->offset,
2139 page_address(page) + *offset, *len);
2140 *offset = pfrag->offset;
2141 pfrag->offset += *len;
2146 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2148 unsigned int offset)
2150 return spd->nr_pages &&
2151 spd->pages[spd->nr_pages - 1] == page &&
2152 (spd->partial[spd->nr_pages - 1].offset +
2153 spd->partial[spd->nr_pages - 1].len == offset);
2157 * Fill page/offset/length into spd, if it can hold more pages.
2159 static bool spd_fill_page(struct splice_pipe_desc *spd,
2160 struct pipe_inode_info *pipe, struct page *page,
2161 unsigned int *len, unsigned int offset,
2165 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2169 page = linear_to_page(page, len, &offset, sk);
2173 if (spd_can_coalesce(spd, page, offset)) {
2174 spd->partial[spd->nr_pages - 1].len += *len;
2178 spd->pages[spd->nr_pages] = page;
2179 spd->partial[spd->nr_pages].len = *len;
2180 spd->partial[spd->nr_pages].offset = offset;
2186 static bool __splice_segment(struct page *page, unsigned int poff,
2187 unsigned int plen, unsigned int *off,
2189 struct splice_pipe_desc *spd, bool linear,
2191 struct pipe_inode_info *pipe)
2196 /* skip this segment if already processed */
2202 /* ignore any bits we already processed */
2208 unsigned int flen = min(*len, plen);
2210 if (spd_fill_page(spd, pipe, page, &flen, poff,
2216 } while (*len && plen);
2222 * Map linear and fragment data from the skb to spd. It reports true if the
2223 * pipe is full or if we already spliced the requested length.
2225 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2226 unsigned int *offset, unsigned int *len,
2227 struct splice_pipe_desc *spd, struct sock *sk)
2230 struct sk_buff *iter;
2232 /* map the linear part :
2233 * If skb->head_frag is set, this 'linear' part is backed by a
2234 * fragment, and if the head is not shared with any clones then
2235 * we can avoid a copy since we own the head portion of this page.
2237 if (__splice_segment(virt_to_page(skb->data),
2238 (unsigned long) skb->data & (PAGE_SIZE - 1),
2241 skb_head_is_locked(skb),
2246 * then map the fragments
2248 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2249 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2251 if (__splice_segment(skb_frag_page(f),
2252 f->page_offset, skb_frag_size(f),
2253 offset, len, spd, false, sk, pipe))
2257 skb_walk_frags(skb, iter) {
2258 if (*offset >= iter->len) {
2259 *offset -= iter->len;
2262 /* __skb_splice_bits() only fails if the output has no room
2263 * left, so no point in going over the frag_list for the error
2266 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2274 * Map data from the skb to a pipe. Should handle both the linear part,
2275 * the fragments, and the frag list.
2277 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2278 struct pipe_inode_info *pipe, unsigned int tlen,
2281 struct partial_page partial[MAX_SKB_FRAGS];
2282 struct page *pages[MAX_SKB_FRAGS];
2283 struct splice_pipe_desc spd = {
2286 .nr_pages_max = MAX_SKB_FRAGS,
2287 .ops = &nosteal_pipe_buf_ops,
2288 .spd_release = sock_spd_release,
2292 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2295 ret = splice_to_pipe(pipe, &spd);
2299 EXPORT_SYMBOL_GPL(skb_splice_bits);
2301 /* Send skb data on a socket. Socket must be locked. */
2302 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2305 unsigned int orig_len = len;
2306 struct sk_buff *head = skb;
2307 unsigned short fragidx;
2312 /* Deal with head data */
2313 while (offset < skb_headlen(skb) && len) {
2317 slen = min_t(int, len, skb_headlen(skb) - offset);
2318 kv.iov_base = skb->data + offset;
2320 memset(&msg, 0, sizeof(msg));
2321 msg.msg_flags = MSG_DONTWAIT;
2323 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2331 /* All the data was skb head? */
2335 /* Make offset relative to start of frags */
2336 offset -= skb_headlen(skb);
2338 /* Find where we are in frag list */
2339 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2340 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2342 if (offset < frag->size)
2345 offset -= frag->size;
2348 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2349 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2351 slen = min_t(size_t, len, frag->size - offset);
2354 ret = kernel_sendpage_locked(sk, frag->page.p,
2355 frag->page_offset + offset,
2356 slen, MSG_DONTWAIT);
2369 /* Process any frag lists */
2372 if (skb_has_frag_list(skb)) {
2373 skb = skb_shinfo(skb)->frag_list;
2376 } else if (skb->next) {
2383 return orig_len - len;
2386 return orig_len == len ? ret : orig_len - len;
2388 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2390 /* Send skb data on a socket. */
2391 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2396 ret = skb_send_sock_locked(sk, skb, offset, len);
2401 EXPORT_SYMBOL_GPL(skb_send_sock);
2404 * skb_store_bits - store bits from kernel buffer to skb
2405 * @skb: destination buffer
2406 * @offset: offset in destination
2407 * @from: source buffer
2408 * @len: number of bytes to copy
2410 * Copy the specified number of bytes from the source buffer to the
2411 * destination skb. This function handles all the messy bits of
2412 * traversing fragment lists and such.
2415 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2417 int start = skb_headlen(skb);
2418 struct sk_buff *frag_iter;
2421 if (offset > (int)skb->len - len)
2424 if ((copy = start - offset) > 0) {
2427 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2428 if ((len -= copy) == 0)
2434 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2435 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2438 WARN_ON(start > offset + len);
2440 end = start + skb_frag_size(frag);
2441 if ((copy = end - offset) > 0) {
2442 u32 p_off, p_len, copied;
2449 skb_frag_foreach_page(frag,
2450 frag->page_offset + offset - start,
2451 copy, p, p_off, p_len, copied) {
2452 vaddr = kmap_atomic(p);
2453 memcpy(vaddr + p_off, from + copied, p_len);
2454 kunmap_atomic(vaddr);
2457 if ((len -= copy) == 0)
2465 skb_walk_frags(skb, frag_iter) {
2468 WARN_ON(start > offset + len);
2470 end = start + frag_iter->len;
2471 if ((copy = end - offset) > 0) {
2474 if (skb_store_bits(frag_iter, offset - start,
2477 if ((len -= copy) == 0)
2490 EXPORT_SYMBOL(skb_store_bits);
2492 /* Checksum skb data. */
2493 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2494 __wsum csum, const struct skb_checksum_ops *ops)
2496 int start = skb_headlen(skb);
2497 int i, copy = start - offset;
2498 struct sk_buff *frag_iter;
2501 /* Checksum header. */
2505 csum = ops->update(skb->data + offset, copy, csum);
2506 if ((len -= copy) == 0)
2512 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2514 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2516 WARN_ON(start > offset + len);
2518 end = start + skb_frag_size(frag);
2519 if ((copy = end - offset) > 0) {
2520 u32 p_off, p_len, copied;
2528 skb_frag_foreach_page(frag,
2529 frag->page_offset + offset - start,
2530 copy, p, p_off, p_len, copied) {
2531 vaddr = kmap_atomic(p);
2532 csum2 = ops->update(vaddr + p_off, p_len, 0);
2533 kunmap_atomic(vaddr);
2534 csum = ops->combine(csum, csum2, pos, p_len);
2545 skb_walk_frags(skb, frag_iter) {
2548 WARN_ON(start > offset + len);
2550 end = start + frag_iter->len;
2551 if ((copy = end - offset) > 0) {
2555 csum2 = __skb_checksum(frag_iter, offset - start,
2557 csum = ops->combine(csum, csum2, pos, copy);
2558 if ((len -= copy) == 0)
2569 EXPORT_SYMBOL(__skb_checksum);
2571 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2572 int len, __wsum csum)
2574 const struct skb_checksum_ops ops = {
2575 .update = csum_partial_ext,
2576 .combine = csum_block_add_ext,
2579 return __skb_checksum(skb, offset, len, csum, &ops);
2581 EXPORT_SYMBOL(skb_checksum);
2583 /* Both of above in one bottle. */
2585 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2586 u8 *to, int len, __wsum csum)
2588 int start = skb_headlen(skb);
2589 int i, copy = start - offset;
2590 struct sk_buff *frag_iter;
2597 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2599 if ((len -= copy) == 0)
2606 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2609 WARN_ON(start > offset + len);
2611 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2612 if ((copy = end - offset) > 0) {
2613 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2614 u32 p_off, p_len, copied;
2622 skb_frag_foreach_page(frag,
2623 frag->page_offset + offset - start,
2624 copy, p, p_off, p_len, copied) {
2625 vaddr = kmap_atomic(p);
2626 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2629 kunmap_atomic(vaddr);
2630 csum = csum_block_add(csum, csum2, pos);
2642 skb_walk_frags(skb, frag_iter) {
2646 WARN_ON(start > offset + len);
2648 end = start + frag_iter->len;
2649 if ((copy = end - offset) > 0) {
2652 csum2 = skb_copy_and_csum_bits(frag_iter,
2655 csum = csum_block_add(csum, csum2, pos);
2656 if ((len -= copy) == 0)
2667 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2669 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2671 net_warn_ratelimited(
2672 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2677 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2678 int offset, int len)
2680 net_warn_ratelimited(
2681 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2686 static const struct skb_checksum_ops default_crc32c_ops = {
2687 .update = warn_crc32c_csum_update,
2688 .combine = warn_crc32c_csum_combine,
2691 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2692 &default_crc32c_ops;
2693 EXPORT_SYMBOL(crc32c_csum_stub);
2696 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2697 * @from: source buffer
2699 * Calculates the amount of linear headroom needed in the 'to' skb passed
2700 * into skb_zerocopy().
2703 skb_zerocopy_headlen(const struct sk_buff *from)
2705 unsigned int hlen = 0;
2707 if (!from->head_frag ||
2708 skb_headlen(from) < L1_CACHE_BYTES ||
2709 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
2710 hlen = skb_headlen(from);
2715 if (skb_has_frag_list(from))
2720 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2723 * skb_zerocopy - Zero copy skb to skb
2724 * @to: destination buffer
2725 * @from: source buffer
2726 * @len: number of bytes to copy from source buffer
2727 * @hlen: size of linear headroom in destination buffer
2729 * Copies up to `len` bytes from `from` to `to` by creating references
2730 * to the frags in the source buffer.
2732 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2733 * headroom in the `to` buffer.
2736 * 0: everything is OK
2737 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2738 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2741 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2744 int plen = 0; /* length of skb->head fragment */
2747 unsigned int offset;
2749 BUG_ON(!from->head_frag && !hlen);
2751 /* dont bother with small payloads */
2752 if (len <= skb_tailroom(to))
2753 return skb_copy_bits(from, 0, skb_put(to, len), len);
2756 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2761 plen = min_t(int, skb_headlen(from), len);
2763 page = virt_to_head_page(from->head);
2764 offset = from->data - (unsigned char *)page_address(page);
2765 __skb_fill_page_desc(to, 0, page, offset, plen);
2772 to->truesize += len + plen;
2773 to->len += len + plen;
2774 to->data_len += len + plen;
2776 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2780 skb_zerocopy_clone(to, from, GFP_ATOMIC);
2782 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2785 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2786 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2787 len -= skb_shinfo(to)->frags[j].size;
2788 skb_frag_ref(to, j);
2791 skb_shinfo(to)->nr_frags = j;
2795 EXPORT_SYMBOL_GPL(skb_zerocopy);
2797 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2802 if (skb->ip_summed == CHECKSUM_PARTIAL)
2803 csstart = skb_checksum_start_offset(skb);
2805 csstart = skb_headlen(skb);
2807 BUG_ON(csstart > skb_headlen(skb));
2809 skb_copy_from_linear_data(skb, to, csstart);
2812 if (csstart != skb->len)
2813 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2814 skb->len - csstart, 0);
2816 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2817 long csstuff = csstart + skb->csum_offset;
2819 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2822 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2825 * skb_dequeue - remove from the head of the queue
2826 * @list: list to dequeue from
2828 * Remove the head of the list. The list lock is taken so the function
2829 * may be used safely with other locking list functions. The head item is
2830 * returned or %NULL if the list is empty.
2833 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2835 unsigned long flags;
2836 struct sk_buff *result;
2838 spin_lock_irqsave(&list->lock, flags);
2839 result = __skb_dequeue(list);
2840 spin_unlock_irqrestore(&list->lock, flags);
2843 EXPORT_SYMBOL(skb_dequeue);
2846 * skb_dequeue_tail - remove from the tail of the queue
2847 * @list: list to dequeue from
2849 * Remove the tail of the list. The list lock is taken so the function
2850 * may be used safely with other locking list functions. The tail item is
2851 * returned or %NULL if the list is empty.
2853 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2855 unsigned long flags;
2856 struct sk_buff *result;
2858 spin_lock_irqsave(&list->lock, flags);
2859 result = __skb_dequeue_tail(list);
2860 spin_unlock_irqrestore(&list->lock, flags);
2863 EXPORT_SYMBOL(skb_dequeue_tail);
2866 * skb_queue_purge - empty a list
2867 * @list: list to empty
2869 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2870 * the list and one reference dropped. This function takes the list
2871 * lock and is atomic with respect to other list locking functions.
2873 void skb_queue_purge(struct sk_buff_head *list)
2875 struct sk_buff *skb;
2876 while ((skb = skb_dequeue(list)) != NULL)
2879 EXPORT_SYMBOL(skb_queue_purge);
2882 * skb_rbtree_purge - empty a skb rbtree
2883 * @root: root of the rbtree to empty
2884 * Return value: the sum of truesizes of all purged skbs.
2886 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2887 * the list and one reference dropped. This function does not take
2888 * any lock. Synchronization should be handled by the caller (e.g., TCP
2889 * out-of-order queue is protected by the socket lock).
2891 unsigned int skb_rbtree_purge(struct rb_root *root)
2893 struct rb_node *p = rb_first(root);
2894 unsigned int sum = 0;
2897 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2900 rb_erase(&skb->rbnode, root);
2901 sum += skb->truesize;
2908 * skb_queue_head - queue a buffer at the list head
2909 * @list: list to use
2910 * @newsk: buffer to queue
2912 * Queue a buffer at the start of the list. This function takes the
2913 * list lock and can be used safely with other locking &sk_buff functions
2916 * A buffer cannot be placed on two lists at the same time.
2918 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2920 unsigned long flags;
2922 spin_lock_irqsave(&list->lock, flags);
2923 __skb_queue_head(list, newsk);
2924 spin_unlock_irqrestore(&list->lock, flags);
2926 EXPORT_SYMBOL(skb_queue_head);
2929 * skb_queue_tail - queue a buffer at the list tail
2930 * @list: list to use
2931 * @newsk: buffer to queue
2933 * Queue a buffer at the tail of the list. This function takes the
2934 * list lock and can be used safely with other locking &sk_buff functions
2937 * A buffer cannot be placed on two lists at the same time.
2939 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2941 unsigned long flags;
2943 spin_lock_irqsave(&list->lock, flags);
2944 __skb_queue_tail(list, newsk);
2945 spin_unlock_irqrestore(&list->lock, flags);
2947 EXPORT_SYMBOL(skb_queue_tail);
2950 * skb_unlink - remove a buffer from a list
2951 * @skb: buffer to remove
2952 * @list: list to use
2954 * Remove a packet from a list. The list locks are taken and this
2955 * function is atomic with respect to other list locked calls
2957 * You must know what list the SKB is on.
2959 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2961 unsigned long flags;
2963 spin_lock_irqsave(&list->lock, flags);
2964 __skb_unlink(skb, list);
2965 spin_unlock_irqrestore(&list->lock, flags);
2967 EXPORT_SYMBOL(skb_unlink);
2970 * skb_append - append a buffer
2971 * @old: buffer to insert after
2972 * @newsk: buffer to insert
2973 * @list: list to use
2975 * Place a packet after a given packet in a list. The list locks are taken
2976 * and this function is atomic with respect to other list locked calls.
2977 * A buffer cannot be placed on two lists at the same time.
2979 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2981 unsigned long flags;
2983 spin_lock_irqsave(&list->lock, flags);
2984 __skb_queue_after(list, old, newsk);
2985 spin_unlock_irqrestore(&list->lock, flags);
2987 EXPORT_SYMBOL(skb_append);
2990 * skb_insert - insert a buffer
2991 * @old: buffer to insert before
2992 * @newsk: buffer to insert
2993 * @list: list to use
2995 * Place a packet before a given packet in a list. The list locks are
2996 * taken and this function is atomic with respect to other list locked
2999 * A buffer cannot be placed on two lists at the same time.
3001 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3003 unsigned long flags;
3005 spin_lock_irqsave(&list->lock, flags);
3006 __skb_insert(newsk, old->prev, old, list);
3007 spin_unlock_irqrestore(&list->lock, flags);
3009 EXPORT_SYMBOL(skb_insert);
3011 static inline void skb_split_inside_header(struct sk_buff *skb,
3012 struct sk_buff* skb1,
3013 const u32 len, const int pos)
3017 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3019 /* And move data appendix as is. */
3020 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3021 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3023 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3024 skb_shinfo(skb)->nr_frags = 0;
3025 skb1->data_len = skb->data_len;
3026 skb1->len += skb1->data_len;
3029 skb_set_tail_pointer(skb, len);
3032 static inline void skb_split_no_header(struct sk_buff *skb,
3033 struct sk_buff* skb1,
3034 const u32 len, int pos)
3037 const int nfrags = skb_shinfo(skb)->nr_frags;
3039 skb_shinfo(skb)->nr_frags = 0;
3040 skb1->len = skb1->data_len = skb->len - len;
3042 skb->data_len = len - pos;
3044 for (i = 0; i < nfrags; i++) {
3045 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3047 if (pos + size > len) {
3048 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3052 * We have two variants in this case:
3053 * 1. Move all the frag to the second
3054 * part, if it is possible. F.e.
3055 * this approach is mandatory for TUX,
3056 * where splitting is expensive.
3057 * 2. Split is accurately. We make this.
3059 skb_frag_ref(skb, i);
3060 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
3061 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3062 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3063 skb_shinfo(skb)->nr_frags++;
3067 skb_shinfo(skb)->nr_frags++;
3070 skb_shinfo(skb1)->nr_frags = k;
3074 * skb_split - Split fragmented skb to two parts at length len.
3075 * @skb: the buffer to split
3076 * @skb1: the buffer to receive the second part
3077 * @len: new length for skb
3079 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3081 int pos = skb_headlen(skb);
3083 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3085 skb_zerocopy_clone(skb1, skb, 0);
3086 if (len < pos) /* Split line is inside header. */
3087 skb_split_inside_header(skb, skb1, len, pos);
3088 else /* Second chunk has no header, nothing to copy. */
3089 skb_split_no_header(skb, skb1, len, pos);
3091 EXPORT_SYMBOL(skb_split);
3093 /* Shifting from/to a cloned skb is a no-go.
3095 * Caller cannot keep skb_shinfo related pointers past calling here!
3097 static int skb_prepare_for_shift(struct sk_buff *skb)
3101 if (skb_cloned(skb)) {
3102 /* Save and restore truesize: pskb_expand_head() may reallocate
3103 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3104 * cannot change truesize at this point.
3106 unsigned int save_truesize = skb->truesize;
3108 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3109 skb->truesize = save_truesize;
3115 * skb_shift - Shifts paged data partially from skb to another
3116 * @tgt: buffer into which tail data gets added
3117 * @skb: buffer from which the paged data comes from
3118 * @shiftlen: shift up to this many bytes
3120 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3121 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3122 * It's up to caller to free skb if everything was shifted.
3124 * If @tgt runs out of frags, the whole operation is aborted.
3126 * Skb cannot include anything else but paged data while tgt is allowed
3127 * to have non-paged data as well.
3129 * TODO: full sized shift could be optimized but that would need
3130 * specialized skb free'er to handle frags without up-to-date nr_frags.
3132 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3134 int from, to, merge, todo;
3135 struct skb_frag_struct *fragfrom, *fragto;
3137 BUG_ON(shiftlen > skb->len);
3139 if (skb_headlen(skb))
3141 if (skb_zcopy(tgt) || skb_zcopy(skb))
3146 to = skb_shinfo(tgt)->nr_frags;
3147 fragfrom = &skb_shinfo(skb)->frags[from];
3149 /* Actual merge is delayed until the point when we know we can
3150 * commit all, so that we don't have to undo partial changes
3153 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3154 fragfrom->page_offset)) {
3159 todo -= skb_frag_size(fragfrom);
3161 if (skb_prepare_for_shift(skb) ||
3162 skb_prepare_for_shift(tgt))
3165 /* All previous frag pointers might be stale! */
3166 fragfrom = &skb_shinfo(skb)->frags[from];
3167 fragto = &skb_shinfo(tgt)->frags[merge];
3169 skb_frag_size_add(fragto, shiftlen);
3170 skb_frag_size_sub(fragfrom, shiftlen);
3171 fragfrom->page_offset += shiftlen;
3179 /* Skip full, not-fitting skb to avoid expensive operations */
3180 if ((shiftlen == skb->len) &&
3181 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3184 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3187 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3188 if (to == MAX_SKB_FRAGS)
3191 fragfrom = &skb_shinfo(skb)->frags[from];
3192 fragto = &skb_shinfo(tgt)->frags[to];
3194 if (todo >= skb_frag_size(fragfrom)) {
3195 *fragto = *fragfrom;
3196 todo -= skb_frag_size(fragfrom);
3201 __skb_frag_ref(fragfrom);
3202 fragto->page = fragfrom->page;
3203 fragto->page_offset = fragfrom->page_offset;
3204 skb_frag_size_set(fragto, todo);
3206 fragfrom->page_offset += todo;
3207 skb_frag_size_sub(fragfrom, todo);
3215 /* Ready to "commit" this state change to tgt */
3216 skb_shinfo(tgt)->nr_frags = to;
3219 fragfrom = &skb_shinfo(skb)->frags[0];
3220 fragto = &skb_shinfo(tgt)->frags[merge];
3222 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3223 __skb_frag_unref(fragfrom);
3226 /* Reposition in the original skb */
3228 while (from < skb_shinfo(skb)->nr_frags)
3229 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3230 skb_shinfo(skb)->nr_frags = to;
3232 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3235 /* Most likely the tgt won't ever need its checksum anymore, skb on
3236 * the other hand might need it if it needs to be resent
3238 tgt->ip_summed = CHECKSUM_PARTIAL;
3239 skb->ip_summed = CHECKSUM_PARTIAL;
3241 /* Yak, is it really working this way? Some helper please? */
3242 skb->len -= shiftlen;
3243 skb->data_len -= shiftlen;
3244 skb->truesize -= shiftlen;
3245 tgt->len += shiftlen;
3246 tgt->data_len += shiftlen;
3247 tgt->truesize += shiftlen;
3253 * skb_prepare_seq_read - Prepare a sequential read of skb data
3254 * @skb: the buffer to read
3255 * @from: lower offset of data to be read
3256 * @to: upper offset of data to be read
3257 * @st: state variable
3259 * Initializes the specified state variable. Must be called before
3260 * invoking skb_seq_read() for the first time.
3262 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3263 unsigned int to, struct skb_seq_state *st)
3265 st->lower_offset = from;
3266 st->upper_offset = to;
3267 st->root_skb = st->cur_skb = skb;
3268 st->frag_idx = st->stepped_offset = 0;
3269 st->frag_data = NULL;
3271 EXPORT_SYMBOL(skb_prepare_seq_read);
3274 * skb_seq_read - Sequentially read skb data
3275 * @consumed: number of bytes consumed by the caller so far
3276 * @data: destination pointer for data to be returned
3277 * @st: state variable
3279 * Reads a block of skb data at @consumed relative to the
3280 * lower offset specified to skb_prepare_seq_read(). Assigns
3281 * the head of the data block to @data and returns the length
3282 * of the block or 0 if the end of the skb data or the upper
3283 * offset has been reached.
3285 * The caller is not required to consume all of the data
3286 * returned, i.e. @consumed is typically set to the number
3287 * of bytes already consumed and the next call to
3288 * skb_seq_read() will return the remaining part of the block.
3290 * Note 1: The size of each block of data returned can be arbitrary,
3291 * this limitation is the cost for zerocopy sequential
3292 * reads of potentially non linear data.
3294 * Note 2: Fragment lists within fragments are not implemented
3295 * at the moment, state->root_skb could be replaced with
3296 * a stack for this purpose.
3298 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3299 struct skb_seq_state *st)
3301 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3304 if (unlikely(abs_offset >= st->upper_offset)) {
3305 if (st->frag_data) {
3306 kunmap_atomic(st->frag_data);
3307 st->frag_data = NULL;
3313 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3315 if (abs_offset < block_limit && !st->frag_data) {
3316 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3317 return block_limit - abs_offset;
3320 if (st->frag_idx == 0 && !st->frag_data)
3321 st->stepped_offset += skb_headlen(st->cur_skb);
3323 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3324 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3325 block_limit = skb_frag_size(frag) + st->stepped_offset;
3327 if (abs_offset < block_limit) {
3329 st->frag_data = kmap_atomic(skb_frag_page(frag));
3331 *data = (u8 *) st->frag_data + frag->page_offset +
3332 (abs_offset - st->stepped_offset);
3334 return block_limit - abs_offset;
3337 if (st->frag_data) {
3338 kunmap_atomic(st->frag_data);
3339 st->frag_data = NULL;
3343 st->stepped_offset += skb_frag_size(frag);
3346 if (st->frag_data) {
3347 kunmap_atomic(st->frag_data);
3348 st->frag_data = NULL;
3351 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3352 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3355 } else if (st->cur_skb->next) {
3356 st->cur_skb = st->cur_skb->next;
3363 EXPORT_SYMBOL(skb_seq_read);
3366 * skb_abort_seq_read - Abort a sequential read of skb data
3367 * @st: state variable
3369 * Must be called if skb_seq_read() was not called until it
3372 void skb_abort_seq_read(struct skb_seq_state *st)
3375 kunmap_atomic(st->frag_data);
3377 EXPORT_SYMBOL(skb_abort_seq_read);
3379 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3381 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3382 struct ts_config *conf,
3383 struct ts_state *state)
3385 return skb_seq_read(offset, text, TS_SKB_CB(state));
3388 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3390 skb_abort_seq_read(TS_SKB_CB(state));
3394 * skb_find_text - Find a text pattern in skb data
3395 * @skb: the buffer to look in
3396 * @from: search offset
3398 * @config: textsearch configuration
3400 * Finds a pattern in the skb data according to the specified
3401 * textsearch configuration. Use textsearch_next() to retrieve
3402 * subsequent occurrences of the pattern. Returns the offset
3403 * to the first occurrence or UINT_MAX if no match was found.
3405 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3406 unsigned int to, struct ts_config *config)
3408 struct ts_state state;
3411 config->get_next_block = skb_ts_get_next_block;
3412 config->finish = skb_ts_finish;
3414 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3416 ret = textsearch_find(config, &state);
3417 return (ret <= to - from ? ret : UINT_MAX);
3419 EXPORT_SYMBOL(skb_find_text);
3422 * skb_append_datato_frags - append the user data to a skb
3423 * @sk: sock structure
3424 * @skb: skb structure to be appended with user data.
3425 * @getfrag: call back function to be used for getting the user data
3426 * @from: pointer to user message iov
3427 * @length: length of the iov message
3429 * Description: This procedure append the user data in the fragment part
3430 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3432 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
3433 int (*getfrag)(void *from, char *to, int offset,
3434 int len, int odd, struct sk_buff *skb),
3435 void *from, int length)
3437 int frg_cnt = skb_shinfo(skb)->nr_frags;
3441 struct page_frag *pfrag = ¤t->task_frag;
3444 /* Return error if we don't have space for new frag */
3445 if (frg_cnt >= MAX_SKB_FRAGS)
3448 if (!sk_page_frag_refill(sk, pfrag))
3451 /* copy the user data to page */
3452 copy = min_t(int, length, pfrag->size - pfrag->offset);
3454 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3455 offset, copy, 0, skb);
3459 /* copy was successful so update the size parameters */
3460 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3463 pfrag->offset += copy;
3464 get_page(pfrag->page);
3466 skb->truesize += copy;
3467 refcount_add(copy, &sk->sk_wmem_alloc);
3469 skb->data_len += copy;
3473 } while (length > 0);
3477 EXPORT_SYMBOL(skb_append_datato_frags);
3479 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3480 int offset, size_t size)
3482 int i = skb_shinfo(skb)->nr_frags;
3484 if (skb_can_coalesce(skb, i, page, offset)) {
3485 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3486 } else if (i < MAX_SKB_FRAGS) {
3488 skb_fill_page_desc(skb, i, page, offset, size);
3495 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3498 * skb_pull_rcsum - pull skb and update receive checksum
3499 * @skb: buffer to update
3500 * @len: length of data pulled
3502 * This function performs an skb_pull on the packet and updates
3503 * the CHECKSUM_COMPLETE checksum. It should be used on
3504 * receive path processing instead of skb_pull unless you know
3505 * that the checksum difference is zero (e.g., a valid IP header)
3506 * or you are setting ip_summed to CHECKSUM_NONE.
3508 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3510 unsigned char *data = skb->data;
3512 BUG_ON(len > skb->len);
3513 __skb_pull(skb, len);
3514 skb_postpull_rcsum(skb, data, len);
3517 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3520 * skb_segment - Perform protocol segmentation on skb.
3521 * @head_skb: buffer to segment
3522 * @features: features for the output path (see dev->features)
3524 * This function performs segmentation on the given skb. It returns
3525 * a pointer to the first in a list of new skbs for the segments.
3526 * In case of error it returns ERR_PTR(err).
3528 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3529 netdev_features_t features)
3531 struct sk_buff *segs = NULL;
3532 struct sk_buff *tail = NULL;
3533 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3534 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3535 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3536 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3537 struct sk_buff *frag_skb = head_skb;
3538 unsigned int offset = doffset;
3539 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3540 unsigned int partial_segs = 0;
3541 unsigned int headroom;
3542 unsigned int len = head_skb->len;
3545 int nfrags = skb_shinfo(head_skb)->nr_frags;
3551 if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
3552 mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
3553 struct sk_buff *check_skb;
3555 for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
3556 if (skb_headlen(check_skb) && !check_skb->head_frag) {
3557 /* gso_size is untrusted, and we have a frag_list with
3558 * a linear non head_frag item.
3560 * If head_skb's headlen does not fit requested gso_size,
3561 * it means that the frag_list members do NOT terminate
3562 * on exact gso_size boundaries. Hence we cannot perform
3563 * skb_frag_t page sharing. Therefore we must fallback to
3564 * copying the frag_list skbs; we do so by disabling SG.
3566 features &= ~NETIF_F_SG;
3572 __skb_push(head_skb, doffset);
3573 proto = skb_network_protocol(head_skb, &dummy);
3574 if (unlikely(!proto))
3575 return ERR_PTR(-EINVAL);
3577 sg = !!(features & NETIF_F_SG);
3578 csum = !!can_checksum_protocol(features, proto);
3580 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3581 if (!(features & NETIF_F_GSO_PARTIAL)) {
3582 struct sk_buff *iter;
3583 unsigned int frag_len;
3586 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3589 /* If we get here then all the required
3590 * GSO features except frag_list are supported.
3591 * Try to split the SKB to multiple GSO SKBs
3592 * with no frag_list.
3593 * Currently we can do that only when the buffers don't
3594 * have a linear part and all the buffers except
3595 * the last are of the same length.
3597 frag_len = list_skb->len;
3598 skb_walk_frags(head_skb, iter) {
3599 if (frag_len != iter->len && iter->next)
3601 if (skb_headlen(iter) && !iter->head_frag)
3607 if (len != frag_len)
3611 /* GSO partial only requires that we trim off any excess that
3612 * doesn't fit into an MSS sized block, so take care of that
3615 partial_segs = len / mss;
3616 if (partial_segs > 1)
3617 mss *= partial_segs;
3623 headroom = skb_headroom(head_skb);
3624 pos = skb_headlen(head_skb);
3627 struct sk_buff *nskb;
3628 skb_frag_t *nskb_frag;
3632 if (unlikely(mss == GSO_BY_FRAGS)) {
3633 len = list_skb->len;
3635 len = head_skb->len - offset;
3640 hsize = skb_headlen(head_skb) - offset;
3643 if (hsize > len || !sg)
3646 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3647 (skb_headlen(list_skb) == len || sg)) {
3648 BUG_ON(skb_headlen(list_skb) > len);
3651 nfrags = skb_shinfo(list_skb)->nr_frags;
3652 frag = skb_shinfo(list_skb)->frags;
3653 frag_skb = list_skb;
3654 pos += skb_headlen(list_skb);
3656 while (pos < offset + len) {
3657 BUG_ON(i >= nfrags);
3659 size = skb_frag_size(frag);
3660 if (pos + size > offset + len)
3668 nskb = skb_clone(list_skb, GFP_ATOMIC);
3669 list_skb = list_skb->next;
3671 if (unlikely(!nskb))
3674 if (unlikely(pskb_trim(nskb, len))) {
3679 hsize = skb_end_offset(nskb);
3680 if (skb_cow_head(nskb, doffset + headroom)) {
3685 nskb->truesize += skb_end_offset(nskb) - hsize;
3686 skb_release_head_state(nskb);
3687 __skb_push(nskb, doffset);
3689 nskb = __alloc_skb(hsize + doffset + headroom,
3690 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3693 if (unlikely(!nskb))
3696 skb_reserve(nskb, headroom);
3697 __skb_put(nskb, doffset);
3706 __copy_skb_header(nskb, head_skb);
3708 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3709 skb_reset_mac_len(nskb);
3711 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3712 nskb->data - tnl_hlen,
3713 doffset + tnl_hlen);
3715 if (nskb->len == len + doffset)
3716 goto perform_csum_check;
3719 if (!nskb->remcsum_offload)
3720 nskb->ip_summed = CHECKSUM_NONE;
3721 SKB_GSO_CB(nskb)->csum =
3722 skb_copy_and_csum_bits(head_skb, offset,
3725 SKB_GSO_CB(nskb)->csum_start =
3726 skb_headroom(nskb) + doffset;
3730 nskb_frag = skb_shinfo(nskb)->frags;
3732 skb_copy_from_linear_data_offset(head_skb, offset,
3733 skb_put(nskb, hsize), hsize);
3735 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3738 while (pos < offset + len) {
3740 BUG_ON(skb_headlen(list_skb));
3743 nfrags = skb_shinfo(list_skb)->nr_frags;
3744 frag = skb_shinfo(list_skb)->frags;
3745 frag_skb = list_skb;
3749 list_skb = list_skb->next;
3752 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3754 net_warn_ratelimited(
3755 "skb_segment: too many frags: %u %u\n",
3761 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3763 if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3767 __skb_frag_ref(nskb_frag);
3768 size = skb_frag_size(nskb_frag);
3771 nskb_frag->page_offset += offset - pos;
3772 skb_frag_size_sub(nskb_frag, offset - pos);
3775 skb_shinfo(nskb)->nr_frags++;
3777 if (pos + size <= offset + len) {
3782 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3790 nskb->data_len = len - hsize;
3791 nskb->len += nskb->data_len;
3792 nskb->truesize += nskb->data_len;
3796 if (skb_has_shared_frag(nskb) &&
3797 __skb_linearize(nskb))
3800 if (!nskb->remcsum_offload)
3801 nskb->ip_summed = CHECKSUM_NONE;
3802 SKB_GSO_CB(nskb)->csum =
3803 skb_checksum(nskb, doffset,
3804 nskb->len - doffset, 0);
3805 SKB_GSO_CB(nskb)->csum_start =
3806 skb_headroom(nskb) + doffset;
3808 } while ((offset += len) < head_skb->len);
3810 /* Some callers want to get the end of the list.
3811 * Put it in segs->prev to avoid walking the list.
3812 * (see validate_xmit_skb_list() for example)
3817 struct sk_buff *iter;
3818 int type = skb_shinfo(head_skb)->gso_type;
3819 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3821 /* Update type to add partial and then remove dodgy if set */
3822 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3823 type &= ~SKB_GSO_DODGY;
3825 /* Update GSO info and prepare to start updating headers on
3826 * our way back down the stack of protocols.
3828 for (iter = segs; iter; iter = iter->next) {
3829 skb_shinfo(iter)->gso_size = gso_size;
3830 skb_shinfo(iter)->gso_segs = partial_segs;
3831 skb_shinfo(iter)->gso_type = type;
3832 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3835 if (tail->len - doffset <= gso_size)
3836 skb_shinfo(tail)->gso_size = 0;
3837 else if (tail != segs)
3838 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3841 /* Following permits correct backpressure, for protocols
3842 * using skb_set_owner_w().
3843 * Idea is to tranfert ownership from head_skb to last segment.
3845 if (head_skb->destructor == sock_wfree) {
3846 swap(tail->truesize, head_skb->truesize);
3847 swap(tail->destructor, head_skb->destructor);
3848 swap(tail->sk, head_skb->sk);
3853 kfree_skb_list(segs);
3854 return ERR_PTR(err);
3856 EXPORT_SYMBOL_GPL(skb_segment);
3858 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3860 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3861 unsigned int offset = skb_gro_offset(skb);
3862 unsigned int headlen = skb_headlen(skb);
3863 unsigned int len = skb_gro_len(skb);
3864 struct sk_buff *lp, *p = *head;
3865 unsigned int delta_truesize;
3867 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
3870 lp = NAPI_GRO_CB(p)->last;
3871 pinfo = skb_shinfo(lp);
3873 if (headlen <= offset) {
3876 int i = skbinfo->nr_frags;
3877 int nr_frags = pinfo->nr_frags + i;
3879 if (nr_frags > MAX_SKB_FRAGS)
3883 pinfo->nr_frags = nr_frags;
3884 skbinfo->nr_frags = 0;
3886 frag = pinfo->frags + nr_frags;
3887 frag2 = skbinfo->frags + i;
3892 frag->page_offset += offset;
3893 skb_frag_size_sub(frag, offset);
3895 /* all fragments truesize : remove (head size + sk_buff) */
3896 delta_truesize = skb->truesize -
3897 SKB_TRUESIZE(skb_end_offset(skb));
3899 skb->truesize -= skb->data_len;
3900 skb->len -= skb->data_len;
3903 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3905 } else if (skb->head_frag) {
3906 int nr_frags = pinfo->nr_frags;
3907 skb_frag_t *frag = pinfo->frags + nr_frags;
3908 struct page *page = virt_to_head_page(skb->head);
3909 unsigned int first_size = headlen - offset;
3910 unsigned int first_offset;
3912 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3915 first_offset = skb->data -
3916 (unsigned char *)page_address(page) +
3919 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3921 frag->page.p = page;
3922 frag->page_offset = first_offset;
3923 skb_frag_size_set(frag, first_size);
3925 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3926 /* We dont need to clear skbinfo->nr_frags here */
3928 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3929 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3934 delta_truesize = skb->truesize;
3935 if (offset > headlen) {
3936 unsigned int eat = offset - headlen;
3938 skbinfo->frags[0].page_offset += eat;
3939 skb_frag_size_sub(&skbinfo->frags[0], eat);
3940 skb->data_len -= eat;
3945 __skb_pull(skb, offset);
3947 if (NAPI_GRO_CB(p)->last == p)
3948 skb_shinfo(p)->frag_list = skb;
3950 NAPI_GRO_CB(p)->last->next = skb;
3951 NAPI_GRO_CB(p)->last = skb;
3952 __skb_header_release(skb);
3956 NAPI_GRO_CB(p)->count++;
3958 p->truesize += delta_truesize;
3961 lp->data_len += len;
3962 lp->truesize += delta_truesize;
3965 NAPI_GRO_CB(skb)->same_flow = 1;
3968 EXPORT_SYMBOL_GPL(skb_gro_receive);
3970 void __init skb_init(void)
3972 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3973 sizeof(struct sk_buff),
3975 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3977 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3978 sizeof(struct sk_buff_fclones),
3980 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3985 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3986 unsigned int recursion_level)
3988 int start = skb_headlen(skb);
3989 int i, copy = start - offset;
3990 struct sk_buff *frag_iter;
3993 if (unlikely(recursion_level >= 24))
3999 sg_set_buf(sg, skb->data + offset, copy);
4001 if ((len -= copy) == 0)
4006 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4009 WARN_ON(start > offset + len);
4011 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4012 if ((copy = end - offset) > 0) {
4013 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4014 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4019 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4020 frag->page_offset+offset-start);
4029 skb_walk_frags(skb, frag_iter) {
4032 WARN_ON(start > offset + len);
4034 end = start + frag_iter->len;
4035 if ((copy = end - offset) > 0) {
4036 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4041 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4042 copy, recursion_level + 1);
4043 if (unlikely(ret < 0))
4046 if ((len -= copy) == 0)
4057 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4058 * @skb: Socket buffer containing the buffers to be mapped
4059 * @sg: The scatter-gather list to map into
4060 * @offset: The offset into the buffer's contents to start mapping
4061 * @len: Length of buffer space to be mapped
4063 * Fill the specified scatter-gather list with mappings/pointers into a
4064 * region of the buffer space attached to a socket buffer. Returns either
4065 * the number of scatterlist items used, or -EMSGSIZE if the contents
4068 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4070 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4075 sg_mark_end(&sg[nsg - 1]);
4079 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4081 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4082 * sglist without mark the sg which contain last skb data as the end.
4083 * So the caller can mannipulate sg list as will when padding new data after
4084 * the first call without calling sg_unmark_end to expend sg list.
4086 * Scenario to use skb_to_sgvec_nomark:
4088 * 2. skb_to_sgvec_nomark(payload1)
4089 * 3. skb_to_sgvec_nomark(payload2)
4091 * This is equivalent to:
4093 * 2. skb_to_sgvec(payload1)
4095 * 4. skb_to_sgvec(payload2)
4097 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4098 * is more preferable.
4100 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4101 int offset, int len)
4103 return __skb_to_sgvec(skb, sg, offset, len, 0);
4105 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4110 * skb_cow_data - Check that a socket buffer's data buffers are writable
4111 * @skb: The socket buffer to check.
4112 * @tailbits: Amount of trailing space to be added
4113 * @trailer: Returned pointer to the skb where the @tailbits space begins
4115 * Make sure that the data buffers attached to a socket buffer are
4116 * writable. If they are not, private copies are made of the data buffers
4117 * and the socket buffer is set to use these instead.
4119 * If @tailbits is given, make sure that there is space to write @tailbits
4120 * bytes of data beyond current end of socket buffer. @trailer will be
4121 * set to point to the skb in which this space begins.
4123 * The number of scatterlist elements required to completely map the
4124 * COW'd and extended socket buffer will be returned.
4126 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4130 struct sk_buff *skb1, **skb_p;
4132 /* If skb is cloned or its head is paged, reallocate
4133 * head pulling out all the pages (pages are considered not writable
4134 * at the moment even if they are anonymous).
4136 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4137 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4140 /* Easy case. Most of packets will go this way. */
4141 if (!skb_has_frag_list(skb)) {
4142 /* A little of trouble, not enough of space for trailer.
4143 * This should not happen, when stack is tuned to generate
4144 * good frames. OK, on miss we reallocate and reserve even more
4145 * space, 128 bytes is fair. */
4147 if (skb_tailroom(skb) < tailbits &&
4148 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4156 /* Misery. We are in troubles, going to mincer fragments... */
4159 skb_p = &skb_shinfo(skb)->frag_list;
4162 while ((skb1 = *skb_p) != NULL) {
4165 /* The fragment is partially pulled by someone,
4166 * this can happen on input. Copy it and everything
4169 if (skb_shared(skb1))
4172 /* If the skb is the last, worry about trailer. */
4174 if (skb1->next == NULL && tailbits) {
4175 if (skb_shinfo(skb1)->nr_frags ||
4176 skb_has_frag_list(skb1) ||
4177 skb_tailroom(skb1) < tailbits)
4178 ntail = tailbits + 128;
4184 skb_shinfo(skb1)->nr_frags ||
4185 skb_has_frag_list(skb1)) {
4186 struct sk_buff *skb2;
4188 /* Fuck, we are miserable poor guys... */
4190 skb2 = skb_copy(skb1, GFP_ATOMIC);
4192 skb2 = skb_copy_expand(skb1,
4196 if (unlikely(skb2 == NULL))
4200 skb_set_owner_w(skb2, skb1->sk);
4202 /* Looking around. Are we still alive?
4203 * OK, link new skb, drop old one */
4205 skb2->next = skb1->next;
4212 skb_p = &skb1->next;
4217 EXPORT_SYMBOL_GPL(skb_cow_data);
4219 static void sock_rmem_free(struct sk_buff *skb)
4221 struct sock *sk = skb->sk;
4223 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4226 static void skb_set_err_queue(struct sk_buff *skb)
4228 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4229 * So, it is safe to (mis)use it to mark skbs on the error queue.
4231 skb->pkt_type = PACKET_OUTGOING;
4232 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4236 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4238 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4240 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4241 (unsigned int)sk->sk_rcvbuf)
4246 skb->destructor = sock_rmem_free;
4247 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4248 skb_set_err_queue(skb);
4250 /* before exiting rcu section, make sure dst is refcounted */
4253 skb_queue_tail(&sk->sk_error_queue, skb);
4254 if (!sock_flag(sk, SOCK_DEAD))
4255 sk->sk_error_report(sk);
4258 EXPORT_SYMBOL(sock_queue_err_skb);
4260 static bool is_icmp_err_skb(const struct sk_buff *skb)
4262 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4263 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4266 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4268 struct sk_buff_head *q = &sk->sk_error_queue;
4269 struct sk_buff *skb, *skb_next = NULL;
4270 bool icmp_next = false;
4271 unsigned long flags;
4273 spin_lock_irqsave(&q->lock, flags);
4274 skb = __skb_dequeue(q);
4275 if (skb && (skb_next = skb_peek(q))) {
4276 icmp_next = is_icmp_err_skb(skb_next);
4278 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
4280 spin_unlock_irqrestore(&q->lock, flags);
4282 if (is_icmp_err_skb(skb) && !icmp_next)
4286 sk->sk_error_report(sk);
4290 EXPORT_SYMBOL(sock_dequeue_err_skb);
4293 * skb_clone_sk - create clone of skb, and take reference to socket
4294 * @skb: the skb to clone
4296 * This function creates a clone of a buffer that holds a reference on
4297 * sk_refcnt. Buffers created via this function are meant to be
4298 * returned using sock_queue_err_skb, or free via kfree_skb.
4300 * When passing buffers allocated with this function to sock_queue_err_skb
4301 * it is necessary to wrap the call with sock_hold/sock_put in order to
4302 * prevent the socket from being released prior to being enqueued on
4303 * the sk_error_queue.
4305 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4307 struct sock *sk = skb->sk;
4308 struct sk_buff *clone;
4310 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4313 clone = skb_clone(skb, GFP_ATOMIC);
4320 clone->destructor = sock_efree;
4324 EXPORT_SYMBOL(skb_clone_sk);
4326 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4331 struct sock_exterr_skb *serr;
4334 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4336 serr = SKB_EXT_ERR(skb);
4337 memset(serr, 0, sizeof(*serr));
4338 serr->ee.ee_errno = ENOMSG;
4339 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4340 serr->ee.ee_info = tstype;
4341 serr->opt_stats = opt_stats;
4342 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4343 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4344 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4345 if (sk->sk_protocol == IPPROTO_TCP &&
4346 sk->sk_type == SOCK_STREAM)
4347 serr->ee.ee_data -= sk->sk_tskey;
4350 err = sock_queue_err_skb(sk, skb);
4356 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4360 if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
4363 read_lock_bh(&sk->sk_callback_lock);
4364 ret = sk->sk_socket && sk->sk_socket->file &&
4365 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4366 read_unlock_bh(&sk->sk_callback_lock);
4370 void skb_complete_tx_timestamp(struct sk_buff *skb,
4371 struct skb_shared_hwtstamps *hwtstamps)
4373 struct sock *sk = skb->sk;
4375 if (!skb_may_tx_timestamp(sk, false))
4378 /* Take a reference to prevent skb_orphan() from freeing the socket,
4379 * but only if the socket refcount is not zero.
4381 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4382 *skb_hwtstamps(skb) = *hwtstamps;
4383 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4391 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4393 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4394 struct skb_shared_hwtstamps *hwtstamps,
4395 struct sock *sk, int tstype)
4397 struct sk_buff *skb;
4398 bool tsonly, opt_stats = false;
4403 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4404 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4407 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4408 if (!skb_may_tx_timestamp(sk, tsonly))
4413 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4414 sk->sk_protocol == IPPROTO_TCP &&
4415 sk->sk_type == SOCK_STREAM) {
4416 skb = tcp_get_timestamping_opt_stats(sk);
4420 skb = alloc_skb(0, GFP_ATOMIC);
4422 skb = skb_clone(orig_skb, GFP_ATOMIC);
4428 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4430 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4434 *skb_hwtstamps(skb) = *hwtstamps;
4436 skb->tstamp = ktime_get_real();
4438 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4440 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4442 void skb_tstamp_tx(struct sk_buff *orig_skb,
4443 struct skb_shared_hwtstamps *hwtstamps)
4445 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4448 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4450 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4452 struct sock *sk = skb->sk;
4453 struct sock_exterr_skb *serr;
4456 skb->wifi_acked_valid = 1;
4457 skb->wifi_acked = acked;
4459 serr = SKB_EXT_ERR(skb);
4460 memset(serr, 0, sizeof(*serr));
4461 serr->ee.ee_errno = ENOMSG;
4462 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4464 /* Take a reference to prevent skb_orphan() from freeing the socket,
4465 * but only if the socket refcount is not zero.
4467 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4468 err = sock_queue_err_skb(sk, skb);
4474 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4477 * skb_partial_csum_set - set up and verify partial csum values for packet
4478 * @skb: the skb to set
4479 * @start: the number of bytes after skb->data to start checksumming.
4480 * @off: the offset from start to place the checksum.
4482 * For untrusted partially-checksummed packets, we need to make sure the values
4483 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4485 * This function checks and sets those values and skb->ip_summed: if this
4486 * returns false you should drop the packet.
4488 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4490 if (unlikely(start > skb_headlen(skb)) ||
4491 unlikely((int)start + off > skb_headlen(skb) - 2)) {
4492 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4493 start, off, skb_headlen(skb));
4496 skb->ip_summed = CHECKSUM_PARTIAL;
4497 skb->csum_start = skb_headroom(skb) + start;
4498 skb->csum_offset = off;
4499 skb_set_transport_header(skb, start);
4502 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4504 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4507 if (skb_headlen(skb) >= len)
4510 /* If we need to pullup then pullup to the max, so we
4511 * won't need to do it again.
4516 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4519 if (skb_headlen(skb) < len)
4525 #define MAX_TCP_HDR_LEN (15 * 4)
4527 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4528 typeof(IPPROTO_IP) proto,
4535 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4536 off + MAX_TCP_HDR_LEN);
4537 if (!err && !skb_partial_csum_set(skb, off,
4538 offsetof(struct tcphdr,
4541 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4544 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4545 off + sizeof(struct udphdr));
4546 if (!err && !skb_partial_csum_set(skb, off,
4547 offsetof(struct udphdr,
4550 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4553 return ERR_PTR(-EPROTO);
4556 /* This value should be large enough to cover a tagged ethernet header plus
4557 * maximally sized IP and TCP or UDP headers.
4559 #define MAX_IP_HDR_LEN 128
4561 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4570 err = skb_maybe_pull_tail(skb,
4571 sizeof(struct iphdr),
4576 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4579 off = ip_hdrlen(skb);
4586 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4588 return PTR_ERR(csum);
4591 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4594 ip_hdr(skb)->protocol, 0);
4601 /* This value should be large enough to cover a tagged ethernet header plus
4602 * an IPv6 header, all options, and a maximal TCP or UDP header.
4604 #define MAX_IPV6_HDR_LEN 256
4606 #define OPT_HDR(type, skb, off) \
4607 (type *)(skb_network_header(skb) + (off))
4609 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4622 off = sizeof(struct ipv6hdr);
4624 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4628 nexthdr = ipv6_hdr(skb)->nexthdr;
4630 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4631 while (off <= len && !done) {
4633 case IPPROTO_DSTOPTS:
4634 case IPPROTO_HOPOPTS:
4635 case IPPROTO_ROUTING: {
4636 struct ipv6_opt_hdr *hp;
4638 err = skb_maybe_pull_tail(skb,
4640 sizeof(struct ipv6_opt_hdr),
4645 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4646 nexthdr = hp->nexthdr;
4647 off += ipv6_optlen(hp);
4651 struct ip_auth_hdr *hp;
4653 err = skb_maybe_pull_tail(skb,
4655 sizeof(struct ip_auth_hdr),
4660 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4661 nexthdr = hp->nexthdr;
4662 off += ipv6_authlen(hp);
4665 case IPPROTO_FRAGMENT: {
4666 struct frag_hdr *hp;
4668 err = skb_maybe_pull_tail(skb,
4670 sizeof(struct frag_hdr),
4675 hp = OPT_HDR(struct frag_hdr, skb, off);
4677 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4680 nexthdr = hp->nexthdr;
4681 off += sizeof(struct frag_hdr);
4692 if (!done || fragment)
4695 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4697 return PTR_ERR(csum);
4700 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4701 &ipv6_hdr(skb)->daddr,
4702 skb->len - off, nexthdr, 0);
4710 * skb_checksum_setup - set up partial checksum offset
4711 * @skb: the skb to set up
4712 * @recalculate: if true the pseudo-header checksum will be recalculated
4714 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4718 switch (skb->protocol) {
4719 case htons(ETH_P_IP):
4720 err = skb_checksum_setup_ipv4(skb, recalculate);
4723 case htons(ETH_P_IPV6):
4724 err = skb_checksum_setup_ipv6(skb, recalculate);
4734 EXPORT_SYMBOL(skb_checksum_setup);
4737 * skb_checksum_maybe_trim - maybe trims the given skb
4738 * @skb: the skb to check
4739 * @transport_len: the data length beyond the network header
4741 * Checks whether the given skb has data beyond the given transport length.
4742 * If so, returns a cloned skb trimmed to this transport length.
4743 * Otherwise returns the provided skb. Returns NULL in error cases
4744 * (e.g. transport_len exceeds skb length or out-of-memory).
4746 * Caller needs to set the skb transport header and free any returned skb if it
4747 * differs from the provided skb.
4749 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4750 unsigned int transport_len)
4752 struct sk_buff *skb_chk;
4753 unsigned int len = skb_transport_offset(skb) + transport_len;
4758 else if (skb->len == len)
4761 skb_chk = skb_clone(skb, GFP_ATOMIC);
4765 ret = pskb_trim_rcsum(skb_chk, len);
4775 * skb_checksum_trimmed - validate checksum of an skb
4776 * @skb: the skb to check
4777 * @transport_len: the data length beyond the network header
4778 * @skb_chkf: checksum function to use
4780 * Applies the given checksum function skb_chkf to the provided skb.
4781 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4783 * If the skb has data beyond the given transport length, then a
4784 * trimmed & cloned skb is checked and returned.
4786 * Caller needs to set the skb transport header and free any returned skb if it
4787 * differs from the provided skb.
4789 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4790 unsigned int transport_len,
4791 __sum16(*skb_chkf)(struct sk_buff *skb))
4793 struct sk_buff *skb_chk;
4794 unsigned int offset = skb_transport_offset(skb);
4797 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4801 if (!pskb_may_pull(skb_chk, offset))
4804 skb_pull_rcsum(skb_chk, offset);
4805 ret = skb_chkf(skb_chk);
4806 skb_push_rcsum(skb_chk, offset);
4814 if (skb_chk && skb_chk != skb)
4820 EXPORT_SYMBOL(skb_checksum_trimmed);
4822 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4824 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4827 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4829 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4832 skb_release_head_state(skb);
4833 kmem_cache_free(skbuff_head_cache, skb);
4838 EXPORT_SYMBOL(kfree_skb_partial);
4841 * skb_try_coalesce - try to merge skb to prior one
4843 * @from: buffer to add
4844 * @fragstolen: pointer to boolean
4845 * @delta_truesize: how much more was allocated than was requested
4847 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4848 bool *fragstolen, int *delta_truesize)
4850 int i, delta, len = from->len;
4852 *fragstolen = false;
4857 if (len <= skb_tailroom(to)) {
4859 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4860 *delta_truesize = 0;
4864 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4866 if (skb_zcopy(to) || skb_zcopy(from))
4869 if (skb_headlen(from) != 0) {
4871 unsigned int offset;
4873 if (skb_shinfo(to)->nr_frags +
4874 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4877 if (skb_head_is_locked(from))
4880 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4882 page = virt_to_head_page(from->head);
4883 offset = from->data - (unsigned char *)page_address(page);
4885 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4886 page, offset, skb_headlen(from));
4889 if (skb_shinfo(to)->nr_frags +
4890 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4893 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4896 WARN_ON_ONCE(delta < len);
4898 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4899 skb_shinfo(from)->frags,
4900 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4901 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4903 if (!skb_cloned(from))
4904 skb_shinfo(from)->nr_frags = 0;
4906 /* if the skb is not cloned this does nothing
4907 * since we set nr_frags to 0.
4909 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4910 skb_frag_ref(from, i);
4912 to->truesize += delta;
4914 to->data_len += len;
4916 *delta_truesize = delta;
4919 EXPORT_SYMBOL(skb_try_coalesce);
4922 * skb_scrub_packet - scrub an skb
4924 * @skb: buffer to clean
4925 * @xnet: packet is crossing netns
4927 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4928 * into/from a tunnel. Some information have to be cleared during these
4930 * skb_scrub_packet can also be used to clean a skb before injecting it in
4931 * another namespace (@xnet == true). We have to clear all information in the
4932 * skb that could impact namespace isolation.
4934 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4937 skb->pkt_type = PACKET_HOST;
4943 nf_reset_trace(skb);
4945 #ifdef CONFIG_NET_SWITCHDEV
4946 skb->offload_fwd_mark = 0;
4956 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4959 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4963 * skb_gso_transport_seglen is used to determine the real size of the
4964 * individual segments, including Layer4 headers (TCP/UDP).
4966 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4968 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4970 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4971 unsigned int thlen = 0;
4973 if (skb->encapsulation) {
4974 thlen = skb_inner_transport_header(skb) -
4975 skb_transport_header(skb);
4977 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4978 thlen += inner_tcp_hdrlen(skb);
4979 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4980 thlen = tcp_hdrlen(skb);
4981 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4982 thlen = sizeof(struct sctphdr);
4984 /* UFO sets gso_size to the size of the fragmentation
4985 * payload, i.e. the size of the L4 (UDP) header is already
4988 return thlen + shinfo->gso_size;
4990 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4993 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
4995 * There are a couple of instances where we have a GSO skb, and we
4996 * want to determine what size it would be after it is segmented.
4998 * We might want to check:
4999 * - L3+L4+payload size (e.g. IP forwarding)
5000 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5002 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5004 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5005 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5007 * @max_len: The maximum permissible length.
5009 * Returns true if the segmented length <= max length.
5011 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5012 unsigned int seg_len,
5013 unsigned int max_len) {
5014 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5015 const struct sk_buff *iter;
5017 if (shinfo->gso_size != GSO_BY_FRAGS)
5018 return seg_len <= max_len;
5020 /* Undo this so we can re-use header sizes */
5021 seg_len -= GSO_BY_FRAGS;
5023 skb_walk_frags(skb, iter) {
5024 if (seg_len + skb_headlen(iter) > max_len)
5032 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
5035 * @mtu: MTU to validate against
5037 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
5040 bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
5042 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5044 EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
5047 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5050 * @len: length to validate against
5052 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5053 * length once split, including L2, L3 and L4 headers and the payload.
5055 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5057 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5059 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5061 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5065 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5070 mac_len = skb->data - skb_mac_header(skb);
5071 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5072 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5073 mac_len - VLAN_HLEN - ETH_TLEN);
5075 skb->mac_header += VLAN_HLEN;
5079 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5081 struct vlan_hdr *vhdr;
5084 if (unlikely(skb_vlan_tag_present(skb))) {
5085 /* vlan_tci is already set-up so leave this for another time */
5089 skb = skb_share_check(skb, GFP_ATOMIC);
5092 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5093 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
5096 vhdr = (struct vlan_hdr *)skb->data;
5097 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5098 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5100 skb_pull_rcsum(skb, VLAN_HLEN);
5101 vlan_set_encap_proto(skb, vhdr);
5103 skb = skb_reorder_vlan_header(skb);
5107 skb_reset_network_header(skb);
5108 skb_reset_transport_header(skb);
5109 skb_reset_mac_len(skb);
5117 EXPORT_SYMBOL(skb_vlan_untag);
5119 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5121 if (!pskb_may_pull(skb, write_len))
5124 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5127 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5129 EXPORT_SYMBOL(skb_ensure_writable);
5131 /* remove VLAN header from packet and update csum accordingly.
5132 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5134 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5136 struct vlan_hdr *vhdr;
5137 int offset = skb->data - skb_mac_header(skb);
5140 if (WARN_ONCE(offset,
5141 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5146 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5150 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5152 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5153 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5155 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5156 __skb_pull(skb, VLAN_HLEN);
5158 vlan_set_encap_proto(skb, vhdr);
5159 skb->mac_header += VLAN_HLEN;
5161 if (skb_network_offset(skb) < ETH_HLEN)
5162 skb_set_network_header(skb, ETH_HLEN);
5164 skb_reset_mac_len(skb);
5168 EXPORT_SYMBOL(__skb_vlan_pop);
5170 /* Pop a vlan tag either from hwaccel or from payload.
5171 * Expects skb->data at mac header.
5173 int skb_vlan_pop(struct sk_buff *skb)
5179 if (likely(skb_vlan_tag_present(skb))) {
5182 if (unlikely(!eth_type_vlan(skb->protocol)))
5185 err = __skb_vlan_pop(skb, &vlan_tci);
5189 /* move next vlan tag to hw accel tag */
5190 if (likely(!eth_type_vlan(skb->protocol)))
5193 vlan_proto = skb->protocol;
5194 err = __skb_vlan_pop(skb, &vlan_tci);
5198 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5201 EXPORT_SYMBOL(skb_vlan_pop);
5203 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5204 * Expects skb->data at mac header.
5206 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5208 if (skb_vlan_tag_present(skb)) {
5209 int offset = skb->data - skb_mac_header(skb);
5212 if (WARN_ONCE(offset,
5213 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5218 err = __vlan_insert_tag(skb, skb->vlan_proto,
5219 skb_vlan_tag_get(skb));
5223 skb->protocol = skb->vlan_proto;
5224 skb->mac_len += VLAN_HLEN;
5226 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5228 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5231 EXPORT_SYMBOL(skb_vlan_push);
5234 * alloc_skb_with_frags - allocate skb with page frags
5236 * @header_len: size of linear part
5237 * @data_len: needed length in frags
5238 * @max_page_order: max page order desired.
5239 * @errcode: pointer to error code if any
5240 * @gfp_mask: allocation mask
5242 * This can be used to allocate a paged skb, given a maximal order for frags.
5244 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5245 unsigned long data_len,
5250 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5251 unsigned long chunk;
5252 struct sk_buff *skb;
5256 *errcode = -EMSGSIZE;
5257 /* Note this test could be relaxed, if we succeed to allocate
5258 * high order pages...
5260 if (npages > MAX_SKB_FRAGS)
5263 *errcode = -ENOBUFS;
5264 skb = alloc_skb(header_len, gfp_mask);
5268 skb->truesize += npages << PAGE_SHIFT;
5270 for (i = 0; npages > 0; i++) {
5271 int order = max_page_order;
5274 if (npages >= 1 << order) {
5275 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
5282 /* Do not retry other high order allocations */
5288 page = alloc_page(gfp_mask);
5292 chunk = min_t(unsigned long, data_len,
5293 PAGE_SIZE << order);
5294 skb_fill_page_desc(skb, i, page, 0, chunk);
5296 npages -= 1 << order;
5304 EXPORT_SYMBOL(alloc_skb_with_frags);
5306 /* carve out the first off bytes from skb when off < headlen */
5307 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5308 const int headlen, gfp_t gfp_mask)
5311 int size = skb_end_offset(skb);
5312 int new_hlen = headlen - off;
5315 size = SKB_DATA_ALIGN(size);
5317 if (skb_pfmemalloc(skb))
5318 gfp_mask |= __GFP_MEMALLOC;
5319 data = kmalloc_reserve(size +
5320 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5321 gfp_mask, NUMA_NO_NODE, NULL);
5325 size = SKB_WITH_OVERHEAD(ksize(data));
5327 /* Copy real data, and all frags */
5328 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5331 memcpy((struct skb_shared_info *)(data + size),
5333 offsetof(struct skb_shared_info,
5334 frags[skb_shinfo(skb)->nr_frags]));
5335 if (skb_cloned(skb)) {
5336 /* drop the old head gracefully */
5337 if (skb_orphan_frags(skb, gfp_mask)) {
5341 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5342 skb_frag_ref(skb, i);
5343 if (skb_has_frag_list(skb))
5344 skb_clone_fraglist(skb);
5345 skb_release_data(skb);
5347 /* we can reuse existing recount- all we did was
5356 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5359 skb->end = skb->head + size;
5361 skb_set_tail_pointer(skb, skb_headlen(skb));
5362 skb_headers_offset_update(skb, 0);
5366 atomic_set(&skb_shinfo(skb)->dataref, 1);
5371 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5373 /* carve out the first eat bytes from skb's frag_list. May recurse into
5376 static int pskb_carve_frag_list(struct sk_buff *skb,
5377 struct skb_shared_info *shinfo, int eat,
5380 struct sk_buff *list = shinfo->frag_list;
5381 struct sk_buff *clone = NULL;
5382 struct sk_buff *insp = NULL;
5386 pr_err("Not enough bytes to eat. Want %d\n", eat);
5389 if (list->len <= eat) {
5390 /* Eaten as whole. */
5395 /* Eaten partially. */
5396 if (skb_shared(list)) {
5397 clone = skb_clone(list, gfp_mask);
5403 /* This may be pulled without problems. */
5406 if (pskb_carve(list, eat, gfp_mask) < 0) {
5414 /* Free pulled out fragments. */
5415 while ((list = shinfo->frag_list) != insp) {
5416 shinfo->frag_list = list->next;
5419 /* And insert new clone at head. */
5422 shinfo->frag_list = clone;
5427 /* carve off first len bytes from skb. Split line (off) is in the
5428 * non-linear part of skb
5430 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5431 int pos, gfp_t gfp_mask)
5434 int size = skb_end_offset(skb);
5436 const int nfrags = skb_shinfo(skb)->nr_frags;
5437 struct skb_shared_info *shinfo;
5439 size = SKB_DATA_ALIGN(size);
5441 if (skb_pfmemalloc(skb))
5442 gfp_mask |= __GFP_MEMALLOC;
5443 data = kmalloc_reserve(size +
5444 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5445 gfp_mask, NUMA_NO_NODE, NULL);
5449 size = SKB_WITH_OVERHEAD(ksize(data));
5451 memcpy((struct skb_shared_info *)(data + size),
5452 skb_shinfo(skb), offsetof(struct skb_shared_info,
5453 frags[skb_shinfo(skb)->nr_frags]));
5454 if (skb_orphan_frags(skb, gfp_mask)) {
5458 shinfo = (struct skb_shared_info *)(data + size);
5459 for (i = 0; i < nfrags; i++) {
5460 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5462 if (pos + fsize > off) {
5463 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5467 * We have two variants in this case:
5468 * 1. Move all the frag to the second
5469 * part, if it is possible. F.e.
5470 * this approach is mandatory for TUX,
5471 * where splitting is expensive.
5472 * 2. Split is accurately. We make this.
5474 shinfo->frags[0].page_offset += off - pos;
5475 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5477 skb_frag_ref(skb, i);
5482 shinfo->nr_frags = k;
5483 if (skb_has_frag_list(skb))
5484 skb_clone_fraglist(skb);
5486 /* split line is in frag list */
5487 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
5488 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
5489 if (skb_has_frag_list(skb))
5490 kfree_skb_list(skb_shinfo(skb)->frag_list);
5494 skb_release_data(skb);
5499 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5502 skb->end = skb->head + size;
5504 skb_reset_tail_pointer(skb);
5505 skb_headers_offset_update(skb, 0);
5510 skb->data_len = skb->len;
5511 atomic_set(&skb_shinfo(skb)->dataref, 1);
5515 /* remove len bytes from the beginning of the skb */
5516 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5518 int headlen = skb_headlen(skb);
5521 return pskb_carve_inside_header(skb, len, headlen, gfp);
5523 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5526 /* Extract to_copy bytes starting at off from skb, and return this in
5529 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5530 int to_copy, gfp_t gfp)
5532 struct sk_buff *clone = skb_clone(skb, gfp);
5537 if (pskb_carve(clone, off, gfp) < 0 ||
5538 pskb_trim(clone, to_copy)) {
5544 EXPORT_SYMBOL(pskb_extract);
5547 * skb_condense - try to get rid of fragments/frag_list if possible
5550 * Can be used to save memory before skb is added to a busy queue.
5551 * If packet has bytes in frags and enough tail room in skb->head,
5552 * pull all of them, so that we can free the frags right now and adjust
5555 * We do not reallocate skb->head thus can not fail.
5556 * Caller must re-evaluate skb->truesize if needed.
5558 void skb_condense(struct sk_buff *skb)
5560 if (skb->data_len) {
5561 if (skb->data_len > skb->end - skb->tail ||
5565 /* Nice, we can free page frag(s) right now */
5566 __pskb_pull_tail(skb, skb->data_len);
5568 /* At this point, skb->truesize might be over estimated,
5569 * because skb had a fragment, and fragments do not tell
5571 * When we pulled its content into skb->head, fragment
5572 * was freed, but __pskb_pull_tail() could not possibly
5573 * adjust skb->truesize, not knowing the frag truesize.
5575 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));