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