scsi: ufs: fix race between clock gating and devfreq scaling work
[linux/fpc-iii.git] / net / core / skbuff.c
blob68ecb7d71c2b2b9fbe0536b40004759bb1c076e5
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/sctp.h>
53 #include <linux/netdevice.h>
54 #ifdef CONFIG_NET_CLS_ACT
55 #include <net/pkt_sched.h>
56 #endif
57 #include <linux/string.h>
58 #include <linux/skbuff.h>
59 #include <linux/splice.h>
60 #include <linux/cache.h>
61 #include <linux/rtnetlink.h>
62 #include <linux/init.h>
63 #include <linux/scatterlist.h>
64 #include <linux/errqueue.h>
65 #include <linux/prefetch.h>
66 #include <linux/if_vlan.h>
68 #include <net/protocol.h>
69 #include <net/dst.h>
70 #include <net/sock.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
73 #include <net/xfrm.h>
75 #include <asm/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
83 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84 EXPORT_SYMBOL(sysctl_max_skb_frags);
86 /**
87 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99 const char msg[])
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
105 BUG();
108 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
113 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115 skb_panic(skb, sz, addr, __func__);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
131 void *obj;
132 bool ret_pfmemalloc = false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
148 out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
152 return obj;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
161 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
163 struct sk_buff *skb;
165 /* Get the HEAD */
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
168 if (!skb)
169 goto out;
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
176 memset(skb, 0, offsetof(struct sk_buff, tail));
177 skb->head = NULL;
178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
181 skb->mac_header = (typeof(skb->mac_header))~0U;
182 out:
183 return skb;
187 * __alloc_skb - allocate a network buffer
188 * @size: size to allocate
189 * @gfp_mask: allocation mask
190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
194 * @node: numa node to allocate memory on
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
201 * %GFP_ATOMIC.
203 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
204 int flags, int node)
206 struct kmem_cache *cache;
207 struct skb_shared_info *shinfo;
208 struct sk_buff *skb;
209 u8 *data;
210 bool pfmemalloc;
212 cache = (flags & SKB_ALLOC_FCLONE)
213 ? skbuff_fclone_cache : skbuff_head_cache;
215 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216 gfp_mask |= __GFP_MEMALLOC;
218 /* Get the HEAD */
219 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
220 if (!skb)
221 goto out;
222 prefetchw(skb);
224 /* We do our best to align skb_shared_info on a separate cache
225 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
226 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
227 * Both skb->head and skb_shared_info are cache line aligned.
229 size = SKB_DATA_ALIGN(size);
230 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
231 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
232 if (!data)
233 goto nodata;
234 /* kmalloc(size) might give us more room than requested.
235 * Put skb_shared_info exactly at the end of allocated zone,
236 * to allow max possible filling before reallocation.
238 size = SKB_WITH_OVERHEAD(ksize(data));
239 prefetchw(data + size);
242 * Only clear those fields we need to clear, not those that we will
243 * actually initialise below. Hence, don't put any more fields after
244 * the tail pointer in struct sk_buff!
246 memset(skb, 0, offsetof(struct sk_buff, tail));
247 /* Account for allocated memory : skb + skb->head */
248 skb->truesize = SKB_TRUESIZE(size);
249 skb->pfmemalloc = pfmemalloc;
250 atomic_set(&skb->users, 1);
251 skb->head = data;
252 skb->data = data;
253 skb_reset_tail_pointer(skb);
254 skb->end = skb->tail + size;
255 skb->mac_header = (typeof(skb->mac_header))~0U;
256 skb->transport_header = (typeof(skb->transport_header))~0U;
258 /* make sure we initialize shinfo sequentially */
259 shinfo = skb_shinfo(skb);
260 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
261 atomic_set(&shinfo->dataref, 1);
262 kmemcheck_annotate_variable(shinfo->destructor_arg);
264 if (flags & SKB_ALLOC_FCLONE) {
265 struct sk_buff_fclones *fclones;
267 fclones = container_of(skb, struct sk_buff_fclones, skb1);
269 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
270 skb->fclone = SKB_FCLONE_ORIG;
271 atomic_set(&fclones->fclone_ref, 1);
273 fclones->skb2.fclone = SKB_FCLONE_CLONE;
274 fclones->skb2.pfmemalloc = pfmemalloc;
276 out:
277 return skb;
278 nodata:
279 kmem_cache_free(cache, skb);
280 skb = NULL;
281 goto out;
283 EXPORT_SYMBOL(__alloc_skb);
286 * __build_skb - build a network buffer
287 * @data: data buffer provided by caller
288 * @frag_size: size of data, or 0 if head was kmalloced
290 * Allocate a new &sk_buff. Caller provides space holding head and
291 * skb_shared_info. @data must have been allocated by kmalloc() only if
292 * @frag_size is 0, otherwise data should come from the page allocator
293 * or vmalloc()
294 * The return is the new skb buffer.
295 * On a failure the return is %NULL, and @data is not freed.
296 * Notes :
297 * Before IO, driver allocates only data buffer where NIC put incoming frame
298 * Driver should add room at head (NET_SKB_PAD) and
299 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
300 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
301 * before giving packet to stack.
302 * RX rings only contains data buffers, not full skbs.
304 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
306 struct skb_shared_info *shinfo;
307 struct sk_buff *skb;
308 unsigned int size = frag_size ? : ksize(data);
310 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
311 if (!skb)
312 return NULL;
314 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
316 memset(skb, 0, offsetof(struct sk_buff, tail));
317 skb->truesize = SKB_TRUESIZE(size);
318 atomic_set(&skb->users, 1);
319 skb->head = data;
320 skb->data = data;
321 skb_reset_tail_pointer(skb);
322 skb->end = skb->tail + size;
323 skb->mac_header = (typeof(skb->mac_header))~0U;
324 skb->transport_header = (typeof(skb->transport_header))~0U;
326 /* make sure we initialize shinfo sequentially */
327 shinfo = skb_shinfo(skb);
328 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
329 atomic_set(&shinfo->dataref, 1);
330 kmemcheck_annotate_variable(shinfo->destructor_arg);
332 return skb;
335 /* build_skb() is wrapper over __build_skb(), that specifically
336 * takes care of skb->head and skb->pfmemalloc
337 * This means that if @frag_size is not zero, then @data must be backed
338 * by a page fragment, not kmalloc() or vmalloc()
340 struct sk_buff *build_skb(void *data, unsigned int frag_size)
342 struct sk_buff *skb = __build_skb(data, frag_size);
344 if (skb && frag_size) {
345 skb->head_frag = 1;
346 if (page_is_pfmemalloc(virt_to_head_page(data)))
347 skb->pfmemalloc = 1;
349 return skb;
351 EXPORT_SYMBOL(build_skb);
353 #define NAPI_SKB_CACHE_SIZE 64
355 struct napi_alloc_cache {
356 struct page_frag_cache page;
357 size_t skb_count;
358 void *skb_cache[NAPI_SKB_CACHE_SIZE];
361 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
362 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
364 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
366 struct page_frag_cache *nc;
367 unsigned long flags;
368 void *data;
370 local_irq_save(flags);
371 nc = this_cpu_ptr(&netdev_alloc_cache);
372 data = __alloc_page_frag(nc, fragsz, gfp_mask);
373 local_irq_restore(flags);
374 return data;
378 * netdev_alloc_frag - allocate a page fragment
379 * @fragsz: fragment size
381 * Allocates a frag from a page for receive buffer.
382 * Uses GFP_ATOMIC allocations.
384 void *netdev_alloc_frag(unsigned int fragsz)
386 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
388 EXPORT_SYMBOL(netdev_alloc_frag);
390 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
392 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
394 return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
397 void *napi_alloc_frag(unsigned int fragsz)
399 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
401 EXPORT_SYMBOL(napi_alloc_frag);
404 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
405 * @dev: network device to receive on
406 * @len: length to allocate
407 * @gfp_mask: get_free_pages mask, passed to alloc_skb
409 * Allocate a new &sk_buff and assign it a usage count of one. The
410 * buffer has NET_SKB_PAD headroom built in. Users should allocate
411 * the headroom they think they need without accounting for the
412 * built in space. The built in space is used for optimisations.
414 * %NULL is returned if there is no free memory.
416 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
417 gfp_t gfp_mask)
419 struct page_frag_cache *nc;
420 unsigned long flags;
421 struct sk_buff *skb;
422 bool pfmemalloc;
423 void *data;
425 len += NET_SKB_PAD;
427 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
428 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
429 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
430 if (!skb)
431 goto skb_fail;
432 goto skb_success;
435 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
436 len = SKB_DATA_ALIGN(len);
438 if (sk_memalloc_socks())
439 gfp_mask |= __GFP_MEMALLOC;
441 local_irq_save(flags);
443 nc = this_cpu_ptr(&netdev_alloc_cache);
444 data = __alloc_page_frag(nc, len, gfp_mask);
445 pfmemalloc = nc->pfmemalloc;
447 local_irq_restore(flags);
449 if (unlikely(!data))
450 return NULL;
452 skb = __build_skb(data, len);
453 if (unlikely(!skb)) {
454 skb_free_frag(data);
455 return NULL;
458 /* use OR instead of assignment to avoid clearing of bits in mask */
459 if (pfmemalloc)
460 skb->pfmemalloc = 1;
461 skb->head_frag = 1;
463 skb_success:
464 skb_reserve(skb, NET_SKB_PAD);
465 skb->dev = dev;
467 skb_fail:
468 return skb;
470 EXPORT_SYMBOL(__netdev_alloc_skb);
473 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
474 * @napi: napi instance this buffer was allocated for
475 * @len: length to allocate
476 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
478 * Allocate a new sk_buff for use in NAPI receive. This buffer will
479 * attempt to allocate the head from a special reserved region used
480 * only for NAPI Rx allocation. By doing this we can save several
481 * CPU cycles by avoiding having to disable and re-enable IRQs.
483 * %NULL is returned if there is no free memory.
485 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
486 gfp_t gfp_mask)
488 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
489 struct sk_buff *skb;
490 void *data;
492 len += NET_SKB_PAD + NET_IP_ALIGN;
494 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
495 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
496 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
497 if (!skb)
498 goto skb_fail;
499 goto skb_success;
502 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
503 len = SKB_DATA_ALIGN(len);
505 if (sk_memalloc_socks())
506 gfp_mask |= __GFP_MEMALLOC;
508 data = __alloc_page_frag(&nc->page, len, gfp_mask);
509 if (unlikely(!data))
510 return NULL;
512 skb = __build_skb(data, len);
513 if (unlikely(!skb)) {
514 skb_free_frag(data);
515 return NULL;
518 /* use OR instead of assignment to avoid clearing of bits in mask */
519 if (nc->page.pfmemalloc)
520 skb->pfmemalloc = 1;
521 skb->head_frag = 1;
523 skb_success:
524 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
525 skb->dev = napi->dev;
527 skb_fail:
528 return skb;
530 EXPORT_SYMBOL(__napi_alloc_skb);
532 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
533 int size, unsigned int truesize)
535 skb_fill_page_desc(skb, i, page, off, size);
536 skb->len += size;
537 skb->data_len += size;
538 skb->truesize += truesize;
540 EXPORT_SYMBOL(skb_add_rx_frag);
542 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
543 unsigned int truesize)
545 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
547 skb_frag_size_add(frag, size);
548 skb->len += size;
549 skb->data_len += size;
550 skb->truesize += truesize;
552 EXPORT_SYMBOL(skb_coalesce_rx_frag);
554 static void skb_drop_list(struct sk_buff **listp)
556 kfree_skb_list(*listp);
557 *listp = NULL;
560 static inline void skb_drop_fraglist(struct sk_buff *skb)
562 skb_drop_list(&skb_shinfo(skb)->frag_list);
565 static void skb_clone_fraglist(struct sk_buff *skb)
567 struct sk_buff *list;
569 skb_walk_frags(skb, list)
570 skb_get(list);
573 static void skb_free_head(struct sk_buff *skb)
575 unsigned char *head = skb->head;
577 if (skb->head_frag)
578 skb_free_frag(head);
579 else
580 kfree(head);
583 static void skb_release_data(struct sk_buff *skb)
585 struct skb_shared_info *shinfo = skb_shinfo(skb);
586 int i;
588 if (skb->cloned &&
589 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
590 &shinfo->dataref))
591 return;
593 for (i = 0; i < shinfo->nr_frags; i++)
594 __skb_frag_unref(&shinfo->frags[i]);
597 * If skb buf is from userspace, we need to notify the caller
598 * the lower device DMA has done;
600 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
601 struct ubuf_info *uarg;
603 uarg = shinfo->destructor_arg;
604 if (uarg->callback)
605 uarg->callback(uarg, true);
608 if (shinfo->frag_list)
609 kfree_skb_list(shinfo->frag_list);
611 skb_free_head(skb);
615 * Free an skbuff by memory without cleaning the state.
617 static void kfree_skbmem(struct sk_buff *skb)
619 struct sk_buff_fclones *fclones;
621 switch (skb->fclone) {
622 case SKB_FCLONE_UNAVAILABLE:
623 kmem_cache_free(skbuff_head_cache, skb);
624 return;
626 case SKB_FCLONE_ORIG:
627 fclones = container_of(skb, struct sk_buff_fclones, skb1);
629 /* We usually free the clone (TX completion) before original skb
630 * This test would have no chance to be true for the clone,
631 * while here, branch prediction will be good.
633 if (atomic_read(&fclones->fclone_ref) == 1)
634 goto fastpath;
635 break;
637 default: /* SKB_FCLONE_CLONE */
638 fclones = container_of(skb, struct sk_buff_fclones, skb2);
639 break;
641 if (!atomic_dec_and_test(&fclones->fclone_ref))
642 return;
643 fastpath:
644 kmem_cache_free(skbuff_fclone_cache, fclones);
647 static void skb_release_head_state(struct sk_buff *skb)
649 skb_dst_drop(skb);
650 #ifdef CONFIG_XFRM
651 secpath_put(skb->sp);
652 #endif
653 if (skb->destructor) {
654 WARN_ON(in_irq());
655 skb->destructor(skb);
657 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
658 nf_conntrack_put(skb->nfct);
659 #endif
660 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
661 nf_bridge_put(skb->nf_bridge);
662 #endif
665 /* Free everything but the sk_buff shell. */
666 static void skb_release_all(struct sk_buff *skb)
668 skb_release_head_state(skb);
669 if (likely(skb->head))
670 skb_release_data(skb);
674 * __kfree_skb - private function
675 * @skb: buffer
677 * Free an sk_buff. Release anything attached to the buffer.
678 * Clean the state. This is an internal helper function. Users should
679 * always call kfree_skb
682 void __kfree_skb(struct sk_buff *skb)
684 skb_release_all(skb);
685 kfree_skbmem(skb);
687 EXPORT_SYMBOL(__kfree_skb);
690 * kfree_skb - free an sk_buff
691 * @skb: buffer to free
693 * Drop a reference to the buffer and free it if the usage count has
694 * hit zero.
696 void kfree_skb(struct sk_buff *skb)
698 if (unlikely(!skb))
699 return;
700 if (likely(atomic_read(&skb->users) == 1))
701 smp_rmb();
702 else if (likely(!atomic_dec_and_test(&skb->users)))
703 return;
704 trace_kfree_skb(skb, __builtin_return_address(0));
705 __kfree_skb(skb);
707 EXPORT_SYMBOL(kfree_skb);
709 void kfree_skb_list(struct sk_buff *segs)
711 while (segs) {
712 struct sk_buff *next = segs->next;
714 kfree_skb(segs);
715 segs = next;
718 EXPORT_SYMBOL(kfree_skb_list);
721 * skb_tx_error - report an sk_buff xmit error
722 * @skb: buffer that triggered an error
724 * Report xmit error if a device callback is tracking this skb.
725 * skb must be freed afterwards.
727 void skb_tx_error(struct sk_buff *skb)
729 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
730 struct ubuf_info *uarg;
732 uarg = skb_shinfo(skb)->destructor_arg;
733 if (uarg->callback)
734 uarg->callback(uarg, false);
735 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
738 EXPORT_SYMBOL(skb_tx_error);
741 * consume_skb - free an skbuff
742 * @skb: buffer to free
744 * Drop a ref to the buffer and free it if the usage count has hit zero
745 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
746 * is being dropped after a failure and notes that
748 void consume_skb(struct sk_buff *skb)
750 if (unlikely(!skb))
751 return;
752 if (likely(atomic_read(&skb->users) == 1))
753 smp_rmb();
754 else if (likely(!atomic_dec_and_test(&skb->users)))
755 return;
756 trace_consume_skb(skb);
757 __kfree_skb(skb);
759 EXPORT_SYMBOL(consume_skb);
761 void __kfree_skb_flush(void)
763 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
765 /* flush skb_cache if containing objects */
766 if (nc->skb_count) {
767 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
768 nc->skb_cache);
769 nc->skb_count = 0;
773 static inline void _kfree_skb_defer(struct sk_buff *skb)
775 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
777 /* drop skb->head and call any destructors for packet */
778 skb_release_all(skb);
780 /* record skb to CPU local list */
781 nc->skb_cache[nc->skb_count++] = skb;
783 #ifdef CONFIG_SLUB
784 /* SLUB writes into objects when freeing */
785 prefetchw(skb);
786 #endif
788 /* flush skb_cache if it is filled */
789 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
790 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
791 nc->skb_cache);
792 nc->skb_count = 0;
795 void __kfree_skb_defer(struct sk_buff *skb)
797 _kfree_skb_defer(skb);
800 void napi_consume_skb(struct sk_buff *skb, int budget)
802 if (unlikely(!skb))
803 return;
805 /* Zero budget indicate non-NAPI context called us, like netpoll */
806 if (unlikely(!budget)) {
807 dev_consume_skb_any(skb);
808 return;
811 if (likely(atomic_read(&skb->users) == 1))
812 smp_rmb();
813 else if (likely(!atomic_dec_and_test(&skb->users)))
814 return;
815 /* if reaching here SKB is ready to free */
816 trace_consume_skb(skb);
818 /* if SKB is a clone, don't handle this case */
819 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
820 __kfree_skb(skb);
821 return;
824 _kfree_skb_defer(skb);
826 EXPORT_SYMBOL(napi_consume_skb);
828 /* Make sure a field is enclosed inside headers_start/headers_end section */
829 #define CHECK_SKB_FIELD(field) \
830 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
831 offsetof(struct sk_buff, headers_start)); \
832 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
833 offsetof(struct sk_buff, headers_end)); \
835 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
837 new->tstamp = old->tstamp;
838 /* We do not copy old->sk */
839 new->dev = old->dev;
840 memcpy(new->cb, old->cb, sizeof(old->cb));
841 skb_dst_copy(new, old);
842 #ifdef CONFIG_XFRM
843 new->sp = secpath_get(old->sp);
844 #endif
845 __nf_copy(new, old, false);
847 /* Note : this field could be in headers_start/headers_end section
848 * It is not yet because we do not want to have a 16 bit hole
850 new->queue_mapping = old->queue_mapping;
852 memcpy(&new->headers_start, &old->headers_start,
853 offsetof(struct sk_buff, headers_end) -
854 offsetof(struct sk_buff, headers_start));
855 CHECK_SKB_FIELD(protocol);
856 CHECK_SKB_FIELD(csum);
857 CHECK_SKB_FIELD(hash);
858 CHECK_SKB_FIELD(priority);
859 CHECK_SKB_FIELD(skb_iif);
860 CHECK_SKB_FIELD(vlan_proto);
861 CHECK_SKB_FIELD(vlan_tci);
862 CHECK_SKB_FIELD(transport_header);
863 CHECK_SKB_FIELD(network_header);
864 CHECK_SKB_FIELD(mac_header);
865 CHECK_SKB_FIELD(inner_protocol);
866 CHECK_SKB_FIELD(inner_transport_header);
867 CHECK_SKB_FIELD(inner_network_header);
868 CHECK_SKB_FIELD(inner_mac_header);
869 CHECK_SKB_FIELD(mark);
870 #ifdef CONFIG_NETWORK_SECMARK
871 CHECK_SKB_FIELD(secmark);
872 #endif
873 #ifdef CONFIG_NET_RX_BUSY_POLL
874 CHECK_SKB_FIELD(napi_id);
875 #endif
876 #ifdef CONFIG_XPS
877 CHECK_SKB_FIELD(sender_cpu);
878 #endif
879 #ifdef CONFIG_NET_SCHED
880 CHECK_SKB_FIELD(tc_index);
881 #ifdef CONFIG_NET_CLS_ACT
882 CHECK_SKB_FIELD(tc_verd);
883 #endif
884 #endif
889 * You should not add any new code to this function. Add it to
890 * __copy_skb_header above instead.
892 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
894 #define C(x) n->x = skb->x
896 n->next = n->prev = NULL;
897 n->sk = NULL;
898 __copy_skb_header(n, skb);
900 C(len);
901 C(data_len);
902 C(mac_len);
903 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
904 n->cloned = 1;
905 n->nohdr = 0;
906 n->peeked = 0;
907 C(pfmemalloc);
908 n->destructor = NULL;
909 C(tail);
910 C(end);
911 C(head);
912 C(head_frag);
913 C(data);
914 C(truesize);
915 atomic_set(&n->users, 1);
917 atomic_inc(&(skb_shinfo(skb)->dataref));
918 skb->cloned = 1;
920 return n;
921 #undef C
925 * skb_morph - morph one skb into another
926 * @dst: the skb to receive the contents
927 * @src: the skb to supply the contents
929 * This is identical to skb_clone except that the target skb is
930 * supplied by the user.
932 * The target skb is returned upon exit.
934 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
936 skb_release_all(dst);
937 return __skb_clone(dst, src);
939 EXPORT_SYMBOL_GPL(skb_morph);
942 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
943 * @skb: the skb to modify
944 * @gfp_mask: allocation priority
946 * This must be called on SKBTX_DEV_ZEROCOPY skb.
947 * It will copy all frags into kernel and drop the reference
948 * to userspace pages.
950 * If this function is called from an interrupt gfp_mask() must be
951 * %GFP_ATOMIC.
953 * Returns 0 on success or a negative error code on failure
954 * to allocate kernel memory to copy to.
956 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
958 int i;
959 int num_frags = skb_shinfo(skb)->nr_frags;
960 struct page *page, *head = NULL;
961 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
963 for (i = 0; i < num_frags; i++) {
964 u8 *vaddr;
965 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
967 page = alloc_page(gfp_mask);
968 if (!page) {
969 while (head) {
970 struct page *next = (struct page *)page_private(head);
971 put_page(head);
972 head = next;
974 return -ENOMEM;
976 vaddr = kmap_atomic(skb_frag_page(f));
977 memcpy(page_address(page),
978 vaddr + f->page_offset, skb_frag_size(f));
979 kunmap_atomic(vaddr);
980 set_page_private(page, (unsigned long)head);
981 head = page;
984 /* skb frags release userspace buffers */
985 for (i = 0; i < num_frags; i++)
986 skb_frag_unref(skb, i);
988 uarg->callback(uarg, false);
990 /* skb frags point to kernel buffers */
991 for (i = num_frags - 1; i >= 0; i--) {
992 __skb_fill_page_desc(skb, i, head, 0,
993 skb_shinfo(skb)->frags[i].size);
994 head = (struct page *)page_private(head);
997 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
998 return 0;
1000 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1003 * skb_clone - duplicate an sk_buff
1004 * @skb: buffer to clone
1005 * @gfp_mask: allocation priority
1007 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1008 * copies share the same packet data but not structure. The new
1009 * buffer has a reference count of 1. If the allocation fails the
1010 * function returns %NULL otherwise the new buffer is returned.
1012 * If this function is called from an interrupt gfp_mask() must be
1013 * %GFP_ATOMIC.
1016 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1018 struct sk_buff_fclones *fclones = container_of(skb,
1019 struct sk_buff_fclones,
1020 skb1);
1021 struct sk_buff *n;
1023 if (skb_orphan_frags(skb, gfp_mask))
1024 return NULL;
1026 if (skb->fclone == SKB_FCLONE_ORIG &&
1027 atomic_read(&fclones->fclone_ref) == 1) {
1028 n = &fclones->skb2;
1029 atomic_set(&fclones->fclone_ref, 2);
1030 } else {
1031 if (skb_pfmemalloc(skb))
1032 gfp_mask |= __GFP_MEMALLOC;
1034 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1035 if (!n)
1036 return NULL;
1038 kmemcheck_annotate_bitfield(n, flags1);
1039 n->fclone = SKB_FCLONE_UNAVAILABLE;
1042 return __skb_clone(n, skb);
1044 EXPORT_SYMBOL(skb_clone);
1046 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1048 /* Only adjust this if it actually is csum_start rather than csum */
1049 if (skb->ip_summed == CHECKSUM_PARTIAL)
1050 skb->csum_start += off;
1051 /* {transport,network,mac}_header and tail are relative to skb->head */
1052 skb->transport_header += off;
1053 skb->network_header += off;
1054 if (skb_mac_header_was_set(skb))
1055 skb->mac_header += off;
1056 skb->inner_transport_header += off;
1057 skb->inner_network_header += off;
1058 skb->inner_mac_header += off;
1061 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1063 __copy_skb_header(new, old);
1065 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1066 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1067 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1070 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1072 if (skb_pfmemalloc(skb))
1073 return SKB_ALLOC_RX;
1074 return 0;
1078 * skb_copy - create private copy of an sk_buff
1079 * @skb: buffer to copy
1080 * @gfp_mask: allocation priority
1082 * Make a copy of both an &sk_buff and its data. This is used when the
1083 * caller wishes to modify the data and needs a private copy of the
1084 * data to alter. Returns %NULL on failure or the pointer to the buffer
1085 * on success. The returned buffer has a reference count of 1.
1087 * As by-product this function converts non-linear &sk_buff to linear
1088 * one, so that &sk_buff becomes completely private and caller is allowed
1089 * to modify all the data of returned buffer. This means that this
1090 * function is not recommended for use in circumstances when only
1091 * header is going to be modified. Use pskb_copy() instead.
1094 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1096 int headerlen = skb_headroom(skb);
1097 unsigned int size = skb_end_offset(skb) + skb->data_len;
1098 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1099 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1101 if (!n)
1102 return NULL;
1104 /* Set the data pointer */
1105 skb_reserve(n, headerlen);
1106 /* Set the tail pointer and length */
1107 skb_put(n, skb->len);
1109 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1110 BUG();
1112 copy_skb_header(n, skb);
1113 return n;
1115 EXPORT_SYMBOL(skb_copy);
1118 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1119 * @skb: buffer to copy
1120 * @headroom: headroom of new skb
1121 * @gfp_mask: allocation priority
1122 * @fclone: if true allocate the copy of the skb from the fclone
1123 * cache instead of the head cache; it is recommended to set this
1124 * to true for the cases where the copy will likely be cloned
1126 * Make a copy of both an &sk_buff and part of its data, located
1127 * in header. Fragmented data remain shared. This is used when
1128 * the caller wishes to modify only header of &sk_buff and needs
1129 * private copy of the header to alter. Returns %NULL on failure
1130 * or the pointer to the buffer on success.
1131 * The returned buffer has a reference count of 1.
1134 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1135 gfp_t gfp_mask, bool fclone)
1137 unsigned int size = skb_headlen(skb) + headroom;
1138 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1139 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1141 if (!n)
1142 goto out;
1144 /* Set the data pointer */
1145 skb_reserve(n, headroom);
1146 /* Set the tail pointer and length */
1147 skb_put(n, skb_headlen(skb));
1148 /* Copy the bytes */
1149 skb_copy_from_linear_data(skb, n->data, n->len);
1151 n->truesize += skb->data_len;
1152 n->data_len = skb->data_len;
1153 n->len = skb->len;
1155 if (skb_shinfo(skb)->nr_frags) {
1156 int i;
1158 if (skb_orphan_frags(skb, gfp_mask)) {
1159 kfree_skb(n);
1160 n = NULL;
1161 goto out;
1163 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1164 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1165 skb_frag_ref(skb, i);
1167 skb_shinfo(n)->nr_frags = i;
1170 if (skb_has_frag_list(skb)) {
1171 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1172 skb_clone_fraglist(n);
1175 copy_skb_header(n, skb);
1176 out:
1177 return n;
1179 EXPORT_SYMBOL(__pskb_copy_fclone);
1182 * pskb_expand_head - reallocate header of &sk_buff
1183 * @skb: buffer to reallocate
1184 * @nhead: room to add at head
1185 * @ntail: room to add at tail
1186 * @gfp_mask: allocation priority
1188 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1189 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1190 * reference count of 1. Returns zero in the case of success or error,
1191 * if expansion failed. In the last case, &sk_buff is not changed.
1193 * All the pointers pointing into skb header may change and must be
1194 * reloaded after call to this function.
1197 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1198 gfp_t gfp_mask)
1200 int i;
1201 u8 *data;
1202 int size = nhead + skb_end_offset(skb) + ntail;
1203 long off;
1205 BUG_ON(nhead < 0);
1207 if (skb_shared(skb))
1208 BUG();
1210 size = SKB_DATA_ALIGN(size);
1212 if (skb_pfmemalloc(skb))
1213 gfp_mask |= __GFP_MEMALLOC;
1214 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1215 gfp_mask, NUMA_NO_NODE, NULL);
1216 if (!data)
1217 goto nodata;
1218 size = SKB_WITH_OVERHEAD(ksize(data));
1220 /* Copy only real data... and, alas, header. This should be
1221 * optimized for the cases when header is void.
1223 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1225 memcpy((struct skb_shared_info *)(data + size),
1226 skb_shinfo(skb),
1227 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1230 * if shinfo is shared we must drop the old head gracefully, but if it
1231 * is not we can just drop the old head and let the existing refcount
1232 * be since all we did is relocate the values
1234 if (skb_cloned(skb)) {
1235 /* copy this zero copy skb frags */
1236 if (skb_orphan_frags(skb, gfp_mask))
1237 goto nofrags;
1238 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1239 skb_frag_ref(skb, i);
1241 if (skb_has_frag_list(skb))
1242 skb_clone_fraglist(skb);
1244 skb_release_data(skb);
1245 } else {
1246 skb_free_head(skb);
1248 off = (data + nhead) - skb->head;
1250 skb->head = data;
1251 skb->head_frag = 0;
1252 skb->data += off;
1253 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1254 skb->end = size;
1255 off = nhead;
1256 #else
1257 skb->end = skb->head + size;
1258 #endif
1259 skb->tail += off;
1260 skb_headers_offset_update(skb, nhead);
1261 skb->cloned = 0;
1262 skb->hdr_len = 0;
1263 skb->nohdr = 0;
1264 atomic_set(&skb_shinfo(skb)->dataref, 1);
1265 return 0;
1267 nofrags:
1268 kfree(data);
1269 nodata:
1270 return -ENOMEM;
1272 EXPORT_SYMBOL(pskb_expand_head);
1274 /* Make private copy of skb with writable head and some headroom */
1276 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1278 struct sk_buff *skb2;
1279 int delta = headroom - skb_headroom(skb);
1281 if (delta <= 0)
1282 skb2 = pskb_copy(skb, GFP_ATOMIC);
1283 else {
1284 skb2 = skb_clone(skb, GFP_ATOMIC);
1285 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1286 GFP_ATOMIC)) {
1287 kfree_skb(skb2);
1288 skb2 = NULL;
1291 return skb2;
1293 EXPORT_SYMBOL(skb_realloc_headroom);
1296 * skb_copy_expand - copy and expand sk_buff
1297 * @skb: buffer to copy
1298 * @newheadroom: new free bytes at head
1299 * @newtailroom: new free bytes at tail
1300 * @gfp_mask: allocation priority
1302 * Make a copy of both an &sk_buff and its data and while doing so
1303 * allocate additional space.
1305 * This is used when the caller wishes to modify the data and needs a
1306 * private copy of the data to alter as well as more space for new fields.
1307 * Returns %NULL on failure or the pointer to the buffer
1308 * on success. The returned buffer has a reference count of 1.
1310 * You must pass %GFP_ATOMIC as the allocation priority if this function
1311 * is called from an interrupt.
1313 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1314 int newheadroom, int newtailroom,
1315 gfp_t gfp_mask)
1318 * Allocate the copy buffer
1320 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1321 gfp_mask, skb_alloc_rx_flag(skb),
1322 NUMA_NO_NODE);
1323 int oldheadroom = skb_headroom(skb);
1324 int head_copy_len, head_copy_off;
1326 if (!n)
1327 return NULL;
1329 skb_reserve(n, newheadroom);
1331 /* Set the tail pointer and length */
1332 skb_put(n, skb->len);
1334 head_copy_len = oldheadroom;
1335 head_copy_off = 0;
1336 if (newheadroom <= head_copy_len)
1337 head_copy_len = newheadroom;
1338 else
1339 head_copy_off = newheadroom - head_copy_len;
1341 /* Copy the linear header and data. */
1342 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1343 skb->len + head_copy_len))
1344 BUG();
1346 copy_skb_header(n, skb);
1348 skb_headers_offset_update(n, newheadroom - oldheadroom);
1350 return n;
1352 EXPORT_SYMBOL(skb_copy_expand);
1355 * skb_pad - zero pad the tail of an skb
1356 * @skb: buffer to pad
1357 * @pad: space to pad
1359 * Ensure that a buffer is followed by a padding area that is zero
1360 * filled. Used by network drivers which may DMA or transfer data
1361 * beyond the buffer end onto the wire.
1363 * May return error in out of memory cases. The skb is freed on error.
1366 int skb_pad(struct sk_buff *skb, int pad)
1368 int err;
1369 int ntail;
1371 /* If the skbuff is non linear tailroom is always zero.. */
1372 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1373 memset(skb->data+skb->len, 0, pad);
1374 return 0;
1377 ntail = skb->data_len + pad - (skb->end - skb->tail);
1378 if (likely(skb_cloned(skb) || ntail > 0)) {
1379 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1380 if (unlikely(err))
1381 goto free_skb;
1384 /* FIXME: The use of this function with non-linear skb's really needs
1385 * to be audited.
1387 err = skb_linearize(skb);
1388 if (unlikely(err))
1389 goto free_skb;
1391 memset(skb->data + skb->len, 0, pad);
1392 return 0;
1394 free_skb:
1395 kfree_skb(skb);
1396 return err;
1398 EXPORT_SYMBOL(skb_pad);
1401 * pskb_put - add data to the tail of a potentially fragmented buffer
1402 * @skb: start of the buffer to use
1403 * @tail: tail fragment of the buffer to use
1404 * @len: amount of data to add
1406 * This function extends the used data area of the potentially
1407 * fragmented buffer. @tail must be the last fragment of @skb -- or
1408 * @skb itself. If this would exceed the total buffer size the kernel
1409 * will panic. A pointer to the first byte of the extra data is
1410 * returned.
1413 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1415 if (tail != skb) {
1416 skb->data_len += len;
1417 skb->len += len;
1419 return skb_put(tail, len);
1421 EXPORT_SYMBOL_GPL(pskb_put);
1424 * skb_put - add data to a buffer
1425 * @skb: buffer to use
1426 * @len: amount of data to add
1428 * This function extends the used data area of the buffer. If this would
1429 * exceed the total buffer size the kernel will panic. A pointer to the
1430 * first byte of the extra data is returned.
1432 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1434 unsigned char *tmp = skb_tail_pointer(skb);
1435 SKB_LINEAR_ASSERT(skb);
1436 skb->tail += len;
1437 skb->len += len;
1438 if (unlikely(skb->tail > skb->end))
1439 skb_over_panic(skb, len, __builtin_return_address(0));
1440 return tmp;
1442 EXPORT_SYMBOL(skb_put);
1445 * skb_push - add data to the start of a buffer
1446 * @skb: buffer to use
1447 * @len: amount of data to add
1449 * This function extends the used data area of the buffer at the buffer
1450 * start. If this would exceed the total buffer headroom the kernel will
1451 * panic. A pointer to the first byte of the extra data is returned.
1453 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1455 skb->data -= len;
1456 skb->len += len;
1457 if (unlikely(skb->data<skb->head))
1458 skb_under_panic(skb, len, __builtin_return_address(0));
1459 return skb->data;
1461 EXPORT_SYMBOL(skb_push);
1464 * skb_pull - remove data from the start of a buffer
1465 * @skb: buffer to use
1466 * @len: amount of data to remove
1468 * This function removes data from the start of a buffer, returning
1469 * the memory to the headroom. A pointer to the next data in the buffer
1470 * is returned. Once the data has been pulled future pushes will overwrite
1471 * the old data.
1473 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1475 return skb_pull_inline(skb, len);
1477 EXPORT_SYMBOL(skb_pull);
1480 * skb_trim - remove end from a buffer
1481 * @skb: buffer to alter
1482 * @len: new length
1484 * Cut the length of a buffer down by removing data from the tail. If
1485 * the buffer is already under the length specified it is not modified.
1486 * The skb must be linear.
1488 void skb_trim(struct sk_buff *skb, unsigned int len)
1490 if (skb->len > len)
1491 __skb_trim(skb, len);
1493 EXPORT_SYMBOL(skb_trim);
1495 /* Trims skb to length len. It can change skb pointers.
1498 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1500 struct sk_buff **fragp;
1501 struct sk_buff *frag;
1502 int offset = skb_headlen(skb);
1503 int nfrags = skb_shinfo(skb)->nr_frags;
1504 int i;
1505 int err;
1507 if (skb_cloned(skb) &&
1508 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1509 return err;
1511 i = 0;
1512 if (offset >= len)
1513 goto drop_pages;
1515 for (; i < nfrags; i++) {
1516 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1518 if (end < len) {
1519 offset = end;
1520 continue;
1523 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1525 drop_pages:
1526 skb_shinfo(skb)->nr_frags = i;
1528 for (; i < nfrags; i++)
1529 skb_frag_unref(skb, i);
1531 if (skb_has_frag_list(skb))
1532 skb_drop_fraglist(skb);
1533 goto done;
1536 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1537 fragp = &frag->next) {
1538 int end = offset + frag->len;
1540 if (skb_shared(frag)) {
1541 struct sk_buff *nfrag;
1543 nfrag = skb_clone(frag, GFP_ATOMIC);
1544 if (unlikely(!nfrag))
1545 return -ENOMEM;
1547 nfrag->next = frag->next;
1548 consume_skb(frag);
1549 frag = nfrag;
1550 *fragp = frag;
1553 if (end < len) {
1554 offset = end;
1555 continue;
1558 if (end > len &&
1559 unlikely((err = pskb_trim(frag, len - offset))))
1560 return err;
1562 if (frag->next)
1563 skb_drop_list(&frag->next);
1564 break;
1567 done:
1568 if (len > skb_headlen(skb)) {
1569 skb->data_len -= skb->len - len;
1570 skb->len = len;
1571 } else {
1572 skb->len = len;
1573 skb->data_len = 0;
1574 skb_set_tail_pointer(skb, len);
1577 return 0;
1579 EXPORT_SYMBOL(___pskb_trim);
1581 /* Note : use pskb_trim_rcsum() instead of calling this directly
1583 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1585 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1586 int delta = skb->len - len;
1588 skb->csum = csum_block_sub(skb->csum,
1589 skb_checksum(skb, len, delta, 0),
1590 len);
1592 return __pskb_trim(skb, len);
1594 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1597 * __pskb_pull_tail - advance tail of skb header
1598 * @skb: buffer to reallocate
1599 * @delta: number of bytes to advance tail
1601 * The function makes a sense only on a fragmented &sk_buff,
1602 * it expands header moving its tail forward and copying necessary
1603 * data from fragmented part.
1605 * &sk_buff MUST have reference count of 1.
1607 * Returns %NULL (and &sk_buff does not change) if pull failed
1608 * or value of new tail of skb in the case of success.
1610 * All the pointers pointing into skb header may change and must be
1611 * reloaded after call to this function.
1614 /* Moves tail of skb head forward, copying data from fragmented part,
1615 * when it is necessary.
1616 * 1. It may fail due to malloc failure.
1617 * 2. It may change skb pointers.
1619 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1621 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1623 /* If skb has not enough free space at tail, get new one
1624 * plus 128 bytes for future expansions. If we have enough
1625 * room at tail, reallocate without expansion only if skb is cloned.
1627 int i, k, eat = (skb->tail + delta) - skb->end;
1629 if (eat > 0 || skb_cloned(skb)) {
1630 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1631 GFP_ATOMIC))
1632 return NULL;
1635 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1636 BUG();
1638 /* Optimization: no fragments, no reasons to preestimate
1639 * size of pulled pages. Superb.
1641 if (!skb_has_frag_list(skb))
1642 goto pull_pages;
1644 /* Estimate size of pulled pages. */
1645 eat = delta;
1646 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1647 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1649 if (size >= eat)
1650 goto pull_pages;
1651 eat -= size;
1654 /* If we need update frag list, we are in troubles.
1655 * Certainly, it possible to add an offset to skb data,
1656 * but taking into account that pulling is expected to
1657 * be very rare operation, it is worth to fight against
1658 * further bloating skb head and crucify ourselves here instead.
1659 * Pure masohism, indeed. 8)8)
1661 if (eat) {
1662 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1663 struct sk_buff *clone = NULL;
1664 struct sk_buff *insp = NULL;
1666 do {
1667 BUG_ON(!list);
1669 if (list->len <= eat) {
1670 /* Eaten as whole. */
1671 eat -= list->len;
1672 list = list->next;
1673 insp = list;
1674 } else {
1675 /* Eaten partially. */
1677 if (skb_shared(list)) {
1678 /* Sucks! We need to fork list. :-( */
1679 clone = skb_clone(list, GFP_ATOMIC);
1680 if (!clone)
1681 return NULL;
1682 insp = list->next;
1683 list = clone;
1684 } else {
1685 /* This may be pulled without
1686 * problems. */
1687 insp = list;
1689 if (!pskb_pull(list, eat)) {
1690 kfree_skb(clone);
1691 return NULL;
1693 break;
1695 } while (eat);
1697 /* Free pulled out fragments. */
1698 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1699 skb_shinfo(skb)->frag_list = list->next;
1700 kfree_skb(list);
1702 /* And insert new clone at head. */
1703 if (clone) {
1704 clone->next = list;
1705 skb_shinfo(skb)->frag_list = clone;
1708 /* Success! Now we may commit changes to skb data. */
1710 pull_pages:
1711 eat = delta;
1712 k = 0;
1713 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1714 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1716 if (size <= eat) {
1717 skb_frag_unref(skb, i);
1718 eat -= size;
1719 } else {
1720 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1721 if (eat) {
1722 skb_shinfo(skb)->frags[k].page_offset += eat;
1723 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1724 eat = 0;
1726 k++;
1729 skb_shinfo(skb)->nr_frags = k;
1731 skb->tail += delta;
1732 skb->data_len -= delta;
1734 return skb_tail_pointer(skb);
1736 EXPORT_SYMBOL(__pskb_pull_tail);
1739 * skb_copy_bits - copy bits from skb to kernel buffer
1740 * @skb: source skb
1741 * @offset: offset in source
1742 * @to: destination buffer
1743 * @len: number of bytes to copy
1745 * Copy the specified number of bytes from the source skb to the
1746 * destination buffer.
1748 * CAUTION ! :
1749 * If its prototype is ever changed,
1750 * check arch/{*}/net/{*}.S files,
1751 * since it is called from BPF assembly code.
1753 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1755 int start = skb_headlen(skb);
1756 struct sk_buff *frag_iter;
1757 int i, copy;
1759 if (offset > (int)skb->len - len)
1760 goto fault;
1762 /* Copy header. */
1763 if ((copy = start - offset) > 0) {
1764 if (copy > len)
1765 copy = len;
1766 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1767 if ((len -= copy) == 0)
1768 return 0;
1769 offset += copy;
1770 to += copy;
1773 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1774 int end;
1775 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1777 WARN_ON(start > offset + len);
1779 end = start + skb_frag_size(f);
1780 if ((copy = end - offset) > 0) {
1781 u8 *vaddr;
1783 if (copy > len)
1784 copy = len;
1786 vaddr = kmap_atomic(skb_frag_page(f));
1787 memcpy(to,
1788 vaddr + f->page_offset + offset - start,
1789 copy);
1790 kunmap_atomic(vaddr);
1792 if ((len -= copy) == 0)
1793 return 0;
1794 offset += copy;
1795 to += copy;
1797 start = end;
1800 skb_walk_frags(skb, frag_iter) {
1801 int end;
1803 WARN_ON(start > offset + len);
1805 end = start + frag_iter->len;
1806 if ((copy = end - offset) > 0) {
1807 if (copy > len)
1808 copy = len;
1809 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1810 goto fault;
1811 if ((len -= copy) == 0)
1812 return 0;
1813 offset += copy;
1814 to += copy;
1816 start = end;
1819 if (!len)
1820 return 0;
1822 fault:
1823 return -EFAULT;
1825 EXPORT_SYMBOL(skb_copy_bits);
1828 * Callback from splice_to_pipe(), if we need to release some pages
1829 * at the end of the spd in case we error'ed out in filling the pipe.
1831 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1833 put_page(spd->pages[i]);
1836 static struct page *linear_to_page(struct page *page, unsigned int *len,
1837 unsigned int *offset,
1838 struct sock *sk)
1840 struct page_frag *pfrag = sk_page_frag(sk);
1842 if (!sk_page_frag_refill(sk, pfrag))
1843 return NULL;
1845 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1847 memcpy(page_address(pfrag->page) + pfrag->offset,
1848 page_address(page) + *offset, *len);
1849 *offset = pfrag->offset;
1850 pfrag->offset += *len;
1852 return pfrag->page;
1855 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1856 struct page *page,
1857 unsigned int offset)
1859 return spd->nr_pages &&
1860 spd->pages[spd->nr_pages - 1] == page &&
1861 (spd->partial[spd->nr_pages - 1].offset +
1862 spd->partial[spd->nr_pages - 1].len == offset);
1866 * Fill page/offset/length into spd, if it can hold more pages.
1868 static bool spd_fill_page(struct splice_pipe_desc *spd,
1869 struct pipe_inode_info *pipe, struct page *page,
1870 unsigned int *len, unsigned int offset,
1871 bool linear,
1872 struct sock *sk)
1874 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1875 return true;
1877 if (linear) {
1878 page = linear_to_page(page, len, &offset, sk);
1879 if (!page)
1880 return true;
1882 if (spd_can_coalesce(spd, page, offset)) {
1883 spd->partial[spd->nr_pages - 1].len += *len;
1884 return false;
1886 get_page(page);
1887 spd->pages[spd->nr_pages] = page;
1888 spd->partial[spd->nr_pages].len = *len;
1889 spd->partial[spd->nr_pages].offset = offset;
1890 spd->nr_pages++;
1892 return false;
1895 static bool __splice_segment(struct page *page, unsigned int poff,
1896 unsigned int plen, unsigned int *off,
1897 unsigned int *len,
1898 struct splice_pipe_desc *spd, bool linear,
1899 struct sock *sk,
1900 struct pipe_inode_info *pipe)
1902 if (!*len)
1903 return true;
1905 /* skip this segment if already processed */
1906 if (*off >= plen) {
1907 *off -= plen;
1908 return false;
1911 /* ignore any bits we already processed */
1912 poff += *off;
1913 plen -= *off;
1914 *off = 0;
1916 do {
1917 unsigned int flen = min(*len, plen);
1919 if (spd_fill_page(spd, pipe, page, &flen, poff,
1920 linear, sk))
1921 return true;
1922 poff += flen;
1923 plen -= flen;
1924 *len -= flen;
1925 } while (*len && plen);
1927 return false;
1931 * Map linear and fragment data from the skb to spd. It reports true if the
1932 * pipe is full or if we already spliced the requested length.
1934 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1935 unsigned int *offset, unsigned int *len,
1936 struct splice_pipe_desc *spd, struct sock *sk)
1938 int seg;
1939 struct sk_buff *iter;
1941 /* map the linear part :
1942 * If skb->head_frag is set, this 'linear' part is backed by a
1943 * fragment, and if the head is not shared with any clones then
1944 * we can avoid a copy since we own the head portion of this page.
1946 if (__splice_segment(virt_to_page(skb->data),
1947 (unsigned long) skb->data & (PAGE_SIZE - 1),
1948 skb_headlen(skb),
1949 offset, len, spd,
1950 skb_head_is_locked(skb),
1951 sk, pipe))
1952 return true;
1955 * then map the fragments
1957 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1958 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1960 if (__splice_segment(skb_frag_page(f),
1961 f->page_offset, skb_frag_size(f),
1962 offset, len, spd, false, sk, pipe))
1963 return true;
1966 skb_walk_frags(skb, iter) {
1967 if (*offset >= iter->len) {
1968 *offset -= iter->len;
1969 continue;
1971 /* __skb_splice_bits() only fails if the output has no room
1972 * left, so no point in going over the frag_list for the error
1973 * case.
1975 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1976 return true;
1979 return false;
1983 * Map data from the skb to a pipe. Should handle both the linear part,
1984 * the fragments, and the frag list.
1986 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1987 struct pipe_inode_info *pipe, unsigned int tlen,
1988 unsigned int flags)
1990 struct partial_page partial[MAX_SKB_FRAGS];
1991 struct page *pages[MAX_SKB_FRAGS];
1992 struct splice_pipe_desc spd = {
1993 .pages = pages,
1994 .partial = partial,
1995 .nr_pages_max = MAX_SKB_FRAGS,
1996 .flags = flags,
1997 .ops = &nosteal_pipe_buf_ops,
1998 .spd_release = sock_spd_release,
2000 int ret = 0;
2002 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2004 if (spd.nr_pages)
2005 ret = splice_to_pipe(pipe, &spd);
2007 return ret;
2009 EXPORT_SYMBOL_GPL(skb_splice_bits);
2012 * skb_store_bits - store bits from kernel buffer to skb
2013 * @skb: destination buffer
2014 * @offset: offset in destination
2015 * @from: source buffer
2016 * @len: number of bytes to copy
2018 * Copy the specified number of bytes from the source buffer to the
2019 * destination skb. This function handles all the messy bits of
2020 * traversing fragment lists and such.
2023 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2025 int start = skb_headlen(skb);
2026 struct sk_buff *frag_iter;
2027 int i, copy;
2029 if (offset > (int)skb->len - len)
2030 goto fault;
2032 if ((copy = start - offset) > 0) {
2033 if (copy > len)
2034 copy = len;
2035 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2036 if ((len -= copy) == 0)
2037 return 0;
2038 offset += copy;
2039 from += copy;
2042 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2043 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2044 int end;
2046 WARN_ON(start > offset + len);
2048 end = start + skb_frag_size(frag);
2049 if ((copy = end - offset) > 0) {
2050 u8 *vaddr;
2052 if (copy > len)
2053 copy = len;
2055 vaddr = kmap_atomic(skb_frag_page(frag));
2056 memcpy(vaddr + frag->page_offset + offset - start,
2057 from, copy);
2058 kunmap_atomic(vaddr);
2060 if ((len -= copy) == 0)
2061 return 0;
2062 offset += copy;
2063 from += copy;
2065 start = end;
2068 skb_walk_frags(skb, frag_iter) {
2069 int end;
2071 WARN_ON(start > offset + len);
2073 end = start + frag_iter->len;
2074 if ((copy = end - offset) > 0) {
2075 if (copy > len)
2076 copy = len;
2077 if (skb_store_bits(frag_iter, offset - start,
2078 from, copy))
2079 goto fault;
2080 if ((len -= copy) == 0)
2081 return 0;
2082 offset += copy;
2083 from += copy;
2085 start = end;
2087 if (!len)
2088 return 0;
2090 fault:
2091 return -EFAULT;
2093 EXPORT_SYMBOL(skb_store_bits);
2095 /* Checksum skb data. */
2096 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2097 __wsum csum, const struct skb_checksum_ops *ops)
2099 int start = skb_headlen(skb);
2100 int i, copy = start - offset;
2101 struct sk_buff *frag_iter;
2102 int pos = 0;
2104 /* Checksum header. */
2105 if (copy > 0) {
2106 if (copy > len)
2107 copy = len;
2108 csum = ops->update(skb->data + offset, copy, csum);
2109 if ((len -= copy) == 0)
2110 return csum;
2111 offset += copy;
2112 pos = copy;
2115 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2116 int end;
2117 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2119 WARN_ON(start > offset + len);
2121 end = start + skb_frag_size(frag);
2122 if ((copy = end - offset) > 0) {
2123 __wsum csum2;
2124 u8 *vaddr;
2126 if (copy > len)
2127 copy = len;
2128 vaddr = kmap_atomic(skb_frag_page(frag));
2129 csum2 = ops->update(vaddr + frag->page_offset +
2130 offset - start, copy, 0);
2131 kunmap_atomic(vaddr);
2132 csum = ops->combine(csum, csum2, pos, copy);
2133 if (!(len -= copy))
2134 return csum;
2135 offset += copy;
2136 pos += copy;
2138 start = end;
2141 skb_walk_frags(skb, frag_iter) {
2142 int end;
2144 WARN_ON(start > offset + len);
2146 end = start + frag_iter->len;
2147 if ((copy = end - offset) > 0) {
2148 __wsum csum2;
2149 if (copy > len)
2150 copy = len;
2151 csum2 = __skb_checksum(frag_iter, offset - start,
2152 copy, 0, ops);
2153 csum = ops->combine(csum, csum2, pos, copy);
2154 if ((len -= copy) == 0)
2155 return csum;
2156 offset += copy;
2157 pos += copy;
2159 start = end;
2161 BUG_ON(len);
2163 return csum;
2165 EXPORT_SYMBOL(__skb_checksum);
2167 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2168 int len, __wsum csum)
2170 const struct skb_checksum_ops ops = {
2171 .update = csum_partial_ext,
2172 .combine = csum_block_add_ext,
2175 return __skb_checksum(skb, offset, len, csum, &ops);
2177 EXPORT_SYMBOL(skb_checksum);
2179 /* Both of above in one bottle. */
2181 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2182 u8 *to, int len, __wsum csum)
2184 int start = skb_headlen(skb);
2185 int i, copy = start - offset;
2186 struct sk_buff *frag_iter;
2187 int pos = 0;
2189 /* Copy header. */
2190 if (copy > 0) {
2191 if (copy > len)
2192 copy = len;
2193 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2194 copy, csum);
2195 if ((len -= copy) == 0)
2196 return csum;
2197 offset += copy;
2198 to += copy;
2199 pos = copy;
2202 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2203 int end;
2205 WARN_ON(start > offset + len);
2207 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2208 if ((copy = end - offset) > 0) {
2209 __wsum csum2;
2210 u8 *vaddr;
2211 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2213 if (copy > len)
2214 copy = len;
2215 vaddr = kmap_atomic(skb_frag_page(frag));
2216 csum2 = csum_partial_copy_nocheck(vaddr +
2217 frag->page_offset +
2218 offset - start, to,
2219 copy, 0);
2220 kunmap_atomic(vaddr);
2221 csum = csum_block_add(csum, csum2, pos);
2222 if (!(len -= copy))
2223 return csum;
2224 offset += copy;
2225 to += copy;
2226 pos += copy;
2228 start = end;
2231 skb_walk_frags(skb, frag_iter) {
2232 __wsum csum2;
2233 int end;
2235 WARN_ON(start > offset + len);
2237 end = start + frag_iter->len;
2238 if ((copy = end - offset) > 0) {
2239 if (copy > len)
2240 copy = len;
2241 csum2 = skb_copy_and_csum_bits(frag_iter,
2242 offset - start,
2243 to, copy, 0);
2244 csum = csum_block_add(csum, csum2, pos);
2245 if ((len -= copy) == 0)
2246 return csum;
2247 offset += copy;
2248 to += copy;
2249 pos += copy;
2251 start = end;
2253 BUG_ON(len);
2254 return csum;
2256 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2259 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2260 * @from: source buffer
2262 * Calculates the amount of linear headroom needed in the 'to' skb passed
2263 * into skb_zerocopy().
2265 unsigned int
2266 skb_zerocopy_headlen(const struct sk_buff *from)
2268 unsigned int hlen = 0;
2270 if (!from->head_frag ||
2271 skb_headlen(from) < L1_CACHE_BYTES ||
2272 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2273 hlen = skb_headlen(from);
2275 if (skb_has_frag_list(from))
2276 hlen = from->len;
2278 return hlen;
2280 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2283 * skb_zerocopy - Zero copy skb to skb
2284 * @to: destination buffer
2285 * @from: source buffer
2286 * @len: number of bytes to copy from source buffer
2287 * @hlen: size of linear headroom in destination buffer
2289 * Copies up to `len` bytes from `from` to `to` by creating references
2290 * to the frags in the source buffer.
2292 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2293 * headroom in the `to` buffer.
2295 * Return value:
2296 * 0: everything is OK
2297 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2298 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2301 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2303 int i, j = 0;
2304 int plen = 0; /* length of skb->head fragment */
2305 int ret;
2306 struct page *page;
2307 unsigned int offset;
2309 BUG_ON(!from->head_frag && !hlen);
2311 /* dont bother with small payloads */
2312 if (len <= skb_tailroom(to))
2313 return skb_copy_bits(from, 0, skb_put(to, len), len);
2315 if (hlen) {
2316 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2317 if (unlikely(ret))
2318 return ret;
2319 len -= hlen;
2320 } else {
2321 plen = min_t(int, skb_headlen(from), len);
2322 if (plen) {
2323 page = virt_to_head_page(from->head);
2324 offset = from->data - (unsigned char *)page_address(page);
2325 __skb_fill_page_desc(to, 0, page, offset, plen);
2326 get_page(page);
2327 j = 1;
2328 len -= plen;
2332 to->truesize += len + plen;
2333 to->len += len + plen;
2334 to->data_len += len + plen;
2336 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2337 skb_tx_error(from);
2338 return -ENOMEM;
2341 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2342 if (!len)
2343 break;
2344 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2345 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2346 len -= skb_shinfo(to)->frags[j].size;
2347 skb_frag_ref(to, j);
2348 j++;
2350 skb_shinfo(to)->nr_frags = j;
2352 return 0;
2354 EXPORT_SYMBOL_GPL(skb_zerocopy);
2356 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2358 __wsum csum;
2359 long csstart;
2361 if (skb->ip_summed == CHECKSUM_PARTIAL)
2362 csstart = skb_checksum_start_offset(skb);
2363 else
2364 csstart = skb_headlen(skb);
2366 BUG_ON(csstart > skb_headlen(skb));
2368 skb_copy_from_linear_data(skb, to, csstart);
2370 csum = 0;
2371 if (csstart != skb->len)
2372 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2373 skb->len - csstart, 0);
2375 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2376 long csstuff = csstart + skb->csum_offset;
2378 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2381 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2384 * skb_dequeue - remove from the head of the queue
2385 * @list: list to dequeue from
2387 * Remove the head of the list. The list lock is taken so the function
2388 * may be used safely with other locking list functions. The head item is
2389 * returned or %NULL if the list is empty.
2392 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2394 unsigned long flags;
2395 struct sk_buff *result;
2397 spin_lock_irqsave(&list->lock, flags);
2398 result = __skb_dequeue(list);
2399 spin_unlock_irqrestore(&list->lock, flags);
2400 return result;
2402 EXPORT_SYMBOL(skb_dequeue);
2405 * skb_dequeue_tail - remove from the tail of the queue
2406 * @list: list to dequeue from
2408 * Remove the tail of the list. The list lock is taken so the function
2409 * may be used safely with other locking list functions. The tail item is
2410 * returned or %NULL if the list is empty.
2412 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2414 unsigned long flags;
2415 struct sk_buff *result;
2417 spin_lock_irqsave(&list->lock, flags);
2418 result = __skb_dequeue_tail(list);
2419 spin_unlock_irqrestore(&list->lock, flags);
2420 return result;
2422 EXPORT_SYMBOL(skb_dequeue_tail);
2425 * skb_queue_purge - empty a list
2426 * @list: list to empty
2428 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2429 * the list and one reference dropped. This function takes the list
2430 * lock and is atomic with respect to other list locking functions.
2432 void skb_queue_purge(struct sk_buff_head *list)
2434 struct sk_buff *skb;
2435 while ((skb = skb_dequeue(list)) != NULL)
2436 kfree_skb(skb);
2438 EXPORT_SYMBOL(skb_queue_purge);
2441 * skb_rbtree_purge - empty a skb rbtree
2442 * @root: root of the rbtree to empty
2443 * Return value: the sum of truesizes of all purged skbs.
2445 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2446 * the list and one reference dropped. This function does not take
2447 * any lock. Synchronization should be handled by the caller (e.g., TCP
2448 * out-of-order queue is protected by the socket lock).
2450 unsigned int skb_rbtree_purge(struct rb_root *root)
2452 struct rb_node *p = rb_first(root);
2453 unsigned int sum = 0;
2455 while (p) {
2456 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2458 p = rb_next(p);
2459 rb_erase(&skb->rbnode, root);
2460 sum += skb->truesize;
2461 kfree_skb(skb);
2463 return sum;
2467 * skb_queue_head - queue a buffer at the list head
2468 * @list: list to use
2469 * @newsk: buffer to queue
2471 * Queue a buffer at the start of the list. This function takes the
2472 * list lock and can be used safely with other locking &sk_buff functions
2473 * safely.
2475 * A buffer cannot be placed on two lists at the same time.
2477 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2479 unsigned long flags;
2481 spin_lock_irqsave(&list->lock, flags);
2482 __skb_queue_head(list, newsk);
2483 spin_unlock_irqrestore(&list->lock, flags);
2485 EXPORT_SYMBOL(skb_queue_head);
2488 * skb_queue_tail - queue a buffer at the list tail
2489 * @list: list to use
2490 * @newsk: buffer to queue
2492 * Queue a buffer at the tail of the list. This function takes the
2493 * list lock and can be used safely with other locking &sk_buff functions
2494 * safely.
2496 * A buffer cannot be placed on two lists at the same time.
2498 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2500 unsigned long flags;
2502 spin_lock_irqsave(&list->lock, flags);
2503 __skb_queue_tail(list, newsk);
2504 spin_unlock_irqrestore(&list->lock, flags);
2506 EXPORT_SYMBOL(skb_queue_tail);
2509 * skb_unlink - remove a buffer from a list
2510 * @skb: buffer to remove
2511 * @list: list to use
2513 * Remove a packet from a list. The list locks are taken and this
2514 * function is atomic with respect to other list locked calls
2516 * You must know what list the SKB is on.
2518 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2520 unsigned long flags;
2522 spin_lock_irqsave(&list->lock, flags);
2523 __skb_unlink(skb, list);
2524 spin_unlock_irqrestore(&list->lock, flags);
2526 EXPORT_SYMBOL(skb_unlink);
2529 * skb_append - append a buffer
2530 * @old: buffer to insert after
2531 * @newsk: buffer to insert
2532 * @list: list to use
2534 * Place a packet after a given packet in a list. The list locks are taken
2535 * and this function is atomic with respect to other list locked calls.
2536 * A buffer cannot be placed on two lists at the same time.
2538 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2540 unsigned long flags;
2542 spin_lock_irqsave(&list->lock, flags);
2543 __skb_queue_after(list, old, newsk);
2544 spin_unlock_irqrestore(&list->lock, flags);
2546 EXPORT_SYMBOL(skb_append);
2549 * skb_insert - insert a buffer
2550 * @old: buffer to insert before
2551 * @newsk: buffer to insert
2552 * @list: list to use
2554 * Place a packet before a given packet in a list. The list locks are
2555 * taken and this function is atomic with respect to other list locked
2556 * calls.
2558 * A buffer cannot be placed on two lists at the same time.
2560 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2562 unsigned long flags;
2564 spin_lock_irqsave(&list->lock, flags);
2565 __skb_insert(newsk, old->prev, old, list);
2566 spin_unlock_irqrestore(&list->lock, flags);
2568 EXPORT_SYMBOL(skb_insert);
2570 static inline void skb_split_inside_header(struct sk_buff *skb,
2571 struct sk_buff* skb1,
2572 const u32 len, const int pos)
2574 int i;
2576 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2577 pos - len);
2578 /* And move data appendix as is. */
2579 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2580 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2582 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2583 skb_shinfo(skb)->nr_frags = 0;
2584 skb1->data_len = skb->data_len;
2585 skb1->len += skb1->data_len;
2586 skb->data_len = 0;
2587 skb->len = len;
2588 skb_set_tail_pointer(skb, len);
2591 static inline void skb_split_no_header(struct sk_buff *skb,
2592 struct sk_buff* skb1,
2593 const u32 len, int pos)
2595 int i, k = 0;
2596 const int nfrags = skb_shinfo(skb)->nr_frags;
2598 skb_shinfo(skb)->nr_frags = 0;
2599 skb1->len = skb1->data_len = skb->len - len;
2600 skb->len = len;
2601 skb->data_len = len - pos;
2603 for (i = 0; i < nfrags; i++) {
2604 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2606 if (pos + size > len) {
2607 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2609 if (pos < len) {
2610 /* Split frag.
2611 * We have two variants in this case:
2612 * 1. Move all the frag to the second
2613 * part, if it is possible. F.e.
2614 * this approach is mandatory for TUX,
2615 * where splitting is expensive.
2616 * 2. Split is accurately. We make this.
2618 skb_frag_ref(skb, i);
2619 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2620 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2621 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2622 skb_shinfo(skb)->nr_frags++;
2624 k++;
2625 } else
2626 skb_shinfo(skb)->nr_frags++;
2627 pos += size;
2629 skb_shinfo(skb1)->nr_frags = k;
2633 * skb_split - Split fragmented skb to two parts at length len.
2634 * @skb: the buffer to split
2635 * @skb1: the buffer to receive the second part
2636 * @len: new length for skb
2638 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2640 int pos = skb_headlen(skb);
2642 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2643 SKBTX_SHARED_FRAG;
2644 if (len < pos) /* Split line is inside header. */
2645 skb_split_inside_header(skb, skb1, len, pos);
2646 else /* Second chunk has no header, nothing to copy. */
2647 skb_split_no_header(skb, skb1, len, pos);
2649 EXPORT_SYMBOL(skb_split);
2651 /* Shifting from/to a cloned skb is a no-go.
2653 * Caller cannot keep skb_shinfo related pointers past calling here!
2655 static int skb_prepare_for_shift(struct sk_buff *skb)
2657 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2661 * skb_shift - Shifts paged data partially from skb to another
2662 * @tgt: buffer into which tail data gets added
2663 * @skb: buffer from which the paged data comes from
2664 * @shiftlen: shift up to this many bytes
2666 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2667 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2668 * It's up to caller to free skb if everything was shifted.
2670 * If @tgt runs out of frags, the whole operation is aborted.
2672 * Skb cannot include anything else but paged data while tgt is allowed
2673 * to have non-paged data as well.
2675 * TODO: full sized shift could be optimized but that would need
2676 * specialized skb free'er to handle frags without up-to-date nr_frags.
2678 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2680 int from, to, merge, todo;
2681 struct skb_frag_struct *fragfrom, *fragto;
2683 BUG_ON(shiftlen > skb->len);
2684 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2686 todo = shiftlen;
2687 from = 0;
2688 to = skb_shinfo(tgt)->nr_frags;
2689 fragfrom = &skb_shinfo(skb)->frags[from];
2691 /* Actual merge is delayed until the point when we know we can
2692 * commit all, so that we don't have to undo partial changes
2694 if (!to ||
2695 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2696 fragfrom->page_offset)) {
2697 merge = -1;
2698 } else {
2699 merge = to - 1;
2701 todo -= skb_frag_size(fragfrom);
2702 if (todo < 0) {
2703 if (skb_prepare_for_shift(skb) ||
2704 skb_prepare_for_shift(tgt))
2705 return 0;
2707 /* All previous frag pointers might be stale! */
2708 fragfrom = &skb_shinfo(skb)->frags[from];
2709 fragto = &skb_shinfo(tgt)->frags[merge];
2711 skb_frag_size_add(fragto, shiftlen);
2712 skb_frag_size_sub(fragfrom, shiftlen);
2713 fragfrom->page_offset += shiftlen;
2715 goto onlymerged;
2718 from++;
2721 /* Skip full, not-fitting skb to avoid expensive operations */
2722 if ((shiftlen == skb->len) &&
2723 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2724 return 0;
2726 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2727 return 0;
2729 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2730 if (to == MAX_SKB_FRAGS)
2731 return 0;
2733 fragfrom = &skb_shinfo(skb)->frags[from];
2734 fragto = &skb_shinfo(tgt)->frags[to];
2736 if (todo >= skb_frag_size(fragfrom)) {
2737 *fragto = *fragfrom;
2738 todo -= skb_frag_size(fragfrom);
2739 from++;
2740 to++;
2742 } else {
2743 __skb_frag_ref(fragfrom);
2744 fragto->page = fragfrom->page;
2745 fragto->page_offset = fragfrom->page_offset;
2746 skb_frag_size_set(fragto, todo);
2748 fragfrom->page_offset += todo;
2749 skb_frag_size_sub(fragfrom, todo);
2750 todo = 0;
2752 to++;
2753 break;
2757 /* Ready to "commit" this state change to tgt */
2758 skb_shinfo(tgt)->nr_frags = to;
2760 if (merge >= 0) {
2761 fragfrom = &skb_shinfo(skb)->frags[0];
2762 fragto = &skb_shinfo(tgt)->frags[merge];
2764 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2765 __skb_frag_unref(fragfrom);
2768 /* Reposition in the original skb */
2769 to = 0;
2770 while (from < skb_shinfo(skb)->nr_frags)
2771 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2772 skb_shinfo(skb)->nr_frags = to;
2774 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2776 onlymerged:
2777 /* Most likely the tgt won't ever need its checksum anymore, skb on
2778 * the other hand might need it if it needs to be resent
2780 tgt->ip_summed = CHECKSUM_PARTIAL;
2781 skb->ip_summed = CHECKSUM_PARTIAL;
2783 /* Yak, is it really working this way? Some helper please? */
2784 skb->len -= shiftlen;
2785 skb->data_len -= shiftlen;
2786 skb->truesize -= shiftlen;
2787 tgt->len += shiftlen;
2788 tgt->data_len += shiftlen;
2789 tgt->truesize += shiftlen;
2791 return shiftlen;
2795 * skb_prepare_seq_read - Prepare a sequential read of skb data
2796 * @skb: the buffer to read
2797 * @from: lower offset of data to be read
2798 * @to: upper offset of data to be read
2799 * @st: state variable
2801 * Initializes the specified state variable. Must be called before
2802 * invoking skb_seq_read() for the first time.
2804 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2805 unsigned int to, struct skb_seq_state *st)
2807 st->lower_offset = from;
2808 st->upper_offset = to;
2809 st->root_skb = st->cur_skb = skb;
2810 st->frag_idx = st->stepped_offset = 0;
2811 st->frag_data = NULL;
2813 EXPORT_SYMBOL(skb_prepare_seq_read);
2816 * skb_seq_read - Sequentially read skb data
2817 * @consumed: number of bytes consumed by the caller so far
2818 * @data: destination pointer for data to be returned
2819 * @st: state variable
2821 * Reads a block of skb data at @consumed relative to the
2822 * lower offset specified to skb_prepare_seq_read(). Assigns
2823 * the head of the data block to @data and returns the length
2824 * of the block or 0 if the end of the skb data or the upper
2825 * offset has been reached.
2827 * The caller is not required to consume all of the data
2828 * returned, i.e. @consumed is typically set to the number
2829 * of bytes already consumed and the next call to
2830 * skb_seq_read() will return the remaining part of the block.
2832 * Note 1: The size of each block of data returned can be arbitrary,
2833 * this limitation is the cost for zerocopy sequential
2834 * reads of potentially non linear data.
2836 * Note 2: Fragment lists within fragments are not implemented
2837 * at the moment, state->root_skb could be replaced with
2838 * a stack for this purpose.
2840 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2841 struct skb_seq_state *st)
2843 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2844 skb_frag_t *frag;
2846 if (unlikely(abs_offset >= st->upper_offset)) {
2847 if (st->frag_data) {
2848 kunmap_atomic(st->frag_data);
2849 st->frag_data = NULL;
2851 return 0;
2854 next_skb:
2855 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2857 if (abs_offset < block_limit && !st->frag_data) {
2858 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2859 return block_limit - abs_offset;
2862 if (st->frag_idx == 0 && !st->frag_data)
2863 st->stepped_offset += skb_headlen(st->cur_skb);
2865 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2866 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2867 block_limit = skb_frag_size(frag) + st->stepped_offset;
2869 if (abs_offset < block_limit) {
2870 if (!st->frag_data)
2871 st->frag_data = kmap_atomic(skb_frag_page(frag));
2873 *data = (u8 *) st->frag_data + frag->page_offset +
2874 (abs_offset - st->stepped_offset);
2876 return block_limit - abs_offset;
2879 if (st->frag_data) {
2880 kunmap_atomic(st->frag_data);
2881 st->frag_data = NULL;
2884 st->frag_idx++;
2885 st->stepped_offset += skb_frag_size(frag);
2888 if (st->frag_data) {
2889 kunmap_atomic(st->frag_data);
2890 st->frag_data = NULL;
2893 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2894 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2895 st->frag_idx = 0;
2896 goto next_skb;
2897 } else if (st->cur_skb->next) {
2898 st->cur_skb = st->cur_skb->next;
2899 st->frag_idx = 0;
2900 goto next_skb;
2903 return 0;
2905 EXPORT_SYMBOL(skb_seq_read);
2908 * skb_abort_seq_read - Abort a sequential read of skb data
2909 * @st: state variable
2911 * Must be called if skb_seq_read() was not called until it
2912 * returned 0.
2914 void skb_abort_seq_read(struct skb_seq_state *st)
2916 if (st->frag_data)
2917 kunmap_atomic(st->frag_data);
2919 EXPORT_SYMBOL(skb_abort_seq_read);
2921 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2923 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2924 struct ts_config *conf,
2925 struct ts_state *state)
2927 return skb_seq_read(offset, text, TS_SKB_CB(state));
2930 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2932 skb_abort_seq_read(TS_SKB_CB(state));
2936 * skb_find_text - Find a text pattern in skb data
2937 * @skb: the buffer to look in
2938 * @from: search offset
2939 * @to: search limit
2940 * @config: textsearch configuration
2942 * Finds a pattern in the skb data according to the specified
2943 * textsearch configuration. Use textsearch_next() to retrieve
2944 * subsequent occurrences of the pattern. Returns the offset
2945 * to the first occurrence or UINT_MAX if no match was found.
2947 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2948 unsigned int to, struct ts_config *config)
2950 struct ts_state state;
2951 unsigned int ret;
2953 config->get_next_block = skb_ts_get_next_block;
2954 config->finish = skb_ts_finish;
2956 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2958 ret = textsearch_find(config, &state);
2959 return (ret <= to - from ? ret : UINT_MAX);
2961 EXPORT_SYMBOL(skb_find_text);
2964 * skb_append_datato_frags - append the user data to a skb
2965 * @sk: sock structure
2966 * @skb: skb structure to be appended with user data.
2967 * @getfrag: call back function to be used for getting the user data
2968 * @from: pointer to user message iov
2969 * @length: length of the iov message
2971 * Description: This procedure append the user data in the fragment part
2972 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2974 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2975 int (*getfrag)(void *from, char *to, int offset,
2976 int len, int odd, struct sk_buff *skb),
2977 void *from, int length)
2979 int frg_cnt = skb_shinfo(skb)->nr_frags;
2980 int copy;
2981 int offset = 0;
2982 int ret;
2983 struct page_frag *pfrag = &current->task_frag;
2985 do {
2986 /* Return error if we don't have space for new frag */
2987 if (frg_cnt >= MAX_SKB_FRAGS)
2988 return -EMSGSIZE;
2990 if (!sk_page_frag_refill(sk, pfrag))
2991 return -ENOMEM;
2993 /* copy the user data to page */
2994 copy = min_t(int, length, pfrag->size - pfrag->offset);
2996 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2997 offset, copy, 0, skb);
2998 if (ret < 0)
2999 return -EFAULT;
3001 /* copy was successful so update the size parameters */
3002 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3003 copy);
3004 frg_cnt++;
3005 pfrag->offset += copy;
3006 get_page(pfrag->page);
3008 skb->truesize += copy;
3009 atomic_add(copy, &sk->sk_wmem_alloc);
3010 skb->len += copy;
3011 skb->data_len += copy;
3012 offset += copy;
3013 length -= copy;
3015 } while (length > 0);
3017 return 0;
3019 EXPORT_SYMBOL(skb_append_datato_frags);
3021 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3022 int offset, size_t size)
3024 int i = skb_shinfo(skb)->nr_frags;
3026 if (skb_can_coalesce(skb, i, page, offset)) {
3027 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3028 } else if (i < MAX_SKB_FRAGS) {
3029 get_page(page);
3030 skb_fill_page_desc(skb, i, page, offset, size);
3031 } else {
3032 return -EMSGSIZE;
3035 return 0;
3037 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3040 * skb_pull_rcsum - pull skb and update receive checksum
3041 * @skb: buffer to update
3042 * @len: length of data pulled
3044 * This function performs an skb_pull on the packet and updates
3045 * the CHECKSUM_COMPLETE checksum. It should be used on
3046 * receive path processing instead of skb_pull unless you know
3047 * that the checksum difference is zero (e.g., a valid IP header)
3048 * or you are setting ip_summed to CHECKSUM_NONE.
3050 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3052 unsigned char *data = skb->data;
3054 BUG_ON(len > skb->len);
3055 __skb_pull(skb, len);
3056 skb_postpull_rcsum(skb, data, len);
3057 return skb->data;
3059 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3062 * skb_segment - Perform protocol segmentation on skb.
3063 * @head_skb: buffer to segment
3064 * @features: features for the output path (see dev->features)
3066 * This function performs segmentation on the given skb. It returns
3067 * a pointer to the first in a list of new skbs for the segments.
3068 * In case of error it returns ERR_PTR(err).
3070 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3071 netdev_features_t features)
3073 struct sk_buff *segs = NULL;
3074 struct sk_buff *tail = NULL;
3075 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3076 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3077 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3078 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3079 struct sk_buff *frag_skb = head_skb;
3080 unsigned int offset = doffset;
3081 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3082 unsigned int partial_segs = 0;
3083 unsigned int headroom;
3084 unsigned int len = head_skb->len;
3085 __be16 proto;
3086 bool csum, sg;
3087 int nfrags = skb_shinfo(head_skb)->nr_frags;
3088 int err = -ENOMEM;
3089 int i = 0;
3090 int pos;
3091 int dummy;
3093 __skb_push(head_skb, doffset);
3094 proto = skb_network_protocol(head_skb, &dummy);
3095 if (unlikely(!proto))
3096 return ERR_PTR(-EINVAL);
3098 sg = !!(features & NETIF_F_SG);
3099 csum = !!can_checksum_protocol(features, proto);
3101 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3102 if (!(features & NETIF_F_GSO_PARTIAL)) {
3103 struct sk_buff *iter;
3104 unsigned int frag_len;
3106 if (!list_skb ||
3107 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3108 goto normal;
3110 /* If we get here then all the required
3111 * GSO features except frag_list are supported.
3112 * Try to split the SKB to multiple GSO SKBs
3113 * with no frag_list.
3114 * Currently we can do that only when the buffers don't
3115 * have a linear part and all the buffers except
3116 * the last are of the same length.
3118 frag_len = list_skb->len;
3119 skb_walk_frags(head_skb, iter) {
3120 if (frag_len != iter->len && iter->next)
3121 goto normal;
3122 if (skb_headlen(iter))
3123 goto normal;
3125 len -= iter->len;
3128 if (len != frag_len)
3129 goto normal;
3132 /* GSO partial only requires that we trim off any excess that
3133 * doesn't fit into an MSS sized block, so take care of that
3134 * now.
3136 partial_segs = len / mss;
3137 if (partial_segs > 1)
3138 mss *= partial_segs;
3139 else
3140 partial_segs = 0;
3143 normal:
3144 headroom = skb_headroom(head_skb);
3145 pos = skb_headlen(head_skb);
3147 do {
3148 struct sk_buff *nskb;
3149 skb_frag_t *nskb_frag;
3150 int hsize;
3151 int size;
3153 if (unlikely(mss == GSO_BY_FRAGS)) {
3154 len = list_skb->len;
3155 } else {
3156 len = head_skb->len - offset;
3157 if (len > mss)
3158 len = mss;
3161 hsize = skb_headlen(head_skb) - offset;
3162 if (hsize < 0)
3163 hsize = 0;
3164 if (hsize > len || !sg)
3165 hsize = len;
3167 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3168 (skb_headlen(list_skb) == len || sg)) {
3169 BUG_ON(skb_headlen(list_skb) > len);
3171 i = 0;
3172 nfrags = skb_shinfo(list_skb)->nr_frags;
3173 frag = skb_shinfo(list_skb)->frags;
3174 frag_skb = list_skb;
3175 pos += skb_headlen(list_skb);
3177 while (pos < offset + len) {
3178 BUG_ON(i >= nfrags);
3180 size = skb_frag_size(frag);
3181 if (pos + size > offset + len)
3182 break;
3184 i++;
3185 pos += size;
3186 frag++;
3189 nskb = skb_clone(list_skb, GFP_ATOMIC);
3190 list_skb = list_skb->next;
3192 if (unlikely(!nskb))
3193 goto err;
3195 if (unlikely(pskb_trim(nskb, len))) {
3196 kfree_skb(nskb);
3197 goto err;
3200 hsize = skb_end_offset(nskb);
3201 if (skb_cow_head(nskb, doffset + headroom)) {
3202 kfree_skb(nskb);
3203 goto err;
3206 nskb->truesize += skb_end_offset(nskb) - hsize;
3207 skb_release_head_state(nskb);
3208 __skb_push(nskb, doffset);
3209 } else {
3210 nskb = __alloc_skb(hsize + doffset + headroom,
3211 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3212 NUMA_NO_NODE);
3214 if (unlikely(!nskb))
3215 goto err;
3217 skb_reserve(nskb, headroom);
3218 __skb_put(nskb, doffset);
3221 if (segs)
3222 tail->next = nskb;
3223 else
3224 segs = nskb;
3225 tail = nskb;
3227 __copy_skb_header(nskb, head_skb);
3229 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3230 skb_reset_mac_len(nskb);
3232 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3233 nskb->data - tnl_hlen,
3234 doffset + tnl_hlen);
3236 if (nskb->len == len + doffset)
3237 goto perform_csum_check;
3239 if (!sg) {
3240 if (!nskb->remcsum_offload)
3241 nskb->ip_summed = CHECKSUM_NONE;
3242 SKB_GSO_CB(nskb)->csum =
3243 skb_copy_and_csum_bits(head_skb, offset,
3244 skb_put(nskb, len),
3245 len, 0);
3246 SKB_GSO_CB(nskb)->csum_start =
3247 skb_headroom(nskb) + doffset;
3248 continue;
3251 nskb_frag = skb_shinfo(nskb)->frags;
3253 skb_copy_from_linear_data_offset(head_skb, offset,
3254 skb_put(nskb, hsize), hsize);
3256 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3257 SKBTX_SHARED_FRAG;
3259 while (pos < offset + len) {
3260 if (i >= nfrags) {
3261 BUG_ON(skb_headlen(list_skb));
3263 i = 0;
3264 nfrags = skb_shinfo(list_skb)->nr_frags;
3265 frag = skb_shinfo(list_skb)->frags;
3266 frag_skb = list_skb;
3268 BUG_ON(!nfrags);
3270 list_skb = list_skb->next;
3273 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3274 MAX_SKB_FRAGS)) {
3275 net_warn_ratelimited(
3276 "skb_segment: too many frags: %u %u\n",
3277 pos, mss);
3278 err = -EINVAL;
3279 goto err;
3282 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3283 goto err;
3285 *nskb_frag = *frag;
3286 __skb_frag_ref(nskb_frag);
3287 size = skb_frag_size(nskb_frag);
3289 if (pos < offset) {
3290 nskb_frag->page_offset += offset - pos;
3291 skb_frag_size_sub(nskb_frag, offset - pos);
3294 skb_shinfo(nskb)->nr_frags++;
3296 if (pos + size <= offset + len) {
3297 i++;
3298 frag++;
3299 pos += size;
3300 } else {
3301 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3302 goto skip_fraglist;
3305 nskb_frag++;
3308 skip_fraglist:
3309 nskb->data_len = len - hsize;
3310 nskb->len += nskb->data_len;
3311 nskb->truesize += nskb->data_len;
3313 perform_csum_check:
3314 if (!csum) {
3315 if (skb_has_shared_frag(nskb) &&
3316 __skb_linearize(nskb))
3317 goto err;
3319 if (!nskb->remcsum_offload)
3320 nskb->ip_summed = CHECKSUM_NONE;
3321 SKB_GSO_CB(nskb)->csum =
3322 skb_checksum(nskb, doffset,
3323 nskb->len - doffset, 0);
3324 SKB_GSO_CB(nskb)->csum_start =
3325 skb_headroom(nskb) + doffset;
3327 } while ((offset += len) < head_skb->len);
3329 /* Some callers want to get the end of the list.
3330 * Put it in segs->prev to avoid walking the list.
3331 * (see validate_xmit_skb_list() for example)
3333 segs->prev = tail;
3335 if (partial_segs) {
3336 struct sk_buff *iter;
3337 int type = skb_shinfo(head_skb)->gso_type;
3338 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3340 /* Update type to add partial and then remove dodgy if set */
3341 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3342 type &= ~SKB_GSO_DODGY;
3344 /* Update GSO info and prepare to start updating headers on
3345 * our way back down the stack of protocols.
3347 for (iter = segs; iter; iter = iter->next) {
3348 skb_shinfo(iter)->gso_size = gso_size;
3349 skb_shinfo(iter)->gso_segs = partial_segs;
3350 skb_shinfo(iter)->gso_type = type;
3351 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3354 if (tail->len - doffset <= gso_size)
3355 skb_shinfo(tail)->gso_size = 0;
3356 else if (tail != segs)
3357 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3360 /* Following permits correct backpressure, for protocols
3361 * using skb_set_owner_w().
3362 * Idea is to tranfert ownership from head_skb to last segment.
3364 if (head_skb->destructor == sock_wfree) {
3365 swap(tail->truesize, head_skb->truesize);
3366 swap(tail->destructor, head_skb->destructor);
3367 swap(tail->sk, head_skb->sk);
3369 return segs;
3371 err:
3372 kfree_skb_list(segs);
3373 return ERR_PTR(err);
3375 EXPORT_SYMBOL_GPL(skb_segment);
3377 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3379 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3380 unsigned int offset = skb_gro_offset(skb);
3381 unsigned int headlen = skb_headlen(skb);
3382 unsigned int len = skb_gro_len(skb);
3383 struct sk_buff *lp, *p = *head;
3384 unsigned int delta_truesize;
3386 if (unlikely(p->len + len >= 65536))
3387 return -E2BIG;
3389 lp = NAPI_GRO_CB(p)->last;
3390 pinfo = skb_shinfo(lp);
3392 if (headlen <= offset) {
3393 skb_frag_t *frag;
3394 skb_frag_t *frag2;
3395 int i = skbinfo->nr_frags;
3396 int nr_frags = pinfo->nr_frags + i;
3398 if (nr_frags > MAX_SKB_FRAGS)
3399 goto merge;
3401 offset -= headlen;
3402 pinfo->nr_frags = nr_frags;
3403 skbinfo->nr_frags = 0;
3405 frag = pinfo->frags + nr_frags;
3406 frag2 = skbinfo->frags + i;
3407 do {
3408 *--frag = *--frag2;
3409 } while (--i);
3411 frag->page_offset += offset;
3412 skb_frag_size_sub(frag, offset);
3414 /* all fragments truesize : remove (head size + sk_buff) */
3415 delta_truesize = skb->truesize -
3416 SKB_TRUESIZE(skb_end_offset(skb));
3418 skb->truesize -= skb->data_len;
3419 skb->len -= skb->data_len;
3420 skb->data_len = 0;
3422 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3423 goto done;
3424 } else if (skb->head_frag) {
3425 int nr_frags = pinfo->nr_frags;
3426 skb_frag_t *frag = pinfo->frags + nr_frags;
3427 struct page *page = virt_to_head_page(skb->head);
3428 unsigned int first_size = headlen - offset;
3429 unsigned int first_offset;
3431 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3432 goto merge;
3434 first_offset = skb->data -
3435 (unsigned char *)page_address(page) +
3436 offset;
3438 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3440 frag->page.p = page;
3441 frag->page_offset = first_offset;
3442 skb_frag_size_set(frag, first_size);
3444 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3445 /* We dont need to clear skbinfo->nr_frags here */
3447 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3448 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3449 goto done;
3452 merge:
3453 delta_truesize = skb->truesize;
3454 if (offset > headlen) {
3455 unsigned int eat = offset - headlen;
3457 skbinfo->frags[0].page_offset += eat;
3458 skb_frag_size_sub(&skbinfo->frags[0], eat);
3459 skb->data_len -= eat;
3460 skb->len -= eat;
3461 offset = headlen;
3464 __skb_pull(skb, offset);
3466 if (NAPI_GRO_CB(p)->last == p)
3467 skb_shinfo(p)->frag_list = skb;
3468 else
3469 NAPI_GRO_CB(p)->last->next = skb;
3470 NAPI_GRO_CB(p)->last = skb;
3471 __skb_header_release(skb);
3472 lp = p;
3474 done:
3475 NAPI_GRO_CB(p)->count++;
3476 p->data_len += len;
3477 p->truesize += delta_truesize;
3478 p->len += len;
3479 if (lp != p) {
3480 lp->data_len += len;
3481 lp->truesize += delta_truesize;
3482 lp->len += len;
3484 NAPI_GRO_CB(skb)->same_flow = 1;
3485 return 0;
3487 EXPORT_SYMBOL_GPL(skb_gro_receive);
3489 void __init skb_init(void)
3491 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3492 sizeof(struct sk_buff),
3494 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3495 NULL);
3496 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3497 sizeof(struct sk_buff_fclones),
3499 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3500 NULL);
3503 static int
3504 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3505 unsigned int recursion_level)
3507 int start = skb_headlen(skb);
3508 int i, copy = start - offset;
3509 struct sk_buff *frag_iter;
3510 int elt = 0;
3512 if (unlikely(recursion_level >= 24))
3513 return -EMSGSIZE;
3515 if (copy > 0) {
3516 if (copy > len)
3517 copy = len;
3518 sg_set_buf(sg, skb->data + offset, copy);
3519 elt++;
3520 if ((len -= copy) == 0)
3521 return elt;
3522 offset += copy;
3525 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3526 int end;
3528 WARN_ON(start > offset + len);
3530 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3531 if ((copy = end - offset) > 0) {
3532 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3533 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3534 return -EMSGSIZE;
3536 if (copy > len)
3537 copy = len;
3538 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3539 frag->page_offset+offset-start);
3540 elt++;
3541 if (!(len -= copy))
3542 return elt;
3543 offset += copy;
3545 start = end;
3548 skb_walk_frags(skb, frag_iter) {
3549 int end, ret;
3551 WARN_ON(start > offset + len);
3553 end = start + frag_iter->len;
3554 if ((copy = end - offset) > 0) {
3555 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3556 return -EMSGSIZE;
3558 if (copy > len)
3559 copy = len;
3560 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3561 copy, recursion_level + 1);
3562 if (unlikely(ret < 0))
3563 return ret;
3564 elt += ret;
3565 if ((len -= copy) == 0)
3566 return elt;
3567 offset += copy;
3569 start = end;
3571 BUG_ON(len);
3572 return elt;
3576 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3577 * @skb: Socket buffer containing the buffers to be mapped
3578 * @sg: The scatter-gather list to map into
3579 * @offset: The offset into the buffer's contents to start mapping
3580 * @len: Length of buffer space to be mapped
3582 * Fill the specified scatter-gather list with mappings/pointers into a
3583 * region of the buffer space attached to a socket buffer. Returns either
3584 * the number of scatterlist items used, or -EMSGSIZE if the contents
3585 * could not fit.
3587 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3589 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3591 if (nsg <= 0)
3592 return nsg;
3594 sg_mark_end(&sg[nsg - 1]);
3596 return nsg;
3598 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3600 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3601 * sglist without mark the sg which contain last skb data as the end.
3602 * So the caller can mannipulate sg list as will when padding new data after
3603 * the first call without calling sg_unmark_end to expend sg list.
3605 * Scenario to use skb_to_sgvec_nomark:
3606 * 1. sg_init_table
3607 * 2. skb_to_sgvec_nomark(payload1)
3608 * 3. skb_to_sgvec_nomark(payload2)
3610 * This is equivalent to:
3611 * 1. sg_init_table
3612 * 2. skb_to_sgvec(payload1)
3613 * 3. sg_unmark_end
3614 * 4. skb_to_sgvec(payload2)
3616 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3617 * is more preferable.
3619 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3620 int offset, int len)
3622 return __skb_to_sgvec(skb, sg, offset, len, 0);
3624 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3629 * skb_cow_data - Check that a socket buffer's data buffers are writable
3630 * @skb: The socket buffer to check.
3631 * @tailbits: Amount of trailing space to be added
3632 * @trailer: Returned pointer to the skb where the @tailbits space begins
3634 * Make sure that the data buffers attached to a socket buffer are
3635 * writable. If they are not, private copies are made of the data buffers
3636 * and the socket buffer is set to use these instead.
3638 * If @tailbits is given, make sure that there is space to write @tailbits
3639 * bytes of data beyond current end of socket buffer. @trailer will be
3640 * set to point to the skb in which this space begins.
3642 * The number of scatterlist elements required to completely map the
3643 * COW'd and extended socket buffer will be returned.
3645 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3647 int copyflag;
3648 int elt;
3649 struct sk_buff *skb1, **skb_p;
3651 /* If skb is cloned or its head is paged, reallocate
3652 * head pulling out all the pages (pages are considered not writable
3653 * at the moment even if they are anonymous).
3655 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3656 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3657 return -ENOMEM;
3659 /* Easy case. Most of packets will go this way. */
3660 if (!skb_has_frag_list(skb)) {
3661 /* A little of trouble, not enough of space for trailer.
3662 * This should not happen, when stack is tuned to generate
3663 * good frames. OK, on miss we reallocate and reserve even more
3664 * space, 128 bytes is fair. */
3666 if (skb_tailroom(skb) < tailbits &&
3667 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3668 return -ENOMEM;
3670 /* Voila! */
3671 *trailer = skb;
3672 return 1;
3675 /* Misery. We are in troubles, going to mincer fragments... */
3677 elt = 1;
3678 skb_p = &skb_shinfo(skb)->frag_list;
3679 copyflag = 0;
3681 while ((skb1 = *skb_p) != NULL) {
3682 int ntail = 0;
3684 /* The fragment is partially pulled by someone,
3685 * this can happen on input. Copy it and everything
3686 * after it. */
3688 if (skb_shared(skb1))
3689 copyflag = 1;
3691 /* If the skb is the last, worry about trailer. */
3693 if (skb1->next == NULL && tailbits) {
3694 if (skb_shinfo(skb1)->nr_frags ||
3695 skb_has_frag_list(skb1) ||
3696 skb_tailroom(skb1) < tailbits)
3697 ntail = tailbits + 128;
3700 if (copyflag ||
3701 skb_cloned(skb1) ||
3702 ntail ||
3703 skb_shinfo(skb1)->nr_frags ||
3704 skb_has_frag_list(skb1)) {
3705 struct sk_buff *skb2;
3707 /* Fuck, we are miserable poor guys... */
3708 if (ntail == 0)
3709 skb2 = skb_copy(skb1, GFP_ATOMIC);
3710 else
3711 skb2 = skb_copy_expand(skb1,
3712 skb_headroom(skb1),
3713 ntail,
3714 GFP_ATOMIC);
3715 if (unlikely(skb2 == NULL))
3716 return -ENOMEM;
3718 if (skb1->sk)
3719 skb_set_owner_w(skb2, skb1->sk);
3721 /* Looking around. Are we still alive?
3722 * OK, link new skb, drop old one */
3724 skb2->next = skb1->next;
3725 *skb_p = skb2;
3726 kfree_skb(skb1);
3727 skb1 = skb2;
3729 elt++;
3730 *trailer = skb1;
3731 skb_p = &skb1->next;
3734 return elt;
3736 EXPORT_SYMBOL_GPL(skb_cow_data);
3738 static void sock_rmem_free(struct sk_buff *skb)
3740 struct sock *sk = skb->sk;
3742 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3746 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3748 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3750 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3751 (unsigned int)sk->sk_rcvbuf)
3752 return -ENOMEM;
3754 skb_orphan(skb);
3755 skb->sk = sk;
3756 skb->destructor = sock_rmem_free;
3757 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3759 /* before exiting rcu section, make sure dst is refcounted */
3760 skb_dst_force(skb);
3762 skb_queue_tail(&sk->sk_error_queue, skb);
3763 if (!sock_flag(sk, SOCK_DEAD))
3764 sk->sk_error_report(sk);
3765 return 0;
3767 EXPORT_SYMBOL(sock_queue_err_skb);
3769 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3771 struct sk_buff_head *q = &sk->sk_error_queue;
3772 struct sk_buff *skb, *skb_next;
3773 unsigned long flags;
3774 int err = 0;
3776 spin_lock_irqsave(&q->lock, flags);
3777 skb = __skb_dequeue(q);
3778 if (skb && (skb_next = skb_peek(q)))
3779 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3780 spin_unlock_irqrestore(&q->lock, flags);
3782 sk->sk_err = err;
3783 if (err)
3784 sk->sk_error_report(sk);
3786 return skb;
3788 EXPORT_SYMBOL(sock_dequeue_err_skb);
3791 * skb_clone_sk - create clone of skb, and take reference to socket
3792 * @skb: the skb to clone
3794 * This function creates a clone of a buffer that holds a reference on
3795 * sk_refcnt. Buffers created via this function are meant to be
3796 * returned using sock_queue_err_skb, or free via kfree_skb.
3798 * When passing buffers allocated with this function to sock_queue_err_skb
3799 * it is necessary to wrap the call with sock_hold/sock_put in order to
3800 * prevent the socket from being released prior to being enqueued on
3801 * the sk_error_queue.
3803 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3805 struct sock *sk = skb->sk;
3806 struct sk_buff *clone;
3808 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3809 return NULL;
3811 clone = skb_clone(skb, GFP_ATOMIC);
3812 if (!clone) {
3813 sock_put(sk);
3814 return NULL;
3817 clone->sk = sk;
3818 clone->destructor = sock_efree;
3820 return clone;
3822 EXPORT_SYMBOL(skb_clone_sk);
3824 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3825 struct sock *sk,
3826 int tstype)
3828 struct sock_exterr_skb *serr;
3829 int err;
3831 serr = SKB_EXT_ERR(skb);
3832 memset(serr, 0, sizeof(*serr));
3833 serr->ee.ee_errno = ENOMSG;
3834 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3835 serr->ee.ee_info = tstype;
3836 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
3837 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3838 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3839 if (sk->sk_protocol == IPPROTO_TCP &&
3840 sk->sk_type == SOCK_STREAM)
3841 serr->ee.ee_data -= sk->sk_tskey;
3844 err = sock_queue_err_skb(sk, skb);
3846 if (err)
3847 kfree_skb(skb);
3850 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3852 bool ret;
3854 if (likely(sysctl_tstamp_allow_data || tsonly))
3855 return true;
3857 read_lock_bh(&sk->sk_callback_lock);
3858 ret = sk->sk_socket && sk->sk_socket->file &&
3859 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3860 read_unlock_bh(&sk->sk_callback_lock);
3861 return ret;
3864 void skb_complete_tx_timestamp(struct sk_buff *skb,
3865 struct skb_shared_hwtstamps *hwtstamps)
3867 struct sock *sk = skb->sk;
3869 if (!skb_may_tx_timestamp(sk, false))
3870 goto err;
3872 /* Take a reference to prevent skb_orphan() from freeing the socket,
3873 * but only if the socket refcount is not zero.
3875 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3876 *skb_hwtstamps(skb) = *hwtstamps;
3877 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3878 sock_put(sk);
3879 return;
3882 err:
3883 kfree_skb(skb);
3885 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3887 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3888 struct skb_shared_hwtstamps *hwtstamps,
3889 struct sock *sk, int tstype)
3891 struct sk_buff *skb;
3892 bool tsonly;
3894 if (!sk)
3895 return;
3897 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3898 if (!skb_may_tx_timestamp(sk, tsonly))
3899 return;
3901 if (tsonly)
3902 skb = alloc_skb(0, GFP_ATOMIC);
3903 else
3904 skb = skb_clone(orig_skb, GFP_ATOMIC);
3905 if (!skb)
3906 return;
3908 if (tsonly) {
3909 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3910 SKBTX_ANY_TSTAMP;
3911 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3914 if (hwtstamps)
3915 *skb_hwtstamps(skb) = *hwtstamps;
3916 else
3917 skb->tstamp = ktime_get_real();
3919 __skb_complete_tx_timestamp(skb, sk, tstype);
3921 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3923 void skb_tstamp_tx(struct sk_buff *orig_skb,
3924 struct skb_shared_hwtstamps *hwtstamps)
3926 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3927 SCM_TSTAMP_SND);
3929 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3931 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3933 struct sock *sk = skb->sk;
3934 struct sock_exterr_skb *serr;
3935 int err = 1;
3937 skb->wifi_acked_valid = 1;
3938 skb->wifi_acked = acked;
3940 serr = SKB_EXT_ERR(skb);
3941 memset(serr, 0, sizeof(*serr));
3942 serr->ee.ee_errno = ENOMSG;
3943 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3945 /* Take a reference to prevent skb_orphan() from freeing the socket,
3946 * but only if the socket refcount is not zero.
3948 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3949 err = sock_queue_err_skb(sk, skb);
3950 sock_put(sk);
3952 if (err)
3953 kfree_skb(skb);
3955 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3958 * skb_partial_csum_set - set up and verify partial csum values for packet
3959 * @skb: the skb to set
3960 * @start: the number of bytes after skb->data to start checksumming.
3961 * @off: the offset from start to place the checksum.
3963 * For untrusted partially-checksummed packets, we need to make sure the values
3964 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3966 * This function checks and sets those values and skb->ip_summed: if this
3967 * returns false you should drop the packet.
3969 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3971 if (unlikely(start > skb_headlen(skb)) ||
3972 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3973 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3974 start, off, skb_headlen(skb));
3975 return false;
3977 skb->ip_summed = CHECKSUM_PARTIAL;
3978 skb->csum_start = skb_headroom(skb) + start;
3979 skb->csum_offset = off;
3980 skb_set_transport_header(skb, start);
3981 return true;
3983 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3985 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3986 unsigned int max)
3988 if (skb_headlen(skb) >= len)
3989 return 0;
3991 /* If we need to pullup then pullup to the max, so we
3992 * won't need to do it again.
3994 if (max > skb->len)
3995 max = skb->len;
3997 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3998 return -ENOMEM;
4000 if (skb_headlen(skb) < len)
4001 return -EPROTO;
4003 return 0;
4006 #define MAX_TCP_HDR_LEN (15 * 4)
4008 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4009 typeof(IPPROTO_IP) proto,
4010 unsigned int off)
4012 switch (proto) {
4013 int err;
4015 case IPPROTO_TCP:
4016 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4017 off + MAX_TCP_HDR_LEN);
4018 if (!err && !skb_partial_csum_set(skb, off,
4019 offsetof(struct tcphdr,
4020 check)))
4021 err = -EPROTO;
4022 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4024 case IPPROTO_UDP:
4025 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4026 off + sizeof(struct udphdr));
4027 if (!err && !skb_partial_csum_set(skb, off,
4028 offsetof(struct udphdr,
4029 check)))
4030 err = -EPROTO;
4031 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4034 return ERR_PTR(-EPROTO);
4037 /* This value should be large enough to cover a tagged ethernet header plus
4038 * maximally sized IP and TCP or UDP headers.
4040 #define MAX_IP_HDR_LEN 128
4042 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4044 unsigned int off;
4045 bool fragment;
4046 __sum16 *csum;
4047 int err;
4049 fragment = false;
4051 err = skb_maybe_pull_tail(skb,
4052 sizeof(struct iphdr),
4053 MAX_IP_HDR_LEN);
4054 if (err < 0)
4055 goto out;
4057 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4058 fragment = true;
4060 off = ip_hdrlen(skb);
4062 err = -EPROTO;
4064 if (fragment)
4065 goto out;
4067 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4068 if (IS_ERR(csum))
4069 return PTR_ERR(csum);
4071 if (recalculate)
4072 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4073 ip_hdr(skb)->daddr,
4074 skb->len - off,
4075 ip_hdr(skb)->protocol, 0);
4076 err = 0;
4078 out:
4079 return err;
4082 /* This value should be large enough to cover a tagged ethernet header plus
4083 * an IPv6 header, all options, and a maximal TCP or UDP header.
4085 #define MAX_IPV6_HDR_LEN 256
4087 #define OPT_HDR(type, skb, off) \
4088 (type *)(skb_network_header(skb) + (off))
4090 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4092 int err;
4093 u8 nexthdr;
4094 unsigned int off;
4095 unsigned int len;
4096 bool fragment;
4097 bool done;
4098 __sum16 *csum;
4100 fragment = false;
4101 done = false;
4103 off = sizeof(struct ipv6hdr);
4105 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4106 if (err < 0)
4107 goto out;
4109 nexthdr = ipv6_hdr(skb)->nexthdr;
4111 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4112 while (off <= len && !done) {
4113 switch (nexthdr) {
4114 case IPPROTO_DSTOPTS:
4115 case IPPROTO_HOPOPTS:
4116 case IPPROTO_ROUTING: {
4117 struct ipv6_opt_hdr *hp;
4119 err = skb_maybe_pull_tail(skb,
4120 off +
4121 sizeof(struct ipv6_opt_hdr),
4122 MAX_IPV6_HDR_LEN);
4123 if (err < 0)
4124 goto out;
4126 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4127 nexthdr = hp->nexthdr;
4128 off += ipv6_optlen(hp);
4129 break;
4131 case IPPROTO_AH: {
4132 struct ip_auth_hdr *hp;
4134 err = skb_maybe_pull_tail(skb,
4135 off +
4136 sizeof(struct ip_auth_hdr),
4137 MAX_IPV6_HDR_LEN);
4138 if (err < 0)
4139 goto out;
4141 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4142 nexthdr = hp->nexthdr;
4143 off += ipv6_authlen(hp);
4144 break;
4146 case IPPROTO_FRAGMENT: {
4147 struct frag_hdr *hp;
4149 err = skb_maybe_pull_tail(skb,
4150 off +
4151 sizeof(struct frag_hdr),
4152 MAX_IPV6_HDR_LEN);
4153 if (err < 0)
4154 goto out;
4156 hp = OPT_HDR(struct frag_hdr, skb, off);
4158 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4159 fragment = true;
4161 nexthdr = hp->nexthdr;
4162 off += sizeof(struct frag_hdr);
4163 break;
4165 default:
4166 done = true;
4167 break;
4171 err = -EPROTO;
4173 if (!done || fragment)
4174 goto out;
4176 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4177 if (IS_ERR(csum))
4178 return PTR_ERR(csum);
4180 if (recalculate)
4181 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4182 &ipv6_hdr(skb)->daddr,
4183 skb->len - off, nexthdr, 0);
4184 err = 0;
4186 out:
4187 return err;
4191 * skb_checksum_setup - set up partial checksum offset
4192 * @skb: the skb to set up
4193 * @recalculate: if true the pseudo-header checksum will be recalculated
4195 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4197 int err;
4199 switch (skb->protocol) {
4200 case htons(ETH_P_IP):
4201 err = skb_checksum_setup_ipv4(skb, recalculate);
4202 break;
4204 case htons(ETH_P_IPV6):
4205 err = skb_checksum_setup_ipv6(skb, recalculate);
4206 break;
4208 default:
4209 err = -EPROTO;
4210 break;
4213 return err;
4215 EXPORT_SYMBOL(skb_checksum_setup);
4218 * skb_checksum_maybe_trim - maybe trims the given skb
4219 * @skb: the skb to check
4220 * @transport_len: the data length beyond the network header
4222 * Checks whether the given skb has data beyond the given transport length.
4223 * If so, returns a cloned skb trimmed to this transport length.
4224 * Otherwise returns the provided skb. Returns NULL in error cases
4225 * (e.g. transport_len exceeds skb length or out-of-memory).
4227 * Caller needs to set the skb transport header and free any returned skb if it
4228 * differs from the provided skb.
4230 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4231 unsigned int transport_len)
4233 struct sk_buff *skb_chk;
4234 unsigned int len = skb_transport_offset(skb) + transport_len;
4235 int ret;
4237 if (skb->len < len)
4238 return NULL;
4239 else if (skb->len == len)
4240 return skb;
4242 skb_chk = skb_clone(skb, GFP_ATOMIC);
4243 if (!skb_chk)
4244 return NULL;
4246 ret = pskb_trim_rcsum(skb_chk, len);
4247 if (ret) {
4248 kfree_skb(skb_chk);
4249 return NULL;
4252 return skb_chk;
4256 * skb_checksum_trimmed - validate checksum of an skb
4257 * @skb: the skb to check
4258 * @transport_len: the data length beyond the network header
4259 * @skb_chkf: checksum function to use
4261 * Applies the given checksum function skb_chkf to the provided skb.
4262 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4264 * If the skb has data beyond the given transport length, then a
4265 * trimmed & cloned skb is checked and returned.
4267 * Caller needs to set the skb transport header and free any returned skb if it
4268 * differs from the provided skb.
4270 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4271 unsigned int transport_len,
4272 __sum16(*skb_chkf)(struct sk_buff *skb))
4274 struct sk_buff *skb_chk;
4275 unsigned int offset = skb_transport_offset(skb);
4276 __sum16 ret;
4278 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4279 if (!skb_chk)
4280 goto err;
4282 if (!pskb_may_pull(skb_chk, offset))
4283 goto err;
4285 skb_pull_rcsum(skb_chk, offset);
4286 ret = skb_chkf(skb_chk);
4287 skb_push_rcsum(skb_chk, offset);
4289 if (ret)
4290 goto err;
4292 return skb_chk;
4294 err:
4295 if (skb_chk && skb_chk != skb)
4296 kfree_skb(skb_chk);
4298 return NULL;
4301 EXPORT_SYMBOL(skb_checksum_trimmed);
4303 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4305 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4306 skb->dev->name);
4308 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4310 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4312 if (head_stolen) {
4313 skb_release_head_state(skb);
4314 kmem_cache_free(skbuff_head_cache, skb);
4315 } else {
4316 __kfree_skb(skb);
4319 EXPORT_SYMBOL(kfree_skb_partial);
4322 * skb_try_coalesce - try to merge skb to prior one
4323 * @to: prior buffer
4324 * @from: buffer to add
4325 * @fragstolen: pointer to boolean
4326 * @delta_truesize: how much more was allocated than was requested
4328 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4329 bool *fragstolen, int *delta_truesize)
4331 int i, delta, len = from->len;
4333 *fragstolen = false;
4335 if (skb_cloned(to))
4336 return false;
4338 if (len <= skb_tailroom(to)) {
4339 if (len)
4340 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4341 *delta_truesize = 0;
4342 return true;
4345 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4346 return false;
4348 if (skb_headlen(from) != 0) {
4349 struct page *page;
4350 unsigned int offset;
4352 if (skb_shinfo(to)->nr_frags +
4353 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4354 return false;
4356 if (skb_head_is_locked(from))
4357 return false;
4359 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4361 page = virt_to_head_page(from->head);
4362 offset = from->data - (unsigned char *)page_address(page);
4364 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4365 page, offset, skb_headlen(from));
4366 *fragstolen = true;
4367 } else {
4368 if (skb_shinfo(to)->nr_frags +
4369 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4370 return false;
4372 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4375 WARN_ON_ONCE(delta < len);
4377 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4378 skb_shinfo(from)->frags,
4379 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4380 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4382 if (!skb_cloned(from))
4383 skb_shinfo(from)->nr_frags = 0;
4385 /* if the skb is not cloned this does nothing
4386 * since we set nr_frags to 0.
4388 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4389 skb_frag_ref(from, i);
4391 to->truesize += delta;
4392 to->len += len;
4393 to->data_len += len;
4395 *delta_truesize = delta;
4396 return true;
4398 EXPORT_SYMBOL(skb_try_coalesce);
4401 * skb_scrub_packet - scrub an skb
4403 * @skb: buffer to clean
4404 * @xnet: packet is crossing netns
4406 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4407 * into/from a tunnel. Some information have to be cleared during these
4408 * operations.
4409 * skb_scrub_packet can also be used to clean a skb before injecting it in
4410 * another namespace (@xnet == true). We have to clear all information in the
4411 * skb that could impact namespace isolation.
4413 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4415 skb->tstamp.tv64 = 0;
4416 skb->pkt_type = PACKET_HOST;
4417 skb->skb_iif = 0;
4418 skb->ignore_df = 0;
4419 skb_dst_drop(skb);
4420 secpath_reset(skb);
4421 nf_reset(skb);
4422 nf_reset_trace(skb);
4424 if (!xnet)
4425 return;
4427 ipvs_reset(skb);
4428 skb_orphan(skb);
4429 skb->mark = 0;
4431 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4434 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4436 * @skb: GSO skb
4438 * skb_gso_transport_seglen is used to determine the real size of the
4439 * individual segments, including Layer4 headers (TCP/UDP).
4441 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4443 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4445 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4446 unsigned int thlen = 0;
4448 if (skb->encapsulation) {
4449 thlen = skb_inner_transport_header(skb) -
4450 skb_transport_header(skb);
4452 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4453 thlen += inner_tcp_hdrlen(skb);
4454 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4455 thlen = tcp_hdrlen(skb);
4456 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4457 thlen = sizeof(struct sctphdr);
4459 /* UFO sets gso_size to the size of the fragmentation
4460 * payload, i.e. the size of the L4 (UDP) header is already
4461 * accounted for.
4463 return thlen + shinfo->gso_size;
4465 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4468 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4470 * @skb: GSO skb
4471 * @mtu: MTU to validate against
4473 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4474 * once split.
4476 bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4478 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4479 const struct sk_buff *iter;
4480 unsigned int hlen;
4482 hlen = skb_gso_network_seglen(skb);
4484 if (shinfo->gso_size != GSO_BY_FRAGS)
4485 return hlen <= mtu;
4487 /* Undo this so we can re-use header sizes */
4488 hlen -= GSO_BY_FRAGS;
4490 skb_walk_frags(skb, iter) {
4491 if (hlen + skb_headlen(iter) > mtu)
4492 return false;
4495 return true;
4497 EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4499 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4501 int mac_len;
4503 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4504 kfree_skb(skb);
4505 return NULL;
4508 mac_len = skb->data - skb_mac_header(skb);
4509 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
4510 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
4511 mac_len - VLAN_HLEN - ETH_TLEN);
4513 skb->mac_header += VLAN_HLEN;
4514 return skb;
4517 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4519 struct vlan_hdr *vhdr;
4520 u16 vlan_tci;
4522 if (unlikely(skb_vlan_tag_present(skb))) {
4523 /* vlan_tci is already set-up so leave this for another time */
4524 return skb;
4527 skb = skb_share_check(skb, GFP_ATOMIC);
4528 if (unlikely(!skb))
4529 goto err_free;
4531 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4532 goto err_free;
4534 vhdr = (struct vlan_hdr *)skb->data;
4535 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4536 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4538 skb_pull_rcsum(skb, VLAN_HLEN);
4539 vlan_set_encap_proto(skb, vhdr);
4541 skb = skb_reorder_vlan_header(skb);
4542 if (unlikely(!skb))
4543 goto err_free;
4545 skb_reset_network_header(skb);
4546 skb_reset_transport_header(skb);
4547 skb_reset_mac_len(skb);
4549 return skb;
4551 err_free:
4552 kfree_skb(skb);
4553 return NULL;
4555 EXPORT_SYMBOL(skb_vlan_untag);
4557 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4559 if (!pskb_may_pull(skb, write_len))
4560 return -ENOMEM;
4562 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4563 return 0;
4565 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4567 EXPORT_SYMBOL(skb_ensure_writable);
4569 /* remove VLAN header from packet and update csum accordingly.
4570 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4572 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4574 struct vlan_hdr *vhdr;
4575 int offset = skb->data - skb_mac_header(skb);
4576 int err;
4578 if (WARN_ONCE(offset,
4579 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4580 offset)) {
4581 return -EINVAL;
4584 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4585 if (unlikely(err))
4586 return err;
4588 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4590 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4591 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4593 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4594 __skb_pull(skb, VLAN_HLEN);
4596 vlan_set_encap_proto(skb, vhdr);
4597 skb->mac_header += VLAN_HLEN;
4599 if (skb_network_offset(skb) < ETH_HLEN)
4600 skb_set_network_header(skb, ETH_HLEN);
4602 skb_reset_mac_len(skb);
4604 return err;
4606 EXPORT_SYMBOL(__skb_vlan_pop);
4608 /* Pop a vlan tag either from hwaccel or from payload.
4609 * Expects skb->data at mac header.
4611 int skb_vlan_pop(struct sk_buff *skb)
4613 u16 vlan_tci;
4614 __be16 vlan_proto;
4615 int err;
4617 if (likely(skb_vlan_tag_present(skb))) {
4618 skb->vlan_tci = 0;
4619 } else {
4620 if (unlikely(!eth_type_vlan(skb->protocol)))
4621 return 0;
4623 err = __skb_vlan_pop(skb, &vlan_tci);
4624 if (err)
4625 return err;
4627 /* move next vlan tag to hw accel tag */
4628 if (likely(!eth_type_vlan(skb->protocol)))
4629 return 0;
4631 vlan_proto = skb->protocol;
4632 err = __skb_vlan_pop(skb, &vlan_tci);
4633 if (unlikely(err))
4634 return err;
4636 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4637 return 0;
4639 EXPORT_SYMBOL(skb_vlan_pop);
4641 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4642 * Expects skb->data at mac header.
4644 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4646 if (skb_vlan_tag_present(skb)) {
4647 int offset = skb->data - skb_mac_header(skb);
4648 int err;
4650 if (WARN_ONCE(offset,
4651 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4652 offset)) {
4653 return -EINVAL;
4656 err = __vlan_insert_tag(skb, skb->vlan_proto,
4657 skb_vlan_tag_get(skb));
4658 if (err)
4659 return err;
4661 skb->protocol = skb->vlan_proto;
4662 skb->mac_len += VLAN_HLEN;
4664 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4666 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4667 return 0;
4669 EXPORT_SYMBOL(skb_vlan_push);
4672 * alloc_skb_with_frags - allocate skb with page frags
4674 * @header_len: size of linear part
4675 * @data_len: needed length in frags
4676 * @max_page_order: max page order desired.
4677 * @errcode: pointer to error code if any
4678 * @gfp_mask: allocation mask
4680 * This can be used to allocate a paged skb, given a maximal order for frags.
4682 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4683 unsigned long data_len,
4684 int max_page_order,
4685 int *errcode,
4686 gfp_t gfp_mask)
4688 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4689 unsigned long chunk;
4690 struct sk_buff *skb;
4691 struct page *page;
4692 gfp_t gfp_head;
4693 int i;
4695 *errcode = -EMSGSIZE;
4696 /* Note this test could be relaxed, if we succeed to allocate
4697 * high order pages...
4699 if (npages > MAX_SKB_FRAGS)
4700 return NULL;
4702 gfp_head = gfp_mask;
4703 if (gfp_head & __GFP_DIRECT_RECLAIM)
4704 gfp_head |= __GFP_REPEAT;
4706 *errcode = -ENOBUFS;
4707 skb = alloc_skb(header_len, gfp_head);
4708 if (!skb)
4709 return NULL;
4711 skb->truesize += npages << PAGE_SHIFT;
4713 for (i = 0; npages > 0; i++) {
4714 int order = max_page_order;
4716 while (order) {
4717 if (npages >= 1 << order) {
4718 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4719 __GFP_COMP |
4720 __GFP_NOWARN |
4721 __GFP_NORETRY,
4722 order);
4723 if (page)
4724 goto fill_page;
4725 /* Do not retry other high order allocations */
4726 order = 1;
4727 max_page_order = 0;
4729 order--;
4731 page = alloc_page(gfp_mask);
4732 if (!page)
4733 goto failure;
4734 fill_page:
4735 chunk = min_t(unsigned long, data_len,
4736 PAGE_SIZE << order);
4737 skb_fill_page_desc(skb, i, page, 0, chunk);
4738 data_len -= chunk;
4739 npages -= 1 << order;
4741 return skb;
4743 failure:
4744 kfree_skb(skb);
4745 return NULL;
4747 EXPORT_SYMBOL(alloc_skb_with_frags);
4749 /* carve out the first off bytes from skb when off < headlen */
4750 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4751 const int headlen, gfp_t gfp_mask)
4753 int i;
4754 int size = skb_end_offset(skb);
4755 int new_hlen = headlen - off;
4756 u8 *data;
4758 size = SKB_DATA_ALIGN(size);
4760 if (skb_pfmemalloc(skb))
4761 gfp_mask |= __GFP_MEMALLOC;
4762 data = kmalloc_reserve(size +
4763 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4764 gfp_mask, NUMA_NO_NODE, NULL);
4765 if (!data)
4766 return -ENOMEM;
4768 size = SKB_WITH_OVERHEAD(ksize(data));
4770 /* Copy real data, and all frags */
4771 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4772 skb->len -= off;
4774 memcpy((struct skb_shared_info *)(data + size),
4775 skb_shinfo(skb),
4776 offsetof(struct skb_shared_info,
4777 frags[skb_shinfo(skb)->nr_frags]));
4778 if (skb_cloned(skb)) {
4779 /* drop the old head gracefully */
4780 if (skb_orphan_frags(skb, gfp_mask)) {
4781 kfree(data);
4782 return -ENOMEM;
4784 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4785 skb_frag_ref(skb, i);
4786 if (skb_has_frag_list(skb))
4787 skb_clone_fraglist(skb);
4788 skb_release_data(skb);
4789 } else {
4790 /* we can reuse existing recount- all we did was
4791 * relocate values
4793 skb_free_head(skb);
4796 skb->head = data;
4797 skb->data = data;
4798 skb->head_frag = 0;
4799 #ifdef NET_SKBUFF_DATA_USES_OFFSET
4800 skb->end = size;
4801 #else
4802 skb->end = skb->head + size;
4803 #endif
4804 skb_set_tail_pointer(skb, skb_headlen(skb));
4805 skb_headers_offset_update(skb, 0);
4806 skb->cloned = 0;
4807 skb->hdr_len = 0;
4808 skb->nohdr = 0;
4809 atomic_set(&skb_shinfo(skb)->dataref, 1);
4811 return 0;
4814 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4816 /* carve out the first eat bytes from skb's frag_list. May recurse into
4817 * pskb_carve()
4819 static int pskb_carve_frag_list(struct sk_buff *skb,
4820 struct skb_shared_info *shinfo, int eat,
4821 gfp_t gfp_mask)
4823 struct sk_buff *list = shinfo->frag_list;
4824 struct sk_buff *clone = NULL;
4825 struct sk_buff *insp = NULL;
4827 do {
4828 if (!list) {
4829 pr_err("Not enough bytes to eat. Want %d\n", eat);
4830 return -EFAULT;
4832 if (list->len <= eat) {
4833 /* Eaten as whole. */
4834 eat -= list->len;
4835 list = list->next;
4836 insp = list;
4837 } else {
4838 /* Eaten partially. */
4839 if (skb_shared(list)) {
4840 clone = skb_clone(list, gfp_mask);
4841 if (!clone)
4842 return -ENOMEM;
4843 insp = list->next;
4844 list = clone;
4845 } else {
4846 /* This may be pulled without problems. */
4847 insp = list;
4849 if (pskb_carve(list, eat, gfp_mask) < 0) {
4850 kfree_skb(clone);
4851 return -ENOMEM;
4853 break;
4855 } while (eat);
4857 /* Free pulled out fragments. */
4858 while ((list = shinfo->frag_list) != insp) {
4859 shinfo->frag_list = list->next;
4860 kfree_skb(list);
4862 /* And insert new clone at head. */
4863 if (clone) {
4864 clone->next = list;
4865 shinfo->frag_list = clone;
4867 return 0;
4870 /* carve off first len bytes from skb. Split line (off) is in the
4871 * non-linear part of skb
4873 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4874 int pos, gfp_t gfp_mask)
4876 int i, k = 0;
4877 int size = skb_end_offset(skb);
4878 u8 *data;
4879 const int nfrags = skb_shinfo(skb)->nr_frags;
4880 struct skb_shared_info *shinfo;
4882 size = SKB_DATA_ALIGN(size);
4884 if (skb_pfmemalloc(skb))
4885 gfp_mask |= __GFP_MEMALLOC;
4886 data = kmalloc_reserve(size +
4887 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4888 gfp_mask, NUMA_NO_NODE, NULL);
4889 if (!data)
4890 return -ENOMEM;
4892 size = SKB_WITH_OVERHEAD(ksize(data));
4894 memcpy((struct skb_shared_info *)(data + size),
4895 skb_shinfo(skb), offsetof(struct skb_shared_info,
4896 frags[skb_shinfo(skb)->nr_frags]));
4897 if (skb_orphan_frags(skb, gfp_mask)) {
4898 kfree(data);
4899 return -ENOMEM;
4901 shinfo = (struct skb_shared_info *)(data + size);
4902 for (i = 0; i < nfrags; i++) {
4903 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4905 if (pos + fsize > off) {
4906 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4908 if (pos < off) {
4909 /* Split frag.
4910 * We have two variants in this case:
4911 * 1. Move all the frag to the second
4912 * part, if it is possible. F.e.
4913 * this approach is mandatory for TUX,
4914 * where splitting is expensive.
4915 * 2. Split is accurately. We make this.
4917 shinfo->frags[0].page_offset += off - pos;
4918 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4920 skb_frag_ref(skb, i);
4921 k++;
4923 pos += fsize;
4925 shinfo->nr_frags = k;
4926 if (skb_has_frag_list(skb))
4927 skb_clone_fraglist(skb);
4929 if (k == 0) {
4930 /* split line is in frag list */
4931 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4933 skb_release_data(skb);
4935 skb->head = data;
4936 skb->head_frag = 0;
4937 skb->data = data;
4938 #ifdef NET_SKBUFF_DATA_USES_OFFSET
4939 skb->end = size;
4940 #else
4941 skb->end = skb->head + size;
4942 #endif
4943 skb_reset_tail_pointer(skb);
4944 skb_headers_offset_update(skb, 0);
4945 skb->cloned = 0;
4946 skb->hdr_len = 0;
4947 skb->nohdr = 0;
4948 skb->len -= off;
4949 skb->data_len = skb->len;
4950 atomic_set(&skb_shinfo(skb)->dataref, 1);
4951 return 0;
4954 /* remove len bytes from the beginning of the skb */
4955 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4957 int headlen = skb_headlen(skb);
4959 if (len < headlen)
4960 return pskb_carve_inside_header(skb, len, headlen, gfp);
4961 else
4962 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4965 /* Extract to_copy bytes starting at off from skb, and return this in
4966 * a new skb
4968 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
4969 int to_copy, gfp_t gfp)
4971 struct sk_buff *clone = skb_clone(skb, gfp);
4973 if (!clone)
4974 return NULL;
4976 if (pskb_carve(clone, off, gfp) < 0 ||
4977 pskb_trim(clone, to_copy)) {
4978 kfree_skb(clone);
4979 return NULL;
4981 return clone;
4983 EXPORT_SYMBOL(pskb_extract);