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>
66 #include <linux/locallock.h>
68 #include <net/protocol.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
75 #include <asm/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
83 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84 EXPORT_SYMBOL(sysctl_max_skb_frags);
87 * skb_panic - private function for out-of-line support
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
108 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
113 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115 skb_panic(skb, sz, addr, __func__);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
132 bool ret_pfmemalloc = false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
150 *pfmemalloc = ret_pfmemalloc;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
161 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
176 memset(skb, 0, offsetof(struct sk_buff, tail));
178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
181 skb->mac_header = (typeof(skb->mac_header))~0U;
187 * __alloc_skb - allocate a network buffer
188 * @size: size to allocate
189 * @gfp_mask: allocation mask
190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
194 * @node: numa node to allocate memory on
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
203 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
206 struct kmem_cache *cache;
207 struct skb_shared_info *shinfo;
212 cache = (flags & SKB_ALLOC_FCLONE)
213 ? skbuff_fclone_cache : skbuff_head_cache;
215 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216 gfp_mask |= __GFP_MEMALLOC;
219 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
224 /* We do our best to align skb_shared_info on a separate cache
225 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
226 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
227 * Both skb->head and skb_shared_info are cache line aligned.
229 size = SKB_DATA_ALIGN(size);
230 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
231 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
234 /* kmalloc(size) might give us more room than requested.
235 * Put skb_shared_info exactly at the end of allocated zone,
236 * to allow max possible filling before reallocation.
238 size = SKB_WITH_OVERHEAD(ksize(data));
239 prefetchw(data + size);
242 * Only clear those fields we need to clear, not those that we will
243 * actually initialise below. Hence, don't put any more fields after
244 * the tail pointer in struct sk_buff!
246 memset(skb, 0, offsetof(struct sk_buff, tail));
247 /* Account for allocated memory : skb + skb->head */
248 skb->truesize = SKB_TRUESIZE(size);
249 skb->pfmemalloc = pfmemalloc;
250 atomic_set(&skb->users, 1);
253 skb_reset_tail_pointer(skb);
254 skb->end = skb->tail + size;
255 skb->mac_header = (typeof(skb->mac_header))~0U;
256 skb->transport_header = (typeof(skb->transport_header))~0U;
258 /* make sure we initialize shinfo sequentially */
259 shinfo = skb_shinfo(skb);
260 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
261 atomic_set(&shinfo->dataref, 1);
262 kmemcheck_annotate_variable(shinfo->destructor_arg);
264 if (flags & SKB_ALLOC_FCLONE) {
265 struct sk_buff_fclones *fclones;
267 fclones = container_of(skb, struct sk_buff_fclones, skb1);
269 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
270 skb->fclone = SKB_FCLONE_ORIG;
271 atomic_set(&fclones->fclone_ref, 1);
273 fclones->skb2.fclone = SKB_FCLONE_CLONE;
274 fclones->skb2.pfmemalloc = pfmemalloc;
279 kmem_cache_free(cache, skb);
283 EXPORT_SYMBOL(__alloc_skb);
286 * __build_skb - build a network buffer
287 * @data: data buffer provided by caller
288 * @frag_size: size of data, or 0 if head was kmalloced
290 * Allocate a new &sk_buff. Caller provides space holding head and
291 * skb_shared_info. @data must have been allocated by kmalloc() only if
292 * @frag_size is 0, otherwise data should come from the page allocator
294 * The return is the new skb buffer.
295 * On a failure the return is %NULL, and @data is not freed.
297 * Before IO, driver allocates only data buffer where NIC put incoming frame
298 * Driver should add room at head (NET_SKB_PAD) and
299 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
300 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
301 * before giving packet to stack.
302 * RX rings only contains data buffers, not full skbs.
304 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
306 struct skb_shared_info *shinfo;
308 unsigned int size = frag_size ? : ksize(data);
310 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
314 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
316 memset(skb, 0, offsetof(struct sk_buff, tail));
317 skb->truesize = SKB_TRUESIZE(size);
318 atomic_set(&skb->users, 1);
321 skb_reset_tail_pointer(skb);
322 skb->end = skb->tail + size;
323 skb->mac_header = (typeof(skb->mac_header))~0U;
324 skb->transport_header = (typeof(skb->transport_header))~0U;
326 /* make sure we initialize shinfo sequentially */
327 shinfo = skb_shinfo(skb);
328 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
329 atomic_set(&shinfo->dataref, 1);
330 kmemcheck_annotate_variable(shinfo->destructor_arg);
335 /* build_skb() is wrapper over __build_skb(), that specifically
336 * takes care of skb->head and skb->pfmemalloc
337 * This means that if @frag_size is not zero, then @data must be backed
338 * by a page fragment, not kmalloc() or vmalloc()
340 struct sk_buff *build_skb(void *data, unsigned int frag_size)
342 struct sk_buff *skb = __build_skb(data, frag_size);
344 if (skb && frag_size) {
346 if (page_is_pfmemalloc(virt_to_head_page(data)))
351 EXPORT_SYMBOL(build_skb);
353 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
354 static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
355 static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
356 static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
358 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
360 struct page_frag_cache *nc;
364 local_lock_irqsave(netdev_alloc_lock, flags);
365 nc = this_cpu_ptr(&netdev_alloc_cache);
366 data = __alloc_page_frag(nc, fragsz, gfp_mask);
367 local_unlock_irqrestore(netdev_alloc_lock, flags);
372 * netdev_alloc_frag - allocate a page fragment
373 * @fragsz: fragment size
375 * Allocates a frag from a page for receive buffer.
376 * Uses GFP_ATOMIC allocations.
378 void *netdev_alloc_frag(unsigned int fragsz)
380 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
382 EXPORT_SYMBOL(netdev_alloc_frag);
384 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
386 struct page_frag_cache *nc;
389 nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
390 data = __alloc_page_frag(nc, fragsz, gfp_mask);
391 put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
395 void *napi_alloc_frag(unsigned int fragsz)
397 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399 EXPORT_SYMBOL(napi_alloc_frag);
402 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
403 * @dev: network device to receive on
404 * @len: length to allocate
405 * @gfp_mask: get_free_pages mask, passed to alloc_skb
407 * Allocate a new &sk_buff and assign it a usage count of one. The
408 * buffer has NET_SKB_PAD headroom built in. Users should allocate
409 * the headroom they think they need without accounting for the
410 * built in space. The built in space is used for optimisations.
412 * %NULL is returned if there is no free memory.
414 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
417 struct page_frag_cache *nc;
425 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
426 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
427 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
433 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
434 len = SKB_DATA_ALIGN(len);
436 if (sk_memalloc_socks())
437 gfp_mask |= __GFP_MEMALLOC;
439 local_lock_irqsave(netdev_alloc_lock, flags);
441 nc = this_cpu_ptr(&netdev_alloc_cache);
442 data = __alloc_page_frag(nc, len, gfp_mask);
443 pfmemalloc = nc->pfmemalloc;
445 local_unlock_irqrestore(netdev_alloc_lock, flags);
450 skb = __build_skb(data, len);
451 if (unlikely(!skb)) {
456 /* use OR instead of assignment to avoid clearing of bits in mask */
462 skb_reserve(skb, NET_SKB_PAD);
468 EXPORT_SYMBOL(__netdev_alloc_skb);
471 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
472 * @napi: napi instance this buffer was allocated for
473 * @len: length to allocate
474 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
476 * Allocate a new sk_buff for use in NAPI receive. This buffer will
477 * attempt to allocate the head from a special reserved region used
478 * only for NAPI Rx allocation. By doing this we can save several
479 * CPU cycles by avoiding having to disable and re-enable IRQs.
481 * %NULL is returned if there is no free memory.
483 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
486 struct page_frag_cache *nc;
491 len += NET_SKB_PAD + NET_IP_ALIGN;
493 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
494 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
495 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
501 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
502 len = SKB_DATA_ALIGN(len);
504 if (sk_memalloc_socks())
505 gfp_mask |= __GFP_MEMALLOC;
507 nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
508 data = __alloc_page_frag(nc, len, gfp_mask);
509 pfmemalloc = nc->pfmemalloc;
510 put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
515 skb = __build_skb(data, len);
516 if (unlikely(!skb)) {
521 /* use OR instead of assignment to avoid clearing of bits in mask */
527 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
528 skb->dev = napi->dev;
533 EXPORT_SYMBOL(__napi_alloc_skb);
535 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
536 int size, unsigned int truesize)
538 skb_fill_page_desc(skb, i, page, off, size);
540 skb->data_len += size;
541 skb->truesize += truesize;
543 EXPORT_SYMBOL(skb_add_rx_frag);
545 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
546 unsigned int truesize)
548 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
550 skb_frag_size_add(frag, size);
552 skb->data_len += size;
553 skb->truesize += truesize;
555 EXPORT_SYMBOL(skb_coalesce_rx_frag);
557 static void skb_drop_list(struct sk_buff **listp)
559 kfree_skb_list(*listp);
563 static inline void skb_drop_fraglist(struct sk_buff *skb)
565 skb_drop_list(&skb_shinfo(skb)->frag_list);
568 static void skb_clone_fraglist(struct sk_buff *skb)
570 struct sk_buff *list;
572 skb_walk_frags(skb, list)
576 static void skb_free_head(struct sk_buff *skb)
578 unsigned char *head = skb->head;
586 static void skb_release_data(struct sk_buff *skb)
588 struct skb_shared_info *shinfo = skb_shinfo(skb);
592 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
596 for (i = 0; i < shinfo->nr_frags; i++)
597 __skb_frag_unref(&shinfo->frags[i]);
600 * If skb buf is from userspace, we need to notify the caller
601 * the lower device DMA has done;
603 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
604 struct ubuf_info *uarg;
606 uarg = shinfo->destructor_arg;
608 uarg->callback(uarg, true);
611 if (shinfo->frag_list)
612 kfree_skb_list(shinfo->frag_list);
618 * Free an skbuff by memory without cleaning the state.
620 static void kfree_skbmem(struct sk_buff *skb)
622 struct sk_buff_fclones *fclones;
624 switch (skb->fclone) {
625 case SKB_FCLONE_UNAVAILABLE:
626 kmem_cache_free(skbuff_head_cache, skb);
629 case SKB_FCLONE_ORIG:
630 fclones = container_of(skb, struct sk_buff_fclones, skb1);
632 /* We usually free the clone (TX completion) before original skb
633 * This test would have no chance to be true for the clone,
634 * while here, branch prediction will be good.
636 if (atomic_read(&fclones->fclone_ref) == 1)
640 default: /* SKB_FCLONE_CLONE */
641 fclones = container_of(skb, struct sk_buff_fclones, skb2);
644 if (!atomic_dec_and_test(&fclones->fclone_ref))
647 kmem_cache_free(skbuff_fclone_cache, fclones);
650 static void skb_release_head_state(struct sk_buff *skb)
654 secpath_put(skb->sp);
656 if (skb->destructor) {
658 skb->destructor(skb);
660 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
661 nf_conntrack_put(skb->nfct);
663 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
664 nf_bridge_put(skb->nf_bridge);
668 /* Free everything but the sk_buff shell. */
669 static void skb_release_all(struct sk_buff *skb)
671 skb_release_head_state(skb);
672 if (likely(skb->head))
673 skb_release_data(skb);
677 * __kfree_skb - private function
680 * Free an sk_buff. Release anything attached to the buffer.
681 * Clean the state. This is an internal helper function. Users should
682 * always call kfree_skb
685 void __kfree_skb(struct sk_buff *skb)
687 skb_release_all(skb);
690 EXPORT_SYMBOL(__kfree_skb);
693 * kfree_skb - free an sk_buff
694 * @skb: buffer to free
696 * Drop a reference to the buffer and free it if the usage count has
699 void kfree_skb(struct sk_buff *skb)
703 if (likely(atomic_read(&skb->users) == 1))
705 else if (likely(!atomic_dec_and_test(&skb->users)))
707 trace_kfree_skb(skb, __builtin_return_address(0));
710 EXPORT_SYMBOL(kfree_skb);
712 void kfree_skb_list(struct sk_buff *segs)
715 struct sk_buff *next = segs->next;
721 EXPORT_SYMBOL(kfree_skb_list);
724 * skb_tx_error - report an sk_buff xmit error
725 * @skb: buffer that triggered an error
727 * Report xmit error if a device callback is tracking this skb.
728 * skb must be freed afterwards.
730 void skb_tx_error(struct sk_buff *skb)
732 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
733 struct ubuf_info *uarg;
735 uarg = skb_shinfo(skb)->destructor_arg;
737 uarg->callback(uarg, false);
738 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
741 EXPORT_SYMBOL(skb_tx_error);
744 * consume_skb - free an skbuff
745 * @skb: buffer to free
747 * Drop a ref to the buffer and free it if the usage count has hit zero
748 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
749 * is being dropped after a failure and notes that
751 void consume_skb(struct sk_buff *skb)
755 if (likely(atomic_read(&skb->users) == 1))
757 else if (likely(!atomic_dec_and_test(&skb->users)))
759 trace_consume_skb(skb);
762 EXPORT_SYMBOL(consume_skb);
764 /* Make sure a field is enclosed inside headers_start/headers_end section */
765 #define CHECK_SKB_FIELD(field) \
766 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
767 offsetof(struct sk_buff, headers_start)); \
768 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
769 offsetof(struct sk_buff, headers_end)); \
771 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
773 new->tstamp = old->tstamp;
774 /* We do not copy old->sk */
776 memcpy(new->cb, old->cb, sizeof(old->cb));
777 skb_dst_copy(new, old);
779 new->sp = secpath_get(old->sp);
781 __nf_copy(new, old, false);
783 /* Note : this field could be in headers_start/headers_end section
784 * It is not yet because we do not want to have a 16 bit hole
786 new->queue_mapping = old->queue_mapping;
788 memcpy(&new->headers_start, &old->headers_start,
789 offsetof(struct sk_buff, headers_end) -
790 offsetof(struct sk_buff, headers_start));
791 CHECK_SKB_FIELD(protocol);
792 CHECK_SKB_FIELD(csum);
793 CHECK_SKB_FIELD(hash);
794 CHECK_SKB_FIELD(priority);
795 CHECK_SKB_FIELD(skb_iif);
796 CHECK_SKB_FIELD(vlan_proto);
797 CHECK_SKB_FIELD(vlan_tci);
798 CHECK_SKB_FIELD(transport_header);
799 CHECK_SKB_FIELD(network_header);
800 CHECK_SKB_FIELD(mac_header);
801 CHECK_SKB_FIELD(inner_protocol);
802 CHECK_SKB_FIELD(inner_transport_header);
803 CHECK_SKB_FIELD(inner_network_header);
804 CHECK_SKB_FIELD(inner_mac_header);
805 CHECK_SKB_FIELD(mark);
806 #ifdef CONFIG_NETWORK_SECMARK
807 CHECK_SKB_FIELD(secmark);
809 #ifdef CONFIG_NET_RX_BUSY_POLL
810 CHECK_SKB_FIELD(napi_id);
813 CHECK_SKB_FIELD(sender_cpu);
815 #ifdef CONFIG_NET_SCHED
816 CHECK_SKB_FIELD(tc_index);
817 #ifdef CONFIG_NET_CLS_ACT
818 CHECK_SKB_FIELD(tc_verd);
825 * You should not add any new code to this function. Add it to
826 * __copy_skb_header above instead.
828 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
830 #define C(x) n->x = skb->x
832 n->next = n->prev = NULL;
834 __copy_skb_header(n, skb);
839 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
842 n->destructor = NULL;
849 atomic_set(&n->users, 1);
851 atomic_inc(&(skb_shinfo(skb)->dataref));
859 * skb_morph - morph one skb into another
860 * @dst: the skb to receive the contents
861 * @src: the skb to supply the contents
863 * This is identical to skb_clone except that the target skb is
864 * supplied by the user.
866 * The target skb is returned upon exit.
868 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
870 skb_release_all(dst);
871 return __skb_clone(dst, src);
873 EXPORT_SYMBOL_GPL(skb_morph);
876 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
877 * @skb: the skb to modify
878 * @gfp_mask: allocation priority
880 * This must be called on SKBTX_DEV_ZEROCOPY skb.
881 * It will copy all frags into kernel and drop the reference
882 * to userspace pages.
884 * If this function is called from an interrupt gfp_mask() must be
887 * Returns 0 on success or a negative error code on failure
888 * to allocate kernel memory to copy to.
890 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
893 int num_frags = skb_shinfo(skb)->nr_frags;
894 struct page *page, *head = NULL;
895 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
897 for (i = 0; i < num_frags; i++) {
899 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
901 page = alloc_page(gfp_mask);
904 struct page *next = (struct page *)page_private(head);
910 vaddr = kmap_atomic(skb_frag_page(f));
911 memcpy(page_address(page),
912 vaddr + f->page_offset, skb_frag_size(f));
913 kunmap_atomic(vaddr);
914 set_page_private(page, (unsigned long)head);
918 /* skb frags release userspace buffers */
919 for (i = 0; i < num_frags; i++)
920 skb_frag_unref(skb, i);
922 uarg->callback(uarg, false);
924 /* skb frags point to kernel buffers */
925 for (i = num_frags - 1; i >= 0; i--) {
926 __skb_fill_page_desc(skb, i, head, 0,
927 skb_shinfo(skb)->frags[i].size);
928 head = (struct page *)page_private(head);
931 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
934 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
937 * skb_clone - duplicate an sk_buff
938 * @skb: buffer to clone
939 * @gfp_mask: allocation priority
941 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
942 * copies share the same packet data but not structure. The new
943 * buffer has a reference count of 1. If the allocation fails the
944 * function returns %NULL otherwise the new buffer is returned.
946 * If this function is called from an interrupt gfp_mask() must be
950 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
952 struct sk_buff_fclones *fclones = container_of(skb,
953 struct sk_buff_fclones,
957 if (skb_orphan_frags(skb, gfp_mask))
960 if (skb->fclone == SKB_FCLONE_ORIG &&
961 atomic_read(&fclones->fclone_ref) == 1) {
963 atomic_set(&fclones->fclone_ref, 2);
965 if (skb_pfmemalloc(skb))
966 gfp_mask |= __GFP_MEMALLOC;
968 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
972 kmemcheck_annotate_bitfield(n, flags1);
973 n->fclone = SKB_FCLONE_UNAVAILABLE;
976 return __skb_clone(n, skb);
978 EXPORT_SYMBOL(skb_clone);
980 static void skb_headers_offset_update(struct sk_buff *skb, int off)
982 /* Only adjust this if it actually is csum_start rather than csum */
983 if (skb->ip_summed == CHECKSUM_PARTIAL)
984 skb->csum_start += off;
985 /* {transport,network,mac}_header and tail are relative to skb->head */
986 skb->transport_header += off;
987 skb->network_header += off;
988 if (skb_mac_header_was_set(skb))
989 skb->mac_header += off;
990 skb->inner_transport_header += off;
991 skb->inner_network_header += off;
992 skb->inner_mac_header += off;
995 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
997 __copy_skb_header(new, old);
999 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1000 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1001 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1004 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1006 if (skb_pfmemalloc(skb))
1007 return SKB_ALLOC_RX;
1012 * skb_copy - create private copy of an sk_buff
1013 * @skb: buffer to copy
1014 * @gfp_mask: allocation priority
1016 * Make a copy of both an &sk_buff and its data. This is used when the
1017 * caller wishes to modify the data and needs a private copy of the
1018 * data to alter. Returns %NULL on failure or the pointer to the buffer
1019 * on success. The returned buffer has a reference count of 1.
1021 * As by-product this function converts non-linear &sk_buff to linear
1022 * one, so that &sk_buff becomes completely private and caller is allowed
1023 * to modify all the data of returned buffer. This means that this
1024 * function is not recommended for use in circumstances when only
1025 * header is going to be modified. Use pskb_copy() instead.
1028 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1030 int headerlen = skb_headroom(skb);
1031 unsigned int size = skb_end_offset(skb) + skb->data_len;
1032 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1033 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1038 /* Set the data pointer */
1039 skb_reserve(n, headerlen);
1040 /* Set the tail pointer and length */
1041 skb_put(n, skb->len);
1043 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1046 copy_skb_header(n, skb);
1049 EXPORT_SYMBOL(skb_copy);
1052 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1053 * @skb: buffer to copy
1054 * @headroom: headroom of new skb
1055 * @gfp_mask: allocation priority
1056 * @fclone: if true allocate the copy of the skb from the fclone
1057 * cache instead of the head cache; it is recommended to set this
1058 * to true for the cases where the copy will likely be cloned
1060 * Make a copy of both an &sk_buff and part of its data, located
1061 * in header. Fragmented data remain shared. This is used when
1062 * the caller wishes to modify only header of &sk_buff and needs
1063 * private copy of the header to alter. Returns %NULL on failure
1064 * or the pointer to the buffer on success.
1065 * The returned buffer has a reference count of 1.
1068 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1069 gfp_t gfp_mask, bool fclone)
1071 unsigned int size = skb_headlen(skb) + headroom;
1072 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1073 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1078 /* Set the data pointer */
1079 skb_reserve(n, headroom);
1080 /* Set the tail pointer and length */
1081 skb_put(n, skb_headlen(skb));
1082 /* Copy the bytes */
1083 skb_copy_from_linear_data(skb, n->data, n->len);
1085 n->truesize += skb->data_len;
1086 n->data_len = skb->data_len;
1089 if (skb_shinfo(skb)->nr_frags) {
1092 if (skb_orphan_frags(skb, gfp_mask)) {
1097 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1098 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1099 skb_frag_ref(skb, i);
1101 skb_shinfo(n)->nr_frags = i;
1104 if (skb_has_frag_list(skb)) {
1105 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1106 skb_clone_fraglist(n);
1109 copy_skb_header(n, skb);
1113 EXPORT_SYMBOL(__pskb_copy_fclone);
1116 * pskb_expand_head - reallocate header of &sk_buff
1117 * @skb: buffer to reallocate
1118 * @nhead: room to add at head
1119 * @ntail: room to add at tail
1120 * @gfp_mask: allocation priority
1122 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1123 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1124 * reference count of 1. Returns zero in the case of success or error,
1125 * if expansion failed. In the last case, &sk_buff is not changed.
1127 * All the pointers pointing into skb header may change and must be
1128 * reloaded after call to this function.
1131 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1136 int size = nhead + skb_end_offset(skb) + ntail;
1141 if (skb_shared(skb))
1144 size = SKB_DATA_ALIGN(size);
1146 if (skb_pfmemalloc(skb))
1147 gfp_mask |= __GFP_MEMALLOC;
1148 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1149 gfp_mask, NUMA_NO_NODE, NULL);
1152 size = SKB_WITH_OVERHEAD(ksize(data));
1154 /* Copy only real data... and, alas, header. This should be
1155 * optimized for the cases when header is void.
1157 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1159 memcpy((struct skb_shared_info *)(data + size),
1161 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1164 * if shinfo is shared we must drop the old head gracefully, but if it
1165 * is not we can just drop the old head and let the existing refcount
1166 * be since all we did is relocate the values
1168 if (skb_cloned(skb)) {
1169 /* copy this zero copy skb frags */
1170 if (skb_orphan_frags(skb, gfp_mask))
1172 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1173 skb_frag_ref(skb, i);
1175 if (skb_has_frag_list(skb))
1176 skb_clone_fraglist(skb);
1178 skb_release_data(skb);
1182 off = (data + nhead) - skb->head;
1187 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1191 skb->end = skb->head + size;
1194 skb_headers_offset_update(skb, nhead);
1198 atomic_set(&skb_shinfo(skb)->dataref, 1);
1206 EXPORT_SYMBOL(pskb_expand_head);
1208 /* Make private copy of skb with writable head and some headroom */
1210 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1212 struct sk_buff *skb2;
1213 int delta = headroom - skb_headroom(skb);
1216 skb2 = pskb_copy(skb, GFP_ATOMIC);
1218 skb2 = skb_clone(skb, GFP_ATOMIC);
1219 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1227 EXPORT_SYMBOL(skb_realloc_headroom);
1230 * skb_copy_expand - copy and expand sk_buff
1231 * @skb: buffer to copy
1232 * @newheadroom: new free bytes at head
1233 * @newtailroom: new free bytes at tail
1234 * @gfp_mask: allocation priority
1236 * Make a copy of both an &sk_buff and its data and while doing so
1237 * allocate additional space.
1239 * This is used when the caller wishes to modify the data and needs a
1240 * private copy of the data to alter as well as more space for new fields.
1241 * Returns %NULL on failure or the pointer to the buffer
1242 * on success. The returned buffer has a reference count of 1.
1244 * You must pass %GFP_ATOMIC as the allocation priority if this function
1245 * is called from an interrupt.
1247 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1248 int newheadroom, int newtailroom,
1252 * Allocate the copy buffer
1254 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1255 gfp_mask, skb_alloc_rx_flag(skb),
1257 int oldheadroom = skb_headroom(skb);
1258 int head_copy_len, head_copy_off;
1263 skb_reserve(n, newheadroom);
1265 /* Set the tail pointer and length */
1266 skb_put(n, skb->len);
1268 head_copy_len = oldheadroom;
1270 if (newheadroom <= head_copy_len)
1271 head_copy_len = newheadroom;
1273 head_copy_off = newheadroom - head_copy_len;
1275 /* Copy the linear header and data. */
1276 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1277 skb->len + head_copy_len))
1280 copy_skb_header(n, skb);
1282 skb_headers_offset_update(n, newheadroom - oldheadroom);
1286 EXPORT_SYMBOL(skb_copy_expand);
1289 * skb_pad - zero pad the tail of an skb
1290 * @skb: buffer to pad
1291 * @pad: space to pad
1293 * Ensure that a buffer is followed by a padding area that is zero
1294 * filled. Used by network drivers which may DMA or transfer data
1295 * beyond the buffer end onto the wire.
1297 * May return error in out of memory cases. The skb is freed on error.
1300 int skb_pad(struct sk_buff *skb, int pad)
1305 /* If the skbuff is non linear tailroom is always zero.. */
1306 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1307 memset(skb->data+skb->len, 0, pad);
1311 ntail = skb->data_len + pad - (skb->end - skb->tail);
1312 if (likely(skb_cloned(skb) || ntail > 0)) {
1313 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1318 /* FIXME: The use of this function with non-linear skb's really needs
1321 err = skb_linearize(skb);
1325 memset(skb->data + skb->len, 0, pad);
1332 EXPORT_SYMBOL(skb_pad);
1335 * pskb_put - add data to the tail of a potentially fragmented buffer
1336 * @skb: start of the buffer to use
1337 * @tail: tail fragment of the buffer to use
1338 * @len: amount of data to add
1340 * This function extends the used data area of the potentially
1341 * fragmented buffer. @tail must be the last fragment of @skb -- or
1342 * @skb itself. If this would exceed the total buffer size the kernel
1343 * will panic. A pointer to the first byte of the extra data is
1347 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1350 skb->data_len += len;
1353 return skb_put(tail, len);
1355 EXPORT_SYMBOL_GPL(pskb_put);
1358 * skb_put - add data to a buffer
1359 * @skb: buffer to use
1360 * @len: amount of data to add
1362 * This function extends the used data area of the buffer. If this would
1363 * exceed the total buffer size the kernel will panic. A pointer to the
1364 * first byte of the extra data is returned.
1366 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1368 unsigned char *tmp = skb_tail_pointer(skb);
1369 SKB_LINEAR_ASSERT(skb);
1372 if (unlikely(skb->tail > skb->end))
1373 skb_over_panic(skb, len, __builtin_return_address(0));
1376 EXPORT_SYMBOL(skb_put);
1379 * skb_push - add data to the start of a buffer
1380 * @skb: buffer to use
1381 * @len: amount of data to add
1383 * This function extends the used data area of the buffer at the buffer
1384 * start. If this would exceed the total buffer headroom the kernel will
1385 * panic. A pointer to the first byte of the extra data is returned.
1387 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1391 if (unlikely(skb->data<skb->head))
1392 skb_under_panic(skb, len, __builtin_return_address(0));
1395 EXPORT_SYMBOL(skb_push);
1398 * skb_pull - remove data from the start of a buffer
1399 * @skb: buffer to use
1400 * @len: amount of data to remove
1402 * This function removes data from the start of a buffer, returning
1403 * the memory to the headroom. A pointer to the next data in the buffer
1404 * is returned. Once the data has been pulled future pushes will overwrite
1407 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1409 return skb_pull_inline(skb, len);
1411 EXPORT_SYMBOL(skb_pull);
1414 * skb_trim - remove end from a buffer
1415 * @skb: buffer to alter
1418 * Cut the length of a buffer down by removing data from the tail. If
1419 * the buffer is already under the length specified it is not modified.
1420 * The skb must be linear.
1422 void skb_trim(struct sk_buff *skb, unsigned int len)
1425 __skb_trim(skb, len);
1427 EXPORT_SYMBOL(skb_trim);
1429 /* Trims skb to length len. It can change skb pointers.
1432 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1434 struct sk_buff **fragp;
1435 struct sk_buff *frag;
1436 int offset = skb_headlen(skb);
1437 int nfrags = skb_shinfo(skb)->nr_frags;
1441 if (skb_cloned(skb) &&
1442 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1449 for (; i < nfrags; i++) {
1450 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1457 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1460 skb_shinfo(skb)->nr_frags = i;
1462 for (; i < nfrags; i++)
1463 skb_frag_unref(skb, i);
1465 if (skb_has_frag_list(skb))
1466 skb_drop_fraglist(skb);
1470 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1471 fragp = &frag->next) {
1472 int end = offset + frag->len;
1474 if (skb_shared(frag)) {
1475 struct sk_buff *nfrag;
1477 nfrag = skb_clone(frag, GFP_ATOMIC);
1478 if (unlikely(!nfrag))
1481 nfrag->next = frag->next;
1493 unlikely((err = pskb_trim(frag, len - offset))))
1497 skb_drop_list(&frag->next);
1502 if (len > skb_headlen(skb)) {
1503 skb->data_len -= skb->len - len;
1508 skb_set_tail_pointer(skb, len);
1513 EXPORT_SYMBOL(___pskb_trim);
1516 * __pskb_pull_tail - advance tail of skb header
1517 * @skb: buffer to reallocate
1518 * @delta: number of bytes to advance tail
1520 * The function makes a sense only on a fragmented &sk_buff,
1521 * it expands header moving its tail forward and copying necessary
1522 * data from fragmented part.
1524 * &sk_buff MUST have reference count of 1.
1526 * Returns %NULL (and &sk_buff does not change) if pull failed
1527 * or value of new tail of skb in the case of success.
1529 * All the pointers pointing into skb header may change and must be
1530 * reloaded after call to this function.
1533 /* Moves tail of skb head forward, copying data from fragmented part,
1534 * when it is necessary.
1535 * 1. It may fail due to malloc failure.
1536 * 2. It may change skb pointers.
1538 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1540 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1542 /* If skb has not enough free space at tail, get new one
1543 * plus 128 bytes for future expansions. If we have enough
1544 * room at tail, reallocate without expansion only if skb is cloned.
1546 int i, k, eat = (skb->tail + delta) - skb->end;
1548 if (eat > 0 || skb_cloned(skb)) {
1549 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1554 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1557 /* Optimization: no fragments, no reasons to preestimate
1558 * size of pulled pages. Superb.
1560 if (!skb_has_frag_list(skb))
1563 /* Estimate size of pulled pages. */
1565 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1566 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1573 /* If we need update frag list, we are in troubles.
1574 * Certainly, it possible to add an offset to skb data,
1575 * but taking into account that pulling is expected to
1576 * be very rare operation, it is worth to fight against
1577 * further bloating skb head and crucify ourselves here instead.
1578 * Pure masohism, indeed. 8)8)
1581 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1582 struct sk_buff *clone = NULL;
1583 struct sk_buff *insp = NULL;
1588 if (list->len <= eat) {
1589 /* Eaten as whole. */
1594 /* Eaten partially. */
1596 if (skb_shared(list)) {
1597 /* Sucks! We need to fork list. :-( */
1598 clone = skb_clone(list, GFP_ATOMIC);
1604 /* This may be pulled without
1608 if (!pskb_pull(list, eat)) {
1616 /* Free pulled out fragments. */
1617 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1618 skb_shinfo(skb)->frag_list = list->next;
1621 /* And insert new clone at head. */
1624 skb_shinfo(skb)->frag_list = clone;
1627 /* Success! Now we may commit changes to skb data. */
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1633 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1636 skb_frag_unref(skb, i);
1639 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1641 skb_shinfo(skb)->frags[k].page_offset += eat;
1642 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1648 skb_shinfo(skb)->nr_frags = k;
1651 skb->data_len -= delta;
1653 return skb_tail_pointer(skb);
1655 EXPORT_SYMBOL(__pskb_pull_tail);
1658 * skb_copy_bits - copy bits from skb to kernel buffer
1660 * @offset: offset in source
1661 * @to: destination buffer
1662 * @len: number of bytes to copy
1664 * Copy the specified number of bytes from the source skb to the
1665 * destination buffer.
1668 * If its prototype is ever changed,
1669 * check arch/{*}/net/{*}.S files,
1670 * since it is called from BPF assembly code.
1672 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1674 int start = skb_headlen(skb);
1675 struct sk_buff *frag_iter;
1678 if (offset > (int)skb->len - len)
1682 if ((copy = start - offset) > 0) {
1685 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1686 if ((len -= copy) == 0)
1692 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1694 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1696 WARN_ON(start > offset + len);
1698 end = start + skb_frag_size(f);
1699 if ((copy = end - offset) > 0) {
1705 vaddr = kmap_atomic(skb_frag_page(f));
1707 vaddr + f->page_offset + offset - start,
1709 kunmap_atomic(vaddr);
1711 if ((len -= copy) == 0)
1719 skb_walk_frags(skb, frag_iter) {
1722 WARN_ON(start > offset + len);
1724 end = start + frag_iter->len;
1725 if ((copy = end - offset) > 0) {
1728 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1730 if ((len -= copy) == 0)
1744 EXPORT_SYMBOL(skb_copy_bits);
1747 * Callback from splice_to_pipe(), if we need to release some pages
1748 * at the end of the spd in case we error'ed out in filling the pipe.
1750 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1752 put_page(spd->pages[i]);
1755 static struct page *linear_to_page(struct page *page, unsigned int *len,
1756 unsigned int *offset,
1759 struct page_frag *pfrag = sk_page_frag(sk);
1761 if (!sk_page_frag_refill(sk, pfrag))
1764 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1766 memcpy(page_address(pfrag->page) + pfrag->offset,
1767 page_address(page) + *offset, *len);
1768 *offset = pfrag->offset;
1769 pfrag->offset += *len;
1774 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1776 unsigned int offset)
1778 return spd->nr_pages &&
1779 spd->pages[spd->nr_pages - 1] == page &&
1780 (spd->partial[spd->nr_pages - 1].offset +
1781 spd->partial[spd->nr_pages - 1].len == offset);
1785 * Fill page/offset/length into spd, if it can hold more pages.
1787 static bool spd_fill_page(struct splice_pipe_desc *spd,
1788 struct pipe_inode_info *pipe, struct page *page,
1789 unsigned int *len, unsigned int offset,
1793 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1797 page = linear_to_page(page, len, &offset, sk);
1801 if (spd_can_coalesce(spd, page, offset)) {
1802 spd->partial[spd->nr_pages - 1].len += *len;
1806 spd->pages[spd->nr_pages] = page;
1807 spd->partial[spd->nr_pages].len = *len;
1808 spd->partial[spd->nr_pages].offset = offset;
1814 static bool __splice_segment(struct page *page, unsigned int poff,
1815 unsigned int plen, unsigned int *off,
1817 struct splice_pipe_desc *spd, bool linear,
1819 struct pipe_inode_info *pipe)
1824 /* skip this segment if already processed */
1830 /* ignore any bits we already processed */
1836 unsigned int flen = min(*len, plen);
1838 if (spd_fill_page(spd, pipe, page, &flen, poff,
1844 } while (*len && plen);
1850 * Map linear and fragment data from the skb to spd. It reports true if the
1851 * pipe is full or if we already spliced the requested length.
1853 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1854 unsigned int *offset, unsigned int *len,
1855 struct splice_pipe_desc *spd, struct sock *sk)
1859 /* map the linear part :
1860 * If skb->head_frag is set, this 'linear' part is backed by a
1861 * fragment, and if the head is not shared with any clones then
1862 * we can avoid a copy since we own the head portion of this page.
1864 if (__splice_segment(virt_to_page(skb->data),
1865 (unsigned long) skb->data & (PAGE_SIZE - 1),
1868 skb_head_is_locked(skb),
1873 * then map the fragments
1875 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1876 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1878 if (__splice_segment(skb_frag_page(f),
1879 f->page_offset, skb_frag_size(f),
1880 offset, len, spd, false, sk, pipe))
1887 ssize_t skb_socket_splice(struct sock *sk,
1888 struct pipe_inode_info *pipe,
1889 struct splice_pipe_desc *spd)
1893 /* Drop the socket lock, otherwise we have reverse
1894 * locking dependencies between sk_lock and i_mutex
1895 * here as compared to sendfile(). We enter here
1896 * with the socket lock held, and splice_to_pipe() will
1897 * grab the pipe inode lock. For sendfile() emulation,
1898 * we call into ->sendpage() with the i_mutex lock held
1899 * and networking will grab the socket lock.
1902 ret = splice_to_pipe(pipe, spd);
1909 * Map data from the skb to a pipe. Should handle both the linear part,
1910 * the fragments, and the frag list. It does NOT handle frag lists within
1911 * the frag list, if such a thing exists. We'd probably need to recurse to
1912 * handle that cleanly.
1914 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1915 struct pipe_inode_info *pipe, unsigned int tlen,
1917 ssize_t (*splice_cb)(struct sock *,
1918 struct pipe_inode_info *,
1919 struct splice_pipe_desc *))
1921 struct partial_page partial[MAX_SKB_FRAGS];
1922 struct page *pages[MAX_SKB_FRAGS];
1923 struct splice_pipe_desc spd = {
1926 .nr_pages_max = MAX_SKB_FRAGS,
1928 .ops = &nosteal_pipe_buf_ops,
1929 .spd_release = sock_spd_release,
1931 struct sk_buff *frag_iter;
1935 * __skb_splice_bits() only fails if the output has no room left,
1936 * so no point in going over the frag_list for the error case.
1938 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1944 * now see if we have a frag_list to map
1946 skb_walk_frags(skb, frag_iter) {
1949 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1955 ret = splice_cb(sk, pipe, &spd);
1959 EXPORT_SYMBOL_GPL(skb_splice_bits);
1962 * skb_store_bits - store bits from kernel buffer to skb
1963 * @skb: destination buffer
1964 * @offset: offset in destination
1965 * @from: source buffer
1966 * @len: number of bytes to copy
1968 * Copy the specified number of bytes from the source buffer to the
1969 * destination skb. This function handles all the messy bits of
1970 * traversing fragment lists and such.
1973 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1975 int start = skb_headlen(skb);
1976 struct sk_buff *frag_iter;
1979 if (offset > (int)skb->len - len)
1982 if ((copy = start - offset) > 0) {
1985 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1986 if ((len -= copy) == 0)
1992 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1993 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1996 WARN_ON(start > offset + len);
1998 end = start + skb_frag_size(frag);
1999 if ((copy = end - offset) > 0) {
2005 vaddr = kmap_atomic(skb_frag_page(frag));
2006 memcpy(vaddr + frag->page_offset + offset - start,
2008 kunmap_atomic(vaddr);
2010 if ((len -= copy) == 0)
2018 skb_walk_frags(skb, frag_iter) {
2021 WARN_ON(start > offset + len);
2023 end = start + frag_iter->len;
2024 if ((copy = end - offset) > 0) {
2027 if (skb_store_bits(frag_iter, offset - start,
2030 if ((len -= copy) == 0)
2043 EXPORT_SYMBOL(skb_store_bits);
2045 /* Checksum skb data. */
2046 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2047 __wsum csum, const struct skb_checksum_ops *ops)
2049 int start = skb_headlen(skb);
2050 int i, copy = start - offset;
2051 struct sk_buff *frag_iter;
2054 /* Checksum header. */
2058 csum = ops->update(skb->data + offset, copy, csum);
2059 if ((len -= copy) == 0)
2065 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2067 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2069 WARN_ON(start > offset + len);
2071 end = start + skb_frag_size(frag);
2072 if ((copy = end - offset) > 0) {
2078 vaddr = kmap_atomic(skb_frag_page(frag));
2079 csum2 = ops->update(vaddr + frag->page_offset +
2080 offset - start, copy, 0);
2081 kunmap_atomic(vaddr);
2082 csum = ops->combine(csum, csum2, pos, copy);
2091 skb_walk_frags(skb, frag_iter) {
2094 WARN_ON(start > offset + len);
2096 end = start + frag_iter->len;
2097 if ((copy = end - offset) > 0) {
2101 csum2 = __skb_checksum(frag_iter, offset - start,
2103 csum = ops->combine(csum, csum2, pos, copy);
2104 if ((len -= copy) == 0)
2115 EXPORT_SYMBOL(__skb_checksum);
2117 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2118 int len, __wsum csum)
2120 const struct skb_checksum_ops ops = {
2121 .update = csum_partial_ext,
2122 .combine = csum_block_add_ext,
2125 return __skb_checksum(skb, offset, len, csum, &ops);
2127 EXPORT_SYMBOL(skb_checksum);
2129 /* Both of above in one bottle. */
2131 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2132 u8 *to, int len, __wsum csum)
2134 int start = skb_headlen(skb);
2135 int i, copy = start - offset;
2136 struct sk_buff *frag_iter;
2143 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2145 if ((len -= copy) == 0)
2152 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2155 WARN_ON(start > offset + len);
2157 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2158 if ((copy = end - offset) > 0) {
2161 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2165 vaddr = kmap_atomic(skb_frag_page(frag));
2166 csum2 = csum_partial_copy_nocheck(vaddr +
2170 kunmap_atomic(vaddr);
2171 csum = csum_block_add(csum, csum2, pos);
2181 skb_walk_frags(skb, frag_iter) {
2185 WARN_ON(start > offset + len);
2187 end = start + frag_iter->len;
2188 if ((copy = end - offset) > 0) {
2191 csum2 = skb_copy_and_csum_bits(frag_iter,
2194 csum = csum_block_add(csum, csum2, pos);
2195 if ((len -= copy) == 0)
2206 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2209 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2210 * @from: source buffer
2212 * Calculates the amount of linear headroom needed in the 'to' skb passed
2213 * into skb_zerocopy().
2216 skb_zerocopy_headlen(const struct sk_buff *from)
2218 unsigned int hlen = 0;
2220 if (!from->head_frag ||
2221 skb_headlen(from) < L1_CACHE_BYTES ||
2222 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2223 hlen = skb_headlen(from);
2225 if (skb_has_frag_list(from))
2230 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2233 * skb_zerocopy - Zero copy skb to skb
2234 * @to: destination buffer
2235 * @from: source buffer
2236 * @len: number of bytes to copy from source buffer
2237 * @hlen: size of linear headroom in destination buffer
2239 * Copies up to `len` bytes from `from` to `to` by creating references
2240 * to the frags in the source buffer.
2242 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2243 * headroom in the `to` buffer.
2246 * 0: everything is OK
2247 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2248 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2251 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2254 int plen = 0; /* length of skb->head fragment */
2257 unsigned int offset;
2259 BUG_ON(!from->head_frag && !hlen);
2261 /* dont bother with small payloads */
2262 if (len <= skb_tailroom(to))
2263 return skb_copy_bits(from, 0, skb_put(to, len), len);
2266 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2271 plen = min_t(int, skb_headlen(from), len);
2273 page = virt_to_head_page(from->head);
2274 offset = from->data - (unsigned char *)page_address(page);
2275 __skb_fill_page_desc(to, 0, page, offset, plen);
2282 to->truesize += len + plen;
2283 to->len += len + plen;
2284 to->data_len += len + plen;
2286 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2291 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2294 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2295 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2296 len -= skb_shinfo(to)->frags[j].size;
2297 skb_frag_ref(to, j);
2300 skb_shinfo(to)->nr_frags = j;
2304 EXPORT_SYMBOL_GPL(skb_zerocopy);
2306 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2311 if (skb->ip_summed == CHECKSUM_PARTIAL)
2312 csstart = skb_checksum_start_offset(skb);
2314 csstart = skb_headlen(skb);
2316 BUG_ON(csstart > skb_headlen(skb));
2318 skb_copy_from_linear_data(skb, to, csstart);
2321 if (csstart != skb->len)
2322 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2323 skb->len - csstart, 0);
2325 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2326 long csstuff = csstart + skb->csum_offset;
2328 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2331 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2334 * skb_dequeue - remove from the head of the queue
2335 * @list: list to dequeue from
2337 * Remove the head of the list. The list lock is taken so the function
2338 * may be used safely with other locking list functions. The head item is
2339 * returned or %NULL if the list is empty.
2342 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2344 unsigned long flags;
2345 struct sk_buff *result;
2347 spin_lock_irqsave(&list->lock, flags);
2348 result = __skb_dequeue(list);
2349 spin_unlock_irqrestore(&list->lock, flags);
2352 EXPORT_SYMBOL(skb_dequeue);
2355 * skb_dequeue_tail - remove from the tail of the queue
2356 * @list: list to dequeue from
2358 * Remove the tail of the list. The list lock is taken so the function
2359 * may be used safely with other locking list functions. The tail item is
2360 * returned or %NULL if the list is empty.
2362 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2364 unsigned long flags;
2365 struct sk_buff *result;
2367 spin_lock_irqsave(&list->lock, flags);
2368 result = __skb_dequeue_tail(list);
2369 spin_unlock_irqrestore(&list->lock, flags);
2372 EXPORT_SYMBOL(skb_dequeue_tail);
2375 * skb_queue_purge - empty a list
2376 * @list: list to empty
2378 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2379 * the list and one reference dropped. This function takes the list
2380 * lock and is atomic with respect to other list locking functions.
2382 void skb_queue_purge(struct sk_buff_head *list)
2384 struct sk_buff *skb;
2385 while ((skb = skb_dequeue(list)) != NULL)
2388 EXPORT_SYMBOL(skb_queue_purge);
2391 * skb_queue_head - queue a buffer at the list head
2392 * @list: list to use
2393 * @newsk: buffer to queue
2395 * Queue a buffer at the start of the list. This function takes the
2396 * list lock and can be used safely with other locking &sk_buff functions
2399 * A buffer cannot be placed on two lists at the same time.
2401 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2403 unsigned long flags;
2405 spin_lock_irqsave(&list->lock, flags);
2406 __skb_queue_head(list, newsk);
2407 spin_unlock_irqrestore(&list->lock, flags);
2409 EXPORT_SYMBOL(skb_queue_head);
2412 * skb_queue_tail - queue a buffer at the list tail
2413 * @list: list to use
2414 * @newsk: buffer to queue
2416 * Queue a buffer at the tail of the list. This function takes the
2417 * list lock and can be used safely with other locking &sk_buff functions
2420 * A buffer cannot be placed on two lists at the same time.
2422 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2424 unsigned long flags;
2426 spin_lock_irqsave(&list->lock, flags);
2427 __skb_queue_tail(list, newsk);
2428 spin_unlock_irqrestore(&list->lock, flags);
2430 EXPORT_SYMBOL(skb_queue_tail);
2433 * skb_unlink - remove a buffer from a list
2434 * @skb: buffer to remove
2435 * @list: list to use
2437 * Remove a packet from a list. The list locks are taken and this
2438 * function is atomic with respect to other list locked calls
2440 * You must know what list the SKB is on.
2442 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2444 unsigned long flags;
2446 spin_lock_irqsave(&list->lock, flags);
2447 __skb_unlink(skb, list);
2448 spin_unlock_irqrestore(&list->lock, flags);
2450 EXPORT_SYMBOL(skb_unlink);
2453 * skb_append - append a buffer
2454 * @old: buffer to insert after
2455 * @newsk: buffer to insert
2456 * @list: list to use
2458 * Place a packet after a given packet in a list. The list locks are taken
2459 * and this function is atomic with respect to other list locked calls.
2460 * A buffer cannot be placed on two lists at the same time.
2462 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2464 unsigned long flags;
2466 spin_lock_irqsave(&list->lock, flags);
2467 __skb_queue_after(list, old, newsk);
2468 spin_unlock_irqrestore(&list->lock, flags);
2470 EXPORT_SYMBOL(skb_append);
2473 * skb_insert - insert a buffer
2474 * @old: buffer to insert before
2475 * @newsk: buffer to insert
2476 * @list: list to use
2478 * Place a packet before a given packet in a list. The list locks are
2479 * taken and this function is atomic with respect to other list locked
2482 * A buffer cannot be placed on two lists at the same time.
2484 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2486 unsigned long flags;
2488 spin_lock_irqsave(&list->lock, flags);
2489 __skb_insert(newsk, old->prev, old, list);
2490 spin_unlock_irqrestore(&list->lock, flags);
2492 EXPORT_SYMBOL(skb_insert);
2494 static inline void skb_split_inside_header(struct sk_buff *skb,
2495 struct sk_buff* skb1,
2496 const u32 len, const int pos)
2500 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2502 /* And move data appendix as is. */
2503 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2504 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2506 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2507 skb_shinfo(skb)->nr_frags = 0;
2508 skb1->data_len = skb->data_len;
2509 skb1->len += skb1->data_len;
2512 skb_set_tail_pointer(skb, len);
2515 static inline void skb_split_no_header(struct sk_buff *skb,
2516 struct sk_buff* skb1,
2517 const u32 len, int pos)
2520 const int nfrags = skb_shinfo(skb)->nr_frags;
2522 skb_shinfo(skb)->nr_frags = 0;
2523 skb1->len = skb1->data_len = skb->len - len;
2525 skb->data_len = len - pos;
2527 for (i = 0; i < nfrags; i++) {
2528 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2530 if (pos + size > len) {
2531 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2535 * We have two variants in this case:
2536 * 1. Move all the frag to the second
2537 * part, if it is possible. F.e.
2538 * this approach is mandatory for TUX,
2539 * where splitting is expensive.
2540 * 2. Split is accurately. We make this.
2542 skb_frag_ref(skb, i);
2543 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2544 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2545 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2546 skb_shinfo(skb)->nr_frags++;
2550 skb_shinfo(skb)->nr_frags++;
2553 skb_shinfo(skb1)->nr_frags = k;
2557 * skb_split - Split fragmented skb to two parts at length len.
2558 * @skb: the buffer to split
2559 * @skb1: the buffer to receive the second part
2560 * @len: new length for skb
2562 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2564 int pos = skb_headlen(skb);
2566 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2567 if (len < pos) /* Split line is inside header. */
2568 skb_split_inside_header(skb, skb1, len, pos);
2569 else /* Second chunk has no header, nothing to copy. */
2570 skb_split_no_header(skb, skb1, len, pos);
2572 EXPORT_SYMBOL(skb_split);
2574 /* Shifting from/to a cloned skb is a no-go.
2576 * Caller cannot keep skb_shinfo related pointers past calling here!
2578 static int skb_prepare_for_shift(struct sk_buff *skb)
2580 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2584 * skb_shift - Shifts paged data partially from skb to another
2585 * @tgt: buffer into which tail data gets added
2586 * @skb: buffer from which the paged data comes from
2587 * @shiftlen: shift up to this many bytes
2589 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2590 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2591 * It's up to caller to free skb if everything was shifted.
2593 * If @tgt runs out of frags, the whole operation is aborted.
2595 * Skb cannot include anything else but paged data while tgt is allowed
2596 * to have non-paged data as well.
2598 * TODO: full sized shift could be optimized but that would need
2599 * specialized skb free'er to handle frags without up-to-date nr_frags.
2601 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2603 int from, to, merge, todo;
2604 struct skb_frag_struct *fragfrom, *fragto;
2606 BUG_ON(shiftlen > skb->len);
2607 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2611 to = skb_shinfo(tgt)->nr_frags;
2612 fragfrom = &skb_shinfo(skb)->frags[from];
2614 /* Actual merge is delayed until the point when we know we can
2615 * commit all, so that we don't have to undo partial changes
2618 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2619 fragfrom->page_offset)) {
2624 todo -= skb_frag_size(fragfrom);
2626 if (skb_prepare_for_shift(skb) ||
2627 skb_prepare_for_shift(tgt))
2630 /* All previous frag pointers might be stale! */
2631 fragfrom = &skb_shinfo(skb)->frags[from];
2632 fragto = &skb_shinfo(tgt)->frags[merge];
2634 skb_frag_size_add(fragto, shiftlen);
2635 skb_frag_size_sub(fragfrom, shiftlen);
2636 fragfrom->page_offset += shiftlen;
2644 /* Skip full, not-fitting skb to avoid expensive operations */
2645 if ((shiftlen == skb->len) &&
2646 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2649 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2652 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2653 if (to == MAX_SKB_FRAGS)
2656 fragfrom = &skb_shinfo(skb)->frags[from];
2657 fragto = &skb_shinfo(tgt)->frags[to];
2659 if (todo >= skb_frag_size(fragfrom)) {
2660 *fragto = *fragfrom;
2661 todo -= skb_frag_size(fragfrom);
2666 __skb_frag_ref(fragfrom);
2667 fragto->page = fragfrom->page;
2668 fragto->page_offset = fragfrom->page_offset;
2669 skb_frag_size_set(fragto, todo);
2671 fragfrom->page_offset += todo;
2672 skb_frag_size_sub(fragfrom, todo);
2680 /* Ready to "commit" this state change to tgt */
2681 skb_shinfo(tgt)->nr_frags = to;
2684 fragfrom = &skb_shinfo(skb)->frags[0];
2685 fragto = &skb_shinfo(tgt)->frags[merge];
2687 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2688 __skb_frag_unref(fragfrom);
2691 /* Reposition in the original skb */
2693 while (from < skb_shinfo(skb)->nr_frags)
2694 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2695 skb_shinfo(skb)->nr_frags = to;
2697 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2700 /* Most likely the tgt won't ever need its checksum anymore, skb on
2701 * the other hand might need it if it needs to be resent
2703 tgt->ip_summed = CHECKSUM_PARTIAL;
2704 skb->ip_summed = CHECKSUM_PARTIAL;
2706 /* Yak, is it really working this way? Some helper please? */
2707 skb->len -= shiftlen;
2708 skb->data_len -= shiftlen;
2709 skb->truesize -= shiftlen;
2710 tgt->len += shiftlen;
2711 tgt->data_len += shiftlen;
2712 tgt->truesize += shiftlen;
2718 * skb_prepare_seq_read - Prepare a sequential read of skb data
2719 * @skb: the buffer to read
2720 * @from: lower offset of data to be read
2721 * @to: upper offset of data to be read
2722 * @st: state variable
2724 * Initializes the specified state variable. Must be called before
2725 * invoking skb_seq_read() for the first time.
2727 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2728 unsigned int to, struct skb_seq_state *st)
2730 st->lower_offset = from;
2731 st->upper_offset = to;
2732 st->root_skb = st->cur_skb = skb;
2733 st->frag_idx = st->stepped_offset = 0;
2734 st->frag_data = NULL;
2736 EXPORT_SYMBOL(skb_prepare_seq_read);
2739 * skb_seq_read - Sequentially read skb data
2740 * @consumed: number of bytes consumed by the caller so far
2741 * @data: destination pointer for data to be returned
2742 * @st: state variable
2744 * Reads a block of skb data at @consumed relative to the
2745 * lower offset specified to skb_prepare_seq_read(). Assigns
2746 * the head of the data block to @data and returns the length
2747 * of the block or 0 if the end of the skb data or the upper
2748 * offset has been reached.
2750 * The caller is not required to consume all of the data
2751 * returned, i.e. @consumed is typically set to the number
2752 * of bytes already consumed and the next call to
2753 * skb_seq_read() will return the remaining part of the block.
2755 * Note 1: The size of each block of data returned can be arbitrary,
2756 * this limitation is the cost for zerocopy sequential
2757 * reads of potentially non linear data.
2759 * Note 2: Fragment lists within fragments are not implemented
2760 * at the moment, state->root_skb could be replaced with
2761 * a stack for this purpose.
2763 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2764 struct skb_seq_state *st)
2766 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2769 if (unlikely(abs_offset >= st->upper_offset)) {
2770 if (st->frag_data) {
2771 kunmap_atomic(st->frag_data);
2772 st->frag_data = NULL;
2778 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2780 if (abs_offset < block_limit && !st->frag_data) {
2781 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2782 return block_limit - abs_offset;
2785 if (st->frag_idx == 0 && !st->frag_data)
2786 st->stepped_offset += skb_headlen(st->cur_skb);
2788 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2789 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2790 block_limit = skb_frag_size(frag) + st->stepped_offset;
2792 if (abs_offset < block_limit) {
2794 st->frag_data = kmap_atomic(skb_frag_page(frag));
2796 *data = (u8 *) st->frag_data + frag->page_offset +
2797 (abs_offset - st->stepped_offset);
2799 return block_limit - abs_offset;
2802 if (st->frag_data) {
2803 kunmap_atomic(st->frag_data);
2804 st->frag_data = NULL;
2808 st->stepped_offset += skb_frag_size(frag);
2811 if (st->frag_data) {
2812 kunmap_atomic(st->frag_data);
2813 st->frag_data = NULL;
2816 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2817 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2820 } else if (st->cur_skb->next) {
2821 st->cur_skb = st->cur_skb->next;
2828 EXPORT_SYMBOL(skb_seq_read);
2831 * skb_abort_seq_read - Abort a sequential read of skb data
2832 * @st: state variable
2834 * Must be called if skb_seq_read() was not called until it
2837 void skb_abort_seq_read(struct skb_seq_state *st)
2840 kunmap_atomic(st->frag_data);
2842 EXPORT_SYMBOL(skb_abort_seq_read);
2844 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2846 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2847 struct ts_config *conf,
2848 struct ts_state *state)
2850 return skb_seq_read(offset, text, TS_SKB_CB(state));
2853 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2855 skb_abort_seq_read(TS_SKB_CB(state));
2859 * skb_find_text - Find a text pattern in skb data
2860 * @skb: the buffer to look in
2861 * @from: search offset
2863 * @config: textsearch configuration
2865 * Finds a pattern in the skb data according to the specified
2866 * textsearch configuration. Use textsearch_next() to retrieve
2867 * subsequent occurrences of the pattern. Returns the offset
2868 * to the first occurrence or UINT_MAX if no match was found.
2870 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2871 unsigned int to, struct ts_config *config)
2873 struct ts_state state;
2876 config->get_next_block = skb_ts_get_next_block;
2877 config->finish = skb_ts_finish;
2879 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2881 ret = textsearch_find(config, &state);
2882 return (ret <= to - from ? ret : UINT_MAX);
2884 EXPORT_SYMBOL(skb_find_text);
2887 * skb_append_datato_frags - append the user data to a skb
2888 * @sk: sock structure
2889 * @skb: skb structure to be appended with user data.
2890 * @getfrag: call back function to be used for getting the user data
2891 * @from: pointer to user message iov
2892 * @length: length of the iov message
2894 * Description: This procedure append the user data in the fragment part
2895 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2897 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2898 int (*getfrag)(void *from, char *to, int offset,
2899 int len, int odd, struct sk_buff *skb),
2900 void *from, int length)
2902 int frg_cnt = skb_shinfo(skb)->nr_frags;
2906 struct page_frag *pfrag = ¤t->task_frag;
2909 /* Return error if we don't have space for new frag */
2910 if (frg_cnt >= MAX_SKB_FRAGS)
2913 if (!sk_page_frag_refill(sk, pfrag))
2916 /* copy the user data to page */
2917 copy = min_t(int, length, pfrag->size - pfrag->offset);
2919 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2920 offset, copy, 0, skb);
2924 /* copy was successful so update the size parameters */
2925 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2928 pfrag->offset += copy;
2929 get_page(pfrag->page);
2931 skb->truesize += copy;
2932 atomic_add(copy, &sk->sk_wmem_alloc);
2934 skb->data_len += copy;
2938 } while (length > 0);
2942 EXPORT_SYMBOL(skb_append_datato_frags);
2944 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2945 int offset, size_t size)
2947 int i = skb_shinfo(skb)->nr_frags;
2949 if (skb_can_coalesce(skb, i, page, offset)) {
2950 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2951 } else if (i < MAX_SKB_FRAGS) {
2953 skb_fill_page_desc(skb, i, page, offset, size);
2960 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2963 * skb_pull_rcsum - pull skb and update receive checksum
2964 * @skb: buffer to update
2965 * @len: length of data pulled
2967 * This function performs an skb_pull on the packet and updates
2968 * the CHECKSUM_COMPLETE checksum. It should be used on
2969 * receive path processing instead of skb_pull unless you know
2970 * that the checksum difference is zero (e.g., a valid IP header)
2971 * or you are setting ip_summed to CHECKSUM_NONE.
2973 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2975 unsigned char *data = skb->data;
2977 BUG_ON(len > skb->len);
2978 __skb_pull(skb, len);
2979 skb_postpull_rcsum(skb, data, len);
2982 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2985 * skb_segment - Perform protocol segmentation on skb.
2986 * @head_skb: buffer to segment
2987 * @features: features for the output path (see dev->features)
2989 * This function performs segmentation on the given skb. It returns
2990 * a pointer to the first in a list of new skbs for the segments.
2991 * In case of error it returns ERR_PTR(err).
2993 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2994 netdev_features_t features)
2996 struct sk_buff *segs = NULL;
2997 struct sk_buff *tail = NULL;
2998 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2999 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3000 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3001 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3002 struct sk_buff *frag_skb = head_skb;
3003 unsigned int offset = doffset;
3004 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3005 unsigned int headroom;
3009 int sg = !!(features & NETIF_F_SG);
3010 int nfrags = skb_shinfo(head_skb)->nr_frags;
3016 __skb_push(head_skb, doffset);
3017 proto = skb_network_protocol(head_skb, &dummy);
3018 if (unlikely(!proto))
3019 return ERR_PTR(-EINVAL);
3021 csum = !head_skb->encap_hdr_csum &&
3022 !!can_checksum_protocol(features, proto);
3024 headroom = skb_headroom(head_skb);
3025 pos = skb_headlen(head_skb);
3028 struct sk_buff *nskb;
3029 skb_frag_t *nskb_frag;
3033 len = head_skb->len - offset;
3037 hsize = skb_headlen(head_skb) - offset;
3040 if (hsize > len || !sg)
3043 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3044 (skb_headlen(list_skb) == len || sg)) {
3045 BUG_ON(skb_headlen(list_skb) > len);
3048 nfrags = skb_shinfo(list_skb)->nr_frags;
3049 frag = skb_shinfo(list_skb)->frags;
3050 frag_skb = list_skb;
3051 pos += skb_headlen(list_skb);
3053 while (pos < offset + len) {
3054 BUG_ON(i >= nfrags);
3056 size = skb_frag_size(frag);
3057 if (pos + size > offset + len)
3065 nskb = skb_clone(list_skb, GFP_ATOMIC);
3066 list_skb = list_skb->next;
3068 if (unlikely(!nskb))
3071 if (unlikely(pskb_trim(nskb, len))) {
3076 hsize = skb_end_offset(nskb);
3077 if (skb_cow_head(nskb, doffset + headroom)) {
3082 nskb->truesize += skb_end_offset(nskb) - hsize;
3083 skb_release_head_state(nskb);
3084 __skb_push(nskb, doffset);
3086 nskb = __alloc_skb(hsize + doffset + headroom,
3087 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3090 if (unlikely(!nskb))
3093 skb_reserve(nskb, headroom);
3094 __skb_put(nskb, doffset);
3103 __copy_skb_header(nskb, head_skb);
3105 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3106 skb_reset_mac_len(nskb);
3108 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3109 nskb->data - tnl_hlen,
3110 doffset + tnl_hlen);
3112 if (nskb->len == len + doffset)
3113 goto perform_csum_check;
3115 if (!sg && !nskb->remcsum_offload) {
3116 nskb->ip_summed = CHECKSUM_NONE;
3117 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3120 SKB_GSO_CB(nskb)->csum_start =
3121 skb_headroom(nskb) + doffset;
3125 nskb_frag = skb_shinfo(nskb)->frags;
3127 skb_copy_from_linear_data_offset(head_skb, offset,
3128 skb_put(nskb, hsize), hsize);
3130 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3133 while (pos < offset + len) {
3135 BUG_ON(skb_headlen(list_skb));
3138 nfrags = skb_shinfo(list_skb)->nr_frags;
3139 frag = skb_shinfo(list_skb)->frags;
3140 frag_skb = list_skb;
3144 list_skb = list_skb->next;
3147 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3149 net_warn_ratelimited(
3150 "skb_segment: too many frags: %u %u\n",
3155 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3159 __skb_frag_ref(nskb_frag);
3160 size = skb_frag_size(nskb_frag);
3163 nskb_frag->page_offset += offset - pos;
3164 skb_frag_size_sub(nskb_frag, offset - pos);
3167 skb_shinfo(nskb)->nr_frags++;
3169 if (pos + size <= offset + len) {
3174 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3182 nskb->data_len = len - hsize;
3183 nskb->len += nskb->data_len;
3184 nskb->truesize += nskb->data_len;
3187 if (!csum && !nskb->remcsum_offload) {
3188 nskb->csum = skb_checksum(nskb, doffset,
3189 nskb->len - doffset, 0);
3190 nskb->ip_summed = CHECKSUM_NONE;
3191 SKB_GSO_CB(nskb)->csum_start =
3192 skb_headroom(nskb) + doffset;
3194 } while ((offset += len) < head_skb->len);
3196 /* Some callers want to get the end of the list.
3197 * Put it in segs->prev to avoid walking the list.
3198 * (see validate_xmit_skb_list() for example)
3202 /* Following permits correct backpressure, for protocols
3203 * using skb_set_owner_w().
3204 * Idea is to tranfert ownership from head_skb to last segment.
3206 if (head_skb->destructor == sock_wfree) {
3207 swap(tail->truesize, head_skb->truesize);
3208 swap(tail->destructor, head_skb->destructor);
3209 swap(tail->sk, head_skb->sk);
3214 kfree_skb_list(segs);
3215 return ERR_PTR(err);
3217 EXPORT_SYMBOL_GPL(skb_segment);
3219 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3221 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3222 unsigned int offset = skb_gro_offset(skb);
3223 unsigned int headlen = skb_headlen(skb);
3224 unsigned int len = skb_gro_len(skb);
3225 struct sk_buff *lp, *p = *head;
3226 unsigned int delta_truesize;
3228 if (unlikely(p->len + len >= 65536))
3231 lp = NAPI_GRO_CB(p)->last;
3232 pinfo = skb_shinfo(lp);
3234 if (headlen <= offset) {
3237 int i = skbinfo->nr_frags;
3238 int nr_frags = pinfo->nr_frags + i;
3240 if (nr_frags > MAX_SKB_FRAGS)
3244 pinfo->nr_frags = nr_frags;
3245 skbinfo->nr_frags = 0;
3247 frag = pinfo->frags + nr_frags;
3248 frag2 = skbinfo->frags + i;
3253 frag->page_offset += offset;
3254 skb_frag_size_sub(frag, offset);
3256 /* all fragments truesize : remove (head size + sk_buff) */
3257 delta_truesize = skb->truesize -
3258 SKB_TRUESIZE(skb_end_offset(skb));
3260 skb->truesize -= skb->data_len;
3261 skb->len -= skb->data_len;
3264 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3266 } else if (skb->head_frag) {
3267 int nr_frags = pinfo->nr_frags;
3268 skb_frag_t *frag = pinfo->frags + nr_frags;
3269 struct page *page = virt_to_head_page(skb->head);
3270 unsigned int first_size = headlen - offset;
3271 unsigned int first_offset;
3273 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3276 first_offset = skb->data -
3277 (unsigned char *)page_address(page) +
3280 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3282 frag->page.p = page;
3283 frag->page_offset = first_offset;
3284 skb_frag_size_set(frag, first_size);
3286 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3287 /* We dont need to clear skbinfo->nr_frags here */
3289 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3290 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3295 delta_truesize = skb->truesize;
3296 if (offset > headlen) {
3297 unsigned int eat = offset - headlen;
3299 skbinfo->frags[0].page_offset += eat;
3300 skb_frag_size_sub(&skbinfo->frags[0], eat);
3301 skb->data_len -= eat;
3306 __skb_pull(skb, offset);
3308 if (NAPI_GRO_CB(p)->last == p)
3309 skb_shinfo(p)->frag_list = skb;
3311 NAPI_GRO_CB(p)->last->next = skb;
3312 NAPI_GRO_CB(p)->last = skb;
3313 __skb_header_release(skb);
3317 NAPI_GRO_CB(p)->count++;
3319 p->truesize += delta_truesize;
3322 lp->data_len += len;
3323 lp->truesize += delta_truesize;
3326 NAPI_GRO_CB(skb)->same_flow = 1;
3330 void __init skb_init(void)
3332 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3333 sizeof(struct sk_buff),
3335 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3337 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3338 sizeof(struct sk_buff_fclones),
3340 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3345 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3346 * @skb: Socket buffer containing the buffers to be mapped
3347 * @sg: The scatter-gather list to map into
3348 * @offset: The offset into the buffer's contents to start mapping
3349 * @len: Length of buffer space to be mapped
3351 * Fill the specified scatter-gather list with mappings/pointers into a
3352 * region of the buffer space attached to a socket buffer.
3355 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3357 int start = skb_headlen(skb);
3358 int i, copy = start - offset;
3359 struct sk_buff *frag_iter;
3365 sg_set_buf(sg, skb->data + offset, copy);
3367 if ((len -= copy) == 0)
3372 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3375 WARN_ON(start > offset + len);
3377 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3378 if ((copy = end - offset) > 0) {
3379 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3383 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3384 frag->page_offset+offset-start);
3393 skb_walk_frags(skb, frag_iter) {
3396 WARN_ON(start > offset + len);
3398 end = start + frag_iter->len;
3399 if ((copy = end - offset) > 0) {
3402 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3404 if ((len -= copy) == 0)
3414 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3415 * sglist without mark the sg which contain last skb data as the end.
3416 * So the caller can mannipulate sg list as will when padding new data after
3417 * the first call without calling sg_unmark_end to expend sg list.
3419 * Scenario to use skb_to_sgvec_nomark:
3421 * 2. skb_to_sgvec_nomark(payload1)
3422 * 3. skb_to_sgvec_nomark(payload2)
3424 * This is equivalent to:
3426 * 2. skb_to_sgvec(payload1)
3428 * 4. skb_to_sgvec(payload2)
3430 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3431 * is more preferable.
3433 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3434 int offset, int len)
3436 return __skb_to_sgvec(skb, sg, offset, len);
3438 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3440 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3442 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3444 sg_mark_end(&sg[nsg - 1]);
3448 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3451 * skb_cow_data - Check that a socket buffer's data buffers are writable
3452 * @skb: The socket buffer to check.
3453 * @tailbits: Amount of trailing space to be added
3454 * @trailer: Returned pointer to the skb where the @tailbits space begins
3456 * Make sure that the data buffers attached to a socket buffer are
3457 * writable. If they are not, private copies are made of the data buffers
3458 * and the socket buffer is set to use these instead.
3460 * If @tailbits is given, make sure that there is space to write @tailbits
3461 * bytes of data beyond current end of socket buffer. @trailer will be
3462 * set to point to the skb in which this space begins.
3464 * The number of scatterlist elements required to completely map the
3465 * COW'd and extended socket buffer will be returned.
3467 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3471 struct sk_buff *skb1, **skb_p;
3473 /* If skb is cloned or its head is paged, reallocate
3474 * head pulling out all the pages (pages are considered not writable
3475 * at the moment even if they are anonymous).
3477 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3478 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3481 /* Easy case. Most of packets will go this way. */
3482 if (!skb_has_frag_list(skb)) {
3483 /* A little of trouble, not enough of space for trailer.
3484 * This should not happen, when stack is tuned to generate
3485 * good frames. OK, on miss we reallocate and reserve even more
3486 * space, 128 bytes is fair. */
3488 if (skb_tailroom(skb) < tailbits &&
3489 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3497 /* Misery. We are in troubles, going to mincer fragments... */
3500 skb_p = &skb_shinfo(skb)->frag_list;
3503 while ((skb1 = *skb_p) != NULL) {
3506 /* The fragment is partially pulled by someone,
3507 * this can happen on input. Copy it and everything
3510 if (skb_shared(skb1))
3513 /* If the skb is the last, worry about trailer. */
3515 if (skb1->next == NULL && tailbits) {
3516 if (skb_shinfo(skb1)->nr_frags ||
3517 skb_has_frag_list(skb1) ||
3518 skb_tailroom(skb1) < tailbits)
3519 ntail = tailbits + 128;
3525 skb_shinfo(skb1)->nr_frags ||
3526 skb_has_frag_list(skb1)) {
3527 struct sk_buff *skb2;
3529 /* Fuck, we are miserable poor guys... */
3531 skb2 = skb_copy(skb1, GFP_ATOMIC);
3533 skb2 = skb_copy_expand(skb1,
3537 if (unlikely(skb2 == NULL))
3541 skb_set_owner_w(skb2, skb1->sk);
3543 /* Looking around. Are we still alive?
3544 * OK, link new skb, drop old one */
3546 skb2->next = skb1->next;
3553 skb_p = &skb1->next;
3558 EXPORT_SYMBOL_GPL(skb_cow_data);
3560 static void sock_rmem_free(struct sk_buff *skb)
3562 struct sock *sk = skb->sk;
3564 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3568 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3570 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3572 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3573 (unsigned int)sk->sk_rcvbuf)
3578 skb->destructor = sock_rmem_free;
3579 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3581 /* before exiting rcu section, make sure dst is refcounted */
3584 skb_queue_tail(&sk->sk_error_queue, skb);
3585 if (!sock_flag(sk, SOCK_DEAD))
3586 sk->sk_data_ready(sk);
3589 EXPORT_SYMBOL(sock_queue_err_skb);
3591 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3593 struct sk_buff_head *q = &sk->sk_error_queue;
3594 struct sk_buff *skb, *skb_next;
3595 unsigned long flags;
3598 spin_lock_irqsave(&q->lock, flags);
3599 skb = __skb_dequeue(q);
3600 if (skb && (skb_next = skb_peek(q)))
3601 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3602 spin_unlock_irqrestore(&q->lock, flags);
3606 sk->sk_error_report(sk);
3610 EXPORT_SYMBOL(sock_dequeue_err_skb);
3613 * skb_clone_sk - create clone of skb, and take reference to socket
3614 * @skb: the skb to clone
3616 * This function creates a clone of a buffer that holds a reference on
3617 * sk_refcnt. Buffers created via this function are meant to be
3618 * returned using sock_queue_err_skb, or free via kfree_skb.
3620 * When passing buffers allocated with this function to sock_queue_err_skb
3621 * it is necessary to wrap the call with sock_hold/sock_put in order to
3622 * prevent the socket from being released prior to being enqueued on
3623 * the sk_error_queue.
3625 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3627 struct sock *sk = skb->sk;
3628 struct sk_buff *clone;
3630 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3633 clone = skb_clone(skb, GFP_ATOMIC);
3640 clone->destructor = sock_efree;
3644 EXPORT_SYMBOL(skb_clone_sk);
3646 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3650 struct sock_exterr_skb *serr;
3653 serr = SKB_EXT_ERR(skb);
3654 memset(serr, 0, sizeof(*serr));
3655 serr->ee.ee_errno = ENOMSG;
3656 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3657 serr->ee.ee_info = tstype;
3658 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3659 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3660 if (sk->sk_protocol == IPPROTO_TCP &&
3661 sk->sk_type == SOCK_STREAM)
3662 serr->ee.ee_data -= sk->sk_tskey;
3665 err = sock_queue_err_skb(sk, skb);
3671 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3675 if (likely(sysctl_tstamp_allow_data || tsonly))
3678 read_lock_bh(&sk->sk_callback_lock);
3679 ret = sk->sk_socket && sk->sk_socket->file &&
3680 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3681 read_unlock_bh(&sk->sk_callback_lock);
3685 void skb_complete_tx_timestamp(struct sk_buff *skb,
3686 struct skb_shared_hwtstamps *hwtstamps)
3688 struct sock *sk = skb->sk;
3690 if (!skb_may_tx_timestamp(sk, false))
3693 /* take a reference to prevent skb_orphan() from freeing the socket */
3696 *skb_hwtstamps(skb) = *hwtstamps;
3697 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3701 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3703 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3704 struct skb_shared_hwtstamps *hwtstamps,
3705 struct sock *sk, int tstype)
3707 struct sk_buff *skb;
3713 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3714 if (!skb_may_tx_timestamp(sk, tsonly))
3718 skb = alloc_skb(0, GFP_ATOMIC);
3720 skb = skb_clone(orig_skb, GFP_ATOMIC);
3725 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3726 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3730 *skb_hwtstamps(skb) = *hwtstamps;
3732 skb->tstamp = ktime_get_real();
3734 __skb_complete_tx_timestamp(skb, sk, tstype);
3736 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3738 void skb_tstamp_tx(struct sk_buff *orig_skb,
3739 struct skb_shared_hwtstamps *hwtstamps)
3741 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3744 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3746 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3748 struct sock *sk = skb->sk;
3749 struct sock_exterr_skb *serr;
3752 skb->wifi_acked_valid = 1;
3753 skb->wifi_acked = acked;
3755 serr = SKB_EXT_ERR(skb);
3756 memset(serr, 0, sizeof(*serr));
3757 serr->ee.ee_errno = ENOMSG;
3758 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3760 /* take a reference to prevent skb_orphan() from freeing the socket */
3763 err = sock_queue_err_skb(sk, skb);
3769 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3772 * skb_partial_csum_set - set up and verify partial csum values for packet
3773 * @skb: the skb to set
3774 * @start: the number of bytes after skb->data to start checksumming.
3775 * @off: the offset from start to place the checksum.
3777 * For untrusted partially-checksummed packets, we need to make sure the values
3778 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3780 * This function checks and sets those values and skb->ip_summed: if this
3781 * returns false you should drop the packet.
3783 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3785 if (unlikely(start > skb_headlen(skb)) ||
3786 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3787 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3788 start, off, skb_headlen(skb));
3791 skb->ip_summed = CHECKSUM_PARTIAL;
3792 skb->csum_start = skb_headroom(skb) + start;
3793 skb->csum_offset = off;
3794 skb_set_transport_header(skb, start);
3797 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3799 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3802 if (skb_headlen(skb) >= len)
3805 /* If we need to pullup then pullup to the max, so we
3806 * won't need to do it again.
3811 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3814 if (skb_headlen(skb) < len)
3820 #define MAX_TCP_HDR_LEN (15 * 4)
3822 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3823 typeof(IPPROTO_IP) proto,
3830 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3831 off + MAX_TCP_HDR_LEN);
3832 if (!err && !skb_partial_csum_set(skb, off,
3833 offsetof(struct tcphdr,
3836 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3839 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3840 off + sizeof(struct udphdr));
3841 if (!err && !skb_partial_csum_set(skb, off,
3842 offsetof(struct udphdr,
3845 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3848 return ERR_PTR(-EPROTO);
3851 /* This value should be large enough to cover a tagged ethernet header plus
3852 * maximally sized IP and TCP or UDP headers.
3854 #define MAX_IP_HDR_LEN 128
3856 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3865 err = skb_maybe_pull_tail(skb,
3866 sizeof(struct iphdr),
3871 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3874 off = ip_hdrlen(skb);
3881 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3883 return PTR_ERR(csum);
3886 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3889 ip_hdr(skb)->protocol, 0);
3896 /* This value should be large enough to cover a tagged ethernet header plus
3897 * an IPv6 header, all options, and a maximal TCP or UDP header.
3899 #define MAX_IPV6_HDR_LEN 256
3901 #define OPT_HDR(type, skb, off) \
3902 (type *)(skb_network_header(skb) + (off))
3904 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3917 off = sizeof(struct ipv6hdr);
3919 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3923 nexthdr = ipv6_hdr(skb)->nexthdr;
3925 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3926 while (off <= len && !done) {
3928 case IPPROTO_DSTOPTS:
3929 case IPPROTO_HOPOPTS:
3930 case IPPROTO_ROUTING: {
3931 struct ipv6_opt_hdr *hp;
3933 err = skb_maybe_pull_tail(skb,
3935 sizeof(struct ipv6_opt_hdr),
3940 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3941 nexthdr = hp->nexthdr;
3942 off += ipv6_optlen(hp);
3946 struct ip_auth_hdr *hp;
3948 err = skb_maybe_pull_tail(skb,
3950 sizeof(struct ip_auth_hdr),
3955 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3956 nexthdr = hp->nexthdr;
3957 off += ipv6_authlen(hp);
3960 case IPPROTO_FRAGMENT: {
3961 struct frag_hdr *hp;
3963 err = skb_maybe_pull_tail(skb,
3965 sizeof(struct frag_hdr),
3970 hp = OPT_HDR(struct frag_hdr, skb, off);
3972 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3975 nexthdr = hp->nexthdr;
3976 off += sizeof(struct frag_hdr);
3987 if (!done || fragment)
3990 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3992 return PTR_ERR(csum);
3995 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3996 &ipv6_hdr(skb)->daddr,
3997 skb->len - off, nexthdr, 0);
4005 * skb_checksum_setup - set up partial checksum offset
4006 * @skb: the skb to set up
4007 * @recalculate: if true the pseudo-header checksum will be recalculated
4009 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4013 switch (skb->protocol) {
4014 case htons(ETH_P_IP):
4015 err = skb_checksum_setup_ipv4(skb, recalculate);
4018 case htons(ETH_P_IPV6):
4019 err = skb_checksum_setup_ipv6(skb, recalculate);
4029 EXPORT_SYMBOL(skb_checksum_setup);
4032 * skb_checksum_maybe_trim - maybe trims the given skb
4033 * @skb: the skb to check
4034 * @transport_len: the data length beyond the network header
4036 * Checks whether the given skb has data beyond the given transport length.
4037 * If so, returns a cloned skb trimmed to this transport length.
4038 * Otherwise returns the provided skb. Returns NULL in error cases
4039 * (e.g. transport_len exceeds skb length or out-of-memory).
4041 * Caller needs to set the skb transport header and free any returned skb if it
4042 * differs from the provided skb.
4044 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4045 unsigned int transport_len)
4047 struct sk_buff *skb_chk;
4048 unsigned int len = skb_transport_offset(skb) + transport_len;
4053 else if (skb->len == len)
4056 skb_chk = skb_clone(skb, GFP_ATOMIC);
4060 ret = pskb_trim_rcsum(skb_chk, len);
4070 * skb_checksum_trimmed - validate checksum of an skb
4071 * @skb: the skb to check
4072 * @transport_len: the data length beyond the network header
4073 * @skb_chkf: checksum function to use
4075 * Applies the given checksum function skb_chkf to the provided skb.
4076 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4078 * If the skb has data beyond the given transport length, then a
4079 * trimmed & cloned skb is checked and returned.
4081 * Caller needs to set the skb transport header and free any returned skb if it
4082 * differs from the provided skb.
4084 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4085 unsigned int transport_len,
4086 __sum16(*skb_chkf)(struct sk_buff *skb))
4088 struct sk_buff *skb_chk;
4089 unsigned int offset = skb_transport_offset(skb);
4092 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4096 if (!pskb_may_pull(skb_chk, offset))
4099 skb_pull_rcsum(skb_chk, offset);
4100 ret = skb_chkf(skb_chk);
4101 skb_push_rcsum(skb_chk, offset);
4109 if (skb_chk && skb_chk != skb)
4115 EXPORT_SYMBOL(skb_checksum_trimmed);
4117 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4119 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4122 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4124 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4127 skb_release_head_state(skb);
4128 kmem_cache_free(skbuff_head_cache, skb);
4133 EXPORT_SYMBOL(kfree_skb_partial);
4136 * skb_try_coalesce - try to merge skb to prior one
4138 * @from: buffer to add
4139 * @fragstolen: pointer to boolean
4140 * @delta_truesize: how much more was allocated than was requested
4142 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4143 bool *fragstolen, int *delta_truesize)
4145 int i, delta, len = from->len;
4147 *fragstolen = false;
4152 if (len <= skb_tailroom(to)) {
4154 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4155 *delta_truesize = 0;
4159 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4162 if (skb_headlen(from) != 0) {
4164 unsigned int offset;
4166 if (skb_shinfo(to)->nr_frags +
4167 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4170 if (skb_head_is_locked(from))
4173 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4175 page = virt_to_head_page(from->head);
4176 offset = from->data - (unsigned char *)page_address(page);
4178 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4179 page, offset, skb_headlen(from));
4182 if (skb_shinfo(to)->nr_frags +
4183 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4186 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4189 WARN_ON_ONCE(delta < len);
4191 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4192 skb_shinfo(from)->frags,
4193 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4194 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4196 if (!skb_cloned(from))
4197 skb_shinfo(from)->nr_frags = 0;
4199 /* if the skb is not cloned this does nothing
4200 * since we set nr_frags to 0.
4202 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4203 skb_frag_ref(from, i);
4205 to->truesize += delta;
4207 to->data_len += len;
4209 *delta_truesize = delta;
4212 EXPORT_SYMBOL(skb_try_coalesce);
4215 * skb_scrub_packet - scrub an skb
4217 * @skb: buffer to clean
4218 * @xnet: packet is crossing netns
4220 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4221 * into/from a tunnel. Some information have to be cleared during these
4223 * skb_scrub_packet can also be used to clean a skb before injecting it in
4224 * another namespace (@xnet == true). We have to clear all information in the
4225 * skb that could impact namespace isolation.
4227 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4229 skb->tstamp.tv64 = 0;
4230 skb->pkt_type = PACKET_HOST;
4234 skb_sender_cpu_clear(skb);
4237 nf_reset_trace(skb);
4245 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4248 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4252 * skb_gso_transport_seglen is used to determine the real size of the
4253 * individual segments, including Layer4 headers (TCP/UDP).
4255 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4257 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4259 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4260 unsigned int thlen = 0;
4262 if (skb->encapsulation) {
4263 thlen = skb_inner_transport_header(skb) -
4264 skb_transport_header(skb);
4266 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4267 thlen += inner_tcp_hdrlen(skb);
4268 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4269 thlen = tcp_hdrlen(skb);
4271 /* UFO sets gso_size to the size of the fragmentation
4272 * payload, i.e. the size of the L4 (UDP) header is already
4275 return thlen + shinfo->gso_size;
4277 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4279 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4281 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4286 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4288 skb->mac_header += VLAN_HLEN;
4292 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4294 struct vlan_hdr *vhdr;
4297 if (unlikely(skb_vlan_tag_present(skb))) {
4298 /* vlan_tci is already set-up so leave this for another time */
4302 skb = skb_share_check(skb, GFP_ATOMIC);
4306 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4309 vhdr = (struct vlan_hdr *)skb->data;
4310 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4311 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4313 skb_pull_rcsum(skb, VLAN_HLEN);
4314 vlan_set_encap_proto(skb, vhdr);
4316 skb = skb_reorder_vlan_header(skb);
4320 skb_reset_network_header(skb);
4321 skb_reset_transport_header(skb);
4322 skb_reset_mac_len(skb);
4330 EXPORT_SYMBOL(skb_vlan_untag);
4332 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4334 if (!pskb_may_pull(skb, write_len))
4337 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4340 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4342 EXPORT_SYMBOL(skb_ensure_writable);
4344 /* remove VLAN header from packet and update csum accordingly. */
4345 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4347 struct vlan_hdr *vhdr;
4348 unsigned int offset = skb->data - skb_mac_header(skb);
4351 __skb_push(skb, offset);
4352 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4356 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4358 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4359 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4361 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4362 __skb_pull(skb, VLAN_HLEN);
4364 vlan_set_encap_proto(skb, vhdr);
4365 skb->mac_header += VLAN_HLEN;
4367 if (skb_network_offset(skb) < ETH_HLEN)
4368 skb_set_network_header(skb, ETH_HLEN);
4370 skb_reset_mac_len(skb);
4372 __skb_pull(skb, offset);
4377 int skb_vlan_pop(struct sk_buff *skb)
4383 if (likely(skb_vlan_tag_present(skb))) {
4386 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4387 skb->protocol != htons(ETH_P_8021AD)) ||
4388 skb->len < VLAN_ETH_HLEN))
4391 err = __skb_vlan_pop(skb, &vlan_tci);
4395 /* move next vlan tag to hw accel tag */
4396 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4397 skb->protocol != htons(ETH_P_8021AD)) ||
4398 skb->len < VLAN_ETH_HLEN))
4401 vlan_proto = skb->protocol;
4402 err = __skb_vlan_pop(skb, &vlan_tci);
4406 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4409 EXPORT_SYMBOL(skb_vlan_pop);
4411 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4413 if (skb_vlan_tag_present(skb)) {
4414 unsigned int offset = skb->data - skb_mac_header(skb);
4417 /* __vlan_insert_tag expect skb->data pointing to mac header.
4418 * So change skb->data before calling it and change back to
4419 * original position later
4421 __skb_push(skb, offset);
4422 err = __vlan_insert_tag(skb, skb->vlan_proto,
4423 skb_vlan_tag_get(skb));
4425 __skb_pull(skb, offset);
4429 skb->protocol = skb->vlan_proto;
4430 skb->mac_len += VLAN_HLEN;
4432 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4433 __skb_pull(skb, offset);
4435 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4438 EXPORT_SYMBOL(skb_vlan_push);
4441 * alloc_skb_with_frags - allocate skb with page frags
4443 * @header_len: size of linear part
4444 * @data_len: needed length in frags
4445 * @max_page_order: max page order desired.
4446 * @errcode: pointer to error code if any
4447 * @gfp_mask: allocation mask
4449 * This can be used to allocate a paged skb, given a maximal order for frags.
4451 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4452 unsigned long data_len,
4457 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4458 unsigned long chunk;
4459 struct sk_buff *skb;
4464 *errcode = -EMSGSIZE;
4465 /* Note this test could be relaxed, if we succeed to allocate
4466 * high order pages...
4468 if (npages > MAX_SKB_FRAGS)
4471 gfp_head = gfp_mask;
4472 if (gfp_head & __GFP_DIRECT_RECLAIM)
4473 gfp_head |= __GFP_REPEAT;
4475 *errcode = -ENOBUFS;
4476 skb = alloc_skb(header_len, gfp_head);
4480 skb->truesize += npages << PAGE_SHIFT;
4482 for (i = 0; npages > 0; i++) {
4483 int order = max_page_order;
4486 if (npages >= 1 << order) {
4487 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4494 /* Do not retry other high order allocations */
4500 page = alloc_page(gfp_mask);
4504 chunk = min_t(unsigned long, data_len,
4505 PAGE_SIZE << order);
4506 skb_fill_page_desc(skb, i, page, 0, chunk);
4508 npages -= 1 << order;
4516 EXPORT_SYMBOL(alloc_skb_with_frags);