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