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