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