Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
fork
Configure Feed
Select the types of activity you want to include in your feed.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Routines having to do with the 'struct sk_buff' memory handlers.
4 *
5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 *
8 * Fixes:
9 * Alan Cox : Fixed the worst of the load
10 * balancer bugs.
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
23 *
24 * NOTE:
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
29 */
30
31/*
32 * The functions in this file will not compile correctly with gcc 2.4.x
33 */
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
40#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/slab.h>
45#include <linux/tcp.h>
46#include <linux/udp.h>
47#include <linux/sctp.h>
48#include <linux/netdevice.h>
49#ifdef CONFIG_NET_CLS_ACT
50#include <net/pkt_sched.h>
51#endif
52#include <linux/string.h>
53#include <linux/skbuff.h>
54#include <linux/splice.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
58#include <linux/scatterlist.h>
59#include <linux/errqueue.h>
60#include <linux/prefetch.h>
61#include <linux/if_vlan.h>
62
63#include <net/protocol.h>
64#include <net/dst.h>
65#include <net/sock.h>
66#include <net/checksum.h>
67#include <net/ip6_checksum.h>
68#include <net/xfrm.h>
69
70#include <linux/uaccess.h>
71#include <trace/events/skb.h>
72#include <linux/highmem.h>
73#include <linux/capability.h>
74#include <linux/user_namespace.h>
75
76#include "datagram.h"
77
78struct kmem_cache *skbuff_head_cache __ro_after_init;
79static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
80#ifdef CONFIG_SKB_EXTENSIONS
81static struct kmem_cache *skbuff_ext_cache __ro_after_init;
82#endif
83int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
85
86/**
87 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
91 * @msg: skb_over_panic or skb_under_panic
92 *
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
97 */
98static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99 const char msg[])
100{
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
105 BUG();
106}
107
108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109{
110 skb_panic(skb, sz, addr, __func__);
111}
112
113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
161/**
162 * __alloc_skb - allocate a network buffer
163 * @size: size to allocate
164 * @gfp_mask: allocation mask
165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
169 * @node: numa node to allocate memory on
170 *
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
174 *
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
176 * %GFP_ATOMIC.
177 */
178struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
179 int flags, int node)
180{
181 struct kmem_cache *cache;
182 struct skb_shared_info *shinfo;
183 struct sk_buff *skb;
184 u8 *data;
185 bool pfmemalloc;
186
187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
189
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
192
193 /* Get the HEAD */
194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
195 if (!skb)
196 goto out;
197 prefetchw(skb);
198
199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
203 */
204 size = SKB_DATA_ALIGN(size);
205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
207 if (!data)
208 goto nodata;
209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
212 */
213 size = SKB_WITH_OVERHEAD(ksize(data));
214 prefetchw(data + size);
215
216 /*
217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
220 */
221 memset(skb, 0, offsetof(struct sk_buff, tail));
222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
224 skb->pfmemalloc = pfmemalloc;
225 refcount_set(&skb->users, 1);
226 skb->head = data;
227 skb->data = data;
228 skb_reset_tail_pointer(skb);
229 skb->end = skb->tail + size;
230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
232
233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
236 atomic_set(&shinfo->dataref, 1);
237
238 if (flags & SKB_ALLOC_FCLONE) {
239 struct sk_buff_fclones *fclones;
240
241 fclones = container_of(skb, struct sk_buff_fclones, skb1);
242
243 skb->fclone = SKB_FCLONE_ORIG;
244 refcount_set(&fclones->fclone_ref, 1);
245
246 fclones->skb2.fclone = SKB_FCLONE_CLONE;
247 }
248out:
249 return skb;
250nodata:
251 kmem_cache_free(cache, skb);
252 skb = NULL;
253 goto out;
254}
255EXPORT_SYMBOL(__alloc_skb);
256
257/* Caller must provide SKB that is memset cleared */
258static struct sk_buff *__build_skb_around(struct sk_buff *skb,
259 void *data, unsigned int frag_size)
260{
261 struct skb_shared_info *shinfo;
262 unsigned int size = frag_size ? : ksize(data);
263
264 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
265
266 /* Assumes caller memset cleared SKB */
267 skb->truesize = SKB_TRUESIZE(size);
268 refcount_set(&skb->users, 1);
269 skb->head = data;
270 skb->data = data;
271 skb_reset_tail_pointer(skb);
272 skb->end = skb->tail + size;
273 skb->mac_header = (typeof(skb->mac_header))~0U;
274 skb->transport_header = (typeof(skb->transport_header))~0U;
275
276 /* make sure we initialize shinfo sequentially */
277 shinfo = skb_shinfo(skb);
278 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
279 atomic_set(&shinfo->dataref, 1);
280
281 return skb;
282}
283
284/**
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
288 *
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.
302 */
303struct sk_buff *__build_skb(void *data, unsigned int frag_size)
304{
305 struct sk_buff *skb;
306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (unlikely(!skb))
309 return NULL;
310
311 memset(skb, 0, offsetof(struct sk_buff, tail));
312
313 return __build_skb_around(skb, data, frag_size);
314}
315
316/* build_skb() is wrapper over __build_skb(), that specifically
317 * takes care of skb->head and skb->pfmemalloc
318 * This means that if @frag_size is not zero, then @data must be backed
319 * by a page fragment, not kmalloc() or vmalloc()
320 */
321struct sk_buff *build_skb(void *data, unsigned int frag_size)
322{
323 struct sk_buff *skb = __build_skb(data, frag_size);
324
325 if (skb && frag_size) {
326 skb->head_frag = 1;
327 if (page_is_pfmemalloc(virt_to_head_page(data)))
328 skb->pfmemalloc = 1;
329 }
330 return skb;
331}
332EXPORT_SYMBOL(build_skb);
333
334/**
335 * build_skb_around - build a network buffer around provided skb
336 * @skb: sk_buff provide by caller, must be memset cleared
337 * @data: data buffer provided by caller
338 * @frag_size: size of data, or 0 if head was kmalloced
339 */
340struct sk_buff *build_skb_around(struct sk_buff *skb,
341 void *data, unsigned int frag_size)
342{
343 if (unlikely(!skb))
344 return NULL;
345
346 skb = __build_skb_around(skb, data, frag_size);
347
348 if (skb && frag_size) {
349 skb->head_frag = 1;
350 if (page_is_pfmemalloc(virt_to_head_page(data)))
351 skb->pfmemalloc = 1;
352 }
353 return skb;
354}
355EXPORT_SYMBOL(build_skb_around);
356
357#define NAPI_SKB_CACHE_SIZE 64
358
359struct napi_alloc_cache {
360 struct page_frag_cache page;
361 unsigned int skb_count;
362 void *skb_cache[NAPI_SKB_CACHE_SIZE];
363};
364
365static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
366static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
367
368static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
369{
370 struct page_frag_cache *nc;
371 unsigned long flags;
372 void *data;
373
374 local_irq_save(flags);
375 nc = this_cpu_ptr(&netdev_alloc_cache);
376 data = page_frag_alloc(nc, fragsz, gfp_mask);
377 local_irq_restore(flags);
378 return data;
379}
380
381/**
382 * netdev_alloc_frag - allocate a page fragment
383 * @fragsz: fragment size
384 *
385 * Allocates a frag from a page for receive buffer.
386 * Uses GFP_ATOMIC allocations.
387 */
388void *netdev_alloc_frag(unsigned int fragsz)
389{
390 fragsz = SKB_DATA_ALIGN(fragsz);
391
392 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
393}
394EXPORT_SYMBOL(netdev_alloc_frag);
395
396static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
397{
398 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
399
400 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
401}
402
403void *napi_alloc_frag(unsigned int fragsz)
404{
405 fragsz = SKB_DATA_ALIGN(fragsz);
406
407 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
408}
409EXPORT_SYMBOL(napi_alloc_frag);
410
411/**
412 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
413 * @dev: network device to receive on
414 * @len: length to allocate
415 * @gfp_mask: get_free_pages mask, passed to alloc_skb
416 *
417 * Allocate a new &sk_buff and assign it a usage count of one. The
418 * buffer has NET_SKB_PAD headroom built in. Users should allocate
419 * the headroom they think they need without accounting for the
420 * built in space. The built in space is used for optimisations.
421 *
422 * %NULL is returned if there is no free memory.
423 */
424struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
425 gfp_t gfp_mask)
426{
427 struct page_frag_cache *nc;
428 unsigned long flags;
429 struct sk_buff *skb;
430 bool pfmemalloc;
431 void *data;
432
433 len += NET_SKB_PAD;
434
435 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
436 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
437 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
438 if (!skb)
439 goto skb_fail;
440 goto skb_success;
441 }
442
443 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
444 len = SKB_DATA_ALIGN(len);
445
446 if (sk_memalloc_socks())
447 gfp_mask |= __GFP_MEMALLOC;
448
449 local_irq_save(flags);
450
451 nc = this_cpu_ptr(&netdev_alloc_cache);
452 data = page_frag_alloc(nc, len, gfp_mask);
453 pfmemalloc = nc->pfmemalloc;
454
455 local_irq_restore(flags);
456
457 if (unlikely(!data))
458 return NULL;
459
460 skb = __build_skb(data, len);
461 if (unlikely(!skb)) {
462 skb_free_frag(data);
463 return NULL;
464 }
465
466 /* use OR instead of assignment to avoid clearing of bits in mask */
467 if (pfmemalloc)
468 skb->pfmemalloc = 1;
469 skb->head_frag = 1;
470
471skb_success:
472 skb_reserve(skb, NET_SKB_PAD);
473 skb->dev = dev;
474
475skb_fail:
476 return skb;
477}
478EXPORT_SYMBOL(__netdev_alloc_skb);
479
480/**
481 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
482 * @napi: napi instance this buffer was allocated for
483 * @len: length to allocate
484 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
485 *
486 * Allocate a new sk_buff for use in NAPI receive. This buffer will
487 * attempt to allocate the head from a special reserved region used
488 * only for NAPI Rx allocation. By doing this we can save several
489 * CPU cycles by avoiding having to disable and re-enable IRQs.
490 *
491 * %NULL is returned if there is no free memory.
492 */
493struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
494 gfp_t gfp_mask)
495{
496 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
497 struct sk_buff *skb;
498 void *data;
499
500 len += NET_SKB_PAD + NET_IP_ALIGN;
501
502 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
503 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
504 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
505 if (!skb)
506 goto skb_fail;
507 goto skb_success;
508 }
509
510 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
511 len = SKB_DATA_ALIGN(len);
512
513 if (sk_memalloc_socks())
514 gfp_mask |= __GFP_MEMALLOC;
515
516 data = page_frag_alloc(&nc->page, len, gfp_mask);
517 if (unlikely(!data))
518 return NULL;
519
520 skb = __build_skb(data, len);
521 if (unlikely(!skb)) {
522 skb_free_frag(data);
523 return NULL;
524 }
525
526 /* use OR instead of assignment to avoid clearing of bits in mask */
527 if (nc->page.pfmemalloc)
528 skb->pfmemalloc = 1;
529 skb->head_frag = 1;
530
531skb_success:
532 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
533 skb->dev = napi->dev;
534
535skb_fail:
536 return skb;
537}
538EXPORT_SYMBOL(__napi_alloc_skb);
539
540void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
541 int size, unsigned int truesize)
542{
543 skb_fill_page_desc(skb, i, page, off, size);
544 skb->len += size;
545 skb->data_len += size;
546 skb->truesize += truesize;
547}
548EXPORT_SYMBOL(skb_add_rx_frag);
549
550void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
551 unsigned int truesize)
552{
553 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
554
555 skb_frag_size_add(frag, size);
556 skb->len += size;
557 skb->data_len += size;
558 skb->truesize += truesize;
559}
560EXPORT_SYMBOL(skb_coalesce_rx_frag);
561
562static void skb_drop_list(struct sk_buff **listp)
563{
564 kfree_skb_list(*listp);
565 *listp = NULL;
566}
567
568static inline void skb_drop_fraglist(struct sk_buff *skb)
569{
570 skb_drop_list(&skb_shinfo(skb)->frag_list);
571}
572
573static void skb_clone_fraglist(struct sk_buff *skb)
574{
575 struct sk_buff *list;
576
577 skb_walk_frags(skb, list)
578 skb_get(list);
579}
580
581static void skb_free_head(struct sk_buff *skb)
582{
583 unsigned char *head = skb->head;
584
585 if (skb->head_frag)
586 skb_free_frag(head);
587 else
588 kfree(head);
589}
590
591static void skb_release_data(struct sk_buff *skb)
592{
593 struct skb_shared_info *shinfo = skb_shinfo(skb);
594 int i;
595
596 if (skb->cloned &&
597 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
598 &shinfo->dataref))
599 return;
600
601 for (i = 0; i < shinfo->nr_frags; i++)
602 __skb_frag_unref(&shinfo->frags[i]);
603
604 if (shinfo->frag_list)
605 kfree_skb_list(shinfo->frag_list);
606
607 skb_zcopy_clear(skb, true);
608 skb_free_head(skb);
609}
610
611/*
612 * Free an skbuff by memory without cleaning the state.
613 */
614static void kfree_skbmem(struct sk_buff *skb)
615{
616 struct sk_buff_fclones *fclones;
617
618 switch (skb->fclone) {
619 case SKB_FCLONE_UNAVAILABLE:
620 kmem_cache_free(skbuff_head_cache, skb);
621 return;
622
623 case SKB_FCLONE_ORIG:
624 fclones = container_of(skb, struct sk_buff_fclones, skb1);
625
626 /* We usually free the clone (TX completion) before original skb
627 * This test would have no chance to be true for the clone,
628 * while here, branch prediction will be good.
629 */
630 if (refcount_read(&fclones->fclone_ref) == 1)
631 goto fastpath;
632 break;
633
634 default: /* SKB_FCLONE_CLONE */
635 fclones = container_of(skb, struct sk_buff_fclones, skb2);
636 break;
637 }
638 if (!refcount_dec_and_test(&fclones->fclone_ref))
639 return;
640fastpath:
641 kmem_cache_free(skbuff_fclone_cache, fclones);
642}
643
644void skb_release_head_state(struct sk_buff *skb)
645{
646 skb_dst_drop(skb);
647 if (skb->destructor) {
648 WARN_ON(in_irq());
649 skb->destructor(skb);
650 }
651#if IS_ENABLED(CONFIG_NF_CONNTRACK)
652 nf_conntrack_put(skb_nfct(skb));
653#endif
654 skb_ext_put(skb);
655}
656
657/* Free everything but the sk_buff shell. */
658static void skb_release_all(struct sk_buff *skb)
659{
660 skb_release_head_state(skb);
661 if (likely(skb->head))
662 skb_release_data(skb);
663}
664
665/**
666 * __kfree_skb - private function
667 * @skb: buffer
668 *
669 * Free an sk_buff. Release anything attached to the buffer.
670 * Clean the state. This is an internal helper function. Users should
671 * always call kfree_skb
672 */
673
674void __kfree_skb(struct sk_buff *skb)
675{
676 skb_release_all(skb);
677 kfree_skbmem(skb);
678}
679EXPORT_SYMBOL(__kfree_skb);
680
681/**
682 * kfree_skb - free an sk_buff
683 * @skb: buffer to free
684 *
685 * Drop a reference to the buffer and free it if the usage count has
686 * hit zero.
687 */
688void kfree_skb(struct sk_buff *skb)
689{
690 if (!skb_unref(skb))
691 return;
692
693 trace_kfree_skb(skb, __builtin_return_address(0));
694 __kfree_skb(skb);
695}
696EXPORT_SYMBOL(kfree_skb);
697
698void kfree_skb_list(struct sk_buff *segs)
699{
700 while (segs) {
701 struct sk_buff *next = segs->next;
702
703 kfree_skb(segs);
704 segs = next;
705 }
706}
707EXPORT_SYMBOL(kfree_skb_list);
708
709/**
710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
712 *
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
715 */
716void skb_tx_error(struct sk_buff *skb)
717{
718 skb_zcopy_clear(skb, true);
719}
720EXPORT_SYMBOL(skb_tx_error);
721
722/**
723 * consume_skb - free an skbuff
724 * @skb: buffer to free
725 *
726 * Drop a ref to the buffer and free it if the usage count has hit zero
727 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
728 * is being dropped after a failure and notes that
729 */
730void consume_skb(struct sk_buff *skb)
731{
732 if (!skb_unref(skb))
733 return;
734
735 trace_consume_skb(skb);
736 __kfree_skb(skb);
737}
738EXPORT_SYMBOL(consume_skb);
739
740/**
741 * consume_stateless_skb - free an skbuff, assuming it is stateless
742 * @skb: buffer to free
743 *
744 * Alike consume_skb(), but this variant assumes that this is the last
745 * skb reference and all the head states have been already dropped
746 */
747void __consume_stateless_skb(struct sk_buff *skb)
748{
749 trace_consume_skb(skb);
750 skb_release_data(skb);
751 kfree_skbmem(skb);
752}
753
754void __kfree_skb_flush(void)
755{
756 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
757
758 /* flush skb_cache if containing objects */
759 if (nc->skb_count) {
760 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
761 nc->skb_cache);
762 nc->skb_count = 0;
763 }
764}
765
766static inline void _kfree_skb_defer(struct sk_buff *skb)
767{
768 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
769
770 /* drop skb->head and call any destructors for packet */
771 skb_release_all(skb);
772
773 /* record skb to CPU local list */
774 nc->skb_cache[nc->skb_count++] = skb;
775
776#ifdef CONFIG_SLUB
777 /* SLUB writes into objects when freeing */
778 prefetchw(skb);
779#endif
780
781 /* flush skb_cache if it is filled */
782 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
783 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
784 nc->skb_cache);
785 nc->skb_count = 0;
786 }
787}
788void __kfree_skb_defer(struct sk_buff *skb)
789{
790 _kfree_skb_defer(skb);
791}
792
793void napi_consume_skb(struct sk_buff *skb, int budget)
794{
795 if (unlikely(!skb))
796 return;
797
798 /* Zero budget indicate non-NAPI context called us, like netpoll */
799 if (unlikely(!budget)) {
800 dev_consume_skb_any(skb);
801 return;
802 }
803
804 if (!skb_unref(skb))
805 return;
806
807 /* if reaching here SKB is ready to free */
808 trace_consume_skb(skb);
809
810 /* if SKB is a clone, don't handle this case */
811 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
812 __kfree_skb(skb);
813 return;
814 }
815
816 _kfree_skb_defer(skb);
817}
818EXPORT_SYMBOL(napi_consume_skb);
819
820/* Make sure a field is enclosed inside headers_start/headers_end section */
821#define CHECK_SKB_FIELD(field) \
822 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
823 offsetof(struct sk_buff, headers_start)); \
824 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
825 offsetof(struct sk_buff, headers_end)); \
826
827static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
828{
829 new->tstamp = old->tstamp;
830 /* We do not copy old->sk */
831 new->dev = old->dev;
832 memcpy(new->cb, old->cb, sizeof(old->cb));
833 skb_dst_copy(new, old);
834 __skb_ext_copy(new, old);
835 __nf_copy(new, old, false);
836
837 /* Note : this field could be in headers_start/headers_end section
838 * It is not yet because we do not want to have a 16 bit hole
839 */
840 new->queue_mapping = old->queue_mapping;
841
842 memcpy(&new->headers_start, &old->headers_start,
843 offsetof(struct sk_buff, headers_end) -
844 offsetof(struct sk_buff, headers_start));
845 CHECK_SKB_FIELD(protocol);
846 CHECK_SKB_FIELD(csum);
847 CHECK_SKB_FIELD(hash);
848 CHECK_SKB_FIELD(priority);
849 CHECK_SKB_FIELD(skb_iif);
850 CHECK_SKB_FIELD(vlan_proto);
851 CHECK_SKB_FIELD(vlan_tci);
852 CHECK_SKB_FIELD(transport_header);
853 CHECK_SKB_FIELD(network_header);
854 CHECK_SKB_FIELD(mac_header);
855 CHECK_SKB_FIELD(inner_protocol);
856 CHECK_SKB_FIELD(inner_transport_header);
857 CHECK_SKB_FIELD(inner_network_header);
858 CHECK_SKB_FIELD(inner_mac_header);
859 CHECK_SKB_FIELD(mark);
860#ifdef CONFIG_NETWORK_SECMARK
861 CHECK_SKB_FIELD(secmark);
862#endif
863#ifdef CONFIG_NET_RX_BUSY_POLL
864 CHECK_SKB_FIELD(napi_id);
865#endif
866#ifdef CONFIG_XPS
867 CHECK_SKB_FIELD(sender_cpu);
868#endif
869#ifdef CONFIG_NET_SCHED
870 CHECK_SKB_FIELD(tc_index);
871#endif
872
873}
874
875/*
876 * You should not add any new code to this function. Add it to
877 * __copy_skb_header above instead.
878 */
879static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
880{
881#define C(x) n->x = skb->x
882
883 n->next = n->prev = NULL;
884 n->sk = NULL;
885 __copy_skb_header(n, skb);
886
887 C(len);
888 C(data_len);
889 C(mac_len);
890 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
891 n->cloned = 1;
892 n->nohdr = 0;
893 n->peeked = 0;
894 C(pfmemalloc);
895 n->destructor = NULL;
896 C(tail);
897 C(end);
898 C(head);
899 C(head_frag);
900 C(data);
901 C(truesize);
902 refcount_set(&n->users, 1);
903
904 atomic_inc(&(skb_shinfo(skb)->dataref));
905 skb->cloned = 1;
906
907 return n;
908#undef C
909}
910
911/**
912 * skb_morph - morph one skb into another
913 * @dst: the skb to receive the contents
914 * @src: the skb to supply the contents
915 *
916 * This is identical to skb_clone except that the target skb is
917 * supplied by the user.
918 *
919 * The target skb is returned upon exit.
920 */
921struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
922{
923 skb_release_all(dst);
924 return __skb_clone(dst, src);
925}
926EXPORT_SYMBOL_GPL(skb_morph);
927
928int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
929{
930 unsigned long max_pg, num_pg, new_pg, old_pg;
931 struct user_struct *user;
932
933 if (capable(CAP_IPC_LOCK) || !size)
934 return 0;
935
936 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
937 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
938 user = mmp->user ? : current_user();
939
940 do {
941 old_pg = atomic_long_read(&user->locked_vm);
942 new_pg = old_pg + num_pg;
943 if (new_pg > max_pg)
944 return -ENOBUFS;
945 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
946 old_pg);
947
948 if (!mmp->user) {
949 mmp->user = get_uid(user);
950 mmp->num_pg = num_pg;
951 } else {
952 mmp->num_pg += num_pg;
953 }
954
955 return 0;
956}
957EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
958
959void mm_unaccount_pinned_pages(struct mmpin *mmp)
960{
961 if (mmp->user) {
962 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
963 free_uid(mmp->user);
964 }
965}
966EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
967
968struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
969{
970 struct ubuf_info *uarg;
971 struct sk_buff *skb;
972
973 WARN_ON_ONCE(!in_task());
974
975 skb = sock_omalloc(sk, 0, GFP_KERNEL);
976 if (!skb)
977 return NULL;
978
979 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
980 uarg = (void *)skb->cb;
981 uarg->mmp.user = NULL;
982
983 if (mm_account_pinned_pages(&uarg->mmp, size)) {
984 kfree_skb(skb);
985 return NULL;
986 }
987
988 uarg->callback = sock_zerocopy_callback;
989 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
990 uarg->len = 1;
991 uarg->bytelen = size;
992 uarg->zerocopy = 1;
993 refcount_set(&uarg->refcnt, 1);
994 sock_hold(sk);
995
996 return uarg;
997}
998EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
999
1000static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1001{
1002 return container_of((void *)uarg, struct sk_buff, cb);
1003}
1004
1005struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1006 struct ubuf_info *uarg)
1007{
1008 if (uarg) {
1009 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1010 u32 bytelen, next;
1011
1012 /* realloc only when socket is locked (TCP, UDP cork),
1013 * so uarg->len and sk_zckey access is serialized
1014 */
1015 if (!sock_owned_by_user(sk)) {
1016 WARN_ON_ONCE(1);
1017 return NULL;
1018 }
1019
1020 bytelen = uarg->bytelen + size;
1021 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1022 /* TCP can create new skb to attach new uarg */
1023 if (sk->sk_type == SOCK_STREAM)
1024 goto new_alloc;
1025 return NULL;
1026 }
1027
1028 next = (u32)atomic_read(&sk->sk_zckey);
1029 if ((u32)(uarg->id + uarg->len) == next) {
1030 if (mm_account_pinned_pages(&uarg->mmp, size))
1031 return NULL;
1032 uarg->len++;
1033 uarg->bytelen = bytelen;
1034 atomic_set(&sk->sk_zckey, ++next);
1035
1036 /* no extra ref when appending to datagram (MSG_MORE) */
1037 if (sk->sk_type == SOCK_STREAM)
1038 sock_zerocopy_get(uarg);
1039
1040 return uarg;
1041 }
1042 }
1043
1044new_alloc:
1045 return sock_zerocopy_alloc(sk, size);
1046}
1047EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1048
1049static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1050{
1051 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1052 u32 old_lo, old_hi;
1053 u64 sum_len;
1054
1055 old_lo = serr->ee.ee_info;
1056 old_hi = serr->ee.ee_data;
1057 sum_len = old_hi - old_lo + 1ULL + len;
1058
1059 if (sum_len >= (1ULL << 32))
1060 return false;
1061
1062 if (lo != old_hi + 1)
1063 return false;
1064
1065 serr->ee.ee_data += len;
1066 return true;
1067}
1068
1069void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1070{
1071 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1072 struct sock_exterr_skb *serr;
1073 struct sock *sk = skb->sk;
1074 struct sk_buff_head *q;
1075 unsigned long flags;
1076 u32 lo, hi;
1077 u16 len;
1078
1079 mm_unaccount_pinned_pages(&uarg->mmp);
1080
1081 /* if !len, there was only 1 call, and it was aborted
1082 * so do not queue a completion notification
1083 */
1084 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1085 goto release;
1086
1087 len = uarg->len;
1088 lo = uarg->id;
1089 hi = uarg->id + len - 1;
1090
1091 serr = SKB_EXT_ERR(skb);
1092 memset(serr, 0, sizeof(*serr));
1093 serr->ee.ee_errno = 0;
1094 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1095 serr->ee.ee_data = hi;
1096 serr->ee.ee_info = lo;
1097 if (!success)
1098 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1099
1100 q = &sk->sk_error_queue;
1101 spin_lock_irqsave(&q->lock, flags);
1102 tail = skb_peek_tail(q);
1103 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1104 !skb_zerocopy_notify_extend(tail, lo, len)) {
1105 __skb_queue_tail(q, skb);
1106 skb = NULL;
1107 }
1108 spin_unlock_irqrestore(&q->lock, flags);
1109
1110 sk->sk_error_report(sk);
1111
1112release:
1113 consume_skb(skb);
1114 sock_put(sk);
1115}
1116EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1117
1118void sock_zerocopy_put(struct ubuf_info *uarg)
1119{
1120 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
1121 if (uarg->callback)
1122 uarg->callback(uarg, uarg->zerocopy);
1123 else
1124 consume_skb(skb_from_uarg(uarg));
1125 }
1126}
1127EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1128
1129void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1130{
1131 if (uarg) {
1132 struct sock *sk = skb_from_uarg(uarg)->sk;
1133
1134 atomic_dec(&sk->sk_zckey);
1135 uarg->len--;
1136
1137 if (have_uref)
1138 sock_zerocopy_put(uarg);
1139 }
1140}
1141EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1142
1143int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1144{
1145 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1146}
1147EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1148
1149int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1150 struct msghdr *msg, int len,
1151 struct ubuf_info *uarg)
1152{
1153 struct ubuf_info *orig_uarg = skb_zcopy(skb);
1154 struct iov_iter orig_iter = msg->msg_iter;
1155 int err, orig_len = skb->len;
1156
1157 /* An skb can only point to one uarg. This edge case happens when
1158 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1159 */
1160 if (orig_uarg && uarg != orig_uarg)
1161 return -EEXIST;
1162
1163 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1164 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1165 struct sock *save_sk = skb->sk;
1166
1167 /* Streams do not free skb on error. Reset to prev state. */
1168 msg->msg_iter = orig_iter;
1169 skb->sk = sk;
1170 ___pskb_trim(skb, orig_len);
1171 skb->sk = save_sk;
1172 return err;
1173 }
1174
1175 skb_zcopy_set(skb, uarg, NULL);
1176 return skb->len - orig_len;
1177}
1178EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1179
1180static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1181 gfp_t gfp_mask)
1182{
1183 if (skb_zcopy(orig)) {
1184 if (skb_zcopy(nskb)) {
1185 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1186 if (!gfp_mask) {
1187 WARN_ON_ONCE(1);
1188 return -ENOMEM;
1189 }
1190 if (skb_uarg(nskb) == skb_uarg(orig))
1191 return 0;
1192 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1193 return -EIO;
1194 }
1195 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1196 }
1197 return 0;
1198}
1199
1200/**
1201 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1202 * @skb: the skb to modify
1203 * @gfp_mask: allocation priority
1204 *
1205 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1206 * It will copy all frags into kernel and drop the reference
1207 * to userspace pages.
1208 *
1209 * If this function is called from an interrupt gfp_mask() must be
1210 * %GFP_ATOMIC.
1211 *
1212 * Returns 0 on success or a negative error code on failure
1213 * to allocate kernel memory to copy to.
1214 */
1215int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1216{
1217 int num_frags = skb_shinfo(skb)->nr_frags;
1218 struct page *page, *head = NULL;
1219 int i, new_frags;
1220 u32 d_off;
1221
1222 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1223 return -EINVAL;
1224
1225 if (!num_frags)
1226 goto release;
1227
1228 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1229 for (i = 0; i < new_frags; i++) {
1230 page = alloc_page(gfp_mask);
1231 if (!page) {
1232 while (head) {
1233 struct page *next = (struct page *)page_private(head);
1234 put_page(head);
1235 head = next;
1236 }
1237 return -ENOMEM;
1238 }
1239 set_page_private(page, (unsigned long)head);
1240 head = page;
1241 }
1242
1243 page = head;
1244 d_off = 0;
1245 for (i = 0; i < num_frags; i++) {
1246 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1247 u32 p_off, p_len, copied;
1248 struct page *p;
1249 u8 *vaddr;
1250
1251 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1252 p, p_off, p_len, copied) {
1253 u32 copy, done = 0;
1254 vaddr = kmap_atomic(p);
1255
1256 while (done < p_len) {
1257 if (d_off == PAGE_SIZE) {
1258 d_off = 0;
1259 page = (struct page *)page_private(page);
1260 }
1261 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1262 memcpy(page_address(page) + d_off,
1263 vaddr + p_off + done, copy);
1264 done += copy;
1265 d_off += copy;
1266 }
1267 kunmap_atomic(vaddr);
1268 }
1269 }
1270
1271 /* skb frags release userspace buffers */
1272 for (i = 0; i < num_frags; i++)
1273 skb_frag_unref(skb, i);
1274
1275 /* skb frags point to kernel buffers */
1276 for (i = 0; i < new_frags - 1; i++) {
1277 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1278 head = (struct page *)page_private(head);
1279 }
1280 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1281 skb_shinfo(skb)->nr_frags = new_frags;
1282
1283release:
1284 skb_zcopy_clear(skb, false);
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1288
1289/**
1290 * skb_clone - duplicate an sk_buff
1291 * @skb: buffer to clone
1292 * @gfp_mask: allocation priority
1293 *
1294 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1295 * copies share the same packet data but not structure. The new
1296 * buffer has a reference count of 1. If the allocation fails the
1297 * function returns %NULL otherwise the new buffer is returned.
1298 *
1299 * If this function is called from an interrupt gfp_mask() must be
1300 * %GFP_ATOMIC.
1301 */
1302
1303struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1304{
1305 struct sk_buff_fclones *fclones = container_of(skb,
1306 struct sk_buff_fclones,
1307 skb1);
1308 struct sk_buff *n;
1309
1310 if (skb_orphan_frags(skb, gfp_mask))
1311 return NULL;
1312
1313 if (skb->fclone == SKB_FCLONE_ORIG &&
1314 refcount_read(&fclones->fclone_ref) == 1) {
1315 n = &fclones->skb2;
1316 refcount_set(&fclones->fclone_ref, 2);
1317 } else {
1318 if (skb_pfmemalloc(skb))
1319 gfp_mask |= __GFP_MEMALLOC;
1320
1321 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1322 if (!n)
1323 return NULL;
1324
1325 n->fclone = SKB_FCLONE_UNAVAILABLE;
1326 }
1327
1328 return __skb_clone(n, skb);
1329}
1330EXPORT_SYMBOL(skb_clone);
1331
1332void skb_headers_offset_update(struct sk_buff *skb, int off)
1333{
1334 /* Only adjust this if it actually is csum_start rather than csum */
1335 if (skb->ip_summed == CHECKSUM_PARTIAL)
1336 skb->csum_start += off;
1337 /* {transport,network,mac}_header and tail are relative to skb->head */
1338 skb->transport_header += off;
1339 skb->network_header += off;
1340 if (skb_mac_header_was_set(skb))
1341 skb->mac_header += off;
1342 skb->inner_transport_header += off;
1343 skb->inner_network_header += off;
1344 skb->inner_mac_header += off;
1345}
1346EXPORT_SYMBOL(skb_headers_offset_update);
1347
1348void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1349{
1350 __copy_skb_header(new, old);
1351
1352 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1353 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1354 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1355}
1356EXPORT_SYMBOL(skb_copy_header);
1357
1358static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1359{
1360 if (skb_pfmemalloc(skb))
1361 return SKB_ALLOC_RX;
1362 return 0;
1363}
1364
1365/**
1366 * skb_copy - create private copy of an sk_buff
1367 * @skb: buffer to copy
1368 * @gfp_mask: allocation priority
1369 *
1370 * Make a copy of both an &sk_buff and its data. This is used when the
1371 * caller wishes to modify the data and needs a private copy of the
1372 * data to alter. Returns %NULL on failure or the pointer to the buffer
1373 * on success. The returned buffer has a reference count of 1.
1374 *
1375 * As by-product this function converts non-linear &sk_buff to linear
1376 * one, so that &sk_buff becomes completely private and caller is allowed
1377 * to modify all the data of returned buffer. This means that this
1378 * function is not recommended for use in circumstances when only
1379 * header is going to be modified. Use pskb_copy() instead.
1380 */
1381
1382struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1383{
1384 int headerlen = skb_headroom(skb);
1385 unsigned int size = skb_end_offset(skb) + skb->data_len;
1386 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1387 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1388
1389 if (!n)
1390 return NULL;
1391
1392 /* Set the data pointer */
1393 skb_reserve(n, headerlen);
1394 /* Set the tail pointer and length */
1395 skb_put(n, skb->len);
1396
1397 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1398
1399 skb_copy_header(n, skb);
1400 return n;
1401}
1402EXPORT_SYMBOL(skb_copy);
1403
1404/**
1405 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1406 * @skb: buffer to copy
1407 * @headroom: headroom of new skb
1408 * @gfp_mask: allocation priority
1409 * @fclone: if true allocate the copy of the skb from the fclone
1410 * cache instead of the head cache; it is recommended to set this
1411 * to true for the cases where the copy will likely be cloned
1412 *
1413 * Make a copy of both an &sk_buff and part of its data, located
1414 * in header. Fragmented data remain shared. This is used when
1415 * the caller wishes to modify only header of &sk_buff and needs
1416 * private copy of the header to alter. Returns %NULL on failure
1417 * or the pointer to the buffer on success.
1418 * The returned buffer has a reference count of 1.
1419 */
1420
1421struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1422 gfp_t gfp_mask, bool fclone)
1423{
1424 unsigned int size = skb_headlen(skb) + headroom;
1425 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1426 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1427
1428 if (!n)
1429 goto out;
1430
1431 /* Set the data pointer */
1432 skb_reserve(n, headroom);
1433 /* Set the tail pointer and length */
1434 skb_put(n, skb_headlen(skb));
1435 /* Copy the bytes */
1436 skb_copy_from_linear_data(skb, n->data, n->len);
1437
1438 n->truesize += skb->data_len;
1439 n->data_len = skb->data_len;
1440 n->len = skb->len;
1441
1442 if (skb_shinfo(skb)->nr_frags) {
1443 int i;
1444
1445 if (skb_orphan_frags(skb, gfp_mask) ||
1446 skb_zerocopy_clone(n, skb, gfp_mask)) {
1447 kfree_skb(n);
1448 n = NULL;
1449 goto out;
1450 }
1451 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1452 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1453 skb_frag_ref(skb, i);
1454 }
1455 skb_shinfo(n)->nr_frags = i;
1456 }
1457
1458 if (skb_has_frag_list(skb)) {
1459 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1460 skb_clone_fraglist(n);
1461 }
1462
1463 skb_copy_header(n, skb);
1464out:
1465 return n;
1466}
1467EXPORT_SYMBOL(__pskb_copy_fclone);
1468
1469/**
1470 * pskb_expand_head - reallocate header of &sk_buff
1471 * @skb: buffer to reallocate
1472 * @nhead: room to add at head
1473 * @ntail: room to add at tail
1474 * @gfp_mask: allocation priority
1475 *
1476 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1477 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1478 * reference count of 1. Returns zero in the case of success or error,
1479 * if expansion failed. In the last case, &sk_buff is not changed.
1480 *
1481 * All the pointers pointing into skb header may change and must be
1482 * reloaded after call to this function.
1483 */
1484
1485int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1486 gfp_t gfp_mask)
1487{
1488 int i, osize = skb_end_offset(skb);
1489 int size = osize + nhead + ntail;
1490 long off;
1491 u8 *data;
1492
1493 BUG_ON(nhead < 0);
1494
1495 BUG_ON(skb_shared(skb));
1496
1497 size = SKB_DATA_ALIGN(size);
1498
1499 if (skb_pfmemalloc(skb))
1500 gfp_mask |= __GFP_MEMALLOC;
1501 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1502 gfp_mask, NUMA_NO_NODE, NULL);
1503 if (!data)
1504 goto nodata;
1505 size = SKB_WITH_OVERHEAD(ksize(data));
1506
1507 /* Copy only real data... and, alas, header. This should be
1508 * optimized for the cases when header is void.
1509 */
1510 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1511
1512 memcpy((struct skb_shared_info *)(data + size),
1513 skb_shinfo(skb),
1514 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1515
1516 /*
1517 * if shinfo is shared we must drop the old head gracefully, but if it
1518 * is not we can just drop the old head and let the existing refcount
1519 * be since all we did is relocate the values
1520 */
1521 if (skb_cloned(skb)) {
1522 if (skb_orphan_frags(skb, gfp_mask))
1523 goto nofrags;
1524 if (skb_zcopy(skb))
1525 refcount_inc(&skb_uarg(skb)->refcnt);
1526 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1527 skb_frag_ref(skb, i);
1528
1529 if (skb_has_frag_list(skb))
1530 skb_clone_fraglist(skb);
1531
1532 skb_release_data(skb);
1533 } else {
1534 skb_free_head(skb);
1535 }
1536 off = (data + nhead) - skb->head;
1537
1538 skb->head = data;
1539 skb->head_frag = 0;
1540 skb->data += off;
1541#ifdef NET_SKBUFF_DATA_USES_OFFSET
1542 skb->end = size;
1543 off = nhead;
1544#else
1545 skb->end = skb->head + size;
1546#endif
1547 skb->tail += off;
1548 skb_headers_offset_update(skb, nhead);
1549 skb->cloned = 0;
1550 skb->hdr_len = 0;
1551 skb->nohdr = 0;
1552 atomic_set(&skb_shinfo(skb)->dataref, 1);
1553
1554 skb_metadata_clear(skb);
1555
1556 /* It is not generally safe to change skb->truesize.
1557 * For the moment, we really care of rx path, or
1558 * when skb is orphaned (not attached to a socket).
1559 */
1560 if (!skb->sk || skb->destructor == sock_edemux)
1561 skb->truesize += size - osize;
1562
1563 return 0;
1564
1565nofrags:
1566 kfree(data);
1567nodata:
1568 return -ENOMEM;
1569}
1570EXPORT_SYMBOL(pskb_expand_head);
1571
1572/* Make private copy of skb with writable head and some headroom */
1573
1574struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1575{
1576 struct sk_buff *skb2;
1577 int delta = headroom - skb_headroom(skb);
1578
1579 if (delta <= 0)
1580 skb2 = pskb_copy(skb, GFP_ATOMIC);
1581 else {
1582 skb2 = skb_clone(skb, GFP_ATOMIC);
1583 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1584 GFP_ATOMIC)) {
1585 kfree_skb(skb2);
1586 skb2 = NULL;
1587 }
1588 }
1589 return skb2;
1590}
1591EXPORT_SYMBOL(skb_realloc_headroom);
1592
1593/**
1594 * skb_copy_expand - copy and expand sk_buff
1595 * @skb: buffer to copy
1596 * @newheadroom: new free bytes at head
1597 * @newtailroom: new free bytes at tail
1598 * @gfp_mask: allocation priority
1599 *
1600 * Make a copy of both an &sk_buff and its data and while doing so
1601 * allocate additional space.
1602 *
1603 * This is used when the caller wishes to modify the data and needs a
1604 * private copy of the data to alter as well as more space for new fields.
1605 * Returns %NULL on failure or the pointer to the buffer
1606 * on success. The returned buffer has a reference count of 1.
1607 *
1608 * You must pass %GFP_ATOMIC as the allocation priority if this function
1609 * is called from an interrupt.
1610 */
1611struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1612 int newheadroom, int newtailroom,
1613 gfp_t gfp_mask)
1614{
1615 /*
1616 * Allocate the copy buffer
1617 */
1618 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1619 gfp_mask, skb_alloc_rx_flag(skb),
1620 NUMA_NO_NODE);
1621 int oldheadroom = skb_headroom(skb);
1622 int head_copy_len, head_copy_off;
1623
1624 if (!n)
1625 return NULL;
1626
1627 skb_reserve(n, newheadroom);
1628
1629 /* Set the tail pointer and length */
1630 skb_put(n, skb->len);
1631
1632 head_copy_len = oldheadroom;
1633 head_copy_off = 0;
1634 if (newheadroom <= head_copy_len)
1635 head_copy_len = newheadroom;
1636 else
1637 head_copy_off = newheadroom - head_copy_len;
1638
1639 /* Copy the linear header and data. */
1640 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1641 skb->len + head_copy_len));
1642
1643 skb_copy_header(n, skb);
1644
1645 skb_headers_offset_update(n, newheadroom - oldheadroom);
1646
1647 return n;
1648}
1649EXPORT_SYMBOL(skb_copy_expand);
1650
1651/**
1652 * __skb_pad - zero pad the tail of an skb
1653 * @skb: buffer to pad
1654 * @pad: space to pad
1655 * @free_on_error: free buffer on error
1656 *
1657 * Ensure that a buffer is followed by a padding area that is zero
1658 * filled. Used by network drivers which may DMA or transfer data
1659 * beyond the buffer end onto the wire.
1660 *
1661 * May return error in out of memory cases. The skb is freed on error
1662 * if @free_on_error is true.
1663 */
1664
1665int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
1666{
1667 int err;
1668 int ntail;
1669
1670 /* If the skbuff is non linear tailroom is always zero.. */
1671 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1672 memset(skb->data+skb->len, 0, pad);
1673 return 0;
1674 }
1675
1676 ntail = skb->data_len + pad - (skb->end - skb->tail);
1677 if (likely(skb_cloned(skb) || ntail > 0)) {
1678 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1679 if (unlikely(err))
1680 goto free_skb;
1681 }
1682
1683 /* FIXME: The use of this function with non-linear skb's really needs
1684 * to be audited.
1685 */
1686 err = skb_linearize(skb);
1687 if (unlikely(err))
1688 goto free_skb;
1689
1690 memset(skb->data + skb->len, 0, pad);
1691 return 0;
1692
1693free_skb:
1694 if (free_on_error)
1695 kfree_skb(skb);
1696 return err;
1697}
1698EXPORT_SYMBOL(__skb_pad);
1699
1700/**
1701 * pskb_put - add data to the tail of a potentially fragmented buffer
1702 * @skb: start of the buffer to use
1703 * @tail: tail fragment of the buffer to use
1704 * @len: amount of data to add
1705 *
1706 * This function extends the used data area of the potentially
1707 * fragmented buffer. @tail must be the last fragment of @skb -- or
1708 * @skb itself. If this would exceed the total buffer size the kernel
1709 * will panic. A pointer to the first byte of the extra data is
1710 * returned.
1711 */
1712
1713void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1714{
1715 if (tail != skb) {
1716 skb->data_len += len;
1717 skb->len += len;
1718 }
1719 return skb_put(tail, len);
1720}
1721EXPORT_SYMBOL_GPL(pskb_put);
1722
1723/**
1724 * skb_put - add data to a buffer
1725 * @skb: buffer to use
1726 * @len: amount of data to add
1727 *
1728 * This function extends the used data area of the buffer. If this would
1729 * exceed the total buffer size the kernel will panic. A pointer to the
1730 * first byte of the extra data is returned.
1731 */
1732void *skb_put(struct sk_buff *skb, unsigned int len)
1733{
1734 void *tmp = skb_tail_pointer(skb);
1735 SKB_LINEAR_ASSERT(skb);
1736 skb->tail += len;
1737 skb->len += len;
1738 if (unlikely(skb->tail > skb->end))
1739 skb_over_panic(skb, len, __builtin_return_address(0));
1740 return tmp;
1741}
1742EXPORT_SYMBOL(skb_put);
1743
1744/**
1745 * skb_push - add data to the start of a buffer
1746 * @skb: buffer to use
1747 * @len: amount of data to add
1748 *
1749 * This function extends the used data area of the buffer at the buffer
1750 * start. If this would exceed the total buffer headroom the kernel will
1751 * panic. A pointer to the first byte of the extra data is returned.
1752 */
1753void *skb_push(struct sk_buff *skb, unsigned int len)
1754{
1755 skb->data -= len;
1756 skb->len += len;
1757 if (unlikely(skb->data < skb->head))
1758 skb_under_panic(skb, len, __builtin_return_address(0));
1759 return skb->data;
1760}
1761EXPORT_SYMBOL(skb_push);
1762
1763/**
1764 * skb_pull - remove data from the start of a buffer
1765 * @skb: buffer to use
1766 * @len: amount of data to remove
1767 *
1768 * This function removes data from the start of a buffer, returning
1769 * the memory to the headroom. A pointer to the next data in the buffer
1770 * is returned. Once the data has been pulled future pushes will overwrite
1771 * the old data.
1772 */
1773void *skb_pull(struct sk_buff *skb, unsigned int len)
1774{
1775 return skb_pull_inline(skb, len);
1776}
1777EXPORT_SYMBOL(skb_pull);
1778
1779/**
1780 * skb_trim - remove end from a buffer
1781 * @skb: buffer to alter
1782 * @len: new length
1783 *
1784 * Cut the length of a buffer down by removing data from the tail. If
1785 * the buffer is already under the length specified it is not modified.
1786 * The skb must be linear.
1787 */
1788void skb_trim(struct sk_buff *skb, unsigned int len)
1789{
1790 if (skb->len > len)
1791 __skb_trim(skb, len);
1792}
1793EXPORT_SYMBOL(skb_trim);
1794
1795/* Trims skb to length len. It can change skb pointers.
1796 */
1797
1798int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1799{
1800 struct sk_buff **fragp;
1801 struct sk_buff *frag;
1802 int offset = skb_headlen(skb);
1803 int nfrags = skb_shinfo(skb)->nr_frags;
1804 int i;
1805 int err;
1806
1807 if (skb_cloned(skb) &&
1808 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1809 return err;
1810
1811 i = 0;
1812 if (offset >= len)
1813 goto drop_pages;
1814
1815 for (; i < nfrags; i++) {
1816 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1817
1818 if (end < len) {
1819 offset = end;
1820 continue;
1821 }
1822
1823 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1824
1825drop_pages:
1826 skb_shinfo(skb)->nr_frags = i;
1827
1828 for (; i < nfrags; i++)
1829 skb_frag_unref(skb, i);
1830
1831 if (skb_has_frag_list(skb))
1832 skb_drop_fraglist(skb);
1833 goto done;
1834 }
1835
1836 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1837 fragp = &frag->next) {
1838 int end = offset + frag->len;
1839
1840 if (skb_shared(frag)) {
1841 struct sk_buff *nfrag;
1842
1843 nfrag = skb_clone(frag, GFP_ATOMIC);
1844 if (unlikely(!nfrag))
1845 return -ENOMEM;
1846
1847 nfrag->next = frag->next;
1848 consume_skb(frag);
1849 frag = nfrag;
1850 *fragp = frag;
1851 }
1852
1853 if (end < len) {
1854 offset = end;
1855 continue;
1856 }
1857
1858 if (end > len &&
1859 unlikely((err = pskb_trim(frag, len - offset))))
1860 return err;
1861
1862 if (frag->next)
1863 skb_drop_list(&frag->next);
1864 break;
1865 }
1866
1867done:
1868 if (len > skb_headlen(skb)) {
1869 skb->data_len -= skb->len - len;
1870 skb->len = len;
1871 } else {
1872 skb->len = len;
1873 skb->data_len = 0;
1874 skb_set_tail_pointer(skb, len);
1875 }
1876
1877 if (!skb->sk || skb->destructor == sock_edemux)
1878 skb_condense(skb);
1879 return 0;
1880}
1881EXPORT_SYMBOL(___pskb_trim);
1882
1883/* Note : use pskb_trim_rcsum() instead of calling this directly
1884 */
1885int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1886{
1887 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1888 int delta = skb->len - len;
1889
1890 skb->csum = csum_block_sub(skb->csum,
1891 skb_checksum(skb, len, delta, 0),
1892 len);
1893 }
1894 return __pskb_trim(skb, len);
1895}
1896EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1897
1898/**
1899 * __pskb_pull_tail - advance tail of skb header
1900 * @skb: buffer to reallocate
1901 * @delta: number of bytes to advance tail
1902 *
1903 * The function makes a sense only on a fragmented &sk_buff,
1904 * it expands header moving its tail forward and copying necessary
1905 * data from fragmented part.
1906 *
1907 * &sk_buff MUST have reference count of 1.
1908 *
1909 * Returns %NULL (and &sk_buff does not change) if pull failed
1910 * or value of new tail of skb in the case of success.
1911 *
1912 * All the pointers pointing into skb header may change and must be
1913 * reloaded after call to this function.
1914 */
1915
1916/* Moves tail of skb head forward, copying data from fragmented part,
1917 * when it is necessary.
1918 * 1. It may fail due to malloc failure.
1919 * 2. It may change skb pointers.
1920 *
1921 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1922 */
1923void *__pskb_pull_tail(struct sk_buff *skb, int delta)
1924{
1925 /* If skb has not enough free space at tail, get new one
1926 * plus 128 bytes for future expansions. If we have enough
1927 * room at tail, reallocate without expansion only if skb is cloned.
1928 */
1929 int i, k, eat = (skb->tail + delta) - skb->end;
1930
1931 if (eat > 0 || skb_cloned(skb)) {
1932 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1933 GFP_ATOMIC))
1934 return NULL;
1935 }
1936
1937 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1938 skb_tail_pointer(skb), delta));
1939
1940 /* Optimization: no fragments, no reasons to preestimate
1941 * size of pulled pages. Superb.
1942 */
1943 if (!skb_has_frag_list(skb))
1944 goto pull_pages;
1945
1946 /* Estimate size of pulled pages. */
1947 eat = delta;
1948 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1949 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1950
1951 if (size >= eat)
1952 goto pull_pages;
1953 eat -= size;
1954 }
1955
1956 /* If we need update frag list, we are in troubles.
1957 * Certainly, it is possible to add an offset to skb data,
1958 * but taking into account that pulling is expected to
1959 * be very rare operation, it is worth to fight against
1960 * further bloating skb head and crucify ourselves here instead.
1961 * Pure masohism, indeed. 8)8)
1962 */
1963 if (eat) {
1964 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1965 struct sk_buff *clone = NULL;
1966 struct sk_buff *insp = NULL;
1967
1968 do {
1969 if (list->len <= eat) {
1970 /* Eaten as whole. */
1971 eat -= list->len;
1972 list = list->next;
1973 insp = list;
1974 } else {
1975 /* Eaten partially. */
1976
1977 if (skb_shared(list)) {
1978 /* Sucks! We need to fork list. :-( */
1979 clone = skb_clone(list, GFP_ATOMIC);
1980 if (!clone)
1981 return NULL;
1982 insp = list->next;
1983 list = clone;
1984 } else {
1985 /* This may be pulled without
1986 * problems. */
1987 insp = list;
1988 }
1989 if (!pskb_pull(list, eat)) {
1990 kfree_skb(clone);
1991 return NULL;
1992 }
1993 break;
1994 }
1995 } while (eat);
1996
1997 /* Free pulled out fragments. */
1998 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1999 skb_shinfo(skb)->frag_list = list->next;
2000 kfree_skb(list);
2001 }
2002 /* And insert new clone at head. */
2003 if (clone) {
2004 clone->next = list;
2005 skb_shinfo(skb)->frag_list = clone;
2006 }
2007 }
2008 /* Success! Now we may commit changes to skb data. */
2009
2010pull_pages:
2011 eat = delta;
2012 k = 0;
2013 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2014 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2015
2016 if (size <= eat) {
2017 skb_frag_unref(skb, i);
2018 eat -= size;
2019 } else {
2020 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
2021 if (eat) {
2022 skb_shinfo(skb)->frags[k].page_offset += eat;
2023 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
2024 if (!i)
2025 goto end;
2026 eat = 0;
2027 }
2028 k++;
2029 }
2030 }
2031 skb_shinfo(skb)->nr_frags = k;
2032
2033end:
2034 skb->tail += delta;
2035 skb->data_len -= delta;
2036
2037 if (!skb->data_len)
2038 skb_zcopy_clear(skb, false);
2039
2040 return skb_tail_pointer(skb);
2041}
2042EXPORT_SYMBOL(__pskb_pull_tail);
2043
2044/**
2045 * skb_copy_bits - copy bits from skb to kernel buffer
2046 * @skb: source skb
2047 * @offset: offset in source
2048 * @to: destination buffer
2049 * @len: number of bytes to copy
2050 *
2051 * Copy the specified number of bytes from the source skb to the
2052 * destination buffer.
2053 *
2054 * CAUTION ! :
2055 * If its prototype is ever changed,
2056 * check arch/{*}/net/{*}.S files,
2057 * since it is called from BPF assembly code.
2058 */
2059int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2060{
2061 int start = skb_headlen(skb);
2062 struct sk_buff *frag_iter;
2063 int i, copy;
2064
2065 if (offset > (int)skb->len - len)
2066 goto fault;
2067
2068 /* Copy header. */
2069 if ((copy = start - offset) > 0) {
2070 if (copy > len)
2071 copy = len;
2072 skb_copy_from_linear_data_offset(skb, offset, to, copy);
2073 if ((len -= copy) == 0)
2074 return 0;
2075 offset += copy;
2076 to += copy;
2077 }
2078
2079 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2080 int end;
2081 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2082
2083 WARN_ON(start > offset + len);
2084
2085 end = start + skb_frag_size(f);
2086 if ((copy = end - offset) > 0) {
2087 u32 p_off, p_len, copied;
2088 struct page *p;
2089 u8 *vaddr;
2090
2091 if (copy > len)
2092 copy = len;
2093
2094 skb_frag_foreach_page(f,
2095 f->page_offset + offset - start,
2096 copy, p, p_off, p_len, copied) {
2097 vaddr = kmap_atomic(p);
2098 memcpy(to + copied, vaddr + p_off, p_len);
2099 kunmap_atomic(vaddr);
2100 }
2101
2102 if ((len -= copy) == 0)
2103 return 0;
2104 offset += copy;
2105 to += copy;
2106 }
2107 start = end;
2108 }
2109
2110 skb_walk_frags(skb, frag_iter) {
2111 int end;
2112
2113 WARN_ON(start > offset + len);
2114
2115 end = start + frag_iter->len;
2116 if ((copy = end - offset) > 0) {
2117 if (copy > len)
2118 copy = len;
2119 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2120 goto fault;
2121 if ((len -= copy) == 0)
2122 return 0;
2123 offset += copy;
2124 to += copy;
2125 }
2126 start = end;
2127 }
2128
2129 if (!len)
2130 return 0;
2131
2132fault:
2133 return -EFAULT;
2134}
2135EXPORT_SYMBOL(skb_copy_bits);
2136
2137/*
2138 * Callback from splice_to_pipe(), if we need to release some pages
2139 * at the end of the spd in case we error'ed out in filling the pipe.
2140 */
2141static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2142{
2143 put_page(spd->pages[i]);
2144}
2145
2146static struct page *linear_to_page(struct page *page, unsigned int *len,
2147 unsigned int *offset,
2148 struct sock *sk)
2149{
2150 struct page_frag *pfrag = sk_page_frag(sk);
2151
2152 if (!sk_page_frag_refill(sk, pfrag))
2153 return NULL;
2154
2155 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2156
2157 memcpy(page_address(pfrag->page) + pfrag->offset,
2158 page_address(page) + *offset, *len);
2159 *offset = pfrag->offset;
2160 pfrag->offset += *len;
2161
2162 return pfrag->page;
2163}
2164
2165static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2166 struct page *page,
2167 unsigned int offset)
2168{
2169 return spd->nr_pages &&
2170 spd->pages[spd->nr_pages - 1] == page &&
2171 (spd->partial[spd->nr_pages - 1].offset +
2172 spd->partial[spd->nr_pages - 1].len == offset);
2173}
2174
2175/*
2176 * Fill page/offset/length into spd, if it can hold more pages.
2177 */
2178static bool spd_fill_page(struct splice_pipe_desc *spd,
2179 struct pipe_inode_info *pipe, struct page *page,
2180 unsigned int *len, unsigned int offset,
2181 bool linear,
2182 struct sock *sk)
2183{
2184 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2185 return true;
2186
2187 if (linear) {
2188 page = linear_to_page(page, len, &offset, sk);
2189 if (!page)
2190 return true;
2191 }
2192 if (spd_can_coalesce(spd, page, offset)) {
2193 spd->partial[spd->nr_pages - 1].len += *len;
2194 return false;
2195 }
2196 get_page(page);
2197 spd->pages[spd->nr_pages] = page;
2198 spd->partial[spd->nr_pages].len = *len;
2199 spd->partial[spd->nr_pages].offset = offset;
2200 spd->nr_pages++;
2201
2202 return false;
2203}
2204
2205static bool __splice_segment(struct page *page, unsigned int poff,
2206 unsigned int plen, unsigned int *off,
2207 unsigned int *len,
2208 struct splice_pipe_desc *spd, bool linear,
2209 struct sock *sk,
2210 struct pipe_inode_info *pipe)
2211{
2212 if (!*len)
2213 return true;
2214
2215 /* skip this segment if already processed */
2216 if (*off >= plen) {
2217 *off -= plen;
2218 return false;
2219 }
2220
2221 /* ignore any bits we already processed */
2222 poff += *off;
2223 plen -= *off;
2224 *off = 0;
2225
2226 do {
2227 unsigned int flen = min(*len, plen);
2228
2229 if (spd_fill_page(spd, pipe, page, &flen, poff,
2230 linear, sk))
2231 return true;
2232 poff += flen;
2233 plen -= flen;
2234 *len -= flen;
2235 } while (*len && plen);
2236
2237 return false;
2238}
2239
2240/*
2241 * Map linear and fragment data from the skb to spd. It reports true if the
2242 * pipe is full or if we already spliced the requested length.
2243 */
2244static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2245 unsigned int *offset, unsigned int *len,
2246 struct splice_pipe_desc *spd, struct sock *sk)
2247{
2248 int seg;
2249 struct sk_buff *iter;
2250
2251 /* map the linear part :
2252 * If skb->head_frag is set, this 'linear' part is backed by a
2253 * fragment, and if the head is not shared with any clones then
2254 * we can avoid a copy since we own the head portion of this page.
2255 */
2256 if (__splice_segment(virt_to_page(skb->data),
2257 (unsigned long) skb->data & (PAGE_SIZE - 1),
2258 skb_headlen(skb),
2259 offset, len, spd,
2260 skb_head_is_locked(skb),
2261 sk, pipe))
2262 return true;
2263
2264 /*
2265 * then map the fragments
2266 */
2267 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2268 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2269
2270 if (__splice_segment(skb_frag_page(f),
2271 f->page_offset, skb_frag_size(f),
2272 offset, len, spd, false, sk, pipe))
2273 return true;
2274 }
2275
2276 skb_walk_frags(skb, iter) {
2277 if (*offset >= iter->len) {
2278 *offset -= iter->len;
2279 continue;
2280 }
2281 /* __skb_splice_bits() only fails if the output has no room
2282 * left, so no point in going over the frag_list for the error
2283 * case.
2284 */
2285 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2286 return true;
2287 }
2288
2289 return false;
2290}
2291
2292/*
2293 * Map data from the skb to a pipe. Should handle both the linear part,
2294 * the fragments, and the frag list.
2295 */
2296int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2297 struct pipe_inode_info *pipe, unsigned int tlen,
2298 unsigned int flags)
2299{
2300 struct partial_page partial[MAX_SKB_FRAGS];
2301 struct page *pages[MAX_SKB_FRAGS];
2302 struct splice_pipe_desc spd = {
2303 .pages = pages,
2304 .partial = partial,
2305 .nr_pages_max = MAX_SKB_FRAGS,
2306 .ops = &nosteal_pipe_buf_ops,
2307 .spd_release = sock_spd_release,
2308 };
2309 int ret = 0;
2310
2311 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2312
2313 if (spd.nr_pages)
2314 ret = splice_to_pipe(pipe, &spd);
2315
2316 return ret;
2317}
2318EXPORT_SYMBOL_GPL(skb_splice_bits);
2319
2320/* Send skb data on a socket. Socket must be locked. */
2321int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2322 int len)
2323{
2324 unsigned int orig_len = len;
2325 struct sk_buff *head = skb;
2326 unsigned short fragidx;
2327 int slen, ret;
2328
2329do_frag_list:
2330
2331 /* Deal with head data */
2332 while (offset < skb_headlen(skb) && len) {
2333 struct kvec kv;
2334 struct msghdr msg;
2335
2336 slen = min_t(int, len, skb_headlen(skb) - offset);
2337 kv.iov_base = skb->data + offset;
2338 kv.iov_len = slen;
2339 memset(&msg, 0, sizeof(msg));
2340 msg.msg_flags = MSG_DONTWAIT;
2341
2342 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2343 if (ret <= 0)
2344 goto error;
2345
2346 offset += ret;
2347 len -= ret;
2348 }
2349
2350 /* All the data was skb head? */
2351 if (!len)
2352 goto out;
2353
2354 /* Make offset relative to start of frags */
2355 offset -= skb_headlen(skb);
2356
2357 /* Find where we are in frag list */
2358 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2359 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2360
2361 if (offset < frag->size)
2362 break;
2363
2364 offset -= frag->size;
2365 }
2366
2367 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2368 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2369
2370 slen = min_t(size_t, len, frag->size - offset);
2371
2372 while (slen) {
2373 ret = kernel_sendpage_locked(sk, frag->page.p,
2374 frag->page_offset + offset,
2375 slen, MSG_DONTWAIT);
2376 if (ret <= 0)
2377 goto error;
2378
2379 len -= ret;
2380 offset += ret;
2381 slen -= ret;
2382 }
2383
2384 offset = 0;
2385 }
2386
2387 if (len) {
2388 /* Process any frag lists */
2389
2390 if (skb == head) {
2391 if (skb_has_frag_list(skb)) {
2392 skb = skb_shinfo(skb)->frag_list;
2393 goto do_frag_list;
2394 }
2395 } else if (skb->next) {
2396 skb = skb->next;
2397 goto do_frag_list;
2398 }
2399 }
2400
2401out:
2402 return orig_len - len;
2403
2404error:
2405 return orig_len == len ? ret : orig_len - len;
2406}
2407EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2408
2409/**
2410 * skb_store_bits - store bits from kernel buffer to skb
2411 * @skb: destination buffer
2412 * @offset: offset in destination
2413 * @from: source buffer
2414 * @len: number of bytes to copy
2415 *
2416 * Copy the specified number of bytes from the source buffer to the
2417 * destination skb. This function handles all the messy bits of
2418 * traversing fragment lists and such.
2419 */
2420
2421int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2422{
2423 int start = skb_headlen(skb);
2424 struct sk_buff *frag_iter;
2425 int i, copy;
2426
2427 if (offset > (int)skb->len - len)
2428 goto fault;
2429
2430 if ((copy = start - offset) > 0) {
2431 if (copy > len)
2432 copy = len;
2433 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2434 if ((len -= copy) == 0)
2435 return 0;
2436 offset += copy;
2437 from += copy;
2438 }
2439
2440 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2441 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2442 int end;
2443
2444 WARN_ON(start > offset + len);
2445
2446 end = start + skb_frag_size(frag);
2447 if ((copy = end - offset) > 0) {
2448 u32 p_off, p_len, copied;
2449 struct page *p;
2450 u8 *vaddr;
2451
2452 if (copy > len)
2453 copy = len;
2454
2455 skb_frag_foreach_page(frag,
2456 frag->page_offset + offset - start,
2457 copy, p, p_off, p_len, copied) {
2458 vaddr = kmap_atomic(p);
2459 memcpy(vaddr + p_off, from + copied, p_len);
2460 kunmap_atomic(vaddr);
2461 }
2462
2463 if ((len -= copy) == 0)
2464 return 0;
2465 offset += copy;
2466 from += copy;
2467 }
2468 start = end;
2469 }
2470
2471 skb_walk_frags(skb, frag_iter) {
2472 int end;
2473
2474 WARN_ON(start > offset + len);
2475
2476 end = start + frag_iter->len;
2477 if ((copy = end - offset) > 0) {
2478 if (copy > len)
2479 copy = len;
2480 if (skb_store_bits(frag_iter, offset - start,
2481 from, copy))
2482 goto fault;
2483 if ((len -= copy) == 0)
2484 return 0;
2485 offset += copy;
2486 from += copy;
2487 }
2488 start = end;
2489 }
2490 if (!len)
2491 return 0;
2492
2493fault:
2494 return -EFAULT;
2495}
2496EXPORT_SYMBOL(skb_store_bits);
2497
2498/* Checksum skb data. */
2499__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2500 __wsum csum, const struct skb_checksum_ops *ops)
2501{
2502 int start = skb_headlen(skb);
2503 int i, copy = start - offset;
2504 struct sk_buff *frag_iter;
2505 int pos = 0;
2506
2507 /* Checksum header. */
2508 if (copy > 0) {
2509 if (copy > len)
2510 copy = len;
2511 csum = ops->update(skb->data + offset, copy, csum);
2512 if ((len -= copy) == 0)
2513 return csum;
2514 offset += copy;
2515 pos = copy;
2516 }
2517
2518 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2519 int end;
2520 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2521
2522 WARN_ON(start > offset + len);
2523
2524 end = start + skb_frag_size(frag);
2525 if ((copy = end - offset) > 0) {
2526 u32 p_off, p_len, copied;
2527 struct page *p;
2528 __wsum csum2;
2529 u8 *vaddr;
2530
2531 if (copy > len)
2532 copy = len;
2533
2534 skb_frag_foreach_page(frag,
2535 frag->page_offset + offset - start,
2536 copy, p, p_off, p_len, copied) {
2537 vaddr = kmap_atomic(p);
2538 csum2 = ops->update(vaddr + p_off, p_len, 0);
2539 kunmap_atomic(vaddr);
2540 csum = ops->combine(csum, csum2, pos, p_len);
2541 pos += p_len;
2542 }
2543
2544 if (!(len -= copy))
2545 return csum;
2546 offset += copy;
2547 }
2548 start = end;
2549 }
2550
2551 skb_walk_frags(skb, frag_iter) {
2552 int end;
2553
2554 WARN_ON(start > offset + len);
2555
2556 end = start + frag_iter->len;
2557 if ((copy = end - offset) > 0) {
2558 __wsum csum2;
2559 if (copy > len)
2560 copy = len;
2561 csum2 = __skb_checksum(frag_iter, offset - start,
2562 copy, 0, ops);
2563 csum = ops->combine(csum, csum2, pos, copy);
2564 if ((len -= copy) == 0)
2565 return csum;
2566 offset += copy;
2567 pos += copy;
2568 }
2569 start = end;
2570 }
2571 BUG_ON(len);
2572
2573 return csum;
2574}
2575EXPORT_SYMBOL(__skb_checksum);
2576
2577__wsum skb_checksum(const struct sk_buff *skb, int offset,
2578 int len, __wsum csum)
2579{
2580 const struct skb_checksum_ops ops = {
2581 .update = csum_partial_ext,
2582 .combine = csum_block_add_ext,
2583 };
2584
2585 return __skb_checksum(skb, offset, len, csum, &ops);
2586}
2587EXPORT_SYMBOL(skb_checksum);
2588
2589/* Both of above in one bottle. */
2590
2591__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2592 u8 *to, int len, __wsum csum)
2593{
2594 int start = skb_headlen(skb);
2595 int i, copy = start - offset;
2596 struct sk_buff *frag_iter;
2597 int pos = 0;
2598
2599 /* Copy header. */
2600 if (copy > 0) {
2601 if (copy > len)
2602 copy = len;
2603 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2604 copy, csum);
2605 if ((len -= copy) == 0)
2606 return csum;
2607 offset += copy;
2608 to += copy;
2609 pos = copy;
2610 }
2611
2612 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2613 int end;
2614
2615 WARN_ON(start > offset + len);
2616
2617 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2618 if ((copy = end - offset) > 0) {
2619 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2620 u32 p_off, p_len, copied;
2621 struct page *p;
2622 __wsum csum2;
2623 u8 *vaddr;
2624
2625 if (copy > len)
2626 copy = len;
2627
2628 skb_frag_foreach_page(frag,
2629 frag->page_offset + offset - start,
2630 copy, p, p_off, p_len, copied) {
2631 vaddr = kmap_atomic(p);
2632 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2633 to + copied,
2634 p_len, 0);
2635 kunmap_atomic(vaddr);
2636 csum = csum_block_add(csum, csum2, pos);
2637 pos += p_len;
2638 }
2639
2640 if (!(len -= copy))
2641 return csum;
2642 offset += copy;
2643 to += copy;
2644 }
2645 start = end;
2646 }
2647
2648 skb_walk_frags(skb, frag_iter) {
2649 __wsum csum2;
2650 int end;
2651
2652 WARN_ON(start > offset + len);
2653
2654 end = start + frag_iter->len;
2655 if ((copy = end - offset) > 0) {
2656 if (copy > len)
2657 copy = len;
2658 csum2 = skb_copy_and_csum_bits(frag_iter,
2659 offset - start,
2660 to, copy, 0);
2661 csum = csum_block_add(csum, csum2, pos);
2662 if ((len -= copy) == 0)
2663 return csum;
2664 offset += copy;
2665 to += copy;
2666 pos += copy;
2667 }
2668 start = end;
2669 }
2670 BUG_ON(len);
2671 return csum;
2672}
2673EXPORT_SYMBOL(skb_copy_and_csum_bits);
2674
2675__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2676{
2677 __sum16 sum;
2678
2679 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
2680 /* See comments in __skb_checksum_complete(). */
2681 if (likely(!sum)) {
2682 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2683 !skb->csum_complete_sw)
2684 netdev_rx_csum_fault(skb->dev, skb);
2685 }
2686 if (!skb_shared(skb))
2687 skb->csum_valid = !sum;
2688 return sum;
2689}
2690EXPORT_SYMBOL(__skb_checksum_complete_head);
2691
2692/* This function assumes skb->csum already holds pseudo header's checksum,
2693 * which has been changed from the hardware checksum, for example, by
2694 * __skb_checksum_validate_complete(). And, the original skb->csum must
2695 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2696 *
2697 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2698 * zero. The new checksum is stored back into skb->csum unless the skb is
2699 * shared.
2700 */
2701__sum16 __skb_checksum_complete(struct sk_buff *skb)
2702{
2703 __wsum csum;
2704 __sum16 sum;
2705
2706 csum = skb_checksum(skb, 0, skb->len, 0);
2707
2708 sum = csum_fold(csum_add(skb->csum, csum));
2709 /* This check is inverted, because we already knew the hardware
2710 * checksum is invalid before calling this function. So, if the
2711 * re-computed checksum is valid instead, then we have a mismatch
2712 * between the original skb->csum and skb_checksum(). This means either
2713 * the original hardware checksum is incorrect or we screw up skb->csum
2714 * when moving skb->data around.
2715 */
2716 if (likely(!sum)) {
2717 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2718 !skb->csum_complete_sw)
2719 netdev_rx_csum_fault(skb->dev, skb);
2720 }
2721
2722 if (!skb_shared(skb)) {
2723 /* Save full packet checksum */
2724 skb->csum = csum;
2725 skb->ip_summed = CHECKSUM_COMPLETE;
2726 skb->csum_complete_sw = 1;
2727 skb->csum_valid = !sum;
2728 }
2729
2730 return sum;
2731}
2732EXPORT_SYMBOL(__skb_checksum_complete);
2733
2734static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2735{
2736 net_warn_ratelimited(
2737 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2738 __func__);
2739 return 0;
2740}
2741
2742static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2743 int offset, int len)
2744{
2745 net_warn_ratelimited(
2746 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2747 __func__);
2748 return 0;
2749}
2750
2751static const struct skb_checksum_ops default_crc32c_ops = {
2752 .update = warn_crc32c_csum_update,
2753 .combine = warn_crc32c_csum_combine,
2754};
2755
2756const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2757 &default_crc32c_ops;
2758EXPORT_SYMBOL(crc32c_csum_stub);
2759
2760 /**
2761 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2762 * @from: source buffer
2763 *
2764 * Calculates the amount of linear headroom needed in the 'to' skb passed
2765 * into skb_zerocopy().
2766 */
2767unsigned int
2768skb_zerocopy_headlen(const struct sk_buff *from)
2769{
2770 unsigned int hlen = 0;
2771
2772 if (!from->head_frag ||
2773 skb_headlen(from) < L1_CACHE_BYTES ||
2774 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2775 hlen = skb_headlen(from);
2776
2777 if (skb_has_frag_list(from))
2778 hlen = from->len;
2779
2780 return hlen;
2781}
2782EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2783
2784/**
2785 * skb_zerocopy - Zero copy skb to skb
2786 * @to: destination buffer
2787 * @from: source buffer
2788 * @len: number of bytes to copy from source buffer
2789 * @hlen: size of linear headroom in destination buffer
2790 *
2791 * Copies up to `len` bytes from `from` to `to` by creating references
2792 * to the frags in the source buffer.
2793 *
2794 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2795 * headroom in the `to` buffer.
2796 *
2797 * Return value:
2798 * 0: everything is OK
2799 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2800 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2801 */
2802int
2803skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2804{
2805 int i, j = 0;
2806 int plen = 0; /* length of skb->head fragment */
2807 int ret;
2808 struct page *page;
2809 unsigned int offset;
2810
2811 BUG_ON(!from->head_frag && !hlen);
2812
2813 /* dont bother with small payloads */
2814 if (len <= skb_tailroom(to))
2815 return skb_copy_bits(from, 0, skb_put(to, len), len);
2816
2817 if (hlen) {
2818 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2819 if (unlikely(ret))
2820 return ret;
2821 len -= hlen;
2822 } else {
2823 plen = min_t(int, skb_headlen(from), len);
2824 if (plen) {
2825 page = virt_to_head_page(from->head);
2826 offset = from->data - (unsigned char *)page_address(page);
2827 __skb_fill_page_desc(to, 0, page, offset, plen);
2828 get_page(page);
2829 j = 1;
2830 len -= plen;
2831 }
2832 }
2833
2834 to->truesize += len + plen;
2835 to->len += len + plen;
2836 to->data_len += len + plen;
2837
2838 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2839 skb_tx_error(from);
2840 return -ENOMEM;
2841 }
2842 skb_zerocopy_clone(to, from, GFP_ATOMIC);
2843
2844 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2845 if (!len)
2846 break;
2847 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2848 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2849 len -= skb_shinfo(to)->frags[j].size;
2850 skb_frag_ref(to, j);
2851 j++;
2852 }
2853 skb_shinfo(to)->nr_frags = j;
2854
2855 return 0;
2856}
2857EXPORT_SYMBOL_GPL(skb_zerocopy);
2858
2859void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2860{
2861 __wsum csum;
2862 long csstart;
2863
2864 if (skb->ip_summed == CHECKSUM_PARTIAL)
2865 csstart = skb_checksum_start_offset(skb);
2866 else
2867 csstart = skb_headlen(skb);
2868
2869 BUG_ON(csstart > skb_headlen(skb));
2870
2871 skb_copy_from_linear_data(skb, to, csstart);
2872
2873 csum = 0;
2874 if (csstart != skb->len)
2875 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2876 skb->len - csstart, 0);
2877
2878 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2879 long csstuff = csstart + skb->csum_offset;
2880
2881 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2882 }
2883}
2884EXPORT_SYMBOL(skb_copy_and_csum_dev);
2885
2886/**
2887 * skb_dequeue - remove from the head of the queue
2888 * @list: list to dequeue from
2889 *
2890 * Remove the head of the list. The list lock is taken so the function
2891 * may be used safely with other locking list functions. The head item is
2892 * returned or %NULL if the list is empty.
2893 */
2894
2895struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2896{
2897 unsigned long flags;
2898 struct sk_buff *result;
2899
2900 spin_lock_irqsave(&list->lock, flags);
2901 result = __skb_dequeue(list);
2902 spin_unlock_irqrestore(&list->lock, flags);
2903 return result;
2904}
2905EXPORT_SYMBOL(skb_dequeue);
2906
2907/**
2908 * skb_dequeue_tail - remove from the tail of the queue
2909 * @list: list to dequeue from
2910 *
2911 * Remove the tail of the list. The list lock is taken so the function
2912 * may be used safely with other locking list functions. The tail item is
2913 * returned or %NULL if the list is empty.
2914 */
2915struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2916{
2917 unsigned long flags;
2918 struct sk_buff *result;
2919
2920 spin_lock_irqsave(&list->lock, flags);
2921 result = __skb_dequeue_tail(list);
2922 spin_unlock_irqrestore(&list->lock, flags);
2923 return result;
2924}
2925EXPORT_SYMBOL(skb_dequeue_tail);
2926
2927/**
2928 * skb_queue_purge - empty a list
2929 * @list: list to empty
2930 *
2931 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2932 * the list and one reference dropped. This function takes the list
2933 * lock and is atomic with respect to other list locking functions.
2934 */
2935void skb_queue_purge(struct sk_buff_head *list)
2936{
2937 struct sk_buff *skb;
2938 while ((skb = skb_dequeue(list)) != NULL)
2939 kfree_skb(skb);
2940}
2941EXPORT_SYMBOL(skb_queue_purge);
2942
2943/**
2944 * skb_rbtree_purge - empty a skb rbtree
2945 * @root: root of the rbtree to empty
2946 * Return value: the sum of truesizes of all purged skbs.
2947 *
2948 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2949 * the list and one reference dropped. This function does not take
2950 * any lock. Synchronization should be handled by the caller (e.g., TCP
2951 * out-of-order queue is protected by the socket lock).
2952 */
2953unsigned int skb_rbtree_purge(struct rb_root *root)
2954{
2955 struct rb_node *p = rb_first(root);
2956 unsigned int sum = 0;
2957
2958 while (p) {
2959 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2960
2961 p = rb_next(p);
2962 rb_erase(&skb->rbnode, root);
2963 sum += skb->truesize;
2964 kfree_skb(skb);
2965 }
2966 return sum;
2967}
2968
2969/**
2970 * skb_queue_head - queue a buffer at the list head
2971 * @list: list to use
2972 * @newsk: buffer to queue
2973 *
2974 * Queue a buffer at the start of the list. This function takes the
2975 * list lock and can be used safely with other locking &sk_buff functions
2976 * safely.
2977 *
2978 * A buffer cannot be placed on two lists at the same time.
2979 */
2980void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2981{
2982 unsigned long flags;
2983
2984 spin_lock_irqsave(&list->lock, flags);
2985 __skb_queue_head(list, newsk);
2986 spin_unlock_irqrestore(&list->lock, flags);
2987}
2988EXPORT_SYMBOL(skb_queue_head);
2989
2990/**
2991 * skb_queue_tail - queue a buffer at the list tail
2992 * @list: list to use
2993 * @newsk: buffer to queue
2994 *
2995 * Queue a buffer at the tail of the list. This function takes the
2996 * list lock and can be used safely with other locking &sk_buff functions
2997 * safely.
2998 *
2999 * A buffer cannot be placed on two lists at the same time.
3000 */
3001void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3002{
3003 unsigned long flags;
3004
3005 spin_lock_irqsave(&list->lock, flags);
3006 __skb_queue_tail(list, newsk);
3007 spin_unlock_irqrestore(&list->lock, flags);
3008}
3009EXPORT_SYMBOL(skb_queue_tail);
3010
3011/**
3012 * skb_unlink - remove a buffer from a list
3013 * @skb: buffer to remove
3014 * @list: list to use
3015 *
3016 * Remove a packet from a list. The list locks are taken and this
3017 * function is atomic with respect to other list locked calls
3018 *
3019 * You must know what list the SKB is on.
3020 */
3021void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3022{
3023 unsigned long flags;
3024
3025 spin_lock_irqsave(&list->lock, flags);
3026 __skb_unlink(skb, list);
3027 spin_unlock_irqrestore(&list->lock, flags);
3028}
3029EXPORT_SYMBOL(skb_unlink);
3030
3031/**
3032 * skb_append - append a buffer
3033 * @old: buffer to insert after
3034 * @newsk: buffer to insert
3035 * @list: list to use
3036 *
3037 * Place a packet after a given packet in a list. The list locks are taken
3038 * and this function is atomic with respect to other list locked calls.
3039 * A buffer cannot be placed on two lists at the same time.
3040 */
3041void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3042{
3043 unsigned long flags;
3044
3045 spin_lock_irqsave(&list->lock, flags);
3046 __skb_queue_after(list, old, newsk);
3047 spin_unlock_irqrestore(&list->lock, flags);
3048}
3049EXPORT_SYMBOL(skb_append);
3050
3051static inline void skb_split_inside_header(struct sk_buff *skb,
3052 struct sk_buff* skb1,
3053 const u32 len, const int pos)
3054{
3055 int i;
3056
3057 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3058 pos - len);
3059 /* And move data appendix as is. */
3060 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3061 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3062
3063 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3064 skb_shinfo(skb)->nr_frags = 0;
3065 skb1->data_len = skb->data_len;
3066 skb1->len += skb1->data_len;
3067 skb->data_len = 0;
3068 skb->len = len;
3069 skb_set_tail_pointer(skb, len);
3070}
3071
3072static inline void skb_split_no_header(struct sk_buff *skb,
3073 struct sk_buff* skb1,
3074 const u32 len, int pos)
3075{
3076 int i, k = 0;
3077 const int nfrags = skb_shinfo(skb)->nr_frags;
3078
3079 skb_shinfo(skb)->nr_frags = 0;
3080 skb1->len = skb1->data_len = skb->len - len;
3081 skb->len = len;
3082 skb->data_len = len - pos;
3083
3084 for (i = 0; i < nfrags; i++) {
3085 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3086
3087 if (pos + size > len) {
3088 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3089
3090 if (pos < len) {
3091 /* Split frag.
3092 * We have two variants in this case:
3093 * 1. Move all the frag to the second
3094 * part, if it is possible. F.e.
3095 * this approach is mandatory for TUX,
3096 * where splitting is expensive.
3097 * 2. Split is accurately. We make this.
3098 */
3099 skb_frag_ref(skb, i);
3100 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
3101 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3102 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3103 skb_shinfo(skb)->nr_frags++;
3104 }
3105 k++;
3106 } else
3107 skb_shinfo(skb)->nr_frags++;
3108 pos += size;
3109 }
3110 skb_shinfo(skb1)->nr_frags = k;
3111}
3112
3113/**
3114 * skb_split - Split fragmented skb to two parts at length len.
3115 * @skb: the buffer to split
3116 * @skb1: the buffer to receive the second part
3117 * @len: new length for skb
3118 */
3119void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3120{
3121 int pos = skb_headlen(skb);
3122
3123 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3124 SKBTX_SHARED_FRAG;
3125 skb_zerocopy_clone(skb1, skb, 0);
3126 if (len < pos) /* Split line is inside header. */
3127 skb_split_inside_header(skb, skb1, len, pos);
3128 else /* Second chunk has no header, nothing to copy. */
3129 skb_split_no_header(skb, skb1, len, pos);
3130}
3131EXPORT_SYMBOL(skb_split);
3132
3133/* Shifting from/to a cloned skb is a no-go.
3134 *
3135 * Caller cannot keep skb_shinfo related pointers past calling here!
3136 */
3137static int skb_prepare_for_shift(struct sk_buff *skb)
3138{
3139 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3140}
3141
3142/**
3143 * skb_shift - Shifts paged data partially from skb to another
3144 * @tgt: buffer into which tail data gets added
3145 * @skb: buffer from which the paged data comes from
3146 * @shiftlen: shift up to this many bytes
3147 *
3148 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3149 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3150 * It's up to caller to free skb if everything was shifted.
3151 *
3152 * If @tgt runs out of frags, the whole operation is aborted.
3153 *
3154 * Skb cannot include anything else but paged data while tgt is allowed
3155 * to have non-paged data as well.
3156 *
3157 * TODO: full sized shift could be optimized but that would need
3158 * specialized skb free'er to handle frags without up-to-date nr_frags.
3159 */
3160int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3161{
3162 int from, to, merge, todo;
3163 struct skb_frag_struct *fragfrom, *fragto;
3164
3165 BUG_ON(shiftlen > skb->len);
3166
3167 if (skb_headlen(skb))
3168 return 0;
3169 if (skb_zcopy(tgt) || skb_zcopy(skb))
3170 return 0;
3171
3172 todo = shiftlen;
3173 from = 0;
3174 to = skb_shinfo(tgt)->nr_frags;
3175 fragfrom = &skb_shinfo(skb)->frags[from];
3176
3177 /* Actual merge is delayed until the point when we know we can
3178 * commit all, so that we don't have to undo partial changes
3179 */
3180 if (!to ||
3181 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3182 fragfrom->page_offset)) {
3183 merge = -1;
3184 } else {
3185 merge = to - 1;
3186
3187 todo -= skb_frag_size(fragfrom);
3188 if (todo < 0) {
3189 if (skb_prepare_for_shift(skb) ||
3190 skb_prepare_for_shift(tgt))
3191 return 0;
3192
3193 /* All previous frag pointers might be stale! */
3194 fragfrom = &skb_shinfo(skb)->frags[from];
3195 fragto = &skb_shinfo(tgt)->frags[merge];
3196
3197 skb_frag_size_add(fragto, shiftlen);
3198 skb_frag_size_sub(fragfrom, shiftlen);
3199 fragfrom->page_offset += shiftlen;
3200
3201 goto onlymerged;
3202 }
3203
3204 from++;
3205 }
3206
3207 /* Skip full, not-fitting skb to avoid expensive operations */
3208 if ((shiftlen == skb->len) &&
3209 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3210 return 0;
3211
3212 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3213 return 0;
3214
3215 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3216 if (to == MAX_SKB_FRAGS)
3217 return 0;
3218
3219 fragfrom = &skb_shinfo(skb)->frags[from];
3220 fragto = &skb_shinfo(tgt)->frags[to];
3221
3222 if (todo >= skb_frag_size(fragfrom)) {
3223 *fragto = *fragfrom;
3224 todo -= skb_frag_size(fragfrom);
3225 from++;
3226 to++;
3227
3228 } else {
3229 __skb_frag_ref(fragfrom);
3230 fragto->page = fragfrom->page;
3231 fragto->page_offset = fragfrom->page_offset;
3232 skb_frag_size_set(fragto, todo);
3233
3234 fragfrom->page_offset += todo;
3235 skb_frag_size_sub(fragfrom, todo);
3236 todo = 0;
3237
3238 to++;
3239 break;
3240 }
3241 }
3242
3243 /* Ready to "commit" this state change to tgt */
3244 skb_shinfo(tgt)->nr_frags = to;
3245
3246 if (merge >= 0) {
3247 fragfrom = &skb_shinfo(skb)->frags[0];
3248 fragto = &skb_shinfo(tgt)->frags[merge];
3249
3250 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3251 __skb_frag_unref(fragfrom);
3252 }
3253
3254 /* Reposition in the original skb */
3255 to = 0;
3256 while (from < skb_shinfo(skb)->nr_frags)
3257 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3258 skb_shinfo(skb)->nr_frags = to;
3259
3260 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3261
3262onlymerged:
3263 /* Most likely the tgt won't ever need its checksum anymore, skb on
3264 * the other hand might need it if it needs to be resent
3265 */
3266 tgt->ip_summed = CHECKSUM_PARTIAL;
3267 skb->ip_summed = CHECKSUM_PARTIAL;
3268
3269 /* Yak, is it really working this way? Some helper please? */
3270 skb->len -= shiftlen;
3271 skb->data_len -= shiftlen;
3272 skb->truesize -= shiftlen;
3273 tgt->len += shiftlen;
3274 tgt->data_len += shiftlen;
3275 tgt->truesize += shiftlen;
3276
3277 return shiftlen;
3278}
3279
3280/**
3281 * skb_prepare_seq_read - Prepare a sequential read of skb data
3282 * @skb: the buffer to read
3283 * @from: lower offset of data to be read
3284 * @to: upper offset of data to be read
3285 * @st: state variable
3286 *
3287 * Initializes the specified state variable. Must be called before
3288 * invoking skb_seq_read() for the first time.
3289 */
3290void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3291 unsigned int to, struct skb_seq_state *st)
3292{
3293 st->lower_offset = from;
3294 st->upper_offset = to;
3295 st->root_skb = st->cur_skb = skb;
3296 st->frag_idx = st->stepped_offset = 0;
3297 st->frag_data = NULL;
3298}
3299EXPORT_SYMBOL(skb_prepare_seq_read);
3300
3301/**
3302 * skb_seq_read - Sequentially read skb data
3303 * @consumed: number of bytes consumed by the caller so far
3304 * @data: destination pointer for data to be returned
3305 * @st: state variable
3306 *
3307 * Reads a block of skb data at @consumed relative to the
3308 * lower offset specified to skb_prepare_seq_read(). Assigns
3309 * the head of the data block to @data and returns the length
3310 * of the block or 0 if the end of the skb data or the upper
3311 * offset has been reached.
3312 *
3313 * The caller is not required to consume all of the data
3314 * returned, i.e. @consumed is typically set to the number
3315 * of bytes already consumed and the next call to
3316 * skb_seq_read() will return the remaining part of the block.
3317 *
3318 * Note 1: The size of each block of data returned can be arbitrary,
3319 * this limitation is the cost for zerocopy sequential
3320 * reads of potentially non linear data.
3321 *
3322 * Note 2: Fragment lists within fragments are not implemented
3323 * at the moment, state->root_skb could be replaced with
3324 * a stack for this purpose.
3325 */
3326unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3327 struct skb_seq_state *st)
3328{
3329 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3330 skb_frag_t *frag;
3331
3332 if (unlikely(abs_offset >= st->upper_offset)) {
3333 if (st->frag_data) {
3334 kunmap_atomic(st->frag_data);
3335 st->frag_data = NULL;
3336 }
3337 return 0;
3338 }
3339
3340next_skb:
3341 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3342
3343 if (abs_offset < block_limit && !st->frag_data) {
3344 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3345 return block_limit - abs_offset;
3346 }
3347
3348 if (st->frag_idx == 0 && !st->frag_data)
3349 st->stepped_offset += skb_headlen(st->cur_skb);
3350
3351 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3352 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3353 block_limit = skb_frag_size(frag) + st->stepped_offset;
3354
3355 if (abs_offset < block_limit) {
3356 if (!st->frag_data)
3357 st->frag_data = kmap_atomic(skb_frag_page(frag));
3358
3359 *data = (u8 *) st->frag_data + frag->page_offset +
3360 (abs_offset - st->stepped_offset);
3361
3362 return block_limit - abs_offset;
3363 }
3364
3365 if (st->frag_data) {
3366 kunmap_atomic(st->frag_data);
3367 st->frag_data = NULL;
3368 }
3369
3370 st->frag_idx++;
3371 st->stepped_offset += skb_frag_size(frag);
3372 }
3373
3374 if (st->frag_data) {
3375 kunmap_atomic(st->frag_data);
3376 st->frag_data = NULL;
3377 }
3378
3379 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3380 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3381 st->frag_idx = 0;
3382 goto next_skb;
3383 } else if (st->cur_skb->next) {
3384 st->cur_skb = st->cur_skb->next;
3385 st->frag_idx = 0;
3386 goto next_skb;
3387 }
3388
3389 return 0;
3390}
3391EXPORT_SYMBOL(skb_seq_read);
3392
3393/**
3394 * skb_abort_seq_read - Abort a sequential read of skb data
3395 * @st: state variable
3396 *
3397 * Must be called if skb_seq_read() was not called until it
3398 * returned 0.
3399 */
3400void skb_abort_seq_read(struct skb_seq_state *st)
3401{
3402 if (st->frag_data)
3403 kunmap_atomic(st->frag_data);
3404}
3405EXPORT_SYMBOL(skb_abort_seq_read);
3406
3407#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3408
3409static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3410 struct ts_config *conf,
3411 struct ts_state *state)
3412{
3413 return skb_seq_read(offset, text, TS_SKB_CB(state));
3414}
3415
3416static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3417{
3418 skb_abort_seq_read(TS_SKB_CB(state));
3419}
3420
3421/**
3422 * skb_find_text - Find a text pattern in skb data
3423 * @skb: the buffer to look in
3424 * @from: search offset
3425 * @to: search limit
3426 * @config: textsearch configuration
3427 *
3428 * Finds a pattern in the skb data according to the specified
3429 * textsearch configuration. Use textsearch_next() to retrieve
3430 * subsequent occurrences of the pattern. Returns the offset
3431 * to the first occurrence or UINT_MAX if no match was found.
3432 */
3433unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3434 unsigned int to, struct ts_config *config)
3435{
3436 struct ts_state state;
3437 unsigned int ret;
3438
3439 config->get_next_block = skb_ts_get_next_block;
3440 config->finish = skb_ts_finish;
3441
3442 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3443
3444 ret = textsearch_find(config, &state);
3445 return (ret <= to - from ? ret : UINT_MAX);
3446}
3447EXPORT_SYMBOL(skb_find_text);
3448
3449int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3450 int offset, size_t size)
3451{
3452 int i = skb_shinfo(skb)->nr_frags;
3453
3454 if (skb_can_coalesce(skb, i, page, offset)) {
3455 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3456 } else if (i < MAX_SKB_FRAGS) {
3457 get_page(page);
3458 skb_fill_page_desc(skb, i, page, offset, size);
3459 } else {
3460 return -EMSGSIZE;
3461 }
3462
3463 return 0;
3464}
3465EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3466
3467/**
3468 * skb_pull_rcsum - pull skb and update receive checksum
3469 * @skb: buffer to update
3470 * @len: length of data pulled
3471 *
3472 * This function performs an skb_pull on the packet and updates
3473 * the CHECKSUM_COMPLETE checksum. It should be used on
3474 * receive path processing instead of skb_pull unless you know
3475 * that the checksum difference is zero (e.g., a valid IP header)
3476 * or you are setting ip_summed to CHECKSUM_NONE.
3477 */
3478void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3479{
3480 unsigned char *data = skb->data;
3481
3482 BUG_ON(len > skb->len);
3483 __skb_pull(skb, len);
3484 skb_postpull_rcsum(skb, data, len);
3485 return skb->data;
3486}
3487EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3488
3489static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3490{
3491 skb_frag_t head_frag;
3492 struct page *page;
3493
3494 page = virt_to_head_page(frag_skb->head);
3495 head_frag.page.p = page;
3496 head_frag.page_offset = frag_skb->data -
3497 (unsigned char *)page_address(page);
3498 head_frag.size = skb_headlen(frag_skb);
3499 return head_frag;
3500}
3501
3502/**
3503 * skb_segment - Perform protocol segmentation on skb.
3504 * @head_skb: buffer to segment
3505 * @features: features for the output path (see dev->features)
3506 *
3507 * This function performs segmentation on the given skb. It returns
3508 * a pointer to the first in a list of new skbs for the segments.
3509 * In case of error it returns ERR_PTR(err).
3510 */
3511struct sk_buff *skb_segment(struct sk_buff *head_skb,
3512 netdev_features_t features)
3513{
3514 struct sk_buff *segs = NULL;
3515 struct sk_buff *tail = NULL;
3516 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3517 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3518 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3519 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3520 struct sk_buff *frag_skb = head_skb;
3521 unsigned int offset = doffset;
3522 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3523 unsigned int partial_segs = 0;
3524 unsigned int headroom;
3525 unsigned int len = head_skb->len;
3526 __be16 proto;
3527 bool csum, sg;
3528 int nfrags = skb_shinfo(head_skb)->nr_frags;
3529 int err = -ENOMEM;
3530 int i = 0;
3531 int pos;
3532 int dummy;
3533
3534 __skb_push(head_skb, doffset);
3535 proto = skb_network_protocol(head_skb, &dummy);
3536 if (unlikely(!proto))
3537 return ERR_PTR(-EINVAL);
3538
3539 sg = !!(features & NETIF_F_SG);
3540 csum = !!can_checksum_protocol(features, proto);
3541
3542 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3543 if (!(features & NETIF_F_GSO_PARTIAL)) {
3544 struct sk_buff *iter;
3545 unsigned int frag_len;
3546
3547 if (!list_skb ||
3548 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3549 goto normal;
3550
3551 /* If we get here then all the required
3552 * GSO features except frag_list are supported.
3553 * Try to split the SKB to multiple GSO SKBs
3554 * with no frag_list.
3555 * Currently we can do that only when the buffers don't
3556 * have a linear part and all the buffers except
3557 * the last are of the same length.
3558 */
3559 frag_len = list_skb->len;
3560 skb_walk_frags(head_skb, iter) {
3561 if (frag_len != iter->len && iter->next)
3562 goto normal;
3563 if (skb_headlen(iter) && !iter->head_frag)
3564 goto normal;
3565
3566 len -= iter->len;
3567 }
3568
3569 if (len != frag_len)
3570 goto normal;
3571 }
3572
3573 /* GSO partial only requires that we trim off any excess that
3574 * doesn't fit into an MSS sized block, so take care of that
3575 * now.
3576 */
3577 partial_segs = len / mss;
3578 if (partial_segs > 1)
3579 mss *= partial_segs;
3580 else
3581 partial_segs = 0;
3582 }
3583
3584normal:
3585 headroom = skb_headroom(head_skb);
3586 pos = skb_headlen(head_skb);
3587
3588 do {
3589 struct sk_buff *nskb;
3590 skb_frag_t *nskb_frag;
3591 int hsize;
3592 int size;
3593
3594 if (unlikely(mss == GSO_BY_FRAGS)) {
3595 len = list_skb->len;
3596 } else {
3597 len = head_skb->len - offset;
3598 if (len > mss)
3599 len = mss;
3600 }
3601
3602 hsize = skb_headlen(head_skb) - offset;
3603 if (hsize < 0)
3604 hsize = 0;
3605 if (hsize > len || !sg)
3606 hsize = len;
3607
3608 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3609 (skb_headlen(list_skb) == len || sg)) {
3610 BUG_ON(skb_headlen(list_skb) > len);
3611
3612 i = 0;
3613 nfrags = skb_shinfo(list_skb)->nr_frags;
3614 frag = skb_shinfo(list_skb)->frags;
3615 frag_skb = list_skb;
3616 pos += skb_headlen(list_skb);
3617
3618 while (pos < offset + len) {
3619 BUG_ON(i >= nfrags);
3620
3621 size = skb_frag_size(frag);
3622 if (pos + size > offset + len)
3623 break;
3624
3625 i++;
3626 pos += size;
3627 frag++;
3628 }
3629
3630 nskb = skb_clone(list_skb, GFP_ATOMIC);
3631 list_skb = list_skb->next;
3632
3633 if (unlikely(!nskb))
3634 goto err;
3635
3636 if (unlikely(pskb_trim(nskb, len))) {
3637 kfree_skb(nskb);
3638 goto err;
3639 }
3640
3641 hsize = skb_end_offset(nskb);
3642 if (skb_cow_head(nskb, doffset + headroom)) {
3643 kfree_skb(nskb);
3644 goto err;
3645 }
3646
3647 nskb->truesize += skb_end_offset(nskb) - hsize;
3648 skb_release_head_state(nskb);
3649 __skb_push(nskb, doffset);
3650 } else {
3651 nskb = __alloc_skb(hsize + doffset + headroom,
3652 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3653 NUMA_NO_NODE);
3654
3655 if (unlikely(!nskb))
3656 goto err;
3657
3658 skb_reserve(nskb, headroom);
3659 __skb_put(nskb, doffset);
3660 }
3661
3662 if (segs)
3663 tail->next = nskb;
3664 else
3665 segs = nskb;
3666 tail = nskb;
3667
3668 __copy_skb_header(nskb, head_skb);
3669
3670 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3671 skb_reset_mac_len(nskb);
3672
3673 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3674 nskb->data - tnl_hlen,
3675 doffset + tnl_hlen);
3676
3677 if (nskb->len == len + doffset)
3678 goto perform_csum_check;
3679
3680 if (!sg) {
3681 if (!nskb->remcsum_offload)
3682 nskb->ip_summed = CHECKSUM_NONE;
3683 SKB_GSO_CB(nskb)->csum =
3684 skb_copy_and_csum_bits(head_skb, offset,
3685 skb_put(nskb, len),
3686 len, 0);
3687 SKB_GSO_CB(nskb)->csum_start =
3688 skb_headroom(nskb) + doffset;
3689 continue;
3690 }
3691
3692 nskb_frag = skb_shinfo(nskb)->frags;
3693
3694 skb_copy_from_linear_data_offset(head_skb, offset,
3695 skb_put(nskb, hsize), hsize);
3696
3697 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3698 SKBTX_SHARED_FRAG;
3699
3700 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3701 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3702 goto err;
3703
3704 while (pos < offset + len) {
3705 if (i >= nfrags) {
3706 i = 0;
3707 nfrags = skb_shinfo(list_skb)->nr_frags;
3708 frag = skb_shinfo(list_skb)->frags;
3709 frag_skb = list_skb;
3710 if (!skb_headlen(list_skb)) {
3711 BUG_ON(!nfrags);
3712 } else {
3713 BUG_ON(!list_skb->head_frag);
3714
3715 /* to make room for head_frag. */
3716 i--;
3717 frag--;
3718 }
3719 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3720 skb_zerocopy_clone(nskb, frag_skb,
3721 GFP_ATOMIC))
3722 goto err;
3723
3724 list_skb = list_skb->next;
3725 }
3726
3727 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3728 MAX_SKB_FRAGS)) {
3729 net_warn_ratelimited(
3730 "skb_segment: too many frags: %u %u\n",
3731 pos, mss);
3732 err = -EINVAL;
3733 goto err;
3734 }
3735
3736 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
3737 __skb_frag_ref(nskb_frag);
3738 size = skb_frag_size(nskb_frag);
3739
3740 if (pos < offset) {
3741 nskb_frag->page_offset += offset - pos;
3742 skb_frag_size_sub(nskb_frag, offset - pos);
3743 }
3744
3745 skb_shinfo(nskb)->nr_frags++;
3746
3747 if (pos + size <= offset + len) {
3748 i++;
3749 frag++;
3750 pos += size;
3751 } else {
3752 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3753 goto skip_fraglist;
3754 }
3755
3756 nskb_frag++;
3757 }
3758
3759skip_fraglist:
3760 nskb->data_len = len - hsize;
3761 nskb->len += nskb->data_len;
3762 nskb->truesize += nskb->data_len;
3763
3764perform_csum_check:
3765 if (!csum) {
3766 if (skb_has_shared_frag(nskb) &&
3767 __skb_linearize(nskb))
3768 goto err;
3769
3770 if (!nskb->remcsum_offload)
3771 nskb->ip_summed = CHECKSUM_NONE;
3772 SKB_GSO_CB(nskb)->csum =
3773 skb_checksum(nskb, doffset,
3774 nskb->len - doffset, 0);
3775 SKB_GSO_CB(nskb)->csum_start =
3776 skb_headroom(nskb) + doffset;
3777 }
3778 } while ((offset += len) < head_skb->len);
3779
3780 /* Some callers want to get the end of the list.
3781 * Put it in segs->prev to avoid walking the list.
3782 * (see validate_xmit_skb_list() for example)
3783 */
3784 segs->prev = tail;
3785
3786 if (partial_segs) {
3787 struct sk_buff *iter;
3788 int type = skb_shinfo(head_skb)->gso_type;
3789 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3790
3791 /* Update type to add partial and then remove dodgy if set */
3792 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3793 type &= ~SKB_GSO_DODGY;
3794
3795 /* Update GSO info and prepare to start updating headers on
3796 * our way back down the stack of protocols.
3797 */
3798 for (iter = segs; iter; iter = iter->next) {
3799 skb_shinfo(iter)->gso_size = gso_size;
3800 skb_shinfo(iter)->gso_segs = partial_segs;
3801 skb_shinfo(iter)->gso_type = type;
3802 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3803 }
3804
3805 if (tail->len - doffset <= gso_size)
3806 skb_shinfo(tail)->gso_size = 0;
3807 else if (tail != segs)
3808 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3809 }
3810
3811 /* Following permits correct backpressure, for protocols
3812 * using skb_set_owner_w().
3813 * Idea is to tranfert ownership from head_skb to last segment.
3814 */
3815 if (head_skb->destructor == sock_wfree) {
3816 swap(tail->truesize, head_skb->truesize);
3817 swap(tail->destructor, head_skb->destructor);
3818 swap(tail->sk, head_skb->sk);
3819 }
3820 return segs;
3821
3822err:
3823 kfree_skb_list(segs);
3824 return ERR_PTR(err);
3825}
3826EXPORT_SYMBOL_GPL(skb_segment);
3827
3828int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
3829{
3830 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3831 unsigned int offset = skb_gro_offset(skb);
3832 unsigned int headlen = skb_headlen(skb);
3833 unsigned int len = skb_gro_len(skb);
3834 unsigned int delta_truesize;
3835 struct sk_buff *lp;
3836
3837 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
3838 return -E2BIG;
3839
3840 lp = NAPI_GRO_CB(p)->last;
3841 pinfo = skb_shinfo(lp);
3842
3843 if (headlen <= offset) {
3844 skb_frag_t *frag;
3845 skb_frag_t *frag2;
3846 int i = skbinfo->nr_frags;
3847 int nr_frags = pinfo->nr_frags + i;
3848
3849 if (nr_frags > MAX_SKB_FRAGS)
3850 goto merge;
3851
3852 offset -= headlen;
3853 pinfo->nr_frags = nr_frags;
3854 skbinfo->nr_frags = 0;
3855
3856 frag = pinfo->frags + nr_frags;
3857 frag2 = skbinfo->frags + i;
3858 do {
3859 *--frag = *--frag2;
3860 } while (--i);
3861
3862 frag->page_offset += offset;
3863 skb_frag_size_sub(frag, offset);
3864
3865 /* all fragments truesize : remove (head size + sk_buff) */
3866 delta_truesize = skb->truesize -
3867 SKB_TRUESIZE(skb_end_offset(skb));
3868
3869 skb->truesize -= skb->data_len;
3870 skb->len -= skb->data_len;
3871 skb->data_len = 0;
3872
3873 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3874 goto done;
3875 } else if (skb->head_frag) {
3876 int nr_frags = pinfo->nr_frags;
3877 skb_frag_t *frag = pinfo->frags + nr_frags;
3878 struct page *page = virt_to_head_page(skb->head);
3879 unsigned int first_size = headlen - offset;
3880 unsigned int first_offset;
3881
3882 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3883 goto merge;
3884
3885 first_offset = skb->data -
3886 (unsigned char *)page_address(page) +
3887 offset;
3888
3889 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3890
3891 frag->page.p = page;
3892 frag->page_offset = first_offset;
3893 skb_frag_size_set(frag, first_size);
3894
3895 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3896 /* We dont need to clear skbinfo->nr_frags here */
3897
3898 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3899 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3900 goto done;
3901 }
3902
3903merge:
3904 delta_truesize = skb->truesize;
3905 if (offset > headlen) {
3906 unsigned int eat = offset - headlen;
3907
3908 skbinfo->frags[0].page_offset += eat;
3909 skb_frag_size_sub(&skbinfo->frags[0], eat);
3910 skb->data_len -= eat;
3911 skb->len -= eat;
3912 offset = headlen;
3913 }
3914
3915 __skb_pull(skb, offset);
3916
3917 if (NAPI_GRO_CB(p)->last == p)
3918 skb_shinfo(p)->frag_list = skb;
3919 else
3920 NAPI_GRO_CB(p)->last->next = skb;
3921 NAPI_GRO_CB(p)->last = skb;
3922 __skb_header_release(skb);
3923 lp = p;
3924
3925done:
3926 NAPI_GRO_CB(p)->count++;
3927 p->data_len += len;
3928 p->truesize += delta_truesize;
3929 p->len += len;
3930 if (lp != p) {
3931 lp->data_len += len;
3932 lp->truesize += delta_truesize;
3933 lp->len += len;
3934 }
3935 NAPI_GRO_CB(skb)->same_flow = 1;
3936 return 0;
3937}
3938EXPORT_SYMBOL_GPL(skb_gro_receive);
3939
3940#ifdef CONFIG_SKB_EXTENSIONS
3941#define SKB_EXT_ALIGN_VALUE 8
3942#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3943
3944static const u8 skb_ext_type_len[] = {
3945#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3946 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3947#endif
3948#ifdef CONFIG_XFRM
3949 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3950#endif
3951};
3952
3953static __always_inline unsigned int skb_ext_total_length(void)
3954{
3955 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3956#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3957 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3958#endif
3959#ifdef CONFIG_XFRM
3960 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3961#endif
3962 0;
3963}
3964
3965static void skb_extensions_init(void)
3966{
3967 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3968 BUILD_BUG_ON(skb_ext_total_length() > 255);
3969
3970 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3971 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3972 0,
3973 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3974 NULL);
3975}
3976#else
3977static void skb_extensions_init(void) {}
3978#endif
3979
3980void __init skb_init(void)
3981{
3982 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
3983 sizeof(struct sk_buff),
3984 0,
3985 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3986 offsetof(struct sk_buff, cb),
3987 sizeof_field(struct sk_buff, cb),
3988 NULL);
3989 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3990 sizeof(struct sk_buff_fclones),
3991 0,
3992 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3993 NULL);
3994 skb_extensions_init();
3995}
3996
3997static int
3998__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3999 unsigned int recursion_level)
4000{
4001 int start = skb_headlen(skb);
4002 int i, copy = start - offset;
4003 struct sk_buff *frag_iter;
4004 int elt = 0;
4005
4006 if (unlikely(recursion_level >= 24))
4007 return -EMSGSIZE;
4008
4009 if (copy > 0) {
4010 if (copy > len)
4011 copy = len;
4012 sg_set_buf(sg, skb->data + offset, copy);
4013 elt++;
4014 if ((len -= copy) == 0)
4015 return elt;
4016 offset += copy;
4017 }
4018
4019 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4020 int end;
4021
4022 WARN_ON(start > offset + len);
4023
4024 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4025 if ((copy = end - offset) > 0) {
4026 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4027 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4028 return -EMSGSIZE;
4029
4030 if (copy > len)
4031 copy = len;
4032 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4033 frag->page_offset+offset-start);
4034 elt++;
4035 if (!(len -= copy))
4036 return elt;
4037 offset += copy;
4038 }
4039 start = end;
4040 }
4041
4042 skb_walk_frags(skb, frag_iter) {
4043 int end, ret;
4044
4045 WARN_ON(start > offset + len);
4046
4047 end = start + frag_iter->len;
4048 if ((copy = end - offset) > 0) {
4049 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4050 return -EMSGSIZE;
4051
4052 if (copy > len)
4053 copy = len;
4054 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4055 copy, recursion_level + 1);
4056 if (unlikely(ret < 0))
4057 return ret;
4058 elt += ret;
4059 if ((len -= copy) == 0)
4060 return elt;
4061 offset += copy;
4062 }
4063 start = end;
4064 }
4065 BUG_ON(len);
4066 return elt;
4067}
4068
4069/**
4070 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4071 * @skb: Socket buffer containing the buffers to be mapped
4072 * @sg: The scatter-gather list to map into
4073 * @offset: The offset into the buffer's contents to start mapping
4074 * @len: Length of buffer space to be mapped
4075 *
4076 * Fill the specified scatter-gather list with mappings/pointers into a
4077 * region of the buffer space attached to a socket buffer. Returns either
4078 * the number of scatterlist items used, or -EMSGSIZE if the contents
4079 * could not fit.
4080 */
4081int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4082{
4083 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4084
4085 if (nsg <= 0)
4086 return nsg;
4087
4088 sg_mark_end(&sg[nsg - 1]);
4089
4090 return nsg;
4091}
4092EXPORT_SYMBOL_GPL(skb_to_sgvec);
4093
4094/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4095 * sglist without mark the sg which contain last skb data as the end.
4096 * So the caller can mannipulate sg list as will when padding new data after
4097 * the first call without calling sg_unmark_end to expend sg list.
4098 *
4099 * Scenario to use skb_to_sgvec_nomark:
4100 * 1. sg_init_table
4101 * 2. skb_to_sgvec_nomark(payload1)
4102 * 3. skb_to_sgvec_nomark(payload2)
4103 *
4104 * This is equivalent to:
4105 * 1. sg_init_table
4106 * 2. skb_to_sgvec(payload1)
4107 * 3. sg_unmark_end
4108 * 4. skb_to_sgvec(payload2)
4109 *
4110 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4111 * is more preferable.
4112 */
4113int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4114 int offset, int len)
4115{
4116 return __skb_to_sgvec(skb, sg, offset, len, 0);
4117}
4118EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4119
4120
4121
4122/**
4123 * skb_cow_data - Check that a socket buffer's data buffers are writable
4124 * @skb: The socket buffer to check.
4125 * @tailbits: Amount of trailing space to be added
4126 * @trailer: Returned pointer to the skb where the @tailbits space begins
4127 *
4128 * Make sure that the data buffers attached to a socket buffer are
4129 * writable. If they are not, private copies are made of the data buffers
4130 * and the socket buffer is set to use these instead.
4131 *
4132 * If @tailbits is given, make sure that there is space to write @tailbits
4133 * bytes of data beyond current end of socket buffer. @trailer will be
4134 * set to point to the skb in which this space begins.
4135 *
4136 * The number of scatterlist elements required to completely map the
4137 * COW'd and extended socket buffer will be returned.
4138 */
4139int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4140{
4141 int copyflag;
4142 int elt;
4143 struct sk_buff *skb1, **skb_p;
4144
4145 /* If skb is cloned or its head is paged, reallocate
4146 * head pulling out all the pages (pages are considered not writable
4147 * at the moment even if they are anonymous).
4148 */
4149 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4150 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4151 return -ENOMEM;
4152
4153 /* Easy case. Most of packets will go this way. */
4154 if (!skb_has_frag_list(skb)) {
4155 /* A little of trouble, not enough of space for trailer.
4156 * This should not happen, when stack is tuned to generate
4157 * good frames. OK, on miss we reallocate and reserve even more
4158 * space, 128 bytes is fair. */
4159
4160 if (skb_tailroom(skb) < tailbits &&
4161 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4162 return -ENOMEM;
4163
4164 /* Voila! */
4165 *trailer = skb;
4166 return 1;
4167 }
4168
4169 /* Misery. We are in troubles, going to mincer fragments... */
4170
4171 elt = 1;
4172 skb_p = &skb_shinfo(skb)->frag_list;
4173 copyflag = 0;
4174
4175 while ((skb1 = *skb_p) != NULL) {
4176 int ntail = 0;
4177
4178 /* The fragment is partially pulled by someone,
4179 * this can happen on input. Copy it and everything
4180 * after it. */
4181
4182 if (skb_shared(skb1))
4183 copyflag = 1;
4184
4185 /* If the skb is the last, worry about trailer. */
4186
4187 if (skb1->next == NULL && tailbits) {
4188 if (skb_shinfo(skb1)->nr_frags ||
4189 skb_has_frag_list(skb1) ||
4190 skb_tailroom(skb1) < tailbits)
4191 ntail = tailbits + 128;
4192 }
4193
4194 if (copyflag ||
4195 skb_cloned(skb1) ||
4196 ntail ||
4197 skb_shinfo(skb1)->nr_frags ||
4198 skb_has_frag_list(skb1)) {
4199 struct sk_buff *skb2;
4200
4201 /* Fuck, we are miserable poor guys... */
4202 if (ntail == 0)
4203 skb2 = skb_copy(skb1, GFP_ATOMIC);
4204 else
4205 skb2 = skb_copy_expand(skb1,
4206 skb_headroom(skb1),
4207 ntail,
4208 GFP_ATOMIC);
4209 if (unlikely(skb2 == NULL))
4210 return -ENOMEM;
4211
4212 if (skb1->sk)
4213 skb_set_owner_w(skb2, skb1->sk);
4214
4215 /* Looking around. Are we still alive?
4216 * OK, link new skb, drop old one */
4217
4218 skb2->next = skb1->next;
4219 *skb_p = skb2;
4220 kfree_skb(skb1);
4221 skb1 = skb2;
4222 }
4223 elt++;
4224 *trailer = skb1;
4225 skb_p = &skb1->next;
4226 }
4227
4228 return elt;
4229}
4230EXPORT_SYMBOL_GPL(skb_cow_data);
4231
4232static void sock_rmem_free(struct sk_buff *skb)
4233{
4234 struct sock *sk = skb->sk;
4235
4236 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4237}
4238
4239static void skb_set_err_queue(struct sk_buff *skb)
4240{
4241 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4242 * So, it is safe to (mis)use it to mark skbs on the error queue.
4243 */
4244 skb->pkt_type = PACKET_OUTGOING;
4245 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4246}
4247
4248/*
4249 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4250 */
4251int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4252{
4253 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4254 (unsigned int)sk->sk_rcvbuf)
4255 return -ENOMEM;
4256
4257 skb_orphan(skb);
4258 skb->sk = sk;
4259 skb->destructor = sock_rmem_free;
4260 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4261 skb_set_err_queue(skb);
4262
4263 /* before exiting rcu section, make sure dst is refcounted */
4264 skb_dst_force(skb);
4265
4266 skb_queue_tail(&sk->sk_error_queue, skb);
4267 if (!sock_flag(sk, SOCK_DEAD))
4268 sk->sk_error_report(sk);
4269 return 0;
4270}
4271EXPORT_SYMBOL(sock_queue_err_skb);
4272
4273static bool is_icmp_err_skb(const struct sk_buff *skb)
4274{
4275 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4276 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4277}
4278
4279struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4280{
4281 struct sk_buff_head *q = &sk->sk_error_queue;
4282 struct sk_buff *skb, *skb_next = NULL;
4283 bool icmp_next = false;
4284 unsigned long flags;
4285
4286 spin_lock_irqsave(&q->lock, flags);
4287 skb = __skb_dequeue(q);
4288 if (skb && (skb_next = skb_peek(q))) {
4289 icmp_next = is_icmp_err_skb(skb_next);
4290 if (icmp_next)
4291 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4292 }
4293 spin_unlock_irqrestore(&q->lock, flags);
4294
4295 if (is_icmp_err_skb(skb) && !icmp_next)
4296 sk->sk_err = 0;
4297
4298 if (skb_next)
4299 sk->sk_error_report(sk);
4300
4301 return skb;
4302}
4303EXPORT_SYMBOL(sock_dequeue_err_skb);
4304
4305/**
4306 * skb_clone_sk - create clone of skb, and take reference to socket
4307 * @skb: the skb to clone
4308 *
4309 * This function creates a clone of a buffer that holds a reference on
4310 * sk_refcnt. Buffers created via this function are meant to be
4311 * returned using sock_queue_err_skb, or free via kfree_skb.
4312 *
4313 * When passing buffers allocated with this function to sock_queue_err_skb
4314 * it is necessary to wrap the call with sock_hold/sock_put in order to
4315 * prevent the socket from being released prior to being enqueued on
4316 * the sk_error_queue.
4317 */
4318struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4319{
4320 struct sock *sk = skb->sk;
4321 struct sk_buff *clone;
4322
4323 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4324 return NULL;
4325
4326 clone = skb_clone(skb, GFP_ATOMIC);
4327 if (!clone) {
4328 sock_put(sk);
4329 return NULL;
4330 }
4331
4332 clone->sk = sk;
4333 clone->destructor = sock_efree;
4334
4335 return clone;
4336}
4337EXPORT_SYMBOL(skb_clone_sk);
4338
4339static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4340 struct sock *sk,
4341 int tstype,
4342 bool opt_stats)
4343{
4344 struct sock_exterr_skb *serr;
4345 int err;
4346
4347 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4348
4349 serr = SKB_EXT_ERR(skb);
4350 memset(serr, 0, sizeof(*serr));
4351 serr->ee.ee_errno = ENOMSG;
4352 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4353 serr->ee.ee_info = tstype;
4354 serr->opt_stats = opt_stats;
4355 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4356 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4357 serr->ee.ee_data = skb_shinfo(skb)->tskey;
4358 if (sk->sk_protocol == IPPROTO_TCP &&
4359 sk->sk_type == SOCK_STREAM)
4360 serr->ee.ee_data -= sk->sk_tskey;
4361 }
4362
4363 err = sock_queue_err_skb(sk, skb);
4364
4365 if (err)
4366 kfree_skb(skb);
4367}
4368
4369static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4370{
4371 bool ret;
4372
4373 if (likely(sysctl_tstamp_allow_data || tsonly))
4374 return true;
4375
4376 read_lock_bh(&sk->sk_callback_lock);
4377 ret = sk->sk_socket && sk->sk_socket->file &&
4378 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4379 read_unlock_bh(&sk->sk_callback_lock);
4380 return ret;
4381}
4382
4383void skb_complete_tx_timestamp(struct sk_buff *skb,
4384 struct skb_shared_hwtstamps *hwtstamps)
4385{
4386 struct sock *sk = skb->sk;
4387
4388 if (!skb_may_tx_timestamp(sk, false))
4389 goto err;
4390
4391 /* Take a reference to prevent skb_orphan() from freeing the socket,
4392 * but only if the socket refcount is not zero.
4393 */
4394 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4395 *skb_hwtstamps(skb) = *hwtstamps;
4396 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4397 sock_put(sk);
4398 return;
4399 }
4400
4401err:
4402 kfree_skb(skb);
4403}
4404EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4405
4406void __skb_tstamp_tx(struct sk_buff *orig_skb,
4407 struct skb_shared_hwtstamps *hwtstamps,
4408 struct sock *sk, int tstype)
4409{
4410 struct sk_buff *skb;
4411 bool tsonly, opt_stats = false;
4412
4413 if (!sk)
4414 return;
4415
4416 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4417 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4418 return;
4419
4420 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4421 if (!skb_may_tx_timestamp(sk, tsonly))
4422 return;
4423
4424 if (tsonly) {
4425#ifdef CONFIG_INET
4426 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4427 sk->sk_protocol == IPPROTO_TCP &&
4428 sk->sk_type == SOCK_STREAM) {
4429 skb = tcp_get_timestamping_opt_stats(sk);
4430 opt_stats = true;
4431 } else
4432#endif
4433 skb = alloc_skb(0, GFP_ATOMIC);
4434 } else {
4435 skb = skb_clone(orig_skb, GFP_ATOMIC);
4436 }
4437 if (!skb)
4438 return;
4439
4440 if (tsonly) {
4441 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4442 SKBTX_ANY_TSTAMP;
4443 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4444 }
4445
4446 if (hwtstamps)
4447 *skb_hwtstamps(skb) = *hwtstamps;
4448 else
4449 skb->tstamp = ktime_get_real();
4450
4451 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4452}
4453EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4454
4455void skb_tstamp_tx(struct sk_buff *orig_skb,
4456 struct skb_shared_hwtstamps *hwtstamps)
4457{
4458 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4459 SCM_TSTAMP_SND);
4460}
4461EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4462
4463void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4464{
4465 struct sock *sk = skb->sk;
4466 struct sock_exterr_skb *serr;
4467 int err = 1;
4468
4469 skb->wifi_acked_valid = 1;
4470 skb->wifi_acked = acked;
4471
4472 serr = SKB_EXT_ERR(skb);
4473 memset(serr, 0, sizeof(*serr));
4474 serr->ee.ee_errno = ENOMSG;
4475 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4476
4477 /* Take a reference to prevent skb_orphan() from freeing the socket,
4478 * but only if the socket refcount is not zero.
4479 */
4480 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4481 err = sock_queue_err_skb(sk, skb);
4482 sock_put(sk);
4483 }
4484 if (err)
4485 kfree_skb(skb);
4486}
4487EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4488
4489/**
4490 * skb_partial_csum_set - set up and verify partial csum values for packet
4491 * @skb: the skb to set
4492 * @start: the number of bytes after skb->data to start checksumming.
4493 * @off: the offset from start to place the checksum.
4494 *
4495 * For untrusted partially-checksummed packets, we need to make sure the values
4496 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4497 *
4498 * This function checks and sets those values and skb->ip_summed: if this
4499 * returns false you should drop the packet.
4500 */
4501bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4502{
4503 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4504 u32 csum_start = skb_headroom(skb) + (u32)start;
4505
4506 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4507 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4508 start, off, skb_headroom(skb), skb_headlen(skb));
4509 return false;
4510 }
4511 skb->ip_summed = CHECKSUM_PARTIAL;
4512 skb->csum_start = csum_start;
4513 skb->csum_offset = off;
4514 skb_set_transport_header(skb, start);
4515 return true;
4516}
4517EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4518
4519static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4520 unsigned int max)
4521{
4522 if (skb_headlen(skb) >= len)
4523 return 0;
4524
4525 /* If we need to pullup then pullup to the max, so we
4526 * won't need to do it again.
4527 */
4528 if (max > skb->len)
4529 max = skb->len;
4530
4531 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4532 return -ENOMEM;
4533
4534 if (skb_headlen(skb) < len)
4535 return -EPROTO;
4536
4537 return 0;
4538}
4539
4540#define MAX_TCP_HDR_LEN (15 * 4)
4541
4542static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4543 typeof(IPPROTO_IP) proto,
4544 unsigned int off)
4545{
4546 switch (proto) {
4547 int err;
4548
4549 case IPPROTO_TCP:
4550 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4551 off + MAX_TCP_HDR_LEN);
4552 if (!err && !skb_partial_csum_set(skb, off,
4553 offsetof(struct tcphdr,
4554 check)))
4555 err = -EPROTO;
4556 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4557
4558 case IPPROTO_UDP:
4559 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4560 off + sizeof(struct udphdr));
4561 if (!err && !skb_partial_csum_set(skb, off,
4562 offsetof(struct udphdr,
4563 check)))
4564 err = -EPROTO;
4565 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4566 }
4567
4568 return ERR_PTR(-EPROTO);
4569}
4570
4571/* This value should be large enough to cover a tagged ethernet header plus
4572 * maximally sized IP and TCP or UDP headers.
4573 */
4574#define MAX_IP_HDR_LEN 128
4575
4576static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4577{
4578 unsigned int off;
4579 bool fragment;
4580 __sum16 *csum;
4581 int err;
4582
4583 fragment = false;
4584
4585 err = skb_maybe_pull_tail(skb,
4586 sizeof(struct iphdr),
4587 MAX_IP_HDR_LEN);
4588 if (err < 0)
4589 goto out;
4590
4591 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4592 fragment = true;
4593
4594 off = ip_hdrlen(skb);
4595
4596 err = -EPROTO;
4597
4598 if (fragment)
4599 goto out;
4600
4601 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4602 if (IS_ERR(csum))
4603 return PTR_ERR(csum);
4604
4605 if (recalculate)
4606 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4607 ip_hdr(skb)->daddr,
4608 skb->len - off,
4609 ip_hdr(skb)->protocol, 0);
4610 err = 0;
4611
4612out:
4613 return err;
4614}
4615
4616/* This value should be large enough to cover a tagged ethernet header plus
4617 * an IPv6 header, all options, and a maximal TCP or UDP header.
4618 */
4619#define MAX_IPV6_HDR_LEN 256
4620
4621#define OPT_HDR(type, skb, off) \
4622 (type *)(skb_network_header(skb) + (off))
4623
4624static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4625{
4626 int err;
4627 u8 nexthdr;
4628 unsigned int off;
4629 unsigned int len;
4630 bool fragment;
4631 bool done;
4632 __sum16 *csum;
4633
4634 fragment = false;
4635 done = false;
4636
4637 off = sizeof(struct ipv6hdr);
4638
4639 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4640 if (err < 0)
4641 goto out;
4642
4643 nexthdr = ipv6_hdr(skb)->nexthdr;
4644
4645 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4646 while (off <= len && !done) {
4647 switch (nexthdr) {
4648 case IPPROTO_DSTOPTS:
4649 case IPPROTO_HOPOPTS:
4650 case IPPROTO_ROUTING: {
4651 struct ipv6_opt_hdr *hp;
4652
4653 err = skb_maybe_pull_tail(skb,
4654 off +
4655 sizeof(struct ipv6_opt_hdr),
4656 MAX_IPV6_HDR_LEN);
4657 if (err < 0)
4658 goto out;
4659
4660 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4661 nexthdr = hp->nexthdr;
4662 off += ipv6_optlen(hp);
4663 break;
4664 }
4665 case IPPROTO_AH: {
4666 struct ip_auth_hdr *hp;
4667
4668 err = skb_maybe_pull_tail(skb,
4669 off +
4670 sizeof(struct ip_auth_hdr),
4671 MAX_IPV6_HDR_LEN);
4672 if (err < 0)
4673 goto out;
4674
4675 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4676 nexthdr = hp->nexthdr;
4677 off += ipv6_authlen(hp);
4678 break;
4679 }
4680 case IPPROTO_FRAGMENT: {
4681 struct frag_hdr *hp;
4682
4683 err = skb_maybe_pull_tail(skb,
4684 off +
4685 sizeof(struct frag_hdr),
4686 MAX_IPV6_HDR_LEN);
4687 if (err < 0)
4688 goto out;
4689
4690 hp = OPT_HDR(struct frag_hdr, skb, off);
4691
4692 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4693 fragment = true;
4694
4695 nexthdr = hp->nexthdr;
4696 off += sizeof(struct frag_hdr);
4697 break;
4698 }
4699 default:
4700 done = true;
4701 break;
4702 }
4703 }
4704
4705 err = -EPROTO;
4706
4707 if (!done || fragment)
4708 goto out;
4709
4710 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4711 if (IS_ERR(csum))
4712 return PTR_ERR(csum);
4713
4714 if (recalculate)
4715 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4716 &ipv6_hdr(skb)->daddr,
4717 skb->len - off, nexthdr, 0);
4718 err = 0;
4719
4720out:
4721 return err;
4722}
4723
4724/**
4725 * skb_checksum_setup - set up partial checksum offset
4726 * @skb: the skb to set up
4727 * @recalculate: if true the pseudo-header checksum will be recalculated
4728 */
4729int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4730{
4731 int err;
4732
4733 switch (skb->protocol) {
4734 case htons(ETH_P_IP):
4735 err = skb_checksum_setup_ipv4(skb, recalculate);
4736 break;
4737
4738 case htons(ETH_P_IPV6):
4739 err = skb_checksum_setup_ipv6(skb, recalculate);
4740 break;
4741
4742 default:
4743 err = -EPROTO;
4744 break;
4745 }
4746
4747 return err;
4748}
4749EXPORT_SYMBOL(skb_checksum_setup);
4750
4751/**
4752 * skb_checksum_maybe_trim - maybe trims the given skb
4753 * @skb: the skb to check
4754 * @transport_len: the data length beyond the network header
4755 *
4756 * Checks whether the given skb has data beyond the given transport length.
4757 * If so, returns a cloned skb trimmed to this transport length.
4758 * Otherwise returns the provided skb. Returns NULL in error cases
4759 * (e.g. transport_len exceeds skb length or out-of-memory).
4760 *
4761 * Caller needs to set the skb transport header and free any returned skb if it
4762 * differs from the provided skb.
4763 */
4764static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4765 unsigned int transport_len)
4766{
4767 struct sk_buff *skb_chk;
4768 unsigned int len = skb_transport_offset(skb) + transport_len;
4769 int ret;
4770
4771 if (skb->len < len)
4772 return NULL;
4773 else if (skb->len == len)
4774 return skb;
4775
4776 skb_chk = skb_clone(skb, GFP_ATOMIC);
4777 if (!skb_chk)
4778 return NULL;
4779
4780 ret = pskb_trim_rcsum(skb_chk, len);
4781 if (ret) {
4782 kfree_skb(skb_chk);
4783 return NULL;
4784 }
4785
4786 return skb_chk;
4787}
4788
4789/**
4790 * skb_checksum_trimmed - validate checksum of an skb
4791 * @skb: the skb to check
4792 * @transport_len: the data length beyond the network header
4793 * @skb_chkf: checksum function to use
4794 *
4795 * Applies the given checksum function skb_chkf to the provided skb.
4796 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4797 *
4798 * If the skb has data beyond the given transport length, then a
4799 * trimmed & cloned skb is checked and returned.
4800 *
4801 * Caller needs to set the skb transport header and free any returned skb if it
4802 * differs from the provided skb.
4803 */
4804struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4805 unsigned int transport_len,
4806 __sum16(*skb_chkf)(struct sk_buff *skb))
4807{
4808 struct sk_buff *skb_chk;
4809 unsigned int offset = skb_transport_offset(skb);
4810 __sum16 ret;
4811
4812 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4813 if (!skb_chk)
4814 goto err;
4815
4816 if (!pskb_may_pull(skb_chk, offset))
4817 goto err;
4818
4819 skb_pull_rcsum(skb_chk, offset);
4820 ret = skb_chkf(skb_chk);
4821 skb_push_rcsum(skb_chk, offset);
4822
4823 if (ret)
4824 goto err;
4825
4826 return skb_chk;
4827
4828err:
4829 if (skb_chk && skb_chk != skb)
4830 kfree_skb(skb_chk);
4831
4832 return NULL;
4833
4834}
4835EXPORT_SYMBOL(skb_checksum_trimmed);
4836
4837void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4838{
4839 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4840 skb->dev->name);
4841}
4842EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4843
4844void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4845{
4846 if (head_stolen) {
4847 skb_release_head_state(skb);
4848 kmem_cache_free(skbuff_head_cache, skb);
4849 } else {
4850 __kfree_skb(skb);
4851 }
4852}
4853EXPORT_SYMBOL(kfree_skb_partial);
4854
4855/**
4856 * skb_try_coalesce - try to merge skb to prior one
4857 * @to: prior buffer
4858 * @from: buffer to add
4859 * @fragstolen: pointer to boolean
4860 * @delta_truesize: how much more was allocated than was requested
4861 */
4862bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4863 bool *fragstolen, int *delta_truesize)
4864{
4865 struct skb_shared_info *to_shinfo, *from_shinfo;
4866 int i, delta, len = from->len;
4867
4868 *fragstolen = false;
4869
4870 if (skb_cloned(to))
4871 return false;
4872
4873 if (len <= skb_tailroom(to)) {
4874 if (len)
4875 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4876 *delta_truesize = 0;
4877 return true;
4878 }
4879
4880 to_shinfo = skb_shinfo(to);
4881 from_shinfo = skb_shinfo(from);
4882 if (to_shinfo->frag_list || from_shinfo->frag_list)
4883 return false;
4884 if (skb_zcopy(to) || skb_zcopy(from))
4885 return false;
4886
4887 if (skb_headlen(from) != 0) {
4888 struct page *page;
4889 unsigned int offset;
4890
4891 if (to_shinfo->nr_frags +
4892 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
4893 return false;
4894
4895 if (skb_head_is_locked(from))
4896 return false;
4897
4898 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4899
4900 page = virt_to_head_page(from->head);
4901 offset = from->data - (unsigned char *)page_address(page);
4902
4903 skb_fill_page_desc(to, to_shinfo->nr_frags,
4904 page, offset, skb_headlen(from));
4905 *fragstolen = true;
4906 } else {
4907 if (to_shinfo->nr_frags +
4908 from_shinfo->nr_frags > MAX_SKB_FRAGS)
4909 return false;
4910
4911 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4912 }
4913
4914 WARN_ON_ONCE(delta < len);
4915
4916 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4917 from_shinfo->frags,
4918 from_shinfo->nr_frags * sizeof(skb_frag_t));
4919 to_shinfo->nr_frags += from_shinfo->nr_frags;
4920
4921 if (!skb_cloned(from))
4922 from_shinfo->nr_frags = 0;
4923
4924 /* if the skb is not cloned this does nothing
4925 * since we set nr_frags to 0.
4926 */
4927 for (i = 0; i < from_shinfo->nr_frags; i++)
4928 __skb_frag_ref(&from_shinfo->frags[i]);
4929
4930 to->truesize += delta;
4931 to->len += len;
4932 to->data_len += len;
4933
4934 *delta_truesize = delta;
4935 return true;
4936}
4937EXPORT_SYMBOL(skb_try_coalesce);
4938
4939/**
4940 * skb_scrub_packet - scrub an skb
4941 *
4942 * @skb: buffer to clean
4943 * @xnet: packet is crossing netns
4944 *
4945 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4946 * into/from a tunnel. Some information have to be cleared during these
4947 * operations.
4948 * skb_scrub_packet can also be used to clean a skb before injecting it in
4949 * another namespace (@xnet == true). We have to clear all information in the
4950 * skb that could impact namespace isolation.
4951 */
4952void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4953{
4954 skb->pkt_type = PACKET_HOST;
4955 skb->skb_iif = 0;
4956 skb->ignore_df = 0;
4957 skb_dst_drop(skb);
4958 secpath_reset(skb);
4959 nf_reset(skb);
4960 nf_reset_trace(skb);
4961
4962#ifdef CONFIG_NET_SWITCHDEV
4963 skb->offload_fwd_mark = 0;
4964 skb->offload_l3_fwd_mark = 0;
4965#endif
4966
4967 if (!xnet)
4968 return;
4969
4970 ipvs_reset(skb);
4971 skb->mark = 0;
4972 skb->tstamp = 0;
4973}
4974EXPORT_SYMBOL_GPL(skb_scrub_packet);
4975
4976/**
4977 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4978 *
4979 * @skb: GSO skb
4980 *
4981 * skb_gso_transport_seglen is used to determine the real size of the
4982 * individual segments, including Layer4 headers (TCP/UDP).
4983 *
4984 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4985 */
4986static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4987{
4988 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4989 unsigned int thlen = 0;
4990
4991 if (skb->encapsulation) {
4992 thlen = skb_inner_transport_header(skb) -
4993 skb_transport_header(skb);
4994
4995 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4996 thlen += inner_tcp_hdrlen(skb);
4997 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4998 thlen = tcp_hdrlen(skb);
4999 } else if (unlikely(skb_is_gso_sctp(skb))) {
5000 thlen = sizeof(struct sctphdr);
5001 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5002 thlen = sizeof(struct udphdr);
5003 }
5004 /* UFO sets gso_size to the size of the fragmentation
5005 * payload, i.e. the size of the L4 (UDP) header is already
5006 * accounted for.
5007 */
5008 return thlen + shinfo->gso_size;
5009}
5010
5011/**
5012 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5013 *
5014 * @skb: GSO skb
5015 *
5016 * skb_gso_network_seglen is used to determine the real size of the
5017 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5018 *
5019 * The MAC/L2 header is not accounted for.
5020 */
5021static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5022{
5023 unsigned int hdr_len = skb_transport_header(skb) -
5024 skb_network_header(skb);
5025
5026 return hdr_len + skb_gso_transport_seglen(skb);
5027}
5028
5029/**
5030 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5031 *
5032 * @skb: GSO skb
5033 *
5034 * skb_gso_mac_seglen is used to determine the real size of the
5035 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5036 * headers (TCP/UDP).
5037 */
5038static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5039{
5040 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5041
5042 return hdr_len + skb_gso_transport_seglen(skb);
5043}
5044
5045/**
5046 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5047 *
5048 * There are a couple of instances where we have a GSO skb, and we
5049 * want to determine what size it would be after it is segmented.
5050 *
5051 * We might want to check:
5052 * - L3+L4+payload size (e.g. IP forwarding)
5053 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5054 *
5055 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5056 *
5057 * @skb: GSO skb
5058 *
5059 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5060 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5061 *
5062 * @max_len: The maximum permissible length.
5063 *
5064 * Returns true if the segmented length <= max length.
5065 */
5066static inline bool skb_gso_size_check(const struct sk_buff *skb,
5067 unsigned int seg_len,
5068 unsigned int max_len) {
5069 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5070 const struct sk_buff *iter;
5071
5072 if (shinfo->gso_size != GSO_BY_FRAGS)
5073 return seg_len <= max_len;
5074
5075 /* Undo this so we can re-use header sizes */
5076 seg_len -= GSO_BY_FRAGS;
5077
5078 skb_walk_frags(skb, iter) {
5079 if (seg_len + skb_headlen(iter) > max_len)
5080 return false;
5081 }
5082
5083 return true;
5084}
5085
5086/**
5087 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5088 *
5089 * @skb: GSO skb
5090 * @mtu: MTU to validate against
5091 *
5092 * skb_gso_validate_network_len validates if a given skb will fit a
5093 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5094 * payload.
5095 */
5096bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5097{
5098 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5099}
5100EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5101
5102/**
5103 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5104 *
5105 * @skb: GSO skb
5106 * @len: length to validate against
5107 *
5108 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5109 * length once split, including L2, L3 and L4 headers and the payload.
5110 */
5111bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5112{
5113 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5114}
5115EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5116
5117static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5118{
5119 int mac_len, meta_len;
5120 void *meta;
5121
5122 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5123 kfree_skb(skb);
5124 return NULL;
5125 }
5126
5127 mac_len = skb->data - skb_mac_header(skb);
5128 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5129 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5130 mac_len - VLAN_HLEN - ETH_TLEN);
5131 }
5132
5133 meta_len = skb_metadata_len(skb);
5134 if (meta_len) {
5135 meta = skb_metadata_end(skb) - meta_len;
5136 memmove(meta + VLAN_HLEN, meta, meta_len);
5137 }
5138
5139 skb->mac_header += VLAN_HLEN;
5140 return skb;
5141}
5142
5143struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5144{
5145 struct vlan_hdr *vhdr;
5146 u16 vlan_tci;
5147
5148 if (unlikely(skb_vlan_tag_present(skb))) {
5149 /* vlan_tci is already set-up so leave this for another time */
5150 return skb;
5151 }
5152
5153 skb = skb_share_check(skb, GFP_ATOMIC);
5154 if (unlikely(!skb))
5155 goto err_free;
5156
5157 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5158 goto err_free;
5159
5160 vhdr = (struct vlan_hdr *)skb->data;
5161 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5162 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5163
5164 skb_pull_rcsum(skb, VLAN_HLEN);
5165 vlan_set_encap_proto(skb, vhdr);
5166
5167 skb = skb_reorder_vlan_header(skb);
5168 if (unlikely(!skb))
5169 goto err_free;
5170
5171 skb_reset_network_header(skb);
5172 skb_reset_transport_header(skb);
5173 skb_reset_mac_len(skb);
5174
5175 return skb;
5176
5177err_free:
5178 kfree_skb(skb);
5179 return NULL;
5180}
5181EXPORT_SYMBOL(skb_vlan_untag);
5182
5183int skb_ensure_writable(struct sk_buff *skb, int write_len)
5184{
5185 if (!pskb_may_pull(skb, write_len))
5186 return -ENOMEM;
5187
5188 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5189 return 0;
5190
5191 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5192}
5193EXPORT_SYMBOL(skb_ensure_writable);
5194
5195/* remove VLAN header from packet and update csum accordingly.
5196 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5197 */
5198int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5199{
5200 struct vlan_hdr *vhdr;
5201 int offset = skb->data - skb_mac_header(skb);
5202 int err;
5203
5204 if (WARN_ONCE(offset,
5205 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5206 offset)) {
5207 return -EINVAL;
5208 }
5209
5210 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5211 if (unlikely(err))
5212 return err;
5213
5214 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5215
5216 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5217 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5218
5219 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5220 __skb_pull(skb, VLAN_HLEN);
5221
5222 vlan_set_encap_proto(skb, vhdr);
5223 skb->mac_header += VLAN_HLEN;
5224
5225 if (skb_network_offset(skb) < ETH_HLEN)
5226 skb_set_network_header(skb, ETH_HLEN);
5227
5228 skb_reset_mac_len(skb);
5229
5230 return err;
5231}
5232EXPORT_SYMBOL(__skb_vlan_pop);
5233
5234/* Pop a vlan tag either from hwaccel or from payload.
5235 * Expects skb->data at mac header.
5236 */
5237int skb_vlan_pop(struct sk_buff *skb)
5238{
5239 u16 vlan_tci;
5240 __be16 vlan_proto;
5241 int err;
5242
5243 if (likely(skb_vlan_tag_present(skb))) {
5244 __vlan_hwaccel_clear_tag(skb);
5245 } else {
5246 if (unlikely(!eth_type_vlan(skb->protocol)))
5247 return 0;
5248
5249 err = __skb_vlan_pop(skb, &vlan_tci);
5250 if (err)
5251 return err;
5252 }
5253 /* move next vlan tag to hw accel tag */
5254 if (likely(!eth_type_vlan(skb->protocol)))
5255 return 0;
5256
5257 vlan_proto = skb->protocol;
5258 err = __skb_vlan_pop(skb, &vlan_tci);
5259 if (unlikely(err))
5260 return err;
5261
5262 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5263 return 0;
5264}
5265EXPORT_SYMBOL(skb_vlan_pop);
5266
5267/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5268 * Expects skb->data at mac header.
5269 */
5270int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5271{
5272 if (skb_vlan_tag_present(skb)) {
5273 int offset = skb->data - skb_mac_header(skb);
5274 int err;
5275
5276 if (WARN_ONCE(offset,
5277 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5278 offset)) {
5279 return -EINVAL;
5280 }
5281
5282 err = __vlan_insert_tag(skb, skb->vlan_proto,
5283 skb_vlan_tag_get(skb));
5284 if (err)
5285 return err;
5286
5287 skb->protocol = skb->vlan_proto;
5288 skb->mac_len += VLAN_HLEN;
5289
5290 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5291 }
5292 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5293 return 0;
5294}
5295EXPORT_SYMBOL(skb_vlan_push);
5296
5297/**
5298 * alloc_skb_with_frags - allocate skb with page frags
5299 *
5300 * @header_len: size of linear part
5301 * @data_len: needed length in frags
5302 * @max_page_order: max page order desired.
5303 * @errcode: pointer to error code if any
5304 * @gfp_mask: allocation mask
5305 *
5306 * This can be used to allocate a paged skb, given a maximal order for frags.
5307 */
5308struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5309 unsigned long data_len,
5310 int max_page_order,
5311 int *errcode,
5312 gfp_t gfp_mask)
5313{
5314 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5315 unsigned long chunk;
5316 struct sk_buff *skb;
5317 struct page *page;
5318 int i;
5319
5320 *errcode = -EMSGSIZE;
5321 /* Note this test could be relaxed, if we succeed to allocate
5322 * high order pages...
5323 */
5324 if (npages > MAX_SKB_FRAGS)
5325 return NULL;
5326
5327 *errcode = -ENOBUFS;
5328 skb = alloc_skb(header_len, gfp_mask);
5329 if (!skb)
5330 return NULL;
5331
5332 skb->truesize += npages << PAGE_SHIFT;
5333
5334 for (i = 0; npages > 0; i++) {
5335 int order = max_page_order;
5336
5337 while (order) {
5338 if (npages >= 1 << order) {
5339 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
5340 __GFP_COMP |
5341 __GFP_NOWARN,
5342 order);
5343 if (page)
5344 goto fill_page;
5345 /* Do not retry other high order allocations */
5346 order = 1;
5347 max_page_order = 0;
5348 }
5349 order--;
5350 }
5351 page = alloc_page(gfp_mask);
5352 if (!page)
5353 goto failure;
5354fill_page:
5355 chunk = min_t(unsigned long, data_len,
5356 PAGE_SIZE << order);
5357 skb_fill_page_desc(skb, i, page, 0, chunk);
5358 data_len -= chunk;
5359 npages -= 1 << order;
5360 }
5361 return skb;
5362
5363failure:
5364 kfree_skb(skb);
5365 return NULL;
5366}
5367EXPORT_SYMBOL(alloc_skb_with_frags);
5368
5369/* carve out the first off bytes from skb when off < headlen */
5370static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5371 const int headlen, gfp_t gfp_mask)
5372{
5373 int i;
5374 int size = skb_end_offset(skb);
5375 int new_hlen = headlen - off;
5376 u8 *data;
5377
5378 size = SKB_DATA_ALIGN(size);
5379
5380 if (skb_pfmemalloc(skb))
5381 gfp_mask |= __GFP_MEMALLOC;
5382 data = kmalloc_reserve(size +
5383 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5384 gfp_mask, NUMA_NO_NODE, NULL);
5385 if (!data)
5386 return -ENOMEM;
5387
5388 size = SKB_WITH_OVERHEAD(ksize(data));
5389
5390 /* Copy real data, and all frags */
5391 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5392 skb->len -= off;
5393
5394 memcpy((struct skb_shared_info *)(data + size),
5395 skb_shinfo(skb),
5396 offsetof(struct skb_shared_info,
5397 frags[skb_shinfo(skb)->nr_frags]));
5398 if (skb_cloned(skb)) {
5399 /* drop the old head gracefully */
5400 if (skb_orphan_frags(skb, gfp_mask)) {
5401 kfree(data);
5402 return -ENOMEM;
5403 }
5404 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5405 skb_frag_ref(skb, i);
5406 if (skb_has_frag_list(skb))
5407 skb_clone_fraglist(skb);
5408 skb_release_data(skb);
5409 } else {
5410 /* we can reuse existing recount- all we did was
5411 * relocate values
5412 */
5413 skb_free_head(skb);
5414 }
5415
5416 skb->head = data;
5417 skb->data = data;
5418 skb->head_frag = 0;
5419#ifdef NET_SKBUFF_DATA_USES_OFFSET
5420 skb->end = size;
5421#else
5422 skb->end = skb->head + size;
5423#endif
5424 skb_set_tail_pointer(skb, skb_headlen(skb));
5425 skb_headers_offset_update(skb, 0);
5426 skb->cloned = 0;
5427 skb->hdr_len = 0;
5428 skb->nohdr = 0;
5429 atomic_set(&skb_shinfo(skb)->dataref, 1);
5430
5431 return 0;
5432}
5433
5434static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5435
5436/* carve out the first eat bytes from skb's frag_list. May recurse into
5437 * pskb_carve()
5438 */
5439static int pskb_carve_frag_list(struct sk_buff *skb,
5440 struct skb_shared_info *shinfo, int eat,
5441 gfp_t gfp_mask)
5442{
5443 struct sk_buff *list = shinfo->frag_list;
5444 struct sk_buff *clone = NULL;
5445 struct sk_buff *insp = NULL;
5446
5447 do {
5448 if (!list) {
5449 pr_err("Not enough bytes to eat. Want %d\n", eat);
5450 return -EFAULT;
5451 }
5452 if (list->len <= eat) {
5453 /* Eaten as whole. */
5454 eat -= list->len;
5455 list = list->next;
5456 insp = list;
5457 } else {
5458 /* Eaten partially. */
5459 if (skb_shared(list)) {
5460 clone = skb_clone(list, gfp_mask);
5461 if (!clone)
5462 return -ENOMEM;
5463 insp = list->next;
5464 list = clone;
5465 } else {
5466 /* This may be pulled without problems. */
5467 insp = list;
5468 }
5469 if (pskb_carve(list, eat, gfp_mask) < 0) {
5470 kfree_skb(clone);
5471 return -ENOMEM;
5472 }
5473 break;
5474 }
5475 } while (eat);
5476
5477 /* Free pulled out fragments. */
5478 while ((list = shinfo->frag_list) != insp) {
5479 shinfo->frag_list = list->next;
5480 kfree_skb(list);
5481 }
5482 /* And insert new clone at head. */
5483 if (clone) {
5484 clone->next = list;
5485 shinfo->frag_list = clone;
5486 }
5487 return 0;
5488}
5489
5490/* carve off first len bytes from skb. Split line (off) is in the
5491 * non-linear part of skb
5492 */
5493static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5494 int pos, gfp_t gfp_mask)
5495{
5496 int i, k = 0;
5497 int size = skb_end_offset(skb);
5498 u8 *data;
5499 const int nfrags = skb_shinfo(skb)->nr_frags;
5500 struct skb_shared_info *shinfo;
5501
5502 size = SKB_DATA_ALIGN(size);
5503
5504 if (skb_pfmemalloc(skb))
5505 gfp_mask |= __GFP_MEMALLOC;
5506 data = kmalloc_reserve(size +
5507 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5508 gfp_mask, NUMA_NO_NODE, NULL);
5509 if (!data)
5510 return -ENOMEM;
5511
5512 size = SKB_WITH_OVERHEAD(ksize(data));
5513
5514 memcpy((struct skb_shared_info *)(data + size),
5515 skb_shinfo(skb), offsetof(struct skb_shared_info,
5516 frags[skb_shinfo(skb)->nr_frags]));
5517 if (skb_orphan_frags(skb, gfp_mask)) {
5518 kfree(data);
5519 return -ENOMEM;
5520 }
5521 shinfo = (struct skb_shared_info *)(data + size);
5522 for (i = 0; i < nfrags; i++) {
5523 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5524
5525 if (pos + fsize > off) {
5526 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5527
5528 if (pos < off) {
5529 /* Split frag.
5530 * We have two variants in this case:
5531 * 1. Move all the frag to the second
5532 * part, if it is possible. F.e.
5533 * this approach is mandatory for TUX,
5534 * where splitting is expensive.
5535 * 2. Split is accurately. We make this.
5536 */
5537 shinfo->frags[0].page_offset += off - pos;
5538 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5539 }
5540 skb_frag_ref(skb, i);
5541 k++;
5542 }
5543 pos += fsize;
5544 }
5545 shinfo->nr_frags = k;
5546 if (skb_has_frag_list(skb))
5547 skb_clone_fraglist(skb);
5548
5549 if (k == 0) {
5550 /* split line is in frag list */
5551 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5552 }
5553 skb_release_data(skb);
5554
5555 skb->head = data;
5556 skb->head_frag = 0;
5557 skb->data = data;
5558#ifdef NET_SKBUFF_DATA_USES_OFFSET
5559 skb->end = size;
5560#else
5561 skb->end = skb->head + size;
5562#endif
5563 skb_reset_tail_pointer(skb);
5564 skb_headers_offset_update(skb, 0);
5565 skb->cloned = 0;
5566 skb->hdr_len = 0;
5567 skb->nohdr = 0;
5568 skb->len -= off;
5569 skb->data_len = skb->len;
5570 atomic_set(&skb_shinfo(skb)->dataref, 1);
5571 return 0;
5572}
5573
5574/* remove len bytes from the beginning of the skb */
5575static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5576{
5577 int headlen = skb_headlen(skb);
5578
5579 if (len < headlen)
5580 return pskb_carve_inside_header(skb, len, headlen, gfp);
5581 else
5582 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5583}
5584
5585/* Extract to_copy bytes starting at off from skb, and return this in
5586 * a new skb
5587 */
5588struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5589 int to_copy, gfp_t gfp)
5590{
5591 struct sk_buff *clone = skb_clone(skb, gfp);
5592
5593 if (!clone)
5594 return NULL;
5595
5596 if (pskb_carve(clone, off, gfp) < 0 ||
5597 pskb_trim(clone, to_copy)) {
5598 kfree_skb(clone);
5599 return NULL;
5600 }
5601 return clone;
5602}
5603EXPORT_SYMBOL(pskb_extract);
5604
5605/**
5606 * skb_condense - try to get rid of fragments/frag_list if possible
5607 * @skb: buffer
5608 *
5609 * Can be used to save memory before skb is added to a busy queue.
5610 * If packet has bytes in frags and enough tail room in skb->head,
5611 * pull all of them, so that we can free the frags right now and adjust
5612 * truesize.
5613 * Notes:
5614 * We do not reallocate skb->head thus can not fail.
5615 * Caller must re-evaluate skb->truesize if needed.
5616 */
5617void skb_condense(struct sk_buff *skb)
5618{
5619 if (skb->data_len) {
5620 if (skb->data_len > skb->end - skb->tail ||
5621 skb_cloned(skb))
5622 return;
5623
5624 /* Nice, we can free page frag(s) right now */
5625 __pskb_pull_tail(skb, skb->data_len);
5626 }
5627 /* At this point, skb->truesize might be over estimated,
5628 * because skb had a fragment, and fragments do not tell
5629 * their truesize.
5630 * When we pulled its content into skb->head, fragment
5631 * was freed, but __pskb_pull_tail() could not possibly
5632 * adjust skb->truesize, not knowing the frag truesize.
5633 */
5634 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5635}
5636
5637#ifdef CONFIG_SKB_EXTENSIONS
5638static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5639{
5640 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5641}
5642
5643static struct skb_ext *skb_ext_alloc(void)
5644{
5645 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5646
5647 if (new) {
5648 memset(new->offset, 0, sizeof(new->offset));
5649 refcount_set(&new->refcnt, 1);
5650 }
5651
5652 return new;
5653}
5654
5655static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5656 unsigned int old_active)
5657{
5658 struct skb_ext *new;
5659
5660 if (refcount_read(&old->refcnt) == 1)
5661 return old;
5662
5663 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5664 if (!new)
5665 return NULL;
5666
5667 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5668 refcount_set(&new->refcnt, 1);
5669
5670#ifdef CONFIG_XFRM
5671 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5672 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5673 unsigned int i;
5674
5675 for (i = 0; i < sp->len; i++)
5676 xfrm_state_hold(sp->xvec[i]);
5677 }
5678#endif
5679 __skb_ext_put(old);
5680 return new;
5681}
5682
5683/**
5684 * skb_ext_add - allocate space for given extension, COW if needed
5685 * @skb: buffer
5686 * @id: extension to allocate space for
5687 *
5688 * Allocates enough space for the given extension.
5689 * If the extension is already present, a pointer to that extension
5690 * is returned.
5691 *
5692 * If the skb was cloned, COW applies and the returned memory can be
5693 * modified without changing the extension space of clones buffers.
5694 *
5695 * Returns pointer to the extension or NULL on allocation failure.
5696 */
5697void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5698{
5699 struct skb_ext *new, *old = NULL;
5700 unsigned int newlen, newoff;
5701
5702 if (skb->active_extensions) {
5703 old = skb->extensions;
5704
5705 new = skb_ext_maybe_cow(old, skb->active_extensions);
5706 if (!new)
5707 return NULL;
5708
5709 if (__skb_ext_exist(new, id))
5710 goto set_active;
5711
5712 newoff = new->chunks;
5713 } else {
5714 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5715
5716 new = skb_ext_alloc();
5717 if (!new)
5718 return NULL;
5719 }
5720
5721 newlen = newoff + skb_ext_type_len[id];
5722 new->chunks = newlen;
5723 new->offset[id] = newoff;
5724set_active:
5725 skb->extensions = new;
5726 skb->active_extensions |= 1 << id;
5727 return skb_ext_get_ptr(new, id);
5728}
5729EXPORT_SYMBOL(skb_ext_add);
5730
5731#ifdef CONFIG_XFRM
5732static void skb_ext_put_sp(struct sec_path *sp)
5733{
5734 unsigned int i;
5735
5736 for (i = 0; i < sp->len; i++)
5737 xfrm_state_put(sp->xvec[i]);
5738}
5739#endif
5740
5741void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5742{
5743 struct skb_ext *ext = skb->extensions;
5744
5745 skb->active_extensions &= ~(1 << id);
5746 if (skb->active_extensions == 0) {
5747 skb->extensions = NULL;
5748 __skb_ext_put(ext);
5749#ifdef CONFIG_XFRM
5750 } else if (id == SKB_EXT_SEC_PATH &&
5751 refcount_read(&ext->refcnt) == 1) {
5752 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5753
5754 skb_ext_put_sp(sp);
5755 sp->len = 0;
5756#endif
5757 }
5758}
5759EXPORT_SYMBOL(__skb_ext_del);
5760
5761void __skb_ext_put(struct skb_ext *ext)
5762{
5763 /* If this is last clone, nothing can increment
5764 * it after check passes. Avoids one atomic op.
5765 */
5766 if (refcount_read(&ext->refcnt) == 1)
5767 goto free_now;
5768
5769 if (!refcount_dec_and_test(&ext->refcnt))
5770 return;
5771free_now:
5772#ifdef CONFIG_XFRM
5773 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5774 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5775#endif
5776
5777 kmem_cache_free(skbuff_ext_cache, ext);
5778}
5779EXPORT_SYMBOL(__skb_ext_put);
5780#endif /* CONFIG_SKB_EXTENSIONS */