1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines having to do with the 'struct sk_buff' memory handlers.
5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
9 * Alan Cox : Fixed the worst of the load
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
32 * The functions in this file will not compile correctly with gcc 2.4.x
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
41 #include <linux/interrupt.h>
43 #include <linux/inet.h>
44 #include <linux/slab.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 #include <linux/sctp.h>
48 #include <linux/netdevice.h>
49 #ifdef CONFIG_NET_CLS_ACT
50 #include <net/pkt_sched.h>
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/splice.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
59 #include <linux/errqueue.h>
60 #include <linux/prefetch.h>
61 #include <linux/if_vlan.h>
63 #include <net/protocol.h>
66 #include <net/checksum.h>
67 #include <net/ip6_checksum.h>
70 #include <linux/uaccess.h>
71 #include <trace/events/skb.h>
72 #include <linux/highmem.h>
73 #include <linux/capability.h>
74 #include <linux/user_namespace.h>
78 struct kmem_cache *skbuff_head_cache __ro_after_init;
79 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
80 #ifdef CONFIG_SKB_EXTENSIONS
81 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
83 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84 EXPORT_SYMBOL(sysctl_max_skb_frags);
87 * skb_panic - private function for out-of-line support
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
108 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
113 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115 skb_panic(skb, sz, addr, __func__);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
132 bool ret_pfmemalloc = false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
150 *pfmemalloc = ret_pfmemalloc;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
162 * __alloc_skb - allocate a network buffer
163 * @size: size to allocate
164 * @gfp_mask: allocation mask
165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
169 * @node: numa node to allocate memory on
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
178 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
181 struct kmem_cache *cache;
182 struct skb_shared_info *shinfo;
187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
204 size = SKB_DATA_ALIGN(size);
205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
213 size = SKB_WITH_OVERHEAD(ksize(data));
214 prefetchw(data + size);
217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
221 memset(skb, 0, offsetof(struct sk_buff, tail));
222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
224 skb->pfmemalloc = pfmemalloc;
225 refcount_set(&skb->users, 1);
228 skb_reset_tail_pointer(skb);
229 skb->end = skb->tail + size;
230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
236 atomic_set(&shinfo->dataref, 1);
238 if (flags & SKB_ALLOC_FCLONE) {
239 struct sk_buff_fclones *fclones;
241 fclones = container_of(skb, struct sk_buff_fclones, skb1);
243 skb->fclone = SKB_FCLONE_ORIG;
244 refcount_set(&fclones->fclone_ref, 1);
246 fclones->skb2.fclone = SKB_FCLONE_CLONE;
251 kmem_cache_free(cache, skb);
255 EXPORT_SYMBOL(__alloc_skb);
257 /* Caller must provide SKB that is memset cleared */
258 static struct sk_buff *__build_skb_around(struct sk_buff *skb,
259 void *data, unsigned int frag_size)
261 struct skb_shared_info *shinfo;
262 unsigned int size = frag_size ? : ksize(data);
264 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
266 /* Assumes caller memset cleared SKB */
267 skb->truesize = SKB_TRUESIZE(size);
268 refcount_set(&skb->users, 1);
271 skb_reset_tail_pointer(skb);
272 skb->end = skb->tail + size;
273 skb->mac_header = (typeof(skb->mac_header))~0U;
274 skb->transport_header = (typeof(skb->transport_header))~0U;
276 /* make sure we initialize shinfo sequentially */
277 shinfo = skb_shinfo(skb);
278 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
279 atomic_set(&shinfo->dataref, 1);
285 * __build_skb - build a network buffer
286 * @data: data buffer provided by caller
287 * @frag_size: size of data, or 0 if head was kmalloced
289 * Allocate a new &sk_buff. Caller provides space holding head and
290 * skb_shared_info. @data must have been allocated by kmalloc() only if
291 * @frag_size is 0, otherwise data should come from the page allocator
293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
303 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
311 memset(skb, 0, offsetof(struct sk_buff, tail));
313 return __build_skb_around(skb, data, frag_size);
316 /* build_skb() is wrapper over __build_skb(), that specifically
317 * takes care of skb->head and skb->pfmemalloc
318 * This means that if @frag_size is not zero, then @data must be backed
319 * by a page fragment, not kmalloc() or vmalloc()
321 struct sk_buff *build_skb(void *data, unsigned int frag_size)
323 struct sk_buff *skb = __build_skb(data, frag_size);
325 if (skb && frag_size) {
327 if (page_is_pfmemalloc(virt_to_head_page(data)))
332 EXPORT_SYMBOL(build_skb);
335 * build_skb_around - build a network buffer around provided skb
336 * @skb: sk_buff provide by caller, must be memset cleared
337 * @data: data buffer provided by caller
338 * @frag_size: size of data, or 0 if head was kmalloced
340 struct sk_buff *build_skb_around(struct sk_buff *skb,
341 void *data, unsigned int frag_size)
346 skb = __build_skb_around(skb, data, frag_size);
348 if (skb && frag_size) {
350 if (page_is_pfmemalloc(virt_to_head_page(data)))
355 EXPORT_SYMBOL(build_skb_around);
357 #define NAPI_SKB_CACHE_SIZE 64
359 struct napi_alloc_cache {
360 struct page_frag_cache page;
361 unsigned int skb_count;
362 void *skb_cache[NAPI_SKB_CACHE_SIZE];
365 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
366 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
368 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
370 struct page_frag_cache *nc;
374 local_irq_save(flags);
375 nc = this_cpu_ptr(&netdev_alloc_cache);
376 data = page_frag_alloc(nc, fragsz, gfp_mask);
377 local_irq_restore(flags);
382 * netdev_alloc_frag - allocate a page fragment
383 * @fragsz: fragment size
385 * Allocates a frag from a page for receive buffer.
386 * Uses GFP_ATOMIC allocations.
388 void *netdev_alloc_frag(unsigned int fragsz)
390 fragsz = SKB_DATA_ALIGN(fragsz);
392 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
394 EXPORT_SYMBOL(netdev_alloc_frag);
396 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
398 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
400 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
403 void *napi_alloc_frag(unsigned int fragsz)
405 fragsz = SKB_DATA_ALIGN(fragsz);
407 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
409 EXPORT_SYMBOL(napi_alloc_frag);
412 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
413 * @dev: network device to receive on
414 * @len: length to allocate
415 * @gfp_mask: get_free_pages mask, passed to alloc_skb
417 * Allocate a new &sk_buff and assign it a usage count of one. The
418 * buffer has NET_SKB_PAD headroom built in. Users should allocate
419 * the headroom they think they need without accounting for the
420 * built in space. The built in space is used for optimisations.
422 * %NULL is returned if there is no free memory.
424 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
427 struct page_frag_cache *nc;
435 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
436 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
437 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
443 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
444 len = SKB_DATA_ALIGN(len);
446 if (sk_memalloc_socks())
447 gfp_mask |= __GFP_MEMALLOC;
449 local_irq_save(flags);
451 nc = this_cpu_ptr(&netdev_alloc_cache);
452 data = page_frag_alloc(nc, len, gfp_mask);
453 pfmemalloc = nc->pfmemalloc;
455 local_irq_restore(flags);
460 skb = __build_skb(data, len);
461 if (unlikely(!skb)) {
466 /* use OR instead of assignment to avoid clearing of bits in mask */
472 skb_reserve(skb, NET_SKB_PAD);
478 EXPORT_SYMBOL(__netdev_alloc_skb);
481 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
482 * @napi: napi instance this buffer was allocated for
483 * @len: length to allocate
484 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
486 * Allocate a new sk_buff for use in NAPI receive. This buffer will
487 * attempt to allocate the head from a special reserved region used
488 * only for NAPI Rx allocation. By doing this we can save several
489 * CPU cycles by avoiding having to disable and re-enable IRQs.
491 * %NULL is returned if there is no free memory.
493 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
496 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
500 len += NET_SKB_PAD + NET_IP_ALIGN;
502 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
503 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
504 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
510 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
511 len = SKB_DATA_ALIGN(len);
513 if (sk_memalloc_socks())
514 gfp_mask |= __GFP_MEMALLOC;
516 data = page_frag_alloc(&nc->page, len, gfp_mask);
520 skb = __build_skb(data, len);
521 if (unlikely(!skb)) {
526 /* use OR instead of assignment to avoid clearing of bits in mask */
527 if (nc->page.pfmemalloc)
532 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
533 skb->dev = napi->dev;
538 EXPORT_SYMBOL(__napi_alloc_skb);
540 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
541 int size, unsigned int truesize)
543 skb_fill_page_desc(skb, i, page, off, size);
545 skb->data_len += size;
546 skb->truesize += truesize;
548 EXPORT_SYMBOL(skb_add_rx_frag);
550 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
551 unsigned int truesize)
553 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
555 skb_frag_size_add(frag, size);
557 skb->data_len += size;
558 skb->truesize += truesize;
560 EXPORT_SYMBOL(skb_coalesce_rx_frag);
562 static void skb_drop_list(struct sk_buff **listp)
564 kfree_skb_list(*listp);
568 static inline void skb_drop_fraglist(struct sk_buff *skb)
570 skb_drop_list(&skb_shinfo(skb)->frag_list);
573 static void skb_clone_fraglist(struct sk_buff *skb)
575 struct sk_buff *list;
577 skb_walk_frags(skb, list)
581 static void skb_free_head(struct sk_buff *skb)
583 unsigned char *head = skb->head;
591 static void skb_release_data(struct sk_buff *skb)
593 struct skb_shared_info *shinfo = skb_shinfo(skb);
597 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
601 for (i = 0; i < shinfo->nr_frags; i++)
602 __skb_frag_unref(&shinfo->frags[i]);
604 if (shinfo->frag_list)
605 kfree_skb_list(shinfo->frag_list);
607 skb_zcopy_clear(skb, true);
612 * Free an skbuff by memory without cleaning the state.
614 static void kfree_skbmem(struct sk_buff *skb)
616 struct sk_buff_fclones *fclones;
618 switch (skb->fclone) {
619 case SKB_FCLONE_UNAVAILABLE:
620 kmem_cache_free(skbuff_head_cache, skb);
623 case SKB_FCLONE_ORIG:
624 fclones = container_of(skb, struct sk_buff_fclones, skb1);
626 /* We usually free the clone (TX completion) before original skb
627 * This test would have no chance to be true for the clone,
628 * while here, branch prediction will be good.
630 if (refcount_read(&fclones->fclone_ref) == 1)
634 default: /* SKB_FCLONE_CLONE */
635 fclones = container_of(skb, struct sk_buff_fclones, skb2);
638 if (!refcount_dec_and_test(&fclones->fclone_ref))
641 kmem_cache_free(skbuff_fclone_cache, fclones);
644 void skb_release_head_state(struct sk_buff *skb)
647 if (skb->destructor) {
649 skb->destructor(skb);
651 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
652 nf_conntrack_put(skb_nfct(skb));
657 /* Free everything but the sk_buff shell. */
658 static void skb_release_all(struct sk_buff *skb)
660 skb_release_head_state(skb);
661 if (likely(skb->head))
662 skb_release_data(skb);
666 * __kfree_skb - private function
669 * Free an sk_buff. Release anything attached to the buffer.
670 * Clean the state. This is an internal helper function. Users should
671 * always call kfree_skb
674 void __kfree_skb(struct sk_buff *skb)
676 skb_release_all(skb);
679 EXPORT_SYMBOL(__kfree_skb);
682 * kfree_skb - free an sk_buff
683 * @skb: buffer to free
685 * Drop a reference to the buffer and free it if the usage count has
688 void kfree_skb(struct sk_buff *skb)
693 trace_kfree_skb(skb, __builtin_return_address(0));
696 EXPORT_SYMBOL(kfree_skb);
698 void kfree_skb_list(struct sk_buff *segs)
701 struct sk_buff *next = segs->next;
707 EXPORT_SYMBOL(kfree_skb_list);
710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
716 void skb_tx_error(struct sk_buff *skb)
718 skb_zcopy_clear(skb, true);
720 EXPORT_SYMBOL(skb_tx_error);
723 * consume_skb - free an skbuff
724 * @skb: buffer to free
726 * Drop a ref to the buffer and free it if the usage count has hit zero
727 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
728 * is being dropped after a failure and notes that
730 void consume_skb(struct sk_buff *skb)
735 trace_consume_skb(skb);
738 EXPORT_SYMBOL(consume_skb);
741 * consume_stateless_skb - free an skbuff, assuming it is stateless
742 * @skb: buffer to free
744 * Alike consume_skb(), but this variant assumes that this is the last
745 * skb reference and all the head states have been already dropped
747 void __consume_stateless_skb(struct sk_buff *skb)
749 trace_consume_skb(skb);
750 skb_release_data(skb);
754 void __kfree_skb_flush(void)
756 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
758 /* flush skb_cache if containing objects */
760 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
766 static inline void _kfree_skb_defer(struct sk_buff *skb)
768 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
770 /* drop skb->head and call any destructors for packet */
771 skb_release_all(skb);
773 /* record skb to CPU local list */
774 nc->skb_cache[nc->skb_count++] = skb;
777 /* SLUB writes into objects when freeing */
781 /* flush skb_cache if it is filled */
782 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
783 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
788 void __kfree_skb_defer(struct sk_buff *skb)
790 _kfree_skb_defer(skb);
793 void napi_consume_skb(struct sk_buff *skb, int budget)
798 /* Zero budget indicate non-NAPI context called us, like netpoll */
799 if (unlikely(!budget)) {
800 dev_consume_skb_any(skb);
807 /* if reaching here SKB is ready to free */
808 trace_consume_skb(skb);
810 /* if SKB is a clone, don't handle this case */
811 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
816 _kfree_skb_defer(skb);
818 EXPORT_SYMBOL(napi_consume_skb);
820 /* Make sure a field is enclosed inside headers_start/headers_end section */
821 #define CHECK_SKB_FIELD(field) \
822 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
823 offsetof(struct sk_buff, headers_start)); \
824 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
825 offsetof(struct sk_buff, headers_end)); \
827 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
829 new->tstamp = old->tstamp;
830 /* We do not copy old->sk */
832 memcpy(new->cb, old->cb, sizeof(old->cb));
833 skb_dst_copy(new, old);
834 __skb_ext_copy(new, old);
835 __nf_copy(new, old, false);
837 /* Note : this field could be in headers_start/headers_end section
838 * It is not yet because we do not want to have a 16 bit hole
840 new->queue_mapping = old->queue_mapping;
842 memcpy(&new->headers_start, &old->headers_start,
843 offsetof(struct sk_buff, headers_end) -
844 offsetof(struct sk_buff, headers_start));
845 CHECK_SKB_FIELD(protocol);
846 CHECK_SKB_FIELD(csum);
847 CHECK_SKB_FIELD(hash);
848 CHECK_SKB_FIELD(priority);
849 CHECK_SKB_FIELD(skb_iif);
850 CHECK_SKB_FIELD(vlan_proto);
851 CHECK_SKB_FIELD(vlan_tci);
852 CHECK_SKB_FIELD(transport_header);
853 CHECK_SKB_FIELD(network_header);
854 CHECK_SKB_FIELD(mac_header);
855 CHECK_SKB_FIELD(inner_protocol);
856 CHECK_SKB_FIELD(inner_transport_header);
857 CHECK_SKB_FIELD(inner_network_header);
858 CHECK_SKB_FIELD(inner_mac_header);
859 CHECK_SKB_FIELD(mark);
860 #ifdef CONFIG_NETWORK_SECMARK
861 CHECK_SKB_FIELD(secmark);
863 #ifdef CONFIG_NET_RX_BUSY_POLL
864 CHECK_SKB_FIELD(napi_id);
867 CHECK_SKB_FIELD(sender_cpu);
869 #ifdef CONFIG_NET_SCHED
870 CHECK_SKB_FIELD(tc_index);
876 * You should not add any new code to this function. Add it to
877 * __copy_skb_header above instead.
879 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
881 #define C(x) n->x = skb->x
883 n->next = n->prev = NULL;
885 __copy_skb_header(n, skb);
890 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
895 n->destructor = NULL;
902 refcount_set(&n->users, 1);
904 atomic_inc(&(skb_shinfo(skb)->dataref));
912 * skb_morph - morph one skb into another
913 * @dst: the skb to receive the contents
914 * @src: the skb to supply the contents
916 * This is identical to skb_clone except that the target skb is
917 * supplied by the user.
919 * The target skb is returned upon exit.
921 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
923 skb_release_all(dst);
924 return __skb_clone(dst, src);
926 EXPORT_SYMBOL_GPL(skb_morph);
928 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
930 unsigned long max_pg, num_pg, new_pg, old_pg;
931 struct user_struct *user;
933 if (capable(CAP_IPC_LOCK) || !size)
936 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
937 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
938 user = mmp->user ? : current_user();
941 old_pg = atomic_long_read(&user->locked_vm);
942 new_pg = old_pg + num_pg;
945 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
949 mmp->user = get_uid(user);
950 mmp->num_pg = num_pg;
952 mmp->num_pg += num_pg;
957 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
959 void mm_unaccount_pinned_pages(struct mmpin *mmp)
962 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
966 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
968 struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
970 struct ubuf_info *uarg;
973 WARN_ON_ONCE(!in_task());
975 skb = sock_omalloc(sk, 0, GFP_KERNEL);
979 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
980 uarg = (void *)skb->cb;
981 uarg->mmp.user = NULL;
983 if (mm_account_pinned_pages(&uarg->mmp, size)) {
988 uarg->callback = sock_zerocopy_callback;
989 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
991 uarg->bytelen = size;
993 refcount_set(&uarg->refcnt, 1);
998 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
1000 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1002 return container_of((void *)uarg, struct sk_buff, cb);
1005 struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1006 struct ubuf_info *uarg)
1009 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1012 /* realloc only when socket is locked (TCP, UDP cork),
1013 * so uarg->len and sk_zckey access is serialized
1015 if (!sock_owned_by_user(sk)) {
1020 bytelen = uarg->bytelen + size;
1021 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1022 /* TCP can create new skb to attach new uarg */
1023 if (sk->sk_type == SOCK_STREAM)
1028 next = (u32)atomic_read(&sk->sk_zckey);
1029 if ((u32)(uarg->id + uarg->len) == next) {
1030 if (mm_account_pinned_pages(&uarg->mmp, size))
1033 uarg->bytelen = bytelen;
1034 atomic_set(&sk->sk_zckey, ++next);
1036 /* no extra ref when appending to datagram (MSG_MORE) */
1037 if (sk->sk_type == SOCK_STREAM)
1038 sock_zerocopy_get(uarg);
1045 return sock_zerocopy_alloc(sk, size);
1047 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1049 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1051 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1055 old_lo = serr->ee.ee_info;
1056 old_hi = serr->ee.ee_data;
1057 sum_len = old_hi - old_lo + 1ULL + len;
1059 if (sum_len >= (1ULL << 32))
1062 if (lo != old_hi + 1)
1065 serr->ee.ee_data += len;
1069 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1071 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1072 struct sock_exterr_skb *serr;
1073 struct sock *sk = skb->sk;
1074 struct sk_buff_head *q;
1075 unsigned long flags;
1079 mm_unaccount_pinned_pages(&uarg->mmp);
1081 /* if !len, there was only 1 call, and it was aborted
1082 * so do not queue a completion notification
1084 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1089 hi = uarg->id + len - 1;
1091 serr = SKB_EXT_ERR(skb);
1092 memset(serr, 0, sizeof(*serr));
1093 serr->ee.ee_errno = 0;
1094 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1095 serr->ee.ee_data = hi;
1096 serr->ee.ee_info = lo;
1098 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1100 q = &sk->sk_error_queue;
1101 spin_lock_irqsave(&q->lock, flags);
1102 tail = skb_peek_tail(q);
1103 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1104 !skb_zerocopy_notify_extend(tail, lo, len)) {
1105 __skb_queue_tail(q, skb);
1108 spin_unlock_irqrestore(&q->lock, flags);
1110 sk->sk_error_report(sk);
1116 EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1118 void sock_zerocopy_put(struct ubuf_info *uarg)
1120 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
1122 uarg->callback(uarg, uarg->zerocopy);
1124 consume_skb(skb_from_uarg(uarg));
1127 EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1129 void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1132 struct sock *sk = skb_from_uarg(uarg)->sk;
1134 atomic_dec(&sk->sk_zckey);
1138 sock_zerocopy_put(uarg);
1141 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1143 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1145 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1147 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1149 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1150 struct msghdr *msg, int len,
1151 struct ubuf_info *uarg)
1153 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1154 struct iov_iter orig_iter = msg->msg_iter;
1155 int err, orig_len = skb->len;
1157 /* An skb can only point to one uarg. This edge case happens when
1158 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1160 if (orig_uarg && uarg != orig_uarg)
1163 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1164 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1165 struct sock *save_sk = skb->sk;
1167 /* Streams do not free skb on error. Reset to prev state. */
1168 msg->msg_iter = orig_iter;
1170 ___pskb_trim(skb, orig_len);
1175 skb_zcopy_set(skb, uarg, NULL);
1176 return skb->len - orig_len;
1178 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1180 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1183 if (skb_zcopy(orig)) {
1184 if (skb_zcopy(nskb)) {
1185 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1190 if (skb_uarg(nskb) == skb_uarg(orig))
1192 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1195 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1201 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1202 * @skb: the skb to modify
1203 * @gfp_mask: allocation priority
1205 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1206 * It will copy all frags into kernel and drop the reference
1207 * to userspace pages.
1209 * If this function is called from an interrupt gfp_mask() must be
1212 * Returns 0 on success or a negative error code on failure
1213 * to allocate kernel memory to copy to.
1215 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1217 int num_frags = skb_shinfo(skb)->nr_frags;
1218 struct page *page, *head = NULL;
1222 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1228 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1229 for (i = 0; i < new_frags; i++) {
1230 page = alloc_page(gfp_mask);
1233 struct page *next = (struct page *)page_private(head);
1239 set_page_private(page, (unsigned long)head);
1245 for (i = 0; i < num_frags; i++) {
1246 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1247 u32 p_off, p_len, copied;
1251 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1252 p, p_off, p_len, copied) {
1254 vaddr = kmap_atomic(p);
1256 while (done < p_len) {
1257 if (d_off == PAGE_SIZE) {
1259 page = (struct page *)page_private(page);
1261 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1262 memcpy(page_address(page) + d_off,
1263 vaddr + p_off + done, copy);
1267 kunmap_atomic(vaddr);
1271 /* skb frags release userspace buffers */
1272 for (i = 0; i < num_frags; i++)
1273 skb_frag_unref(skb, i);
1275 /* skb frags point to kernel buffers */
1276 for (i = 0; i < new_frags - 1; i++) {
1277 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1278 head = (struct page *)page_private(head);
1280 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1281 skb_shinfo(skb)->nr_frags = new_frags;
1284 skb_zcopy_clear(skb, false);
1287 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1290 * skb_clone - duplicate an sk_buff
1291 * @skb: buffer to clone
1292 * @gfp_mask: allocation priority
1294 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1295 * copies share the same packet data but not structure. The new
1296 * buffer has a reference count of 1. If the allocation fails the
1297 * function returns %NULL otherwise the new buffer is returned.
1299 * If this function is called from an interrupt gfp_mask() must be
1303 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1305 struct sk_buff_fclones *fclones = container_of(skb,
1306 struct sk_buff_fclones,
1310 if (skb_orphan_frags(skb, gfp_mask))
1313 if (skb->fclone == SKB_FCLONE_ORIG &&
1314 refcount_read(&fclones->fclone_ref) == 1) {
1316 refcount_set(&fclones->fclone_ref, 2);
1318 if (skb_pfmemalloc(skb))
1319 gfp_mask |= __GFP_MEMALLOC;
1321 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1325 n->fclone = SKB_FCLONE_UNAVAILABLE;
1328 return __skb_clone(n, skb);
1330 EXPORT_SYMBOL(skb_clone);
1332 void skb_headers_offset_update(struct sk_buff *skb, int off)
1334 /* Only adjust this if it actually is csum_start rather than csum */
1335 if (skb->ip_summed == CHECKSUM_PARTIAL)
1336 skb->csum_start += off;
1337 /* {transport,network,mac}_header and tail are relative to skb->head */
1338 skb->transport_header += off;
1339 skb->network_header += off;
1340 if (skb_mac_header_was_set(skb))
1341 skb->mac_header += off;
1342 skb->inner_transport_header += off;
1343 skb->inner_network_header += off;
1344 skb->inner_mac_header += off;
1346 EXPORT_SYMBOL(skb_headers_offset_update);
1348 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1350 __copy_skb_header(new, old);
1352 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1353 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1354 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1356 EXPORT_SYMBOL(skb_copy_header);
1358 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1360 if (skb_pfmemalloc(skb))
1361 return SKB_ALLOC_RX;
1366 * skb_copy - create private copy of an sk_buff
1367 * @skb: buffer to copy
1368 * @gfp_mask: allocation priority
1370 * Make a copy of both an &sk_buff and its data. This is used when the
1371 * caller wishes to modify the data and needs a private copy of the
1372 * data to alter. Returns %NULL on failure or the pointer to the buffer
1373 * on success. The returned buffer has a reference count of 1.
1375 * As by-product this function converts non-linear &sk_buff to linear
1376 * one, so that &sk_buff becomes completely private and caller is allowed
1377 * to modify all the data of returned buffer. This means that this
1378 * function is not recommended for use in circumstances when only
1379 * header is going to be modified. Use pskb_copy() instead.
1382 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1384 int headerlen = skb_headroom(skb);
1385 unsigned int size = skb_end_offset(skb) + skb->data_len;
1386 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1387 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1392 /* Set the data pointer */
1393 skb_reserve(n, headerlen);
1394 /* Set the tail pointer and length */
1395 skb_put(n, skb->len);
1397 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1399 skb_copy_header(n, skb);
1402 EXPORT_SYMBOL(skb_copy);
1405 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1406 * @skb: buffer to copy
1407 * @headroom: headroom of new skb
1408 * @gfp_mask: allocation priority
1409 * @fclone: if true allocate the copy of the skb from the fclone
1410 * cache instead of the head cache; it is recommended to set this
1411 * to true for the cases where the copy will likely be cloned
1413 * Make a copy of both an &sk_buff and part of its data, located
1414 * in header. Fragmented data remain shared. This is used when
1415 * the caller wishes to modify only header of &sk_buff and needs
1416 * private copy of the header to alter. Returns %NULL on failure
1417 * or the pointer to the buffer on success.
1418 * The returned buffer has a reference count of 1.
1421 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1422 gfp_t gfp_mask, bool fclone)
1424 unsigned int size = skb_headlen(skb) + headroom;
1425 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1426 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1431 /* Set the data pointer */
1432 skb_reserve(n, headroom);
1433 /* Set the tail pointer and length */
1434 skb_put(n, skb_headlen(skb));
1435 /* Copy the bytes */
1436 skb_copy_from_linear_data(skb, n->data, n->len);
1438 n->truesize += skb->data_len;
1439 n->data_len = skb->data_len;
1442 if (skb_shinfo(skb)->nr_frags) {
1445 if (skb_orphan_frags(skb, gfp_mask) ||
1446 skb_zerocopy_clone(n, skb, gfp_mask)) {
1451 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1452 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1453 skb_frag_ref(skb, i);
1455 skb_shinfo(n)->nr_frags = i;
1458 if (skb_has_frag_list(skb)) {
1459 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1460 skb_clone_fraglist(n);
1463 skb_copy_header(n, skb);
1467 EXPORT_SYMBOL(__pskb_copy_fclone);
1470 * pskb_expand_head - reallocate header of &sk_buff
1471 * @skb: buffer to reallocate
1472 * @nhead: room to add at head
1473 * @ntail: room to add at tail
1474 * @gfp_mask: allocation priority
1476 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1477 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1478 * reference count of 1. Returns zero in the case of success or error,
1479 * if expansion failed. In the last case, &sk_buff is not changed.
1481 * All the pointers pointing into skb header may change and must be
1482 * reloaded after call to this function.
1485 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1488 int i, osize = skb_end_offset(skb);
1489 int size = osize + nhead + ntail;
1495 BUG_ON(skb_shared(skb));
1497 size = SKB_DATA_ALIGN(size);
1499 if (skb_pfmemalloc(skb))
1500 gfp_mask |= __GFP_MEMALLOC;
1501 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1502 gfp_mask, NUMA_NO_NODE, NULL);
1505 size = SKB_WITH_OVERHEAD(ksize(data));
1507 /* Copy only real data... and, alas, header. This should be
1508 * optimized for the cases when header is void.
1510 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1512 memcpy((struct skb_shared_info *)(data + size),
1514 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1517 * if shinfo is shared we must drop the old head gracefully, but if it
1518 * is not we can just drop the old head and let the existing refcount
1519 * be since all we did is relocate the values
1521 if (skb_cloned(skb)) {
1522 if (skb_orphan_frags(skb, gfp_mask))
1525 refcount_inc(&skb_uarg(skb)->refcnt);
1526 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1527 skb_frag_ref(skb, i);
1529 if (skb_has_frag_list(skb))
1530 skb_clone_fraglist(skb);
1532 skb_release_data(skb);
1536 off = (data + nhead) - skb->head;
1541 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1545 skb->end = skb->head + size;
1548 skb_headers_offset_update(skb, nhead);
1552 atomic_set(&skb_shinfo(skb)->dataref, 1);
1554 skb_metadata_clear(skb);
1556 /* It is not generally safe to change skb->truesize.
1557 * For the moment, we really care of rx path, or
1558 * when skb is orphaned (not attached to a socket).
1560 if (!skb->sk || skb->destructor == sock_edemux)
1561 skb->truesize += size - osize;
1570 EXPORT_SYMBOL(pskb_expand_head);
1572 /* Make private copy of skb with writable head and some headroom */
1574 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1576 struct sk_buff *skb2;
1577 int delta = headroom - skb_headroom(skb);
1580 skb2 = pskb_copy(skb, GFP_ATOMIC);
1582 skb2 = skb_clone(skb, GFP_ATOMIC);
1583 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1591 EXPORT_SYMBOL(skb_realloc_headroom);
1594 * skb_copy_expand - copy and expand sk_buff
1595 * @skb: buffer to copy
1596 * @newheadroom: new free bytes at head
1597 * @newtailroom: new free bytes at tail
1598 * @gfp_mask: allocation priority
1600 * Make a copy of both an &sk_buff and its data and while doing so
1601 * allocate additional space.
1603 * This is used when the caller wishes to modify the data and needs a
1604 * private copy of the data to alter as well as more space for new fields.
1605 * Returns %NULL on failure or the pointer to the buffer
1606 * on success. The returned buffer has a reference count of 1.
1608 * You must pass %GFP_ATOMIC as the allocation priority if this function
1609 * is called from an interrupt.
1611 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1612 int newheadroom, int newtailroom,
1616 * Allocate the copy buffer
1618 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1619 gfp_mask, skb_alloc_rx_flag(skb),
1621 int oldheadroom = skb_headroom(skb);
1622 int head_copy_len, head_copy_off;
1627 skb_reserve(n, newheadroom);
1629 /* Set the tail pointer and length */
1630 skb_put(n, skb->len);
1632 head_copy_len = oldheadroom;
1634 if (newheadroom <= head_copy_len)
1635 head_copy_len = newheadroom;
1637 head_copy_off = newheadroom - head_copy_len;
1639 /* Copy the linear header and data. */
1640 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1641 skb->len + head_copy_len));
1643 skb_copy_header(n, skb);
1645 skb_headers_offset_update(n, newheadroom - oldheadroom);
1649 EXPORT_SYMBOL(skb_copy_expand);
1652 * __skb_pad - zero pad the tail of an skb
1653 * @skb: buffer to pad
1654 * @pad: space to pad
1655 * @free_on_error: free buffer on error
1657 * Ensure that a buffer is followed by a padding area that is zero
1658 * filled. Used by network drivers which may DMA or transfer data
1659 * beyond the buffer end onto the wire.
1661 * May return error in out of memory cases. The skb is freed on error
1662 * if @free_on_error is true.
1665 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1670 /* If the skbuff is non linear tailroom is always zero.. */
1671 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1672 memset(skb->data+skb->len, 0, pad);
1676 ntail = skb->data_len + pad - (skb->end - skb->tail);
1677 if (likely(skb_cloned(skb) || ntail > 0)) {
1678 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1683 /* FIXME: The use of this function with non-linear skb's really needs
1686 err = skb_linearize(skb);
1690 memset(skb->data + skb->len, 0, pad);
1698 EXPORT_SYMBOL(__skb_pad);
1701 * pskb_put - add data to the tail of a potentially fragmented buffer
1702 * @skb: start of the buffer to use
1703 * @tail: tail fragment of the buffer to use
1704 * @len: amount of data to add
1706 * This function extends the used data area of the potentially
1707 * fragmented buffer. @tail must be the last fragment of @skb -- or
1708 * @skb itself. If this would exceed the total buffer size the kernel
1709 * will panic. A pointer to the first byte of the extra data is
1713 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1716 skb->data_len += len;
1719 return skb_put(tail, len);
1721 EXPORT_SYMBOL_GPL(pskb_put);
1724 * skb_put - add data to a buffer
1725 * @skb: buffer to use
1726 * @len: amount of data to add
1728 * This function extends the used data area of the buffer. If this would
1729 * exceed the total buffer size the kernel will panic. A pointer to the
1730 * first byte of the extra data is returned.
1732 void *skb_put(struct sk_buff *skb, unsigned int len)
1734 void *tmp = skb_tail_pointer(skb);
1735 SKB_LINEAR_ASSERT(skb);
1738 if (unlikely(skb->tail > skb->end))
1739 skb_over_panic(skb, len, __builtin_return_address(0));
1742 EXPORT_SYMBOL(skb_put);
1745 * skb_push - add data to the start of a buffer
1746 * @skb: buffer to use
1747 * @len: amount of data to add
1749 * This function extends the used data area of the buffer at the buffer
1750 * start. If this would exceed the total buffer headroom the kernel will
1751 * panic. A pointer to the first byte of the extra data is returned.
1753 void *skb_push(struct sk_buff *skb, unsigned int len)
1757 if (unlikely(skb->data < skb->head))
1758 skb_under_panic(skb, len, __builtin_return_address(0));
1761 EXPORT_SYMBOL(skb_push);
1764 * skb_pull - remove data from the start of a buffer
1765 * @skb: buffer to use
1766 * @len: amount of data to remove
1768 * This function removes data from the start of a buffer, returning
1769 * the memory to the headroom. A pointer to the next data in the buffer
1770 * is returned. Once the data has been pulled future pushes will overwrite
1773 void *skb_pull(struct sk_buff *skb, unsigned int len)
1775 return skb_pull_inline(skb, len);
1777 EXPORT_SYMBOL(skb_pull);
1780 * skb_trim - remove end from a buffer
1781 * @skb: buffer to alter
1784 * Cut the length of a buffer down by removing data from the tail. If
1785 * the buffer is already under the length specified it is not modified.
1786 * The skb must be linear.
1788 void skb_trim(struct sk_buff *skb, unsigned int len)
1791 __skb_trim(skb, len);
1793 EXPORT_SYMBOL(skb_trim);
1795 /* Trims skb to length len. It can change skb pointers.
1798 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1800 struct sk_buff **fragp;
1801 struct sk_buff *frag;
1802 int offset = skb_headlen(skb);
1803 int nfrags = skb_shinfo(skb)->nr_frags;
1807 if (skb_cloned(skb) &&
1808 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1815 for (; i < nfrags; i++) {
1816 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1823 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1826 skb_shinfo(skb)->nr_frags = i;
1828 for (; i < nfrags; i++)
1829 skb_frag_unref(skb, i);
1831 if (skb_has_frag_list(skb))
1832 skb_drop_fraglist(skb);
1836 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1837 fragp = &frag->next) {
1838 int end = offset + frag->len;
1840 if (skb_shared(frag)) {
1841 struct sk_buff *nfrag;
1843 nfrag = skb_clone(frag, GFP_ATOMIC);
1844 if (unlikely(!nfrag))
1847 nfrag->next = frag->next;
1859 unlikely((err = pskb_trim(frag, len - offset))))
1863 skb_drop_list(&frag->next);
1868 if (len > skb_headlen(skb)) {
1869 skb->data_len -= skb->len - len;
1874 skb_set_tail_pointer(skb, len);
1877 if (!skb->sk || skb->destructor == sock_edemux)
1881 EXPORT_SYMBOL(___pskb_trim);
1883 /* Note : use pskb_trim_rcsum() instead of calling this directly
1885 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1887 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1888 int delta = skb->len - len;
1890 skb->csum = csum_block_sub(skb->csum,
1891 skb_checksum(skb, len, delta, 0),
1894 return __pskb_trim(skb, len);
1896 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1899 * __pskb_pull_tail - advance tail of skb header
1900 * @skb: buffer to reallocate
1901 * @delta: number of bytes to advance tail
1903 * The function makes a sense only on a fragmented &sk_buff,
1904 * it expands header moving its tail forward and copying necessary
1905 * data from fragmented part.
1907 * &sk_buff MUST have reference count of 1.
1909 * Returns %NULL (and &sk_buff does not change) if pull failed
1910 * or value of new tail of skb in the case of success.
1912 * All the pointers pointing into skb header may change and must be
1913 * reloaded after call to this function.
1916 /* Moves tail of skb head forward, copying data from fragmented part,
1917 * when it is necessary.
1918 * 1. It may fail due to malloc failure.
1919 * 2. It may change skb pointers.
1921 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1923 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1925 /* If skb has not enough free space at tail, get new one
1926 * plus 128 bytes for future expansions. If we have enough
1927 * room at tail, reallocate without expansion only if skb is cloned.
1929 int i, k, eat = (skb->tail + delta) - skb->end;
1931 if (eat > 0 || skb_cloned(skb)) {
1932 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1937 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1938 skb_tail_pointer(skb), delta));
1940 /* Optimization: no fragments, no reasons to preestimate
1941 * size of pulled pages. Superb.
1943 if (!skb_has_frag_list(skb))
1946 /* Estimate size of pulled pages. */
1948 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1949 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1956 /* If we need update frag list, we are in troubles.
1957 * Certainly, it is possible to add an offset to skb data,
1958 * but taking into account that pulling is expected to
1959 * be very rare operation, it is worth to fight against
1960 * further bloating skb head and crucify ourselves here instead.
1961 * Pure masohism, indeed. 8)8)
1964 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1965 struct sk_buff *clone = NULL;
1966 struct sk_buff *insp = NULL;
1969 if (list->len <= eat) {
1970 /* Eaten as whole. */
1975 /* Eaten partially. */
1977 if (skb_shared(list)) {
1978 /* Sucks! We need to fork list. :-( */
1979 clone = skb_clone(list, GFP_ATOMIC);
1985 /* This may be pulled without
1989 if (!pskb_pull(list, eat)) {
1997 /* Free pulled out fragments. */
1998 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1999 skb_shinfo(skb)->frag_list = list->next;
2002 /* And insert new clone at head. */
2005 skb_shinfo(skb)->frag_list = clone;
2008 /* Success! Now we may commit changes to skb data. */
2013 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2014 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2017 skb_frag_unref(skb, i);
2020 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
2022 skb_shinfo(skb)->frags[k].page_offset += eat;
2023 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
2031 skb_shinfo(skb)->nr_frags = k;
2035 skb->data_len -= delta;
2038 skb_zcopy_clear(skb, false);
2040 return skb_tail_pointer(skb);
2042 EXPORT_SYMBOL(__pskb_pull_tail);
2045 * skb_copy_bits - copy bits from skb to kernel buffer
2047 * @offset: offset in source
2048 * @to: destination buffer
2049 * @len: number of bytes to copy
2051 * Copy the specified number of bytes from the source skb to the
2052 * destination buffer.
2055 * If its prototype is ever changed,
2056 * check arch/{*}/net/{*}.S files,
2057 * since it is called from BPF assembly code.
2059 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2061 int start = skb_headlen(skb);
2062 struct sk_buff *frag_iter;
2065 if (offset > (int)skb->len - len)
2069 if ((copy = start - offset) > 0) {
2072 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2073 if ((len -= copy) == 0)
2079 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2081 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2083 WARN_ON(start > offset + len);
2085 end = start + skb_frag_size(f);
2086 if ((copy = end - offset) > 0) {
2087 u32 p_off, p_len, copied;
2094 skb_frag_foreach_page(f,
2095 f->page_offset + offset - start,
2096 copy, p, p_off, p_len, copied) {
2097 vaddr = kmap_atomic(p);
2098 memcpy(to + copied, vaddr + p_off, p_len);
2099 kunmap_atomic(vaddr);
2102 if ((len -= copy) == 0)
2110 skb_walk_frags(skb, frag_iter) {
2113 WARN_ON(start > offset + len);
2115 end = start + frag_iter->len;
2116 if ((copy = end - offset) > 0) {
2119 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2121 if ((len -= copy) == 0)
2135 EXPORT_SYMBOL(skb_copy_bits);
2138 * Callback from splice_to_pipe(), if we need to release some pages
2139 * at the end of the spd in case we error'ed out in filling the pipe.
2141 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2143 put_page(spd->pages[i]);
2146 static struct page *linear_to_page(struct page *page, unsigned int *len,
2147 unsigned int *offset,
2150 struct page_frag *pfrag = sk_page_frag(sk);
2152 if (!sk_page_frag_refill(sk, pfrag))
2155 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2157 memcpy(page_address(pfrag->page) + pfrag->offset,
2158 page_address(page) + *offset, *len);
2159 *offset = pfrag->offset;
2160 pfrag->offset += *len;
2165 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2167 unsigned int offset)
2169 return spd->nr_pages &&
2170 spd->pages[spd->nr_pages - 1] == page &&
2171 (spd->partial[spd->nr_pages - 1].offset +
2172 spd->partial[spd->nr_pages - 1].len == offset);
2176 * Fill page/offset/length into spd, if it can hold more pages.
2178 static bool spd_fill_page(struct splice_pipe_desc *spd,
2179 struct pipe_inode_info *pipe, struct page *page,
2180 unsigned int *len, unsigned int offset,
2184 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2188 page = linear_to_page(page, len, &offset, sk);
2192 if (spd_can_coalesce(spd, page, offset)) {
2193 spd->partial[spd->nr_pages - 1].len += *len;
2197 spd->pages[spd->nr_pages] = page;
2198 spd->partial[spd->nr_pages].len = *len;
2199 spd->partial[spd->nr_pages].offset = offset;
2205 static bool __splice_segment(struct page *page, unsigned int poff,
2206 unsigned int plen, unsigned int *off,
2208 struct splice_pipe_desc *spd, bool linear,
2210 struct pipe_inode_info *pipe)
2215 /* skip this segment if already processed */
2221 /* ignore any bits we already processed */
2227 unsigned int flen = min(*len, plen);
2229 if (spd_fill_page(spd, pipe, page, &flen, poff,
2235 } while (*len && plen);
2241 * Map linear and fragment data from the skb to spd. It reports true if the
2242 * pipe is full or if we already spliced the requested length.
2244 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2245 unsigned int *offset, unsigned int *len,
2246 struct splice_pipe_desc *spd, struct sock *sk)
2249 struct sk_buff *iter;
2251 /* map the linear part :
2252 * If skb->head_frag is set, this 'linear' part is backed by a
2253 * fragment, and if the head is not shared with any clones then
2254 * we can avoid a copy since we own the head portion of this page.
2256 if (__splice_segment(virt_to_page(skb->data),
2257 (unsigned long) skb->data & (PAGE_SIZE - 1),
2260 skb_head_is_locked(skb),
2265 * then map the fragments
2267 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2268 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2270 if (__splice_segment(skb_frag_page(f),
2271 f->page_offset, skb_frag_size(f),
2272 offset, len, spd, false, sk, pipe))
2276 skb_walk_frags(skb, iter) {
2277 if (*offset >= iter->len) {
2278 *offset -= iter->len;
2281 /* __skb_splice_bits() only fails if the output has no room
2282 * left, so no point in going over the frag_list for the error
2285 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2293 * Map data from the skb to a pipe. Should handle both the linear part,
2294 * the fragments, and the frag list.
2296 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2297 struct pipe_inode_info *pipe, unsigned int tlen,
2300 struct partial_page partial[MAX_SKB_FRAGS];
2301 struct page *pages[MAX_SKB_FRAGS];
2302 struct splice_pipe_desc spd = {
2305 .nr_pages_max = MAX_SKB_FRAGS,
2306 .ops = &nosteal_pipe_buf_ops,
2307 .spd_release = sock_spd_release,
2311 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2314 ret = splice_to_pipe(pipe, &spd);
2318 EXPORT_SYMBOL_GPL(skb_splice_bits);
2320 /* Send skb data on a socket. Socket must be locked. */
2321 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2324 unsigned int orig_len = len;
2325 struct sk_buff *head = skb;
2326 unsigned short fragidx;
2331 /* Deal with head data */
2332 while (offset < skb_headlen(skb) && len) {
2336 slen = min_t(int, len, skb_headlen(skb) - offset);
2337 kv.iov_base = skb->data + offset;
2339 memset(&msg, 0, sizeof(msg));
2341 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2349 /* All the data was skb head? */
2353 /* Make offset relative to start of frags */
2354 offset -= skb_headlen(skb);
2356 /* Find where we are in frag list */
2357 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2358 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2360 if (offset < frag->size)
2363 offset -= frag->size;
2366 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2367 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2369 slen = min_t(size_t, len, frag->size - offset);
2372 ret = kernel_sendpage_locked(sk, frag->page.p,
2373 frag->page_offset + offset,
2374 slen, MSG_DONTWAIT);
2387 /* Process any frag lists */
2390 if (skb_has_frag_list(skb)) {
2391 skb = skb_shinfo(skb)->frag_list;
2394 } else if (skb->next) {
2401 return orig_len - len;
2404 return orig_len == len ? ret : orig_len - len;
2406 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2409 * skb_store_bits - store bits from kernel buffer to skb
2410 * @skb: destination buffer
2411 * @offset: offset in destination
2412 * @from: source buffer
2413 * @len: number of bytes to copy
2415 * Copy the specified number of bytes from the source buffer to the
2416 * destination skb. This function handles all the messy bits of
2417 * traversing fragment lists and such.
2420 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2422 int start = skb_headlen(skb);
2423 struct sk_buff *frag_iter;
2426 if (offset > (int)skb->len - len)
2429 if ((copy = start - offset) > 0) {
2432 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2433 if ((len -= copy) == 0)
2439 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2440 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2443 WARN_ON(start > offset + len);
2445 end = start + skb_frag_size(frag);
2446 if ((copy = end - offset) > 0) {
2447 u32 p_off, p_len, copied;
2454 skb_frag_foreach_page(frag,
2455 frag->page_offset + offset - start,
2456 copy, p, p_off, p_len, copied) {
2457 vaddr = kmap_atomic(p);
2458 memcpy(vaddr + p_off, from + copied, p_len);
2459 kunmap_atomic(vaddr);
2462 if ((len -= copy) == 0)
2470 skb_walk_frags(skb, frag_iter) {
2473 WARN_ON(start > offset + len);
2475 end = start + frag_iter->len;
2476 if ((copy = end - offset) > 0) {
2479 if (skb_store_bits(frag_iter, offset - start,
2482 if ((len -= copy) == 0)
2495 EXPORT_SYMBOL(skb_store_bits);
2497 /* Checksum skb data. */
2498 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2499 __wsum csum, const struct skb_checksum_ops *ops)
2501 int start = skb_headlen(skb);
2502 int i, copy = start - offset;
2503 struct sk_buff *frag_iter;
2506 /* Checksum header. */
2510 csum = ops->update(skb->data + offset, copy, csum);
2511 if ((len -= copy) == 0)
2517 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2519 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2521 WARN_ON(start > offset + len);
2523 end = start + skb_frag_size(frag);
2524 if ((copy = end - offset) > 0) {
2525 u32 p_off, p_len, copied;
2533 skb_frag_foreach_page(frag,
2534 frag->page_offset + offset - start,
2535 copy, p, p_off, p_len, copied) {
2536 vaddr = kmap_atomic(p);
2537 csum2 = ops->update(vaddr + p_off, p_len, 0);
2538 kunmap_atomic(vaddr);
2539 csum = ops->combine(csum, csum2, pos, p_len);
2550 skb_walk_frags(skb, frag_iter) {
2553 WARN_ON(start > offset + len);
2555 end = start + frag_iter->len;
2556 if ((copy = end - offset) > 0) {
2560 csum2 = __skb_checksum(frag_iter, offset - start,
2562 csum = ops->combine(csum, csum2, pos, copy);
2563 if ((len -= copy) == 0)
2574 EXPORT_SYMBOL(__skb_checksum);
2576 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2577 int len, __wsum csum)
2579 const struct skb_checksum_ops ops = {
2580 .update = csum_partial_ext,
2581 .combine = csum_block_add_ext,
2584 return __skb_checksum(skb, offset, len, csum, &ops);
2586 EXPORT_SYMBOL(skb_checksum);
2588 /* Both of above in one bottle. */
2590 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2591 u8 *to, int len, __wsum csum)
2593 int start = skb_headlen(skb);
2594 int i, copy = start - offset;
2595 struct sk_buff *frag_iter;
2602 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2604 if ((len -= copy) == 0)
2611 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2614 WARN_ON(start > offset + len);
2616 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2617 if ((copy = end - offset) > 0) {
2618 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2619 u32 p_off, p_len, copied;
2627 skb_frag_foreach_page(frag,
2628 frag->page_offset + offset - start,
2629 copy, p, p_off, p_len, copied) {
2630 vaddr = kmap_atomic(p);
2631 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2634 kunmap_atomic(vaddr);
2635 csum = csum_block_add(csum, csum2, pos);
2647 skb_walk_frags(skb, frag_iter) {
2651 WARN_ON(start > offset + len);
2653 end = start + frag_iter->len;
2654 if ((copy = end - offset) > 0) {
2657 csum2 = skb_copy_and_csum_bits(frag_iter,
2660 csum = csum_block_add(csum, csum2, pos);
2661 if ((len -= copy) == 0)
2672 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2674 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2678 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
2679 /* See comments in __skb_checksum_complete(). */
2681 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2682 !skb->csum_complete_sw)
2683 netdev_rx_csum_fault(skb->dev, skb);
2685 if (!skb_shared(skb))
2686 skb->csum_valid = !sum;
2689 EXPORT_SYMBOL(__skb_checksum_complete_head);
2691 /* This function assumes skb->csum already holds pseudo header's checksum,
2692 * which has been changed from the hardware checksum, for example, by
2693 * __skb_checksum_validate_complete(). And, the original skb->csum must
2694 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2696 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2697 * zero. The new checksum is stored back into skb->csum unless the skb is
2700 __sum16 __skb_checksum_complete(struct sk_buff *skb)
2705 csum = skb_checksum(skb, 0, skb->len, 0);
2707 sum = csum_fold(csum_add(skb->csum, csum));
2708 /* This check is inverted, because we already knew the hardware
2709 * checksum is invalid before calling this function. So, if the
2710 * re-computed checksum is valid instead, then we have a mismatch
2711 * between the original skb->csum and skb_checksum(). This means either
2712 * the original hardware checksum is incorrect or we screw up skb->csum
2713 * when moving skb->data around.
2716 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2717 !skb->csum_complete_sw)
2718 netdev_rx_csum_fault(skb->dev, skb);
2721 if (!skb_shared(skb)) {
2722 /* Save full packet checksum */
2724 skb->ip_summed = CHECKSUM_COMPLETE;
2725 skb->csum_complete_sw = 1;
2726 skb->csum_valid = !sum;
2731 EXPORT_SYMBOL(__skb_checksum_complete);
2733 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2735 net_warn_ratelimited(
2736 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2741 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2742 int offset, int len)
2744 net_warn_ratelimited(
2745 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2750 static const struct skb_checksum_ops default_crc32c_ops = {
2751 .update = warn_crc32c_csum_update,
2752 .combine = warn_crc32c_csum_combine,
2755 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2756 &default_crc32c_ops;
2757 EXPORT_SYMBOL(crc32c_csum_stub);
2760 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2761 * @from: source buffer
2763 * Calculates the amount of linear headroom needed in the 'to' skb passed
2764 * into skb_zerocopy().
2767 skb_zerocopy_headlen(const struct sk_buff *from)
2769 unsigned int hlen = 0;
2771 if (!from->head_frag ||
2772 skb_headlen(from) < L1_CACHE_BYTES ||
2773 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2774 hlen = skb_headlen(from);
2776 if (skb_has_frag_list(from))
2781 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2784 * skb_zerocopy - Zero copy skb to skb
2785 * @to: destination buffer
2786 * @from: source buffer
2787 * @len: number of bytes to copy from source buffer
2788 * @hlen: size of linear headroom in destination buffer
2790 * Copies up to `len` bytes from `from` to `to` by creating references
2791 * to the frags in the source buffer.
2793 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2794 * headroom in the `to` buffer.
2797 * 0: everything is OK
2798 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2799 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2802 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2805 int plen = 0; /* length of skb->head fragment */
2808 unsigned int offset;
2810 BUG_ON(!from->head_frag && !hlen);
2812 /* dont bother with small payloads */
2813 if (len <= skb_tailroom(to))
2814 return skb_copy_bits(from, 0, skb_put(to, len), len);
2817 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2822 plen = min_t(int, skb_headlen(from), len);
2824 page = virt_to_head_page(from->head);
2825 offset = from->data - (unsigned char *)page_address(page);
2826 __skb_fill_page_desc(to, 0, page, offset, plen);
2833 to->truesize += len + plen;
2834 to->len += len + plen;
2835 to->data_len += len + plen;
2837 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2841 skb_zerocopy_clone(to, from, GFP_ATOMIC);
2843 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2846 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2847 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2848 len -= skb_shinfo(to)->frags[j].size;
2849 skb_frag_ref(to, j);
2852 skb_shinfo(to)->nr_frags = j;
2856 EXPORT_SYMBOL_GPL(skb_zerocopy);
2858 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2863 if (skb->ip_summed == CHECKSUM_PARTIAL)
2864 csstart = skb_checksum_start_offset(skb);
2866 csstart = skb_headlen(skb);
2868 BUG_ON(csstart > skb_headlen(skb));
2870 skb_copy_from_linear_data(skb, to, csstart);
2873 if (csstart != skb->len)
2874 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2875 skb->len - csstart, 0);
2877 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2878 long csstuff = csstart + skb->csum_offset;
2880 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2883 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2886 * skb_dequeue - remove from the head of the queue
2887 * @list: list to dequeue from
2889 * Remove the head of the list. The list lock is taken so the function
2890 * may be used safely with other locking list functions. The head item is
2891 * returned or %NULL if the list is empty.
2894 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2896 unsigned long flags;
2897 struct sk_buff *result;
2899 spin_lock_irqsave(&list->lock, flags);
2900 result = __skb_dequeue(list);
2901 spin_unlock_irqrestore(&list->lock, flags);
2904 EXPORT_SYMBOL(skb_dequeue);
2907 * skb_dequeue_tail - remove from the tail of the queue
2908 * @list: list to dequeue from
2910 * Remove the tail of the list. The list lock is taken so the function
2911 * may be used safely with other locking list functions. The tail item is
2912 * returned or %NULL if the list is empty.
2914 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2916 unsigned long flags;
2917 struct sk_buff *result;
2919 spin_lock_irqsave(&list->lock, flags);
2920 result = __skb_dequeue_tail(list);
2921 spin_unlock_irqrestore(&list->lock, flags);
2924 EXPORT_SYMBOL(skb_dequeue_tail);
2927 * skb_queue_purge - empty a list
2928 * @list: list to empty
2930 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2931 * the list and one reference dropped. This function takes the list
2932 * lock and is atomic with respect to other list locking functions.
2934 void skb_queue_purge(struct sk_buff_head *list)
2936 struct sk_buff *skb;
2937 while ((skb = skb_dequeue(list)) != NULL)
2940 EXPORT_SYMBOL(skb_queue_purge);
2943 * skb_rbtree_purge - empty a skb rbtree
2944 * @root: root of the rbtree to empty
2945 * Return value: the sum of truesizes of all purged skbs.
2947 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2948 * the list and one reference dropped. This function does not take
2949 * any lock. Synchronization should be handled by the caller (e.g., TCP
2950 * out-of-order queue is protected by the socket lock).
2952 unsigned int skb_rbtree_purge(struct rb_root *root)
2954 struct rb_node *p = rb_first(root);
2955 unsigned int sum = 0;
2958 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2961 rb_erase(&skb->rbnode, root);
2962 sum += skb->truesize;
2969 * skb_queue_head - queue a buffer at the list head
2970 * @list: list to use
2971 * @newsk: buffer to queue
2973 * Queue a buffer at the start of the list. This function takes the
2974 * list lock and can be used safely with other locking &sk_buff functions
2977 * A buffer cannot be placed on two lists at the same time.
2979 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2981 unsigned long flags;
2983 spin_lock_irqsave(&list->lock, flags);
2984 __skb_queue_head(list, newsk);
2985 spin_unlock_irqrestore(&list->lock, flags);
2987 EXPORT_SYMBOL(skb_queue_head);
2990 * skb_queue_tail - queue a buffer at the list tail
2991 * @list: list to use
2992 * @newsk: buffer to queue
2994 * Queue a buffer at the tail of the list. This function takes the
2995 * list lock and can be used safely with other locking &sk_buff functions
2998 * A buffer cannot be placed on two lists at the same time.
3000 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3002 unsigned long flags;
3004 spin_lock_irqsave(&list->lock, flags);
3005 __skb_queue_tail(list, newsk);
3006 spin_unlock_irqrestore(&list->lock, flags);
3008 EXPORT_SYMBOL(skb_queue_tail);
3011 * skb_unlink - remove a buffer from a list
3012 * @skb: buffer to remove
3013 * @list: list to use
3015 * Remove a packet from a list. The list locks are taken and this
3016 * function is atomic with respect to other list locked calls
3018 * You must know what list the SKB is on.
3020 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3022 unsigned long flags;
3024 spin_lock_irqsave(&list->lock, flags);
3025 __skb_unlink(skb, list);
3026 spin_unlock_irqrestore(&list->lock, flags);
3028 EXPORT_SYMBOL(skb_unlink);
3031 * skb_append - append a buffer
3032 * @old: buffer to insert after
3033 * @newsk: buffer to insert
3034 * @list: list to use
3036 * Place a packet after a given packet in a list. The list locks are taken
3037 * and this function is atomic with respect to other list locked calls.
3038 * A buffer cannot be placed on two lists at the same time.
3040 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3042 unsigned long flags;
3044 spin_lock_irqsave(&list->lock, flags);
3045 __skb_queue_after(list, old, newsk);
3046 spin_unlock_irqrestore(&list->lock, flags);
3048 EXPORT_SYMBOL(skb_append);
3050 static inline void skb_split_inside_header(struct sk_buff *skb,
3051 struct sk_buff* skb1,
3052 const u32 len, const int pos)
3056 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3058 /* And move data appendix as is. */
3059 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3060 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3062 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3063 skb_shinfo(skb)->nr_frags = 0;
3064 skb1->data_len = skb->data_len;
3065 skb1->len += skb1->data_len;
3068 skb_set_tail_pointer(skb, len);
3071 static inline void skb_split_no_header(struct sk_buff *skb,
3072 struct sk_buff* skb1,
3073 const u32 len, int pos)
3076 const int nfrags = skb_shinfo(skb)->nr_frags;
3078 skb_shinfo(skb)->nr_frags = 0;
3079 skb1->len = skb1->data_len = skb->len - len;
3081 skb->data_len = len - pos;
3083 for (i = 0; i < nfrags; i++) {
3084 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3086 if (pos + size > len) {
3087 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3091 * We have two variants in this case:
3092 * 1. Move all the frag to the second
3093 * part, if it is possible. F.e.
3094 * this approach is mandatory for TUX,
3095 * where splitting is expensive.
3096 * 2. Split is accurately. We make this.
3098 skb_frag_ref(skb, i);
3099 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
3100 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3101 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3102 skb_shinfo(skb)->nr_frags++;
3106 skb_shinfo(skb)->nr_frags++;
3109 skb_shinfo(skb1)->nr_frags = k;
3113 * skb_split - Split fragmented skb to two parts at length len.
3114 * @skb: the buffer to split
3115 * @skb1: the buffer to receive the second part
3116 * @len: new length for skb
3118 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3120 int pos = skb_headlen(skb);
3122 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3124 skb_zerocopy_clone(skb1, skb, 0);
3125 if (len < pos) /* Split line is inside header. */
3126 skb_split_inside_header(skb, skb1, len, pos);
3127 else /* Second chunk has no header, nothing to copy. */
3128 skb_split_no_header(skb, skb1, len, pos);
3130 EXPORT_SYMBOL(skb_split);
3132 /* Shifting from/to a cloned skb is a no-go.
3134 * Caller cannot keep skb_shinfo related pointers past calling here!
3136 static int skb_prepare_for_shift(struct sk_buff *skb)
3138 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3142 * skb_shift - Shifts paged data partially from skb to another
3143 * @tgt: buffer into which tail data gets added
3144 * @skb: buffer from which the paged data comes from
3145 * @shiftlen: shift up to this many bytes
3147 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3148 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3149 * It's up to caller to free skb if everything was shifted.
3151 * If @tgt runs out of frags, the whole operation is aborted.
3153 * Skb cannot include anything else but paged data while tgt is allowed
3154 * to have non-paged data as well.
3156 * TODO: full sized shift could be optimized but that would need
3157 * specialized skb free'er to handle frags without up-to-date nr_frags.
3159 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3161 int from, to, merge, todo;
3162 struct skb_frag_struct *fragfrom, *fragto;
3164 BUG_ON(shiftlen > skb->len);
3166 if (skb_headlen(skb))
3168 if (skb_zcopy(tgt) || skb_zcopy(skb))
3173 to = skb_shinfo(tgt)->nr_frags;
3174 fragfrom = &skb_shinfo(skb)->frags[from];
3176 /* Actual merge is delayed until the point when we know we can
3177 * commit all, so that we don't have to undo partial changes
3180 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3181 fragfrom->page_offset)) {
3186 todo -= skb_frag_size(fragfrom);
3188 if (skb_prepare_for_shift(skb) ||
3189 skb_prepare_for_shift(tgt))
3192 /* All previous frag pointers might be stale! */
3193 fragfrom = &skb_shinfo(skb)->frags[from];
3194 fragto = &skb_shinfo(tgt)->frags[merge];
3196 skb_frag_size_add(fragto, shiftlen);
3197 skb_frag_size_sub(fragfrom, shiftlen);
3198 fragfrom->page_offset += shiftlen;
3206 /* Skip full, not-fitting skb to avoid expensive operations */
3207 if ((shiftlen == skb->len) &&
3208 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3211 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3214 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3215 if (to == MAX_SKB_FRAGS)
3218 fragfrom = &skb_shinfo(skb)->frags[from];
3219 fragto = &skb_shinfo(tgt)->frags[to];
3221 if (todo >= skb_frag_size(fragfrom)) {
3222 *fragto = *fragfrom;
3223 todo -= skb_frag_size(fragfrom);
3228 __skb_frag_ref(fragfrom);
3229 fragto->page = fragfrom->page;
3230 fragto->page_offset = fragfrom->page_offset;
3231 skb_frag_size_set(fragto, todo);
3233 fragfrom->page_offset += todo;
3234 skb_frag_size_sub(fragfrom, todo);
3242 /* Ready to "commit" this state change to tgt */
3243 skb_shinfo(tgt)->nr_frags = to;
3246 fragfrom = &skb_shinfo(skb)->frags[0];
3247 fragto = &skb_shinfo(tgt)->frags[merge];
3249 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3250 __skb_frag_unref(fragfrom);
3253 /* Reposition in the original skb */
3255 while (from < skb_shinfo(skb)->nr_frags)
3256 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3257 skb_shinfo(skb)->nr_frags = to;
3259 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3262 /* Most likely the tgt won't ever need its checksum anymore, skb on
3263 * the other hand might need it if it needs to be resent
3265 tgt->ip_summed = CHECKSUM_PARTIAL;
3266 skb->ip_summed = CHECKSUM_PARTIAL;
3268 /* Yak, is it really working this way? Some helper please? */
3269 skb->len -= shiftlen;
3270 skb->data_len -= shiftlen;
3271 skb->truesize -= shiftlen;
3272 tgt->len += shiftlen;
3273 tgt->data_len += shiftlen;
3274 tgt->truesize += shiftlen;
3280 * skb_prepare_seq_read - Prepare a sequential read of skb data
3281 * @skb: the buffer to read
3282 * @from: lower offset of data to be read
3283 * @to: upper offset of data to be read
3284 * @st: state variable
3286 * Initializes the specified state variable. Must be called before
3287 * invoking skb_seq_read() for the first time.
3289 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3290 unsigned int to, struct skb_seq_state *st)
3292 st->lower_offset = from;
3293 st->upper_offset = to;
3294 st->root_skb = st->cur_skb = skb;
3295 st->frag_idx = st->stepped_offset = 0;
3296 st->frag_data = NULL;
3298 EXPORT_SYMBOL(skb_prepare_seq_read);
3301 * skb_seq_read - Sequentially read skb data
3302 * @consumed: number of bytes consumed by the caller so far
3303 * @data: destination pointer for data to be returned
3304 * @st: state variable
3306 * Reads a block of skb data at @consumed relative to the
3307 * lower offset specified to skb_prepare_seq_read(). Assigns
3308 * the head of the data block to @data and returns the length
3309 * of the block or 0 if the end of the skb data or the upper
3310 * offset has been reached.
3312 * The caller is not required to consume all of the data
3313 * returned, i.e. @consumed is typically set to the number
3314 * of bytes already consumed and the next call to
3315 * skb_seq_read() will return the remaining part of the block.
3317 * Note 1: The size of each block of data returned can be arbitrary,
3318 * this limitation is the cost for zerocopy sequential
3319 * reads of potentially non linear data.
3321 * Note 2: Fragment lists within fragments are not implemented
3322 * at the moment, state->root_skb could be replaced with
3323 * a stack for this purpose.
3325 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3326 struct skb_seq_state *st)
3328 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3331 if (unlikely(abs_offset >= st->upper_offset)) {
3332 if (st->frag_data) {
3333 kunmap_atomic(st->frag_data);
3334 st->frag_data = NULL;
3340 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3342 if (abs_offset < block_limit && !st->frag_data) {
3343 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3344 return block_limit - abs_offset;
3347 if (st->frag_idx == 0 && !st->frag_data)
3348 st->stepped_offset += skb_headlen(st->cur_skb);
3350 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3351 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3352 block_limit = skb_frag_size(frag) + st->stepped_offset;
3354 if (abs_offset < block_limit) {
3356 st->frag_data = kmap_atomic(skb_frag_page(frag));
3358 *data = (u8 *) st->frag_data + frag->page_offset +
3359 (abs_offset - st->stepped_offset);
3361 return block_limit - abs_offset;
3364 if (st->frag_data) {
3365 kunmap_atomic(st->frag_data);
3366 st->frag_data = NULL;
3370 st->stepped_offset += skb_frag_size(frag);
3373 if (st->frag_data) {
3374 kunmap_atomic(st->frag_data);
3375 st->frag_data = NULL;
3378 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3379 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3382 } else if (st->cur_skb->next) {
3383 st->cur_skb = st->cur_skb->next;
3390 EXPORT_SYMBOL(skb_seq_read);
3393 * skb_abort_seq_read - Abort a sequential read of skb data
3394 * @st: state variable
3396 * Must be called if skb_seq_read() was not called until it
3399 void skb_abort_seq_read(struct skb_seq_state *st)
3402 kunmap_atomic(st->frag_data);
3404 EXPORT_SYMBOL(skb_abort_seq_read);
3406 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3408 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3409 struct ts_config *conf,
3410 struct ts_state *state)
3412 return skb_seq_read(offset, text, TS_SKB_CB(state));
3415 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3417 skb_abort_seq_read(TS_SKB_CB(state));
3421 * skb_find_text - Find a text pattern in skb data
3422 * @skb: the buffer to look in
3423 * @from: search offset
3425 * @config: textsearch configuration
3427 * Finds a pattern in the skb data according to the specified
3428 * textsearch configuration. Use textsearch_next() to retrieve
3429 * subsequent occurrences of the pattern. Returns the offset
3430 * to the first occurrence or UINT_MAX if no match was found.
3432 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3433 unsigned int to, struct ts_config *config)
3435 struct ts_state state;
3438 config->get_next_block = skb_ts_get_next_block;
3439 config->finish = skb_ts_finish;
3441 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3443 ret = textsearch_find(config, &state);
3444 return (ret <= to - from ? ret : UINT_MAX);
3446 EXPORT_SYMBOL(skb_find_text);
3448 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3449 int offset, size_t size)
3451 int i = skb_shinfo(skb)->nr_frags;
3453 if (skb_can_coalesce(skb, i, page, offset)) {
3454 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3455 } else if (i < MAX_SKB_FRAGS) {
3457 skb_fill_page_desc(skb, i, page, offset, size);
3464 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3467 * skb_pull_rcsum - pull skb and update receive checksum
3468 * @skb: buffer to update
3469 * @len: length of data pulled
3471 * This function performs an skb_pull on the packet and updates
3472 * the CHECKSUM_COMPLETE checksum. It should be used on
3473 * receive path processing instead of skb_pull unless you know
3474 * that the checksum difference is zero (e.g., a valid IP header)
3475 * or you are setting ip_summed to CHECKSUM_NONE.
3477 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3479 unsigned char *data = skb->data;
3481 BUG_ON(len > skb->len);
3482 __skb_pull(skb, len);
3483 skb_postpull_rcsum(skb, data, len);
3486 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3488 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3490 skb_frag_t head_frag;
3493 page = virt_to_head_page(frag_skb->head);
3494 head_frag.page.p = page;
3495 head_frag.page_offset = frag_skb->data -
3496 (unsigned char *)page_address(page);
3497 head_frag.size = skb_headlen(frag_skb);
3502 * skb_segment - Perform protocol segmentation on skb.
3503 * @head_skb: buffer to segment
3504 * @features: features for the output path (see dev->features)
3506 * This function performs segmentation on the given skb. It returns
3507 * a pointer to the first in a list of new skbs for the segments.
3508 * In case of error it returns ERR_PTR(err).
3510 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3511 netdev_features_t features)
3513 struct sk_buff *segs = NULL;
3514 struct sk_buff *tail = NULL;
3515 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3516 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3517 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3518 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3519 struct sk_buff *frag_skb = head_skb;
3520 unsigned int offset = doffset;
3521 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3522 unsigned int partial_segs = 0;
3523 unsigned int headroom;
3524 unsigned int len = head_skb->len;
3527 int nfrags = skb_shinfo(head_skb)->nr_frags;
3533 __skb_push(head_skb, doffset);
3534 proto = skb_network_protocol(head_skb, &dummy);
3535 if (unlikely(!proto))
3536 return ERR_PTR(-EINVAL);
3538 sg = !!(features & NETIF_F_SG);
3539 csum = !!can_checksum_protocol(features, proto);
3541 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3542 if (!(features & NETIF_F_GSO_PARTIAL)) {
3543 struct sk_buff *iter;
3544 unsigned int frag_len;
3547 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3550 /* If we get here then all the required
3551 * GSO features except frag_list are supported.
3552 * Try to split the SKB to multiple GSO SKBs
3553 * with no frag_list.
3554 * Currently we can do that only when the buffers don't
3555 * have a linear part and all the buffers except
3556 * the last are of the same length.
3558 frag_len = list_skb->len;
3559 skb_walk_frags(head_skb, iter) {
3560 if (frag_len != iter->len && iter->next)
3562 if (skb_headlen(iter) && !iter->head_frag)
3568 if (len != frag_len)
3572 /* GSO partial only requires that we trim off any excess that
3573 * doesn't fit into an MSS sized block, so take care of that
3576 partial_segs = len / mss;
3577 if (partial_segs > 1)
3578 mss *= partial_segs;
3584 headroom = skb_headroom(head_skb);
3585 pos = skb_headlen(head_skb);
3588 struct sk_buff *nskb;
3589 skb_frag_t *nskb_frag;
3593 if (unlikely(mss == GSO_BY_FRAGS)) {
3594 len = list_skb->len;
3596 len = head_skb->len - offset;
3601 hsize = skb_headlen(head_skb) - offset;
3604 if (hsize > len || !sg)
3607 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3608 (skb_headlen(list_skb) == len || sg)) {
3609 BUG_ON(skb_headlen(list_skb) > len);
3612 nfrags = skb_shinfo(list_skb)->nr_frags;
3613 frag = skb_shinfo(list_skb)->frags;
3614 frag_skb = list_skb;
3615 pos += skb_headlen(list_skb);
3617 while (pos < offset + len) {
3618 BUG_ON(i >= nfrags);
3620 size = skb_frag_size(frag);
3621 if (pos + size > offset + len)
3629 nskb = skb_clone(list_skb, GFP_ATOMIC);
3630 list_skb = list_skb->next;
3632 if (unlikely(!nskb))
3635 if (unlikely(pskb_trim(nskb, len))) {
3640 hsize = skb_end_offset(nskb);
3641 if (skb_cow_head(nskb, doffset + headroom)) {
3646 nskb->truesize += skb_end_offset(nskb) - hsize;
3647 skb_release_head_state(nskb);
3648 __skb_push(nskb, doffset);
3650 nskb = __alloc_skb(hsize + doffset + headroom,
3651 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3654 if (unlikely(!nskb))
3657 skb_reserve(nskb, headroom);
3658 __skb_put(nskb, doffset);
3667 __copy_skb_header(nskb, head_skb);
3669 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3670 skb_reset_mac_len(nskb);
3672 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3673 nskb->data - tnl_hlen,
3674 doffset + tnl_hlen);
3676 if (nskb->len == len + doffset)
3677 goto perform_csum_check;
3680 if (!nskb->remcsum_offload)
3681 nskb->ip_summed = CHECKSUM_NONE;
3682 SKB_GSO_CB(nskb)->csum =
3683 skb_copy_and_csum_bits(head_skb, offset,
3686 SKB_GSO_CB(nskb)->csum_start =
3687 skb_headroom(nskb) + doffset;
3691 nskb_frag = skb_shinfo(nskb)->frags;
3693 skb_copy_from_linear_data_offset(head_skb, offset,
3694 skb_put(nskb, hsize), hsize);
3696 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3699 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3700 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3703 while (pos < offset + len) {
3706 nfrags = skb_shinfo(list_skb)->nr_frags;
3707 frag = skb_shinfo(list_skb)->frags;
3708 frag_skb = list_skb;
3709 if (!skb_headlen(list_skb)) {
3712 BUG_ON(!list_skb->head_frag);
3714 /* to make room for head_frag. */
3718 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3719 skb_zerocopy_clone(nskb, frag_skb,
3723 list_skb = list_skb->next;
3726 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3728 net_warn_ratelimited(
3729 "skb_segment: too many frags: %u %u\n",
3735 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
3736 __skb_frag_ref(nskb_frag);
3737 size = skb_frag_size(nskb_frag);
3740 nskb_frag->page_offset += offset - pos;
3741 skb_frag_size_sub(nskb_frag, offset - pos);
3744 skb_shinfo(nskb)->nr_frags++;
3746 if (pos + size <= offset + len) {
3751 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3759 nskb->data_len = len - hsize;
3760 nskb->len += nskb->data_len;
3761 nskb->truesize += nskb->data_len;
3765 if (skb_has_shared_frag(nskb) &&
3766 __skb_linearize(nskb))
3769 if (!nskb->remcsum_offload)
3770 nskb->ip_summed = CHECKSUM_NONE;
3771 SKB_GSO_CB(nskb)->csum =
3772 skb_checksum(nskb, doffset,
3773 nskb->len - doffset, 0);
3774 SKB_GSO_CB(nskb)->csum_start =
3775 skb_headroom(nskb) + doffset;
3777 } while ((offset += len) < head_skb->len);
3779 /* Some callers want to get the end of the list.
3780 * Put it in segs->prev to avoid walking the list.
3781 * (see validate_xmit_skb_list() for example)
3786 struct sk_buff *iter;
3787 int type = skb_shinfo(head_skb)->gso_type;
3788 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3790 /* Update type to add partial and then remove dodgy if set */
3791 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3792 type &= ~SKB_GSO_DODGY;
3794 /* Update GSO info and prepare to start updating headers on
3795 * our way back down the stack of protocols.
3797 for (iter = segs; iter; iter = iter->next) {
3798 skb_shinfo(iter)->gso_size = gso_size;
3799 skb_shinfo(iter)->gso_segs = partial_segs;
3800 skb_shinfo(iter)->gso_type = type;
3801 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3804 if (tail->len - doffset <= gso_size)
3805 skb_shinfo(tail)->gso_size = 0;
3806 else if (tail != segs)
3807 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3810 /* Following permits correct backpressure, for protocols
3811 * using skb_set_owner_w().
3812 * Idea is to tranfert ownership from head_skb to last segment.
3814 if (head_skb->destructor == sock_wfree) {
3815 swap(tail->truesize, head_skb->truesize);
3816 swap(tail->destructor, head_skb->destructor);
3817 swap(tail->sk, head_skb->sk);
3822 kfree_skb_list(segs);
3823 return ERR_PTR(err);
3825 EXPORT_SYMBOL_GPL(skb_segment);
3827 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
3829 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3830 unsigned int offset = skb_gro_offset(skb);
3831 unsigned int headlen = skb_headlen(skb);
3832 unsigned int len = skb_gro_len(skb);
3833 unsigned int delta_truesize;
3836 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
3839 lp = NAPI_GRO_CB(p)->last;
3840 pinfo = skb_shinfo(lp);
3842 if (headlen <= offset) {
3845 int i = skbinfo->nr_frags;
3846 int nr_frags = pinfo->nr_frags + i;
3848 if (nr_frags > MAX_SKB_FRAGS)
3852 pinfo->nr_frags = nr_frags;
3853 skbinfo->nr_frags = 0;
3855 frag = pinfo->frags + nr_frags;
3856 frag2 = skbinfo->frags + i;
3861 frag->page_offset += offset;
3862 skb_frag_size_sub(frag, offset);
3864 /* all fragments truesize : remove (head size + sk_buff) */
3865 delta_truesize = skb->truesize -
3866 SKB_TRUESIZE(skb_end_offset(skb));
3868 skb->truesize -= skb->data_len;
3869 skb->len -= skb->data_len;
3872 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3874 } else if (skb->head_frag) {
3875 int nr_frags = pinfo->nr_frags;
3876 skb_frag_t *frag = pinfo->frags + nr_frags;
3877 struct page *page = virt_to_head_page(skb->head);
3878 unsigned int first_size = headlen - offset;
3879 unsigned int first_offset;
3881 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3884 first_offset = skb->data -
3885 (unsigned char *)page_address(page) +
3888 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3890 frag->page.p = page;
3891 frag->page_offset = first_offset;
3892 skb_frag_size_set(frag, first_size);
3894 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3895 /* We dont need to clear skbinfo->nr_frags here */
3897 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3898 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3903 delta_truesize = skb->truesize;
3904 if (offset > headlen) {
3905 unsigned int eat = offset - headlen;
3907 skbinfo->frags[0].page_offset += eat;
3908 skb_frag_size_sub(&skbinfo->frags[0], eat);
3909 skb->data_len -= eat;
3914 __skb_pull(skb, offset);
3916 if (NAPI_GRO_CB(p)->last == p)
3917 skb_shinfo(p)->frag_list = skb;
3919 NAPI_GRO_CB(p)->last->next = skb;
3920 NAPI_GRO_CB(p)->last = skb;
3921 __skb_header_release(skb);
3925 NAPI_GRO_CB(p)->count++;
3927 p->truesize += delta_truesize;
3930 lp->data_len += len;
3931 lp->truesize += delta_truesize;
3934 NAPI_GRO_CB(skb)->same_flow = 1;
3937 EXPORT_SYMBOL_GPL(skb_gro_receive);
3939 #ifdef CONFIG_SKB_EXTENSIONS
3940 #define SKB_EXT_ALIGN_VALUE 8
3941 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3943 static const u8 skb_ext_type_len[] = {
3944 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3945 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3948 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3952 static __always_inline unsigned int skb_ext_total_length(void)
3954 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3955 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3956 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3959 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3964 static void skb_extensions_init(void)
3966 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3967 BUILD_BUG_ON(skb_ext_total_length() > 255);
3969 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3970 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3972 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3976 static void skb_extensions_init(void) {}
3979 void __init skb_init(void)
3981 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
3982 sizeof(struct sk_buff),
3984 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3985 offsetof(struct sk_buff, cb),
3986 sizeof_field(struct sk_buff, cb),
3988 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3989 sizeof(struct sk_buff_fclones),
3991 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3993 skb_extensions_init();
3997 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3998 unsigned int recursion_level)
4000 int start = skb_headlen(skb);
4001 int i, copy = start - offset;
4002 struct sk_buff *frag_iter;
4005 if (unlikely(recursion_level >= 24))
4011 sg_set_buf(sg, skb->data + offset, copy);
4013 if ((len -= copy) == 0)
4018 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4021 WARN_ON(start > offset + len);
4023 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4024 if ((copy = end - offset) > 0) {
4025 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4026 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4031 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4032 frag->page_offset+offset-start);
4041 skb_walk_frags(skb, frag_iter) {
4044 WARN_ON(start > offset + len);
4046 end = start + frag_iter->len;
4047 if ((copy = end - offset) > 0) {
4048 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4053 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4054 copy, recursion_level + 1);
4055 if (unlikely(ret < 0))
4058 if ((len -= copy) == 0)
4069 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4070 * @skb: Socket buffer containing the buffers to be mapped
4071 * @sg: The scatter-gather list to map into
4072 * @offset: The offset into the buffer's contents to start mapping
4073 * @len: Length of buffer space to be mapped
4075 * Fill the specified scatter-gather list with mappings/pointers into a
4076 * region of the buffer space attached to a socket buffer. Returns either
4077 * the number of scatterlist items used, or -EMSGSIZE if the contents
4080 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4082 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4087 sg_mark_end(&sg[nsg - 1]);
4091 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4093 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4094 * sglist without mark the sg which contain last skb data as the end.
4095 * So the caller can mannipulate sg list as will when padding new data after
4096 * the first call without calling sg_unmark_end to expend sg list.
4098 * Scenario to use skb_to_sgvec_nomark:
4100 * 2. skb_to_sgvec_nomark(payload1)
4101 * 3. skb_to_sgvec_nomark(payload2)
4103 * This is equivalent to:
4105 * 2. skb_to_sgvec(payload1)
4107 * 4. skb_to_sgvec(payload2)
4109 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4110 * is more preferable.
4112 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4113 int offset, int len)
4115 return __skb_to_sgvec(skb, sg, offset, len, 0);
4117 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4122 * skb_cow_data - Check that a socket buffer's data buffers are writable
4123 * @skb: The socket buffer to check.
4124 * @tailbits: Amount of trailing space to be added
4125 * @trailer: Returned pointer to the skb where the @tailbits space begins
4127 * Make sure that the data buffers attached to a socket buffer are
4128 * writable. If they are not, private copies are made of the data buffers
4129 * and the socket buffer is set to use these instead.
4131 * If @tailbits is given, make sure that there is space to write @tailbits
4132 * bytes of data beyond current end of socket buffer. @trailer will be
4133 * set to point to the skb in which this space begins.
4135 * The number of scatterlist elements required to completely map the
4136 * COW'd and extended socket buffer will be returned.
4138 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4142 struct sk_buff *skb1, **skb_p;
4144 /* If skb is cloned or its head is paged, reallocate
4145 * head pulling out all the pages (pages are considered not writable
4146 * at the moment even if they are anonymous).
4148 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4149 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4152 /* Easy case. Most of packets will go this way. */
4153 if (!skb_has_frag_list(skb)) {
4154 /* A little of trouble, not enough of space for trailer.
4155 * This should not happen, when stack is tuned to generate
4156 * good frames. OK, on miss we reallocate and reserve even more
4157 * space, 128 bytes is fair. */
4159 if (skb_tailroom(skb) < tailbits &&
4160 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4168 /* Misery. We are in troubles, going to mincer fragments... */
4171 skb_p = &skb_shinfo(skb)->frag_list;
4174 while ((skb1 = *skb_p) != NULL) {
4177 /* The fragment is partially pulled by someone,
4178 * this can happen on input. Copy it and everything
4181 if (skb_shared(skb1))
4184 /* If the skb is the last, worry about trailer. */
4186 if (skb1->next == NULL && tailbits) {
4187 if (skb_shinfo(skb1)->nr_frags ||
4188 skb_has_frag_list(skb1) ||
4189 skb_tailroom(skb1) < tailbits)
4190 ntail = tailbits + 128;
4196 skb_shinfo(skb1)->nr_frags ||
4197 skb_has_frag_list(skb1)) {
4198 struct sk_buff *skb2;
4200 /* Fuck, we are miserable poor guys... */
4202 skb2 = skb_copy(skb1, GFP_ATOMIC);
4204 skb2 = skb_copy_expand(skb1,
4208 if (unlikely(skb2 == NULL))
4212 skb_set_owner_w(skb2, skb1->sk);
4214 /* Looking around. Are we still alive?
4215 * OK, link new skb, drop old one */
4217 skb2->next = skb1->next;
4224 skb_p = &skb1->next;
4229 EXPORT_SYMBOL_GPL(skb_cow_data);
4231 static void sock_rmem_free(struct sk_buff *skb)
4233 struct sock *sk = skb->sk;
4235 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4238 static void skb_set_err_queue(struct sk_buff *skb)
4240 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4241 * So, it is safe to (mis)use it to mark skbs on the error queue.
4243 skb->pkt_type = PACKET_OUTGOING;
4244 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4248 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4250 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4252 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4253 (unsigned int)sk->sk_rcvbuf)
4258 skb->destructor = sock_rmem_free;
4259 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4260 skb_set_err_queue(skb);
4262 /* before exiting rcu section, make sure dst is refcounted */
4265 skb_queue_tail(&sk->sk_error_queue, skb);
4266 if (!sock_flag(sk, SOCK_DEAD))
4267 sk->sk_error_report(sk);
4270 EXPORT_SYMBOL(sock_queue_err_skb);
4272 static bool is_icmp_err_skb(const struct sk_buff *skb)
4274 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4275 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4278 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4280 struct sk_buff_head *q = &sk->sk_error_queue;
4281 struct sk_buff *skb, *skb_next = NULL;
4282 bool icmp_next = false;
4283 unsigned long flags;
4285 spin_lock_irqsave(&q->lock, flags);
4286 skb = __skb_dequeue(q);
4287 if (skb && (skb_next = skb_peek(q))) {
4288 icmp_next = is_icmp_err_skb(skb_next);
4290 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4292 spin_unlock_irqrestore(&q->lock, flags);
4294 if (is_icmp_err_skb(skb) && !icmp_next)
4298 sk->sk_error_report(sk);
4302 EXPORT_SYMBOL(sock_dequeue_err_skb);
4305 * skb_clone_sk - create clone of skb, and take reference to socket
4306 * @skb: the skb to clone
4308 * This function creates a clone of a buffer that holds a reference on
4309 * sk_refcnt. Buffers created via this function are meant to be
4310 * returned using sock_queue_err_skb, or free via kfree_skb.
4312 * When passing buffers allocated with this function to sock_queue_err_skb
4313 * it is necessary to wrap the call with sock_hold/sock_put in order to
4314 * prevent the socket from being released prior to being enqueued on
4315 * the sk_error_queue.
4317 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4319 struct sock *sk = skb->sk;
4320 struct sk_buff *clone;
4322 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4325 clone = skb_clone(skb, GFP_ATOMIC);
4332 clone->destructor = sock_efree;
4336 EXPORT_SYMBOL(skb_clone_sk);
4338 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4343 struct sock_exterr_skb *serr;
4346 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4348 serr = SKB_EXT_ERR(skb);
4349 memset(serr, 0, sizeof(*serr));
4350 serr->ee.ee_errno = ENOMSG;
4351 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4352 serr->ee.ee_info = tstype;
4353 serr->opt_stats = opt_stats;
4354 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4355 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4356 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4357 if (sk->sk_protocol == IPPROTO_TCP &&
4358 sk->sk_type == SOCK_STREAM)
4359 serr->ee.ee_data -= sk->sk_tskey;
4362 err = sock_queue_err_skb(sk, skb);
4368 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4372 if (likely(sysctl_tstamp_allow_data || tsonly))
4375 read_lock_bh(&sk->sk_callback_lock);
4376 ret = sk->sk_socket && sk->sk_socket->file &&
4377 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4378 read_unlock_bh(&sk->sk_callback_lock);
4382 void skb_complete_tx_timestamp(struct sk_buff *skb,
4383 struct skb_shared_hwtstamps *hwtstamps)
4385 struct sock *sk = skb->sk;
4387 if (!skb_may_tx_timestamp(sk, false))
4390 /* Take a reference to prevent skb_orphan() from freeing the socket,
4391 * but only if the socket refcount is not zero.
4393 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4394 *skb_hwtstamps(skb) = *hwtstamps;
4395 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4403 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4405 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4406 struct skb_shared_hwtstamps *hwtstamps,
4407 struct sock *sk, int tstype)
4409 struct sk_buff *skb;
4410 bool tsonly, opt_stats = false;
4415 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4416 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4419 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4420 if (!skb_may_tx_timestamp(sk, tsonly))
4425 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4426 sk->sk_protocol == IPPROTO_TCP &&
4427 sk->sk_type == SOCK_STREAM) {
4428 skb = tcp_get_timestamping_opt_stats(sk);
4432 skb = alloc_skb(0, GFP_ATOMIC);
4434 skb = skb_clone(orig_skb, GFP_ATOMIC);
4440 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4442 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4446 *skb_hwtstamps(skb) = *hwtstamps;
4448 skb->tstamp = ktime_get_real();
4450 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4452 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4454 void skb_tstamp_tx(struct sk_buff *orig_skb,
4455 struct skb_shared_hwtstamps *hwtstamps)
4457 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4460 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4462 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4464 struct sock *sk = skb->sk;
4465 struct sock_exterr_skb *serr;
4468 skb->wifi_acked_valid = 1;
4469 skb->wifi_acked = acked;
4471 serr = SKB_EXT_ERR(skb);
4472 memset(serr, 0, sizeof(*serr));
4473 serr->ee.ee_errno = ENOMSG;
4474 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4476 /* Take a reference to prevent skb_orphan() from freeing the socket,
4477 * but only if the socket refcount is not zero.
4479 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4480 err = sock_queue_err_skb(sk, skb);
4486 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4489 * skb_partial_csum_set - set up and verify partial csum values for packet
4490 * @skb: the skb to set
4491 * @start: the number of bytes after skb->data to start checksumming.
4492 * @off: the offset from start to place the checksum.
4494 * For untrusted partially-checksummed packets, we need to make sure the values
4495 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4497 * This function checks and sets those values and skb->ip_summed: if this
4498 * returns false you should drop the packet.
4500 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4502 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4503 u32 csum_start = skb_headroom(skb) + (u32)start;
4505 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4506 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4507 start, off, skb_headroom(skb), skb_headlen(skb));
4510 skb->ip_summed = CHECKSUM_PARTIAL;
4511 skb->csum_start = csum_start;
4512 skb->csum_offset = off;
4513 skb_set_transport_header(skb, start);
4516 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4518 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4521 if (skb_headlen(skb) >= len)
4524 /* If we need to pullup then pullup to the max, so we
4525 * won't need to do it again.
4530 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4533 if (skb_headlen(skb) < len)
4539 #define MAX_TCP_HDR_LEN (15 * 4)
4541 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4542 typeof(IPPROTO_IP) proto,
4549 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4550 off + MAX_TCP_HDR_LEN);
4551 if (!err && !skb_partial_csum_set(skb, off,
4552 offsetof(struct tcphdr,
4555 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4558 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4559 off + sizeof(struct udphdr));
4560 if (!err && !skb_partial_csum_set(skb, off,
4561 offsetof(struct udphdr,
4564 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4567 return ERR_PTR(-EPROTO);
4570 /* This value should be large enough to cover a tagged ethernet header plus
4571 * maximally sized IP and TCP or UDP headers.
4573 #define MAX_IP_HDR_LEN 128
4575 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4584 err = skb_maybe_pull_tail(skb,
4585 sizeof(struct iphdr),
4590 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4593 off = ip_hdrlen(skb);
4600 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4602 return PTR_ERR(csum);
4605 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4608 ip_hdr(skb)->protocol, 0);
4615 /* This value should be large enough to cover a tagged ethernet header plus
4616 * an IPv6 header, all options, and a maximal TCP or UDP header.
4618 #define MAX_IPV6_HDR_LEN 256
4620 #define OPT_HDR(type, skb, off) \
4621 (type *)(skb_network_header(skb) + (off))
4623 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4636 off = sizeof(struct ipv6hdr);
4638 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4642 nexthdr = ipv6_hdr(skb)->nexthdr;
4644 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4645 while (off <= len && !done) {
4647 case IPPROTO_DSTOPTS:
4648 case IPPROTO_HOPOPTS:
4649 case IPPROTO_ROUTING: {
4650 struct ipv6_opt_hdr *hp;
4652 err = skb_maybe_pull_tail(skb,
4654 sizeof(struct ipv6_opt_hdr),
4659 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4660 nexthdr = hp->nexthdr;
4661 off += ipv6_optlen(hp);
4665 struct ip_auth_hdr *hp;
4667 err = skb_maybe_pull_tail(skb,
4669 sizeof(struct ip_auth_hdr),
4674 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4675 nexthdr = hp->nexthdr;
4676 off += ipv6_authlen(hp);
4679 case IPPROTO_FRAGMENT: {
4680 struct frag_hdr *hp;
4682 err = skb_maybe_pull_tail(skb,
4684 sizeof(struct frag_hdr),
4689 hp = OPT_HDR(struct frag_hdr, skb, off);
4691 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4694 nexthdr = hp->nexthdr;
4695 off += sizeof(struct frag_hdr);
4706 if (!done || fragment)
4709 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4711 return PTR_ERR(csum);
4714 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4715 &ipv6_hdr(skb)->daddr,
4716 skb->len - off, nexthdr, 0);
4724 * skb_checksum_setup - set up partial checksum offset
4725 * @skb: the skb to set up
4726 * @recalculate: if true the pseudo-header checksum will be recalculated
4728 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4732 switch (skb->protocol) {
4733 case htons(ETH_P_IP):
4734 err = skb_checksum_setup_ipv4(skb, recalculate);
4737 case htons(ETH_P_IPV6):
4738 err = skb_checksum_setup_ipv6(skb, recalculate);
4748 EXPORT_SYMBOL(skb_checksum_setup);
4751 * skb_checksum_maybe_trim - maybe trims the given skb
4752 * @skb: the skb to check
4753 * @transport_len: the data length beyond the network header
4755 * Checks whether the given skb has data beyond the given transport length.
4756 * If so, returns a cloned skb trimmed to this transport length.
4757 * Otherwise returns the provided skb. Returns NULL in error cases
4758 * (e.g. transport_len exceeds skb length or out-of-memory).
4760 * Caller needs to set the skb transport header and free any returned skb if it
4761 * differs from the provided skb.
4763 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4764 unsigned int transport_len)
4766 struct sk_buff *skb_chk;
4767 unsigned int len = skb_transport_offset(skb) + transport_len;
4772 else if (skb->len == len)
4775 skb_chk = skb_clone(skb, GFP_ATOMIC);
4779 ret = pskb_trim_rcsum(skb_chk, len);
4789 * skb_checksum_trimmed - validate checksum of an skb
4790 * @skb: the skb to check
4791 * @transport_len: the data length beyond the network header
4792 * @skb_chkf: checksum function to use
4794 * Applies the given checksum function skb_chkf to the provided skb.
4795 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4797 * If the skb has data beyond the given transport length, then a
4798 * trimmed & cloned skb is checked and returned.
4800 * Caller needs to set the skb transport header and free any returned skb if it
4801 * differs from the provided skb.
4803 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4804 unsigned int transport_len,
4805 __sum16(*skb_chkf)(struct sk_buff *skb))
4807 struct sk_buff *skb_chk;
4808 unsigned int offset = skb_transport_offset(skb);
4811 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4815 if (!pskb_may_pull(skb_chk, offset))
4818 skb_pull_rcsum(skb_chk, offset);
4819 ret = skb_chkf(skb_chk);
4820 skb_push_rcsum(skb_chk, offset);
4828 if (skb_chk && skb_chk != skb)
4834 EXPORT_SYMBOL(skb_checksum_trimmed);
4836 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4838 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4841 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4843 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4846 skb_release_head_state(skb);
4847 kmem_cache_free(skbuff_head_cache, skb);
4852 EXPORT_SYMBOL(kfree_skb_partial);
4855 * skb_try_coalesce - try to merge skb to prior one
4857 * @from: buffer to add
4858 * @fragstolen: pointer to boolean
4859 * @delta_truesize: how much more was allocated than was requested
4861 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4862 bool *fragstolen, int *delta_truesize)
4864 struct skb_shared_info *to_shinfo, *from_shinfo;
4865 int i, delta, len = from->len;
4867 *fragstolen = false;
4872 if (len <= skb_tailroom(to)) {
4874 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4875 *delta_truesize = 0;
4879 to_shinfo = skb_shinfo(to);
4880 from_shinfo = skb_shinfo(from);
4881 if (to_shinfo->frag_list || from_shinfo->frag_list)
4883 if (skb_zcopy(to) || skb_zcopy(from))
4886 if (skb_headlen(from) != 0) {
4888 unsigned int offset;
4890 if (to_shinfo->nr_frags +
4891 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
4894 if (skb_head_is_locked(from))
4897 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4899 page = virt_to_head_page(from->head);
4900 offset = from->data - (unsigned char *)page_address(page);
4902 skb_fill_page_desc(to, to_shinfo->nr_frags,
4903 page, offset, skb_headlen(from));
4906 if (to_shinfo->nr_frags +
4907 from_shinfo->nr_frags > MAX_SKB_FRAGS)
4910 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4913 WARN_ON_ONCE(delta < len);
4915 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4917 from_shinfo->nr_frags * sizeof(skb_frag_t));
4918 to_shinfo->nr_frags += from_shinfo->nr_frags;
4920 if (!skb_cloned(from))
4921 from_shinfo->nr_frags = 0;
4923 /* if the skb is not cloned this does nothing
4924 * since we set nr_frags to 0.
4926 for (i = 0; i < from_shinfo->nr_frags; i++)
4927 __skb_frag_ref(&from_shinfo->frags[i]);
4929 to->truesize += delta;
4931 to->data_len += len;
4933 *delta_truesize = delta;
4936 EXPORT_SYMBOL(skb_try_coalesce);
4939 * skb_scrub_packet - scrub an skb
4941 * @skb: buffer to clean
4942 * @xnet: packet is crossing netns
4944 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4945 * into/from a tunnel. Some information have to be cleared during these
4947 * skb_scrub_packet can also be used to clean a skb before injecting it in
4948 * another namespace (@xnet == true). We have to clear all information in the
4949 * skb that could impact namespace isolation.
4951 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4953 skb->pkt_type = PACKET_HOST;
4959 nf_reset_trace(skb);
4961 #ifdef CONFIG_NET_SWITCHDEV
4962 skb->offload_fwd_mark = 0;
4963 skb->offload_l3_fwd_mark = 0;
4973 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4976 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4980 * skb_gso_transport_seglen is used to determine the real size of the
4981 * individual segments, including Layer4 headers (TCP/UDP).
4983 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4985 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4987 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4988 unsigned int thlen = 0;
4990 if (skb->encapsulation) {
4991 thlen = skb_inner_transport_header(skb) -
4992 skb_transport_header(skb);
4994 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4995 thlen += inner_tcp_hdrlen(skb);
4996 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4997 thlen = tcp_hdrlen(skb);
4998 } else if (unlikely(skb_is_gso_sctp(skb))) {
4999 thlen = sizeof(struct sctphdr);
5000 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5001 thlen = sizeof(struct udphdr);
5003 /* UFO sets gso_size to the size of the fragmentation
5004 * payload, i.e. the size of the L4 (UDP) header is already
5007 return thlen + shinfo->gso_size;
5011 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5015 * skb_gso_network_seglen is used to determine the real size of the
5016 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5018 * The MAC/L2 header is not accounted for.
5020 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5022 unsigned int hdr_len = skb_transport_header(skb) -
5023 skb_network_header(skb);
5025 return hdr_len + skb_gso_transport_seglen(skb);
5029 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5033 * skb_gso_mac_seglen is used to determine the real size of the
5034 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5035 * headers (TCP/UDP).
5037 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5039 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5041 return hdr_len + skb_gso_transport_seglen(skb);
5045 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5047 * There are a couple of instances where we have a GSO skb, and we
5048 * want to determine what size it would be after it is segmented.
5050 * We might want to check:
5051 * - L3+L4+payload size (e.g. IP forwarding)
5052 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5054 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5058 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5059 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5061 * @max_len: The maximum permissible length.
5063 * Returns true if the segmented length <= max length.
5065 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5066 unsigned int seg_len,
5067 unsigned int max_len) {
5068 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5069 const struct sk_buff *iter;
5071 if (shinfo->gso_size != GSO_BY_FRAGS)
5072 return seg_len <= max_len;
5074 /* Undo this so we can re-use header sizes */
5075 seg_len -= GSO_BY_FRAGS;
5077 skb_walk_frags(skb, iter) {
5078 if (seg_len + skb_headlen(iter) > max_len)
5086 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5089 * @mtu: MTU to validate against
5091 * skb_gso_validate_network_len validates if a given skb will fit a
5092 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5095 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5097 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5099 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5102 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5105 * @len: length to validate against
5107 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5108 * length once split, including L2, L3 and L4 headers and the payload.
5110 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5112 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5114 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5116 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5118 int mac_len, meta_len;
5121 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5126 mac_len = skb->data - skb_mac_header(skb);
5127 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5128 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5129 mac_len - VLAN_HLEN - ETH_TLEN);
5132 meta_len = skb_metadata_len(skb);
5134 meta = skb_metadata_end(skb) - meta_len;
5135 memmove(meta + VLAN_HLEN, meta, meta_len);
5138 skb->mac_header += VLAN_HLEN;
5142 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5144 struct vlan_hdr *vhdr;
5147 if (unlikely(skb_vlan_tag_present(skb))) {
5148 /* vlan_tci is already set-up so leave this for another time */
5152 skb = skb_share_check(skb, GFP_ATOMIC);
5156 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5159 vhdr = (struct vlan_hdr *)skb->data;
5160 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5161 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5163 skb_pull_rcsum(skb, VLAN_HLEN);
5164 vlan_set_encap_proto(skb, vhdr);
5166 skb = skb_reorder_vlan_header(skb);
5170 skb_reset_network_header(skb);
5171 skb_reset_transport_header(skb);
5172 skb_reset_mac_len(skb);
5180 EXPORT_SYMBOL(skb_vlan_untag);
5182 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5184 if (!pskb_may_pull(skb, write_len))
5187 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5190 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5192 EXPORT_SYMBOL(skb_ensure_writable);
5194 /* remove VLAN header from packet and update csum accordingly.
5195 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5197 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5199 struct vlan_hdr *vhdr;
5200 int offset = skb->data - skb_mac_header(skb);
5203 if (WARN_ONCE(offset,
5204 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5209 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5213 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5215 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5216 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5218 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5219 __skb_pull(skb, VLAN_HLEN);
5221 vlan_set_encap_proto(skb, vhdr);
5222 skb->mac_header += VLAN_HLEN;
5224 if (skb_network_offset(skb) < ETH_HLEN)
5225 skb_set_network_header(skb, ETH_HLEN);
5227 skb_reset_mac_len(skb);
5231 EXPORT_SYMBOL(__skb_vlan_pop);
5233 /* Pop a vlan tag either from hwaccel or from payload.
5234 * Expects skb->data at mac header.
5236 int skb_vlan_pop(struct sk_buff *skb)
5242 if (likely(skb_vlan_tag_present(skb))) {
5243 __vlan_hwaccel_clear_tag(skb);
5245 if (unlikely(!eth_type_vlan(skb->protocol)))
5248 err = __skb_vlan_pop(skb, &vlan_tci);
5252 /* move next vlan tag to hw accel tag */
5253 if (likely(!eth_type_vlan(skb->protocol)))
5256 vlan_proto = skb->protocol;
5257 err = __skb_vlan_pop(skb, &vlan_tci);
5261 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5264 EXPORT_SYMBOL(skb_vlan_pop);
5266 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5267 * Expects skb->data at mac header.
5269 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5271 if (skb_vlan_tag_present(skb)) {
5272 int offset = skb->data - skb_mac_header(skb);
5275 if (WARN_ONCE(offset,
5276 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5281 err = __vlan_insert_tag(skb, skb->vlan_proto,
5282 skb_vlan_tag_get(skb));
5286 skb->protocol = skb->vlan_proto;
5287 skb->mac_len += VLAN_HLEN;
5289 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5291 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5294 EXPORT_SYMBOL(skb_vlan_push);
5297 * alloc_skb_with_frags - allocate skb with page frags
5299 * @header_len: size of linear part
5300 * @data_len: needed length in frags
5301 * @max_page_order: max page order desired.
5302 * @errcode: pointer to error code if any
5303 * @gfp_mask: allocation mask
5305 * This can be used to allocate a paged skb, given a maximal order for frags.
5307 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5308 unsigned long data_len,
5313 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5314 unsigned long chunk;
5315 struct sk_buff *skb;
5319 *errcode = -EMSGSIZE;
5320 /* Note this test could be relaxed, if we succeed to allocate
5321 * high order pages...
5323 if (npages > MAX_SKB_FRAGS)
5326 *errcode = -ENOBUFS;
5327 skb = alloc_skb(header_len, gfp_mask);
5331 skb->truesize += npages << PAGE_SHIFT;
5333 for (i = 0; npages > 0; i++) {
5334 int order = max_page_order;
5337 if (npages >= 1 << order) {
5338 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
5344 /* Do not retry other high order allocations */
5350 page = alloc_page(gfp_mask);
5354 chunk = min_t(unsigned long, data_len,
5355 PAGE_SIZE << order);
5356 skb_fill_page_desc(skb, i, page, 0, chunk);
5358 npages -= 1 << order;
5366 EXPORT_SYMBOL(alloc_skb_with_frags);
5368 /* carve out the first off bytes from skb when off < headlen */
5369 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5370 const int headlen, gfp_t gfp_mask)
5373 int size = skb_end_offset(skb);
5374 int new_hlen = headlen - off;
5377 size = SKB_DATA_ALIGN(size);
5379 if (skb_pfmemalloc(skb))
5380 gfp_mask |= __GFP_MEMALLOC;
5381 data = kmalloc_reserve(size +
5382 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5383 gfp_mask, NUMA_NO_NODE, NULL);
5387 size = SKB_WITH_OVERHEAD(ksize(data));
5389 /* Copy real data, and all frags */
5390 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5393 memcpy((struct skb_shared_info *)(data + size),
5395 offsetof(struct skb_shared_info,
5396 frags[skb_shinfo(skb)->nr_frags]));
5397 if (skb_cloned(skb)) {
5398 /* drop the old head gracefully */
5399 if (skb_orphan_frags(skb, gfp_mask)) {
5403 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5404 skb_frag_ref(skb, i);
5405 if (skb_has_frag_list(skb))
5406 skb_clone_fraglist(skb);
5407 skb_release_data(skb);
5409 /* we can reuse existing recount- all we did was
5418 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5421 skb->end = skb->head + size;
5423 skb_set_tail_pointer(skb, skb_headlen(skb));
5424 skb_headers_offset_update(skb, 0);
5428 atomic_set(&skb_shinfo(skb)->dataref, 1);
5433 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5435 /* carve out the first eat bytes from skb's frag_list. May recurse into
5438 static int pskb_carve_frag_list(struct sk_buff *skb,
5439 struct skb_shared_info *shinfo, int eat,
5442 struct sk_buff *list = shinfo->frag_list;
5443 struct sk_buff *clone = NULL;
5444 struct sk_buff *insp = NULL;
5448 pr_err("Not enough bytes to eat. Want %d\n", eat);
5451 if (list->len <= eat) {
5452 /* Eaten as whole. */
5457 /* Eaten partially. */
5458 if (skb_shared(list)) {
5459 clone = skb_clone(list, gfp_mask);
5465 /* This may be pulled without problems. */
5468 if (pskb_carve(list, eat, gfp_mask) < 0) {
5476 /* Free pulled out fragments. */
5477 while ((list = shinfo->frag_list) != insp) {
5478 shinfo->frag_list = list->next;
5481 /* And insert new clone at head. */
5484 shinfo->frag_list = clone;
5489 /* carve off first len bytes from skb. Split line (off) is in the
5490 * non-linear part of skb
5492 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5493 int pos, gfp_t gfp_mask)
5496 int size = skb_end_offset(skb);
5498 const int nfrags = skb_shinfo(skb)->nr_frags;
5499 struct skb_shared_info *shinfo;
5501 size = SKB_DATA_ALIGN(size);
5503 if (skb_pfmemalloc(skb))
5504 gfp_mask |= __GFP_MEMALLOC;
5505 data = kmalloc_reserve(size +
5506 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5507 gfp_mask, NUMA_NO_NODE, NULL);
5511 size = SKB_WITH_OVERHEAD(ksize(data));
5513 memcpy((struct skb_shared_info *)(data + size),
5514 skb_shinfo(skb), offsetof(struct skb_shared_info,
5515 frags[skb_shinfo(skb)->nr_frags]));
5516 if (skb_orphan_frags(skb, gfp_mask)) {
5520 shinfo = (struct skb_shared_info *)(data + size);
5521 for (i = 0; i < nfrags; i++) {
5522 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5524 if (pos + fsize > off) {
5525 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5529 * We have two variants in this case:
5530 * 1. Move all the frag to the second
5531 * part, if it is possible. F.e.
5532 * this approach is mandatory for TUX,
5533 * where splitting is expensive.
5534 * 2. Split is accurately. We make this.
5536 shinfo->frags[0].page_offset += off - pos;
5537 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5539 skb_frag_ref(skb, i);
5544 shinfo->nr_frags = k;
5545 if (skb_has_frag_list(skb))
5546 skb_clone_fraglist(skb);
5549 /* split line is in frag list */
5550 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5552 skb_release_data(skb);
5557 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5560 skb->end = skb->head + size;
5562 skb_reset_tail_pointer(skb);
5563 skb_headers_offset_update(skb, 0);
5568 skb->data_len = skb->len;
5569 atomic_set(&skb_shinfo(skb)->dataref, 1);
5573 /* remove len bytes from the beginning of the skb */
5574 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5576 int headlen = skb_headlen(skb);
5579 return pskb_carve_inside_header(skb, len, headlen, gfp);
5581 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5584 /* Extract to_copy bytes starting at off from skb, and return this in
5587 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5588 int to_copy, gfp_t gfp)
5590 struct sk_buff *clone = skb_clone(skb, gfp);
5595 if (pskb_carve(clone, off, gfp) < 0 ||
5596 pskb_trim(clone, to_copy)) {
5602 EXPORT_SYMBOL(pskb_extract);
5605 * skb_condense - try to get rid of fragments/frag_list if possible
5608 * Can be used to save memory before skb is added to a busy queue.
5609 * If packet has bytes in frags and enough tail room in skb->head,
5610 * pull all of them, so that we can free the frags right now and adjust
5613 * We do not reallocate skb->head thus can not fail.
5614 * Caller must re-evaluate skb->truesize if needed.
5616 void skb_condense(struct sk_buff *skb)
5618 if (skb->data_len) {
5619 if (skb->data_len > skb->end - skb->tail ||
5623 /* Nice, we can free page frag(s) right now */
5624 __pskb_pull_tail(skb, skb->data_len);
5626 /* At this point, skb->truesize might be over estimated,
5627 * because skb had a fragment, and fragments do not tell
5629 * When we pulled its content into skb->head, fragment
5630 * was freed, but __pskb_pull_tail() could not possibly
5631 * adjust skb->truesize, not knowing the frag truesize.
5633 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5636 #ifdef CONFIG_SKB_EXTENSIONS
5637 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5639 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5642 static struct skb_ext *skb_ext_alloc(void)
5644 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5647 memset(new->offset, 0, sizeof(new->offset));
5648 refcount_set(&new->refcnt, 1);
5654 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5655 unsigned int old_active)
5657 struct skb_ext *new;
5659 if (refcount_read(&old->refcnt) == 1)
5662 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5666 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5667 refcount_set(&new->refcnt, 1);
5670 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5671 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5674 for (i = 0; i < sp->len; i++)
5675 xfrm_state_hold(sp->xvec[i]);
5683 * skb_ext_add - allocate space for given extension, COW if needed
5685 * @id: extension to allocate space for
5687 * Allocates enough space for the given extension.
5688 * If the extension is already present, a pointer to that extension
5691 * If the skb was cloned, COW applies and the returned memory can be
5692 * modified without changing the extension space of clones buffers.
5694 * Returns pointer to the extension or NULL on allocation failure.
5696 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5698 struct skb_ext *new, *old = NULL;
5699 unsigned int newlen, newoff;
5701 if (skb->active_extensions) {
5702 old = skb->extensions;
5704 new = skb_ext_maybe_cow(old, skb->active_extensions);
5708 if (__skb_ext_exist(new, id))
5711 newoff = new->chunks;
5713 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5715 new = skb_ext_alloc();
5720 newlen = newoff + skb_ext_type_len[id];
5721 new->chunks = newlen;
5722 new->offset[id] = newoff;
5724 skb->extensions = new;
5725 skb->active_extensions |= 1 << id;
5726 return skb_ext_get_ptr(new, id);
5728 EXPORT_SYMBOL(skb_ext_add);
5731 static void skb_ext_put_sp(struct sec_path *sp)
5735 for (i = 0; i < sp->len; i++)
5736 xfrm_state_put(sp->xvec[i]);
5740 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5742 struct skb_ext *ext = skb->extensions;
5744 skb->active_extensions &= ~(1 << id);
5745 if (skb->active_extensions == 0) {
5746 skb->extensions = NULL;
5749 } else if (id == SKB_EXT_SEC_PATH &&
5750 refcount_read(&ext->refcnt) == 1) {
5751 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5758 EXPORT_SYMBOL(__skb_ext_del);
5760 void __skb_ext_put(struct skb_ext *ext)
5762 /* If this is last clone, nothing can increment
5763 * it after check passes. Avoids one atomic op.
5765 if (refcount_read(&ext->refcnt) == 1)
5768 if (!refcount_dec_and_test(&ext->refcnt))
5772 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5773 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5776 kmem_cache_free(skbuff_ext_cache, ext);
5778 EXPORT_SYMBOL(__skb_ext_put);
5779 #endif /* CONFIG_SKB_EXTENSIONS */