KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / net / core / skbuff.c
blob235c639d370bf304d06d5602cb9ca35fb6bb8dec
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>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
68 #include <net/dst.h>
69 #include <net/sock.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
72 #include <net/xfrm.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
78 struct kmem_cache *skbuff_head_cache __read_mostly;
79 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
80 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
81 EXPORT_SYMBOL(sysctl_max_skb_frags);
83 /**
84 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
88 * @msg: skb_over_panic or skb_under_panic
90 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
95 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
96 const char msg[])
98 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99 msg, addr, skb->len, sz, skb->head, skb->data,
100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
102 BUG();
105 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
107 skb_panic(skb, sz, addr, __func__);
110 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
112 skb_panic(skb, sz, addr, __func__);
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
122 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
125 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
128 void *obj;
129 bool ret_pfmemalloc = false;
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
145 out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
149 return obj;
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
158 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
160 struct sk_buff *skb;
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
173 memset(skb, 0, offsetof(struct sk_buff, tail));
174 skb->head = NULL;
175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
178 skb->mac_header = (typeof(skb->mac_header))~0U;
179 out:
180 return skb;
184 * __alloc_skb - allocate a network buffer
185 * @size: size to allocate
186 * @gfp_mask: allocation mask
187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
191 * @node: numa node to allocate memory on
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
200 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
201 int flags, int node)
203 struct kmem_cache *cache;
204 struct skb_shared_info *shinfo;
205 struct sk_buff *skb;
206 u8 *data;
207 bool pfmemalloc;
209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
215 /* Get the HEAD */
216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
217 if (!skb)
218 goto out;
219 prefetchw(skb);
221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
226 size = SKB_DATA_ALIGN(size);
227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
229 if (!data)
230 goto nodata;
231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
235 size = SKB_WITH_OVERHEAD(ksize(data));
236 prefetchw(data + size);
239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
243 memset(skb, 0, offsetof(struct sk_buff, tail));
244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
246 skb->pfmemalloc = pfmemalloc;
247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
250 skb_reset_tail_pointer(skb);
251 skb->end = skb->tail + size;
252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
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 data, 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() only if
289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
301 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
305 unsigned int size = frag_size ? : ksize(data);
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
329 return skb;
332 /* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
337 struct sk_buff *build_skb(void *data, unsigned int frag_size)
339 struct sk_buff *skb = __build_skb(data, frag_size);
341 if (skb && frag_size) {
342 skb->head_frag = 1;
343 if (virt_to_head_page(data)->pfmemalloc)
344 skb->pfmemalloc = 1;
346 return skb;
348 EXPORT_SYMBOL(build_skb);
350 struct netdev_alloc_cache {
351 struct page_frag frag;
352 /* we maintain a pagecount bias, so that we dont dirty cache line
353 * containing page->_count every time we allocate a fragment.
355 unsigned int pagecnt_bias;
357 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
359 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
361 struct netdev_alloc_cache *nc;
362 void *data = NULL;
363 int order;
364 unsigned long flags;
366 local_irq_save(flags);
367 nc = &__get_cpu_var(netdev_alloc_cache);
368 if (unlikely(!nc->frag.page)) {
369 refill:
370 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
371 gfp_t gfp = gfp_mask;
373 if (order) {
374 gfp |= __GFP_COMP | __GFP_NOWARN |
375 __GFP_NOMEMALLOC;
376 gfp &= ~__GFP_WAIT;
378 nc->frag.page = alloc_pages(gfp, order);
379 if (likely(nc->frag.page))
380 break;
381 if (--order < 0)
382 goto end;
384 nc->frag.size = PAGE_SIZE << order;
385 recycle:
386 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
387 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
388 nc->frag.offset = 0;
391 if (nc->frag.offset + fragsz > nc->frag.size) {
392 /* avoid unnecessary locked operations if possible */
393 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
394 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
395 goto recycle;
396 goto refill;
399 data = page_address(nc->frag.page) + nc->frag.offset;
400 nc->frag.offset += fragsz;
401 nc->pagecnt_bias--;
402 end:
403 local_irq_restore(flags);
404 return data;
408 * netdev_alloc_frag - allocate a page fragment
409 * @fragsz: fragment size
411 * Allocates a frag from a page for receive buffer.
412 * Uses GFP_ATOMIC allocations.
414 void *netdev_alloc_frag(unsigned int fragsz)
416 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
418 EXPORT_SYMBOL(netdev_alloc_frag);
421 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
422 * @dev: network device to receive on
423 * @length: length to allocate
424 * @gfp_mask: get_free_pages mask, passed to alloc_skb
426 * Allocate a new &sk_buff and assign it a usage count of one. The
427 * buffer has unspecified headroom built in. Users should allocate
428 * the headroom they think they need without accounting for the
429 * built in space. The built in space is used for optimisations.
431 * %NULL is returned if there is no free memory.
433 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
434 unsigned int length, gfp_t gfp_mask)
436 struct sk_buff *skb = NULL;
437 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
438 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
440 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
441 void *data;
443 if (sk_memalloc_socks())
444 gfp_mask |= __GFP_MEMALLOC;
446 data = __netdev_alloc_frag(fragsz, gfp_mask);
448 if (likely(data)) {
449 skb = build_skb(data, fragsz);
450 if (unlikely(!skb))
451 put_page(virt_to_head_page(data));
453 } else {
454 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
455 SKB_ALLOC_RX, NUMA_NO_NODE);
457 if (likely(skb)) {
458 skb_reserve(skb, NET_SKB_PAD);
459 skb->dev = dev;
461 return skb;
463 EXPORT_SYMBOL(__netdev_alloc_skb);
465 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
466 int size, unsigned int truesize)
468 skb_fill_page_desc(skb, i, page, off, size);
469 skb->len += size;
470 skb->data_len += size;
471 skb->truesize += truesize;
473 EXPORT_SYMBOL(skb_add_rx_frag);
475 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
476 unsigned int truesize)
478 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
480 skb_frag_size_add(frag, size);
481 skb->len += size;
482 skb->data_len += size;
483 skb->truesize += truesize;
485 EXPORT_SYMBOL(skb_coalesce_rx_frag);
487 static void skb_drop_list(struct sk_buff **listp)
489 kfree_skb_list(*listp);
490 *listp = NULL;
493 static inline void skb_drop_fraglist(struct sk_buff *skb)
495 skb_drop_list(&skb_shinfo(skb)->frag_list);
498 static void skb_clone_fraglist(struct sk_buff *skb)
500 struct sk_buff *list;
502 skb_walk_frags(skb, list)
503 skb_get(list);
506 static void skb_free_head(struct sk_buff *skb)
508 if (skb->head_frag)
509 put_page(virt_to_head_page(skb->head));
510 else
511 kfree(skb->head);
514 static void skb_release_data(struct sk_buff *skb)
516 if (!skb->cloned ||
517 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
518 &skb_shinfo(skb)->dataref)) {
519 if (skb_shinfo(skb)->nr_frags) {
520 int i;
521 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
522 skb_frag_unref(skb, i);
526 * If skb buf is from userspace, we need to notify the caller
527 * the lower device DMA has done;
529 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
530 struct ubuf_info *uarg;
532 uarg = skb_shinfo(skb)->destructor_arg;
533 if (uarg->callback)
534 uarg->callback(uarg, true);
537 if (skb_has_frag_list(skb))
538 skb_drop_fraglist(skb);
540 skb_free_head(skb);
545 * Free an skbuff by memory without cleaning the state.
547 static void kfree_skbmem(struct sk_buff *skb)
549 struct sk_buff *other;
550 atomic_t *fclone_ref;
552 switch (skb->fclone) {
553 case SKB_FCLONE_UNAVAILABLE:
554 kmem_cache_free(skbuff_head_cache, skb);
555 break;
557 case SKB_FCLONE_ORIG:
558 fclone_ref = (atomic_t *) (skb + 2);
559 if (atomic_dec_and_test(fclone_ref))
560 kmem_cache_free(skbuff_fclone_cache, skb);
561 break;
563 case SKB_FCLONE_CLONE:
564 fclone_ref = (atomic_t *) (skb + 1);
565 other = skb - 1;
567 /* The clone portion is available for
568 * fast-cloning again.
570 skb->fclone = SKB_FCLONE_UNAVAILABLE;
572 if (atomic_dec_and_test(fclone_ref))
573 kmem_cache_free(skbuff_fclone_cache, other);
574 break;
578 static void skb_release_head_state(struct sk_buff *skb)
580 skb_dst_drop(skb);
581 #ifdef CONFIG_XFRM
582 secpath_put(skb->sp);
583 #endif
584 if (skb->destructor) {
585 WARN_ON(in_irq());
586 skb->destructor(skb);
588 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
589 nf_conntrack_put(skb->nfct);
590 #endif
591 #ifdef CONFIG_BRIDGE_NETFILTER
592 nf_bridge_put(skb->nf_bridge);
593 #endif
594 /* XXX: IS this still necessary? - JHS */
595 #ifdef CONFIG_NET_SCHED
596 skb->tc_index = 0;
597 #ifdef CONFIG_NET_CLS_ACT
598 skb->tc_verd = 0;
599 #endif
600 #endif
603 /* Free everything but the sk_buff shell. */
604 static void skb_release_all(struct sk_buff *skb)
606 skb_release_head_state(skb);
607 if (likely(skb->head))
608 skb_release_data(skb);
612 * __kfree_skb - private function
613 * @skb: buffer
615 * Free an sk_buff. Release anything attached to the buffer.
616 * Clean the state. This is an internal helper function. Users should
617 * always call kfree_skb
620 void __kfree_skb(struct sk_buff *skb)
622 skb_release_all(skb);
623 kfree_skbmem(skb);
625 EXPORT_SYMBOL(__kfree_skb);
628 * kfree_skb - free an sk_buff
629 * @skb: buffer to free
631 * Drop a reference to the buffer and free it if the usage count has
632 * hit zero.
634 void kfree_skb(struct sk_buff *skb)
636 if (unlikely(!skb))
637 return;
638 if (likely(atomic_read(&skb->users) == 1))
639 smp_rmb();
640 else if (likely(!atomic_dec_and_test(&skb->users)))
641 return;
642 trace_kfree_skb(skb, __builtin_return_address(0));
643 __kfree_skb(skb);
645 EXPORT_SYMBOL(kfree_skb);
647 void kfree_skb_list(struct sk_buff *segs)
649 while (segs) {
650 struct sk_buff *next = segs->next;
652 kfree_skb(segs);
653 segs = next;
656 EXPORT_SYMBOL(kfree_skb_list);
659 * skb_tx_error - report an sk_buff xmit error
660 * @skb: buffer that triggered an error
662 * Report xmit error if a device callback is tracking this skb.
663 * skb must be freed afterwards.
665 void skb_tx_error(struct sk_buff *skb)
667 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
668 struct ubuf_info *uarg;
670 uarg = skb_shinfo(skb)->destructor_arg;
671 if (uarg->callback)
672 uarg->callback(uarg, false);
673 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
676 EXPORT_SYMBOL(skb_tx_error);
679 * consume_skb - free an skbuff
680 * @skb: buffer to free
682 * Drop a ref to the buffer and free it if the usage count has hit zero
683 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
684 * is being dropped after a failure and notes that
686 void consume_skb(struct sk_buff *skb)
688 if (unlikely(!skb))
689 return;
690 if (likely(atomic_read(&skb->users) == 1))
691 smp_rmb();
692 else if (likely(!atomic_dec_and_test(&skb->users)))
693 return;
694 trace_consume_skb(skb);
695 __kfree_skb(skb);
697 EXPORT_SYMBOL(consume_skb);
699 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
701 new->tstamp = old->tstamp;
702 new->dev = old->dev;
703 new->transport_header = old->transport_header;
704 new->network_header = old->network_header;
705 new->mac_header = old->mac_header;
706 new->inner_protocol = old->inner_protocol;
707 new->inner_transport_header = old->inner_transport_header;
708 new->inner_network_header = old->inner_network_header;
709 new->inner_mac_header = old->inner_mac_header;
710 skb_dst_copy(new, old);
711 skb_copy_hash(new, old);
712 new->ooo_okay = old->ooo_okay;
713 new->no_fcs = old->no_fcs;
714 new->encapsulation = old->encapsulation;
715 new->encap_hdr_csum = old->encap_hdr_csum;
716 new->csum_valid = old->csum_valid;
717 new->csum_complete_sw = old->csum_complete_sw;
718 #ifdef CONFIG_XFRM
719 new->sp = secpath_get(old->sp);
720 #endif
721 memcpy(new->cb, old->cb, sizeof(old->cb));
722 new->csum = old->csum;
723 new->ignore_df = old->ignore_df;
724 new->pkt_type = old->pkt_type;
725 new->ip_summed = old->ip_summed;
726 skb_copy_queue_mapping(new, old);
727 new->priority = old->priority;
728 #if IS_ENABLED(CONFIG_IP_VS)
729 new->ipvs_property = old->ipvs_property;
730 #endif
731 new->pfmemalloc = old->pfmemalloc;
732 new->protocol = old->protocol;
733 new->mark = old->mark;
734 new->skb_iif = old->skb_iif;
735 __nf_copy(new, old);
736 #ifdef CONFIG_NET_SCHED
737 new->tc_index = old->tc_index;
738 #ifdef CONFIG_NET_CLS_ACT
739 new->tc_verd = old->tc_verd;
740 #endif
741 #endif
742 new->vlan_proto = old->vlan_proto;
743 new->vlan_tci = old->vlan_tci;
745 skb_copy_secmark(new, old);
747 #ifdef CONFIG_NET_RX_BUSY_POLL
748 new->napi_id = old->napi_id;
749 #endif
753 * You should not add any new code to this function. Add it to
754 * __copy_skb_header above instead.
756 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
758 #define C(x) n->x = skb->x
760 n->next = n->prev = NULL;
761 n->sk = NULL;
762 __copy_skb_header(n, skb);
764 C(len);
765 C(data_len);
766 C(mac_len);
767 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
768 n->cloned = 1;
769 n->nohdr = 0;
770 n->destructor = NULL;
771 C(tail);
772 C(end);
773 C(head);
774 C(head_frag);
775 C(data);
776 C(truesize);
777 atomic_set(&n->users, 1);
779 atomic_inc(&(skb_shinfo(skb)->dataref));
780 skb->cloned = 1;
782 return n;
783 #undef C
787 * skb_morph - morph one skb into another
788 * @dst: the skb to receive the contents
789 * @src: the skb to supply the contents
791 * This is identical to skb_clone except that the target skb is
792 * supplied by the user.
794 * The target skb is returned upon exit.
796 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
798 skb_release_all(dst);
799 return __skb_clone(dst, src);
801 EXPORT_SYMBOL_GPL(skb_morph);
804 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
805 * @skb: the skb to modify
806 * @gfp_mask: allocation priority
808 * This must be called on SKBTX_DEV_ZEROCOPY skb.
809 * It will copy all frags into kernel and drop the reference
810 * to userspace pages.
812 * If this function is called from an interrupt gfp_mask() must be
813 * %GFP_ATOMIC.
815 * Returns 0 on success or a negative error code on failure
816 * to allocate kernel memory to copy to.
818 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
820 int i;
821 int num_frags = skb_shinfo(skb)->nr_frags;
822 struct page *page, *head = NULL;
823 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
825 for (i = 0; i < num_frags; i++) {
826 u8 *vaddr;
827 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
829 page = alloc_page(gfp_mask);
830 if (!page) {
831 while (head) {
832 struct page *next = (struct page *)page_private(head);
833 put_page(head);
834 head = next;
836 return -ENOMEM;
838 vaddr = kmap_atomic(skb_frag_page(f));
839 memcpy(page_address(page),
840 vaddr + f->page_offset, skb_frag_size(f));
841 kunmap_atomic(vaddr);
842 set_page_private(page, (unsigned long)head);
843 head = page;
846 /* skb frags release userspace buffers */
847 for (i = 0; i < num_frags; i++)
848 skb_frag_unref(skb, i);
850 uarg->callback(uarg, false);
852 /* skb frags point to kernel buffers */
853 for (i = num_frags - 1; i >= 0; i--) {
854 __skb_fill_page_desc(skb, i, head, 0,
855 skb_shinfo(skb)->frags[i].size);
856 head = (struct page *)page_private(head);
859 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
860 return 0;
862 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
865 * skb_clone - duplicate an sk_buff
866 * @skb: buffer to clone
867 * @gfp_mask: allocation priority
869 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
870 * copies share the same packet data but not structure. The new
871 * buffer has a reference count of 1. If the allocation fails the
872 * function returns %NULL otherwise the new buffer is returned.
874 * If this function is called from an interrupt gfp_mask() must be
875 * %GFP_ATOMIC.
878 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
880 struct sk_buff *n;
882 if (skb_orphan_frags(skb, gfp_mask))
883 return NULL;
885 n = skb + 1;
886 if (skb->fclone == SKB_FCLONE_ORIG &&
887 n->fclone == SKB_FCLONE_UNAVAILABLE) {
888 atomic_t *fclone_ref = (atomic_t *) (n + 1);
889 n->fclone = SKB_FCLONE_CLONE;
890 atomic_inc(fclone_ref);
891 } else {
892 if (skb_pfmemalloc(skb))
893 gfp_mask |= __GFP_MEMALLOC;
895 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
896 if (!n)
897 return NULL;
899 kmemcheck_annotate_bitfield(n, flags1);
900 kmemcheck_annotate_bitfield(n, flags2);
901 n->fclone = SKB_FCLONE_UNAVAILABLE;
904 return __skb_clone(n, skb);
906 EXPORT_SYMBOL(skb_clone);
908 static void skb_headers_offset_update(struct sk_buff *skb, int off)
910 /* Only adjust this if it actually is csum_start rather than csum */
911 if (skb->ip_summed == CHECKSUM_PARTIAL)
912 skb->csum_start += off;
913 /* {transport,network,mac}_header and tail are relative to skb->head */
914 skb->transport_header += off;
915 skb->network_header += off;
916 if (skb_mac_header_was_set(skb))
917 skb->mac_header += off;
918 skb->inner_transport_header += off;
919 skb->inner_network_header += off;
920 skb->inner_mac_header += off;
923 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
925 __copy_skb_header(new, old);
927 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
928 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
929 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
932 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
934 if (skb_pfmemalloc(skb))
935 return SKB_ALLOC_RX;
936 return 0;
940 * skb_copy - create private copy of an sk_buff
941 * @skb: buffer to copy
942 * @gfp_mask: allocation priority
944 * Make a copy of both an &sk_buff and its data. This is used when the
945 * caller wishes to modify the data and needs a private copy of the
946 * data to alter. Returns %NULL on failure or the pointer to the buffer
947 * on success. The returned buffer has a reference count of 1.
949 * As by-product this function converts non-linear &sk_buff to linear
950 * one, so that &sk_buff becomes completely private and caller is allowed
951 * to modify all the data of returned buffer. This means that this
952 * function is not recommended for use in circumstances when only
953 * header is going to be modified. Use pskb_copy() instead.
956 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
958 int headerlen = skb_headroom(skb);
959 unsigned int size = skb_end_offset(skb) + skb->data_len;
960 struct sk_buff *n = __alloc_skb(size, gfp_mask,
961 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
963 if (!n)
964 return NULL;
966 /* Set the data pointer */
967 skb_reserve(n, headerlen);
968 /* Set the tail pointer and length */
969 skb_put(n, skb->len);
971 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
972 BUG();
974 copy_skb_header(n, skb);
975 return n;
977 EXPORT_SYMBOL(skb_copy);
980 * __pskb_copy_fclone - create copy of an sk_buff with private head.
981 * @skb: buffer to copy
982 * @headroom: headroom of new skb
983 * @gfp_mask: allocation priority
984 * @fclone: if true allocate the copy of the skb from the fclone
985 * cache instead of the head cache; it is recommended to set this
986 * to true for the cases where the copy will likely be cloned
988 * Make a copy of both an &sk_buff and part of its data, located
989 * in header. Fragmented data remain shared. This is used when
990 * the caller wishes to modify only header of &sk_buff and needs
991 * private copy of the header to alter. Returns %NULL on failure
992 * or the pointer to the buffer on success.
993 * The returned buffer has a reference count of 1.
996 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
997 gfp_t gfp_mask, bool fclone)
999 unsigned int size = skb_headlen(skb) + headroom;
1000 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1001 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1003 if (!n)
1004 goto out;
1006 /* Set the data pointer */
1007 skb_reserve(n, headroom);
1008 /* Set the tail pointer and length */
1009 skb_put(n, skb_headlen(skb));
1010 /* Copy the bytes */
1011 skb_copy_from_linear_data(skb, n->data, n->len);
1013 n->truesize += skb->data_len;
1014 n->data_len = skb->data_len;
1015 n->len = skb->len;
1017 if (skb_shinfo(skb)->nr_frags) {
1018 int i;
1020 if (skb_orphan_frags(skb, gfp_mask)) {
1021 kfree_skb(n);
1022 n = NULL;
1023 goto out;
1025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1026 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1027 skb_frag_ref(skb, i);
1029 skb_shinfo(n)->nr_frags = i;
1032 if (skb_has_frag_list(skb)) {
1033 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1034 skb_clone_fraglist(n);
1037 copy_skb_header(n, skb);
1038 out:
1039 return n;
1041 EXPORT_SYMBOL(__pskb_copy_fclone);
1044 * pskb_expand_head - reallocate header of &sk_buff
1045 * @skb: buffer to reallocate
1046 * @nhead: room to add at head
1047 * @ntail: room to add at tail
1048 * @gfp_mask: allocation priority
1050 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1051 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1052 * reference count of 1. Returns zero in the case of success or error,
1053 * if expansion failed. In the last case, &sk_buff is not changed.
1055 * All the pointers pointing into skb header may change and must be
1056 * reloaded after call to this function.
1059 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1060 gfp_t gfp_mask)
1062 int i;
1063 u8 *data;
1064 int size = nhead + skb_end_offset(skb) + ntail;
1065 long off;
1067 BUG_ON(nhead < 0);
1069 if (skb_shared(skb))
1070 BUG();
1072 size = SKB_DATA_ALIGN(size);
1074 if (skb_pfmemalloc(skb))
1075 gfp_mask |= __GFP_MEMALLOC;
1076 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1077 gfp_mask, NUMA_NO_NODE, NULL);
1078 if (!data)
1079 goto nodata;
1080 size = SKB_WITH_OVERHEAD(ksize(data));
1082 /* Copy only real data... and, alas, header. This should be
1083 * optimized for the cases when header is void.
1085 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1087 memcpy((struct skb_shared_info *)(data + size),
1088 skb_shinfo(skb),
1089 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1092 * if shinfo is shared we must drop the old head gracefully, but if it
1093 * is not we can just drop the old head and let the existing refcount
1094 * be since all we did is relocate the values
1096 if (skb_cloned(skb)) {
1097 /* copy this zero copy skb frags */
1098 if (skb_orphan_frags(skb, gfp_mask))
1099 goto nofrags;
1100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1101 skb_frag_ref(skb, i);
1103 if (skb_has_frag_list(skb))
1104 skb_clone_fraglist(skb);
1106 skb_release_data(skb);
1107 } else {
1108 skb_free_head(skb);
1110 off = (data + nhead) - skb->head;
1112 skb->head = data;
1113 skb->head_frag = 0;
1114 skb->data += off;
1115 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1116 skb->end = size;
1117 off = nhead;
1118 #else
1119 skb->end = skb->head + size;
1120 #endif
1121 skb->tail += off;
1122 skb_headers_offset_update(skb, nhead);
1123 skb->cloned = 0;
1124 skb->hdr_len = 0;
1125 skb->nohdr = 0;
1126 atomic_set(&skb_shinfo(skb)->dataref, 1);
1127 return 0;
1129 nofrags:
1130 kfree(data);
1131 nodata:
1132 return -ENOMEM;
1134 EXPORT_SYMBOL(pskb_expand_head);
1136 /* Make private copy of skb with writable head and some headroom */
1138 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1140 struct sk_buff *skb2;
1141 int delta = headroom - skb_headroom(skb);
1143 if (delta <= 0)
1144 skb2 = pskb_copy(skb, GFP_ATOMIC);
1145 else {
1146 skb2 = skb_clone(skb, GFP_ATOMIC);
1147 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1148 GFP_ATOMIC)) {
1149 kfree_skb(skb2);
1150 skb2 = NULL;
1153 return skb2;
1155 EXPORT_SYMBOL(skb_realloc_headroom);
1158 * skb_copy_expand - copy and expand sk_buff
1159 * @skb: buffer to copy
1160 * @newheadroom: new free bytes at head
1161 * @newtailroom: new free bytes at tail
1162 * @gfp_mask: allocation priority
1164 * Make a copy of both an &sk_buff and its data and while doing so
1165 * allocate additional space.
1167 * This is used when the caller wishes to modify the data and needs a
1168 * private copy of the data to alter as well as more space for new fields.
1169 * Returns %NULL on failure or the pointer to the buffer
1170 * on success. The returned buffer has a reference count of 1.
1172 * You must pass %GFP_ATOMIC as the allocation priority if this function
1173 * is called from an interrupt.
1175 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1176 int newheadroom, int newtailroom,
1177 gfp_t gfp_mask)
1180 * Allocate the copy buffer
1182 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1183 gfp_mask, skb_alloc_rx_flag(skb),
1184 NUMA_NO_NODE);
1185 int oldheadroom = skb_headroom(skb);
1186 int head_copy_len, head_copy_off;
1188 if (!n)
1189 return NULL;
1191 skb_reserve(n, newheadroom);
1193 /* Set the tail pointer and length */
1194 skb_put(n, skb->len);
1196 head_copy_len = oldheadroom;
1197 head_copy_off = 0;
1198 if (newheadroom <= head_copy_len)
1199 head_copy_len = newheadroom;
1200 else
1201 head_copy_off = newheadroom - head_copy_len;
1203 /* Copy the linear header and data. */
1204 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1205 skb->len + head_copy_len))
1206 BUG();
1208 copy_skb_header(n, skb);
1210 skb_headers_offset_update(n, newheadroom - oldheadroom);
1212 return n;
1214 EXPORT_SYMBOL(skb_copy_expand);
1217 * skb_pad - zero pad the tail of an skb
1218 * @skb: buffer to pad
1219 * @pad: space to pad
1221 * Ensure that a buffer is followed by a padding area that is zero
1222 * filled. Used by network drivers which may DMA or transfer data
1223 * beyond the buffer end onto the wire.
1225 * May return error in out of memory cases. The skb is freed on error.
1228 int skb_pad(struct sk_buff *skb, int pad)
1230 int err;
1231 int ntail;
1233 /* If the skbuff is non linear tailroom is always zero.. */
1234 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1235 memset(skb->data+skb->len, 0, pad);
1236 return 0;
1239 ntail = skb->data_len + pad - (skb->end - skb->tail);
1240 if (likely(skb_cloned(skb) || ntail > 0)) {
1241 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1242 if (unlikely(err))
1243 goto free_skb;
1246 /* FIXME: The use of this function with non-linear skb's really needs
1247 * to be audited.
1249 err = skb_linearize(skb);
1250 if (unlikely(err))
1251 goto free_skb;
1253 memset(skb->data + skb->len, 0, pad);
1254 return 0;
1256 free_skb:
1257 kfree_skb(skb);
1258 return err;
1260 EXPORT_SYMBOL(skb_pad);
1263 * pskb_put - add data to the tail of a potentially fragmented buffer
1264 * @skb: start of the buffer to use
1265 * @tail: tail fragment of the buffer to use
1266 * @len: amount of data to add
1268 * This function extends the used data area of the potentially
1269 * fragmented buffer. @tail must be the last fragment of @skb -- or
1270 * @skb itself. If this would exceed the total buffer size the kernel
1271 * will panic. A pointer to the first byte of the extra data is
1272 * returned.
1275 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1277 if (tail != skb) {
1278 skb->data_len += len;
1279 skb->len += len;
1281 return skb_put(tail, len);
1283 EXPORT_SYMBOL_GPL(pskb_put);
1286 * skb_put - add data to a buffer
1287 * @skb: buffer to use
1288 * @len: amount of data to add
1290 * This function extends the used data area of the buffer. If this would
1291 * exceed the total buffer size the kernel will panic. A pointer to the
1292 * first byte of the extra data is returned.
1294 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1296 unsigned char *tmp = skb_tail_pointer(skb);
1297 SKB_LINEAR_ASSERT(skb);
1298 skb->tail += len;
1299 skb->len += len;
1300 if (unlikely(skb->tail > skb->end))
1301 skb_over_panic(skb, len, __builtin_return_address(0));
1302 return tmp;
1304 EXPORT_SYMBOL(skb_put);
1307 * skb_push - add data to the start of a buffer
1308 * @skb: buffer to use
1309 * @len: amount of data to add
1311 * This function extends the used data area of the buffer at the buffer
1312 * start. If this would exceed the total buffer headroom the kernel will
1313 * panic. A pointer to the first byte of the extra data is returned.
1315 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1317 skb->data -= len;
1318 skb->len += len;
1319 if (unlikely(skb->data<skb->head))
1320 skb_under_panic(skb, len, __builtin_return_address(0));
1321 return skb->data;
1323 EXPORT_SYMBOL(skb_push);
1326 * skb_pull - remove data from the start of a buffer
1327 * @skb: buffer to use
1328 * @len: amount of data to remove
1330 * This function removes data from the start of a buffer, returning
1331 * the memory to the headroom. A pointer to the next data in the buffer
1332 * is returned. Once the data has been pulled future pushes will overwrite
1333 * the old data.
1335 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1337 return skb_pull_inline(skb, len);
1339 EXPORT_SYMBOL(skb_pull);
1342 * skb_trim - remove end from a buffer
1343 * @skb: buffer to alter
1344 * @len: new length
1346 * Cut the length of a buffer down by removing data from the tail. If
1347 * the buffer is already under the length specified it is not modified.
1348 * The skb must be linear.
1350 void skb_trim(struct sk_buff *skb, unsigned int len)
1352 if (skb->len > len)
1353 __skb_trim(skb, len);
1355 EXPORT_SYMBOL(skb_trim);
1357 /* Trims skb to length len. It can change skb pointers.
1360 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1362 struct sk_buff **fragp;
1363 struct sk_buff *frag;
1364 int offset = skb_headlen(skb);
1365 int nfrags = skb_shinfo(skb)->nr_frags;
1366 int i;
1367 int err;
1369 if (skb_cloned(skb) &&
1370 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1371 return err;
1373 i = 0;
1374 if (offset >= len)
1375 goto drop_pages;
1377 for (; i < nfrags; i++) {
1378 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1380 if (end < len) {
1381 offset = end;
1382 continue;
1385 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1387 drop_pages:
1388 skb_shinfo(skb)->nr_frags = i;
1390 for (; i < nfrags; i++)
1391 skb_frag_unref(skb, i);
1393 if (skb_has_frag_list(skb))
1394 skb_drop_fraglist(skb);
1395 goto done;
1398 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1399 fragp = &frag->next) {
1400 int end = offset + frag->len;
1402 if (skb_shared(frag)) {
1403 struct sk_buff *nfrag;
1405 nfrag = skb_clone(frag, GFP_ATOMIC);
1406 if (unlikely(!nfrag))
1407 return -ENOMEM;
1409 nfrag->next = frag->next;
1410 consume_skb(frag);
1411 frag = nfrag;
1412 *fragp = frag;
1415 if (end < len) {
1416 offset = end;
1417 continue;
1420 if (end > len &&
1421 unlikely((err = pskb_trim(frag, len - offset))))
1422 return err;
1424 if (frag->next)
1425 skb_drop_list(&frag->next);
1426 break;
1429 done:
1430 if (len > skb_headlen(skb)) {
1431 skb->data_len -= skb->len - len;
1432 skb->len = len;
1433 } else {
1434 skb->len = len;
1435 skb->data_len = 0;
1436 skb_set_tail_pointer(skb, len);
1439 return 0;
1441 EXPORT_SYMBOL(___pskb_trim);
1444 * __pskb_pull_tail - advance tail of skb header
1445 * @skb: buffer to reallocate
1446 * @delta: number of bytes to advance tail
1448 * The function makes a sense only on a fragmented &sk_buff,
1449 * it expands header moving its tail forward and copying necessary
1450 * data from fragmented part.
1452 * &sk_buff MUST have reference count of 1.
1454 * Returns %NULL (and &sk_buff does not change) if pull failed
1455 * or value of new tail of skb in the case of success.
1457 * All the pointers pointing into skb header may change and must be
1458 * reloaded after call to this function.
1461 /* Moves tail of skb head forward, copying data from fragmented part,
1462 * when it is necessary.
1463 * 1. It may fail due to malloc failure.
1464 * 2. It may change skb pointers.
1466 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1468 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1470 /* If skb has not enough free space at tail, get new one
1471 * plus 128 bytes for future expansions. If we have enough
1472 * room at tail, reallocate without expansion only if skb is cloned.
1474 int i, k, eat = (skb->tail + delta) - skb->end;
1476 if (eat > 0 || skb_cloned(skb)) {
1477 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1478 GFP_ATOMIC))
1479 return NULL;
1482 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1483 BUG();
1485 /* Optimization: no fragments, no reasons to preestimate
1486 * size of pulled pages. Superb.
1488 if (!skb_has_frag_list(skb))
1489 goto pull_pages;
1491 /* Estimate size of pulled pages. */
1492 eat = delta;
1493 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1494 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1496 if (size >= eat)
1497 goto pull_pages;
1498 eat -= size;
1501 /* If we need update frag list, we are in troubles.
1502 * Certainly, it possible to add an offset to skb data,
1503 * but taking into account that pulling is expected to
1504 * be very rare operation, it is worth to fight against
1505 * further bloating skb head and crucify ourselves here instead.
1506 * Pure masohism, indeed. 8)8)
1508 if (eat) {
1509 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1510 struct sk_buff *clone = NULL;
1511 struct sk_buff *insp = NULL;
1513 do {
1514 BUG_ON(!list);
1516 if (list->len <= eat) {
1517 /* Eaten as whole. */
1518 eat -= list->len;
1519 list = list->next;
1520 insp = list;
1521 } else {
1522 /* Eaten partially. */
1524 if (skb_shared(list)) {
1525 /* Sucks! We need to fork list. :-( */
1526 clone = skb_clone(list, GFP_ATOMIC);
1527 if (!clone)
1528 return NULL;
1529 insp = list->next;
1530 list = clone;
1531 } else {
1532 /* This may be pulled without
1533 * problems. */
1534 insp = list;
1536 if (!pskb_pull(list, eat)) {
1537 kfree_skb(clone);
1538 return NULL;
1540 break;
1542 } while (eat);
1544 /* Free pulled out fragments. */
1545 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1546 skb_shinfo(skb)->frag_list = list->next;
1547 kfree_skb(list);
1549 /* And insert new clone at head. */
1550 if (clone) {
1551 clone->next = list;
1552 skb_shinfo(skb)->frag_list = clone;
1555 /* Success! Now we may commit changes to skb data. */
1557 pull_pages:
1558 eat = delta;
1559 k = 0;
1560 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1561 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1563 if (size <= eat) {
1564 skb_frag_unref(skb, i);
1565 eat -= size;
1566 } else {
1567 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1568 if (eat) {
1569 skb_shinfo(skb)->frags[k].page_offset += eat;
1570 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1571 eat = 0;
1573 k++;
1576 skb_shinfo(skb)->nr_frags = k;
1578 skb->tail += delta;
1579 skb->data_len -= delta;
1581 return skb_tail_pointer(skb);
1583 EXPORT_SYMBOL(__pskb_pull_tail);
1586 * skb_copy_bits - copy bits from skb to kernel buffer
1587 * @skb: source skb
1588 * @offset: offset in source
1589 * @to: destination buffer
1590 * @len: number of bytes to copy
1592 * Copy the specified number of bytes from the source skb to the
1593 * destination buffer.
1595 * CAUTION ! :
1596 * If its prototype is ever changed,
1597 * check arch/{*}/net/{*}.S files,
1598 * since it is called from BPF assembly code.
1600 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1602 int start = skb_headlen(skb);
1603 struct sk_buff *frag_iter;
1604 int i, copy;
1606 if (offset > (int)skb->len - len)
1607 goto fault;
1609 /* Copy header. */
1610 if ((copy = start - offset) > 0) {
1611 if (copy > len)
1612 copy = len;
1613 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1614 if ((len -= copy) == 0)
1615 return 0;
1616 offset += copy;
1617 to += copy;
1620 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1621 int end;
1622 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1624 WARN_ON(start > offset + len);
1626 end = start + skb_frag_size(f);
1627 if ((copy = end - offset) > 0) {
1628 u8 *vaddr;
1630 if (copy > len)
1631 copy = len;
1633 vaddr = kmap_atomic(skb_frag_page(f));
1634 memcpy(to,
1635 vaddr + f->page_offset + offset - start,
1636 copy);
1637 kunmap_atomic(vaddr);
1639 if ((len -= copy) == 0)
1640 return 0;
1641 offset += copy;
1642 to += copy;
1644 start = end;
1647 skb_walk_frags(skb, frag_iter) {
1648 int end;
1650 WARN_ON(start > offset + len);
1652 end = start + frag_iter->len;
1653 if ((copy = end - offset) > 0) {
1654 if (copy > len)
1655 copy = len;
1656 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1657 goto fault;
1658 if ((len -= copy) == 0)
1659 return 0;
1660 offset += copy;
1661 to += copy;
1663 start = end;
1666 if (!len)
1667 return 0;
1669 fault:
1670 return -EFAULT;
1672 EXPORT_SYMBOL(skb_copy_bits);
1675 * Callback from splice_to_pipe(), if we need to release some pages
1676 * at the end of the spd in case we error'ed out in filling the pipe.
1678 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1680 put_page(spd->pages[i]);
1683 static struct page *linear_to_page(struct page *page, unsigned int *len,
1684 unsigned int *offset,
1685 struct sock *sk)
1687 struct page_frag *pfrag = sk_page_frag(sk);
1689 if (!sk_page_frag_refill(sk, pfrag))
1690 return NULL;
1692 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1694 memcpy(page_address(pfrag->page) + pfrag->offset,
1695 page_address(page) + *offset, *len);
1696 *offset = pfrag->offset;
1697 pfrag->offset += *len;
1699 return pfrag->page;
1702 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1703 struct page *page,
1704 unsigned int offset)
1706 return spd->nr_pages &&
1707 spd->pages[spd->nr_pages - 1] == page &&
1708 (spd->partial[spd->nr_pages - 1].offset +
1709 spd->partial[spd->nr_pages - 1].len == offset);
1713 * Fill page/offset/length into spd, if it can hold more pages.
1715 static bool spd_fill_page(struct splice_pipe_desc *spd,
1716 struct pipe_inode_info *pipe, struct page *page,
1717 unsigned int *len, unsigned int offset,
1718 bool linear,
1719 struct sock *sk)
1721 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1722 return true;
1724 if (linear) {
1725 page = linear_to_page(page, len, &offset, sk);
1726 if (!page)
1727 return true;
1729 if (spd_can_coalesce(spd, page, offset)) {
1730 spd->partial[spd->nr_pages - 1].len += *len;
1731 return false;
1733 get_page(page);
1734 spd->pages[spd->nr_pages] = page;
1735 spd->partial[spd->nr_pages].len = *len;
1736 spd->partial[spd->nr_pages].offset = offset;
1737 spd->nr_pages++;
1739 return false;
1742 static bool __splice_segment(struct page *page, unsigned int poff,
1743 unsigned int plen, unsigned int *off,
1744 unsigned int *len,
1745 struct splice_pipe_desc *spd, bool linear,
1746 struct sock *sk,
1747 struct pipe_inode_info *pipe)
1749 if (!*len)
1750 return true;
1752 /* skip this segment if already processed */
1753 if (*off >= plen) {
1754 *off -= plen;
1755 return false;
1758 /* ignore any bits we already processed */
1759 poff += *off;
1760 plen -= *off;
1761 *off = 0;
1763 do {
1764 unsigned int flen = min(*len, plen);
1766 if (spd_fill_page(spd, pipe, page, &flen, poff,
1767 linear, sk))
1768 return true;
1769 poff += flen;
1770 plen -= flen;
1771 *len -= flen;
1772 } while (*len && plen);
1774 return false;
1778 * Map linear and fragment data from the skb to spd. It reports true if the
1779 * pipe is full or if we already spliced the requested length.
1781 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1782 unsigned int *offset, unsigned int *len,
1783 struct splice_pipe_desc *spd, struct sock *sk)
1785 int seg;
1787 /* map the linear part :
1788 * If skb->head_frag is set, this 'linear' part is backed by a
1789 * fragment, and if the head is not shared with any clones then
1790 * we can avoid a copy since we own the head portion of this page.
1792 if (__splice_segment(virt_to_page(skb->data),
1793 (unsigned long) skb->data & (PAGE_SIZE - 1),
1794 skb_headlen(skb),
1795 offset, len, spd,
1796 skb_head_is_locked(skb),
1797 sk, pipe))
1798 return true;
1801 * then map the fragments
1803 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1804 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1806 if (__splice_segment(skb_frag_page(f),
1807 f->page_offset, skb_frag_size(f),
1808 offset, len, spd, false, sk, pipe))
1809 return true;
1812 return false;
1816 * Map data from the skb to a pipe. Should handle both the linear part,
1817 * the fragments, and the frag list. It does NOT handle frag lists within
1818 * the frag list, if such a thing exists. We'd probably need to recurse to
1819 * handle that cleanly.
1821 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1822 struct pipe_inode_info *pipe, unsigned int tlen,
1823 unsigned int flags)
1825 struct partial_page partial[MAX_SKB_FRAGS];
1826 struct page *pages[MAX_SKB_FRAGS];
1827 struct splice_pipe_desc spd = {
1828 .pages = pages,
1829 .partial = partial,
1830 .nr_pages_max = MAX_SKB_FRAGS,
1831 .flags = flags,
1832 .ops = &nosteal_pipe_buf_ops,
1833 .spd_release = sock_spd_release,
1835 struct sk_buff *frag_iter;
1836 struct sock *sk = skb->sk;
1837 int ret = 0;
1840 * __skb_splice_bits() only fails if the output has no room left,
1841 * so no point in going over the frag_list for the error case.
1843 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1844 goto done;
1845 else if (!tlen)
1846 goto done;
1849 * now see if we have a frag_list to map
1851 skb_walk_frags(skb, frag_iter) {
1852 if (!tlen)
1853 break;
1854 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1855 break;
1858 done:
1859 if (spd.nr_pages) {
1861 * Drop the socket lock, otherwise we have reverse
1862 * locking dependencies between sk_lock and i_mutex
1863 * here as compared to sendfile(). We enter here
1864 * with the socket lock held, and splice_to_pipe() will
1865 * grab the pipe inode lock. For sendfile() emulation,
1866 * we call into ->sendpage() with the i_mutex lock held
1867 * and networking will grab the socket lock.
1869 release_sock(sk);
1870 ret = splice_to_pipe(pipe, &spd);
1871 lock_sock(sk);
1874 return ret;
1878 * skb_store_bits - store bits from kernel buffer to skb
1879 * @skb: destination buffer
1880 * @offset: offset in destination
1881 * @from: source buffer
1882 * @len: number of bytes to copy
1884 * Copy the specified number of bytes from the source buffer to the
1885 * destination skb. This function handles all the messy bits of
1886 * traversing fragment lists and such.
1889 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1891 int start = skb_headlen(skb);
1892 struct sk_buff *frag_iter;
1893 int i, copy;
1895 if (offset > (int)skb->len - len)
1896 goto fault;
1898 if ((copy = start - offset) > 0) {
1899 if (copy > len)
1900 copy = len;
1901 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1902 if ((len -= copy) == 0)
1903 return 0;
1904 offset += copy;
1905 from += copy;
1908 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1909 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1910 int end;
1912 WARN_ON(start > offset + len);
1914 end = start + skb_frag_size(frag);
1915 if ((copy = end - offset) > 0) {
1916 u8 *vaddr;
1918 if (copy > len)
1919 copy = len;
1921 vaddr = kmap_atomic(skb_frag_page(frag));
1922 memcpy(vaddr + frag->page_offset + offset - start,
1923 from, copy);
1924 kunmap_atomic(vaddr);
1926 if ((len -= copy) == 0)
1927 return 0;
1928 offset += copy;
1929 from += copy;
1931 start = end;
1934 skb_walk_frags(skb, frag_iter) {
1935 int end;
1937 WARN_ON(start > offset + len);
1939 end = start + frag_iter->len;
1940 if ((copy = end - offset) > 0) {
1941 if (copy > len)
1942 copy = len;
1943 if (skb_store_bits(frag_iter, offset - start,
1944 from, copy))
1945 goto fault;
1946 if ((len -= copy) == 0)
1947 return 0;
1948 offset += copy;
1949 from += copy;
1951 start = end;
1953 if (!len)
1954 return 0;
1956 fault:
1957 return -EFAULT;
1959 EXPORT_SYMBOL(skb_store_bits);
1961 /* Checksum skb data. */
1962 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1963 __wsum csum, const struct skb_checksum_ops *ops)
1965 int start = skb_headlen(skb);
1966 int i, copy = start - offset;
1967 struct sk_buff *frag_iter;
1968 int pos = 0;
1970 /* Checksum header. */
1971 if (copy > 0) {
1972 if (copy > len)
1973 copy = len;
1974 csum = ops->update(skb->data + offset, copy, csum);
1975 if ((len -= copy) == 0)
1976 return csum;
1977 offset += copy;
1978 pos = copy;
1981 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1982 int end;
1983 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1985 WARN_ON(start > offset + len);
1987 end = start + skb_frag_size(frag);
1988 if ((copy = end - offset) > 0) {
1989 __wsum csum2;
1990 u8 *vaddr;
1992 if (copy > len)
1993 copy = len;
1994 vaddr = kmap_atomic(skb_frag_page(frag));
1995 csum2 = ops->update(vaddr + frag->page_offset +
1996 offset - start, copy, 0);
1997 kunmap_atomic(vaddr);
1998 csum = ops->combine(csum, csum2, pos, copy);
1999 if (!(len -= copy))
2000 return csum;
2001 offset += copy;
2002 pos += copy;
2004 start = end;
2007 skb_walk_frags(skb, frag_iter) {
2008 int end;
2010 WARN_ON(start > offset + len);
2012 end = start + frag_iter->len;
2013 if ((copy = end - offset) > 0) {
2014 __wsum csum2;
2015 if (copy > len)
2016 copy = len;
2017 csum2 = __skb_checksum(frag_iter, offset - start,
2018 copy, 0, ops);
2019 csum = ops->combine(csum, csum2, pos, copy);
2020 if ((len -= copy) == 0)
2021 return csum;
2022 offset += copy;
2023 pos += copy;
2025 start = end;
2027 BUG_ON(len);
2029 return csum;
2031 EXPORT_SYMBOL(__skb_checksum);
2033 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2034 int len, __wsum csum)
2036 const struct skb_checksum_ops ops = {
2037 .update = csum_partial_ext,
2038 .combine = csum_block_add_ext,
2041 return __skb_checksum(skb, offset, len, csum, &ops);
2043 EXPORT_SYMBOL(skb_checksum);
2045 /* Both of above in one bottle. */
2047 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2048 u8 *to, int len, __wsum csum)
2050 int start = skb_headlen(skb);
2051 int i, copy = start - offset;
2052 struct sk_buff *frag_iter;
2053 int pos = 0;
2055 /* Copy header. */
2056 if (copy > 0) {
2057 if (copy > len)
2058 copy = len;
2059 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2060 copy, csum);
2061 if ((len -= copy) == 0)
2062 return csum;
2063 offset += copy;
2064 to += copy;
2065 pos = copy;
2068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2069 int end;
2071 WARN_ON(start > offset + len);
2073 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2074 if ((copy = end - offset) > 0) {
2075 __wsum csum2;
2076 u8 *vaddr;
2077 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2079 if (copy > len)
2080 copy = len;
2081 vaddr = kmap_atomic(skb_frag_page(frag));
2082 csum2 = csum_partial_copy_nocheck(vaddr +
2083 frag->page_offset +
2084 offset - start, to,
2085 copy, 0);
2086 kunmap_atomic(vaddr);
2087 csum = csum_block_add(csum, csum2, pos);
2088 if (!(len -= copy))
2089 return csum;
2090 offset += copy;
2091 to += copy;
2092 pos += copy;
2094 start = end;
2097 skb_walk_frags(skb, frag_iter) {
2098 __wsum csum2;
2099 int end;
2101 WARN_ON(start > offset + len);
2103 end = start + frag_iter->len;
2104 if ((copy = end - offset) > 0) {
2105 if (copy > len)
2106 copy = len;
2107 csum2 = skb_copy_and_csum_bits(frag_iter,
2108 offset - start,
2109 to, copy, 0);
2110 csum = csum_block_add(csum, csum2, pos);
2111 if ((len -= copy) == 0)
2112 return csum;
2113 offset += copy;
2114 to += copy;
2115 pos += copy;
2117 start = end;
2119 BUG_ON(len);
2120 return csum;
2122 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2125 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2126 * @from: source buffer
2128 * Calculates the amount of linear headroom needed in the 'to' skb passed
2129 * into skb_zerocopy().
2131 unsigned int
2132 skb_zerocopy_headlen(const struct sk_buff *from)
2134 unsigned int hlen = 0;
2136 if (!from->head_frag ||
2137 skb_headlen(from) < L1_CACHE_BYTES ||
2138 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2139 hlen = skb_headlen(from);
2141 if (skb_has_frag_list(from))
2142 hlen = from->len;
2144 return hlen;
2146 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2149 * skb_zerocopy - Zero copy skb to skb
2150 * @to: destination buffer
2151 * @from: source buffer
2152 * @len: number of bytes to copy from source buffer
2153 * @hlen: size of linear headroom in destination buffer
2155 * Copies up to `len` bytes from `from` to `to` by creating references
2156 * to the frags in the source buffer.
2158 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2159 * headroom in the `to` buffer.
2161 * Return value:
2162 * 0: everything is OK
2163 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2164 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2167 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2169 int i, j = 0;
2170 int plen = 0; /* length of skb->head fragment */
2171 int ret;
2172 struct page *page;
2173 unsigned int offset;
2175 BUG_ON(!from->head_frag && !hlen);
2177 /* dont bother with small payloads */
2178 if (len <= skb_tailroom(to))
2179 return skb_copy_bits(from, 0, skb_put(to, len), len);
2181 if (hlen) {
2182 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2183 if (unlikely(ret))
2184 return ret;
2185 len -= hlen;
2186 } else {
2187 plen = min_t(int, skb_headlen(from), len);
2188 if (plen) {
2189 page = virt_to_head_page(from->head);
2190 offset = from->data - (unsigned char *)page_address(page);
2191 __skb_fill_page_desc(to, 0, page, offset, plen);
2192 get_page(page);
2193 j = 1;
2194 len -= plen;
2198 to->truesize += len + plen;
2199 to->len += len + plen;
2200 to->data_len += len + plen;
2202 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2203 skb_tx_error(from);
2204 return -ENOMEM;
2207 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2208 if (!len)
2209 break;
2210 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2211 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2212 len -= skb_shinfo(to)->frags[j].size;
2213 skb_frag_ref(to, j);
2214 j++;
2216 skb_shinfo(to)->nr_frags = j;
2218 return 0;
2220 EXPORT_SYMBOL_GPL(skb_zerocopy);
2222 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2224 __wsum csum;
2225 long csstart;
2227 if (skb->ip_summed == CHECKSUM_PARTIAL)
2228 csstart = skb_checksum_start_offset(skb);
2229 else
2230 csstart = skb_headlen(skb);
2232 BUG_ON(csstart > skb_headlen(skb));
2234 skb_copy_from_linear_data(skb, to, csstart);
2236 csum = 0;
2237 if (csstart != skb->len)
2238 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2239 skb->len - csstart, 0);
2241 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2242 long csstuff = csstart + skb->csum_offset;
2244 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2247 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2250 * skb_dequeue - remove from the head of the queue
2251 * @list: list to dequeue from
2253 * Remove the head of the list. The list lock is taken so the function
2254 * may be used safely with other locking list functions. The head item is
2255 * returned or %NULL if the list is empty.
2258 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2260 unsigned long flags;
2261 struct sk_buff *result;
2263 spin_lock_irqsave(&list->lock, flags);
2264 result = __skb_dequeue(list);
2265 spin_unlock_irqrestore(&list->lock, flags);
2266 return result;
2268 EXPORT_SYMBOL(skb_dequeue);
2271 * skb_dequeue_tail - remove from the tail of the queue
2272 * @list: list to dequeue from
2274 * Remove the tail of the list. The list lock is taken so the function
2275 * may be used safely with other locking list functions. The tail item is
2276 * returned or %NULL if the list is empty.
2278 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2280 unsigned long flags;
2281 struct sk_buff *result;
2283 spin_lock_irqsave(&list->lock, flags);
2284 result = __skb_dequeue_tail(list);
2285 spin_unlock_irqrestore(&list->lock, flags);
2286 return result;
2288 EXPORT_SYMBOL(skb_dequeue_tail);
2291 * skb_queue_purge - empty a list
2292 * @list: list to empty
2294 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2295 * the list and one reference dropped. This function takes the list
2296 * lock and is atomic with respect to other list locking functions.
2298 void skb_queue_purge(struct sk_buff_head *list)
2300 struct sk_buff *skb;
2301 while ((skb = skb_dequeue(list)) != NULL)
2302 kfree_skb(skb);
2304 EXPORT_SYMBOL(skb_queue_purge);
2307 * skb_queue_head - queue a buffer at the list head
2308 * @list: list to use
2309 * @newsk: buffer to queue
2311 * Queue a buffer at the start of the list. This function takes the
2312 * list lock and can be used safely with other locking &sk_buff functions
2313 * safely.
2315 * A buffer cannot be placed on two lists at the same time.
2317 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2319 unsigned long flags;
2321 spin_lock_irqsave(&list->lock, flags);
2322 __skb_queue_head(list, newsk);
2323 spin_unlock_irqrestore(&list->lock, flags);
2325 EXPORT_SYMBOL(skb_queue_head);
2328 * skb_queue_tail - queue a buffer at the list tail
2329 * @list: list to use
2330 * @newsk: buffer to queue
2332 * Queue a buffer at the tail of the list. This function takes the
2333 * list lock and can be used safely with other locking &sk_buff functions
2334 * safely.
2336 * A buffer cannot be placed on two lists at the same time.
2338 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2340 unsigned long flags;
2342 spin_lock_irqsave(&list->lock, flags);
2343 __skb_queue_tail(list, newsk);
2344 spin_unlock_irqrestore(&list->lock, flags);
2346 EXPORT_SYMBOL(skb_queue_tail);
2349 * skb_unlink - remove a buffer from a list
2350 * @skb: buffer to remove
2351 * @list: list to use
2353 * Remove a packet from a list. The list locks are taken and this
2354 * function is atomic with respect to other list locked calls
2356 * You must know what list the SKB is on.
2358 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2360 unsigned long flags;
2362 spin_lock_irqsave(&list->lock, flags);
2363 __skb_unlink(skb, list);
2364 spin_unlock_irqrestore(&list->lock, flags);
2366 EXPORT_SYMBOL(skb_unlink);
2369 * skb_append - append a buffer
2370 * @old: buffer to insert after
2371 * @newsk: buffer to insert
2372 * @list: list to use
2374 * Place a packet after a given packet in a list. The list locks are taken
2375 * and this function is atomic with respect to other list locked calls.
2376 * A buffer cannot be placed on two lists at the same time.
2378 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2380 unsigned long flags;
2382 spin_lock_irqsave(&list->lock, flags);
2383 __skb_queue_after(list, old, newsk);
2384 spin_unlock_irqrestore(&list->lock, flags);
2386 EXPORT_SYMBOL(skb_append);
2389 * skb_insert - insert a buffer
2390 * @old: buffer to insert before
2391 * @newsk: buffer to insert
2392 * @list: list to use
2394 * Place a packet before a given packet in a list. The list locks are
2395 * taken and this function is atomic with respect to other list locked
2396 * calls.
2398 * A buffer cannot be placed on two lists at the same time.
2400 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2402 unsigned long flags;
2404 spin_lock_irqsave(&list->lock, flags);
2405 __skb_insert(newsk, old->prev, old, list);
2406 spin_unlock_irqrestore(&list->lock, flags);
2408 EXPORT_SYMBOL(skb_insert);
2410 static inline void skb_split_inside_header(struct sk_buff *skb,
2411 struct sk_buff* skb1,
2412 const u32 len, const int pos)
2414 int i;
2416 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2417 pos - len);
2418 /* And move data appendix as is. */
2419 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2420 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2422 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2423 skb_shinfo(skb)->nr_frags = 0;
2424 skb1->data_len = skb->data_len;
2425 skb1->len += skb1->data_len;
2426 skb->data_len = 0;
2427 skb->len = len;
2428 skb_set_tail_pointer(skb, len);
2431 static inline void skb_split_no_header(struct sk_buff *skb,
2432 struct sk_buff* skb1,
2433 const u32 len, int pos)
2435 int i, k = 0;
2436 const int nfrags = skb_shinfo(skb)->nr_frags;
2438 skb_shinfo(skb)->nr_frags = 0;
2439 skb1->len = skb1->data_len = skb->len - len;
2440 skb->len = len;
2441 skb->data_len = len - pos;
2443 for (i = 0; i < nfrags; i++) {
2444 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2446 if (pos + size > len) {
2447 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2449 if (pos < len) {
2450 /* Split frag.
2451 * We have two variants in this case:
2452 * 1. Move all the frag to the second
2453 * part, if it is possible. F.e.
2454 * this approach is mandatory for TUX,
2455 * where splitting is expensive.
2456 * 2. Split is accurately. We make this.
2458 skb_frag_ref(skb, i);
2459 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2460 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2461 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2462 skb_shinfo(skb)->nr_frags++;
2464 k++;
2465 } else
2466 skb_shinfo(skb)->nr_frags++;
2467 pos += size;
2469 skb_shinfo(skb1)->nr_frags = k;
2473 * skb_split - Split fragmented skb to two parts at length len.
2474 * @skb: the buffer to split
2475 * @skb1: the buffer to receive the second part
2476 * @len: new length for skb
2478 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2480 int pos = skb_headlen(skb);
2482 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2483 if (len < pos) /* Split line is inside header. */
2484 skb_split_inside_header(skb, skb1, len, pos);
2485 else /* Second chunk has no header, nothing to copy. */
2486 skb_split_no_header(skb, skb1, len, pos);
2488 EXPORT_SYMBOL(skb_split);
2490 /* Shifting from/to a cloned skb is a no-go.
2492 * Caller cannot keep skb_shinfo related pointers past calling here!
2494 static int skb_prepare_for_shift(struct sk_buff *skb)
2496 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2500 * skb_shift - Shifts paged data partially from skb to another
2501 * @tgt: buffer into which tail data gets added
2502 * @skb: buffer from which the paged data comes from
2503 * @shiftlen: shift up to this many bytes
2505 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2506 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2507 * It's up to caller to free skb if everything was shifted.
2509 * If @tgt runs out of frags, the whole operation is aborted.
2511 * Skb cannot include anything else but paged data while tgt is allowed
2512 * to have non-paged data as well.
2514 * TODO: full sized shift could be optimized but that would need
2515 * specialized skb free'er to handle frags without up-to-date nr_frags.
2517 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2519 int from, to, merge, todo;
2520 struct skb_frag_struct *fragfrom, *fragto;
2522 BUG_ON(shiftlen > skb->len);
2523 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2525 todo = shiftlen;
2526 from = 0;
2527 to = skb_shinfo(tgt)->nr_frags;
2528 fragfrom = &skb_shinfo(skb)->frags[from];
2530 /* Actual merge is delayed until the point when we know we can
2531 * commit all, so that we don't have to undo partial changes
2533 if (!to ||
2534 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2535 fragfrom->page_offset)) {
2536 merge = -1;
2537 } else {
2538 merge = to - 1;
2540 todo -= skb_frag_size(fragfrom);
2541 if (todo < 0) {
2542 if (skb_prepare_for_shift(skb) ||
2543 skb_prepare_for_shift(tgt))
2544 return 0;
2546 /* All previous frag pointers might be stale! */
2547 fragfrom = &skb_shinfo(skb)->frags[from];
2548 fragto = &skb_shinfo(tgt)->frags[merge];
2550 skb_frag_size_add(fragto, shiftlen);
2551 skb_frag_size_sub(fragfrom, shiftlen);
2552 fragfrom->page_offset += shiftlen;
2554 goto onlymerged;
2557 from++;
2560 /* Skip full, not-fitting skb to avoid expensive operations */
2561 if ((shiftlen == skb->len) &&
2562 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2563 return 0;
2565 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2566 return 0;
2568 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2569 if (to == MAX_SKB_FRAGS)
2570 return 0;
2572 fragfrom = &skb_shinfo(skb)->frags[from];
2573 fragto = &skb_shinfo(tgt)->frags[to];
2575 if (todo >= skb_frag_size(fragfrom)) {
2576 *fragto = *fragfrom;
2577 todo -= skb_frag_size(fragfrom);
2578 from++;
2579 to++;
2581 } else {
2582 __skb_frag_ref(fragfrom);
2583 fragto->page = fragfrom->page;
2584 fragto->page_offset = fragfrom->page_offset;
2585 skb_frag_size_set(fragto, todo);
2587 fragfrom->page_offset += todo;
2588 skb_frag_size_sub(fragfrom, todo);
2589 todo = 0;
2591 to++;
2592 break;
2596 /* Ready to "commit" this state change to tgt */
2597 skb_shinfo(tgt)->nr_frags = to;
2599 if (merge >= 0) {
2600 fragfrom = &skb_shinfo(skb)->frags[0];
2601 fragto = &skb_shinfo(tgt)->frags[merge];
2603 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2604 __skb_frag_unref(fragfrom);
2607 /* Reposition in the original skb */
2608 to = 0;
2609 while (from < skb_shinfo(skb)->nr_frags)
2610 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2611 skb_shinfo(skb)->nr_frags = to;
2613 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2615 onlymerged:
2616 /* Most likely the tgt won't ever need its checksum anymore, skb on
2617 * the other hand might need it if it needs to be resent
2619 tgt->ip_summed = CHECKSUM_PARTIAL;
2620 skb->ip_summed = CHECKSUM_PARTIAL;
2622 /* Yak, is it really working this way? Some helper please? */
2623 skb->len -= shiftlen;
2624 skb->data_len -= shiftlen;
2625 skb->truesize -= shiftlen;
2626 tgt->len += shiftlen;
2627 tgt->data_len += shiftlen;
2628 tgt->truesize += shiftlen;
2630 return shiftlen;
2634 * skb_prepare_seq_read - Prepare a sequential read of skb data
2635 * @skb: the buffer to read
2636 * @from: lower offset of data to be read
2637 * @to: upper offset of data to be read
2638 * @st: state variable
2640 * Initializes the specified state variable. Must be called before
2641 * invoking skb_seq_read() for the first time.
2643 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2644 unsigned int to, struct skb_seq_state *st)
2646 st->lower_offset = from;
2647 st->upper_offset = to;
2648 st->root_skb = st->cur_skb = skb;
2649 st->frag_idx = st->stepped_offset = 0;
2650 st->frag_data = NULL;
2652 EXPORT_SYMBOL(skb_prepare_seq_read);
2655 * skb_seq_read - Sequentially read skb data
2656 * @consumed: number of bytes consumed by the caller so far
2657 * @data: destination pointer for data to be returned
2658 * @st: state variable
2660 * Reads a block of skb data at @consumed relative to the
2661 * lower offset specified to skb_prepare_seq_read(). Assigns
2662 * the head of the data block to @data and returns the length
2663 * of the block or 0 if the end of the skb data or the upper
2664 * offset has been reached.
2666 * The caller is not required to consume all of the data
2667 * returned, i.e. @consumed is typically set to the number
2668 * of bytes already consumed and the next call to
2669 * skb_seq_read() will return the remaining part of the block.
2671 * Note 1: The size of each block of data returned can be arbitrary,
2672 * this limitation is the cost for zerocopy seqeuental
2673 * reads of potentially non linear data.
2675 * Note 2: Fragment lists within fragments are not implemented
2676 * at the moment, state->root_skb could be replaced with
2677 * a stack for this purpose.
2679 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2680 struct skb_seq_state *st)
2682 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2683 skb_frag_t *frag;
2685 if (unlikely(abs_offset >= st->upper_offset)) {
2686 if (st->frag_data) {
2687 kunmap_atomic(st->frag_data);
2688 st->frag_data = NULL;
2690 return 0;
2693 next_skb:
2694 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2696 if (abs_offset < block_limit && !st->frag_data) {
2697 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2698 return block_limit - abs_offset;
2701 if (st->frag_idx == 0 && !st->frag_data)
2702 st->stepped_offset += skb_headlen(st->cur_skb);
2704 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2705 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2706 block_limit = skb_frag_size(frag) + st->stepped_offset;
2708 if (abs_offset < block_limit) {
2709 if (!st->frag_data)
2710 st->frag_data = kmap_atomic(skb_frag_page(frag));
2712 *data = (u8 *) st->frag_data + frag->page_offset +
2713 (abs_offset - st->stepped_offset);
2715 return block_limit - abs_offset;
2718 if (st->frag_data) {
2719 kunmap_atomic(st->frag_data);
2720 st->frag_data = NULL;
2723 st->frag_idx++;
2724 st->stepped_offset += skb_frag_size(frag);
2727 if (st->frag_data) {
2728 kunmap_atomic(st->frag_data);
2729 st->frag_data = NULL;
2732 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2733 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2734 st->frag_idx = 0;
2735 goto next_skb;
2736 } else if (st->cur_skb->next) {
2737 st->cur_skb = st->cur_skb->next;
2738 st->frag_idx = 0;
2739 goto next_skb;
2742 return 0;
2744 EXPORT_SYMBOL(skb_seq_read);
2747 * skb_abort_seq_read - Abort a sequential read of skb data
2748 * @st: state variable
2750 * Must be called if skb_seq_read() was not called until it
2751 * returned 0.
2753 void skb_abort_seq_read(struct skb_seq_state *st)
2755 if (st->frag_data)
2756 kunmap_atomic(st->frag_data);
2758 EXPORT_SYMBOL(skb_abort_seq_read);
2760 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2762 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2763 struct ts_config *conf,
2764 struct ts_state *state)
2766 return skb_seq_read(offset, text, TS_SKB_CB(state));
2769 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2771 skb_abort_seq_read(TS_SKB_CB(state));
2775 * skb_find_text - Find a text pattern in skb data
2776 * @skb: the buffer to look in
2777 * @from: search offset
2778 * @to: search limit
2779 * @config: textsearch configuration
2780 * @state: uninitialized textsearch state variable
2782 * Finds a pattern in the skb data according to the specified
2783 * textsearch configuration. Use textsearch_next() to retrieve
2784 * subsequent occurrences of the pattern. Returns the offset
2785 * to the first occurrence or UINT_MAX if no match was found.
2787 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2788 unsigned int to, struct ts_config *config,
2789 struct ts_state *state)
2791 unsigned int ret;
2793 config->get_next_block = skb_ts_get_next_block;
2794 config->finish = skb_ts_finish;
2796 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2798 ret = textsearch_find(config, state);
2799 return (ret <= to - from ? ret : UINT_MAX);
2801 EXPORT_SYMBOL(skb_find_text);
2804 * skb_append_datato_frags - append the user data to a skb
2805 * @sk: sock structure
2806 * @skb: skb structure to be appened with user data.
2807 * @getfrag: call back function to be used for getting the user data
2808 * @from: pointer to user message iov
2809 * @length: length of the iov message
2811 * Description: This procedure append the user data in the fragment part
2812 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2814 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2815 int (*getfrag)(void *from, char *to, int offset,
2816 int len, int odd, struct sk_buff *skb),
2817 void *from, int length)
2819 int frg_cnt = skb_shinfo(skb)->nr_frags;
2820 int copy;
2821 int offset = 0;
2822 int ret;
2823 struct page_frag *pfrag = &current->task_frag;
2825 do {
2826 /* Return error if we don't have space for new frag */
2827 if (frg_cnt >= MAX_SKB_FRAGS)
2828 return -EMSGSIZE;
2830 if (!sk_page_frag_refill(sk, pfrag))
2831 return -ENOMEM;
2833 /* copy the user data to page */
2834 copy = min_t(int, length, pfrag->size - pfrag->offset);
2836 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2837 offset, copy, 0, skb);
2838 if (ret < 0)
2839 return -EFAULT;
2841 /* copy was successful so update the size parameters */
2842 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2843 copy);
2844 frg_cnt++;
2845 pfrag->offset += copy;
2846 get_page(pfrag->page);
2848 skb->truesize += copy;
2849 atomic_add(copy, &sk->sk_wmem_alloc);
2850 skb->len += copy;
2851 skb->data_len += copy;
2852 offset += copy;
2853 length -= copy;
2855 } while (length > 0);
2857 return 0;
2859 EXPORT_SYMBOL(skb_append_datato_frags);
2862 * skb_pull_rcsum - pull skb and update receive checksum
2863 * @skb: buffer to update
2864 * @len: length of data pulled
2866 * This function performs an skb_pull on the packet and updates
2867 * the CHECKSUM_COMPLETE checksum. It should be used on
2868 * receive path processing instead of skb_pull unless you know
2869 * that the checksum difference is zero (e.g., a valid IP header)
2870 * or you are setting ip_summed to CHECKSUM_NONE.
2872 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2874 unsigned char *data = skb->data;
2876 BUG_ON(len > skb->len);
2877 __skb_pull(skb, len);
2878 skb_postpull_rcsum(skb, data, len);
2879 return skb->data;
2881 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2884 * skb_segment - Perform protocol segmentation on skb.
2885 * @head_skb: buffer to segment
2886 * @features: features for the output path (see dev->features)
2888 * This function performs segmentation on the given skb. It returns
2889 * a pointer to the first in a list of new skbs for the segments.
2890 * In case of error it returns ERR_PTR(err).
2892 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2893 netdev_features_t features)
2895 struct sk_buff *segs = NULL;
2896 struct sk_buff *tail = NULL;
2897 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2898 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2899 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2900 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2901 struct sk_buff *frag_skb = head_skb;
2902 unsigned int offset = doffset;
2903 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2904 unsigned int headroom;
2905 unsigned int len;
2906 __be16 proto;
2907 bool csum;
2908 int sg = !!(features & NETIF_F_SG);
2909 int nfrags = skb_shinfo(head_skb)->nr_frags;
2910 int err = -ENOMEM;
2911 int i = 0;
2912 int pos;
2913 int dummy;
2915 __skb_push(head_skb, doffset);
2916 proto = skb_network_protocol(head_skb, &dummy);
2917 if (unlikely(!proto))
2918 return ERR_PTR(-EINVAL);
2920 csum = !head_skb->encap_hdr_csum &&
2921 !!can_checksum_protocol(features, proto);
2923 headroom = skb_headroom(head_skb);
2924 pos = skb_headlen(head_skb);
2926 do {
2927 struct sk_buff *nskb;
2928 skb_frag_t *nskb_frag;
2929 int hsize;
2930 int size;
2932 len = head_skb->len - offset;
2933 if (len > mss)
2934 len = mss;
2936 hsize = skb_headlen(head_skb) - offset;
2937 if (hsize < 0)
2938 hsize = 0;
2939 if (hsize > len || !sg)
2940 hsize = len;
2942 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2943 (skb_headlen(list_skb) == len || sg)) {
2944 BUG_ON(skb_headlen(list_skb) > len);
2946 i = 0;
2947 nfrags = skb_shinfo(list_skb)->nr_frags;
2948 frag = skb_shinfo(list_skb)->frags;
2949 frag_skb = list_skb;
2950 pos += skb_headlen(list_skb);
2952 while (pos < offset + len) {
2953 BUG_ON(i >= nfrags);
2955 size = skb_frag_size(frag);
2956 if (pos + size > offset + len)
2957 break;
2959 i++;
2960 pos += size;
2961 frag++;
2964 nskb = skb_clone(list_skb, GFP_ATOMIC);
2965 list_skb = list_skb->next;
2967 if (unlikely(!nskb))
2968 goto err;
2970 if (unlikely(pskb_trim(nskb, len))) {
2971 kfree_skb(nskb);
2972 goto err;
2975 hsize = skb_end_offset(nskb);
2976 if (skb_cow_head(nskb, doffset + headroom)) {
2977 kfree_skb(nskb);
2978 goto err;
2981 nskb->truesize += skb_end_offset(nskb) - hsize;
2982 skb_release_head_state(nskb);
2983 __skb_push(nskb, doffset);
2984 } else {
2985 nskb = __alloc_skb(hsize + doffset + headroom,
2986 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
2987 NUMA_NO_NODE);
2989 if (unlikely(!nskb))
2990 goto err;
2992 skb_reserve(nskb, headroom);
2993 __skb_put(nskb, doffset);
2996 if (segs)
2997 tail->next = nskb;
2998 else
2999 segs = nskb;
3000 tail = nskb;
3002 __copy_skb_header(nskb, head_skb);
3004 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3005 skb_reset_mac_len(nskb);
3007 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3008 nskb->data - tnl_hlen,
3009 doffset + tnl_hlen);
3011 if (nskb->len == len + doffset)
3012 goto perform_csum_check;
3014 if (!sg) {
3015 nskb->ip_summed = CHECKSUM_NONE;
3016 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3017 skb_put(nskb, len),
3018 len, 0);
3019 SKB_GSO_CB(nskb)->csum_start =
3020 skb_headroom(nskb) + doffset;
3021 continue;
3024 nskb_frag = skb_shinfo(nskb)->frags;
3026 skb_copy_from_linear_data_offset(head_skb, offset,
3027 skb_put(nskb, hsize), hsize);
3029 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3030 SKBTX_SHARED_FRAG;
3032 while (pos < offset + len) {
3033 if (i >= nfrags) {
3034 BUG_ON(skb_headlen(list_skb));
3036 i = 0;
3037 nfrags = skb_shinfo(list_skb)->nr_frags;
3038 frag = skb_shinfo(list_skb)->frags;
3039 frag_skb = list_skb;
3041 BUG_ON(!nfrags);
3043 list_skb = list_skb->next;
3046 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3047 MAX_SKB_FRAGS)) {
3048 net_warn_ratelimited(
3049 "skb_segment: too many frags: %u %u\n",
3050 pos, mss);
3051 goto err;
3054 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3055 goto err;
3057 *nskb_frag = *frag;
3058 __skb_frag_ref(nskb_frag);
3059 size = skb_frag_size(nskb_frag);
3061 if (pos < offset) {
3062 nskb_frag->page_offset += offset - pos;
3063 skb_frag_size_sub(nskb_frag, offset - pos);
3066 skb_shinfo(nskb)->nr_frags++;
3068 if (pos + size <= offset + len) {
3069 i++;
3070 frag++;
3071 pos += size;
3072 } else {
3073 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3074 goto skip_fraglist;
3077 nskb_frag++;
3080 skip_fraglist:
3081 nskb->data_len = len - hsize;
3082 nskb->len += nskb->data_len;
3083 nskb->truesize += nskb->data_len;
3085 perform_csum_check:
3086 if (!csum) {
3087 nskb->csum = skb_checksum(nskb, doffset,
3088 nskb->len - doffset, 0);
3089 nskb->ip_summed = CHECKSUM_NONE;
3090 SKB_GSO_CB(nskb)->csum_start =
3091 skb_headroom(nskb) + doffset;
3093 } while ((offset += len) < head_skb->len);
3095 return segs;
3097 err:
3098 kfree_skb_list(segs);
3099 return ERR_PTR(err);
3101 EXPORT_SYMBOL_GPL(skb_segment);
3103 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3105 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3106 unsigned int offset = skb_gro_offset(skb);
3107 unsigned int headlen = skb_headlen(skb);
3108 struct sk_buff *nskb, *lp, *p = *head;
3109 unsigned int len = skb_gro_len(skb);
3110 unsigned int delta_truesize;
3111 unsigned int headroom;
3113 if (unlikely(p->len + len >= 65536))
3114 return -E2BIG;
3116 lp = NAPI_GRO_CB(p)->last;
3117 pinfo = skb_shinfo(lp);
3119 if (headlen <= offset) {
3120 skb_frag_t *frag;
3121 skb_frag_t *frag2;
3122 int i = skbinfo->nr_frags;
3123 int nr_frags = pinfo->nr_frags + i;
3125 if (nr_frags > MAX_SKB_FRAGS)
3126 goto merge;
3128 offset -= headlen;
3129 pinfo->nr_frags = nr_frags;
3130 skbinfo->nr_frags = 0;
3132 frag = pinfo->frags + nr_frags;
3133 frag2 = skbinfo->frags + i;
3134 do {
3135 *--frag = *--frag2;
3136 } while (--i);
3138 frag->page_offset += offset;
3139 skb_frag_size_sub(frag, offset);
3141 /* all fragments truesize : remove (head size + sk_buff) */
3142 delta_truesize = skb->truesize -
3143 SKB_TRUESIZE(skb_end_offset(skb));
3145 skb->truesize -= skb->data_len;
3146 skb->len -= skb->data_len;
3147 skb->data_len = 0;
3149 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3150 goto done;
3151 } else if (skb->head_frag) {
3152 int nr_frags = pinfo->nr_frags;
3153 skb_frag_t *frag = pinfo->frags + nr_frags;
3154 struct page *page = virt_to_head_page(skb->head);
3155 unsigned int first_size = headlen - offset;
3156 unsigned int first_offset;
3158 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3159 goto merge;
3161 first_offset = skb->data -
3162 (unsigned char *)page_address(page) +
3163 offset;
3165 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3167 frag->page.p = page;
3168 frag->page_offset = first_offset;
3169 skb_frag_size_set(frag, first_size);
3171 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3172 /* We dont need to clear skbinfo->nr_frags here */
3174 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3175 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3176 goto done;
3178 /* switch back to head shinfo */
3179 pinfo = skb_shinfo(p);
3181 if (pinfo->frag_list)
3182 goto merge;
3183 if (skb_gro_len(p) != pinfo->gso_size)
3184 return -E2BIG;
3186 headroom = skb_headroom(p);
3187 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3188 if (unlikely(!nskb))
3189 return -ENOMEM;
3191 __copy_skb_header(nskb, p);
3192 nskb->mac_len = p->mac_len;
3194 skb_reserve(nskb, headroom);
3195 __skb_put(nskb, skb_gro_offset(p));
3197 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3198 skb_set_network_header(nskb, skb_network_offset(p));
3199 skb_set_transport_header(nskb, skb_transport_offset(p));
3201 __skb_pull(p, skb_gro_offset(p));
3202 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3203 p->data - skb_mac_header(p));
3205 skb_shinfo(nskb)->frag_list = p;
3206 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3207 pinfo->gso_size = 0;
3208 skb_header_release(p);
3209 NAPI_GRO_CB(nskb)->last = p;
3211 nskb->data_len += p->len;
3212 nskb->truesize += p->truesize;
3213 nskb->len += p->len;
3215 *head = nskb;
3216 nskb->next = p->next;
3217 p->next = NULL;
3219 p = nskb;
3221 merge:
3222 delta_truesize = skb->truesize;
3223 if (offset > headlen) {
3224 unsigned int eat = offset - headlen;
3226 skbinfo->frags[0].page_offset += eat;
3227 skb_frag_size_sub(&skbinfo->frags[0], eat);
3228 skb->data_len -= eat;
3229 skb->len -= eat;
3230 offset = headlen;
3233 __skb_pull(skb, offset);
3235 if (NAPI_GRO_CB(p)->last == p)
3236 skb_shinfo(p)->frag_list = skb;
3237 else
3238 NAPI_GRO_CB(p)->last->next = skb;
3239 NAPI_GRO_CB(p)->last = skb;
3240 skb_header_release(skb);
3241 lp = p;
3243 done:
3244 NAPI_GRO_CB(p)->count++;
3245 p->data_len += len;
3246 p->truesize += delta_truesize;
3247 p->len += len;
3248 if (lp != p) {
3249 lp->data_len += len;
3250 lp->truesize += delta_truesize;
3251 lp->len += len;
3253 NAPI_GRO_CB(skb)->same_flow = 1;
3254 return 0;
3256 EXPORT_SYMBOL_GPL(skb_gro_receive);
3258 void __init skb_init(void)
3260 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3261 sizeof(struct sk_buff),
3263 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3264 NULL);
3265 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3266 (2*sizeof(struct sk_buff)) +
3267 sizeof(atomic_t),
3269 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3270 NULL);
3274 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3275 * @skb: Socket buffer containing the buffers to be mapped
3276 * @sg: The scatter-gather list to map into
3277 * @offset: The offset into the buffer's contents to start mapping
3278 * @len: Length of buffer space to be mapped
3280 * Fill the specified scatter-gather list with mappings/pointers into a
3281 * region of the buffer space attached to a socket buffer.
3283 static int
3284 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3286 int start = skb_headlen(skb);
3287 int i, copy = start - offset;
3288 struct sk_buff *frag_iter;
3289 int elt = 0;
3291 if (copy > 0) {
3292 if (copy > len)
3293 copy = len;
3294 sg_set_buf(sg, skb->data + offset, copy);
3295 elt++;
3296 if ((len -= copy) == 0)
3297 return elt;
3298 offset += copy;
3301 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3302 int end;
3304 WARN_ON(start > offset + len);
3306 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3307 if ((copy = end - offset) > 0) {
3308 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3310 if (copy > len)
3311 copy = len;
3312 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3313 frag->page_offset+offset-start);
3314 elt++;
3315 if (!(len -= copy))
3316 return elt;
3317 offset += copy;
3319 start = end;
3322 skb_walk_frags(skb, frag_iter) {
3323 int end;
3325 WARN_ON(start > offset + len);
3327 end = start + frag_iter->len;
3328 if ((copy = end - offset) > 0) {
3329 if (copy > len)
3330 copy = len;
3331 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3332 copy);
3333 if ((len -= copy) == 0)
3334 return elt;
3335 offset += copy;
3337 start = end;
3339 BUG_ON(len);
3340 return elt;
3343 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3344 * sglist without mark the sg which contain last skb data as the end.
3345 * So the caller can mannipulate sg list as will when padding new data after
3346 * the first call without calling sg_unmark_end to expend sg list.
3348 * Scenario to use skb_to_sgvec_nomark:
3349 * 1. sg_init_table
3350 * 2. skb_to_sgvec_nomark(payload1)
3351 * 3. skb_to_sgvec_nomark(payload2)
3353 * This is equivalent to:
3354 * 1. sg_init_table
3355 * 2. skb_to_sgvec(payload1)
3356 * 3. sg_unmark_end
3357 * 4. skb_to_sgvec(payload2)
3359 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3360 * is more preferable.
3362 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3363 int offset, int len)
3365 return __skb_to_sgvec(skb, sg, offset, len);
3367 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3369 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3371 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3373 sg_mark_end(&sg[nsg - 1]);
3375 return nsg;
3377 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3380 * skb_cow_data - Check that a socket buffer's data buffers are writable
3381 * @skb: The socket buffer to check.
3382 * @tailbits: Amount of trailing space to be added
3383 * @trailer: Returned pointer to the skb where the @tailbits space begins
3385 * Make sure that the data buffers attached to a socket buffer are
3386 * writable. If they are not, private copies are made of the data buffers
3387 * and the socket buffer is set to use these instead.
3389 * If @tailbits is given, make sure that there is space to write @tailbits
3390 * bytes of data beyond current end of socket buffer. @trailer will be
3391 * set to point to the skb in which this space begins.
3393 * The number of scatterlist elements required to completely map the
3394 * COW'd and extended socket buffer will be returned.
3396 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3398 int copyflag;
3399 int elt;
3400 struct sk_buff *skb1, **skb_p;
3402 /* If skb is cloned or its head is paged, reallocate
3403 * head pulling out all the pages (pages are considered not writable
3404 * at the moment even if they are anonymous).
3406 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3407 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3408 return -ENOMEM;
3410 /* Easy case. Most of packets will go this way. */
3411 if (!skb_has_frag_list(skb)) {
3412 /* A little of trouble, not enough of space for trailer.
3413 * This should not happen, when stack is tuned to generate
3414 * good frames. OK, on miss we reallocate and reserve even more
3415 * space, 128 bytes is fair. */
3417 if (skb_tailroom(skb) < tailbits &&
3418 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3419 return -ENOMEM;
3421 /* Voila! */
3422 *trailer = skb;
3423 return 1;
3426 /* Misery. We are in troubles, going to mincer fragments... */
3428 elt = 1;
3429 skb_p = &skb_shinfo(skb)->frag_list;
3430 copyflag = 0;
3432 while ((skb1 = *skb_p) != NULL) {
3433 int ntail = 0;
3435 /* The fragment is partially pulled by someone,
3436 * this can happen on input. Copy it and everything
3437 * after it. */
3439 if (skb_shared(skb1))
3440 copyflag = 1;
3442 /* If the skb is the last, worry about trailer. */
3444 if (skb1->next == NULL && tailbits) {
3445 if (skb_shinfo(skb1)->nr_frags ||
3446 skb_has_frag_list(skb1) ||
3447 skb_tailroom(skb1) < tailbits)
3448 ntail = tailbits + 128;
3451 if (copyflag ||
3452 skb_cloned(skb1) ||
3453 ntail ||
3454 skb_shinfo(skb1)->nr_frags ||
3455 skb_has_frag_list(skb1)) {
3456 struct sk_buff *skb2;
3458 /* Fuck, we are miserable poor guys... */
3459 if (ntail == 0)
3460 skb2 = skb_copy(skb1, GFP_ATOMIC);
3461 else
3462 skb2 = skb_copy_expand(skb1,
3463 skb_headroom(skb1),
3464 ntail,
3465 GFP_ATOMIC);
3466 if (unlikely(skb2 == NULL))
3467 return -ENOMEM;
3469 if (skb1->sk)
3470 skb_set_owner_w(skb2, skb1->sk);
3472 /* Looking around. Are we still alive?
3473 * OK, link new skb, drop old one */
3475 skb2->next = skb1->next;
3476 *skb_p = skb2;
3477 kfree_skb(skb1);
3478 skb1 = skb2;
3480 elt++;
3481 *trailer = skb1;
3482 skb_p = &skb1->next;
3485 return elt;
3487 EXPORT_SYMBOL_GPL(skb_cow_data);
3489 static void sock_rmem_free(struct sk_buff *skb)
3491 struct sock *sk = skb->sk;
3493 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3497 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3499 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3501 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3502 (unsigned int)sk->sk_rcvbuf)
3503 return -ENOMEM;
3505 skb_orphan(skb);
3506 skb->sk = sk;
3507 skb->destructor = sock_rmem_free;
3508 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3510 /* before exiting rcu section, make sure dst is refcounted */
3511 skb_dst_force(skb);
3513 skb_queue_tail(&sk->sk_error_queue, skb);
3514 if (!sock_flag(sk, SOCK_DEAD))
3515 sk->sk_data_ready(sk);
3516 return 0;
3518 EXPORT_SYMBOL(sock_queue_err_skb);
3520 void skb_tstamp_tx(struct sk_buff *orig_skb,
3521 struct skb_shared_hwtstamps *hwtstamps)
3523 struct sock *sk = orig_skb->sk;
3524 struct sock_exterr_skb *serr;
3525 struct sk_buff *skb;
3526 int err;
3528 if (!sk)
3529 return;
3531 if (hwtstamps) {
3532 *skb_hwtstamps(orig_skb) =
3533 *hwtstamps;
3534 } else {
3536 * no hardware time stamps available,
3537 * so keep the shared tx_flags and only
3538 * store software time stamp
3540 orig_skb->tstamp = ktime_get_real();
3543 skb = skb_clone(orig_skb, GFP_ATOMIC);
3544 if (!skb)
3545 return;
3547 serr = SKB_EXT_ERR(skb);
3548 memset(serr, 0, sizeof(*serr));
3549 serr->ee.ee_errno = ENOMSG;
3550 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3552 err = sock_queue_err_skb(sk, skb);
3554 if (err)
3555 kfree_skb(skb);
3557 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3559 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3561 struct sock *sk = skb->sk;
3562 struct sock_exterr_skb *serr;
3563 int err;
3565 skb->wifi_acked_valid = 1;
3566 skb->wifi_acked = acked;
3568 serr = SKB_EXT_ERR(skb);
3569 memset(serr, 0, sizeof(*serr));
3570 serr->ee.ee_errno = ENOMSG;
3571 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3573 err = sock_queue_err_skb(sk, skb);
3574 if (err)
3575 kfree_skb(skb);
3577 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3581 * skb_partial_csum_set - set up and verify partial csum values for packet
3582 * @skb: the skb to set
3583 * @start: the number of bytes after skb->data to start checksumming.
3584 * @off: the offset from start to place the checksum.
3586 * For untrusted partially-checksummed packets, we need to make sure the values
3587 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3589 * This function checks and sets those values and skb->ip_summed: if this
3590 * returns false you should drop the packet.
3592 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3594 if (unlikely(start > skb_headlen(skb)) ||
3595 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3596 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3597 start, off, skb_headlen(skb));
3598 return false;
3600 skb->ip_summed = CHECKSUM_PARTIAL;
3601 skb->csum_start = skb_headroom(skb) + start;
3602 skb->csum_offset = off;
3603 skb_set_transport_header(skb, start);
3604 return true;
3606 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3608 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3609 unsigned int max)
3611 if (skb_headlen(skb) >= len)
3612 return 0;
3614 /* If we need to pullup then pullup to the max, so we
3615 * won't need to do it again.
3617 if (max > skb->len)
3618 max = skb->len;
3620 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3621 return -ENOMEM;
3623 if (skb_headlen(skb) < len)
3624 return -EPROTO;
3626 return 0;
3629 #define MAX_TCP_HDR_LEN (15 * 4)
3631 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3632 typeof(IPPROTO_IP) proto,
3633 unsigned int off)
3635 switch (proto) {
3636 int err;
3638 case IPPROTO_TCP:
3639 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3640 off + MAX_TCP_HDR_LEN);
3641 if (!err && !skb_partial_csum_set(skb, off,
3642 offsetof(struct tcphdr,
3643 check)))
3644 err = -EPROTO;
3645 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3647 case IPPROTO_UDP:
3648 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3649 off + sizeof(struct udphdr));
3650 if (!err && !skb_partial_csum_set(skb, off,
3651 offsetof(struct udphdr,
3652 check)))
3653 err = -EPROTO;
3654 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3657 return ERR_PTR(-EPROTO);
3660 /* This value should be large enough to cover a tagged ethernet header plus
3661 * maximally sized IP and TCP or UDP headers.
3663 #define MAX_IP_HDR_LEN 128
3665 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3667 unsigned int off;
3668 bool fragment;
3669 __sum16 *csum;
3670 int err;
3672 fragment = false;
3674 err = skb_maybe_pull_tail(skb,
3675 sizeof(struct iphdr),
3676 MAX_IP_HDR_LEN);
3677 if (err < 0)
3678 goto out;
3680 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3681 fragment = true;
3683 off = ip_hdrlen(skb);
3685 err = -EPROTO;
3687 if (fragment)
3688 goto out;
3690 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3691 if (IS_ERR(csum))
3692 return PTR_ERR(csum);
3694 if (recalculate)
3695 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3696 ip_hdr(skb)->daddr,
3697 skb->len - off,
3698 ip_hdr(skb)->protocol, 0);
3699 err = 0;
3701 out:
3702 return err;
3705 /* This value should be large enough to cover a tagged ethernet header plus
3706 * an IPv6 header, all options, and a maximal TCP or UDP header.
3708 #define MAX_IPV6_HDR_LEN 256
3710 #define OPT_HDR(type, skb, off) \
3711 (type *)(skb_network_header(skb) + (off))
3713 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3715 int err;
3716 u8 nexthdr;
3717 unsigned int off;
3718 unsigned int len;
3719 bool fragment;
3720 bool done;
3721 __sum16 *csum;
3723 fragment = false;
3724 done = false;
3726 off = sizeof(struct ipv6hdr);
3728 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3729 if (err < 0)
3730 goto out;
3732 nexthdr = ipv6_hdr(skb)->nexthdr;
3734 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3735 while (off <= len && !done) {
3736 switch (nexthdr) {
3737 case IPPROTO_DSTOPTS:
3738 case IPPROTO_HOPOPTS:
3739 case IPPROTO_ROUTING: {
3740 struct ipv6_opt_hdr *hp;
3742 err = skb_maybe_pull_tail(skb,
3743 off +
3744 sizeof(struct ipv6_opt_hdr),
3745 MAX_IPV6_HDR_LEN);
3746 if (err < 0)
3747 goto out;
3749 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3750 nexthdr = hp->nexthdr;
3751 off += ipv6_optlen(hp);
3752 break;
3754 case IPPROTO_AH: {
3755 struct ip_auth_hdr *hp;
3757 err = skb_maybe_pull_tail(skb,
3758 off +
3759 sizeof(struct ip_auth_hdr),
3760 MAX_IPV6_HDR_LEN);
3761 if (err < 0)
3762 goto out;
3764 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3765 nexthdr = hp->nexthdr;
3766 off += ipv6_authlen(hp);
3767 break;
3769 case IPPROTO_FRAGMENT: {
3770 struct frag_hdr *hp;
3772 err = skb_maybe_pull_tail(skb,
3773 off +
3774 sizeof(struct frag_hdr),
3775 MAX_IPV6_HDR_LEN);
3776 if (err < 0)
3777 goto out;
3779 hp = OPT_HDR(struct frag_hdr, skb, off);
3781 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3782 fragment = true;
3784 nexthdr = hp->nexthdr;
3785 off += sizeof(struct frag_hdr);
3786 break;
3788 default:
3789 done = true;
3790 break;
3794 err = -EPROTO;
3796 if (!done || fragment)
3797 goto out;
3799 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3800 if (IS_ERR(csum))
3801 return PTR_ERR(csum);
3803 if (recalculate)
3804 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3805 &ipv6_hdr(skb)->daddr,
3806 skb->len - off, nexthdr, 0);
3807 err = 0;
3809 out:
3810 return err;
3814 * skb_checksum_setup - set up partial checksum offset
3815 * @skb: the skb to set up
3816 * @recalculate: if true the pseudo-header checksum will be recalculated
3818 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3820 int err;
3822 switch (skb->protocol) {
3823 case htons(ETH_P_IP):
3824 err = skb_checksum_setup_ipv4(skb, recalculate);
3825 break;
3827 case htons(ETH_P_IPV6):
3828 err = skb_checksum_setup_ipv6(skb, recalculate);
3829 break;
3831 default:
3832 err = -EPROTO;
3833 break;
3836 return err;
3838 EXPORT_SYMBOL(skb_checksum_setup);
3840 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3842 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3843 skb->dev->name);
3845 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3847 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3849 if (head_stolen) {
3850 skb_release_head_state(skb);
3851 kmem_cache_free(skbuff_head_cache, skb);
3852 } else {
3853 __kfree_skb(skb);
3856 EXPORT_SYMBOL(kfree_skb_partial);
3859 * skb_try_coalesce - try to merge skb to prior one
3860 * @to: prior buffer
3861 * @from: buffer to add
3862 * @fragstolen: pointer to boolean
3863 * @delta_truesize: how much more was allocated than was requested
3865 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3866 bool *fragstolen, int *delta_truesize)
3868 int i, delta, len = from->len;
3870 *fragstolen = false;
3872 if (skb_cloned(to))
3873 return false;
3875 if (len <= skb_tailroom(to)) {
3876 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3877 *delta_truesize = 0;
3878 return true;
3881 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3882 return false;
3884 if (skb_headlen(from) != 0) {
3885 struct page *page;
3886 unsigned int offset;
3888 if (skb_shinfo(to)->nr_frags +
3889 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3890 return false;
3892 if (skb_head_is_locked(from))
3893 return false;
3895 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3897 page = virt_to_head_page(from->head);
3898 offset = from->data - (unsigned char *)page_address(page);
3900 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3901 page, offset, skb_headlen(from));
3902 *fragstolen = true;
3903 } else {
3904 if (skb_shinfo(to)->nr_frags +
3905 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3906 return false;
3908 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3911 WARN_ON_ONCE(delta < len);
3913 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3914 skb_shinfo(from)->frags,
3915 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3916 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3918 if (!skb_cloned(from))
3919 skb_shinfo(from)->nr_frags = 0;
3921 /* if the skb is not cloned this does nothing
3922 * since we set nr_frags to 0.
3924 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3925 skb_frag_ref(from, i);
3927 to->truesize += delta;
3928 to->len += len;
3929 to->data_len += len;
3931 *delta_truesize = delta;
3932 return true;
3934 EXPORT_SYMBOL(skb_try_coalesce);
3937 * skb_scrub_packet - scrub an skb
3939 * @skb: buffer to clean
3940 * @xnet: packet is crossing netns
3942 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
3943 * into/from a tunnel. Some information have to be cleared during these
3944 * operations.
3945 * skb_scrub_packet can also be used to clean a skb before injecting it in
3946 * another namespace (@xnet == true). We have to clear all information in the
3947 * skb that could impact namespace isolation.
3949 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
3951 skb->tstamp.tv64 = 0;
3952 skb->pkt_type = PACKET_HOST;
3953 skb->skb_iif = 0;
3954 skb->ignore_df = 0;
3955 skb_dst_drop(skb);
3956 skb_init_secmark(skb);
3957 secpath_reset(skb);
3958 nf_reset(skb);
3959 nf_reset_trace(skb);
3961 if (!xnet)
3962 return;
3964 ipvs_reset(skb);
3965 skb_orphan(skb);
3966 skb->mark = 0;
3968 EXPORT_SYMBOL_GPL(skb_scrub_packet);
3971 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3973 * @skb: GSO skb
3975 * skb_gso_transport_seglen is used to determine the real size of the
3976 * individual segments, including Layer4 headers (TCP/UDP).
3978 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3980 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3982 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3984 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3985 return tcp_hdrlen(skb) + shinfo->gso_size;
3987 /* UFO sets gso_size to the size of the fragmentation
3988 * payload, i.e. the size of the L4 (UDP) header is already
3989 * accounted for.
3991 return shinfo->gso_size;
3993 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
3995 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
3997 if (skb_cow(skb, skb_headroom(skb)) < 0) {
3998 kfree_skb(skb);
3999 return NULL;
4002 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4003 2 * ETH_ALEN);
4004 skb->mac_header += VLAN_HLEN;
4005 return skb;
4008 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4010 struct vlan_hdr *vhdr;
4011 u16 vlan_tci;
4013 if (unlikely(vlan_tx_tag_present(skb))) {
4014 /* vlan_tci is already set-up so leave this for another time */
4015 return skb;
4018 skb = skb_share_check(skb, GFP_ATOMIC);
4019 if (unlikely(!skb))
4020 goto err_free;
4022 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4023 goto err_free;
4025 vhdr = (struct vlan_hdr *)skb->data;
4026 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4027 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4029 skb_pull_rcsum(skb, VLAN_HLEN);
4030 vlan_set_encap_proto(skb, vhdr);
4032 skb = skb_reorder_vlan_header(skb);
4033 if (unlikely(!skb))
4034 goto err_free;
4036 skb_reset_network_header(skb);
4037 skb_reset_transport_header(skb);
4038 skb_reset_mac_len(skb);
4040 return skb;
4042 err_free:
4043 kfree_skb(skb);
4044 return NULL;
4046 EXPORT_SYMBOL(skb_vlan_untag);