Linux 4.1.16
[linux/fpc-iii.git] / net / core / skbuff.c
blob2e5fcda165705716b7e6e2ea3a7fffc05fe0e04e
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>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
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_fclones *fclones;
264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
267 skb->fclone = SKB_FCLONE_ORIG;
268 atomic_set(&fclones->fclone_ref, 1);
270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
271 fclones->skb2.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 (page_is_pfmemalloc(virt_to_head_page(data)))
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);
358 static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
360 static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
361 gfp_t gfp_mask)
363 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
364 struct page *page = NULL;
365 gfp_t gfp = gfp_mask;
367 if (order) {
368 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
369 __GFP_NOMEMALLOC;
370 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
371 nc->frag.size = PAGE_SIZE << (page ? order : 0);
374 if (unlikely(!page))
375 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
377 nc->frag.page = page;
379 return page;
382 static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
383 unsigned int fragsz, gfp_t gfp_mask)
385 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
386 struct page *page = nc->frag.page;
387 unsigned int size;
388 int offset;
390 if (unlikely(!page)) {
391 refill:
392 page = __page_frag_refill(nc, gfp_mask);
393 if (!page)
394 return NULL;
396 /* if size can vary use frag.size else just use PAGE_SIZE */
397 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
399 /* Even if we own the page, we do not use atomic_set().
400 * This would break get_page_unless_zero() users.
402 atomic_add(size - 1, &page->_count);
404 /* reset page count bias and offset to start of new frag */
405 nc->pagecnt_bias = size;
406 nc->frag.offset = size;
409 offset = nc->frag.offset - fragsz;
410 if (unlikely(offset < 0)) {
411 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
412 goto refill;
414 /* if size can vary use frag.size else just use PAGE_SIZE */
415 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
417 /* OK, page count is 0, we can safely set it */
418 atomic_set(&page->_count, size);
420 /* reset page count bias and offset to start of new frag */
421 nc->pagecnt_bias = size;
422 offset = size - fragsz;
425 nc->pagecnt_bias--;
426 nc->frag.offset = offset;
428 return page_address(page) + offset;
431 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
433 unsigned long flags;
434 void *data;
436 local_irq_save(flags);
437 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
438 local_irq_restore(flags);
439 return data;
443 * netdev_alloc_frag - allocate a page fragment
444 * @fragsz: fragment size
446 * Allocates a frag from a page for receive buffer.
447 * Uses GFP_ATOMIC allocations.
449 void *netdev_alloc_frag(unsigned int fragsz)
451 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
453 EXPORT_SYMBOL(netdev_alloc_frag);
455 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
457 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
460 void *napi_alloc_frag(unsigned int fragsz)
462 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
464 EXPORT_SYMBOL(napi_alloc_frag);
467 * __alloc_rx_skb - allocate an skbuff for rx
468 * @length: length to allocate
469 * @gfp_mask: get_free_pages mask, passed to alloc_skb
470 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
471 * allocations in case we have to fallback to __alloc_skb()
472 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
473 * from napi_cache instead of netdev_cache.
475 * Allocate a new &sk_buff and assign it a usage count of one. The
476 * buffer has unspecified headroom built in. Users should allocate
477 * the headroom they think they need without accounting for the
478 * built in space. The built in space is used for optimisations.
480 * %NULL is returned if there is no free memory.
482 static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
483 int flags)
485 struct sk_buff *skb = NULL;
486 unsigned int fragsz = SKB_DATA_ALIGN(length) +
487 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
489 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
490 void *data;
492 if (sk_memalloc_socks())
493 gfp_mask |= __GFP_MEMALLOC;
495 data = (flags & SKB_ALLOC_NAPI) ?
496 __napi_alloc_frag(fragsz, gfp_mask) :
497 __netdev_alloc_frag(fragsz, gfp_mask);
499 if (likely(data)) {
500 skb = build_skb(data, fragsz);
501 if (unlikely(!skb))
502 put_page(virt_to_head_page(data));
504 } else {
505 skb = __alloc_skb(length, gfp_mask,
506 SKB_ALLOC_RX, NUMA_NO_NODE);
508 return skb;
512 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
513 * @dev: network device to receive on
514 * @length: length to allocate
515 * @gfp_mask: get_free_pages mask, passed to alloc_skb
517 * Allocate a new &sk_buff and assign it a usage count of one. The
518 * buffer has NET_SKB_PAD headroom built in. Users should allocate
519 * the headroom they think they need without accounting for the
520 * built in space. The built in space is used for optimisations.
522 * %NULL is returned if there is no free memory.
524 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
525 unsigned int length, gfp_t gfp_mask)
527 struct sk_buff *skb;
529 length += NET_SKB_PAD;
530 skb = __alloc_rx_skb(length, gfp_mask, 0);
532 if (likely(skb)) {
533 skb_reserve(skb, NET_SKB_PAD);
534 skb->dev = dev;
537 return skb;
539 EXPORT_SYMBOL(__netdev_alloc_skb);
542 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
543 * @napi: napi instance this buffer was allocated for
544 * @length: length to allocate
545 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
547 * Allocate a new sk_buff for use in NAPI receive. This buffer will
548 * attempt to allocate the head from a special reserved region used
549 * only for NAPI Rx allocation. By doing this we can save several
550 * CPU cycles by avoiding having to disable and re-enable IRQs.
552 * %NULL is returned if there is no free memory.
554 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
555 unsigned int length, gfp_t gfp_mask)
557 struct sk_buff *skb;
559 length += NET_SKB_PAD + NET_IP_ALIGN;
560 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
562 if (likely(skb)) {
563 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
564 skb->dev = napi->dev;
567 return skb;
569 EXPORT_SYMBOL(__napi_alloc_skb);
571 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
572 int size, unsigned int truesize)
574 skb_fill_page_desc(skb, i, page, off, size);
575 skb->len += size;
576 skb->data_len += size;
577 skb->truesize += truesize;
579 EXPORT_SYMBOL(skb_add_rx_frag);
581 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
582 unsigned int truesize)
584 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
586 skb_frag_size_add(frag, size);
587 skb->len += size;
588 skb->data_len += size;
589 skb->truesize += truesize;
591 EXPORT_SYMBOL(skb_coalesce_rx_frag);
593 static void skb_drop_list(struct sk_buff **listp)
595 kfree_skb_list(*listp);
596 *listp = NULL;
599 static inline void skb_drop_fraglist(struct sk_buff *skb)
601 skb_drop_list(&skb_shinfo(skb)->frag_list);
604 static void skb_clone_fraglist(struct sk_buff *skb)
606 struct sk_buff *list;
608 skb_walk_frags(skb, list)
609 skb_get(list);
612 static void skb_free_head(struct sk_buff *skb)
614 if (skb->head_frag)
615 put_page(virt_to_head_page(skb->head));
616 else
617 kfree(skb->head);
620 static void skb_release_data(struct sk_buff *skb)
622 struct skb_shared_info *shinfo = skb_shinfo(skb);
623 int i;
625 if (skb->cloned &&
626 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
627 &shinfo->dataref))
628 return;
630 for (i = 0; i < shinfo->nr_frags; i++)
631 __skb_frag_unref(&shinfo->frags[i]);
634 * If skb buf is from userspace, we need to notify the caller
635 * the lower device DMA has done;
637 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
638 struct ubuf_info *uarg;
640 uarg = shinfo->destructor_arg;
641 if (uarg->callback)
642 uarg->callback(uarg, true);
645 if (shinfo->frag_list)
646 kfree_skb_list(shinfo->frag_list);
648 skb_free_head(skb);
652 * Free an skbuff by memory without cleaning the state.
654 static void kfree_skbmem(struct sk_buff *skb)
656 struct sk_buff_fclones *fclones;
658 switch (skb->fclone) {
659 case SKB_FCLONE_UNAVAILABLE:
660 kmem_cache_free(skbuff_head_cache, skb);
661 return;
663 case SKB_FCLONE_ORIG:
664 fclones = container_of(skb, struct sk_buff_fclones, skb1);
666 /* We usually free the clone (TX completion) before original skb
667 * This test would have no chance to be true for the clone,
668 * while here, branch prediction will be good.
670 if (atomic_read(&fclones->fclone_ref) == 1)
671 goto fastpath;
672 break;
674 default: /* SKB_FCLONE_CLONE */
675 fclones = container_of(skb, struct sk_buff_fclones, skb2);
676 break;
678 if (!atomic_dec_and_test(&fclones->fclone_ref))
679 return;
680 fastpath:
681 kmem_cache_free(skbuff_fclone_cache, fclones);
684 static void skb_release_head_state(struct sk_buff *skb)
686 skb_dst_drop(skb);
687 #ifdef CONFIG_XFRM
688 secpath_put(skb->sp);
689 #endif
690 if (skb->destructor) {
691 WARN_ON(in_irq());
692 skb->destructor(skb);
694 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
695 nf_conntrack_put(skb->nfct);
696 #endif
697 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
698 nf_bridge_put(skb->nf_bridge);
699 #endif
702 /* Free everything but the sk_buff shell. */
703 static void skb_release_all(struct sk_buff *skb)
705 skb_release_head_state(skb);
706 if (likely(skb->head))
707 skb_release_data(skb);
711 * __kfree_skb - private function
712 * @skb: buffer
714 * Free an sk_buff. Release anything attached to the buffer.
715 * Clean the state. This is an internal helper function. Users should
716 * always call kfree_skb
719 void __kfree_skb(struct sk_buff *skb)
721 skb_release_all(skb);
722 kfree_skbmem(skb);
724 EXPORT_SYMBOL(__kfree_skb);
727 * kfree_skb - free an sk_buff
728 * @skb: buffer to free
730 * Drop a reference to the buffer and free it if the usage count has
731 * hit zero.
733 void kfree_skb(struct sk_buff *skb)
735 if (unlikely(!skb))
736 return;
737 if (likely(atomic_read(&skb->users) == 1))
738 smp_rmb();
739 else if (likely(!atomic_dec_and_test(&skb->users)))
740 return;
741 trace_kfree_skb(skb, __builtin_return_address(0));
742 __kfree_skb(skb);
744 EXPORT_SYMBOL(kfree_skb);
746 void kfree_skb_list(struct sk_buff *segs)
748 while (segs) {
749 struct sk_buff *next = segs->next;
751 kfree_skb(segs);
752 segs = next;
755 EXPORT_SYMBOL(kfree_skb_list);
758 * skb_tx_error - report an sk_buff xmit error
759 * @skb: buffer that triggered an error
761 * Report xmit error if a device callback is tracking this skb.
762 * skb must be freed afterwards.
764 void skb_tx_error(struct sk_buff *skb)
766 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
767 struct ubuf_info *uarg;
769 uarg = skb_shinfo(skb)->destructor_arg;
770 if (uarg->callback)
771 uarg->callback(uarg, false);
772 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
775 EXPORT_SYMBOL(skb_tx_error);
778 * consume_skb - free an skbuff
779 * @skb: buffer to free
781 * Drop a ref to the buffer and free it if the usage count has hit zero
782 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
783 * is being dropped after a failure and notes that
785 void consume_skb(struct sk_buff *skb)
787 if (unlikely(!skb))
788 return;
789 if (likely(atomic_read(&skb->users) == 1))
790 smp_rmb();
791 else if (likely(!atomic_dec_and_test(&skb->users)))
792 return;
793 trace_consume_skb(skb);
794 __kfree_skb(skb);
796 EXPORT_SYMBOL(consume_skb);
798 /* Make sure a field is enclosed inside headers_start/headers_end section */
799 #define CHECK_SKB_FIELD(field) \
800 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
801 offsetof(struct sk_buff, headers_start)); \
802 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
803 offsetof(struct sk_buff, headers_end)); \
805 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
807 new->tstamp = old->tstamp;
808 /* We do not copy old->sk */
809 new->dev = old->dev;
810 memcpy(new->cb, old->cb, sizeof(old->cb));
811 skb_dst_copy(new, old);
812 #ifdef CONFIG_XFRM
813 new->sp = secpath_get(old->sp);
814 #endif
815 __nf_copy(new, old, false);
817 /* Note : this field could be in headers_start/headers_end section
818 * It is not yet because we do not want to have a 16 bit hole
820 new->queue_mapping = old->queue_mapping;
822 memcpy(&new->headers_start, &old->headers_start,
823 offsetof(struct sk_buff, headers_end) -
824 offsetof(struct sk_buff, headers_start));
825 CHECK_SKB_FIELD(protocol);
826 CHECK_SKB_FIELD(csum);
827 CHECK_SKB_FIELD(hash);
828 CHECK_SKB_FIELD(priority);
829 CHECK_SKB_FIELD(skb_iif);
830 CHECK_SKB_FIELD(vlan_proto);
831 CHECK_SKB_FIELD(vlan_tci);
832 CHECK_SKB_FIELD(transport_header);
833 CHECK_SKB_FIELD(network_header);
834 CHECK_SKB_FIELD(mac_header);
835 CHECK_SKB_FIELD(inner_protocol);
836 CHECK_SKB_FIELD(inner_transport_header);
837 CHECK_SKB_FIELD(inner_network_header);
838 CHECK_SKB_FIELD(inner_mac_header);
839 CHECK_SKB_FIELD(mark);
840 #ifdef CONFIG_NETWORK_SECMARK
841 CHECK_SKB_FIELD(secmark);
842 #endif
843 #ifdef CONFIG_NET_RX_BUSY_POLL
844 CHECK_SKB_FIELD(napi_id);
845 #endif
846 #ifdef CONFIG_XPS
847 CHECK_SKB_FIELD(sender_cpu);
848 #endif
849 #ifdef CONFIG_NET_SCHED
850 CHECK_SKB_FIELD(tc_index);
851 #ifdef CONFIG_NET_CLS_ACT
852 CHECK_SKB_FIELD(tc_verd);
853 #endif
854 #endif
859 * You should not add any new code to this function. Add it to
860 * __copy_skb_header above instead.
862 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
864 #define C(x) n->x = skb->x
866 n->next = n->prev = NULL;
867 n->sk = NULL;
868 __copy_skb_header(n, skb);
870 C(len);
871 C(data_len);
872 C(mac_len);
873 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
874 n->cloned = 1;
875 n->nohdr = 0;
876 n->destructor = NULL;
877 C(tail);
878 C(end);
879 C(head);
880 C(head_frag);
881 C(data);
882 C(truesize);
883 atomic_set(&n->users, 1);
885 atomic_inc(&(skb_shinfo(skb)->dataref));
886 skb->cloned = 1;
888 return n;
889 #undef C
893 * skb_morph - morph one skb into another
894 * @dst: the skb to receive the contents
895 * @src: the skb to supply the contents
897 * This is identical to skb_clone except that the target skb is
898 * supplied by the user.
900 * The target skb is returned upon exit.
902 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
904 skb_release_all(dst);
905 return __skb_clone(dst, src);
907 EXPORT_SYMBOL_GPL(skb_morph);
910 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
911 * @skb: the skb to modify
912 * @gfp_mask: allocation priority
914 * This must be called on SKBTX_DEV_ZEROCOPY skb.
915 * It will copy all frags into kernel and drop the reference
916 * to userspace pages.
918 * If this function is called from an interrupt gfp_mask() must be
919 * %GFP_ATOMIC.
921 * Returns 0 on success or a negative error code on failure
922 * to allocate kernel memory to copy to.
924 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
926 int i;
927 int num_frags = skb_shinfo(skb)->nr_frags;
928 struct page *page, *head = NULL;
929 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
931 for (i = 0; i < num_frags; i++) {
932 u8 *vaddr;
933 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
935 page = alloc_page(gfp_mask);
936 if (!page) {
937 while (head) {
938 struct page *next = (struct page *)page_private(head);
939 put_page(head);
940 head = next;
942 return -ENOMEM;
944 vaddr = kmap_atomic(skb_frag_page(f));
945 memcpy(page_address(page),
946 vaddr + f->page_offset, skb_frag_size(f));
947 kunmap_atomic(vaddr);
948 set_page_private(page, (unsigned long)head);
949 head = page;
952 /* skb frags release userspace buffers */
953 for (i = 0; i < num_frags; i++)
954 skb_frag_unref(skb, i);
956 uarg->callback(uarg, false);
958 /* skb frags point to kernel buffers */
959 for (i = num_frags - 1; i >= 0; i--) {
960 __skb_fill_page_desc(skb, i, head, 0,
961 skb_shinfo(skb)->frags[i].size);
962 head = (struct page *)page_private(head);
965 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
966 return 0;
968 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
971 * skb_clone - duplicate an sk_buff
972 * @skb: buffer to clone
973 * @gfp_mask: allocation priority
975 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
976 * copies share the same packet data but not structure. The new
977 * buffer has a reference count of 1. If the allocation fails the
978 * function returns %NULL otherwise the new buffer is returned.
980 * If this function is called from an interrupt gfp_mask() must be
981 * %GFP_ATOMIC.
984 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
986 struct sk_buff_fclones *fclones = container_of(skb,
987 struct sk_buff_fclones,
988 skb1);
989 struct sk_buff *n;
991 if (skb_orphan_frags(skb, gfp_mask))
992 return NULL;
994 if (skb->fclone == SKB_FCLONE_ORIG &&
995 atomic_read(&fclones->fclone_ref) == 1) {
996 n = &fclones->skb2;
997 atomic_set(&fclones->fclone_ref, 2);
998 } else {
999 if (skb_pfmemalloc(skb))
1000 gfp_mask |= __GFP_MEMALLOC;
1002 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1003 if (!n)
1004 return NULL;
1006 kmemcheck_annotate_bitfield(n, flags1);
1007 n->fclone = SKB_FCLONE_UNAVAILABLE;
1010 return __skb_clone(n, skb);
1012 EXPORT_SYMBOL(skb_clone);
1014 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1016 /* Only adjust this if it actually is csum_start rather than csum */
1017 if (skb->ip_summed == CHECKSUM_PARTIAL)
1018 skb->csum_start += off;
1019 /* {transport,network,mac}_header and tail are relative to skb->head */
1020 skb->transport_header += off;
1021 skb->network_header += off;
1022 if (skb_mac_header_was_set(skb))
1023 skb->mac_header += off;
1024 skb->inner_transport_header += off;
1025 skb->inner_network_header += off;
1026 skb->inner_mac_header += off;
1029 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1031 __copy_skb_header(new, old);
1033 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1034 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1035 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1038 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1040 if (skb_pfmemalloc(skb))
1041 return SKB_ALLOC_RX;
1042 return 0;
1046 * skb_copy - create private copy of an sk_buff
1047 * @skb: buffer to copy
1048 * @gfp_mask: allocation priority
1050 * Make a copy of both an &sk_buff and its data. This is used when the
1051 * caller wishes to modify the data and needs a private copy of the
1052 * data to alter. Returns %NULL on failure or the pointer to the buffer
1053 * on success. The returned buffer has a reference count of 1.
1055 * As by-product this function converts non-linear &sk_buff to linear
1056 * one, so that &sk_buff becomes completely private and caller is allowed
1057 * to modify all the data of returned buffer. This means that this
1058 * function is not recommended for use in circumstances when only
1059 * header is going to be modified. Use pskb_copy() instead.
1062 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1064 int headerlen = skb_headroom(skb);
1065 unsigned int size = skb_end_offset(skb) + skb->data_len;
1066 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1067 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1069 if (!n)
1070 return NULL;
1072 /* Set the data pointer */
1073 skb_reserve(n, headerlen);
1074 /* Set the tail pointer and length */
1075 skb_put(n, skb->len);
1077 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1078 BUG();
1080 copy_skb_header(n, skb);
1081 return n;
1083 EXPORT_SYMBOL(skb_copy);
1086 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1087 * @skb: buffer to copy
1088 * @headroom: headroom of new skb
1089 * @gfp_mask: allocation priority
1090 * @fclone: if true allocate the copy of the skb from the fclone
1091 * cache instead of the head cache; it is recommended to set this
1092 * to true for the cases where the copy will likely be cloned
1094 * Make a copy of both an &sk_buff and part of its data, located
1095 * in header. Fragmented data remain shared. This is used when
1096 * the caller wishes to modify only header of &sk_buff and needs
1097 * private copy of the header to alter. Returns %NULL on failure
1098 * or the pointer to the buffer on success.
1099 * The returned buffer has a reference count of 1.
1102 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1103 gfp_t gfp_mask, bool fclone)
1105 unsigned int size = skb_headlen(skb) + headroom;
1106 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1107 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1109 if (!n)
1110 goto out;
1112 /* Set the data pointer */
1113 skb_reserve(n, headroom);
1114 /* Set the tail pointer and length */
1115 skb_put(n, skb_headlen(skb));
1116 /* Copy the bytes */
1117 skb_copy_from_linear_data(skb, n->data, n->len);
1119 n->truesize += skb->data_len;
1120 n->data_len = skb->data_len;
1121 n->len = skb->len;
1123 if (skb_shinfo(skb)->nr_frags) {
1124 int i;
1126 if (skb_orphan_frags(skb, gfp_mask)) {
1127 kfree_skb(n);
1128 n = NULL;
1129 goto out;
1131 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1132 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1133 skb_frag_ref(skb, i);
1135 skb_shinfo(n)->nr_frags = i;
1138 if (skb_has_frag_list(skb)) {
1139 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1140 skb_clone_fraglist(n);
1143 copy_skb_header(n, skb);
1144 out:
1145 return n;
1147 EXPORT_SYMBOL(__pskb_copy_fclone);
1150 * pskb_expand_head - reallocate header of &sk_buff
1151 * @skb: buffer to reallocate
1152 * @nhead: room to add at head
1153 * @ntail: room to add at tail
1154 * @gfp_mask: allocation priority
1156 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1157 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1158 * reference count of 1. Returns zero in the case of success or error,
1159 * if expansion failed. In the last case, &sk_buff is not changed.
1161 * All the pointers pointing into skb header may change and must be
1162 * reloaded after call to this function.
1165 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1166 gfp_t gfp_mask)
1168 int i;
1169 u8 *data;
1170 int size = nhead + skb_end_offset(skb) + ntail;
1171 long off;
1173 BUG_ON(nhead < 0);
1175 if (skb_shared(skb))
1176 BUG();
1178 size = SKB_DATA_ALIGN(size);
1180 if (skb_pfmemalloc(skb))
1181 gfp_mask |= __GFP_MEMALLOC;
1182 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1183 gfp_mask, NUMA_NO_NODE, NULL);
1184 if (!data)
1185 goto nodata;
1186 size = SKB_WITH_OVERHEAD(ksize(data));
1188 /* Copy only real data... and, alas, header. This should be
1189 * optimized for the cases when header is void.
1191 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1193 memcpy((struct skb_shared_info *)(data + size),
1194 skb_shinfo(skb),
1195 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1198 * if shinfo is shared we must drop the old head gracefully, but if it
1199 * is not we can just drop the old head and let the existing refcount
1200 * be since all we did is relocate the values
1202 if (skb_cloned(skb)) {
1203 /* copy this zero copy skb frags */
1204 if (skb_orphan_frags(skb, gfp_mask))
1205 goto nofrags;
1206 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1207 skb_frag_ref(skb, i);
1209 if (skb_has_frag_list(skb))
1210 skb_clone_fraglist(skb);
1212 skb_release_data(skb);
1213 } else {
1214 skb_free_head(skb);
1216 off = (data + nhead) - skb->head;
1218 skb->head = data;
1219 skb->head_frag = 0;
1220 skb->data += off;
1221 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1222 skb->end = size;
1223 off = nhead;
1224 #else
1225 skb->end = skb->head + size;
1226 #endif
1227 skb->tail += off;
1228 skb_headers_offset_update(skb, nhead);
1229 skb->cloned = 0;
1230 skb->hdr_len = 0;
1231 skb->nohdr = 0;
1232 atomic_set(&skb_shinfo(skb)->dataref, 1);
1233 return 0;
1235 nofrags:
1236 kfree(data);
1237 nodata:
1238 return -ENOMEM;
1240 EXPORT_SYMBOL(pskb_expand_head);
1242 /* Make private copy of skb with writable head and some headroom */
1244 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1246 struct sk_buff *skb2;
1247 int delta = headroom - skb_headroom(skb);
1249 if (delta <= 0)
1250 skb2 = pskb_copy(skb, GFP_ATOMIC);
1251 else {
1252 skb2 = skb_clone(skb, GFP_ATOMIC);
1253 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1254 GFP_ATOMIC)) {
1255 kfree_skb(skb2);
1256 skb2 = NULL;
1259 return skb2;
1261 EXPORT_SYMBOL(skb_realloc_headroom);
1264 * skb_copy_expand - copy and expand sk_buff
1265 * @skb: buffer to copy
1266 * @newheadroom: new free bytes at head
1267 * @newtailroom: new free bytes at tail
1268 * @gfp_mask: allocation priority
1270 * Make a copy of both an &sk_buff and its data and while doing so
1271 * allocate additional space.
1273 * This is used when the caller wishes to modify the data and needs a
1274 * private copy of the data to alter as well as more space for new fields.
1275 * Returns %NULL on failure or the pointer to the buffer
1276 * on success. The returned buffer has a reference count of 1.
1278 * You must pass %GFP_ATOMIC as the allocation priority if this function
1279 * is called from an interrupt.
1281 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1282 int newheadroom, int newtailroom,
1283 gfp_t gfp_mask)
1286 * Allocate the copy buffer
1288 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1289 gfp_mask, skb_alloc_rx_flag(skb),
1290 NUMA_NO_NODE);
1291 int oldheadroom = skb_headroom(skb);
1292 int head_copy_len, head_copy_off;
1294 if (!n)
1295 return NULL;
1297 skb_reserve(n, newheadroom);
1299 /* Set the tail pointer and length */
1300 skb_put(n, skb->len);
1302 head_copy_len = oldheadroom;
1303 head_copy_off = 0;
1304 if (newheadroom <= head_copy_len)
1305 head_copy_len = newheadroom;
1306 else
1307 head_copy_off = newheadroom - head_copy_len;
1309 /* Copy the linear header and data. */
1310 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1311 skb->len + head_copy_len))
1312 BUG();
1314 copy_skb_header(n, skb);
1316 skb_headers_offset_update(n, newheadroom - oldheadroom);
1318 return n;
1320 EXPORT_SYMBOL(skb_copy_expand);
1323 * skb_pad - zero pad the tail of an skb
1324 * @skb: buffer to pad
1325 * @pad: space to pad
1327 * Ensure that a buffer is followed by a padding area that is zero
1328 * filled. Used by network drivers which may DMA or transfer data
1329 * beyond the buffer end onto the wire.
1331 * May return error in out of memory cases. The skb is freed on error.
1334 int skb_pad(struct sk_buff *skb, int pad)
1336 int err;
1337 int ntail;
1339 /* If the skbuff is non linear tailroom is always zero.. */
1340 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1341 memset(skb->data+skb->len, 0, pad);
1342 return 0;
1345 ntail = skb->data_len + pad - (skb->end - skb->tail);
1346 if (likely(skb_cloned(skb) || ntail > 0)) {
1347 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1348 if (unlikely(err))
1349 goto free_skb;
1352 /* FIXME: The use of this function with non-linear skb's really needs
1353 * to be audited.
1355 err = skb_linearize(skb);
1356 if (unlikely(err))
1357 goto free_skb;
1359 memset(skb->data + skb->len, 0, pad);
1360 return 0;
1362 free_skb:
1363 kfree_skb(skb);
1364 return err;
1366 EXPORT_SYMBOL(skb_pad);
1369 * pskb_put - add data to the tail of a potentially fragmented buffer
1370 * @skb: start of the buffer to use
1371 * @tail: tail fragment of the buffer to use
1372 * @len: amount of data to add
1374 * This function extends the used data area of the potentially
1375 * fragmented buffer. @tail must be the last fragment of @skb -- or
1376 * @skb itself. If this would exceed the total buffer size the kernel
1377 * will panic. A pointer to the first byte of the extra data is
1378 * returned.
1381 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1383 if (tail != skb) {
1384 skb->data_len += len;
1385 skb->len += len;
1387 return skb_put(tail, len);
1389 EXPORT_SYMBOL_GPL(pskb_put);
1392 * skb_put - add data to a buffer
1393 * @skb: buffer to use
1394 * @len: amount of data to add
1396 * This function extends the used data area of the buffer. If this would
1397 * exceed the total buffer size the kernel will panic. A pointer to the
1398 * first byte of the extra data is returned.
1400 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1402 unsigned char *tmp = skb_tail_pointer(skb);
1403 SKB_LINEAR_ASSERT(skb);
1404 skb->tail += len;
1405 skb->len += len;
1406 if (unlikely(skb->tail > skb->end))
1407 skb_over_panic(skb, len, __builtin_return_address(0));
1408 return tmp;
1410 EXPORT_SYMBOL(skb_put);
1413 * skb_push - add data to the start of a buffer
1414 * @skb: buffer to use
1415 * @len: amount of data to add
1417 * This function extends the used data area of the buffer at the buffer
1418 * start. If this would exceed the total buffer headroom the kernel will
1419 * panic. A pointer to the first byte of the extra data is returned.
1421 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1423 skb->data -= len;
1424 skb->len += len;
1425 if (unlikely(skb->data<skb->head))
1426 skb_under_panic(skb, len, __builtin_return_address(0));
1427 return skb->data;
1429 EXPORT_SYMBOL(skb_push);
1432 * skb_pull - remove data from the start of a buffer
1433 * @skb: buffer to use
1434 * @len: amount of data to remove
1436 * This function removes data from the start of a buffer, returning
1437 * the memory to the headroom. A pointer to the next data in the buffer
1438 * is returned. Once the data has been pulled future pushes will overwrite
1439 * the old data.
1441 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1443 return skb_pull_inline(skb, len);
1445 EXPORT_SYMBOL(skb_pull);
1448 * skb_trim - remove end from a buffer
1449 * @skb: buffer to alter
1450 * @len: new length
1452 * Cut the length of a buffer down by removing data from the tail. If
1453 * the buffer is already under the length specified it is not modified.
1454 * The skb must be linear.
1456 void skb_trim(struct sk_buff *skb, unsigned int len)
1458 if (skb->len > len)
1459 __skb_trim(skb, len);
1461 EXPORT_SYMBOL(skb_trim);
1463 /* Trims skb to length len. It can change skb pointers.
1466 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1468 struct sk_buff **fragp;
1469 struct sk_buff *frag;
1470 int offset = skb_headlen(skb);
1471 int nfrags = skb_shinfo(skb)->nr_frags;
1472 int i;
1473 int err;
1475 if (skb_cloned(skb) &&
1476 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1477 return err;
1479 i = 0;
1480 if (offset >= len)
1481 goto drop_pages;
1483 for (; i < nfrags; i++) {
1484 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1486 if (end < len) {
1487 offset = end;
1488 continue;
1491 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1493 drop_pages:
1494 skb_shinfo(skb)->nr_frags = i;
1496 for (; i < nfrags; i++)
1497 skb_frag_unref(skb, i);
1499 if (skb_has_frag_list(skb))
1500 skb_drop_fraglist(skb);
1501 goto done;
1504 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1505 fragp = &frag->next) {
1506 int end = offset + frag->len;
1508 if (skb_shared(frag)) {
1509 struct sk_buff *nfrag;
1511 nfrag = skb_clone(frag, GFP_ATOMIC);
1512 if (unlikely(!nfrag))
1513 return -ENOMEM;
1515 nfrag->next = frag->next;
1516 consume_skb(frag);
1517 frag = nfrag;
1518 *fragp = frag;
1521 if (end < len) {
1522 offset = end;
1523 continue;
1526 if (end > len &&
1527 unlikely((err = pskb_trim(frag, len - offset))))
1528 return err;
1530 if (frag->next)
1531 skb_drop_list(&frag->next);
1532 break;
1535 done:
1536 if (len > skb_headlen(skb)) {
1537 skb->data_len -= skb->len - len;
1538 skb->len = len;
1539 } else {
1540 skb->len = len;
1541 skb->data_len = 0;
1542 skb_set_tail_pointer(skb, len);
1545 return 0;
1547 EXPORT_SYMBOL(___pskb_trim);
1550 * __pskb_pull_tail - advance tail of skb header
1551 * @skb: buffer to reallocate
1552 * @delta: number of bytes to advance tail
1554 * The function makes a sense only on a fragmented &sk_buff,
1555 * it expands header moving its tail forward and copying necessary
1556 * data from fragmented part.
1558 * &sk_buff MUST have reference count of 1.
1560 * Returns %NULL (and &sk_buff does not change) if pull failed
1561 * or value of new tail of skb in the case of success.
1563 * All the pointers pointing into skb header may change and must be
1564 * reloaded after call to this function.
1567 /* Moves tail of skb head forward, copying data from fragmented part,
1568 * when it is necessary.
1569 * 1. It may fail due to malloc failure.
1570 * 2. It may change skb pointers.
1572 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1574 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1576 /* If skb has not enough free space at tail, get new one
1577 * plus 128 bytes for future expansions. If we have enough
1578 * room at tail, reallocate without expansion only if skb is cloned.
1580 int i, k, eat = (skb->tail + delta) - skb->end;
1582 if (eat > 0 || skb_cloned(skb)) {
1583 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1584 GFP_ATOMIC))
1585 return NULL;
1588 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1589 BUG();
1591 /* Optimization: no fragments, no reasons to preestimate
1592 * size of pulled pages. Superb.
1594 if (!skb_has_frag_list(skb))
1595 goto pull_pages;
1597 /* Estimate size of pulled pages. */
1598 eat = delta;
1599 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1600 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1602 if (size >= eat)
1603 goto pull_pages;
1604 eat -= size;
1607 /* If we need update frag list, we are in troubles.
1608 * Certainly, it possible to add an offset to skb data,
1609 * but taking into account that pulling is expected to
1610 * be very rare operation, it is worth to fight against
1611 * further bloating skb head and crucify ourselves here instead.
1612 * Pure masohism, indeed. 8)8)
1614 if (eat) {
1615 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1616 struct sk_buff *clone = NULL;
1617 struct sk_buff *insp = NULL;
1619 do {
1620 BUG_ON(!list);
1622 if (list->len <= eat) {
1623 /* Eaten as whole. */
1624 eat -= list->len;
1625 list = list->next;
1626 insp = list;
1627 } else {
1628 /* Eaten partially. */
1630 if (skb_shared(list)) {
1631 /* Sucks! We need to fork list. :-( */
1632 clone = skb_clone(list, GFP_ATOMIC);
1633 if (!clone)
1634 return NULL;
1635 insp = list->next;
1636 list = clone;
1637 } else {
1638 /* This may be pulled without
1639 * problems. */
1640 insp = list;
1642 if (!pskb_pull(list, eat)) {
1643 kfree_skb(clone);
1644 return NULL;
1646 break;
1648 } while (eat);
1650 /* Free pulled out fragments. */
1651 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1652 skb_shinfo(skb)->frag_list = list->next;
1653 kfree_skb(list);
1655 /* And insert new clone at head. */
1656 if (clone) {
1657 clone->next = list;
1658 skb_shinfo(skb)->frag_list = clone;
1661 /* Success! Now we may commit changes to skb data. */
1663 pull_pages:
1664 eat = delta;
1665 k = 0;
1666 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1667 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1669 if (size <= eat) {
1670 skb_frag_unref(skb, i);
1671 eat -= size;
1672 } else {
1673 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1674 if (eat) {
1675 skb_shinfo(skb)->frags[k].page_offset += eat;
1676 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1677 eat = 0;
1679 k++;
1682 skb_shinfo(skb)->nr_frags = k;
1684 skb->tail += delta;
1685 skb->data_len -= delta;
1687 return skb_tail_pointer(skb);
1689 EXPORT_SYMBOL(__pskb_pull_tail);
1692 * skb_copy_bits - copy bits from skb to kernel buffer
1693 * @skb: source skb
1694 * @offset: offset in source
1695 * @to: destination buffer
1696 * @len: number of bytes to copy
1698 * Copy the specified number of bytes from the source skb to the
1699 * destination buffer.
1701 * CAUTION ! :
1702 * If its prototype is ever changed,
1703 * check arch/{*}/net/{*}.S files,
1704 * since it is called from BPF assembly code.
1706 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1708 int start = skb_headlen(skb);
1709 struct sk_buff *frag_iter;
1710 int i, copy;
1712 if (offset > (int)skb->len - len)
1713 goto fault;
1715 /* Copy header. */
1716 if ((copy = start - offset) > 0) {
1717 if (copy > len)
1718 copy = len;
1719 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1720 if ((len -= copy) == 0)
1721 return 0;
1722 offset += copy;
1723 to += copy;
1726 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1727 int end;
1728 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1730 WARN_ON(start > offset + len);
1732 end = start + skb_frag_size(f);
1733 if ((copy = end - offset) > 0) {
1734 u8 *vaddr;
1736 if (copy > len)
1737 copy = len;
1739 vaddr = kmap_atomic(skb_frag_page(f));
1740 memcpy(to,
1741 vaddr + f->page_offset + offset - start,
1742 copy);
1743 kunmap_atomic(vaddr);
1745 if ((len -= copy) == 0)
1746 return 0;
1747 offset += copy;
1748 to += copy;
1750 start = end;
1753 skb_walk_frags(skb, frag_iter) {
1754 int end;
1756 WARN_ON(start > offset + len);
1758 end = start + frag_iter->len;
1759 if ((copy = end - offset) > 0) {
1760 if (copy > len)
1761 copy = len;
1762 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1763 goto fault;
1764 if ((len -= copy) == 0)
1765 return 0;
1766 offset += copy;
1767 to += copy;
1769 start = end;
1772 if (!len)
1773 return 0;
1775 fault:
1776 return -EFAULT;
1778 EXPORT_SYMBOL(skb_copy_bits);
1781 * Callback from splice_to_pipe(), if we need to release some pages
1782 * at the end of the spd in case we error'ed out in filling the pipe.
1784 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1786 put_page(spd->pages[i]);
1789 static struct page *linear_to_page(struct page *page, unsigned int *len,
1790 unsigned int *offset,
1791 struct sock *sk)
1793 struct page_frag *pfrag = sk_page_frag(sk);
1795 if (!sk_page_frag_refill(sk, pfrag))
1796 return NULL;
1798 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1800 memcpy(page_address(pfrag->page) + pfrag->offset,
1801 page_address(page) + *offset, *len);
1802 *offset = pfrag->offset;
1803 pfrag->offset += *len;
1805 return pfrag->page;
1808 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1809 struct page *page,
1810 unsigned int offset)
1812 return spd->nr_pages &&
1813 spd->pages[spd->nr_pages - 1] == page &&
1814 (spd->partial[spd->nr_pages - 1].offset +
1815 spd->partial[spd->nr_pages - 1].len == offset);
1819 * Fill page/offset/length into spd, if it can hold more pages.
1821 static bool spd_fill_page(struct splice_pipe_desc *spd,
1822 struct pipe_inode_info *pipe, struct page *page,
1823 unsigned int *len, unsigned int offset,
1824 bool linear,
1825 struct sock *sk)
1827 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1828 return true;
1830 if (linear) {
1831 page = linear_to_page(page, len, &offset, sk);
1832 if (!page)
1833 return true;
1835 if (spd_can_coalesce(spd, page, offset)) {
1836 spd->partial[spd->nr_pages - 1].len += *len;
1837 return false;
1839 get_page(page);
1840 spd->pages[spd->nr_pages] = page;
1841 spd->partial[spd->nr_pages].len = *len;
1842 spd->partial[spd->nr_pages].offset = offset;
1843 spd->nr_pages++;
1845 return false;
1848 static bool __splice_segment(struct page *page, unsigned int poff,
1849 unsigned int plen, unsigned int *off,
1850 unsigned int *len,
1851 struct splice_pipe_desc *spd, bool linear,
1852 struct sock *sk,
1853 struct pipe_inode_info *pipe)
1855 if (!*len)
1856 return true;
1858 /* skip this segment if already processed */
1859 if (*off >= plen) {
1860 *off -= plen;
1861 return false;
1864 /* ignore any bits we already processed */
1865 poff += *off;
1866 plen -= *off;
1867 *off = 0;
1869 do {
1870 unsigned int flen = min(*len, plen);
1872 if (spd_fill_page(spd, pipe, page, &flen, poff,
1873 linear, sk))
1874 return true;
1875 poff += flen;
1876 plen -= flen;
1877 *len -= flen;
1878 } while (*len && plen);
1880 return false;
1884 * Map linear and fragment data from the skb to spd. It reports true if the
1885 * pipe is full or if we already spliced the requested length.
1887 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1888 unsigned int *offset, unsigned int *len,
1889 struct splice_pipe_desc *spd, struct sock *sk)
1891 int seg;
1893 /* map the linear part :
1894 * If skb->head_frag is set, this 'linear' part is backed by a
1895 * fragment, and if the head is not shared with any clones then
1896 * we can avoid a copy since we own the head portion of this page.
1898 if (__splice_segment(virt_to_page(skb->data),
1899 (unsigned long) skb->data & (PAGE_SIZE - 1),
1900 skb_headlen(skb),
1901 offset, len, spd,
1902 skb_head_is_locked(skb),
1903 sk, pipe))
1904 return true;
1907 * then map the fragments
1909 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1910 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1912 if (__splice_segment(skb_frag_page(f),
1913 f->page_offset, skb_frag_size(f),
1914 offset, len, spd, false, sk, pipe))
1915 return true;
1918 return false;
1922 * Map data from the skb to a pipe. Should handle both the linear part,
1923 * the fragments, and the frag list. It does NOT handle frag lists within
1924 * the frag list, if such a thing exists. We'd probably need to recurse to
1925 * handle that cleanly.
1927 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1928 struct pipe_inode_info *pipe, unsigned int tlen,
1929 unsigned int flags)
1931 struct partial_page partial[MAX_SKB_FRAGS];
1932 struct page *pages[MAX_SKB_FRAGS];
1933 struct splice_pipe_desc spd = {
1934 .pages = pages,
1935 .partial = partial,
1936 .nr_pages_max = MAX_SKB_FRAGS,
1937 .flags = flags,
1938 .ops = &nosteal_pipe_buf_ops,
1939 .spd_release = sock_spd_release,
1941 struct sk_buff *frag_iter;
1942 struct sock *sk = skb->sk;
1943 int ret = 0;
1946 * __skb_splice_bits() only fails if the output has no room left,
1947 * so no point in going over the frag_list for the error case.
1949 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1950 goto done;
1951 else if (!tlen)
1952 goto done;
1955 * now see if we have a frag_list to map
1957 skb_walk_frags(skb, frag_iter) {
1958 if (!tlen)
1959 break;
1960 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1961 break;
1964 done:
1965 if (spd.nr_pages) {
1967 * Drop the socket lock, otherwise we have reverse
1968 * locking dependencies between sk_lock and i_mutex
1969 * here as compared to sendfile(). We enter here
1970 * with the socket lock held, and splice_to_pipe() will
1971 * grab the pipe inode lock. For sendfile() emulation,
1972 * we call into ->sendpage() with the i_mutex lock held
1973 * and networking will grab the socket lock.
1975 release_sock(sk);
1976 ret = splice_to_pipe(pipe, &spd);
1977 lock_sock(sk);
1980 return ret;
1984 * skb_store_bits - store bits from kernel buffer to skb
1985 * @skb: destination buffer
1986 * @offset: offset in destination
1987 * @from: source buffer
1988 * @len: number of bytes to copy
1990 * Copy the specified number of bytes from the source buffer to the
1991 * destination skb. This function handles all the messy bits of
1992 * traversing fragment lists and such.
1995 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1997 int start = skb_headlen(skb);
1998 struct sk_buff *frag_iter;
1999 int i, copy;
2001 if (offset > (int)skb->len - len)
2002 goto fault;
2004 if ((copy = start - offset) > 0) {
2005 if (copy > len)
2006 copy = len;
2007 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2008 if ((len -= copy) == 0)
2009 return 0;
2010 offset += copy;
2011 from += copy;
2014 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2015 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2016 int end;
2018 WARN_ON(start > offset + len);
2020 end = start + skb_frag_size(frag);
2021 if ((copy = end - offset) > 0) {
2022 u8 *vaddr;
2024 if (copy > len)
2025 copy = len;
2027 vaddr = kmap_atomic(skb_frag_page(frag));
2028 memcpy(vaddr + frag->page_offset + offset - start,
2029 from, copy);
2030 kunmap_atomic(vaddr);
2032 if ((len -= copy) == 0)
2033 return 0;
2034 offset += copy;
2035 from += copy;
2037 start = end;
2040 skb_walk_frags(skb, frag_iter) {
2041 int end;
2043 WARN_ON(start > offset + len);
2045 end = start + frag_iter->len;
2046 if ((copy = end - offset) > 0) {
2047 if (copy > len)
2048 copy = len;
2049 if (skb_store_bits(frag_iter, offset - start,
2050 from, copy))
2051 goto fault;
2052 if ((len -= copy) == 0)
2053 return 0;
2054 offset += copy;
2055 from += copy;
2057 start = end;
2059 if (!len)
2060 return 0;
2062 fault:
2063 return -EFAULT;
2065 EXPORT_SYMBOL(skb_store_bits);
2067 /* Checksum skb data. */
2068 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2069 __wsum csum, const struct skb_checksum_ops *ops)
2071 int start = skb_headlen(skb);
2072 int i, copy = start - offset;
2073 struct sk_buff *frag_iter;
2074 int pos = 0;
2076 /* Checksum header. */
2077 if (copy > 0) {
2078 if (copy > len)
2079 copy = len;
2080 csum = ops->update(skb->data + offset, copy, csum);
2081 if ((len -= copy) == 0)
2082 return csum;
2083 offset += copy;
2084 pos = copy;
2087 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2088 int end;
2089 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2091 WARN_ON(start > offset + len);
2093 end = start + skb_frag_size(frag);
2094 if ((copy = end - offset) > 0) {
2095 __wsum csum2;
2096 u8 *vaddr;
2098 if (copy > len)
2099 copy = len;
2100 vaddr = kmap_atomic(skb_frag_page(frag));
2101 csum2 = ops->update(vaddr + frag->page_offset +
2102 offset - start, copy, 0);
2103 kunmap_atomic(vaddr);
2104 csum = ops->combine(csum, csum2, pos, copy);
2105 if (!(len -= copy))
2106 return csum;
2107 offset += copy;
2108 pos += copy;
2110 start = end;
2113 skb_walk_frags(skb, frag_iter) {
2114 int end;
2116 WARN_ON(start > offset + len);
2118 end = start + frag_iter->len;
2119 if ((copy = end - offset) > 0) {
2120 __wsum csum2;
2121 if (copy > len)
2122 copy = len;
2123 csum2 = __skb_checksum(frag_iter, offset - start,
2124 copy, 0, ops);
2125 csum = ops->combine(csum, csum2, pos, copy);
2126 if ((len -= copy) == 0)
2127 return csum;
2128 offset += copy;
2129 pos += copy;
2131 start = end;
2133 BUG_ON(len);
2135 return csum;
2137 EXPORT_SYMBOL(__skb_checksum);
2139 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2140 int len, __wsum csum)
2142 const struct skb_checksum_ops ops = {
2143 .update = csum_partial_ext,
2144 .combine = csum_block_add_ext,
2147 return __skb_checksum(skb, offset, len, csum, &ops);
2149 EXPORT_SYMBOL(skb_checksum);
2151 /* Both of above in one bottle. */
2153 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2154 u8 *to, int len, __wsum csum)
2156 int start = skb_headlen(skb);
2157 int i, copy = start - offset;
2158 struct sk_buff *frag_iter;
2159 int pos = 0;
2161 /* Copy header. */
2162 if (copy > 0) {
2163 if (copy > len)
2164 copy = len;
2165 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2166 copy, csum);
2167 if ((len -= copy) == 0)
2168 return csum;
2169 offset += copy;
2170 to += copy;
2171 pos = copy;
2174 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2175 int end;
2177 WARN_ON(start > offset + len);
2179 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2180 if ((copy = end - offset) > 0) {
2181 __wsum csum2;
2182 u8 *vaddr;
2183 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2185 if (copy > len)
2186 copy = len;
2187 vaddr = kmap_atomic(skb_frag_page(frag));
2188 csum2 = csum_partial_copy_nocheck(vaddr +
2189 frag->page_offset +
2190 offset - start, to,
2191 copy, 0);
2192 kunmap_atomic(vaddr);
2193 csum = csum_block_add(csum, csum2, pos);
2194 if (!(len -= copy))
2195 return csum;
2196 offset += copy;
2197 to += copy;
2198 pos += copy;
2200 start = end;
2203 skb_walk_frags(skb, frag_iter) {
2204 __wsum csum2;
2205 int end;
2207 WARN_ON(start > offset + len);
2209 end = start + frag_iter->len;
2210 if ((copy = end - offset) > 0) {
2211 if (copy > len)
2212 copy = len;
2213 csum2 = skb_copy_and_csum_bits(frag_iter,
2214 offset - start,
2215 to, copy, 0);
2216 csum = csum_block_add(csum, csum2, pos);
2217 if ((len -= copy) == 0)
2218 return csum;
2219 offset += copy;
2220 to += copy;
2221 pos += copy;
2223 start = end;
2225 BUG_ON(len);
2226 return csum;
2228 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2231 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2232 * @from: source buffer
2234 * Calculates the amount of linear headroom needed in the 'to' skb passed
2235 * into skb_zerocopy().
2237 unsigned int
2238 skb_zerocopy_headlen(const struct sk_buff *from)
2240 unsigned int hlen = 0;
2242 if (!from->head_frag ||
2243 skb_headlen(from) < L1_CACHE_BYTES ||
2244 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2245 hlen = skb_headlen(from);
2247 if (skb_has_frag_list(from))
2248 hlen = from->len;
2250 return hlen;
2252 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2255 * skb_zerocopy - Zero copy skb to skb
2256 * @to: destination buffer
2257 * @from: source buffer
2258 * @len: number of bytes to copy from source buffer
2259 * @hlen: size of linear headroom in destination buffer
2261 * Copies up to `len` bytes from `from` to `to` by creating references
2262 * to the frags in the source buffer.
2264 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2265 * headroom in the `to` buffer.
2267 * Return value:
2268 * 0: everything is OK
2269 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2270 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2273 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2275 int i, j = 0;
2276 int plen = 0; /* length of skb->head fragment */
2277 int ret;
2278 struct page *page;
2279 unsigned int offset;
2281 BUG_ON(!from->head_frag && !hlen);
2283 /* dont bother with small payloads */
2284 if (len <= skb_tailroom(to))
2285 return skb_copy_bits(from, 0, skb_put(to, len), len);
2287 if (hlen) {
2288 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2289 if (unlikely(ret))
2290 return ret;
2291 len -= hlen;
2292 } else {
2293 plen = min_t(int, skb_headlen(from), len);
2294 if (plen) {
2295 page = virt_to_head_page(from->head);
2296 offset = from->data - (unsigned char *)page_address(page);
2297 __skb_fill_page_desc(to, 0, page, offset, plen);
2298 get_page(page);
2299 j = 1;
2300 len -= plen;
2304 to->truesize += len + plen;
2305 to->len += len + plen;
2306 to->data_len += len + plen;
2308 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2309 skb_tx_error(from);
2310 return -ENOMEM;
2313 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2314 if (!len)
2315 break;
2316 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2317 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2318 len -= skb_shinfo(to)->frags[j].size;
2319 skb_frag_ref(to, j);
2320 j++;
2322 skb_shinfo(to)->nr_frags = j;
2324 return 0;
2326 EXPORT_SYMBOL_GPL(skb_zerocopy);
2328 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2330 __wsum csum;
2331 long csstart;
2333 if (skb->ip_summed == CHECKSUM_PARTIAL)
2334 csstart = skb_checksum_start_offset(skb);
2335 else
2336 csstart = skb_headlen(skb);
2338 BUG_ON(csstart > skb_headlen(skb));
2340 skb_copy_from_linear_data(skb, to, csstart);
2342 csum = 0;
2343 if (csstart != skb->len)
2344 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2345 skb->len - csstart, 0);
2347 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2348 long csstuff = csstart + skb->csum_offset;
2350 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2353 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2356 * skb_dequeue - remove from the head of the queue
2357 * @list: list to dequeue from
2359 * Remove the head of the list. The list lock is taken so the function
2360 * may be used safely with other locking list functions. The head item is
2361 * returned or %NULL if the list is empty.
2364 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2366 unsigned long flags;
2367 struct sk_buff *result;
2369 spin_lock_irqsave(&list->lock, flags);
2370 result = __skb_dequeue(list);
2371 spin_unlock_irqrestore(&list->lock, flags);
2372 return result;
2374 EXPORT_SYMBOL(skb_dequeue);
2377 * skb_dequeue_tail - remove from the tail of the queue
2378 * @list: list to dequeue from
2380 * Remove the tail of the list. The list lock is taken so the function
2381 * may be used safely with other locking list functions. The tail item is
2382 * returned or %NULL if the list is empty.
2384 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2386 unsigned long flags;
2387 struct sk_buff *result;
2389 spin_lock_irqsave(&list->lock, flags);
2390 result = __skb_dequeue_tail(list);
2391 spin_unlock_irqrestore(&list->lock, flags);
2392 return result;
2394 EXPORT_SYMBOL(skb_dequeue_tail);
2397 * skb_queue_purge - empty a list
2398 * @list: list to empty
2400 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2401 * the list and one reference dropped. This function takes the list
2402 * lock and is atomic with respect to other list locking functions.
2404 void skb_queue_purge(struct sk_buff_head *list)
2406 struct sk_buff *skb;
2407 while ((skb = skb_dequeue(list)) != NULL)
2408 kfree_skb(skb);
2410 EXPORT_SYMBOL(skb_queue_purge);
2413 * skb_queue_head - queue a buffer at the list head
2414 * @list: list to use
2415 * @newsk: buffer to queue
2417 * Queue a buffer at the start of the list. This function takes the
2418 * list lock and can be used safely with other locking &sk_buff functions
2419 * safely.
2421 * A buffer cannot be placed on two lists at the same time.
2423 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2425 unsigned long flags;
2427 spin_lock_irqsave(&list->lock, flags);
2428 __skb_queue_head(list, newsk);
2429 spin_unlock_irqrestore(&list->lock, flags);
2431 EXPORT_SYMBOL(skb_queue_head);
2434 * skb_queue_tail - queue a buffer at the list tail
2435 * @list: list to use
2436 * @newsk: buffer to queue
2438 * Queue a buffer at the tail of the list. This function takes the
2439 * list lock and can be used safely with other locking &sk_buff functions
2440 * safely.
2442 * A buffer cannot be placed on two lists at the same time.
2444 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2446 unsigned long flags;
2448 spin_lock_irqsave(&list->lock, flags);
2449 __skb_queue_tail(list, newsk);
2450 spin_unlock_irqrestore(&list->lock, flags);
2452 EXPORT_SYMBOL(skb_queue_tail);
2455 * skb_unlink - remove a buffer from a list
2456 * @skb: buffer to remove
2457 * @list: list to use
2459 * Remove a packet from a list. The list locks are taken and this
2460 * function is atomic with respect to other list locked calls
2462 * You must know what list the SKB is on.
2464 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2466 unsigned long flags;
2468 spin_lock_irqsave(&list->lock, flags);
2469 __skb_unlink(skb, list);
2470 spin_unlock_irqrestore(&list->lock, flags);
2472 EXPORT_SYMBOL(skb_unlink);
2475 * skb_append - append a buffer
2476 * @old: buffer to insert after
2477 * @newsk: buffer to insert
2478 * @list: list to use
2480 * Place a packet after a given packet in a list. The list locks are taken
2481 * and this function is atomic with respect to other list locked calls.
2482 * A buffer cannot be placed on two lists at the same time.
2484 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2486 unsigned long flags;
2488 spin_lock_irqsave(&list->lock, flags);
2489 __skb_queue_after(list, old, newsk);
2490 spin_unlock_irqrestore(&list->lock, flags);
2492 EXPORT_SYMBOL(skb_append);
2495 * skb_insert - insert a buffer
2496 * @old: buffer to insert before
2497 * @newsk: buffer to insert
2498 * @list: list to use
2500 * Place a packet before a given packet in a list. The list locks are
2501 * taken and this function is atomic with respect to other list locked
2502 * calls.
2504 * A buffer cannot be placed on two lists at the same time.
2506 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2508 unsigned long flags;
2510 spin_lock_irqsave(&list->lock, flags);
2511 __skb_insert(newsk, old->prev, old, list);
2512 spin_unlock_irqrestore(&list->lock, flags);
2514 EXPORT_SYMBOL(skb_insert);
2516 static inline void skb_split_inside_header(struct sk_buff *skb,
2517 struct sk_buff* skb1,
2518 const u32 len, const int pos)
2520 int i;
2522 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2523 pos - len);
2524 /* And move data appendix as is. */
2525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2526 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2528 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2529 skb_shinfo(skb)->nr_frags = 0;
2530 skb1->data_len = skb->data_len;
2531 skb1->len += skb1->data_len;
2532 skb->data_len = 0;
2533 skb->len = len;
2534 skb_set_tail_pointer(skb, len);
2537 static inline void skb_split_no_header(struct sk_buff *skb,
2538 struct sk_buff* skb1,
2539 const u32 len, int pos)
2541 int i, k = 0;
2542 const int nfrags = skb_shinfo(skb)->nr_frags;
2544 skb_shinfo(skb)->nr_frags = 0;
2545 skb1->len = skb1->data_len = skb->len - len;
2546 skb->len = len;
2547 skb->data_len = len - pos;
2549 for (i = 0; i < nfrags; i++) {
2550 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2552 if (pos + size > len) {
2553 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2555 if (pos < len) {
2556 /* Split frag.
2557 * We have two variants in this case:
2558 * 1. Move all the frag to the second
2559 * part, if it is possible. F.e.
2560 * this approach is mandatory for TUX,
2561 * where splitting is expensive.
2562 * 2. Split is accurately. We make this.
2564 skb_frag_ref(skb, i);
2565 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2566 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2567 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2568 skb_shinfo(skb)->nr_frags++;
2570 k++;
2571 } else
2572 skb_shinfo(skb)->nr_frags++;
2573 pos += size;
2575 skb_shinfo(skb1)->nr_frags = k;
2579 * skb_split - Split fragmented skb to two parts at length len.
2580 * @skb: the buffer to split
2581 * @skb1: the buffer to receive the second part
2582 * @len: new length for skb
2584 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2586 int pos = skb_headlen(skb);
2588 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2589 if (len < pos) /* Split line is inside header. */
2590 skb_split_inside_header(skb, skb1, len, pos);
2591 else /* Second chunk has no header, nothing to copy. */
2592 skb_split_no_header(skb, skb1, len, pos);
2594 EXPORT_SYMBOL(skb_split);
2596 /* Shifting from/to a cloned skb is a no-go.
2598 * Caller cannot keep skb_shinfo related pointers past calling here!
2600 static int skb_prepare_for_shift(struct sk_buff *skb)
2602 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2606 * skb_shift - Shifts paged data partially from skb to another
2607 * @tgt: buffer into which tail data gets added
2608 * @skb: buffer from which the paged data comes from
2609 * @shiftlen: shift up to this many bytes
2611 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2612 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2613 * It's up to caller to free skb if everything was shifted.
2615 * If @tgt runs out of frags, the whole operation is aborted.
2617 * Skb cannot include anything else but paged data while tgt is allowed
2618 * to have non-paged data as well.
2620 * TODO: full sized shift could be optimized but that would need
2621 * specialized skb free'er to handle frags without up-to-date nr_frags.
2623 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2625 int from, to, merge, todo;
2626 struct skb_frag_struct *fragfrom, *fragto;
2628 BUG_ON(shiftlen > skb->len);
2629 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2631 todo = shiftlen;
2632 from = 0;
2633 to = skb_shinfo(tgt)->nr_frags;
2634 fragfrom = &skb_shinfo(skb)->frags[from];
2636 /* Actual merge is delayed until the point when we know we can
2637 * commit all, so that we don't have to undo partial changes
2639 if (!to ||
2640 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2641 fragfrom->page_offset)) {
2642 merge = -1;
2643 } else {
2644 merge = to - 1;
2646 todo -= skb_frag_size(fragfrom);
2647 if (todo < 0) {
2648 if (skb_prepare_for_shift(skb) ||
2649 skb_prepare_for_shift(tgt))
2650 return 0;
2652 /* All previous frag pointers might be stale! */
2653 fragfrom = &skb_shinfo(skb)->frags[from];
2654 fragto = &skb_shinfo(tgt)->frags[merge];
2656 skb_frag_size_add(fragto, shiftlen);
2657 skb_frag_size_sub(fragfrom, shiftlen);
2658 fragfrom->page_offset += shiftlen;
2660 goto onlymerged;
2663 from++;
2666 /* Skip full, not-fitting skb to avoid expensive operations */
2667 if ((shiftlen == skb->len) &&
2668 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2669 return 0;
2671 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2672 return 0;
2674 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2675 if (to == MAX_SKB_FRAGS)
2676 return 0;
2678 fragfrom = &skb_shinfo(skb)->frags[from];
2679 fragto = &skb_shinfo(tgt)->frags[to];
2681 if (todo >= skb_frag_size(fragfrom)) {
2682 *fragto = *fragfrom;
2683 todo -= skb_frag_size(fragfrom);
2684 from++;
2685 to++;
2687 } else {
2688 __skb_frag_ref(fragfrom);
2689 fragto->page = fragfrom->page;
2690 fragto->page_offset = fragfrom->page_offset;
2691 skb_frag_size_set(fragto, todo);
2693 fragfrom->page_offset += todo;
2694 skb_frag_size_sub(fragfrom, todo);
2695 todo = 0;
2697 to++;
2698 break;
2702 /* Ready to "commit" this state change to tgt */
2703 skb_shinfo(tgt)->nr_frags = to;
2705 if (merge >= 0) {
2706 fragfrom = &skb_shinfo(skb)->frags[0];
2707 fragto = &skb_shinfo(tgt)->frags[merge];
2709 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2710 __skb_frag_unref(fragfrom);
2713 /* Reposition in the original skb */
2714 to = 0;
2715 while (from < skb_shinfo(skb)->nr_frags)
2716 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2717 skb_shinfo(skb)->nr_frags = to;
2719 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2721 onlymerged:
2722 /* Most likely the tgt won't ever need its checksum anymore, skb on
2723 * the other hand might need it if it needs to be resent
2725 tgt->ip_summed = CHECKSUM_PARTIAL;
2726 skb->ip_summed = CHECKSUM_PARTIAL;
2728 /* Yak, is it really working this way? Some helper please? */
2729 skb->len -= shiftlen;
2730 skb->data_len -= shiftlen;
2731 skb->truesize -= shiftlen;
2732 tgt->len += shiftlen;
2733 tgt->data_len += shiftlen;
2734 tgt->truesize += shiftlen;
2736 return shiftlen;
2740 * skb_prepare_seq_read - Prepare a sequential read of skb data
2741 * @skb: the buffer to read
2742 * @from: lower offset of data to be read
2743 * @to: upper offset of data to be read
2744 * @st: state variable
2746 * Initializes the specified state variable. Must be called before
2747 * invoking skb_seq_read() for the first time.
2749 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2750 unsigned int to, struct skb_seq_state *st)
2752 st->lower_offset = from;
2753 st->upper_offset = to;
2754 st->root_skb = st->cur_skb = skb;
2755 st->frag_idx = st->stepped_offset = 0;
2756 st->frag_data = NULL;
2758 EXPORT_SYMBOL(skb_prepare_seq_read);
2761 * skb_seq_read - Sequentially read skb data
2762 * @consumed: number of bytes consumed by the caller so far
2763 * @data: destination pointer for data to be returned
2764 * @st: state variable
2766 * Reads a block of skb data at @consumed relative to the
2767 * lower offset specified to skb_prepare_seq_read(). Assigns
2768 * the head of the data block to @data and returns the length
2769 * of the block or 0 if the end of the skb data or the upper
2770 * offset has been reached.
2772 * The caller is not required to consume all of the data
2773 * returned, i.e. @consumed is typically set to the number
2774 * of bytes already consumed and the next call to
2775 * skb_seq_read() will return the remaining part of the block.
2777 * Note 1: The size of each block of data returned can be arbitrary,
2778 * this limitation is the cost for zerocopy sequential
2779 * reads of potentially non linear data.
2781 * Note 2: Fragment lists within fragments are not implemented
2782 * at the moment, state->root_skb could be replaced with
2783 * a stack for this purpose.
2785 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2786 struct skb_seq_state *st)
2788 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2789 skb_frag_t *frag;
2791 if (unlikely(abs_offset >= st->upper_offset)) {
2792 if (st->frag_data) {
2793 kunmap_atomic(st->frag_data);
2794 st->frag_data = NULL;
2796 return 0;
2799 next_skb:
2800 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2802 if (abs_offset < block_limit && !st->frag_data) {
2803 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2804 return block_limit - abs_offset;
2807 if (st->frag_idx == 0 && !st->frag_data)
2808 st->stepped_offset += skb_headlen(st->cur_skb);
2810 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2811 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2812 block_limit = skb_frag_size(frag) + st->stepped_offset;
2814 if (abs_offset < block_limit) {
2815 if (!st->frag_data)
2816 st->frag_data = kmap_atomic(skb_frag_page(frag));
2818 *data = (u8 *) st->frag_data + frag->page_offset +
2819 (abs_offset - st->stepped_offset);
2821 return block_limit - abs_offset;
2824 if (st->frag_data) {
2825 kunmap_atomic(st->frag_data);
2826 st->frag_data = NULL;
2829 st->frag_idx++;
2830 st->stepped_offset += skb_frag_size(frag);
2833 if (st->frag_data) {
2834 kunmap_atomic(st->frag_data);
2835 st->frag_data = NULL;
2838 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2839 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2840 st->frag_idx = 0;
2841 goto next_skb;
2842 } else if (st->cur_skb->next) {
2843 st->cur_skb = st->cur_skb->next;
2844 st->frag_idx = 0;
2845 goto next_skb;
2848 return 0;
2850 EXPORT_SYMBOL(skb_seq_read);
2853 * skb_abort_seq_read - Abort a sequential read of skb data
2854 * @st: state variable
2856 * Must be called if skb_seq_read() was not called until it
2857 * returned 0.
2859 void skb_abort_seq_read(struct skb_seq_state *st)
2861 if (st->frag_data)
2862 kunmap_atomic(st->frag_data);
2864 EXPORT_SYMBOL(skb_abort_seq_read);
2866 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2868 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2869 struct ts_config *conf,
2870 struct ts_state *state)
2872 return skb_seq_read(offset, text, TS_SKB_CB(state));
2875 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2877 skb_abort_seq_read(TS_SKB_CB(state));
2881 * skb_find_text - Find a text pattern in skb data
2882 * @skb: the buffer to look in
2883 * @from: search offset
2884 * @to: search limit
2885 * @config: textsearch configuration
2887 * Finds a pattern in the skb data according to the specified
2888 * textsearch configuration. Use textsearch_next() to retrieve
2889 * subsequent occurrences of the pattern. Returns the offset
2890 * to the first occurrence or UINT_MAX if no match was found.
2892 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2893 unsigned int to, struct ts_config *config)
2895 struct ts_state state;
2896 unsigned int ret;
2898 config->get_next_block = skb_ts_get_next_block;
2899 config->finish = skb_ts_finish;
2901 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2903 ret = textsearch_find(config, &state);
2904 return (ret <= to - from ? ret : UINT_MAX);
2906 EXPORT_SYMBOL(skb_find_text);
2909 * skb_append_datato_frags - append the user data to a skb
2910 * @sk: sock structure
2911 * @skb: skb structure to be appended with user data.
2912 * @getfrag: call back function to be used for getting the user data
2913 * @from: pointer to user message iov
2914 * @length: length of the iov message
2916 * Description: This procedure append the user data in the fragment part
2917 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2919 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2920 int (*getfrag)(void *from, char *to, int offset,
2921 int len, int odd, struct sk_buff *skb),
2922 void *from, int length)
2924 int frg_cnt = skb_shinfo(skb)->nr_frags;
2925 int copy;
2926 int offset = 0;
2927 int ret;
2928 struct page_frag *pfrag = &current->task_frag;
2930 do {
2931 /* Return error if we don't have space for new frag */
2932 if (frg_cnt >= MAX_SKB_FRAGS)
2933 return -EMSGSIZE;
2935 if (!sk_page_frag_refill(sk, pfrag))
2936 return -ENOMEM;
2938 /* copy the user data to page */
2939 copy = min_t(int, length, pfrag->size - pfrag->offset);
2941 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2942 offset, copy, 0, skb);
2943 if (ret < 0)
2944 return -EFAULT;
2946 /* copy was successful so update the size parameters */
2947 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2948 copy);
2949 frg_cnt++;
2950 pfrag->offset += copy;
2951 get_page(pfrag->page);
2953 skb->truesize += copy;
2954 atomic_add(copy, &sk->sk_wmem_alloc);
2955 skb->len += copy;
2956 skb->data_len += copy;
2957 offset += copy;
2958 length -= copy;
2960 } while (length > 0);
2962 return 0;
2964 EXPORT_SYMBOL(skb_append_datato_frags);
2967 * skb_pull_rcsum - pull skb and update receive checksum
2968 * @skb: buffer to update
2969 * @len: length of data pulled
2971 * This function performs an skb_pull on the packet and updates
2972 * the CHECKSUM_COMPLETE checksum. It should be used on
2973 * receive path processing instead of skb_pull unless you know
2974 * that the checksum difference is zero (e.g., a valid IP header)
2975 * or you are setting ip_summed to CHECKSUM_NONE.
2977 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2979 unsigned char *data = skb->data;
2981 BUG_ON(len > skb->len);
2982 __skb_pull(skb, len);
2983 skb_postpull_rcsum(skb, data, len);
2984 return skb->data;
2986 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2989 * skb_segment - Perform protocol segmentation on skb.
2990 * @head_skb: buffer to segment
2991 * @features: features for the output path (see dev->features)
2993 * This function performs segmentation on the given skb. It returns
2994 * a pointer to the first in a list of new skbs for the segments.
2995 * In case of error it returns ERR_PTR(err).
2997 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2998 netdev_features_t features)
3000 struct sk_buff *segs = NULL;
3001 struct sk_buff *tail = NULL;
3002 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3003 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3004 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3005 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3006 struct sk_buff *frag_skb = head_skb;
3007 unsigned int offset = doffset;
3008 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3009 unsigned int headroom;
3010 unsigned int len;
3011 __be16 proto;
3012 bool csum;
3013 int sg = !!(features & NETIF_F_SG);
3014 int nfrags = skb_shinfo(head_skb)->nr_frags;
3015 int err = -ENOMEM;
3016 int i = 0;
3017 int pos;
3018 int dummy;
3020 __skb_push(head_skb, doffset);
3021 proto = skb_network_protocol(head_skb, &dummy);
3022 if (unlikely(!proto))
3023 return ERR_PTR(-EINVAL);
3025 csum = !head_skb->encap_hdr_csum &&
3026 !!can_checksum_protocol(features, proto);
3028 headroom = skb_headroom(head_skb);
3029 pos = skb_headlen(head_skb);
3031 do {
3032 struct sk_buff *nskb;
3033 skb_frag_t *nskb_frag;
3034 int hsize;
3035 int size;
3037 len = head_skb->len - offset;
3038 if (len > mss)
3039 len = mss;
3041 hsize = skb_headlen(head_skb) - offset;
3042 if (hsize < 0)
3043 hsize = 0;
3044 if (hsize > len || !sg)
3045 hsize = len;
3047 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3048 (skb_headlen(list_skb) == len || sg)) {
3049 BUG_ON(skb_headlen(list_skb) > len);
3051 i = 0;
3052 nfrags = skb_shinfo(list_skb)->nr_frags;
3053 frag = skb_shinfo(list_skb)->frags;
3054 frag_skb = list_skb;
3055 pos += skb_headlen(list_skb);
3057 while (pos < offset + len) {
3058 BUG_ON(i >= nfrags);
3060 size = skb_frag_size(frag);
3061 if (pos + size > offset + len)
3062 break;
3064 i++;
3065 pos += size;
3066 frag++;
3069 nskb = skb_clone(list_skb, GFP_ATOMIC);
3070 list_skb = list_skb->next;
3072 if (unlikely(!nskb))
3073 goto err;
3075 if (unlikely(pskb_trim(nskb, len))) {
3076 kfree_skb(nskb);
3077 goto err;
3080 hsize = skb_end_offset(nskb);
3081 if (skb_cow_head(nskb, doffset + headroom)) {
3082 kfree_skb(nskb);
3083 goto err;
3086 nskb->truesize += skb_end_offset(nskb) - hsize;
3087 skb_release_head_state(nskb);
3088 __skb_push(nskb, doffset);
3089 } else {
3090 nskb = __alloc_skb(hsize + doffset + headroom,
3091 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3092 NUMA_NO_NODE);
3094 if (unlikely(!nskb))
3095 goto err;
3097 skb_reserve(nskb, headroom);
3098 __skb_put(nskb, doffset);
3101 if (segs)
3102 tail->next = nskb;
3103 else
3104 segs = nskb;
3105 tail = nskb;
3107 __copy_skb_header(nskb, head_skb);
3109 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3110 skb_reset_mac_len(nskb);
3112 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3113 nskb->data - tnl_hlen,
3114 doffset + tnl_hlen);
3116 if (nskb->len == len + doffset)
3117 goto perform_csum_check;
3119 if (!sg && !nskb->remcsum_offload) {
3120 nskb->ip_summed = CHECKSUM_NONE;
3121 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3122 skb_put(nskb, len),
3123 len, 0);
3124 SKB_GSO_CB(nskb)->csum_start =
3125 skb_headroom(nskb) + doffset;
3126 continue;
3129 nskb_frag = skb_shinfo(nskb)->frags;
3131 skb_copy_from_linear_data_offset(head_skb, offset,
3132 skb_put(nskb, hsize), hsize);
3134 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3135 SKBTX_SHARED_FRAG;
3137 while (pos < offset + len) {
3138 if (i >= nfrags) {
3139 BUG_ON(skb_headlen(list_skb));
3141 i = 0;
3142 nfrags = skb_shinfo(list_skb)->nr_frags;
3143 frag = skb_shinfo(list_skb)->frags;
3144 frag_skb = list_skb;
3146 BUG_ON(!nfrags);
3148 list_skb = list_skb->next;
3151 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3152 MAX_SKB_FRAGS)) {
3153 net_warn_ratelimited(
3154 "skb_segment: too many frags: %u %u\n",
3155 pos, mss);
3156 goto err;
3159 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3160 goto err;
3162 *nskb_frag = *frag;
3163 __skb_frag_ref(nskb_frag);
3164 size = skb_frag_size(nskb_frag);
3166 if (pos < offset) {
3167 nskb_frag->page_offset += offset - pos;
3168 skb_frag_size_sub(nskb_frag, offset - pos);
3171 skb_shinfo(nskb)->nr_frags++;
3173 if (pos + size <= offset + len) {
3174 i++;
3175 frag++;
3176 pos += size;
3177 } else {
3178 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3179 goto skip_fraglist;
3182 nskb_frag++;
3185 skip_fraglist:
3186 nskb->data_len = len - hsize;
3187 nskb->len += nskb->data_len;
3188 nskb->truesize += nskb->data_len;
3190 perform_csum_check:
3191 if (!csum && !nskb->remcsum_offload) {
3192 nskb->csum = skb_checksum(nskb, doffset,
3193 nskb->len - doffset, 0);
3194 nskb->ip_summed = CHECKSUM_NONE;
3195 SKB_GSO_CB(nskb)->csum_start =
3196 skb_headroom(nskb) + doffset;
3198 } while ((offset += len) < head_skb->len);
3200 /* Some callers want to get the end of the list.
3201 * Put it in segs->prev to avoid walking the list.
3202 * (see validate_xmit_skb_list() for example)
3204 segs->prev = tail;
3206 /* Following permits correct backpressure, for protocols
3207 * using skb_set_owner_w().
3208 * Idea is to tranfert ownership from head_skb to last segment.
3210 if (head_skb->destructor == sock_wfree) {
3211 swap(tail->truesize, head_skb->truesize);
3212 swap(tail->destructor, head_skb->destructor);
3213 swap(tail->sk, head_skb->sk);
3215 return segs;
3217 err:
3218 kfree_skb_list(segs);
3219 return ERR_PTR(err);
3221 EXPORT_SYMBOL_GPL(skb_segment);
3223 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3225 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3226 unsigned int offset = skb_gro_offset(skb);
3227 unsigned int headlen = skb_headlen(skb);
3228 unsigned int len = skb_gro_len(skb);
3229 struct sk_buff *lp, *p = *head;
3230 unsigned int delta_truesize;
3232 if (unlikely(p->len + len >= 65536))
3233 return -E2BIG;
3235 lp = NAPI_GRO_CB(p)->last;
3236 pinfo = skb_shinfo(lp);
3238 if (headlen <= offset) {
3239 skb_frag_t *frag;
3240 skb_frag_t *frag2;
3241 int i = skbinfo->nr_frags;
3242 int nr_frags = pinfo->nr_frags + i;
3244 if (nr_frags > MAX_SKB_FRAGS)
3245 goto merge;
3247 offset -= headlen;
3248 pinfo->nr_frags = nr_frags;
3249 skbinfo->nr_frags = 0;
3251 frag = pinfo->frags + nr_frags;
3252 frag2 = skbinfo->frags + i;
3253 do {
3254 *--frag = *--frag2;
3255 } while (--i);
3257 frag->page_offset += offset;
3258 skb_frag_size_sub(frag, offset);
3260 /* all fragments truesize : remove (head size + sk_buff) */
3261 delta_truesize = skb->truesize -
3262 SKB_TRUESIZE(skb_end_offset(skb));
3264 skb->truesize -= skb->data_len;
3265 skb->len -= skb->data_len;
3266 skb->data_len = 0;
3268 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3269 goto done;
3270 } else if (skb->head_frag) {
3271 int nr_frags = pinfo->nr_frags;
3272 skb_frag_t *frag = pinfo->frags + nr_frags;
3273 struct page *page = virt_to_head_page(skb->head);
3274 unsigned int first_size = headlen - offset;
3275 unsigned int first_offset;
3277 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3278 goto merge;
3280 first_offset = skb->data -
3281 (unsigned char *)page_address(page) +
3282 offset;
3284 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3286 frag->page.p = page;
3287 frag->page_offset = first_offset;
3288 skb_frag_size_set(frag, first_size);
3290 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3291 /* We dont need to clear skbinfo->nr_frags here */
3293 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3294 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3295 goto done;
3298 merge:
3299 delta_truesize = skb->truesize;
3300 if (offset > headlen) {
3301 unsigned int eat = offset - headlen;
3303 skbinfo->frags[0].page_offset += eat;
3304 skb_frag_size_sub(&skbinfo->frags[0], eat);
3305 skb->data_len -= eat;
3306 skb->len -= eat;
3307 offset = headlen;
3310 __skb_pull(skb, offset);
3312 if (NAPI_GRO_CB(p)->last == p)
3313 skb_shinfo(p)->frag_list = skb;
3314 else
3315 NAPI_GRO_CB(p)->last->next = skb;
3316 NAPI_GRO_CB(p)->last = skb;
3317 __skb_header_release(skb);
3318 lp = p;
3320 done:
3321 NAPI_GRO_CB(p)->count++;
3322 p->data_len += len;
3323 p->truesize += delta_truesize;
3324 p->len += len;
3325 if (lp != p) {
3326 lp->data_len += len;
3327 lp->truesize += delta_truesize;
3328 lp->len += len;
3330 NAPI_GRO_CB(skb)->same_flow = 1;
3331 return 0;
3334 void __init skb_init(void)
3336 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3337 sizeof(struct sk_buff),
3339 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3340 NULL);
3341 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3342 sizeof(struct sk_buff_fclones),
3344 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3345 NULL);
3349 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3350 * @skb: Socket buffer containing the buffers to be mapped
3351 * @sg: The scatter-gather list to map into
3352 * @offset: The offset into the buffer's contents to start mapping
3353 * @len: Length of buffer space to be mapped
3355 * Fill the specified scatter-gather list with mappings/pointers into a
3356 * region of the buffer space attached to a socket buffer.
3358 static int
3359 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3361 int start = skb_headlen(skb);
3362 int i, copy = start - offset;
3363 struct sk_buff *frag_iter;
3364 int elt = 0;
3366 if (copy > 0) {
3367 if (copy > len)
3368 copy = len;
3369 sg_set_buf(sg, skb->data + offset, copy);
3370 elt++;
3371 if ((len -= copy) == 0)
3372 return elt;
3373 offset += copy;
3376 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3377 int end;
3379 WARN_ON(start > offset + len);
3381 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3382 if ((copy = end - offset) > 0) {
3383 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3385 if (copy > len)
3386 copy = len;
3387 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3388 frag->page_offset+offset-start);
3389 elt++;
3390 if (!(len -= copy))
3391 return elt;
3392 offset += copy;
3394 start = end;
3397 skb_walk_frags(skb, frag_iter) {
3398 int end;
3400 WARN_ON(start > offset + len);
3402 end = start + frag_iter->len;
3403 if ((copy = end - offset) > 0) {
3404 if (copy > len)
3405 copy = len;
3406 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3407 copy);
3408 if ((len -= copy) == 0)
3409 return elt;
3410 offset += copy;
3412 start = end;
3414 BUG_ON(len);
3415 return elt;
3418 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3419 * sglist without mark the sg which contain last skb data as the end.
3420 * So the caller can mannipulate sg list as will when padding new data after
3421 * the first call without calling sg_unmark_end to expend sg list.
3423 * Scenario to use skb_to_sgvec_nomark:
3424 * 1. sg_init_table
3425 * 2. skb_to_sgvec_nomark(payload1)
3426 * 3. skb_to_sgvec_nomark(payload2)
3428 * This is equivalent to:
3429 * 1. sg_init_table
3430 * 2. skb_to_sgvec(payload1)
3431 * 3. sg_unmark_end
3432 * 4. skb_to_sgvec(payload2)
3434 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3435 * is more preferable.
3437 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3438 int offset, int len)
3440 return __skb_to_sgvec(skb, sg, offset, len);
3442 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3444 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3446 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3448 sg_mark_end(&sg[nsg - 1]);
3450 return nsg;
3452 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3455 * skb_cow_data - Check that a socket buffer's data buffers are writable
3456 * @skb: The socket buffer to check.
3457 * @tailbits: Amount of trailing space to be added
3458 * @trailer: Returned pointer to the skb where the @tailbits space begins
3460 * Make sure that the data buffers attached to a socket buffer are
3461 * writable. If they are not, private copies are made of the data buffers
3462 * and the socket buffer is set to use these instead.
3464 * If @tailbits is given, make sure that there is space to write @tailbits
3465 * bytes of data beyond current end of socket buffer. @trailer will be
3466 * set to point to the skb in which this space begins.
3468 * The number of scatterlist elements required to completely map the
3469 * COW'd and extended socket buffer will be returned.
3471 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3473 int copyflag;
3474 int elt;
3475 struct sk_buff *skb1, **skb_p;
3477 /* If skb is cloned or its head is paged, reallocate
3478 * head pulling out all the pages (pages are considered not writable
3479 * at the moment even if they are anonymous).
3481 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3482 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3483 return -ENOMEM;
3485 /* Easy case. Most of packets will go this way. */
3486 if (!skb_has_frag_list(skb)) {
3487 /* A little of trouble, not enough of space for trailer.
3488 * This should not happen, when stack is tuned to generate
3489 * good frames. OK, on miss we reallocate and reserve even more
3490 * space, 128 bytes is fair. */
3492 if (skb_tailroom(skb) < tailbits &&
3493 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3494 return -ENOMEM;
3496 /* Voila! */
3497 *trailer = skb;
3498 return 1;
3501 /* Misery. We are in troubles, going to mincer fragments... */
3503 elt = 1;
3504 skb_p = &skb_shinfo(skb)->frag_list;
3505 copyflag = 0;
3507 while ((skb1 = *skb_p) != NULL) {
3508 int ntail = 0;
3510 /* The fragment is partially pulled by someone,
3511 * this can happen on input. Copy it and everything
3512 * after it. */
3514 if (skb_shared(skb1))
3515 copyflag = 1;
3517 /* If the skb is the last, worry about trailer. */
3519 if (skb1->next == NULL && tailbits) {
3520 if (skb_shinfo(skb1)->nr_frags ||
3521 skb_has_frag_list(skb1) ||
3522 skb_tailroom(skb1) < tailbits)
3523 ntail = tailbits + 128;
3526 if (copyflag ||
3527 skb_cloned(skb1) ||
3528 ntail ||
3529 skb_shinfo(skb1)->nr_frags ||
3530 skb_has_frag_list(skb1)) {
3531 struct sk_buff *skb2;
3533 /* Fuck, we are miserable poor guys... */
3534 if (ntail == 0)
3535 skb2 = skb_copy(skb1, GFP_ATOMIC);
3536 else
3537 skb2 = skb_copy_expand(skb1,
3538 skb_headroom(skb1),
3539 ntail,
3540 GFP_ATOMIC);
3541 if (unlikely(skb2 == NULL))
3542 return -ENOMEM;
3544 if (skb1->sk)
3545 skb_set_owner_w(skb2, skb1->sk);
3547 /* Looking around. Are we still alive?
3548 * OK, link new skb, drop old one */
3550 skb2->next = skb1->next;
3551 *skb_p = skb2;
3552 kfree_skb(skb1);
3553 skb1 = skb2;
3555 elt++;
3556 *trailer = skb1;
3557 skb_p = &skb1->next;
3560 return elt;
3562 EXPORT_SYMBOL_GPL(skb_cow_data);
3564 static void sock_rmem_free(struct sk_buff *skb)
3566 struct sock *sk = skb->sk;
3568 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3572 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3574 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3576 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3577 (unsigned int)sk->sk_rcvbuf)
3578 return -ENOMEM;
3580 skb_orphan(skb);
3581 skb->sk = sk;
3582 skb->destructor = sock_rmem_free;
3583 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3585 /* before exiting rcu section, make sure dst is refcounted */
3586 skb_dst_force(skb);
3588 skb_queue_tail(&sk->sk_error_queue, skb);
3589 if (!sock_flag(sk, SOCK_DEAD))
3590 sk->sk_data_ready(sk);
3591 return 0;
3593 EXPORT_SYMBOL(sock_queue_err_skb);
3595 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3597 struct sk_buff_head *q = &sk->sk_error_queue;
3598 struct sk_buff *skb, *skb_next;
3599 unsigned long flags;
3600 int err = 0;
3602 spin_lock_irqsave(&q->lock, flags);
3603 skb = __skb_dequeue(q);
3604 if (skb && (skb_next = skb_peek(q)))
3605 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3606 spin_unlock_irqrestore(&q->lock, flags);
3608 sk->sk_err = err;
3609 if (err)
3610 sk->sk_error_report(sk);
3612 return skb;
3614 EXPORT_SYMBOL(sock_dequeue_err_skb);
3617 * skb_clone_sk - create clone of skb, and take reference to socket
3618 * @skb: the skb to clone
3620 * This function creates a clone of a buffer that holds a reference on
3621 * sk_refcnt. Buffers created via this function are meant to be
3622 * returned using sock_queue_err_skb, or free via kfree_skb.
3624 * When passing buffers allocated with this function to sock_queue_err_skb
3625 * it is necessary to wrap the call with sock_hold/sock_put in order to
3626 * prevent the socket from being released prior to being enqueued on
3627 * the sk_error_queue.
3629 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3631 struct sock *sk = skb->sk;
3632 struct sk_buff *clone;
3634 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3635 return NULL;
3637 clone = skb_clone(skb, GFP_ATOMIC);
3638 if (!clone) {
3639 sock_put(sk);
3640 return NULL;
3643 clone->sk = sk;
3644 clone->destructor = sock_efree;
3646 return clone;
3648 EXPORT_SYMBOL(skb_clone_sk);
3650 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3651 struct sock *sk,
3652 int tstype)
3654 struct sock_exterr_skb *serr;
3655 int err;
3657 serr = SKB_EXT_ERR(skb);
3658 memset(serr, 0, sizeof(*serr));
3659 serr->ee.ee_errno = ENOMSG;
3660 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3661 serr->ee.ee_info = tstype;
3662 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3663 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3664 if (sk->sk_protocol == IPPROTO_TCP &&
3665 sk->sk_type == SOCK_STREAM)
3666 serr->ee.ee_data -= sk->sk_tskey;
3669 err = sock_queue_err_skb(sk, skb);
3671 if (err)
3672 kfree_skb(skb);
3675 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3677 bool ret;
3679 if (likely(sysctl_tstamp_allow_data || tsonly))
3680 return true;
3682 read_lock_bh(&sk->sk_callback_lock);
3683 ret = sk->sk_socket && sk->sk_socket->file &&
3684 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3685 read_unlock_bh(&sk->sk_callback_lock);
3686 return ret;
3689 void skb_complete_tx_timestamp(struct sk_buff *skb,
3690 struct skb_shared_hwtstamps *hwtstamps)
3692 struct sock *sk = skb->sk;
3694 if (!skb_may_tx_timestamp(sk, false))
3695 return;
3697 /* take a reference to prevent skb_orphan() from freeing the socket */
3698 sock_hold(sk);
3700 *skb_hwtstamps(skb) = *hwtstamps;
3701 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3703 sock_put(sk);
3705 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3707 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3708 struct skb_shared_hwtstamps *hwtstamps,
3709 struct sock *sk, int tstype)
3711 struct sk_buff *skb;
3712 bool tsonly;
3714 if (!sk)
3715 return;
3717 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3718 if (!skb_may_tx_timestamp(sk, tsonly))
3719 return;
3721 if (tsonly)
3722 skb = alloc_skb(0, GFP_ATOMIC);
3723 else
3724 skb = skb_clone(orig_skb, GFP_ATOMIC);
3725 if (!skb)
3726 return;
3728 if (tsonly) {
3729 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3730 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3733 if (hwtstamps)
3734 *skb_hwtstamps(skb) = *hwtstamps;
3735 else
3736 skb->tstamp = ktime_get_real();
3738 __skb_complete_tx_timestamp(skb, sk, tstype);
3740 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3742 void skb_tstamp_tx(struct sk_buff *orig_skb,
3743 struct skb_shared_hwtstamps *hwtstamps)
3745 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3746 SCM_TSTAMP_SND);
3748 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3750 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3752 struct sock *sk = skb->sk;
3753 struct sock_exterr_skb *serr;
3754 int err;
3756 skb->wifi_acked_valid = 1;
3757 skb->wifi_acked = acked;
3759 serr = SKB_EXT_ERR(skb);
3760 memset(serr, 0, sizeof(*serr));
3761 serr->ee.ee_errno = ENOMSG;
3762 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3764 /* take a reference to prevent skb_orphan() from freeing the socket */
3765 sock_hold(sk);
3767 err = sock_queue_err_skb(sk, skb);
3768 if (err)
3769 kfree_skb(skb);
3771 sock_put(sk);
3773 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3776 * skb_partial_csum_set - set up and verify partial csum values for packet
3777 * @skb: the skb to set
3778 * @start: the number of bytes after skb->data to start checksumming.
3779 * @off: the offset from start to place the checksum.
3781 * For untrusted partially-checksummed packets, we need to make sure the values
3782 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3784 * This function checks and sets those values and skb->ip_summed: if this
3785 * returns false you should drop the packet.
3787 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3789 if (unlikely(start > skb_headlen(skb)) ||
3790 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3791 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3792 start, off, skb_headlen(skb));
3793 return false;
3795 skb->ip_summed = CHECKSUM_PARTIAL;
3796 skb->csum_start = skb_headroom(skb) + start;
3797 skb->csum_offset = off;
3798 skb_set_transport_header(skb, start);
3799 return true;
3801 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3803 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3804 unsigned int max)
3806 if (skb_headlen(skb) >= len)
3807 return 0;
3809 /* If we need to pullup then pullup to the max, so we
3810 * won't need to do it again.
3812 if (max > skb->len)
3813 max = skb->len;
3815 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3816 return -ENOMEM;
3818 if (skb_headlen(skb) < len)
3819 return -EPROTO;
3821 return 0;
3824 #define MAX_TCP_HDR_LEN (15 * 4)
3826 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3827 typeof(IPPROTO_IP) proto,
3828 unsigned int off)
3830 switch (proto) {
3831 int err;
3833 case IPPROTO_TCP:
3834 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3835 off + MAX_TCP_HDR_LEN);
3836 if (!err && !skb_partial_csum_set(skb, off,
3837 offsetof(struct tcphdr,
3838 check)))
3839 err = -EPROTO;
3840 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3842 case IPPROTO_UDP:
3843 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3844 off + sizeof(struct udphdr));
3845 if (!err && !skb_partial_csum_set(skb, off,
3846 offsetof(struct udphdr,
3847 check)))
3848 err = -EPROTO;
3849 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3852 return ERR_PTR(-EPROTO);
3855 /* This value should be large enough to cover a tagged ethernet header plus
3856 * maximally sized IP and TCP or UDP headers.
3858 #define MAX_IP_HDR_LEN 128
3860 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3862 unsigned int off;
3863 bool fragment;
3864 __sum16 *csum;
3865 int err;
3867 fragment = false;
3869 err = skb_maybe_pull_tail(skb,
3870 sizeof(struct iphdr),
3871 MAX_IP_HDR_LEN);
3872 if (err < 0)
3873 goto out;
3875 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3876 fragment = true;
3878 off = ip_hdrlen(skb);
3880 err = -EPROTO;
3882 if (fragment)
3883 goto out;
3885 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3886 if (IS_ERR(csum))
3887 return PTR_ERR(csum);
3889 if (recalculate)
3890 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3891 ip_hdr(skb)->daddr,
3892 skb->len - off,
3893 ip_hdr(skb)->protocol, 0);
3894 err = 0;
3896 out:
3897 return err;
3900 /* This value should be large enough to cover a tagged ethernet header plus
3901 * an IPv6 header, all options, and a maximal TCP or UDP header.
3903 #define MAX_IPV6_HDR_LEN 256
3905 #define OPT_HDR(type, skb, off) \
3906 (type *)(skb_network_header(skb) + (off))
3908 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3910 int err;
3911 u8 nexthdr;
3912 unsigned int off;
3913 unsigned int len;
3914 bool fragment;
3915 bool done;
3916 __sum16 *csum;
3918 fragment = false;
3919 done = false;
3921 off = sizeof(struct ipv6hdr);
3923 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3924 if (err < 0)
3925 goto out;
3927 nexthdr = ipv6_hdr(skb)->nexthdr;
3929 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3930 while (off <= len && !done) {
3931 switch (nexthdr) {
3932 case IPPROTO_DSTOPTS:
3933 case IPPROTO_HOPOPTS:
3934 case IPPROTO_ROUTING: {
3935 struct ipv6_opt_hdr *hp;
3937 err = skb_maybe_pull_tail(skb,
3938 off +
3939 sizeof(struct ipv6_opt_hdr),
3940 MAX_IPV6_HDR_LEN);
3941 if (err < 0)
3942 goto out;
3944 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3945 nexthdr = hp->nexthdr;
3946 off += ipv6_optlen(hp);
3947 break;
3949 case IPPROTO_AH: {
3950 struct ip_auth_hdr *hp;
3952 err = skb_maybe_pull_tail(skb,
3953 off +
3954 sizeof(struct ip_auth_hdr),
3955 MAX_IPV6_HDR_LEN);
3956 if (err < 0)
3957 goto out;
3959 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3960 nexthdr = hp->nexthdr;
3961 off += ipv6_authlen(hp);
3962 break;
3964 case IPPROTO_FRAGMENT: {
3965 struct frag_hdr *hp;
3967 err = skb_maybe_pull_tail(skb,
3968 off +
3969 sizeof(struct frag_hdr),
3970 MAX_IPV6_HDR_LEN);
3971 if (err < 0)
3972 goto out;
3974 hp = OPT_HDR(struct frag_hdr, skb, off);
3976 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3977 fragment = true;
3979 nexthdr = hp->nexthdr;
3980 off += sizeof(struct frag_hdr);
3981 break;
3983 default:
3984 done = true;
3985 break;
3989 err = -EPROTO;
3991 if (!done || fragment)
3992 goto out;
3994 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3995 if (IS_ERR(csum))
3996 return PTR_ERR(csum);
3998 if (recalculate)
3999 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4000 &ipv6_hdr(skb)->daddr,
4001 skb->len - off, nexthdr, 0);
4002 err = 0;
4004 out:
4005 return err;
4009 * skb_checksum_setup - set up partial checksum offset
4010 * @skb: the skb to set up
4011 * @recalculate: if true the pseudo-header checksum will be recalculated
4013 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4015 int err;
4017 switch (skb->protocol) {
4018 case htons(ETH_P_IP):
4019 err = skb_checksum_setup_ipv4(skb, recalculate);
4020 break;
4022 case htons(ETH_P_IPV6):
4023 err = skb_checksum_setup_ipv6(skb, recalculate);
4024 break;
4026 default:
4027 err = -EPROTO;
4028 break;
4031 return err;
4033 EXPORT_SYMBOL(skb_checksum_setup);
4035 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4037 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4038 skb->dev->name);
4040 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4042 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4044 if (head_stolen) {
4045 skb_release_head_state(skb);
4046 kmem_cache_free(skbuff_head_cache, skb);
4047 } else {
4048 __kfree_skb(skb);
4051 EXPORT_SYMBOL(kfree_skb_partial);
4054 * skb_try_coalesce - try to merge skb to prior one
4055 * @to: prior buffer
4056 * @from: buffer to add
4057 * @fragstolen: pointer to boolean
4058 * @delta_truesize: how much more was allocated than was requested
4060 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4061 bool *fragstolen, int *delta_truesize)
4063 int i, delta, len = from->len;
4065 *fragstolen = false;
4067 if (skb_cloned(to))
4068 return false;
4070 if (len <= skb_tailroom(to)) {
4071 if (len)
4072 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4073 *delta_truesize = 0;
4074 return true;
4077 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4078 return false;
4080 if (skb_headlen(from) != 0) {
4081 struct page *page;
4082 unsigned int offset;
4084 if (skb_shinfo(to)->nr_frags +
4085 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4086 return false;
4088 if (skb_head_is_locked(from))
4089 return false;
4091 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4093 page = virt_to_head_page(from->head);
4094 offset = from->data - (unsigned char *)page_address(page);
4096 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4097 page, offset, skb_headlen(from));
4098 *fragstolen = true;
4099 } else {
4100 if (skb_shinfo(to)->nr_frags +
4101 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4102 return false;
4104 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4107 WARN_ON_ONCE(delta < len);
4109 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4110 skb_shinfo(from)->frags,
4111 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4112 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4114 if (!skb_cloned(from))
4115 skb_shinfo(from)->nr_frags = 0;
4117 /* if the skb is not cloned this does nothing
4118 * since we set nr_frags to 0.
4120 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4121 skb_frag_ref(from, i);
4123 to->truesize += delta;
4124 to->len += len;
4125 to->data_len += len;
4127 *delta_truesize = delta;
4128 return true;
4130 EXPORT_SYMBOL(skb_try_coalesce);
4133 * skb_scrub_packet - scrub an skb
4135 * @skb: buffer to clean
4136 * @xnet: packet is crossing netns
4138 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4139 * into/from a tunnel. Some information have to be cleared during these
4140 * operations.
4141 * skb_scrub_packet can also be used to clean a skb before injecting it in
4142 * another namespace (@xnet == true). We have to clear all information in the
4143 * skb that could impact namespace isolation.
4145 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4147 skb->tstamp.tv64 = 0;
4148 skb->pkt_type = PACKET_HOST;
4149 skb->skb_iif = 0;
4150 skb->ignore_df = 0;
4151 skb_dst_drop(skb);
4152 skb_sender_cpu_clear(skb);
4153 secpath_reset(skb);
4154 nf_reset(skb);
4155 nf_reset_trace(skb);
4157 if (!xnet)
4158 return;
4160 skb_orphan(skb);
4161 skb->mark = 0;
4163 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4166 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4168 * @skb: GSO skb
4170 * skb_gso_transport_seglen is used to determine the real size of the
4171 * individual segments, including Layer4 headers (TCP/UDP).
4173 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4175 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4177 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4178 unsigned int thlen = 0;
4180 if (skb->encapsulation) {
4181 thlen = skb_inner_transport_header(skb) -
4182 skb_transport_header(skb);
4184 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4185 thlen += inner_tcp_hdrlen(skb);
4186 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4187 thlen = tcp_hdrlen(skb);
4189 /* UFO sets gso_size to the size of the fragmentation
4190 * payload, i.e. the size of the L4 (UDP) header is already
4191 * accounted for.
4193 return thlen + shinfo->gso_size;
4195 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4197 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4199 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4200 kfree_skb(skb);
4201 return NULL;
4204 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4205 2 * ETH_ALEN);
4206 skb->mac_header += VLAN_HLEN;
4207 return skb;
4210 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4212 struct vlan_hdr *vhdr;
4213 u16 vlan_tci;
4215 if (unlikely(skb_vlan_tag_present(skb))) {
4216 /* vlan_tci is already set-up so leave this for another time */
4217 return skb;
4220 skb = skb_share_check(skb, GFP_ATOMIC);
4221 if (unlikely(!skb))
4222 goto err_free;
4224 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4225 goto err_free;
4227 vhdr = (struct vlan_hdr *)skb->data;
4228 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4229 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4231 skb_pull_rcsum(skb, VLAN_HLEN);
4232 vlan_set_encap_proto(skb, vhdr);
4234 skb = skb_reorder_vlan_header(skb);
4235 if (unlikely(!skb))
4236 goto err_free;
4238 skb_reset_network_header(skb);
4239 skb_reset_transport_header(skb);
4240 skb_reset_mac_len(skb);
4242 return skb;
4244 err_free:
4245 kfree_skb(skb);
4246 return NULL;
4248 EXPORT_SYMBOL(skb_vlan_untag);
4250 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4252 if (!pskb_may_pull(skb, write_len))
4253 return -ENOMEM;
4255 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4256 return 0;
4258 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4260 EXPORT_SYMBOL(skb_ensure_writable);
4262 /* remove VLAN header from packet and update csum accordingly. */
4263 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4265 struct vlan_hdr *vhdr;
4266 unsigned int offset = skb->data - skb_mac_header(skb);
4267 int err;
4269 __skb_push(skb, offset);
4270 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4271 if (unlikely(err))
4272 goto pull;
4274 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4276 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4277 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4279 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4280 __skb_pull(skb, VLAN_HLEN);
4282 vlan_set_encap_proto(skb, vhdr);
4283 skb->mac_header += VLAN_HLEN;
4285 if (skb_network_offset(skb) < ETH_HLEN)
4286 skb_set_network_header(skb, ETH_HLEN);
4288 skb_reset_mac_len(skb);
4289 pull:
4290 __skb_pull(skb, offset);
4292 return err;
4295 int skb_vlan_pop(struct sk_buff *skb)
4297 u16 vlan_tci;
4298 __be16 vlan_proto;
4299 int err;
4301 if (likely(skb_vlan_tag_present(skb))) {
4302 skb->vlan_tci = 0;
4303 } else {
4304 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4305 skb->protocol != htons(ETH_P_8021AD)) ||
4306 skb->len < VLAN_ETH_HLEN))
4307 return 0;
4309 err = __skb_vlan_pop(skb, &vlan_tci);
4310 if (err)
4311 return err;
4313 /* move next vlan tag to hw accel tag */
4314 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4315 skb->protocol != htons(ETH_P_8021AD)) ||
4316 skb->len < VLAN_ETH_HLEN))
4317 return 0;
4319 vlan_proto = skb->protocol;
4320 err = __skb_vlan_pop(skb, &vlan_tci);
4321 if (unlikely(err))
4322 return err;
4324 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4325 return 0;
4327 EXPORT_SYMBOL(skb_vlan_pop);
4329 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4331 if (skb_vlan_tag_present(skb)) {
4332 unsigned int offset = skb->data - skb_mac_header(skb);
4333 int err;
4335 /* __vlan_insert_tag expect skb->data pointing to mac header.
4336 * So change skb->data before calling it and change back to
4337 * original position later
4339 __skb_push(skb, offset);
4340 err = __vlan_insert_tag(skb, skb->vlan_proto,
4341 skb_vlan_tag_get(skb));
4342 if (err)
4343 return err;
4344 skb->protocol = skb->vlan_proto;
4345 skb->mac_len += VLAN_HLEN;
4346 __skb_pull(skb, offset);
4348 if (skb->ip_summed == CHECKSUM_COMPLETE)
4349 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4350 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4352 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4353 return 0;
4355 EXPORT_SYMBOL(skb_vlan_push);
4358 * alloc_skb_with_frags - allocate skb with page frags
4360 * @header_len: size of linear part
4361 * @data_len: needed length in frags
4362 * @max_page_order: max page order desired.
4363 * @errcode: pointer to error code if any
4364 * @gfp_mask: allocation mask
4366 * This can be used to allocate a paged skb, given a maximal order for frags.
4368 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4369 unsigned long data_len,
4370 int max_page_order,
4371 int *errcode,
4372 gfp_t gfp_mask)
4374 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4375 unsigned long chunk;
4376 struct sk_buff *skb;
4377 struct page *page;
4378 gfp_t gfp_head;
4379 int i;
4381 *errcode = -EMSGSIZE;
4382 /* Note this test could be relaxed, if we succeed to allocate
4383 * high order pages...
4385 if (npages > MAX_SKB_FRAGS)
4386 return NULL;
4388 gfp_head = gfp_mask;
4389 if (gfp_head & __GFP_WAIT)
4390 gfp_head |= __GFP_REPEAT;
4392 *errcode = -ENOBUFS;
4393 skb = alloc_skb(header_len, gfp_head);
4394 if (!skb)
4395 return NULL;
4397 skb->truesize += npages << PAGE_SHIFT;
4399 for (i = 0; npages > 0; i++) {
4400 int order = max_page_order;
4402 while (order) {
4403 if (npages >= 1 << order) {
4404 page = alloc_pages((gfp_mask & ~__GFP_WAIT) |
4405 __GFP_COMP |
4406 __GFP_NOWARN |
4407 __GFP_NORETRY,
4408 order);
4409 if (page)
4410 goto fill_page;
4411 /* Do not retry other high order allocations */
4412 order = 1;
4413 max_page_order = 0;
4415 order--;
4417 page = alloc_page(gfp_mask);
4418 if (!page)
4419 goto failure;
4420 fill_page:
4421 chunk = min_t(unsigned long, data_len,
4422 PAGE_SIZE << order);
4423 skb_fill_page_desc(skb, i, page, 0, chunk);
4424 data_len -= chunk;
4425 npages -= 1 << order;
4427 return skb;
4429 failure:
4430 kfree_skb(skb);
4431 return NULL;
4433 EXPORT_SYMBOL(alloc_skb_with_frags);