ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / core / skbuff.c
blob9703924ed0713f61a12d5a497be872efad4df894
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
55 #endif
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
68 #include <net/dst.h>
69 #include <net/sock.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
72 #include <net/xfrm.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83 EXPORT_SYMBOL(sysctl_max_skb_frags);
85 /**
86 * skb_panic - private function for out-of-line support
87 * @skb: buffer
88 * @sz: size
89 * @addr: address
90 * @msg: skb_over_panic or skb_under_panic
92 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
98 const char msg[])
100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 msg, addr, skb->len, sz, skb->head, skb->data,
102 (unsigned long)skb->tail, (unsigned long)skb->end,
103 skb->dev ? skb->dev->name : "<NULL>");
104 BUG();
107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109 skb_panic(skb, sz, addr, __func__);
112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114 skb_panic(skb, sz, addr, __func__);
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
122 * memory is free
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 unsigned long ip, bool *pfmemalloc)
130 void *obj;
131 bool ret_pfmemalloc = false;
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
137 obj = kmalloc_node_track_caller(size,
138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
139 node);
140 if (obj || !(gfp_pfmemalloc_allowed(flags)))
141 goto out;
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc = true;
145 obj = kmalloc_node_track_caller(size, flags, node);
147 out:
148 if (pfmemalloc)
149 *pfmemalloc = ret_pfmemalloc;
151 return obj;
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
156 * [BEEP] leaks.
160 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
162 struct sk_buff *skb;
164 /* Get the HEAD */
165 skb = kmem_cache_alloc_node(skbuff_head_cache,
166 gfp_mask & ~__GFP_DMA, node);
167 if (!skb)
168 goto out;
171 * Only clear those fields we need to clear, not those that we will
172 * actually initialise below. Hence, don't put any more fields after
173 * the tail pointer in struct sk_buff!
175 memset(skb, 0, offsetof(struct sk_buff, tail));
176 skb->head = NULL;
177 skb->truesize = sizeof(struct sk_buff);
178 atomic_set(&skb->users, 1);
180 skb->mac_header = (typeof(skb->mac_header))~0U;
181 out:
182 return skb;
186 * __alloc_skb - allocate a network buffer
187 * @size: size to allocate
188 * @gfp_mask: allocation mask
189 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
190 * instead of head cache and allocate a cloned (child) skb.
191 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
192 * allocations in case the data is required for writeback
193 * @node: numa node to allocate memory on
195 * Allocate a new &sk_buff. The returned buffer has no headroom and a
196 * tail room of at least size bytes. The object has a reference count
197 * of one. The return is the buffer. On a failure the return is %NULL.
199 * Buffers may only be allocated from interrupts using a @gfp_mask of
200 * %GFP_ATOMIC.
202 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
203 int flags, int node)
205 struct kmem_cache *cache;
206 struct skb_shared_info *shinfo;
207 struct sk_buff *skb;
208 u8 *data;
209 bool pfmemalloc;
211 cache = (flags & SKB_ALLOC_FCLONE)
212 ? skbuff_fclone_cache : skbuff_head_cache;
214 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
215 gfp_mask |= __GFP_MEMALLOC;
217 /* Get the HEAD */
218 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
219 if (!skb)
220 goto out;
221 prefetchw(skb);
223 /* We do our best to align skb_shared_info on a separate cache
224 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
225 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
226 * Both skb->head and skb_shared_info are cache line aligned.
228 size = SKB_DATA_ALIGN(size);
229 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
230 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
231 if (!data)
232 goto nodata;
233 /* kmalloc(size) might give us more room than requested.
234 * Put skb_shared_info exactly at the end of allocated zone,
235 * to allow max possible filling before reallocation.
237 size = SKB_WITH_OVERHEAD(ksize(data));
238 prefetchw(data + size);
241 * Only clear those fields we need to clear, not those that we will
242 * actually initialise below. Hence, don't put any more fields after
243 * the tail pointer in struct sk_buff!
245 memset(skb, 0, offsetof(struct sk_buff, tail));
246 /* Account for allocated memory : skb + skb->head */
247 skb->truesize = SKB_TRUESIZE(size);
248 skb->pfmemalloc = pfmemalloc;
249 atomic_set(&skb->users, 1);
250 skb->head = data;
251 skb->data = data;
252 skb_reset_tail_pointer(skb);
253 skb->end = skb->tail + size;
254 skb->mac_header = (typeof(skb->mac_header))~0U;
255 skb->transport_header = (typeof(skb->transport_header))~0U;
257 /* make sure we initialize shinfo sequentially */
258 shinfo = skb_shinfo(skb);
259 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
260 atomic_set(&shinfo->dataref, 1);
261 kmemcheck_annotate_variable(shinfo->destructor_arg);
263 if (flags & SKB_ALLOC_FCLONE) {
264 struct sk_buff_fclones *fclones;
266 fclones = container_of(skb, struct sk_buff_fclones, skb1);
268 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
269 skb->fclone = SKB_FCLONE_ORIG;
270 atomic_set(&fclones->fclone_ref, 1);
272 fclones->skb2.fclone = SKB_FCLONE_CLONE;
273 fclones->skb2.pfmemalloc = pfmemalloc;
275 out:
276 return skb;
277 nodata:
278 kmem_cache_free(cache, skb);
279 skb = NULL;
280 goto out;
282 EXPORT_SYMBOL(__alloc_skb);
285 * __build_skb - build a network buffer
286 * @data: data buffer provided by caller
287 * @frag_size: size of data, or 0 if head was kmalloced
289 * Allocate a new &sk_buff. Caller provides space holding head and
290 * skb_shared_info. @data must have been allocated by kmalloc() only if
291 * @frag_size is 0, otherwise data should come from the page allocator
292 * or vmalloc()
293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
295 * Notes :
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
303 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
305 struct skb_shared_info *shinfo;
306 struct sk_buff *skb;
307 unsigned int size = frag_size ? : ksize(data);
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310 if (!skb)
311 return NULL;
313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
318 skb->head = data;
319 skb->data = data;
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
331 return skb;
334 /* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
339 struct sk_buff *build_skb(void *data, unsigned int frag_size)
341 struct sk_buff *skb = __build_skb(data, frag_size);
343 if (skb && frag_size) {
344 skb->head_frag = 1;
345 if (page_is_pfmemalloc(virt_to_head_page(data)))
346 skb->pfmemalloc = 1;
348 return skb;
350 EXPORT_SYMBOL(build_skb);
352 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
353 static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
355 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
357 struct page_frag_cache *nc;
358 unsigned long flags;
359 void *data;
361 local_irq_save(flags);
362 nc = this_cpu_ptr(&netdev_alloc_cache);
363 data = __alloc_page_frag(nc, fragsz, gfp_mask);
364 local_irq_restore(flags);
365 return data;
369 * netdev_alloc_frag - allocate a page fragment
370 * @fragsz: fragment size
372 * Allocates a frag from a page for receive buffer.
373 * Uses GFP_ATOMIC allocations.
375 void *netdev_alloc_frag(unsigned int fragsz)
377 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
379 EXPORT_SYMBOL(netdev_alloc_frag);
381 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
383 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
385 return __alloc_page_frag(nc, fragsz, gfp_mask);
388 void *napi_alloc_frag(unsigned int fragsz)
390 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
392 EXPORT_SYMBOL(napi_alloc_frag);
395 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
396 * @dev: network device to receive on
397 * @len: length to allocate
398 * @gfp_mask: get_free_pages mask, passed to alloc_skb
400 * Allocate a new &sk_buff and assign it a usage count of one. The
401 * buffer has NET_SKB_PAD headroom built in. Users should allocate
402 * the headroom they think they need without accounting for the
403 * built in space. The built in space is used for optimisations.
405 * %NULL is returned if there is no free memory.
407 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
408 gfp_t gfp_mask)
410 struct page_frag_cache *nc;
411 unsigned long flags;
412 struct sk_buff *skb;
413 bool pfmemalloc;
414 void *data;
416 len += NET_SKB_PAD;
418 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
419 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
420 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
421 if (!skb)
422 goto skb_fail;
423 goto skb_success;
426 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
427 len = SKB_DATA_ALIGN(len);
429 if (sk_memalloc_socks())
430 gfp_mask |= __GFP_MEMALLOC;
432 local_irq_save(flags);
434 nc = this_cpu_ptr(&netdev_alloc_cache);
435 data = __alloc_page_frag(nc, len, gfp_mask);
436 pfmemalloc = nc->pfmemalloc;
438 local_irq_restore(flags);
440 if (unlikely(!data))
441 return NULL;
443 skb = __build_skb(data, len);
444 if (unlikely(!skb)) {
445 skb_free_frag(data);
446 return NULL;
449 /* use OR instead of assignment to avoid clearing of bits in mask */
450 if (pfmemalloc)
451 skb->pfmemalloc = 1;
452 skb->head_frag = 1;
454 skb_success:
455 skb_reserve(skb, NET_SKB_PAD);
456 skb->dev = dev;
458 skb_fail:
459 return skb;
461 EXPORT_SYMBOL(__netdev_alloc_skb);
464 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
465 * @napi: napi instance this buffer was allocated for
466 * @len: length to allocate
467 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
469 * Allocate a new sk_buff for use in NAPI receive. This buffer will
470 * attempt to allocate the head from a special reserved region used
471 * only for NAPI Rx allocation. By doing this we can save several
472 * CPU cycles by avoiding having to disable and re-enable IRQs.
474 * %NULL is returned if there is no free memory.
476 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
477 gfp_t gfp_mask)
479 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
480 struct sk_buff *skb;
481 void *data;
483 len += NET_SKB_PAD + NET_IP_ALIGN;
485 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
486 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
487 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
488 if (!skb)
489 goto skb_fail;
490 goto skb_success;
493 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
494 len = SKB_DATA_ALIGN(len);
496 if (sk_memalloc_socks())
497 gfp_mask |= __GFP_MEMALLOC;
499 data = __alloc_page_frag(nc, len, gfp_mask);
500 if (unlikely(!data))
501 return NULL;
503 skb = __build_skb(data, len);
504 if (unlikely(!skb)) {
505 skb_free_frag(data);
506 return NULL;
509 /* use OR instead of assignment to avoid clearing of bits in mask */
510 if (nc->pfmemalloc)
511 skb->pfmemalloc = 1;
512 skb->head_frag = 1;
514 skb_success:
515 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
516 skb->dev = napi->dev;
518 skb_fail:
519 return skb;
521 EXPORT_SYMBOL(__napi_alloc_skb);
523 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
524 int size, unsigned int truesize)
526 skb_fill_page_desc(skb, i, page, off, size);
527 skb->len += size;
528 skb->data_len += size;
529 skb->truesize += truesize;
531 EXPORT_SYMBOL(skb_add_rx_frag);
533 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
534 unsigned int truesize)
536 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
538 skb_frag_size_add(frag, size);
539 skb->len += size;
540 skb->data_len += size;
541 skb->truesize += truesize;
543 EXPORT_SYMBOL(skb_coalesce_rx_frag);
545 static void skb_drop_list(struct sk_buff **listp)
547 kfree_skb_list(*listp);
548 *listp = NULL;
551 static inline void skb_drop_fraglist(struct sk_buff *skb)
553 skb_drop_list(&skb_shinfo(skb)->frag_list);
556 static void skb_clone_fraglist(struct sk_buff *skb)
558 struct sk_buff *list;
560 skb_walk_frags(skb, list)
561 skb_get(list);
564 static void skb_free_head(struct sk_buff *skb)
566 unsigned char *head = skb->head;
568 if (skb->head_frag)
569 skb_free_frag(head);
570 else
571 kfree(head);
574 static void skb_release_data(struct sk_buff *skb)
576 struct skb_shared_info *shinfo = skb_shinfo(skb);
577 int i;
579 if (skb->cloned &&
580 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
581 &shinfo->dataref))
582 return;
584 for (i = 0; i < shinfo->nr_frags; i++)
585 __skb_frag_unref(&shinfo->frags[i]);
588 * If skb buf is from userspace, we need to notify the caller
589 * the lower device DMA has done;
591 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
592 struct ubuf_info *uarg;
594 uarg = shinfo->destructor_arg;
595 if (uarg->callback)
596 uarg->callback(uarg, true);
599 if (shinfo->frag_list)
600 kfree_skb_list(shinfo->frag_list);
602 skb_free_head(skb);
606 * Free an skbuff by memory without cleaning the state.
608 static void kfree_skbmem(struct sk_buff *skb)
610 struct sk_buff_fclones *fclones;
612 switch (skb->fclone) {
613 case SKB_FCLONE_UNAVAILABLE:
614 kmem_cache_free(skbuff_head_cache, skb);
615 return;
617 case SKB_FCLONE_ORIG:
618 fclones = container_of(skb, struct sk_buff_fclones, skb1);
620 /* We usually free the clone (TX completion) before original skb
621 * This test would have no chance to be true for the clone,
622 * while here, branch prediction will be good.
624 if (atomic_read(&fclones->fclone_ref) == 1)
625 goto fastpath;
626 break;
628 default: /* SKB_FCLONE_CLONE */
629 fclones = container_of(skb, struct sk_buff_fclones, skb2);
630 break;
632 if (!atomic_dec_and_test(&fclones->fclone_ref))
633 return;
634 fastpath:
635 kmem_cache_free(skbuff_fclone_cache, fclones);
638 static void skb_release_head_state(struct sk_buff *skb)
640 skb_dst_drop(skb);
641 #ifdef CONFIG_XFRM
642 secpath_put(skb->sp);
643 #endif
644 if (skb->destructor) {
645 WARN_ON(in_irq());
646 skb->destructor(skb);
648 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
649 nf_conntrack_put(skb->nfct);
650 #endif
651 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
652 nf_bridge_put(skb->nf_bridge);
653 #endif
656 /* Free everything but the sk_buff shell. */
657 static void skb_release_all(struct sk_buff *skb)
659 skb_release_head_state(skb);
660 if (likely(skb->head))
661 skb_release_data(skb);
665 * __kfree_skb - private function
666 * @skb: buffer
668 * Free an sk_buff. Release anything attached to the buffer.
669 * Clean the state. This is an internal helper function. Users should
670 * always call kfree_skb
673 void __kfree_skb(struct sk_buff *skb)
675 skb_release_all(skb);
676 kfree_skbmem(skb);
678 EXPORT_SYMBOL(__kfree_skb);
681 * kfree_skb - free an sk_buff
682 * @skb: buffer to free
684 * Drop a reference to the buffer and free it if the usage count has
685 * hit zero.
687 void kfree_skb(struct sk_buff *skb)
689 if (unlikely(!skb))
690 return;
691 if (likely(atomic_read(&skb->users) == 1))
692 smp_rmb();
693 else if (likely(!atomic_dec_and_test(&skb->users)))
694 return;
695 trace_kfree_skb(skb, __builtin_return_address(0));
696 __kfree_skb(skb);
698 EXPORT_SYMBOL(kfree_skb);
700 void kfree_skb_list(struct sk_buff *segs)
702 while (segs) {
703 struct sk_buff *next = segs->next;
705 kfree_skb(segs);
706 segs = next;
709 EXPORT_SYMBOL(kfree_skb_list);
712 * skb_tx_error - report an sk_buff xmit error
713 * @skb: buffer that triggered an error
715 * Report xmit error if a device callback is tracking this skb.
716 * skb must be freed afterwards.
718 void skb_tx_error(struct sk_buff *skb)
720 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
721 struct ubuf_info *uarg;
723 uarg = skb_shinfo(skb)->destructor_arg;
724 if (uarg->callback)
725 uarg->callback(uarg, false);
726 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
729 EXPORT_SYMBOL(skb_tx_error);
732 * consume_skb - free an skbuff
733 * @skb: buffer to free
735 * Drop a ref to the buffer and free it if the usage count has hit zero
736 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
737 * is being dropped after a failure and notes that
739 void consume_skb(struct sk_buff *skb)
741 if (unlikely(!skb))
742 return;
743 if (likely(atomic_read(&skb->users) == 1))
744 smp_rmb();
745 else if (likely(!atomic_dec_and_test(&skb->users)))
746 return;
747 trace_consume_skb(skb);
748 __kfree_skb(skb);
750 EXPORT_SYMBOL(consume_skb);
752 /* Make sure a field is enclosed inside headers_start/headers_end section */
753 #define CHECK_SKB_FIELD(field) \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
755 offsetof(struct sk_buff, headers_start)); \
756 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
757 offsetof(struct sk_buff, headers_end)); \
759 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
761 new->tstamp = old->tstamp;
762 /* We do not copy old->sk */
763 new->dev = old->dev;
764 memcpy(new->cb, old->cb, sizeof(old->cb));
765 skb_dst_copy(new, old);
766 #ifdef CONFIG_XFRM
767 new->sp = secpath_get(old->sp);
768 #endif
769 __nf_copy(new, old, false);
771 /* Note : this field could be in headers_start/headers_end section
772 * It is not yet because we do not want to have a 16 bit hole
774 new->queue_mapping = old->queue_mapping;
776 memcpy(&new->headers_start, &old->headers_start,
777 offsetof(struct sk_buff, headers_end) -
778 offsetof(struct sk_buff, headers_start));
779 CHECK_SKB_FIELD(protocol);
780 CHECK_SKB_FIELD(csum);
781 CHECK_SKB_FIELD(hash);
782 CHECK_SKB_FIELD(priority);
783 CHECK_SKB_FIELD(skb_iif);
784 CHECK_SKB_FIELD(vlan_proto);
785 CHECK_SKB_FIELD(vlan_tci);
786 CHECK_SKB_FIELD(transport_header);
787 CHECK_SKB_FIELD(network_header);
788 CHECK_SKB_FIELD(mac_header);
789 CHECK_SKB_FIELD(inner_protocol);
790 CHECK_SKB_FIELD(inner_transport_header);
791 CHECK_SKB_FIELD(inner_network_header);
792 CHECK_SKB_FIELD(inner_mac_header);
793 CHECK_SKB_FIELD(mark);
794 #ifdef CONFIG_NETWORK_SECMARK
795 CHECK_SKB_FIELD(secmark);
796 #endif
797 #ifdef CONFIG_NET_RX_BUSY_POLL
798 CHECK_SKB_FIELD(napi_id);
799 #endif
800 #ifdef CONFIG_XPS
801 CHECK_SKB_FIELD(sender_cpu);
802 #endif
803 #ifdef CONFIG_NET_SCHED
804 CHECK_SKB_FIELD(tc_index);
805 #ifdef CONFIG_NET_CLS_ACT
806 CHECK_SKB_FIELD(tc_verd);
807 #endif
808 #endif
813 * You should not add any new code to this function. Add it to
814 * __copy_skb_header above instead.
816 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
818 #define C(x) n->x = skb->x
820 n->next = n->prev = NULL;
821 n->sk = NULL;
822 __copy_skb_header(n, skb);
824 C(len);
825 C(data_len);
826 C(mac_len);
827 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
828 n->cloned = 1;
829 n->nohdr = 0;
830 n->peeked = 0;
831 C(pfmemalloc);
832 n->destructor = NULL;
833 C(tail);
834 C(end);
835 C(head);
836 C(head_frag);
837 C(data);
838 C(truesize);
839 atomic_set(&n->users, 1);
841 atomic_inc(&(skb_shinfo(skb)->dataref));
842 skb->cloned = 1;
844 return n;
845 #undef C
849 * skb_morph - morph one skb into another
850 * @dst: the skb to receive the contents
851 * @src: the skb to supply the contents
853 * This is identical to skb_clone except that the target skb is
854 * supplied by the user.
856 * The target skb is returned upon exit.
858 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
860 skb_release_all(dst);
861 return __skb_clone(dst, src);
863 EXPORT_SYMBOL_GPL(skb_morph);
866 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
867 * @skb: the skb to modify
868 * @gfp_mask: allocation priority
870 * This must be called on SKBTX_DEV_ZEROCOPY skb.
871 * It will copy all frags into kernel and drop the reference
872 * to userspace pages.
874 * If this function is called from an interrupt gfp_mask() must be
875 * %GFP_ATOMIC.
877 * Returns 0 on success or a negative error code on failure
878 * to allocate kernel memory to copy to.
880 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
882 int i;
883 int num_frags = skb_shinfo(skb)->nr_frags;
884 struct page *page, *head = NULL;
885 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
887 for (i = 0; i < num_frags; i++) {
888 u8 *vaddr;
889 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
891 page = alloc_page(gfp_mask);
892 if (!page) {
893 while (head) {
894 struct page *next = (struct page *)page_private(head);
895 put_page(head);
896 head = next;
898 return -ENOMEM;
900 vaddr = kmap_atomic(skb_frag_page(f));
901 memcpy(page_address(page),
902 vaddr + f->page_offset, skb_frag_size(f));
903 kunmap_atomic(vaddr);
904 set_page_private(page, (unsigned long)head);
905 head = page;
908 /* skb frags release userspace buffers */
909 for (i = 0; i < num_frags; i++)
910 skb_frag_unref(skb, i);
912 uarg->callback(uarg, false);
914 /* skb frags point to kernel buffers */
915 for (i = num_frags - 1; i >= 0; i--) {
916 __skb_fill_page_desc(skb, i, head, 0,
917 skb_shinfo(skb)->frags[i].size);
918 head = (struct page *)page_private(head);
921 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
922 return 0;
924 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
927 * skb_clone - duplicate an sk_buff
928 * @skb: buffer to clone
929 * @gfp_mask: allocation priority
931 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
932 * copies share the same packet data but not structure. The new
933 * buffer has a reference count of 1. If the allocation fails the
934 * function returns %NULL otherwise the new buffer is returned.
936 * If this function is called from an interrupt gfp_mask() must be
937 * %GFP_ATOMIC.
940 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
942 struct sk_buff_fclones *fclones = container_of(skb,
943 struct sk_buff_fclones,
944 skb1);
945 struct sk_buff *n;
947 if (skb_orphan_frags(skb, gfp_mask))
948 return NULL;
950 if (skb->fclone == SKB_FCLONE_ORIG &&
951 atomic_read(&fclones->fclone_ref) == 1) {
952 n = &fclones->skb2;
953 atomic_set(&fclones->fclone_ref, 2);
954 } else {
955 if (skb_pfmemalloc(skb))
956 gfp_mask |= __GFP_MEMALLOC;
958 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
959 if (!n)
960 return NULL;
962 kmemcheck_annotate_bitfield(n, flags1);
963 n->fclone = SKB_FCLONE_UNAVAILABLE;
966 return __skb_clone(n, skb);
968 EXPORT_SYMBOL(skb_clone);
970 static void skb_headers_offset_update(struct sk_buff *skb, int off)
972 /* Only adjust this if it actually is csum_start rather than csum */
973 if (skb->ip_summed == CHECKSUM_PARTIAL)
974 skb->csum_start += off;
975 /* {transport,network,mac}_header and tail are relative to skb->head */
976 skb->transport_header += off;
977 skb->network_header += off;
978 if (skb_mac_header_was_set(skb))
979 skb->mac_header += off;
980 skb->inner_transport_header += off;
981 skb->inner_network_header += off;
982 skb->inner_mac_header += off;
985 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
987 __copy_skb_header(new, old);
989 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
990 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
991 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
994 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
996 if (skb_pfmemalloc(skb))
997 return SKB_ALLOC_RX;
998 return 0;
1002 * skb_copy - create private copy of an sk_buff
1003 * @skb: buffer to copy
1004 * @gfp_mask: allocation priority
1006 * Make a copy of both an &sk_buff and its data. This is used when the
1007 * caller wishes to modify the data and needs a private copy of the
1008 * data to alter. Returns %NULL on failure or the pointer to the buffer
1009 * on success. The returned buffer has a reference count of 1.
1011 * As by-product this function converts non-linear &sk_buff to linear
1012 * one, so that &sk_buff becomes completely private and caller is allowed
1013 * to modify all the data of returned buffer. This means that this
1014 * function is not recommended for use in circumstances when only
1015 * header is going to be modified. Use pskb_copy() instead.
1018 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1020 int headerlen = skb_headroom(skb);
1021 unsigned int size = skb_end_offset(skb) + skb->data_len;
1022 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1023 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1025 if (!n)
1026 return NULL;
1028 /* Set the data pointer */
1029 skb_reserve(n, headerlen);
1030 /* Set the tail pointer and length */
1031 skb_put(n, skb->len);
1033 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1034 BUG();
1036 copy_skb_header(n, skb);
1037 return n;
1039 EXPORT_SYMBOL(skb_copy);
1042 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1043 * @skb: buffer to copy
1044 * @headroom: headroom of new skb
1045 * @gfp_mask: allocation priority
1046 * @fclone: if true allocate the copy of the skb from the fclone
1047 * cache instead of the head cache; it is recommended to set this
1048 * to true for the cases where the copy will likely be cloned
1050 * Make a copy of both an &sk_buff and part of its data, located
1051 * in header. Fragmented data remain shared. This is used when
1052 * the caller wishes to modify only header of &sk_buff and needs
1053 * private copy of the header to alter. Returns %NULL on failure
1054 * or the pointer to the buffer on success.
1055 * The returned buffer has a reference count of 1.
1058 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1059 gfp_t gfp_mask, bool fclone)
1061 unsigned int size = skb_headlen(skb) + headroom;
1062 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1063 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1065 if (!n)
1066 goto out;
1068 /* Set the data pointer */
1069 skb_reserve(n, headroom);
1070 /* Set the tail pointer and length */
1071 skb_put(n, skb_headlen(skb));
1072 /* Copy the bytes */
1073 skb_copy_from_linear_data(skb, n->data, n->len);
1075 n->truesize += skb->data_len;
1076 n->data_len = skb->data_len;
1077 n->len = skb->len;
1079 if (skb_shinfo(skb)->nr_frags) {
1080 int i;
1082 if (skb_orphan_frags(skb, gfp_mask)) {
1083 kfree_skb(n);
1084 n = NULL;
1085 goto out;
1087 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1088 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1089 skb_frag_ref(skb, i);
1091 skb_shinfo(n)->nr_frags = i;
1094 if (skb_has_frag_list(skb)) {
1095 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1096 skb_clone_fraglist(n);
1099 copy_skb_header(n, skb);
1100 out:
1101 return n;
1103 EXPORT_SYMBOL(__pskb_copy_fclone);
1106 * pskb_expand_head - reallocate header of &sk_buff
1107 * @skb: buffer to reallocate
1108 * @nhead: room to add at head
1109 * @ntail: room to add at tail
1110 * @gfp_mask: allocation priority
1112 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1113 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1114 * reference count of 1. Returns zero in the case of success or error,
1115 * if expansion failed. In the last case, &sk_buff is not changed.
1117 * All the pointers pointing into skb header may change and must be
1118 * reloaded after call to this function.
1121 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1122 gfp_t gfp_mask)
1124 int i;
1125 u8 *data;
1126 int size = nhead + skb_end_offset(skb) + ntail;
1127 long off;
1129 BUG_ON(nhead < 0);
1131 if (skb_shared(skb))
1132 BUG();
1134 size = SKB_DATA_ALIGN(size);
1136 if (skb_pfmemalloc(skb))
1137 gfp_mask |= __GFP_MEMALLOC;
1138 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1139 gfp_mask, NUMA_NO_NODE, NULL);
1140 if (!data)
1141 goto nodata;
1142 size = SKB_WITH_OVERHEAD(ksize(data));
1144 /* Copy only real data... and, alas, header. This should be
1145 * optimized for the cases when header is void.
1147 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1149 memcpy((struct skb_shared_info *)(data + size),
1150 skb_shinfo(skb),
1151 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1154 * if shinfo is shared we must drop the old head gracefully, but if it
1155 * is not we can just drop the old head and let the existing refcount
1156 * be since all we did is relocate the values
1158 if (skb_cloned(skb)) {
1159 /* copy this zero copy skb frags */
1160 if (skb_orphan_frags(skb, gfp_mask))
1161 goto nofrags;
1162 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1163 skb_frag_ref(skb, i);
1165 if (skb_has_frag_list(skb))
1166 skb_clone_fraglist(skb);
1168 skb_release_data(skb);
1169 } else {
1170 skb_free_head(skb);
1172 off = (data + nhead) - skb->head;
1174 skb->head = data;
1175 skb->head_frag = 0;
1176 skb->data += off;
1177 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1178 skb->end = size;
1179 off = nhead;
1180 #else
1181 skb->end = skb->head + size;
1182 #endif
1183 skb->tail += off;
1184 skb_headers_offset_update(skb, nhead);
1185 skb->cloned = 0;
1186 skb->hdr_len = 0;
1187 skb->nohdr = 0;
1188 atomic_set(&skb_shinfo(skb)->dataref, 1);
1189 return 0;
1191 nofrags:
1192 kfree(data);
1193 nodata:
1194 return -ENOMEM;
1196 EXPORT_SYMBOL(pskb_expand_head);
1198 /* Make private copy of skb with writable head and some headroom */
1200 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1202 struct sk_buff *skb2;
1203 int delta = headroom - skb_headroom(skb);
1205 if (delta <= 0)
1206 skb2 = pskb_copy(skb, GFP_ATOMIC);
1207 else {
1208 skb2 = skb_clone(skb, GFP_ATOMIC);
1209 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1210 GFP_ATOMIC)) {
1211 kfree_skb(skb2);
1212 skb2 = NULL;
1215 return skb2;
1217 EXPORT_SYMBOL(skb_realloc_headroom);
1220 * skb_copy_expand - copy and expand sk_buff
1221 * @skb: buffer to copy
1222 * @newheadroom: new free bytes at head
1223 * @newtailroom: new free bytes at tail
1224 * @gfp_mask: allocation priority
1226 * Make a copy of both an &sk_buff and its data and while doing so
1227 * allocate additional space.
1229 * This is used when the caller wishes to modify the data and needs a
1230 * private copy of the data to alter as well as more space for new fields.
1231 * Returns %NULL on failure or the pointer to the buffer
1232 * on success. The returned buffer has a reference count of 1.
1234 * You must pass %GFP_ATOMIC as the allocation priority if this function
1235 * is called from an interrupt.
1237 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1238 int newheadroom, int newtailroom,
1239 gfp_t gfp_mask)
1242 * Allocate the copy buffer
1244 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1245 gfp_mask, skb_alloc_rx_flag(skb),
1246 NUMA_NO_NODE);
1247 int oldheadroom = skb_headroom(skb);
1248 int head_copy_len, head_copy_off;
1250 if (!n)
1251 return NULL;
1253 skb_reserve(n, newheadroom);
1255 /* Set the tail pointer and length */
1256 skb_put(n, skb->len);
1258 head_copy_len = oldheadroom;
1259 head_copy_off = 0;
1260 if (newheadroom <= head_copy_len)
1261 head_copy_len = newheadroom;
1262 else
1263 head_copy_off = newheadroom - head_copy_len;
1265 /* Copy the linear header and data. */
1266 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1267 skb->len + head_copy_len))
1268 BUG();
1270 copy_skb_header(n, skb);
1272 skb_headers_offset_update(n, newheadroom - oldheadroom);
1274 return n;
1276 EXPORT_SYMBOL(skb_copy_expand);
1279 * skb_pad - zero pad the tail of an skb
1280 * @skb: buffer to pad
1281 * @pad: space to pad
1283 * Ensure that a buffer is followed by a padding area that is zero
1284 * filled. Used by network drivers which may DMA or transfer data
1285 * beyond the buffer end onto the wire.
1287 * May return error in out of memory cases. The skb is freed on error.
1290 int skb_pad(struct sk_buff *skb, int pad)
1292 int err;
1293 int ntail;
1295 /* If the skbuff is non linear tailroom is always zero.. */
1296 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1297 memset(skb->data+skb->len, 0, pad);
1298 return 0;
1301 ntail = skb->data_len + pad - (skb->end - skb->tail);
1302 if (likely(skb_cloned(skb) || ntail > 0)) {
1303 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1304 if (unlikely(err))
1305 goto free_skb;
1308 /* FIXME: The use of this function with non-linear skb's really needs
1309 * to be audited.
1311 err = skb_linearize(skb);
1312 if (unlikely(err))
1313 goto free_skb;
1315 memset(skb->data + skb->len, 0, pad);
1316 return 0;
1318 free_skb:
1319 kfree_skb(skb);
1320 return err;
1322 EXPORT_SYMBOL(skb_pad);
1325 * pskb_put - add data to the tail of a potentially fragmented buffer
1326 * @skb: start of the buffer to use
1327 * @tail: tail fragment of the buffer to use
1328 * @len: amount of data to add
1330 * This function extends the used data area of the potentially
1331 * fragmented buffer. @tail must be the last fragment of @skb -- or
1332 * @skb itself. If this would exceed the total buffer size the kernel
1333 * will panic. A pointer to the first byte of the extra data is
1334 * returned.
1337 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1339 if (tail != skb) {
1340 skb->data_len += len;
1341 skb->len += len;
1343 return skb_put(tail, len);
1345 EXPORT_SYMBOL_GPL(pskb_put);
1348 * skb_put - add data to a buffer
1349 * @skb: buffer to use
1350 * @len: amount of data to add
1352 * This function extends the used data area of the buffer. If this would
1353 * exceed the total buffer size the kernel will panic. A pointer to the
1354 * first byte of the extra data is returned.
1356 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1358 unsigned char *tmp = skb_tail_pointer(skb);
1359 SKB_LINEAR_ASSERT(skb);
1360 skb->tail += len;
1361 skb->len += len;
1362 if (unlikely(skb->tail > skb->end))
1363 skb_over_panic(skb, len, __builtin_return_address(0));
1364 return tmp;
1366 EXPORT_SYMBOL(skb_put);
1369 * skb_push - add data to the start of a buffer
1370 * @skb: buffer to use
1371 * @len: amount of data to add
1373 * This function extends the used data area of the buffer at the buffer
1374 * start. If this would exceed the total buffer headroom the kernel will
1375 * panic. A pointer to the first byte of the extra data is returned.
1377 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1379 skb->data -= len;
1380 skb->len += len;
1381 if (unlikely(skb->data<skb->head))
1382 skb_under_panic(skb, len, __builtin_return_address(0));
1383 return skb->data;
1385 EXPORT_SYMBOL(skb_push);
1388 * skb_pull - remove data from the start of a buffer
1389 * @skb: buffer to use
1390 * @len: amount of data to remove
1392 * This function removes data from the start of a buffer, returning
1393 * the memory to the headroom. A pointer to the next data in the buffer
1394 * is returned. Once the data has been pulled future pushes will overwrite
1395 * the old data.
1397 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1399 return skb_pull_inline(skb, len);
1401 EXPORT_SYMBOL(skb_pull);
1404 * skb_trim - remove end from a buffer
1405 * @skb: buffer to alter
1406 * @len: new length
1408 * Cut the length of a buffer down by removing data from the tail. If
1409 * the buffer is already under the length specified it is not modified.
1410 * The skb must be linear.
1412 void skb_trim(struct sk_buff *skb, unsigned int len)
1414 if (skb->len > len)
1415 __skb_trim(skb, len);
1417 EXPORT_SYMBOL(skb_trim);
1419 /* Trims skb to length len. It can change skb pointers.
1422 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1424 struct sk_buff **fragp;
1425 struct sk_buff *frag;
1426 int offset = skb_headlen(skb);
1427 int nfrags = skb_shinfo(skb)->nr_frags;
1428 int i;
1429 int err;
1431 if (skb_cloned(skb) &&
1432 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1433 return err;
1435 i = 0;
1436 if (offset >= len)
1437 goto drop_pages;
1439 for (; i < nfrags; i++) {
1440 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1442 if (end < len) {
1443 offset = end;
1444 continue;
1447 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1449 drop_pages:
1450 skb_shinfo(skb)->nr_frags = i;
1452 for (; i < nfrags; i++)
1453 skb_frag_unref(skb, i);
1455 if (skb_has_frag_list(skb))
1456 skb_drop_fraglist(skb);
1457 goto done;
1460 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1461 fragp = &frag->next) {
1462 int end = offset + frag->len;
1464 if (skb_shared(frag)) {
1465 struct sk_buff *nfrag;
1467 nfrag = skb_clone(frag, GFP_ATOMIC);
1468 if (unlikely(!nfrag))
1469 return -ENOMEM;
1471 nfrag->next = frag->next;
1472 consume_skb(frag);
1473 frag = nfrag;
1474 *fragp = frag;
1477 if (end < len) {
1478 offset = end;
1479 continue;
1482 if (end > len &&
1483 unlikely((err = pskb_trim(frag, len - offset))))
1484 return err;
1486 if (frag->next)
1487 skb_drop_list(&frag->next);
1488 break;
1491 done:
1492 if (len > skb_headlen(skb)) {
1493 skb->data_len -= skb->len - len;
1494 skb->len = len;
1495 } else {
1496 skb->len = len;
1497 skb->data_len = 0;
1498 skb_set_tail_pointer(skb, len);
1501 return 0;
1503 EXPORT_SYMBOL(___pskb_trim);
1506 * __pskb_pull_tail - advance tail of skb header
1507 * @skb: buffer to reallocate
1508 * @delta: number of bytes to advance tail
1510 * The function makes a sense only on a fragmented &sk_buff,
1511 * it expands header moving its tail forward and copying necessary
1512 * data from fragmented part.
1514 * &sk_buff MUST have reference count of 1.
1516 * Returns %NULL (and &sk_buff does not change) if pull failed
1517 * or value of new tail of skb in the case of success.
1519 * All the pointers pointing into skb header may change and must be
1520 * reloaded after call to this function.
1523 /* Moves tail of skb head forward, copying data from fragmented part,
1524 * when it is necessary.
1525 * 1. It may fail due to malloc failure.
1526 * 2. It may change skb pointers.
1528 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1530 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1532 /* If skb has not enough free space at tail, get new one
1533 * plus 128 bytes for future expansions. If we have enough
1534 * room at tail, reallocate without expansion only if skb is cloned.
1536 int i, k, eat = (skb->tail + delta) - skb->end;
1538 if (eat > 0 || skb_cloned(skb)) {
1539 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1540 GFP_ATOMIC))
1541 return NULL;
1544 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1545 BUG();
1547 /* Optimization: no fragments, no reasons to preestimate
1548 * size of pulled pages. Superb.
1550 if (!skb_has_frag_list(skb))
1551 goto pull_pages;
1553 /* Estimate size of pulled pages. */
1554 eat = delta;
1555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1556 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1558 if (size >= eat)
1559 goto pull_pages;
1560 eat -= size;
1563 /* If we need update frag list, we are in troubles.
1564 * Certainly, it possible to add an offset to skb data,
1565 * but taking into account that pulling is expected to
1566 * be very rare operation, it is worth to fight against
1567 * further bloating skb head and crucify ourselves here instead.
1568 * Pure masohism, indeed. 8)8)
1570 if (eat) {
1571 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1572 struct sk_buff *clone = NULL;
1573 struct sk_buff *insp = NULL;
1575 do {
1576 BUG_ON(!list);
1578 if (list->len <= eat) {
1579 /* Eaten as whole. */
1580 eat -= list->len;
1581 list = list->next;
1582 insp = list;
1583 } else {
1584 /* Eaten partially. */
1586 if (skb_shared(list)) {
1587 /* Sucks! We need to fork list. :-( */
1588 clone = skb_clone(list, GFP_ATOMIC);
1589 if (!clone)
1590 return NULL;
1591 insp = list->next;
1592 list = clone;
1593 } else {
1594 /* This may be pulled without
1595 * problems. */
1596 insp = list;
1598 if (!pskb_pull(list, eat)) {
1599 kfree_skb(clone);
1600 return NULL;
1602 break;
1604 } while (eat);
1606 /* Free pulled out fragments. */
1607 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1608 skb_shinfo(skb)->frag_list = list->next;
1609 kfree_skb(list);
1611 /* And insert new clone at head. */
1612 if (clone) {
1613 clone->next = list;
1614 skb_shinfo(skb)->frag_list = clone;
1617 /* Success! Now we may commit changes to skb data. */
1619 pull_pages:
1620 eat = delta;
1621 k = 0;
1622 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1623 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1625 if (size <= eat) {
1626 skb_frag_unref(skb, i);
1627 eat -= size;
1628 } else {
1629 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1630 if (eat) {
1631 skb_shinfo(skb)->frags[k].page_offset += eat;
1632 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1633 eat = 0;
1635 k++;
1638 skb_shinfo(skb)->nr_frags = k;
1640 skb->tail += delta;
1641 skb->data_len -= delta;
1643 return skb_tail_pointer(skb);
1645 EXPORT_SYMBOL(__pskb_pull_tail);
1648 * skb_copy_bits - copy bits from skb to kernel buffer
1649 * @skb: source skb
1650 * @offset: offset in source
1651 * @to: destination buffer
1652 * @len: number of bytes to copy
1654 * Copy the specified number of bytes from the source skb to the
1655 * destination buffer.
1657 * CAUTION ! :
1658 * If its prototype is ever changed,
1659 * check arch/{*}/net/{*}.S files,
1660 * since it is called from BPF assembly code.
1662 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1664 int start = skb_headlen(skb);
1665 struct sk_buff *frag_iter;
1666 int i, copy;
1668 if (offset > (int)skb->len - len)
1669 goto fault;
1671 /* Copy header. */
1672 if ((copy = start - offset) > 0) {
1673 if (copy > len)
1674 copy = len;
1675 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1676 if ((len -= copy) == 0)
1677 return 0;
1678 offset += copy;
1679 to += copy;
1682 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1683 int end;
1684 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1686 WARN_ON(start > offset + len);
1688 end = start + skb_frag_size(f);
1689 if ((copy = end - offset) > 0) {
1690 u8 *vaddr;
1692 if (copy > len)
1693 copy = len;
1695 vaddr = kmap_atomic(skb_frag_page(f));
1696 memcpy(to,
1697 vaddr + f->page_offset + offset - start,
1698 copy);
1699 kunmap_atomic(vaddr);
1701 if ((len -= copy) == 0)
1702 return 0;
1703 offset += copy;
1704 to += copy;
1706 start = end;
1709 skb_walk_frags(skb, frag_iter) {
1710 int end;
1712 WARN_ON(start > offset + len);
1714 end = start + frag_iter->len;
1715 if ((copy = end - offset) > 0) {
1716 if (copy > len)
1717 copy = len;
1718 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1719 goto fault;
1720 if ((len -= copy) == 0)
1721 return 0;
1722 offset += copy;
1723 to += copy;
1725 start = end;
1728 if (!len)
1729 return 0;
1731 fault:
1732 return -EFAULT;
1734 EXPORT_SYMBOL(skb_copy_bits);
1737 * Callback from splice_to_pipe(), if we need to release some pages
1738 * at the end of the spd in case we error'ed out in filling the pipe.
1740 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1742 put_page(spd->pages[i]);
1745 static struct page *linear_to_page(struct page *page, unsigned int *len,
1746 unsigned int *offset,
1747 struct sock *sk)
1749 struct page_frag *pfrag = sk_page_frag(sk);
1751 if (!sk_page_frag_refill(sk, pfrag))
1752 return NULL;
1754 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1756 memcpy(page_address(pfrag->page) + pfrag->offset,
1757 page_address(page) + *offset, *len);
1758 *offset = pfrag->offset;
1759 pfrag->offset += *len;
1761 return pfrag->page;
1764 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1765 struct page *page,
1766 unsigned int offset)
1768 return spd->nr_pages &&
1769 spd->pages[spd->nr_pages - 1] == page &&
1770 (spd->partial[spd->nr_pages - 1].offset +
1771 spd->partial[spd->nr_pages - 1].len == offset);
1775 * Fill page/offset/length into spd, if it can hold more pages.
1777 static bool spd_fill_page(struct splice_pipe_desc *spd,
1778 struct pipe_inode_info *pipe, struct page *page,
1779 unsigned int *len, unsigned int offset,
1780 bool linear,
1781 struct sock *sk)
1783 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1784 return true;
1786 if (linear) {
1787 page = linear_to_page(page, len, &offset, sk);
1788 if (!page)
1789 return true;
1791 if (spd_can_coalesce(spd, page, offset)) {
1792 spd->partial[spd->nr_pages - 1].len += *len;
1793 return false;
1795 get_page(page);
1796 spd->pages[spd->nr_pages] = page;
1797 spd->partial[spd->nr_pages].len = *len;
1798 spd->partial[spd->nr_pages].offset = offset;
1799 spd->nr_pages++;
1801 return false;
1804 static bool __splice_segment(struct page *page, unsigned int poff,
1805 unsigned int plen, unsigned int *off,
1806 unsigned int *len,
1807 struct splice_pipe_desc *spd, bool linear,
1808 struct sock *sk,
1809 struct pipe_inode_info *pipe)
1811 if (!*len)
1812 return true;
1814 /* skip this segment if already processed */
1815 if (*off >= plen) {
1816 *off -= plen;
1817 return false;
1820 /* ignore any bits we already processed */
1821 poff += *off;
1822 plen -= *off;
1823 *off = 0;
1825 do {
1826 unsigned int flen = min(*len, plen);
1828 if (spd_fill_page(spd, pipe, page, &flen, poff,
1829 linear, sk))
1830 return true;
1831 poff += flen;
1832 plen -= flen;
1833 *len -= flen;
1834 } while (*len && plen);
1836 return false;
1840 * Map linear and fragment data from the skb to spd. It reports true if the
1841 * pipe is full or if we already spliced the requested length.
1843 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1844 unsigned int *offset, unsigned int *len,
1845 struct splice_pipe_desc *spd, struct sock *sk)
1847 int seg;
1849 /* map the linear part :
1850 * If skb->head_frag is set, this 'linear' part is backed by a
1851 * fragment, and if the head is not shared with any clones then
1852 * we can avoid a copy since we own the head portion of this page.
1854 if (__splice_segment(virt_to_page(skb->data),
1855 (unsigned long) skb->data & (PAGE_SIZE - 1),
1856 skb_headlen(skb),
1857 offset, len, spd,
1858 skb_head_is_locked(skb),
1859 sk, pipe))
1860 return true;
1863 * then map the fragments
1865 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1866 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1868 if (__splice_segment(skb_frag_page(f),
1869 f->page_offset, skb_frag_size(f),
1870 offset, len, spd, false, sk, pipe))
1871 return true;
1874 return false;
1877 ssize_t skb_socket_splice(struct sock *sk,
1878 struct pipe_inode_info *pipe,
1879 struct splice_pipe_desc *spd)
1881 int ret;
1883 /* Drop the socket lock, otherwise we have reverse
1884 * locking dependencies between sk_lock and i_mutex
1885 * here as compared to sendfile(). We enter here
1886 * with the socket lock held, and splice_to_pipe() will
1887 * grab the pipe inode lock. For sendfile() emulation,
1888 * we call into ->sendpage() with the i_mutex lock held
1889 * and networking will grab the socket lock.
1891 release_sock(sk);
1892 ret = splice_to_pipe(pipe, spd);
1893 lock_sock(sk);
1895 return ret;
1899 * Map data from the skb to a pipe. Should handle both the linear part,
1900 * the fragments, and the frag list. It does NOT handle frag lists within
1901 * the frag list, if such a thing exists. We'd probably need to recurse to
1902 * handle that cleanly.
1904 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1905 struct pipe_inode_info *pipe, unsigned int tlen,
1906 unsigned int flags,
1907 ssize_t (*splice_cb)(struct sock *,
1908 struct pipe_inode_info *,
1909 struct splice_pipe_desc *))
1911 struct partial_page partial[MAX_SKB_FRAGS];
1912 struct page *pages[MAX_SKB_FRAGS];
1913 struct splice_pipe_desc spd = {
1914 .pages = pages,
1915 .partial = partial,
1916 .nr_pages_max = MAX_SKB_FRAGS,
1917 .flags = flags,
1918 .ops = &nosteal_pipe_buf_ops,
1919 .spd_release = sock_spd_release,
1921 struct sk_buff *frag_iter;
1922 int ret = 0;
1925 * __skb_splice_bits() only fails if the output has no room left,
1926 * so no point in going over the frag_list for the error case.
1928 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1929 goto done;
1930 else if (!tlen)
1931 goto done;
1934 * now see if we have a frag_list to map
1936 skb_walk_frags(skb, frag_iter) {
1937 if (!tlen)
1938 break;
1939 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1940 break;
1943 done:
1944 if (spd.nr_pages)
1945 ret = splice_cb(sk, pipe, &spd);
1947 return ret;
1949 EXPORT_SYMBOL_GPL(skb_splice_bits);
1952 * skb_store_bits - store bits from kernel buffer to skb
1953 * @skb: destination buffer
1954 * @offset: offset in destination
1955 * @from: source buffer
1956 * @len: number of bytes to copy
1958 * Copy the specified number of bytes from the source buffer to the
1959 * destination skb. This function handles all the messy bits of
1960 * traversing fragment lists and such.
1963 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1965 int start = skb_headlen(skb);
1966 struct sk_buff *frag_iter;
1967 int i, copy;
1969 if (offset > (int)skb->len - len)
1970 goto fault;
1972 if ((copy = start - offset) > 0) {
1973 if (copy > len)
1974 copy = len;
1975 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1976 if ((len -= copy) == 0)
1977 return 0;
1978 offset += copy;
1979 from += copy;
1982 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1983 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1984 int end;
1986 WARN_ON(start > offset + len);
1988 end = start + skb_frag_size(frag);
1989 if ((copy = end - offset) > 0) {
1990 u8 *vaddr;
1992 if (copy > len)
1993 copy = len;
1995 vaddr = kmap_atomic(skb_frag_page(frag));
1996 memcpy(vaddr + frag->page_offset + offset - start,
1997 from, copy);
1998 kunmap_atomic(vaddr);
2000 if ((len -= copy) == 0)
2001 return 0;
2002 offset += copy;
2003 from += copy;
2005 start = end;
2008 skb_walk_frags(skb, frag_iter) {
2009 int end;
2011 WARN_ON(start > offset + len);
2013 end = start + frag_iter->len;
2014 if ((copy = end - offset) > 0) {
2015 if (copy > len)
2016 copy = len;
2017 if (skb_store_bits(frag_iter, offset - start,
2018 from, copy))
2019 goto fault;
2020 if ((len -= copy) == 0)
2021 return 0;
2022 offset += copy;
2023 from += copy;
2025 start = end;
2027 if (!len)
2028 return 0;
2030 fault:
2031 return -EFAULT;
2033 EXPORT_SYMBOL(skb_store_bits);
2035 /* Checksum skb data. */
2036 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2037 __wsum csum, const struct skb_checksum_ops *ops)
2039 int start = skb_headlen(skb);
2040 int i, copy = start - offset;
2041 struct sk_buff *frag_iter;
2042 int pos = 0;
2044 /* Checksum header. */
2045 if (copy > 0) {
2046 if (copy > len)
2047 copy = len;
2048 csum = ops->update(skb->data + offset, copy, csum);
2049 if ((len -= copy) == 0)
2050 return csum;
2051 offset += copy;
2052 pos = copy;
2055 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2056 int end;
2057 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2059 WARN_ON(start > offset + len);
2061 end = start + skb_frag_size(frag);
2062 if ((copy = end - offset) > 0) {
2063 __wsum csum2;
2064 u8 *vaddr;
2066 if (copy > len)
2067 copy = len;
2068 vaddr = kmap_atomic(skb_frag_page(frag));
2069 csum2 = ops->update(vaddr + frag->page_offset +
2070 offset - start, copy, 0);
2071 kunmap_atomic(vaddr);
2072 csum = ops->combine(csum, csum2, pos, copy);
2073 if (!(len -= copy))
2074 return csum;
2075 offset += copy;
2076 pos += copy;
2078 start = end;
2081 skb_walk_frags(skb, frag_iter) {
2082 int end;
2084 WARN_ON(start > offset + len);
2086 end = start + frag_iter->len;
2087 if ((copy = end - offset) > 0) {
2088 __wsum csum2;
2089 if (copy > len)
2090 copy = len;
2091 csum2 = __skb_checksum(frag_iter, offset - start,
2092 copy, 0, ops);
2093 csum = ops->combine(csum, csum2, pos, copy);
2094 if ((len -= copy) == 0)
2095 return csum;
2096 offset += copy;
2097 pos += copy;
2099 start = end;
2101 BUG_ON(len);
2103 return csum;
2105 EXPORT_SYMBOL(__skb_checksum);
2107 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2108 int len, __wsum csum)
2110 const struct skb_checksum_ops ops = {
2111 .update = csum_partial_ext,
2112 .combine = csum_block_add_ext,
2115 return __skb_checksum(skb, offset, len, csum, &ops);
2117 EXPORT_SYMBOL(skb_checksum);
2119 /* Both of above in one bottle. */
2121 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2122 u8 *to, int len, __wsum csum)
2124 int start = skb_headlen(skb);
2125 int i, copy = start - offset;
2126 struct sk_buff *frag_iter;
2127 int pos = 0;
2129 /* Copy header. */
2130 if (copy > 0) {
2131 if (copy > len)
2132 copy = len;
2133 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2134 copy, csum);
2135 if ((len -= copy) == 0)
2136 return csum;
2137 offset += copy;
2138 to += copy;
2139 pos = copy;
2142 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2143 int end;
2145 WARN_ON(start > offset + len);
2147 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2148 if ((copy = end - offset) > 0) {
2149 __wsum csum2;
2150 u8 *vaddr;
2151 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2153 if (copy > len)
2154 copy = len;
2155 vaddr = kmap_atomic(skb_frag_page(frag));
2156 csum2 = csum_partial_copy_nocheck(vaddr +
2157 frag->page_offset +
2158 offset - start, to,
2159 copy, 0);
2160 kunmap_atomic(vaddr);
2161 csum = csum_block_add(csum, csum2, pos);
2162 if (!(len -= copy))
2163 return csum;
2164 offset += copy;
2165 to += copy;
2166 pos += copy;
2168 start = end;
2171 skb_walk_frags(skb, frag_iter) {
2172 __wsum csum2;
2173 int end;
2175 WARN_ON(start > offset + len);
2177 end = start + frag_iter->len;
2178 if ((copy = end - offset) > 0) {
2179 if (copy > len)
2180 copy = len;
2181 csum2 = skb_copy_and_csum_bits(frag_iter,
2182 offset - start,
2183 to, copy, 0);
2184 csum = csum_block_add(csum, csum2, pos);
2185 if ((len -= copy) == 0)
2186 return csum;
2187 offset += copy;
2188 to += copy;
2189 pos += copy;
2191 start = end;
2193 BUG_ON(len);
2194 return csum;
2196 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2199 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2200 * @from: source buffer
2202 * Calculates the amount of linear headroom needed in the 'to' skb passed
2203 * into skb_zerocopy().
2205 unsigned int
2206 skb_zerocopy_headlen(const struct sk_buff *from)
2208 unsigned int hlen = 0;
2210 if (!from->head_frag ||
2211 skb_headlen(from) < L1_CACHE_BYTES ||
2212 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2213 hlen = skb_headlen(from);
2215 if (skb_has_frag_list(from))
2216 hlen = from->len;
2218 return hlen;
2220 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2223 * skb_zerocopy - Zero copy skb to skb
2224 * @to: destination buffer
2225 * @from: source buffer
2226 * @len: number of bytes to copy from source buffer
2227 * @hlen: size of linear headroom in destination buffer
2229 * Copies up to `len` bytes from `from` to `to` by creating references
2230 * to the frags in the source buffer.
2232 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2233 * headroom in the `to` buffer.
2235 * Return value:
2236 * 0: everything is OK
2237 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2238 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2241 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2243 int i, j = 0;
2244 int plen = 0; /* length of skb->head fragment */
2245 int ret;
2246 struct page *page;
2247 unsigned int offset;
2249 BUG_ON(!from->head_frag && !hlen);
2251 /* dont bother with small payloads */
2252 if (len <= skb_tailroom(to))
2253 return skb_copy_bits(from, 0, skb_put(to, len), len);
2255 if (hlen) {
2256 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2257 if (unlikely(ret))
2258 return ret;
2259 len -= hlen;
2260 } else {
2261 plen = min_t(int, skb_headlen(from), len);
2262 if (plen) {
2263 page = virt_to_head_page(from->head);
2264 offset = from->data - (unsigned char *)page_address(page);
2265 __skb_fill_page_desc(to, 0, page, offset, plen);
2266 get_page(page);
2267 j = 1;
2268 len -= plen;
2272 to->truesize += len + plen;
2273 to->len += len + plen;
2274 to->data_len += len + plen;
2276 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2277 skb_tx_error(from);
2278 return -ENOMEM;
2281 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2282 if (!len)
2283 break;
2284 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2285 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2286 len -= skb_shinfo(to)->frags[j].size;
2287 skb_frag_ref(to, j);
2288 j++;
2290 skb_shinfo(to)->nr_frags = j;
2292 return 0;
2294 EXPORT_SYMBOL_GPL(skb_zerocopy);
2296 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2298 __wsum csum;
2299 long csstart;
2301 if (skb->ip_summed == CHECKSUM_PARTIAL)
2302 csstart = skb_checksum_start_offset(skb);
2303 else
2304 csstart = skb_headlen(skb);
2306 BUG_ON(csstart > skb_headlen(skb));
2308 skb_copy_from_linear_data(skb, to, csstart);
2310 csum = 0;
2311 if (csstart != skb->len)
2312 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2313 skb->len - csstart, 0);
2315 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2316 long csstuff = csstart + skb->csum_offset;
2318 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2321 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2324 * skb_dequeue - remove from the head of the queue
2325 * @list: list to dequeue from
2327 * Remove the head of the list. The list lock is taken so the function
2328 * may be used safely with other locking list functions. The head item is
2329 * returned or %NULL if the list is empty.
2332 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2334 unsigned long flags;
2335 struct sk_buff *result;
2337 spin_lock_irqsave(&list->lock, flags);
2338 result = __skb_dequeue(list);
2339 spin_unlock_irqrestore(&list->lock, flags);
2340 return result;
2342 EXPORT_SYMBOL(skb_dequeue);
2345 * skb_dequeue_tail - remove from the tail of the queue
2346 * @list: list to dequeue from
2348 * Remove the tail of the list. The list lock is taken so the function
2349 * may be used safely with other locking list functions. The tail item is
2350 * returned or %NULL if the list is empty.
2352 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2354 unsigned long flags;
2355 struct sk_buff *result;
2357 spin_lock_irqsave(&list->lock, flags);
2358 result = __skb_dequeue_tail(list);
2359 spin_unlock_irqrestore(&list->lock, flags);
2360 return result;
2362 EXPORT_SYMBOL(skb_dequeue_tail);
2365 * skb_queue_purge - empty a list
2366 * @list: list to empty
2368 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2369 * the list and one reference dropped. This function takes the list
2370 * lock and is atomic with respect to other list locking functions.
2372 void skb_queue_purge(struct sk_buff_head *list)
2374 struct sk_buff *skb;
2375 while ((skb = skb_dequeue(list)) != NULL)
2376 kfree_skb(skb);
2378 EXPORT_SYMBOL(skb_queue_purge);
2381 * skb_rbtree_purge - empty a skb rbtree
2382 * @root: root of the rbtree to empty
2384 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2385 * the list and one reference dropped. This function does not take
2386 * any lock. Synchronization should be handled by the caller (e.g., TCP
2387 * out-of-order queue is protected by the socket lock).
2389 void skb_rbtree_purge(struct rb_root *root)
2391 struct sk_buff *skb, *next;
2393 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2394 kfree_skb(skb);
2396 *root = RB_ROOT;
2400 * skb_queue_head - queue a buffer at the list head
2401 * @list: list to use
2402 * @newsk: buffer to queue
2404 * Queue a buffer at the start of the list. This function takes the
2405 * list lock and can be used safely with other locking &sk_buff functions
2406 * safely.
2408 * A buffer cannot be placed on two lists at the same time.
2410 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2412 unsigned long flags;
2414 spin_lock_irqsave(&list->lock, flags);
2415 __skb_queue_head(list, newsk);
2416 spin_unlock_irqrestore(&list->lock, flags);
2418 EXPORT_SYMBOL(skb_queue_head);
2421 * skb_queue_tail - queue a buffer at the list tail
2422 * @list: list to use
2423 * @newsk: buffer to queue
2425 * Queue a buffer at the tail of the list. This function takes the
2426 * list lock and can be used safely with other locking &sk_buff functions
2427 * safely.
2429 * A buffer cannot be placed on two lists at the same time.
2431 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2433 unsigned long flags;
2435 spin_lock_irqsave(&list->lock, flags);
2436 __skb_queue_tail(list, newsk);
2437 spin_unlock_irqrestore(&list->lock, flags);
2439 EXPORT_SYMBOL(skb_queue_tail);
2442 * skb_unlink - remove a buffer from a list
2443 * @skb: buffer to remove
2444 * @list: list to use
2446 * Remove a packet from a list. The list locks are taken and this
2447 * function is atomic with respect to other list locked calls
2449 * You must know what list the SKB is on.
2451 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2453 unsigned long flags;
2455 spin_lock_irqsave(&list->lock, flags);
2456 __skb_unlink(skb, list);
2457 spin_unlock_irqrestore(&list->lock, flags);
2459 EXPORT_SYMBOL(skb_unlink);
2462 * skb_append - append a buffer
2463 * @old: buffer to insert after
2464 * @newsk: buffer to insert
2465 * @list: list to use
2467 * Place a packet after a given packet in a list. The list locks are taken
2468 * and this function is atomic with respect to other list locked calls.
2469 * A buffer cannot be placed on two lists at the same time.
2471 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2473 unsigned long flags;
2475 spin_lock_irqsave(&list->lock, flags);
2476 __skb_queue_after(list, old, newsk);
2477 spin_unlock_irqrestore(&list->lock, flags);
2479 EXPORT_SYMBOL(skb_append);
2482 * skb_insert - insert a buffer
2483 * @old: buffer to insert before
2484 * @newsk: buffer to insert
2485 * @list: list to use
2487 * Place a packet before a given packet in a list. The list locks are
2488 * taken and this function is atomic with respect to other list locked
2489 * calls.
2491 * A buffer cannot be placed on two lists at the same time.
2493 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2495 unsigned long flags;
2497 spin_lock_irqsave(&list->lock, flags);
2498 __skb_insert(newsk, old->prev, old, list);
2499 spin_unlock_irqrestore(&list->lock, flags);
2501 EXPORT_SYMBOL(skb_insert);
2503 static inline void skb_split_inside_header(struct sk_buff *skb,
2504 struct sk_buff* skb1,
2505 const u32 len, const int pos)
2507 int i;
2509 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2510 pos - len);
2511 /* And move data appendix as is. */
2512 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2513 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2515 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2516 skb_shinfo(skb)->nr_frags = 0;
2517 skb1->data_len = skb->data_len;
2518 skb1->len += skb1->data_len;
2519 skb->data_len = 0;
2520 skb->len = len;
2521 skb_set_tail_pointer(skb, len);
2524 static inline void skb_split_no_header(struct sk_buff *skb,
2525 struct sk_buff* skb1,
2526 const u32 len, int pos)
2528 int i, k = 0;
2529 const int nfrags = skb_shinfo(skb)->nr_frags;
2531 skb_shinfo(skb)->nr_frags = 0;
2532 skb1->len = skb1->data_len = skb->len - len;
2533 skb->len = len;
2534 skb->data_len = len - pos;
2536 for (i = 0; i < nfrags; i++) {
2537 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2539 if (pos + size > len) {
2540 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2542 if (pos < len) {
2543 /* Split frag.
2544 * We have two variants in this case:
2545 * 1. Move all the frag to the second
2546 * part, if it is possible. F.e.
2547 * this approach is mandatory for TUX,
2548 * where splitting is expensive.
2549 * 2. Split is accurately. We make this.
2551 skb_frag_ref(skb, i);
2552 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2553 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2554 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2555 skb_shinfo(skb)->nr_frags++;
2557 k++;
2558 } else
2559 skb_shinfo(skb)->nr_frags++;
2560 pos += size;
2562 skb_shinfo(skb1)->nr_frags = k;
2566 * skb_split - Split fragmented skb to two parts at length len.
2567 * @skb: the buffer to split
2568 * @skb1: the buffer to receive the second part
2569 * @len: new length for skb
2571 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2573 int pos = skb_headlen(skb);
2575 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2576 SKBTX_SHARED_FRAG;
2577 if (len < pos) /* Split line is inside header. */
2578 skb_split_inside_header(skb, skb1, len, pos);
2579 else /* Second chunk has no header, nothing to copy. */
2580 skb_split_no_header(skb, skb1, len, pos);
2582 EXPORT_SYMBOL(skb_split);
2584 /* Shifting from/to a cloned skb is a no-go.
2586 * Caller cannot keep skb_shinfo related pointers past calling here!
2588 static int skb_prepare_for_shift(struct sk_buff *skb)
2590 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2594 * skb_shift - Shifts paged data partially from skb to another
2595 * @tgt: buffer into which tail data gets added
2596 * @skb: buffer from which the paged data comes from
2597 * @shiftlen: shift up to this many bytes
2599 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2600 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2601 * It's up to caller to free skb if everything was shifted.
2603 * If @tgt runs out of frags, the whole operation is aborted.
2605 * Skb cannot include anything else but paged data while tgt is allowed
2606 * to have non-paged data as well.
2608 * TODO: full sized shift could be optimized but that would need
2609 * specialized skb free'er to handle frags without up-to-date nr_frags.
2611 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2613 int from, to, merge, todo;
2614 struct skb_frag_struct *fragfrom, *fragto;
2616 BUG_ON(shiftlen > skb->len);
2617 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2619 todo = shiftlen;
2620 from = 0;
2621 to = skb_shinfo(tgt)->nr_frags;
2622 fragfrom = &skb_shinfo(skb)->frags[from];
2624 /* Actual merge is delayed until the point when we know we can
2625 * commit all, so that we don't have to undo partial changes
2627 if (!to ||
2628 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2629 fragfrom->page_offset)) {
2630 merge = -1;
2631 } else {
2632 merge = to - 1;
2634 todo -= skb_frag_size(fragfrom);
2635 if (todo < 0) {
2636 if (skb_prepare_for_shift(skb) ||
2637 skb_prepare_for_shift(tgt))
2638 return 0;
2640 /* All previous frag pointers might be stale! */
2641 fragfrom = &skb_shinfo(skb)->frags[from];
2642 fragto = &skb_shinfo(tgt)->frags[merge];
2644 skb_frag_size_add(fragto, shiftlen);
2645 skb_frag_size_sub(fragfrom, shiftlen);
2646 fragfrom->page_offset += shiftlen;
2648 goto onlymerged;
2651 from++;
2654 /* Skip full, not-fitting skb to avoid expensive operations */
2655 if ((shiftlen == skb->len) &&
2656 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2657 return 0;
2659 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2660 return 0;
2662 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2663 if (to == MAX_SKB_FRAGS)
2664 return 0;
2666 fragfrom = &skb_shinfo(skb)->frags[from];
2667 fragto = &skb_shinfo(tgt)->frags[to];
2669 if (todo >= skb_frag_size(fragfrom)) {
2670 *fragto = *fragfrom;
2671 todo -= skb_frag_size(fragfrom);
2672 from++;
2673 to++;
2675 } else {
2676 __skb_frag_ref(fragfrom);
2677 fragto->page = fragfrom->page;
2678 fragto->page_offset = fragfrom->page_offset;
2679 skb_frag_size_set(fragto, todo);
2681 fragfrom->page_offset += todo;
2682 skb_frag_size_sub(fragfrom, todo);
2683 todo = 0;
2685 to++;
2686 break;
2690 /* Ready to "commit" this state change to tgt */
2691 skb_shinfo(tgt)->nr_frags = to;
2693 if (merge >= 0) {
2694 fragfrom = &skb_shinfo(skb)->frags[0];
2695 fragto = &skb_shinfo(tgt)->frags[merge];
2697 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2698 __skb_frag_unref(fragfrom);
2701 /* Reposition in the original skb */
2702 to = 0;
2703 while (from < skb_shinfo(skb)->nr_frags)
2704 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2705 skb_shinfo(skb)->nr_frags = to;
2707 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2709 onlymerged:
2710 /* Most likely the tgt won't ever need its checksum anymore, skb on
2711 * the other hand might need it if it needs to be resent
2713 tgt->ip_summed = CHECKSUM_PARTIAL;
2714 skb->ip_summed = CHECKSUM_PARTIAL;
2716 /* Yak, is it really working this way? Some helper please? */
2717 skb->len -= shiftlen;
2718 skb->data_len -= shiftlen;
2719 skb->truesize -= shiftlen;
2720 tgt->len += shiftlen;
2721 tgt->data_len += shiftlen;
2722 tgt->truesize += shiftlen;
2724 return shiftlen;
2728 * skb_prepare_seq_read - Prepare a sequential read of skb data
2729 * @skb: the buffer to read
2730 * @from: lower offset of data to be read
2731 * @to: upper offset of data to be read
2732 * @st: state variable
2734 * Initializes the specified state variable. Must be called before
2735 * invoking skb_seq_read() for the first time.
2737 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2738 unsigned int to, struct skb_seq_state *st)
2740 st->lower_offset = from;
2741 st->upper_offset = to;
2742 st->root_skb = st->cur_skb = skb;
2743 st->frag_idx = st->stepped_offset = 0;
2744 st->frag_data = NULL;
2746 EXPORT_SYMBOL(skb_prepare_seq_read);
2749 * skb_seq_read - Sequentially read skb data
2750 * @consumed: number of bytes consumed by the caller so far
2751 * @data: destination pointer for data to be returned
2752 * @st: state variable
2754 * Reads a block of skb data at @consumed relative to the
2755 * lower offset specified to skb_prepare_seq_read(). Assigns
2756 * the head of the data block to @data and returns the length
2757 * of the block or 0 if the end of the skb data or the upper
2758 * offset has been reached.
2760 * The caller is not required to consume all of the data
2761 * returned, i.e. @consumed is typically set to the number
2762 * of bytes already consumed and the next call to
2763 * skb_seq_read() will return the remaining part of the block.
2765 * Note 1: The size of each block of data returned can be arbitrary,
2766 * this limitation is the cost for zerocopy sequential
2767 * reads of potentially non linear data.
2769 * Note 2: Fragment lists within fragments are not implemented
2770 * at the moment, state->root_skb could be replaced with
2771 * a stack for this purpose.
2773 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2774 struct skb_seq_state *st)
2776 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2777 skb_frag_t *frag;
2779 if (unlikely(abs_offset >= st->upper_offset)) {
2780 if (st->frag_data) {
2781 kunmap_atomic(st->frag_data);
2782 st->frag_data = NULL;
2784 return 0;
2787 next_skb:
2788 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2790 if (abs_offset < block_limit && !st->frag_data) {
2791 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2792 return block_limit - abs_offset;
2795 if (st->frag_idx == 0 && !st->frag_data)
2796 st->stepped_offset += skb_headlen(st->cur_skb);
2798 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2799 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2800 block_limit = skb_frag_size(frag) + st->stepped_offset;
2802 if (abs_offset < block_limit) {
2803 if (!st->frag_data)
2804 st->frag_data = kmap_atomic(skb_frag_page(frag));
2806 *data = (u8 *) st->frag_data + frag->page_offset +
2807 (abs_offset - st->stepped_offset);
2809 return block_limit - abs_offset;
2812 if (st->frag_data) {
2813 kunmap_atomic(st->frag_data);
2814 st->frag_data = NULL;
2817 st->frag_idx++;
2818 st->stepped_offset += skb_frag_size(frag);
2821 if (st->frag_data) {
2822 kunmap_atomic(st->frag_data);
2823 st->frag_data = NULL;
2826 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2827 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2828 st->frag_idx = 0;
2829 goto next_skb;
2830 } else if (st->cur_skb->next) {
2831 st->cur_skb = st->cur_skb->next;
2832 st->frag_idx = 0;
2833 goto next_skb;
2836 return 0;
2838 EXPORT_SYMBOL(skb_seq_read);
2841 * skb_abort_seq_read - Abort a sequential read of skb data
2842 * @st: state variable
2844 * Must be called if skb_seq_read() was not called until it
2845 * returned 0.
2847 void skb_abort_seq_read(struct skb_seq_state *st)
2849 if (st->frag_data)
2850 kunmap_atomic(st->frag_data);
2852 EXPORT_SYMBOL(skb_abort_seq_read);
2854 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2856 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2857 struct ts_config *conf,
2858 struct ts_state *state)
2860 return skb_seq_read(offset, text, TS_SKB_CB(state));
2863 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2865 skb_abort_seq_read(TS_SKB_CB(state));
2869 * skb_find_text - Find a text pattern in skb data
2870 * @skb: the buffer to look in
2871 * @from: search offset
2872 * @to: search limit
2873 * @config: textsearch configuration
2875 * Finds a pattern in the skb data according to the specified
2876 * textsearch configuration. Use textsearch_next() to retrieve
2877 * subsequent occurrences of the pattern. Returns the offset
2878 * to the first occurrence or UINT_MAX if no match was found.
2880 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2881 unsigned int to, struct ts_config *config)
2883 struct ts_state state;
2884 unsigned int ret;
2886 config->get_next_block = skb_ts_get_next_block;
2887 config->finish = skb_ts_finish;
2889 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2891 ret = textsearch_find(config, &state);
2892 return (ret <= to - from ? ret : UINT_MAX);
2894 EXPORT_SYMBOL(skb_find_text);
2897 * skb_append_datato_frags - append the user data to a skb
2898 * @sk: sock structure
2899 * @skb: skb structure to be appended with user data.
2900 * @getfrag: call back function to be used for getting the user data
2901 * @from: pointer to user message iov
2902 * @length: length of the iov message
2904 * Description: This procedure append the user data in the fragment part
2905 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2907 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2908 int (*getfrag)(void *from, char *to, int offset,
2909 int len, int odd, struct sk_buff *skb),
2910 void *from, int length)
2912 int frg_cnt = skb_shinfo(skb)->nr_frags;
2913 int copy;
2914 int offset = 0;
2915 int ret;
2916 struct page_frag *pfrag = &current->task_frag;
2918 do {
2919 /* Return error if we don't have space for new frag */
2920 if (frg_cnt >= MAX_SKB_FRAGS)
2921 return -EMSGSIZE;
2923 if (!sk_page_frag_refill(sk, pfrag))
2924 return -ENOMEM;
2926 /* copy the user data to page */
2927 copy = min_t(int, length, pfrag->size - pfrag->offset);
2929 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2930 offset, copy, 0, skb);
2931 if (ret < 0)
2932 return -EFAULT;
2934 /* copy was successful so update the size parameters */
2935 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2936 copy);
2937 frg_cnt++;
2938 pfrag->offset += copy;
2939 get_page(pfrag->page);
2941 skb->truesize += copy;
2942 atomic_add(copy, &sk->sk_wmem_alloc);
2943 skb->len += copy;
2944 skb->data_len += copy;
2945 offset += copy;
2946 length -= copy;
2948 } while (length > 0);
2950 return 0;
2952 EXPORT_SYMBOL(skb_append_datato_frags);
2954 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2955 int offset, size_t size)
2957 int i = skb_shinfo(skb)->nr_frags;
2959 if (skb_can_coalesce(skb, i, page, offset)) {
2960 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2961 } else if (i < MAX_SKB_FRAGS) {
2962 get_page(page);
2963 skb_fill_page_desc(skb, i, page, offset, size);
2964 } else {
2965 return -EMSGSIZE;
2968 return 0;
2970 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2973 * skb_pull_rcsum - pull skb and update receive checksum
2974 * @skb: buffer to update
2975 * @len: length of data pulled
2977 * This function performs an skb_pull on the packet and updates
2978 * the CHECKSUM_COMPLETE checksum. It should be used on
2979 * receive path processing instead of skb_pull unless you know
2980 * that the checksum difference is zero (e.g., a valid IP header)
2981 * or you are setting ip_summed to CHECKSUM_NONE.
2983 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2985 unsigned char *data = skb->data;
2987 BUG_ON(len > skb->len);
2988 __skb_pull(skb, len);
2989 skb_postpull_rcsum(skb, data, len);
2990 return skb->data;
2992 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2995 * skb_segment - Perform protocol segmentation on skb.
2996 * @head_skb: buffer to segment
2997 * @features: features for the output path (see dev->features)
2999 * This function performs segmentation on the given skb. It returns
3000 * a pointer to the first in a list of new skbs for the segments.
3001 * In case of error it returns ERR_PTR(err).
3003 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3004 netdev_features_t features)
3006 struct sk_buff *segs = NULL;
3007 struct sk_buff *tail = NULL;
3008 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3009 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3010 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3011 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3012 struct sk_buff *frag_skb = head_skb;
3013 unsigned int offset = doffset;
3014 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3015 unsigned int headroom;
3016 unsigned int len;
3017 __be16 proto;
3018 bool csum;
3019 int sg = !!(features & NETIF_F_SG);
3020 int nfrags = skb_shinfo(head_skb)->nr_frags;
3021 int err = -ENOMEM;
3022 int i = 0;
3023 int pos;
3024 int dummy;
3026 __skb_push(head_skb, doffset);
3027 proto = skb_network_protocol(head_skb, &dummy);
3028 if (unlikely(!proto))
3029 return ERR_PTR(-EINVAL);
3031 csum = !head_skb->encap_hdr_csum &&
3032 !!can_checksum_protocol(features, proto);
3034 headroom = skb_headroom(head_skb);
3035 pos = skb_headlen(head_skb);
3037 do {
3038 struct sk_buff *nskb;
3039 skb_frag_t *nskb_frag;
3040 int hsize;
3041 int size;
3043 len = head_skb->len - offset;
3044 if (len > mss)
3045 len = mss;
3047 hsize = skb_headlen(head_skb) - offset;
3048 if (hsize < 0)
3049 hsize = 0;
3050 if (hsize > len || !sg)
3051 hsize = len;
3053 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3054 (skb_headlen(list_skb) == len || sg)) {
3055 BUG_ON(skb_headlen(list_skb) > len);
3057 i = 0;
3058 nfrags = skb_shinfo(list_skb)->nr_frags;
3059 frag = skb_shinfo(list_skb)->frags;
3060 frag_skb = list_skb;
3061 pos += skb_headlen(list_skb);
3063 while (pos < offset + len) {
3064 BUG_ON(i >= nfrags);
3066 size = skb_frag_size(frag);
3067 if (pos + size > offset + len)
3068 break;
3070 i++;
3071 pos += size;
3072 frag++;
3075 nskb = skb_clone(list_skb, GFP_ATOMIC);
3076 list_skb = list_skb->next;
3078 if (unlikely(!nskb))
3079 goto err;
3081 if (unlikely(pskb_trim(nskb, len))) {
3082 kfree_skb(nskb);
3083 goto err;
3086 hsize = skb_end_offset(nskb);
3087 if (skb_cow_head(nskb, doffset + headroom)) {
3088 kfree_skb(nskb);
3089 goto err;
3092 nskb->truesize += skb_end_offset(nskb) - hsize;
3093 skb_release_head_state(nskb);
3094 __skb_push(nskb, doffset);
3095 } else {
3096 nskb = __alloc_skb(hsize + doffset + headroom,
3097 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3098 NUMA_NO_NODE);
3100 if (unlikely(!nskb))
3101 goto err;
3103 skb_reserve(nskb, headroom);
3104 __skb_put(nskb, doffset);
3107 if (segs)
3108 tail->next = nskb;
3109 else
3110 segs = nskb;
3111 tail = nskb;
3113 __copy_skb_header(nskb, head_skb);
3115 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3116 skb_reset_mac_len(nskb);
3118 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3119 nskb->data - tnl_hlen,
3120 doffset + tnl_hlen);
3122 if (nskb->len == len + doffset)
3123 goto perform_csum_check;
3125 if (!sg && !nskb->remcsum_offload) {
3126 nskb->ip_summed = CHECKSUM_NONE;
3127 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3128 skb_put(nskb, len),
3129 len, 0);
3130 SKB_GSO_CB(nskb)->csum_start =
3131 skb_headroom(nskb) + doffset;
3132 continue;
3135 nskb_frag = skb_shinfo(nskb)->frags;
3137 skb_copy_from_linear_data_offset(head_skb, offset,
3138 skb_put(nskb, hsize), hsize);
3140 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3141 SKBTX_SHARED_FRAG;
3143 while (pos < offset + len) {
3144 if (i >= nfrags) {
3145 BUG_ON(skb_headlen(list_skb));
3147 i = 0;
3148 nfrags = skb_shinfo(list_skb)->nr_frags;
3149 frag = skb_shinfo(list_skb)->frags;
3150 frag_skb = list_skb;
3152 BUG_ON(!nfrags);
3154 list_skb = list_skb->next;
3157 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3158 MAX_SKB_FRAGS)) {
3159 net_warn_ratelimited(
3160 "skb_segment: too many frags: %u %u\n",
3161 pos, mss);
3162 goto err;
3165 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3166 goto err;
3168 *nskb_frag = *frag;
3169 __skb_frag_ref(nskb_frag);
3170 size = skb_frag_size(nskb_frag);
3172 if (pos < offset) {
3173 nskb_frag->page_offset += offset - pos;
3174 skb_frag_size_sub(nskb_frag, offset - pos);
3177 skb_shinfo(nskb)->nr_frags++;
3179 if (pos + size <= offset + len) {
3180 i++;
3181 frag++;
3182 pos += size;
3183 } else {
3184 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3185 goto skip_fraglist;
3188 nskb_frag++;
3191 skip_fraglist:
3192 nskb->data_len = len - hsize;
3193 nskb->len += nskb->data_len;
3194 nskb->truesize += nskb->data_len;
3196 perform_csum_check:
3197 if (!csum && !nskb->remcsum_offload) {
3198 nskb->csum = skb_checksum(nskb, doffset,
3199 nskb->len - doffset, 0);
3200 nskb->ip_summed = CHECKSUM_NONE;
3201 SKB_GSO_CB(nskb)->csum_start =
3202 skb_headroom(nskb) + doffset;
3204 } while ((offset += len) < head_skb->len);
3206 /* Some callers want to get the end of the list.
3207 * Put it in segs->prev to avoid walking the list.
3208 * (see validate_xmit_skb_list() for example)
3210 segs->prev = tail;
3212 /* Following permits correct backpressure, for protocols
3213 * using skb_set_owner_w().
3214 * Idea is to tranfert ownership from head_skb to last segment.
3216 if (head_skb->destructor == sock_wfree) {
3217 swap(tail->truesize, head_skb->truesize);
3218 swap(tail->destructor, head_skb->destructor);
3219 swap(tail->sk, head_skb->sk);
3221 return segs;
3223 err:
3224 kfree_skb_list(segs);
3225 return ERR_PTR(err);
3227 EXPORT_SYMBOL_GPL(skb_segment);
3229 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3231 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3232 unsigned int offset = skb_gro_offset(skb);
3233 unsigned int headlen = skb_headlen(skb);
3234 unsigned int len = skb_gro_len(skb);
3235 struct sk_buff *lp, *p = *head;
3236 unsigned int delta_truesize;
3238 if (unlikely(p->len + len >= 65536))
3239 return -E2BIG;
3241 lp = NAPI_GRO_CB(p)->last;
3242 pinfo = skb_shinfo(lp);
3244 if (headlen <= offset) {
3245 skb_frag_t *frag;
3246 skb_frag_t *frag2;
3247 int i = skbinfo->nr_frags;
3248 int nr_frags = pinfo->nr_frags + i;
3250 if (nr_frags > MAX_SKB_FRAGS)
3251 goto merge;
3253 offset -= headlen;
3254 pinfo->nr_frags = nr_frags;
3255 skbinfo->nr_frags = 0;
3257 frag = pinfo->frags + nr_frags;
3258 frag2 = skbinfo->frags + i;
3259 do {
3260 *--frag = *--frag2;
3261 } while (--i);
3263 frag->page_offset += offset;
3264 skb_frag_size_sub(frag, offset);
3266 /* all fragments truesize : remove (head size + sk_buff) */
3267 delta_truesize = skb->truesize -
3268 SKB_TRUESIZE(skb_end_offset(skb));
3270 skb->truesize -= skb->data_len;
3271 skb->len -= skb->data_len;
3272 skb->data_len = 0;
3274 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3275 goto done;
3276 } else if (skb->head_frag) {
3277 int nr_frags = pinfo->nr_frags;
3278 skb_frag_t *frag = pinfo->frags + nr_frags;
3279 struct page *page = virt_to_head_page(skb->head);
3280 unsigned int first_size = headlen - offset;
3281 unsigned int first_offset;
3283 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3284 goto merge;
3286 first_offset = skb->data -
3287 (unsigned char *)page_address(page) +
3288 offset;
3290 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3292 frag->page.p = page;
3293 frag->page_offset = first_offset;
3294 skb_frag_size_set(frag, first_size);
3296 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3297 /* We dont need to clear skbinfo->nr_frags here */
3299 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3300 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3301 goto done;
3304 merge:
3305 delta_truesize = skb->truesize;
3306 if (offset > headlen) {
3307 unsigned int eat = offset - headlen;
3309 skbinfo->frags[0].page_offset += eat;
3310 skb_frag_size_sub(&skbinfo->frags[0], eat);
3311 skb->data_len -= eat;
3312 skb->len -= eat;
3313 offset = headlen;
3316 __skb_pull(skb, offset);
3318 if (NAPI_GRO_CB(p)->last == p)
3319 skb_shinfo(p)->frag_list = skb;
3320 else
3321 NAPI_GRO_CB(p)->last->next = skb;
3322 NAPI_GRO_CB(p)->last = skb;
3323 __skb_header_release(skb);
3324 lp = p;
3326 done:
3327 NAPI_GRO_CB(p)->count++;
3328 p->data_len += len;
3329 p->truesize += delta_truesize;
3330 p->len += len;
3331 if (lp != p) {
3332 lp->data_len += len;
3333 lp->truesize += delta_truesize;
3334 lp->len += len;
3336 NAPI_GRO_CB(skb)->same_flow = 1;
3337 return 0;
3340 void __init skb_init(void)
3342 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3343 sizeof(struct sk_buff),
3345 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3346 NULL);
3347 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3348 sizeof(struct sk_buff_fclones),
3350 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3351 NULL);
3354 static int
3355 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3356 unsigned int recursion_level)
3358 int start = skb_headlen(skb);
3359 int i, copy = start - offset;
3360 struct sk_buff *frag_iter;
3361 int elt = 0;
3363 if (unlikely(recursion_level >= 24))
3364 return -EMSGSIZE;
3366 if (copy > 0) {
3367 if (copy > len)
3368 copy = len;
3369 sg_set_buf(sg, skb->data + offset, copy);
3370 elt++;
3371 if ((len -= copy) == 0)
3372 return elt;
3373 offset += copy;
3376 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3377 int end;
3379 WARN_ON(start > offset + len);
3381 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3382 if ((copy = end - offset) > 0) {
3383 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3384 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3385 return -EMSGSIZE;
3387 if (copy > len)
3388 copy = len;
3389 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3390 frag->page_offset+offset-start);
3391 elt++;
3392 if (!(len -= copy))
3393 return elt;
3394 offset += copy;
3396 start = end;
3399 skb_walk_frags(skb, frag_iter) {
3400 int end, ret;
3402 WARN_ON(start > offset + len);
3404 end = start + frag_iter->len;
3405 if ((copy = end - offset) > 0) {
3406 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3407 return -EMSGSIZE;
3409 if (copy > len)
3410 copy = len;
3411 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3412 copy, recursion_level + 1);
3413 if (unlikely(ret < 0))
3414 return ret;
3415 elt += ret;
3416 if ((len -= copy) == 0)
3417 return elt;
3418 offset += copy;
3420 start = end;
3422 BUG_ON(len);
3423 return elt;
3427 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3428 * @skb: Socket buffer containing the buffers to be mapped
3429 * @sg: The scatter-gather list to map into
3430 * @offset: The offset into the buffer's contents to start mapping
3431 * @len: Length of buffer space to be mapped
3433 * Fill the specified scatter-gather list with mappings/pointers into a
3434 * region of the buffer space attached to a socket buffer. Returns either
3435 * the number of scatterlist items used, or -EMSGSIZE if the contents
3436 * could not fit.
3438 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3440 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3442 if (nsg <= 0)
3443 return nsg;
3445 sg_mark_end(&sg[nsg - 1]);
3447 return nsg;
3449 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3451 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3452 * sglist without mark the sg which contain last skb data as the end.
3453 * So the caller can mannipulate sg list as will when padding new data after
3454 * the first call without calling sg_unmark_end to expend sg list.
3456 * Scenario to use skb_to_sgvec_nomark:
3457 * 1. sg_init_table
3458 * 2. skb_to_sgvec_nomark(payload1)
3459 * 3. skb_to_sgvec_nomark(payload2)
3461 * This is equivalent to:
3462 * 1. sg_init_table
3463 * 2. skb_to_sgvec(payload1)
3464 * 3. sg_unmark_end
3465 * 4. skb_to_sgvec(payload2)
3467 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3468 * is more preferable.
3470 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3471 int offset, int len)
3473 return __skb_to_sgvec(skb, sg, offset, len, 0);
3475 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3480 * skb_cow_data - Check that a socket buffer's data buffers are writable
3481 * @skb: The socket buffer to check.
3482 * @tailbits: Amount of trailing space to be added
3483 * @trailer: Returned pointer to the skb where the @tailbits space begins
3485 * Make sure that the data buffers attached to a socket buffer are
3486 * writable. If they are not, private copies are made of the data buffers
3487 * and the socket buffer is set to use these instead.
3489 * If @tailbits is given, make sure that there is space to write @tailbits
3490 * bytes of data beyond current end of socket buffer. @trailer will be
3491 * set to point to the skb in which this space begins.
3493 * The number of scatterlist elements required to completely map the
3494 * COW'd and extended socket buffer will be returned.
3496 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3498 int copyflag;
3499 int elt;
3500 struct sk_buff *skb1, **skb_p;
3502 /* If skb is cloned or its head is paged, reallocate
3503 * head pulling out all the pages (pages are considered not writable
3504 * at the moment even if they are anonymous).
3506 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3507 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3508 return -ENOMEM;
3510 /* Easy case. Most of packets will go this way. */
3511 if (!skb_has_frag_list(skb)) {
3512 /* A little of trouble, not enough of space for trailer.
3513 * This should not happen, when stack is tuned to generate
3514 * good frames. OK, on miss we reallocate and reserve even more
3515 * space, 128 bytes is fair. */
3517 if (skb_tailroom(skb) < tailbits &&
3518 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3519 return -ENOMEM;
3521 /* Voila! */
3522 *trailer = skb;
3523 return 1;
3526 /* Misery. We are in troubles, going to mincer fragments... */
3528 elt = 1;
3529 skb_p = &skb_shinfo(skb)->frag_list;
3530 copyflag = 0;
3532 while ((skb1 = *skb_p) != NULL) {
3533 int ntail = 0;
3535 /* The fragment is partially pulled by someone,
3536 * this can happen on input. Copy it and everything
3537 * after it. */
3539 if (skb_shared(skb1))
3540 copyflag = 1;
3542 /* If the skb is the last, worry about trailer. */
3544 if (skb1->next == NULL && tailbits) {
3545 if (skb_shinfo(skb1)->nr_frags ||
3546 skb_has_frag_list(skb1) ||
3547 skb_tailroom(skb1) < tailbits)
3548 ntail = tailbits + 128;
3551 if (copyflag ||
3552 skb_cloned(skb1) ||
3553 ntail ||
3554 skb_shinfo(skb1)->nr_frags ||
3555 skb_has_frag_list(skb1)) {
3556 struct sk_buff *skb2;
3558 /* Fuck, we are miserable poor guys... */
3559 if (ntail == 0)
3560 skb2 = skb_copy(skb1, GFP_ATOMIC);
3561 else
3562 skb2 = skb_copy_expand(skb1,
3563 skb_headroom(skb1),
3564 ntail,
3565 GFP_ATOMIC);
3566 if (unlikely(skb2 == NULL))
3567 return -ENOMEM;
3569 if (skb1->sk)
3570 skb_set_owner_w(skb2, skb1->sk);
3572 /* Looking around. Are we still alive?
3573 * OK, link new skb, drop old one */
3575 skb2->next = skb1->next;
3576 *skb_p = skb2;
3577 kfree_skb(skb1);
3578 skb1 = skb2;
3580 elt++;
3581 *trailer = skb1;
3582 skb_p = &skb1->next;
3585 return elt;
3587 EXPORT_SYMBOL_GPL(skb_cow_data);
3589 static void sock_rmem_free(struct sk_buff *skb)
3591 struct sock *sk = skb->sk;
3593 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3597 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3599 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3601 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3602 (unsigned int)sk->sk_rcvbuf)
3603 return -ENOMEM;
3605 skb_orphan(skb);
3606 skb->sk = sk;
3607 skb->destructor = sock_rmem_free;
3608 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3610 /* before exiting rcu section, make sure dst is refcounted */
3611 skb_dst_force(skb);
3613 skb_queue_tail(&sk->sk_error_queue, skb);
3614 if (!sock_flag(sk, SOCK_DEAD))
3615 sk->sk_error_report(sk);
3616 return 0;
3618 EXPORT_SYMBOL(sock_queue_err_skb);
3620 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3622 struct sk_buff_head *q = &sk->sk_error_queue;
3623 struct sk_buff *skb, *skb_next;
3624 unsigned long flags;
3625 int err = 0;
3627 spin_lock_irqsave(&q->lock, flags);
3628 skb = __skb_dequeue(q);
3629 if (skb && (skb_next = skb_peek(q)))
3630 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3631 spin_unlock_irqrestore(&q->lock, flags);
3633 sk->sk_err = err;
3634 if (err)
3635 sk->sk_error_report(sk);
3637 return skb;
3639 EXPORT_SYMBOL(sock_dequeue_err_skb);
3642 * skb_clone_sk - create clone of skb, and take reference to socket
3643 * @skb: the skb to clone
3645 * This function creates a clone of a buffer that holds a reference on
3646 * sk_refcnt. Buffers created via this function are meant to be
3647 * returned using sock_queue_err_skb, or free via kfree_skb.
3649 * When passing buffers allocated with this function to sock_queue_err_skb
3650 * it is necessary to wrap the call with sock_hold/sock_put in order to
3651 * prevent the socket from being released prior to being enqueued on
3652 * the sk_error_queue.
3654 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3656 struct sock *sk = skb->sk;
3657 struct sk_buff *clone;
3659 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3660 return NULL;
3662 clone = skb_clone(skb, GFP_ATOMIC);
3663 if (!clone) {
3664 sock_put(sk);
3665 return NULL;
3668 clone->sk = sk;
3669 clone->destructor = sock_efree;
3671 return clone;
3673 EXPORT_SYMBOL(skb_clone_sk);
3675 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3676 struct sock *sk,
3677 int tstype)
3679 struct sock_exterr_skb *serr;
3680 int err;
3682 serr = SKB_EXT_ERR(skb);
3683 memset(serr, 0, sizeof(*serr));
3684 serr->ee.ee_errno = ENOMSG;
3685 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3686 serr->ee.ee_info = tstype;
3687 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3688 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3689 if (sk->sk_protocol == IPPROTO_TCP &&
3690 sk->sk_type == SOCK_STREAM)
3691 serr->ee.ee_data -= sk->sk_tskey;
3694 err = sock_queue_err_skb(sk, skb);
3696 if (err)
3697 kfree_skb(skb);
3700 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3702 bool ret;
3704 if (likely(sysctl_tstamp_allow_data || tsonly))
3705 return true;
3707 read_lock_bh(&sk->sk_callback_lock);
3708 ret = sk->sk_socket && sk->sk_socket->file &&
3709 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3710 read_unlock_bh(&sk->sk_callback_lock);
3711 return ret;
3714 void skb_complete_tx_timestamp(struct sk_buff *skb,
3715 struct skb_shared_hwtstamps *hwtstamps)
3717 struct sock *sk = skb->sk;
3719 if (!skb_may_tx_timestamp(sk, false))
3720 goto err;
3722 /* Take a reference to prevent skb_orphan() from freeing the socket,
3723 * but only if the socket refcount is not zero.
3725 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3726 *skb_hwtstamps(skb) = *hwtstamps;
3727 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3728 sock_put(sk);
3729 return;
3732 err:
3733 kfree_skb(skb);
3735 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3737 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3738 struct skb_shared_hwtstamps *hwtstamps,
3739 struct sock *sk, int tstype)
3741 struct sk_buff *skb;
3742 bool tsonly;
3744 if (!sk)
3745 return;
3747 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3748 if (!skb_may_tx_timestamp(sk, tsonly))
3749 return;
3751 if (tsonly)
3752 skb = alloc_skb(0, GFP_ATOMIC);
3753 else
3754 skb = skb_clone(orig_skb, GFP_ATOMIC);
3755 if (!skb)
3756 return;
3758 if (tsonly) {
3759 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3760 SKBTX_ANY_TSTAMP;
3761 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3764 if (hwtstamps)
3765 *skb_hwtstamps(skb) = *hwtstamps;
3766 else
3767 skb->tstamp = ktime_get_real();
3769 __skb_complete_tx_timestamp(skb, sk, tstype);
3771 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3773 void skb_tstamp_tx(struct sk_buff *orig_skb,
3774 struct skb_shared_hwtstamps *hwtstamps)
3776 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3777 SCM_TSTAMP_SND);
3779 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3781 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3783 struct sock *sk = skb->sk;
3784 struct sock_exterr_skb *serr;
3785 int err = 1;
3787 skb->wifi_acked_valid = 1;
3788 skb->wifi_acked = acked;
3790 serr = SKB_EXT_ERR(skb);
3791 memset(serr, 0, sizeof(*serr));
3792 serr->ee.ee_errno = ENOMSG;
3793 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3795 /* Take a reference to prevent skb_orphan() from freeing the socket,
3796 * but only if the socket refcount is not zero.
3798 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3799 err = sock_queue_err_skb(sk, skb);
3800 sock_put(sk);
3802 if (err)
3803 kfree_skb(skb);
3805 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3808 * skb_partial_csum_set - set up and verify partial csum values for packet
3809 * @skb: the skb to set
3810 * @start: the number of bytes after skb->data to start checksumming.
3811 * @off: the offset from start to place the checksum.
3813 * For untrusted partially-checksummed packets, we need to make sure the values
3814 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3816 * This function checks and sets those values and skb->ip_summed: if this
3817 * returns false you should drop the packet.
3819 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3821 if (unlikely(start > skb_headlen(skb)) ||
3822 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3823 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3824 start, off, skb_headlen(skb));
3825 return false;
3827 skb->ip_summed = CHECKSUM_PARTIAL;
3828 skb->csum_start = skb_headroom(skb) + start;
3829 skb->csum_offset = off;
3830 skb_set_transport_header(skb, start);
3831 return true;
3833 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3835 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3836 unsigned int max)
3838 if (skb_headlen(skb) >= len)
3839 return 0;
3841 /* If we need to pullup then pullup to the max, so we
3842 * won't need to do it again.
3844 if (max > skb->len)
3845 max = skb->len;
3847 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3848 return -ENOMEM;
3850 if (skb_headlen(skb) < len)
3851 return -EPROTO;
3853 return 0;
3856 #define MAX_TCP_HDR_LEN (15 * 4)
3858 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3859 typeof(IPPROTO_IP) proto,
3860 unsigned int off)
3862 switch (proto) {
3863 int err;
3865 case IPPROTO_TCP:
3866 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3867 off + MAX_TCP_HDR_LEN);
3868 if (!err && !skb_partial_csum_set(skb, off,
3869 offsetof(struct tcphdr,
3870 check)))
3871 err = -EPROTO;
3872 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3874 case IPPROTO_UDP:
3875 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3876 off + sizeof(struct udphdr));
3877 if (!err && !skb_partial_csum_set(skb, off,
3878 offsetof(struct udphdr,
3879 check)))
3880 err = -EPROTO;
3881 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3884 return ERR_PTR(-EPROTO);
3887 /* This value should be large enough to cover a tagged ethernet header plus
3888 * maximally sized IP and TCP or UDP headers.
3890 #define MAX_IP_HDR_LEN 128
3892 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3894 unsigned int off;
3895 bool fragment;
3896 __sum16 *csum;
3897 int err;
3899 fragment = false;
3901 err = skb_maybe_pull_tail(skb,
3902 sizeof(struct iphdr),
3903 MAX_IP_HDR_LEN);
3904 if (err < 0)
3905 goto out;
3907 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3908 fragment = true;
3910 off = ip_hdrlen(skb);
3912 err = -EPROTO;
3914 if (fragment)
3915 goto out;
3917 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3918 if (IS_ERR(csum))
3919 return PTR_ERR(csum);
3921 if (recalculate)
3922 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3923 ip_hdr(skb)->daddr,
3924 skb->len - off,
3925 ip_hdr(skb)->protocol, 0);
3926 err = 0;
3928 out:
3929 return err;
3932 /* This value should be large enough to cover a tagged ethernet header plus
3933 * an IPv6 header, all options, and a maximal TCP or UDP header.
3935 #define MAX_IPV6_HDR_LEN 256
3937 #define OPT_HDR(type, skb, off) \
3938 (type *)(skb_network_header(skb) + (off))
3940 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3942 int err;
3943 u8 nexthdr;
3944 unsigned int off;
3945 unsigned int len;
3946 bool fragment;
3947 bool done;
3948 __sum16 *csum;
3950 fragment = false;
3951 done = false;
3953 off = sizeof(struct ipv6hdr);
3955 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3956 if (err < 0)
3957 goto out;
3959 nexthdr = ipv6_hdr(skb)->nexthdr;
3961 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3962 while (off <= len && !done) {
3963 switch (nexthdr) {
3964 case IPPROTO_DSTOPTS:
3965 case IPPROTO_HOPOPTS:
3966 case IPPROTO_ROUTING: {
3967 struct ipv6_opt_hdr *hp;
3969 err = skb_maybe_pull_tail(skb,
3970 off +
3971 sizeof(struct ipv6_opt_hdr),
3972 MAX_IPV6_HDR_LEN);
3973 if (err < 0)
3974 goto out;
3976 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3977 nexthdr = hp->nexthdr;
3978 off += ipv6_optlen(hp);
3979 break;
3981 case IPPROTO_AH: {
3982 struct ip_auth_hdr *hp;
3984 err = skb_maybe_pull_tail(skb,
3985 off +
3986 sizeof(struct ip_auth_hdr),
3987 MAX_IPV6_HDR_LEN);
3988 if (err < 0)
3989 goto out;
3991 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3992 nexthdr = hp->nexthdr;
3993 off += ipv6_authlen(hp);
3994 break;
3996 case IPPROTO_FRAGMENT: {
3997 struct frag_hdr *hp;
3999 err = skb_maybe_pull_tail(skb,
4000 off +
4001 sizeof(struct frag_hdr),
4002 MAX_IPV6_HDR_LEN);
4003 if (err < 0)
4004 goto out;
4006 hp = OPT_HDR(struct frag_hdr, skb, off);
4008 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4009 fragment = true;
4011 nexthdr = hp->nexthdr;
4012 off += sizeof(struct frag_hdr);
4013 break;
4015 default:
4016 done = true;
4017 break;
4021 err = -EPROTO;
4023 if (!done || fragment)
4024 goto out;
4026 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4027 if (IS_ERR(csum))
4028 return PTR_ERR(csum);
4030 if (recalculate)
4031 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4032 &ipv6_hdr(skb)->daddr,
4033 skb->len - off, nexthdr, 0);
4034 err = 0;
4036 out:
4037 return err;
4041 * skb_checksum_setup - set up partial checksum offset
4042 * @skb: the skb to set up
4043 * @recalculate: if true the pseudo-header checksum will be recalculated
4045 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4047 int err;
4049 switch (skb->protocol) {
4050 case htons(ETH_P_IP):
4051 err = skb_checksum_setup_ipv4(skb, recalculate);
4052 break;
4054 case htons(ETH_P_IPV6):
4055 err = skb_checksum_setup_ipv6(skb, recalculate);
4056 break;
4058 default:
4059 err = -EPROTO;
4060 break;
4063 return err;
4065 EXPORT_SYMBOL(skb_checksum_setup);
4068 * skb_checksum_maybe_trim - maybe trims the given skb
4069 * @skb: the skb to check
4070 * @transport_len: the data length beyond the network header
4072 * Checks whether the given skb has data beyond the given transport length.
4073 * If so, returns a cloned skb trimmed to this transport length.
4074 * Otherwise returns the provided skb. Returns NULL in error cases
4075 * (e.g. transport_len exceeds skb length or out-of-memory).
4077 * Caller needs to set the skb transport header and free any returned skb if it
4078 * differs from the provided skb.
4080 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4081 unsigned int transport_len)
4083 struct sk_buff *skb_chk;
4084 unsigned int len = skb_transport_offset(skb) + transport_len;
4085 int ret;
4087 if (skb->len < len)
4088 return NULL;
4089 else if (skb->len == len)
4090 return skb;
4092 skb_chk = skb_clone(skb, GFP_ATOMIC);
4093 if (!skb_chk)
4094 return NULL;
4096 ret = pskb_trim_rcsum(skb_chk, len);
4097 if (ret) {
4098 kfree_skb(skb_chk);
4099 return NULL;
4102 return skb_chk;
4106 * skb_checksum_trimmed - validate checksum of an skb
4107 * @skb: the skb to check
4108 * @transport_len: the data length beyond the network header
4109 * @skb_chkf: checksum function to use
4111 * Applies the given checksum function skb_chkf to the provided skb.
4112 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4114 * If the skb has data beyond the given transport length, then a
4115 * trimmed & cloned skb is checked and returned.
4117 * Caller needs to set the skb transport header and free any returned skb if it
4118 * differs from the provided skb.
4120 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4121 unsigned int transport_len,
4122 __sum16(*skb_chkf)(struct sk_buff *skb))
4124 struct sk_buff *skb_chk;
4125 unsigned int offset = skb_transport_offset(skb);
4126 __sum16 ret;
4128 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4129 if (!skb_chk)
4130 goto err;
4132 if (!pskb_may_pull(skb_chk, offset))
4133 goto err;
4135 skb_pull_rcsum(skb_chk, offset);
4136 ret = skb_chkf(skb_chk);
4137 skb_push_rcsum(skb_chk, offset);
4139 if (ret)
4140 goto err;
4142 return skb_chk;
4144 err:
4145 if (skb_chk && skb_chk != skb)
4146 kfree_skb(skb_chk);
4148 return NULL;
4151 EXPORT_SYMBOL(skb_checksum_trimmed);
4153 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4155 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4156 skb->dev->name);
4158 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4160 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4162 if (head_stolen) {
4163 skb_release_head_state(skb);
4164 kmem_cache_free(skbuff_head_cache, skb);
4165 } else {
4166 __kfree_skb(skb);
4169 EXPORT_SYMBOL(kfree_skb_partial);
4172 * skb_try_coalesce - try to merge skb to prior one
4173 * @to: prior buffer
4174 * @from: buffer to add
4175 * @fragstolen: pointer to boolean
4176 * @delta_truesize: how much more was allocated than was requested
4178 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4179 bool *fragstolen, int *delta_truesize)
4181 int i, delta, len = from->len;
4183 *fragstolen = false;
4185 if (skb_cloned(to))
4186 return false;
4188 if (len <= skb_tailroom(to)) {
4189 if (len)
4190 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4191 *delta_truesize = 0;
4192 return true;
4195 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4196 return false;
4198 if (skb_headlen(from) != 0) {
4199 struct page *page;
4200 unsigned int offset;
4202 if (skb_shinfo(to)->nr_frags +
4203 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4204 return false;
4206 if (skb_head_is_locked(from))
4207 return false;
4209 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4211 page = virt_to_head_page(from->head);
4212 offset = from->data - (unsigned char *)page_address(page);
4214 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4215 page, offset, skb_headlen(from));
4216 *fragstolen = true;
4217 } else {
4218 if (skb_shinfo(to)->nr_frags +
4219 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4220 return false;
4222 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4225 WARN_ON_ONCE(delta < len);
4227 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4228 skb_shinfo(from)->frags,
4229 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4230 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4232 if (!skb_cloned(from))
4233 skb_shinfo(from)->nr_frags = 0;
4235 /* if the skb is not cloned this does nothing
4236 * since we set nr_frags to 0.
4238 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4239 skb_frag_ref(from, i);
4241 to->truesize += delta;
4242 to->len += len;
4243 to->data_len += len;
4245 *delta_truesize = delta;
4246 return true;
4248 EXPORT_SYMBOL(skb_try_coalesce);
4251 * skb_scrub_packet - scrub an skb
4253 * @skb: buffer to clean
4254 * @xnet: packet is crossing netns
4256 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4257 * into/from a tunnel. Some information have to be cleared during these
4258 * operations.
4259 * skb_scrub_packet can also be used to clean a skb before injecting it in
4260 * another namespace (@xnet == true). We have to clear all information in the
4261 * skb that could impact namespace isolation.
4263 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4265 skb->tstamp.tv64 = 0;
4266 skb->pkt_type = PACKET_HOST;
4267 skb->skb_iif = 0;
4268 skb->ignore_df = 0;
4269 skb_dst_drop(skb);
4270 skb_sender_cpu_clear(skb);
4271 secpath_reset(skb);
4272 nf_reset(skb);
4273 nf_reset_trace(skb);
4275 if (!xnet)
4276 return;
4278 ipvs_reset(skb);
4279 skb_orphan(skb);
4280 skb->mark = 0;
4282 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4285 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4287 * @skb: GSO skb
4289 * skb_gso_transport_seglen is used to determine the real size of the
4290 * individual segments, including Layer4 headers (TCP/UDP).
4292 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4294 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4296 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4297 unsigned int thlen = 0;
4299 if (skb->encapsulation) {
4300 thlen = skb_inner_transport_header(skb) -
4301 skb_transport_header(skb);
4303 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4304 thlen += inner_tcp_hdrlen(skb);
4305 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4306 thlen = tcp_hdrlen(skb);
4308 /* UFO sets gso_size to the size of the fragmentation
4309 * payload, i.e. the size of the L4 (UDP) header is already
4310 * accounted for.
4312 return thlen + shinfo->gso_size;
4314 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4316 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4318 int mac_len;
4320 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4321 kfree_skb(skb);
4322 return NULL;
4325 mac_len = skb->data - skb_mac_header(skb);
4326 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
4327 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
4328 mac_len - VLAN_HLEN - ETH_TLEN);
4330 skb->mac_header += VLAN_HLEN;
4331 return skb;
4334 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4336 struct vlan_hdr *vhdr;
4337 u16 vlan_tci;
4339 if (unlikely(skb_vlan_tag_present(skb))) {
4340 /* vlan_tci is already set-up so leave this for another time */
4341 return skb;
4344 skb = skb_share_check(skb, GFP_ATOMIC);
4345 if (unlikely(!skb))
4346 goto err_free;
4348 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4349 goto err_free;
4351 vhdr = (struct vlan_hdr *)skb->data;
4352 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4353 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4355 skb_pull_rcsum(skb, VLAN_HLEN);
4356 vlan_set_encap_proto(skb, vhdr);
4358 skb = skb_reorder_vlan_header(skb);
4359 if (unlikely(!skb))
4360 goto err_free;
4362 skb_reset_network_header(skb);
4363 skb_reset_transport_header(skb);
4364 skb_reset_mac_len(skb);
4366 return skb;
4368 err_free:
4369 kfree_skb(skb);
4370 return NULL;
4372 EXPORT_SYMBOL(skb_vlan_untag);
4374 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4376 if (!pskb_may_pull(skb, write_len))
4377 return -ENOMEM;
4379 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4380 return 0;
4382 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4384 EXPORT_SYMBOL(skb_ensure_writable);
4386 /* remove VLAN header from packet and update csum accordingly. */
4387 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4389 struct vlan_hdr *vhdr;
4390 unsigned int offset = skb->data - skb_mac_header(skb);
4391 int err;
4393 __skb_push(skb, offset);
4394 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4395 if (unlikely(err))
4396 goto pull;
4398 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4400 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4401 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4403 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4404 __skb_pull(skb, VLAN_HLEN);
4406 vlan_set_encap_proto(skb, vhdr);
4407 skb->mac_header += VLAN_HLEN;
4409 if (skb_network_offset(skb) < ETH_HLEN)
4410 skb_set_network_header(skb, ETH_HLEN);
4412 skb_reset_mac_len(skb);
4413 pull:
4414 __skb_pull(skb, offset);
4416 return err;
4419 int skb_vlan_pop(struct sk_buff *skb)
4421 u16 vlan_tci;
4422 __be16 vlan_proto;
4423 int err;
4425 if (likely(skb_vlan_tag_present(skb))) {
4426 skb->vlan_tci = 0;
4427 } else {
4428 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4429 skb->protocol != htons(ETH_P_8021AD)) ||
4430 skb->len < VLAN_ETH_HLEN))
4431 return 0;
4433 err = __skb_vlan_pop(skb, &vlan_tci);
4434 if (err)
4435 return err;
4437 /* move next vlan tag to hw accel tag */
4438 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4439 skb->protocol != htons(ETH_P_8021AD)) ||
4440 skb->len < VLAN_ETH_HLEN))
4441 return 0;
4443 vlan_proto = skb->protocol;
4444 err = __skb_vlan_pop(skb, &vlan_tci);
4445 if (unlikely(err))
4446 return err;
4448 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4449 return 0;
4451 EXPORT_SYMBOL(skb_vlan_pop);
4453 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4455 if (skb_vlan_tag_present(skb)) {
4456 unsigned int offset = skb->data - skb_mac_header(skb);
4457 int err;
4459 /* __vlan_insert_tag expect skb->data pointing to mac header.
4460 * So change skb->data before calling it and change back to
4461 * original position later
4463 __skb_push(skb, offset);
4464 err = __vlan_insert_tag(skb, skb->vlan_proto,
4465 skb_vlan_tag_get(skb));
4466 if (err) {
4467 __skb_pull(skb, offset);
4468 return err;
4471 skb->protocol = skb->vlan_proto;
4472 skb->mac_len += VLAN_HLEN;
4474 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4475 __skb_pull(skb, offset);
4477 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4478 return 0;
4480 EXPORT_SYMBOL(skb_vlan_push);
4483 * alloc_skb_with_frags - allocate skb with page frags
4485 * @header_len: size of linear part
4486 * @data_len: needed length in frags
4487 * @max_page_order: max page order desired.
4488 * @errcode: pointer to error code if any
4489 * @gfp_mask: allocation mask
4491 * This can be used to allocate a paged skb, given a maximal order for frags.
4493 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4494 unsigned long data_len,
4495 int max_page_order,
4496 int *errcode,
4497 gfp_t gfp_mask)
4499 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4500 unsigned long chunk;
4501 struct sk_buff *skb;
4502 struct page *page;
4503 gfp_t gfp_head;
4504 int i;
4506 *errcode = -EMSGSIZE;
4507 /* Note this test could be relaxed, if we succeed to allocate
4508 * high order pages...
4510 if (npages > MAX_SKB_FRAGS)
4511 return NULL;
4513 gfp_head = gfp_mask;
4514 if (gfp_head & __GFP_DIRECT_RECLAIM)
4515 gfp_head |= __GFP_REPEAT;
4517 *errcode = -ENOBUFS;
4518 skb = alloc_skb(header_len, gfp_head);
4519 if (!skb)
4520 return NULL;
4522 skb->truesize += npages << PAGE_SHIFT;
4524 for (i = 0; npages > 0; i++) {
4525 int order = max_page_order;
4527 while (order) {
4528 if (npages >= 1 << order) {
4529 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4530 __GFP_COMP |
4531 __GFP_NOWARN |
4532 __GFP_NORETRY,
4533 order);
4534 if (page)
4535 goto fill_page;
4536 /* Do not retry other high order allocations */
4537 order = 1;
4538 max_page_order = 0;
4540 order--;
4542 page = alloc_page(gfp_mask);
4543 if (!page)
4544 goto failure;
4545 fill_page:
4546 chunk = min_t(unsigned long, data_len,
4547 PAGE_SIZE << order);
4548 skb_fill_page_desc(skb, i, page, 0, chunk);
4549 data_len -= chunk;
4550 npages -= 1 << order;
4552 return skb;
4554 failure:
4555 kfree_skb(skb);
4556 return NULL;
4558 EXPORT_SYMBOL(alloc_skb_with_frags);