2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83 EXPORT_SYMBOL(sysctl_max_skb_frags);
86 * skb_panic - private function for out-of-line support
90 * @msg: skb_over_panic or skb_under_panic
92 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 msg, addr, skb->len, sz, skb->head, skb->data,
102 (unsigned long)skb->tail, (unsigned long)skb->end,
103 skb->dev ? skb->dev->name : "<NULL>");
107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109 skb_panic(skb, sz, addr, __func__);
112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114 skb_panic(skb, sz, addr, __func__);
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 unsigned long ip, bool *pfmemalloc)
131 bool ret_pfmemalloc = false;
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
137 obj = kmalloc_node_track_caller(size,
138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 if (obj || !(gfp_pfmemalloc_allowed(flags)))
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc = true;
145 obj = kmalloc_node_track_caller(size, flags, node);
149 *pfmemalloc = ret_pfmemalloc;
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
160 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
165 skb = kmem_cache_alloc_node(skbuff_head_cache,
166 gfp_mask & ~__GFP_DMA, node);
171 * Only clear those fields we need to clear, not those that we will
172 * actually initialise below. Hence, don't put any more fields after
173 * the tail pointer in struct sk_buff!
175 memset(skb, 0, offsetof(struct sk_buff, tail));
177 skb->truesize = sizeof(struct sk_buff);
178 atomic_set(&skb->users, 1);
180 skb->mac_header = (typeof(skb->mac_header))~0U;
186 * __alloc_skb - allocate a network buffer
187 * @size: size to allocate
188 * @gfp_mask: allocation mask
189 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
190 * instead of head cache and allocate a cloned (child) skb.
191 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
192 * allocations in case the data is required for writeback
193 * @node: numa node to allocate memory on
195 * Allocate a new &sk_buff. The returned buffer has no headroom and a
196 * tail room of at least size bytes. The object has a reference count
197 * of one. The return is the buffer. On a failure the return is %NULL.
199 * Buffers may only be allocated from interrupts using a @gfp_mask of
202 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
205 struct kmem_cache *cache;
206 struct skb_shared_info *shinfo;
211 cache = (flags & SKB_ALLOC_FCLONE)
212 ? skbuff_fclone_cache : skbuff_head_cache;
214 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
215 gfp_mask |= __GFP_MEMALLOC;
218 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
223 /* We do our best to align skb_shared_info on a separate cache
224 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
225 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
226 * Both skb->head and skb_shared_info are cache line aligned.
228 size = SKB_DATA_ALIGN(size);
229 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
230 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
233 /* kmalloc(size) might give us more room than requested.
234 * Put skb_shared_info exactly at the end of allocated zone,
235 * to allow max possible filling before reallocation.
237 size = SKB_WITH_OVERHEAD(ksize(data));
238 prefetchw(data + size);
241 * Only clear those fields we need to clear, not those that we will
242 * actually initialise below. Hence, don't put any more fields after
243 * the tail pointer in struct sk_buff!
245 memset(skb, 0, offsetof(struct sk_buff, tail));
246 /* Account for allocated memory : skb + skb->head */
247 skb->truesize = SKB_TRUESIZE(size);
248 skb->pfmemalloc = pfmemalloc;
249 atomic_set(&skb->users, 1);
252 skb_reset_tail_pointer(skb);
253 skb->end = skb->tail + size;
254 skb->mac_header = (typeof(skb->mac_header))~0U;
255 skb->transport_header = (typeof(skb->transport_header))~0U;
257 /* make sure we initialize shinfo sequentially */
258 shinfo = skb_shinfo(skb);
259 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
260 atomic_set(&shinfo->dataref, 1);
261 kmemcheck_annotate_variable(shinfo->destructor_arg);
263 if (flags & SKB_ALLOC_FCLONE) {
264 struct sk_buff_fclones *fclones;
266 fclones = container_of(skb, struct sk_buff_fclones, skb1);
268 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
269 skb->fclone = SKB_FCLONE_ORIG;
270 atomic_set(&fclones->fclone_ref, 1);
272 fclones->skb2.fclone = SKB_FCLONE_CLONE;
273 fclones->skb2.pfmemalloc = pfmemalloc;
278 kmem_cache_free(cache, skb);
282 EXPORT_SYMBOL(__alloc_skb);
285 * __build_skb - build a network buffer
286 * @data: data buffer provided by caller
287 * @frag_size: size of data, or 0 if head was kmalloced
289 * Allocate a new &sk_buff. Caller provides space holding head and
290 * skb_shared_info. @data must have been allocated by kmalloc() only if
291 * @frag_size is 0, otherwise data should come from the page allocator
293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
303 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
305 struct skb_shared_info *shinfo;
307 unsigned int size = frag_size ? : ksize(data);
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
334 /* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
339 struct sk_buff *build_skb(void *data, unsigned int frag_size)
341 struct sk_buff *skb = __build_skb(data, frag_size);
343 if (skb && frag_size) {
345 if (page_is_pfmemalloc(virt_to_head_page(data)))
350 EXPORT_SYMBOL(build_skb);
352 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
353 static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
355 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
357 struct page_frag_cache *nc;
361 local_irq_save(flags);
362 nc = this_cpu_ptr(&netdev_alloc_cache);
363 data = __alloc_page_frag(nc, fragsz, gfp_mask);
364 local_irq_restore(flags);
369 * netdev_alloc_frag - allocate a page fragment
370 * @fragsz: fragment size
372 * Allocates a frag from a page for receive buffer.
373 * Uses GFP_ATOMIC allocations.
375 void *netdev_alloc_frag(unsigned int fragsz)
377 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
379 EXPORT_SYMBOL(netdev_alloc_frag);
381 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
383 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
385 return __alloc_page_frag(nc, fragsz, gfp_mask);
388 void *napi_alloc_frag(unsigned int fragsz)
390 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
392 EXPORT_SYMBOL(napi_alloc_frag);
395 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
396 * @dev: network device to receive on
397 * @len: length to allocate
398 * @gfp_mask: get_free_pages mask, passed to alloc_skb
400 * Allocate a new &sk_buff and assign it a usage count of one. The
401 * buffer has NET_SKB_PAD headroom built in. Users should allocate
402 * the headroom they think they need without accounting for the
403 * built in space. The built in space is used for optimisations.
405 * %NULL is returned if there is no free memory.
407 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
410 struct page_frag_cache *nc;
418 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
419 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
420 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
426 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
427 len = SKB_DATA_ALIGN(len);
429 if (sk_memalloc_socks())
430 gfp_mask |= __GFP_MEMALLOC;
432 local_irq_save(flags);
434 nc = this_cpu_ptr(&netdev_alloc_cache);
435 data = __alloc_page_frag(nc, len, gfp_mask);
436 pfmemalloc = nc->pfmemalloc;
438 local_irq_restore(flags);
443 skb = __build_skb(data, len);
444 if (unlikely(!skb)) {
449 /* use OR instead of assignment to avoid clearing of bits in mask */
455 skb_reserve(skb, NET_SKB_PAD);
461 EXPORT_SYMBOL(__netdev_alloc_skb);
464 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
465 * @napi: napi instance this buffer was allocated for
466 * @len: length to allocate
467 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
469 * Allocate a new sk_buff for use in NAPI receive. This buffer will
470 * attempt to allocate the head from a special reserved region used
471 * only for NAPI Rx allocation. By doing this we can save several
472 * CPU cycles by avoiding having to disable and re-enable IRQs.
474 * %NULL is returned if there is no free memory.
476 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
479 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
483 len += NET_SKB_PAD + NET_IP_ALIGN;
485 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
486 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
487 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
493 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
494 len = SKB_DATA_ALIGN(len);
496 if (sk_memalloc_socks())
497 gfp_mask |= __GFP_MEMALLOC;
499 data = __alloc_page_frag(nc, len, gfp_mask);
503 skb = __build_skb(data, len);
504 if (unlikely(!skb)) {
509 /* use OR instead of assignment to avoid clearing of bits in mask */
515 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
516 skb->dev = napi->dev;
521 EXPORT_SYMBOL(__napi_alloc_skb);
523 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
524 int size, unsigned int truesize)
526 skb_fill_page_desc(skb, i, page, off, size);
528 skb->data_len += size;
529 skb->truesize += truesize;
531 EXPORT_SYMBOL(skb_add_rx_frag);
533 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
534 unsigned int truesize)
536 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
538 skb_frag_size_add(frag, size);
540 skb->data_len += size;
541 skb->truesize += truesize;
543 EXPORT_SYMBOL(skb_coalesce_rx_frag);
545 static void skb_drop_list(struct sk_buff **listp)
547 kfree_skb_list(*listp);
551 static inline void skb_drop_fraglist(struct sk_buff *skb)
553 skb_drop_list(&skb_shinfo(skb)->frag_list);
556 static void skb_clone_fraglist(struct sk_buff *skb)
558 struct sk_buff *list;
560 skb_walk_frags(skb, list)
564 static void skb_free_head(struct sk_buff *skb)
566 unsigned char *head = skb->head;
574 static void skb_release_data(struct sk_buff *skb)
576 struct skb_shared_info *shinfo = skb_shinfo(skb);
580 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
584 for (i = 0; i < shinfo->nr_frags; i++)
585 __skb_frag_unref(&shinfo->frags[i]);
588 * If skb buf is from userspace, we need to notify the caller
589 * the lower device DMA has done;
591 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
592 struct ubuf_info *uarg;
594 uarg = shinfo->destructor_arg;
596 uarg->callback(uarg, true);
599 if (shinfo->frag_list)
600 kfree_skb_list(shinfo->frag_list);
606 * Free an skbuff by memory without cleaning the state.
608 static void kfree_skbmem(struct sk_buff *skb)
610 struct sk_buff_fclones *fclones;
612 switch (skb->fclone) {
613 case SKB_FCLONE_UNAVAILABLE:
614 kmem_cache_free(skbuff_head_cache, skb);
617 case SKB_FCLONE_ORIG:
618 fclones = container_of(skb, struct sk_buff_fclones, skb1);
620 /* We usually free the clone (TX completion) before original skb
621 * This test would have no chance to be true for the clone,
622 * while here, branch prediction will be good.
624 if (atomic_read(&fclones->fclone_ref) == 1)
628 default: /* SKB_FCLONE_CLONE */
629 fclones = container_of(skb, struct sk_buff_fclones, skb2);
632 if (!atomic_dec_and_test(&fclones->fclone_ref))
635 kmem_cache_free(skbuff_fclone_cache, fclones);
638 static void skb_release_head_state(struct sk_buff *skb)
642 secpath_put(skb->sp);
644 if (skb->destructor) {
646 skb->destructor(skb);
648 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
649 nf_conntrack_put(skb->nfct);
651 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
652 nf_bridge_put(skb->nf_bridge);
656 /* Free everything but the sk_buff shell. */
657 static void skb_release_all(struct sk_buff *skb)
659 skb_release_head_state(skb);
660 if (likely(skb->head))
661 skb_release_data(skb);
665 * __kfree_skb - private function
668 * Free an sk_buff. Release anything attached to the buffer.
669 * Clean the state. This is an internal helper function. Users should
670 * always call kfree_skb
673 void __kfree_skb(struct sk_buff *skb)
675 skb_release_all(skb);
678 EXPORT_SYMBOL(__kfree_skb);
681 * kfree_skb - free an sk_buff
682 * @skb: buffer to free
684 * Drop a reference to the buffer and free it if the usage count has
687 void kfree_skb(struct sk_buff *skb)
691 if (likely(atomic_read(&skb->users) == 1))
693 else if (likely(!atomic_dec_and_test(&skb->users)))
695 trace_kfree_skb(skb, __builtin_return_address(0));
698 EXPORT_SYMBOL(kfree_skb);
700 void kfree_skb_list(struct sk_buff *segs)
703 struct sk_buff *next = segs->next;
709 EXPORT_SYMBOL(kfree_skb_list);
712 * skb_tx_error - report an sk_buff xmit error
713 * @skb: buffer that triggered an error
715 * Report xmit error if a device callback is tracking this skb.
716 * skb must be freed afterwards.
718 void skb_tx_error(struct sk_buff *skb)
720 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
721 struct ubuf_info *uarg;
723 uarg = skb_shinfo(skb)->destructor_arg;
725 uarg->callback(uarg, false);
726 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
729 EXPORT_SYMBOL(skb_tx_error);
732 * consume_skb - free an skbuff
733 * @skb: buffer to free
735 * Drop a ref to the buffer and free it if the usage count has hit zero
736 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
737 * is being dropped after a failure and notes that
739 void consume_skb(struct sk_buff *skb)
743 if (likely(atomic_read(&skb->users) == 1))
745 else if (likely(!atomic_dec_and_test(&skb->users)))
747 trace_consume_skb(skb);
750 EXPORT_SYMBOL(consume_skb);
752 /* Make sure a field is enclosed inside headers_start/headers_end section */
753 #define CHECK_SKB_FIELD(field) \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
755 offsetof(struct sk_buff, headers_start)); \
756 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
757 offsetof(struct sk_buff, headers_end)); \
759 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
761 new->tstamp = old->tstamp;
762 /* We do not copy old->sk */
764 memcpy(new->cb, old->cb, sizeof(old->cb));
765 skb_dst_copy(new, old);
767 new->sp = secpath_get(old->sp);
769 __nf_copy(new, old, false);
771 /* Note : this field could be in headers_start/headers_end section
772 * It is not yet because we do not want to have a 16 bit hole
774 new->queue_mapping = old->queue_mapping;
776 memcpy(&new->headers_start, &old->headers_start,
777 offsetof(struct sk_buff, headers_end) -
778 offsetof(struct sk_buff, headers_start));
779 CHECK_SKB_FIELD(protocol);
780 CHECK_SKB_FIELD(csum);
781 CHECK_SKB_FIELD(hash);
782 CHECK_SKB_FIELD(priority);
783 CHECK_SKB_FIELD(skb_iif);
784 CHECK_SKB_FIELD(vlan_proto);
785 CHECK_SKB_FIELD(vlan_tci);
786 CHECK_SKB_FIELD(transport_header);
787 CHECK_SKB_FIELD(network_header);
788 CHECK_SKB_FIELD(mac_header);
789 CHECK_SKB_FIELD(inner_protocol);
790 CHECK_SKB_FIELD(inner_transport_header);
791 CHECK_SKB_FIELD(inner_network_header);
792 CHECK_SKB_FIELD(inner_mac_header);
793 CHECK_SKB_FIELD(mark);
794 #ifdef CONFIG_NETWORK_SECMARK
795 CHECK_SKB_FIELD(secmark);
797 #ifdef CONFIG_NET_RX_BUSY_POLL
798 CHECK_SKB_FIELD(napi_id);
801 CHECK_SKB_FIELD(sender_cpu);
803 #ifdef CONFIG_NET_SCHED
804 CHECK_SKB_FIELD(tc_index);
805 #ifdef CONFIG_NET_CLS_ACT
806 CHECK_SKB_FIELD(tc_verd);
813 * You should not add any new code to this function. Add it to
814 * __copy_skb_header above instead.
816 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
818 #define C(x) n->x = skb->x
820 n->next = n->prev = NULL;
822 __copy_skb_header(n, skb);
827 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
830 n->destructor = NULL;
837 atomic_set(&n->users, 1);
839 atomic_inc(&(skb_shinfo(skb)->dataref));
847 * skb_morph - morph one skb into another
848 * @dst: the skb to receive the contents
849 * @src: the skb to supply the contents
851 * This is identical to skb_clone except that the target skb is
852 * supplied by the user.
854 * The target skb is returned upon exit.
856 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
858 skb_release_all(dst);
859 return __skb_clone(dst, src);
861 EXPORT_SYMBOL_GPL(skb_morph);
864 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
865 * @skb: the skb to modify
866 * @gfp_mask: allocation priority
868 * This must be called on SKBTX_DEV_ZEROCOPY skb.
869 * It will copy all frags into kernel and drop the reference
870 * to userspace pages.
872 * If this function is called from an interrupt gfp_mask() must be
875 * Returns 0 on success or a negative error code on failure
876 * to allocate kernel memory to copy to.
878 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
881 int num_frags = skb_shinfo(skb)->nr_frags;
882 struct page *page, *head = NULL;
883 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
885 for (i = 0; i < num_frags; i++) {
887 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
889 page = alloc_page(gfp_mask);
892 struct page *next = (struct page *)page_private(head);
898 vaddr = kmap_atomic(skb_frag_page(f));
899 memcpy(page_address(page),
900 vaddr + f->page_offset, skb_frag_size(f));
901 kunmap_atomic(vaddr);
902 set_page_private(page, (unsigned long)head);
906 /* skb frags release userspace buffers */
907 for (i = 0; i < num_frags; i++)
908 skb_frag_unref(skb, i);
910 uarg->callback(uarg, false);
912 /* skb frags point to kernel buffers */
913 for (i = num_frags - 1; i >= 0; i--) {
914 __skb_fill_page_desc(skb, i, head, 0,
915 skb_shinfo(skb)->frags[i].size);
916 head = (struct page *)page_private(head);
919 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
922 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
925 * skb_clone - duplicate an sk_buff
926 * @skb: buffer to clone
927 * @gfp_mask: allocation priority
929 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
930 * copies share the same packet data but not structure. The new
931 * buffer has a reference count of 1. If the allocation fails the
932 * function returns %NULL otherwise the new buffer is returned.
934 * If this function is called from an interrupt gfp_mask() must be
938 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
940 struct sk_buff_fclones *fclones = container_of(skb,
941 struct sk_buff_fclones,
945 if (skb_orphan_frags(skb, gfp_mask))
948 if (skb->fclone == SKB_FCLONE_ORIG &&
949 atomic_read(&fclones->fclone_ref) == 1) {
951 atomic_set(&fclones->fclone_ref, 2);
953 if (skb_pfmemalloc(skb))
954 gfp_mask |= __GFP_MEMALLOC;
956 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
960 kmemcheck_annotate_bitfield(n, flags1);
961 n->fclone = SKB_FCLONE_UNAVAILABLE;
964 return __skb_clone(n, skb);
966 EXPORT_SYMBOL(skb_clone);
968 static void skb_headers_offset_update(struct sk_buff *skb, int off)
970 /* Only adjust this if it actually is csum_start rather than csum */
971 if (skb->ip_summed == CHECKSUM_PARTIAL)
972 skb->csum_start += off;
973 /* {transport,network,mac}_header and tail are relative to skb->head */
974 skb->transport_header += off;
975 skb->network_header += off;
976 if (skb_mac_header_was_set(skb))
977 skb->mac_header += off;
978 skb->inner_transport_header += off;
979 skb->inner_network_header += off;
980 skb->inner_mac_header += off;
983 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
985 __copy_skb_header(new, old);
987 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
988 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
989 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
992 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
994 if (skb_pfmemalloc(skb))
1000 * skb_copy - create private copy of an sk_buff
1001 * @skb: buffer to copy
1002 * @gfp_mask: allocation priority
1004 * Make a copy of both an &sk_buff and its data. This is used when the
1005 * caller wishes to modify the data and needs a private copy of the
1006 * data to alter. Returns %NULL on failure or the pointer to the buffer
1007 * on success. The returned buffer has a reference count of 1.
1009 * As by-product this function converts non-linear &sk_buff to linear
1010 * one, so that &sk_buff becomes completely private and caller is allowed
1011 * to modify all the data of returned buffer. This means that this
1012 * function is not recommended for use in circumstances when only
1013 * header is going to be modified. Use pskb_copy() instead.
1016 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1018 int headerlen = skb_headroom(skb);
1019 unsigned int size = skb_end_offset(skb) + skb->data_len;
1020 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1021 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1026 /* Set the data pointer */
1027 skb_reserve(n, headerlen);
1028 /* Set the tail pointer and length */
1029 skb_put(n, skb->len);
1031 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1034 copy_skb_header(n, skb);
1037 EXPORT_SYMBOL(skb_copy);
1040 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1041 * @skb: buffer to copy
1042 * @headroom: headroom of new skb
1043 * @gfp_mask: allocation priority
1044 * @fclone: if true allocate the copy of the skb from the fclone
1045 * cache instead of the head cache; it is recommended to set this
1046 * to true for the cases where the copy will likely be cloned
1048 * Make a copy of both an &sk_buff and part of its data, located
1049 * in header. Fragmented data remain shared. This is used when
1050 * the caller wishes to modify only header of &sk_buff and needs
1051 * private copy of the header to alter. Returns %NULL on failure
1052 * or the pointer to the buffer on success.
1053 * The returned buffer has a reference count of 1.
1056 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1057 gfp_t gfp_mask, bool fclone)
1059 unsigned int size = skb_headlen(skb) + headroom;
1060 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1061 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1066 /* Set the data pointer */
1067 skb_reserve(n, headroom);
1068 /* Set the tail pointer and length */
1069 skb_put(n, skb_headlen(skb));
1070 /* Copy the bytes */
1071 skb_copy_from_linear_data(skb, n->data, n->len);
1073 n->truesize += skb->data_len;
1074 n->data_len = skb->data_len;
1077 if (skb_shinfo(skb)->nr_frags) {
1080 if (skb_orphan_frags(skb, gfp_mask)) {
1085 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1086 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1087 skb_frag_ref(skb, i);
1089 skb_shinfo(n)->nr_frags = i;
1092 if (skb_has_frag_list(skb)) {
1093 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1094 skb_clone_fraglist(n);
1097 copy_skb_header(n, skb);
1101 EXPORT_SYMBOL(__pskb_copy_fclone);
1104 * pskb_expand_head - reallocate header of &sk_buff
1105 * @skb: buffer to reallocate
1106 * @nhead: room to add at head
1107 * @ntail: room to add at tail
1108 * @gfp_mask: allocation priority
1110 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1111 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1112 * reference count of 1. Returns zero in the case of success or error,
1113 * if expansion failed. In the last case, &sk_buff is not changed.
1115 * All the pointers pointing into skb header may change and must be
1116 * reloaded after call to this function.
1119 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1124 int size = nhead + skb_end_offset(skb) + ntail;
1129 if (skb_shared(skb))
1132 size = SKB_DATA_ALIGN(size);
1134 if (skb_pfmemalloc(skb))
1135 gfp_mask |= __GFP_MEMALLOC;
1136 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1137 gfp_mask, NUMA_NO_NODE, NULL);
1140 size = SKB_WITH_OVERHEAD(ksize(data));
1142 /* Copy only real data... and, alas, header. This should be
1143 * optimized for the cases when header is void.
1145 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1147 memcpy((struct skb_shared_info *)(data + size),
1149 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1152 * if shinfo is shared we must drop the old head gracefully, but if it
1153 * is not we can just drop the old head and let the existing refcount
1154 * be since all we did is relocate the values
1156 if (skb_cloned(skb)) {
1157 /* copy this zero copy skb frags */
1158 if (skb_orphan_frags(skb, gfp_mask))
1160 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1161 skb_frag_ref(skb, i);
1163 if (skb_has_frag_list(skb))
1164 skb_clone_fraglist(skb);
1166 skb_release_data(skb);
1170 off = (data + nhead) - skb->head;
1175 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1179 skb->end = skb->head + size;
1182 skb_headers_offset_update(skb, nhead);
1186 atomic_set(&skb_shinfo(skb)->dataref, 1);
1194 EXPORT_SYMBOL(pskb_expand_head);
1196 /* Make private copy of skb with writable head and some headroom */
1198 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1200 struct sk_buff *skb2;
1201 int delta = headroom - skb_headroom(skb);
1204 skb2 = pskb_copy(skb, GFP_ATOMIC);
1206 skb2 = skb_clone(skb, GFP_ATOMIC);
1207 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1215 EXPORT_SYMBOL(skb_realloc_headroom);
1218 * skb_copy_expand - copy and expand sk_buff
1219 * @skb: buffer to copy
1220 * @newheadroom: new free bytes at head
1221 * @newtailroom: new free bytes at tail
1222 * @gfp_mask: allocation priority
1224 * Make a copy of both an &sk_buff and its data and while doing so
1225 * allocate additional space.
1227 * This is used when the caller wishes to modify the data and needs a
1228 * private copy of the data to alter as well as more space for new fields.
1229 * Returns %NULL on failure or the pointer to the buffer
1230 * on success. The returned buffer has a reference count of 1.
1232 * You must pass %GFP_ATOMIC as the allocation priority if this function
1233 * is called from an interrupt.
1235 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1236 int newheadroom, int newtailroom,
1240 * Allocate the copy buffer
1242 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1243 gfp_mask, skb_alloc_rx_flag(skb),
1245 int oldheadroom = skb_headroom(skb);
1246 int head_copy_len, head_copy_off;
1251 skb_reserve(n, newheadroom);
1253 /* Set the tail pointer and length */
1254 skb_put(n, skb->len);
1256 head_copy_len = oldheadroom;
1258 if (newheadroom <= head_copy_len)
1259 head_copy_len = newheadroom;
1261 head_copy_off = newheadroom - head_copy_len;
1263 /* Copy the linear header and data. */
1264 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1265 skb->len + head_copy_len))
1268 copy_skb_header(n, skb);
1270 skb_headers_offset_update(n, newheadroom - oldheadroom);
1274 EXPORT_SYMBOL(skb_copy_expand);
1277 * skb_pad - zero pad the tail of an skb
1278 * @skb: buffer to pad
1279 * @pad: space to pad
1281 * Ensure that a buffer is followed by a padding area that is zero
1282 * filled. Used by network drivers which may DMA or transfer data
1283 * beyond the buffer end onto the wire.
1285 * May return error in out of memory cases. The skb is freed on error.
1288 int skb_pad(struct sk_buff *skb, int pad)
1293 /* If the skbuff is non linear tailroom is always zero.. */
1294 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1295 memset(skb->data+skb->len, 0, pad);
1299 ntail = skb->data_len + pad - (skb->end - skb->tail);
1300 if (likely(skb_cloned(skb) || ntail > 0)) {
1301 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1306 /* FIXME: The use of this function with non-linear skb's really needs
1309 err = skb_linearize(skb);
1313 memset(skb->data + skb->len, 0, pad);
1320 EXPORT_SYMBOL(skb_pad);
1323 * pskb_put - add data to the tail of a potentially fragmented buffer
1324 * @skb: start of the buffer to use
1325 * @tail: tail fragment of the buffer to use
1326 * @len: amount of data to add
1328 * This function extends the used data area of the potentially
1329 * fragmented buffer. @tail must be the last fragment of @skb -- or
1330 * @skb itself. If this would exceed the total buffer size the kernel
1331 * will panic. A pointer to the first byte of the extra data is
1335 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1338 skb->data_len += len;
1341 return skb_put(tail, len);
1343 EXPORT_SYMBOL_GPL(pskb_put);
1346 * skb_put - add data to a buffer
1347 * @skb: buffer to use
1348 * @len: amount of data to add
1350 * This function extends the used data area of the buffer. If this would
1351 * exceed the total buffer size the kernel will panic. A pointer to the
1352 * first byte of the extra data is returned.
1354 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1356 unsigned char *tmp = skb_tail_pointer(skb);
1357 SKB_LINEAR_ASSERT(skb);
1360 if (unlikely(skb->tail > skb->end))
1361 skb_over_panic(skb, len, __builtin_return_address(0));
1364 EXPORT_SYMBOL(skb_put);
1367 * skb_push - add data to the start of a buffer
1368 * @skb: buffer to use
1369 * @len: amount of data to add
1371 * This function extends the used data area of the buffer at the buffer
1372 * start. If this would exceed the total buffer headroom the kernel will
1373 * panic. A pointer to the first byte of the extra data is returned.
1375 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1379 if (unlikely(skb->data<skb->head))
1380 skb_under_panic(skb, len, __builtin_return_address(0));
1383 EXPORT_SYMBOL(skb_push);
1386 * skb_pull - remove data from the start of a buffer
1387 * @skb: buffer to use
1388 * @len: amount of data to remove
1390 * This function removes data from the start of a buffer, returning
1391 * the memory to the headroom. A pointer to the next data in the buffer
1392 * is returned. Once the data has been pulled future pushes will overwrite
1395 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1397 return skb_pull_inline(skb, len);
1399 EXPORT_SYMBOL(skb_pull);
1402 * skb_trim - remove end from a buffer
1403 * @skb: buffer to alter
1406 * Cut the length of a buffer down by removing data from the tail. If
1407 * the buffer is already under the length specified it is not modified.
1408 * The skb must be linear.
1410 void skb_trim(struct sk_buff *skb, unsigned int len)
1413 __skb_trim(skb, len);
1415 EXPORT_SYMBOL(skb_trim);
1417 /* Trims skb to length len. It can change skb pointers.
1420 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1422 struct sk_buff **fragp;
1423 struct sk_buff *frag;
1424 int offset = skb_headlen(skb);
1425 int nfrags = skb_shinfo(skb)->nr_frags;
1429 if (skb_cloned(skb) &&
1430 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1437 for (; i < nfrags; i++) {
1438 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1445 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1448 skb_shinfo(skb)->nr_frags = i;
1450 for (; i < nfrags; i++)
1451 skb_frag_unref(skb, i);
1453 if (skb_has_frag_list(skb))
1454 skb_drop_fraglist(skb);
1458 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1459 fragp = &frag->next) {
1460 int end = offset + frag->len;
1462 if (skb_shared(frag)) {
1463 struct sk_buff *nfrag;
1465 nfrag = skb_clone(frag, GFP_ATOMIC);
1466 if (unlikely(!nfrag))
1469 nfrag->next = frag->next;
1481 unlikely((err = pskb_trim(frag, len - offset))))
1485 skb_drop_list(&frag->next);
1490 if (len > skb_headlen(skb)) {
1491 skb->data_len -= skb->len - len;
1496 skb_set_tail_pointer(skb, len);
1501 EXPORT_SYMBOL(___pskb_trim);
1504 * __pskb_pull_tail - advance tail of skb header
1505 * @skb: buffer to reallocate
1506 * @delta: number of bytes to advance tail
1508 * The function makes a sense only on a fragmented &sk_buff,
1509 * it expands header moving its tail forward and copying necessary
1510 * data from fragmented part.
1512 * &sk_buff MUST have reference count of 1.
1514 * Returns %NULL (and &sk_buff does not change) if pull failed
1515 * or value of new tail of skb in the case of success.
1517 * All the pointers pointing into skb header may change and must be
1518 * reloaded after call to this function.
1521 /* Moves tail of skb head forward, copying data from fragmented part,
1522 * when it is necessary.
1523 * 1. It may fail due to malloc failure.
1524 * 2. It may change skb pointers.
1526 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1528 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1530 /* If skb has not enough free space at tail, get new one
1531 * plus 128 bytes for future expansions. If we have enough
1532 * room at tail, reallocate without expansion only if skb is cloned.
1534 int i, k, eat = (skb->tail + delta) - skb->end;
1536 if (eat > 0 || skb_cloned(skb)) {
1537 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1542 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1545 /* Optimization: no fragments, no reasons to preestimate
1546 * size of pulled pages. Superb.
1548 if (!skb_has_frag_list(skb))
1551 /* Estimate size of pulled pages. */
1553 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1554 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1561 /* If we need update frag list, we are in troubles.
1562 * Certainly, it possible to add an offset to skb data,
1563 * but taking into account that pulling is expected to
1564 * be very rare operation, it is worth to fight against
1565 * further bloating skb head and crucify ourselves here instead.
1566 * Pure masohism, indeed. 8)8)
1569 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1570 struct sk_buff *clone = NULL;
1571 struct sk_buff *insp = NULL;
1576 if (list->len <= eat) {
1577 /* Eaten as whole. */
1582 /* Eaten partially. */
1584 if (skb_shared(list)) {
1585 /* Sucks! We need to fork list. :-( */
1586 clone = skb_clone(list, GFP_ATOMIC);
1592 /* This may be pulled without
1596 if (!pskb_pull(list, eat)) {
1604 /* Free pulled out fragments. */
1605 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1606 skb_shinfo(skb)->frag_list = list->next;
1609 /* And insert new clone at head. */
1612 skb_shinfo(skb)->frag_list = clone;
1615 /* Success! Now we may commit changes to skb data. */
1620 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1621 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1624 skb_frag_unref(skb, i);
1627 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1629 skb_shinfo(skb)->frags[k].page_offset += eat;
1630 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1636 skb_shinfo(skb)->nr_frags = k;
1639 skb->data_len -= delta;
1641 return skb_tail_pointer(skb);
1643 EXPORT_SYMBOL(__pskb_pull_tail);
1646 * skb_copy_bits - copy bits from skb to kernel buffer
1648 * @offset: offset in source
1649 * @to: destination buffer
1650 * @len: number of bytes to copy
1652 * Copy the specified number of bytes from the source skb to the
1653 * destination buffer.
1656 * If its prototype is ever changed,
1657 * check arch/{*}/net/{*}.S files,
1658 * since it is called from BPF assembly code.
1660 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1662 int start = skb_headlen(skb);
1663 struct sk_buff *frag_iter;
1666 if (offset > (int)skb->len - len)
1670 if ((copy = start - offset) > 0) {
1673 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1674 if ((len -= copy) == 0)
1680 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1682 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1684 WARN_ON(start > offset + len);
1686 end = start + skb_frag_size(f);
1687 if ((copy = end - offset) > 0) {
1693 vaddr = kmap_atomic(skb_frag_page(f));
1695 vaddr + f->page_offset + offset - start,
1697 kunmap_atomic(vaddr);
1699 if ((len -= copy) == 0)
1707 skb_walk_frags(skb, frag_iter) {
1710 WARN_ON(start > offset + len);
1712 end = start + frag_iter->len;
1713 if ((copy = end - offset) > 0) {
1716 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1718 if ((len -= copy) == 0)
1732 EXPORT_SYMBOL(skb_copy_bits);
1735 * Callback from splice_to_pipe(), if we need to release some pages
1736 * at the end of the spd in case we error'ed out in filling the pipe.
1738 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1740 put_page(spd->pages[i]);
1743 static struct page *linear_to_page(struct page *page, unsigned int *len,
1744 unsigned int *offset,
1747 struct page_frag *pfrag = sk_page_frag(sk);
1749 if (!sk_page_frag_refill(sk, pfrag))
1752 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1754 memcpy(page_address(pfrag->page) + pfrag->offset,
1755 page_address(page) + *offset, *len);
1756 *offset = pfrag->offset;
1757 pfrag->offset += *len;
1762 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1764 unsigned int offset)
1766 return spd->nr_pages &&
1767 spd->pages[spd->nr_pages - 1] == page &&
1768 (spd->partial[spd->nr_pages - 1].offset +
1769 spd->partial[spd->nr_pages - 1].len == offset);
1773 * Fill page/offset/length into spd, if it can hold more pages.
1775 static bool spd_fill_page(struct splice_pipe_desc *spd,
1776 struct pipe_inode_info *pipe, struct page *page,
1777 unsigned int *len, unsigned int offset,
1781 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1785 page = linear_to_page(page, len, &offset, sk);
1789 if (spd_can_coalesce(spd, page, offset)) {
1790 spd->partial[spd->nr_pages - 1].len += *len;
1794 spd->pages[spd->nr_pages] = page;
1795 spd->partial[spd->nr_pages].len = *len;
1796 spd->partial[spd->nr_pages].offset = offset;
1802 static bool __splice_segment(struct page *page, unsigned int poff,
1803 unsigned int plen, unsigned int *off,
1805 struct splice_pipe_desc *spd, bool linear,
1807 struct pipe_inode_info *pipe)
1812 /* skip this segment if already processed */
1818 /* ignore any bits we already processed */
1824 unsigned int flen = min(*len, plen);
1826 if (spd_fill_page(spd, pipe, page, &flen, poff,
1832 } while (*len && plen);
1838 * Map linear and fragment data from the skb to spd. It reports true if the
1839 * pipe is full or if we already spliced the requested length.
1841 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1842 unsigned int *offset, unsigned int *len,
1843 struct splice_pipe_desc *spd, struct sock *sk)
1847 /* map the linear part :
1848 * If skb->head_frag is set, this 'linear' part is backed by a
1849 * fragment, and if the head is not shared with any clones then
1850 * we can avoid a copy since we own the head portion of this page.
1852 if (__splice_segment(virt_to_page(skb->data),
1853 (unsigned long) skb->data & (PAGE_SIZE - 1),
1856 skb_head_is_locked(skb),
1861 * then map the fragments
1863 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1864 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1866 if (__splice_segment(skb_frag_page(f),
1867 f->page_offset, skb_frag_size(f),
1868 offset, len, spd, false, sk, pipe))
1875 ssize_t skb_socket_splice(struct sock *sk,
1876 struct pipe_inode_info *pipe,
1877 struct splice_pipe_desc *spd)
1881 /* Drop the socket lock, otherwise we have reverse
1882 * locking dependencies between sk_lock and i_mutex
1883 * here as compared to sendfile(). We enter here
1884 * with the socket lock held, and splice_to_pipe() will
1885 * grab the pipe inode lock. For sendfile() emulation,
1886 * we call into ->sendpage() with the i_mutex lock held
1887 * and networking will grab the socket lock.
1890 ret = splice_to_pipe(pipe, spd);
1897 * Map data from the skb to a pipe. Should handle both the linear part,
1898 * the fragments, and the frag list. It does NOT handle frag lists within
1899 * the frag list, if such a thing exists. We'd probably need to recurse to
1900 * handle that cleanly.
1902 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1903 struct pipe_inode_info *pipe, unsigned int tlen,
1905 ssize_t (*splice_cb)(struct sock *,
1906 struct pipe_inode_info *,
1907 struct splice_pipe_desc *))
1909 struct partial_page partial[MAX_SKB_FRAGS];
1910 struct page *pages[MAX_SKB_FRAGS];
1911 struct splice_pipe_desc spd = {
1914 .nr_pages_max = MAX_SKB_FRAGS,
1916 .ops = &nosteal_pipe_buf_ops,
1917 .spd_release = sock_spd_release,
1919 struct sk_buff *frag_iter;
1923 * __skb_splice_bits() only fails if the output has no room left,
1924 * so no point in going over the frag_list for the error case.
1926 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1932 * now see if we have a frag_list to map
1934 skb_walk_frags(skb, frag_iter) {
1937 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1943 ret = splice_cb(sk, pipe, &spd);
1947 EXPORT_SYMBOL_GPL(skb_splice_bits);
1950 * skb_store_bits - store bits from kernel buffer to skb
1951 * @skb: destination buffer
1952 * @offset: offset in destination
1953 * @from: source buffer
1954 * @len: number of bytes to copy
1956 * Copy the specified number of bytes from the source buffer to the
1957 * destination skb. This function handles all the messy bits of
1958 * traversing fragment lists and such.
1961 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1963 int start = skb_headlen(skb);
1964 struct sk_buff *frag_iter;
1967 if (offset > (int)skb->len - len)
1970 if ((copy = start - offset) > 0) {
1973 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1974 if ((len -= copy) == 0)
1980 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1981 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1984 WARN_ON(start > offset + len);
1986 end = start + skb_frag_size(frag);
1987 if ((copy = end - offset) > 0) {
1993 vaddr = kmap_atomic(skb_frag_page(frag));
1994 memcpy(vaddr + frag->page_offset + offset - start,
1996 kunmap_atomic(vaddr);
1998 if ((len -= copy) == 0)
2006 skb_walk_frags(skb, frag_iter) {
2009 WARN_ON(start > offset + len);
2011 end = start + frag_iter->len;
2012 if ((copy = end - offset) > 0) {
2015 if (skb_store_bits(frag_iter, offset - start,
2018 if ((len -= copy) == 0)
2031 EXPORT_SYMBOL(skb_store_bits);
2033 /* Checksum skb data. */
2034 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2035 __wsum csum, const struct skb_checksum_ops *ops)
2037 int start = skb_headlen(skb);
2038 int i, copy = start - offset;
2039 struct sk_buff *frag_iter;
2042 /* Checksum header. */
2046 csum = ops->update(skb->data + offset, copy, csum);
2047 if ((len -= copy) == 0)
2053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2055 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2057 WARN_ON(start > offset + len);
2059 end = start + skb_frag_size(frag);
2060 if ((copy = end - offset) > 0) {
2066 vaddr = kmap_atomic(skb_frag_page(frag));
2067 csum2 = ops->update(vaddr + frag->page_offset +
2068 offset - start, copy, 0);
2069 kunmap_atomic(vaddr);
2070 csum = ops->combine(csum, csum2, pos, copy);
2079 skb_walk_frags(skb, frag_iter) {
2082 WARN_ON(start > offset + len);
2084 end = start + frag_iter->len;
2085 if ((copy = end - offset) > 0) {
2089 csum2 = __skb_checksum(frag_iter, offset - start,
2091 csum = ops->combine(csum, csum2, pos, copy);
2092 if ((len -= copy) == 0)
2103 EXPORT_SYMBOL(__skb_checksum);
2105 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2106 int len, __wsum csum)
2108 const struct skb_checksum_ops ops = {
2109 .update = csum_partial_ext,
2110 .combine = csum_block_add_ext,
2113 return __skb_checksum(skb, offset, len, csum, &ops);
2115 EXPORT_SYMBOL(skb_checksum);
2117 /* Both of above in one bottle. */
2119 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2120 u8 *to, int len, __wsum csum)
2122 int start = skb_headlen(skb);
2123 int i, copy = start - offset;
2124 struct sk_buff *frag_iter;
2131 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2133 if ((len -= copy) == 0)
2140 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2143 WARN_ON(start > offset + len);
2145 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2146 if ((copy = end - offset) > 0) {
2149 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2153 vaddr = kmap_atomic(skb_frag_page(frag));
2154 csum2 = csum_partial_copy_nocheck(vaddr +
2158 kunmap_atomic(vaddr);
2159 csum = csum_block_add(csum, csum2, pos);
2169 skb_walk_frags(skb, frag_iter) {
2173 WARN_ON(start > offset + len);
2175 end = start + frag_iter->len;
2176 if ((copy = end - offset) > 0) {
2179 csum2 = skb_copy_and_csum_bits(frag_iter,
2182 csum = csum_block_add(csum, csum2, pos);
2183 if ((len -= copy) == 0)
2194 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2197 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2198 * @from: source buffer
2200 * Calculates the amount of linear headroom needed in the 'to' skb passed
2201 * into skb_zerocopy().
2204 skb_zerocopy_headlen(const struct sk_buff *from)
2206 unsigned int hlen = 0;
2208 if (!from->head_frag ||
2209 skb_headlen(from) < L1_CACHE_BYTES ||
2210 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2211 hlen = skb_headlen(from);
2213 if (skb_has_frag_list(from))
2218 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2221 * skb_zerocopy - Zero copy skb to skb
2222 * @to: destination buffer
2223 * @from: source buffer
2224 * @len: number of bytes to copy from source buffer
2225 * @hlen: size of linear headroom in destination buffer
2227 * Copies up to `len` bytes from `from` to `to` by creating references
2228 * to the frags in the source buffer.
2230 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2231 * headroom in the `to` buffer.
2234 * 0: everything is OK
2235 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2236 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2239 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2242 int plen = 0; /* length of skb->head fragment */
2245 unsigned int offset;
2247 BUG_ON(!from->head_frag && !hlen);
2249 /* dont bother with small payloads */
2250 if (len <= skb_tailroom(to))
2251 return skb_copy_bits(from, 0, skb_put(to, len), len);
2254 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2259 plen = min_t(int, skb_headlen(from), len);
2261 page = virt_to_head_page(from->head);
2262 offset = from->data - (unsigned char *)page_address(page);
2263 __skb_fill_page_desc(to, 0, page, offset, plen);
2270 to->truesize += len + plen;
2271 to->len += len + plen;
2272 to->data_len += len + plen;
2274 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2279 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2282 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2283 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2284 len -= skb_shinfo(to)->frags[j].size;
2285 skb_frag_ref(to, j);
2288 skb_shinfo(to)->nr_frags = j;
2292 EXPORT_SYMBOL_GPL(skb_zerocopy);
2294 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2299 if (skb->ip_summed == CHECKSUM_PARTIAL)
2300 csstart = skb_checksum_start_offset(skb);
2302 csstart = skb_headlen(skb);
2304 BUG_ON(csstart > skb_headlen(skb));
2306 skb_copy_from_linear_data(skb, to, csstart);
2309 if (csstart != skb->len)
2310 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2311 skb->len - csstart, 0);
2313 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2314 long csstuff = csstart + skb->csum_offset;
2316 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2319 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2322 * skb_dequeue - remove from the head of the queue
2323 * @list: list to dequeue from
2325 * Remove the head of the list. The list lock is taken so the function
2326 * may be used safely with other locking list functions. The head item is
2327 * returned or %NULL if the list is empty.
2330 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2332 unsigned long flags;
2333 struct sk_buff *result;
2335 spin_lock_irqsave(&list->lock, flags);
2336 result = __skb_dequeue(list);
2337 spin_unlock_irqrestore(&list->lock, flags);
2340 EXPORT_SYMBOL(skb_dequeue);
2343 * skb_dequeue_tail - remove from the tail of the queue
2344 * @list: list to dequeue from
2346 * Remove the tail of the list. The list lock is taken so the function
2347 * may be used safely with other locking list functions. The tail item is
2348 * returned or %NULL if the list is empty.
2350 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2352 unsigned long flags;
2353 struct sk_buff *result;
2355 spin_lock_irqsave(&list->lock, flags);
2356 result = __skb_dequeue_tail(list);
2357 spin_unlock_irqrestore(&list->lock, flags);
2360 EXPORT_SYMBOL(skb_dequeue_tail);
2363 * skb_queue_purge - empty a list
2364 * @list: list to empty
2366 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2367 * the list and one reference dropped. This function takes the list
2368 * lock and is atomic with respect to other list locking functions.
2370 void skb_queue_purge(struct sk_buff_head *list)
2372 struct sk_buff *skb;
2373 while ((skb = skb_dequeue(list)) != NULL)
2376 EXPORT_SYMBOL(skb_queue_purge);
2379 * skb_queue_head - queue a buffer at the list head
2380 * @list: list to use
2381 * @newsk: buffer to queue
2383 * Queue a buffer at the start of the list. This function takes the
2384 * list lock and can be used safely with other locking &sk_buff functions
2387 * A buffer cannot be placed on two lists at the same time.
2389 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2391 unsigned long flags;
2393 spin_lock_irqsave(&list->lock, flags);
2394 __skb_queue_head(list, newsk);
2395 spin_unlock_irqrestore(&list->lock, flags);
2397 EXPORT_SYMBOL(skb_queue_head);
2400 * skb_queue_tail - queue a buffer at the list tail
2401 * @list: list to use
2402 * @newsk: buffer to queue
2404 * Queue a buffer at the tail of the list. This function takes the
2405 * list lock and can be used safely with other locking &sk_buff functions
2408 * A buffer cannot be placed on two lists at the same time.
2410 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2412 unsigned long flags;
2414 spin_lock_irqsave(&list->lock, flags);
2415 __skb_queue_tail(list, newsk);
2416 spin_unlock_irqrestore(&list->lock, flags);
2418 EXPORT_SYMBOL(skb_queue_tail);
2421 * skb_unlink - remove a buffer from a list
2422 * @skb: buffer to remove
2423 * @list: list to use
2425 * Remove a packet from a list. The list locks are taken and this
2426 * function is atomic with respect to other list locked calls
2428 * You must know what list the SKB is on.
2430 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2432 unsigned long flags;
2434 spin_lock_irqsave(&list->lock, flags);
2435 __skb_unlink(skb, list);
2436 spin_unlock_irqrestore(&list->lock, flags);
2438 EXPORT_SYMBOL(skb_unlink);
2441 * skb_append - append a buffer
2442 * @old: buffer to insert after
2443 * @newsk: buffer to insert
2444 * @list: list to use
2446 * Place a packet after a given packet in a list. The list locks are taken
2447 * and this function is atomic with respect to other list locked calls.
2448 * A buffer cannot be placed on two lists at the same time.
2450 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2452 unsigned long flags;
2454 spin_lock_irqsave(&list->lock, flags);
2455 __skb_queue_after(list, old, newsk);
2456 spin_unlock_irqrestore(&list->lock, flags);
2458 EXPORT_SYMBOL(skb_append);
2461 * skb_insert - insert a buffer
2462 * @old: buffer to insert before
2463 * @newsk: buffer to insert
2464 * @list: list to use
2466 * Place a packet before a given packet in a list. The list locks are
2467 * taken and this function is atomic with respect to other list locked
2470 * A buffer cannot be placed on two lists at the same time.
2472 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2474 unsigned long flags;
2476 spin_lock_irqsave(&list->lock, flags);
2477 __skb_insert(newsk, old->prev, old, list);
2478 spin_unlock_irqrestore(&list->lock, flags);
2480 EXPORT_SYMBOL(skb_insert);
2482 static inline void skb_split_inside_header(struct sk_buff *skb,
2483 struct sk_buff* skb1,
2484 const u32 len, const int pos)
2488 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2490 /* And move data appendix as is. */
2491 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2492 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2494 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2495 skb_shinfo(skb)->nr_frags = 0;
2496 skb1->data_len = skb->data_len;
2497 skb1->len += skb1->data_len;
2500 skb_set_tail_pointer(skb, len);
2503 static inline void skb_split_no_header(struct sk_buff *skb,
2504 struct sk_buff* skb1,
2505 const u32 len, int pos)
2508 const int nfrags = skb_shinfo(skb)->nr_frags;
2510 skb_shinfo(skb)->nr_frags = 0;
2511 skb1->len = skb1->data_len = skb->len - len;
2513 skb->data_len = len - pos;
2515 for (i = 0; i < nfrags; i++) {
2516 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2518 if (pos + size > len) {
2519 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2523 * We have two variants in this case:
2524 * 1. Move all the frag to the second
2525 * part, if it is possible. F.e.
2526 * this approach is mandatory for TUX,
2527 * where splitting is expensive.
2528 * 2. Split is accurately. We make this.
2530 skb_frag_ref(skb, i);
2531 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2532 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2533 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2534 skb_shinfo(skb)->nr_frags++;
2538 skb_shinfo(skb)->nr_frags++;
2541 skb_shinfo(skb1)->nr_frags = k;
2545 * skb_split - Split fragmented skb to two parts at length len.
2546 * @skb: the buffer to split
2547 * @skb1: the buffer to receive the second part
2548 * @len: new length for skb
2550 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2552 int pos = skb_headlen(skb);
2554 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2555 if (len < pos) /* Split line is inside header. */
2556 skb_split_inside_header(skb, skb1, len, pos);
2557 else /* Second chunk has no header, nothing to copy. */
2558 skb_split_no_header(skb, skb1, len, pos);
2560 EXPORT_SYMBOL(skb_split);
2562 /* Shifting from/to a cloned skb is a no-go.
2564 * Caller cannot keep skb_shinfo related pointers past calling here!
2566 static int skb_prepare_for_shift(struct sk_buff *skb)
2568 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2572 * skb_shift - Shifts paged data partially from skb to another
2573 * @tgt: buffer into which tail data gets added
2574 * @skb: buffer from which the paged data comes from
2575 * @shiftlen: shift up to this many bytes
2577 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2578 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2579 * It's up to caller to free skb if everything was shifted.
2581 * If @tgt runs out of frags, the whole operation is aborted.
2583 * Skb cannot include anything else but paged data while tgt is allowed
2584 * to have non-paged data as well.
2586 * TODO: full sized shift could be optimized but that would need
2587 * specialized skb free'er to handle frags without up-to-date nr_frags.
2589 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2591 int from, to, merge, todo;
2592 struct skb_frag_struct *fragfrom, *fragto;
2594 BUG_ON(shiftlen > skb->len);
2595 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2599 to = skb_shinfo(tgt)->nr_frags;
2600 fragfrom = &skb_shinfo(skb)->frags[from];
2602 /* Actual merge is delayed until the point when we know we can
2603 * commit all, so that we don't have to undo partial changes
2606 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2607 fragfrom->page_offset)) {
2612 todo -= skb_frag_size(fragfrom);
2614 if (skb_prepare_for_shift(skb) ||
2615 skb_prepare_for_shift(tgt))
2618 /* All previous frag pointers might be stale! */
2619 fragfrom = &skb_shinfo(skb)->frags[from];
2620 fragto = &skb_shinfo(tgt)->frags[merge];
2622 skb_frag_size_add(fragto, shiftlen);
2623 skb_frag_size_sub(fragfrom, shiftlen);
2624 fragfrom->page_offset += shiftlen;
2632 /* Skip full, not-fitting skb to avoid expensive operations */
2633 if ((shiftlen == skb->len) &&
2634 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2637 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2640 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2641 if (to == MAX_SKB_FRAGS)
2644 fragfrom = &skb_shinfo(skb)->frags[from];
2645 fragto = &skb_shinfo(tgt)->frags[to];
2647 if (todo >= skb_frag_size(fragfrom)) {
2648 *fragto = *fragfrom;
2649 todo -= skb_frag_size(fragfrom);
2654 __skb_frag_ref(fragfrom);
2655 fragto->page = fragfrom->page;
2656 fragto->page_offset = fragfrom->page_offset;
2657 skb_frag_size_set(fragto, todo);
2659 fragfrom->page_offset += todo;
2660 skb_frag_size_sub(fragfrom, todo);
2668 /* Ready to "commit" this state change to tgt */
2669 skb_shinfo(tgt)->nr_frags = to;
2672 fragfrom = &skb_shinfo(skb)->frags[0];
2673 fragto = &skb_shinfo(tgt)->frags[merge];
2675 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2676 __skb_frag_unref(fragfrom);
2679 /* Reposition in the original skb */
2681 while (from < skb_shinfo(skb)->nr_frags)
2682 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2683 skb_shinfo(skb)->nr_frags = to;
2685 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2688 /* Most likely the tgt won't ever need its checksum anymore, skb on
2689 * the other hand might need it if it needs to be resent
2691 tgt->ip_summed = CHECKSUM_PARTIAL;
2692 skb->ip_summed = CHECKSUM_PARTIAL;
2694 /* Yak, is it really working this way? Some helper please? */
2695 skb->len -= shiftlen;
2696 skb->data_len -= shiftlen;
2697 skb->truesize -= shiftlen;
2698 tgt->len += shiftlen;
2699 tgt->data_len += shiftlen;
2700 tgt->truesize += shiftlen;
2706 * skb_prepare_seq_read - Prepare a sequential read of skb data
2707 * @skb: the buffer to read
2708 * @from: lower offset of data to be read
2709 * @to: upper offset of data to be read
2710 * @st: state variable
2712 * Initializes the specified state variable. Must be called before
2713 * invoking skb_seq_read() for the first time.
2715 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2716 unsigned int to, struct skb_seq_state *st)
2718 st->lower_offset = from;
2719 st->upper_offset = to;
2720 st->root_skb = st->cur_skb = skb;
2721 st->frag_idx = st->stepped_offset = 0;
2722 st->frag_data = NULL;
2724 EXPORT_SYMBOL(skb_prepare_seq_read);
2727 * skb_seq_read - Sequentially read skb data
2728 * @consumed: number of bytes consumed by the caller so far
2729 * @data: destination pointer for data to be returned
2730 * @st: state variable
2732 * Reads a block of skb data at @consumed relative to the
2733 * lower offset specified to skb_prepare_seq_read(). Assigns
2734 * the head of the data block to @data and returns the length
2735 * of the block or 0 if the end of the skb data or the upper
2736 * offset has been reached.
2738 * The caller is not required to consume all of the data
2739 * returned, i.e. @consumed is typically set to the number
2740 * of bytes already consumed and the next call to
2741 * skb_seq_read() will return the remaining part of the block.
2743 * Note 1: The size of each block of data returned can be arbitrary,
2744 * this limitation is the cost for zerocopy sequential
2745 * reads of potentially non linear data.
2747 * Note 2: Fragment lists within fragments are not implemented
2748 * at the moment, state->root_skb could be replaced with
2749 * a stack for this purpose.
2751 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2752 struct skb_seq_state *st)
2754 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2757 if (unlikely(abs_offset >= st->upper_offset)) {
2758 if (st->frag_data) {
2759 kunmap_atomic(st->frag_data);
2760 st->frag_data = NULL;
2766 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2768 if (abs_offset < block_limit && !st->frag_data) {
2769 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2770 return block_limit - abs_offset;
2773 if (st->frag_idx == 0 && !st->frag_data)
2774 st->stepped_offset += skb_headlen(st->cur_skb);
2776 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2777 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2778 block_limit = skb_frag_size(frag) + st->stepped_offset;
2780 if (abs_offset < block_limit) {
2782 st->frag_data = kmap_atomic(skb_frag_page(frag));
2784 *data = (u8 *) st->frag_data + frag->page_offset +
2785 (abs_offset - st->stepped_offset);
2787 return block_limit - abs_offset;
2790 if (st->frag_data) {
2791 kunmap_atomic(st->frag_data);
2792 st->frag_data = NULL;
2796 st->stepped_offset += skb_frag_size(frag);
2799 if (st->frag_data) {
2800 kunmap_atomic(st->frag_data);
2801 st->frag_data = NULL;
2804 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2805 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2808 } else if (st->cur_skb->next) {
2809 st->cur_skb = st->cur_skb->next;
2816 EXPORT_SYMBOL(skb_seq_read);
2819 * skb_abort_seq_read - Abort a sequential read of skb data
2820 * @st: state variable
2822 * Must be called if skb_seq_read() was not called until it
2825 void skb_abort_seq_read(struct skb_seq_state *st)
2828 kunmap_atomic(st->frag_data);
2830 EXPORT_SYMBOL(skb_abort_seq_read);
2832 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2834 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2835 struct ts_config *conf,
2836 struct ts_state *state)
2838 return skb_seq_read(offset, text, TS_SKB_CB(state));
2841 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2843 skb_abort_seq_read(TS_SKB_CB(state));
2847 * skb_find_text - Find a text pattern in skb data
2848 * @skb: the buffer to look in
2849 * @from: search offset
2851 * @config: textsearch configuration
2853 * Finds a pattern in the skb data according to the specified
2854 * textsearch configuration. Use textsearch_next() to retrieve
2855 * subsequent occurrences of the pattern. Returns the offset
2856 * to the first occurrence or UINT_MAX if no match was found.
2858 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2859 unsigned int to, struct ts_config *config)
2861 struct ts_state state;
2864 config->get_next_block = skb_ts_get_next_block;
2865 config->finish = skb_ts_finish;
2867 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2869 ret = textsearch_find(config, &state);
2870 return (ret <= to - from ? ret : UINT_MAX);
2872 EXPORT_SYMBOL(skb_find_text);
2875 * skb_append_datato_frags - append the user data to a skb
2876 * @sk: sock structure
2877 * @skb: skb structure to be appended with user data.
2878 * @getfrag: call back function to be used for getting the user data
2879 * @from: pointer to user message iov
2880 * @length: length of the iov message
2882 * Description: This procedure append the user data in the fragment part
2883 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2885 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2886 int (*getfrag)(void *from, char *to, int offset,
2887 int len, int odd, struct sk_buff *skb),
2888 void *from, int length)
2890 int frg_cnt = skb_shinfo(skb)->nr_frags;
2894 struct page_frag *pfrag = ¤t->task_frag;
2897 /* Return error if we don't have space for new frag */
2898 if (frg_cnt >= MAX_SKB_FRAGS)
2901 if (!sk_page_frag_refill(sk, pfrag))
2904 /* copy the user data to page */
2905 copy = min_t(int, length, pfrag->size - pfrag->offset);
2907 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2908 offset, copy, 0, skb);
2912 /* copy was successful so update the size parameters */
2913 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2916 pfrag->offset += copy;
2917 get_page(pfrag->page);
2919 skb->truesize += copy;
2920 atomic_add(copy, &sk->sk_wmem_alloc);
2922 skb->data_len += copy;
2926 } while (length > 0);
2930 EXPORT_SYMBOL(skb_append_datato_frags);
2932 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2933 int offset, size_t size)
2935 int i = skb_shinfo(skb)->nr_frags;
2937 if (skb_can_coalesce(skb, i, page, offset)) {
2938 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2939 } else if (i < MAX_SKB_FRAGS) {
2941 skb_fill_page_desc(skb, i, page, offset, size);
2948 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2951 * skb_pull_rcsum - pull skb and update receive checksum
2952 * @skb: buffer to update
2953 * @len: length of data pulled
2955 * This function performs an skb_pull on the packet and updates
2956 * the CHECKSUM_COMPLETE checksum. It should be used on
2957 * receive path processing instead of skb_pull unless you know
2958 * that the checksum difference is zero (e.g., a valid IP header)
2959 * or you are setting ip_summed to CHECKSUM_NONE.
2961 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2963 unsigned char *data = skb->data;
2965 BUG_ON(len > skb->len);
2966 __skb_pull(skb, len);
2967 skb_postpull_rcsum(skb, data, len);
2970 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2973 * skb_segment - Perform protocol segmentation on skb.
2974 * @head_skb: buffer to segment
2975 * @features: features for the output path (see dev->features)
2977 * This function performs segmentation on the given skb. It returns
2978 * a pointer to the first in a list of new skbs for the segments.
2979 * In case of error it returns ERR_PTR(err).
2981 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2982 netdev_features_t features)
2984 struct sk_buff *segs = NULL;
2985 struct sk_buff *tail = NULL;
2986 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2987 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2988 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2989 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2990 struct sk_buff *frag_skb = head_skb;
2991 unsigned int offset = doffset;
2992 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2993 unsigned int headroom;
2997 int sg = !!(features & NETIF_F_SG);
2998 int nfrags = skb_shinfo(head_skb)->nr_frags;
3004 __skb_push(head_skb, doffset);
3005 proto = skb_network_protocol(head_skb, &dummy);
3006 if (unlikely(!proto))
3007 return ERR_PTR(-EINVAL);
3009 csum = !head_skb->encap_hdr_csum &&
3010 !!can_checksum_protocol(features, proto);
3012 headroom = skb_headroom(head_skb);
3013 pos = skb_headlen(head_skb);
3016 struct sk_buff *nskb;
3017 skb_frag_t *nskb_frag;
3021 len = head_skb->len - offset;
3025 hsize = skb_headlen(head_skb) - offset;
3028 if (hsize > len || !sg)
3031 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3032 (skb_headlen(list_skb) == len || sg)) {
3033 BUG_ON(skb_headlen(list_skb) > len);
3036 nfrags = skb_shinfo(list_skb)->nr_frags;
3037 frag = skb_shinfo(list_skb)->frags;
3038 frag_skb = list_skb;
3039 pos += skb_headlen(list_skb);
3041 while (pos < offset + len) {
3042 BUG_ON(i >= nfrags);
3044 size = skb_frag_size(frag);
3045 if (pos + size > offset + len)
3053 nskb = skb_clone(list_skb, GFP_ATOMIC);
3054 list_skb = list_skb->next;
3056 if (unlikely(!nskb))
3059 if (unlikely(pskb_trim(nskb, len))) {
3064 hsize = skb_end_offset(nskb);
3065 if (skb_cow_head(nskb, doffset + headroom)) {
3070 nskb->truesize += skb_end_offset(nskb) - hsize;
3071 skb_release_head_state(nskb);
3072 __skb_push(nskb, doffset);
3074 nskb = __alloc_skb(hsize + doffset + headroom,
3075 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3078 if (unlikely(!nskb))
3081 skb_reserve(nskb, headroom);
3082 __skb_put(nskb, doffset);
3091 __copy_skb_header(nskb, head_skb);
3093 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3094 skb_reset_mac_len(nskb);
3096 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3097 nskb->data - tnl_hlen,
3098 doffset + tnl_hlen);
3100 if (nskb->len == len + doffset)
3101 goto perform_csum_check;
3103 if (!sg && !nskb->remcsum_offload) {
3104 nskb->ip_summed = CHECKSUM_NONE;
3105 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3108 SKB_GSO_CB(nskb)->csum_start =
3109 skb_headroom(nskb) + doffset;
3113 nskb_frag = skb_shinfo(nskb)->frags;
3115 skb_copy_from_linear_data_offset(head_skb, offset,
3116 skb_put(nskb, hsize), hsize);
3118 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3121 while (pos < offset + len) {
3123 BUG_ON(skb_headlen(list_skb));
3126 nfrags = skb_shinfo(list_skb)->nr_frags;
3127 frag = skb_shinfo(list_skb)->frags;
3128 frag_skb = list_skb;
3132 list_skb = list_skb->next;
3135 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3137 net_warn_ratelimited(
3138 "skb_segment: too many frags: %u %u\n",
3143 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3147 __skb_frag_ref(nskb_frag);
3148 size = skb_frag_size(nskb_frag);
3151 nskb_frag->page_offset += offset - pos;
3152 skb_frag_size_sub(nskb_frag, offset - pos);
3155 skb_shinfo(nskb)->nr_frags++;
3157 if (pos + size <= offset + len) {
3162 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3170 nskb->data_len = len - hsize;
3171 nskb->len += nskb->data_len;
3172 nskb->truesize += nskb->data_len;
3175 if (!csum && !nskb->remcsum_offload) {
3176 nskb->csum = skb_checksum(nskb, doffset,
3177 nskb->len - doffset, 0);
3178 nskb->ip_summed = CHECKSUM_NONE;
3179 SKB_GSO_CB(nskb)->csum_start =
3180 skb_headroom(nskb) + doffset;
3182 } while ((offset += len) < head_skb->len);
3184 /* Some callers want to get the end of the list.
3185 * Put it in segs->prev to avoid walking the list.
3186 * (see validate_xmit_skb_list() for example)
3190 /* Following permits correct backpressure, for protocols
3191 * using skb_set_owner_w().
3192 * Idea is to tranfert ownership from head_skb to last segment.
3194 if (head_skb->destructor == sock_wfree) {
3195 swap(tail->truesize, head_skb->truesize);
3196 swap(tail->destructor, head_skb->destructor);
3197 swap(tail->sk, head_skb->sk);
3202 kfree_skb_list(segs);
3203 return ERR_PTR(err);
3205 EXPORT_SYMBOL_GPL(skb_segment);
3207 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3209 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3210 unsigned int offset = skb_gro_offset(skb);
3211 unsigned int headlen = skb_headlen(skb);
3212 unsigned int len = skb_gro_len(skb);
3213 struct sk_buff *lp, *p = *head;
3214 unsigned int delta_truesize;
3216 if (unlikely(p->len + len >= 65536))
3219 lp = NAPI_GRO_CB(p)->last;
3220 pinfo = skb_shinfo(lp);
3222 if (headlen <= offset) {
3225 int i = skbinfo->nr_frags;
3226 int nr_frags = pinfo->nr_frags + i;
3228 if (nr_frags > MAX_SKB_FRAGS)
3232 pinfo->nr_frags = nr_frags;
3233 skbinfo->nr_frags = 0;
3235 frag = pinfo->frags + nr_frags;
3236 frag2 = skbinfo->frags + i;
3241 frag->page_offset += offset;
3242 skb_frag_size_sub(frag, offset);
3244 /* all fragments truesize : remove (head size + sk_buff) */
3245 delta_truesize = skb->truesize -
3246 SKB_TRUESIZE(skb_end_offset(skb));
3248 skb->truesize -= skb->data_len;
3249 skb->len -= skb->data_len;
3252 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3254 } else if (skb->head_frag) {
3255 int nr_frags = pinfo->nr_frags;
3256 skb_frag_t *frag = pinfo->frags + nr_frags;
3257 struct page *page = virt_to_head_page(skb->head);
3258 unsigned int first_size = headlen - offset;
3259 unsigned int first_offset;
3261 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3264 first_offset = skb->data -
3265 (unsigned char *)page_address(page) +
3268 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3270 frag->page.p = page;
3271 frag->page_offset = first_offset;
3272 skb_frag_size_set(frag, first_size);
3274 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3275 /* We dont need to clear skbinfo->nr_frags here */
3277 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3278 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3283 delta_truesize = skb->truesize;
3284 if (offset > headlen) {
3285 unsigned int eat = offset - headlen;
3287 skbinfo->frags[0].page_offset += eat;
3288 skb_frag_size_sub(&skbinfo->frags[0], eat);
3289 skb->data_len -= eat;
3294 __skb_pull(skb, offset);
3296 if (NAPI_GRO_CB(p)->last == p)
3297 skb_shinfo(p)->frag_list = skb;
3299 NAPI_GRO_CB(p)->last->next = skb;
3300 NAPI_GRO_CB(p)->last = skb;
3301 __skb_header_release(skb);
3305 NAPI_GRO_CB(p)->count++;
3307 p->truesize += delta_truesize;
3310 lp->data_len += len;
3311 lp->truesize += delta_truesize;
3314 NAPI_GRO_CB(skb)->same_flow = 1;
3318 void __init skb_init(void)
3320 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3321 sizeof(struct sk_buff),
3323 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3325 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3326 sizeof(struct sk_buff_fclones),
3328 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3333 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3334 * @skb: Socket buffer containing the buffers to be mapped
3335 * @sg: The scatter-gather list to map into
3336 * @offset: The offset into the buffer's contents to start mapping
3337 * @len: Length of buffer space to be mapped
3339 * Fill the specified scatter-gather list with mappings/pointers into a
3340 * region of the buffer space attached to a socket buffer.
3343 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3345 int start = skb_headlen(skb);
3346 int i, copy = start - offset;
3347 struct sk_buff *frag_iter;
3353 sg_set_buf(sg, skb->data + offset, copy);
3355 if ((len -= copy) == 0)
3360 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3363 WARN_ON(start > offset + len);
3365 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3366 if ((copy = end - offset) > 0) {
3367 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3371 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3372 frag->page_offset+offset-start);
3381 skb_walk_frags(skb, frag_iter) {
3384 WARN_ON(start > offset + len);
3386 end = start + frag_iter->len;
3387 if ((copy = end - offset) > 0) {
3390 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3392 if ((len -= copy) == 0)
3402 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3403 * sglist without mark the sg which contain last skb data as the end.
3404 * So the caller can mannipulate sg list as will when padding new data after
3405 * the first call without calling sg_unmark_end to expend sg list.
3407 * Scenario to use skb_to_sgvec_nomark:
3409 * 2. skb_to_sgvec_nomark(payload1)
3410 * 3. skb_to_sgvec_nomark(payload2)
3412 * This is equivalent to:
3414 * 2. skb_to_sgvec(payload1)
3416 * 4. skb_to_sgvec(payload2)
3418 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3419 * is more preferable.
3421 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3422 int offset, int len)
3424 return __skb_to_sgvec(skb, sg, offset, len);
3426 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3428 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3430 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3432 sg_mark_end(&sg[nsg - 1]);
3436 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3439 * skb_cow_data - Check that a socket buffer's data buffers are writable
3440 * @skb: The socket buffer to check.
3441 * @tailbits: Amount of trailing space to be added
3442 * @trailer: Returned pointer to the skb where the @tailbits space begins
3444 * Make sure that the data buffers attached to a socket buffer are
3445 * writable. If they are not, private copies are made of the data buffers
3446 * and the socket buffer is set to use these instead.
3448 * If @tailbits is given, make sure that there is space to write @tailbits
3449 * bytes of data beyond current end of socket buffer. @trailer will be
3450 * set to point to the skb in which this space begins.
3452 * The number of scatterlist elements required to completely map the
3453 * COW'd and extended socket buffer will be returned.
3455 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3459 struct sk_buff *skb1, **skb_p;
3461 /* If skb is cloned or its head is paged, reallocate
3462 * head pulling out all the pages (pages are considered not writable
3463 * at the moment even if they are anonymous).
3465 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3466 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3469 /* Easy case. Most of packets will go this way. */
3470 if (!skb_has_frag_list(skb)) {
3471 /* A little of trouble, not enough of space for trailer.
3472 * This should not happen, when stack is tuned to generate
3473 * good frames. OK, on miss we reallocate and reserve even more
3474 * space, 128 bytes is fair. */
3476 if (skb_tailroom(skb) < tailbits &&
3477 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3485 /* Misery. We are in troubles, going to mincer fragments... */
3488 skb_p = &skb_shinfo(skb)->frag_list;
3491 while ((skb1 = *skb_p) != NULL) {
3494 /* The fragment is partially pulled by someone,
3495 * this can happen on input. Copy it and everything
3498 if (skb_shared(skb1))
3501 /* If the skb is the last, worry about trailer. */
3503 if (skb1->next == NULL && tailbits) {
3504 if (skb_shinfo(skb1)->nr_frags ||
3505 skb_has_frag_list(skb1) ||
3506 skb_tailroom(skb1) < tailbits)
3507 ntail = tailbits + 128;
3513 skb_shinfo(skb1)->nr_frags ||
3514 skb_has_frag_list(skb1)) {
3515 struct sk_buff *skb2;
3517 /* Fuck, we are miserable poor guys... */
3519 skb2 = skb_copy(skb1, GFP_ATOMIC);
3521 skb2 = skb_copy_expand(skb1,
3525 if (unlikely(skb2 == NULL))
3529 skb_set_owner_w(skb2, skb1->sk);
3531 /* Looking around. Are we still alive?
3532 * OK, link new skb, drop old one */
3534 skb2->next = skb1->next;
3541 skb_p = &skb1->next;
3546 EXPORT_SYMBOL_GPL(skb_cow_data);
3548 static void sock_rmem_free(struct sk_buff *skb)
3550 struct sock *sk = skb->sk;
3552 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3556 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3558 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3560 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3561 (unsigned int)sk->sk_rcvbuf)
3566 skb->destructor = sock_rmem_free;
3567 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3569 /* before exiting rcu section, make sure dst is refcounted */
3572 skb_queue_tail(&sk->sk_error_queue, skb);
3573 if (!sock_flag(sk, SOCK_DEAD))
3574 sk->sk_data_ready(sk);
3577 EXPORT_SYMBOL(sock_queue_err_skb);
3579 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3581 struct sk_buff_head *q = &sk->sk_error_queue;
3582 struct sk_buff *skb, *skb_next;
3583 unsigned long flags;
3586 spin_lock_irqsave(&q->lock, flags);
3587 skb = __skb_dequeue(q);
3588 if (skb && (skb_next = skb_peek(q)))
3589 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3590 spin_unlock_irqrestore(&q->lock, flags);
3594 sk->sk_error_report(sk);
3598 EXPORT_SYMBOL(sock_dequeue_err_skb);
3601 * skb_clone_sk - create clone of skb, and take reference to socket
3602 * @skb: the skb to clone
3604 * This function creates a clone of a buffer that holds a reference on
3605 * sk_refcnt. Buffers created via this function are meant to be
3606 * returned using sock_queue_err_skb, or free via kfree_skb.
3608 * When passing buffers allocated with this function to sock_queue_err_skb
3609 * it is necessary to wrap the call with sock_hold/sock_put in order to
3610 * prevent the socket from being released prior to being enqueued on
3611 * the sk_error_queue.
3613 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3615 struct sock *sk = skb->sk;
3616 struct sk_buff *clone;
3618 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3621 clone = skb_clone(skb, GFP_ATOMIC);
3628 clone->destructor = sock_efree;
3632 EXPORT_SYMBOL(skb_clone_sk);
3634 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3638 struct sock_exterr_skb *serr;
3641 serr = SKB_EXT_ERR(skb);
3642 memset(serr, 0, sizeof(*serr));
3643 serr->ee.ee_errno = ENOMSG;
3644 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3645 serr->ee.ee_info = tstype;
3646 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3647 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3648 if (sk->sk_protocol == IPPROTO_TCP &&
3649 sk->sk_type == SOCK_STREAM)
3650 serr->ee.ee_data -= sk->sk_tskey;
3653 err = sock_queue_err_skb(sk, skb);
3659 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3663 if (likely(sysctl_tstamp_allow_data || tsonly))
3666 read_lock_bh(&sk->sk_callback_lock);
3667 ret = sk->sk_socket && sk->sk_socket->file &&
3668 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3669 read_unlock_bh(&sk->sk_callback_lock);
3673 void skb_complete_tx_timestamp(struct sk_buff *skb,
3674 struct skb_shared_hwtstamps *hwtstamps)
3676 struct sock *sk = skb->sk;
3678 if (!skb_may_tx_timestamp(sk, false))
3681 /* take a reference to prevent skb_orphan() from freeing the socket */
3684 *skb_hwtstamps(skb) = *hwtstamps;
3685 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3689 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3691 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3692 struct skb_shared_hwtstamps *hwtstamps,
3693 struct sock *sk, int tstype)
3695 struct sk_buff *skb;
3701 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3702 if (!skb_may_tx_timestamp(sk, tsonly))
3706 skb = alloc_skb(0, GFP_ATOMIC);
3708 skb = skb_clone(orig_skb, GFP_ATOMIC);
3713 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3714 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3718 *skb_hwtstamps(skb) = *hwtstamps;
3720 skb->tstamp = ktime_get_real();
3722 __skb_complete_tx_timestamp(skb, sk, tstype);
3724 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3726 void skb_tstamp_tx(struct sk_buff *orig_skb,
3727 struct skb_shared_hwtstamps *hwtstamps)
3729 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3732 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3734 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3736 struct sock *sk = skb->sk;
3737 struct sock_exterr_skb *serr;
3740 skb->wifi_acked_valid = 1;
3741 skb->wifi_acked = acked;
3743 serr = SKB_EXT_ERR(skb);
3744 memset(serr, 0, sizeof(*serr));
3745 serr->ee.ee_errno = ENOMSG;
3746 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3748 /* take a reference to prevent skb_orphan() from freeing the socket */
3751 err = sock_queue_err_skb(sk, skb);
3757 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3760 * skb_partial_csum_set - set up and verify partial csum values for packet
3761 * @skb: the skb to set
3762 * @start: the number of bytes after skb->data to start checksumming.
3763 * @off: the offset from start to place the checksum.
3765 * For untrusted partially-checksummed packets, we need to make sure the values
3766 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3768 * This function checks and sets those values and skb->ip_summed: if this
3769 * returns false you should drop the packet.
3771 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3773 if (unlikely(start > skb_headlen(skb)) ||
3774 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3775 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3776 start, off, skb_headlen(skb));
3779 skb->ip_summed = CHECKSUM_PARTIAL;
3780 skb->csum_start = skb_headroom(skb) + start;
3781 skb->csum_offset = off;
3782 skb_set_transport_header(skb, start);
3785 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3787 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3790 if (skb_headlen(skb) >= len)
3793 /* If we need to pullup then pullup to the max, so we
3794 * won't need to do it again.
3799 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3802 if (skb_headlen(skb) < len)
3808 #define MAX_TCP_HDR_LEN (15 * 4)
3810 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3811 typeof(IPPROTO_IP) proto,
3818 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3819 off + MAX_TCP_HDR_LEN);
3820 if (!err && !skb_partial_csum_set(skb, off,
3821 offsetof(struct tcphdr,
3824 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3827 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3828 off + sizeof(struct udphdr));
3829 if (!err && !skb_partial_csum_set(skb, off,
3830 offsetof(struct udphdr,
3833 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3836 return ERR_PTR(-EPROTO);
3839 /* This value should be large enough to cover a tagged ethernet header plus
3840 * maximally sized IP and TCP or UDP headers.
3842 #define MAX_IP_HDR_LEN 128
3844 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3853 err = skb_maybe_pull_tail(skb,
3854 sizeof(struct iphdr),
3859 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3862 off = ip_hdrlen(skb);
3869 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3871 return PTR_ERR(csum);
3874 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3877 ip_hdr(skb)->protocol, 0);
3884 /* This value should be large enough to cover a tagged ethernet header plus
3885 * an IPv6 header, all options, and a maximal TCP or UDP header.
3887 #define MAX_IPV6_HDR_LEN 256
3889 #define OPT_HDR(type, skb, off) \
3890 (type *)(skb_network_header(skb) + (off))
3892 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3905 off = sizeof(struct ipv6hdr);
3907 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3911 nexthdr = ipv6_hdr(skb)->nexthdr;
3913 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3914 while (off <= len && !done) {
3916 case IPPROTO_DSTOPTS:
3917 case IPPROTO_HOPOPTS:
3918 case IPPROTO_ROUTING: {
3919 struct ipv6_opt_hdr *hp;
3921 err = skb_maybe_pull_tail(skb,
3923 sizeof(struct ipv6_opt_hdr),
3928 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3929 nexthdr = hp->nexthdr;
3930 off += ipv6_optlen(hp);
3934 struct ip_auth_hdr *hp;
3936 err = skb_maybe_pull_tail(skb,
3938 sizeof(struct ip_auth_hdr),
3943 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3944 nexthdr = hp->nexthdr;
3945 off += ipv6_authlen(hp);
3948 case IPPROTO_FRAGMENT: {
3949 struct frag_hdr *hp;
3951 err = skb_maybe_pull_tail(skb,
3953 sizeof(struct frag_hdr),
3958 hp = OPT_HDR(struct frag_hdr, skb, off);
3960 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3963 nexthdr = hp->nexthdr;
3964 off += sizeof(struct frag_hdr);
3975 if (!done || fragment)
3978 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3980 return PTR_ERR(csum);
3983 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3984 &ipv6_hdr(skb)->daddr,
3985 skb->len - off, nexthdr, 0);
3993 * skb_checksum_setup - set up partial checksum offset
3994 * @skb: the skb to set up
3995 * @recalculate: if true the pseudo-header checksum will be recalculated
3997 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4001 switch (skb->protocol) {
4002 case htons(ETH_P_IP):
4003 err = skb_checksum_setup_ipv4(skb, recalculate);
4006 case htons(ETH_P_IPV6):
4007 err = skb_checksum_setup_ipv6(skb, recalculate);
4017 EXPORT_SYMBOL(skb_checksum_setup);
4020 * skb_checksum_maybe_trim - maybe trims the given skb
4021 * @skb: the skb to check
4022 * @transport_len: the data length beyond the network header
4024 * Checks whether the given skb has data beyond the given transport length.
4025 * If so, returns a cloned skb trimmed to this transport length.
4026 * Otherwise returns the provided skb. Returns NULL in error cases
4027 * (e.g. transport_len exceeds skb length or out-of-memory).
4029 * Caller needs to set the skb transport header and free any returned skb if it
4030 * differs from the provided skb.
4032 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4033 unsigned int transport_len)
4035 struct sk_buff *skb_chk;
4036 unsigned int len = skb_transport_offset(skb) + transport_len;
4041 else if (skb->len == len)
4044 skb_chk = skb_clone(skb, GFP_ATOMIC);
4048 ret = pskb_trim_rcsum(skb_chk, len);
4058 * skb_checksum_trimmed - validate checksum of an skb
4059 * @skb: the skb to check
4060 * @transport_len: the data length beyond the network header
4061 * @skb_chkf: checksum function to use
4063 * Applies the given checksum function skb_chkf to the provided skb.
4064 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4066 * If the skb has data beyond the given transport length, then a
4067 * trimmed & cloned skb is checked and returned.
4069 * Caller needs to set the skb transport header and free any returned skb if it
4070 * differs from the provided skb.
4072 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4073 unsigned int transport_len,
4074 __sum16(*skb_chkf)(struct sk_buff *skb))
4076 struct sk_buff *skb_chk;
4077 unsigned int offset = skb_transport_offset(skb);
4080 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4084 if (!pskb_may_pull(skb_chk, offset))
4087 __skb_pull(skb_chk, offset);
4088 ret = skb_chkf(skb_chk);
4089 __skb_push(skb_chk, offset);
4097 if (skb_chk && skb_chk != skb)
4103 EXPORT_SYMBOL(skb_checksum_trimmed);
4105 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4107 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4110 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4112 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4115 skb_release_head_state(skb);
4116 kmem_cache_free(skbuff_head_cache, skb);
4121 EXPORT_SYMBOL(kfree_skb_partial);
4124 * skb_try_coalesce - try to merge skb to prior one
4126 * @from: buffer to add
4127 * @fragstolen: pointer to boolean
4128 * @delta_truesize: how much more was allocated than was requested
4130 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4131 bool *fragstolen, int *delta_truesize)
4133 int i, delta, len = from->len;
4135 *fragstolen = false;
4140 if (len <= skb_tailroom(to)) {
4142 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4143 *delta_truesize = 0;
4147 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4150 if (skb_headlen(from) != 0) {
4152 unsigned int offset;
4154 if (skb_shinfo(to)->nr_frags +
4155 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4158 if (skb_head_is_locked(from))
4161 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4163 page = virt_to_head_page(from->head);
4164 offset = from->data - (unsigned char *)page_address(page);
4166 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4167 page, offset, skb_headlen(from));
4170 if (skb_shinfo(to)->nr_frags +
4171 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4174 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4177 WARN_ON_ONCE(delta < len);
4179 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4180 skb_shinfo(from)->frags,
4181 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4182 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4184 if (!skb_cloned(from))
4185 skb_shinfo(from)->nr_frags = 0;
4187 /* if the skb is not cloned this does nothing
4188 * since we set nr_frags to 0.
4190 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4191 skb_frag_ref(from, i);
4193 to->truesize += delta;
4195 to->data_len += len;
4197 *delta_truesize = delta;
4200 EXPORT_SYMBOL(skb_try_coalesce);
4203 * skb_scrub_packet - scrub an skb
4205 * @skb: buffer to clean
4206 * @xnet: packet is crossing netns
4208 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4209 * into/from a tunnel. Some information have to be cleared during these
4211 * skb_scrub_packet can also be used to clean a skb before injecting it in
4212 * another namespace (@xnet == true). We have to clear all information in the
4213 * skb that could impact namespace isolation.
4215 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4217 skb->tstamp.tv64 = 0;
4218 skb->pkt_type = PACKET_HOST;
4222 skb_sender_cpu_clear(skb);
4225 nf_reset_trace(skb);
4233 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4236 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4240 * skb_gso_transport_seglen is used to determine the real size of the
4241 * individual segments, including Layer4 headers (TCP/UDP).
4243 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4245 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4247 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4248 unsigned int thlen = 0;
4250 if (skb->encapsulation) {
4251 thlen = skb_inner_transport_header(skb) -
4252 skb_transport_header(skb);
4254 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4255 thlen += inner_tcp_hdrlen(skb);
4256 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4257 thlen = tcp_hdrlen(skb);
4259 /* UFO sets gso_size to the size of the fragmentation
4260 * payload, i.e. the size of the L4 (UDP) header is already
4263 return thlen + shinfo->gso_size;
4265 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4267 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4269 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4274 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4276 skb->mac_header += VLAN_HLEN;
4280 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4282 struct vlan_hdr *vhdr;
4285 if (unlikely(skb_vlan_tag_present(skb))) {
4286 /* vlan_tci is already set-up so leave this for another time */
4290 skb = skb_share_check(skb, GFP_ATOMIC);
4294 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4297 vhdr = (struct vlan_hdr *)skb->data;
4298 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4299 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4301 skb_pull_rcsum(skb, VLAN_HLEN);
4302 vlan_set_encap_proto(skb, vhdr);
4304 skb = skb_reorder_vlan_header(skb);
4308 skb_reset_network_header(skb);
4309 skb_reset_transport_header(skb);
4310 skb_reset_mac_len(skb);
4318 EXPORT_SYMBOL(skb_vlan_untag);
4320 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4322 if (!pskb_may_pull(skb, write_len))
4325 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4328 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4330 EXPORT_SYMBOL(skb_ensure_writable);
4332 /* remove VLAN header from packet and update csum accordingly. */
4333 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4335 struct vlan_hdr *vhdr;
4336 unsigned int offset = skb->data - skb_mac_header(skb);
4339 __skb_push(skb, offset);
4340 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4344 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4346 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4347 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4349 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4350 __skb_pull(skb, VLAN_HLEN);
4352 vlan_set_encap_proto(skb, vhdr);
4353 skb->mac_header += VLAN_HLEN;
4355 if (skb_network_offset(skb) < ETH_HLEN)
4356 skb_set_network_header(skb, ETH_HLEN);
4358 skb_reset_mac_len(skb);
4360 __skb_pull(skb, offset);
4365 int skb_vlan_pop(struct sk_buff *skb)
4371 if (likely(skb_vlan_tag_present(skb))) {
4374 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4375 skb->protocol != htons(ETH_P_8021AD)) ||
4376 skb->len < VLAN_ETH_HLEN))
4379 err = __skb_vlan_pop(skb, &vlan_tci);
4383 /* move next vlan tag to hw accel tag */
4384 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4385 skb->protocol != htons(ETH_P_8021AD)) ||
4386 skb->len < VLAN_ETH_HLEN))
4389 vlan_proto = skb->protocol;
4390 err = __skb_vlan_pop(skb, &vlan_tci);
4394 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4397 EXPORT_SYMBOL(skb_vlan_pop);
4399 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4401 if (skb_vlan_tag_present(skb)) {
4402 unsigned int offset = skb->data - skb_mac_header(skb);
4405 /* __vlan_insert_tag expect skb->data pointing to mac header.
4406 * So change skb->data before calling it and change back to
4407 * original position later
4409 __skb_push(skb, offset);
4410 err = __vlan_insert_tag(skb, skb->vlan_proto,
4411 skb_vlan_tag_get(skb));
4414 skb->protocol = skb->vlan_proto;
4415 skb->mac_len += VLAN_HLEN;
4416 __skb_pull(skb, offset);
4418 if (skb->ip_summed == CHECKSUM_COMPLETE)
4419 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4420 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4422 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4425 EXPORT_SYMBOL(skb_vlan_push);
4428 * alloc_skb_with_frags - allocate skb with page frags
4430 * @header_len: size of linear part
4431 * @data_len: needed length in frags
4432 * @max_page_order: max page order desired.
4433 * @errcode: pointer to error code if any
4434 * @gfp_mask: allocation mask
4436 * This can be used to allocate a paged skb, given a maximal order for frags.
4438 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4439 unsigned long data_len,
4444 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4445 unsigned long chunk;
4446 struct sk_buff *skb;
4451 *errcode = -EMSGSIZE;
4452 /* Note this test could be relaxed, if we succeed to allocate
4453 * high order pages...
4455 if (npages > MAX_SKB_FRAGS)
4458 gfp_head = gfp_mask;
4459 if (gfp_head & __GFP_DIRECT_RECLAIM)
4460 gfp_head |= __GFP_REPEAT;
4462 *errcode = -ENOBUFS;
4463 skb = alloc_skb(header_len, gfp_head);
4467 skb->truesize += npages << PAGE_SHIFT;
4469 for (i = 0; npages > 0; i++) {
4470 int order = max_page_order;
4473 if (npages >= 1 << order) {
4474 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4481 /* Do not retry other high order allocations */
4487 page = alloc_page(gfp_mask);
4491 chunk = min_t(unsigned long, data_len,
4492 PAGE_SIZE << order);
4493 skb_fill_page_desc(skb, i, page, 0, chunk);
4495 npages -= 1 << order;
4503 EXPORT_SYMBOL(alloc_skb_with_frags);