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