arm64: mm: ensure that the zero page is visible to the page table walker
[linux/fpc-iii.git] / net / core / skbuff.c
blob05195b8c871890b8081dd7734eeceabaf0637f84
1 /*
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>
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
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
23 * NOTE:
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>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
55 #endif
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
66 #include <net/protocol.h>
67 #include <net/dst.h>
68 #include <net/sock.h>
69 #include <net/checksum.h>
70 #include <net/xfrm.h>
72 #include <asm/uaccess.h>
73 #include <trace/events/skb.h>
74 #include <linux/highmem.h>
76 struct kmem_cache *skbuff_head_cache __read_mostly;
77 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
79 /**
80 * skb_panic - private function for out-of-line support
81 * @skb: buffer
82 * @sz: size
83 * @addr: address
84 * @msg: skb_over_panic or skb_under_panic
86 * Out-of-line support for skb_put() and skb_push().
87 * Called via the wrapper skb_over_panic() or skb_under_panic().
88 * Keep out of line to prevent kernel bloat.
89 * __builtin_return_address is not used because it is not always reliable.
91 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
92 const char msg[])
94 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
95 msg, addr, skb->len, sz, skb->head, skb->data,
96 (unsigned long)skb->tail, (unsigned long)skb->end,
97 skb->dev ? skb->dev->name : "<NULL>");
98 BUG();
101 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
103 skb_panic(skb, sz, addr, __func__);
106 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
108 skb_panic(skb, sz, addr, __func__);
112 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
113 * the caller if emergency pfmemalloc reserves are being used. If it is and
114 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
115 * may be used. Otherwise, the packet data may be discarded until enough
116 * memory is free
118 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
119 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
121 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
122 unsigned long ip, bool *pfmemalloc)
124 void *obj;
125 bool ret_pfmemalloc = false;
128 * Try a regular allocation, when that fails and we're not entitled
129 * to the reserves, fail.
131 obj = kmalloc_node_track_caller(size,
132 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
133 node);
134 if (obj || !(gfp_pfmemalloc_allowed(flags)))
135 goto out;
137 /* Try again but now we are using pfmemalloc reserves */
138 ret_pfmemalloc = true;
139 obj = kmalloc_node_track_caller(size, flags, node);
141 out:
142 if (pfmemalloc)
143 *pfmemalloc = ret_pfmemalloc;
145 return obj;
148 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
149 * 'private' fields and also do memory statistics to find all the
150 * [BEEP] leaks.
154 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
156 struct sk_buff *skb;
158 /* Get the HEAD */
159 skb = kmem_cache_alloc_node(skbuff_head_cache,
160 gfp_mask & ~__GFP_DMA, node);
161 if (!skb)
162 goto out;
165 * Only clear those fields we need to clear, not those that we will
166 * actually initialise below. Hence, don't put any more fields after
167 * the tail pointer in struct sk_buff!
169 memset(skb, 0, offsetof(struct sk_buff, tail));
170 skb->head = NULL;
171 skb->truesize = sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
174 #ifdef NET_SKBUFF_DATA_USES_OFFSET
175 skb->mac_header = ~0U;
176 #endif
177 out:
178 return skb;
182 * __alloc_skb - allocate a network buffer
183 * @size: size to allocate
184 * @gfp_mask: allocation mask
185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
189 * @node: numa node to allocate memory on
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
198 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
199 int flags, int node)
201 struct kmem_cache *cache;
202 struct skb_shared_info *shinfo;
203 struct sk_buff *skb;
204 u8 *data;
205 bool pfmemalloc;
207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
213 /* Get the HEAD */
214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
215 if (!skb)
216 goto out;
217 prefetchw(skb);
219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
224 size = SKB_DATA_ALIGN(size);
225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
227 if (!data)
228 goto nodata;
229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
233 size = SKB_WITH_OVERHEAD(ksize(data));
234 prefetchw(data + size);
237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
241 memset(skb, 0, offsetof(struct sk_buff, tail));
242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
244 skb->pfmemalloc = pfmemalloc;
245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
248 skb_reset_tail_pointer(skb);
249 skb->end = skb->tail + size;
250 #ifdef NET_SKBUFF_DATA_USES_OFFSET
251 skb->mac_header = ~0U;
252 skb->transport_header = ~0U;
253 #endif
255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
258 atomic_set(&shinfo->dataref, 1);
259 kmemcheck_annotate_variable(shinfo->destructor_arg);
261 if (flags & SKB_ALLOC_FCLONE) {
262 struct sk_buff *child = skb + 1;
263 atomic_t *fclone_ref = (atomic_t *) (child + 1);
265 kmemcheck_annotate_bitfield(child, flags1);
266 kmemcheck_annotate_bitfield(child, flags2);
267 skb->fclone = SKB_FCLONE_ORIG;
268 atomic_set(fclone_ref, 1);
270 child->fclone = SKB_FCLONE_UNAVAILABLE;
271 child->pfmemalloc = pfmemalloc;
273 out:
274 return skb;
275 nodata:
276 kmem_cache_free(cache, skb);
277 skb = NULL;
278 goto out;
280 EXPORT_SYMBOL(__alloc_skb);
283 * build_skb - build a network buffer
284 * @data: data buffer provided by caller
285 * @frag_size: size of fragment, or 0 if head was kmalloced
287 * Allocate a new &sk_buff. Caller provides space holding head and
288 * skb_shared_info. @data must have been allocated by kmalloc()
289 * The return is the new skb buffer.
290 * On a failure the return is %NULL, and @data is not freed.
291 * Notes :
292 * Before IO, driver allocates only data buffer where NIC put incoming frame
293 * Driver should add room at head (NET_SKB_PAD) and
294 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
295 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
296 * before giving packet to stack.
297 * RX rings only contains data buffers, not full skbs.
299 struct sk_buff *build_skb(void *data, unsigned int frag_size)
301 struct skb_shared_info *shinfo;
302 struct sk_buff *skb;
303 unsigned int size = frag_size ? : ksize(data);
305 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
306 if (!skb)
307 return NULL;
309 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
311 memset(skb, 0, offsetof(struct sk_buff, tail));
312 skb->truesize = SKB_TRUESIZE(size);
313 skb->head_frag = frag_size != 0;
314 atomic_set(&skb->users, 1);
315 skb->head = data;
316 skb->data = data;
317 skb_reset_tail_pointer(skb);
318 skb->end = skb->tail + size;
319 #ifdef NET_SKBUFF_DATA_USES_OFFSET
320 skb->mac_header = ~0U;
321 skb->transport_header = ~0U;
322 #endif
324 /* make sure we initialize shinfo sequentially */
325 shinfo = skb_shinfo(skb);
326 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
327 atomic_set(&shinfo->dataref, 1);
328 kmemcheck_annotate_variable(shinfo->destructor_arg);
330 return skb;
332 EXPORT_SYMBOL(build_skb);
334 struct netdev_alloc_cache {
335 struct page_frag frag;
336 /* we maintain a pagecount bias, so that we dont dirty cache line
337 * containing page->_count every time we allocate a fragment.
339 unsigned int pagecnt_bias;
341 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
343 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
345 struct netdev_alloc_cache *nc;
346 void *data = NULL;
347 int order;
348 unsigned long flags;
350 local_irq_save(flags);
351 nc = &__get_cpu_var(netdev_alloc_cache);
352 if (unlikely(!nc->frag.page)) {
353 refill:
354 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
355 gfp_t gfp = gfp_mask;
357 if (order)
358 gfp |= __GFP_COMP | __GFP_NOWARN;
359 nc->frag.page = alloc_pages(gfp, order);
360 if (likely(nc->frag.page))
361 break;
362 if (--order < 0)
363 goto end;
365 nc->frag.size = PAGE_SIZE << order;
366 recycle:
367 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
369 nc->frag.offset = 0;
372 if (nc->frag.offset + fragsz > nc->frag.size) {
373 /* avoid unnecessary locked operations if possible */
374 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
375 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
376 goto recycle;
377 goto refill;
380 data = page_address(nc->frag.page) + nc->frag.offset;
381 nc->frag.offset += fragsz;
382 nc->pagecnt_bias--;
383 end:
384 local_irq_restore(flags);
385 return data;
389 * netdev_alloc_frag - allocate a page fragment
390 * @fragsz: fragment size
392 * Allocates a frag from a page for receive buffer.
393 * Uses GFP_ATOMIC allocations.
395 void *netdev_alloc_frag(unsigned int fragsz)
397 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399 EXPORT_SYMBOL(netdev_alloc_frag);
402 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
403 * @dev: network device to receive on
404 * @length: length to allocate
405 * @gfp_mask: get_free_pages mask, passed to alloc_skb
407 * Allocate a new &sk_buff and assign it a usage count of one. The
408 * buffer has unspecified headroom built in. Users should allocate
409 * the headroom they think they need without accounting for the
410 * built in space. The built in space is used for optimisations.
412 * %NULL is returned if there is no free memory.
414 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
415 unsigned int length, gfp_t gfp_mask)
417 struct sk_buff *skb = NULL;
418 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
419 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
421 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
422 void *data;
424 if (sk_memalloc_socks())
425 gfp_mask |= __GFP_MEMALLOC;
427 data = __netdev_alloc_frag(fragsz, gfp_mask);
429 if (likely(data)) {
430 skb = build_skb(data, fragsz);
431 if (unlikely(!skb))
432 put_page(virt_to_head_page(data));
434 } else {
435 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
436 SKB_ALLOC_RX, NUMA_NO_NODE);
438 if (likely(skb)) {
439 skb_reserve(skb, NET_SKB_PAD);
440 skb->dev = dev;
442 return skb;
444 EXPORT_SYMBOL(__netdev_alloc_skb);
446 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
447 int size, unsigned int truesize)
449 skb_fill_page_desc(skb, i, page, off, size);
450 skb->len += size;
451 skb->data_len += size;
452 skb->truesize += truesize;
454 EXPORT_SYMBOL(skb_add_rx_frag);
456 static void skb_drop_list(struct sk_buff **listp)
458 kfree_skb_list(*listp);
459 *listp = NULL;
462 static inline void skb_drop_fraglist(struct sk_buff *skb)
464 skb_drop_list(&skb_shinfo(skb)->frag_list);
467 static void skb_clone_fraglist(struct sk_buff *skb)
469 struct sk_buff *list;
471 skb_walk_frags(skb, list)
472 skb_get(list);
475 static void skb_free_head(struct sk_buff *skb)
477 if (skb->head_frag)
478 put_page(virt_to_head_page(skb->head));
479 else
480 kfree(skb->head);
483 static void skb_release_data(struct sk_buff *skb)
485 if (!skb->cloned ||
486 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
487 &skb_shinfo(skb)->dataref)) {
488 if (skb_shinfo(skb)->nr_frags) {
489 int i;
490 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
491 skb_frag_unref(skb, i);
495 * If skb buf is from userspace, we need to notify the caller
496 * the lower device DMA has done;
498 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
499 struct ubuf_info *uarg;
501 uarg = skb_shinfo(skb)->destructor_arg;
502 if (uarg->callback)
503 uarg->callback(uarg, true);
506 if (skb_has_frag_list(skb))
507 skb_drop_fraglist(skb);
509 skb_free_head(skb);
514 * Free an skbuff by memory without cleaning the state.
516 static void kfree_skbmem(struct sk_buff *skb)
518 struct sk_buff *other;
519 atomic_t *fclone_ref;
521 switch (skb->fclone) {
522 case SKB_FCLONE_UNAVAILABLE:
523 kmem_cache_free(skbuff_head_cache, skb);
524 break;
526 case SKB_FCLONE_ORIG:
527 fclone_ref = (atomic_t *) (skb + 2);
528 if (atomic_dec_and_test(fclone_ref))
529 kmem_cache_free(skbuff_fclone_cache, skb);
530 break;
532 case SKB_FCLONE_CLONE:
533 fclone_ref = (atomic_t *) (skb + 1);
534 other = skb - 1;
536 /* The clone portion is available for
537 * fast-cloning again.
539 skb->fclone = SKB_FCLONE_UNAVAILABLE;
541 if (atomic_dec_and_test(fclone_ref))
542 kmem_cache_free(skbuff_fclone_cache, other);
543 break;
547 static void skb_release_head_state(struct sk_buff *skb)
549 skb_dst_drop(skb);
550 #ifdef CONFIG_XFRM
551 secpath_put(skb->sp);
552 #endif
553 if (skb->destructor) {
554 WARN_ON(in_irq());
555 skb->destructor(skb);
557 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
558 nf_conntrack_put(skb->nfct);
559 #endif
560 #ifdef CONFIG_BRIDGE_NETFILTER
561 nf_bridge_put(skb->nf_bridge);
562 #endif
563 /* XXX: IS this still necessary? - JHS */
564 #ifdef CONFIG_NET_SCHED
565 skb->tc_index = 0;
566 #ifdef CONFIG_NET_CLS_ACT
567 skb->tc_verd = 0;
568 #endif
569 #endif
572 /* Free everything but the sk_buff shell. */
573 static void skb_release_all(struct sk_buff *skb)
575 skb_release_head_state(skb);
576 if (likely(skb->head))
577 skb_release_data(skb);
581 * __kfree_skb - private function
582 * @skb: buffer
584 * Free an sk_buff. Release anything attached to the buffer.
585 * Clean the state. This is an internal helper function. Users should
586 * always call kfree_skb
589 void __kfree_skb(struct sk_buff *skb)
591 skb_release_all(skb);
592 kfree_skbmem(skb);
594 EXPORT_SYMBOL(__kfree_skb);
597 * kfree_skb - free an sk_buff
598 * @skb: buffer to free
600 * Drop a reference to the buffer and free it if the usage count has
601 * hit zero.
603 void kfree_skb(struct sk_buff *skb)
605 if (unlikely(!skb))
606 return;
607 if (likely(atomic_read(&skb->users) == 1))
608 smp_rmb();
609 else if (likely(!atomic_dec_and_test(&skb->users)))
610 return;
611 trace_kfree_skb(skb, __builtin_return_address(0));
612 __kfree_skb(skb);
614 EXPORT_SYMBOL(kfree_skb);
616 void kfree_skb_list(struct sk_buff *segs)
618 while (segs) {
619 struct sk_buff *next = segs->next;
621 kfree_skb(segs);
622 segs = next;
625 EXPORT_SYMBOL(kfree_skb_list);
628 * skb_tx_error - report an sk_buff xmit error
629 * @skb: buffer that triggered an error
631 * Report xmit error if a device callback is tracking this skb.
632 * skb must be freed afterwards.
634 void skb_tx_error(struct sk_buff *skb)
636 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
637 struct ubuf_info *uarg;
639 uarg = skb_shinfo(skb)->destructor_arg;
640 if (uarg->callback)
641 uarg->callback(uarg, false);
642 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
645 EXPORT_SYMBOL(skb_tx_error);
648 * consume_skb - free an skbuff
649 * @skb: buffer to free
651 * Drop a ref to the buffer and free it if the usage count has hit zero
652 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
653 * is being dropped after a failure and notes that
655 void consume_skb(struct sk_buff *skb)
657 if (unlikely(!skb))
658 return;
659 if (likely(atomic_read(&skb->users) == 1))
660 smp_rmb();
661 else if (likely(!atomic_dec_and_test(&skb->users)))
662 return;
663 trace_consume_skb(skb);
664 __kfree_skb(skb);
666 EXPORT_SYMBOL(consume_skb);
668 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
670 new->tstamp = old->tstamp;
671 new->dev = old->dev;
672 new->transport_header = old->transport_header;
673 new->network_header = old->network_header;
674 new->mac_header = old->mac_header;
675 new->inner_transport_header = old->inner_transport_header;
676 new->inner_network_header = old->inner_network_header;
677 new->inner_mac_header = old->inner_mac_header;
678 skb_dst_copy(new, old);
679 new->rxhash = old->rxhash;
680 new->ooo_okay = old->ooo_okay;
681 new->l4_rxhash = old->l4_rxhash;
682 new->no_fcs = old->no_fcs;
683 new->encapsulation = old->encapsulation;
684 #ifdef CONFIG_XFRM
685 new->sp = secpath_get(old->sp);
686 #endif
687 memcpy(new->cb, old->cb, sizeof(old->cb));
688 new->csum = old->csum;
689 new->local_df = old->local_df;
690 new->pkt_type = old->pkt_type;
691 new->ip_summed = old->ip_summed;
692 skb_copy_queue_mapping(new, old);
693 new->priority = old->priority;
694 #if IS_ENABLED(CONFIG_IP_VS)
695 new->ipvs_property = old->ipvs_property;
696 #endif
697 new->pfmemalloc = old->pfmemalloc;
698 new->protocol = old->protocol;
699 new->mark = old->mark;
700 new->skb_iif = old->skb_iif;
701 __nf_copy(new, old);
702 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
703 new->nf_trace = old->nf_trace;
704 #endif
705 #ifdef CONFIG_NET_SCHED
706 new->tc_index = old->tc_index;
707 #ifdef CONFIG_NET_CLS_ACT
708 new->tc_verd = old->tc_verd;
709 #endif
710 #endif
711 new->vlan_proto = old->vlan_proto;
712 new->vlan_tci = old->vlan_tci;
714 skb_copy_secmark(new, old);
718 * You should not add any new code to this function. Add it to
719 * __copy_skb_header above instead.
721 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
723 #define C(x) n->x = skb->x
725 n->next = n->prev = NULL;
726 n->sk = NULL;
727 __copy_skb_header(n, skb);
729 C(len);
730 C(data_len);
731 C(mac_len);
732 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
733 n->cloned = 1;
734 n->nohdr = 0;
735 n->destructor = NULL;
736 C(tail);
737 C(end);
738 C(head);
739 C(head_frag);
740 C(data);
741 C(truesize);
742 atomic_set(&n->users, 1);
744 atomic_inc(&(skb_shinfo(skb)->dataref));
745 skb->cloned = 1;
747 return n;
748 #undef C
752 * skb_morph - morph one skb into another
753 * @dst: the skb to receive the contents
754 * @src: the skb to supply the contents
756 * This is identical to skb_clone except that the target skb is
757 * supplied by the user.
759 * The target skb is returned upon exit.
761 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
763 skb_release_all(dst);
764 return __skb_clone(dst, src);
766 EXPORT_SYMBOL_GPL(skb_morph);
769 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
770 * @skb: the skb to modify
771 * @gfp_mask: allocation priority
773 * This must be called on SKBTX_DEV_ZEROCOPY skb.
774 * It will copy all frags into kernel and drop the reference
775 * to userspace pages.
777 * If this function is called from an interrupt gfp_mask() must be
778 * %GFP_ATOMIC.
780 * Returns 0 on success or a negative error code on failure
781 * to allocate kernel memory to copy to.
783 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
785 int i;
786 int num_frags = skb_shinfo(skb)->nr_frags;
787 struct page *page, *head = NULL;
788 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
790 for (i = 0; i < num_frags; i++) {
791 u8 *vaddr;
792 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
794 page = alloc_page(gfp_mask);
795 if (!page) {
796 while (head) {
797 struct page *next = (struct page *)head->private;
798 put_page(head);
799 head = next;
801 return -ENOMEM;
803 vaddr = kmap_atomic(skb_frag_page(f));
804 memcpy(page_address(page),
805 vaddr + f->page_offset, skb_frag_size(f));
806 kunmap_atomic(vaddr);
807 page->private = (unsigned long)head;
808 head = page;
811 /* skb frags release userspace buffers */
812 for (i = 0; i < num_frags; i++)
813 skb_frag_unref(skb, i);
815 uarg->callback(uarg, false);
817 /* skb frags point to kernel buffers */
818 for (i = num_frags - 1; i >= 0; i--) {
819 __skb_fill_page_desc(skb, i, head, 0,
820 skb_shinfo(skb)->frags[i].size);
821 head = (struct page *)head->private;
824 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
825 return 0;
827 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
830 * skb_clone - duplicate an sk_buff
831 * @skb: buffer to clone
832 * @gfp_mask: allocation priority
834 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
835 * copies share the same packet data but not structure. The new
836 * buffer has a reference count of 1. If the allocation fails the
837 * function returns %NULL otherwise the new buffer is returned.
839 * If this function is called from an interrupt gfp_mask() must be
840 * %GFP_ATOMIC.
843 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
845 struct sk_buff *n;
847 if (skb_orphan_frags(skb, gfp_mask))
848 return NULL;
850 n = skb + 1;
851 if (skb->fclone == SKB_FCLONE_ORIG &&
852 n->fclone == SKB_FCLONE_UNAVAILABLE) {
853 atomic_t *fclone_ref = (atomic_t *) (n + 1);
854 n->fclone = SKB_FCLONE_CLONE;
855 atomic_inc(fclone_ref);
856 } else {
857 if (skb_pfmemalloc(skb))
858 gfp_mask |= __GFP_MEMALLOC;
860 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
861 if (!n)
862 return NULL;
864 kmemcheck_annotate_bitfield(n, flags1);
865 kmemcheck_annotate_bitfield(n, flags2);
866 n->fclone = SKB_FCLONE_UNAVAILABLE;
869 return __skb_clone(n, skb);
871 EXPORT_SYMBOL(skb_clone);
873 static void skb_headers_offset_update(struct sk_buff *skb, int off)
875 /* {transport,network,mac}_header and tail are relative to skb->head */
876 skb->transport_header += off;
877 skb->network_header += off;
878 if (skb_mac_header_was_set(skb))
879 skb->mac_header += off;
880 skb->inner_transport_header += off;
881 skb->inner_network_header += off;
882 skb->inner_mac_header += off;
885 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
887 #ifndef NET_SKBUFF_DATA_USES_OFFSET
889 * Shift between the two data areas in bytes
891 unsigned long offset = new->data - old->data;
892 #endif
894 __copy_skb_header(new, old);
896 #ifndef NET_SKBUFF_DATA_USES_OFFSET
897 skb_headers_offset_update(new, offset);
898 #endif
899 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
900 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
901 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
904 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
906 if (skb_pfmemalloc(skb))
907 return SKB_ALLOC_RX;
908 return 0;
912 * skb_copy - create private copy of an sk_buff
913 * @skb: buffer to copy
914 * @gfp_mask: allocation priority
916 * Make a copy of both an &sk_buff and its data. This is used when the
917 * caller wishes to modify the data and needs a private copy of the
918 * data to alter. Returns %NULL on failure or the pointer to the buffer
919 * on success. The returned buffer has a reference count of 1.
921 * As by-product this function converts non-linear &sk_buff to linear
922 * one, so that &sk_buff becomes completely private and caller is allowed
923 * to modify all the data of returned buffer. This means that this
924 * function is not recommended for use in circumstances when only
925 * header is going to be modified. Use pskb_copy() instead.
928 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
930 int headerlen = skb_headroom(skb);
931 unsigned int size = skb_end_offset(skb) + skb->data_len;
932 struct sk_buff *n = __alloc_skb(size, gfp_mask,
933 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
935 if (!n)
936 return NULL;
938 /* Set the data pointer */
939 skb_reserve(n, headerlen);
940 /* Set the tail pointer and length */
941 skb_put(n, skb->len);
943 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
944 BUG();
946 copy_skb_header(n, skb);
947 return n;
949 EXPORT_SYMBOL(skb_copy);
952 * __pskb_copy - create copy of an sk_buff with private head.
953 * @skb: buffer to copy
954 * @headroom: headroom of new skb
955 * @gfp_mask: allocation priority
957 * Make a copy of both an &sk_buff and part of its data, located
958 * in header. Fragmented data remain shared. This is used when
959 * the caller wishes to modify only header of &sk_buff and needs
960 * private copy of the header to alter. Returns %NULL on failure
961 * or the pointer to the buffer on success.
962 * The returned buffer has a reference count of 1.
965 struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
967 unsigned int size = skb_headlen(skb) + headroom;
968 struct sk_buff *n = __alloc_skb(size, gfp_mask,
969 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
971 if (!n)
972 goto out;
974 /* Set the data pointer */
975 skb_reserve(n, headroom);
976 /* Set the tail pointer and length */
977 skb_put(n, skb_headlen(skb));
978 /* Copy the bytes */
979 skb_copy_from_linear_data(skb, n->data, n->len);
981 n->truesize += skb->data_len;
982 n->data_len = skb->data_len;
983 n->len = skb->len;
985 if (skb_shinfo(skb)->nr_frags) {
986 int i;
988 if (skb_orphan_frags(skb, gfp_mask)) {
989 kfree_skb(n);
990 n = NULL;
991 goto out;
993 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
994 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
995 skb_frag_ref(skb, i);
997 skb_shinfo(n)->nr_frags = i;
1000 if (skb_has_frag_list(skb)) {
1001 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1002 skb_clone_fraglist(n);
1005 copy_skb_header(n, skb);
1006 out:
1007 return n;
1009 EXPORT_SYMBOL(__pskb_copy);
1012 * pskb_expand_head - reallocate header of &sk_buff
1013 * @skb: buffer to reallocate
1014 * @nhead: room to add at head
1015 * @ntail: room to add at tail
1016 * @gfp_mask: allocation priority
1018 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1019 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1020 * reference count of 1. Returns zero in the case of success or error,
1021 * if expansion failed. In the last case, &sk_buff is not changed.
1023 * All the pointers pointing into skb header may change and must be
1024 * reloaded after call to this function.
1027 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1028 gfp_t gfp_mask)
1030 int i;
1031 u8 *data;
1032 int size = nhead + skb_end_offset(skb) + ntail;
1033 long off;
1035 BUG_ON(nhead < 0);
1037 if (skb_shared(skb))
1038 BUG();
1040 size = SKB_DATA_ALIGN(size);
1042 if (skb_pfmemalloc(skb))
1043 gfp_mask |= __GFP_MEMALLOC;
1044 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1045 gfp_mask, NUMA_NO_NODE, NULL);
1046 if (!data)
1047 goto nodata;
1048 size = SKB_WITH_OVERHEAD(ksize(data));
1050 /* Copy only real data... and, alas, header. This should be
1051 * optimized for the cases when header is void.
1053 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1055 memcpy((struct skb_shared_info *)(data + size),
1056 skb_shinfo(skb),
1057 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1060 * if shinfo is shared we must drop the old head gracefully, but if it
1061 * is not we can just drop the old head and let the existing refcount
1062 * be since all we did is relocate the values
1064 if (skb_cloned(skb)) {
1065 /* copy this zero copy skb frags */
1066 if (skb_orphan_frags(skb, gfp_mask))
1067 goto nofrags;
1068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1069 skb_frag_ref(skb, i);
1071 if (skb_has_frag_list(skb))
1072 skb_clone_fraglist(skb);
1074 skb_release_data(skb);
1075 } else {
1076 skb_free_head(skb);
1078 off = (data + nhead) - skb->head;
1080 skb->head = data;
1081 skb->head_frag = 0;
1082 skb->data += off;
1083 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1084 skb->end = size;
1085 off = nhead;
1086 #else
1087 skb->end = skb->head + size;
1088 #endif
1089 skb->tail += off;
1090 skb_headers_offset_update(skb, off);
1091 /* Only adjust this if it actually is csum_start rather than csum */
1092 if (skb->ip_summed == CHECKSUM_PARTIAL)
1093 skb->csum_start += nhead;
1094 skb->cloned = 0;
1095 skb->hdr_len = 0;
1096 skb->nohdr = 0;
1097 atomic_set(&skb_shinfo(skb)->dataref, 1);
1098 return 0;
1100 nofrags:
1101 kfree(data);
1102 nodata:
1103 return -ENOMEM;
1105 EXPORT_SYMBOL(pskb_expand_head);
1107 /* Make private copy of skb with writable head and some headroom */
1109 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1111 struct sk_buff *skb2;
1112 int delta = headroom - skb_headroom(skb);
1114 if (delta <= 0)
1115 skb2 = pskb_copy(skb, GFP_ATOMIC);
1116 else {
1117 skb2 = skb_clone(skb, GFP_ATOMIC);
1118 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1119 GFP_ATOMIC)) {
1120 kfree_skb(skb2);
1121 skb2 = NULL;
1124 return skb2;
1126 EXPORT_SYMBOL(skb_realloc_headroom);
1129 * skb_copy_expand - copy and expand sk_buff
1130 * @skb: buffer to copy
1131 * @newheadroom: new free bytes at head
1132 * @newtailroom: new free bytes at tail
1133 * @gfp_mask: allocation priority
1135 * Make a copy of both an &sk_buff and its data and while doing so
1136 * allocate additional space.
1138 * This is used when the caller wishes to modify the data and needs a
1139 * private copy of the data to alter as well as more space for new fields.
1140 * Returns %NULL on failure or the pointer to the buffer
1141 * on success. The returned buffer has a reference count of 1.
1143 * You must pass %GFP_ATOMIC as the allocation priority if this function
1144 * is called from an interrupt.
1146 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1147 int newheadroom, int newtailroom,
1148 gfp_t gfp_mask)
1151 * Allocate the copy buffer
1153 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1154 gfp_mask, skb_alloc_rx_flag(skb),
1155 NUMA_NO_NODE);
1156 int oldheadroom = skb_headroom(skb);
1157 int head_copy_len, head_copy_off;
1158 int off;
1160 if (!n)
1161 return NULL;
1163 skb_reserve(n, newheadroom);
1165 /* Set the tail pointer and length */
1166 skb_put(n, skb->len);
1168 head_copy_len = oldheadroom;
1169 head_copy_off = 0;
1170 if (newheadroom <= head_copy_len)
1171 head_copy_len = newheadroom;
1172 else
1173 head_copy_off = newheadroom - head_copy_len;
1175 /* Copy the linear header and data. */
1176 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1177 skb->len + head_copy_len))
1178 BUG();
1180 copy_skb_header(n, skb);
1182 off = newheadroom - oldheadroom;
1183 if (n->ip_summed == CHECKSUM_PARTIAL)
1184 n->csum_start += off;
1185 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1186 skb_headers_offset_update(n, off);
1187 #endif
1189 return n;
1191 EXPORT_SYMBOL(skb_copy_expand);
1194 * skb_pad - zero pad the tail of an skb
1195 * @skb: buffer to pad
1196 * @pad: space to pad
1198 * Ensure that a buffer is followed by a padding area that is zero
1199 * filled. Used by network drivers which may DMA or transfer data
1200 * beyond the buffer end onto the wire.
1202 * May return error in out of memory cases. The skb is freed on error.
1205 int skb_pad(struct sk_buff *skb, int pad)
1207 int err;
1208 int ntail;
1210 /* If the skbuff is non linear tailroom is always zero.. */
1211 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1212 memset(skb->data+skb->len, 0, pad);
1213 return 0;
1216 ntail = skb->data_len + pad - (skb->end - skb->tail);
1217 if (likely(skb_cloned(skb) || ntail > 0)) {
1218 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1219 if (unlikely(err))
1220 goto free_skb;
1223 /* FIXME: The use of this function with non-linear skb's really needs
1224 * to be audited.
1226 err = skb_linearize(skb);
1227 if (unlikely(err))
1228 goto free_skb;
1230 memset(skb->data + skb->len, 0, pad);
1231 return 0;
1233 free_skb:
1234 kfree_skb(skb);
1235 return err;
1237 EXPORT_SYMBOL(skb_pad);
1240 * skb_put - add data to a buffer
1241 * @skb: buffer to use
1242 * @len: amount of data to add
1244 * This function extends the used data area of the buffer. If this would
1245 * exceed the total buffer size the kernel will panic. A pointer to the
1246 * first byte of the extra data is returned.
1248 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1250 unsigned char *tmp = skb_tail_pointer(skb);
1251 SKB_LINEAR_ASSERT(skb);
1252 skb->tail += len;
1253 skb->len += len;
1254 if (unlikely(skb->tail > skb->end))
1255 skb_over_panic(skb, len, __builtin_return_address(0));
1256 return tmp;
1258 EXPORT_SYMBOL(skb_put);
1261 * skb_push - add data to the start of a buffer
1262 * @skb: buffer to use
1263 * @len: amount of data to add
1265 * This function extends the used data area of the buffer at the buffer
1266 * start. If this would exceed the total buffer headroom the kernel will
1267 * panic. A pointer to the first byte of the extra data is returned.
1269 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1271 skb->data -= len;
1272 skb->len += len;
1273 if (unlikely(skb->data<skb->head))
1274 skb_under_panic(skb, len, __builtin_return_address(0));
1275 return skb->data;
1277 EXPORT_SYMBOL(skb_push);
1280 * skb_pull - remove data from the start of a buffer
1281 * @skb: buffer to use
1282 * @len: amount of data to remove
1284 * This function removes data from the start of a buffer, returning
1285 * the memory to the headroom. A pointer to the next data in the buffer
1286 * is returned. Once the data has been pulled future pushes will overwrite
1287 * the old data.
1289 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1291 return skb_pull_inline(skb, len);
1293 EXPORT_SYMBOL(skb_pull);
1296 * skb_trim - remove end from a buffer
1297 * @skb: buffer to alter
1298 * @len: new length
1300 * Cut the length of a buffer down by removing data from the tail. If
1301 * the buffer is already under the length specified it is not modified.
1302 * The skb must be linear.
1304 void skb_trim(struct sk_buff *skb, unsigned int len)
1306 if (skb->len > len)
1307 __skb_trim(skb, len);
1309 EXPORT_SYMBOL(skb_trim);
1311 /* Trims skb to length len. It can change skb pointers.
1314 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1316 struct sk_buff **fragp;
1317 struct sk_buff *frag;
1318 int offset = skb_headlen(skb);
1319 int nfrags = skb_shinfo(skb)->nr_frags;
1320 int i;
1321 int err;
1323 if (skb_cloned(skb) &&
1324 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1325 return err;
1327 i = 0;
1328 if (offset >= len)
1329 goto drop_pages;
1331 for (; i < nfrags; i++) {
1332 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1334 if (end < len) {
1335 offset = end;
1336 continue;
1339 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1341 drop_pages:
1342 skb_shinfo(skb)->nr_frags = i;
1344 for (; i < nfrags; i++)
1345 skb_frag_unref(skb, i);
1347 if (skb_has_frag_list(skb))
1348 skb_drop_fraglist(skb);
1349 goto done;
1352 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1353 fragp = &frag->next) {
1354 int end = offset + frag->len;
1356 if (skb_shared(frag)) {
1357 struct sk_buff *nfrag;
1359 nfrag = skb_clone(frag, GFP_ATOMIC);
1360 if (unlikely(!nfrag))
1361 return -ENOMEM;
1363 nfrag->next = frag->next;
1364 consume_skb(frag);
1365 frag = nfrag;
1366 *fragp = frag;
1369 if (end < len) {
1370 offset = end;
1371 continue;
1374 if (end > len &&
1375 unlikely((err = pskb_trim(frag, len - offset))))
1376 return err;
1378 if (frag->next)
1379 skb_drop_list(&frag->next);
1380 break;
1383 done:
1384 if (len > skb_headlen(skb)) {
1385 skb->data_len -= skb->len - len;
1386 skb->len = len;
1387 } else {
1388 skb->len = len;
1389 skb->data_len = 0;
1390 skb_set_tail_pointer(skb, len);
1393 return 0;
1395 EXPORT_SYMBOL(___pskb_trim);
1398 * __pskb_pull_tail - advance tail of skb header
1399 * @skb: buffer to reallocate
1400 * @delta: number of bytes to advance tail
1402 * The function makes a sense only on a fragmented &sk_buff,
1403 * it expands header moving its tail forward and copying necessary
1404 * data from fragmented part.
1406 * &sk_buff MUST have reference count of 1.
1408 * Returns %NULL (and &sk_buff does not change) if pull failed
1409 * or value of new tail of skb in the case of success.
1411 * All the pointers pointing into skb header may change and must be
1412 * reloaded after call to this function.
1415 /* Moves tail of skb head forward, copying data from fragmented part,
1416 * when it is necessary.
1417 * 1. It may fail due to malloc failure.
1418 * 2. It may change skb pointers.
1420 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1422 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1424 /* If skb has not enough free space at tail, get new one
1425 * plus 128 bytes for future expansions. If we have enough
1426 * room at tail, reallocate without expansion only if skb is cloned.
1428 int i, k, eat = (skb->tail + delta) - skb->end;
1430 if (eat > 0 || skb_cloned(skb)) {
1431 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1432 GFP_ATOMIC))
1433 return NULL;
1436 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1437 BUG();
1439 /* Optimization: no fragments, no reasons to preestimate
1440 * size of pulled pages. Superb.
1442 if (!skb_has_frag_list(skb))
1443 goto pull_pages;
1445 /* Estimate size of pulled pages. */
1446 eat = delta;
1447 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1448 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1450 if (size >= eat)
1451 goto pull_pages;
1452 eat -= size;
1455 /* If we need update frag list, we are in troubles.
1456 * Certainly, it possible to add an offset to skb data,
1457 * but taking into account that pulling is expected to
1458 * be very rare operation, it is worth to fight against
1459 * further bloating skb head and crucify ourselves here instead.
1460 * Pure masohism, indeed. 8)8)
1462 if (eat) {
1463 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1464 struct sk_buff *clone = NULL;
1465 struct sk_buff *insp = NULL;
1467 do {
1468 BUG_ON(!list);
1470 if (list->len <= eat) {
1471 /* Eaten as whole. */
1472 eat -= list->len;
1473 list = list->next;
1474 insp = list;
1475 } else {
1476 /* Eaten partially. */
1478 if (skb_shared(list)) {
1479 /* Sucks! We need to fork list. :-( */
1480 clone = skb_clone(list, GFP_ATOMIC);
1481 if (!clone)
1482 return NULL;
1483 insp = list->next;
1484 list = clone;
1485 } else {
1486 /* This may be pulled without
1487 * problems. */
1488 insp = list;
1490 if (!pskb_pull(list, eat)) {
1491 kfree_skb(clone);
1492 return NULL;
1494 break;
1496 } while (eat);
1498 /* Free pulled out fragments. */
1499 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1500 skb_shinfo(skb)->frag_list = list->next;
1501 kfree_skb(list);
1503 /* And insert new clone at head. */
1504 if (clone) {
1505 clone->next = list;
1506 skb_shinfo(skb)->frag_list = clone;
1509 /* Success! Now we may commit changes to skb data. */
1511 pull_pages:
1512 eat = delta;
1513 k = 0;
1514 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1515 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1517 if (size <= eat) {
1518 skb_frag_unref(skb, i);
1519 eat -= size;
1520 } else {
1521 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1522 if (eat) {
1523 skb_shinfo(skb)->frags[k].page_offset += eat;
1524 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1525 eat = 0;
1527 k++;
1530 skb_shinfo(skb)->nr_frags = k;
1532 skb->tail += delta;
1533 skb->data_len -= delta;
1535 return skb_tail_pointer(skb);
1537 EXPORT_SYMBOL(__pskb_pull_tail);
1540 * skb_copy_bits - copy bits from skb to kernel buffer
1541 * @skb: source skb
1542 * @offset: offset in source
1543 * @to: destination buffer
1544 * @len: number of bytes to copy
1546 * Copy the specified number of bytes from the source skb to the
1547 * destination buffer.
1549 * CAUTION ! :
1550 * If its prototype is ever changed,
1551 * check arch/{*}/net/{*}.S files,
1552 * since it is called from BPF assembly code.
1554 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1556 int start = skb_headlen(skb);
1557 struct sk_buff *frag_iter;
1558 int i, copy;
1560 if (offset > (int)skb->len - len)
1561 goto fault;
1563 /* Copy header. */
1564 if ((copy = start - offset) > 0) {
1565 if (copy > len)
1566 copy = len;
1567 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1568 if ((len -= copy) == 0)
1569 return 0;
1570 offset += copy;
1571 to += copy;
1574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1575 int end;
1576 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1578 WARN_ON(start > offset + len);
1580 end = start + skb_frag_size(f);
1581 if ((copy = end - offset) > 0) {
1582 u8 *vaddr;
1584 if (copy > len)
1585 copy = len;
1587 vaddr = kmap_atomic(skb_frag_page(f));
1588 memcpy(to,
1589 vaddr + f->page_offset + offset - start,
1590 copy);
1591 kunmap_atomic(vaddr);
1593 if ((len -= copy) == 0)
1594 return 0;
1595 offset += copy;
1596 to += copy;
1598 start = end;
1601 skb_walk_frags(skb, frag_iter) {
1602 int end;
1604 WARN_ON(start > offset + len);
1606 end = start + frag_iter->len;
1607 if ((copy = end - offset) > 0) {
1608 if (copy > len)
1609 copy = len;
1610 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1611 goto fault;
1612 if ((len -= copy) == 0)
1613 return 0;
1614 offset += copy;
1615 to += copy;
1617 start = end;
1620 if (!len)
1621 return 0;
1623 fault:
1624 return -EFAULT;
1626 EXPORT_SYMBOL(skb_copy_bits);
1629 * Callback from splice_to_pipe(), if we need to release some pages
1630 * at the end of the spd in case we error'ed out in filling the pipe.
1632 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1634 put_page(spd->pages[i]);
1637 static struct page *linear_to_page(struct page *page, unsigned int *len,
1638 unsigned int *offset,
1639 struct sock *sk)
1641 struct page_frag *pfrag = sk_page_frag(sk);
1643 if (!sk_page_frag_refill(sk, pfrag))
1644 return NULL;
1646 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1648 memcpy(page_address(pfrag->page) + pfrag->offset,
1649 page_address(page) + *offset, *len);
1650 *offset = pfrag->offset;
1651 pfrag->offset += *len;
1653 return pfrag->page;
1656 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1657 struct page *page,
1658 unsigned int offset)
1660 return spd->nr_pages &&
1661 spd->pages[spd->nr_pages - 1] == page &&
1662 (spd->partial[spd->nr_pages - 1].offset +
1663 spd->partial[spd->nr_pages - 1].len == offset);
1667 * Fill page/offset/length into spd, if it can hold more pages.
1669 static bool spd_fill_page(struct splice_pipe_desc *spd,
1670 struct pipe_inode_info *pipe, struct page *page,
1671 unsigned int *len, unsigned int offset,
1672 bool linear,
1673 struct sock *sk)
1675 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1676 return true;
1678 if (linear) {
1679 page = linear_to_page(page, len, &offset, sk);
1680 if (!page)
1681 return true;
1683 if (spd_can_coalesce(spd, page, offset)) {
1684 spd->partial[spd->nr_pages - 1].len += *len;
1685 return false;
1687 get_page(page);
1688 spd->pages[spd->nr_pages] = page;
1689 spd->partial[spd->nr_pages].len = *len;
1690 spd->partial[spd->nr_pages].offset = offset;
1691 spd->nr_pages++;
1693 return false;
1696 static bool __splice_segment(struct page *page, unsigned int poff,
1697 unsigned int plen, unsigned int *off,
1698 unsigned int *len,
1699 struct splice_pipe_desc *spd, bool linear,
1700 struct sock *sk,
1701 struct pipe_inode_info *pipe)
1703 if (!*len)
1704 return true;
1706 /* skip this segment if already processed */
1707 if (*off >= plen) {
1708 *off -= plen;
1709 return false;
1712 /* ignore any bits we already processed */
1713 poff += *off;
1714 plen -= *off;
1715 *off = 0;
1717 do {
1718 unsigned int flen = min(*len, plen);
1720 if (spd_fill_page(spd, pipe, page, &flen, poff,
1721 linear, sk))
1722 return true;
1723 poff += flen;
1724 plen -= flen;
1725 *len -= flen;
1726 } while (*len && plen);
1728 return false;
1732 * Map linear and fragment data from the skb to spd. It reports true if the
1733 * pipe is full or if we already spliced the requested length.
1735 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1736 unsigned int *offset, unsigned int *len,
1737 struct splice_pipe_desc *spd, struct sock *sk)
1739 int seg;
1741 /* map the linear part :
1742 * If skb->head_frag is set, this 'linear' part is backed by a
1743 * fragment, and if the head is not shared with any clones then
1744 * we can avoid a copy since we own the head portion of this page.
1746 if (__splice_segment(virt_to_page(skb->data),
1747 (unsigned long) skb->data & (PAGE_SIZE - 1),
1748 skb_headlen(skb),
1749 offset, len, spd,
1750 skb_head_is_locked(skb),
1751 sk, pipe))
1752 return true;
1755 * then map the fragments
1757 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1758 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1760 if (__splice_segment(skb_frag_page(f),
1761 f->page_offset, skb_frag_size(f),
1762 offset, len, spd, false, sk, pipe))
1763 return true;
1766 return false;
1770 * Map data from the skb to a pipe. Should handle both the linear part,
1771 * the fragments, and the frag list. It does NOT handle frag lists within
1772 * the frag list, if such a thing exists. We'd probably need to recurse to
1773 * handle that cleanly.
1775 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1776 struct pipe_inode_info *pipe, unsigned int tlen,
1777 unsigned int flags)
1779 struct partial_page partial[MAX_SKB_FRAGS];
1780 struct page *pages[MAX_SKB_FRAGS];
1781 struct splice_pipe_desc spd = {
1782 .pages = pages,
1783 .partial = partial,
1784 .nr_pages_max = MAX_SKB_FRAGS,
1785 .flags = flags,
1786 .ops = &nosteal_pipe_buf_ops,
1787 .spd_release = sock_spd_release,
1789 struct sk_buff *frag_iter;
1790 struct sock *sk = skb->sk;
1791 int ret = 0;
1794 * __skb_splice_bits() only fails if the output has no room left,
1795 * so no point in going over the frag_list for the error case.
1797 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1798 goto done;
1799 else if (!tlen)
1800 goto done;
1803 * now see if we have a frag_list to map
1805 skb_walk_frags(skb, frag_iter) {
1806 if (!tlen)
1807 break;
1808 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1809 break;
1812 done:
1813 if (spd.nr_pages) {
1815 * Drop the socket lock, otherwise we have reverse
1816 * locking dependencies between sk_lock and i_mutex
1817 * here as compared to sendfile(). We enter here
1818 * with the socket lock held, and splice_to_pipe() will
1819 * grab the pipe inode lock. For sendfile() emulation,
1820 * we call into ->sendpage() with the i_mutex lock held
1821 * and networking will grab the socket lock.
1823 release_sock(sk);
1824 ret = splice_to_pipe(pipe, &spd);
1825 lock_sock(sk);
1828 return ret;
1832 * skb_store_bits - store bits from kernel buffer to skb
1833 * @skb: destination buffer
1834 * @offset: offset in destination
1835 * @from: source buffer
1836 * @len: number of bytes to copy
1838 * Copy the specified number of bytes from the source buffer to the
1839 * destination skb. This function handles all the messy bits of
1840 * traversing fragment lists and such.
1843 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1845 int start = skb_headlen(skb);
1846 struct sk_buff *frag_iter;
1847 int i, copy;
1849 if (offset > (int)skb->len - len)
1850 goto fault;
1852 if ((copy = start - offset) > 0) {
1853 if (copy > len)
1854 copy = len;
1855 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1856 if ((len -= copy) == 0)
1857 return 0;
1858 offset += copy;
1859 from += copy;
1862 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1863 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1864 int end;
1866 WARN_ON(start > offset + len);
1868 end = start + skb_frag_size(frag);
1869 if ((copy = end - offset) > 0) {
1870 u8 *vaddr;
1872 if (copy > len)
1873 copy = len;
1875 vaddr = kmap_atomic(skb_frag_page(frag));
1876 memcpy(vaddr + frag->page_offset + offset - start,
1877 from, copy);
1878 kunmap_atomic(vaddr);
1880 if ((len -= copy) == 0)
1881 return 0;
1882 offset += copy;
1883 from += copy;
1885 start = end;
1888 skb_walk_frags(skb, frag_iter) {
1889 int end;
1891 WARN_ON(start > offset + len);
1893 end = start + frag_iter->len;
1894 if ((copy = end - offset) > 0) {
1895 if (copy > len)
1896 copy = len;
1897 if (skb_store_bits(frag_iter, offset - start,
1898 from, copy))
1899 goto fault;
1900 if ((len -= copy) == 0)
1901 return 0;
1902 offset += copy;
1903 from += copy;
1905 start = end;
1907 if (!len)
1908 return 0;
1910 fault:
1911 return -EFAULT;
1913 EXPORT_SYMBOL(skb_store_bits);
1915 /* Checksum skb data. */
1917 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1918 int len, __wsum csum)
1920 int start = skb_headlen(skb);
1921 int i, copy = start - offset;
1922 struct sk_buff *frag_iter;
1923 int pos = 0;
1925 /* Checksum header. */
1926 if (copy > 0) {
1927 if (copy > len)
1928 copy = len;
1929 csum = csum_partial(skb->data + offset, copy, csum);
1930 if ((len -= copy) == 0)
1931 return csum;
1932 offset += copy;
1933 pos = copy;
1936 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1937 int end;
1938 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1940 WARN_ON(start > offset + len);
1942 end = start + skb_frag_size(frag);
1943 if ((copy = end - offset) > 0) {
1944 __wsum csum2;
1945 u8 *vaddr;
1947 if (copy > len)
1948 copy = len;
1949 vaddr = kmap_atomic(skb_frag_page(frag));
1950 csum2 = csum_partial(vaddr + frag->page_offset +
1951 offset - start, copy, 0);
1952 kunmap_atomic(vaddr);
1953 csum = csum_block_add(csum, csum2, pos);
1954 if (!(len -= copy))
1955 return csum;
1956 offset += copy;
1957 pos += copy;
1959 start = end;
1962 skb_walk_frags(skb, frag_iter) {
1963 int end;
1965 WARN_ON(start > offset + len);
1967 end = start + frag_iter->len;
1968 if ((copy = end - offset) > 0) {
1969 __wsum csum2;
1970 if (copy > len)
1971 copy = len;
1972 csum2 = skb_checksum(frag_iter, offset - start,
1973 copy, 0);
1974 csum = csum_block_add(csum, csum2, pos);
1975 if ((len -= copy) == 0)
1976 return csum;
1977 offset += copy;
1978 pos += copy;
1980 start = end;
1982 BUG_ON(len);
1984 return csum;
1986 EXPORT_SYMBOL(skb_checksum);
1988 /* Both of above in one bottle. */
1990 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1991 u8 *to, int len, __wsum csum)
1993 int start = skb_headlen(skb);
1994 int i, copy = start - offset;
1995 struct sk_buff *frag_iter;
1996 int pos = 0;
1998 /* Copy header. */
1999 if (copy > 0) {
2000 if (copy > len)
2001 copy = len;
2002 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2003 copy, csum);
2004 if ((len -= copy) == 0)
2005 return csum;
2006 offset += copy;
2007 to += copy;
2008 pos = copy;
2011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2012 int end;
2014 WARN_ON(start > offset + len);
2016 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2017 if ((copy = end - offset) > 0) {
2018 __wsum csum2;
2019 u8 *vaddr;
2020 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2022 if (copy > len)
2023 copy = len;
2024 vaddr = kmap_atomic(skb_frag_page(frag));
2025 csum2 = csum_partial_copy_nocheck(vaddr +
2026 frag->page_offset +
2027 offset - start, to,
2028 copy, 0);
2029 kunmap_atomic(vaddr);
2030 csum = csum_block_add(csum, csum2, pos);
2031 if (!(len -= copy))
2032 return csum;
2033 offset += copy;
2034 to += copy;
2035 pos += copy;
2037 start = end;
2040 skb_walk_frags(skb, frag_iter) {
2041 __wsum csum2;
2042 int end;
2044 WARN_ON(start > offset + len);
2046 end = start + frag_iter->len;
2047 if ((copy = end - offset) > 0) {
2048 if (copy > len)
2049 copy = len;
2050 csum2 = skb_copy_and_csum_bits(frag_iter,
2051 offset - start,
2052 to, copy, 0);
2053 csum = csum_block_add(csum, csum2, pos);
2054 if ((len -= copy) == 0)
2055 return csum;
2056 offset += copy;
2057 to += copy;
2058 pos += copy;
2060 start = end;
2062 BUG_ON(len);
2063 return csum;
2065 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2067 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2069 __wsum csum;
2070 long csstart;
2072 if (skb->ip_summed == CHECKSUM_PARTIAL)
2073 csstart = skb_checksum_start_offset(skb);
2074 else
2075 csstart = skb_headlen(skb);
2077 BUG_ON(csstart > skb_headlen(skb));
2079 skb_copy_from_linear_data(skb, to, csstart);
2081 csum = 0;
2082 if (csstart != skb->len)
2083 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2084 skb->len - csstart, 0);
2086 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2087 long csstuff = csstart + skb->csum_offset;
2089 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2092 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2095 * skb_dequeue - remove from the head of the queue
2096 * @list: list to dequeue from
2098 * Remove the head of the list. The list lock is taken so the function
2099 * may be used safely with other locking list functions. The head item is
2100 * returned or %NULL if the list is empty.
2103 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2105 unsigned long flags;
2106 struct sk_buff *result;
2108 spin_lock_irqsave(&list->lock, flags);
2109 result = __skb_dequeue(list);
2110 spin_unlock_irqrestore(&list->lock, flags);
2111 return result;
2113 EXPORT_SYMBOL(skb_dequeue);
2116 * skb_dequeue_tail - remove from the tail of the queue
2117 * @list: list to dequeue from
2119 * Remove the tail of the list. The list lock is taken so the function
2120 * may be used safely with other locking list functions. The tail item is
2121 * returned or %NULL if the list is empty.
2123 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2125 unsigned long flags;
2126 struct sk_buff *result;
2128 spin_lock_irqsave(&list->lock, flags);
2129 result = __skb_dequeue_tail(list);
2130 spin_unlock_irqrestore(&list->lock, flags);
2131 return result;
2133 EXPORT_SYMBOL(skb_dequeue_tail);
2136 * skb_queue_purge - empty a list
2137 * @list: list to empty
2139 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2140 * the list and one reference dropped. This function takes the list
2141 * lock and is atomic with respect to other list locking functions.
2143 void skb_queue_purge(struct sk_buff_head *list)
2145 struct sk_buff *skb;
2146 while ((skb = skb_dequeue(list)) != NULL)
2147 kfree_skb(skb);
2149 EXPORT_SYMBOL(skb_queue_purge);
2152 * skb_queue_head - queue a buffer at the list head
2153 * @list: list to use
2154 * @newsk: buffer to queue
2156 * Queue a buffer at the start of the list. This function takes the
2157 * list lock and can be used safely with other locking &sk_buff functions
2158 * safely.
2160 * A buffer cannot be placed on two lists at the same time.
2162 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2164 unsigned long flags;
2166 spin_lock_irqsave(&list->lock, flags);
2167 __skb_queue_head(list, newsk);
2168 spin_unlock_irqrestore(&list->lock, flags);
2170 EXPORT_SYMBOL(skb_queue_head);
2173 * skb_queue_tail - queue a buffer at the list tail
2174 * @list: list to use
2175 * @newsk: buffer to queue
2177 * Queue a buffer at the tail of the list. This function takes the
2178 * list lock and can be used safely with other locking &sk_buff functions
2179 * safely.
2181 * A buffer cannot be placed on two lists at the same time.
2183 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2185 unsigned long flags;
2187 spin_lock_irqsave(&list->lock, flags);
2188 __skb_queue_tail(list, newsk);
2189 spin_unlock_irqrestore(&list->lock, flags);
2191 EXPORT_SYMBOL(skb_queue_tail);
2194 * skb_unlink - remove a buffer from a list
2195 * @skb: buffer to remove
2196 * @list: list to use
2198 * Remove a packet from a list. The list locks are taken and this
2199 * function is atomic with respect to other list locked calls
2201 * You must know what list the SKB is on.
2203 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2205 unsigned long flags;
2207 spin_lock_irqsave(&list->lock, flags);
2208 __skb_unlink(skb, list);
2209 spin_unlock_irqrestore(&list->lock, flags);
2211 EXPORT_SYMBOL(skb_unlink);
2214 * skb_append - append a buffer
2215 * @old: buffer to insert after
2216 * @newsk: buffer to insert
2217 * @list: list to use
2219 * Place a packet after a given packet in a list. The list locks are taken
2220 * and this function is atomic with respect to other list locked calls.
2221 * A buffer cannot be placed on two lists at the same time.
2223 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2225 unsigned long flags;
2227 spin_lock_irqsave(&list->lock, flags);
2228 __skb_queue_after(list, old, newsk);
2229 spin_unlock_irqrestore(&list->lock, flags);
2231 EXPORT_SYMBOL(skb_append);
2234 * skb_insert - insert a buffer
2235 * @old: buffer to insert before
2236 * @newsk: buffer to insert
2237 * @list: list to use
2239 * Place a packet before a given packet in a list. The list locks are
2240 * taken and this function is atomic with respect to other list locked
2241 * calls.
2243 * A buffer cannot be placed on two lists at the same time.
2245 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2247 unsigned long flags;
2249 spin_lock_irqsave(&list->lock, flags);
2250 __skb_insert(newsk, old->prev, old, list);
2251 spin_unlock_irqrestore(&list->lock, flags);
2253 EXPORT_SYMBOL(skb_insert);
2255 static inline void skb_split_inside_header(struct sk_buff *skb,
2256 struct sk_buff* skb1,
2257 const u32 len, const int pos)
2259 int i;
2261 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2262 pos - len);
2263 /* And move data appendix as is. */
2264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2265 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2267 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2268 skb_shinfo(skb)->nr_frags = 0;
2269 skb1->data_len = skb->data_len;
2270 skb1->len += skb1->data_len;
2271 skb->data_len = 0;
2272 skb->len = len;
2273 skb_set_tail_pointer(skb, len);
2276 static inline void skb_split_no_header(struct sk_buff *skb,
2277 struct sk_buff* skb1,
2278 const u32 len, int pos)
2280 int i, k = 0;
2281 const int nfrags = skb_shinfo(skb)->nr_frags;
2283 skb_shinfo(skb)->nr_frags = 0;
2284 skb1->len = skb1->data_len = skb->len - len;
2285 skb->len = len;
2286 skb->data_len = len - pos;
2288 for (i = 0; i < nfrags; i++) {
2289 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2291 if (pos + size > len) {
2292 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2294 if (pos < len) {
2295 /* Split frag.
2296 * We have two variants in this case:
2297 * 1. Move all the frag to the second
2298 * part, if it is possible. F.e.
2299 * this approach is mandatory for TUX,
2300 * where splitting is expensive.
2301 * 2. Split is accurately. We make this.
2303 skb_frag_ref(skb, i);
2304 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2305 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2306 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2307 skb_shinfo(skb)->nr_frags++;
2309 k++;
2310 } else
2311 skb_shinfo(skb)->nr_frags++;
2312 pos += size;
2314 skb_shinfo(skb1)->nr_frags = k;
2318 * skb_split - Split fragmented skb to two parts at length len.
2319 * @skb: the buffer to split
2320 * @skb1: the buffer to receive the second part
2321 * @len: new length for skb
2323 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2325 int pos = skb_headlen(skb);
2327 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2328 if (len < pos) /* Split line is inside header. */
2329 skb_split_inside_header(skb, skb1, len, pos);
2330 else /* Second chunk has no header, nothing to copy. */
2331 skb_split_no_header(skb, skb1, len, pos);
2333 EXPORT_SYMBOL(skb_split);
2335 /* Shifting from/to a cloned skb is a no-go.
2337 * Caller cannot keep skb_shinfo related pointers past calling here!
2339 static int skb_prepare_for_shift(struct sk_buff *skb)
2341 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2345 * skb_shift - Shifts paged data partially from skb to another
2346 * @tgt: buffer into which tail data gets added
2347 * @skb: buffer from which the paged data comes from
2348 * @shiftlen: shift up to this many bytes
2350 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2351 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2352 * It's up to caller to free skb if everything was shifted.
2354 * If @tgt runs out of frags, the whole operation is aborted.
2356 * Skb cannot include anything else but paged data while tgt is allowed
2357 * to have non-paged data as well.
2359 * TODO: full sized shift could be optimized but that would need
2360 * specialized skb free'er to handle frags without up-to-date nr_frags.
2362 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2364 int from, to, merge, todo;
2365 struct skb_frag_struct *fragfrom, *fragto;
2367 BUG_ON(shiftlen > skb->len);
2368 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2370 todo = shiftlen;
2371 from = 0;
2372 to = skb_shinfo(tgt)->nr_frags;
2373 fragfrom = &skb_shinfo(skb)->frags[from];
2375 /* Actual merge is delayed until the point when we know we can
2376 * commit all, so that we don't have to undo partial changes
2378 if (!to ||
2379 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2380 fragfrom->page_offset)) {
2381 merge = -1;
2382 } else {
2383 merge = to - 1;
2385 todo -= skb_frag_size(fragfrom);
2386 if (todo < 0) {
2387 if (skb_prepare_for_shift(skb) ||
2388 skb_prepare_for_shift(tgt))
2389 return 0;
2391 /* All previous frag pointers might be stale! */
2392 fragfrom = &skb_shinfo(skb)->frags[from];
2393 fragto = &skb_shinfo(tgt)->frags[merge];
2395 skb_frag_size_add(fragto, shiftlen);
2396 skb_frag_size_sub(fragfrom, shiftlen);
2397 fragfrom->page_offset += shiftlen;
2399 goto onlymerged;
2402 from++;
2405 /* Skip full, not-fitting skb to avoid expensive operations */
2406 if ((shiftlen == skb->len) &&
2407 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2408 return 0;
2410 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2411 return 0;
2413 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2414 if (to == MAX_SKB_FRAGS)
2415 return 0;
2417 fragfrom = &skb_shinfo(skb)->frags[from];
2418 fragto = &skb_shinfo(tgt)->frags[to];
2420 if (todo >= skb_frag_size(fragfrom)) {
2421 *fragto = *fragfrom;
2422 todo -= skb_frag_size(fragfrom);
2423 from++;
2424 to++;
2426 } else {
2427 __skb_frag_ref(fragfrom);
2428 fragto->page = fragfrom->page;
2429 fragto->page_offset = fragfrom->page_offset;
2430 skb_frag_size_set(fragto, todo);
2432 fragfrom->page_offset += todo;
2433 skb_frag_size_sub(fragfrom, todo);
2434 todo = 0;
2436 to++;
2437 break;
2441 /* Ready to "commit" this state change to tgt */
2442 skb_shinfo(tgt)->nr_frags = to;
2444 if (merge >= 0) {
2445 fragfrom = &skb_shinfo(skb)->frags[0];
2446 fragto = &skb_shinfo(tgt)->frags[merge];
2448 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2449 __skb_frag_unref(fragfrom);
2452 /* Reposition in the original skb */
2453 to = 0;
2454 while (from < skb_shinfo(skb)->nr_frags)
2455 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2456 skb_shinfo(skb)->nr_frags = to;
2458 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2460 onlymerged:
2461 /* Most likely the tgt won't ever need its checksum anymore, skb on
2462 * the other hand might need it if it needs to be resent
2464 tgt->ip_summed = CHECKSUM_PARTIAL;
2465 skb->ip_summed = CHECKSUM_PARTIAL;
2467 /* Yak, is it really working this way? Some helper please? */
2468 skb->len -= shiftlen;
2469 skb->data_len -= shiftlen;
2470 skb->truesize -= shiftlen;
2471 tgt->len += shiftlen;
2472 tgt->data_len += shiftlen;
2473 tgt->truesize += shiftlen;
2475 return shiftlen;
2479 * skb_prepare_seq_read - Prepare a sequential read of skb data
2480 * @skb: the buffer to read
2481 * @from: lower offset of data to be read
2482 * @to: upper offset of data to be read
2483 * @st: state variable
2485 * Initializes the specified state variable. Must be called before
2486 * invoking skb_seq_read() for the first time.
2488 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2489 unsigned int to, struct skb_seq_state *st)
2491 st->lower_offset = from;
2492 st->upper_offset = to;
2493 st->root_skb = st->cur_skb = skb;
2494 st->frag_idx = st->stepped_offset = 0;
2495 st->frag_data = NULL;
2497 EXPORT_SYMBOL(skb_prepare_seq_read);
2500 * skb_seq_read - Sequentially read skb data
2501 * @consumed: number of bytes consumed by the caller so far
2502 * @data: destination pointer for data to be returned
2503 * @st: state variable
2505 * Reads a block of skb data at &consumed relative to the
2506 * lower offset specified to skb_prepare_seq_read(). Assigns
2507 * the head of the data block to &data and returns the length
2508 * of the block or 0 if the end of the skb data or the upper
2509 * offset has been reached.
2511 * The caller is not required to consume all of the data
2512 * returned, i.e. &consumed is typically set to the number
2513 * of bytes already consumed and the next call to
2514 * skb_seq_read() will return the remaining part of the block.
2516 * Note 1: The size of each block of data returned can be arbitrary,
2517 * this limitation is the cost for zerocopy seqeuental
2518 * reads of potentially non linear data.
2520 * Note 2: Fragment lists within fragments are not implemented
2521 * at the moment, state->root_skb could be replaced with
2522 * a stack for this purpose.
2524 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2525 struct skb_seq_state *st)
2527 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2528 skb_frag_t *frag;
2530 if (unlikely(abs_offset >= st->upper_offset))
2531 return 0;
2533 next_skb:
2534 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2536 if (abs_offset < block_limit && !st->frag_data) {
2537 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2538 return block_limit - abs_offset;
2541 if (st->frag_idx == 0 && !st->frag_data)
2542 st->stepped_offset += skb_headlen(st->cur_skb);
2544 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2545 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2546 block_limit = skb_frag_size(frag) + st->stepped_offset;
2548 if (abs_offset < block_limit) {
2549 if (!st->frag_data)
2550 st->frag_data = kmap_atomic(skb_frag_page(frag));
2552 *data = (u8 *) st->frag_data + frag->page_offset +
2553 (abs_offset - st->stepped_offset);
2555 return block_limit - abs_offset;
2558 if (st->frag_data) {
2559 kunmap_atomic(st->frag_data);
2560 st->frag_data = NULL;
2563 st->frag_idx++;
2564 st->stepped_offset += skb_frag_size(frag);
2567 if (st->frag_data) {
2568 kunmap_atomic(st->frag_data);
2569 st->frag_data = NULL;
2572 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2573 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2574 st->frag_idx = 0;
2575 goto next_skb;
2576 } else if (st->cur_skb->next) {
2577 st->cur_skb = st->cur_skb->next;
2578 st->frag_idx = 0;
2579 goto next_skb;
2582 return 0;
2584 EXPORT_SYMBOL(skb_seq_read);
2587 * skb_abort_seq_read - Abort a sequential read of skb data
2588 * @st: state variable
2590 * Must be called if skb_seq_read() was not called until it
2591 * returned 0.
2593 void skb_abort_seq_read(struct skb_seq_state *st)
2595 if (st->frag_data)
2596 kunmap_atomic(st->frag_data);
2598 EXPORT_SYMBOL(skb_abort_seq_read);
2600 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2602 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2603 struct ts_config *conf,
2604 struct ts_state *state)
2606 return skb_seq_read(offset, text, TS_SKB_CB(state));
2609 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2611 skb_abort_seq_read(TS_SKB_CB(state));
2615 * skb_find_text - Find a text pattern in skb data
2616 * @skb: the buffer to look in
2617 * @from: search offset
2618 * @to: search limit
2619 * @config: textsearch configuration
2620 * @state: uninitialized textsearch state variable
2622 * Finds a pattern in the skb data according to the specified
2623 * textsearch configuration. Use textsearch_next() to retrieve
2624 * subsequent occurrences of the pattern. Returns the offset
2625 * to the first occurrence or UINT_MAX if no match was found.
2627 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2628 unsigned int to, struct ts_config *config,
2629 struct ts_state *state)
2631 unsigned int ret;
2633 config->get_next_block = skb_ts_get_next_block;
2634 config->finish = skb_ts_finish;
2636 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2638 ret = textsearch_find(config, state);
2639 return (ret <= to - from ? ret : UINT_MAX);
2641 EXPORT_SYMBOL(skb_find_text);
2644 * skb_append_datato_frags - append the user data to a skb
2645 * @sk: sock structure
2646 * @skb: skb structure to be appened with user data.
2647 * @getfrag: call back function to be used for getting the user data
2648 * @from: pointer to user message iov
2649 * @length: length of the iov message
2651 * Description: This procedure append the user data in the fragment part
2652 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2654 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2655 int (*getfrag)(void *from, char *to, int offset,
2656 int len, int odd, struct sk_buff *skb),
2657 void *from, int length)
2659 int frg_cnt = skb_shinfo(skb)->nr_frags;
2660 int copy;
2661 int offset = 0;
2662 int ret;
2663 struct page_frag *pfrag = &current->task_frag;
2665 do {
2666 /* Return error if we don't have space for new frag */
2667 if (frg_cnt >= MAX_SKB_FRAGS)
2668 return -EMSGSIZE;
2670 if (!sk_page_frag_refill(sk, pfrag))
2671 return -ENOMEM;
2673 /* copy the user data to page */
2674 copy = min_t(int, length, pfrag->size - pfrag->offset);
2676 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2677 offset, copy, 0, skb);
2678 if (ret < 0)
2679 return -EFAULT;
2681 /* copy was successful so update the size parameters */
2682 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2683 copy);
2684 frg_cnt++;
2685 pfrag->offset += copy;
2686 get_page(pfrag->page);
2688 skb->truesize += copy;
2689 atomic_add(copy, &sk->sk_wmem_alloc);
2690 skb->len += copy;
2691 skb->data_len += copy;
2692 offset += copy;
2693 length -= copy;
2695 } while (length > 0);
2697 return 0;
2699 EXPORT_SYMBOL(skb_append_datato_frags);
2702 * skb_pull_rcsum - pull skb and update receive checksum
2703 * @skb: buffer to update
2704 * @len: length of data pulled
2706 * This function performs an skb_pull on the packet and updates
2707 * the CHECKSUM_COMPLETE checksum. It should be used on
2708 * receive path processing instead of skb_pull unless you know
2709 * that the checksum difference is zero (e.g., a valid IP header)
2710 * or you are setting ip_summed to CHECKSUM_NONE.
2712 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2714 unsigned char *data = skb->data;
2716 BUG_ON(len > skb->len);
2717 __skb_pull(skb, len);
2718 skb_postpull_rcsum(skb, data, len);
2719 return skb->data;
2721 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2724 * skb_segment - Perform protocol segmentation on skb.
2725 * @skb: buffer to segment
2726 * @features: features for the output path (see dev->features)
2728 * This function performs segmentation on the given skb. It returns
2729 * a pointer to the first in a list of new skbs for the segments.
2730 * In case of error it returns ERR_PTR(err).
2732 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2734 struct sk_buff *segs = NULL;
2735 struct sk_buff *tail = NULL;
2736 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2737 unsigned int mss = skb_shinfo(skb)->gso_size;
2738 unsigned int doffset = skb->data - skb_mac_header(skb);
2739 unsigned int offset = doffset;
2740 unsigned int tnl_hlen = skb_tnl_header_len(skb);
2741 unsigned int headroom;
2742 unsigned int len;
2743 __be16 proto;
2744 bool csum;
2745 int sg = !!(features & NETIF_F_SG);
2746 int nfrags = skb_shinfo(skb)->nr_frags;
2747 int err = -ENOMEM;
2748 int i = 0;
2749 int pos;
2751 proto = skb_network_protocol(skb);
2752 if (unlikely(!proto))
2753 return ERR_PTR(-EINVAL);
2755 csum = !!can_checksum_protocol(features, proto);
2756 __skb_push(skb, doffset);
2757 headroom = skb_headroom(skb);
2758 pos = skb_headlen(skb);
2760 do {
2761 struct sk_buff *nskb;
2762 skb_frag_t *frag;
2763 int hsize;
2764 int size;
2766 len = skb->len - offset;
2767 if (len > mss)
2768 len = mss;
2770 hsize = skb_headlen(skb) - offset;
2771 if (hsize < 0)
2772 hsize = 0;
2773 if (hsize > len || !sg)
2774 hsize = len;
2776 if (!hsize && i >= nfrags) {
2777 BUG_ON(fskb->len != len);
2779 pos += len;
2780 nskb = skb_clone(fskb, GFP_ATOMIC);
2781 fskb = fskb->next;
2783 if (unlikely(!nskb))
2784 goto err;
2786 hsize = skb_end_offset(nskb);
2787 if (skb_cow_head(nskb, doffset + headroom)) {
2788 kfree_skb(nskb);
2789 goto err;
2792 nskb->truesize += skb_end_offset(nskb) - hsize;
2793 skb_release_head_state(nskb);
2794 __skb_push(nskb, doffset);
2795 } else {
2796 nskb = __alloc_skb(hsize + doffset + headroom,
2797 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2798 NUMA_NO_NODE);
2800 if (unlikely(!nskb))
2801 goto err;
2803 skb_reserve(nskb, headroom);
2804 __skb_put(nskb, doffset);
2807 if (segs)
2808 tail->next = nskb;
2809 else
2810 segs = nskb;
2811 tail = nskb;
2813 __copy_skb_header(nskb, skb);
2815 /* nskb and skb might have different headroom */
2816 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2817 nskb->csum_start += skb_headroom(nskb) - headroom;
2819 skb_reset_mac_header(nskb);
2820 skb_set_network_header(nskb, skb->mac_len);
2821 nskb->transport_header = (nskb->network_header +
2822 skb_network_header_len(skb));
2823 skb_reset_mac_len(nskb);
2825 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
2826 nskb->data - tnl_hlen,
2827 doffset + tnl_hlen);
2829 if (fskb != skb_shinfo(skb)->frag_list)
2830 goto perform_csum_check;
2832 if (!sg) {
2833 nskb->ip_summed = CHECKSUM_NONE;
2834 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2835 skb_put(nskb, len),
2836 len, 0);
2837 continue;
2840 frag = skb_shinfo(nskb)->frags;
2842 skb_copy_from_linear_data_offset(skb, offset,
2843 skb_put(nskb, hsize), hsize);
2845 skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2847 while (pos < offset + len && i < nfrags) {
2848 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
2849 goto err;
2850 *frag = skb_shinfo(skb)->frags[i];
2851 __skb_frag_ref(frag);
2852 size = skb_frag_size(frag);
2854 if (pos < offset) {
2855 frag->page_offset += offset - pos;
2856 skb_frag_size_sub(frag, offset - pos);
2859 skb_shinfo(nskb)->nr_frags++;
2861 if (pos + size <= offset + len) {
2862 i++;
2863 pos += size;
2864 } else {
2865 skb_frag_size_sub(frag, pos + size - (offset + len));
2866 goto skip_fraglist;
2869 frag++;
2872 if (pos < offset + len) {
2873 struct sk_buff *fskb2 = fskb;
2875 BUG_ON(pos + fskb->len != offset + len);
2877 pos += fskb->len;
2878 fskb = fskb->next;
2880 if (fskb2->next) {
2881 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2882 if (!fskb2)
2883 goto err;
2884 } else
2885 skb_get(fskb2);
2887 SKB_FRAG_ASSERT(nskb);
2888 skb_shinfo(nskb)->frag_list = fskb2;
2891 skip_fraglist:
2892 nskb->data_len = len - hsize;
2893 nskb->len += nskb->data_len;
2894 nskb->truesize += nskb->data_len;
2896 perform_csum_check:
2897 if (!csum) {
2898 nskb->csum = skb_checksum(nskb, doffset,
2899 nskb->len - doffset, 0);
2900 nskb->ip_summed = CHECKSUM_NONE;
2902 } while ((offset += len) < skb->len);
2904 return segs;
2906 err:
2907 while ((skb = segs)) {
2908 segs = skb->next;
2909 kfree_skb(skb);
2911 return ERR_PTR(err);
2913 EXPORT_SYMBOL_GPL(skb_segment);
2915 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2917 struct sk_buff *p = *head;
2918 struct sk_buff *nskb;
2919 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2920 struct skb_shared_info *pinfo = skb_shinfo(p);
2921 unsigned int headroom;
2922 unsigned int len = skb_gro_len(skb);
2923 unsigned int offset = skb_gro_offset(skb);
2924 unsigned int headlen = skb_headlen(skb);
2925 unsigned int delta_truesize;
2927 if (p->len + len >= 65536)
2928 return -E2BIG;
2930 if (pinfo->frag_list)
2931 goto merge;
2932 else if (headlen <= offset) {
2933 skb_frag_t *frag;
2934 skb_frag_t *frag2;
2935 int i = skbinfo->nr_frags;
2936 int nr_frags = pinfo->nr_frags + i;
2938 offset -= headlen;
2940 if (nr_frags > MAX_SKB_FRAGS)
2941 return -E2BIG;
2943 pinfo->nr_frags = nr_frags;
2944 skbinfo->nr_frags = 0;
2946 frag = pinfo->frags + nr_frags;
2947 frag2 = skbinfo->frags + i;
2948 do {
2949 *--frag = *--frag2;
2950 } while (--i);
2952 frag->page_offset += offset;
2953 skb_frag_size_sub(frag, offset);
2955 /* all fragments truesize : remove (head size + sk_buff) */
2956 delta_truesize = skb->truesize -
2957 SKB_TRUESIZE(skb_end_offset(skb));
2959 skb->truesize -= skb->data_len;
2960 skb->len -= skb->data_len;
2961 skb->data_len = 0;
2963 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
2964 goto done;
2965 } else if (skb->head_frag) {
2966 int nr_frags = pinfo->nr_frags;
2967 skb_frag_t *frag = pinfo->frags + nr_frags;
2968 struct page *page = virt_to_head_page(skb->head);
2969 unsigned int first_size = headlen - offset;
2970 unsigned int first_offset;
2972 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2973 return -E2BIG;
2975 first_offset = skb->data -
2976 (unsigned char *)page_address(page) +
2977 offset;
2979 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2981 frag->page.p = page;
2982 frag->page_offset = first_offset;
2983 skb_frag_size_set(frag, first_size);
2985 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2986 /* We dont need to clear skbinfo->nr_frags here */
2988 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
2989 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2990 goto done;
2991 } else if (skb_gro_len(p) != pinfo->gso_size)
2992 return -E2BIG;
2994 headroom = skb_headroom(p);
2995 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2996 if (unlikely(!nskb))
2997 return -ENOMEM;
2999 __copy_skb_header(nskb, p);
3000 nskb->mac_len = p->mac_len;
3002 skb_reserve(nskb, headroom);
3003 __skb_put(nskb, skb_gro_offset(p));
3005 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3006 skb_set_network_header(nskb, skb_network_offset(p));
3007 skb_set_transport_header(nskb, skb_transport_offset(p));
3009 __skb_pull(p, skb_gro_offset(p));
3010 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3011 p->data - skb_mac_header(p));
3013 skb_shinfo(nskb)->frag_list = p;
3014 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3015 pinfo->gso_size = 0;
3016 skb_header_release(p);
3017 NAPI_GRO_CB(nskb)->last = p;
3019 nskb->data_len += p->len;
3020 nskb->truesize += p->truesize;
3021 nskb->len += p->len;
3023 *head = nskb;
3024 nskb->next = p->next;
3025 p->next = NULL;
3027 p = nskb;
3029 merge:
3030 delta_truesize = skb->truesize;
3031 if (offset > headlen) {
3032 unsigned int eat = offset - headlen;
3034 skbinfo->frags[0].page_offset += eat;
3035 skb_frag_size_sub(&skbinfo->frags[0], eat);
3036 skb->data_len -= eat;
3037 skb->len -= eat;
3038 offset = headlen;
3041 __skb_pull(skb, offset);
3043 NAPI_GRO_CB(p)->last->next = skb;
3044 NAPI_GRO_CB(p)->last = skb;
3045 skb_header_release(skb);
3047 done:
3048 NAPI_GRO_CB(p)->count++;
3049 p->data_len += len;
3050 p->truesize += delta_truesize;
3051 p->len += len;
3053 NAPI_GRO_CB(skb)->same_flow = 1;
3054 return 0;
3056 EXPORT_SYMBOL_GPL(skb_gro_receive);
3058 void __init skb_init(void)
3060 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3061 sizeof(struct sk_buff),
3063 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3064 NULL);
3065 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3066 (2*sizeof(struct sk_buff)) +
3067 sizeof(atomic_t),
3069 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3070 NULL);
3074 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3075 * @skb: Socket buffer containing the buffers to be mapped
3076 * @sg: The scatter-gather list to map into
3077 * @offset: The offset into the buffer's contents to start mapping
3078 * @len: Length of buffer space to be mapped
3080 * Fill the specified scatter-gather list with mappings/pointers into a
3081 * region of the buffer space attached to a socket buffer.
3083 static int
3084 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3086 int start = skb_headlen(skb);
3087 int i, copy = start - offset;
3088 struct sk_buff *frag_iter;
3089 int elt = 0;
3091 if (copy > 0) {
3092 if (copy > len)
3093 copy = len;
3094 sg_set_buf(sg, skb->data + offset, copy);
3095 elt++;
3096 if ((len -= copy) == 0)
3097 return elt;
3098 offset += copy;
3101 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3102 int end;
3104 WARN_ON(start > offset + len);
3106 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3107 if ((copy = end - offset) > 0) {
3108 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3110 if (copy > len)
3111 copy = len;
3112 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3113 frag->page_offset+offset-start);
3114 elt++;
3115 if (!(len -= copy))
3116 return elt;
3117 offset += copy;
3119 start = end;
3122 skb_walk_frags(skb, frag_iter) {
3123 int end;
3125 WARN_ON(start > offset + len);
3127 end = start + frag_iter->len;
3128 if ((copy = end - offset) > 0) {
3129 if (copy > len)
3130 copy = len;
3131 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3132 copy);
3133 if ((len -= copy) == 0)
3134 return elt;
3135 offset += copy;
3137 start = end;
3139 BUG_ON(len);
3140 return elt;
3143 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3145 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3147 sg_mark_end(&sg[nsg - 1]);
3149 return nsg;
3151 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3154 * skb_cow_data - Check that a socket buffer's data buffers are writable
3155 * @skb: The socket buffer to check.
3156 * @tailbits: Amount of trailing space to be added
3157 * @trailer: Returned pointer to the skb where the @tailbits space begins
3159 * Make sure that the data buffers attached to a socket buffer are
3160 * writable. If they are not, private copies are made of the data buffers
3161 * and the socket buffer is set to use these instead.
3163 * If @tailbits is given, make sure that there is space to write @tailbits
3164 * bytes of data beyond current end of socket buffer. @trailer will be
3165 * set to point to the skb in which this space begins.
3167 * The number of scatterlist elements required to completely map the
3168 * COW'd and extended socket buffer will be returned.
3170 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3172 int copyflag;
3173 int elt;
3174 struct sk_buff *skb1, **skb_p;
3176 /* If skb is cloned or its head is paged, reallocate
3177 * head pulling out all the pages (pages are considered not writable
3178 * at the moment even if they are anonymous).
3180 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3181 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3182 return -ENOMEM;
3184 /* Easy case. Most of packets will go this way. */
3185 if (!skb_has_frag_list(skb)) {
3186 /* A little of trouble, not enough of space for trailer.
3187 * This should not happen, when stack is tuned to generate
3188 * good frames. OK, on miss we reallocate and reserve even more
3189 * space, 128 bytes is fair. */
3191 if (skb_tailroom(skb) < tailbits &&
3192 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3193 return -ENOMEM;
3195 /* Voila! */
3196 *trailer = skb;
3197 return 1;
3200 /* Misery. We are in troubles, going to mincer fragments... */
3202 elt = 1;
3203 skb_p = &skb_shinfo(skb)->frag_list;
3204 copyflag = 0;
3206 while ((skb1 = *skb_p) != NULL) {
3207 int ntail = 0;
3209 /* The fragment is partially pulled by someone,
3210 * this can happen on input. Copy it and everything
3211 * after it. */
3213 if (skb_shared(skb1))
3214 copyflag = 1;
3216 /* If the skb is the last, worry about trailer. */
3218 if (skb1->next == NULL && tailbits) {
3219 if (skb_shinfo(skb1)->nr_frags ||
3220 skb_has_frag_list(skb1) ||
3221 skb_tailroom(skb1) < tailbits)
3222 ntail = tailbits + 128;
3225 if (copyflag ||
3226 skb_cloned(skb1) ||
3227 ntail ||
3228 skb_shinfo(skb1)->nr_frags ||
3229 skb_has_frag_list(skb1)) {
3230 struct sk_buff *skb2;
3232 /* Fuck, we are miserable poor guys... */
3233 if (ntail == 0)
3234 skb2 = skb_copy(skb1, GFP_ATOMIC);
3235 else
3236 skb2 = skb_copy_expand(skb1,
3237 skb_headroom(skb1),
3238 ntail,
3239 GFP_ATOMIC);
3240 if (unlikely(skb2 == NULL))
3241 return -ENOMEM;
3243 if (skb1->sk)
3244 skb_set_owner_w(skb2, skb1->sk);
3246 /* Looking around. Are we still alive?
3247 * OK, link new skb, drop old one */
3249 skb2->next = skb1->next;
3250 *skb_p = skb2;
3251 kfree_skb(skb1);
3252 skb1 = skb2;
3254 elt++;
3255 *trailer = skb1;
3256 skb_p = &skb1->next;
3259 return elt;
3261 EXPORT_SYMBOL_GPL(skb_cow_data);
3263 static void sock_rmem_free(struct sk_buff *skb)
3265 struct sock *sk = skb->sk;
3267 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3271 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3273 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3275 int len = skb->len;
3277 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3278 (unsigned int)sk->sk_rcvbuf)
3279 return -ENOMEM;
3281 skb_orphan(skb);
3282 skb->sk = sk;
3283 skb->destructor = sock_rmem_free;
3284 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3286 /* before exiting rcu section, make sure dst is refcounted */
3287 skb_dst_force(skb);
3289 skb_queue_tail(&sk->sk_error_queue, skb);
3290 if (!sock_flag(sk, SOCK_DEAD))
3291 sk->sk_data_ready(sk, len);
3292 return 0;
3294 EXPORT_SYMBOL(sock_queue_err_skb);
3296 void skb_tstamp_tx(struct sk_buff *orig_skb,
3297 struct skb_shared_hwtstamps *hwtstamps)
3299 struct sock *sk = orig_skb->sk;
3300 struct sock_exterr_skb *serr;
3301 struct sk_buff *skb;
3302 int err;
3304 if (!sk)
3305 return;
3307 if (hwtstamps) {
3308 *skb_hwtstamps(orig_skb) =
3309 *hwtstamps;
3310 } else {
3312 * no hardware time stamps available,
3313 * so keep the shared tx_flags and only
3314 * store software time stamp
3316 orig_skb->tstamp = ktime_get_real();
3319 skb = skb_clone(orig_skb, GFP_ATOMIC);
3320 if (!skb)
3321 return;
3323 serr = SKB_EXT_ERR(skb);
3324 memset(serr, 0, sizeof(*serr));
3325 serr->ee.ee_errno = ENOMSG;
3326 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3328 err = sock_queue_err_skb(sk, skb);
3330 if (err)
3331 kfree_skb(skb);
3333 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3335 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3337 struct sock *sk = skb->sk;
3338 struct sock_exterr_skb *serr;
3339 int err;
3341 skb->wifi_acked_valid = 1;
3342 skb->wifi_acked = acked;
3344 serr = SKB_EXT_ERR(skb);
3345 memset(serr, 0, sizeof(*serr));
3346 serr->ee.ee_errno = ENOMSG;
3347 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3349 err = sock_queue_err_skb(sk, skb);
3350 if (err)
3351 kfree_skb(skb);
3353 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3357 * skb_partial_csum_set - set up and verify partial csum values for packet
3358 * @skb: the skb to set
3359 * @start: the number of bytes after skb->data to start checksumming.
3360 * @off: the offset from start to place the checksum.
3362 * For untrusted partially-checksummed packets, we need to make sure the values
3363 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3365 * This function checks and sets those values and skb->ip_summed: if this
3366 * returns false you should drop the packet.
3368 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3370 if (unlikely(start > skb_headlen(skb)) ||
3371 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3372 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3373 start, off, skb_headlen(skb));
3374 return false;
3376 skb->ip_summed = CHECKSUM_PARTIAL;
3377 skb->csum_start = skb_headroom(skb) + start;
3378 skb->csum_offset = off;
3379 skb_set_transport_header(skb, start);
3380 return true;
3382 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3384 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3386 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3387 skb->dev->name);
3389 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3391 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3393 if (head_stolen) {
3394 skb_release_head_state(skb);
3395 kmem_cache_free(skbuff_head_cache, skb);
3396 } else {
3397 __kfree_skb(skb);
3400 EXPORT_SYMBOL(kfree_skb_partial);
3403 * skb_try_coalesce - try to merge skb to prior one
3404 * @to: prior buffer
3405 * @from: buffer to add
3406 * @fragstolen: pointer to boolean
3407 * @delta_truesize: how much more was allocated than was requested
3409 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3410 bool *fragstolen, int *delta_truesize)
3412 int i, delta, len = from->len;
3414 *fragstolen = false;
3416 if (skb_cloned(to))
3417 return false;
3419 if (len <= skb_tailroom(to)) {
3420 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3421 *delta_truesize = 0;
3422 return true;
3425 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3426 return false;
3428 if (skb_headlen(from) != 0) {
3429 struct page *page;
3430 unsigned int offset;
3432 if (skb_shinfo(to)->nr_frags +
3433 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3434 return false;
3436 if (skb_head_is_locked(from))
3437 return false;
3439 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3441 page = virt_to_head_page(from->head);
3442 offset = from->data - (unsigned char *)page_address(page);
3444 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3445 page, offset, skb_headlen(from));
3446 *fragstolen = true;
3447 } else {
3448 if (skb_shinfo(to)->nr_frags +
3449 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3450 return false;
3452 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3455 WARN_ON_ONCE(delta < len);
3457 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3458 skb_shinfo(from)->frags,
3459 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3460 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3462 if (!skb_cloned(from))
3463 skb_shinfo(from)->nr_frags = 0;
3465 /* if the skb is not cloned this does nothing
3466 * since we set nr_frags to 0.
3468 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3469 skb_frag_ref(from, i);
3471 to->truesize += delta;
3472 to->len += len;
3473 to->data_len += len;
3475 *delta_truesize = delta;
3476 return true;
3478 EXPORT_SYMBOL(skb_try_coalesce);
3481 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3483 * @skb: GSO skb
3485 * skb_gso_transport_seglen is used to determine the real size of the
3486 * individual segments, including Layer4 headers (TCP/UDP).
3488 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3490 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3492 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3494 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3495 return tcp_hdrlen(skb) + shinfo->gso_size;
3497 /* UFO sets gso_size to the size of the fragmentation
3498 * payload, i.e. the size of the L4 (UDP) header is already
3499 * accounted for.
3501 return shinfo->gso_size;
3503 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);