]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/core/skbuff.c
net: skbuff: add skb_append_pagefrags and use it
[mirror_ubuntu-bionic-kernel.git] / net / core / skbuff.c
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35 /*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
55 #endif
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
66
67 #include <net/protocol.h>
68 #include <net/dst.h>
69 #include <net/sock.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
72 #include <net/xfrm.h>
73
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
79
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82
83 /**
84 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
88 * @msg: skb_over_panic or skb_under_panic
89 *
90 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
94 */
95 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
96 const char msg[])
97 {
98 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99 msg, addr, skb->len, sz, skb->head, skb->data,
100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
102 BUG();
103 }
104
105 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
106 {
107 skb_panic(skb, sz, addr, __func__);
108 }
109
110 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111 {
112 skb_panic(skb, sz, addr, __func__);
113 }
114
115 /*
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
121 */
122 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
124
125 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
127 {
128 void *obj;
129 bool ret_pfmemalloc = false;
130
131 /*
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
134 */
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
140
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
144
145 out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
148
149 return obj;
150 }
151
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
155 *
156 */
157
158 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
159 {
160 struct sk_buff *skb;
161
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
167
168 /*
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
172 */
173 memset(skb, 0, offsetof(struct sk_buff, tail));
174 skb->head = NULL;
175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
177
178 skb->mac_header = (typeof(skb->mac_header))~0U;
179 out:
180 return skb;
181 }
182
183 /**
184 * __alloc_skb - allocate a network buffer
185 * @size: size to allocate
186 * @gfp_mask: allocation mask
187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
191 * @node: numa node to allocate memory on
192 *
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
196 *
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
199 */
200 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
201 int flags, int node)
202 {
203 struct kmem_cache *cache;
204 struct skb_shared_info *shinfo;
205 struct sk_buff *skb;
206 u8 *data;
207 bool pfmemalloc;
208
209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
211
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
214
215 /* Get the HEAD */
216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
217 if (!skb)
218 goto out;
219 prefetchw(skb);
220
221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
225 */
226 size = SKB_DATA_ALIGN(size);
227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
229 if (!data)
230 goto nodata;
231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
234 */
235 size = SKB_WITH_OVERHEAD(ksize(data));
236 prefetchw(data + size);
237
238 /*
239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
242 */
243 memset(skb, 0, offsetof(struct sk_buff, tail));
244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
246 skb->pfmemalloc = pfmemalloc;
247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
250 skb_reset_tail_pointer(skb);
251 skb->end = skb->tail + size;
252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
254
255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
258 atomic_set(&shinfo->dataref, 1);
259 kmemcheck_annotate_variable(shinfo->destructor_arg);
260
261 if (flags & SKB_ALLOC_FCLONE) {
262 struct sk_buff_fclones *fclones;
263
264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
265
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
267 skb->fclone = SKB_FCLONE_ORIG;
268 atomic_set(&fclones->fclone_ref, 1);
269
270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
271 fclones->skb2.pfmemalloc = pfmemalloc;
272 }
273 out:
274 return skb;
275 nodata:
276 kmem_cache_free(cache, skb);
277 skb = NULL;
278 goto out;
279 }
280 EXPORT_SYMBOL(__alloc_skb);
281
282 /**
283 * __build_skb - build a network buffer
284 * @data: data buffer provided by caller
285 * @frag_size: size of data, or 0 if head was kmalloced
286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
288 * skb_shared_info. @data must have been allocated by kmalloc() only if
289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
300 */
301 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
302 {
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
305 unsigned int size = frag_size ? : ksize(data);
306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
310
311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
312
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
322
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
328
329 return skb;
330 }
331
332 /* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
336 */
337 struct sk_buff *build_skb(void *data, unsigned int frag_size)
338 {
339 struct sk_buff *skb = __build_skb(data, frag_size);
340
341 if (skb && frag_size) {
342 skb->head_frag = 1;
343 if (virt_to_head_page(data)->pfmemalloc)
344 skb->pfmemalloc = 1;
345 }
346 return skb;
347 }
348 EXPORT_SYMBOL(build_skb);
349
350 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
351 static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
352
353 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
354 {
355 struct page_frag_cache *nc;
356 unsigned long flags;
357 void *data;
358
359 local_irq_save(flags);
360 nc = this_cpu_ptr(&netdev_alloc_cache);
361 data = __alloc_page_frag(nc, fragsz, gfp_mask);
362 local_irq_restore(flags);
363 return data;
364 }
365
366 /**
367 * netdev_alloc_frag - allocate a page fragment
368 * @fragsz: fragment size
369 *
370 * Allocates a frag from a page for receive buffer.
371 * Uses GFP_ATOMIC allocations.
372 */
373 void *netdev_alloc_frag(unsigned int fragsz)
374 {
375 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
376 }
377 EXPORT_SYMBOL(netdev_alloc_frag);
378
379 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
380 {
381 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
382
383 return __alloc_page_frag(nc, fragsz, gfp_mask);
384 }
385
386 void *napi_alloc_frag(unsigned int fragsz)
387 {
388 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
389 }
390 EXPORT_SYMBOL(napi_alloc_frag);
391
392 /**
393 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
394 * @dev: network device to receive on
395 * @length: length to allocate
396 * @gfp_mask: get_free_pages mask, passed to alloc_skb
397 *
398 * Allocate a new &sk_buff and assign it a usage count of one. The
399 * buffer has NET_SKB_PAD headroom built in. Users should allocate
400 * the headroom they think they need without accounting for the
401 * built in space. The built in space is used for optimisations.
402 *
403 * %NULL is returned if there is no free memory.
404 */
405 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
406 gfp_t gfp_mask)
407 {
408 struct page_frag_cache *nc;
409 unsigned long flags;
410 struct sk_buff *skb;
411 bool pfmemalloc;
412 void *data;
413
414 len += NET_SKB_PAD;
415
416 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
417 (gfp_mask & (__GFP_WAIT | GFP_DMA))) {
418 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
419 if (!skb)
420 goto skb_fail;
421 goto skb_success;
422 }
423
424 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
425 len = SKB_DATA_ALIGN(len);
426
427 if (sk_memalloc_socks())
428 gfp_mask |= __GFP_MEMALLOC;
429
430 local_irq_save(flags);
431
432 nc = this_cpu_ptr(&netdev_alloc_cache);
433 data = __alloc_page_frag(nc, len, gfp_mask);
434 pfmemalloc = nc->pfmemalloc;
435
436 local_irq_restore(flags);
437
438 if (unlikely(!data))
439 return NULL;
440
441 skb = __build_skb(data, len);
442 if (unlikely(!skb)) {
443 skb_free_frag(data);
444 return NULL;
445 }
446
447 /* use OR instead of assignment to avoid clearing of bits in mask */
448 if (pfmemalloc)
449 skb->pfmemalloc = 1;
450 skb->head_frag = 1;
451
452 skb_success:
453 skb_reserve(skb, NET_SKB_PAD);
454 skb->dev = dev;
455
456 skb_fail:
457 return skb;
458 }
459 EXPORT_SYMBOL(__netdev_alloc_skb);
460
461 /**
462 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
463 * @napi: napi instance this buffer was allocated for
464 * @length: length to allocate
465 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
466 *
467 * Allocate a new sk_buff for use in NAPI receive. This buffer will
468 * attempt to allocate the head from a special reserved region used
469 * only for NAPI Rx allocation. By doing this we can save several
470 * CPU cycles by avoiding having to disable and re-enable IRQs.
471 *
472 * %NULL is returned if there is no free memory.
473 */
474 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
475 gfp_t gfp_mask)
476 {
477 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
478 struct sk_buff *skb;
479 void *data;
480
481 len += NET_SKB_PAD + NET_IP_ALIGN;
482
483 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
484 (gfp_mask & (__GFP_WAIT | GFP_DMA))) {
485 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
486 if (!skb)
487 goto skb_fail;
488 goto skb_success;
489 }
490
491 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
492 len = SKB_DATA_ALIGN(len);
493
494 if (sk_memalloc_socks())
495 gfp_mask |= __GFP_MEMALLOC;
496
497 data = __alloc_page_frag(nc, len, gfp_mask);
498 if (unlikely(!data))
499 return NULL;
500
501 skb = __build_skb(data, len);
502 if (unlikely(!skb)) {
503 skb_free_frag(data);
504 return NULL;
505 }
506
507 /* use OR instead of assignment to avoid clearing of bits in mask */
508 if (nc->pfmemalloc)
509 skb->pfmemalloc = 1;
510 skb->head_frag = 1;
511
512 skb_success:
513 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
514 skb->dev = napi->dev;
515
516 skb_fail:
517 return skb;
518 }
519 EXPORT_SYMBOL(__napi_alloc_skb);
520
521 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
522 int size, unsigned int truesize)
523 {
524 skb_fill_page_desc(skb, i, page, off, size);
525 skb->len += size;
526 skb->data_len += size;
527 skb->truesize += truesize;
528 }
529 EXPORT_SYMBOL(skb_add_rx_frag);
530
531 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
532 unsigned int truesize)
533 {
534 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
535
536 skb_frag_size_add(frag, size);
537 skb->len += size;
538 skb->data_len += size;
539 skb->truesize += truesize;
540 }
541 EXPORT_SYMBOL(skb_coalesce_rx_frag);
542
543 static void skb_drop_list(struct sk_buff **listp)
544 {
545 kfree_skb_list(*listp);
546 *listp = NULL;
547 }
548
549 static inline void skb_drop_fraglist(struct sk_buff *skb)
550 {
551 skb_drop_list(&skb_shinfo(skb)->frag_list);
552 }
553
554 static void skb_clone_fraglist(struct sk_buff *skb)
555 {
556 struct sk_buff *list;
557
558 skb_walk_frags(skb, list)
559 skb_get(list);
560 }
561
562 static void skb_free_head(struct sk_buff *skb)
563 {
564 unsigned char *head = skb->head;
565
566 if (skb->head_frag)
567 skb_free_frag(head);
568 else
569 kfree(head);
570 }
571
572 static void skb_release_data(struct sk_buff *skb)
573 {
574 struct skb_shared_info *shinfo = skb_shinfo(skb);
575 int i;
576
577 if (skb->cloned &&
578 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
579 &shinfo->dataref))
580 return;
581
582 for (i = 0; i < shinfo->nr_frags; i++)
583 __skb_frag_unref(&shinfo->frags[i]);
584
585 /*
586 * If skb buf is from userspace, we need to notify the caller
587 * the lower device DMA has done;
588 */
589 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
590 struct ubuf_info *uarg;
591
592 uarg = shinfo->destructor_arg;
593 if (uarg->callback)
594 uarg->callback(uarg, true);
595 }
596
597 if (shinfo->frag_list)
598 kfree_skb_list(shinfo->frag_list);
599
600 skb_free_head(skb);
601 }
602
603 /*
604 * Free an skbuff by memory without cleaning the state.
605 */
606 static void kfree_skbmem(struct sk_buff *skb)
607 {
608 struct sk_buff_fclones *fclones;
609
610 switch (skb->fclone) {
611 case SKB_FCLONE_UNAVAILABLE:
612 kmem_cache_free(skbuff_head_cache, skb);
613 return;
614
615 case SKB_FCLONE_ORIG:
616 fclones = container_of(skb, struct sk_buff_fclones, skb1);
617
618 /* We usually free the clone (TX completion) before original skb
619 * This test would have no chance to be true for the clone,
620 * while here, branch prediction will be good.
621 */
622 if (atomic_read(&fclones->fclone_ref) == 1)
623 goto fastpath;
624 break;
625
626 default: /* SKB_FCLONE_CLONE */
627 fclones = container_of(skb, struct sk_buff_fclones, skb2);
628 break;
629 }
630 if (!atomic_dec_and_test(&fclones->fclone_ref))
631 return;
632 fastpath:
633 kmem_cache_free(skbuff_fclone_cache, fclones);
634 }
635
636 static void skb_release_head_state(struct sk_buff *skb)
637 {
638 skb_dst_drop(skb);
639 #ifdef CONFIG_XFRM
640 secpath_put(skb->sp);
641 #endif
642 if (skb->destructor) {
643 WARN_ON(in_irq());
644 skb->destructor(skb);
645 }
646 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
647 nf_conntrack_put(skb->nfct);
648 #endif
649 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
650 nf_bridge_put(skb->nf_bridge);
651 #endif
652 }
653
654 /* Free everything but the sk_buff shell. */
655 static void skb_release_all(struct sk_buff *skb)
656 {
657 skb_release_head_state(skb);
658 if (likely(skb->head))
659 skb_release_data(skb);
660 }
661
662 /**
663 * __kfree_skb - private function
664 * @skb: buffer
665 *
666 * Free an sk_buff. Release anything attached to the buffer.
667 * Clean the state. This is an internal helper function. Users should
668 * always call kfree_skb
669 */
670
671 void __kfree_skb(struct sk_buff *skb)
672 {
673 skb_release_all(skb);
674 kfree_skbmem(skb);
675 }
676 EXPORT_SYMBOL(__kfree_skb);
677
678 /**
679 * kfree_skb - free an sk_buff
680 * @skb: buffer to free
681 *
682 * Drop a reference to the buffer and free it if the usage count has
683 * hit zero.
684 */
685 void kfree_skb(struct sk_buff *skb)
686 {
687 if (unlikely(!skb))
688 return;
689 if (likely(atomic_read(&skb->users) == 1))
690 smp_rmb();
691 else if (likely(!atomic_dec_and_test(&skb->users)))
692 return;
693 trace_kfree_skb(skb, __builtin_return_address(0));
694 __kfree_skb(skb);
695 }
696 EXPORT_SYMBOL(kfree_skb);
697
698 void kfree_skb_list(struct sk_buff *segs)
699 {
700 while (segs) {
701 struct sk_buff *next = segs->next;
702
703 kfree_skb(segs);
704 segs = next;
705 }
706 }
707 EXPORT_SYMBOL(kfree_skb_list);
708
709 /**
710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
712 *
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
715 */
716 void skb_tx_error(struct sk_buff *skb)
717 {
718 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
719 struct ubuf_info *uarg;
720
721 uarg = skb_shinfo(skb)->destructor_arg;
722 if (uarg->callback)
723 uarg->callback(uarg, false);
724 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
725 }
726 }
727 EXPORT_SYMBOL(skb_tx_error);
728
729 /**
730 * consume_skb - free an skbuff
731 * @skb: buffer to free
732 *
733 * Drop a ref to the buffer and free it if the usage count has hit zero
734 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
735 * is being dropped after a failure and notes that
736 */
737 void consume_skb(struct sk_buff *skb)
738 {
739 if (unlikely(!skb))
740 return;
741 if (likely(atomic_read(&skb->users) == 1))
742 smp_rmb();
743 else if (likely(!atomic_dec_and_test(&skb->users)))
744 return;
745 trace_consume_skb(skb);
746 __kfree_skb(skb);
747 }
748 EXPORT_SYMBOL(consume_skb);
749
750 /* Make sure a field is enclosed inside headers_start/headers_end section */
751 #define CHECK_SKB_FIELD(field) \
752 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
753 offsetof(struct sk_buff, headers_start)); \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
755 offsetof(struct sk_buff, headers_end)); \
756
757 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
758 {
759 new->tstamp = old->tstamp;
760 /* We do not copy old->sk */
761 new->dev = old->dev;
762 memcpy(new->cb, old->cb, sizeof(old->cb));
763 skb_dst_copy(new, old);
764 #ifdef CONFIG_XFRM
765 new->sp = secpath_get(old->sp);
766 #endif
767 __nf_copy(new, old, false);
768
769 /* Note : this field could be in headers_start/headers_end section
770 * It is not yet because we do not want to have a 16 bit hole
771 */
772 new->queue_mapping = old->queue_mapping;
773
774 memcpy(&new->headers_start, &old->headers_start,
775 offsetof(struct sk_buff, headers_end) -
776 offsetof(struct sk_buff, headers_start));
777 CHECK_SKB_FIELD(protocol);
778 CHECK_SKB_FIELD(csum);
779 CHECK_SKB_FIELD(hash);
780 CHECK_SKB_FIELD(priority);
781 CHECK_SKB_FIELD(skb_iif);
782 CHECK_SKB_FIELD(vlan_proto);
783 CHECK_SKB_FIELD(vlan_tci);
784 CHECK_SKB_FIELD(transport_header);
785 CHECK_SKB_FIELD(network_header);
786 CHECK_SKB_FIELD(mac_header);
787 CHECK_SKB_FIELD(inner_protocol);
788 CHECK_SKB_FIELD(inner_transport_header);
789 CHECK_SKB_FIELD(inner_network_header);
790 CHECK_SKB_FIELD(inner_mac_header);
791 CHECK_SKB_FIELD(mark);
792 #ifdef CONFIG_NETWORK_SECMARK
793 CHECK_SKB_FIELD(secmark);
794 #endif
795 #ifdef CONFIG_NET_RX_BUSY_POLL
796 CHECK_SKB_FIELD(napi_id);
797 #endif
798 #ifdef CONFIG_XPS
799 CHECK_SKB_FIELD(sender_cpu);
800 #endif
801 #ifdef CONFIG_NET_SCHED
802 CHECK_SKB_FIELD(tc_index);
803 #ifdef CONFIG_NET_CLS_ACT
804 CHECK_SKB_FIELD(tc_verd);
805 #endif
806 #endif
807
808 }
809
810 /*
811 * You should not add any new code to this function. Add it to
812 * __copy_skb_header above instead.
813 */
814 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
815 {
816 #define C(x) n->x = skb->x
817
818 n->next = n->prev = NULL;
819 n->sk = NULL;
820 __copy_skb_header(n, skb);
821
822 C(len);
823 C(data_len);
824 C(mac_len);
825 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
826 n->cloned = 1;
827 n->nohdr = 0;
828 n->destructor = NULL;
829 C(tail);
830 C(end);
831 C(head);
832 C(head_frag);
833 C(data);
834 C(truesize);
835 atomic_set(&n->users, 1);
836
837 atomic_inc(&(skb_shinfo(skb)->dataref));
838 skb->cloned = 1;
839
840 return n;
841 #undef C
842 }
843
844 /**
845 * skb_morph - morph one skb into another
846 * @dst: the skb to receive the contents
847 * @src: the skb to supply the contents
848 *
849 * This is identical to skb_clone except that the target skb is
850 * supplied by the user.
851 *
852 * The target skb is returned upon exit.
853 */
854 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
855 {
856 skb_release_all(dst);
857 return __skb_clone(dst, src);
858 }
859 EXPORT_SYMBOL_GPL(skb_morph);
860
861 /**
862 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
863 * @skb: the skb to modify
864 * @gfp_mask: allocation priority
865 *
866 * This must be called on SKBTX_DEV_ZEROCOPY skb.
867 * It will copy all frags into kernel and drop the reference
868 * to userspace pages.
869 *
870 * If this function is called from an interrupt gfp_mask() must be
871 * %GFP_ATOMIC.
872 *
873 * Returns 0 on success or a negative error code on failure
874 * to allocate kernel memory to copy to.
875 */
876 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
877 {
878 int i;
879 int num_frags = skb_shinfo(skb)->nr_frags;
880 struct page *page, *head = NULL;
881 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
882
883 for (i = 0; i < num_frags; i++) {
884 u8 *vaddr;
885 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
886
887 page = alloc_page(gfp_mask);
888 if (!page) {
889 while (head) {
890 struct page *next = (struct page *)page_private(head);
891 put_page(head);
892 head = next;
893 }
894 return -ENOMEM;
895 }
896 vaddr = kmap_atomic(skb_frag_page(f));
897 memcpy(page_address(page),
898 vaddr + f->page_offset, skb_frag_size(f));
899 kunmap_atomic(vaddr);
900 set_page_private(page, (unsigned long)head);
901 head = page;
902 }
903
904 /* skb frags release userspace buffers */
905 for (i = 0; i < num_frags; i++)
906 skb_frag_unref(skb, i);
907
908 uarg->callback(uarg, false);
909
910 /* skb frags point to kernel buffers */
911 for (i = num_frags - 1; i >= 0; i--) {
912 __skb_fill_page_desc(skb, i, head, 0,
913 skb_shinfo(skb)->frags[i].size);
914 head = (struct page *)page_private(head);
915 }
916
917 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
918 return 0;
919 }
920 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
921
922 /**
923 * skb_clone - duplicate an sk_buff
924 * @skb: buffer to clone
925 * @gfp_mask: allocation priority
926 *
927 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
928 * copies share the same packet data but not structure. The new
929 * buffer has a reference count of 1. If the allocation fails the
930 * function returns %NULL otherwise the new buffer is returned.
931 *
932 * If this function is called from an interrupt gfp_mask() must be
933 * %GFP_ATOMIC.
934 */
935
936 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
937 {
938 struct sk_buff_fclones *fclones = container_of(skb,
939 struct sk_buff_fclones,
940 skb1);
941 struct sk_buff *n;
942
943 if (skb_orphan_frags(skb, gfp_mask))
944 return NULL;
945
946 if (skb->fclone == SKB_FCLONE_ORIG &&
947 atomic_read(&fclones->fclone_ref) == 1) {
948 n = &fclones->skb2;
949 atomic_set(&fclones->fclone_ref, 2);
950 } else {
951 if (skb_pfmemalloc(skb))
952 gfp_mask |= __GFP_MEMALLOC;
953
954 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
955 if (!n)
956 return NULL;
957
958 kmemcheck_annotate_bitfield(n, flags1);
959 n->fclone = SKB_FCLONE_UNAVAILABLE;
960 }
961
962 return __skb_clone(n, skb);
963 }
964 EXPORT_SYMBOL(skb_clone);
965
966 static void skb_headers_offset_update(struct sk_buff *skb, int off)
967 {
968 /* Only adjust this if it actually is csum_start rather than csum */
969 if (skb->ip_summed == CHECKSUM_PARTIAL)
970 skb->csum_start += off;
971 /* {transport,network,mac}_header and tail are relative to skb->head */
972 skb->transport_header += off;
973 skb->network_header += off;
974 if (skb_mac_header_was_set(skb))
975 skb->mac_header += off;
976 skb->inner_transport_header += off;
977 skb->inner_network_header += off;
978 skb->inner_mac_header += off;
979 }
980
981 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
982 {
983 __copy_skb_header(new, old);
984
985 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
986 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
987 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
988 }
989
990 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
991 {
992 if (skb_pfmemalloc(skb))
993 return SKB_ALLOC_RX;
994 return 0;
995 }
996
997 /**
998 * skb_copy - create private copy of an sk_buff
999 * @skb: buffer to copy
1000 * @gfp_mask: allocation priority
1001 *
1002 * Make a copy of both an &sk_buff and its data. This is used when the
1003 * caller wishes to modify the data and needs a private copy of the
1004 * data to alter. Returns %NULL on failure or the pointer to the buffer
1005 * on success. The returned buffer has a reference count of 1.
1006 *
1007 * As by-product this function converts non-linear &sk_buff to linear
1008 * one, so that &sk_buff becomes completely private and caller is allowed
1009 * to modify all the data of returned buffer. This means that this
1010 * function is not recommended for use in circumstances when only
1011 * header is going to be modified. Use pskb_copy() instead.
1012 */
1013
1014 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1015 {
1016 int headerlen = skb_headroom(skb);
1017 unsigned int size = skb_end_offset(skb) + skb->data_len;
1018 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1019 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1020
1021 if (!n)
1022 return NULL;
1023
1024 /* Set the data pointer */
1025 skb_reserve(n, headerlen);
1026 /* Set the tail pointer and length */
1027 skb_put(n, skb->len);
1028
1029 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1030 BUG();
1031
1032 copy_skb_header(n, skb);
1033 return n;
1034 }
1035 EXPORT_SYMBOL(skb_copy);
1036
1037 /**
1038 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1039 * @skb: buffer to copy
1040 * @headroom: headroom of new skb
1041 * @gfp_mask: allocation priority
1042 * @fclone: if true allocate the copy of the skb from the fclone
1043 * cache instead of the head cache; it is recommended to set this
1044 * to true for the cases where the copy will likely be cloned
1045 *
1046 * Make a copy of both an &sk_buff and part of its data, located
1047 * in header. Fragmented data remain shared. This is used when
1048 * the caller wishes to modify only header of &sk_buff and needs
1049 * private copy of the header to alter. Returns %NULL on failure
1050 * or the pointer to the buffer on success.
1051 * The returned buffer has a reference count of 1.
1052 */
1053
1054 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1055 gfp_t gfp_mask, bool fclone)
1056 {
1057 unsigned int size = skb_headlen(skb) + headroom;
1058 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1059 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1060
1061 if (!n)
1062 goto out;
1063
1064 /* Set the data pointer */
1065 skb_reserve(n, headroom);
1066 /* Set the tail pointer and length */
1067 skb_put(n, skb_headlen(skb));
1068 /* Copy the bytes */
1069 skb_copy_from_linear_data(skb, n->data, n->len);
1070
1071 n->truesize += skb->data_len;
1072 n->data_len = skb->data_len;
1073 n->len = skb->len;
1074
1075 if (skb_shinfo(skb)->nr_frags) {
1076 int i;
1077
1078 if (skb_orphan_frags(skb, gfp_mask)) {
1079 kfree_skb(n);
1080 n = NULL;
1081 goto out;
1082 }
1083 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1084 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1085 skb_frag_ref(skb, i);
1086 }
1087 skb_shinfo(n)->nr_frags = i;
1088 }
1089
1090 if (skb_has_frag_list(skb)) {
1091 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1092 skb_clone_fraglist(n);
1093 }
1094
1095 copy_skb_header(n, skb);
1096 out:
1097 return n;
1098 }
1099 EXPORT_SYMBOL(__pskb_copy_fclone);
1100
1101 /**
1102 * pskb_expand_head - reallocate header of &sk_buff
1103 * @skb: buffer to reallocate
1104 * @nhead: room to add at head
1105 * @ntail: room to add at tail
1106 * @gfp_mask: allocation priority
1107 *
1108 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1109 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1110 * reference count of 1. Returns zero in the case of success or error,
1111 * if expansion failed. In the last case, &sk_buff is not changed.
1112 *
1113 * All the pointers pointing into skb header may change and must be
1114 * reloaded after call to this function.
1115 */
1116
1117 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1118 gfp_t gfp_mask)
1119 {
1120 int i;
1121 u8 *data;
1122 int size = nhead + skb_end_offset(skb) + ntail;
1123 long off;
1124
1125 BUG_ON(nhead < 0);
1126
1127 if (skb_shared(skb))
1128 BUG();
1129
1130 size = SKB_DATA_ALIGN(size);
1131
1132 if (skb_pfmemalloc(skb))
1133 gfp_mask |= __GFP_MEMALLOC;
1134 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1135 gfp_mask, NUMA_NO_NODE, NULL);
1136 if (!data)
1137 goto nodata;
1138 size = SKB_WITH_OVERHEAD(ksize(data));
1139
1140 /* Copy only real data... and, alas, header. This should be
1141 * optimized for the cases when header is void.
1142 */
1143 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1144
1145 memcpy((struct skb_shared_info *)(data + size),
1146 skb_shinfo(skb),
1147 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1148
1149 /*
1150 * if shinfo is shared we must drop the old head gracefully, but if it
1151 * is not we can just drop the old head and let the existing refcount
1152 * be since all we did is relocate the values
1153 */
1154 if (skb_cloned(skb)) {
1155 /* copy this zero copy skb frags */
1156 if (skb_orphan_frags(skb, gfp_mask))
1157 goto nofrags;
1158 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1159 skb_frag_ref(skb, i);
1160
1161 if (skb_has_frag_list(skb))
1162 skb_clone_fraglist(skb);
1163
1164 skb_release_data(skb);
1165 } else {
1166 skb_free_head(skb);
1167 }
1168 off = (data + nhead) - skb->head;
1169
1170 skb->head = data;
1171 skb->head_frag = 0;
1172 skb->data += off;
1173 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1174 skb->end = size;
1175 off = nhead;
1176 #else
1177 skb->end = skb->head + size;
1178 #endif
1179 skb->tail += off;
1180 skb_headers_offset_update(skb, nhead);
1181 skb->cloned = 0;
1182 skb->hdr_len = 0;
1183 skb->nohdr = 0;
1184 atomic_set(&skb_shinfo(skb)->dataref, 1);
1185 return 0;
1186
1187 nofrags:
1188 kfree(data);
1189 nodata:
1190 return -ENOMEM;
1191 }
1192 EXPORT_SYMBOL(pskb_expand_head);
1193
1194 /* Make private copy of skb with writable head and some headroom */
1195
1196 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1197 {
1198 struct sk_buff *skb2;
1199 int delta = headroom - skb_headroom(skb);
1200
1201 if (delta <= 0)
1202 skb2 = pskb_copy(skb, GFP_ATOMIC);
1203 else {
1204 skb2 = skb_clone(skb, GFP_ATOMIC);
1205 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1206 GFP_ATOMIC)) {
1207 kfree_skb(skb2);
1208 skb2 = NULL;
1209 }
1210 }
1211 return skb2;
1212 }
1213 EXPORT_SYMBOL(skb_realloc_headroom);
1214
1215 /**
1216 * skb_copy_expand - copy and expand sk_buff
1217 * @skb: buffer to copy
1218 * @newheadroom: new free bytes at head
1219 * @newtailroom: new free bytes at tail
1220 * @gfp_mask: allocation priority
1221 *
1222 * Make a copy of both an &sk_buff and its data and while doing so
1223 * allocate additional space.
1224 *
1225 * This is used when the caller wishes to modify the data and needs a
1226 * private copy of the data to alter as well as more space for new fields.
1227 * Returns %NULL on failure or the pointer to the buffer
1228 * on success. The returned buffer has a reference count of 1.
1229 *
1230 * You must pass %GFP_ATOMIC as the allocation priority if this function
1231 * is called from an interrupt.
1232 */
1233 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1234 int newheadroom, int newtailroom,
1235 gfp_t gfp_mask)
1236 {
1237 /*
1238 * Allocate the copy buffer
1239 */
1240 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1241 gfp_mask, skb_alloc_rx_flag(skb),
1242 NUMA_NO_NODE);
1243 int oldheadroom = skb_headroom(skb);
1244 int head_copy_len, head_copy_off;
1245
1246 if (!n)
1247 return NULL;
1248
1249 skb_reserve(n, newheadroom);
1250
1251 /* Set the tail pointer and length */
1252 skb_put(n, skb->len);
1253
1254 head_copy_len = oldheadroom;
1255 head_copy_off = 0;
1256 if (newheadroom <= head_copy_len)
1257 head_copy_len = newheadroom;
1258 else
1259 head_copy_off = newheadroom - head_copy_len;
1260
1261 /* Copy the linear header and data. */
1262 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1263 skb->len + head_copy_len))
1264 BUG();
1265
1266 copy_skb_header(n, skb);
1267
1268 skb_headers_offset_update(n, newheadroom - oldheadroom);
1269
1270 return n;
1271 }
1272 EXPORT_SYMBOL(skb_copy_expand);
1273
1274 /**
1275 * skb_pad - zero pad the tail of an skb
1276 * @skb: buffer to pad
1277 * @pad: space to pad
1278 *
1279 * Ensure that a buffer is followed by a padding area that is zero
1280 * filled. Used by network drivers which may DMA or transfer data
1281 * beyond the buffer end onto the wire.
1282 *
1283 * May return error in out of memory cases. The skb is freed on error.
1284 */
1285
1286 int skb_pad(struct sk_buff *skb, int pad)
1287 {
1288 int err;
1289 int ntail;
1290
1291 /* If the skbuff is non linear tailroom is always zero.. */
1292 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1293 memset(skb->data+skb->len, 0, pad);
1294 return 0;
1295 }
1296
1297 ntail = skb->data_len + pad - (skb->end - skb->tail);
1298 if (likely(skb_cloned(skb) || ntail > 0)) {
1299 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1300 if (unlikely(err))
1301 goto free_skb;
1302 }
1303
1304 /* FIXME: The use of this function with non-linear skb's really needs
1305 * to be audited.
1306 */
1307 err = skb_linearize(skb);
1308 if (unlikely(err))
1309 goto free_skb;
1310
1311 memset(skb->data + skb->len, 0, pad);
1312 return 0;
1313
1314 free_skb:
1315 kfree_skb(skb);
1316 return err;
1317 }
1318 EXPORT_SYMBOL(skb_pad);
1319
1320 /**
1321 * pskb_put - add data to the tail of a potentially fragmented buffer
1322 * @skb: start of the buffer to use
1323 * @tail: tail fragment of the buffer to use
1324 * @len: amount of data to add
1325 *
1326 * This function extends the used data area of the potentially
1327 * fragmented buffer. @tail must be the last fragment of @skb -- or
1328 * @skb itself. If this would exceed the total buffer size the kernel
1329 * will panic. A pointer to the first byte of the extra data is
1330 * returned.
1331 */
1332
1333 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1334 {
1335 if (tail != skb) {
1336 skb->data_len += len;
1337 skb->len += len;
1338 }
1339 return skb_put(tail, len);
1340 }
1341 EXPORT_SYMBOL_GPL(pskb_put);
1342
1343 /**
1344 * skb_put - add data to a buffer
1345 * @skb: buffer to use
1346 * @len: amount of data to add
1347 *
1348 * This function extends the used data area of the buffer. If this would
1349 * exceed the total buffer size the kernel will panic. A pointer to the
1350 * first byte of the extra data is returned.
1351 */
1352 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1353 {
1354 unsigned char *tmp = skb_tail_pointer(skb);
1355 SKB_LINEAR_ASSERT(skb);
1356 skb->tail += len;
1357 skb->len += len;
1358 if (unlikely(skb->tail > skb->end))
1359 skb_over_panic(skb, len, __builtin_return_address(0));
1360 return tmp;
1361 }
1362 EXPORT_SYMBOL(skb_put);
1363
1364 /**
1365 * skb_push - add data to the start of a buffer
1366 * @skb: buffer to use
1367 * @len: amount of data to add
1368 *
1369 * This function extends the used data area of the buffer at the buffer
1370 * start. If this would exceed the total buffer headroom the kernel will
1371 * panic. A pointer to the first byte of the extra data is returned.
1372 */
1373 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1374 {
1375 skb->data -= len;
1376 skb->len += len;
1377 if (unlikely(skb->data<skb->head))
1378 skb_under_panic(skb, len, __builtin_return_address(0));
1379 return skb->data;
1380 }
1381 EXPORT_SYMBOL(skb_push);
1382
1383 /**
1384 * skb_pull - remove data from the start of a buffer
1385 * @skb: buffer to use
1386 * @len: amount of data to remove
1387 *
1388 * This function removes data from the start of a buffer, returning
1389 * the memory to the headroom. A pointer to the next data in the buffer
1390 * is returned. Once the data has been pulled future pushes will overwrite
1391 * the old data.
1392 */
1393 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1394 {
1395 return skb_pull_inline(skb, len);
1396 }
1397 EXPORT_SYMBOL(skb_pull);
1398
1399 /**
1400 * skb_trim - remove end from a buffer
1401 * @skb: buffer to alter
1402 * @len: new length
1403 *
1404 * Cut the length of a buffer down by removing data from the tail. If
1405 * the buffer is already under the length specified it is not modified.
1406 * The skb must be linear.
1407 */
1408 void skb_trim(struct sk_buff *skb, unsigned int len)
1409 {
1410 if (skb->len > len)
1411 __skb_trim(skb, len);
1412 }
1413 EXPORT_SYMBOL(skb_trim);
1414
1415 /* Trims skb to length len. It can change skb pointers.
1416 */
1417
1418 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1419 {
1420 struct sk_buff **fragp;
1421 struct sk_buff *frag;
1422 int offset = skb_headlen(skb);
1423 int nfrags = skb_shinfo(skb)->nr_frags;
1424 int i;
1425 int err;
1426
1427 if (skb_cloned(skb) &&
1428 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1429 return err;
1430
1431 i = 0;
1432 if (offset >= len)
1433 goto drop_pages;
1434
1435 for (; i < nfrags; i++) {
1436 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1437
1438 if (end < len) {
1439 offset = end;
1440 continue;
1441 }
1442
1443 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1444
1445 drop_pages:
1446 skb_shinfo(skb)->nr_frags = i;
1447
1448 for (; i < nfrags; i++)
1449 skb_frag_unref(skb, i);
1450
1451 if (skb_has_frag_list(skb))
1452 skb_drop_fraglist(skb);
1453 goto done;
1454 }
1455
1456 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1457 fragp = &frag->next) {
1458 int end = offset + frag->len;
1459
1460 if (skb_shared(frag)) {
1461 struct sk_buff *nfrag;
1462
1463 nfrag = skb_clone(frag, GFP_ATOMIC);
1464 if (unlikely(!nfrag))
1465 return -ENOMEM;
1466
1467 nfrag->next = frag->next;
1468 consume_skb(frag);
1469 frag = nfrag;
1470 *fragp = frag;
1471 }
1472
1473 if (end < len) {
1474 offset = end;
1475 continue;
1476 }
1477
1478 if (end > len &&
1479 unlikely((err = pskb_trim(frag, len - offset))))
1480 return err;
1481
1482 if (frag->next)
1483 skb_drop_list(&frag->next);
1484 break;
1485 }
1486
1487 done:
1488 if (len > skb_headlen(skb)) {
1489 skb->data_len -= skb->len - len;
1490 skb->len = len;
1491 } else {
1492 skb->len = len;
1493 skb->data_len = 0;
1494 skb_set_tail_pointer(skb, len);
1495 }
1496
1497 return 0;
1498 }
1499 EXPORT_SYMBOL(___pskb_trim);
1500
1501 /**
1502 * __pskb_pull_tail - advance tail of skb header
1503 * @skb: buffer to reallocate
1504 * @delta: number of bytes to advance tail
1505 *
1506 * The function makes a sense only on a fragmented &sk_buff,
1507 * it expands header moving its tail forward and copying necessary
1508 * data from fragmented part.
1509 *
1510 * &sk_buff MUST have reference count of 1.
1511 *
1512 * Returns %NULL (and &sk_buff does not change) if pull failed
1513 * or value of new tail of skb in the case of success.
1514 *
1515 * All the pointers pointing into skb header may change and must be
1516 * reloaded after call to this function.
1517 */
1518
1519 /* Moves tail of skb head forward, copying data from fragmented part,
1520 * when it is necessary.
1521 * 1. It may fail due to malloc failure.
1522 * 2. It may change skb pointers.
1523 *
1524 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1525 */
1526 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1527 {
1528 /* If skb has not enough free space at tail, get new one
1529 * plus 128 bytes for future expansions. If we have enough
1530 * room at tail, reallocate without expansion only if skb is cloned.
1531 */
1532 int i, k, eat = (skb->tail + delta) - skb->end;
1533
1534 if (eat > 0 || skb_cloned(skb)) {
1535 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1536 GFP_ATOMIC))
1537 return NULL;
1538 }
1539
1540 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1541 BUG();
1542
1543 /* Optimization: no fragments, no reasons to preestimate
1544 * size of pulled pages. Superb.
1545 */
1546 if (!skb_has_frag_list(skb))
1547 goto pull_pages;
1548
1549 /* Estimate size of pulled pages. */
1550 eat = delta;
1551 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1552 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1553
1554 if (size >= eat)
1555 goto pull_pages;
1556 eat -= size;
1557 }
1558
1559 /* If we need update frag list, we are in troubles.
1560 * Certainly, it possible to add an offset to skb data,
1561 * but taking into account that pulling is expected to
1562 * be very rare operation, it is worth to fight against
1563 * further bloating skb head and crucify ourselves here instead.
1564 * Pure masohism, indeed. 8)8)
1565 */
1566 if (eat) {
1567 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1568 struct sk_buff *clone = NULL;
1569 struct sk_buff *insp = NULL;
1570
1571 do {
1572 BUG_ON(!list);
1573
1574 if (list->len <= eat) {
1575 /* Eaten as whole. */
1576 eat -= list->len;
1577 list = list->next;
1578 insp = list;
1579 } else {
1580 /* Eaten partially. */
1581
1582 if (skb_shared(list)) {
1583 /* Sucks! We need to fork list. :-( */
1584 clone = skb_clone(list, GFP_ATOMIC);
1585 if (!clone)
1586 return NULL;
1587 insp = list->next;
1588 list = clone;
1589 } else {
1590 /* This may be pulled without
1591 * problems. */
1592 insp = list;
1593 }
1594 if (!pskb_pull(list, eat)) {
1595 kfree_skb(clone);
1596 return NULL;
1597 }
1598 break;
1599 }
1600 } while (eat);
1601
1602 /* Free pulled out fragments. */
1603 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1604 skb_shinfo(skb)->frag_list = list->next;
1605 kfree_skb(list);
1606 }
1607 /* And insert new clone at head. */
1608 if (clone) {
1609 clone->next = list;
1610 skb_shinfo(skb)->frag_list = clone;
1611 }
1612 }
1613 /* Success! Now we may commit changes to skb data. */
1614
1615 pull_pages:
1616 eat = delta;
1617 k = 0;
1618 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1619 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1620
1621 if (size <= eat) {
1622 skb_frag_unref(skb, i);
1623 eat -= size;
1624 } else {
1625 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1626 if (eat) {
1627 skb_shinfo(skb)->frags[k].page_offset += eat;
1628 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1629 eat = 0;
1630 }
1631 k++;
1632 }
1633 }
1634 skb_shinfo(skb)->nr_frags = k;
1635
1636 skb->tail += delta;
1637 skb->data_len -= delta;
1638
1639 return skb_tail_pointer(skb);
1640 }
1641 EXPORT_SYMBOL(__pskb_pull_tail);
1642
1643 /**
1644 * skb_copy_bits - copy bits from skb to kernel buffer
1645 * @skb: source skb
1646 * @offset: offset in source
1647 * @to: destination buffer
1648 * @len: number of bytes to copy
1649 *
1650 * Copy the specified number of bytes from the source skb to the
1651 * destination buffer.
1652 *
1653 * CAUTION ! :
1654 * If its prototype is ever changed,
1655 * check arch/{*}/net/{*}.S files,
1656 * since it is called from BPF assembly code.
1657 */
1658 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1659 {
1660 int start = skb_headlen(skb);
1661 struct sk_buff *frag_iter;
1662 int i, copy;
1663
1664 if (offset > (int)skb->len - len)
1665 goto fault;
1666
1667 /* Copy header. */
1668 if ((copy = start - offset) > 0) {
1669 if (copy > len)
1670 copy = len;
1671 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1672 if ((len -= copy) == 0)
1673 return 0;
1674 offset += copy;
1675 to += copy;
1676 }
1677
1678 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1679 int end;
1680 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1681
1682 WARN_ON(start > offset + len);
1683
1684 end = start + skb_frag_size(f);
1685 if ((copy = end - offset) > 0) {
1686 u8 *vaddr;
1687
1688 if (copy > len)
1689 copy = len;
1690
1691 vaddr = kmap_atomic(skb_frag_page(f));
1692 memcpy(to,
1693 vaddr + f->page_offset + offset - start,
1694 copy);
1695 kunmap_atomic(vaddr);
1696
1697 if ((len -= copy) == 0)
1698 return 0;
1699 offset += copy;
1700 to += copy;
1701 }
1702 start = end;
1703 }
1704
1705 skb_walk_frags(skb, frag_iter) {
1706 int end;
1707
1708 WARN_ON(start > offset + len);
1709
1710 end = start + frag_iter->len;
1711 if ((copy = end - offset) > 0) {
1712 if (copy > len)
1713 copy = len;
1714 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1715 goto fault;
1716 if ((len -= copy) == 0)
1717 return 0;
1718 offset += copy;
1719 to += copy;
1720 }
1721 start = end;
1722 }
1723
1724 if (!len)
1725 return 0;
1726
1727 fault:
1728 return -EFAULT;
1729 }
1730 EXPORT_SYMBOL(skb_copy_bits);
1731
1732 /*
1733 * Callback from splice_to_pipe(), if we need to release some pages
1734 * at the end of the spd in case we error'ed out in filling the pipe.
1735 */
1736 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1737 {
1738 put_page(spd->pages[i]);
1739 }
1740
1741 static struct page *linear_to_page(struct page *page, unsigned int *len,
1742 unsigned int *offset,
1743 struct sock *sk)
1744 {
1745 struct page_frag *pfrag = sk_page_frag(sk);
1746
1747 if (!sk_page_frag_refill(sk, pfrag))
1748 return NULL;
1749
1750 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1751
1752 memcpy(page_address(pfrag->page) + pfrag->offset,
1753 page_address(page) + *offset, *len);
1754 *offset = pfrag->offset;
1755 pfrag->offset += *len;
1756
1757 return pfrag->page;
1758 }
1759
1760 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1761 struct page *page,
1762 unsigned int offset)
1763 {
1764 return spd->nr_pages &&
1765 spd->pages[spd->nr_pages - 1] == page &&
1766 (spd->partial[spd->nr_pages - 1].offset +
1767 spd->partial[spd->nr_pages - 1].len == offset);
1768 }
1769
1770 /*
1771 * Fill page/offset/length into spd, if it can hold more pages.
1772 */
1773 static bool spd_fill_page(struct splice_pipe_desc *spd,
1774 struct pipe_inode_info *pipe, struct page *page,
1775 unsigned int *len, unsigned int offset,
1776 bool linear,
1777 struct sock *sk)
1778 {
1779 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1780 return true;
1781
1782 if (linear) {
1783 page = linear_to_page(page, len, &offset, sk);
1784 if (!page)
1785 return true;
1786 }
1787 if (spd_can_coalesce(spd, page, offset)) {
1788 spd->partial[spd->nr_pages - 1].len += *len;
1789 return false;
1790 }
1791 get_page(page);
1792 spd->pages[spd->nr_pages] = page;
1793 spd->partial[spd->nr_pages].len = *len;
1794 spd->partial[spd->nr_pages].offset = offset;
1795 spd->nr_pages++;
1796
1797 return false;
1798 }
1799
1800 static bool __splice_segment(struct page *page, unsigned int poff,
1801 unsigned int plen, unsigned int *off,
1802 unsigned int *len,
1803 struct splice_pipe_desc *spd, bool linear,
1804 struct sock *sk,
1805 struct pipe_inode_info *pipe)
1806 {
1807 if (!*len)
1808 return true;
1809
1810 /* skip this segment if already processed */
1811 if (*off >= plen) {
1812 *off -= plen;
1813 return false;
1814 }
1815
1816 /* ignore any bits we already processed */
1817 poff += *off;
1818 plen -= *off;
1819 *off = 0;
1820
1821 do {
1822 unsigned int flen = min(*len, plen);
1823
1824 if (spd_fill_page(spd, pipe, page, &flen, poff,
1825 linear, sk))
1826 return true;
1827 poff += flen;
1828 plen -= flen;
1829 *len -= flen;
1830 } while (*len && plen);
1831
1832 return false;
1833 }
1834
1835 /*
1836 * Map linear and fragment data from the skb to spd. It reports true if the
1837 * pipe is full or if we already spliced the requested length.
1838 */
1839 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1840 unsigned int *offset, unsigned int *len,
1841 struct splice_pipe_desc *spd, struct sock *sk)
1842 {
1843 int seg;
1844
1845 /* map the linear part :
1846 * If skb->head_frag is set, this 'linear' part is backed by a
1847 * fragment, and if the head is not shared with any clones then
1848 * we can avoid a copy since we own the head portion of this page.
1849 */
1850 if (__splice_segment(virt_to_page(skb->data),
1851 (unsigned long) skb->data & (PAGE_SIZE - 1),
1852 skb_headlen(skb),
1853 offset, len, spd,
1854 skb_head_is_locked(skb),
1855 sk, pipe))
1856 return true;
1857
1858 /*
1859 * then map the fragments
1860 */
1861 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1862 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1863
1864 if (__splice_segment(skb_frag_page(f),
1865 f->page_offset, skb_frag_size(f),
1866 offset, len, spd, false, sk, pipe))
1867 return true;
1868 }
1869
1870 return false;
1871 }
1872
1873 /*
1874 * Map data from the skb to a pipe. Should handle both the linear part,
1875 * the fragments, and the frag list. It does NOT handle frag lists within
1876 * the frag list, if such a thing exists. We'd probably need to recurse to
1877 * handle that cleanly.
1878 */
1879 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1880 struct pipe_inode_info *pipe, unsigned int tlen,
1881 unsigned int flags)
1882 {
1883 struct partial_page partial[MAX_SKB_FRAGS];
1884 struct page *pages[MAX_SKB_FRAGS];
1885 struct splice_pipe_desc spd = {
1886 .pages = pages,
1887 .partial = partial,
1888 .nr_pages_max = MAX_SKB_FRAGS,
1889 .flags = flags,
1890 .ops = &nosteal_pipe_buf_ops,
1891 .spd_release = sock_spd_release,
1892 };
1893 struct sk_buff *frag_iter;
1894 struct sock *sk = skb->sk;
1895 int ret = 0;
1896
1897 /*
1898 * __skb_splice_bits() only fails if the output has no room left,
1899 * so no point in going over the frag_list for the error case.
1900 */
1901 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1902 goto done;
1903 else if (!tlen)
1904 goto done;
1905
1906 /*
1907 * now see if we have a frag_list to map
1908 */
1909 skb_walk_frags(skb, frag_iter) {
1910 if (!tlen)
1911 break;
1912 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1913 break;
1914 }
1915
1916 done:
1917 if (spd.nr_pages) {
1918 /*
1919 * Drop the socket lock, otherwise we have reverse
1920 * locking dependencies between sk_lock and i_mutex
1921 * here as compared to sendfile(). We enter here
1922 * with the socket lock held, and splice_to_pipe() will
1923 * grab the pipe inode lock. For sendfile() emulation,
1924 * we call into ->sendpage() with the i_mutex lock held
1925 * and networking will grab the socket lock.
1926 */
1927 release_sock(sk);
1928 ret = splice_to_pipe(pipe, &spd);
1929 lock_sock(sk);
1930 }
1931
1932 return ret;
1933 }
1934
1935 /**
1936 * skb_store_bits - store bits from kernel buffer to skb
1937 * @skb: destination buffer
1938 * @offset: offset in destination
1939 * @from: source buffer
1940 * @len: number of bytes to copy
1941 *
1942 * Copy the specified number of bytes from the source buffer to the
1943 * destination skb. This function handles all the messy bits of
1944 * traversing fragment lists and such.
1945 */
1946
1947 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1948 {
1949 int start = skb_headlen(skb);
1950 struct sk_buff *frag_iter;
1951 int i, copy;
1952
1953 if (offset > (int)skb->len - len)
1954 goto fault;
1955
1956 if ((copy = start - offset) > 0) {
1957 if (copy > len)
1958 copy = len;
1959 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1960 if ((len -= copy) == 0)
1961 return 0;
1962 offset += copy;
1963 from += copy;
1964 }
1965
1966 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1967 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1968 int end;
1969
1970 WARN_ON(start > offset + len);
1971
1972 end = start + skb_frag_size(frag);
1973 if ((copy = end - offset) > 0) {
1974 u8 *vaddr;
1975
1976 if (copy > len)
1977 copy = len;
1978
1979 vaddr = kmap_atomic(skb_frag_page(frag));
1980 memcpy(vaddr + frag->page_offset + offset - start,
1981 from, copy);
1982 kunmap_atomic(vaddr);
1983
1984 if ((len -= copy) == 0)
1985 return 0;
1986 offset += copy;
1987 from += copy;
1988 }
1989 start = end;
1990 }
1991
1992 skb_walk_frags(skb, frag_iter) {
1993 int end;
1994
1995 WARN_ON(start > offset + len);
1996
1997 end = start + frag_iter->len;
1998 if ((copy = end - offset) > 0) {
1999 if (copy > len)
2000 copy = len;
2001 if (skb_store_bits(frag_iter, offset - start,
2002 from, copy))
2003 goto fault;
2004 if ((len -= copy) == 0)
2005 return 0;
2006 offset += copy;
2007 from += copy;
2008 }
2009 start = end;
2010 }
2011 if (!len)
2012 return 0;
2013
2014 fault:
2015 return -EFAULT;
2016 }
2017 EXPORT_SYMBOL(skb_store_bits);
2018
2019 /* Checksum skb data. */
2020 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2021 __wsum csum, const struct skb_checksum_ops *ops)
2022 {
2023 int start = skb_headlen(skb);
2024 int i, copy = start - offset;
2025 struct sk_buff *frag_iter;
2026 int pos = 0;
2027
2028 /* Checksum header. */
2029 if (copy > 0) {
2030 if (copy > len)
2031 copy = len;
2032 csum = ops->update(skb->data + offset, copy, csum);
2033 if ((len -= copy) == 0)
2034 return csum;
2035 offset += copy;
2036 pos = copy;
2037 }
2038
2039 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2040 int end;
2041 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2042
2043 WARN_ON(start > offset + len);
2044
2045 end = start + skb_frag_size(frag);
2046 if ((copy = end - offset) > 0) {
2047 __wsum csum2;
2048 u8 *vaddr;
2049
2050 if (copy > len)
2051 copy = len;
2052 vaddr = kmap_atomic(skb_frag_page(frag));
2053 csum2 = ops->update(vaddr + frag->page_offset +
2054 offset - start, copy, 0);
2055 kunmap_atomic(vaddr);
2056 csum = ops->combine(csum, csum2, pos, copy);
2057 if (!(len -= copy))
2058 return csum;
2059 offset += copy;
2060 pos += copy;
2061 }
2062 start = end;
2063 }
2064
2065 skb_walk_frags(skb, frag_iter) {
2066 int end;
2067
2068 WARN_ON(start > offset + len);
2069
2070 end = start + frag_iter->len;
2071 if ((copy = end - offset) > 0) {
2072 __wsum csum2;
2073 if (copy > len)
2074 copy = len;
2075 csum2 = __skb_checksum(frag_iter, offset - start,
2076 copy, 0, ops);
2077 csum = ops->combine(csum, csum2, pos, copy);
2078 if ((len -= copy) == 0)
2079 return csum;
2080 offset += copy;
2081 pos += copy;
2082 }
2083 start = end;
2084 }
2085 BUG_ON(len);
2086
2087 return csum;
2088 }
2089 EXPORT_SYMBOL(__skb_checksum);
2090
2091 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2092 int len, __wsum csum)
2093 {
2094 const struct skb_checksum_ops ops = {
2095 .update = csum_partial_ext,
2096 .combine = csum_block_add_ext,
2097 };
2098
2099 return __skb_checksum(skb, offset, len, csum, &ops);
2100 }
2101 EXPORT_SYMBOL(skb_checksum);
2102
2103 /* Both of above in one bottle. */
2104
2105 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2106 u8 *to, int len, __wsum csum)
2107 {
2108 int start = skb_headlen(skb);
2109 int i, copy = start - offset;
2110 struct sk_buff *frag_iter;
2111 int pos = 0;
2112
2113 /* Copy header. */
2114 if (copy > 0) {
2115 if (copy > len)
2116 copy = len;
2117 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2118 copy, csum);
2119 if ((len -= copy) == 0)
2120 return csum;
2121 offset += copy;
2122 to += copy;
2123 pos = copy;
2124 }
2125
2126 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2127 int end;
2128
2129 WARN_ON(start > offset + len);
2130
2131 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2132 if ((copy = end - offset) > 0) {
2133 __wsum csum2;
2134 u8 *vaddr;
2135 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2136
2137 if (copy > len)
2138 copy = len;
2139 vaddr = kmap_atomic(skb_frag_page(frag));
2140 csum2 = csum_partial_copy_nocheck(vaddr +
2141 frag->page_offset +
2142 offset - start, to,
2143 copy, 0);
2144 kunmap_atomic(vaddr);
2145 csum = csum_block_add(csum, csum2, pos);
2146 if (!(len -= copy))
2147 return csum;
2148 offset += copy;
2149 to += copy;
2150 pos += copy;
2151 }
2152 start = end;
2153 }
2154
2155 skb_walk_frags(skb, frag_iter) {
2156 __wsum csum2;
2157 int end;
2158
2159 WARN_ON(start > offset + len);
2160
2161 end = start + frag_iter->len;
2162 if ((copy = end - offset) > 0) {
2163 if (copy > len)
2164 copy = len;
2165 csum2 = skb_copy_and_csum_bits(frag_iter,
2166 offset - start,
2167 to, copy, 0);
2168 csum = csum_block_add(csum, csum2, pos);
2169 if ((len -= copy) == 0)
2170 return csum;
2171 offset += copy;
2172 to += copy;
2173 pos += copy;
2174 }
2175 start = end;
2176 }
2177 BUG_ON(len);
2178 return csum;
2179 }
2180 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2181
2182 /**
2183 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2184 * @from: source buffer
2185 *
2186 * Calculates the amount of linear headroom needed in the 'to' skb passed
2187 * into skb_zerocopy().
2188 */
2189 unsigned int
2190 skb_zerocopy_headlen(const struct sk_buff *from)
2191 {
2192 unsigned int hlen = 0;
2193
2194 if (!from->head_frag ||
2195 skb_headlen(from) < L1_CACHE_BYTES ||
2196 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2197 hlen = skb_headlen(from);
2198
2199 if (skb_has_frag_list(from))
2200 hlen = from->len;
2201
2202 return hlen;
2203 }
2204 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2205
2206 /**
2207 * skb_zerocopy - Zero copy skb to skb
2208 * @to: destination buffer
2209 * @from: source buffer
2210 * @len: number of bytes to copy from source buffer
2211 * @hlen: size of linear headroom in destination buffer
2212 *
2213 * Copies up to `len` bytes from `from` to `to` by creating references
2214 * to the frags in the source buffer.
2215 *
2216 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2217 * headroom in the `to` buffer.
2218 *
2219 * Return value:
2220 * 0: everything is OK
2221 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2222 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2223 */
2224 int
2225 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2226 {
2227 int i, j = 0;
2228 int plen = 0; /* length of skb->head fragment */
2229 int ret;
2230 struct page *page;
2231 unsigned int offset;
2232
2233 BUG_ON(!from->head_frag && !hlen);
2234
2235 /* dont bother with small payloads */
2236 if (len <= skb_tailroom(to))
2237 return skb_copy_bits(from, 0, skb_put(to, len), len);
2238
2239 if (hlen) {
2240 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2241 if (unlikely(ret))
2242 return ret;
2243 len -= hlen;
2244 } else {
2245 plen = min_t(int, skb_headlen(from), len);
2246 if (plen) {
2247 page = virt_to_head_page(from->head);
2248 offset = from->data - (unsigned char *)page_address(page);
2249 __skb_fill_page_desc(to, 0, page, offset, plen);
2250 get_page(page);
2251 j = 1;
2252 len -= plen;
2253 }
2254 }
2255
2256 to->truesize += len + plen;
2257 to->len += len + plen;
2258 to->data_len += len + plen;
2259
2260 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2261 skb_tx_error(from);
2262 return -ENOMEM;
2263 }
2264
2265 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2266 if (!len)
2267 break;
2268 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2269 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2270 len -= skb_shinfo(to)->frags[j].size;
2271 skb_frag_ref(to, j);
2272 j++;
2273 }
2274 skb_shinfo(to)->nr_frags = j;
2275
2276 return 0;
2277 }
2278 EXPORT_SYMBOL_GPL(skb_zerocopy);
2279
2280 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2281 {
2282 __wsum csum;
2283 long csstart;
2284
2285 if (skb->ip_summed == CHECKSUM_PARTIAL)
2286 csstart = skb_checksum_start_offset(skb);
2287 else
2288 csstart = skb_headlen(skb);
2289
2290 BUG_ON(csstart > skb_headlen(skb));
2291
2292 skb_copy_from_linear_data(skb, to, csstart);
2293
2294 csum = 0;
2295 if (csstart != skb->len)
2296 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2297 skb->len - csstart, 0);
2298
2299 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2300 long csstuff = csstart + skb->csum_offset;
2301
2302 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2303 }
2304 }
2305 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2306
2307 /**
2308 * skb_dequeue - remove from the head of the queue
2309 * @list: list to dequeue from
2310 *
2311 * Remove the head of the list. The list lock is taken so the function
2312 * may be used safely with other locking list functions. The head item is
2313 * returned or %NULL if the list is empty.
2314 */
2315
2316 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2317 {
2318 unsigned long flags;
2319 struct sk_buff *result;
2320
2321 spin_lock_irqsave(&list->lock, flags);
2322 result = __skb_dequeue(list);
2323 spin_unlock_irqrestore(&list->lock, flags);
2324 return result;
2325 }
2326 EXPORT_SYMBOL(skb_dequeue);
2327
2328 /**
2329 * skb_dequeue_tail - remove from the tail of the queue
2330 * @list: list to dequeue from
2331 *
2332 * Remove the tail of the list. The list lock is taken so the function
2333 * may be used safely with other locking list functions. The tail item is
2334 * returned or %NULL if the list is empty.
2335 */
2336 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2337 {
2338 unsigned long flags;
2339 struct sk_buff *result;
2340
2341 spin_lock_irqsave(&list->lock, flags);
2342 result = __skb_dequeue_tail(list);
2343 spin_unlock_irqrestore(&list->lock, flags);
2344 return result;
2345 }
2346 EXPORT_SYMBOL(skb_dequeue_tail);
2347
2348 /**
2349 * skb_queue_purge - empty a list
2350 * @list: list to empty
2351 *
2352 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2353 * the list and one reference dropped. This function takes the list
2354 * lock and is atomic with respect to other list locking functions.
2355 */
2356 void skb_queue_purge(struct sk_buff_head *list)
2357 {
2358 struct sk_buff *skb;
2359 while ((skb = skb_dequeue(list)) != NULL)
2360 kfree_skb(skb);
2361 }
2362 EXPORT_SYMBOL(skb_queue_purge);
2363
2364 /**
2365 * skb_queue_head - queue a buffer at the list head
2366 * @list: list to use
2367 * @newsk: buffer to queue
2368 *
2369 * Queue a buffer at the start of the list. This function takes the
2370 * list lock and can be used safely with other locking &sk_buff functions
2371 * safely.
2372 *
2373 * A buffer cannot be placed on two lists at the same time.
2374 */
2375 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2376 {
2377 unsigned long flags;
2378
2379 spin_lock_irqsave(&list->lock, flags);
2380 __skb_queue_head(list, newsk);
2381 spin_unlock_irqrestore(&list->lock, flags);
2382 }
2383 EXPORT_SYMBOL(skb_queue_head);
2384
2385 /**
2386 * skb_queue_tail - queue a buffer at the list tail
2387 * @list: list to use
2388 * @newsk: buffer to queue
2389 *
2390 * Queue a buffer at the tail of the list. This function takes the
2391 * list lock and can be used safely with other locking &sk_buff functions
2392 * safely.
2393 *
2394 * A buffer cannot be placed on two lists at the same time.
2395 */
2396 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2397 {
2398 unsigned long flags;
2399
2400 spin_lock_irqsave(&list->lock, flags);
2401 __skb_queue_tail(list, newsk);
2402 spin_unlock_irqrestore(&list->lock, flags);
2403 }
2404 EXPORT_SYMBOL(skb_queue_tail);
2405
2406 /**
2407 * skb_unlink - remove a buffer from a list
2408 * @skb: buffer to remove
2409 * @list: list to use
2410 *
2411 * Remove a packet from a list. The list locks are taken and this
2412 * function is atomic with respect to other list locked calls
2413 *
2414 * You must know what list the SKB is on.
2415 */
2416 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2417 {
2418 unsigned long flags;
2419
2420 spin_lock_irqsave(&list->lock, flags);
2421 __skb_unlink(skb, list);
2422 spin_unlock_irqrestore(&list->lock, flags);
2423 }
2424 EXPORT_SYMBOL(skb_unlink);
2425
2426 /**
2427 * skb_append - append a buffer
2428 * @old: buffer to insert after
2429 * @newsk: buffer to insert
2430 * @list: list to use
2431 *
2432 * Place a packet after a given packet in a list. The list locks are taken
2433 * and this function is atomic with respect to other list locked calls.
2434 * A buffer cannot be placed on two lists at the same time.
2435 */
2436 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2437 {
2438 unsigned long flags;
2439
2440 spin_lock_irqsave(&list->lock, flags);
2441 __skb_queue_after(list, old, newsk);
2442 spin_unlock_irqrestore(&list->lock, flags);
2443 }
2444 EXPORT_SYMBOL(skb_append);
2445
2446 /**
2447 * skb_insert - insert a buffer
2448 * @old: buffer to insert before
2449 * @newsk: buffer to insert
2450 * @list: list to use
2451 *
2452 * Place a packet before a given packet in a list. The list locks are
2453 * taken and this function is atomic with respect to other list locked
2454 * calls.
2455 *
2456 * A buffer cannot be placed on two lists at the same time.
2457 */
2458 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2459 {
2460 unsigned long flags;
2461
2462 spin_lock_irqsave(&list->lock, flags);
2463 __skb_insert(newsk, old->prev, old, list);
2464 spin_unlock_irqrestore(&list->lock, flags);
2465 }
2466 EXPORT_SYMBOL(skb_insert);
2467
2468 static inline void skb_split_inside_header(struct sk_buff *skb,
2469 struct sk_buff* skb1,
2470 const u32 len, const int pos)
2471 {
2472 int i;
2473
2474 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2475 pos - len);
2476 /* And move data appendix as is. */
2477 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2478 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2479
2480 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2481 skb_shinfo(skb)->nr_frags = 0;
2482 skb1->data_len = skb->data_len;
2483 skb1->len += skb1->data_len;
2484 skb->data_len = 0;
2485 skb->len = len;
2486 skb_set_tail_pointer(skb, len);
2487 }
2488
2489 static inline void skb_split_no_header(struct sk_buff *skb,
2490 struct sk_buff* skb1,
2491 const u32 len, int pos)
2492 {
2493 int i, k = 0;
2494 const int nfrags = skb_shinfo(skb)->nr_frags;
2495
2496 skb_shinfo(skb)->nr_frags = 0;
2497 skb1->len = skb1->data_len = skb->len - len;
2498 skb->len = len;
2499 skb->data_len = len - pos;
2500
2501 for (i = 0; i < nfrags; i++) {
2502 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2503
2504 if (pos + size > len) {
2505 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2506
2507 if (pos < len) {
2508 /* Split frag.
2509 * We have two variants in this case:
2510 * 1. Move all the frag to the second
2511 * part, if it is possible. F.e.
2512 * this approach is mandatory for TUX,
2513 * where splitting is expensive.
2514 * 2. Split is accurately. We make this.
2515 */
2516 skb_frag_ref(skb, i);
2517 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2518 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2519 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2520 skb_shinfo(skb)->nr_frags++;
2521 }
2522 k++;
2523 } else
2524 skb_shinfo(skb)->nr_frags++;
2525 pos += size;
2526 }
2527 skb_shinfo(skb1)->nr_frags = k;
2528 }
2529
2530 /**
2531 * skb_split - Split fragmented skb to two parts at length len.
2532 * @skb: the buffer to split
2533 * @skb1: the buffer to receive the second part
2534 * @len: new length for skb
2535 */
2536 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2537 {
2538 int pos = skb_headlen(skb);
2539
2540 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2541 if (len < pos) /* Split line is inside header. */
2542 skb_split_inside_header(skb, skb1, len, pos);
2543 else /* Second chunk has no header, nothing to copy. */
2544 skb_split_no_header(skb, skb1, len, pos);
2545 }
2546 EXPORT_SYMBOL(skb_split);
2547
2548 /* Shifting from/to a cloned skb is a no-go.
2549 *
2550 * Caller cannot keep skb_shinfo related pointers past calling here!
2551 */
2552 static int skb_prepare_for_shift(struct sk_buff *skb)
2553 {
2554 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2555 }
2556
2557 /**
2558 * skb_shift - Shifts paged data partially from skb to another
2559 * @tgt: buffer into which tail data gets added
2560 * @skb: buffer from which the paged data comes from
2561 * @shiftlen: shift up to this many bytes
2562 *
2563 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2564 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2565 * It's up to caller to free skb if everything was shifted.
2566 *
2567 * If @tgt runs out of frags, the whole operation is aborted.
2568 *
2569 * Skb cannot include anything else but paged data while tgt is allowed
2570 * to have non-paged data as well.
2571 *
2572 * TODO: full sized shift could be optimized but that would need
2573 * specialized skb free'er to handle frags without up-to-date nr_frags.
2574 */
2575 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2576 {
2577 int from, to, merge, todo;
2578 struct skb_frag_struct *fragfrom, *fragto;
2579
2580 BUG_ON(shiftlen > skb->len);
2581 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2582
2583 todo = shiftlen;
2584 from = 0;
2585 to = skb_shinfo(tgt)->nr_frags;
2586 fragfrom = &skb_shinfo(skb)->frags[from];
2587
2588 /* Actual merge is delayed until the point when we know we can
2589 * commit all, so that we don't have to undo partial changes
2590 */
2591 if (!to ||
2592 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2593 fragfrom->page_offset)) {
2594 merge = -1;
2595 } else {
2596 merge = to - 1;
2597
2598 todo -= skb_frag_size(fragfrom);
2599 if (todo < 0) {
2600 if (skb_prepare_for_shift(skb) ||
2601 skb_prepare_for_shift(tgt))
2602 return 0;
2603
2604 /* All previous frag pointers might be stale! */
2605 fragfrom = &skb_shinfo(skb)->frags[from];
2606 fragto = &skb_shinfo(tgt)->frags[merge];
2607
2608 skb_frag_size_add(fragto, shiftlen);
2609 skb_frag_size_sub(fragfrom, shiftlen);
2610 fragfrom->page_offset += shiftlen;
2611
2612 goto onlymerged;
2613 }
2614
2615 from++;
2616 }
2617
2618 /* Skip full, not-fitting skb to avoid expensive operations */
2619 if ((shiftlen == skb->len) &&
2620 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2621 return 0;
2622
2623 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2624 return 0;
2625
2626 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2627 if (to == MAX_SKB_FRAGS)
2628 return 0;
2629
2630 fragfrom = &skb_shinfo(skb)->frags[from];
2631 fragto = &skb_shinfo(tgt)->frags[to];
2632
2633 if (todo >= skb_frag_size(fragfrom)) {
2634 *fragto = *fragfrom;
2635 todo -= skb_frag_size(fragfrom);
2636 from++;
2637 to++;
2638
2639 } else {
2640 __skb_frag_ref(fragfrom);
2641 fragto->page = fragfrom->page;
2642 fragto->page_offset = fragfrom->page_offset;
2643 skb_frag_size_set(fragto, todo);
2644
2645 fragfrom->page_offset += todo;
2646 skb_frag_size_sub(fragfrom, todo);
2647 todo = 0;
2648
2649 to++;
2650 break;
2651 }
2652 }
2653
2654 /* Ready to "commit" this state change to tgt */
2655 skb_shinfo(tgt)->nr_frags = to;
2656
2657 if (merge >= 0) {
2658 fragfrom = &skb_shinfo(skb)->frags[0];
2659 fragto = &skb_shinfo(tgt)->frags[merge];
2660
2661 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2662 __skb_frag_unref(fragfrom);
2663 }
2664
2665 /* Reposition in the original skb */
2666 to = 0;
2667 while (from < skb_shinfo(skb)->nr_frags)
2668 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2669 skb_shinfo(skb)->nr_frags = to;
2670
2671 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2672
2673 onlymerged:
2674 /* Most likely the tgt won't ever need its checksum anymore, skb on
2675 * the other hand might need it if it needs to be resent
2676 */
2677 tgt->ip_summed = CHECKSUM_PARTIAL;
2678 skb->ip_summed = CHECKSUM_PARTIAL;
2679
2680 /* Yak, is it really working this way? Some helper please? */
2681 skb->len -= shiftlen;
2682 skb->data_len -= shiftlen;
2683 skb->truesize -= shiftlen;
2684 tgt->len += shiftlen;
2685 tgt->data_len += shiftlen;
2686 tgt->truesize += shiftlen;
2687
2688 return shiftlen;
2689 }
2690
2691 /**
2692 * skb_prepare_seq_read - Prepare a sequential read of skb data
2693 * @skb: the buffer to read
2694 * @from: lower offset of data to be read
2695 * @to: upper offset of data to be read
2696 * @st: state variable
2697 *
2698 * Initializes the specified state variable. Must be called before
2699 * invoking skb_seq_read() for the first time.
2700 */
2701 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2702 unsigned int to, struct skb_seq_state *st)
2703 {
2704 st->lower_offset = from;
2705 st->upper_offset = to;
2706 st->root_skb = st->cur_skb = skb;
2707 st->frag_idx = st->stepped_offset = 0;
2708 st->frag_data = NULL;
2709 }
2710 EXPORT_SYMBOL(skb_prepare_seq_read);
2711
2712 /**
2713 * skb_seq_read - Sequentially read skb data
2714 * @consumed: number of bytes consumed by the caller so far
2715 * @data: destination pointer for data to be returned
2716 * @st: state variable
2717 *
2718 * Reads a block of skb data at @consumed relative to the
2719 * lower offset specified to skb_prepare_seq_read(). Assigns
2720 * the head of the data block to @data and returns the length
2721 * of the block or 0 if the end of the skb data or the upper
2722 * offset has been reached.
2723 *
2724 * The caller is not required to consume all of the data
2725 * returned, i.e. @consumed is typically set to the number
2726 * of bytes already consumed and the next call to
2727 * skb_seq_read() will return the remaining part of the block.
2728 *
2729 * Note 1: The size of each block of data returned can be arbitrary,
2730 * this limitation is the cost for zerocopy sequential
2731 * reads of potentially non linear data.
2732 *
2733 * Note 2: Fragment lists within fragments are not implemented
2734 * at the moment, state->root_skb could be replaced with
2735 * a stack for this purpose.
2736 */
2737 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2738 struct skb_seq_state *st)
2739 {
2740 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2741 skb_frag_t *frag;
2742
2743 if (unlikely(abs_offset >= st->upper_offset)) {
2744 if (st->frag_data) {
2745 kunmap_atomic(st->frag_data);
2746 st->frag_data = NULL;
2747 }
2748 return 0;
2749 }
2750
2751 next_skb:
2752 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2753
2754 if (abs_offset < block_limit && !st->frag_data) {
2755 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2756 return block_limit - abs_offset;
2757 }
2758
2759 if (st->frag_idx == 0 && !st->frag_data)
2760 st->stepped_offset += skb_headlen(st->cur_skb);
2761
2762 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2763 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2764 block_limit = skb_frag_size(frag) + st->stepped_offset;
2765
2766 if (abs_offset < block_limit) {
2767 if (!st->frag_data)
2768 st->frag_data = kmap_atomic(skb_frag_page(frag));
2769
2770 *data = (u8 *) st->frag_data + frag->page_offset +
2771 (abs_offset - st->stepped_offset);
2772
2773 return block_limit - abs_offset;
2774 }
2775
2776 if (st->frag_data) {
2777 kunmap_atomic(st->frag_data);
2778 st->frag_data = NULL;
2779 }
2780
2781 st->frag_idx++;
2782 st->stepped_offset += skb_frag_size(frag);
2783 }
2784
2785 if (st->frag_data) {
2786 kunmap_atomic(st->frag_data);
2787 st->frag_data = NULL;
2788 }
2789
2790 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2791 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2792 st->frag_idx = 0;
2793 goto next_skb;
2794 } else if (st->cur_skb->next) {
2795 st->cur_skb = st->cur_skb->next;
2796 st->frag_idx = 0;
2797 goto next_skb;
2798 }
2799
2800 return 0;
2801 }
2802 EXPORT_SYMBOL(skb_seq_read);
2803
2804 /**
2805 * skb_abort_seq_read - Abort a sequential read of skb data
2806 * @st: state variable
2807 *
2808 * Must be called if skb_seq_read() was not called until it
2809 * returned 0.
2810 */
2811 void skb_abort_seq_read(struct skb_seq_state *st)
2812 {
2813 if (st->frag_data)
2814 kunmap_atomic(st->frag_data);
2815 }
2816 EXPORT_SYMBOL(skb_abort_seq_read);
2817
2818 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2819
2820 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2821 struct ts_config *conf,
2822 struct ts_state *state)
2823 {
2824 return skb_seq_read(offset, text, TS_SKB_CB(state));
2825 }
2826
2827 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2828 {
2829 skb_abort_seq_read(TS_SKB_CB(state));
2830 }
2831
2832 /**
2833 * skb_find_text - Find a text pattern in skb data
2834 * @skb: the buffer to look in
2835 * @from: search offset
2836 * @to: search limit
2837 * @config: textsearch configuration
2838 *
2839 * Finds a pattern in the skb data according to the specified
2840 * textsearch configuration. Use textsearch_next() to retrieve
2841 * subsequent occurrences of the pattern. Returns the offset
2842 * to the first occurrence or UINT_MAX if no match was found.
2843 */
2844 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2845 unsigned int to, struct ts_config *config)
2846 {
2847 struct ts_state state;
2848 unsigned int ret;
2849
2850 config->get_next_block = skb_ts_get_next_block;
2851 config->finish = skb_ts_finish;
2852
2853 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2854
2855 ret = textsearch_find(config, &state);
2856 return (ret <= to - from ? ret : UINT_MAX);
2857 }
2858 EXPORT_SYMBOL(skb_find_text);
2859
2860 /**
2861 * skb_append_datato_frags - append the user data to a skb
2862 * @sk: sock structure
2863 * @skb: skb structure to be appended with user data.
2864 * @getfrag: call back function to be used for getting the user data
2865 * @from: pointer to user message iov
2866 * @length: length of the iov message
2867 *
2868 * Description: This procedure append the user data in the fragment part
2869 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2870 */
2871 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2872 int (*getfrag)(void *from, char *to, int offset,
2873 int len, int odd, struct sk_buff *skb),
2874 void *from, int length)
2875 {
2876 int frg_cnt = skb_shinfo(skb)->nr_frags;
2877 int copy;
2878 int offset = 0;
2879 int ret;
2880 struct page_frag *pfrag = &current->task_frag;
2881
2882 do {
2883 /* Return error if we don't have space for new frag */
2884 if (frg_cnt >= MAX_SKB_FRAGS)
2885 return -EMSGSIZE;
2886
2887 if (!sk_page_frag_refill(sk, pfrag))
2888 return -ENOMEM;
2889
2890 /* copy the user data to page */
2891 copy = min_t(int, length, pfrag->size - pfrag->offset);
2892
2893 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2894 offset, copy, 0, skb);
2895 if (ret < 0)
2896 return -EFAULT;
2897
2898 /* copy was successful so update the size parameters */
2899 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2900 copy);
2901 frg_cnt++;
2902 pfrag->offset += copy;
2903 get_page(pfrag->page);
2904
2905 skb->truesize += copy;
2906 atomic_add(copy, &sk->sk_wmem_alloc);
2907 skb->len += copy;
2908 skb->data_len += copy;
2909 offset += copy;
2910 length -= copy;
2911
2912 } while (length > 0);
2913
2914 return 0;
2915 }
2916 EXPORT_SYMBOL(skb_append_datato_frags);
2917
2918 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2919 int offset, size_t size)
2920 {
2921 int i = skb_shinfo(skb)->nr_frags;
2922
2923 if (skb_can_coalesce(skb, i, page, offset)) {
2924 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2925 } else if (i < MAX_SKB_FRAGS) {
2926 get_page(page);
2927 skb_fill_page_desc(skb, i, page, offset, size);
2928 } else {
2929 return -EMSGSIZE;
2930 }
2931
2932 return 0;
2933 }
2934 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2935
2936 /**
2937 * skb_pull_rcsum - pull skb and update receive checksum
2938 * @skb: buffer to update
2939 * @len: length of data pulled
2940 *
2941 * This function performs an skb_pull on the packet and updates
2942 * the CHECKSUM_COMPLETE checksum. It should be used on
2943 * receive path processing instead of skb_pull unless you know
2944 * that the checksum difference is zero (e.g., a valid IP header)
2945 * or you are setting ip_summed to CHECKSUM_NONE.
2946 */
2947 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2948 {
2949 BUG_ON(len > skb->len);
2950 skb->len -= len;
2951 BUG_ON(skb->len < skb->data_len);
2952 skb_postpull_rcsum(skb, skb->data, len);
2953 return skb->data += len;
2954 }
2955 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2956
2957 /**
2958 * skb_segment - Perform protocol segmentation on skb.
2959 * @head_skb: buffer to segment
2960 * @features: features for the output path (see dev->features)
2961 *
2962 * This function performs segmentation on the given skb. It returns
2963 * a pointer to the first in a list of new skbs for the segments.
2964 * In case of error it returns ERR_PTR(err).
2965 */
2966 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2967 netdev_features_t features)
2968 {
2969 struct sk_buff *segs = NULL;
2970 struct sk_buff *tail = NULL;
2971 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2972 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2973 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2974 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2975 struct sk_buff *frag_skb = head_skb;
2976 unsigned int offset = doffset;
2977 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2978 unsigned int headroom;
2979 unsigned int len;
2980 __be16 proto;
2981 bool csum;
2982 int sg = !!(features & NETIF_F_SG);
2983 int nfrags = skb_shinfo(head_skb)->nr_frags;
2984 int err = -ENOMEM;
2985 int i = 0;
2986 int pos;
2987 int dummy;
2988
2989 __skb_push(head_skb, doffset);
2990 proto = skb_network_protocol(head_skb, &dummy);
2991 if (unlikely(!proto))
2992 return ERR_PTR(-EINVAL);
2993
2994 csum = !head_skb->encap_hdr_csum &&
2995 !!can_checksum_protocol(features, proto);
2996
2997 headroom = skb_headroom(head_skb);
2998 pos = skb_headlen(head_skb);
2999
3000 do {
3001 struct sk_buff *nskb;
3002 skb_frag_t *nskb_frag;
3003 int hsize;
3004 int size;
3005
3006 len = head_skb->len - offset;
3007 if (len > mss)
3008 len = mss;
3009
3010 hsize = skb_headlen(head_skb) - offset;
3011 if (hsize < 0)
3012 hsize = 0;
3013 if (hsize > len || !sg)
3014 hsize = len;
3015
3016 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3017 (skb_headlen(list_skb) == len || sg)) {
3018 BUG_ON(skb_headlen(list_skb) > len);
3019
3020 i = 0;
3021 nfrags = skb_shinfo(list_skb)->nr_frags;
3022 frag = skb_shinfo(list_skb)->frags;
3023 frag_skb = list_skb;
3024 pos += skb_headlen(list_skb);
3025
3026 while (pos < offset + len) {
3027 BUG_ON(i >= nfrags);
3028
3029 size = skb_frag_size(frag);
3030 if (pos + size > offset + len)
3031 break;
3032
3033 i++;
3034 pos += size;
3035 frag++;
3036 }
3037
3038 nskb = skb_clone(list_skb, GFP_ATOMIC);
3039 list_skb = list_skb->next;
3040
3041 if (unlikely(!nskb))
3042 goto err;
3043
3044 if (unlikely(pskb_trim(nskb, len))) {
3045 kfree_skb(nskb);
3046 goto err;
3047 }
3048
3049 hsize = skb_end_offset(nskb);
3050 if (skb_cow_head(nskb, doffset + headroom)) {
3051 kfree_skb(nskb);
3052 goto err;
3053 }
3054
3055 nskb->truesize += skb_end_offset(nskb) - hsize;
3056 skb_release_head_state(nskb);
3057 __skb_push(nskb, doffset);
3058 } else {
3059 nskb = __alloc_skb(hsize + doffset + headroom,
3060 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3061 NUMA_NO_NODE);
3062
3063 if (unlikely(!nskb))
3064 goto err;
3065
3066 skb_reserve(nskb, headroom);
3067 __skb_put(nskb, doffset);
3068 }
3069
3070 if (segs)
3071 tail->next = nskb;
3072 else
3073 segs = nskb;
3074 tail = nskb;
3075
3076 __copy_skb_header(nskb, head_skb);
3077
3078 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3079 skb_reset_mac_len(nskb);
3080
3081 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3082 nskb->data - tnl_hlen,
3083 doffset + tnl_hlen);
3084
3085 if (nskb->len == len + doffset)
3086 goto perform_csum_check;
3087
3088 if (!sg && !nskb->remcsum_offload) {
3089 nskb->ip_summed = CHECKSUM_NONE;
3090 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3091 skb_put(nskb, len),
3092 len, 0);
3093 SKB_GSO_CB(nskb)->csum_start =
3094 skb_headroom(nskb) + doffset;
3095 continue;
3096 }
3097
3098 nskb_frag = skb_shinfo(nskb)->frags;
3099
3100 skb_copy_from_linear_data_offset(head_skb, offset,
3101 skb_put(nskb, hsize), hsize);
3102
3103 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3104 SKBTX_SHARED_FRAG;
3105
3106 while (pos < offset + len) {
3107 if (i >= nfrags) {
3108 BUG_ON(skb_headlen(list_skb));
3109
3110 i = 0;
3111 nfrags = skb_shinfo(list_skb)->nr_frags;
3112 frag = skb_shinfo(list_skb)->frags;
3113 frag_skb = list_skb;
3114
3115 BUG_ON(!nfrags);
3116
3117 list_skb = list_skb->next;
3118 }
3119
3120 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3121 MAX_SKB_FRAGS)) {
3122 net_warn_ratelimited(
3123 "skb_segment: too many frags: %u %u\n",
3124 pos, mss);
3125 goto err;
3126 }
3127
3128 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3129 goto err;
3130
3131 *nskb_frag = *frag;
3132 __skb_frag_ref(nskb_frag);
3133 size = skb_frag_size(nskb_frag);
3134
3135 if (pos < offset) {
3136 nskb_frag->page_offset += offset - pos;
3137 skb_frag_size_sub(nskb_frag, offset - pos);
3138 }
3139
3140 skb_shinfo(nskb)->nr_frags++;
3141
3142 if (pos + size <= offset + len) {
3143 i++;
3144 frag++;
3145 pos += size;
3146 } else {
3147 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3148 goto skip_fraglist;
3149 }
3150
3151 nskb_frag++;
3152 }
3153
3154 skip_fraglist:
3155 nskb->data_len = len - hsize;
3156 nskb->len += nskb->data_len;
3157 nskb->truesize += nskb->data_len;
3158
3159 perform_csum_check:
3160 if (!csum && !nskb->remcsum_offload) {
3161 nskb->csum = skb_checksum(nskb, doffset,
3162 nskb->len - doffset, 0);
3163 nskb->ip_summed = CHECKSUM_NONE;
3164 SKB_GSO_CB(nskb)->csum_start =
3165 skb_headroom(nskb) + doffset;
3166 }
3167 } while ((offset += len) < head_skb->len);
3168
3169 /* Some callers want to get the end of the list.
3170 * Put it in segs->prev to avoid walking the list.
3171 * (see validate_xmit_skb_list() for example)
3172 */
3173 segs->prev = tail;
3174
3175 /* Following permits correct backpressure, for protocols
3176 * using skb_set_owner_w().
3177 * Idea is to tranfert ownership from head_skb to last segment.
3178 */
3179 if (head_skb->destructor == sock_wfree) {
3180 swap(tail->truesize, head_skb->truesize);
3181 swap(tail->destructor, head_skb->destructor);
3182 swap(tail->sk, head_skb->sk);
3183 }
3184 return segs;
3185
3186 err:
3187 kfree_skb_list(segs);
3188 return ERR_PTR(err);
3189 }
3190 EXPORT_SYMBOL_GPL(skb_segment);
3191
3192 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3193 {
3194 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3195 unsigned int offset = skb_gro_offset(skb);
3196 unsigned int headlen = skb_headlen(skb);
3197 unsigned int len = skb_gro_len(skb);
3198 struct sk_buff *lp, *p = *head;
3199 unsigned int delta_truesize;
3200
3201 if (unlikely(p->len + len >= 65536))
3202 return -E2BIG;
3203
3204 lp = NAPI_GRO_CB(p)->last;
3205 pinfo = skb_shinfo(lp);
3206
3207 if (headlen <= offset) {
3208 skb_frag_t *frag;
3209 skb_frag_t *frag2;
3210 int i = skbinfo->nr_frags;
3211 int nr_frags = pinfo->nr_frags + i;
3212
3213 if (nr_frags > MAX_SKB_FRAGS)
3214 goto merge;
3215
3216 offset -= headlen;
3217 pinfo->nr_frags = nr_frags;
3218 skbinfo->nr_frags = 0;
3219
3220 frag = pinfo->frags + nr_frags;
3221 frag2 = skbinfo->frags + i;
3222 do {
3223 *--frag = *--frag2;
3224 } while (--i);
3225
3226 frag->page_offset += offset;
3227 skb_frag_size_sub(frag, offset);
3228
3229 /* all fragments truesize : remove (head size + sk_buff) */
3230 delta_truesize = skb->truesize -
3231 SKB_TRUESIZE(skb_end_offset(skb));
3232
3233 skb->truesize -= skb->data_len;
3234 skb->len -= skb->data_len;
3235 skb->data_len = 0;
3236
3237 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3238 goto done;
3239 } else if (skb->head_frag) {
3240 int nr_frags = pinfo->nr_frags;
3241 skb_frag_t *frag = pinfo->frags + nr_frags;
3242 struct page *page = virt_to_head_page(skb->head);
3243 unsigned int first_size = headlen - offset;
3244 unsigned int first_offset;
3245
3246 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3247 goto merge;
3248
3249 first_offset = skb->data -
3250 (unsigned char *)page_address(page) +
3251 offset;
3252
3253 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3254
3255 frag->page.p = page;
3256 frag->page_offset = first_offset;
3257 skb_frag_size_set(frag, first_size);
3258
3259 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3260 /* We dont need to clear skbinfo->nr_frags here */
3261
3262 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3263 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3264 goto done;
3265 }
3266
3267 merge:
3268 delta_truesize = skb->truesize;
3269 if (offset > headlen) {
3270 unsigned int eat = offset - headlen;
3271
3272 skbinfo->frags[0].page_offset += eat;
3273 skb_frag_size_sub(&skbinfo->frags[0], eat);
3274 skb->data_len -= eat;
3275 skb->len -= eat;
3276 offset = headlen;
3277 }
3278
3279 __skb_pull(skb, offset);
3280
3281 if (NAPI_GRO_CB(p)->last == p)
3282 skb_shinfo(p)->frag_list = skb;
3283 else
3284 NAPI_GRO_CB(p)->last->next = skb;
3285 NAPI_GRO_CB(p)->last = skb;
3286 __skb_header_release(skb);
3287 lp = p;
3288
3289 done:
3290 NAPI_GRO_CB(p)->count++;
3291 p->data_len += len;
3292 p->truesize += delta_truesize;
3293 p->len += len;
3294 if (lp != p) {
3295 lp->data_len += len;
3296 lp->truesize += delta_truesize;
3297 lp->len += len;
3298 }
3299 NAPI_GRO_CB(skb)->same_flow = 1;
3300 return 0;
3301 }
3302
3303 void __init skb_init(void)
3304 {
3305 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3306 sizeof(struct sk_buff),
3307 0,
3308 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3309 NULL);
3310 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3311 sizeof(struct sk_buff_fclones),
3312 0,
3313 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3314 NULL);
3315 }
3316
3317 /**
3318 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3319 * @skb: Socket buffer containing the buffers to be mapped
3320 * @sg: The scatter-gather list to map into
3321 * @offset: The offset into the buffer's contents to start mapping
3322 * @len: Length of buffer space to be mapped
3323 *
3324 * Fill the specified scatter-gather list with mappings/pointers into a
3325 * region of the buffer space attached to a socket buffer.
3326 */
3327 static int
3328 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3329 {
3330 int start = skb_headlen(skb);
3331 int i, copy = start - offset;
3332 struct sk_buff *frag_iter;
3333 int elt = 0;
3334
3335 if (copy > 0) {
3336 if (copy > len)
3337 copy = len;
3338 sg_set_buf(sg, skb->data + offset, copy);
3339 elt++;
3340 if ((len -= copy) == 0)
3341 return elt;
3342 offset += copy;
3343 }
3344
3345 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3346 int end;
3347
3348 WARN_ON(start > offset + len);
3349
3350 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3351 if ((copy = end - offset) > 0) {
3352 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3353
3354 if (copy > len)
3355 copy = len;
3356 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3357 frag->page_offset+offset-start);
3358 elt++;
3359 if (!(len -= copy))
3360 return elt;
3361 offset += copy;
3362 }
3363 start = end;
3364 }
3365
3366 skb_walk_frags(skb, frag_iter) {
3367 int end;
3368
3369 WARN_ON(start > offset + len);
3370
3371 end = start + frag_iter->len;
3372 if ((copy = end - offset) > 0) {
3373 if (copy > len)
3374 copy = len;
3375 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3376 copy);
3377 if ((len -= copy) == 0)
3378 return elt;
3379 offset += copy;
3380 }
3381 start = end;
3382 }
3383 BUG_ON(len);
3384 return elt;
3385 }
3386
3387 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3388 * sglist without mark the sg which contain last skb data as the end.
3389 * So the caller can mannipulate sg list as will when padding new data after
3390 * the first call without calling sg_unmark_end to expend sg list.
3391 *
3392 * Scenario to use skb_to_sgvec_nomark:
3393 * 1. sg_init_table
3394 * 2. skb_to_sgvec_nomark(payload1)
3395 * 3. skb_to_sgvec_nomark(payload2)
3396 *
3397 * This is equivalent to:
3398 * 1. sg_init_table
3399 * 2. skb_to_sgvec(payload1)
3400 * 3. sg_unmark_end
3401 * 4. skb_to_sgvec(payload2)
3402 *
3403 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3404 * is more preferable.
3405 */
3406 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3407 int offset, int len)
3408 {
3409 return __skb_to_sgvec(skb, sg, offset, len);
3410 }
3411 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3412
3413 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3414 {
3415 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3416
3417 sg_mark_end(&sg[nsg - 1]);
3418
3419 return nsg;
3420 }
3421 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3422
3423 /**
3424 * skb_cow_data - Check that a socket buffer's data buffers are writable
3425 * @skb: The socket buffer to check.
3426 * @tailbits: Amount of trailing space to be added
3427 * @trailer: Returned pointer to the skb where the @tailbits space begins
3428 *
3429 * Make sure that the data buffers attached to a socket buffer are
3430 * writable. If they are not, private copies are made of the data buffers
3431 * and the socket buffer is set to use these instead.
3432 *
3433 * If @tailbits is given, make sure that there is space to write @tailbits
3434 * bytes of data beyond current end of socket buffer. @trailer will be
3435 * set to point to the skb in which this space begins.
3436 *
3437 * The number of scatterlist elements required to completely map the
3438 * COW'd and extended socket buffer will be returned.
3439 */
3440 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3441 {
3442 int copyflag;
3443 int elt;
3444 struct sk_buff *skb1, **skb_p;
3445
3446 /* If skb is cloned or its head is paged, reallocate
3447 * head pulling out all the pages (pages are considered not writable
3448 * at the moment even if they are anonymous).
3449 */
3450 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3451 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3452 return -ENOMEM;
3453
3454 /* Easy case. Most of packets will go this way. */
3455 if (!skb_has_frag_list(skb)) {
3456 /* A little of trouble, not enough of space for trailer.
3457 * This should not happen, when stack is tuned to generate
3458 * good frames. OK, on miss we reallocate and reserve even more
3459 * space, 128 bytes is fair. */
3460
3461 if (skb_tailroom(skb) < tailbits &&
3462 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3463 return -ENOMEM;
3464
3465 /* Voila! */
3466 *trailer = skb;
3467 return 1;
3468 }
3469
3470 /* Misery. We are in troubles, going to mincer fragments... */
3471
3472 elt = 1;
3473 skb_p = &skb_shinfo(skb)->frag_list;
3474 copyflag = 0;
3475
3476 while ((skb1 = *skb_p) != NULL) {
3477 int ntail = 0;
3478
3479 /* The fragment is partially pulled by someone,
3480 * this can happen on input. Copy it and everything
3481 * after it. */
3482
3483 if (skb_shared(skb1))
3484 copyflag = 1;
3485
3486 /* If the skb is the last, worry about trailer. */
3487
3488 if (skb1->next == NULL && tailbits) {
3489 if (skb_shinfo(skb1)->nr_frags ||
3490 skb_has_frag_list(skb1) ||
3491 skb_tailroom(skb1) < tailbits)
3492 ntail = tailbits + 128;
3493 }
3494
3495 if (copyflag ||
3496 skb_cloned(skb1) ||
3497 ntail ||
3498 skb_shinfo(skb1)->nr_frags ||
3499 skb_has_frag_list(skb1)) {
3500 struct sk_buff *skb2;
3501
3502 /* Fuck, we are miserable poor guys... */
3503 if (ntail == 0)
3504 skb2 = skb_copy(skb1, GFP_ATOMIC);
3505 else
3506 skb2 = skb_copy_expand(skb1,
3507 skb_headroom(skb1),
3508 ntail,
3509 GFP_ATOMIC);
3510 if (unlikely(skb2 == NULL))
3511 return -ENOMEM;
3512
3513 if (skb1->sk)
3514 skb_set_owner_w(skb2, skb1->sk);
3515
3516 /* Looking around. Are we still alive?
3517 * OK, link new skb, drop old one */
3518
3519 skb2->next = skb1->next;
3520 *skb_p = skb2;
3521 kfree_skb(skb1);
3522 skb1 = skb2;
3523 }
3524 elt++;
3525 *trailer = skb1;
3526 skb_p = &skb1->next;
3527 }
3528
3529 return elt;
3530 }
3531 EXPORT_SYMBOL_GPL(skb_cow_data);
3532
3533 static void sock_rmem_free(struct sk_buff *skb)
3534 {
3535 struct sock *sk = skb->sk;
3536
3537 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3538 }
3539
3540 /*
3541 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3542 */
3543 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3544 {
3545 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3546 (unsigned int)sk->sk_rcvbuf)
3547 return -ENOMEM;
3548
3549 skb_orphan(skb);
3550 skb->sk = sk;
3551 skb->destructor = sock_rmem_free;
3552 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3553
3554 /* before exiting rcu section, make sure dst is refcounted */
3555 skb_dst_force(skb);
3556
3557 skb_queue_tail(&sk->sk_error_queue, skb);
3558 if (!sock_flag(sk, SOCK_DEAD))
3559 sk->sk_data_ready(sk);
3560 return 0;
3561 }
3562 EXPORT_SYMBOL(sock_queue_err_skb);
3563
3564 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3565 {
3566 struct sk_buff_head *q = &sk->sk_error_queue;
3567 struct sk_buff *skb, *skb_next;
3568 unsigned long flags;
3569 int err = 0;
3570
3571 spin_lock_irqsave(&q->lock, flags);
3572 skb = __skb_dequeue(q);
3573 if (skb && (skb_next = skb_peek(q)))
3574 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3575 spin_unlock_irqrestore(&q->lock, flags);
3576
3577 sk->sk_err = err;
3578 if (err)
3579 sk->sk_error_report(sk);
3580
3581 return skb;
3582 }
3583 EXPORT_SYMBOL(sock_dequeue_err_skb);
3584
3585 /**
3586 * skb_clone_sk - create clone of skb, and take reference to socket
3587 * @skb: the skb to clone
3588 *
3589 * This function creates a clone of a buffer that holds a reference on
3590 * sk_refcnt. Buffers created via this function are meant to be
3591 * returned using sock_queue_err_skb, or free via kfree_skb.
3592 *
3593 * When passing buffers allocated with this function to sock_queue_err_skb
3594 * it is necessary to wrap the call with sock_hold/sock_put in order to
3595 * prevent the socket from being released prior to being enqueued on
3596 * the sk_error_queue.
3597 */
3598 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3599 {
3600 struct sock *sk = skb->sk;
3601 struct sk_buff *clone;
3602
3603 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3604 return NULL;
3605
3606 clone = skb_clone(skb, GFP_ATOMIC);
3607 if (!clone) {
3608 sock_put(sk);
3609 return NULL;
3610 }
3611
3612 clone->sk = sk;
3613 clone->destructor = sock_efree;
3614
3615 return clone;
3616 }
3617 EXPORT_SYMBOL(skb_clone_sk);
3618
3619 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3620 struct sock *sk,
3621 int tstype)
3622 {
3623 struct sock_exterr_skb *serr;
3624 int err;
3625
3626 serr = SKB_EXT_ERR(skb);
3627 memset(serr, 0, sizeof(*serr));
3628 serr->ee.ee_errno = ENOMSG;
3629 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3630 serr->ee.ee_info = tstype;
3631 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3632 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3633 if (sk->sk_protocol == IPPROTO_TCP)
3634 serr->ee.ee_data -= sk->sk_tskey;
3635 }
3636
3637 err = sock_queue_err_skb(sk, skb);
3638
3639 if (err)
3640 kfree_skb(skb);
3641 }
3642
3643 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3644 {
3645 bool ret;
3646
3647 if (likely(sysctl_tstamp_allow_data || tsonly))
3648 return true;
3649
3650 read_lock_bh(&sk->sk_callback_lock);
3651 ret = sk->sk_socket && sk->sk_socket->file &&
3652 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3653 read_unlock_bh(&sk->sk_callback_lock);
3654 return ret;
3655 }
3656
3657 void skb_complete_tx_timestamp(struct sk_buff *skb,
3658 struct skb_shared_hwtstamps *hwtstamps)
3659 {
3660 struct sock *sk = skb->sk;
3661
3662 if (!skb_may_tx_timestamp(sk, false))
3663 return;
3664
3665 /* take a reference to prevent skb_orphan() from freeing the socket */
3666 sock_hold(sk);
3667
3668 *skb_hwtstamps(skb) = *hwtstamps;
3669 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3670
3671 sock_put(sk);
3672 }
3673 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3674
3675 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3676 struct skb_shared_hwtstamps *hwtstamps,
3677 struct sock *sk, int tstype)
3678 {
3679 struct sk_buff *skb;
3680 bool tsonly;
3681
3682 if (!sk)
3683 return;
3684
3685 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3686 if (!skb_may_tx_timestamp(sk, tsonly))
3687 return;
3688
3689 if (tsonly)
3690 skb = alloc_skb(0, GFP_ATOMIC);
3691 else
3692 skb = skb_clone(orig_skb, GFP_ATOMIC);
3693 if (!skb)
3694 return;
3695
3696 if (tsonly) {
3697 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3698 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3699 }
3700
3701 if (hwtstamps)
3702 *skb_hwtstamps(skb) = *hwtstamps;
3703 else
3704 skb->tstamp = ktime_get_real();
3705
3706 __skb_complete_tx_timestamp(skb, sk, tstype);
3707 }
3708 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3709
3710 void skb_tstamp_tx(struct sk_buff *orig_skb,
3711 struct skb_shared_hwtstamps *hwtstamps)
3712 {
3713 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3714 SCM_TSTAMP_SND);
3715 }
3716 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3717
3718 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3719 {
3720 struct sock *sk = skb->sk;
3721 struct sock_exterr_skb *serr;
3722 int err;
3723
3724 skb->wifi_acked_valid = 1;
3725 skb->wifi_acked = acked;
3726
3727 serr = SKB_EXT_ERR(skb);
3728 memset(serr, 0, sizeof(*serr));
3729 serr->ee.ee_errno = ENOMSG;
3730 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3731
3732 /* take a reference to prevent skb_orphan() from freeing the socket */
3733 sock_hold(sk);
3734
3735 err = sock_queue_err_skb(sk, skb);
3736 if (err)
3737 kfree_skb(skb);
3738
3739 sock_put(sk);
3740 }
3741 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3742
3743 /**
3744 * skb_partial_csum_set - set up and verify partial csum values for packet
3745 * @skb: the skb to set
3746 * @start: the number of bytes after skb->data to start checksumming.
3747 * @off: the offset from start to place the checksum.
3748 *
3749 * For untrusted partially-checksummed packets, we need to make sure the values
3750 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3751 *
3752 * This function checks and sets those values and skb->ip_summed: if this
3753 * returns false you should drop the packet.
3754 */
3755 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3756 {
3757 if (unlikely(start > skb_headlen(skb)) ||
3758 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3759 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3760 start, off, skb_headlen(skb));
3761 return false;
3762 }
3763 skb->ip_summed = CHECKSUM_PARTIAL;
3764 skb->csum_start = skb_headroom(skb) + start;
3765 skb->csum_offset = off;
3766 skb_set_transport_header(skb, start);
3767 return true;
3768 }
3769 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3770
3771 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3772 unsigned int max)
3773 {
3774 if (skb_headlen(skb) >= len)
3775 return 0;
3776
3777 /* If we need to pullup then pullup to the max, so we
3778 * won't need to do it again.
3779 */
3780 if (max > skb->len)
3781 max = skb->len;
3782
3783 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3784 return -ENOMEM;
3785
3786 if (skb_headlen(skb) < len)
3787 return -EPROTO;
3788
3789 return 0;
3790 }
3791
3792 #define MAX_TCP_HDR_LEN (15 * 4)
3793
3794 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3795 typeof(IPPROTO_IP) proto,
3796 unsigned int off)
3797 {
3798 switch (proto) {
3799 int err;
3800
3801 case IPPROTO_TCP:
3802 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3803 off + MAX_TCP_HDR_LEN);
3804 if (!err && !skb_partial_csum_set(skb, off,
3805 offsetof(struct tcphdr,
3806 check)))
3807 err = -EPROTO;
3808 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3809
3810 case IPPROTO_UDP:
3811 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3812 off + sizeof(struct udphdr));
3813 if (!err && !skb_partial_csum_set(skb, off,
3814 offsetof(struct udphdr,
3815 check)))
3816 err = -EPROTO;
3817 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3818 }
3819
3820 return ERR_PTR(-EPROTO);
3821 }
3822
3823 /* This value should be large enough to cover a tagged ethernet header plus
3824 * maximally sized IP and TCP or UDP headers.
3825 */
3826 #define MAX_IP_HDR_LEN 128
3827
3828 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3829 {
3830 unsigned int off;
3831 bool fragment;
3832 __sum16 *csum;
3833 int err;
3834
3835 fragment = false;
3836
3837 err = skb_maybe_pull_tail(skb,
3838 sizeof(struct iphdr),
3839 MAX_IP_HDR_LEN);
3840 if (err < 0)
3841 goto out;
3842
3843 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3844 fragment = true;
3845
3846 off = ip_hdrlen(skb);
3847
3848 err = -EPROTO;
3849
3850 if (fragment)
3851 goto out;
3852
3853 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3854 if (IS_ERR(csum))
3855 return PTR_ERR(csum);
3856
3857 if (recalculate)
3858 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3859 ip_hdr(skb)->daddr,
3860 skb->len - off,
3861 ip_hdr(skb)->protocol, 0);
3862 err = 0;
3863
3864 out:
3865 return err;
3866 }
3867
3868 /* This value should be large enough to cover a tagged ethernet header plus
3869 * an IPv6 header, all options, and a maximal TCP or UDP header.
3870 */
3871 #define MAX_IPV6_HDR_LEN 256
3872
3873 #define OPT_HDR(type, skb, off) \
3874 (type *)(skb_network_header(skb) + (off))
3875
3876 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3877 {
3878 int err;
3879 u8 nexthdr;
3880 unsigned int off;
3881 unsigned int len;
3882 bool fragment;
3883 bool done;
3884 __sum16 *csum;
3885
3886 fragment = false;
3887 done = false;
3888
3889 off = sizeof(struct ipv6hdr);
3890
3891 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3892 if (err < 0)
3893 goto out;
3894
3895 nexthdr = ipv6_hdr(skb)->nexthdr;
3896
3897 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3898 while (off <= len && !done) {
3899 switch (nexthdr) {
3900 case IPPROTO_DSTOPTS:
3901 case IPPROTO_HOPOPTS:
3902 case IPPROTO_ROUTING: {
3903 struct ipv6_opt_hdr *hp;
3904
3905 err = skb_maybe_pull_tail(skb,
3906 off +
3907 sizeof(struct ipv6_opt_hdr),
3908 MAX_IPV6_HDR_LEN);
3909 if (err < 0)
3910 goto out;
3911
3912 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3913 nexthdr = hp->nexthdr;
3914 off += ipv6_optlen(hp);
3915 break;
3916 }
3917 case IPPROTO_AH: {
3918 struct ip_auth_hdr *hp;
3919
3920 err = skb_maybe_pull_tail(skb,
3921 off +
3922 sizeof(struct ip_auth_hdr),
3923 MAX_IPV6_HDR_LEN);
3924 if (err < 0)
3925 goto out;
3926
3927 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3928 nexthdr = hp->nexthdr;
3929 off += ipv6_authlen(hp);
3930 break;
3931 }
3932 case IPPROTO_FRAGMENT: {
3933 struct frag_hdr *hp;
3934
3935 err = skb_maybe_pull_tail(skb,
3936 off +
3937 sizeof(struct frag_hdr),
3938 MAX_IPV6_HDR_LEN);
3939 if (err < 0)
3940 goto out;
3941
3942 hp = OPT_HDR(struct frag_hdr, skb, off);
3943
3944 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3945 fragment = true;
3946
3947 nexthdr = hp->nexthdr;
3948 off += sizeof(struct frag_hdr);
3949 break;
3950 }
3951 default:
3952 done = true;
3953 break;
3954 }
3955 }
3956
3957 err = -EPROTO;
3958
3959 if (!done || fragment)
3960 goto out;
3961
3962 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3963 if (IS_ERR(csum))
3964 return PTR_ERR(csum);
3965
3966 if (recalculate)
3967 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3968 &ipv6_hdr(skb)->daddr,
3969 skb->len - off, nexthdr, 0);
3970 err = 0;
3971
3972 out:
3973 return err;
3974 }
3975
3976 /**
3977 * skb_checksum_setup - set up partial checksum offset
3978 * @skb: the skb to set up
3979 * @recalculate: if true the pseudo-header checksum will be recalculated
3980 */
3981 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3982 {
3983 int err;
3984
3985 switch (skb->protocol) {
3986 case htons(ETH_P_IP):
3987 err = skb_checksum_setup_ipv4(skb, recalculate);
3988 break;
3989
3990 case htons(ETH_P_IPV6):
3991 err = skb_checksum_setup_ipv6(skb, recalculate);
3992 break;
3993
3994 default:
3995 err = -EPROTO;
3996 break;
3997 }
3998
3999 return err;
4000 }
4001 EXPORT_SYMBOL(skb_checksum_setup);
4002
4003 /**
4004 * skb_checksum_maybe_trim - maybe trims the given skb
4005 * @skb: the skb to check
4006 * @transport_len: the data length beyond the network header
4007 *
4008 * Checks whether the given skb has data beyond the given transport length.
4009 * If so, returns a cloned skb trimmed to this transport length.
4010 * Otherwise returns the provided skb. Returns NULL in error cases
4011 * (e.g. transport_len exceeds skb length or out-of-memory).
4012 *
4013 * Caller needs to set the skb transport header and release the returned skb.
4014 * Provided skb is consumed.
4015 */
4016 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4017 unsigned int transport_len)
4018 {
4019 struct sk_buff *skb_chk;
4020 unsigned int len = skb_transport_offset(skb) + transport_len;
4021 int ret;
4022
4023 if (skb->len < len) {
4024 kfree_skb(skb);
4025 return NULL;
4026 } else if (skb->len == len) {
4027 return skb;
4028 }
4029
4030 skb_chk = skb_clone(skb, GFP_ATOMIC);
4031 kfree_skb(skb);
4032
4033 if (!skb_chk)
4034 return NULL;
4035
4036 ret = pskb_trim_rcsum(skb_chk, len);
4037 if (ret) {
4038 kfree_skb(skb_chk);
4039 return NULL;
4040 }
4041
4042 return skb_chk;
4043 }
4044
4045 /**
4046 * skb_checksum_trimmed - validate checksum of an skb
4047 * @skb: the skb to check
4048 * @transport_len: the data length beyond the network header
4049 * @skb_chkf: checksum function to use
4050 *
4051 * Applies the given checksum function skb_chkf to the provided skb.
4052 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4053 *
4054 * If the skb has data beyond the given transport length, then a
4055 * trimmed & cloned skb is checked and returned.
4056 *
4057 * Caller needs to set the skb transport header and release the returned skb.
4058 * Provided skb is consumed.
4059 */
4060 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4061 unsigned int transport_len,
4062 __sum16(*skb_chkf)(struct sk_buff *skb))
4063 {
4064 struct sk_buff *skb_chk;
4065 unsigned int offset = skb_transport_offset(skb);
4066 __sum16 ret;
4067
4068 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4069 if (!skb_chk)
4070 return NULL;
4071
4072 if (!pskb_may_pull(skb_chk, offset)) {
4073 kfree_skb(skb_chk);
4074 return NULL;
4075 }
4076
4077 __skb_pull(skb_chk, offset);
4078 ret = skb_chkf(skb_chk);
4079 __skb_push(skb_chk, offset);
4080
4081 if (ret) {
4082 kfree_skb(skb_chk);
4083 return NULL;
4084 }
4085
4086 return skb_chk;
4087 }
4088 EXPORT_SYMBOL(skb_checksum_trimmed);
4089
4090 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4091 {
4092 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4093 skb->dev->name);
4094 }
4095 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4096
4097 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4098 {
4099 if (head_stolen) {
4100 skb_release_head_state(skb);
4101 kmem_cache_free(skbuff_head_cache, skb);
4102 } else {
4103 __kfree_skb(skb);
4104 }
4105 }
4106 EXPORT_SYMBOL(kfree_skb_partial);
4107
4108 /**
4109 * skb_try_coalesce - try to merge skb to prior one
4110 * @to: prior buffer
4111 * @from: buffer to add
4112 * @fragstolen: pointer to boolean
4113 * @delta_truesize: how much more was allocated than was requested
4114 */
4115 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4116 bool *fragstolen, int *delta_truesize)
4117 {
4118 int i, delta, len = from->len;
4119
4120 *fragstolen = false;
4121
4122 if (skb_cloned(to))
4123 return false;
4124
4125 if (len <= skb_tailroom(to)) {
4126 if (len)
4127 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4128 *delta_truesize = 0;
4129 return true;
4130 }
4131
4132 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4133 return false;
4134
4135 if (skb_headlen(from) != 0) {
4136 struct page *page;
4137 unsigned int offset;
4138
4139 if (skb_shinfo(to)->nr_frags +
4140 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4141 return false;
4142
4143 if (skb_head_is_locked(from))
4144 return false;
4145
4146 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4147
4148 page = virt_to_head_page(from->head);
4149 offset = from->data - (unsigned char *)page_address(page);
4150
4151 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4152 page, offset, skb_headlen(from));
4153 *fragstolen = true;
4154 } else {
4155 if (skb_shinfo(to)->nr_frags +
4156 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4157 return false;
4158
4159 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4160 }
4161
4162 WARN_ON_ONCE(delta < len);
4163
4164 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4165 skb_shinfo(from)->frags,
4166 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4167 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4168
4169 if (!skb_cloned(from))
4170 skb_shinfo(from)->nr_frags = 0;
4171
4172 /* if the skb is not cloned this does nothing
4173 * since we set nr_frags to 0.
4174 */
4175 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4176 skb_frag_ref(from, i);
4177
4178 to->truesize += delta;
4179 to->len += len;
4180 to->data_len += len;
4181
4182 *delta_truesize = delta;
4183 return true;
4184 }
4185 EXPORT_SYMBOL(skb_try_coalesce);
4186
4187 /**
4188 * skb_scrub_packet - scrub an skb
4189 *
4190 * @skb: buffer to clean
4191 * @xnet: packet is crossing netns
4192 *
4193 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4194 * into/from a tunnel. Some information have to be cleared during these
4195 * operations.
4196 * skb_scrub_packet can also be used to clean a skb before injecting it in
4197 * another namespace (@xnet == true). We have to clear all information in the
4198 * skb that could impact namespace isolation.
4199 */
4200 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4201 {
4202 skb->tstamp.tv64 = 0;
4203 skb->pkt_type = PACKET_HOST;
4204 skb->skb_iif = 0;
4205 skb->ignore_df = 0;
4206 skb_dst_drop(skb);
4207 skb_sender_cpu_clear(skb);
4208 secpath_reset(skb);
4209 nf_reset(skb);
4210 nf_reset_trace(skb);
4211
4212 if (!xnet)
4213 return;
4214
4215 skb_orphan(skb);
4216 skb->mark = 0;
4217 }
4218 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4219
4220 /**
4221 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4222 *
4223 * @skb: GSO skb
4224 *
4225 * skb_gso_transport_seglen is used to determine the real size of the
4226 * individual segments, including Layer4 headers (TCP/UDP).
4227 *
4228 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4229 */
4230 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4231 {
4232 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4233 unsigned int thlen = 0;
4234
4235 if (skb->encapsulation) {
4236 thlen = skb_inner_transport_header(skb) -
4237 skb_transport_header(skb);
4238
4239 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4240 thlen += inner_tcp_hdrlen(skb);
4241 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4242 thlen = tcp_hdrlen(skb);
4243 }
4244 /* UFO sets gso_size to the size of the fragmentation
4245 * payload, i.e. the size of the L4 (UDP) header is already
4246 * accounted for.
4247 */
4248 return thlen + shinfo->gso_size;
4249 }
4250 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4251
4252 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4253 {
4254 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4255 kfree_skb(skb);
4256 return NULL;
4257 }
4258
4259 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4260 skb->mac_header += VLAN_HLEN;
4261 return skb;
4262 }
4263
4264 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4265 {
4266 struct vlan_hdr *vhdr;
4267 u16 vlan_tci;
4268
4269 if (unlikely(skb_vlan_tag_present(skb))) {
4270 /* vlan_tci is already set-up so leave this for another time */
4271 return skb;
4272 }
4273
4274 skb = skb_share_check(skb, GFP_ATOMIC);
4275 if (unlikely(!skb))
4276 goto err_free;
4277
4278 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4279 goto err_free;
4280
4281 vhdr = (struct vlan_hdr *)skb->data;
4282 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4283 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4284
4285 skb_pull_rcsum(skb, VLAN_HLEN);
4286 vlan_set_encap_proto(skb, vhdr);
4287
4288 skb = skb_reorder_vlan_header(skb);
4289 if (unlikely(!skb))
4290 goto err_free;
4291
4292 skb_reset_network_header(skb);
4293 skb_reset_transport_header(skb);
4294 skb_reset_mac_len(skb);
4295
4296 return skb;
4297
4298 err_free:
4299 kfree_skb(skb);
4300 return NULL;
4301 }
4302 EXPORT_SYMBOL(skb_vlan_untag);
4303
4304 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4305 {
4306 if (!pskb_may_pull(skb, write_len))
4307 return -ENOMEM;
4308
4309 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4310 return 0;
4311
4312 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4313 }
4314 EXPORT_SYMBOL(skb_ensure_writable);
4315
4316 /* remove VLAN header from packet and update csum accordingly. */
4317 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4318 {
4319 struct vlan_hdr *vhdr;
4320 unsigned int offset = skb->data - skb_mac_header(skb);
4321 int err;
4322
4323 __skb_push(skb, offset);
4324 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4325 if (unlikely(err))
4326 goto pull;
4327
4328 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4329
4330 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4331 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4332
4333 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4334 __skb_pull(skb, VLAN_HLEN);
4335
4336 vlan_set_encap_proto(skb, vhdr);
4337 skb->mac_header += VLAN_HLEN;
4338
4339 if (skb_network_offset(skb) < ETH_HLEN)
4340 skb_set_network_header(skb, ETH_HLEN);
4341
4342 skb_reset_mac_len(skb);
4343 pull:
4344 __skb_pull(skb, offset);
4345
4346 return err;
4347 }
4348
4349 int skb_vlan_pop(struct sk_buff *skb)
4350 {
4351 u16 vlan_tci;
4352 __be16 vlan_proto;
4353 int err;
4354
4355 if (likely(skb_vlan_tag_present(skb))) {
4356 skb->vlan_tci = 0;
4357 } else {
4358 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4359 skb->protocol != htons(ETH_P_8021AD)) ||
4360 skb->len < VLAN_ETH_HLEN))
4361 return 0;
4362
4363 err = __skb_vlan_pop(skb, &vlan_tci);
4364 if (err)
4365 return err;
4366 }
4367 /* move next vlan tag to hw accel tag */
4368 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4369 skb->protocol != htons(ETH_P_8021AD)) ||
4370 skb->len < VLAN_ETH_HLEN))
4371 return 0;
4372
4373 vlan_proto = skb->protocol;
4374 err = __skb_vlan_pop(skb, &vlan_tci);
4375 if (unlikely(err))
4376 return err;
4377
4378 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4379 return 0;
4380 }
4381 EXPORT_SYMBOL(skb_vlan_pop);
4382
4383 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4384 {
4385 if (skb_vlan_tag_present(skb)) {
4386 unsigned int offset = skb->data - skb_mac_header(skb);
4387 int err;
4388
4389 /* __vlan_insert_tag expect skb->data pointing to mac header.
4390 * So change skb->data before calling it and change back to
4391 * original position later
4392 */
4393 __skb_push(skb, offset);
4394 err = __vlan_insert_tag(skb, skb->vlan_proto,
4395 skb_vlan_tag_get(skb));
4396 if (err)
4397 return err;
4398 skb->protocol = skb->vlan_proto;
4399 skb->mac_len += VLAN_HLEN;
4400 __skb_pull(skb, offset);
4401
4402 if (skb->ip_summed == CHECKSUM_COMPLETE)
4403 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4404 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4405 }
4406 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4407 return 0;
4408 }
4409 EXPORT_SYMBOL(skb_vlan_push);
4410
4411 /**
4412 * alloc_skb_with_frags - allocate skb with page frags
4413 *
4414 * @header_len: size of linear part
4415 * @data_len: needed length in frags
4416 * @max_page_order: max page order desired.
4417 * @errcode: pointer to error code if any
4418 * @gfp_mask: allocation mask
4419 *
4420 * This can be used to allocate a paged skb, given a maximal order for frags.
4421 */
4422 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4423 unsigned long data_len,
4424 int max_page_order,
4425 int *errcode,
4426 gfp_t gfp_mask)
4427 {
4428 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4429 unsigned long chunk;
4430 struct sk_buff *skb;
4431 struct page *page;
4432 gfp_t gfp_head;
4433 int i;
4434
4435 *errcode = -EMSGSIZE;
4436 /* Note this test could be relaxed, if we succeed to allocate
4437 * high order pages...
4438 */
4439 if (npages > MAX_SKB_FRAGS)
4440 return NULL;
4441
4442 gfp_head = gfp_mask;
4443 if (gfp_head & __GFP_WAIT)
4444 gfp_head |= __GFP_REPEAT;
4445
4446 *errcode = -ENOBUFS;
4447 skb = alloc_skb(header_len, gfp_head);
4448 if (!skb)
4449 return NULL;
4450
4451 skb->truesize += npages << PAGE_SHIFT;
4452
4453 for (i = 0; npages > 0; i++) {
4454 int order = max_page_order;
4455
4456 while (order) {
4457 if (npages >= 1 << order) {
4458 page = alloc_pages(gfp_mask |
4459 __GFP_COMP |
4460 __GFP_NOWARN |
4461 __GFP_NORETRY,
4462 order);
4463 if (page)
4464 goto fill_page;
4465 /* Do not retry other high order allocations */
4466 order = 1;
4467 max_page_order = 0;
4468 }
4469 order--;
4470 }
4471 page = alloc_page(gfp_mask);
4472 if (!page)
4473 goto failure;
4474 fill_page:
4475 chunk = min_t(unsigned long, data_len,
4476 PAGE_SIZE << order);
4477 skb_fill_page_desc(skb, i, page, 0, chunk);
4478 data_len -= chunk;
4479 npages -= 1 << order;
4480 }
4481 return skb;
4482
4483 failure:
4484 kfree_skb(skb);
4485 return NULL;
4486 }
4487 EXPORT_SYMBOL(alloc_skb_with_frags);