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