2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83 EXPORT_SYMBOL(sysctl_max_skb_frags);
86 * skb_panic - private function for out-of-line support
90 * @msg: skb_over_panic or skb_under_panic
92 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 msg, addr, skb->len, sz, skb->head, skb->data,
102 (unsigned long)skb->tail, (unsigned long)skb->end,
103 skb->dev ? skb->dev->name : "<NULL>");
107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109 skb_panic(skb, sz, addr, __func__);
112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114 skb_panic(skb, sz, addr, __func__);
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 unsigned long ip, bool *pfmemalloc)
131 bool ret_pfmemalloc = false;
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
137 obj = kmalloc_node_track_caller(size,
138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 if (obj || !(gfp_pfmemalloc_allowed(flags)))
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc = true;
145 obj = kmalloc_node_track_caller(size, flags, node);
149 *pfmemalloc = ret_pfmemalloc;
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
160 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
165 skb = kmem_cache_alloc_node(skbuff_head_cache,
166 gfp_mask & ~__GFP_DMA, node);
171 * Only clear those fields we need to clear, not those that we will
172 * actually initialise below. Hence, don't put any more fields after
173 * the tail pointer in struct sk_buff!
175 memset(skb, 0, offsetof(struct sk_buff, tail));
177 skb->truesize = sizeof(struct sk_buff);
178 atomic_set(&skb->users, 1);
180 skb->mac_header = (typeof(skb->mac_header))~0U;
186 * __alloc_skb - allocate a network buffer
187 * @size: size to allocate
188 * @gfp_mask: allocation mask
189 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
190 * instead of head cache and allocate a cloned (child) skb.
191 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
192 * allocations in case the data is required for writeback
193 * @node: numa node to allocate memory on
195 * Allocate a new &sk_buff. The returned buffer has no headroom and a
196 * tail room of at least size bytes. The object has a reference count
197 * of one. The return is the buffer. On a failure the return is %NULL.
199 * Buffers may only be allocated from interrupts using a @gfp_mask of
202 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
205 struct kmem_cache *cache;
206 struct skb_shared_info *shinfo;
211 cache = (flags & SKB_ALLOC_FCLONE)
212 ? skbuff_fclone_cache : skbuff_head_cache;
214 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
215 gfp_mask |= __GFP_MEMALLOC;
218 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
223 /* We do our best to align skb_shared_info on a separate cache
224 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
225 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
226 * Both skb->head and skb_shared_info are cache line aligned.
228 size = SKB_DATA_ALIGN(size);
229 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
230 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
233 /* kmalloc(size) might give us more room than requested.
234 * Put skb_shared_info exactly at the end of allocated zone,
235 * to allow max possible filling before reallocation.
237 size = SKB_WITH_OVERHEAD(ksize(data));
238 prefetchw(data + size);
241 * Only clear those fields we need to clear, not those that we will
242 * actually initialise below. Hence, don't put any more fields after
243 * the tail pointer in struct sk_buff!
245 memset(skb, 0, offsetof(struct sk_buff, tail));
246 /* Account for allocated memory : skb + skb->head */
247 skb->truesize = SKB_TRUESIZE(size);
248 skb->pfmemalloc = pfmemalloc;
249 atomic_set(&skb->users, 1);
252 skb_reset_tail_pointer(skb);
253 skb->end = skb->tail + size;
254 skb->mac_header = (typeof(skb->mac_header))~0U;
255 skb->transport_header = (typeof(skb->transport_header))~0U;
257 /* make sure we initialize shinfo sequentially */
258 shinfo = skb_shinfo(skb);
259 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
260 atomic_set(&shinfo->dataref, 1);
261 kmemcheck_annotate_variable(shinfo->destructor_arg);
263 if (flags & SKB_ALLOC_FCLONE) {
264 struct sk_buff_fclones *fclones;
266 fclones = container_of(skb, struct sk_buff_fclones, skb1);
268 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
269 skb->fclone = SKB_FCLONE_ORIG;
270 atomic_set(&fclones->fclone_ref, 1);
272 fclones->skb2.fclone = SKB_FCLONE_CLONE;
273 fclones->skb2.pfmemalloc = pfmemalloc;
278 kmem_cache_free(cache, skb);
282 EXPORT_SYMBOL(__alloc_skb);
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)
305 struct skb_shared_info *shinfo;
307 unsigned int size = frag_size ? : ksize(data);
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
334 /* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
339 struct sk_buff *build_skb(void *data, unsigned int frag_size)
341 struct sk_buff *skb = __build_skb(data, frag_size);
343 if (skb && frag_size) {
345 if (page_is_pfmemalloc(virt_to_head_page(data)))
350 EXPORT_SYMBOL(build_skb);
352 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
353 static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
355 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
357 struct page_frag_cache *nc;
361 local_irq_save(flags);
362 nc = this_cpu_ptr(&netdev_alloc_cache);
363 data = __alloc_page_frag(nc, fragsz, gfp_mask);
364 local_irq_restore(flags);
369 * netdev_alloc_frag - allocate a page fragment
370 * @fragsz: fragment size
372 * Allocates a frag from a page for receive buffer.
373 * Uses GFP_ATOMIC allocations.
375 void *netdev_alloc_frag(unsigned int fragsz)
377 fragsz = SKB_DATA_ALIGN(fragsz);
379 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
381 EXPORT_SYMBOL(netdev_alloc_frag);
383 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
385 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
387 return __alloc_page_frag(nc, fragsz, gfp_mask);
390 void *napi_alloc_frag(unsigned int fragsz)
392 fragsz = SKB_DATA_ALIGN(fragsz);
394 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
396 EXPORT_SYMBOL(napi_alloc_frag);
399 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
400 * @dev: network device to receive on
401 * @len: length to allocate
402 * @gfp_mask: get_free_pages mask, passed to alloc_skb
404 * Allocate a new &sk_buff and assign it a usage count of one. The
405 * buffer has NET_SKB_PAD headroom built in. Users should allocate
406 * the headroom they think they need without accounting for the
407 * built in space. The built in space is used for optimisations.
409 * %NULL is returned if there is no free memory.
411 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
414 struct page_frag_cache *nc;
422 /* If requested length is either too small or too big,
423 * we use kmalloc() for skb->head allocation.
425 if (len <= SKB_WITH_OVERHEAD(1024) ||
426 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
427 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
428 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
434 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
435 len = SKB_DATA_ALIGN(len);
437 if (sk_memalloc_socks())
438 gfp_mask |= __GFP_MEMALLOC;
440 local_irq_save(flags);
442 nc = this_cpu_ptr(&netdev_alloc_cache);
443 data = __alloc_page_frag(nc, len, gfp_mask);
444 pfmemalloc = nc->pfmemalloc;
446 local_irq_restore(flags);
451 skb = __build_skb(data, len);
452 if (unlikely(!skb)) {
457 /* use OR instead of assignment to avoid clearing of bits in mask */
463 skb_reserve(skb, NET_SKB_PAD);
469 EXPORT_SYMBOL(__netdev_alloc_skb);
472 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
473 * @napi: napi instance this buffer was allocated for
474 * @len: length to allocate
475 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
477 * Allocate a new sk_buff for use in NAPI receive. This buffer will
478 * attempt to allocate the head from a special reserved region used
479 * only for NAPI Rx allocation. By doing this we can save several
480 * CPU cycles by avoiding having to disable and re-enable IRQs.
482 * %NULL is returned if there is no free memory.
484 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
487 struct page_frag_cache *nc;
491 len += NET_SKB_PAD + NET_IP_ALIGN;
493 /* If requested length is either too small or too big,
494 * we use kmalloc() for skb->head allocation.
496 if (len <= SKB_WITH_OVERHEAD(1024) ||
497 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
498 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
499 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
505 nc = this_cpu_ptr(&napi_alloc_cache);
506 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
507 len = SKB_DATA_ALIGN(len);
509 if (sk_memalloc_socks())
510 gfp_mask |= __GFP_MEMALLOC;
512 data = __alloc_page_frag(nc, len, gfp_mask);
516 skb = __build_skb(data, len);
517 if (unlikely(!skb)) {
522 /* use OR instead of assignment to avoid clearing of bits in mask */
528 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
529 skb->dev = napi->dev;
534 EXPORT_SYMBOL(__napi_alloc_skb);
536 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
537 int size, unsigned int truesize)
539 skb_fill_page_desc(skb, i, page, off, size);
541 skb->data_len += size;
542 skb->truesize += truesize;
544 EXPORT_SYMBOL(skb_add_rx_frag);
546 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
547 unsigned int truesize)
549 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
551 skb_frag_size_add(frag, size);
553 skb->data_len += size;
554 skb->truesize += truesize;
556 EXPORT_SYMBOL(skb_coalesce_rx_frag);
558 static void skb_drop_list(struct sk_buff **listp)
560 kfree_skb_list(*listp);
564 static inline void skb_drop_fraglist(struct sk_buff *skb)
566 skb_drop_list(&skb_shinfo(skb)->frag_list);
569 static void skb_clone_fraglist(struct sk_buff *skb)
571 struct sk_buff *list;
573 skb_walk_frags(skb, list)
577 static void skb_free_head(struct sk_buff *skb)
579 unsigned char *head = skb->head;
587 static void skb_release_data(struct sk_buff *skb)
589 struct skb_shared_info *shinfo = skb_shinfo(skb);
593 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
597 for (i = 0; i < shinfo->nr_frags; i++)
598 __skb_frag_unref(&shinfo->frags[i]);
601 * If skb buf is from userspace, we need to notify the caller
602 * the lower device DMA has done;
604 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
605 struct ubuf_info *uarg;
607 uarg = shinfo->destructor_arg;
609 uarg->callback(uarg, true);
612 if (shinfo->frag_list)
613 kfree_skb_list(shinfo->frag_list);
619 * Free an skbuff by memory without cleaning the state.
621 static void kfree_skbmem(struct sk_buff *skb)
623 struct sk_buff_fclones *fclones;
625 switch (skb->fclone) {
626 case SKB_FCLONE_UNAVAILABLE:
627 kmem_cache_free(skbuff_head_cache, skb);
630 case SKB_FCLONE_ORIG:
631 fclones = container_of(skb, struct sk_buff_fclones, skb1);
633 /* We usually free the clone (TX completion) before original skb
634 * This test would have no chance to be true for the clone,
635 * while here, branch prediction will be good.
637 if (atomic_read(&fclones->fclone_ref) == 1)
641 default: /* SKB_FCLONE_CLONE */
642 fclones = container_of(skb, struct sk_buff_fclones, skb2);
645 if (!atomic_dec_and_test(&fclones->fclone_ref))
648 kmem_cache_free(skbuff_fclone_cache, fclones);
651 static void skb_release_head_state(struct sk_buff *skb)
655 secpath_put(skb->sp);
657 if (skb->destructor) {
659 skb->destructor(skb);
661 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
662 nf_conntrack_put(skb->nfct);
664 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
665 nf_bridge_put(skb->nf_bridge);
669 /* Free everything but the sk_buff shell. */
670 static void skb_release_all(struct sk_buff *skb)
672 skb_release_head_state(skb);
673 if (likely(skb->head))
674 skb_release_data(skb);
678 * __kfree_skb - private function
681 * Free an sk_buff. Release anything attached to the buffer.
682 * Clean the state. This is an internal helper function. Users should
683 * always call kfree_skb
686 void __kfree_skb(struct sk_buff *skb)
688 skb_release_all(skb);
691 EXPORT_SYMBOL(__kfree_skb);
694 * kfree_skb - free an sk_buff
695 * @skb: buffer to free
697 * Drop a reference to the buffer and free it if the usage count has
700 void kfree_skb(struct sk_buff *skb)
704 if (likely(atomic_read(&skb->users) == 1))
706 else if (likely(!atomic_dec_and_test(&skb->users)))
708 trace_kfree_skb(skb, __builtin_return_address(0));
711 EXPORT_SYMBOL(kfree_skb);
713 void kfree_skb_list(struct sk_buff *segs)
716 struct sk_buff *next = segs->next;
722 EXPORT_SYMBOL(kfree_skb_list);
725 * skb_tx_error - report an sk_buff xmit error
726 * @skb: buffer that triggered an error
728 * Report xmit error if a device callback is tracking this skb.
729 * skb must be freed afterwards.
731 void skb_tx_error(struct sk_buff *skb)
733 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
734 struct ubuf_info *uarg;
736 uarg = skb_shinfo(skb)->destructor_arg;
738 uarg->callback(uarg, false);
739 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
742 EXPORT_SYMBOL(skb_tx_error);
745 * consume_skb - free an skbuff
746 * @skb: buffer to free
748 * Drop a ref to the buffer and free it if the usage count has hit zero
749 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
750 * is being dropped after a failure and notes that
752 void consume_skb(struct sk_buff *skb)
756 if (likely(atomic_read(&skb->users) == 1))
758 else if (likely(!atomic_dec_and_test(&skb->users)))
760 trace_consume_skb(skb);
763 EXPORT_SYMBOL(consume_skb);
765 /* Make sure a field is enclosed inside headers_start/headers_end section */
766 #define CHECK_SKB_FIELD(field) \
767 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
768 offsetof(struct sk_buff, headers_start)); \
769 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
770 offsetof(struct sk_buff, headers_end)); \
772 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
774 new->tstamp = old->tstamp;
775 /* We do not copy old->sk */
777 memcpy(new->cb, old->cb, sizeof(old->cb));
778 skb_dst_copy(new, old);
780 new->sp = secpath_get(old->sp);
782 __nf_copy(new, old, false);
784 /* Note : this field could be in headers_start/headers_end section
785 * It is not yet because we do not want to have a 16 bit hole
787 new->queue_mapping = old->queue_mapping;
789 memcpy(&new->headers_start, &old->headers_start,
790 offsetof(struct sk_buff, headers_end) -
791 offsetof(struct sk_buff, headers_start));
792 CHECK_SKB_FIELD(protocol);
793 CHECK_SKB_FIELD(csum);
794 CHECK_SKB_FIELD(hash);
795 CHECK_SKB_FIELD(priority);
796 CHECK_SKB_FIELD(skb_iif);
797 CHECK_SKB_FIELD(vlan_proto);
798 CHECK_SKB_FIELD(vlan_tci);
799 CHECK_SKB_FIELD(transport_header);
800 CHECK_SKB_FIELD(network_header);
801 CHECK_SKB_FIELD(mac_header);
802 CHECK_SKB_FIELD(inner_protocol);
803 CHECK_SKB_FIELD(inner_transport_header);
804 CHECK_SKB_FIELD(inner_network_header);
805 CHECK_SKB_FIELD(inner_mac_header);
806 CHECK_SKB_FIELD(mark);
807 #ifdef CONFIG_NETWORK_SECMARK
808 CHECK_SKB_FIELD(secmark);
810 #ifdef CONFIG_NET_RX_BUSY_POLL
811 CHECK_SKB_FIELD(napi_id);
814 CHECK_SKB_FIELD(sender_cpu);
816 #ifdef CONFIG_NET_SCHED
817 CHECK_SKB_FIELD(tc_index);
818 #ifdef CONFIG_NET_CLS_ACT
819 CHECK_SKB_FIELD(tc_verd);
826 * You should not add any new code to this function. Add it to
827 * __copy_skb_header above instead.
829 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
831 #define C(x) n->x = skb->x
833 n->next = n->prev = NULL;
835 __copy_skb_header(n, skb);
840 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
845 n->destructor = NULL;
852 atomic_set(&n->users, 1);
854 atomic_inc(&(skb_shinfo(skb)->dataref));
862 * skb_morph - morph one skb into another
863 * @dst: the skb to receive the contents
864 * @src: the skb to supply the contents
866 * This is identical to skb_clone except that the target skb is
867 * supplied by the user.
869 * The target skb is returned upon exit.
871 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
873 skb_release_all(dst);
874 return __skb_clone(dst, src);
876 EXPORT_SYMBOL_GPL(skb_morph);
879 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
880 * @skb: the skb to modify
881 * @gfp_mask: allocation priority
883 * This must be called on SKBTX_DEV_ZEROCOPY skb.
884 * It will copy all frags into kernel and drop the reference
885 * to userspace pages.
887 * If this function is called from an interrupt gfp_mask() must be
890 * Returns 0 on success or a negative error code on failure
891 * to allocate kernel memory to copy to.
893 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
896 int num_frags = skb_shinfo(skb)->nr_frags;
897 struct page *page, *head = NULL;
898 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
900 for (i = 0; i < num_frags; i++) {
902 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
904 page = alloc_page(gfp_mask);
907 struct page *next = (struct page *)page_private(head);
913 vaddr = kmap_atomic(skb_frag_page(f));
914 memcpy(page_address(page),
915 vaddr + f->page_offset, skb_frag_size(f));
916 kunmap_atomic(vaddr);
917 set_page_private(page, (unsigned long)head);
921 /* skb frags release userspace buffers */
922 for (i = 0; i < num_frags; i++)
923 skb_frag_unref(skb, i);
925 uarg->callback(uarg, false);
927 /* skb frags point to kernel buffers */
928 for (i = num_frags - 1; i >= 0; i--) {
929 __skb_fill_page_desc(skb, i, head, 0,
930 skb_shinfo(skb)->frags[i].size);
931 head = (struct page *)page_private(head);
934 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
937 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
940 * skb_clone - duplicate an sk_buff
941 * @skb: buffer to clone
942 * @gfp_mask: allocation priority
944 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
945 * copies share the same packet data but not structure. The new
946 * buffer has a reference count of 1. If the allocation fails the
947 * function returns %NULL otherwise the new buffer is returned.
949 * If this function is called from an interrupt gfp_mask() must be
953 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
955 struct sk_buff_fclones *fclones = container_of(skb,
956 struct sk_buff_fclones,
960 if (skb_orphan_frags(skb, gfp_mask))
963 if (skb->fclone == SKB_FCLONE_ORIG &&
964 atomic_read(&fclones->fclone_ref) == 1) {
966 atomic_set(&fclones->fclone_ref, 2);
968 if (skb_pfmemalloc(skb))
969 gfp_mask |= __GFP_MEMALLOC;
971 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
975 kmemcheck_annotate_bitfield(n, flags1);
976 n->fclone = SKB_FCLONE_UNAVAILABLE;
979 return __skb_clone(n, skb);
981 EXPORT_SYMBOL(skb_clone);
983 static void skb_headers_offset_update(struct sk_buff *skb, int off)
985 /* Only adjust this if it actually is csum_start rather than csum */
986 if (skb->ip_summed == CHECKSUM_PARTIAL)
987 skb->csum_start += off;
988 /* {transport,network,mac}_header and tail are relative to skb->head */
989 skb->transport_header += off;
990 skb->network_header += off;
991 if (skb_mac_header_was_set(skb))
992 skb->mac_header += off;
993 skb->inner_transport_header += off;
994 skb->inner_network_header += off;
995 skb->inner_mac_header += off;
998 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1000 __copy_skb_header(new, old);
1002 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1003 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1004 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1007 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1009 if (skb_pfmemalloc(skb))
1010 return SKB_ALLOC_RX;
1015 * skb_copy - create private copy of an sk_buff
1016 * @skb: buffer to copy
1017 * @gfp_mask: allocation priority
1019 * Make a copy of both an &sk_buff and its data. This is used when the
1020 * caller wishes to modify the data and needs a private copy of the
1021 * data to alter. Returns %NULL on failure or the pointer to the buffer
1022 * on success. The returned buffer has a reference count of 1.
1024 * As by-product this function converts non-linear &sk_buff to linear
1025 * one, so that &sk_buff becomes completely private and caller is allowed
1026 * to modify all the data of returned buffer. This means that this
1027 * function is not recommended for use in circumstances when only
1028 * header is going to be modified. Use pskb_copy() instead.
1031 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1033 int headerlen = skb_headroom(skb);
1034 unsigned int size = skb_end_offset(skb) + skb->data_len;
1035 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1036 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1041 /* Set the data pointer */
1042 skb_reserve(n, headerlen);
1043 /* Set the tail pointer and length */
1044 skb_put(n, skb->len);
1046 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1049 copy_skb_header(n, skb);
1052 EXPORT_SYMBOL(skb_copy);
1055 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1056 * @skb: buffer to copy
1057 * @headroom: headroom of new skb
1058 * @gfp_mask: allocation priority
1059 * @fclone: if true allocate the copy of the skb from the fclone
1060 * cache instead of the head cache; it is recommended to set this
1061 * to true for the cases where the copy will likely be cloned
1063 * Make a copy of both an &sk_buff and part of its data, located
1064 * in header. Fragmented data remain shared. This is used when
1065 * the caller wishes to modify only header of &sk_buff and needs
1066 * private copy of the header to alter. Returns %NULL on failure
1067 * or the pointer to the buffer on success.
1068 * The returned buffer has a reference count of 1.
1071 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1072 gfp_t gfp_mask, bool fclone)
1074 unsigned int size = skb_headlen(skb) + headroom;
1075 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1076 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1081 /* Set the data pointer */
1082 skb_reserve(n, headroom);
1083 /* Set the tail pointer and length */
1084 skb_put(n, skb_headlen(skb));
1085 /* Copy the bytes */
1086 skb_copy_from_linear_data(skb, n->data, n->len);
1088 n->truesize += skb->data_len;
1089 n->data_len = skb->data_len;
1092 if (skb_shinfo(skb)->nr_frags) {
1095 if (skb_orphan_frags(skb, gfp_mask)) {
1100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1101 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1102 skb_frag_ref(skb, i);
1104 skb_shinfo(n)->nr_frags = i;
1107 if (skb_has_frag_list(skb)) {
1108 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1109 skb_clone_fraglist(n);
1112 copy_skb_header(n, skb);
1116 EXPORT_SYMBOL(__pskb_copy_fclone);
1119 * pskb_expand_head - reallocate header of &sk_buff
1120 * @skb: buffer to reallocate
1121 * @nhead: room to add at head
1122 * @ntail: room to add at tail
1123 * @gfp_mask: allocation priority
1125 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1126 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1127 * reference count of 1. Returns zero in the case of success or error,
1128 * if expansion failed. In the last case, &sk_buff is not changed.
1130 * All the pointers pointing into skb header may change and must be
1131 * reloaded after call to this function.
1134 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1139 int size = nhead + skb_end_offset(skb) + ntail;
1144 if (skb_shared(skb))
1147 size = SKB_DATA_ALIGN(size);
1149 if (skb_pfmemalloc(skb))
1150 gfp_mask |= __GFP_MEMALLOC;
1151 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1152 gfp_mask, NUMA_NO_NODE, NULL);
1155 size = SKB_WITH_OVERHEAD(ksize(data));
1157 /* Copy only real data... and, alas, header. This should be
1158 * optimized for the cases when header is void.
1160 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1162 memcpy((struct skb_shared_info *)(data + size),
1164 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1167 * if shinfo is shared we must drop the old head gracefully, but if it
1168 * is not we can just drop the old head and let the existing refcount
1169 * be since all we did is relocate the values
1171 if (skb_cloned(skb)) {
1172 /* copy this zero copy skb frags */
1173 if (skb_orphan_frags(skb, gfp_mask))
1175 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1176 skb_frag_ref(skb, i);
1178 if (skb_has_frag_list(skb))
1179 skb_clone_fraglist(skb);
1181 skb_release_data(skb);
1185 off = (data + nhead) - skb->head;
1190 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1194 skb->end = skb->head + size;
1197 skb_headers_offset_update(skb, nhead);
1201 atomic_set(&skb_shinfo(skb)->dataref, 1);
1209 EXPORT_SYMBOL(pskb_expand_head);
1211 /* Make private copy of skb with writable head and some headroom */
1213 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1215 struct sk_buff *skb2;
1216 int delta = headroom - skb_headroom(skb);
1219 skb2 = pskb_copy(skb, GFP_ATOMIC);
1221 skb2 = skb_clone(skb, GFP_ATOMIC);
1222 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1230 EXPORT_SYMBOL(skb_realloc_headroom);
1233 * skb_copy_expand - copy and expand sk_buff
1234 * @skb: buffer to copy
1235 * @newheadroom: new free bytes at head
1236 * @newtailroom: new free bytes at tail
1237 * @gfp_mask: allocation priority
1239 * Make a copy of both an &sk_buff and its data and while doing so
1240 * allocate additional space.
1242 * This is used when the caller wishes to modify the data and needs a
1243 * private copy of the data to alter as well as more space for new fields.
1244 * Returns %NULL on failure or the pointer to the buffer
1245 * on success. The returned buffer has a reference count of 1.
1247 * You must pass %GFP_ATOMIC as the allocation priority if this function
1248 * is called from an interrupt.
1250 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1251 int newheadroom, int newtailroom,
1255 * Allocate the copy buffer
1257 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1258 gfp_mask, skb_alloc_rx_flag(skb),
1260 int oldheadroom = skb_headroom(skb);
1261 int head_copy_len, head_copy_off;
1266 skb_reserve(n, newheadroom);
1268 /* Set the tail pointer and length */
1269 skb_put(n, skb->len);
1271 head_copy_len = oldheadroom;
1273 if (newheadroom <= head_copy_len)
1274 head_copy_len = newheadroom;
1276 head_copy_off = newheadroom - head_copy_len;
1278 /* Copy the linear header and data. */
1279 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1280 skb->len + head_copy_len))
1283 copy_skb_header(n, skb);
1285 skb_headers_offset_update(n, newheadroom - oldheadroom);
1289 EXPORT_SYMBOL(skb_copy_expand);
1292 * skb_pad - zero pad the tail of an skb
1293 * @skb: buffer to pad
1294 * @pad: space to pad
1296 * Ensure that a buffer is followed by a padding area that is zero
1297 * filled. Used by network drivers which may DMA or transfer data
1298 * beyond the buffer end onto the wire.
1300 * May return error in out of memory cases. The skb is freed on error.
1303 int skb_pad(struct sk_buff *skb, int pad)
1308 /* If the skbuff is non linear tailroom is always zero.. */
1309 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1310 memset(skb->data+skb->len, 0, pad);
1314 ntail = skb->data_len + pad - (skb->end - skb->tail);
1315 if (likely(skb_cloned(skb) || ntail > 0)) {
1316 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1321 /* FIXME: The use of this function with non-linear skb's really needs
1324 err = skb_linearize(skb);
1328 memset(skb->data + skb->len, 0, pad);
1335 EXPORT_SYMBOL(skb_pad);
1338 * pskb_put - add data to the tail of a potentially fragmented buffer
1339 * @skb: start of the buffer to use
1340 * @tail: tail fragment of the buffer to use
1341 * @len: amount of data to add
1343 * This function extends the used data area of the potentially
1344 * fragmented buffer. @tail must be the last fragment of @skb -- or
1345 * @skb itself. If this would exceed the total buffer size the kernel
1346 * will panic. A pointer to the first byte of the extra data is
1350 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1353 skb->data_len += len;
1356 return skb_put(tail, len);
1358 EXPORT_SYMBOL_GPL(pskb_put);
1361 * skb_put - add data to a buffer
1362 * @skb: buffer to use
1363 * @len: amount of data to add
1365 * This function extends the used data area of the buffer. If this would
1366 * exceed the total buffer size the kernel will panic. A pointer to the
1367 * first byte of the extra data is returned.
1369 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1371 unsigned char *tmp = skb_tail_pointer(skb);
1372 SKB_LINEAR_ASSERT(skb);
1375 if (unlikely(skb->tail > skb->end))
1376 skb_over_panic(skb, len, __builtin_return_address(0));
1379 EXPORT_SYMBOL(skb_put);
1382 * skb_push - add data to the start of a buffer
1383 * @skb: buffer to use
1384 * @len: amount of data to add
1386 * This function extends the used data area of the buffer at the buffer
1387 * start. If this would exceed the total buffer headroom the kernel will
1388 * panic. A pointer to the first byte of the extra data is returned.
1390 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1394 if (unlikely(skb->data<skb->head))
1395 skb_under_panic(skb, len, __builtin_return_address(0));
1398 EXPORT_SYMBOL(skb_push);
1401 * skb_pull - remove data from the start of a buffer
1402 * @skb: buffer to use
1403 * @len: amount of data to remove
1405 * This function removes data from the start of a buffer, returning
1406 * the memory to the headroom. A pointer to the next data in the buffer
1407 * is returned. Once the data has been pulled future pushes will overwrite
1410 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1412 return skb_pull_inline(skb, len);
1414 EXPORT_SYMBOL(skb_pull);
1417 * skb_trim - remove end from a buffer
1418 * @skb: buffer to alter
1421 * Cut the length of a buffer down by removing data from the tail. If
1422 * the buffer is already under the length specified it is not modified.
1423 * The skb must be linear.
1425 void skb_trim(struct sk_buff *skb, unsigned int len)
1428 __skb_trim(skb, len);
1430 EXPORT_SYMBOL(skb_trim);
1432 /* Trims skb to length len. It can change skb pointers.
1435 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1437 struct sk_buff **fragp;
1438 struct sk_buff *frag;
1439 int offset = skb_headlen(skb);
1440 int nfrags = skb_shinfo(skb)->nr_frags;
1444 if (skb_cloned(skb) &&
1445 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1452 for (; i < nfrags; i++) {
1453 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1460 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1463 skb_shinfo(skb)->nr_frags = i;
1465 for (; i < nfrags; i++)
1466 skb_frag_unref(skb, i);
1468 if (skb_has_frag_list(skb))
1469 skb_drop_fraglist(skb);
1473 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1474 fragp = &frag->next) {
1475 int end = offset + frag->len;
1477 if (skb_shared(frag)) {
1478 struct sk_buff *nfrag;
1480 nfrag = skb_clone(frag, GFP_ATOMIC);
1481 if (unlikely(!nfrag))
1484 nfrag->next = frag->next;
1496 unlikely((err = pskb_trim(frag, len - offset))))
1500 skb_drop_list(&frag->next);
1505 if (len > skb_headlen(skb)) {
1506 skb->data_len -= skb->len - len;
1511 skb_set_tail_pointer(skb, len);
1516 EXPORT_SYMBOL(___pskb_trim);
1518 /* Note : use pskb_trim_rcsum() instead of calling this directly
1520 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1522 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1523 int delta = skb->len - len;
1525 skb->csum = csum_block_sub(skb->csum,
1526 skb_checksum(skb, len, delta, 0),
1528 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1529 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
1530 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
1532 if (offset + sizeof(__sum16) > hdlen)
1535 return __pskb_trim(skb, len);
1537 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1540 * __pskb_pull_tail - advance tail of skb header
1541 * @skb: buffer to reallocate
1542 * @delta: number of bytes to advance tail
1544 * The function makes a sense only on a fragmented &sk_buff,
1545 * it expands header moving its tail forward and copying necessary
1546 * data from fragmented part.
1548 * &sk_buff MUST have reference count of 1.
1550 * Returns %NULL (and &sk_buff does not change) if pull failed
1551 * or value of new tail of skb in the case of success.
1553 * All the pointers pointing into skb header may change and must be
1554 * reloaded after call to this function.
1557 /* Moves tail of skb head forward, copying data from fragmented part,
1558 * when it is necessary.
1559 * 1. It may fail due to malloc failure.
1560 * 2. It may change skb pointers.
1562 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1564 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1566 /* If skb has not enough free space at tail, get new one
1567 * plus 128 bytes for future expansions. If we have enough
1568 * room at tail, reallocate without expansion only if skb is cloned.
1570 int i, k, eat = (skb->tail + delta) - skb->end;
1572 if (eat > 0 || skb_cloned(skb)) {
1573 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1578 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1581 /* Optimization: no fragments, no reasons to preestimate
1582 * size of pulled pages. Superb.
1584 if (!skb_has_frag_list(skb))
1587 /* Estimate size of pulled pages. */
1589 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1590 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1597 /* If we need update frag list, we are in troubles.
1598 * Certainly, it possible to add an offset to skb data,
1599 * but taking into account that pulling is expected to
1600 * be very rare operation, it is worth to fight against
1601 * further bloating skb head and crucify ourselves here instead.
1602 * Pure masohism, indeed. 8)8)
1605 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1606 struct sk_buff *clone = NULL;
1607 struct sk_buff *insp = NULL;
1612 if (list->len <= eat) {
1613 /* Eaten as whole. */
1618 /* Eaten partially. */
1620 if (skb_shared(list)) {
1621 /* Sucks! We need to fork list. :-( */
1622 clone = skb_clone(list, GFP_ATOMIC);
1628 /* This may be pulled without
1632 if (!pskb_pull(list, eat)) {
1640 /* Free pulled out fragments. */
1641 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1642 skb_shinfo(skb)->frag_list = list->next;
1645 /* And insert new clone at head. */
1648 skb_shinfo(skb)->frag_list = clone;
1651 /* Success! Now we may commit changes to skb data. */
1656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1657 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1660 skb_frag_unref(skb, i);
1663 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1665 skb_shinfo(skb)->frags[k].page_offset += eat;
1666 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1672 skb_shinfo(skb)->nr_frags = k;
1675 skb->data_len -= delta;
1677 return skb_tail_pointer(skb);
1679 EXPORT_SYMBOL(__pskb_pull_tail);
1682 * skb_copy_bits - copy bits from skb to kernel buffer
1684 * @offset: offset in source
1685 * @to: destination buffer
1686 * @len: number of bytes to copy
1688 * Copy the specified number of bytes from the source skb to the
1689 * destination buffer.
1692 * If its prototype is ever changed,
1693 * check arch/{*}/net/{*}.S files,
1694 * since it is called from BPF assembly code.
1696 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1698 int start = skb_headlen(skb);
1699 struct sk_buff *frag_iter;
1702 if (offset > (int)skb->len - len)
1706 if ((copy = start - offset) > 0) {
1709 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1710 if ((len -= copy) == 0)
1716 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1718 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1720 WARN_ON(start > offset + len);
1722 end = start + skb_frag_size(f);
1723 if ((copy = end - offset) > 0) {
1729 vaddr = kmap_atomic(skb_frag_page(f));
1731 vaddr + f->page_offset + offset - start,
1733 kunmap_atomic(vaddr);
1735 if ((len -= copy) == 0)
1743 skb_walk_frags(skb, frag_iter) {
1746 WARN_ON(start > offset + len);
1748 end = start + frag_iter->len;
1749 if ((copy = end - offset) > 0) {
1752 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1754 if ((len -= copy) == 0)
1768 EXPORT_SYMBOL(skb_copy_bits);
1771 * Callback from splice_to_pipe(), if we need to release some pages
1772 * at the end of the spd in case we error'ed out in filling the pipe.
1774 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1776 put_page(spd->pages[i]);
1779 static struct page *linear_to_page(struct page *page, unsigned int *len,
1780 unsigned int *offset,
1783 struct page_frag *pfrag = sk_page_frag(sk);
1785 if (!sk_page_frag_refill(sk, pfrag))
1788 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1790 memcpy(page_address(pfrag->page) + pfrag->offset,
1791 page_address(page) + *offset, *len);
1792 *offset = pfrag->offset;
1793 pfrag->offset += *len;
1798 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1800 unsigned int offset)
1802 return spd->nr_pages &&
1803 spd->pages[spd->nr_pages - 1] == page &&
1804 (spd->partial[spd->nr_pages - 1].offset +
1805 spd->partial[spd->nr_pages - 1].len == offset);
1809 * Fill page/offset/length into spd, if it can hold more pages.
1811 static bool spd_fill_page(struct splice_pipe_desc *spd,
1812 struct pipe_inode_info *pipe, struct page *page,
1813 unsigned int *len, unsigned int offset,
1817 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1821 page = linear_to_page(page, len, &offset, sk);
1825 if (spd_can_coalesce(spd, page, offset)) {
1826 spd->partial[spd->nr_pages - 1].len += *len;
1830 spd->pages[spd->nr_pages] = page;
1831 spd->partial[spd->nr_pages].len = *len;
1832 spd->partial[spd->nr_pages].offset = offset;
1838 static bool __splice_segment(struct page *page, unsigned int poff,
1839 unsigned int plen, unsigned int *off,
1841 struct splice_pipe_desc *spd, bool linear,
1843 struct pipe_inode_info *pipe)
1848 /* skip this segment if already processed */
1854 /* ignore any bits we already processed */
1860 unsigned int flen = min(*len, plen);
1862 if (spd_fill_page(spd, pipe, page, &flen, poff,
1868 } while (*len && plen);
1874 * Map linear and fragment data from the skb to spd. It reports true if the
1875 * pipe is full or if we already spliced the requested length.
1877 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1878 unsigned int *offset, unsigned int *len,
1879 struct splice_pipe_desc *spd, struct sock *sk)
1883 /* map the linear part :
1884 * If skb->head_frag is set, this 'linear' part is backed by a
1885 * fragment, and if the head is not shared with any clones then
1886 * we can avoid a copy since we own the head portion of this page.
1888 if (__splice_segment(virt_to_page(skb->data),
1889 (unsigned long) skb->data & (PAGE_SIZE - 1),
1892 skb_head_is_locked(skb),
1897 * then map the fragments
1899 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1900 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1902 if (__splice_segment(skb_frag_page(f),
1903 f->page_offset, skb_frag_size(f),
1904 offset, len, spd, false, sk, pipe))
1911 ssize_t skb_socket_splice(struct sock *sk,
1912 struct pipe_inode_info *pipe,
1913 struct splice_pipe_desc *spd)
1917 /* Drop the socket lock, otherwise we have reverse
1918 * locking dependencies between sk_lock and i_mutex
1919 * here as compared to sendfile(). We enter here
1920 * with the socket lock held, and splice_to_pipe() will
1921 * grab the pipe inode lock. For sendfile() emulation,
1922 * we call into ->sendpage() with the i_mutex lock held
1923 * and networking will grab the socket lock.
1926 ret = splice_to_pipe(pipe, spd);
1933 * Map data from the skb to a pipe. Should handle both the linear part,
1934 * the fragments, and the frag list. It does NOT handle frag lists within
1935 * the frag list, if such a thing exists. We'd probably need to recurse to
1936 * handle that cleanly.
1938 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1939 struct pipe_inode_info *pipe, unsigned int tlen,
1941 ssize_t (*splice_cb)(struct sock *,
1942 struct pipe_inode_info *,
1943 struct splice_pipe_desc *))
1945 struct partial_page partial[MAX_SKB_FRAGS];
1946 struct page *pages[MAX_SKB_FRAGS];
1947 struct splice_pipe_desc spd = {
1950 .nr_pages_max = MAX_SKB_FRAGS,
1952 .ops = &nosteal_pipe_buf_ops,
1953 .spd_release = sock_spd_release,
1955 struct sk_buff *frag_iter;
1959 * __skb_splice_bits() only fails if the output has no room left,
1960 * so no point in going over the frag_list for the error case.
1962 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1968 * now see if we have a frag_list to map
1970 skb_walk_frags(skb, frag_iter) {
1973 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1979 ret = splice_cb(sk, pipe, &spd);
1983 EXPORT_SYMBOL_GPL(skb_splice_bits);
1986 * skb_store_bits - store bits from kernel buffer to skb
1987 * @skb: destination buffer
1988 * @offset: offset in destination
1989 * @from: source buffer
1990 * @len: number of bytes to copy
1992 * Copy the specified number of bytes from the source buffer to the
1993 * destination skb. This function handles all the messy bits of
1994 * traversing fragment lists and such.
1997 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1999 int start = skb_headlen(skb);
2000 struct sk_buff *frag_iter;
2003 if (offset > (int)skb->len - len)
2006 if ((copy = start - offset) > 0) {
2009 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2010 if ((len -= copy) == 0)
2016 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2017 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2020 WARN_ON(start > offset + len);
2022 end = start + skb_frag_size(frag);
2023 if ((copy = end - offset) > 0) {
2029 vaddr = kmap_atomic(skb_frag_page(frag));
2030 memcpy(vaddr + frag->page_offset + offset - start,
2032 kunmap_atomic(vaddr);
2034 if ((len -= copy) == 0)
2042 skb_walk_frags(skb, frag_iter) {
2045 WARN_ON(start > offset + len);
2047 end = start + frag_iter->len;
2048 if ((copy = end - offset) > 0) {
2051 if (skb_store_bits(frag_iter, offset - start,
2054 if ((len -= copy) == 0)
2067 EXPORT_SYMBOL(skb_store_bits);
2069 /* Checksum skb data. */
2070 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2071 __wsum csum, const struct skb_checksum_ops *ops)
2073 int start = skb_headlen(skb);
2074 int i, copy = start - offset;
2075 struct sk_buff *frag_iter;
2078 /* Checksum header. */
2082 csum = ops->update(skb->data + offset, copy, csum);
2083 if ((len -= copy) == 0)
2089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2091 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2093 WARN_ON(start > offset + len);
2095 end = start + skb_frag_size(frag);
2096 if ((copy = end - offset) > 0) {
2102 vaddr = kmap_atomic(skb_frag_page(frag));
2103 csum2 = ops->update(vaddr + frag->page_offset +
2104 offset - start, copy, 0);
2105 kunmap_atomic(vaddr);
2106 csum = ops->combine(csum, csum2, pos, copy);
2115 skb_walk_frags(skb, frag_iter) {
2118 WARN_ON(start > offset + len);
2120 end = start + frag_iter->len;
2121 if ((copy = end - offset) > 0) {
2125 csum2 = __skb_checksum(frag_iter, offset - start,
2127 csum = ops->combine(csum, csum2, pos, copy);
2128 if ((len -= copy) == 0)
2139 EXPORT_SYMBOL(__skb_checksum);
2141 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2142 int len, __wsum csum)
2144 const struct skb_checksum_ops ops = {
2145 .update = csum_partial_ext,
2146 .combine = csum_block_add_ext,
2149 return __skb_checksum(skb, offset, len, csum, &ops);
2151 EXPORT_SYMBOL(skb_checksum);
2153 /* Both of above in one bottle. */
2155 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2156 u8 *to, int len, __wsum csum)
2158 int start = skb_headlen(skb);
2159 int i, copy = start - offset;
2160 struct sk_buff *frag_iter;
2167 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2169 if ((len -= copy) == 0)
2176 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2179 WARN_ON(start > offset + len);
2181 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2182 if ((copy = end - offset) > 0) {
2185 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2189 vaddr = kmap_atomic(skb_frag_page(frag));
2190 csum2 = csum_partial_copy_nocheck(vaddr +
2194 kunmap_atomic(vaddr);
2195 csum = csum_block_add(csum, csum2, pos);
2205 skb_walk_frags(skb, frag_iter) {
2209 WARN_ON(start > offset + len);
2211 end = start + frag_iter->len;
2212 if ((copy = end - offset) > 0) {
2215 csum2 = skb_copy_and_csum_bits(frag_iter,
2218 csum = csum_block_add(csum, csum2, pos);
2219 if ((len -= copy) == 0)
2230 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2233 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2234 * @from: source buffer
2236 * Calculates the amount of linear headroom needed in the 'to' skb passed
2237 * into skb_zerocopy().
2240 skb_zerocopy_headlen(const struct sk_buff *from)
2242 unsigned int hlen = 0;
2244 if (!from->head_frag ||
2245 skb_headlen(from) < L1_CACHE_BYTES ||
2246 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
2247 hlen = skb_headlen(from);
2252 if (skb_has_frag_list(from))
2257 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2260 * skb_zerocopy - Zero copy skb to skb
2261 * @to: destination buffer
2262 * @from: source buffer
2263 * @len: number of bytes to copy from source buffer
2264 * @hlen: size of linear headroom in destination buffer
2266 * Copies up to `len` bytes from `from` to `to` by creating references
2267 * to the frags in the source buffer.
2269 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2270 * headroom in the `to` buffer.
2273 * 0: everything is OK
2274 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2275 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2278 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2281 int plen = 0; /* length of skb->head fragment */
2284 unsigned int offset;
2286 BUG_ON(!from->head_frag && !hlen);
2288 /* dont bother with small payloads */
2289 if (len <= skb_tailroom(to))
2290 return skb_copy_bits(from, 0, skb_put(to, len), len);
2293 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2298 plen = min_t(int, skb_headlen(from), len);
2300 page = virt_to_head_page(from->head);
2301 offset = from->data - (unsigned char *)page_address(page);
2302 __skb_fill_page_desc(to, 0, page, offset, plen);
2309 to->truesize += len + plen;
2310 to->len += len + plen;
2311 to->data_len += len + plen;
2313 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2318 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2321 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2322 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2323 len -= skb_shinfo(to)->frags[j].size;
2324 skb_frag_ref(to, j);
2327 skb_shinfo(to)->nr_frags = j;
2331 EXPORT_SYMBOL_GPL(skb_zerocopy);
2333 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2338 if (skb->ip_summed == CHECKSUM_PARTIAL)
2339 csstart = skb_checksum_start_offset(skb);
2341 csstart = skb_headlen(skb);
2343 BUG_ON(csstart > skb_headlen(skb));
2345 skb_copy_from_linear_data(skb, to, csstart);
2348 if (csstart != skb->len)
2349 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2350 skb->len - csstart, 0);
2352 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2353 long csstuff = csstart + skb->csum_offset;
2355 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2358 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2361 * skb_dequeue - remove from the head of the queue
2362 * @list: list to dequeue from
2364 * Remove the head of the list. The list lock is taken so the function
2365 * may be used safely with other locking list functions. The head item is
2366 * returned or %NULL if the list is empty.
2369 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2371 unsigned long flags;
2372 struct sk_buff *result;
2374 spin_lock_irqsave(&list->lock, flags);
2375 result = __skb_dequeue(list);
2376 spin_unlock_irqrestore(&list->lock, flags);
2379 EXPORT_SYMBOL(skb_dequeue);
2382 * skb_dequeue_tail - remove from the tail of the queue
2383 * @list: list to dequeue from
2385 * Remove the tail of the list. The list lock is taken so the function
2386 * may be used safely with other locking list functions. The tail item is
2387 * returned or %NULL if the list is empty.
2389 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2391 unsigned long flags;
2392 struct sk_buff *result;
2394 spin_lock_irqsave(&list->lock, flags);
2395 result = __skb_dequeue_tail(list);
2396 spin_unlock_irqrestore(&list->lock, flags);
2399 EXPORT_SYMBOL(skb_dequeue_tail);
2402 * skb_queue_purge - empty a list
2403 * @list: list to empty
2405 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2406 * the list and one reference dropped. This function takes the list
2407 * lock and is atomic with respect to other list locking functions.
2409 void skb_queue_purge(struct sk_buff_head *list)
2411 struct sk_buff *skb;
2412 while ((skb = skb_dequeue(list)) != NULL)
2415 EXPORT_SYMBOL(skb_queue_purge);
2418 * skb_rbtree_purge - empty a skb rbtree
2419 * @root: root of the rbtree to empty
2420 * Return value: the sum of truesizes of all purged skbs.
2422 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2423 * the list and one reference dropped. This function does not take
2424 * any lock. Synchronization should be handled by the caller (e.g., TCP
2425 * out-of-order queue is protected by the socket lock).
2427 unsigned int skb_rbtree_purge(struct rb_root *root)
2429 struct rb_node *p = rb_first(root);
2430 unsigned int sum = 0;
2433 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2436 rb_erase(&skb->rbnode, root);
2437 sum += skb->truesize;
2444 * skb_queue_head - queue a buffer at the list head
2445 * @list: list to use
2446 * @newsk: buffer to queue
2448 * Queue a buffer at the start of the list. This function takes the
2449 * list lock and can be used safely with other locking &sk_buff functions
2452 * A buffer cannot be placed on two lists at the same time.
2454 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2456 unsigned long flags;
2458 spin_lock_irqsave(&list->lock, flags);
2459 __skb_queue_head(list, newsk);
2460 spin_unlock_irqrestore(&list->lock, flags);
2462 EXPORT_SYMBOL(skb_queue_head);
2465 * skb_queue_tail - queue a buffer at the list tail
2466 * @list: list to use
2467 * @newsk: buffer to queue
2469 * Queue a buffer at the tail of the list. This function takes the
2470 * list lock and can be used safely with other locking &sk_buff functions
2473 * A buffer cannot be placed on two lists at the same time.
2475 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2477 unsigned long flags;
2479 spin_lock_irqsave(&list->lock, flags);
2480 __skb_queue_tail(list, newsk);
2481 spin_unlock_irqrestore(&list->lock, flags);
2483 EXPORT_SYMBOL(skb_queue_tail);
2486 * skb_unlink - remove a buffer from a list
2487 * @skb: buffer to remove
2488 * @list: list to use
2490 * Remove a packet from a list. The list locks are taken and this
2491 * function is atomic with respect to other list locked calls
2493 * You must know what list the SKB is on.
2495 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2497 unsigned long flags;
2499 spin_lock_irqsave(&list->lock, flags);
2500 __skb_unlink(skb, list);
2501 spin_unlock_irqrestore(&list->lock, flags);
2503 EXPORT_SYMBOL(skb_unlink);
2506 * skb_append - append a buffer
2507 * @old: buffer to insert after
2508 * @newsk: buffer to insert
2509 * @list: list to use
2511 * Place a packet after a given packet in a list. The list locks are taken
2512 * and this function is atomic with respect to other list locked calls.
2513 * A buffer cannot be placed on two lists at the same time.
2515 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2517 unsigned long flags;
2519 spin_lock_irqsave(&list->lock, flags);
2520 __skb_queue_after(list, old, newsk);
2521 spin_unlock_irqrestore(&list->lock, flags);
2523 EXPORT_SYMBOL(skb_append);
2526 * skb_insert - insert a buffer
2527 * @old: buffer to insert before
2528 * @newsk: buffer to insert
2529 * @list: list to use
2531 * Place a packet before a given packet in a list. The list locks are
2532 * taken and this function is atomic with respect to other list locked
2535 * A buffer cannot be placed on two lists at the same time.
2537 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2539 unsigned long flags;
2541 spin_lock_irqsave(&list->lock, flags);
2542 __skb_insert(newsk, old->prev, old, list);
2543 spin_unlock_irqrestore(&list->lock, flags);
2545 EXPORT_SYMBOL(skb_insert);
2547 static inline void skb_split_inside_header(struct sk_buff *skb,
2548 struct sk_buff* skb1,
2549 const u32 len, const int pos)
2553 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2555 /* And move data appendix as is. */
2556 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2557 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2559 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2560 skb_shinfo(skb)->nr_frags = 0;
2561 skb1->data_len = skb->data_len;
2562 skb1->len += skb1->data_len;
2565 skb_set_tail_pointer(skb, len);
2568 static inline void skb_split_no_header(struct sk_buff *skb,
2569 struct sk_buff* skb1,
2570 const u32 len, int pos)
2573 const int nfrags = skb_shinfo(skb)->nr_frags;
2575 skb_shinfo(skb)->nr_frags = 0;
2576 skb1->len = skb1->data_len = skb->len - len;
2578 skb->data_len = len - pos;
2580 for (i = 0; i < nfrags; i++) {
2581 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2583 if (pos + size > len) {
2584 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2588 * We have two variants in this case:
2589 * 1. Move all the frag to the second
2590 * part, if it is possible. F.e.
2591 * this approach is mandatory for TUX,
2592 * where splitting is expensive.
2593 * 2. Split is accurately. We make this.
2595 skb_frag_ref(skb, i);
2596 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2597 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2598 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2599 skb_shinfo(skb)->nr_frags++;
2603 skb_shinfo(skb)->nr_frags++;
2606 skb_shinfo(skb1)->nr_frags = k;
2610 * skb_split - Split fragmented skb to two parts at length len.
2611 * @skb: the buffer to split
2612 * @skb1: the buffer to receive the second part
2613 * @len: new length for skb
2615 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2617 int pos = skb_headlen(skb);
2619 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2621 if (len < pos) /* Split line is inside header. */
2622 skb_split_inside_header(skb, skb1, len, pos);
2623 else /* Second chunk has no header, nothing to copy. */
2624 skb_split_no_header(skb, skb1, len, pos);
2626 EXPORT_SYMBOL(skb_split);
2628 /* Shifting from/to a cloned skb is a no-go.
2630 * Caller cannot keep skb_shinfo related pointers past calling here!
2632 static int skb_prepare_for_shift(struct sk_buff *skb)
2636 if (skb_cloned(skb)) {
2637 /* Save and restore truesize: pskb_expand_head() may reallocate
2638 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
2639 * cannot change truesize at this point.
2641 unsigned int save_truesize = skb->truesize;
2643 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2644 skb->truesize = save_truesize;
2650 * skb_shift - Shifts paged data partially from skb to another
2651 * @tgt: buffer into which tail data gets added
2652 * @skb: buffer from which the paged data comes from
2653 * @shiftlen: shift up to this many bytes
2655 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2656 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2657 * It's up to caller to free skb if everything was shifted.
2659 * If @tgt runs out of frags, the whole operation is aborted.
2661 * Skb cannot include anything else but paged data while tgt is allowed
2662 * to have non-paged data as well.
2664 * TODO: full sized shift could be optimized but that would need
2665 * specialized skb free'er to handle frags without up-to-date nr_frags.
2667 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2669 int from, to, merge, todo;
2670 struct skb_frag_struct *fragfrom, *fragto;
2672 BUG_ON(shiftlen > skb->len);
2673 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2677 to = skb_shinfo(tgt)->nr_frags;
2678 fragfrom = &skb_shinfo(skb)->frags[from];
2680 /* Actual merge is delayed until the point when we know we can
2681 * commit all, so that we don't have to undo partial changes
2684 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2685 fragfrom->page_offset)) {
2690 todo -= skb_frag_size(fragfrom);
2692 if (skb_prepare_for_shift(skb) ||
2693 skb_prepare_for_shift(tgt))
2696 /* All previous frag pointers might be stale! */
2697 fragfrom = &skb_shinfo(skb)->frags[from];
2698 fragto = &skb_shinfo(tgt)->frags[merge];
2700 skb_frag_size_add(fragto, shiftlen);
2701 skb_frag_size_sub(fragfrom, shiftlen);
2702 fragfrom->page_offset += shiftlen;
2710 /* Skip full, not-fitting skb to avoid expensive operations */
2711 if ((shiftlen == skb->len) &&
2712 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2715 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2718 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2719 if (to == MAX_SKB_FRAGS)
2722 fragfrom = &skb_shinfo(skb)->frags[from];
2723 fragto = &skb_shinfo(tgt)->frags[to];
2725 if (todo >= skb_frag_size(fragfrom)) {
2726 *fragto = *fragfrom;
2727 todo -= skb_frag_size(fragfrom);
2732 __skb_frag_ref(fragfrom);
2733 fragto->page = fragfrom->page;
2734 fragto->page_offset = fragfrom->page_offset;
2735 skb_frag_size_set(fragto, todo);
2737 fragfrom->page_offset += todo;
2738 skb_frag_size_sub(fragfrom, todo);
2746 /* Ready to "commit" this state change to tgt */
2747 skb_shinfo(tgt)->nr_frags = to;
2750 fragfrom = &skb_shinfo(skb)->frags[0];
2751 fragto = &skb_shinfo(tgt)->frags[merge];
2753 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2754 __skb_frag_unref(fragfrom);
2757 /* Reposition in the original skb */
2759 while (from < skb_shinfo(skb)->nr_frags)
2760 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2761 skb_shinfo(skb)->nr_frags = to;
2763 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2766 /* Most likely the tgt won't ever need its checksum anymore, skb on
2767 * the other hand might need it if it needs to be resent
2769 tgt->ip_summed = CHECKSUM_PARTIAL;
2770 skb->ip_summed = CHECKSUM_PARTIAL;
2772 /* Yak, is it really working this way? Some helper please? */
2773 skb->len -= shiftlen;
2774 skb->data_len -= shiftlen;
2775 skb->truesize -= shiftlen;
2776 tgt->len += shiftlen;
2777 tgt->data_len += shiftlen;
2778 tgt->truesize += shiftlen;
2784 * skb_prepare_seq_read - Prepare a sequential read of skb data
2785 * @skb: the buffer to read
2786 * @from: lower offset of data to be read
2787 * @to: upper offset of data to be read
2788 * @st: state variable
2790 * Initializes the specified state variable. Must be called before
2791 * invoking skb_seq_read() for the first time.
2793 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2794 unsigned int to, struct skb_seq_state *st)
2796 st->lower_offset = from;
2797 st->upper_offset = to;
2798 st->root_skb = st->cur_skb = skb;
2799 st->frag_idx = st->stepped_offset = 0;
2800 st->frag_data = NULL;
2802 EXPORT_SYMBOL(skb_prepare_seq_read);
2805 * skb_seq_read - Sequentially read skb data
2806 * @consumed: number of bytes consumed by the caller so far
2807 * @data: destination pointer for data to be returned
2808 * @st: state variable
2810 * Reads a block of skb data at @consumed relative to the
2811 * lower offset specified to skb_prepare_seq_read(). Assigns
2812 * the head of the data block to @data and returns the length
2813 * of the block or 0 if the end of the skb data or the upper
2814 * offset has been reached.
2816 * The caller is not required to consume all of the data
2817 * returned, i.e. @consumed is typically set to the number
2818 * of bytes already consumed and the next call to
2819 * skb_seq_read() will return the remaining part of the block.
2821 * Note 1: The size of each block of data returned can be arbitrary,
2822 * this limitation is the cost for zerocopy sequential
2823 * reads of potentially non linear data.
2825 * Note 2: Fragment lists within fragments are not implemented
2826 * at the moment, state->root_skb could be replaced with
2827 * a stack for this purpose.
2829 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2830 struct skb_seq_state *st)
2832 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2835 if (unlikely(abs_offset >= st->upper_offset)) {
2836 if (st->frag_data) {
2837 kunmap_atomic(st->frag_data);
2838 st->frag_data = NULL;
2844 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2846 if (abs_offset < block_limit && !st->frag_data) {
2847 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2848 return block_limit - abs_offset;
2851 if (st->frag_idx == 0 && !st->frag_data)
2852 st->stepped_offset += skb_headlen(st->cur_skb);
2854 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2855 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2856 block_limit = skb_frag_size(frag) + st->stepped_offset;
2858 if (abs_offset < block_limit) {
2860 st->frag_data = kmap_atomic(skb_frag_page(frag));
2862 *data = (u8 *) st->frag_data + frag->page_offset +
2863 (abs_offset - st->stepped_offset);
2865 return block_limit - abs_offset;
2868 if (st->frag_data) {
2869 kunmap_atomic(st->frag_data);
2870 st->frag_data = NULL;
2874 st->stepped_offset += skb_frag_size(frag);
2877 if (st->frag_data) {
2878 kunmap_atomic(st->frag_data);
2879 st->frag_data = NULL;
2882 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2883 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2886 } else if (st->cur_skb->next) {
2887 st->cur_skb = st->cur_skb->next;
2894 EXPORT_SYMBOL(skb_seq_read);
2897 * skb_abort_seq_read - Abort a sequential read of skb data
2898 * @st: state variable
2900 * Must be called if skb_seq_read() was not called until it
2903 void skb_abort_seq_read(struct skb_seq_state *st)
2906 kunmap_atomic(st->frag_data);
2908 EXPORT_SYMBOL(skb_abort_seq_read);
2910 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2912 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2913 struct ts_config *conf,
2914 struct ts_state *state)
2916 return skb_seq_read(offset, text, TS_SKB_CB(state));
2919 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2921 skb_abort_seq_read(TS_SKB_CB(state));
2925 * skb_find_text - Find a text pattern in skb data
2926 * @skb: the buffer to look in
2927 * @from: search offset
2929 * @config: textsearch configuration
2931 * Finds a pattern in the skb data according to the specified
2932 * textsearch configuration. Use textsearch_next() to retrieve
2933 * subsequent occurrences of the pattern. Returns the offset
2934 * to the first occurrence or UINT_MAX if no match was found.
2936 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2937 unsigned int to, struct ts_config *config)
2939 struct ts_state state;
2942 config->get_next_block = skb_ts_get_next_block;
2943 config->finish = skb_ts_finish;
2945 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2947 ret = textsearch_find(config, &state);
2948 return (ret <= to - from ? ret : UINT_MAX);
2950 EXPORT_SYMBOL(skb_find_text);
2953 * skb_append_datato_frags - append the user data to a skb
2954 * @sk: sock structure
2955 * @skb: skb structure to be appended with user data.
2956 * @getfrag: call back function to be used for getting the user data
2957 * @from: pointer to user message iov
2958 * @length: length of the iov message
2960 * Description: This procedure append the user data in the fragment part
2961 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2963 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2964 int (*getfrag)(void *from, char *to, int offset,
2965 int len, int odd, struct sk_buff *skb),
2966 void *from, int length)
2968 int frg_cnt = skb_shinfo(skb)->nr_frags;
2972 struct page_frag *pfrag = ¤t->task_frag;
2975 /* Return error if we don't have space for new frag */
2976 if (frg_cnt >= MAX_SKB_FRAGS)
2979 if (!sk_page_frag_refill(sk, pfrag))
2982 /* copy the user data to page */
2983 copy = min_t(int, length, pfrag->size - pfrag->offset);
2985 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2986 offset, copy, 0, skb);
2990 /* copy was successful so update the size parameters */
2991 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2994 pfrag->offset += copy;
2995 get_page(pfrag->page);
2997 skb->truesize += copy;
2998 atomic_add(copy, &sk->sk_wmem_alloc);
3000 skb->data_len += copy;
3004 } while (length > 0);
3008 EXPORT_SYMBOL(skb_append_datato_frags);
3010 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3011 int offset, size_t size)
3013 int i = skb_shinfo(skb)->nr_frags;
3015 if (skb_can_coalesce(skb, i, page, offset)) {
3016 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3017 } else if (i < MAX_SKB_FRAGS) {
3019 skb_fill_page_desc(skb, i, page, offset, size);
3026 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3029 * skb_pull_rcsum - pull skb and update receive checksum
3030 * @skb: buffer to update
3031 * @len: length of data pulled
3033 * This function performs an skb_pull on the packet and updates
3034 * the CHECKSUM_COMPLETE checksum. It should be used on
3035 * receive path processing instead of skb_pull unless you know
3036 * that the checksum difference is zero (e.g., a valid IP header)
3037 * or you are setting ip_summed to CHECKSUM_NONE.
3039 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3041 unsigned char *data = skb->data;
3043 BUG_ON(len > skb->len);
3044 __skb_pull(skb, len);
3045 skb_postpull_rcsum(skb, data, len);
3048 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3051 * skb_segment - Perform protocol segmentation on skb.
3052 * @head_skb: buffer to segment
3053 * @features: features for the output path (see dev->features)
3055 * This function performs segmentation on the given skb. It returns
3056 * a pointer to the first in a list of new skbs for the segments.
3057 * In case of error it returns ERR_PTR(err).
3059 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3060 netdev_features_t features)
3062 struct sk_buff *segs = NULL;
3063 struct sk_buff *tail = NULL;
3064 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3065 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3066 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3067 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3068 struct sk_buff *frag_skb = head_skb;
3069 unsigned int offset = doffset;
3070 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3071 unsigned int headroom;
3075 int sg = !!(features & NETIF_F_SG);
3076 int nfrags = skb_shinfo(head_skb)->nr_frags;
3082 __skb_push(head_skb, doffset);
3083 proto = skb_network_protocol(head_skb, &dummy);
3084 if (unlikely(!proto))
3085 return ERR_PTR(-EINVAL);
3087 csum = !head_skb->encap_hdr_csum &&
3088 !!can_checksum_protocol(features, proto);
3090 headroom = skb_headroom(head_skb);
3091 pos = skb_headlen(head_skb);
3094 struct sk_buff *nskb;
3095 skb_frag_t *nskb_frag;
3099 len = head_skb->len - offset;
3103 hsize = skb_headlen(head_skb) - offset;
3106 if (hsize > len || !sg)
3109 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3110 (skb_headlen(list_skb) == len || sg)) {
3111 BUG_ON(skb_headlen(list_skb) > len);
3114 nfrags = skb_shinfo(list_skb)->nr_frags;
3115 frag = skb_shinfo(list_skb)->frags;
3116 frag_skb = list_skb;
3117 pos += skb_headlen(list_skb);
3119 while (pos < offset + len) {
3120 BUG_ON(i >= nfrags);
3122 size = skb_frag_size(frag);
3123 if (pos + size > offset + len)
3131 nskb = skb_clone(list_skb, GFP_ATOMIC);
3132 list_skb = list_skb->next;
3134 if (unlikely(!nskb))
3137 if (unlikely(pskb_trim(nskb, len))) {
3142 hsize = skb_end_offset(nskb);
3143 if (skb_cow_head(nskb, doffset + headroom)) {
3148 nskb->truesize += skb_end_offset(nskb) - hsize;
3149 skb_release_head_state(nskb);
3150 __skb_push(nskb, doffset);
3152 nskb = __alloc_skb(hsize + doffset + headroom,
3153 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3156 if (unlikely(!nskb))
3159 skb_reserve(nskb, headroom);
3160 __skb_put(nskb, doffset);
3169 __copy_skb_header(nskb, head_skb);
3171 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3172 skb_reset_mac_len(nskb);
3174 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3175 nskb->data - tnl_hlen,
3176 doffset + tnl_hlen);
3178 if (nskb->len == len + doffset)
3179 goto perform_csum_check;
3181 if (!sg && !nskb->remcsum_offload) {
3182 nskb->ip_summed = CHECKSUM_NONE;
3183 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3186 SKB_GSO_CB(nskb)->csum_start =
3187 skb_headroom(nskb) + doffset;
3191 nskb_frag = skb_shinfo(nskb)->frags;
3193 skb_copy_from_linear_data_offset(head_skb, offset,
3194 skb_put(nskb, hsize), hsize);
3196 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3199 while (pos < offset + len) {
3201 BUG_ON(skb_headlen(list_skb));
3204 nfrags = skb_shinfo(list_skb)->nr_frags;
3205 frag = skb_shinfo(list_skb)->frags;
3206 frag_skb = list_skb;
3210 list_skb = list_skb->next;
3213 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3215 net_warn_ratelimited(
3216 "skb_segment: too many frags: %u %u\n",
3221 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3225 __skb_frag_ref(nskb_frag);
3226 size = skb_frag_size(nskb_frag);
3229 nskb_frag->page_offset += offset - pos;
3230 skb_frag_size_sub(nskb_frag, offset - pos);
3233 skb_shinfo(nskb)->nr_frags++;
3235 if (pos + size <= offset + len) {
3240 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3248 nskb->data_len = len - hsize;
3249 nskb->len += nskb->data_len;
3250 nskb->truesize += nskb->data_len;
3253 if (!csum && !nskb->remcsum_offload) {
3254 nskb->csum = skb_checksum(nskb, doffset,
3255 nskb->len - doffset, 0);
3256 nskb->ip_summed = CHECKSUM_NONE;
3257 SKB_GSO_CB(nskb)->csum_start =
3258 skb_headroom(nskb) + doffset;
3260 } while ((offset += len) < head_skb->len);
3262 /* Some callers want to get the end of the list.
3263 * Put it in segs->prev to avoid walking the list.
3264 * (see validate_xmit_skb_list() for example)
3268 /* Following permits correct backpressure, for protocols
3269 * using skb_set_owner_w().
3270 * Idea is to tranfert ownership from head_skb to last segment.
3272 if (head_skb->destructor == sock_wfree) {
3273 swap(tail->truesize, head_skb->truesize);
3274 swap(tail->destructor, head_skb->destructor);
3275 swap(tail->sk, head_skb->sk);
3280 kfree_skb_list(segs);
3281 return ERR_PTR(err);
3283 EXPORT_SYMBOL_GPL(skb_segment);
3285 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3287 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3288 unsigned int offset = skb_gro_offset(skb);
3289 unsigned int headlen = skb_headlen(skb);
3290 unsigned int len = skb_gro_len(skb);
3291 struct sk_buff *lp, *p = *head;
3292 unsigned int delta_truesize;
3294 if (unlikely(p->len + len >= 65536))
3297 lp = NAPI_GRO_CB(p)->last;
3298 pinfo = skb_shinfo(lp);
3300 if (headlen <= offset) {
3303 int i = skbinfo->nr_frags;
3304 int nr_frags = pinfo->nr_frags + i;
3306 if (nr_frags > MAX_SKB_FRAGS)
3310 pinfo->nr_frags = nr_frags;
3311 skbinfo->nr_frags = 0;
3313 frag = pinfo->frags + nr_frags;
3314 frag2 = skbinfo->frags + i;
3319 frag->page_offset += offset;
3320 skb_frag_size_sub(frag, offset);
3322 /* all fragments truesize : remove (head size + sk_buff) */
3323 delta_truesize = skb->truesize -
3324 SKB_TRUESIZE(skb_end_offset(skb));
3326 skb->truesize -= skb->data_len;
3327 skb->len -= skb->data_len;
3330 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3332 } else if (skb->head_frag) {
3333 int nr_frags = pinfo->nr_frags;
3334 skb_frag_t *frag = pinfo->frags + nr_frags;
3335 struct page *page = virt_to_head_page(skb->head);
3336 unsigned int first_size = headlen - offset;
3337 unsigned int first_offset;
3339 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3342 first_offset = skb->data -
3343 (unsigned char *)page_address(page) +
3346 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3348 frag->page.p = page;
3349 frag->page_offset = first_offset;
3350 skb_frag_size_set(frag, first_size);
3352 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3353 /* We dont need to clear skbinfo->nr_frags here */
3355 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3356 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3361 delta_truesize = skb->truesize;
3362 if (offset > headlen) {
3363 unsigned int eat = offset - headlen;
3365 skbinfo->frags[0].page_offset += eat;
3366 skb_frag_size_sub(&skbinfo->frags[0], eat);
3367 skb->data_len -= eat;
3372 __skb_pull(skb, offset);
3374 if (NAPI_GRO_CB(p)->last == p)
3375 skb_shinfo(p)->frag_list = skb;
3377 NAPI_GRO_CB(p)->last->next = skb;
3378 NAPI_GRO_CB(p)->last = skb;
3379 __skb_header_release(skb);
3383 NAPI_GRO_CB(p)->count++;
3385 p->truesize += delta_truesize;
3388 lp->data_len += len;
3389 lp->truesize += delta_truesize;
3392 NAPI_GRO_CB(skb)->same_flow = 1;
3396 void __init skb_init(void)
3398 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3399 sizeof(struct sk_buff),
3401 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3403 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3404 sizeof(struct sk_buff_fclones),
3406 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3411 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3412 unsigned int recursion_level)
3414 int start = skb_headlen(skb);
3415 int i, copy = start - offset;
3416 struct sk_buff *frag_iter;
3419 if (unlikely(recursion_level >= 24))
3425 sg_set_buf(sg, skb->data + offset, copy);
3427 if ((len -= copy) == 0)
3432 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3435 WARN_ON(start > offset + len);
3437 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3438 if ((copy = end - offset) > 0) {
3439 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3440 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3445 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3446 frag->page_offset+offset-start);
3455 skb_walk_frags(skb, frag_iter) {
3458 WARN_ON(start > offset + len);
3460 end = start + frag_iter->len;
3461 if ((copy = end - offset) > 0) {
3462 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3467 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3468 copy, recursion_level + 1);
3469 if (unlikely(ret < 0))
3472 if ((len -= copy) == 0)
3483 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3484 * @skb: Socket buffer containing the buffers to be mapped
3485 * @sg: The scatter-gather list to map into
3486 * @offset: The offset into the buffer's contents to start mapping
3487 * @len: Length of buffer space to be mapped
3489 * Fill the specified scatter-gather list with mappings/pointers into a
3490 * region of the buffer space attached to a socket buffer. Returns either
3491 * the number of scatterlist items used, or -EMSGSIZE if the contents
3494 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3496 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3501 sg_mark_end(&sg[nsg - 1]);
3505 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3507 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3508 * sglist without mark the sg which contain last skb data as the end.
3509 * So the caller can mannipulate sg list as will when padding new data after
3510 * the first call without calling sg_unmark_end to expend sg list.
3512 * Scenario to use skb_to_sgvec_nomark:
3514 * 2. skb_to_sgvec_nomark(payload1)
3515 * 3. skb_to_sgvec_nomark(payload2)
3517 * This is equivalent to:
3519 * 2. skb_to_sgvec(payload1)
3521 * 4. skb_to_sgvec(payload2)
3523 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3524 * is more preferable.
3526 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3527 int offset, int len)
3529 return __skb_to_sgvec(skb, sg, offset, len, 0);
3531 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3536 * skb_cow_data - Check that a socket buffer's data buffers are writable
3537 * @skb: The socket buffer to check.
3538 * @tailbits: Amount of trailing space to be added
3539 * @trailer: Returned pointer to the skb where the @tailbits space begins
3541 * Make sure that the data buffers attached to a socket buffer are
3542 * writable. If they are not, private copies are made of the data buffers
3543 * and the socket buffer is set to use these instead.
3545 * If @tailbits is given, make sure that there is space to write @tailbits
3546 * bytes of data beyond current end of socket buffer. @trailer will be
3547 * set to point to the skb in which this space begins.
3549 * The number of scatterlist elements required to completely map the
3550 * COW'd and extended socket buffer will be returned.
3552 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3556 struct sk_buff *skb1, **skb_p;
3558 /* If skb is cloned or its head is paged, reallocate
3559 * head pulling out all the pages (pages are considered not writable
3560 * at the moment even if they are anonymous).
3562 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3563 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3566 /* Easy case. Most of packets will go this way. */
3567 if (!skb_has_frag_list(skb)) {
3568 /* A little of trouble, not enough of space for trailer.
3569 * This should not happen, when stack is tuned to generate
3570 * good frames. OK, on miss we reallocate and reserve even more
3571 * space, 128 bytes is fair. */
3573 if (skb_tailroom(skb) < tailbits &&
3574 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3582 /* Misery. We are in troubles, going to mincer fragments... */
3585 skb_p = &skb_shinfo(skb)->frag_list;
3588 while ((skb1 = *skb_p) != NULL) {
3591 /* The fragment is partially pulled by someone,
3592 * this can happen on input. Copy it and everything
3595 if (skb_shared(skb1))
3598 /* If the skb is the last, worry about trailer. */
3600 if (skb1->next == NULL && tailbits) {
3601 if (skb_shinfo(skb1)->nr_frags ||
3602 skb_has_frag_list(skb1) ||
3603 skb_tailroom(skb1) < tailbits)
3604 ntail = tailbits + 128;
3610 skb_shinfo(skb1)->nr_frags ||
3611 skb_has_frag_list(skb1)) {
3612 struct sk_buff *skb2;
3614 /* Fuck, we are miserable poor guys... */
3616 skb2 = skb_copy(skb1, GFP_ATOMIC);
3618 skb2 = skb_copy_expand(skb1,
3622 if (unlikely(skb2 == NULL))
3626 skb_set_owner_w(skb2, skb1->sk);
3628 /* Looking around. Are we still alive?
3629 * OK, link new skb, drop old one */
3631 skb2->next = skb1->next;
3638 skb_p = &skb1->next;
3643 EXPORT_SYMBOL_GPL(skb_cow_data);
3645 static void sock_rmem_free(struct sk_buff *skb)
3647 struct sock *sk = skb->sk;
3649 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3653 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3655 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3657 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3658 (unsigned int)sk->sk_rcvbuf)
3663 skb->destructor = sock_rmem_free;
3664 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3666 /* before exiting rcu section, make sure dst is refcounted */
3669 skb_queue_tail(&sk->sk_error_queue, skb);
3670 if (!sock_flag(sk, SOCK_DEAD))
3671 sk->sk_error_report(sk);
3674 EXPORT_SYMBOL(sock_queue_err_skb);
3676 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3678 struct sk_buff_head *q = &sk->sk_error_queue;
3679 struct sk_buff *skb, *skb_next;
3680 unsigned long flags;
3683 spin_lock_irqsave(&q->lock, flags);
3684 skb = __skb_dequeue(q);
3685 if (skb && (skb_next = skb_peek(q)))
3686 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3687 spin_unlock_irqrestore(&q->lock, flags);
3691 sk->sk_error_report(sk);
3695 EXPORT_SYMBOL(sock_dequeue_err_skb);
3698 * skb_clone_sk - create clone of skb, and take reference to socket
3699 * @skb: the skb to clone
3701 * This function creates a clone of a buffer that holds a reference on
3702 * sk_refcnt. Buffers created via this function are meant to be
3703 * returned using sock_queue_err_skb, or free via kfree_skb.
3705 * When passing buffers allocated with this function to sock_queue_err_skb
3706 * it is necessary to wrap the call with sock_hold/sock_put in order to
3707 * prevent the socket from being released prior to being enqueued on
3708 * the sk_error_queue.
3710 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3712 struct sock *sk = skb->sk;
3713 struct sk_buff *clone;
3715 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3718 clone = skb_clone(skb, GFP_ATOMIC);
3725 clone->destructor = sock_efree;
3729 EXPORT_SYMBOL(skb_clone_sk);
3731 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3735 struct sock_exterr_skb *serr;
3738 serr = SKB_EXT_ERR(skb);
3739 memset(serr, 0, sizeof(*serr));
3740 serr->ee.ee_errno = ENOMSG;
3741 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3742 serr->ee.ee_info = tstype;
3743 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3744 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3745 if (sk->sk_protocol == IPPROTO_TCP &&
3746 sk->sk_type == SOCK_STREAM)
3747 serr->ee.ee_data -= sk->sk_tskey;
3750 err = sock_queue_err_skb(sk, skb);
3756 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3760 if (likely(sysctl_tstamp_allow_data || tsonly))
3763 read_lock_bh(&sk->sk_callback_lock);
3764 ret = sk->sk_socket && sk->sk_socket->file &&
3765 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3766 read_unlock_bh(&sk->sk_callback_lock);
3770 void skb_complete_tx_timestamp(struct sk_buff *skb,
3771 struct skb_shared_hwtstamps *hwtstamps)
3773 struct sock *sk = skb->sk;
3775 if (!skb_may_tx_timestamp(sk, false))
3778 /* Take a reference to prevent skb_orphan() from freeing the socket,
3779 * but only if the socket refcount is not zero.
3781 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3782 *skb_hwtstamps(skb) = *hwtstamps;
3783 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3791 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3793 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3794 struct skb_shared_hwtstamps *hwtstamps,
3795 struct sock *sk, int tstype)
3797 struct sk_buff *skb;
3803 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3804 if (!skb_may_tx_timestamp(sk, tsonly))
3808 skb = alloc_skb(0, GFP_ATOMIC);
3810 skb = skb_clone(orig_skb, GFP_ATOMIC);
3815 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3817 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3821 *skb_hwtstamps(skb) = *hwtstamps;
3823 skb->tstamp = ktime_get_real();
3825 __skb_complete_tx_timestamp(skb, sk, tstype);
3827 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3829 void skb_tstamp_tx(struct sk_buff *orig_skb,
3830 struct skb_shared_hwtstamps *hwtstamps)
3832 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3835 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3837 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3839 struct sock *sk = skb->sk;
3840 struct sock_exterr_skb *serr;
3843 skb->wifi_acked_valid = 1;
3844 skb->wifi_acked = acked;
3846 serr = SKB_EXT_ERR(skb);
3847 memset(serr, 0, sizeof(*serr));
3848 serr->ee.ee_errno = ENOMSG;
3849 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3851 /* Take a reference to prevent skb_orphan() from freeing the socket,
3852 * but only if the socket refcount is not zero.
3854 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3855 err = sock_queue_err_skb(sk, skb);
3861 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3864 * skb_partial_csum_set - set up and verify partial csum values for packet
3865 * @skb: the skb to set
3866 * @start: the number of bytes after skb->data to start checksumming.
3867 * @off: the offset from start to place the checksum.
3869 * For untrusted partially-checksummed packets, we need to make sure the values
3870 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3872 * This function checks and sets those values and skb->ip_summed: if this
3873 * returns false you should drop the packet.
3875 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3877 if (unlikely(start > skb_headlen(skb)) ||
3878 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3879 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3880 start, off, skb_headlen(skb));
3883 skb->ip_summed = CHECKSUM_PARTIAL;
3884 skb->csum_start = skb_headroom(skb) + start;
3885 skb->csum_offset = off;
3886 skb_set_transport_header(skb, start);
3889 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3891 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3894 if (skb_headlen(skb) >= len)
3897 /* If we need to pullup then pullup to the max, so we
3898 * won't need to do it again.
3903 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3906 if (skb_headlen(skb) < len)
3912 #define MAX_TCP_HDR_LEN (15 * 4)
3914 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3915 typeof(IPPROTO_IP) proto,
3922 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3923 off + MAX_TCP_HDR_LEN);
3924 if (!err && !skb_partial_csum_set(skb, off,
3925 offsetof(struct tcphdr,
3928 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3931 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3932 off + sizeof(struct udphdr));
3933 if (!err && !skb_partial_csum_set(skb, off,
3934 offsetof(struct udphdr,
3937 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3940 return ERR_PTR(-EPROTO);
3943 /* This value should be large enough to cover a tagged ethernet header plus
3944 * maximally sized IP and TCP or UDP headers.
3946 #define MAX_IP_HDR_LEN 128
3948 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3957 err = skb_maybe_pull_tail(skb,
3958 sizeof(struct iphdr),
3963 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3966 off = ip_hdrlen(skb);
3973 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3975 return PTR_ERR(csum);
3978 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3981 ip_hdr(skb)->protocol, 0);
3988 /* This value should be large enough to cover a tagged ethernet header plus
3989 * an IPv6 header, all options, and a maximal TCP or UDP header.
3991 #define MAX_IPV6_HDR_LEN 256
3993 #define OPT_HDR(type, skb, off) \
3994 (type *)(skb_network_header(skb) + (off))
3996 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4009 off = sizeof(struct ipv6hdr);
4011 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4015 nexthdr = ipv6_hdr(skb)->nexthdr;
4017 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4018 while (off <= len && !done) {
4020 case IPPROTO_DSTOPTS:
4021 case IPPROTO_HOPOPTS:
4022 case IPPROTO_ROUTING: {
4023 struct ipv6_opt_hdr *hp;
4025 err = skb_maybe_pull_tail(skb,
4027 sizeof(struct ipv6_opt_hdr),
4032 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4033 nexthdr = hp->nexthdr;
4034 off += ipv6_optlen(hp);
4038 struct ip_auth_hdr *hp;
4040 err = skb_maybe_pull_tail(skb,
4042 sizeof(struct ip_auth_hdr),
4047 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4048 nexthdr = hp->nexthdr;
4049 off += ipv6_authlen(hp);
4052 case IPPROTO_FRAGMENT: {
4053 struct frag_hdr *hp;
4055 err = skb_maybe_pull_tail(skb,
4057 sizeof(struct frag_hdr),
4062 hp = OPT_HDR(struct frag_hdr, skb, off);
4064 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4067 nexthdr = hp->nexthdr;
4068 off += sizeof(struct frag_hdr);
4079 if (!done || fragment)
4082 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4084 return PTR_ERR(csum);
4087 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4088 &ipv6_hdr(skb)->daddr,
4089 skb->len - off, nexthdr, 0);
4097 * skb_checksum_setup - set up partial checksum offset
4098 * @skb: the skb to set up
4099 * @recalculate: if true the pseudo-header checksum will be recalculated
4101 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4105 switch (skb->protocol) {
4106 case htons(ETH_P_IP):
4107 err = skb_checksum_setup_ipv4(skb, recalculate);
4110 case htons(ETH_P_IPV6):
4111 err = skb_checksum_setup_ipv6(skb, recalculate);
4121 EXPORT_SYMBOL(skb_checksum_setup);
4124 * skb_checksum_maybe_trim - maybe trims the given skb
4125 * @skb: the skb to check
4126 * @transport_len: the data length beyond the network header
4128 * Checks whether the given skb has data beyond the given transport length.
4129 * If so, returns a cloned skb trimmed to this transport length.
4130 * Otherwise returns the provided skb. Returns NULL in error cases
4131 * (e.g. transport_len exceeds skb length or out-of-memory).
4133 * Caller needs to set the skb transport header and free any returned skb if it
4134 * differs from the provided skb.
4136 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4137 unsigned int transport_len)
4139 struct sk_buff *skb_chk;
4140 unsigned int len = skb_transport_offset(skb) + transport_len;
4145 else if (skb->len == len)
4148 skb_chk = skb_clone(skb, GFP_ATOMIC);
4152 ret = pskb_trim_rcsum(skb_chk, len);
4162 * skb_checksum_trimmed - validate checksum of an skb
4163 * @skb: the skb to check
4164 * @transport_len: the data length beyond the network header
4165 * @skb_chkf: checksum function to use
4167 * Applies the given checksum function skb_chkf to the provided skb.
4168 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4170 * If the skb has data beyond the given transport length, then a
4171 * trimmed & cloned skb is checked and returned.
4173 * Caller needs to set the skb transport header and free any returned skb if it
4174 * differs from the provided skb.
4176 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4177 unsigned int transport_len,
4178 __sum16(*skb_chkf)(struct sk_buff *skb))
4180 struct sk_buff *skb_chk;
4181 unsigned int offset = skb_transport_offset(skb);
4184 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4188 if (!pskb_may_pull(skb_chk, offset))
4191 skb_pull_rcsum(skb_chk, offset);
4192 ret = skb_chkf(skb_chk);
4193 skb_push_rcsum(skb_chk, offset);
4201 if (skb_chk && skb_chk != skb)
4207 EXPORT_SYMBOL(skb_checksum_trimmed);
4209 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4211 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4214 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4216 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4219 skb_release_head_state(skb);
4220 kmem_cache_free(skbuff_head_cache, skb);
4225 EXPORT_SYMBOL(kfree_skb_partial);
4228 * skb_try_coalesce - try to merge skb to prior one
4230 * @from: buffer to add
4231 * @fragstolen: pointer to boolean
4232 * @delta_truesize: how much more was allocated than was requested
4234 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4235 bool *fragstolen, int *delta_truesize)
4237 int i, delta, len = from->len;
4239 *fragstolen = false;
4244 if (len <= skb_tailroom(to)) {
4246 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4247 *delta_truesize = 0;
4251 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4254 if (skb_headlen(from) != 0) {
4256 unsigned int offset;
4258 if (skb_shinfo(to)->nr_frags +
4259 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4262 if (skb_head_is_locked(from))
4265 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4267 page = virt_to_head_page(from->head);
4268 offset = from->data - (unsigned char *)page_address(page);
4270 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4271 page, offset, skb_headlen(from));
4274 if (skb_shinfo(to)->nr_frags +
4275 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4278 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4281 WARN_ON_ONCE(delta < len);
4283 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4284 skb_shinfo(from)->frags,
4285 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4286 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4288 if (!skb_cloned(from))
4289 skb_shinfo(from)->nr_frags = 0;
4291 /* if the skb is not cloned this does nothing
4292 * since we set nr_frags to 0.
4294 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4295 skb_frag_ref(from, i);
4297 to->truesize += delta;
4299 to->data_len += len;
4301 *delta_truesize = delta;
4304 EXPORT_SYMBOL(skb_try_coalesce);
4307 * skb_scrub_packet - scrub an skb
4309 * @skb: buffer to clean
4310 * @xnet: packet is crossing netns
4312 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4313 * into/from a tunnel. Some information have to be cleared during these
4315 * skb_scrub_packet can also be used to clean a skb before injecting it in
4316 * another namespace (@xnet == true). We have to clear all information in the
4317 * skb that could impact namespace isolation.
4319 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4321 skb->tstamp.tv64 = 0;
4322 skb->pkt_type = PACKET_HOST;
4326 skb_sender_cpu_clear(skb);
4329 nf_reset_trace(skb);
4338 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4341 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4345 * skb_gso_transport_seglen is used to determine the real size of the
4346 * individual segments, including Layer4 headers (TCP/UDP).
4348 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4350 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4352 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4353 unsigned int thlen = 0;
4355 if (skb->encapsulation) {
4356 thlen = skb_inner_transport_header(skb) -
4357 skb_transport_header(skb);
4359 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4360 thlen += inner_tcp_hdrlen(skb);
4361 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4362 thlen = tcp_hdrlen(skb);
4364 /* UFO sets gso_size to the size of the fragmentation
4365 * payload, i.e. the size of the L4 (UDP) header is already
4368 return thlen + shinfo->gso_size;
4370 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4372 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4376 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4381 mac_len = skb->data - skb_mac_header(skb);
4382 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
4383 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
4384 mac_len - VLAN_HLEN - ETH_TLEN);
4386 skb->mac_header += VLAN_HLEN;
4390 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4392 struct vlan_hdr *vhdr;
4395 if (unlikely(skb_vlan_tag_present(skb))) {
4396 /* vlan_tci is already set-up so leave this for another time */
4400 skb = skb_share_check(skb, GFP_ATOMIC);
4403 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
4404 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
4407 vhdr = (struct vlan_hdr *)skb->data;
4408 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4409 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4411 skb_pull_rcsum(skb, VLAN_HLEN);
4412 vlan_set_encap_proto(skb, vhdr);
4414 skb = skb_reorder_vlan_header(skb);
4418 skb_reset_network_header(skb);
4419 skb_reset_transport_header(skb);
4420 skb_reset_mac_len(skb);
4428 EXPORT_SYMBOL(skb_vlan_untag);
4430 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4432 if (!pskb_may_pull(skb, write_len))
4435 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4438 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4440 EXPORT_SYMBOL(skb_ensure_writable);
4442 /* remove VLAN header from packet and update csum accordingly. */
4443 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4445 struct vlan_hdr *vhdr;
4446 unsigned int offset = skb->data - skb_mac_header(skb);
4449 __skb_push(skb, offset);
4450 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4454 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4456 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4457 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4459 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4460 __skb_pull(skb, VLAN_HLEN);
4462 vlan_set_encap_proto(skb, vhdr);
4463 skb->mac_header += VLAN_HLEN;
4465 if (skb_network_offset(skb) < ETH_HLEN)
4466 skb_set_network_header(skb, ETH_HLEN);
4468 skb_reset_mac_len(skb);
4470 __skb_pull(skb, offset);
4475 int skb_vlan_pop(struct sk_buff *skb)
4481 if (likely(skb_vlan_tag_present(skb))) {
4484 if (unlikely(skb->protocol != htons(ETH_P_8021Q) &&
4485 skb->protocol != htons(ETH_P_8021AD)))
4488 err = __skb_vlan_pop(skb, &vlan_tci);
4492 /* move next vlan tag to hw accel tag */
4493 if (likely(skb->protocol != htons(ETH_P_8021Q) &&
4494 skb->protocol != htons(ETH_P_8021AD)))
4497 vlan_proto = skb->protocol;
4498 err = __skb_vlan_pop(skb, &vlan_tci);
4502 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4505 EXPORT_SYMBOL(skb_vlan_pop);
4507 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4509 if (skb_vlan_tag_present(skb)) {
4510 unsigned int offset = skb->data - skb_mac_header(skb);
4513 /* __vlan_insert_tag expect skb->data pointing to mac header.
4514 * So change skb->data before calling it and change back to
4515 * original position later
4517 __skb_push(skb, offset);
4518 err = __vlan_insert_tag(skb, skb->vlan_proto,
4519 skb_vlan_tag_get(skb));
4521 __skb_pull(skb, offset);
4525 skb->protocol = skb->vlan_proto;
4526 skb->mac_len += VLAN_HLEN;
4528 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4529 __skb_pull(skb, offset);
4531 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4534 EXPORT_SYMBOL(skb_vlan_push);
4537 * alloc_skb_with_frags - allocate skb with page frags
4539 * @header_len: size of linear part
4540 * @data_len: needed length in frags
4541 * @max_page_order: max page order desired.
4542 * @errcode: pointer to error code if any
4543 * @gfp_mask: allocation mask
4545 * This can be used to allocate a paged skb, given a maximal order for frags.
4547 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4548 unsigned long data_len,
4553 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4554 unsigned long chunk;
4555 struct sk_buff *skb;
4560 *errcode = -EMSGSIZE;
4561 /* Note this test could be relaxed, if we succeed to allocate
4562 * high order pages...
4564 if (npages > MAX_SKB_FRAGS)
4567 gfp_head = gfp_mask;
4568 if (gfp_head & __GFP_DIRECT_RECLAIM)
4569 gfp_head |= __GFP_REPEAT;
4571 *errcode = -ENOBUFS;
4572 skb = alloc_skb(header_len, gfp_head);
4576 skb->truesize += npages << PAGE_SHIFT;
4578 for (i = 0; npages > 0; i++) {
4579 int order = max_page_order;
4582 if (npages >= 1 << order) {
4583 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4590 /* Do not retry other high order allocations */
4596 page = alloc_page(gfp_mask);
4600 chunk = min_t(unsigned long, data_len,
4601 PAGE_SIZE << order);
4602 skb_fill_page_desc(skb, i, page, 0, chunk);
4604 npages -= 1 << order;
4612 EXPORT_SYMBOL(alloc_skb_with_frags);