rtnetlink: fix userspace API breakage for iproute2 < v3.9.0
[GitHub/mt8127/android_kernel_alcatel_ttab.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
66 #include <net/protocol.h>
67 #include <net/dst.h>
68 #include <net/sock.h>
69 #include <net/checksum.h>
70 #include <net/xfrm.h>
71
72 #include <asm/uaccess.h>
73 #include <trace/events/skb.h>
74 #include <linux/highmem.h>
75
76 struct kmem_cache *skbuff_head_cache __read_mostly;
77 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
78
79 /**
80 * skb_panic - private function for out-of-line support
81 * @skb: buffer
82 * @sz: size
83 * @addr: address
84 * @msg: skb_over_panic or skb_under_panic
85 *
86 * Out-of-line support for skb_put() and skb_push().
87 * Called via the wrapper skb_over_panic() or skb_under_panic().
88 * Keep out of line to prevent kernel bloat.
89 * __builtin_return_address is not used because it is not always reliable.
90 */
91 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
92 const char msg[])
93 {
94 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
95 msg, addr, skb->len, sz, skb->head, skb->data,
96 (unsigned long)skb->tail, (unsigned long)skb->end,
97 skb->dev ? skb->dev->name : "<NULL>");
98 BUG();
99 }
100
101 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
102 {
103 skb_panic(skb, sz, addr, __func__);
104 }
105
106 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
107 {
108 skb_panic(skb, sz, addr, __func__);
109 }
110
111 /*
112 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
113 * the caller if emergency pfmemalloc reserves are being used. If it is and
114 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
115 * may be used. Otherwise, the packet data may be discarded until enough
116 * memory is free
117 */
118 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
119 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
120
121 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
122 unsigned long ip, bool *pfmemalloc)
123 {
124 void *obj;
125 bool ret_pfmemalloc = false;
126
127 /*
128 * Try a regular allocation, when that fails and we're not entitled
129 * to the reserves, fail.
130 */
131 obj = kmalloc_node_track_caller(size,
132 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
133 node);
134 if (obj || !(gfp_pfmemalloc_allowed(flags)))
135 goto out;
136
137 /* Try again but now we are using pfmemalloc reserves */
138 ret_pfmemalloc = true;
139 obj = kmalloc_node_track_caller(size, flags, node);
140
141 out:
142 if (pfmemalloc)
143 *pfmemalloc = ret_pfmemalloc;
144
145 return obj;
146 }
147
148 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
149 * 'private' fields and also do memory statistics to find all the
150 * [BEEP] leaks.
151 *
152 */
153
154 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
155 {
156 struct sk_buff *skb;
157
158 /* Get the HEAD */
159 skb = kmem_cache_alloc_node(skbuff_head_cache,
160 gfp_mask & ~__GFP_DMA, node);
161 if (!skb)
162 goto out;
163
164 /*
165 * Only clear those fields we need to clear, not those that we will
166 * actually initialise below. Hence, don't put any more fields after
167 * the tail pointer in struct sk_buff!
168 */
169 memset(skb, 0, offsetof(struct sk_buff, tail));
170 skb->head = NULL;
171 skb->truesize = sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
173
174 #ifdef NET_SKBUFF_DATA_USES_OFFSET
175 skb->mac_header = ~0U;
176 #endif
177 out:
178 return skb;
179 }
180
181 /**
182 * __alloc_skb - allocate a network buffer
183 * @size: size to allocate
184 * @gfp_mask: allocation mask
185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
189 * @node: numa node to allocate memory on
190 *
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
194 *
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
197 */
198 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
199 int flags, int node)
200 {
201 struct kmem_cache *cache;
202 struct skb_shared_info *shinfo;
203 struct sk_buff *skb;
204 u8 *data;
205 bool pfmemalloc;
206
207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
209
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
212
213 /* Get the HEAD */
214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
215 if (!skb)
216 goto out;
217 prefetchw(skb);
218
219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
223 */
224 size = SKB_DATA_ALIGN(size);
225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
227 if (!data)
228 goto nodata;
229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
232 */
233 size = SKB_WITH_OVERHEAD(ksize(data));
234 prefetchw(data + size);
235
236 /*
237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
240 */
241 memset(skb, 0, offsetof(struct sk_buff, tail));
242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
244 skb->pfmemalloc = pfmemalloc;
245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
248 skb_reset_tail_pointer(skb);
249 skb->end = skb->tail + size;
250 #ifdef NET_SKBUFF_DATA_USES_OFFSET
251 skb->mac_header = ~0U;
252 skb->transport_header = ~0U;
253 #endif
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 *child = skb + 1;
263 atomic_t *fclone_ref = (atomic_t *) (child + 1);
264
265 kmemcheck_annotate_bitfield(child, flags1);
266 kmemcheck_annotate_bitfield(child, flags2);
267 skb->fclone = SKB_FCLONE_ORIG;
268 atomic_set(fclone_ref, 1);
269
270 child->fclone = SKB_FCLONE_UNAVAILABLE;
271 child->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 fragment, 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()
289 * The return is the new skb buffer.
290 * On a failure the return is %NULL, and @data is not freed.
291 * Notes :
292 * Before IO, driver allocates only data buffer where NIC put incoming frame
293 * Driver should add room at head (NET_SKB_PAD) and
294 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
295 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
296 * before giving packet to stack.
297 * RX rings only contains data buffers, not full skbs.
298 */
299 struct sk_buff *build_skb(void *data, unsigned int frag_size)
300 {
301 struct skb_shared_info *shinfo;
302 struct sk_buff *skb;
303 unsigned int size = frag_size ? : ksize(data);
304
305 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
306 if (!skb)
307 return NULL;
308
309 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
310
311 memset(skb, 0, offsetof(struct sk_buff, tail));
312 skb->truesize = SKB_TRUESIZE(size);
313 skb->head_frag = frag_size != 0;
314 atomic_set(&skb->users, 1);
315 skb->head = data;
316 skb->data = data;
317 skb_reset_tail_pointer(skb);
318 skb->end = skb->tail + size;
319 #ifdef NET_SKBUFF_DATA_USES_OFFSET
320 skb->mac_header = ~0U;
321 skb->transport_header = ~0U;
322 #endif
323
324 /* make sure we initialize shinfo sequentially */
325 shinfo = skb_shinfo(skb);
326 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
327 atomic_set(&shinfo->dataref, 1);
328 kmemcheck_annotate_variable(shinfo->destructor_arg);
329
330 return skb;
331 }
332 EXPORT_SYMBOL(build_skb);
333
334 struct netdev_alloc_cache {
335 struct page_frag frag;
336 /* we maintain a pagecount bias, so that we dont dirty cache line
337 * containing page->_count every time we allocate a fragment.
338 */
339 unsigned int pagecnt_bias;
340 };
341 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
342
343 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
344 {
345 struct netdev_alloc_cache *nc;
346 void *data = NULL;
347 int order;
348 unsigned long flags;
349
350 local_irq_save(flags);
351 nc = &__get_cpu_var(netdev_alloc_cache);
352 if (unlikely(!nc->frag.page)) {
353 refill:
354 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
355 gfp_t gfp = gfp_mask;
356
357 if (order)
358 gfp |= __GFP_COMP | __GFP_NOWARN;
359 nc->frag.page = alloc_pages(gfp, order);
360 if (likely(nc->frag.page))
361 break;
362 if (--order < 0)
363 goto end;
364 }
365 nc->frag.size = PAGE_SIZE << order;
366 recycle:
367 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
369 nc->frag.offset = 0;
370 }
371
372 if (nc->frag.offset + fragsz > nc->frag.size) {
373 /* avoid unnecessary locked operations if possible */
374 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
375 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
376 goto recycle;
377 goto refill;
378 }
379
380 data = page_address(nc->frag.page) + nc->frag.offset;
381 nc->frag.offset += fragsz;
382 nc->pagecnt_bias--;
383 end:
384 local_irq_restore(flags);
385 return data;
386 }
387
388 /**
389 * netdev_alloc_frag - allocate a page fragment
390 * @fragsz: fragment size
391 *
392 * Allocates a frag from a page for receive buffer.
393 * Uses GFP_ATOMIC allocations.
394 */
395 void *netdev_alloc_frag(unsigned int fragsz)
396 {
397 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
398 }
399 EXPORT_SYMBOL(netdev_alloc_frag);
400
401 /**
402 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
403 * @dev: network device to receive on
404 * @length: length to allocate
405 * @gfp_mask: get_free_pages mask, passed to alloc_skb
406 *
407 * Allocate a new &sk_buff and assign it a usage count of one. The
408 * buffer has unspecified headroom built in. Users should allocate
409 * the headroom they think they need without accounting for the
410 * built in space. The built in space is used for optimisations.
411 *
412 * %NULL is returned if there is no free memory.
413 */
414 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
415 unsigned int length, gfp_t gfp_mask)
416 {
417 struct sk_buff *skb = NULL;
418 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
419 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
420
421 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
422 void *data;
423
424 if (sk_memalloc_socks())
425 gfp_mask |= __GFP_MEMALLOC;
426
427 data = __netdev_alloc_frag(fragsz, gfp_mask);
428
429 if (likely(data)) {
430 skb = build_skb(data, fragsz);
431 if (unlikely(!skb))
432 put_page(virt_to_head_page(data));
433 }
434 } else {
435 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
436 SKB_ALLOC_RX, NUMA_NO_NODE);
437 }
438 if (likely(skb)) {
439 skb_reserve(skb, NET_SKB_PAD);
440 skb->dev = dev;
441 }
442 return skb;
443 }
444 EXPORT_SYMBOL(__netdev_alloc_skb);
445
446 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
447 int size, unsigned int truesize)
448 {
449 skb_fill_page_desc(skb, i, page, off, size);
450 skb->len += size;
451 skb->data_len += size;
452 skb->truesize += truesize;
453 }
454 EXPORT_SYMBOL(skb_add_rx_frag);
455
456 static void skb_drop_list(struct sk_buff **listp)
457 {
458 kfree_skb_list(*listp);
459 *listp = NULL;
460 }
461
462 static inline void skb_drop_fraglist(struct sk_buff *skb)
463 {
464 skb_drop_list(&skb_shinfo(skb)->frag_list);
465 }
466
467 static void skb_clone_fraglist(struct sk_buff *skb)
468 {
469 struct sk_buff *list;
470
471 skb_walk_frags(skb, list)
472 skb_get(list);
473 }
474
475 static void skb_free_head(struct sk_buff *skb)
476 {
477 if (skb->head_frag)
478 put_page(virt_to_head_page(skb->head));
479 else
480 kfree(skb->head);
481 }
482
483 static void skb_release_data(struct sk_buff *skb)
484 {
485 if (!skb->cloned ||
486 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
487 &skb_shinfo(skb)->dataref)) {
488 if (skb_shinfo(skb)->nr_frags) {
489 int i;
490 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
491 skb_frag_unref(skb, i);
492 }
493
494 /*
495 * If skb buf is from userspace, we need to notify the caller
496 * the lower device DMA has done;
497 */
498 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
499 struct ubuf_info *uarg;
500
501 uarg = skb_shinfo(skb)->destructor_arg;
502 if (uarg->callback)
503 uarg->callback(uarg, true);
504 }
505
506 if (skb_has_frag_list(skb))
507 skb_drop_fraglist(skb);
508
509 skb_free_head(skb);
510 }
511 }
512
513 /*
514 * Free an skbuff by memory without cleaning the state.
515 */
516 static void kfree_skbmem(struct sk_buff *skb)
517 {
518 struct sk_buff *other;
519 atomic_t *fclone_ref;
520
521 switch (skb->fclone) {
522 case SKB_FCLONE_UNAVAILABLE:
523 kmem_cache_free(skbuff_head_cache, skb);
524 break;
525
526 case SKB_FCLONE_ORIG:
527 fclone_ref = (atomic_t *) (skb + 2);
528 if (atomic_dec_and_test(fclone_ref))
529 kmem_cache_free(skbuff_fclone_cache, skb);
530 break;
531
532 case SKB_FCLONE_CLONE:
533 fclone_ref = (atomic_t *) (skb + 1);
534 other = skb - 1;
535
536 /* The clone portion is available for
537 * fast-cloning again.
538 */
539 skb->fclone = SKB_FCLONE_UNAVAILABLE;
540
541 if (atomic_dec_and_test(fclone_ref))
542 kmem_cache_free(skbuff_fclone_cache, other);
543 break;
544 }
545 }
546
547 static void skb_release_head_state(struct sk_buff *skb)
548 {
549 skb_dst_drop(skb);
550 #ifdef CONFIG_XFRM
551 secpath_put(skb->sp);
552 #endif
553 if (skb->destructor) {
554 WARN_ON(in_irq());
555 skb->destructor(skb);
556 }
557 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
558 nf_conntrack_put(skb->nfct);
559 #endif
560 #ifdef CONFIG_BRIDGE_NETFILTER
561 nf_bridge_put(skb->nf_bridge);
562 #endif
563 /* XXX: IS this still necessary? - JHS */
564 #ifdef CONFIG_NET_SCHED
565 skb->tc_index = 0;
566 #ifdef CONFIG_NET_CLS_ACT
567 skb->tc_verd = 0;
568 #endif
569 #endif
570 }
571
572 /* Free everything but the sk_buff shell. */
573 static void skb_release_all(struct sk_buff *skb)
574 {
575 skb_release_head_state(skb);
576 if (likely(skb->head))
577 skb_release_data(skb);
578 }
579
580 /**
581 * __kfree_skb - private function
582 * @skb: buffer
583 *
584 * Free an sk_buff. Release anything attached to the buffer.
585 * Clean the state. This is an internal helper function. Users should
586 * always call kfree_skb
587 */
588
589 void __kfree_skb(struct sk_buff *skb)
590 {
591 skb_release_all(skb);
592 kfree_skbmem(skb);
593 }
594 EXPORT_SYMBOL(__kfree_skb);
595
596 /**
597 * kfree_skb - free an sk_buff
598 * @skb: buffer to free
599 *
600 * Drop a reference to the buffer and free it if the usage count has
601 * hit zero.
602 */
603 void kfree_skb(struct sk_buff *skb)
604 {
605 if (unlikely(!skb))
606 return;
607 if (likely(atomic_read(&skb->users) == 1))
608 smp_rmb();
609 else if (likely(!atomic_dec_and_test(&skb->users)))
610 return;
611 trace_kfree_skb(skb, __builtin_return_address(0));
612 __kfree_skb(skb);
613 }
614 EXPORT_SYMBOL(kfree_skb);
615
616 void kfree_skb_list(struct sk_buff *segs)
617 {
618 while (segs) {
619 struct sk_buff *next = segs->next;
620
621 kfree_skb(segs);
622 segs = next;
623 }
624 }
625 EXPORT_SYMBOL(kfree_skb_list);
626
627 /**
628 * skb_tx_error - report an sk_buff xmit error
629 * @skb: buffer that triggered an error
630 *
631 * Report xmit error if a device callback is tracking this skb.
632 * skb must be freed afterwards.
633 */
634 void skb_tx_error(struct sk_buff *skb)
635 {
636 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
637 struct ubuf_info *uarg;
638
639 uarg = skb_shinfo(skb)->destructor_arg;
640 if (uarg->callback)
641 uarg->callback(uarg, false);
642 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
643 }
644 }
645 EXPORT_SYMBOL(skb_tx_error);
646
647 /**
648 * consume_skb - free an skbuff
649 * @skb: buffer to free
650 *
651 * Drop a ref to the buffer and free it if the usage count has hit zero
652 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
653 * is being dropped after a failure and notes that
654 */
655 void consume_skb(struct sk_buff *skb)
656 {
657 if (unlikely(!skb))
658 return;
659 if (likely(atomic_read(&skb->users) == 1))
660 smp_rmb();
661 else if (likely(!atomic_dec_and_test(&skb->users)))
662 return;
663 trace_consume_skb(skb);
664 __kfree_skb(skb);
665 }
666 EXPORT_SYMBOL(consume_skb);
667
668 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
669 {
670 new->tstamp = old->tstamp;
671 new->dev = old->dev;
672 new->transport_header = old->transport_header;
673 new->network_header = old->network_header;
674 new->mac_header = old->mac_header;
675 new->inner_transport_header = old->inner_transport_header;
676 new->inner_network_header = old->inner_network_header;
677 new->inner_mac_header = old->inner_mac_header;
678 skb_dst_copy(new, old);
679 new->rxhash = old->rxhash;
680 new->ooo_okay = old->ooo_okay;
681 new->l4_rxhash = old->l4_rxhash;
682 new->no_fcs = old->no_fcs;
683 new->encapsulation = old->encapsulation;
684 #ifdef CONFIG_XFRM
685 new->sp = secpath_get(old->sp);
686 #endif
687 memcpy(new->cb, old->cb, sizeof(old->cb));
688 new->csum = old->csum;
689 new->local_df = old->local_df;
690 new->pkt_type = old->pkt_type;
691 new->ip_summed = old->ip_summed;
692 skb_copy_queue_mapping(new, old);
693 new->priority = old->priority;
694 #if IS_ENABLED(CONFIG_IP_VS)
695 new->ipvs_property = old->ipvs_property;
696 #endif
697 new->pfmemalloc = old->pfmemalloc;
698 new->protocol = old->protocol;
699 new->mark = old->mark;
700 new->skb_iif = old->skb_iif;
701 __nf_copy(new, old);
702 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
703 new->nf_trace = old->nf_trace;
704 #endif
705 #ifdef CONFIG_NET_SCHED
706 new->tc_index = old->tc_index;
707 #ifdef CONFIG_NET_CLS_ACT
708 new->tc_verd = old->tc_verd;
709 #endif
710 #endif
711 new->vlan_proto = old->vlan_proto;
712 new->vlan_tci = old->vlan_tci;
713
714 skb_copy_secmark(new, old);
715 }
716
717 /*
718 * You should not add any new code to this function. Add it to
719 * __copy_skb_header above instead.
720 */
721 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
722 {
723 #define C(x) n->x = skb->x
724
725 n->next = n->prev = NULL;
726 n->sk = NULL;
727 __copy_skb_header(n, skb);
728
729 C(len);
730 C(data_len);
731 C(mac_len);
732 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
733 n->cloned = 1;
734 n->nohdr = 0;
735 n->destructor = NULL;
736 C(tail);
737 C(end);
738 C(head);
739 C(head_frag);
740 C(data);
741 C(truesize);
742 atomic_set(&n->users, 1);
743
744 atomic_inc(&(skb_shinfo(skb)->dataref));
745 skb->cloned = 1;
746
747 return n;
748 #undef C
749 }
750
751 /**
752 * skb_morph - morph one skb into another
753 * @dst: the skb to receive the contents
754 * @src: the skb to supply the contents
755 *
756 * This is identical to skb_clone except that the target skb is
757 * supplied by the user.
758 *
759 * The target skb is returned upon exit.
760 */
761 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
762 {
763 skb_release_all(dst);
764 return __skb_clone(dst, src);
765 }
766 EXPORT_SYMBOL_GPL(skb_morph);
767
768 /**
769 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
770 * @skb: the skb to modify
771 * @gfp_mask: allocation priority
772 *
773 * This must be called on SKBTX_DEV_ZEROCOPY skb.
774 * It will copy all frags into kernel and drop the reference
775 * to userspace pages.
776 *
777 * If this function is called from an interrupt gfp_mask() must be
778 * %GFP_ATOMIC.
779 *
780 * Returns 0 on success or a negative error code on failure
781 * to allocate kernel memory to copy to.
782 */
783 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
784 {
785 int i;
786 int num_frags = skb_shinfo(skb)->nr_frags;
787 struct page *page, *head = NULL;
788 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
789
790 for (i = 0; i < num_frags; i++) {
791 u8 *vaddr;
792 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
793
794 page = alloc_page(gfp_mask);
795 if (!page) {
796 while (head) {
797 struct page *next = (struct page *)head->private;
798 put_page(head);
799 head = next;
800 }
801 return -ENOMEM;
802 }
803 vaddr = kmap_atomic(skb_frag_page(f));
804 memcpy(page_address(page),
805 vaddr + f->page_offset, skb_frag_size(f));
806 kunmap_atomic(vaddr);
807 page->private = (unsigned long)head;
808 head = page;
809 }
810
811 /* skb frags release userspace buffers */
812 for (i = 0; i < num_frags; i++)
813 skb_frag_unref(skb, i);
814
815 uarg->callback(uarg, false);
816
817 /* skb frags point to kernel buffers */
818 for (i = num_frags - 1; i >= 0; i--) {
819 __skb_fill_page_desc(skb, i, head, 0,
820 skb_shinfo(skb)->frags[i].size);
821 head = (struct page *)head->private;
822 }
823
824 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
825 return 0;
826 }
827 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
828
829 /**
830 * skb_clone - duplicate an sk_buff
831 * @skb: buffer to clone
832 * @gfp_mask: allocation priority
833 *
834 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
835 * copies share the same packet data but not structure. The new
836 * buffer has a reference count of 1. If the allocation fails the
837 * function returns %NULL otherwise the new buffer is returned.
838 *
839 * If this function is called from an interrupt gfp_mask() must be
840 * %GFP_ATOMIC.
841 */
842
843 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
844 {
845 struct sk_buff *n;
846
847 if (skb_orphan_frags(skb, gfp_mask))
848 return NULL;
849
850 n = skb + 1;
851 if (skb->fclone == SKB_FCLONE_ORIG &&
852 n->fclone == SKB_FCLONE_UNAVAILABLE) {
853 atomic_t *fclone_ref = (atomic_t *) (n + 1);
854 n->fclone = SKB_FCLONE_CLONE;
855 atomic_inc(fclone_ref);
856 } else {
857 if (skb_pfmemalloc(skb))
858 gfp_mask |= __GFP_MEMALLOC;
859
860 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
861 if (!n)
862 return NULL;
863
864 kmemcheck_annotate_bitfield(n, flags1);
865 kmemcheck_annotate_bitfield(n, flags2);
866 n->fclone = SKB_FCLONE_UNAVAILABLE;
867 }
868
869 return __skb_clone(n, skb);
870 }
871 EXPORT_SYMBOL(skb_clone);
872
873 static void skb_headers_offset_update(struct sk_buff *skb, int off)
874 {
875 /* {transport,network,mac}_header and tail are relative to skb->head */
876 skb->transport_header += off;
877 skb->network_header += off;
878 if (skb_mac_header_was_set(skb))
879 skb->mac_header += off;
880 skb->inner_transport_header += off;
881 skb->inner_network_header += off;
882 skb->inner_mac_header += off;
883 }
884
885 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
886 {
887 #ifndef NET_SKBUFF_DATA_USES_OFFSET
888 /*
889 * Shift between the two data areas in bytes
890 */
891 unsigned long offset = new->data - old->data;
892 #endif
893
894 __copy_skb_header(new, old);
895
896 #ifndef NET_SKBUFF_DATA_USES_OFFSET
897 skb_headers_offset_update(new, offset);
898 #endif
899 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
900 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
901 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
902 }
903
904 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
905 {
906 if (skb_pfmemalloc(skb))
907 return SKB_ALLOC_RX;
908 return 0;
909 }
910
911 /**
912 * skb_copy - create private copy of an sk_buff
913 * @skb: buffer to copy
914 * @gfp_mask: allocation priority
915 *
916 * Make a copy of both an &sk_buff and its data. This is used when the
917 * caller wishes to modify the data and needs a private copy of the
918 * data to alter. Returns %NULL on failure or the pointer to the buffer
919 * on success. The returned buffer has a reference count of 1.
920 *
921 * As by-product this function converts non-linear &sk_buff to linear
922 * one, so that &sk_buff becomes completely private and caller is allowed
923 * to modify all the data of returned buffer. This means that this
924 * function is not recommended for use in circumstances when only
925 * header is going to be modified. Use pskb_copy() instead.
926 */
927
928 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
929 {
930 int headerlen = skb_headroom(skb);
931 unsigned int size = skb_end_offset(skb) + skb->data_len;
932 struct sk_buff *n = __alloc_skb(size, gfp_mask,
933 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
934
935 if (!n)
936 return NULL;
937
938 /* Set the data pointer */
939 skb_reserve(n, headerlen);
940 /* Set the tail pointer and length */
941 skb_put(n, skb->len);
942
943 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
944 BUG();
945
946 copy_skb_header(n, skb);
947 return n;
948 }
949 EXPORT_SYMBOL(skb_copy);
950
951 /**
952 * __pskb_copy - create copy of an sk_buff with private head.
953 * @skb: buffer to copy
954 * @headroom: headroom of new skb
955 * @gfp_mask: allocation priority
956 *
957 * Make a copy of both an &sk_buff and part of its data, located
958 * in header. Fragmented data remain shared. This is used when
959 * the caller wishes to modify only header of &sk_buff and needs
960 * private copy of the header to alter. Returns %NULL on failure
961 * or the pointer to the buffer on success.
962 * The returned buffer has a reference count of 1.
963 */
964
965 struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
966 {
967 unsigned int size = skb_headlen(skb) + headroom;
968 struct sk_buff *n = __alloc_skb(size, gfp_mask,
969 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
970
971 if (!n)
972 goto out;
973
974 /* Set the data pointer */
975 skb_reserve(n, headroom);
976 /* Set the tail pointer and length */
977 skb_put(n, skb_headlen(skb));
978 /* Copy the bytes */
979 skb_copy_from_linear_data(skb, n->data, n->len);
980
981 n->truesize += skb->data_len;
982 n->data_len = skb->data_len;
983 n->len = skb->len;
984
985 if (skb_shinfo(skb)->nr_frags) {
986 int i;
987
988 if (skb_orphan_frags(skb, gfp_mask)) {
989 kfree_skb(n);
990 n = NULL;
991 goto out;
992 }
993 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
994 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
995 skb_frag_ref(skb, i);
996 }
997 skb_shinfo(n)->nr_frags = i;
998 }
999
1000 if (skb_has_frag_list(skb)) {
1001 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1002 skb_clone_fraglist(n);
1003 }
1004
1005 copy_skb_header(n, skb);
1006 out:
1007 return n;
1008 }
1009 EXPORT_SYMBOL(__pskb_copy);
1010
1011 /**
1012 * pskb_expand_head - reallocate header of &sk_buff
1013 * @skb: buffer to reallocate
1014 * @nhead: room to add at head
1015 * @ntail: room to add at tail
1016 * @gfp_mask: allocation priority
1017 *
1018 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1019 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1020 * reference count of 1. Returns zero in the case of success or error,
1021 * if expansion failed. In the last case, &sk_buff is not changed.
1022 *
1023 * All the pointers pointing into skb header may change and must be
1024 * reloaded after call to this function.
1025 */
1026
1027 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1028 gfp_t gfp_mask)
1029 {
1030 int i;
1031 u8 *data;
1032 int size = nhead + skb_end_offset(skb) + ntail;
1033 long off;
1034
1035 BUG_ON(nhead < 0);
1036
1037 if (skb_shared(skb))
1038 BUG();
1039
1040 size = SKB_DATA_ALIGN(size);
1041
1042 if (skb_pfmemalloc(skb))
1043 gfp_mask |= __GFP_MEMALLOC;
1044 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1045 gfp_mask, NUMA_NO_NODE, NULL);
1046 if (!data)
1047 goto nodata;
1048 size = SKB_WITH_OVERHEAD(ksize(data));
1049
1050 /* Copy only real data... and, alas, header. This should be
1051 * optimized for the cases when header is void.
1052 */
1053 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1054
1055 memcpy((struct skb_shared_info *)(data + size),
1056 skb_shinfo(skb),
1057 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1058
1059 /*
1060 * if shinfo is shared we must drop the old head gracefully, but if it
1061 * is not we can just drop the old head and let the existing refcount
1062 * be since all we did is relocate the values
1063 */
1064 if (skb_cloned(skb)) {
1065 /* copy this zero copy skb frags */
1066 if (skb_orphan_frags(skb, gfp_mask))
1067 goto nofrags;
1068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1069 skb_frag_ref(skb, i);
1070
1071 if (skb_has_frag_list(skb))
1072 skb_clone_fraglist(skb);
1073
1074 skb_release_data(skb);
1075 } else {
1076 skb_free_head(skb);
1077 }
1078 off = (data + nhead) - skb->head;
1079
1080 skb->head = data;
1081 skb->head_frag = 0;
1082 skb->data += off;
1083 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1084 skb->end = size;
1085 off = nhead;
1086 #else
1087 skb->end = skb->head + size;
1088 #endif
1089 skb->tail += off;
1090 skb_headers_offset_update(skb, off);
1091 /* Only adjust this if it actually is csum_start rather than csum */
1092 if (skb->ip_summed == CHECKSUM_PARTIAL)
1093 skb->csum_start += nhead;
1094 skb->cloned = 0;
1095 skb->hdr_len = 0;
1096 skb->nohdr = 0;
1097 atomic_set(&skb_shinfo(skb)->dataref, 1);
1098 return 0;
1099
1100 nofrags:
1101 kfree(data);
1102 nodata:
1103 return -ENOMEM;
1104 }
1105 EXPORT_SYMBOL(pskb_expand_head);
1106
1107 /* Make private copy of skb with writable head and some headroom */
1108
1109 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1110 {
1111 struct sk_buff *skb2;
1112 int delta = headroom - skb_headroom(skb);
1113
1114 if (delta <= 0)
1115 skb2 = pskb_copy(skb, GFP_ATOMIC);
1116 else {
1117 skb2 = skb_clone(skb, GFP_ATOMIC);
1118 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1119 GFP_ATOMIC)) {
1120 kfree_skb(skb2);
1121 skb2 = NULL;
1122 }
1123 }
1124 return skb2;
1125 }
1126 EXPORT_SYMBOL(skb_realloc_headroom);
1127
1128 /**
1129 * skb_copy_expand - copy and expand sk_buff
1130 * @skb: buffer to copy
1131 * @newheadroom: new free bytes at head
1132 * @newtailroom: new free bytes at tail
1133 * @gfp_mask: allocation priority
1134 *
1135 * Make a copy of both an &sk_buff and its data and while doing so
1136 * allocate additional space.
1137 *
1138 * This is used when the caller wishes to modify the data and needs a
1139 * private copy of the data to alter as well as more space for new fields.
1140 * Returns %NULL on failure or the pointer to the buffer
1141 * on success. The returned buffer has a reference count of 1.
1142 *
1143 * You must pass %GFP_ATOMIC as the allocation priority if this function
1144 * is called from an interrupt.
1145 */
1146 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1147 int newheadroom, int newtailroom,
1148 gfp_t gfp_mask)
1149 {
1150 /*
1151 * Allocate the copy buffer
1152 */
1153 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1154 gfp_mask, skb_alloc_rx_flag(skb),
1155 NUMA_NO_NODE);
1156 int oldheadroom = skb_headroom(skb);
1157 int head_copy_len, head_copy_off;
1158 int off;
1159
1160 if (!n)
1161 return NULL;
1162
1163 skb_reserve(n, newheadroom);
1164
1165 /* Set the tail pointer and length */
1166 skb_put(n, skb->len);
1167
1168 head_copy_len = oldheadroom;
1169 head_copy_off = 0;
1170 if (newheadroom <= head_copy_len)
1171 head_copy_len = newheadroom;
1172 else
1173 head_copy_off = newheadroom - head_copy_len;
1174
1175 /* Copy the linear header and data. */
1176 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1177 skb->len + head_copy_len))
1178 BUG();
1179
1180 copy_skb_header(n, skb);
1181
1182 off = newheadroom - oldheadroom;
1183 if (n->ip_summed == CHECKSUM_PARTIAL)
1184 n->csum_start += off;
1185 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1186 skb_headers_offset_update(n, off);
1187 #endif
1188
1189 return n;
1190 }
1191 EXPORT_SYMBOL(skb_copy_expand);
1192
1193 /**
1194 * skb_pad - zero pad the tail of an skb
1195 * @skb: buffer to pad
1196 * @pad: space to pad
1197 *
1198 * Ensure that a buffer is followed by a padding area that is zero
1199 * filled. Used by network drivers which may DMA or transfer data
1200 * beyond the buffer end onto the wire.
1201 *
1202 * May return error in out of memory cases. The skb is freed on error.
1203 */
1204
1205 int skb_pad(struct sk_buff *skb, int pad)
1206 {
1207 int err;
1208 int ntail;
1209
1210 /* If the skbuff is non linear tailroom is always zero.. */
1211 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1212 memset(skb->data+skb->len, 0, pad);
1213 return 0;
1214 }
1215
1216 ntail = skb->data_len + pad - (skb->end - skb->tail);
1217 if (likely(skb_cloned(skb) || ntail > 0)) {
1218 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1219 if (unlikely(err))
1220 goto free_skb;
1221 }
1222
1223 /* FIXME: The use of this function with non-linear skb's really needs
1224 * to be audited.
1225 */
1226 err = skb_linearize(skb);
1227 if (unlikely(err))
1228 goto free_skb;
1229
1230 memset(skb->data + skb->len, 0, pad);
1231 return 0;
1232
1233 free_skb:
1234 kfree_skb(skb);
1235 return err;
1236 }
1237 EXPORT_SYMBOL(skb_pad);
1238
1239 /**
1240 * skb_put - add data to a buffer
1241 * @skb: buffer to use
1242 * @len: amount of data to add
1243 *
1244 * This function extends the used data area of the buffer. If this would
1245 * exceed the total buffer size the kernel will panic. A pointer to the
1246 * first byte of the extra data is returned.
1247 */
1248 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1249 {
1250 unsigned char *tmp = skb_tail_pointer(skb);
1251 SKB_LINEAR_ASSERT(skb);
1252 skb->tail += len;
1253 skb->len += len;
1254 if (unlikely(skb->tail > skb->end))
1255 skb_over_panic(skb, len, __builtin_return_address(0));
1256 return tmp;
1257 }
1258 EXPORT_SYMBOL(skb_put);
1259
1260 /**
1261 * skb_push - add data to the start of a buffer
1262 * @skb: buffer to use
1263 * @len: amount of data to add
1264 *
1265 * This function extends the used data area of the buffer at the buffer
1266 * start. If this would exceed the total buffer headroom the kernel will
1267 * panic. A pointer to the first byte of the extra data is returned.
1268 */
1269 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1270 {
1271 skb->data -= len;
1272 skb->len += len;
1273 if (unlikely(skb->data<skb->head))
1274 skb_under_panic(skb, len, __builtin_return_address(0));
1275 return skb->data;
1276 }
1277 EXPORT_SYMBOL(skb_push);
1278
1279 /**
1280 * skb_pull - remove data from the start of a buffer
1281 * @skb: buffer to use
1282 * @len: amount of data to remove
1283 *
1284 * This function removes data from the start of a buffer, returning
1285 * the memory to the headroom. A pointer to the next data in the buffer
1286 * is returned. Once the data has been pulled future pushes will overwrite
1287 * the old data.
1288 */
1289 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1290 {
1291 return skb_pull_inline(skb, len);
1292 }
1293 EXPORT_SYMBOL(skb_pull);
1294
1295 /**
1296 * skb_trim - remove end from a buffer
1297 * @skb: buffer to alter
1298 * @len: new length
1299 *
1300 * Cut the length of a buffer down by removing data from the tail. If
1301 * the buffer is already under the length specified it is not modified.
1302 * The skb must be linear.
1303 */
1304 void skb_trim(struct sk_buff *skb, unsigned int len)
1305 {
1306 if (skb->len > len)
1307 __skb_trim(skb, len);
1308 }
1309 EXPORT_SYMBOL(skb_trim);
1310
1311 /* Trims skb to length len. It can change skb pointers.
1312 */
1313
1314 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1315 {
1316 struct sk_buff **fragp;
1317 struct sk_buff *frag;
1318 int offset = skb_headlen(skb);
1319 int nfrags = skb_shinfo(skb)->nr_frags;
1320 int i;
1321 int err;
1322
1323 if (skb_cloned(skb) &&
1324 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1325 return err;
1326
1327 i = 0;
1328 if (offset >= len)
1329 goto drop_pages;
1330
1331 for (; i < nfrags; i++) {
1332 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1333
1334 if (end < len) {
1335 offset = end;
1336 continue;
1337 }
1338
1339 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1340
1341 drop_pages:
1342 skb_shinfo(skb)->nr_frags = i;
1343
1344 for (; i < nfrags; i++)
1345 skb_frag_unref(skb, i);
1346
1347 if (skb_has_frag_list(skb))
1348 skb_drop_fraglist(skb);
1349 goto done;
1350 }
1351
1352 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1353 fragp = &frag->next) {
1354 int end = offset + frag->len;
1355
1356 if (skb_shared(frag)) {
1357 struct sk_buff *nfrag;
1358
1359 nfrag = skb_clone(frag, GFP_ATOMIC);
1360 if (unlikely(!nfrag))
1361 return -ENOMEM;
1362
1363 nfrag->next = frag->next;
1364 consume_skb(frag);
1365 frag = nfrag;
1366 *fragp = frag;
1367 }
1368
1369 if (end < len) {
1370 offset = end;
1371 continue;
1372 }
1373
1374 if (end > len &&
1375 unlikely((err = pskb_trim(frag, len - offset))))
1376 return err;
1377
1378 if (frag->next)
1379 skb_drop_list(&frag->next);
1380 break;
1381 }
1382
1383 done:
1384 if (len > skb_headlen(skb)) {
1385 skb->data_len -= skb->len - len;
1386 skb->len = len;
1387 } else {
1388 skb->len = len;
1389 skb->data_len = 0;
1390 skb_set_tail_pointer(skb, len);
1391 }
1392
1393 return 0;
1394 }
1395 EXPORT_SYMBOL(___pskb_trim);
1396
1397 /**
1398 * __pskb_pull_tail - advance tail of skb header
1399 * @skb: buffer to reallocate
1400 * @delta: number of bytes to advance tail
1401 *
1402 * The function makes a sense only on a fragmented &sk_buff,
1403 * it expands header moving its tail forward and copying necessary
1404 * data from fragmented part.
1405 *
1406 * &sk_buff MUST have reference count of 1.
1407 *
1408 * Returns %NULL (and &sk_buff does not change) if pull failed
1409 * or value of new tail of skb in the case of success.
1410 *
1411 * All the pointers pointing into skb header may change and must be
1412 * reloaded after call to this function.
1413 */
1414
1415 /* Moves tail of skb head forward, copying data from fragmented part,
1416 * when it is necessary.
1417 * 1. It may fail due to malloc failure.
1418 * 2. It may change skb pointers.
1419 *
1420 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1421 */
1422 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1423 {
1424 /* If skb has not enough free space at tail, get new one
1425 * plus 128 bytes for future expansions. If we have enough
1426 * room at tail, reallocate without expansion only if skb is cloned.
1427 */
1428 int i, k, eat = (skb->tail + delta) - skb->end;
1429
1430 if (eat > 0 || skb_cloned(skb)) {
1431 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1432 GFP_ATOMIC))
1433 return NULL;
1434 }
1435
1436 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1437 BUG();
1438
1439 /* Optimization: no fragments, no reasons to preestimate
1440 * size of pulled pages. Superb.
1441 */
1442 if (!skb_has_frag_list(skb))
1443 goto pull_pages;
1444
1445 /* Estimate size of pulled pages. */
1446 eat = delta;
1447 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1448 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1449
1450 if (size >= eat)
1451 goto pull_pages;
1452 eat -= size;
1453 }
1454
1455 /* If we need update frag list, we are in troubles.
1456 * Certainly, it possible to add an offset to skb data,
1457 * but taking into account that pulling is expected to
1458 * be very rare operation, it is worth to fight against
1459 * further bloating skb head and crucify ourselves here instead.
1460 * Pure masohism, indeed. 8)8)
1461 */
1462 if (eat) {
1463 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1464 struct sk_buff *clone = NULL;
1465 struct sk_buff *insp = NULL;
1466
1467 do {
1468 BUG_ON(!list);
1469
1470 if (list->len <= eat) {
1471 /* Eaten as whole. */
1472 eat -= list->len;
1473 list = list->next;
1474 insp = list;
1475 } else {
1476 /* Eaten partially. */
1477
1478 if (skb_shared(list)) {
1479 /* Sucks! We need to fork list. :-( */
1480 clone = skb_clone(list, GFP_ATOMIC);
1481 if (!clone)
1482 return NULL;
1483 insp = list->next;
1484 list = clone;
1485 } else {
1486 /* This may be pulled without
1487 * problems. */
1488 insp = list;
1489 }
1490 if (!pskb_pull(list, eat)) {
1491 kfree_skb(clone);
1492 return NULL;
1493 }
1494 break;
1495 }
1496 } while (eat);
1497
1498 /* Free pulled out fragments. */
1499 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1500 skb_shinfo(skb)->frag_list = list->next;
1501 kfree_skb(list);
1502 }
1503 /* And insert new clone at head. */
1504 if (clone) {
1505 clone->next = list;
1506 skb_shinfo(skb)->frag_list = clone;
1507 }
1508 }
1509 /* Success! Now we may commit changes to skb data. */
1510
1511 pull_pages:
1512 eat = delta;
1513 k = 0;
1514 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1515 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1516
1517 if (size <= eat) {
1518 skb_frag_unref(skb, i);
1519 eat -= size;
1520 } else {
1521 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1522 if (eat) {
1523 skb_shinfo(skb)->frags[k].page_offset += eat;
1524 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1525 eat = 0;
1526 }
1527 k++;
1528 }
1529 }
1530 skb_shinfo(skb)->nr_frags = k;
1531
1532 skb->tail += delta;
1533 skb->data_len -= delta;
1534
1535 return skb_tail_pointer(skb);
1536 }
1537 EXPORT_SYMBOL(__pskb_pull_tail);
1538
1539 /**
1540 * skb_copy_bits - copy bits from skb to kernel buffer
1541 * @skb: source skb
1542 * @offset: offset in source
1543 * @to: destination buffer
1544 * @len: number of bytes to copy
1545 *
1546 * Copy the specified number of bytes from the source skb to the
1547 * destination buffer.
1548 *
1549 * CAUTION ! :
1550 * If its prototype is ever changed,
1551 * check arch/{*}/net/{*}.S files,
1552 * since it is called from BPF assembly code.
1553 */
1554 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1555 {
1556 int start = skb_headlen(skb);
1557 struct sk_buff *frag_iter;
1558 int i, copy;
1559
1560 if (offset > (int)skb->len - len)
1561 goto fault;
1562
1563 /* Copy header. */
1564 if ((copy = start - offset) > 0) {
1565 if (copy > len)
1566 copy = len;
1567 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1568 if ((len -= copy) == 0)
1569 return 0;
1570 offset += copy;
1571 to += copy;
1572 }
1573
1574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1575 int end;
1576 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1577
1578 WARN_ON(start > offset + len);
1579
1580 end = start + skb_frag_size(f);
1581 if ((copy = end - offset) > 0) {
1582 u8 *vaddr;
1583
1584 if (copy > len)
1585 copy = len;
1586
1587 vaddr = kmap_atomic(skb_frag_page(f));
1588 memcpy(to,
1589 vaddr + f->page_offset + offset - start,
1590 copy);
1591 kunmap_atomic(vaddr);
1592
1593 if ((len -= copy) == 0)
1594 return 0;
1595 offset += copy;
1596 to += copy;
1597 }
1598 start = end;
1599 }
1600
1601 skb_walk_frags(skb, frag_iter) {
1602 int end;
1603
1604 WARN_ON(start > offset + len);
1605
1606 end = start + frag_iter->len;
1607 if ((copy = end - offset) > 0) {
1608 if (copy > len)
1609 copy = len;
1610 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1611 goto fault;
1612 if ((len -= copy) == 0)
1613 return 0;
1614 offset += copy;
1615 to += copy;
1616 }
1617 start = end;
1618 }
1619
1620 if (!len)
1621 return 0;
1622
1623 fault:
1624 return -EFAULT;
1625 }
1626 EXPORT_SYMBOL(skb_copy_bits);
1627
1628 /*
1629 * Callback from splice_to_pipe(), if we need to release some pages
1630 * at the end of the spd in case we error'ed out in filling the pipe.
1631 */
1632 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1633 {
1634 put_page(spd->pages[i]);
1635 }
1636
1637 static struct page *linear_to_page(struct page *page, unsigned int *len,
1638 unsigned int *offset,
1639 struct sock *sk)
1640 {
1641 struct page_frag *pfrag = sk_page_frag(sk);
1642
1643 if (!sk_page_frag_refill(sk, pfrag))
1644 return NULL;
1645
1646 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1647
1648 memcpy(page_address(pfrag->page) + pfrag->offset,
1649 page_address(page) + *offset, *len);
1650 *offset = pfrag->offset;
1651 pfrag->offset += *len;
1652
1653 return pfrag->page;
1654 }
1655
1656 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1657 struct page *page,
1658 unsigned int offset)
1659 {
1660 return spd->nr_pages &&
1661 spd->pages[spd->nr_pages - 1] == page &&
1662 (spd->partial[spd->nr_pages - 1].offset +
1663 spd->partial[spd->nr_pages - 1].len == offset);
1664 }
1665
1666 /*
1667 * Fill page/offset/length into spd, if it can hold more pages.
1668 */
1669 static bool spd_fill_page(struct splice_pipe_desc *spd,
1670 struct pipe_inode_info *pipe, struct page *page,
1671 unsigned int *len, unsigned int offset,
1672 bool linear,
1673 struct sock *sk)
1674 {
1675 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1676 return true;
1677
1678 if (linear) {
1679 page = linear_to_page(page, len, &offset, sk);
1680 if (!page)
1681 return true;
1682 }
1683 if (spd_can_coalesce(spd, page, offset)) {
1684 spd->partial[spd->nr_pages - 1].len += *len;
1685 return false;
1686 }
1687 get_page(page);
1688 spd->pages[spd->nr_pages] = page;
1689 spd->partial[spd->nr_pages].len = *len;
1690 spd->partial[spd->nr_pages].offset = offset;
1691 spd->nr_pages++;
1692
1693 return false;
1694 }
1695
1696 static bool __splice_segment(struct page *page, unsigned int poff,
1697 unsigned int plen, unsigned int *off,
1698 unsigned int *len,
1699 struct splice_pipe_desc *spd, bool linear,
1700 struct sock *sk,
1701 struct pipe_inode_info *pipe)
1702 {
1703 if (!*len)
1704 return true;
1705
1706 /* skip this segment if already processed */
1707 if (*off >= plen) {
1708 *off -= plen;
1709 return false;
1710 }
1711
1712 /* ignore any bits we already processed */
1713 poff += *off;
1714 plen -= *off;
1715 *off = 0;
1716
1717 do {
1718 unsigned int flen = min(*len, plen);
1719
1720 if (spd_fill_page(spd, pipe, page, &flen, poff,
1721 linear, sk))
1722 return true;
1723 poff += flen;
1724 plen -= flen;
1725 *len -= flen;
1726 } while (*len && plen);
1727
1728 return false;
1729 }
1730
1731 /*
1732 * Map linear and fragment data from the skb to spd. It reports true if the
1733 * pipe is full or if we already spliced the requested length.
1734 */
1735 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1736 unsigned int *offset, unsigned int *len,
1737 struct splice_pipe_desc *spd, struct sock *sk)
1738 {
1739 int seg;
1740
1741 /* map the linear part :
1742 * If skb->head_frag is set, this 'linear' part is backed by a
1743 * fragment, and if the head is not shared with any clones then
1744 * we can avoid a copy since we own the head portion of this page.
1745 */
1746 if (__splice_segment(virt_to_page(skb->data),
1747 (unsigned long) skb->data & (PAGE_SIZE - 1),
1748 skb_headlen(skb),
1749 offset, len, spd,
1750 skb_head_is_locked(skb),
1751 sk, pipe))
1752 return true;
1753
1754 /*
1755 * then map the fragments
1756 */
1757 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1758 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1759
1760 if (__splice_segment(skb_frag_page(f),
1761 f->page_offset, skb_frag_size(f),
1762 offset, len, spd, false, sk, pipe))
1763 return true;
1764 }
1765
1766 return false;
1767 }
1768
1769 /*
1770 * Map data from the skb to a pipe. Should handle both the linear part,
1771 * the fragments, and the frag list. It does NOT handle frag lists within
1772 * the frag list, if such a thing exists. We'd probably need to recurse to
1773 * handle that cleanly.
1774 */
1775 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1776 struct pipe_inode_info *pipe, unsigned int tlen,
1777 unsigned int flags)
1778 {
1779 struct partial_page partial[MAX_SKB_FRAGS];
1780 struct page *pages[MAX_SKB_FRAGS];
1781 struct splice_pipe_desc spd = {
1782 .pages = pages,
1783 .partial = partial,
1784 .nr_pages_max = MAX_SKB_FRAGS,
1785 .flags = flags,
1786 .ops = &nosteal_pipe_buf_ops,
1787 .spd_release = sock_spd_release,
1788 };
1789 struct sk_buff *frag_iter;
1790 struct sock *sk = skb->sk;
1791 int ret = 0;
1792
1793 /*
1794 * __skb_splice_bits() only fails if the output has no room left,
1795 * so no point in going over the frag_list for the error case.
1796 */
1797 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1798 goto done;
1799 else if (!tlen)
1800 goto done;
1801
1802 /*
1803 * now see if we have a frag_list to map
1804 */
1805 skb_walk_frags(skb, frag_iter) {
1806 if (!tlen)
1807 break;
1808 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1809 break;
1810 }
1811
1812 done:
1813 if (spd.nr_pages) {
1814 /*
1815 * Drop the socket lock, otherwise we have reverse
1816 * locking dependencies between sk_lock and i_mutex
1817 * here as compared to sendfile(). We enter here
1818 * with the socket lock held, and splice_to_pipe() will
1819 * grab the pipe inode lock. For sendfile() emulation,
1820 * we call into ->sendpage() with the i_mutex lock held
1821 * and networking will grab the socket lock.
1822 */
1823 release_sock(sk);
1824 ret = splice_to_pipe(pipe, &spd);
1825 lock_sock(sk);
1826 }
1827
1828 return ret;
1829 }
1830
1831 /**
1832 * skb_store_bits - store bits from kernel buffer to skb
1833 * @skb: destination buffer
1834 * @offset: offset in destination
1835 * @from: source buffer
1836 * @len: number of bytes to copy
1837 *
1838 * Copy the specified number of bytes from the source buffer to the
1839 * destination skb. This function handles all the messy bits of
1840 * traversing fragment lists and such.
1841 */
1842
1843 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1844 {
1845 int start = skb_headlen(skb);
1846 struct sk_buff *frag_iter;
1847 int i, copy;
1848
1849 if (offset > (int)skb->len - len)
1850 goto fault;
1851
1852 if ((copy = start - offset) > 0) {
1853 if (copy > len)
1854 copy = len;
1855 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1856 if ((len -= copy) == 0)
1857 return 0;
1858 offset += copy;
1859 from += copy;
1860 }
1861
1862 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1863 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1864 int end;
1865
1866 WARN_ON(start > offset + len);
1867
1868 end = start + skb_frag_size(frag);
1869 if ((copy = end - offset) > 0) {
1870 u8 *vaddr;
1871
1872 if (copy > len)
1873 copy = len;
1874
1875 vaddr = kmap_atomic(skb_frag_page(frag));
1876 memcpy(vaddr + frag->page_offset + offset - start,
1877 from, copy);
1878 kunmap_atomic(vaddr);
1879
1880 if ((len -= copy) == 0)
1881 return 0;
1882 offset += copy;
1883 from += copy;
1884 }
1885 start = end;
1886 }
1887
1888 skb_walk_frags(skb, frag_iter) {
1889 int end;
1890
1891 WARN_ON(start > offset + len);
1892
1893 end = start + frag_iter->len;
1894 if ((copy = end - offset) > 0) {
1895 if (copy > len)
1896 copy = len;
1897 if (skb_store_bits(frag_iter, offset - start,
1898 from, copy))
1899 goto fault;
1900 if ((len -= copy) == 0)
1901 return 0;
1902 offset += copy;
1903 from += copy;
1904 }
1905 start = end;
1906 }
1907 if (!len)
1908 return 0;
1909
1910 fault:
1911 return -EFAULT;
1912 }
1913 EXPORT_SYMBOL(skb_store_bits);
1914
1915 /* Checksum skb data. */
1916
1917 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1918 int len, __wsum csum)
1919 {
1920 int start = skb_headlen(skb);
1921 int i, copy = start - offset;
1922 struct sk_buff *frag_iter;
1923 int pos = 0;
1924
1925 /* Checksum header. */
1926 if (copy > 0) {
1927 if (copy > len)
1928 copy = len;
1929 csum = csum_partial(skb->data + offset, copy, csum);
1930 if ((len -= copy) == 0)
1931 return csum;
1932 offset += copy;
1933 pos = copy;
1934 }
1935
1936 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1937 int end;
1938 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1939
1940 WARN_ON(start > offset + len);
1941
1942 end = start + skb_frag_size(frag);
1943 if ((copy = end - offset) > 0) {
1944 __wsum csum2;
1945 u8 *vaddr;
1946
1947 if (copy > len)
1948 copy = len;
1949 vaddr = kmap_atomic(skb_frag_page(frag));
1950 csum2 = csum_partial(vaddr + frag->page_offset +
1951 offset - start, copy, 0);
1952 kunmap_atomic(vaddr);
1953 csum = csum_block_add(csum, csum2, pos);
1954 if (!(len -= copy))
1955 return csum;
1956 offset += copy;
1957 pos += copy;
1958 }
1959 start = end;
1960 }
1961
1962 skb_walk_frags(skb, frag_iter) {
1963 int end;
1964
1965 WARN_ON(start > offset + len);
1966
1967 end = start + frag_iter->len;
1968 if ((copy = end - offset) > 0) {
1969 __wsum csum2;
1970 if (copy > len)
1971 copy = len;
1972 csum2 = skb_checksum(frag_iter, offset - start,
1973 copy, 0);
1974 csum = csum_block_add(csum, csum2, pos);
1975 if ((len -= copy) == 0)
1976 return csum;
1977 offset += copy;
1978 pos += copy;
1979 }
1980 start = end;
1981 }
1982 BUG_ON(len);
1983
1984 return csum;
1985 }
1986 EXPORT_SYMBOL(skb_checksum);
1987
1988 /* Both of above in one bottle. */
1989
1990 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1991 u8 *to, int len, __wsum csum)
1992 {
1993 int start = skb_headlen(skb);
1994 int i, copy = start - offset;
1995 struct sk_buff *frag_iter;
1996 int pos = 0;
1997
1998 /* Copy header. */
1999 if (copy > 0) {
2000 if (copy > len)
2001 copy = len;
2002 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2003 copy, csum);
2004 if ((len -= copy) == 0)
2005 return csum;
2006 offset += copy;
2007 to += copy;
2008 pos = copy;
2009 }
2010
2011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2012 int end;
2013
2014 WARN_ON(start > offset + len);
2015
2016 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2017 if ((copy = end - offset) > 0) {
2018 __wsum csum2;
2019 u8 *vaddr;
2020 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2021
2022 if (copy > len)
2023 copy = len;
2024 vaddr = kmap_atomic(skb_frag_page(frag));
2025 csum2 = csum_partial_copy_nocheck(vaddr +
2026 frag->page_offset +
2027 offset - start, to,
2028 copy, 0);
2029 kunmap_atomic(vaddr);
2030 csum = csum_block_add(csum, csum2, pos);
2031 if (!(len -= copy))
2032 return csum;
2033 offset += copy;
2034 to += copy;
2035 pos += copy;
2036 }
2037 start = end;
2038 }
2039
2040 skb_walk_frags(skb, frag_iter) {
2041 __wsum csum2;
2042 int end;
2043
2044 WARN_ON(start > offset + len);
2045
2046 end = start + frag_iter->len;
2047 if ((copy = end - offset) > 0) {
2048 if (copy > len)
2049 copy = len;
2050 csum2 = skb_copy_and_csum_bits(frag_iter,
2051 offset - start,
2052 to, copy, 0);
2053 csum = csum_block_add(csum, csum2, pos);
2054 if ((len -= copy) == 0)
2055 return csum;
2056 offset += copy;
2057 to += copy;
2058 pos += copy;
2059 }
2060 start = end;
2061 }
2062 BUG_ON(len);
2063 return csum;
2064 }
2065 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2066
2067 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2068 {
2069 __wsum csum;
2070 long csstart;
2071
2072 if (skb->ip_summed == CHECKSUM_PARTIAL)
2073 csstart = skb_checksum_start_offset(skb);
2074 else
2075 csstart = skb_headlen(skb);
2076
2077 BUG_ON(csstart > skb_headlen(skb));
2078
2079 skb_copy_from_linear_data(skb, to, csstart);
2080
2081 csum = 0;
2082 if (csstart != skb->len)
2083 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2084 skb->len - csstart, 0);
2085
2086 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2087 long csstuff = csstart + skb->csum_offset;
2088
2089 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2090 }
2091 }
2092 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2093
2094 /**
2095 * skb_dequeue - remove from the head of the queue
2096 * @list: list to dequeue from
2097 *
2098 * Remove the head of the list. The list lock is taken so the function
2099 * may be used safely with other locking list functions. The head item is
2100 * returned or %NULL if the list is empty.
2101 */
2102
2103 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2104 {
2105 unsigned long flags;
2106 struct sk_buff *result;
2107
2108 spin_lock_irqsave(&list->lock, flags);
2109 result = __skb_dequeue(list);
2110 spin_unlock_irqrestore(&list->lock, flags);
2111 return result;
2112 }
2113 EXPORT_SYMBOL(skb_dequeue);
2114
2115 /**
2116 * skb_dequeue_tail - remove from the tail of the queue
2117 * @list: list to dequeue from
2118 *
2119 * Remove the tail of the list. The list lock is taken so the function
2120 * may be used safely with other locking list functions. The tail item is
2121 * returned or %NULL if the list is empty.
2122 */
2123 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2124 {
2125 unsigned long flags;
2126 struct sk_buff *result;
2127
2128 spin_lock_irqsave(&list->lock, flags);
2129 result = __skb_dequeue_tail(list);
2130 spin_unlock_irqrestore(&list->lock, flags);
2131 return result;
2132 }
2133 EXPORT_SYMBOL(skb_dequeue_tail);
2134
2135 /**
2136 * skb_queue_purge - empty a list
2137 * @list: list to empty
2138 *
2139 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2140 * the list and one reference dropped. This function takes the list
2141 * lock and is atomic with respect to other list locking functions.
2142 */
2143 void skb_queue_purge(struct sk_buff_head *list)
2144 {
2145 struct sk_buff *skb;
2146 while ((skb = skb_dequeue(list)) != NULL)
2147 kfree_skb(skb);
2148 }
2149 EXPORT_SYMBOL(skb_queue_purge);
2150
2151 /**
2152 * skb_queue_head - queue a buffer at the list head
2153 * @list: list to use
2154 * @newsk: buffer to queue
2155 *
2156 * Queue a buffer at the start of the list. This function takes the
2157 * list lock and can be used safely with other locking &sk_buff functions
2158 * safely.
2159 *
2160 * A buffer cannot be placed on two lists at the same time.
2161 */
2162 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2163 {
2164 unsigned long flags;
2165
2166 spin_lock_irqsave(&list->lock, flags);
2167 __skb_queue_head(list, newsk);
2168 spin_unlock_irqrestore(&list->lock, flags);
2169 }
2170 EXPORT_SYMBOL(skb_queue_head);
2171
2172 /**
2173 * skb_queue_tail - queue a buffer at the list tail
2174 * @list: list to use
2175 * @newsk: buffer to queue
2176 *
2177 * Queue a buffer at the tail of the list. This function takes the
2178 * list lock and can be used safely with other locking &sk_buff functions
2179 * safely.
2180 *
2181 * A buffer cannot be placed on two lists at the same time.
2182 */
2183 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2184 {
2185 unsigned long flags;
2186
2187 spin_lock_irqsave(&list->lock, flags);
2188 __skb_queue_tail(list, newsk);
2189 spin_unlock_irqrestore(&list->lock, flags);
2190 }
2191 EXPORT_SYMBOL(skb_queue_tail);
2192
2193 /**
2194 * skb_unlink - remove a buffer from a list
2195 * @skb: buffer to remove
2196 * @list: list to use
2197 *
2198 * Remove a packet from a list. The list locks are taken and this
2199 * function is atomic with respect to other list locked calls
2200 *
2201 * You must know what list the SKB is on.
2202 */
2203 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2204 {
2205 unsigned long flags;
2206
2207 spin_lock_irqsave(&list->lock, flags);
2208 __skb_unlink(skb, list);
2209 spin_unlock_irqrestore(&list->lock, flags);
2210 }
2211 EXPORT_SYMBOL(skb_unlink);
2212
2213 /**
2214 * skb_append - append a buffer
2215 * @old: buffer to insert after
2216 * @newsk: buffer to insert
2217 * @list: list to use
2218 *
2219 * Place a packet after a given packet in a list. The list locks are taken
2220 * and this function is atomic with respect to other list locked calls.
2221 * A buffer cannot be placed on two lists at the same time.
2222 */
2223 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2224 {
2225 unsigned long flags;
2226
2227 spin_lock_irqsave(&list->lock, flags);
2228 __skb_queue_after(list, old, newsk);
2229 spin_unlock_irqrestore(&list->lock, flags);
2230 }
2231 EXPORT_SYMBOL(skb_append);
2232
2233 /**
2234 * skb_insert - insert a buffer
2235 * @old: buffer to insert before
2236 * @newsk: buffer to insert
2237 * @list: list to use
2238 *
2239 * Place a packet before a given packet in a list. The list locks are
2240 * taken and this function is atomic with respect to other list locked
2241 * calls.
2242 *
2243 * A buffer cannot be placed on two lists at the same time.
2244 */
2245 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2246 {
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&list->lock, flags);
2250 __skb_insert(newsk, old->prev, old, list);
2251 spin_unlock_irqrestore(&list->lock, flags);
2252 }
2253 EXPORT_SYMBOL(skb_insert);
2254
2255 static inline void skb_split_inside_header(struct sk_buff *skb,
2256 struct sk_buff* skb1,
2257 const u32 len, const int pos)
2258 {
2259 int i;
2260
2261 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2262 pos - len);
2263 /* And move data appendix as is. */
2264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2265 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2266
2267 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2268 skb_shinfo(skb)->nr_frags = 0;
2269 skb1->data_len = skb->data_len;
2270 skb1->len += skb1->data_len;
2271 skb->data_len = 0;
2272 skb->len = len;
2273 skb_set_tail_pointer(skb, len);
2274 }
2275
2276 static inline void skb_split_no_header(struct sk_buff *skb,
2277 struct sk_buff* skb1,
2278 const u32 len, int pos)
2279 {
2280 int i, k = 0;
2281 const int nfrags = skb_shinfo(skb)->nr_frags;
2282
2283 skb_shinfo(skb)->nr_frags = 0;
2284 skb1->len = skb1->data_len = skb->len - len;
2285 skb->len = len;
2286 skb->data_len = len - pos;
2287
2288 for (i = 0; i < nfrags; i++) {
2289 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2290
2291 if (pos + size > len) {
2292 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2293
2294 if (pos < len) {
2295 /* Split frag.
2296 * We have two variants in this case:
2297 * 1. Move all the frag to the second
2298 * part, if it is possible. F.e.
2299 * this approach is mandatory for TUX,
2300 * where splitting is expensive.
2301 * 2. Split is accurately. We make this.
2302 */
2303 skb_frag_ref(skb, i);
2304 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2305 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2306 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2307 skb_shinfo(skb)->nr_frags++;
2308 }
2309 k++;
2310 } else
2311 skb_shinfo(skb)->nr_frags++;
2312 pos += size;
2313 }
2314 skb_shinfo(skb1)->nr_frags = k;
2315 }
2316
2317 /**
2318 * skb_split - Split fragmented skb to two parts at length len.
2319 * @skb: the buffer to split
2320 * @skb1: the buffer to receive the second part
2321 * @len: new length for skb
2322 */
2323 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2324 {
2325 int pos = skb_headlen(skb);
2326
2327 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2328 if (len < pos) /* Split line is inside header. */
2329 skb_split_inside_header(skb, skb1, len, pos);
2330 else /* Second chunk has no header, nothing to copy. */
2331 skb_split_no_header(skb, skb1, len, pos);
2332 }
2333 EXPORT_SYMBOL(skb_split);
2334
2335 /* Shifting from/to a cloned skb is a no-go.
2336 *
2337 * Caller cannot keep skb_shinfo related pointers past calling here!
2338 */
2339 static int skb_prepare_for_shift(struct sk_buff *skb)
2340 {
2341 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2342 }
2343
2344 /**
2345 * skb_shift - Shifts paged data partially from skb to another
2346 * @tgt: buffer into which tail data gets added
2347 * @skb: buffer from which the paged data comes from
2348 * @shiftlen: shift up to this many bytes
2349 *
2350 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2351 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2352 * It's up to caller to free skb if everything was shifted.
2353 *
2354 * If @tgt runs out of frags, the whole operation is aborted.
2355 *
2356 * Skb cannot include anything else but paged data while tgt is allowed
2357 * to have non-paged data as well.
2358 *
2359 * TODO: full sized shift could be optimized but that would need
2360 * specialized skb free'er to handle frags without up-to-date nr_frags.
2361 */
2362 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2363 {
2364 int from, to, merge, todo;
2365 struct skb_frag_struct *fragfrom, *fragto;
2366
2367 BUG_ON(shiftlen > skb->len);
2368 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2369
2370 todo = shiftlen;
2371 from = 0;
2372 to = skb_shinfo(tgt)->nr_frags;
2373 fragfrom = &skb_shinfo(skb)->frags[from];
2374
2375 /* Actual merge is delayed until the point when we know we can
2376 * commit all, so that we don't have to undo partial changes
2377 */
2378 if (!to ||
2379 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2380 fragfrom->page_offset)) {
2381 merge = -1;
2382 } else {
2383 merge = to - 1;
2384
2385 todo -= skb_frag_size(fragfrom);
2386 if (todo < 0) {
2387 if (skb_prepare_for_shift(skb) ||
2388 skb_prepare_for_shift(tgt))
2389 return 0;
2390
2391 /* All previous frag pointers might be stale! */
2392 fragfrom = &skb_shinfo(skb)->frags[from];
2393 fragto = &skb_shinfo(tgt)->frags[merge];
2394
2395 skb_frag_size_add(fragto, shiftlen);
2396 skb_frag_size_sub(fragfrom, shiftlen);
2397 fragfrom->page_offset += shiftlen;
2398
2399 goto onlymerged;
2400 }
2401
2402 from++;
2403 }
2404
2405 /* Skip full, not-fitting skb to avoid expensive operations */
2406 if ((shiftlen == skb->len) &&
2407 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2408 return 0;
2409
2410 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2411 return 0;
2412
2413 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2414 if (to == MAX_SKB_FRAGS)
2415 return 0;
2416
2417 fragfrom = &skb_shinfo(skb)->frags[from];
2418 fragto = &skb_shinfo(tgt)->frags[to];
2419
2420 if (todo >= skb_frag_size(fragfrom)) {
2421 *fragto = *fragfrom;
2422 todo -= skb_frag_size(fragfrom);
2423 from++;
2424 to++;
2425
2426 } else {
2427 __skb_frag_ref(fragfrom);
2428 fragto->page = fragfrom->page;
2429 fragto->page_offset = fragfrom->page_offset;
2430 skb_frag_size_set(fragto, todo);
2431
2432 fragfrom->page_offset += todo;
2433 skb_frag_size_sub(fragfrom, todo);
2434 todo = 0;
2435
2436 to++;
2437 break;
2438 }
2439 }
2440
2441 /* Ready to "commit" this state change to tgt */
2442 skb_shinfo(tgt)->nr_frags = to;
2443
2444 if (merge >= 0) {
2445 fragfrom = &skb_shinfo(skb)->frags[0];
2446 fragto = &skb_shinfo(tgt)->frags[merge];
2447
2448 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2449 __skb_frag_unref(fragfrom);
2450 }
2451
2452 /* Reposition in the original skb */
2453 to = 0;
2454 while (from < skb_shinfo(skb)->nr_frags)
2455 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2456 skb_shinfo(skb)->nr_frags = to;
2457
2458 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2459
2460 onlymerged:
2461 /* Most likely the tgt won't ever need its checksum anymore, skb on
2462 * the other hand might need it if it needs to be resent
2463 */
2464 tgt->ip_summed = CHECKSUM_PARTIAL;
2465 skb->ip_summed = CHECKSUM_PARTIAL;
2466
2467 /* Yak, is it really working this way? Some helper please? */
2468 skb->len -= shiftlen;
2469 skb->data_len -= shiftlen;
2470 skb->truesize -= shiftlen;
2471 tgt->len += shiftlen;
2472 tgt->data_len += shiftlen;
2473 tgt->truesize += shiftlen;
2474
2475 return shiftlen;
2476 }
2477
2478 /**
2479 * skb_prepare_seq_read - Prepare a sequential read of skb data
2480 * @skb: the buffer to read
2481 * @from: lower offset of data to be read
2482 * @to: upper offset of data to be read
2483 * @st: state variable
2484 *
2485 * Initializes the specified state variable. Must be called before
2486 * invoking skb_seq_read() for the first time.
2487 */
2488 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2489 unsigned int to, struct skb_seq_state *st)
2490 {
2491 st->lower_offset = from;
2492 st->upper_offset = to;
2493 st->root_skb = st->cur_skb = skb;
2494 st->frag_idx = st->stepped_offset = 0;
2495 st->frag_data = NULL;
2496 }
2497 EXPORT_SYMBOL(skb_prepare_seq_read);
2498
2499 /**
2500 * skb_seq_read - Sequentially read skb data
2501 * @consumed: number of bytes consumed by the caller so far
2502 * @data: destination pointer for data to be returned
2503 * @st: state variable
2504 *
2505 * Reads a block of skb data at &consumed relative to the
2506 * lower offset specified to skb_prepare_seq_read(). Assigns
2507 * the head of the data block to &data and returns the length
2508 * of the block or 0 if the end of the skb data or the upper
2509 * offset has been reached.
2510 *
2511 * The caller is not required to consume all of the data
2512 * returned, i.e. &consumed is typically set to the number
2513 * of bytes already consumed and the next call to
2514 * skb_seq_read() will return the remaining part of the block.
2515 *
2516 * Note 1: The size of each block of data returned can be arbitrary,
2517 * this limitation is the cost for zerocopy seqeuental
2518 * reads of potentially non linear data.
2519 *
2520 * Note 2: Fragment lists within fragments are not implemented
2521 * at the moment, state->root_skb could be replaced with
2522 * a stack for this purpose.
2523 */
2524 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2525 struct skb_seq_state *st)
2526 {
2527 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2528 skb_frag_t *frag;
2529
2530 if (unlikely(abs_offset >= st->upper_offset))
2531 return 0;
2532
2533 next_skb:
2534 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2535
2536 if (abs_offset < block_limit && !st->frag_data) {
2537 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2538 return block_limit - abs_offset;
2539 }
2540
2541 if (st->frag_idx == 0 && !st->frag_data)
2542 st->stepped_offset += skb_headlen(st->cur_skb);
2543
2544 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2545 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2546 block_limit = skb_frag_size(frag) + st->stepped_offset;
2547
2548 if (abs_offset < block_limit) {
2549 if (!st->frag_data)
2550 st->frag_data = kmap_atomic(skb_frag_page(frag));
2551
2552 *data = (u8 *) st->frag_data + frag->page_offset +
2553 (abs_offset - st->stepped_offset);
2554
2555 return block_limit - abs_offset;
2556 }
2557
2558 if (st->frag_data) {
2559 kunmap_atomic(st->frag_data);
2560 st->frag_data = NULL;
2561 }
2562
2563 st->frag_idx++;
2564 st->stepped_offset += skb_frag_size(frag);
2565 }
2566
2567 if (st->frag_data) {
2568 kunmap_atomic(st->frag_data);
2569 st->frag_data = NULL;
2570 }
2571
2572 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2573 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2574 st->frag_idx = 0;
2575 goto next_skb;
2576 } else if (st->cur_skb->next) {
2577 st->cur_skb = st->cur_skb->next;
2578 st->frag_idx = 0;
2579 goto next_skb;
2580 }
2581
2582 return 0;
2583 }
2584 EXPORT_SYMBOL(skb_seq_read);
2585
2586 /**
2587 * skb_abort_seq_read - Abort a sequential read of skb data
2588 * @st: state variable
2589 *
2590 * Must be called if skb_seq_read() was not called until it
2591 * returned 0.
2592 */
2593 void skb_abort_seq_read(struct skb_seq_state *st)
2594 {
2595 if (st->frag_data)
2596 kunmap_atomic(st->frag_data);
2597 }
2598 EXPORT_SYMBOL(skb_abort_seq_read);
2599
2600 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2601
2602 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2603 struct ts_config *conf,
2604 struct ts_state *state)
2605 {
2606 return skb_seq_read(offset, text, TS_SKB_CB(state));
2607 }
2608
2609 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2610 {
2611 skb_abort_seq_read(TS_SKB_CB(state));
2612 }
2613
2614 /**
2615 * skb_find_text - Find a text pattern in skb data
2616 * @skb: the buffer to look in
2617 * @from: search offset
2618 * @to: search limit
2619 * @config: textsearch configuration
2620 * @state: uninitialized textsearch state variable
2621 *
2622 * Finds a pattern in the skb data according to the specified
2623 * textsearch configuration. Use textsearch_next() to retrieve
2624 * subsequent occurrences of the pattern. Returns the offset
2625 * to the first occurrence or UINT_MAX if no match was found.
2626 */
2627 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2628 unsigned int to, struct ts_config *config,
2629 struct ts_state *state)
2630 {
2631 unsigned int ret;
2632
2633 config->get_next_block = skb_ts_get_next_block;
2634 config->finish = skb_ts_finish;
2635
2636 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2637
2638 ret = textsearch_find(config, state);
2639 return (ret <= to - from ? ret : UINT_MAX);
2640 }
2641 EXPORT_SYMBOL(skb_find_text);
2642
2643 /**
2644 * skb_append_datato_frags - append the user data to a skb
2645 * @sk: sock structure
2646 * @skb: skb structure to be appened with user data.
2647 * @getfrag: call back function to be used for getting the user data
2648 * @from: pointer to user message iov
2649 * @length: length of the iov message
2650 *
2651 * Description: This procedure append the user data in the fragment part
2652 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2653 */
2654 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2655 int (*getfrag)(void *from, char *to, int offset,
2656 int len, int odd, struct sk_buff *skb),
2657 void *from, int length)
2658 {
2659 int frg_cnt = skb_shinfo(skb)->nr_frags;
2660 int copy;
2661 int offset = 0;
2662 int ret;
2663 struct page_frag *pfrag = &current->task_frag;
2664
2665 do {
2666 /* Return error if we don't have space for new frag */
2667 if (frg_cnt >= MAX_SKB_FRAGS)
2668 return -EMSGSIZE;
2669
2670 if (!sk_page_frag_refill(sk, pfrag))
2671 return -ENOMEM;
2672
2673 /* copy the user data to page */
2674 copy = min_t(int, length, pfrag->size - pfrag->offset);
2675
2676 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2677 offset, copy, 0, skb);
2678 if (ret < 0)
2679 return -EFAULT;
2680
2681 /* copy was successful so update the size parameters */
2682 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2683 copy);
2684 frg_cnt++;
2685 pfrag->offset += copy;
2686 get_page(pfrag->page);
2687
2688 skb->truesize += copy;
2689 atomic_add(copy, &sk->sk_wmem_alloc);
2690 skb->len += copy;
2691 skb->data_len += copy;
2692 offset += copy;
2693 length -= copy;
2694
2695 } while (length > 0);
2696
2697 return 0;
2698 }
2699 EXPORT_SYMBOL(skb_append_datato_frags);
2700
2701 /**
2702 * skb_pull_rcsum - pull skb and update receive checksum
2703 * @skb: buffer to update
2704 * @len: length of data pulled
2705 *
2706 * This function performs an skb_pull on the packet and updates
2707 * the CHECKSUM_COMPLETE checksum. It should be used on
2708 * receive path processing instead of skb_pull unless you know
2709 * that the checksum difference is zero (e.g., a valid IP header)
2710 * or you are setting ip_summed to CHECKSUM_NONE.
2711 */
2712 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2713 {
2714 BUG_ON(len > skb->len);
2715 skb->len -= len;
2716 BUG_ON(skb->len < skb->data_len);
2717 skb_postpull_rcsum(skb, skb->data, len);
2718 return skb->data += len;
2719 }
2720 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2721
2722 /**
2723 * skb_segment - Perform protocol segmentation on skb.
2724 * @skb: buffer to segment
2725 * @features: features for the output path (see dev->features)
2726 *
2727 * This function performs segmentation on the given skb. It returns
2728 * a pointer to the first in a list of new skbs for the segments.
2729 * In case of error it returns ERR_PTR(err).
2730 */
2731 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2732 {
2733 struct sk_buff *segs = NULL;
2734 struct sk_buff *tail = NULL;
2735 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2736 unsigned int mss = skb_shinfo(skb)->gso_size;
2737 unsigned int doffset = skb->data - skb_mac_header(skb);
2738 unsigned int offset = doffset;
2739 unsigned int tnl_hlen = skb_tnl_header_len(skb);
2740 unsigned int headroom;
2741 unsigned int len;
2742 __be16 proto;
2743 bool csum;
2744 int sg = !!(features & NETIF_F_SG);
2745 int nfrags = skb_shinfo(skb)->nr_frags;
2746 int err = -ENOMEM;
2747 int i = 0;
2748 int pos;
2749
2750 proto = skb_network_protocol(skb);
2751 if (unlikely(!proto))
2752 return ERR_PTR(-EINVAL);
2753
2754 csum = !!can_checksum_protocol(features, proto);
2755 __skb_push(skb, doffset);
2756 headroom = skb_headroom(skb);
2757 pos = skb_headlen(skb);
2758
2759 do {
2760 struct sk_buff *nskb;
2761 skb_frag_t *frag;
2762 int hsize;
2763 int size;
2764
2765 len = skb->len - offset;
2766 if (len > mss)
2767 len = mss;
2768
2769 hsize = skb_headlen(skb) - offset;
2770 if (hsize < 0)
2771 hsize = 0;
2772 if (hsize > len || !sg)
2773 hsize = len;
2774
2775 if (!hsize && i >= nfrags) {
2776 BUG_ON(fskb->len != len);
2777
2778 pos += len;
2779 nskb = skb_clone(fskb, GFP_ATOMIC);
2780 fskb = fskb->next;
2781
2782 if (unlikely(!nskb))
2783 goto err;
2784
2785 hsize = skb_end_offset(nskb);
2786 if (skb_cow_head(nskb, doffset + headroom)) {
2787 kfree_skb(nskb);
2788 goto err;
2789 }
2790
2791 nskb->truesize += skb_end_offset(nskb) - hsize;
2792 skb_release_head_state(nskb);
2793 __skb_push(nskb, doffset);
2794 } else {
2795 nskb = __alloc_skb(hsize + doffset + headroom,
2796 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2797 NUMA_NO_NODE);
2798
2799 if (unlikely(!nskb))
2800 goto err;
2801
2802 skb_reserve(nskb, headroom);
2803 __skb_put(nskb, doffset);
2804 }
2805
2806 if (segs)
2807 tail->next = nskb;
2808 else
2809 segs = nskb;
2810 tail = nskb;
2811
2812 __copy_skb_header(nskb, skb);
2813 nskb->mac_len = skb->mac_len;
2814
2815 /* nskb and skb might have different headroom */
2816 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2817 nskb->csum_start += skb_headroom(nskb) - headroom;
2818
2819 skb_reset_mac_header(nskb);
2820 skb_set_network_header(nskb, skb->mac_len);
2821 nskb->transport_header = (nskb->network_header +
2822 skb_network_header_len(skb));
2823
2824 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
2825 nskb->data - tnl_hlen,
2826 doffset + tnl_hlen);
2827
2828 if (fskb != skb_shinfo(skb)->frag_list)
2829 goto perform_csum_check;
2830
2831 if (!sg) {
2832 nskb->ip_summed = CHECKSUM_NONE;
2833 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2834 skb_put(nskb, len),
2835 len, 0);
2836 continue;
2837 }
2838
2839 frag = skb_shinfo(nskb)->frags;
2840
2841 skb_copy_from_linear_data_offset(skb, offset,
2842 skb_put(nskb, hsize), hsize);
2843
2844 skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2845
2846 while (pos < offset + len && i < nfrags) {
2847 *frag = skb_shinfo(skb)->frags[i];
2848 __skb_frag_ref(frag);
2849 size = skb_frag_size(frag);
2850
2851 if (pos < offset) {
2852 frag->page_offset += offset - pos;
2853 skb_frag_size_sub(frag, offset - pos);
2854 }
2855
2856 skb_shinfo(nskb)->nr_frags++;
2857
2858 if (pos + size <= offset + len) {
2859 i++;
2860 pos += size;
2861 } else {
2862 skb_frag_size_sub(frag, pos + size - (offset + len));
2863 goto skip_fraglist;
2864 }
2865
2866 frag++;
2867 }
2868
2869 if (pos < offset + len) {
2870 struct sk_buff *fskb2 = fskb;
2871
2872 BUG_ON(pos + fskb->len != offset + len);
2873
2874 pos += fskb->len;
2875 fskb = fskb->next;
2876
2877 if (fskb2->next) {
2878 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2879 if (!fskb2)
2880 goto err;
2881 } else
2882 skb_get(fskb2);
2883
2884 SKB_FRAG_ASSERT(nskb);
2885 skb_shinfo(nskb)->frag_list = fskb2;
2886 }
2887
2888 skip_fraglist:
2889 nskb->data_len = len - hsize;
2890 nskb->len += nskb->data_len;
2891 nskb->truesize += nskb->data_len;
2892
2893 perform_csum_check:
2894 if (!csum) {
2895 nskb->csum = skb_checksum(nskb, doffset,
2896 nskb->len - doffset, 0);
2897 nskb->ip_summed = CHECKSUM_NONE;
2898 }
2899 } while ((offset += len) < skb->len);
2900
2901 return segs;
2902
2903 err:
2904 while ((skb = segs)) {
2905 segs = skb->next;
2906 kfree_skb(skb);
2907 }
2908 return ERR_PTR(err);
2909 }
2910 EXPORT_SYMBOL_GPL(skb_segment);
2911
2912 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2913 {
2914 struct sk_buff *p = *head;
2915 struct sk_buff *nskb;
2916 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2917 struct skb_shared_info *pinfo = skb_shinfo(p);
2918 unsigned int headroom;
2919 unsigned int len = skb_gro_len(skb);
2920 unsigned int offset = skb_gro_offset(skb);
2921 unsigned int headlen = skb_headlen(skb);
2922 unsigned int delta_truesize;
2923
2924 if (p->len + len >= 65536)
2925 return -E2BIG;
2926
2927 if (pinfo->frag_list)
2928 goto merge;
2929 else if (headlen <= offset) {
2930 skb_frag_t *frag;
2931 skb_frag_t *frag2;
2932 int i = skbinfo->nr_frags;
2933 int nr_frags = pinfo->nr_frags + i;
2934
2935 offset -= headlen;
2936
2937 if (nr_frags > MAX_SKB_FRAGS)
2938 return -E2BIG;
2939
2940 pinfo->nr_frags = nr_frags;
2941 skbinfo->nr_frags = 0;
2942
2943 frag = pinfo->frags + nr_frags;
2944 frag2 = skbinfo->frags + i;
2945 do {
2946 *--frag = *--frag2;
2947 } while (--i);
2948
2949 frag->page_offset += offset;
2950 skb_frag_size_sub(frag, offset);
2951
2952 /* all fragments truesize : remove (head size + sk_buff) */
2953 delta_truesize = skb->truesize -
2954 SKB_TRUESIZE(skb_end_offset(skb));
2955
2956 skb->truesize -= skb->data_len;
2957 skb->len -= skb->data_len;
2958 skb->data_len = 0;
2959
2960 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
2961 goto done;
2962 } else if (skb->head_frag) {
2963 int nr_frags = pinfo->nr_frags;
2964 skb_frag_t *frag = pinfo->frags + nr_frags;
2965 struct page *page = virt_to_head_page(skb->head);
2966 unsigned int first_size = headlen - offset;
2967 unsigned int first_offset;
2968
2969 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2970 return -E2BIG;
2971
2972 first_offset = skb->data -
2973 (unsigned char *)page_address(page) +
2974 offset;
2975
2976 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2977
2978 frag->page.p = page;
2979 frag->page_offset = first_offset;
2980 skb_frag_size_set(frag, first_size);
2981
2982 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2983 /* We dont need to clear skbinfo->nr_frags here */
2984
2985 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
2986 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2987 goto done;
2988 } else if (skb_gro_len(p) != pinfo->gso_size)
2989 return -E2BIG;
2990
2991 headroom = skb_headroom(p);
2992 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2993 if (unlikely(!nskb))
2994 return -ENOMEM;
2995
2996 __copy_skb_header(nskb, p);
2997 nskb->mac_len = p->mac_len;
2998
2999 skb_reserve(nskb, headroom);
3000 __skb_put(nskb, skb_gro_offset(p));
3001
3002 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3003 skb_set_network_header(nskb, skb_network_offset(p));
3004 skb_set_transport_header(nskb, skb_transport_offset(p));
3005
3006 __skb_pull(p, skb_gro_offset(p));
3007 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3008 p->data - skb_mac_header(p));
3009
3010 skb_shinfo(nskb)->frag_list = p;
3011 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3012 pinfo->gso_size = 0;
3013 skb_header_release(p);
3014 NAPI_GRO_CB(nskb)->last = p;
3015
3016 nskb->data_len += p->len;
3017 nskb->truesize += p->truesize;
3018 nskb->len += p->len;
3019
3020 *head = nskb;
3021 nskb->next = p->next;
3022 p->next = NULL;
3023
3024 p = nskb;
3025
3026 merge:
3027 delta_truesize = skb->truesize;
3028 if (offset > headlen) {
3029 unsigned int eat = offset - headlen;
3030
3031 skbinfo->frags[0].page_offset += eat;
3032 skb_frag_size_sub(&skbinfo->frags[0], eat);
3033 skb->data_len -= eat;
3034 skb->len -= eat;
3035 offset = headlen;
3036 }
3037
3038 __skb_pull(skb, offset);
3039
3040 NAPI_GRO_CB(p)->last->next = skb;
3041 NAPI_GRO_CB(p)->last = skb;
3042 skb_header_release(skb);
3043
3044 done:
3045 NAPI_GRO_CB(p)->count++;
3046 p->data_len += len;
3047 p->truesize += delta_truesize;
3048 p->len += len;
3049
3050 NAPI_GRO_CB(skb)->same_flow = 1;
3051 return 0;
3052 }
3053 EXPORT_SYMBOL_GPL(skb_gro_receive);
3054
3055 void __init skb_init(void)
3056 {
3057 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3058 sizeof(struct sk_buff),
3059 0,
3060 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3061 NULL);
3062 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3063 (2*sizeof(struct sk_buff)) +
3064 sizeof(atomic_t),
3065 0,
3066 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3067 NULL);
3068 }
3069
3070 /**
3071 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3072 * @skb: Socket buffer containing the buffers to be mapped
3073 * @sg: The scatter-gather list to map into
3074 * @offset: The offset into the buffer's contents to start mapping
3075 * @len: Length of buffer space to be mapped
3076 *
3077 * Fill the specified scatter-gather list with mappings/pointers into a
3078 * region of the buffer space attached to a socket buffer.
3079 */
3080 static int
3081 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3082 {
3083 int start = skb_headlen(skb);
3084 int i, copy = start - offset;
3085 struct sk_buff *frag_iter;
3086 int elt = 0;
3087
3088 if (copy > 0) {
3089 if (copy > len)
3090 copy = len;
3091 sg_set_buf(sg, skb->data + offset, copy);
3092 elt++;
3093 if ((len -= copy) == 0)
3094 return elt;
3095 offset += copy;
3096 }
3097
3098 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3099 int end;
3100
3101 WARN_ON(start > offset + len);
3102
3103 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3104 if ((copy = end - offset) > 0) {
3105 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3106
3107 if (copy > len)
3108 copy = len;
3109 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3110 frag->page_offset+offset-start);
3111 elt++;
3112 if (!(len -= copy))
3113 return elt;
3114 offset += copy;
3115 }
3116 start = end;
3117 }
3118
3119 skb_walk_frags(skb, frag_iter) {
3120 int end;
3121
3122 WARN_ON(start > offset + len);
3123
3124 end = start + frag_iter->len;
3125 if ((copy = end - offset) > 0) {
3126 if (copy > len)
3127 copy = len;
3128 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3129 copy);
3130 if ((len -= copy) == 0)
3131 return elt;
3132 offset += copy;
3133 }
3134 start = end;
3135 }
3136 BUG_ON(len);
3137 return elt;
3138 }
3139
3140 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3141 {
3142 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3143
3144 sg_mark_end(&sg[nsg - 1]);
3145
3146 return nsg;
3147 }
3148 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3149
3150 /**
3151 * skb_cow_data - Check that a socket buffer's data buffers are writable
3152 * @skb: The socket buffer to check.
3153 * @tailbits: Amount of trailing space to be added
3154 * @trailer: Returned pointer to the skb where the @tailbits space begins
3155 *
3156 * Make sure that the data buffers attached to a socket buffer are
3157 * writable. If they are not, private copies are made of the data buffers
3158 * and the socket buffer is set to use these instead.
3159 *
3160 * If @tailbits is given, make sure that there is space to write @tailbits
3161 * bytes of data beyond current end of socket buffer. @trailer will be
3162 * set to point to the skb in which this space begins.
3163 *
3164 * The number of scatterlist elements required to completely map the
3165 * COW'd and extended socket buffer will be returned.
3166 */
3167 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3168 {
3169 int copyflag;
3170 int elt;
3171 struct sk_buff *skb1, **skb_p;
3172
3173 /* If skb is cloned or its head is paged, reallocate
3174 * head pulling out all the pages (pages are considered not writable
3175 * at the moment even if they are anonymous).
3176 */
3177 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3178 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3179 return -ENOMEM;
3180
3181 /* Easy case. Most of packets will go this way. */
3182 if (!skb_has_frag_list(skb)) {
3183 /* A little of trouble, not enough of space for trailer.
3184 * This should not happen, when stack is tuned to generate
3185 * good frames. OK, on miss we reallocate and reserve even more
3186 * space, 128 bytes is fair. */
3187
3188 if (skb_tailroom(skb) < tailbits &&
3189 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3190 return -ENOMEM;
3191
3192 /* Voila! */
3193 *trailer = skb;
3194 return 1;
3195 }
3196
3197 /* Misery. We are in troubles, going to mincer fragments... */
3198
3199 elt = 1;
3200 skb_p = &skb_shinfo(skb)->frag_list;
3201 copyflag = 0;
3202
3203 while ((skb1 = *skb_p) != NULL) {
3204 int ntail = 0;
3205
3206 /* The fragment is partially pulled by someone,
3207 * this can happen on input. Copy it and everything
3208 * after it. */
3209
3210 if (skb_shared(skb1))
3211 copyflag = 1;
3212
3213 /* If the skb is the last, worry about trailer. */
3214
3215 if (skb1->next == NULL && tailbits) {
3216 if (skb_shinfo(skb1)->nr_frags ||
3217 skb_has_frag_list(skb1) ||
3218 skb_tailroom(skb1) < tailbits)
3219 ntail = tailbits + 128;
3220 }
3221
3222 if (copyflag ||
3223 skb_cloned(skb1) ||
3224 ntail ||
3225 skb_shinfo(skb1)->nr_frags ||
3226 skb_has_frag_list(skb1)) {
3227 struct sk_buff *skb2;
3228
3229 /* Fuck, we are miserable poor guys... */
3230 if (ntail == 0)
3231 skb2 = skb_copy(skb1, GFP_ATOMIC);
3232 else
3233 skb2 = skb_copy_expand(skb1,
3234 skb_headroom(skb1),
3235 ntail,
3236 GFP_ATOMIC);
3237 if (unlikely(skb2 == NULL))
3238 return -ENOMEM;
3239
3240 if (skb1->sk)
3241 skb_set_owner_w(skb2, skb1->sk);
3242
3243 /* Looking around. Are we still alive?
3244 * OK, link new skb, drop old one */
3245
3246 skb2->next = skb1->next;
3247 *skb_p = skb2;
3248 kfree_skb(skb1);
3249 skb1 = skb2;
3250 }
3251 elt++;
3252 *trailer = skb1;
3253 skb_p = &skb1->next;
3254 }
3255
3256 return elt;
3257 }
3258 EXPORT_SYMBOL_GPL(skb_cow_data);
3259
3260 static void sock_rmem_free(struct sk_buff *skb)
3261 {
3262 struct sock *sk = skb->sk;
3263
3264 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3265 }
3266
3267 /*
3268 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3269 */
3270 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3271 {
3272 int len = skb->len;
3273
3274 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3275 (unsigned int)sk->sk_rcvbuf)
3276 return -ENOMEM;
3277
3278 skb_orphan(skb);
3279 skb->sk = sk;
3280 skb->destructor = sock_rmem_free;
3281 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3282
3283 /* before exiting rcu section, make sure dst is refcounted */
3284 skb_dst_force(skb);
3285
3286 skb_queue_tail(&sk->sk_error_queue, skb);
3287 if (!sock_flag(sk, SOCK_DEAD))
3288 sk->sk_data_ready(sk, len);
3289 return 0;
3290 }
3291 EXPORT_SYMBOL(sock_queue_err_skb);
3292
3293 void skb_tstamp_tx(struct sk_buff *orig_skb,
3294 struct skb_shared_hwtstamps *hwtstamps)
3295 {
3296 struct sock *sk = orig_skb->sk;
3297 struct sock_exterr_skb *serr;
3298 struct sk_buff *skb;
3299 int err;
3300
3301 if (!sk)
3302 return;
3303
3304 if (hwtstamps) {
3305 *skb_hwtstamps(orig_skb) =
3306 *hwtstamps;
3307 } else {
3308 /*
3309 * no hardware time stamps available,
3310 * so keep the shared tx_flags and only
3311 * store software time stamp
3312 */
3313 orig_skb->tstamp = ktime_get_real();
3314 }
3315
3316 skb = skb_clone(orig_skb, GFP_ATOMIC);
3317 if (!skb)
3318 return;
3319
3320 serr = SKB_EXT_ERR(skb);
3321 memset(serr, 0, sizeof(*serr));
3322 serr->ee.ee_errno = ENOMSG;
3323 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3324
3325 err = sock_queue_err_skb(sk, skb);
3326
3327 if (err)
3328 kfree_skb(skb);
3329 }
3330 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3331
3332 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3333 {
3334 struct sock *sk = skb->sk;
3335 struct sock_exterr_skb *serr;
3336 int err;
3337
3338 skb->wifi_acked_valid = 1;
3339 skb->wifi_acked = acked;
3340
3341 serr = SKB_EXT_ERR(skb);
3342 memset(serr, 0, sizeof(*serr));
3343 serr->ee.ee_errno = ENOMSG;
3344 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3345
3346 err = sock_queue_err_skb(sk, skb);
3347 if (err)
3348 kfree_skb(skb);
3349 }
3350 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3351
3352
3353 /**
3354 * skb_partial_csum_set - set up and verify partial csum values for packet
3355 * @skb: the skb to set
3356 * @start: the number of bytes after skb->data to start checksumming.
3357 * @off: the offset from start to place the checksum.
3358 *
3359 * For untrusted partially-checksummed packets, we need to make sure the values
3360 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3361 *
3362 * This function checks and sets those values and skb->ip_summed: if this
3363 * returns false you should drop the packet.
3364 */
3365 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3366 {
3367 if (unlikely(start > skb_headlen(skb)) ||
3368 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3369 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3370 start, off, skb_headlen(skb));
3371 return false;
3372 }
3373 skb->ip_summed = CHECKSUM_PARTIAL;
3374 skb->csum_start = skb_headroom(skb) + start;
3375 skb->csum_offset = off;
3376 skb_set_transport_header(skb, start);
3377 return true;
3378 }
3379 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3380
3381 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3382 {
3383 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3384 skb->dev->name);
3385 }
3386 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3387
3388 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3389 {
3390 if (head_stolen) {
3391 skb_release_head_state(skb);
3392 kmem_cache_free(skbuff_head_cache, skb);
3393 } else {
3394 __kfree_skb(skb);
3395 }
3396 }
3397 EXPORT_SYMBOL(kfree_skb_partial);
3398
3399 /**
3400 * skb_try_coalesce - try to merge skb to prior one
3401 * @to: prior buffer
3402 * @from: buffer to add
3403 * @fragstolen: pointer to boolean
3404 * @delta_truesize: how much more was allocated than was requested
3405 */
3406 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3407 bool *fragstolen, int *delta_truesize)
3408 {
3409 int i, delta, len = from->len;
3410
3411 *fragstolen = false;
3412
3413 if (skb_cloned(to))
3414 return false;
3415
3416 if (len <= skb_tailroom(to)) {
3417 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3418 *delta_truesize = 0;
3419 return true;
3420 }
3421
3422 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3423 return false;
3424
3425 if (skb_headlen(from) != 0) {
3426 struct page *page;
3427 unsigned int offset;
3428
3429 if (skb_shinfo(to)->nr_frags +
3430 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3431 return false;
3432
3433 if (skb_head_is_locked(from))
3434 return false;
3435
3436 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3437
3438 page = virt_to_head_page(from->head);
3439 offset = from->data - (unsigned char *)page_address(page);
3440
3441 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3442 page, offset, skb_headlen(from));
3443 *fragstolen = true;
3444 } else {
3445 if (skb_shinfo(to)->nr_frags +
3446 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3447 return false;
3448
3449 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3450 }
3451
3452 WARN_ON_ONCE(delta < len);
3453
3454 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3455 skb_shinfo(from)->frags,
3456 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3457 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3458
3459 if (!skb_cloned(from))
3460 skb_shinfo(from)->nr_frags = 0;
3461
3462 /* if the skb is not cloned this does nothing
3463 * since we set nr_frags to 0.
3464 */
3465 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3466 skb_frag_ref(from, i);
3467
3468 to->truesize += delta;
3469 to->len += len;
3470 to->data_len += len;
3471
3472 *delta_truesize = delta;
3473 return true;
3474 }
3475 EXPORT_SYMBOL(skb_try_coalesce);
3476
3477 /**
3478 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3479 *
3480 * @skb: GSO skb
3481 *
3482 * skb_gso_transport_seglen is used to determine the real size of the
3483 * individual segments, including Layer4 headers (TCP/UDP).
3484 *
3485 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3486 */
3487 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3488 {
3489 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3490
3491 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3492 return tcp_hdrlen(skb) + shinfo->gso_size;
3493
3494 /* UFO sets gso_size to the size of the fragmentation
3495 * payload, i.e. the size of the L4 (UDP) header is already
3496 * accounted for.
3497 */
3498 return shinfo->gso_size;
3499 }
3500 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);