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