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/sctp.h>
53 #include <linux/netdevice.h>
54 #ifdef CONFIG_NET_CLS_ACT
55 #include <net/pkt_sched.h>
57 #include <linux/string.h>
58 #include <linux/skbuff.h>
59 #include <linux/splice.h>
60 #include <linux/cache.h>
61 #include <linux/rtnetlink.h>
62 #include <linux/init.h>
63 #include <linux/scatterlist.h>
64 #include <linux/errqueue.h>
65 #include <linux/prefetch.h>
66 #include <linux/if_vlan.h>
68 #include <net/protocol.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
75 #include <linux/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
83 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84 EXPORT_SYMBOL(sysctl_max_skb_frags);
87 * skb_panic - private function for out-of-line support
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
108 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
113 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115 skb_panic(skb, sz, addr, __func__);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
132 bool ret_pfmemalloc = false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
150 *pfmemalloc = ret_pfmemalloc;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
162 * __alloc_skb - allocate a network buffer
163 * @size: size to allocate
164 * @gfp_mask: allocation mask
165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
169 * @node: numa node to allocate memory on
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
178 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
181 struct kmem_cache *cache;
182 struct skb_shared_info *shinfo;
187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
204 size = SKB_DATA_ALIGN(size);
205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
213 size = SKB_WITH_OVERHEAD(ksize(data));
214 prefetchw(data + size);
217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
221 memset(skb, 0, offsetof(struct sk_buff, tail));
222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
224 skb->pfmemalloc = pfmemalloc;
225 refcount_set(&skb->users, 1);
228 skb_reset_tail_pointer(skb);
229 skb->end = skb->tail + size;
230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
236 atomic_set(&shinfo->dataref, 1);
237 kmemcheck_annotate_variable(shinfo->destructor_arg);
239 if (flags & SKB_ALLOC_FCLONE) {
240 struct sk_buff_fclones *fclones;
242 fclones = container_of(skb, struct sk_buff_fclones, skb1);
244 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
245 skb->fclone = SKB_FCLONE_ORIG;
246 refcount_set(&fclones->fclone_ref, 1);
248 fclones->skb2.fclone = SKB_FCLONE_CLONE;
253 kmem_cache_free(cache, skb);
257 EXPORT_SYMBOL(__alloc_skb);
260 * __build_skb - build a network buffer
261 * @data: data buffer provided by caller
262 * @frag_size: size of data, or 0 if head was kmalloced
264 * Allocate a new &sk_buff. Caller provides space holding head and
265 * skb_shared_info. @data must have been allocated by kmalloc() only if
266 * @frag_size is 0, otherwise data should come from the page allocator
268 * The return is the new skb buffer.
269 * On a failure the return is %NULL, and @data is not freed.
271 * Before IO, driver allocates only data buffer where NIC put incoming frame
272 * Driver should add room at head (NET_SKB_PAD) and
273 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
274 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
275 * before giving packet to stack.
276 * RX rings only contains data buffers, not full skbs.
278 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
280 struct skb_shared_info *shinfo;
282 unsigned int size = frag_size ? : ksize(data);
284 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
288 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
290 memset(skb, 0, offsetof(struct sk_buff, tail));
291 skb->truesize = SKB_TRUESIZE(size);
292 refcount_set(&skb->users, 1);
295 skb_reset_tail_pointer(skb);
296 skb->end = skb->tail + size;
297 skb->mac_header = (typeof(skb->mac_header))~0U;
298 skb->transport_header = (typeof(skb->transport_header))~0U;
300 /* make sure we initialize shinfo sequentially */
301 shinfo = skb_shinfo(skb);
302 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
303 atomic_set(&shinfo->dataref, 1);
304 kmemcheck_annotate_variable(shinfo->destructor_arg);
309 /* build_skb() is wrapper over __build_skb(), that specifically
310 * takes care of skb->head and skb->pfmemalloc
311 * This means that if @frag_size is not zero, then @data must be backed
312 * by a page fragment, not kmalloc() or vmalloc()
314 struct sk_buff *build_skb(void *data, unsigned int frag_size)
316 struct sk_buff *skb = __build_skb(data, frag_size);
318 if (skb && frag_size) {
320 if (page_is_pfmemalloc(virt_to_head_page(data)))
325 EXPORT_SYMBOL(build_skb);
327 #define NAPI_SKB_CACHE_SIZE 64
329 struct napi_alloc_cache {
330 struct page_frag_cache page;
331 unsigned int skb_count;
332 void *skb_cache[NAPI_SKB_CACHE_SIZE];
335 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
336 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
338 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
340 struct page_frag_cache *nc;
344 local_irq_save(flags);
345 nc = this_cpu_ptr(&netdev_alloc_cache);
346 data = page_frag_alloc(nc, fragsz, gfp_mask);
347 local_irq_restore(flags);
352 * netdev_alloc_frag - allocate a page fragment
353 * @fragsz: fragment size
355 * Allocates a frag from a page for receive buffer.
356 * Uses GFP_ATOMIC allocations.
358 void *netdev_alloc_frag(unsigned int fragsz)
360 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
362 EXPORT_SYMBOL(netdev_alloc_frag);
364 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
366 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
368 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
371 void *napi_alloc_frag(unsigned int fragsz)
373 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
375 EXPORT_SYMBOL(napi_alloc_frag);
378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
380 * @len: length to allocate
381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
388 * %NULL is returned if there is no free memory.
390 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
393 struct page_frag_cache *nc;
401 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
402 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
403 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
409 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
410 len = SKB_DATA_ALIGN(len);
412 if (sk_memalloc_socks())
413 gfp_mask |= __GFP_MEMALLOC;
415 local_irq_save(flags);
417 nc = this_cpu_ptr(&netdev_alloc_cache);
418 data = page_frag_alloc(nc, len, gfp_mask);
419 pfmemalloc = nc->pfmemalloc;
421 local_irq_restore(flags);
426 skb = __build_skb(data, len);
427 if (unlikely(!skb)) {
432 /* use OR instead of assignment to avoid clearing of bits in mask */
438 skb_reserve(skb, NET_SKB_PAD);
444 EXPORT_SYMBOL(__netdev_alloc_skb);
447 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
448 * @napi: napi instance this buffer was allocated for
449 * @len: length to allocate
450 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
452 * Allocate a new sk_buff for use in NAPI receive. This buffer will
453 * attempt to allocate the head from a special reserved region used
454 * only for NAPI Rx allocation. By doing this we can save several
455 * CPU cycles by avoiding having to disable and re-enable IRQs.
457 * %NULL is returned if there is no free memory.
459 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
462 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
466 len += NET_SKB_PAD + NET_IP_ALIGN;
468 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
469 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
470 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
476 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
477 len = SKB_DATA_ALIGN(len);
479 if (sk_memalloc_socks())
480 gfp_mask |= __GFP_MEMALLOC;
482 data = page_frag_alloc(&nc->page, len, gfp_mask);
486 skb = __build_skb(data, len);
487 if (unlikely(!skb)) {
492 /* use OR instead of assignment to avoid clearing of bits in mask */
493 if (nc->page.pfmemalloc)
498 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
499 skb->dev = napi->dev;
504 EXPORT_SYMBOL(__napi_alloc_skb);
506 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
507 int size, unsigned int truesize)
509 skb_fill_page_desc(skb, i, page, off, size);
511 skb->data_len += size;
512 skb->truesize += truesize;
514 EXPORT_SYMBOL(skb_add_rx_frag);
516 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
517 unsigned int truesize)
519 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
521 skb_frag_size_add(frag, size);
523 skb->data_len += size;
524 skb->truesize += truesize;
526 EXPORT_SYMBOL(skb_coalesce_rx_frag);
528 static void skb_drop_list(struct sk_buff **listp)
530 kfree_skb_list(*listp);
534 static inline void skb_drop_fraglist(struct sk_buff *skb)
536 skb_drop_list(&skb_shinfo(skb)->frag_list);
539 static void skb_clone_fraglist(struct sk_buff *skb)
541 struct sk_buff *list;
543 skb_walk_frags(skb, list)
547 static void skb_free_head(struct sk_buff *skb)
549 unsigned char *head = skb->head;
557 static void skb_release_data(struct sk_buff *skb)
559 struct skb_shared_info *shinfo = skb_shinfo(skb);
563 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
567 for (i = 0; i < shinfo->nr_frags; i++)
568 __skb_frag_unref(&shinfo->frags[i]);
571 * If skb buf is from userspace, we need to notify the caller
572 * the lower device DMA has done;
574 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
575 struct ubuf_info *uarg;
577 uarg = shinfo->destructor_arg;
579 uarg->callback(uarg, true);
582 if (shinfo->frag_list)
583 kfree_skb_list(shinfo->frag_list);
589 * Free an skbuff by memory without cleaning the state.
591 static void kfree_skbmem(struct sk_buff *skb)
593 struct sk_buff_fclones *fclones;
595 switch (skb->fclone) {
596 case SKB_FCLONE_UNAVAILABLE:
597 kmem_cache_free(skbuff_head_cache, skb);
600 case SKB_FCLONE_ORIG:
601 fclones = container_of(skb, struct sk_buff_fclones, skb1);
603 /* We usually free the clone (TX completion) before original skb
604 * This test would have no chance to be true for the clone,
605 * while here, branch prediction will be good.
607 if (refcount_read(&fclones->fclone_ref) == 1)
611 default: /* SKB_FCLONE_CLONE */
612 fclones = container_of(skb, struct sk_buff_fclones, skb2);
615 if (!refcount_dec_and_test(&fclones->fclone_ref))
618 kmem_cache_free(skbuff_fclone_cache, fclones);
621 void skb_release_head_state(struct sk_buff *skb)
625 if (skb->destructor) {
627 skb->destructor(skb);
629 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
630 nf_conntrack_put(skb_nfct(skb));
632 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
633 nf_bridge_put(skb->nf_bridge);
637 /* Free everything but the sk_buff shell. */
638 static void skb_release_all(struct sk_buff *skb)
640 skb_release_head_state(skb);
641 skb_release_data(skb);
645 * __kfree_skb - private function
648 * Free an sk_buff. Release anything attached to the buffer.
649 * Clean the state. This is an internal helper function. Users should
650 * always call kfree_skb
653 void __kfree_skb(struct sk_buff *skb)
655 skb_release_all(skb);
658 EXPORT_SYMBOL(__kfree_skb);
661 * kfree_skb - free an sk_buff
662 * @skb: buffer to free
664 * Drop a reference to the buffer and free it if the usage count has
667 void kfree_skb(struct sk_buff *skb)
672 trace_kfree_skb(skb, __builtin_return_address(0));
675 EXPORT_SYMBOL(kfree_skb);
677 void kfree_skb_list(struct sk_buff *segs)
680 struct sk_buff *next = segs->next;
686 EXPORT_SYMBOL(kfree_skb_list);
689 * skb_tx_error - report an sk_buff xmit error
690 * @skb: buffer that triggered an error
692 * Report xmit error if a device callback is tracking this skb.
693 * skb must be freed afterwards.
695 void skb_tx_error(struct sk_buff *skb)
697 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
698 struct ubuf_info *uarg;
700 uarg = skb_shinfo(skb)->destructor_arg;
702 uarg->callback(uarg, false);
703 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
706 EXPORT_SYMBOL(skb_tx_error);
709 * consume_skb - free an skbuff
710 * @skb: buffer to free
712 * Drop a ref to the buffer and free it if the usage count has hit zero
713 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
714 * is being dropped after a failure and notes that
716 void consume_skb(struct sk_buff *skb)
721 trace_consume_skb(skb);
724 EXPORT_SYMBOL(consume_skb);
727 * consume_stateless_skb - free an skbuff, assuming it is stateless
728 * @skb: buffer to free
730 * Works like consume_skb(), but this variant assumes that all the head
731 * states have been already dropped.
733 void consume_stateless_skb(struct sk_buff *skb)
738 trace_consume_skb(skb);
739 skb_release_data(skb);
743 void __kfree_skb_flush(void)
745 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
747 /* flush skb_cache if containing objects */
749 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
755 static inline void _kfree_skb_defer(struct sk_buff *skb)
757 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
759 /* drop skb->head and call any destructors for packet */
760 skb_release_all(skb);
762 /* record skb to CPU local list */
763 nc->skb_cache[nc->skb_count++] = skb;
766 /* SLUB writes into objects when freeing */
770 /* flush skb_cache if it is filled */
771 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
772 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
777 void __kfree_skb_defer(struct sk_buff *skb)
779 _kfree_skb_defer(skb);
782 void napi_consume_skb(struct sk_buff *skb, int budget)
787 /* Zero budget indicate non-NAPI context called us, like netpoll */
788 if (unlikely(!budget)) {
789 dev_consume_skb_any(skb);
796 /* if reaching here SKB is ready to free */
797 trace_consume_skb(skb);
799 /* if SKB is a clone, don't handle this case */
800 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
805 _kfree_skb_defer(skb);
807 EXPORT_SYMBOL(napi_consume_skb);
809 /* Make sure a field is enclosed inside headers_start/headers_end section */
810 #define CHECK_SKB_FIELD(field) \
811 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
812 offsetof(struct sk_buff, headers_start)); \
813 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
814 offsetof(struct sk_buff, headers_end)); \
816 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
818 new->tstamp = old->tstamp;
819 /* We do not copy old->sk */
821 memcpy(new->cb, old->cb, sizeof(old->cb));
822 skb_dst_copy(new, old);
824 new->sp = secpath_get(old->sp);
826 __nf_copy(new, old, false);
828 /* Note : this field could be in headers_start/headers_end section
829 * It is not yet because we do not want to have a 16 bit hole
831 new->queue_mapping = old->queue_mapping;
833 memcpy(&new->headers_start, &old->headers_start,
834 offsetof(struct sk_buff, headers_end) -
835 offsetof(struct sk_buff, headers_start));
836 CHECK_SKB_FIELD(protocol);
837 CHECK_SKB_FIELD(csum);
838 CHECK_SKB_FIELD(hash);
839 CHECK_SKB_FIELD(priority);
840 CHECK_SKB_FIELD(skb_iif);
841 CHECK_SKB_FIELD(vlan_proto);
842 CHECK_SKB_FIELD(vlan_tci);
843 CHECK_SKB_FIELD(transport_header);
844 CHECK_SKB_FIELD(network_header);
845 CHECK_SKB_FIELD(mac_header);
846 CHECK_SKB_FIELD(inner_protocol);
847 CHECK_SKB_FIELD(inner_transport_header);
848 CHECK_SKB_FIELD(inner_network_header);
849 CHECK_SKB_FIELD(inner_mac_header);
850 CHECK_SKB_FIELD(mark);
851 #ifdef CONFIG_NETWORK_SECMARK
852 CHECK_SKB_FIELD(secmark);
854 #ifdef CONFIG_NET_RX_BUSY_POLL
855 CHECK_SKB_FIELD(napi_id);
858 CHECK_SKB_FIELD(sender_cpu);
860 #ifdef CONFIG_NET_SCHED
861 CHECK_SKB_FIELD(tc_index);
867 * You should not add any new code to this function. Add it to
868 * __copy_skb_header above instead.
870 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
872 #define C(x) n->x = skb->x
874 n->next = n->prev = NULL;
876 __copy_skb_header(n, skb);
881 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
884 n->destructor = NULL;
891 refcount_set(&n->users, 1);
893 atomic_inc(&(skb_shinfo(skb)->dataref));
901 * skb_morph - morph one skb into another
902 * @dst: the skb to receive the contents
903 * @src: the skb to supply the contents
905 * This is identical to skb_clone except that the target skb is
906 * supplied by the user.
908 * The target skb is returned upon exit.
910 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
912 skb_release_all(dst);
913 return __skb_clone(dst, src);
915 EXPORT_SYMBOL_GPL(skb_morph);
918 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
919 * @skb: the skb to modify
920 * @gfp_mask: allocation priority
922 * This must be called on SKBTX_DEV_ZEROCOPY skb.
923 * It will copy all frags into kernel and drop the reference
924 * to userspace pages.
926 * If this function is called from an interrupt gfp_mask() must be
929 * Returns 0 on success or a negative error code on failure
930 * to allocate kernel memory to copy to.
932 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
935 int num_frags = skb_shinfo(skb)->nr_frags;
936 struct page *page, *head = NULL;
937 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
939 for (i = 0; i < num_frags; i++) {
941 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
943 page = alloc_page(gfp_mask);
946 struct page *next = (struct page *)page_private(head);
952 vaddr = kmap_atomic(skb_frag_page(f));
953 memcpy(page_address(page),
954 vaddr + f->page_offset, skb_frag_size(f));
955 kunmap_atomic(vaddr);
956 set_page_private(page, (unsigned long)head);
960 /* skb frags release userspace buffers */
961 for (i = 0; i < num_frags; i++)
962 skb_frag_unref(skb, i);
964 uarg->callback(uarg, false);
966 /* skb frags point to kernel buffers */
967 for (i = num_frags - 1; i >= 0; i--) {
968 __skb_fill_page_desc(skb, i, head, 0,
969 skb_shinfo(skb)->frags[i].size);
970 head = (struct page *)page_private(head);
973 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
976 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
979 * skb_clone - duplicate an sk_buff
980 * @skb: buffer to clone
981 * @gfp_mask: allocation priority
983 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
984 * copies share the same packet data but not structure. The new
985 * buffer has a reference count of 1. If the allocation fails the
986 * function returns %NULL otherwise the new buffer is returned.
988 * If this function is called from an interrupt gfp_mask() must be
992 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
994 struct sk_buff_fclones *fclones = container_of(skb,
995 struct sk_buff_fclones,
999 if (skb_orphan_frags(skb, gfp_mask))
1002 if (skb->fclone == SKB_FCLONE_ORIG &&
1003 refcount_read(&fclones->fclone_ref) == 1) {
1005 refcount_set(&fclones->fclone_ref, 2);
1007 if (skb_pfmemalloc(skb))
1008 gfp_mask |= __GFP_MEMALLOC;
1010 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1014 kmemcheck_annotate_bitfield(n, flags1);
1015 n->fclone = SKB_FCLONE_UNAVAILABLE;
1018 return __skb_clone(n, skb);
1020 EXPORT_SYMBOL(skb_clone);
1022 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1024 /* Only adjust this if it actually is csum_start rather than csum */
1025 if (skb->ip_summed == CHECKSUM_PARTIAL)
1026 skb->csum_start += off;
1027 /* {transport,network,mac}_header and tail are relative to skb->head */
1028 skb->transport_header += off;
1029 skb->network_header += off;
1030 if (skb_mac_header_was_set(skb))
1031 skb->mac_header += off;
1032 skb->inner_transport_header += off;
1033 skb->inner_network_header += off;
1034 skb->inner_mac_header += off;
1037 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1039 __copy_skb_header(new, old);
1041 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1042 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1043 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1046 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1048 if (skb_pfmemalloc(skb))
1049 return SKB_ALLOC_RX;
1054 * skb_copy - create private copy of an sk_buff
1055 * @skb: buffer to copy
1056 * @gfp_mask: allocation priority
1058 * Make a copy of both an &sk_buff and its data. This is used when the
1059 * caller wishes to modify the data and needs a private copy of the
1060 * data to alter. Returns %NULL on failure or the pointer to the buffer
1061 * on success. The returned buffer has a reference count of 1.
1063 * As by-product this function converts non-linear &sk_buff to linear
1064 * one, so that &sk_buff becomes completely private and caller is allowed
1065 * to modify all the data of returned buffer. This means that this
1066 * function is not recommended for use in circumstances when only
1067 * header is going to be modified. Use pskb_copy() instead.
1070 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1072 int headerlen = skb_headroom(skb);
1073 unsigned int size = skb_end_offset(skb) + skb->data_len;
1074 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1075 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1080 /* Set the data pointer */
1081 skb_reserve(n, headerlen);
1082 /* Set the tail pointer and length */
1083 skb_put(n, skb->len);
1085 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1088 copy_skb_header(n, skb);
1091 EXPORT_SYMBOL(skb_copy);
1094 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1095 * @skb: buffer to copy
1096 * @headroom: headroom of new skb
1097 * @gfp_mask: allocation priority
1098 * @fclone: if true allocate the copy of the skb from the fclone
1099 * cache instead of the head cache; it is recommended to set this
1100 * to true for the cases where the copy will likely be cloned
1102 * Make a copy of both an &sk_buff and part of its data, located
1103 * in header. Fragmented data remain shared. This is used when
1104 * the caller wishes to modify only header of &sk_buff and needs
1105 * private copy of the header to alter. Returns %NULL on failure
1106 * or the pointer to the buffer on success.
1107 * The returned buffer has a reference count of 1.
1110 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1111 gfp_t gfp_mask, bool fclone)
1113 unsigned int size = skb_headlen(skb) + headroom;
1114 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1115 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1120 /* Set the data pointer */
1121 skb_reserve(n, headroom);
1122 /* Set the tail pointer and length */
1123 skb_put(n, skb_headlen(skb));
1124 /* Copy the bytes */
1125 skb_copy_from_linear_data(skb, n->data, n->len);
1127 n->truesize += skb->data_len;
1128 n->data_len = skb->data_len;
1131 if (skb_shinfo(skb)->nr_frags) {
1134 if (skb_orphan_frags(skb, gfp_mask)) {
1139 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1140 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1141 skb_frag_ref(skb, i);
1143 skb_shinfo(n)->nr_frags = i;
1146 if (skb_has_frag_list(skb)) {
1147 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1148 skb_clone_fraglist(n);
1151 copy_skb_header(n, skb);
1155 EXPORT_SYMBOL(__pskb_copy_fclone);
1158 * pskb_expand_head - reallocate header of &sk_buff
1159 * @skb: buffer to reallocate
1160 * @nhead: room to add at head
1161 * @ntail: room to add at tail
1162 * @gfp_mask: allocation priority
1164 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1165 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1166 * reference count of 1. Returns zero in the case of success or error,
1167 * if expansion failed. In the last case, &sk_buff is not changed.
1169 * All the pointers pointing into skb header may change and must be
1170 * reloaded after call to this function.
1173 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1176 int i, osize = skb_end_offset(skb);
1177 int size = osize + nhead + ntail;
1183 if (skb_shared(skb))
1186 size = SKB_DATA_ALIGN(size);
1188 if (skb_pfmemalloc(skb))
1189 gfp_mask |= __GFP_MEMALLOC;
1190 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1191 gfp_mask, NUMA_NO_NODE, NULL);
1194 size = SKB_WITH_OVERHEAD(ksize(data));
1196 /* Copy only real data... and, alas, header. This should be
1197 * optimized for the cases when header is void.
1199 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1201 memcpy((struct skb_shared_info *)(data + size),
1203 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1206 * if shinfo is shared we must drop the old head gracefully, but if it
1207 * is not we can just drop the old head and let the existing refcount
1208 * be since all we did is relocate the values
1210 if (skb_cloned(skb)) {
1211 /* copy this zero copy skb frags */
1212 if (skb_orphan_frags(skb, gfp_mask))
1214 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1215 skb_frag_ref(skb, i);
1217 if (skb_has_frag_list(skb))
1218 skb_clone_fraglist(skb);
1220 skb_release_data(skb);
1224 off = (data + nhead) - skb->head;
1229 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1233 skb->end = skb->head + size;
1236 skb_headers_offset_update(skb, nhead);
1240 atomic_set(&skb_shinfo(skb)->dataref, 1);
1242 /* It is not generally safe to change skb->truesize.
1243 * For the moment, we really care of rx path, or
1244 * when skb is orphaned (not attached to a socket).
1246 if (!skb->sk || skb->destructor == sock_edemux)
1247 skb->truesize += size - osize;
1256 EXPORT_SYMBOL(pskb_expand_head);
1258 /* Make private copy of skb with writable head and some headroom */
1260 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1262 struct sk_buff *skb2;
1263 int delta = headroom - skb_headroom(skb);
1266 skb2 = pskb_copy(skb, GFP_ATOMIC);
1268 skb2 = skb_clone(skb, GFP_ATOMIC);
1269 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1277 EXPORT_SYMBOL(skb_realloc_headroom);
1280 * skb_copy_expand - copy and expand sk_buff
1281 * @skb: buffer to copy
1282 * @newheadroom: new free bytes at head
1283 * @newtailroom: new free bytes at tail
1284 * @gfp_mask: allocation priority
1286 * Make a copy of both an &sk_buff and its data and while doing so
1287 * allocate additional space.
1289 * This is used when the caller wishes to modify the data and needs a
1290 * private copy of the data to alter as well as more space for new fields.
1291 * Returns %NULL on failure or the pointer to the buffer
1292 * on success. The returned buffer has a reference count of 1.
1294 * You must pass %GFP_ATOMIC as the allocation priority if this function
1295 * is called from an interrupt.
1297 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1298 int newheadroom, int newtailroom,
1302 * Allocate the copy buffer
1304 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1305 gfp_mask, skb_alloc_rx_flag(skb),
1307 int oldheadroom = skb_headroom(skb);
1308 int head_copy_len, head_copy_off;
1313 skb_reserve(n, newheadroom);
1315 /* Set the tail pointer and length */
1316 skb_put(n, skb->len);
1318 head_copy_len = oldheadroom;
1320 if (newheadroom <= head_copy_len)
1321 head_copy_len = newheadroom;
1323 head_copy_off = newheadroom - head_copy_len;
1325 /* Copy the linear header and data. */
1326 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1327 skb->len + head_copy_len))
1330 copy_skb_header(n, skb);
1332 skb_headers_offset_update(n, newheadroom - oldheadroom);
1336 EXPORT_SYMBOL(skb_copy_expand);
1339 * skb_pad - zero pad the tail of an skb
1340 * @skb: buffer to pad
1341 * @pad: space to pad
1343 * Ensure that a buffer is followed by a padding area that is zero
1344 * filled. Used by network drivers which may DMA or transfer data
1345 * beyond the buffer end onto the wire.
1347 * May return error in out of memory cases. The skb is freed on error.
1350 int skb_pad(struct sk_buff *skb, int pad)
1355 /* If the skbuff is non linear tailroom is always zero.. */
1356 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1357 memset(skb->data+skb->len, 0, pad);
1361 ntail = skb->data_len + pad - (skb->end - skb->tail);
1362 if (likely(skb_cloned(skb) || ntail > 0)) {
1363 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1368 /* FIXME: The use of this function with non-linear skb's really needs
1371 err = skb_linearize(skb);
1375 memset(skb->data + skb->len, 0, pad);
1382 EXPORT_SYMBOL(skb_pad);
1385 * pskb_put - add data to the tail of a potentially fragmented buffer
1386 * @skb: start of the buffer to use
1387 * @tail: tail fragment of the buffer to use
1388 * @len: amount of data to add
1390 * This function extends the used data area of the potentially
1391 * fragmented buffer. @tail must be the last fragment of @skb -- or
1392 * @skb itself. If this would exceed the total buffer size the kernel
1393 * will panic. A pointer to the first byte of the extra data is
1397 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1400 skb->data_len += len;
1403 return skb_put(tail, len);
1405 EXPORT_SYMBOL_GPL(pskb_put);
1408 * skb_put - add data to a buffer
1409 * @skb: buffer to use
1410 * @len: amount of data to add
1412 * This function extends the used data area of the buffer. If this would
1413 * exceed the total buffer size the kernel will panic. A pointer to the
1414 * first byte of the extra data is returned.
1416 void *skb_put(struct sk_buff *skb, unsigned int len)
1418 void *tmp = skb_tail_pointer(skb);
1419 SKB_LINEAR_ASSERT(skb);
1422 if (unlikely(skb->tail > skb->end))
1423 skb_over_panic(skb, len, __builtin_return_address(0));
1426 EXPORT_SYMBOL(skb_put);
1429 * skb_push - add data to the start of a buffer
1430 * @skb: buffer to use
1431 * @len: amount of data to add
1433 * This function extends the used data area of the buffer at the buffer
1434 * start. If this would exceed the total buffer headroom the kernel will
1435 * panic. A pointer to the first byte of the extra data is returned.
1437 void *skb_push(struct sk_buff *skb, unsigned int len)
1441 if (unlikely(skb->data<skb->head))
1442 skb_under_panic(skb, len, __builtin_return_address(0));
1445 EXPORT_SYMBOL(skb_push);
1448 * skb_pull - remove data from the start of a buffer
1449 * @skb: buffer to use
1450 * @len: amount of data to remove
1452 * This function removes data from the start of a buffer, returning
1453 * the memory to the headroom. A pointer to the next data in the buffer
1454 * is returned. Once the data has been pulled future pushes will overwrite
1457 void *skb_pull(struct sk_buff *skb, unsigned int len)
1459 return skb_pull_inline(skb, len);
1461 EXPORT_SYMBOL(skb_pull);
1464 * skb_trim - remove end from a buffer
1465 * @skb: buffer to alter
1468 * Cut the length of a buffer down by removing data from the tail. If
1469 * the buffer is already under the length specified it is not modified.
1470 * The skb must be linear.
1472 void skb_trim(struct sk_buff *skb, unsigned int len)
1475 __skb_trim(skb, len);
1477 EXPORT_SYMBOL(skb_trim);
1479 /* Trims skb to length len. It can change skb pointers.
1482 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1484 struct sk_buff **fragp;
1485 struct sk_buff *frag;
1486 int offset = skb_headlen(skb);
1487 int nfrags = skb_shinfo(skb)->nr_frags;
1491 if (skb_cloned(skb) &&
1492 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1499 for (; i < nfrags; i++) {
1500 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1507 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1510 skb_shinfo(skb)->nr_frags = i;
1512 for (; i < nfrags; i++)
1513 skb_frag_unref(skb, i);
1515 if (skb_has_frag_list(skb))
1516 skb_drop_fraglist(skb);
1520 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1521 fragp = &frag->next) {
1522 int end = offset + frag->len;
1524 if (skb_shared(frag)) {
1525 struct sk_buff *nfrag;
1527 nfrag = skb_clone(frag, GFP_ATOMIC);
1528 if (unlikely(!nfrag))
1531 nfrag->next = frag->next;
1543 unlikely((err = pskb_trim(frag, len - offset))))
1547 skb_drop_list(&frag->next);
1552 if (len > skb_headlen(skb)) {
1553 skb->data_len -= skb->len - len;
1558 skb_set_tail_pointer(skb, len);
1561 if (!skb->sk || skb->destructor == sock_edemux)
1565 EXPORT_SYMBOL(___pskb_trim);
1568 * __pskb_pull_tail - advance tail of skb header
1569 * @skb: buffer to reallocate
1570 * @delta: number of bytes to advance tail
1572 * The function makes a sense only on a fragmented &sk_buff,
1573 * it expands header moving its tail forward and copying necessary
1574 * data from fragmented part.
1576 * &sk_buff MUST have reference count of 1.
1578 * Returns %NULL (and &sk_buff does not change) if pull failed
1579 * or value of new tail of skb in the case of success.
1581 * All the pointers pointing into skb header may change and must be
1582 * reloaded after call to this function.
1585 /* Moves tail of skb head forward, copying data from fragmented part,
1586 * when it is necessary.
1587 * 1. It may fail due to malloc failure.
1588 * 2. It may change skb pointers.
1590 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1592 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1594 /* If skb has not enough free space at tail, get new one
1595 * plus 128 bytes for future expansions. If we have enough
1596 * room at tail, reallocate without expansion only if skb is cloned.
1598 int i, k, eat = (skb->tail + delta) - skb->end;
1600 if (eat > 0 || skb_cloned(skb)) {
1601 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1606 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1609 /* Optimization: no fragments, no reasons to preestimate
1610 * size of pulled pages. Superb.
1612 if (!skb_has_frag_list(skb))
1615 /* Estimate size of pulled pages. */
1617 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1618 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1625 /* If we need update frag list, we are in troubles.
1626 * Certainly, it possible to add an offset to skb data,
1627 * but taking into account that pulling is expected to
1628 * be very rare operation, it is worth to fight against
1629 * further bloating skb head and crucify ourselves here instead.
1630 * Pure masohism, indeed. 8)8)
1633 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1634 struct sk_buff *clone = NULL;
1635 struct sk_buff *insp = NULL;
1640 if (list->len <= eat) {
1641 /* Eaten as whole. */
1646 /* Eaten partially. */
1648 if (skb_shared(list)) {
1649 /* Sucks! We need to fork list. :-( */
1650 clone = skb_clone(list, GFP_ATOMIC);
1656 /* This may be pulled without
1660 if (!pskb_pull(list, eat)) {
1668 /* Free pulled out fragments. */
1669 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1670 skb_shinfo(skb)->frag_list = list->next;
1673 /* And insert new clone at head. */
1676 skb_shinfo(skb)->frag_list = clone;
1679 /* Success! Now we may commit changes to skb data. */
1684 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1685 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1688 skb_frag_unref(skb, i);
1691 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1693 skb_shinfo(skb)->frags[k].page_offset += eat;
1694 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1702 skb_shinfo(skb)->nr_frags = k;
1706 skb->data_len -= delta;
1708 return skb_tail_pointer(skb);
1710 EXPORT_SYMBOL(__pskb_pull_tail);
1713 * skb_copy_bits - copy bits from skb to kernel buffer
1715 * @offset: offset in source
1716 * @to: destination buffer
1717 * @len: number of bytes to copy
1719 * Copy the specified number of bytes from the source skb to the
1720 * destination buffer.
1723 * If its prototype is ever changed,
1724 * check arch/{*}/net/{*}.S files,
1725 * since it is called from BPF assembly code.
1727 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1729 int start = skb_headlen(skb);
1730 struct sk_buff *frag_iter;
1733 if (offset > (int)skb->len - len)
1737 if ((copy = start - offset) > 0) {
1740 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1741 if ((len -= copy) == 0)
1747 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1749 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1751 WARN_ON(start > offset + len);
1753 end = start + skb_frag_size(f);
1754 if ((copy = end - offset) > 0) {
1760 vaddr = kmap_atomic(skb_frag_page(f));
1762 vaddr + f->page_offset + offset - start,
1764 kunmap_atomic(vaddr);
1766 if ((len -= copy) == 0)
1774 skb_walk_frags(skb, frag_iter) {
1777 WARN_ON(start > offset + len);
1779 end = start + frag_iter->len;
1780 if ((copy = end - offset) > 0) {
1783 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1785 if ((len -= copy) == 0)
1799 EXPORT_SYMBOL(skb_copy_bits);
1802 * Callback from splice_to_pipe(), if we need to release some pages
1803 * at the end of the spd in case we error'ed out in filling the pipe.
1805 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1807 put_page(spd->pages[i]);
1810 static struct page *linear_to_page(struct page *page, unsigned int *len,
1811 unsigned int *offset,
1814 struct page_frag *pfrag = sk_page_frag(sk);
1816 if (!sk_page_frag_refill(sk, pfrag))
1819 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1821 memcpy(page_address(pfrag->page) + pfrag->offset,
1822 page_address(page) + *offset, *len);
1823 *offset = pfrag->offset;
1824 pfrag->offset += *len;
1829 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1831 unsigned int offset)
1833 return spd->nr_pages &&
1834 spd->pages[spd->nr_pages - 1] == page &&
1835 (spd->partial[spd->nr_pages - 1].offset +
1836 spd->partial[spd->nr_pages - 1].len == offset);
1840 * Fill page/offset/length into spd, if it can hold more pages.
1842 static bool spd_fill_page(struct splice_pipe_desc *spd,
1843 struct pipe_inode_info *pipe, struct page *page,
1844 unsigned int *len, unsigned int offset,
1848 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1852 page = linear_to_page(page, len, &offset, sk);
1856 if (spd_can_coalesce(spd, page, offset)) {
1857 spd->partial[spd->nr_pages - 1].len += *len;
1861 spd->pages[spd->nr_pages] = page;
1862 spd->partial[spd->nr_pages].len = *len;
1863 spd->partial[spd->nr_pages].offset = offset;
1869 static bool __splice_segment(struct page *page, unsigned int poff,
1870 unsigned int plen, unsigned int *off,
1872 struct splice_pipe_desc *spd, bool linear,
1874 struct pipe_inode_info *pipe)
1879 /* skip this segment if already processed */
1885 /* ignore any bits we already processed */
1891 unsigned int flen = min(*len, plen);
1893 if (spd_fill_page(spd, pipe, page, &flen, poff,
1899 } while (*len && plen);
1905 * Map linear and fragment data from the skb to spd. It reports true if the
1906 * pipe is full or if we already spliced the requested length.
1908 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1909 unsigned int *offset, unsigned int *len,
1910 struct splice_pipe_desc *spd, struct sock *sk)
1913 struct sk_buff *iter;
1915 /* map the linear part :
1916 * If skb->head_frag is set, this 'linear' part is backed by a
1917 * fragment, and if the head is not shared with any clones then
1918 * we can avoid a copy since we own the head portion of this page.
1920 if (__splice_segment(virt_to_page(skb->data),
1921 (unsigned long) skb->data & (PAGE_SIZE - 1),
1924 skb_head_is_locked(skb),
1929 * then map the fragments
1931 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1932 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1934 if (__splice_segment(skb_frag_page(f),
1935 f->page_offset, skb_frag_size(f),
1936 offset, len, spd, false, sk, pipe))
1940 skb_walk_frags(skb, iter) {
1941 if (*offset >= iter->len) {
1942 *offset -= iter->len;
1945 /* __skb_splice_bits() only fails if the output has no room
1946 * left, so no point in going over the frag_list for the error
1949 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1957 * Map data from the skb to a pipe. Should handle both the linear part,
1958 * the fragments, and the frag list.
1960 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1961 struct pipe_inode_info *pipe, unsigned int tlen,
1964 struct partial_page partial[MAX_SKB_FRAGS];
1965 struct page *pages[MAX_SKB_FRAGS];
1966 struct splice_pipe_desc spd = {
1969 .nr_pages_max = MAX_SKB_FRAGS,
1970 .ops = &nosteal_pipe_buf_ops,
1971 .spd_release = sock_spd_release,
1975 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
1978 ret = splice_to_pipe(pipe, &spd);
1982 EXPORT_SYMBOL_GPL(skb_splice_bits);
1985 * skb_store_bits - store bits from kernel buffer to skb
1986 * @skb: destination buffer
1987 * @offset: offset in destination
1988 * @from: source buffer
1989 * @len: number of bytes to copy
1991 * Copy the specified number of bytes from the source buffer to the
1992 * destination skb. This function handles all the messy bits of
1993 * traversing fragment lists and such.
1996 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1998 int start = skb_headlen(skb);
1999 struct sk_buff *frag_iter;
2002 if (offset > (int)skb->len - len)
2005 if ((copy = start - offset) > 0) {
2008 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2009 if ((len -= copy) == 0)
2015 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2016 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2019 WARN_ON(start > offset + len);
2021 end = start + skb_frag_size(frag);
2022 if ((copy = end - offset) > 0) {
2028 vaddr = kmap_atomic(skb_frag_page(frag));
2029 memcpy(vaddr + frag->page_offset + offset - start,
2031 kunmap_atomic(vaddr);
2033 if ((len -= copy) == 0)
2041 skb_walk_frags(skb, frag_iter) {
2044 WARN_ON(start > offset + len);
2046 end = start + frag_iter->len;
2047 if ((copy = end - offset) > 0) {
2050 if (skb_store_bits(frag_iter, offset - start,
2053 if ((len -= copy) == 0)
2066 EXPORT_SYMBOL(skb_store_bits);
2068 /* Checksum skb data. */
2069 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2070 __wsum csum, const struct skb_checksum_ops *ops)
2072 int start = skb_headlen(skb);
2073 int i, copy = start - offset;
2074 struct sk_buff *frag_iter;
2077 /* Checksum header. */
2081 csum = ops->update(skb->data + offset, copy, csum);
2082 if ((len -= copy) == 0)
2088 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2090 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2092 WARN_ON(start > offset + len);
2094 end = start + skb_frag_size(frag);
2095 if ((copy = end - offset) > 0) {
2101 vaddr = kmap_atomic(skb_frag_page(frag));
2102 csum2 = ops->update(vaddr + frag->page_offset +
2103 offset - start, copy, 0);
2104 kunmap_atomic(vaddr);
2105 csum = ops->combine(csum, csum2, pos, copy);
2114 skb_walk_frags(skb, frag_iter) {
2117 WARN_ON(start > offset + len);
2119 end = start + frag_iter->len;
2120 if ((copy = end - offset) > 0) {
2124 csum2 = __skb_checksum(frag_iter, offset - start,
2126 csum = ops->combine(csum, csum2, pos, copy);
2127 if ((len -= copy) == 0)
2138 EXPORT_SYMBOL(__skb_checksum);
2140 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2141 int len, __wsum csum)
2143 const struct skb_checksum_ops ops = {
2144 .update = csum_partial_ext,
2145 .combine = csum_block_add_ext,
2148 return __skb_checksum(skb, offset, len, csum, &ops);
2150 EXPORT_SYMBOL(skb_checksum);
2152 /* Both of above in one bottle. */
2154 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2155 u8 *to, int len, __wsum csum)
2157 int start = skb_headlen(skb);
2158 int i, copy = start - offset;
2159 struct sk_buff *frag_iter;
2166 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2168 if ((len -= copy) == 0)
2175 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2178 WARN_ON(start > offset + len);
2180 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2181 if ((copy = end - offset) > 0) {
2184 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2188 vaddr = kmap_atomic(skb_frag_page(frag));
2189 csum2 = csum_partial_copy_nocheck(vaddr +
2193 kunmap_atomic(vaddr);
2194 csum = csum_block_add(csum, csum2, pos);
2204 skb_walk_frags(skb, frag_iter) {
2208 WARN_ON(start > offset + len);
2210 end = start + frag_iter->len;
2211 if ((copy = end - offset) > 0) {
2214 csum2 = skb_copy_and_csum_bits(frag_iter,
2217 csum = csum_block_add(csum, csum2, pos);
2218 if ((len -= copy) == 0)
2229 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2231 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2233 net_warn_ratelimited(
2234 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2239 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2240 int offset, int len)
2242 net_warn_ratelimited(
2243 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2248 static const struct skb_checksum_ops default_crc32c_ops = {
2249 .update = warn_crc32c_csum_update,
2250 .combine = warn_crc32c_csum_combine,
2253 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2254 &default_crc32c_ops;
2255 EXPORT_SYMBOL(crc32c_csum_stub);
2258 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2259 * @from: source buffer
2261 * Calculates the amount of linear headroom needed in the 'to' skb passed
2262 * into skb_zerocopy().
2265 skb_zerocopy_headlen(const struct sk_buff *from)
2267 unsigned int hlen = 0;
2269 if (!from->head_frag ||
2270 skb_headlen(from) < L1_CACHE_BYTES ||
2271 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2272 hlen = skb_headlen(from);
2274 if (skb_has_frag_list(from))
2279 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2282 * skb_zerocopy - Zero copy skb to skb
2283 * @to: destination buffer
2284 * @from: source buffer
2285 * @len: number of bytes to copy from source buffer
2286 * @hlen: size of linear headroom in destination buffer
2288 * Copies up to `len` bytes from `from` to `to` by creating references
2289 * to the frags in the source buffer.
2291 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2292 * headroom in the `to` buffer.
2295 * 0: everything is OK
2296 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2297 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2300 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2303 int plen = 0; /* length of skb->head fragment */
2306 unsigned int offset;
2308 BUG_ON(!from->head_frag && !hlen);
2310 /* dont bother with small payloads */
2311 if (len <= skb_tailroom(to))
2312 return skb_copy_bits(from, 0, skb_put(to, len), len);
2315 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2320 plen = min_t(int, skb_headlen(from), len);
2322 page = virt_to_head_page(from->head);
2323 offset = from->data - (unsigned char *)page_address(page);
2324 __skb_fill_page_desc(to, 0, page, offset, plen);
2331 to->truesize += len + plen;
2332 to->len += len + plen;
2333 to->data_len += len + plen;
2335 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2340 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2343 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2344 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2345 len -= skb_shinfo(to)->frags[j].size;
2346 skb_frag_ref(to, j);
2349 skb_shinfo(to)->nr_frags = j;
2353 EXPORT_SYMBOL_GPL(skb_zerocopy);
2355 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2360 if (skb->ip_summed == CHECKSUM_PARTIAL)
2361 csstart = skb_checksum_start_offset(skb);
2363 csstart = skb_headlen(skb);
2365 BUG_ON(csstart > skb_headlen(skb));
2367 skb_copy_from_linear_data(skb, to, csstart);
2370 if (csstart != skb->len)
2371 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2372 skb->len - csstart, 0);
2374 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2375 long csstuff = csstart + skb->csum_offset;
2377 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2380 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2383 * skb_dequeue - remove from the head of the queue
2384 * @list: list to dequeue from
2386 * Remove the head of the list. The list lock is taken so the function
2387 * may be used safely with other locking list functions. The head item is
2388 * returned or %NULL if the list is empty.
2391 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2393 unsigned long flags;
2394 struct sk_buff *result;
2396 spin_lock_irqsave(&list->lock, flags);
2397 result = __skb_dequeue(list);
2398 spin_unlock_irqrestore(&list->lock, flags);
2401 EXPORT_SYMBOL(skb_dequeue);
2404 * skb_dequeue_tail - remove from the tail of the queue
2405 * @list: list to dequeue from
2407 * Remove the tail of the list. The list lock is taken so the function
2408 * may be used safely with other locking list functions. The tail item is
2409 * returned or %NULL if the list is empty.
2411 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2413 unsigned long flags;
2414 struct sk_buff *result;
2416 spin_lock_irqsave(&list->lock, flags);
2417 result = __skb_dequeue_tail(list);
2418 spin_unlock_irqrestore(&list->lock, flags);
2421 EXPORT_SYMBOL(skb_dequeue_tail);
2424 * skb_queue_purge - empty a list
2425 * @list: list to empty
2427 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2428 * the list and one reference dropped. This function takes the list
2429 * lock and is atomic with respect to other list locking functions.
2431 void skb_queue_purge(struct sk_buff_head *list)
2433 struct sk_buff *skb;
2434 while ((skb = skb_dequeue(list)) != NULL)
2437 EXPORT_SYMBOL(skb_queue_purge);
2440 * skb_rbtree_purge - empty a skb rbtree
2441 * @root: root of the rbtree to empty
2443 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2444 * the list and one reference dropped. This function does not take
2445 * any lock. Synchronization should be handled by the caller (e.g., TCP
2446 * out-of-order queue is protected by the socket lock).
2448 void skb_rbtree_purge(struct rb_root *root)
2450 struct sk_buff *skb, *next;
2452 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2459 * skb_queue_head - queue a buffer at the list head
2460 * @list: list to use
2461 * @newsk: buffer to queue
2463 * Queue a buffer at the start of the list. This function takes the
2464 * list lock and can be used safely with other locking &sk_buff functions
2467 * A buffer cannot be placed on two lists at the same time.
2469 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2471 unsigned long flags;
2473 spin_lock_irqsave(&list->lock, flags);
2474 __skb_queue_head(list, newsk);
2475 spin_unlock_irqrestore(&list->lock, flags);
2477 EXPORT_SYMBOL(skb_queue_head);
2480 * skb_queue_tail - queue a buffer at the list tail
2481 * @list: list to use
2482 * @newsk: buffer to queue
2484 * Queue a buffer at the tail of the list. This function takes the
2485 * list lock and can be used safely with other locking &sk_buff functions
2488 * A buffer cannot be placed on two lists at the same time.
2490 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2492 unsigned long flags;
2494 spin_lock_irqsave(&list->lock, flags);
2495 __skb_queue_tail(list, newsk);
2496 spin_unlock_irqrestore(&list->lock, flags);
2498 EXPORT_SYMBOL(skb_queue_tail);
2501 * skb_unlink - remove a buffer from a list
2502 * @skb: buffer to remove
2503 * @list: list to use
2505 * Remove a packet from a list. The list locks are taken and this
2506 * function is atomic with respect to other list locked calls
2508 * You must know what list the SKB is on.
2510 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2512 unsigned long flags;
2514 spin_lock_irqsave(&list->lock, flags);
2515 __skb_unlink(skb, list);
2516 spin_unlock_irqrestore(&list->lock, flags);
2518 EXPORT_SYMBOL(skb_unlink);
2521 * skb_append - append a buffer
2522 * @old: buffer to insert after
2523 * @newsk: buffer to insert
2524 * @list: list to use
2526 * Place a packet after a given packet in a list. The list locks are taken
2527 * and this function is atomic with respect to other list locked calls.
2528 * A buffer cannot be placed on two lists at the same time.
2530 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2532 unsigned long flags;
2534 spin_lock_irqsave(&list->lock, flags);
2535 __skb_queue_after(list, old, newsk);
2536 spin_unlock_irqrestore(&list->lock, flags);
2538 EXPORT_SYMBOL(skb_append);
2541 * skb_insert - insert a buffer
2542 * @old: buffer to insert before
2543 * @newsk: buffer to insert
2544 * @list: list to use
2546 * Place a packet before a given packet in a list. The list locks are
2547 * taken and this function is atomic with respect to other list locked
2550 * A buffer cannot be placed on two lists at the same time.
2552 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2554 unsigned long flags;
2556 spin_lock_irqsave(&list->lock, flags);
2557 __skb_insert(newsk, old->prev, old, list);
2558 spin_unlock_irqrestore(&list->lock, flags);
2560 EXPORT_SYMBOL(skb_insert);
2562 static inline void skb_split_inside_header(struct sk_buff *skb,
2563 struct sk_buff* skb1,
2564 const u32 len, const int pos)
2568 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2570 /* And move data appendix as is. */
2571 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2572 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2574 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2575 skb_shinfo(skb)->nr_frags = 0;
2576 skb1->data_len = skb->data_len;
2577 skb1->len += skb1->data_len;
2580 skb_set_tail_pointer(skb, len);
2583 static inline void skb_split_no_header(struct sk_buff *skb,
2584 struct sk_buff* skb1,
2585 const u32 len, int pos)
2588 const int nfrags = skb_shinfo(skb)->nr_frags;
2590 skb_shinfo(skb)->nr_frags = 0;
2591 skb1->len = skb1->data_len = skb->len - len;
2593 skb->data_len = len - pos;
2595 for (i = 0; i < nfrags; i++) {
2596 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2598 if (pos + size > len) {
2599 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2603 * We have two variants in this case:
2604 * 1. Move all the frag to the second
2605 * part, if it is possible. F.e.
2606 * this approach is mandatory for TUX,
2607 * where splitting is expensive.
2608 * 2. Split is accurately. We make this.
2610 skb_frag_ref(skb, i);
2611 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2612 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2613 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2614 skb_shinfo(skb)->nr_frags++;
2618 skb_shinfo(skb)->nr_frags++;
2621 skb_shinfo(skb1)->nr_frags = k;
2625 * skb_split - Split fragmented skb to two parts at length len.
2626 * @skb: the buffer to split
2627 * @skb1: the buffer to receive the second part
2628 * @len: new length for skb
2630 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2632 int pos = skb_headlen(skb);
2634 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2636 if (len < pos) /* Split line is inside header. */
2637 skb_split_inside_header(skb, skb1, len, pos);
2638 else /* Second chunk has no header, nothing to copy. */
2639 skb_split_no_header(skb, skb1, len, pos);
2641 EXPORT_SYMBOL(skb_split);
2643 /* Shifting from/to a cloned skb is a no-go.
2645 * Caller cannot keep skb_shinfo related pointers past calling here!
2647 static int skb_prepare_for_shift(struct sk_buff *skb)
2649 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2653 * skb_shift - Shifts paged data partially from skb to another
2654 * @tgt: buffer into which tail data gets added
2655 * @skb: buffer from which the paged data comes from
2656 * @shiftlen: shift up to this many bytes
2658 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2659 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2660 * It's up to caller to free skb if everything was shifted.
2662 * If @tgt runs out of frags, the whole operation is aborted.
2664 * Skb cannot include anything else but paged data while tgt is allowed
2665 * to have non-paged data as well.
2667 * TODO: full sized shift could be optimized but that would need
2668 * specialized skb free'er to handle frags without up-to-date nr_frags.
2670 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2672 int from, to, merge, todo;
2673 struct skb_frag_struct *fragfrom, *fragto;
2675 BUG_ON(shiftlen > skb->len);
2677 if (skb_headlen(skb))
2682 to = skb_shinfo(tgt)->nr_frags;
2683 fragfrom = &skb_shinfo(skb)->frags[from];
2685 /* Actual merge is delayed until the point when we know we can
2686 * commit all, so that we don't have to undo partial changes
2689 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2690 fragfrom->page_offset)) {
2695 todo -= skb_frag_size(fragfrom);
2697 if (skb_prepare_for_shift(skb) ||
2698 skb_prepare_for_shift(tgt))
2701 /* All previous frag pointers might be stale! */
2702 fragfrom = &skb_shinfo(skb)->frags[from];
2703 fragto = &skb_shinfo(tgt)->frags[merge];
2705 skb_frag_size_add(fragto, shiftlen);
2706 skb_frag_size_sub(fragfrom, shiftlen);
2707 fragfrom->page_offset += shiftlen;
2715 /* Skip full, not-fitting skb to avoid expensive operations */
2716 if ((shiftlen == skb->len) &&
2717 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2720 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2723 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2724 if (to == MAX_SKB_FRAGS)
2727 fragfrom = &skb_shinfo(skb)->frags[from];
2728 fragto = &skb_shinfo(tgt)->frags[to];
2730 if (todo >= skb_frag_size(fragfrom)) {
2731 *fragto = *fragfrom;
2732 todo -= skb_frag_size(fragfrom);
2737 __skb_frag_ref(fragfrom);
2738 fragto->page = fragfrom->page;
2739 fragto->page_offset = fragfrom->page_offset;
2740 skb_frag_size_set(fragto, todo);
2742 fragfrom->page_offset += todo;
2743 skb_frag_size_sub(fragfrom, todo);
2751 /* Ready to "commit" this state change to tgt */
2752 skb_shinfo(tgt)->nr_frags = to;
2755 fragfrom = &skb_shinfo(skb)->frags[0];
2756 fragto = &skb_shinfo(tgt)->frags[merge];
2758 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2759 __skb_frag_unref(fragfrom);
2762 /* Reposition in the original skb */
2764 while (from < skb_shinfo(skb)->nr_frags)
2765 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2766 skb_shinfo(skb)->nr_frags = to;
2768 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2771 /* Most likely the tgt won't ever need its checksum anymore, skb on
2772 * the other hand might need it if it needs to be resent
2774 tgt->ip_summed = CHECKSUM_PARTIAL;
2775 skb->ip_summed = CHECKSUM_PARTIAL;
2777 /* Yak, is it really working this way? Some helper please? */
2778 skb->len -= shiftlen;
2779 skb->data_len -= shiftlen;
2780 skb->truesize -= shiftlen;
2781 tgt->len += shiftlen;
2782 tgt->data_len += shiftlen;
2783 tgt->truesize += shiftlen;
2789 * skb_prepare_seq_read - Prepare a sequential read of skb data
2790 * @skb: the buffer to read
2791 * @from: lower offset of data to be read
2792 * @to: upper offset of data to be read
2793 * @st: state variable
2795 * Initializes the specified state variable. Must be called before
2796 * invoking skb_seq_read() for the first time.
2798 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2799 unsigned int to, struct skb_seq_state *st)
2801 st->lower_offset = from;
2802 st->upper_offset = to;
2803 st->root_skb = st->cur_skb = skb;
2804 st->frag_idx = st->stepped_offset = 0;
2805 st->frag_data = NULL;
2807 EXPORT_SYMBOL(skb_prepare_seq_read);
2810 * skb_seq_read - Sequentially read skb data
2811 * @consumed: number of bytes consumed by the caller so far
2812 * @data: destination pointer for data to be returned
2813 * @st: state variable
2815 * Reads a block of skb data at @consumed relative to the
2816 * lower offset specified to skb_prepare_seq_read(). Assigns
2817 * the head of the data block to @data and returns the length
2818 * of the block or 0 if the end of the skb data or the upper
2819 * offset has been reached.
2821 * The caller is not required to consume all of the data
2822 * returned, i.e. @consumed is typically set to the number
2823 * of bytes already consumed and the next call to
2824 * skb_seq_read() will return the remaining part of the block.
2826 * Note 1: The size of each block of data returned can be arbitrary,
2827 * this limitation is the cost for zerocopy sequential
2828 * reads of potentially non linear data.
2830 * Note 2: Fragment lists within fragments are not implemented
2831 * at the moment, state->root_skb could be replaced with
2832 * a stack for this purpose.
2834 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2835 struct skb_seq_state *st)
2837 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2840 if (unlikely(abs_offset >= st->upper_offset)) {
2841 if (st->frag_data) {
2842 kunmap_atomic(st->frag_data);
2843 st->frag_data = NULL;
2849 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2851 if (abs_offset < block_limit && !st->frag_data) {
2852 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2853 return block_limit - abs_offset;
2856 if (st->frag_idx == 0 && !st->frag_data)
2857 st->stepped_offset += skb_headlen(st->cur_skb);
2859 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2860 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2861 block_limit = skb_frag_size(frag) + st->stepped_offset;
2863 if (abs_offset < block_limit) {
2865 st->frag_data = kmap_atomic(skb_frag_page(frag));
2867 *data = (u8 *) st->frag_data + frag->page_offset +
2868 (abs_offset - st->stepped_offset);
2870 return block_limit - abs_offset;
2873 if (st->frag_data) {
2874 kunmap_atomic(st->frag_data);
2875 st->frag_data = NULL;
2879 st->stepped_offset += skb_frag_size(frag);
2882 if (st->frag_data) {
2883 kunmap_atomic(st->frag_data);
2884 st->frag_data = NULL;
2887 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2888 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2891 } else if (st->cur_skb->next) {
2892 st->cur_skb = st->cur_skb->next;
2899 EXPORT_SYMBOL(skb_seq_read);
2902 * skb_abort_seq_read - Abort a sequential read of skb data
2903 * @st: state variable
2905 * Must be called if skb_seq_read() was not called until it
2908 void skb_abort_seq_read(struct skb_seq_state *st)
2911 kunmap_atomic(st->frag_data);
2913 EXPORT_SYMBOL(skb_abort_seq_read);
2915 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2917 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2918 struct ts_config *conf,
2919 struct ts_state *state)
2921 return skb_seq_read(offset, text, TS_SKB_CB(state));
2924 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2926 skb_abort_seq_read(TS_SKB_CB(state));
2930 * skb_find_text - Find a text pattern in skb data
2931 * @skb: the buffer to look in
2932 * @from: search offset
2934 * @config: textsearch configuration
2936 * Finds a pattern in the skb data according to the specified
2937 * textsearch configuration. Use textsearch_next() to retrieve
2938 * subsequent occurrences of the pattern. Returns the offset
2939 * to the first occurrence or UINT_MAX if no match was found.
2941 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2942 unsigned int to, struct ts_config *config)
2944 struct ts_state state;
2947 config->get_next_block = skb_ts_get_next_block;
2948 config->finish = skb_ts_finish;
2950 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2952 ret = textsearch_find(config, &state);
2953 return (ret <= to - from ? ret : UINT_MAX);
2955 EXPORT_SYMBOL(skb_find_text);
2958 * skb_append_datato_frags - append the user data to a skb
2959 * @sk: sock structure
2960 * @skb: skb structure to be appended with user data.
2961 * @getfrag: call back function to be used for getting the user data
2962 * @from: pointer to user message iov
2963 * @length: length of the iov message
2965 * Description: This procedure append the user data in the fragment part
2966 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2968 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2969 int (*getfrag)(void *from, char *to, int offset,
2970 int len, int odd, struct sk_buff *skb),
2971 void *from, int length)
2973 int frg_cnt = skb_shinfo(skb)->nr_frags;
2977 struct page_frag *pfrag = ¤t->task_frag;
2980 /* Return error if we don't have space for new frag */
2981 if (frg_cnt >= MAX_SKB_FRAGS)
2984 if (!sk_page_frag_refill(sk, pfrag))
2987 /* copy the user data to page */
2988 copy = min_t(int, length, pfrag->size - pfrag->offset);
2990 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2991 offset, copy, 0, skb);
2995 /* copy was successful so update the size parameters */
2996 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2999 pfrag->offset += copy;
3000 get_page(pfrag->page);
3002 skb->truesize += copy;
3003 refcount_add(copy, &sk->sk_wmem_alloc);
3005 skb->data_len += copy;
3009 } while (length > 0);
3013 EXPORT_SYMBOL(skb_append_datato_frags);
3015 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3016 int offset, size_t size)
3018 int i = skb_shinfo(skb)->nr_frags;
3020 if (skb_can_coalesce(skb, i, page, offset)) {
3021 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3022 } else if (i < MAX_SKB_FRAGS) {
3024 skb_fill_page_desc(skb, i, page, offset, size);
3031 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3034 * skb_pull_rcsum - pull skb and update receive checksum
3035 * @skb: buffer to update
3036 * @len: length of data pulled
3038 * This function performs an skb_pull on the packet and updates
3039 * the CHECKSUM_COMPLETE checksum. It should be used on
3040 * receive path processing instead of skb_pull unless you know
3041 * that the checksum difference is zero (e.g., a valid IP header)
3042 * or you are setting ip_summed to CHECKSUM_NONE.
3044 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3046 unsigned char *data = skb->data;
3048 BUG_ON(len > skb->len);
3049 __skb_pull(skb, len);
3050 skb_postpull_rcsum(skb, data, len);
3053 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3056 * skb_segment - Perform protocol segmentation on skb.
3057 * @head_skb: buffer to segment
3058 * @features: features for the output path (see dev->features)
3060 * This function performs segmentation on the given skb. It returns
3061 * a pointer to the first in a list of new skbs for the segments.
3062 * In case of error it returns ERR_PTR(err).
3064 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3065 netdev_features_t features)
3067 struct sk_buff *segs = NULL;
3068 struct sk_buff *tail = NULL;
3069 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3070 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3071 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3072 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3073 struct sk_buff *frag_skb = head_skb;
3074 unsigned int offset = doffset;
3075 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3076 unsigned int partial_segs = 0;
3077 unsigned int headroom;
3078 unsigned int len = head_skb->len;
3081 int nfrags = skb_shinfo(head_skb)->nr_frags;
3087 __skb_push(head_skb, doffset);
3088 proto = skb_network_protocol(head_skb, &dummy);
3089 if (unlikely(!proto))
3090 return ERR_PTR(-EINVAL);
3092 sg = !!(features & NETIF_F_SG);
3093 csum = !!can_checksum_protocol(features, proto);
3095 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3096 if (!(features & NETIF_F_GSO_PARTIAL)) {
3097 struct sk_buff *iter;
3098 unsigned int frag_len;
3101 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3104 /* If we get here then all the required
3105 * GSO features except frag_list are supported.
3106 * Try to split the SKB to multiple GSO SKBs
3107 * with no frag_list.
3108 * Currently we can do that only when the buffers don't
3109 * have a linear part and all the buffers except
3110 * the last are of the same length.
3112 frag_len = list_skb->len;
3113 skb_walk_frags(head_skb, iter) {
3114 if (frag_len != iter->len && iter->next)
3116 if (skb_headlen(iter) && !iter->head_frag)
3122 if (len != frag_len)
3126 /* GSO partial only requires that we trim off any excess that
3127 * doesn't fit into an MSS sized block, so take care of that
3130 partial_segs = len / mss;
3131 if (partial_segs > 1)
3132 mss *= partial_segs;
3138 headroom = skb_headroom(head_skb);
3139 pos = skb_headlen(head_skb);
3142 struct sk_buff *nskb;
3143 skb_frag_t *nskb_frag;
3147 if (unlikely(mss == GSO_BY_FRAGS)) {
3148 len = list_skb->len;
3150 len = head_skb->len - offset;
3155 hsize = skb_headlen(head_skb) - offset;
3158 if (hsize > len || !sg)
3161 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3162 (skb_headlen(list_skb) == len || sg)) {
3163 BUG_ON(skb_headlen(list_skb) > len);
3166 nfrags = skb_shinfo(list_skb)->nr_frags;
3167 frag = skb_shinfo(list_skb)->frags;
3168 frag_skb = list_skb;
3169 pos += skb_headlen(list_skb);
3171 while (pos < offset + len) {
3172 BUG_ON(i >= nfrags);
3174 size = skb_frag_size(frag);
3175 if (pos + size > offset + len)
3183 nskb = skb_clone(list_skb, GFP_ATOMIC);
3184 list_skb = list_skb->next;
3186 if (unlikely(!nskb))
3189 if (unlikely(pskb_trim(nskb, len))) {
3194 hsize = skb_end_offset(nskb);
3195 if (skb_cow_head(nskb, doffset + headroom)) {
3200 nskb->truesize += skb_end_offset(nskb) - hsize;
3201 skb_release_head_state(nskb);
3202 __skb_push(nskb, doffset);
3204 nskb = __alloc_skb(hsize + doffset + headroom,
3205 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3208 if (unlikely(!nskb))
3211 skb_reserve(nskb, headroom);
3212 __skb_put(nskb, doffset);
3221 __copy_skb_header(nskb, head_skb);
3223 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3224 skb_reset_mac_len(nskb);
3226 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3227 nskb->data - tnl_hlen,
3228 doffset + tnl_hlen);
3230 if (nskb->len == len + doffset)
3231 goto perform_csum_check;
3234 if (!nskb->remcsum_offload)
3235 nskb->ip_summed = CHECKSUM_NONE;
3236 SKB_GSO_CB(nskb)->csum =
3237 skb_copy_and_csum_bits(head_skb, offset,
3240 SKB_GSO_CB(nskb)->csum_start =
3241 skb_headroom(nskb) + doffset;
3245 nskb_frag = skb_shinfo(nskb)->frags;
3247 skb_copy_from_linear_data_offset(head_skb, offset,
3248 skb_put(nskb, hsize), hsize);
3250 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3253 while (pos < offset + len) {
3255 BUG_ON(skb_headlen(list_skb));
3258 nfrags = skb_shinfo(list_skb)->nr_frags;
3259 frag = skb_shinfo(list_skb)->frags;
3260 frag_skb = list_skb;
3264 list_skb = list_skb->next;
3267 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3269 net_warn_ratelimited(
3270 "skb_segment: too many frags: %u %u\n",
3275 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3279 __skb_frag_ref(nskb_frag);
3280 size = skb_frag_size(nskb_frag);
3283 nskb_frag->page_offset += offset - pos;
3284 skb_frag_size_sub(nskb_frag, offset - pos);
3287 skb_shinfo(nskb)->nr_frags++;
3289 if (pos + size <= offset + len) {
3294 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3302 nskb->data_len = len - hsize;
3303 nskb->len += nskb->data_len;
3304 nskb->truesize += nskb->data_len;
3308 if (skb_has_shared_frag(nskb)) {
3309 err = __skb_linearize(nskb);
3313 if (!nskb->remcsum_offload)
3314 nskb->ip_summed = CHECKSUM_NONE;
3315 SKB_GSO_CB(nskb)->csum =
3316 skb_checksum(nskb, doffset,
3317 nskb->len - doffset, 0);
3318 SKB_GSO_CB(nskb)->csum_start =
3319 skb_headroom(nskb) + doffset;
3321 } while ((offset += len) < head_skb->len);
3323 /* Some callers want to get the end of the list.
3324 * Put it in segs->prev to avoid walking the list.
3325 * (see validate_xmit_skb_list() for example)
3330 struct sk_buff *iter;
3331 int type = skb_shinfo(head_skb)->gso_type;
3332 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3334 /* Update type to add partial and then remove dodgy if set */
3335 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3336 type &= ~SKB_GSO_DODGY;
3338 /* Update GSO info and prepare to start updating headers on
3339 * our way back down the stack of protocols.
3341 for (iter = segs; iter; iter = iter->next) {
3342 skb_shinfo(iter)->gso_size = gso_size;
3343 skb_shinfo(iter)->gso_segs = partial_segs;
3344 skb_shinfo(iter)->gso_type = type;
3345 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3348 if (tail->len - doffset <= gso_size)
3349 skb_shinfo(tail)->gso_size = 0;
3350 else if (tail != segs)
3351 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3354 /* Following permits correct backpressure, for protocols
3355 * using skb_set_owner_w().
3356 * Idea is to tranfert ownership from head_skb to last segment.
3358 if (head_skb->destructor == sock_wfree) {
3359 swap(tail->truesize, head_skb->truesize);
3360 swap(tail->destructor, head_skb->destructor);
3361 swap(tail->sk, head_skb->sk);
3366 kfree_skb_list(segs);
3367 return ERR_PTR(err);
3369 EXPORT_SYMBOL_GPL(skb_segment);
3371 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3373 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3374 unsigned int offset = skb_gro_offset(skb);
3375 unsigned int headlen = skb_headlen(skb);
3376 unsigned int len = skb_gro_len(skb);
3377 struct sk_buff *lp, *p = *head;
3378 unsigned int delta_truesize;
3380 if (unlikely(p->len + len >= 65536))
3383 lp = NAPI_GRO_CB(p)->last;
3384 pinfo = skb_shinfo(lp);
3386 if (headlen <= offset) {
3389 int i = skbinfo->nr_frags;
3390 int nr_frags = pinfo->nr_frags + i;
3392 if (nr_frags > MAX_SKB_FRAGS)
3396 pinfo->nr_frags = nr_frags;
3397 skbinfo->nr_frags = 0;
3399 frag = pinfo->frags + nr_frags;
3400 frag2 = skbinfo->frags + i;
3405 frag->page_offset += offset;
3406 skb_frag_size_sub(frag, offset);
3408 /* all fragments truesize : remove (head size + sk_buff) */
3409 delta_truesize = skb->truesize -
3410 SKB_TRUESIZE(skb_end_offset(skb));
3412 skb->truesize -= skb->data_len;
3413 skb->len -= skb->data_len;
3416 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3418 } else if (skb->head_frag) {
3419 int nr_frags = pinfo->nr_frags;
3420 skb_frag_t *frag = pinfo->frags + nr_frags;
3421 struct page *page = virt_to_head_page(skb->head);
3422 unsigned int first_size = headlen - offset;
3423 unsigned int first_offset;
3425 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3428 first_offset = skb->data -
3429 (unsigned char *)page_address(page) +
3432 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3434 frag->page.p = page;
3435 frag->page_offset = first_offset;
3436 skb_frag_size_set(frag, first_size);
3438 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3439 /* We dont need to clear skbinfo->nr_frags here */
3441 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3442 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3447 delta_truesize = skb->truesize;
3448 if (offset > headlen) {
3449 unsigned int eat = offset - headlen;
3451 skbinfo->frags[0].page_offset += eat;
3452 skb_frag_size_sub(&skbinfo->frags[0], eat);
3453 skb->data_len -= eat;
3458 __skb_pull(skb, offset);
3460 if (NAPI_GRO_CB(p)->last == p)
3461 skb_shinfo(p)->frag_list = skb;
3463 NAPI_GRO_CB(p)->last->next = skb;
3464 NAPI_GRO_CB(p)->last = skb;
3465 __skb_header_release(skb);
3469 NAPI_GRO_CB(p)->count++;
3471 p->truesize += delta_truesize;
3474 lp->data_len += len;
3475 lp->truesize += delta_truesize;
3478 NAPI_GRO_CB(skb)->same_flow = 1;
3481 EXPORT_SYMBOL_GPL(skb_gro_receive);
3483 void __init skb_init(void)
3485 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3486 sizeof(struct sk_buff),
3488 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3490 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3491 sizeof(struct sk_buff_fclones),
3493 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3498 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3499 unsigned int recursion_level)
3501 int start = skb_headlen(skb);
3502 int i, copy = start - offset;
3503 struct sk_buff *frag_iter;
3506 if (unlikely(recursion_level >= 24))
3512 sg_set_buf(sg, skb->data + offset, copy);
3514 if ((len -= copy) == 0)
3519 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3522 WARN_ON(start > offset + len);
3524 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3525 if ((copy = end - offset) > 0) {
3526 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3527 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3532 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3533 frag->page_offset+offset-start);
3542 skb_walk_frags(skb, frag_iter) {
3545 WARN_ON(start > offset + len);
3547 end = start + frag_iter->len;
3548 if ((copy = end - offset) > 0) {
3549 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3554 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3555 copy, recursion_level + 1);
3556 if (unlikely(ret < 0))
3559 if ((len -= copy) == 0)
3570 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3571 * @skb: Socket buffer containing the buffers to be mapped
3572 * @sg: The scatter-gather list to map into
3573 * @offset: The offset into the buffer's contents to start mapping
3574 * @len: Length of buffer space to be mapped
3576 * Fill the specified scatter-gather list with mappings/pointers into a
3577 * region of the buffer space attached to a socket buffer. Returns either
3578 * the number of scatterlist items used, or -EMSGSIZE if the contents
3581 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3583 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3588 sg_mark_end(&sg[nsg - 1]);
3592 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3594 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3595 * sglist without mark the sg which contain last skb data as the end.
3596 * So the caller can mannipulate sg list as will when padding new data after
3597 * the first call without calling sg_unmark_end to expend sg list.
3599 * Scenario to use skb_to_sgvec_nomark:
3601 * 2. skb_to_sgvec_nomark(payload1)
3602 * 3. skb_to_sgvec_nomark(payload2)
3604 * This is equivalent to:
3606 * 2. skb_to_sgvec(payload1)
3608 * 4. skb_to_sgvec(payload2)
3610 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3611 * is more preferable.
3613 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3614 int offset, int len)
3616 return __skb_to_sgvec(skb, sg, offset, len, 0);
3618 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3623 * skb_cow_data - Check that a socket buffer's data buffers are writable
3624 * @skb: The socket buffer to check.
3625 * @tailbits: Amount of trailing space to be added
3626 * @trailer: Returned pointer to the skb where the @tailbits space begins
3628 * Make sure that the data buffers attached to a socket buffer are
3629 * writable. If they are not, private copies are made of the data buffers
3630 * and the socket buffer is set to use these instead.
3632 * If @tailbits is given, make sure that there is space to write @tailbits
3633 * bytes of data beyond current end of socket buffer. @trailer will be
3634 * set to point to the skb in which this space begins.
3636 * The number of scatterlist elements required to completely map the
3637 * COW'd and extended socket buffer will be returned.
3639 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3643 struct sk_buff *skb1, **skb_p;
3645 /* If skb is cloned or its head is paged, reallocate
3646 * head pulling out all the pages (pages are considered not writable
3647 * at the moment even if they are anonymous).
3649 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3650 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3653 /* Easy case. Most of packets will go this way. */
3654 if (!skb_has_frag_list(skb)) {
3655 /* A little of trouble, not enough of space for trailer.
3656 * This should not happen, when stack is tuned to generate
3657 * good frames. OK, on miss we reallocate and reserve even more
3658 * space, 128 bytes is fair. */
3660 if (skb_tailroom(skb) < tailbits &&
3661 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3669 /* Misery. We are in troubles, going to mincer fragments... */
3672 skb_p = &skb_shinfo(skb)->frag_list;
3675 while ((skb1 = *skb_p) != NULL) {
3678 /* The fragment is partially pulled by someone,
3679 * this can happen on input. Copy it and everything
3682 if (skb_shared(skb1))
3685 /* If the skb is the last, worry about trailer. */
3687 if (skb1->next == NULL && tailbits) {
3688 if (skb_shinfo(skb1)->nr_frags ||
3689 skb_has_frag_list(skb1) ||
3690 skb_tailroom(skb1) < tailbits)
3691 ntail = tailbits + 128;
3697 skb_shinfo(skb1)->nr_frags ||
3698 skb_has_frag_list(skb1)) {
3699 struct sk_buff *skb2;
3701 /* Fuck, we are miserable poor guys... */
3703 skb2 = skb_copy(skb1, GFP_ATOMIC);
3705 skb2 = skb_copy_expand(skb1,
3709 if (unlikely(skb2 == NULL))
3713 skb_set_owner_w(skb2, skb1->sk);
3715 /* Looking around. Are we still alive?
3716 * OK, link new skb, drop old one */
3718 skb2->next = skb1->next;
3725 skb_p = &skb1->next;
3730 EXPORT_SYMBOL_GPL(skb_cow_data);
3732 static void sock_rmem_free(struct sk_buff *skb)
3734 struct sock *sk = skb->sk;
3736 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3739 static void skb_set_err_queue(struct sk_buff *skb)
3741 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3742 * So, it is safe to (mis)use it to mark skbs on the error queue.
3744 skb->pkt_type = PACKET_OUTGOING;
3745 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3749 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3751 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3753 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3754 (unsigned int)sk->sk_rcvbuf)
3759 skb->destructor = sock_rmem_free;
3760 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3761 skb_set_err_queue(skb);
3763 /* before exiting rcu section, make sure dst is refcounted */
3766 skb_queue_tail(&sk->sk_error_queue, skb);
3767 if (!sock_flag(sk, SOCK_DEAD))
3768 sk->sk_data_ready(sk);
3771 EXPORT_SYMBOL(sock_queue_err_skb);
3773 static bool is_icmp_err_skb(const struct sk_buff *skb)
3775 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3776 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3779 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3781 struct sk_buff_head *q = &sk->sk_error_queue;
3782 struct sk_buff *skb, *skb_next = NULL;
3783 bool icmp_next = false;
3784 unsigned long flags;
3786 spin_lock_irqsave(&q->lock, flags);
3787 skb = __skb_dequeue(q);
3788 if (skb && (skb_next = skb_peek(q))) {
3789 icmp_next = is_icmp_err_skb(skb_next);
3791 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3793 spin_unlock_irqrestore(&q->lock, flags);
3795 if (is_icmp_err_skb(skb) && !icmp_next)
3799 sk->sk_error_report(sk);
3803 EXPORT_SYMBOL(sock_dequeue_err_skb);
3806 * skb_clone_sk - create clone of skb, and take reference to socket
3807 * @skb: the skb to clone
3809 * This function creates a clone of a buffer that holds a reference on
3810 * sk_refcnt. Buffers created via this function are meant to be
3811 * returned using sock_queue_err_skb, or free via kfree_skb.
3813 * When passing buffers allocated with this function to sock_queue_err_skb
3814 * it is necessary to wrap the call with sock_hold/sock_put in order to
3815 * prevent the socket from being released prior to being enqueued on
3816 * the sk_error_queue.
3818 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3820 struct sock *sk = skb->sk;
3821 struct sk_buff *clone;
3823 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
3826 clone = skb_clone(skb, GFP_ATOMIC);
3833 clone->destructor = sock_efree;
3837 EXPORT_SYMBOL(skb_clone_sk);
3839 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3844 struct sock_exterr_skb *serr;
3847 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
3849 serr = SKB_EXT_ERR(skb);
3850 memset(serr, 0, sizeof(*serr));
3851 serr->ee.ee_errno = ENOMSG;
3852 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3853 serr->ee.ee_info = tstype;
3854 serr->opt_stats = opt_stats;
3855 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
3856 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3857 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3858 if (sk->sk_protocol == IPPROTO_TCP &&
3859 sk->sk_type == SOCK_STREAM)
3860 serr->ee.ee_data -= sk->sk_tskey;
3863 err = sock_queue_err_skb(sk, skb);
3869 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3873 if (likely(sysctl_tstamp_allow_data || tsonly))
3876 read_lock_bh(&sk->sk_callback_lock);
3877 ret = sk->sk_socket && sk->sk_socket->file &&
3878 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3879 read_unlock_bh(&sk->sk_callback_lock);
3883 void skb_complete_tx_timestamp(struct sk_buff *skb,
3884 struct skb_shared_hwtstamps *hwtstamps)
3886 struct sock *sk = skb->sk;
3888 if (!skb_may_tx_timestamp(sk, false))
3891 /* Take a reference to prevent skb_orphan() from freeing the socket,
3892 * but only if the socket refcount is not zero.
3894 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
3895 *skb_hwtstamps(skb) = *hwtstamps;
3896 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
3900 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3902 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3903 struct skb_shared_hwtstamps *hwtstamps,
3904 struct sock *sk, int tstype)
3906 struct sk_buff *skb;
3907 bool tsonly, opt_stats = false;
3912 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
3913 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
3916 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3917 if (!skb_may_tx_timestamp(sk, tsonly))
3922 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
3923 sk->sk_protocol == IPPROTO_TCP &&
3924 sk->sk_type == SOCK_STREAM) {
3925 skb = tcp_get_timestamping_opt_stats(sk);
3929 skb = alloc_skb(0, GFP_ATOMIC);
3931 skb = skb_clone(orig_skb, GFP_ATOMIC);
3937 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3939 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3943 *skb_hwtstamps(skb) = *hwtstamps;
3945 skb->tstamp = ktime_get_real();
3947 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
3949 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3951 void skb_tstamp_tx(struct sk_buff *orig_skb,
3952 struct skb_shared_hwtstamps *hwtstamps)
3954 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3957 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3959 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3961 struct sock *sk = skb->sk;
3962 struct sock_exterr_skb *serr;
3965 skb->wifi_acked_valid = 1;
3966 skb->wifi_acked = acked;
3968 serr = SKB_EXT_ERR(skb);
3969 memset(serr, 0, sizeof(*serr));
3970 serr->ee.ee_errno = ENOMSG;
3971 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3973 /* Take a reference to prevent skb_orphan() from freeing the socket,
3974 * but only if the socket refcount is not zero.
3976 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
3977 err = sock_queue_err_skb(sk, skb);
3983 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3986 * skb_partial_csum_set - set up and verify partial csum values for packet
3987 * @skb: the skb to set
3988 * @start: the number of bytes after skb->data to start checksumming.
3989 * @off: the offset from start to place the checksum.
3991 * For untrusted partially-checksummed packets, we need to make sure the values
3992 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3994 * This function checks and sets those values and skb->ip_summed: if this
3995 * returns false you should drop the packet.
3997 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3999 if (unlikely(start > skb_headlen(skb)) ||
4000 unlikely((int)start + off > skb_headlen(skb) - 2)) {
4001 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4002 start, off, skb_headlen(skb));
4005 skb->ip_summed = CHECKSUM_PARTIAL;
4006 skb->csum_start = skb_headroom(skb) + start;
4007 skb->csum_offset = off;
4008 skb_set_transport_header(skb, start);
4011 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4013 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4016 if (skb_headlen(skb) >= len)
4019 /* If we need to pullup then pullup to the max, so we
4020 * won't need to do it again.
4025 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4028 if (skb_headlen(skb) < len)
4034 #define MAX_TCP_HDR_LEN (15 * 4)
4036 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4037 typeof(IPPROTO_IP) proto,
4044 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4045 off + MAX_TCP_HDR_LEN);
4046 if (!err && !skb_partial_csum_set(skb, off,
4047 offsetof(struct tcphdr,
4050 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4053 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4054 off + sizeof(struct udphdr));
4055 if (!err && !skb_partial_csum_set(skb, off,
4056 offsetof(struct udphdr,
4059 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4062 return ERR_PTR(-EPROTO);
4065 /* This value should be large enough to cover a tagged ethernet header plus
4066 * maximally sized IP and TCP or UDP headers.
4068 #define MAX_IP_HDR_LEN 128
4070 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4079 err = skb_maybe_pull_tail(skb,
4080 sizeof(struct iphdr),
4085 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4088 off = ip_hdrlen(skb);
4095 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4097 return PTR_ERR(csum);
4100 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4103 ip_hdr(skb)->protocol, 0);
4110 /* This value should be large enough to cover a tagged ethernet header plus
4111 * an IPv6 header, all options, and a maximal TCP or UDP header.
4113 #define MAX_IPV6_HDR_LEN 256
4115 #define OPT_HDR(type, skb, off) \
4116 (type *)(skb_network_header(skb) + (off))
4118 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4131 off = sizeof(struct ipv6hdr);
4133 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4137 nexthdr = ipv6_hdr(skb)->nexthdr;
4139 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4140 while (off <= len && !done) {
4142 case IPPROTO_DSTOPTS:
4143 case IPPROTO_HOPOPTS:
4144 case IPPROTO_ROUTING: {
4145 struct ipv6_opt_hdr *hp;
4147 err = skb_maybe_pull_tail(skb,
4149 sizeof(struct ipv6_opt_hdr),
4154 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4155 nexthdr = hp->nexthdr;
4156 off += ipv6_optlen(hp);
4160 struct ip_auth_hdr *hp;
4162 err = skb_maybe_pull_tail(skb,
4164 sizeof(struct ip_auth_hdr),
4169 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4170 nexthdr = hp->nexthdr;
4171 off += ipv6_authlen(hp);
4174 case IPPROTO_FRAGMENT: {
4175 struct frag_hdr *hp;
4177 err = skb_maybe_pull_tail(skb,
4179 sizeof(struct frag_hdr),
4184 hp = OPT_HDR(struct frag_hdr, skb, off);
4186 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4189 nexthdr = hp->nexthdr;
4190 off += sizeof(struct frag_hdr);
4201 if (!done || fragment)
4204 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4206 return PTR_ERR(csum);
4209 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4210 &ipv6_hdr(skb)->daddr,
4211 skb->len - off, nexthdr, 0);
4219 * skb_checksum_setup - set up partial checksum offset
4220 * @skb: the skb to set up
4221 * @recalculate: if true the pseudo-header checksum will be recalculated
4223 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4227 switch (skb->protocol) {
4228 case htons(ETH_P_IP):
4229 err = skb_checksum_setup_ipv4(skb, recalculate);
4232 case htons(ETH_P_IPV6):
4233 err = skb_checksum_setup_ipv6(skb, recalculate);
4243 EXPORT_SYMBOL(skb_checksum_setup);
4246 * skb_checksum_maybe_trim - maybe trims the given skb
4247 * @skb: the skb to check
4248 * @transport_len: the data length beyond the network header
4250 * Checks whether the given skb has data beyond the given transport length.
4251 * If so, returns a cloned skb trimmed to this transport length.
4252 * Otherwise returns the provided skb. Returns NULL in error cases
4253 * (e.g. transport_len exceeds skb length or out-of-memory).
4255 * Caller needs to set the skb transport header and free any returned skb if it
4256 * differs from the provided skb.
4258 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4259 unsigned int transport_len)
4261 struct sk_buff *skb_chk;
4262 unsigned int len = skb_transport_offset(skb) + transport_len;
4267 else if (skb->len == len)
4270 skb_chk = skb_clone(skb, GFP_ATOMIC);
4274 ret = pskb_trim_rcsum(skb_chk, len);
4284 * skb_checksum_trimmed - validate checksum of an skb
4285 * @skb: the skb to check
4286 * @transport_len: the data length beyond the network header
4287 * @skb_chkf: checksum function to use
4289 * Applies the given checksum function skb_chkf to the provided skb.
4290 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4292 * If the skb has data beyond the given transport length, then a
4293 * trimmed & cloned skb is checked and returned.
4295 * Caller needs to set the skb transport header and free any returned skb if it
4296 * differs from the provided skb.
4298 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4299 unsigned int transport_len,
4300 __sum16(*skb_chkf)(struct sk_buff *skb))
4302 struct sk_buff *skb_chk;
4303 unsigned int offset = skb_transport_offset(skb);
4306 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4310 if (!pskb_may_pull(skb_chk, offset))
4313 skb_pull_rcsum(skb_chk, offset);
4314 ret = skb_chkf(skb_chk);
4315 skb_push_rcsum(skb_chk, offset);
4323 if (skb_chk && skb_chk != skb)
4329 EXPORT_SYMBOL(skb_checksum_trimmed);
4331 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4333 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4336 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4338 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4341 skb_release_head_state(skb);
4342 kmem_cache_free(skbuff_head_cache, skb);
4347 EXPORT_SYMBOL(kfree_skb_partial);
4350 * skb_try_coalesce - try to merge skb to prior one
4352 * @from: buffer to add
4353 * @fragstolen: pointer to boolean
4354 * @delta_truesize: how much more was allocated than was requested
4356 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4357 bool *fragstolen, int *delta_truesize)
4359 int i, delta, len = from->len;
4361 *fragstolen = false;
4366 if (len <= skb_tailroom(to)) {
4368 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4369 *delta_truesize = 0;
4373 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4376 if (skb_headlen(from) != 0) {
4378 unsigned int offset;
4380 if (skb_shinfo(to)->nr_frags +
4381 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4384 if (skb_head_is_locked(from))
4387 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4389 page = virt_to_head_page(from->head);
4390 offset = from->data - (unsigned char *)page_address(page);
4392 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4393 page, offset, skb_headlen(from));
4396 if (skb_shinfo(to)->nr_frags +
4397 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4400 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4403 WARN_ON_ONCE(delta < len);
4405 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4406 skb_shinfo(from)->frags,
4407 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4408 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4410 if (!skb_cloned(from))
4411 skb_shinfo(from)->nr_frags = 0;
4413 /* if the skb is not cloned this does nothing
4414 * since we set nr_frags to 0.
4416 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4417 skb_frag_ref(from, i);
4419 to->truesize += delta;
4421 to->data_len += len;
4423 *delta_truesize = delta;
4426 EXPORT_SYMBOL(skb_try_coalesce);
4429 * skb_scrub_packet - scrub an skb
4431 * @skb: buffer to clean
4432 * @xnet: packet is crossing netns
4434 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4435 * into/from a tunnel. Some information have to be cleared during these
4437 * skb_scrub_packet can also be used to clean a skb before injecting it in
4438 * another namespace (@xnet == true). We have to clear all information in the
4439 * skb that could impact namespace isolation.
4441 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4444 skb->pkt_type = PACKET_HOST;
4450 nf_reset_trace(skb);
4458 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4461 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4465 * skb_gso_transport_seglen is used to determine the real size of the
4466 * individual segments, including Layer4 headers (TCP/UDP).
4468 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4470 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4472 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4473 unsigned int thlen = 0;
4475 if (skb->encapsulation) {
4476 thlen = skb_inner_transport_header(skb) -
4477 skb_transport_header(skb);
4479 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4480 thlen += inner_tcp_hdrlen(skb);
4481 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4482 thlen = tcp_hdrlen(skb);
4483 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4484 thlen = sizeof(struct sctphdr);
4486 /* UFO sets gso_size to the size of the fragmentation
4487 * payload, i.e. the size of the L4 (UDP) header is already
4490 return thlen + shinfo->gso_size;
4492 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4495 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4498 * @mtu: MTU to validate against
4500 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4503 bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4505 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4506 const struct sk_buff *iter;
4509 hlen = skb_gso_network_seglen(skb);
4511 if (shinfo->gso_size != GSO_BY_FRAGS)
4514 /* Undo this so we can re-use header sizes */
4515 hlen -= GSO_BY_FRAGS;
4517 skb_walk_frags(skb, iter) {
4518 if (hlen + skb_headlen(iter) > mtu)
4524 EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4526 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4528 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4533 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4535 skb->mac_header += VLAN_HLEN;
4539 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4541 struct vlan_hdr *vhdr;
4544 if (unlikely(skb_vlan_tag_present(skb))) {
4545 /* vlan_tci is already set-up so leave this for another time */
4549 skb = skb_share_check(skb, GFP_ATOMIC);
4553 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4556 vhdr = (struct vlan_hdr *)skb->data;
4557 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4558 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4560 skb_pull_rcsum(skb, VLAN_HLEN);
4561 vlan_set_encap_proto(skb, vhdr);
4563 skb = skb_reorder_vlan_header(skb);
4567 skb_reset_network_header(skb);
4568 skb_reset_transport_header(skb);
4569 skb_reset_mac_len(skb);
4577 EXPORT_SYMBOL(skb_vlan_untag);
4579 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4581 if (!pskb_may_pull(skb, write_len))
4584 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4587 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4589 EXPORT_SYMBOL(skb_ensure_writable);
4591 /* remove VLAN header from packet and update csum accordingly.
4592 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4594 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4596 struct vlan_hdr *vhdr;
4597 int offset = skb->data - skb_mac_header(skb);
4600 if (WARN_ONCE(offset,
4601 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4606 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4610 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4612 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4613 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4615 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4616 __skb_pull(skb, VLAN_HLEN);
4618 vlan_set_encap_proto(skb, vhdr);
4619 skb->mac_header += VLAN_HLEN;
4621 if (skb_network_offset(skb) < ETH_HLEN)
4622 skb_set_network_header(skb, ETH_HLEN);
4624 skb_reset_mac_len(skb);
4628 EXPORT_SYMBOL(__skb_vlan_pop);
4630 /* Pop a vlan tag either from hwaccel or from payload.
4631 * Expects skb->data at mac header.
4633 int skb_vlan_pop(struct sk_buff *skb)
4639 if (likely(skb_vlan_tag_present(skb))) {
4642 if (unlikely(!eth_type_vlan(skb->protocol)))
4645 err = __skb_vlan_pop(skb, &vlan_tci);
4649 /* move next vlan tag to hw accel tag */
4650 if (likely(!eth_type_vlan(skb->protocol)))
4653 vlan_proto = skb->protocol;
4654 err = __skb_vlan_pop(skb, &vlan_tci);
4658 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4661 EXPORT_SYMBOL(skb_vlan_pop);
4663 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4664 * Expects skb->data at mac header.
4666 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4668 if (skb_vlan_tag_present(skb)) {
4669 int offset = skb->data - skb_mac_header(skb);
4672 if (WARN_ONCE(offset,
4673 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4678 err = __vlan_insert_tag(skb, skb->vlan_proto,
4679 skb_vlan_tag_get(skb));
4683 skb->protocol = skb->vlan_proto;
4684 skb->mac_len += VLAN_HLEN;
4686 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4688 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4691 EXPORT_SYMBOL(skb_vlan_push);
4694 * alloc_skb_with_frags - allocate skb with page frags
4696 * @header_len: size of linear part
4697 * @data_len: needed length in frags
4698 * @max_page_order: max page order desired.
4699 * @errcode: pointer to error code if any
4700 * @gfp_mask: allocation mask
4702 * This can be used to allocate a paged skb, given a maximal order for frags.
4704 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4705 unsigned long data_len,
4710 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4711 unsigned long chunk;
4712 struct sk_buff *skb;
4717 *errcode = -EMSGSIZE;
4718 /* Note this test could be relaxed, if we succeed to allocate
4719 * high order pages...
4721 if (npages > MAX_SKB_FRAGS)
4724 gfp_head = gfp_mask;
4725 if (gfp_head & __GFP_DIRECT_RECLAIM)
4726 gfp_head |= __GFP_RETRY_MAYFAIL;
4728 *errcode = -ENOBUFS;
4729 skb = alloc_skb(header_len, gfp_head);
4733 skb->truesize += npages << PAGE_SHIFT;
4735 for (i = 0; npages > 0; i++) {
4736 int order = max_page_order;
4739 if (npages >= 1 << order) {
4740 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4747 /* Do not retry other high order allocations */
4753 page = alloc_page(gfp_mask);
4757 chunk = min_t(unsigned long, data_len,
4758 PAGE_SIZE << order);
4759 skb_fill_page_desc(skb, i, page, 0, chunk);
4761 npages -= 1 << order;
4769 EXPORT_SYMBOL(alloc_skb_with_frags);
4771 /* carve out the first off bytes from skb when off < headlen */
4772 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4773 const int headlen, gfp_t gfp_mask)
4776 int size = skb_end_offset(skb);
4777 int new_hlen = headlen - off;
4780 size = SKB_DATA_ALIGN(size);
4782 if (skb_pfmemalloc(skb))
4783 gfp_mask |= __GFP_MEMALLOC;
4784 data = kmalloc_reserve(size +
4785 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4786 gfp_mask, NUMA_NO_NODE, NULL);
4790 size = SKB_WITH_OVERHEAD(ksize(data));
4792 /* Copy real data, and all frags */
4793 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4796 memcpy((struct skb_shared_info *)(data + size),
4798 offsetof(struct skb_shared_info,
4799 frags[skb_shinfo(skb)->nr_frags]));
4800 if (skb_cloned(skb)) {
4801 /* drop the old head gracefully */
4802 if (skb_orphan_frags(skb, gfp_mask)) {
4806 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4807 skb_frag_ref(skb, i);
4808 if (skb_has_frag_list(skb))
4809 skb_clone_fraglist(skb);
4810 skb_release_data(skb);
4812 /* we can reuse existing recount- all we did was
4821 #ifdef NET_SKBUFF_DATA_USES_OFFSET
4824 skb->end = skb->head + size;
4826 skb_set_tail_pointer(skb, skb_headlen(skb));
4827 skb_headers_offset_update(skb, 0);
4831 atomic_set(&skb_shinfo(skb)->dataref, 1);
4836 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4838 /* carve out the first eat bytes from skb's frag_list. May recurse into
4841 static int pskb_carve_frag_list(struct sk_buff *skb,
4842 struct skb_shared_info *shinfo, int eat,
4845 struct sk_buff *list = shinfo->frag_list;
4846 struct sk_buff *clone = NULL;
4847 struct sk_buff *insp = NULL;
4851 pr_err("Not enough bytes to eat. Want %d\n", eat);
4854 if (list->len <= eat) {
4855 /* Eaten as whole. */
4860 /* Eaten partially. */
4861 if (skb_shared(list)) {
4862 clone = skb_clone(list, gfp_mask);
4868 /* This may be pulled without problems. */
4871 if (pskb_carve(list, eat, gfp_mask) < 0) {
4879 /* Free pulled out fragments. */
4880 while ((list = shinfo->frag_list) != insp) {
4881 shinfo->frag_list = list->next;
4884 /* And insert new clone at head. */
4887 shinfo->frag_list = clone;
4892 /* carve off first len bytes from skb. Split line (off) is in the
4893 * non-linear part of skb
4895 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4896 int pos, gfp_t gfp_mask)
4899 int size = skb_end_offset(skb);
4901 const int nfrags = skb_shinfo(skb)->nr_frags;
4902 struct skb_shared_info *shinfo;
4904 size = SKB_DATA_ALIGN(size);
4906 if (skb_pfmemalloc(skb))
4907 gfp_mask |= __GFP_MEMALLOC;
4908 data = kmalloc_reserve(size +
4909 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4910 gfp_mask, NUMA_NO_NODE, NULL);
4914 size = SKB_WITH_OVERHEAD(ksize(data));
4916 memcpy((struct skb_shared_info *)(data + size),
4917 skb_shinfo(skb), offsetof(struct skb_shared_info,
4918 frags[skb_shinfo(skb)->nr_frags]));
4919 if (skb_orphan_frags(skb, gfp_mask)) {
4923 shinfo = (struct skb_shared_info *)(data + size);
4924 for (i = 0; i < nfrags; i++) {
4925 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4927 if (pos + fsize > off) {
4928 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4932 * We have two variants in this case:
4933 * 1. Move all the frag to the second
4934 * part, if it is possible. F.e.
4935 * this approach is mandatory for TUX,
4936 * where splitting is expensive.
4937 * 2. Split is accurately. We make this.
4939 shinfo->frags[0].page_offset += off - pos;
4940 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4942 skb_frag_ref(skb, i);
4947 shinfo->nr_frags = k;
4948 if (skb_has_frag_list(skb))
4949 skb_clone_fraglist(skb);
4952 /* split line is in frag list */
4953 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4955 skb_release_data(skb);
4960 #ifdef NET_SKBUFF_DATA_USES_OFFSET
4963 skb->end = skb->head + size;
4965 skb_reset_tail_pointer(skb);
4966 skb_headers_offset_update(skb, 0);
4971 skb->data_len = skb->len;
4972 atomic_set(&skb_shinfo(skb)->dataref, 1);
4976 /* remove len bytes from the beginning of the skb */
4977 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4979 int headlen = skb_headlen(skb);
4982 return pskb_carve_inside_header(skb, len, headlen, gfp);
4984 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4987 /* Extract to_copy bytes starting at off from skb, and return this in
4990 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
4991 int to_copy, gfp_t gfp)
4993 struct sk_buff *clone = skb_clone(skb, gfp);
4998 if (pskb_carve(clone, off, gfp) < 0 ||
4999 pskb_trim(clone, to_copy)) {
5005 EXPORT_SYMBOL(pskb_extract);
5008 * skb_condense - try to get rid of fragments/frag_list if possible
5011 * Can be used to save memory before skb is added to a busy queue.
5012 * If packet has bytes in frags and enough tail room in skb->head,
5013 * pull all of them, so that we can free the frags right now and adjust
5016 * We do not reallocate skb->head thus can not fail.
5017 * Caller must re-evaluate skb->truesize if needed.
5019 void skb_condense(struct sk_buff *skb)
5021 if (skb->data_len) {
5022 if (skb->data_len > skb->end - skb->tail ||
5026 /* Nice, we can free page frag(s) right now */
5027 __pskb_pull_tail(skb, skb->data_len);
5029 /* At this point, skb->truesize might be over estimated,
5030 * because skb had a fragment, and fragments do not tell
5032 * When we pulled its content into skb->head, fragment
5033 * was freed, but __pskb_pull_tail() could not possibly
5034 * adjust skb->truesize, not knowing the frag truesize.
5036 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));