Branch data Line data Source code
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/kmemcheck.h>
45 : : #include <linux/mm.h>
46 : : #include <linux/interrupt.h>
47 : : #include <linux/in.h>
48 : : #include <linux/inet.h>
49 : : #include <linux/slab.h>
50 : : #include <linux/tcp.h>
51 : : #include <linux/udp.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 : :
66 : : #include <net/protocol.h>
67 : : #include <net/dst.h>
68 : : #include <net/sock.h>
69 : : #include <net/checksum.h>
70 : : #include <net/ip6_checksum.h>
71 : : #include <net/xfrm.h>
72 : :
73 : : #include <asm/uaccess.h>
74 : : #include <trace/events/skb.h>
75 : : #include <linux/highmem.h>
76 : :
77 : : struct kmem_cache *skbuff_head_cache __read_mostly;
78 : : static struct kmem_cache *skbuff_fclone_cache __read_mostly;
79 : :
80 : : /**
81 : : * skb_panic - private function for out-of-line support
82 : : * @skb: buffer
83 : : * @sz: size
84 : : * @addr: address
85 : : * @msg: skb_over_panic or skb_under_panic
86 : : *
87 : : * Out-of-line support for skb_put() and skb_push().
88 : : * Called via the wrapper skb_over_panic() or skb_under_panic().
89 : : * Keep out of line to prevent kernel bloat.
90 : : * __builtin_return_address is not used because it is not always reliable.
91 : : */
92 : 0 : static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
93 : : const char msg[])
94 : : {
95 [ # # ]: 0 : pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
96 : : msg, addr, skb->len, sz, skb->head, skb->data,
97 : : (unsigned long)skb->tail, (unsigned long)skb->end,
98 : : skb->dev ? skb->dev->name : "<NULL>");
99 : 0 : BUG();
100 : : }
101 : :
102 : 0 : static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
103 : : {
104 : 0 : skb_panic(skb, sz, addr, __func__);
105 : : }
106 : :
107 : 0 : static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
108 : : {
109 : 0 : skb_panic(skb, sz, addr, __func__);
110 : : }
111 : :
112 : : /*
113 : : * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
114 : : * the caller if emergency pfmemalloc reserves are being used. If it is and
115 : : * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
116 : : * may be used. Otherwise, the packet data may be discarded until enough
117 : : * memory is free
118 : : */
119 : : #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
120 : : __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
121 : :
122 : 1211147 : static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
123 : : unsigned long ip, bool *pfmemalloc)
124 : : {
125 : : void *obj;
126 : : bool ret_pfmemalloc = false;
127 : :
128 : : /*
129 : : * Try a regular allocation, when that fails and we're not entitled
130 : : * to the reserves, fail.
131 : : */
132 : 1211147 : obj = kmalloc_node_track_caller(size,
133 : : flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
134 : : node);
135 [ - + ][ # # ]: 1308088 : if (obj || !(gfp_pfmemalloc_allowed(flags)))
136 : : goto out;
137 : :
138 : : /* Try again but now we are using pfmemalloc reserves */
139 : : ret_pfmemalloc = true;
140 : 0 : obj = kmalloc_node_track_caller(size, flags, node);
141 : :
142 : : out:
143 [ + - ]: 2462404 : if (pfmemalloc)
144 : 1251257 : *pfmemalloc = ret_pfmemalloc;
145 : :
146 : 1251257 : return obj;
147 : : }
148 : :
149 : : /* Allocate a new skbuff. We do this ourselves so we can fill in a few
150 : : * 'private' fields and also do memory statistics to find all the
151 : : * [BEEP] leaks.
152 : : *
153 : : */
154 : :
155 : 0 : struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
156 : : {
157 : : struct sk_buff *skb;
158 : :
159 : : /* Get the HEAD */
160 : 0 : skb = kmem_cache_alloc_node(skbuff_head_cache,
161 : : gfp_mask & ~__GFP_DMA, node);
162 [ # # ]: 0 : if (!skb)
163 : : goto out;
164 : :
165 : : /*
166 : : * Only clear those fields we need to clear, not those that we will
167 : : * actually initialise below. Hence, don't put any more fields after
168 : : * the tail pointer in struct sk_buff!
169 : : */
170 : 0 : memset(skb, 0, offsetof(struct sk_buff, tail));
171 : 0 : skb->head = NULL;
172 : 0 : skb->truesize = sizeof(struct sk_buff);
173 : 0 : atomic_set(&skb->users, 1);
174 : :
175 : 0 : skb->mac_header = (typeof(skb->mac_header))~0U;
176 : : out:
177 : 0 : return skb;
178 : : }
179 : :
180 : : /**
181 : : * __alloc_skb - allocate a network buffer
182 : : * @size: size to allocate
183 : : * @gfp_mask: allocation mask
184 : : * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
185 : : * instead of head cache and allocate a cloned (child) skb.
186 : : * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
187 : : * allocations in case the data is required for writeback
188 : : * @node: numa node to allocate memory on
189 : : *
190 : : * Allocate a new &sk_buff. The returned buffer has no headroom and a
191 : : * tail room of at least size bytes. The object has a reference count
192 : : * of one. The return is the buffer. On a failure the return is %NULL.
193 : : *
194 : : * Buffers may only be allocated from interrupts using a @gfp_mask of
195 : : * %GFP_ATOMIC.
196 : : */
197 : 0 : struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
198 : : int flags, int node)
199 : : {
200 : : struct kmem_cache *cache;
201 : : struct skb_shared_info *shinfo;
202 : : struct sk_buff *skb;
203 : : u8 *data;
204 : : bool pfmemalloc;
205 : :
206 : 1275614 : cache = (flags & SKB_ALLOC_FCLONE)
207 [ + + ]: 1275614 : ? skbuff_fclone_cache : skbuff_head_cache;
208 : :
209 [ - + ][ # # ]: 1275614 : if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
210 : 0 : gfp_mask |= __GFP_MEMALLOC;
211 : :
212 : : /* Get the HEAD */
213 : 0 : skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
214 [ + ]: 1278109 : if (!skb)
215 : : goto out;
216 : : prefetchw(skb);
217 : :
218 : : /* We do our best to align skb_shared_info on a separate cache
219 : : * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
220 : : * aligned memory blocks, unless SLUB/SLAB debug is enabled.
221 : : * Both skb->head and skb_shared_info are cache line aligned.
222 : : */
223 : 1308742 : size = SKB_DATA_ALIGN(size);
224 : 1308742 : size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
225 : 1308742 : data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
226 [ + - ]: 1259281 : if (!data)
227 : : goto nodata;
228 : : /* kmalloc(size) might give us more room than requested.
229 : : * Put skb_shared_info exactly at the end of allocated zone,
230 : : * to allow max possible filling before reallocation.
231 : : */
232 : 1259281 : size = SKB_WITH_OVERHEAD(ksize(data));
233 : 1260463 : prefetchw(data + size);
234 : :
235 : : /*
236 : : * Only clear those fields we need to clear, not those that we will
237 : : * actually initialise below. Hence, don't put any more fields after
238 : : * the tail pointer in struct sk_buff!
239 : : */
240 : 1244703 : memset(skb, 0, offsetof(struct sk_buff, tail));
241 : : /* Account for allocated memory : skb + skb->head */
242 : 1296831 : skb->truesize = SKB_TRUESIZE(size);
243 : 1296831 : skb->pfmemalloc = pfmemalloc;
244 : 1296831 : atomic_set(&skb->users, 1);
245 : 1296831 : skb->head = data;
246 : 1296831 : skb->data = data;
247 : : skb_reset_tail_pointer(skb);
248 : 1296831 : skb->end = skb->tail + size;
249 : 1296831 : skb->mac_header = (typeof(skb->mac_header))~0U;
250 : 1296831 : skb->transport_header = (typeof(skb->transport_header))~0U;
251 : :
252 : : /* make sure we initialize shinfo sequentially */
253 : : shinfo = skb_shinfo(skb);
254 : 1296831 : memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
255 : 1297189 : atomic_set(&shinfo->dataref, 1);
256 : : kmemcheck_annotate_variable(shinfo->destructor_arg);
257 : :
258 [ + + ]: 1297189 : if (flags & SKB_ALLOC_FCLONE) {
259 : : struct sk_buff *child = skb + 1;
260 : : atomic_t *fclone_ref = (atomic_t *) (child + 1);
261 : :
262 : : kmemcheck_annotate_bitfield(child, flags1);
263 : : kmemcheck_annotate_bitfield(child, flags2);
264 : 23402 : skb->fclone = SKB_FCLONE_ORIG;
265 : 23402 : atomic_set(fclone_ref, 1);
266 : :
267 : 23402 : child->fclone = SKB_FCLONE_UNAVAILABLE;
268 : 23402 : child->pfmemalloc = pfmemalloc;
269 : : }
270 : : out:
271 : 1297086 : return skb;
272 : : nodata:
273 : 0 : kmem_cache_free(cache, skb);
274 : : skb = NULL;
275 : 0 : goto out;
276 : : }
277 : : EXPORT_SYMBOL(__alloc_skb);
278 : :
279 : : /**
280 : : * build_skb - build a network buffer
281 : : * @data: data buffer provided by caller
282 : : * @frag_size: size of fragment, or 0 if head was kmalloced
283 : : *
284 : : * Allocate a new &sk_buff. Caller provides space holding head and
285 : : * skb_shared_info. @data must have been allocated by kmalloc() only if
286 : : * @frag_size is 0, otherwise data should come from the page allocator.
287 : : * The return is the new skb buffer.
288 : : * On a failure the return is %NULL, and @data is not freed.
289 : : * Notes :
290 : : * Before IO, driver allocates only data buffer where NIC put incoming frame
291 : : * Driver should add room at head (NET_SKB_PAD) and
292 : : * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
293 : : * After IO, driver calls build_skb(), to allocate sk_buff and populate it
294 : : * before giving packet to stack.
295 : : * RX rings only contains data buffers, not full skbs.
296 : : */
297 : 0 : struct sk_buff *build_skb(void *data, unsigned int frag_size)
298 : : {
299 : : struct skb_shared_info *shinfo;
300 : : struct sk_buff *skb;
301 [ - + ]: 21389 : unsigned int size = frag_size ? : ksize(data);
302 : :
303 : 21389 : skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
304 [ + - ]: 21389 : if (!skb)
305 : : return NULL;
306 : :
307 : 21389 : size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
308 : :
309 : 21389 : memset(skb, 0, offsetof(struct sk_buff, tail));
310 : 21389 : skb->truesize = SKB_TRUESIZE(size);
311 : 21389 : skb->head_frag = frag_size != 0;
312 : 21389 : atomic_set(&skb->users, 1);
313 : 21389 : skb->head = data;
314 : 21389 : skb->data = data;
315 : : skb_reset_tail_pointer(skb);
316 : 21389 : skb->end = skb->tail + size;
317 : 21389 : skb->mac_header = (typeof(skb->mac_header))~0U;
318 : 21389 : skb->transport_header = (typeof(skb->transport_header))~0U;
319 : :
320 : : /* make sure we initialize shinfo sequentially */
321 : : shinfo = skb_shinfo(skb);
322 : 21389 : memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
323 : 21389 : atomic_set(&shinfo->dataref, 1);
324 : : kmemcheck_annotate_variable(shinfo->destructor_arg);
325 : :
326 : 21389 : return skb;
327 : : }
328 : : EXPORT_SYMBOL(build_skb);
329 : :
330 : : struct netdev_alloc_cache {
331 : : struct page_frag frag;
332 : : /* we maintain a pagecount bias, so that we dont dirty cache line
333 : : * containing page->_count every time we allocate a fragment.
334 : : */
335 : : unsigned int pagecnt_bias;
336 : : };
337 : : static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
338 : :
339 : 0 : static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
340 : : {
341 : : struct netdev_alloc_cache *nc;
342 : : void *data = NULL;
343 : : int order;
344 : : unsigned long flags;
345 : :
346 : : local_irq_save(flags);
347 : 42778 : nc = &__get_cpu_var(netdev_alloc_cache);
348 [ - + ]: 21402 : if (unlikely(!nc->frag.page)) {
349 : : refill:
350 : : for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
351 : : gfp_t gfp = gfp_mask;
352 : :
353 [ + - ]: 13 : if (order)
354 : 13 : gfp |= __GFP_COMP | __GFP_NOWARN;
355 : 0 : nc->frag.page = alloc_pages(gfp, order);
356 [ - + ]: 13 : if (likely(nc->frag.page))
357 : : break;
358 [ # # ]: 0 : if (--order < 0)
359 : : goto end;
360 : : }
361 : 13 : nc->frag.size = PAGE_SIZE << order;
362 : : recycle:
363 : 253 : atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
364 : 253 : nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
365 : 253 : nc->frag.offset = 0;
366 : : }
367 : :
368 [ + + ]: 21642 : if (nc->frag.offset + fragsz > nc->frag.size) {
369 : : /* avoid unnecessary locked operations if possible */
370 [ + + + - ]: 266 : if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
371 : 13 : atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
372 : : goto recycle;
373 : : goto refill;
374 : : }
375 : :
376 : 21389 : data = page_address(nc->frag.page) + nc->frag.offset;
377 : 21389 : nc->frag.offset += fragsz;
378 : 21389 : nc->pagecnt_bias--;
379 : : end:
380 [ - + ]: 21389 : local_irq_restore(flags);
381 : 21389 : return data;
382 : : }
383 : :
384 : : /**
385 : : * netdev_alloc_frag - allocate a page fragment
386 : : * @fragsz: fragment size
387 : : *
388 : : * Allocates a frag from a page for receive buffer.
389 : : * Uses GFP_ATOMIC allocations.
390 : : */
391 : 0 : void *netdev_alloc_frag(unsigned int fragsz)
392 : : {
393 : 0 : return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
394 : : }
395 : : EXPORT_SYMBOL(netdev_alloc_frag);
396 : :
397 : : /**
398 : : * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
399 : : * @dev: network device to receive on
400 : : * @length: length to allocate
401 : : * @gfp_mask: get_free_pages mask, passed to alloc_skb
402 : : *
403 : : * Allocate a new &sk_buff and assign it a usage count of one. The
404 : : * buffer has unspecified headroom built in. Users should allocate
405 : : * the headroom they think they need without accounting for the
406 : : * built in space. The built in space is used for optimisations.
407 : : *
408 : : * %NULL is returned if there is no free memory.
409 : : */
410 : 0 : struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
411 : : unsigned int length, gfp_t gfp_mask)
412 : : {
413 : : struct sk_buff *skb = NULL;
414 : 21389 : unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
415 : : SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
416 : :
417 [ + - ][ + - ]: 21389 : if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
418 : : void *data;
419 : :
420 [ - + ]: 21389 : if (sk_memalloc_socks())
421 : 0 : gfp_mask |= __GFP_MEMALLOC;
422 : :
423 : 21389 : data = __netdev_alloc_frag(fragsz, gfp_mask);
424 : :
425 [ + - ]: 21389 : if (likely(data)) {
426 : 21389 : skb = build_skb(data, fragsz);
427 [ - + ]: 21389 : if (unlikely(!skb))
428 : 0 : put_page(virt_to_head_page(data));
429 : : }
430 : : } else {
431 : 0 : skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
432 : : SKB_ALLOC_RX, NUMA_NO_NODE);
433 : : }
434 [ + - ]: 21389 : if (likely(skb)) {
435 : : skb_reserve(skb, NET_SKB_PAD);
436 : 21389 : skb->dev = dev;
437 : : }
438 : 21389 : return skb;
439 : : }
440 : : EXPORT_SYMBOL(__netdev_alloc_skb);
441 : :
442 : 0 : void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
443 : : int size, unsigned int truesize)
444 : : {
445 : : skb_fill_page_desc(skb, i, page, off, size);
446 : 0 : skb->len += size;
447 : 0 : skb->data_len += size;
448 : 0 : skb->truesize += truesize;
449 : 0 : }
450 : : EXPORT_SYMBOL(skb_add_rx_frag);
451 : :
452 : 0 : void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
453 : : unsigned int truesize)
454 : : {
455 : 0 : skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
456 : :
457 : : skb_frag_size_add(frag, size);
458 : 0 : skb->len += size;
459 : 0 : skb->data_len += size;
460 : 0 : skb->truesize += truesize;
461 : 0 : }
462 : : EXPORT_SYMBOL(skb_coalesce_rx_frag);
463 : :
464 : 0 : static void skb_drop_list(struct sk_buff **listp)
465 : : {
466 : 0 : kfree_skb_list(*listp);
467 : 0 : *listp = NULL;
468 : 0 : }
469 : :
470 : : static inline void skb_drop_fraglist(struct sk_buff *skb)
471 : : {
472 : 0 : skb_drop_list(&skb_shinfo(skb)->frag_list);
473 : : }
474 : :
475 : 0 : static void skb_clone_fraglist(struct sk_buff *skb)
476 : : {
477 : : struct sk_buff *list;
478 : :
479 [ # # ]: 0 : skb_walk_frags(skb, list)
480 : : skb_get(list);
481 : 0 : }
482 : :
483 : 0 : static void skb_free_head(struct sk_buff *skb)
484 : : {
485 [ + + ]: 1282427 : if (skb->head_frag)
486 : 21389 : put_page(virt_to_head_page(skb->head));
487 : : else
488 : 1261038 : kfree(skb->head);
489 : 1323049 : }
490 : :
491 : 0 : static void skb_release_data(struct sk_buff *skb)
492 : : {
493 [ + + + ]: 1405671 : if (!skb->cloned ||
494 [ + + ]: 70816 : !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
495 : : &skb_shinfo(skb)->dataref)) {
496 [ + + ]: 2625306 : if (skb_shinfo(skb)->nr_frags) {
497 : : int i;
498 [ + + ]: 32 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
499 : : skb_frag_unref(skb, i);
500 : : }
501 : :
502 : : /*
503 : : * If skb buf is from userspace, we need to notify the caller
504 : : * the lower device DMA has done;
505 : : */
506 [ - + ]: 1290451 : if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
507 : : struct ubuf_info *uarg;
508 : :
509 : 0 : uarg = skb_shinfo(skb)->destructor_arg;
510 [ # # ]: 0 : if (uarg->callback)
511 : 0 : uarg->callback(uarg, true);
512 : : }
513 : :
514 [ - + ]: 1290451 : if (skb_has_frag_list(skb))
515 : : skb_drop_fraglist(skb);
516 : :
517 : 1290451 : skb_free_head(skb);
518 : : }
519 : 30311 : }
520 : :
521 : : /*
522 : : * Free an skbuff by memory without cleaning the state.
523 : : */
524 : 0 : static void kfree_skbmem(struct sk_buff *skb)
525 : : {
526 : : struct sk_buff *other;
527 : : atomic_t *fclone_ref;
528 : :
529 [ + + + ]: 1346679 : switch (skb->fclone) {
530 : : case SKB_FCLONE_UNAVAILABLE:
531 : 1325781 : kmem_cache_free(skbuff_head_cache, skb);
532 : 1335055 : break;
533 : :
534 : : case SKB_FCLONE_ORIG:
535 : 23402 : fclone_ref = (atomic_t *) (skb + 2);
536 [ + + ]: 23402 : if (atomic_dec_and_test(fclone_ref))
537 : 23385 : kmem_cache_free(skbuff_fclone_cache, skb);
538 : : break;
539 : :
540 : : case SKB_FCLONE_CLONE:
541 : 23490 : fclone_ref = (atomic_t *) (skb + 1);
542 : 23490 : other = skb - 1;
543 : :
544 : : /* The clone portion is available for
545 : : * fast-cloning again.
546 : : */
547 : 23490 : skb->fclone = SKB_FCLONE_UNAVAILABLE;
548 : :
549 [ + + ]: 23490 : if (atomic_dec_and_test(fclone_ref))
550 : 17 : kmem_cache_free(skbuff_fclone_cache, other);
551 : : break;
552 : : }
553 : 9274 : }
554 : :
555 : 0 : static void skb_release_head_state(struct sk_buff *skb)
556 : : {
557 : : skb_dst_drop(skb);
558 : : #ifdef CONFIG_XFRM
559 : 1331630 : secpath_put(skb->sp);
560 : : #endif
561 [ + + ]: 2636363 : if (skb->destructor) {
562 [ - + ]: 1254497 : WARN_ON(in_irq());
563 : 1254497 : skb->destructor(skb);
564 : : }
565 : : #if IS_ENABLED(CONFIG_NF_CONNTRACK)
566 : 1334067 : nf_conntrack_put(skb->nfct);
567 : : #endif
568 : : #ifdef CONFIG_BRIDGE_NETFILTER
569 : 1334067 : nf_bridge_put(skb->nf_bridge);
570 : : #endif
571 : : /* XXX: IS this still necessary? - JHS */
572 : : #ifdef CONFIG_NET_SCHED
573 : : skb->tc_index = 0;
574 : : #ifdef CONFIG_NET_CLS_ACT
575 : : skb->tc_verd = 0;
576 : : #endif
577 : : #endif
578 : 1334067 : }
579 : :
580 : : /* Free everything but the sk_buff shell. */
581 : 0 : static void skb_release_all(struct sk_buff *skb)
582 : : {
583 : 1328744 : skb_release_head_state(skb);
584 [ + ]: 1324196 : if (likely(skb->head))
585 : 1325354 : skb_release_data(skb);
586 : 38427 : }
587 : :
588 : : /**
589 : : * __kfree_skb - private function
590 : : * @skb: buffer
591 : : *
592 : : * Free an sk_buff. Release anything attached to the buffer.
593 : : * Clean the state. This is an internal helper function. Users should
594 : : * always call kfree_skb
595 : : */
596 : :
597 : 0 : void __kfree_skb(struct sk_buff *skb)
598 : : {
599 : 1335890 : skb_release_all(skb);
600 : 1345943 : kfree_skbmem(skb);
601 : 45057 : }
602 : : EXPORT_SYMBOL(__kfree_skb);
603 : :
604 : : /**
605 : : * kfree_skb - free an sk_buff
606 : : * @skb: buffer to free
607 : : *
608 : : * Drop a reference to the buffer and free it if the usage count has
609 : : * hit zero.
610 : : */
611 : 0 : void kfree_skb(struct sk_buff *skb)
612 : : {
613 [ + ]: 471490 : if (unlikely(!skb))
614 : : return;
615 [ + + ]: 471864 : if (likely(atomic_read(&skb->users) == 1))
616 : 471854 : smp_rmb();
617 [ - + ]: 10 : else if (likely(!atomic_dec_and_test(&skb->users)))
618 : : return;
619 : 460506 : trace_kfree_skb(skb, __builtin_return_address(0));
620 : : __kfree_skb(skb);
621 : : }
622 : : EXPORT_SYMBOL(kfree_skb);
623 : :
624 : 0 : void kfree_skb_list(struct sk_buff *segs)
625 : : {
626 [ # # ][ # # ]: 0 : while (segs) {
[ # # ][ # # ]
627 : 0 : struct sk_buff *next = segs->next;
628 : :
629 : 0 : kfree_skb(segs);
630 : : segs = next;
631 : : }
632 : 0 : }
633 : : EXPORT_SYMBOL(kfree_skb_list);
634 : :
635 : : /**
636 : : * skb_tx_error - report an sk_buff xmit error
637 : : * @skb: buffer that triggered an error
638 : : *
639 : : * Report xmit error if a device callback is tracking this skb.
640 : : * skb must be freed afterwards.
641 : : */
642 : 0 : void skb_tx_error(struct sk_buff *skb)
643 : : {
644 [ # # ]: 0 : if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
645 : : struct ubuf_info *uarg;
646 : :
647 : 0 : uarg = skb_shinfo(skb)->destructor_arg;
648 [ # # ]: 0 : if (uarg->callback)
649 : 0 : uarg->callback(uarg, false);
650 : 0 : skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
651 : : }
652 : 0 : }
653 : : EXPORT_SYMBOL(skb_tx_error);
654 : :
655 : : /**
656 : : * consume_skb - free an skbuff
657 : : * @skb: buffer to free
658 : : *
659 : : * Drop a ref to the buffer and free it if the usage count has hit zero
660 : : * Functions identically to kfree_skb, but kfree_skb assumes that the frame
661 : : * is being dropped after a failure and notes that
662 : : */
663 : 0 : void consume_skb(struct sk_buff *skb)
664 : : {
665 [ + ]: 866156 : if (unlikely(!skb))
666 : : return;
667 [ + + ]: 867596 : if (likely(atomic_read(&skb->users) == 1))
668 : 842064 : smp_rmb();
669 [ - + ]: 25532 : else if (likely(!atomic_dec_and_test(&skb->users)))
670 : : return;
671 : : trace_consume_skb(skb);
672 : : __kfree_skb(skb);
673 : : }
674 : : EXPORT_SYMBOL(consume_skb);
675 : :
676 : 0 : static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
677 : : {
678 : 46873 : new->tstamp = old->tstamp;
679 : 46873 : new->dev = old->dev;
680 : 46873 : new->transport_header = old->transport_header;
681 : 46873 : new->network_header = old->network_header;
682 : 46873 : new->mac_header = old->mac_header;
683 : 46873 : new->inner_protocol = old->inner_protocol;
684 : 46873 : new->inner_transport_header = old->inner_transport_header;
685 : 46873 : new->inner_network_header = old->inner_network_header;
686 : 46873 : new->inner_mac_header = old->inner_mac_header;
687 : : skb_dst_copy(new, old);
688 : : skb_copy_hash(new, old);
689 : 93746 : new->ooo_okay = old->ooo_okay;
690 : 93746 : new->no_fcs = old->no_fcs;
691 : 93746 : new->encapsulation = old->encapsulation;
692 : : #ifdef CONFIG_XFRM
693 : 140619 : new->sp = secpath_get(old->sp);
694 : : #endif
695 : 46873 : memcpy(new->cb, old->cb, sizeof(old->cb));
696 : 46873 : new->csum = old->csum;
697 : 46873 : new->local_df = old->local_df;
698 : 46873 : new->pkt_type = old->pkt_type;
699 : 46873 : new->ip_summed = old->ip_summed;
700 : : skb_copy_queue_mapping(new, old);
701 : 46873 : new->priority = old->priority;
702 : : #if IS_ENABLED(CONFIG_IP_VS)
703 : : new->ipvs_property = old->ipvs_property;
704 : : #endif
705 : 46873 : new->pfmemalloc = old->pfmemalloc;
706 : 46873 : new->protocol = old->protocol;
707 : 46873 : new->mark = old->mark;
708 : 46873 : new->skb_iif = old->skb_iif;
709 : : __nf_copy(new, old);
710 : : #ifdef CONFIG_NET_SCHED
711 : : new->tc_index = old->tc_index;
712 : : #ifdef CONFIG_NET_CLS_ACT
713 : : new->tc_verd = old->tc_verd;
714 : : #endif
715 : : #endif
716 : 46873 : new->vlan_proto = old->vlan_proto;
717 : 46873 : new->vlan_tci = old->vlan_tci;
718 : :
719 : : skb_copy_secmark(new, old);
720 : :
721 : : #ifdef CONFIG_NET_RX_BUSY_POLL
722 : 46873 : new->napi_id = old->napi_id;
723 : : #endif
724 : 46873 : }
725 : :
726 : : /*
727 : : * You should not add any new code to this function. Add it to
728 : : * __copy_skb_header above instead.
729 : : */
730 : 0 : static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
731 : : {
732 : : #define C(x) n->x = skb->x
733 : :
734 : 46873 : n->next = n->prev = NULL;
735 : 46873 : n->sk = NULL;
736 : 46873 : __copy_skb_header(n, skb);
737 : :
738 : 46873 : C(len);
739 : 46873 : C(data_len);
740 : 46873 : C(mac_len);
741 [ + + ]: 93746 : n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
742 : 46873 : n->cloned = 1;
743 : 46873 : n->nohdr = 0;
744 : 46873 : n->destructor = NULL;
745 : 46873 : C(tail);
746 : 46873 : C(end);
747 : 46873 : C(head);
748 : 46873 : C(head_frag);
749 : 46873 : C(data);
750 : 46873 : C(truesize);
751 : 46873 : atomic_set(&n->users, 1);
752 : :
753 : 46873 : atomic_inc(&(skb_shinfo(skb)->dataref));
754 : 46872 : skb->cloned = 1;
755 : :
756 : 46872 : return n;
757 : : #undef C
758 : : }
759 : :
760 : : /**
761 : : * skb_morph - morph one skb into another
762 : : * @dst: the skb to receive the contents
763 : : * @src: the skb to supply the contents
764 : : *
765 : : * This is identical to skb_clone except that the target skb is
766 : : * supplied by the user.
767 : : *
768 : : * The target skb is returned upon exit.
769 : : */
770 : 0 : struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
771 : : {
772 : 0 : skb_release_all(dst);
773 : 0 : return __skb_clone(dst, src);
774 : : }
775 : : EXPORT_SYMBOL_GPL(skb_morph);
776 : :
777 : : /**
778 : : * skb_copy_ubufs - copy userspace skb frags buffers to kernel
779 : : * @skb: the skb to modify
780 : : * @gfp_mask: allocation priority
781 : : *
782 : : * This must be called on SKBTX_DEV_ZEROCOPY skb.
783 : : * It will copy all frags into kernel and drop the reference
784 : : * to userspace pages.
785 : : *
786 : : * If this function is called from an interrupt gfp_mask() must be
787 : : * %GFP_ATOMIC.
788 : : *
789 : : * Returns 0 on success or a negative error code on failure
790 : : * to allocate kernel memory to copy to.
791 : : */
792 : 0 : int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
793 : : {
794 : : int i;
795 : 0 : int num_frags = skb_shinfo(skb)->nr_frags;
796 : : struct page *page, *head = NULL;
797 : 0 : struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
798 : :
799 [ # # ]: 0 : for (i = 0; i < num_frags; i++) {
800 : : u8 *vaddr;
801 : 0 : skb_frag_t *f = &skb_shinfo(skb)->frags[i];
802 : :
803 : : page = alloc_page(gfp_mask);
804 [ # # ]: 0 : if (!page) {
805 [ # # ]: 0 : while (head) {
806 : 0 : struct page *next = (struct page *)page_private(head);
807 : 0 : put_page(head);
808 : : head = next;
809 : : }
810 : : return -ENOMEM;
811 : : }
812 : 0 : vaddr = kmap_atomic(skb_frag_page(f));
813 : 0 : memcpy(page_address(page),
814 : 0 : vaddr + f->page_offset, skb_frag_size(f));
815 : 0 : kunmap_atomic(vaddr);
816 : 0 : set_page_private(page, (unsigned long)head);
817 : : head = page;
818 : : }
819 : :
820 : : /* skb frags release userspace buffers */
821 [ # # ]: 0 : for (i = 0; i < num_frags; i++)
822 : : skb_frag_unref(skb, i);
823 : :
824 : 0 : uarg->callback(uarg, false);
825 : :
826 : : /* skb frags point to kernel buffers */
827 [ # # ]: 0 : for (i = num_frags - 1; i >= 0; i--) {
828 : : __skb_fill_page_desc(skb, i, head, 0,
829 : 0 : skb_shinfo(skb)->frags[i].size);
830 : 0 : head = (struct page *)page_private(head);
831 : : }
832 : :
833 : 0 : skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
834 : 0 : return 0;
835 : : }
836 : : EXPORT_SYMBOL_GPL(skb_copy_ubufs);
837 : :
838 : : /**
839 : : * skb_clone - duplicate an sk_buff
840 : : * @skb: buffer to clone
841 : : * @gfp_mask: allocation priority
842 : : *
843 : : * Duplicate an &sk_buff. The new one is not owned by a socket. Both
844 : : * copies share the same packet data but not structure. The new
845 : : * buffer has a reference count of 1. If the allocation fails the
846 : : * function returns %NULL otherwise the new buffer is returned.
847 : : *
848 : : * If this function is called from an interrupt gfp_mask() must be
849 : : * %GFP_ATOMIC.
850 : : */
851 : :
852 : 0 : struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
853 : : {
854 : : struct sk_buff *n;
855 : :
856 [ + - ]: 46873 : if (skb_orphan_frags(skb, gfp_mask))
857 : : return NULL;
858 : :
859 : 46873 : n = skb + 1;
860 [ + + ][ + - ]: 46873 : if (skb->fclone == SKB_FCLONE_ORIG &&
861 : 23490 : n->fclone == SKB_FCLONE_UNAVAILABLE) {
862 : 23490 : atomic_t *fclone_ref = (atomic_t *) (n + 1);
863 : 23490 : n->fclone = SKB_FCLONE_CLONE;
864 : : atomic_inc(fclone_ref);
865 : : } else {
866 [ - + ]: 23383 : if (skb_pfmemalloc(skb))
867 : 0 : gfp_mask |= __GFP_MEMALLOC;
868 : :
869 : 23383 : n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
870 [ + - ]: 23383 : if (!n)
871 : : return NULL;
872 : :
873 : : kmemcheck_annotate_bitfield(n, flags1);
874 : : kmemcheck_annotate_bitfield(n, flags2);
875 : 23383 : n->fclone = SKB_FCLONE_UNAVAILABLE;
876 : : }
877 : :
878 : 46873 : return __skb_clone(n, skb);
879 : : }
880 : : EXPORT_SYMBOL(skb_clone);
881 : :
882 : 0 : static void skb_headers_offset_update(struct sk_buff *skb, int off)
883 : : {
884 : : /* Only adjust this if it actually is csum_start rather than csum */
885 [ # # ]: 0 : if (skb->ip_summed == CHECKSUM_PARTIAL)
886 : 0 : skb->csum_start += off;
887 : : /* {transport,network,mac}_header and tail are relative to skb->head */
888 : 0 : skb->transport_header += off;
889 : 0 : skb->network_header += off;
890 [ # # ]: 0 : if (skb_mac_header_was_set(skb))
891 : 0 : skb->mac_header += off;
892 : 0 : skb->inner_transport_header += off;
893 : 0 : skb->inner_network_header += off;
894 : 0 : skb->inner_mac_header += off;
895 : 0 : }
896 : :
897 : 0 : static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
898 : : {
899 : 0 : __copy_skb_header(new, old);
900 : :
901 : 0 : skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
902 : 0 : skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
903 : 0 : skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
904 : 0 : }
905 : :
906 : : static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
907 : : {
908 [ # # ][ # # ]: 0 : if (skb_pfmemalloc(skb))
[ # # ][ # # ]
909 : : return SKB_ALLOC_RX;
910 : : return 0;
911 : : }
912 : :
913 : : /**
914 : : * skb_copy - create private copy of an sk_buff
915 : : * @skb: buffer to copy
916 : : * @gfp_mask: allocation priority
917 : : *
918 : : * Make a copy of both an &sk_buff and its data. This is used when the
919 : : * caller wishes to modify the data and needs a private copy of the
920 : : * data to alter. Returns %NULL on failure or the pointer to the buffer
921 : : * on success. The returned buffer has a reference count of 1.
922 : : *
923 : : * As by-product this function converts non-linear &sk_buff to linear
924 : : * one, so that &sk_buff becomes completely private and caller is allowed
925 : : * to modify all the data of returned buffer. This means that this
926 : : * function is not recommended for use in circumstances when only
927 : : * header is going to be modified. Use pskb_copy() instead.
928 : : */
929 : :
930 : 0 : struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
931 : : {
932 : : int headerlen = skb_headroom(skb);
933 : 0 : unsigned int size = skb_end_offset(skb) + skb->data_len;
934 : 0 : struct sk_buff *n = __alloc_skb(size, gfp_mask,
935 : : skb_alloc_rx_flag(skb), NUMA_NO_NODE);
936 : :
937 [ # # ]: 0 : if (!n)
938 : : return NULL;
939 : :
940 : : /* Set the data pointer */
941 : : skb_reserve(n, headerlen);
942 : : /* Set the tail pointer and length */
943 : 0 : skb_put(n, skb->len);
944 : :
945 [ # # ]: 0 : if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
946 : 0 : BUG();
947 : :
948 : 0 : copy_skb_header(n, skb);
949 : 0 : return n;
950 : : }
951 : : EXPORT_SYMBOL(skb_copy);
952 : :
953 : : /**
954 : : * __pskb_copy - create copy of an sk_buff with private head.
955 : : * @skb: buffer to copy
956 : : * @headroom: headroom of new skb
957 : : * @gfp_mask: allocation priority
958 : : *
959 : : * Make a copy of both an &sk_buff and part of its data, located
960 : : * in header. Fragmented data remain shared. This is used when
961 : : * the caller wishes to modify only header of &sk_buff and needs
962 : : * private copy of the header to alter. Returns %NULL on failure
963 : : * or the pointer to the buffer on success.
964 : : * The returned buffer has a reference count of 1.
965 : : */
966 : :
967 : 0 : struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
968 : : {
969 : 0 : unsigned int size = skb_headlen(skb) + headroom;
970 : 0 : struct sk_buff *n = __alloc_skb(size, gfp_mask,
971 : : skb_alloc_rx_flag(skb), NUMA_NO_NODE);
972 : :
973 [ # # ]: 0 : if (!n)
974 : : goto out;
975 : :
976 : : /* Set the data pointer */
977 : : skb_reserve(n, headroom);
978 : : /* Set the tail pointer and length */
979 : 0 : skb_put(n, skb_headlen(skb));
980 : : /* Copy the bytes */
981 : 0 : skb_copy_from_linear_data(skb, n->data, n->len);
982 : :
983 : 0 : n->truesize += skb->data_len;
984 : 0 : n->data_len = skb->data_len;
985 : 0 : n->len = skb->len;
986 : :
987 [ # # ]: 0 : if (skb_shinfo(skb)->nr_frags) {
988 : : int i;
989 : :
990 [ # # ]: 0 : if (skb_orphan_frags(skb, gfp_mask)) {
991 : 0 : kfree_skb(n);
992 : : n = NULL;
993 : 0 : goto out;
994 : : }
995 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
996 : 0 : skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
997 : : skb_frag_ref(skb, i);
998 : : }
999 : 0 : skb_shinfo(n)->nr_frags = i;
1000 : : }
1001 : :
1002 [ # # ]: 0 : if (skb_has_frag_list(skb)) {
1003 : 0 : skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1004 : 0 : skb_clone_fraglist(n);
1005 : : }
1006 : :
1007 : 0 : copy_skb_header(n, skb);
1008 : : out:
1009 : 0 : return n;
1010 : : }
1011 : : EXPORT_SYMBOL(__pskb_copy);
1012 : :
1013 : : /**
1014 : : * pskb_expand_head - reallocate header of &sk_buff
1015 : : * @skb: buffer to reallocate
1016 : : * @nhead: room to add at head
1017 : : * @ntail: room to add at tail
1018 : : * @gfp_mask: allocation priority
1019 : : *
1020 : : * Expands (or creates identical copy, if @nhead and @ntail are zero)
1021 : : * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1022 : : * reference count of 1. Returns zero in the case of success or error,
1023 : : * if expansion failed. In the last case, &sk_buff is not changed.
1024 : : *
1025 : : * All the pointers pointing into skb header may change and must be
1026 : : * reloaded after call to this function.
1027 : : */
1028 : :
1029 : 0 : int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1030 : : gfp_t gfp_mask)
1031 : : {
1032 : : int i;
1033 : : u8 *data;
1034 : 0 : int size = nhead + skb_end_offset(skb) + ntail;
1035 : : long off;
1036 : :
1037 [ # # ]: 0 : BUG_ON(nhead < 0);
1038 : :
1039 [ # # ]: 0 : if (skb_shared(skb))
1040 : 0 : BUG();
1041 : :
1042 : 0 : size = SKB_DATA_ALIGN(size);
1043 : :
1044 [ # # ]: 0 : if (skb_pfmemalloc(skb))
1045 : 0 : gfp_mask |= __GFP_MEMALLOC;
1046 : 0 : data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1047 : : gfp_mask, NUMA_NO_NODE, NULL);
1048 [ # # ]: 0 : if (!data)
1049 : : goto nodata;
1050 : 0 : size = SKB_WITH_OVERHEAD(ksize(data));
1051 : :
1052 : : /* Copy only real data... and, alas, header. This should be
1053 : : * optimized for the cases when header is void.
1054 : : */
1055 : 0 : memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1056 : :
1057 : 0 : memcpy((struct skb_shared_info *)(data + size),
1058 : : skb_shinfo(skb),
1059 : 0 : offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1060 : :
1061 : : /*
1062 : : * if shinfo is shared we must drop the old head gracefully, but if it
1063 : : * is not we can just drop the old head and let the existing refcount
1064 : : * be since all we did is relocate the values
1065 : : */
1066 [ # # ]: 0 : if (skb_cloned(skb)) {
1067 : : /* copy this zero copy skb frags */
1068 [ # # ]: 0 : if (skb_orphan_frags(skb, gfp_mask))
1069 : : goto nofrags;
1070 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1071 : : skb_frag_ref(skb, i);
1072 : :
1073 [ # # ]: 0 : if (skb_has_frag_list(skb))
1074 : 0 : skb_clone_fraglist(skb);
1075 : :
1076 : 0 : skb_release_data(skb);
1077 : : } else {
1078 : 0 : skb_free_head(skb);
1079 : : }
1080 : 0 : off = (data + nhead) - skb->head;
1081 : :
1082 : 0 : skb->head = data;
1083 : 0 : skb->head_frag = 0;
1084 : 0 : skb->data += off;
1085 : : #ifdef NET_SKBUFF_DATA_USES_OFFSET
1086 : : skb->end = size;
1087 : : off = nhead;
1088 : : #else
1089 : 0 : skb->end = skb->head + size;
1090 : : #endif
1091 : 0 : skb->tail += off;
1092 : 0 : skb_headers_offset_update(skb, nhead);
1093 : 0 : skb->cloned = 0;
1094 : 0 : skb->hdr_len = 0;
1095 : 0 : skb->nohdr = 0;
1096 : 0 : atomic_set(&skb_shinfo(skb)->dataref, 1);
1097 : 0 : return 0;
1098 : :
1099 : : nofrags:
1100 : 0 : kfree(data);
1101 : : nodata:
1102 : : return -ENOMEM;
1103 : : }
1104 : : EXPORT_SYMBOL(pskb_expand_head);
1105 : :
1106 : : /* Make private copy of skb with writable head and some headroom */
1107 : :
1108 : 0 : struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1109 : : {
1110 : : struct sk_buff *skb2;
1111 : 0 : int delta = headroom - skb_headroom(skb);
1112 : :
1113 [ # # ]: 0 : if (delta <= 0)
1114 : : skb2 = pskb_copy(skb, GFP_ATOMIC);
1115 : : else {
1116 : 0 : skb2 = skb_clone(skb, GFP_ATOMIC);
1117 [ # # ][ # # ]: 0 : if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1118 : : GFP_ATOMIC)) {
1119 : 0 : kfree_skb(skb2);
1120 : : skb2 = NULL;
1121 : : }
1122 : : }
1123 : 0 : return skb2;
1124 : : }
1125 : : EXPORT_SYMBOL(skb_realloc_headroom);
1126 : :
1127 : : /**
1128 : : * skb_copy_expand - copy and expand sk_buff
1129 : : * @skb: buffer to copy
1130 : : * @newheadroom: new free bytes at head
1131 : : * @newtailroom: new free bytes at tail
1132 : : * @gfp_mask: allocation priority
1133 : : *
1134 : : * Make a copy of both an &sk_buff and its data and while doing so
1135 : : * allocate additional space.
1136 : : *
1137 : : * This is used when the caller wishes to modify the data and needs a
1138 : : * private copy of the data to alter as well as more space for new fields.
1139 : : * Returns %NULL on failure or the pointer to the buffer
1140 : : * on success. The returned buffer has a reference count of 1.
1141 : : *
1142 : : * You must pass %GFP_ATOMIC as the allocation priority if this function
1143 : : * is called from an interrupt.
1144 : : */
1145 : 0 : struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1146 : : int newheadroom, int newtailroom,
1147 : : gfp_t gfp_mask)
1148 : : {
1149 : : /*
1150 : : * Allocate the copy buffer
1151 : : */
1152 : 0 : struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1153 : : gfp_mask, skb_alloc_rx_flag(skb),
1154 : : NUMA_NO_NODE);
1155 : : int oldheadroom = skb_headroom(skb);
1156 : : int head_copy_len, head_copy_off;
1157 : :
1158 [ # # ]: 0 : if (!n)
1159 : : return NULL;
1160 : :
1161 : : skb_reserve(n, newheadroom);
1162 : :
1163 : : /* Set the tail pointer and length */
1164 : 0 : skb_put(n, skb->len);
1165 : :
1166 : : head_copy_len = oldheadroom;
1167 : : head_copy_off = 0;
1168 [ # # ]: 0 : if (newheadroom <= head_copy_len)
1169 : : head_copy_len = newheadroom;
1170 : : else
1171 : 0 : head_copy_off = newheadroom - head_copy_len;
1172 : :
1173 : : /* Copy the linear header and data. */
1174 [ # # ]: 0 : if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1175 : 0 : skb->len + head_copy_len))
1176 : 0 : BUG();
1177 : :
1178 : 0 : copy_skb_header(n, skb);
1179 : :
1180 : 0 : skb_headers_offset_update(n, newheadroom - oldheadroom);
1181 : :
1182 : 0 : return n;
1183 : : }
1184 : : EXPORT_SYMBOL(skb_copy_expand);
1185 : :
1186 : : /**
1187 : : * skb_pad - zero pad the tail of an skb
1188 : : * @skb: buffer to pad
1189 : : * @pad: space to pad
1190 : : *
1191 : : * Ensure that a buffer is followed by a padding area that is zero
1192 : : * filled. Used by network drivers which may DMA or transfer data
1193 : : * beyond the buffer end onto the wire.
1194 : : *
1195 : : * May return error in out of memory cases. The skb is freed on error.
1196 : : */
1197 : :
1198 : 0 : int skb_pad(struct sk_buff *skb, int pad)
1199 : : {
1200 : : int err;
1201 : : int ntail;
1202 : :
1203 : : /* If the skbuff is non linear tailroom is always zero.. */
1204 [ # # ][ # # ]: 0 : if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1205 [ # # ]: 0 : memset(skb->data+skb->len, 0, pad);
1206 : : return 0;
1207 : : }
1208 : :
1209 : 0 : ntail = skb->data_len + pad - (skb->end - skb->tail);
1210 [ # # ][ # # ]: 0 : if (likely(skb_cloned(skb) || ntail > 0)) {
1211 : 0 : err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1212 [ # # ]: 0 : if (unlikely(err))
1213 : : goto free_skb;
1214 : : }
1215 : :
1216 : : /* FIXME: The use of this function with non-linear skb's really needs
1217 : : * to be audited.
1218 : : */
1219 : : err = skb_linearize(skb);
1220 [ # # ]: 0 : if (unlikely(err))
1221 : : goto free_skb;
1222 : :
1223 [ # # ]: 0 : memset(skb->data + skb->len, 0, pad);
1224 : : return 0;
1225 : :
1226 : : free_skb:
1227 : 0 : kfree_skb(skb);
1228 : 0 : return err;
1229 : : }
1230 : : EXPORT_SYMBOL(skb_pad);
1231 : :
1232 : : /**
1233 : : * pskb_put - add data to the tail of a potentially fragmented buffer
1234 : : * @skb: start of the buffer to use
1235 : : * @tail: tail fragment of the buffer to use
1236 : : * @len: amount of data to add
1237 : : *
1238 : : * This function extends the used data area of the potentially
1239 : : * fragmented buffer. @tail must be the last fragment of @skb -- or
1240 : : * @skb itself. If this would exceed the total buffer size the kernel
1241 : : * will panic. A pointer to the first byte of the extra data is
1242 : : * returned.
1243 : : */
1244 : :
1245 : 0 : unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1246 : : {
1247 [ # # ]: 0 : if (tail != skb) {
1248 : 0 : skb->data_len += len;
1249 : 0 : skb->len += len;
1250 : : }
1251 : 0 : return skb_put(tail, len);
1252 : : }
1253 : : EXPORT_SYMBOL_GPL(pskb_put);
1254 : :
1255 : : /**
1256 : : * skb_put - add data to a buffer
1257 : : * @skb: buffer to use
1258 : : * @len: amount of data to add
1259 : : *
1260 : : * This function extends the used data area of the buffer. If this would
1261 : : * exceed the total buffer size the kernel will panic. A pointer to the
1262 : : * first byte of the extra data is returned.
1263 : : */
1264 : 0 : unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1265 : : {
1266 : : unsigned char *tmp = skb_tail_pointer(skb);
1267 [ - + ]: 1325182 : SKB_LINEAR_ASSERT(skb);
1268 : 1325182 : skb->tail += len;
1269 : 1325182 : skb->len += len;
1270 [ - + ]: 1325182 : if (unlikely(skb->tail > skb->end))
1271 : 0 : skb_over_panic(skb, len, __builtin_return_address(0));
1272 : 1325182 : return tmp;
1273 : : }
1274 : : EXPORT_SYMBOL(skb_put);
1275 : :
1276 : : /**
1277 : : * skb_push - add data to the start of a buffer
1278 : : * @skb: buffer to use
1279 : : * @len: amount of data to add
1280 : : *
1281 : : * This function extends the used data area of the buffer at the buffer
1282 : : * start. If this would exceed the total buffer headroom the kernel will
1283 : : * panic. A pointer to the first byte of the extra data is returned.
1284 : : */
1285 : 0 : unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1286 : : {
1287 : 115533 : skb->data -= len;
1288 : 115533 : skb->len += len;
1289 [ - + ]: 115533 : if (unlikely(skb->data<skb->head))
1290 : 0 : skb_under_panic(skb, len, __builtin_return_address(0));
1291 : 115533 : return skb->data;
1292 : : }
1293 : : EXPORT_SYMBOL(skb_push);
1294 : :
1295 : : /**
1296 : : * skb_pull - remove data from the start of a buffer
1297 : : * @skb: buffer to use
1298 : : * @len: amount of data to remove
1299 : : *
1300 : : * This function removes data from the start of a buffer, returning
1301 : : * the memory to the headroom. A pointer to the next data in the buffer
1302 : : * is returned. Once the data has been pulled future pushes will overwrite
1303 : : * the old data.
1304 : : */
1305 : 0 : unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1306 : : {
1307 : 0 : return skb_pull_inline(skb, len);
1308 : : }
1309 : : EXPORT_SYMBOL(skb_pull);
1310 : :
1311 : : /**
1312 : : * skb_trim - remove end from a buffer
1313 : : * @skb: buffer to alter
1314 : : * @len: new length
1315 : : *
1316 : : * Cut the length of a buffer down by removing data from the tail. If
1317 : : * the buffer is already under the length specified it is not modified.
1318 : : * The skb must be linear.
1319 : : */
1320 : 0 : void skb_trim(struct sk_buff *skb, unsigned int len)
1321 : : {
1322 [ + ]: 10 : if (skb->len > len)
1323 : : __skb_trim(skb, len);
1324 : 0 : }
1325 : : EXPORT_SYMBOL(skb_trim);
1326 : :
1327 : : /* Trims skb to length len. It can change skb pointers.
1328 : : */
1329 : :
1330 : 0 : int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1331 : : {
1332 : : struct sk_buff **fragp;
1333 : : struct sk_buff *frag;
1334 : 0 : int offset = skb_headlen(skb);
1335 : 0 : int nfrags = skb_shinfo(skb)->nr_frags;
1336 : : int i;
1337 : : int err;
1338 : :
1339 [ # # ][ # # ]: 0 : if (skb_cloned(skb) &&
1340 : 0 : unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1341 : : return err;
1342 : :
1343 : : i = 0;
1344 [ # # ]: 0 : if (offset >= len)
1345 : : goto drop_pages;
1346 : :
1347 [ # # ]: 0 : for (; i < nfrags; i++) {
1348 : 0 : int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1349 : :
1350 [ # # ]: 0 : if (end < len) {
1351 : : offset = end;
1352 : 0 : continue;
1353 : : }
1354 : :
1355 : 0 : skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1356 : :
1357 : : drop_pages:
1358 : 0 : skb_shinfo(skb)->nr_frags = i;
1359 : :
1360 [ # # ]: 0 : for (; i < nfrags; i++)
1361 : : skb_frag_unref(skb, i);
1362 : :
1363 [ # # ]: 0 : if (skb_has_frag_list(skb))
1364 : : skb_drop_fraglist(skb);
1365 : : goto done;
1366 : : }
1367 : :
1368 [ # # ]: 0 : for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1369 : 0 : fragp = &frag->next) {
1370 : 0 : int end = offset + frag->len;
1371 : :
1372 [ # # ]: 0 : if (skb_shared(frag)) {
1373 : : struct sk_buff *nfrag;
1374 : :
1375 : 0 : nfrag = skb_clone(frag, GFP_ATOMIC);
1376 [ # # ]: 0 : if (unlikely(!nfrag))
1377 : : return -ENOMEM;
1378 : :
1379 : 0 : nfrag->next = frag->next;
1380 : 0 : consume_skb(frag);
1381 : : frag = nfrag;
1382 : 0 : *fragp = frag;
1383 : : }
1384 : :
1385 [ # # ]: 0 : if (end < len) {
1386 : : offset = end;
1387 : 0 : continue;
1388 : : }
1389 : :
1390 [ # # ][ # # ]: 0 : if (end > len &&
1391 : 0 : unlikely((err = pskb_trim(frag, len - offset))))
1392 : : return err;
1393 : :
1394 [ # # ]: 0 : if (frag->next)
1395 : : skb_drop_list(&frag->next);
1396 : : break;
1397 : : }
1398 : :
1399 : : done:
1400 [ # # ]: 0 : if (len > skb_headlen(skb)) {
1401 : 0 : skb->data_len -= skb->len - len;
1402 : 0 : skb->len = len;
1403 : : } else {
1404 : 0 : skb->len = len;
1405 : 0 : skb->data_len = 0;
1406 : : skb_set_tail_pointer(skb, len);
1407 : : }
1408 : :
1409 : : return 0;
1410 : : }
1411 : : EXPORT_SYMBOL(___pskb_trim);
1412 : :
1413 : : /**
1414 : : * __pskb_pull_tail - advance tail of skb header
1415 : : * @skb: buffer to reallocate
1416 : : * @delta: number of bytes to advance tail
1417 : : *
1418 : : * The function makes a sense only on a fragmented &sk_buff,
1419 : : * it expands header moving its tail forward and copying necessary
1420 : : * data from fragmented part.
1421 : : *
1422 : : * &sk_buff MUST have reference count of 1.
1423 : : *
1424 : : * Returns %NULL (and &sk_buff does not change) if pull failed
1425 : : * or value of new tail of skb in the case of success.
1426 : : *
1427 : : * All the pointers pointing into skb header may change and must be
1428 : : * reloaded after call to this function.
1429 : : */
1430 : :
1431 : : /* Moves tail of skb head forward, copying data from fragmented part,
1432 : : * when it is necessary.
1433 : : * 1. It may fail due to malloc failure.
1434 : : * 2. It may change skb pointers.
1435 : : *
1436 : : * It is pretty complicated. Luckily, it is called only in exceptional cases.
1437 : : */
1438 : 0 : unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1439 : : {
1440 : : /* If skb has not enough free space at tail, get new one
1441 : : * plus 128 bytes for future expansions. If we have enough
1442 : : * room at tail, reallocate without expansion only if skb is cloned.
1443 : : */
1444 : 0 : int i, k, eat = (skb->tail + delta) - skb->end;
1445 : :
1446 [ # # ][ # # ]: 0 : if (eat > 0 || skb_cloned(skb)) {
1447 [ # # ][ # # ]: 0 : if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1448 : : GFP_ATOMIC))
1449 : : return NULL;
1450 : : }
1451 : :
1452 [ # # ]: 0 : if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1453 : 0 : BUG();
1454 : :
1455 : : /* Optimization: no fragments, no reasons to preestimate
1456 : : * size of pulled pages. Superb.
1457 : : */
1458 [ # # ]: 0 : if (!skb_has_frag_list(skb))
1459 : : goto pull_pages;
1460 : :
1461 : : /* Estimate size of pulled pages. */
1462 : : eat = delta;
1463 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1464 : 0 : int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1465 : :
1466 [ # # ]: 0 : if (size >= eat)
1467 : : goto pull_pages;
1468 : 0 : eat -= size;
1469 : : }
1470 : :
1471 : : /* If we need update frag list, we are in troubles.
1472 : : * Certainly, it possible to add an offset to skb data,
1473 : : * but taking into account that pulling is expected to
1474 : : * be very rare operation, it is worth to fight against
1475 : : * further bloating skb head and crucify ourselves here instead.
1476 : : * Pure masohism, indeed. 8)8)
1477 : : */
1478 [ # # ]: 0 : if (eat) {
1479 : : struct sk_buff *list = skb_shinfo(skb)->frag_list;
1480 : : struct sk_buff *clone = NULL;
1481 : : struct sk_buff *insp = NULL;
1482 : :
1483 : : do {
1484 [ # # ]: 0 : BUG_ON(!list);
1485 : :
1486 [ # # ]: 0 : if (list->len <= eat) {
1487 : : /* Eaten as whole. */
1488 : 0 : eat -= list->len;
1489 : 0 : list = list->next;
1490 : : insp = list;
1491 : : } else {
1492 : : /* Eaten partially. */
1493 : :
1494 [ # # ]: 0 : if (skb_shared(list)) {
1495 : : /* Sucks! We need to fork list. :-( */
1496 : 0 : clone = skb_clone(list, GFP_ATOMIC);
1497 [ # # ]: 0 : if (!clone)
1498 : : return NULL;
1499 : 0 : insp = list->next;
1500 : : list = clone;
1501 : : } else {
1502 : : /* This may be pulled without
1503 : : * problems. */
1504 : : insp = list;
1505 : : }
1506 [ # # ]: 0 : if (!pskb_pull(list, eat)) {
1507 : 0 : kfree_skb(clone);
1508 : 0 : return NULL;
1509 : : }
1510 : : break;
1511 : : }
1512 [ # # ]: 0 : } while (eat);
1513 : :
1514 : : /* Free pulled out fragments. */
1515 [ # # ]: 0 : while ((list = skb_shinfo(skb)->frag_list) != insp) {
1516 : 0 : skb_shinfo(skb)->frag_list = list->next;
1517 : 0 : kfree_skb(list);
1518 : : }
1519 : : /* And insert new clone at head. */
1520 [ # # ]: 0 : if (clone) {
1521 : 0 : clone->next = list;
1522 : 0 : skb_shinfo(skb)->frag_list = clone;
1523 : : }
1524 : : }
1525 : : /* Success! Now we may commit changes to skb data. */
1526 : :
1527 : : pull_pages:
1528 : : eat = delta;
1529 : : k = 0;
1530 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1531 : 0 : int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1532 : :
1533 [ # # ]: 0 : if (size <= eat) {
1534 : : skb_frag_unref(skb, i);
1535 : 0 : eat -= size;
1536 : : } else {
1537 : 0 : skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1538 [ # # ]: 0 : if (eat) {
1539 : 0 : skb_shinfo(skb)->frags[k].page_offset += eat;
1540 : 0 : skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1541 : : eat = 0;
1542 : : }
1543 : 0 : k++;
1544 : : }
1545 : : }
1546 : 0 : skb_shinfo(skb)->nr_frags = k;
1547 : :
1548 : 0 : skb->tail += delta;
1549 : 0 : skb->data_len -= delta;
1550 : :
1551 : 0 : return skb_tail_pointer(skb);
1552 : : }
1553 : : EXPORT_SYMBOL(__pskb_pull_tail);
1554 : :
1555 : : /**
1556 : : * skb_copy_bits - copy bits from skb to kernel buffer
1557 : : * @skb: source skb
1558 : : * @offset: offset in source
1559 : : * @to: destination buffer
1560 : : * @len: number of bytes to copy
1561 : : *
1562 : : * Copy the specified number of bytes from the source skb to the
1563 : : * destination buffer.
1564 : : *
1565 : : * CAUTION ! :
1566 : : * If its prototype is ever changed,
1567 : : * check arch/{*}/net/{*}.S files,
1568 : : * since it is called from BPF assembly code.
1569 : : */
1570 : 0 : int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1571 : : {
1572 : 0 : int start = skb_headlen(skb);
1573 : : struct sk_buff *frag_iter;
1574 : : int i, copy;
1575 : :
1576 [ # # ]: 0 : if (offset > (int)skb->len - len)
1577 : : goto fault;
1578 : :
1579 : : /* Copy header. */
1580 [ # # ]: 0 : if ((copy = start - offset) > 0) {
1581 [ # # ]: 0 : if (copy > len)
1582 : : copy = len;
1583 : 0 : skb_copy_from_linear_data_offset(skb, offset, to, copy);
1584 [ # # ]: 0 : if ((len -= copy) == 0)
1585 : : return 0;
1586 : 0 : offset += copy;
1587 : 0 : to += copy;
1588 : : }
1589 : :
1590 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1591 : : int end;
1592 : 0 : skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1593 : :
1594 [ # # ]: 0 : WARN_ON(start > offset + len);
1595 : :
1596 : 0 : end = start + skb_frag_size(f);
1597 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1598 : : u8 *vaddr;
1599 : :
1600 [ # # ]: 0 : if (copy > len)
1601 : : copy = len;
1602 : :
1603 : 0 : vaddr = kmap_atomic(skb_frag_page(f));
1604 : 0 : memcpy(to,
1605 : 0 : vaddr + f->page_offset + offset - start,
1606 : : copy);
1607 : 0 : kunmap_atomic(vaddr);
1608 : :
1609 [ # # ]: 0 : if ((len -= copy) == 0)
1610 : : return 0;
1611 : 0 : offset += copy;
1612 : 0 : to += copy;
1613 : : }
1614 : : start = end;
1615 : : }
1616 : :
1617 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
1618 : : int end;
1619 : :
1620 [ # # ]: 0 : WARN_ON(start > offset + len);
1621 : :
1622 : 0 : end = start + frag_iter->len;
1623 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1624 [ # # ]: 0 : if (copy > len)
1625 : : copy = len;
1626 [ # # ]: 0 : if (skb_copy_bits(frag_iter, offset - start, to, copy))
1627 : : goto fault;
1628 [ # # ]: 0 : if ((len -= copy) == 0)
1629 : : return 0;
1630 : 0 : offset += copy;
1631 : 0 : to += copy;
1632 : : }
1633 : : start = end;
1634 : : }
1635 : :
1636 [ # # ]: 0 : if (!len)
1637 : : return 0;
1638 : :
1639 : : fault:
1640 : : return -EFAULT;
1641 : : }
1642 : : EXPORT_SYMBOL(skb_copy_bits);
1643 : :
1644 : : /*
1645 : : * Callback from splice_to_pipe(), if we need to release some pages
1646 : : * at the end of the spd in case we error'ed out in filling the pipe.
1647 : : */
1648 : 0 : static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1649 : : {
1650 : 0 : put_page(spd->pages[i]);
1651 : 0 : }
1652 : :
1653 : 0 : static struct page *linear_to_page(struct page *page, unsigned int *len,
1654 : : unsigned int *offset,
1655 : : struct sock *sk)
1656 : : {
1657 : : struct page_frag *pfrag = sk_page_frag(sk);
1658 : :
1659 [ # # ]: 0 : if (!sk_page_frag_refill(sk, pfrag))
1660 : : return NULL;
1661 : :
1662 : 0 : *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1663 : :
1664 : 0 : memcpy(page_address(pfrag->page) + pfrag->offset,
1665 : 0 : page_address(page) + *offset, *len);
1666 : 0 : *offset = pfrag->offset;
1667 : 0 : pfrag->offset += *len;
1668 : :
1669 : 0 : return pfrag->page;
1670 : : }
1671 : :
1672 : 0 : static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1673 : : struct page *page,
1674 : : unsigned int offset)
1675 : : {
1676 [ # # ]: 0 : return spd->nr_pages &&
1677 [ # # ][ # # ]: 0 : spd->pages[spd->nr_pages - 1] == page &&
1678 : 0 : (spd->partial[spd->nr_pages - 1].offset +
1679 : 0 : spd->partial[spd->nr_pages - 1].len == offset);
1680 : : }
1681 : :
1682 : : /*
1683 : : * Fill page/offset/length into spd, if it can hold more pages.
1684 : : */
1685 : 0 : static bool spd_fill_page(struct splice_pipe_desc *spd,
1686 : : struct pipe_inode_info *pipe, struct page *page,
1687 : : unsigned int *len, unsigned int offset,
1688 : : bool linear,
1689 : : struct sock *sk)
1690 : : {
1691 [ # # ]: 0 : if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1692 : : return true;
1693 : :
1694 [ # # ]: 0 : if (linear) {
1695 : 0 : page = linear_to_page(page, len, &offset, sk);
1696 [ # # ]: 0 : if (!page)
1697 : : return true;
1698 : : }
1699 [ # # ]: 0 : if (spd_can_coalesce(spd, page, offset)) {
1700 : 0 : spd->partial[spd->nr_pages - 1].len += *len;
1701 : : return false;
1702 : : }
1703 : : get_page(page);
1704 : 0 : spd->pages[spd->nr_pages] = page;
1705 : 0 : spd->partial[spd->nr_pages].len = *len;
1706 : 0 : spd->partial[spd->nr_pages].offset = offset;
1707 : 0 : spd->nr_pages++;
1708 : :
1709 : : return false;
1710 : : }
1711 : :
1712 : 0 : static bool __splice_segment(struct page *page, unsigned int poff,
1713 : : unsigned int plen, unsigned int *off,
1714 : : unsigned int *len,
1715 : : struct splice_pipe_desc *spd, bool linear,
1716 : : struct sock *sk,
1717 : : struct pipe_inode_info *pipe)
1718 : : {
1719 [ # # ]: 0 : if (!*len)
1720 : : return true;
1721 : :
1722 : : /* skip this segment if already processed */
1723 [ # # ]: 0 : if (*off >= plen) {
1724 : 0 : *off -= plen;
1725 : : return false;
1726 : : }
1727 : :
1728 : : /* ignore any bits we already processed */
1729 : 0 : poff += *off;
1730 : 0 : plen -= *off;
1731 : 0 : *off = 0;
1732 : :
1733 : : do {
1734 : 0 : unsigned int flen = min(*len, plen);
1735 : :
1736 [ # # ]: 0 : if (spd_fill_page(spd, pipe, page, &flen, poff,
1737 : : linear, sk))
1738 : 0 : return true;
1739 : 0 : poff += flen;
1740 : 0 : plen -= flen;
1741 : 0 : *len -= flen;
1742 [ # # ][ # # ]: 0 : } while (*len && plen);
1743 : :
1744 : : return false;
1745 : : }
1746 : :
1747 : : /*
1748 : : * Map linear and fragment data from the skb to spd. It reports true if the
1749 : : * pipe is full or if we already spliced the requested length.
1750 : : */
1751 : 0 : static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1752 : : unsigned int *offset, unsigned int *len,
1753 : : struct splice_pipe_desc *spd, struct sock *sk)
1754 : : {
1755 : : int seg;
1756 : :
1757 : : /* map the linear part :
1758 : : * If skb->head_frag is set, this 'linear' part is backed by a
1759 : : * fragment, and if the head is not shared with any clones then
1760 : : * we can avoid a copy since we own the head portion of this page.
1761 : : */
1762 [ # # ]: 0 : if (__splice_segment(virt_to_page(skb->data),
1763 : : (unsigned long) skb->data & (PAGE_SIZE - 1),
1764 : : skb_headlen(skb),
1765 : : offset, len, spd,
1766 : : skb_head_is_locked(skb),
1767 : : sk, pipe))
1768 : : return true;
1769 : :
1770 : : /*
1771 : : * then map the fragments
1772 : : */
1773 [ # # ]: 0 : for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1774 : 0 : const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1775 : :
1776 [ # # ]: 0 : if (__splice_segment(skb_frag_page(f),
1777 : 0 : f->page_offset, skb_frag_size(f),
1778 : : offset, len, spd, false, sk, pipe))
1779 : : return true;
1780 : : }
1781 : :
1782 : : return false;
1783 : : }
1784 : :
1785 : : /*
1786 : : * Map data from the skb to a pipe. Should handle both the linear part,
1787 : : * the fragments, and the frag list. It does NOT handle frag lists within
1788 : : * the frag list, if such a thing exists. We'd probably need to recurse to
1789 : : * handle that cleanly.
1790 : : */
1791 : 0 : int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1792 : : struct pipe_inode_info *pipe, unsigned int tlen,
1793 : : unsigned int flags)
1794 : : {
1795 : : struct partial_page partial[MAX_SKB_FRAGS];
1796 : : struct page *pages[MAX_SKB_FRAGS];
1797 : 0 : struct splice_pipe_desc spd = {
1798 : : .pages = pages,
1799 : : .partial = partial,
1800 : : .nr_pages_max = MAX_SKB_FRAGS,
1801 : : .flags = flags,
1802 : : .ops = &nosteal_pipe_buf_ops,
1803 : : .spd_release = sock_spd_release,
1804 : : };
1805 : : struct sk_buff *frag_iter;
1806 : 0 : struct sock *sk = skb->sk;
1807 : : int ret = 0;
1808 : :
1809 : : /*
1810 : : * __skb_splice_bits() only fails if the output has no room left,
1811 : : * so no point in going over the frag_list for the error case.
1812 : : */
1813 [ # # ]: 0 : if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1814 : : goto done;
1815 [ # # ]: 0 : else if (!tlen)
1816 : : goto done;
1817 : :
1818 : : /*
1819 : : * now see if we have a frag_list to map
1820 : : */
1821 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
1822 [ # # ]: 0 : if (!tlen)
1823 : : break;
1824 [ # # ]: 0 : if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1825 : : break;
1826 : : }
1827 : :
1828 : : done:
1829 [ # # ]: 0 : if (spd.nr_pages) {
1830 : : /*
1831 : : * Drop the socket lock, otherwise we have reverse
1832 : : * locking dependencies between sk_lock and i_mutex
1833 : : * here as compared to sendfile(). We enter here
1834 : : * with the socket lock held, and splice_to_pipe() will
1835 : : * grab the pipe inode lock. For sendfile() emulation,
1836 : : * we call into ->sendpage() with the i_mutex lock held
1837 : : * and networking will grab the socket lock.
1838 : : */
1839 : 0 : release_sock(sk);
1840 : 0 : ret = splice_to_pipe(pipe, &spd);
1841 : : lock_sock(sk);
1842 : : }
1843 : :
1844 : 0 : return ret;
1845 : : }
1846 : :
1847 : : /**
1848 : : * skb_store_bits - store bits from kernel buffer to skb
1849 : : * @skb: destination buffer
1850 : : * @offset: offset in destination
1851 : : * @from: source buffer
1852 : : * @len: number of bytes to copy
1853 : : *
1854 : : * Copy the specified number of bytes from the source buffer to the
1855 : : * destination skb. This function handles all the messy bits of
1856 : : * traversing fragment lists and such.
1857 : : */
1858 : :
1859 : 0 : int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1860 : : {
1861 : 0 : int start = skb_headlen(skb);
1862 : : struct sk_buff *frag_iter;
1863 : : int i, copy;
1864 : :
1865 [ # # ]: 0 : if (offset > (int)skb->len - len)
1866 : : goto fault;
1867 : :
1868 [ # # ]: 0 : if ((copy = start - offset) > 0) {
1869 [ # # ]: 0 : if (copy > len)
1870 : : copy = len;
1871 : 0 : skb_copy_to_linear_data_offset(skb, offset, from, copy);
1872 [ # # ]: 0 : if ((len -= copy) == 0)
1873 : : return 0;
1874 : 0 : offset += copy;
1875 : 0 : from += copy;
1876 : : }
1877 : :
1878 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1879 : 0 : skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1880 : : int end;
1881 : :
1882 [ # # ]: 0 : WARN_ON(start > offset + len);
1883 : :
1884 : 0 : end = start + skb_frag_size(frag);
1885 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1886 : : u8 *vaddr;
1887 : :
1888 [ # # ]: 0 : if (copy > len)
1889 : : copy = len;
1890 : :
1891 : 0 : vaddr = kmap_atomic(skb_frag_page(frag));
1892 : 0 : memcpy(vaddr + frag->page_offset + offset - start,
1893 : : from, copy);
1894 : 0 : kunmap_atomic(vaddr);
1895 : :
1896 [ # # ]: 0 : if ((len -= copy) == 0)
1897 : : return 0;
1898 : 0 : offset += copy;
1899 : 0 : from += copy;
1900 : : }
1901 : : start = end;
1902 : : }
1903 : :
1904 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
1905 : : int end;
1906 : :
1907 [ # # ]: 0 : WARN_ON(start > offset + len);
1908 : :
1909 : 0 : end = start + frag_iter->len;
1910 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1911 [ # # ]: 0 : if (copy > len)
1912 : : copy = len;
1913 [ # # ]: 0 : if (skb_store_bits(frag_iter, offset - start,
1914 : : from, copy))
1915 : : goto fault;
1916 [ # # ]: 0 : if ((len -= copy) == 0)
1917 : : return 0;
1918 : 0 : offset += copy;
1919 : 0 : from += copy;
1920 : : }
1921 : : start = end;
1922 : : }
1923 [ # # ]: 0 : if (!len)
1924 : : return 0;
1925 : :
1926 : : fault:
1927 : : return -EFAULT;
1928 : : }
1929 : : EXPORT_SYMBOL(skb_store_bits);
1930 : :
1931 : : /* Checksum skb data. */
1932 : 0 : __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1933 : : __wsum csum, const struct skb_checksum_ops *ops)
1934 : : {
1935 : 21016 : int start = skb_headlen(skb);
1936 : 21016 : int i, copy = start - offset;
1937 : : struct sk_buff *frag_iter;
1938 : : int pos = 0;
1939 : :
1940 : : /* Checksum header. */
1941 [ + - ]: 21016 : if (copy > 0) {
1942 [ - + ]: 21016 : if (copy > len)
1943 : : copy = len;
1944 : 21016 : csum = ops->update(skb->data + offset, copy, csum);
1945 [ - + ]: 21016 : if ((len -= copy) == 0)
1946 : : return csum;
1947 : 0 : offset += copy;
1948 : : pos = copy;
1949 : : }
1950 : :
1951 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1952 : : int end;
1953 : 0 : skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1954 : :
1955 [ # # ]: 0 : WARN_ON(start > offset + len);
1956 : :
1957 : 0 : end = start + skb_frag_size(frag);
1958 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1959 : : __wsum csum2;
1960 : : u8 *vaddr;
1961 : :
1962 [ # # ]: 0 : if (copy > len)
1963 : : copy = len;
1964 : 0 : vaddr = kmap_atomic(skb_frag_page(frag));
1965 : 0 : csum2 = ops->update(vaddr + frag->page_offset +
1966 : 0 : offset - start, copy, 0);
1967 : 0 : kunmap_atomic(vaddr);
1968 : 0 : csum = ops->combine(csum, csum2, pos, copy);
1969 [ # # ]: 0 : if (!(len -= copy))
1970 : : return csum;
1971 : 0 : offset += copy;
1972 : 0 : pos += copy;
1973 : : }
1974 : : start = end;
1975 : : }
1976 : :
1977 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
1978 : : int end;
1979 : :
1980 [ # # ]: 0 : WARN_ON(start > offset + len);
1981 : :
1982 : 0 : end = start + frag_iter->len;
1983 [ # # ]: 0 : if ((copy = end - offset) > 0) {
1984 : : __wsum csum2;
1985 [ # # ]: 0 : if (copy > len)
1986 : : copy = len;
1987 : 0 : csum2 = __skb_checksum(frag_iter, offset - start,
1988 : : copy, 0, ops);
1989 : 0 : csum = ops->combine(csum, csum2, pos, copy);
1990 [ # # ]: 0 : if ((len -= copy) == 0)
1991 : : return csum;
1992 : 0 : offset += copy;
1993 : 0 : pos += copy;
1994 : : }
1995 : : start = end;
1996 : : }
1997 [ # # ]: 0 : BUG_ON(len);
1998 : :
1999 : : return csum;
2000 : : }
2001 : : EXPORT_SYMBOL(__skb_checksum);
2002 : :
2003 : 0 : __wsum skb_checksum(const struct sk_buff *skb, int offset,
2004 : : int len, __wsum csum)
2005 : : {
2006 : 21016 : const struct skb_checksum_ops ops = {
2007 : : .update = csum_partial_ext,
2008 : : .combine = csum_block_add_ext,
2009 : : };
2010 : :
2011 : 21016 : return __skb_checksum(skb, offset, len, csum, &ops);
2012 : : }
2013 : : EXPORT_SYMBOL(skb_checksum);
2014 : :
2015 : : /* Both of above in one bottle. */
2016 : :
2017 : 0 : __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2018 : : u8 *to, int len, __wsum csum)
2019 : : {
2020 : 0 : int start = skb_headlen(skb);
2021 : 0 : int i, copy = start - offset;
2022 : : struct sk_buff *frag_iter;
2023 : : int pos = 0;
2024 : :
2025 : : /* Copy header. */
2026 [ # # ]: 0 : if (copy > 0) {
2027 [ # # ]: 0 : if (copy > len)
2028 : : copy = len;
2029 : 0 : csum = csum_partial_copy_nocheck(skb->data + offset, to,
2030 : : copy, csum);
2031 [ # # ]: 0 : if ((len -= copy) == 0)
2032 : : return csum;
2033 : 0 : offset += copy;
2034 : 0 : to += copy;
2035 : : pos = copy;
2036 : : }
2037 : :
2038 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2039 : : int end;
2040 : :
2041 [ # # ]: 0 : WARN_ON(start > offset + len);
2042 : :
2043 : 0 : end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2044 [ # # ]: 0 : if ((copy = end - offset) > 0) {
2045 : : __wsum csum2;
2046 : : u8 *vaddr;
2047 : 0 : skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2048 : :
2049 [ # # ]: 0 : if (copy > len)
2050 : : copy = len;
2051 : 0 : vaddr = kmap_atomic(skb_frag_page(frag));
2052 : 0 : csum2 = csum_partial_copy_nocheck(vaddr +
2053 : 0 : frag->page_offset +
2054 : 0 : offset - start, to,
2055 : : copy, 0);
2056 : 0 : kunmap_atomic(vaddr);
2057 : : csum = csum_block_add(csum, csum2, pos);
2058 [ # # ]: 0 : if (!(len -= copy))
2059 : : return csum;
2060 : 0 : offset += copy;
2061 : 0 : to += copy;
2062 : 0 : pos += copy;
2063 : : }
2064 : : start = end;
2065 : : }
2066 : :
2067 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
2068 : : __wsum csum2;
2069 : : int end;
2070 : :
2071 [ # # ]: 0 : WARN_ON(start > offset + len);
2072 : :
2073 : 0 : end = start + frag_iter->len;
2074 [ # # ]: 0 : if ((copy = end - offset) > 0) {
2075 [ # # ]: 0 : if (copy > len)
2076 : : copy = len;
2077 : 0 : csum2 = skb_copy_and_csum_bits(frag_iter,
2078 : : offset - start,
2079 : : to, copy, 0);
2080 : : csum = csum_block_add(csum, csum2, pos);
2081 [ # # ]: 0 : if ((len -= copy) == 0)
2082 : : return csum;
2083 : 0 : offset += copy;
2084 : 0 : to += copy;
2085 : 0 : pos += copy;
2086 : : }
2087 : : start = end;
2088 : : }
2089 [ # # ]: 0 : BUG_ON(len);
2090 : : return csum;
2091 : : }
2092 : : EXPORT_SYMBOL(skb_copy_and_csum_bits);
2093 : :
2094 : : /**
2095 : : * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2096 : : * @from: source buffer
2097 : : *
2098 : : * Calculates the amount of linear headroom needed in the 'to' skb passed
2099 : : * into skb_zerocopy().
2100 : : */
2101 : : unsigned int
2102 : 0 : skb_zerocopy_headlen(const struct sk_buff *from)
2103 : : {
2104 : : unsigned int hlen = 0;
2105 : :
2106 [ # # ][ # # ]: 0 : if (!from->head_frag ||
2107 [ # # ]: 0 : skb_headlen(from) < L1_CACHE_BYTES ||
2108 : 0 : skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2109 : : hlen = skb_headlen(from);
2110 : :
2111 [ # # ]: 0 : if (skb_has_frag_list(from))
2112 : 0 : hlen = from->len;
2113 : :
2114 : 0 : return hlen;
2115 : : }
2116 : : EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2117 : :
2118 : : /**
2119 : : * skb_zerocopy - Zero copy skb to skb
2120 : : * @to: destination buffer
2121 : : * @from: source buffer
2122 : : * @len: number of bytes to copy from source buffer
2123 : : * @hlen: size of linear headroom in destination buffer
2124 : : *
2125 : : * Copies up to `len` bytes from `from` to `to` by creating references
2126 : : * to the frags in the source buffer.
2127 : : *
2128 : : * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2129 : : * headroom in the `to` buffer.
2130 : : *
2131 : : * Return value:
2132 : : * 0: everything is OK
2133 : : * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2134 : : * -EFAULT: skb_copy_bits() found some problem with skb geometry
2135 : : */
2136 : : int
2137 : 0 : skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2138 : : {
2139 : : int i, j = 0;
2140 : : int plen = 0; /* length of skb->head fragment */
2141 : : int ret;
2142 : : struct page *page;
2143 : : unsigned int offset;
2144 : :
2145 [ # # ][ # # ]: 0 : BUG_ON(!from->head_frag && !hlen);
2146 : :
2147 : : /* dont bother with small payloads */
2148 [ # # ]: 0 : if (len <= skb_tailroom(to))
2149 : 0 : return skb_copy_bits(from, 0, skb_put(to, len), len);
2150 : :
2151 [ # # ]: 0 : if (hlen) {
2152 : 0 : ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2153 [ # # ]: 0 : if (unlikely(ret))
2154 : : return ret;
2155 : 0 : len -= hlen;
2156 : : } else {
2157 : 0 : plen = min_t(int, skb_headlen(from), len);
2158 [ # # ]: 0 : if (plen) {
2159 : 0 : page = virt_to_head_page(from->head);
2160 : 0 : offset = from->data - (unsigned char *)page_address(page);
2161 : : __skb_fill_page_desc(to, 0, page, offset, plen);
2162 : : get_page(page);
2163 : : j = 1;
2164 : 0 : len -= plen;
2165 : : }
2166 : : }
2167 : :
2168 : 0 : to->truesize += len + plen;
2169 : 0 : to->len += len + plen;
2170 : 0 : to->data_len += len + plen;
2171 : :
2172 [ # # ]: 0 : if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2173 : 0 : skb_tx_error(from);
2174 : 0 : return -ENOMEM;
2175 : : }
2176 : :
2177 [ # # ]: 0 : for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2178 [ # # ]: 0 : if (!len)
2179 : : break;
2180 : 0 : skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2181 : 0 : skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2182 : 0 : len -= skb_shinfo(to)->frags[j].size;
2183 : : skb_frag_ref(to, j);
2184 : 0 : j++;
2185 : : }
2186 : 0 : skb_shinfo(to)->nr_frags = j;
2187 : :
2188 : 0 : return 0;
2189 : : }
2190 : : EXPORT_SYMBOL_GPL(skb_zerocopy);
2191 : :
2192 : 0 : void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2193 : : {
2194 : : __wsum csum;
2195 : : long csstart;
2196 : :
2197 [ # # ]: 0 : if (skb->ip_summed == CHECKSUM_PARTIAL)
2198 : : csstart = skb_checksum_start_offset(skb);
2199 : : else
2200 : 0 : csstart = skb_headlen(skb);
2201 : :
2202 [ # # ]: 0 : BUG_ON(csstart > skb_headlen(skb));
2203 : :
2204 : : skb_copy_from_linear_data(skb, to, csstart);
2205 : :
2206 : : csum = 0;
2207 [ # # ]: 0 : if (csstart != skb->len)
2208 : 0 : csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2209 : 0 : skb->len - csstart, 0);
2210 : :
2211 [ # # ]: 0 : if (skb->ip_summed == CHECKSUM_PARTIAL) {
2212 : 0 : long csstuff = csstart + skb->csum_offset;
2213 : :
2214 : 0 : *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2215 : : }
2216 : 0 : }
2217 : : EXPORT_SYMBOL(skb_copy_and_csum_dev);
2218 : :
2219 : : /**
2220 : : * skb_dequeue - remove from the head of the queue
2221 : : * @list: list to dequeue from
2222 : : *
2223 : : * Remove the head of the list. The list lock is taken so the function
2224 : : * may be used safely with other locking list functions. The head item is
2225 : : * returned or %NULL if the list is empty.
2226 : : */
2227 : :
2228 : 0 : struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2229 : : {
2230 : : unsigned long flags;
2231 : : struct sk_buff *result;
2232 : :
2233 : 1040281 : spin_lock_irqsave(&list->lock, flags);
2234 : : result = __skb_dequeue(list);
2235 : : spin_unlock_irqrestore(&list->lock, flags);
2236 : 1079702 : return result;
2237 : : }
2238 : : EXPORT_SYMBOL(skb_dequeue);
2239 : :
2240 : : /**
2241 : : * skb_dequeue_tail - remove from the tail of the queue
2242 : : * @list: list to dequeue from
2243 : : *
2244 : : * Remove the tail of the list. The list lock is taken so the function
2245 : : * may be used safely with other locking list functions. The tail item is
2246 : : * returned or %NULL if the list is empty.
2247 : : */
2248 : 0 : struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2249 : : {
2250 : : unsigned long flags;
2251 : : struct sk_buff *result;
2252 : :
2253 : 0 : spin_lock_irqsave(&list->lock, flags);
2254 : : result = __skb_dequeue_tail(list);
2255 : : spin_unlock_irqrestore(&list->lock, flags);
2256 : 0 : return result;
2257 : : }
2258 : : EXPORT_SYMBOL(skb_dequeue_tail);
2259 : :
2260 : : /**
2261 : : * skb_queue_purge - empty a list
2262 : : * @list: list to empty
2263 : : *
2264 : : * Delete all buffers on an &sk_buff list. Each buffer is removed from
2265 : : * the list and one reference dropped. This function takes the list
2266 : : * lock and is atomic with respect to other list locking functions.
2267 : : */
2268 : 0 : void skb_queue_purge(struct sk_buff_head *list)
2269 : : {
2270 : : struct sk_buff *skb;
2271 [ - + ]: 515820 : while ((skb = skb_dequeue(list)) != NULL)
2272 : 0 : kfree_skb(skb);
2273 : 539189 : }
2274 : : EXPORT_SYMBOL(skb_queue_purge);
2275 : :
2276 : : /**
2277 : : * skb_queue_head - queue a buffer at the list head
2278 : : * @list: list to use
2279 : : * @newsk: buffer to queue
2280 : : *
2281 : : * Queue a buffer at the start of the list. This function takes the
2282 : : * list lock and can be used safely with other locking &sk_buff functions
2283 : : * safely.
2284 : : *
2285 : : * A buffer cannot be placed on two lists at the same time.
2286 : : */
2287 : 0 : void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2288 : : {
2289 : : unsigned long flags;
2290 : :
2291 : 0 : spin_lock_irqsave(&list->lock, flags);
2292 : : __skb_queue_head(list, newsk);
2293 : : spin_unlock_irqrestore(&list->lock, flags);
2294 : 0 : }
2295 : : EXPORT_SYMBOL(skb_queue_head);
2296 : :
2297 : : /**
2298 : : * skb_queue_tail - queue a buffer at the list tail
2299 : : * @list: list to use
2300 : : * @newsk: buffer to queue
2301 : : *
2302 : : * Queue a buffer at the tail of the list. This function takes the
2303 : : * list lock and can be used safely with other locking &sk_buff functions
2304 : : * safely.
2305 : : *
2306 : : * A buffer cannot be placed on two lists at the same time.
2307 : : */
2308 : 0 : void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2309 : : {
2310 : : unsigned long flags;
2311 : :
2312 : 787933 : spin_lock_irqsave(&list->lock, flags);
2313 : : __skb_queue_tail(list, newsk);
2314 : : spin_unlock_irqrestore(&list->lock, flags);
2315 : 815753 : }
2316 : : EXPORT_SYMBOL(skb_queue_tail);
2317 : :
2318 : : /**
2319 : : * skb_unlink - remove a buffer from a list
2320 : : * @skb: buffer to remove
2321 : : * @list: list to use
2322 : : *
2323 : : * Remove a packet from a list. The list locks are taken and this
2324 : : * function is atomic with respect to other list locked calls
2325 : : *
2326 : : * You must know what list the SKB is on.
2327 : : */
2328 : 0 : void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2329 : : {
2330 : : unsigned long flags;
2331 : :
2332 : 768852 : spin_lock_irqsave(&list->lock, flags);
2333 : : __skb_unlink(skb, list);
2334 : : spin_unlock_irqrestore(&list->lock, flags);
2335 : 794758 : }
2336 : : EXPORT_SYMBOL(skb_unlink);
2337 : :
2338 : : /**
2339 : : * skb_append - append a buffer
2340 : : * @old: buffer to insert after
2341 : : * @newsk: buffer to insert
2342 : : * @list: list to use
2343 : : *
2344 : : * Place a packet after a given packet in a list. The list locks are taken
2345 : : * and this function is atomic with respect to other list locked calls.
2346 : : * A buffer cannot be placed on two lists at the same time.
2347 : : */
2348 : 0 : void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2349 : : {
2350 : : unsigned long flags;
2351 : :
2352 : 0 : spin_lock_irqsave(&list->lock, flags);
2353 : : __skb_queue_after(list, old, newsk);
2354 : : spin_unlock_irqrestore(&list->lock, flags);
2355 : 0 : }
2356 : : EXPORT_SYMBOL(skb_append);
2357 : :
2358 : : /**
2359 : : * skb_insert - insert a buffer
2360 : : * @old: buffer to insert before
2361 : : * @newsk: buffer to insert
2362 : : * @list: list to use
2363 : : *
2364 : : * Place a packet before a given packet in a list. The list locks are
2365 : : * taken and this function is atomic with respect to other list locked
2366 : : * calls.
2367 : : *
2368 : : * A buffer cannot be placed on two lists at the same time.
2369 : : */
2370 : 0 : void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2371 : : {
2372 : : unsigned long flags;
2373 : :
2374 : 0 : spin_lock_irqsave(&list->lock, flags);
2375 : 0 : __skb_insert(newsk, old->prev, old, list);
2376 : : spin_unlock_irqrestore(&list->lock, flags);
2377 : 0 : }
2378 : : EXPORT_SYMBOL(skb_insert);
2379 : :
2380 : 0 : static inline void skb_split_inside_header(struct sk_buff *skb,
2381 : 0 : struct sk_buff* skb1,
2382 : : const u32 len, const int pos)
2383 : : {
2384 : : int i;
2385 : :
2386 : 0 : skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2387 : : pos - len);
2388 : : /* And move data appendix as is. */
2389 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2390 : 0 : skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2391 : :
2392 : 0 : skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2393 : 0 : skb_shinfo(skb)->nr_frags = 0;
2394 : 0 : skb1->data_len = skb->data_len;
2395 : 0 : skb1->len += skb1->data_len;
2396 : 0 : skb->data_len = 0;
2397 : 0 : skb->len = len;
2398 : : skb_set_tail_pointer(skb, len);
2399 : : }
2400 : :
2401 : 0 : static inline void skb_split_no_header(struct sk_buff *skb,
2402 : 0 : struct sk_buff* skb1,
2403 : : const u32 len, int pos)
2404 : : {
2405 : : int i, k = 0;
2406 : 0 : const int nfrags = skb_shinfo(skb)->nr_frags;
2407 : :
2408 : 0 : skb_shinfo(skb)->nr_frags = 0;
2409 : 0 : skb1->len = skb1->data_len = skb->len - len;
2410 : 0 : skb->len = len;
2411 : 0 : skb->data_len = len - pos;
2412 : :
2413 [ # # ]: 0 : for (i = 0; i < nfrags; i++) {
2414 : 0 : int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2415 : :
2416 [ # # ]: 0 : if (pos + size > len) {
2417 : 0 : skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2418 : :
2419 [ # # ]: 0 : if (pos < len) {
2420 : : /* Split frag.
2421 : : * We have two variants in this case:
2422 : : * 1. Move all the frag to the second
2423 : : * part, if it is possible. F.e.
2424 : : * this approach is mandatory for TUX,
2425 : : * where splitting is expensive.
2426 : : * 2. Split is accurately. We make this.
2427 : : */
2428 : : skb_frag_ref(skb, i);
2429 : 0 : skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2430 : 0 : skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2431 : 0 : skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2432 : 0 : skb_shinfo(skb)->nr_frags++;
2433 : : }
2434 : 0 : k++;
2435 : : } else
2436 : 0 : skb_shinfo(skb)->nr_frags++;
2437 : : pos += size;
2438 : : }
2439 : 0 : skb_shinfo(skb1)->nr_frags = k;
2440 : : }
2441 : :
2442 : : /**
2443 : : * skb_split - Split fragmented skb to two parts at length len.
2444 : : * @skb: the buffer to split
2445 : : * @skb1: the buffer to receive the second part
2446 : : * @len: new length for skb
2447 : : */
2448 : 0 : void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2449 : : {
2450 : 0 : int pos = skb_headlen(skb);
2451 : :
2452 : 0 : skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2453 [ # # ]: 0 : if (len < pos) /* Split line is inside header. */
2454 : : skb_split_inside_header(skb, skb1, len, pos);
2455 : : else /* Second chunk has no header, nothing to copy. */
2456 : : skb_split_no_header(skb, skb1, len, pos);
2457 : 0 : }
2458 : : EXPORT_SYMBOL(skb_split);
2459 : :
2460 : : /* Shifting from/to a cloned skb is a no-go.
2461 : : *
2462 : : * Caller cannot keep skb_shinfo related pointers past calling here!
2463 : : */
2464 : 0 : static int skb_prepare_for_shift(struct sk_buff *skb)
2465 : : {
2466 [ # # ][ # # ]: 0 : return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2467 : : }
2468 : :
2469 : : /**
2470 : : * skb_shift - Shifts paged data partially from skb to another
2471 : : * @tgt: buffer into which tail data gets added
2472 : : * @skb: buffer from which the paged data comes from
2473 : : * @shiftlen: shift up to this many bytes
2474 : : *
2475 : : * Attempts to shift up to shiftlen worth of bytes, which may be less than
2476 : : * the length of the skb, from skb to tgt. Returns number bytes shifted.
2477 : : * It's up to caller to free skb if everything was shifted.
2478 : : *
2479 : : * If @tgt runs out of frags, the whole operation is aborted.
2480 : : *
2481 : : * Skb cannot include anything else but paged data while tgt is allowed
2482 : : * to have non-paged data as well.
2483 : : *
2484 : : * TODO: full sized shift could be optimized but that would need
2485 : : * specialized skb free'er to handle frags without up-to-date nr_frags.
2486 : : */
2487 : 0 : int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2488 : : {
2489 : : int from, to, merge, todo;
2490 : 0 : struct skb_frag_struct *fragfrom, *fragto;
2491 : :
2492 [ # # ]: 0 : BUG_ON(shiftlen > skb->len);
2493 [ # # ]: 0 : BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2494 : :
2495 : : todo = shiftlen;
2496 : : from = 0;
2497 : 0 : to = skb_shinfo(tgt)->nr_frags;
2498 : : fragfrom = &skb_shinfo(skb)->frags[from];
2499 : :
2500 : : /* Actual merge is delayed until the point when we know we can
2501 : : * commit all, so that we don't have to undo partial changes
2502 : : */
2503 [ # # ][ # # ]: 0 : if (!to ||
2504 : : !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2505 : 0 : fragfrom->page_offset)) {
2506 : : merge = -1;
2507 : : } else {
2508 : 0 : merge = to - 1;
2509 : :
2510 : 0 : todo -= skb_frag_size(fragfrom);
2511 [ # # ]: 0 : if (todo < 0) {
2512 [ # # # # ]: 0 : if (skb_prepare_for_shift(skb) ||
2513 : 0 : skb_prepare_for_shift(tgt))
2514 : : return 0;
2515 : :
2516 : : /* All previous frag pointers might be stale! */
2517 : : fragfrom = &skb_shinfo(skb)->frags[from];
2518 : 0 : fragto = &skb_shinfo(tgt)->frags[merge];
2519 : :
2520 : : skb_frag_size_add(fragto, shiftlen);
2521 : : skb_frag_size_sub(fragfrom, shiftlen);
2522 : 0 : fragfrom->page_offset += shiftlen;
2523 : :
2524 : 0 : goto onlymerged;
2525 : : }
2526 : :
2527 : : from++;
2528 : : }
2529 : :
2530 : : /* Skip full, not-fitting skb to avoid expensive operations */
2531 [ # # ][ # # ]: 0 : if ((shiftlen == skb->len) &&
2532 : 0 : (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2533 : : return 0;
2534 : :
2535 [ # # ][ # # ]: 0 : if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2536 : : return 0;
2537 : :
2538 [ # # ][ # # ]: 0 : while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2539 [ # # ]: 0 : if (to == MAX_SKB_FRAGS)
2540 : : return 0;
2541 : :
2542 : 0 : fragfrom = &skb_shinfo(skb)->frags[from];
2543 : 0 : fragto = &skb_shinfo(tgt)->frags[to];
2544 : :
2545 [ # # ]: 0 : if (todo >= skb_frag_size(fragfrom)) {
2546 : 0 : *fragto = *fragfrom;
2547 : 0 : todo -= skb_frag_size(fragfrom);
2548 : 0 : from++;
2549 : 0 : to++;
2550 : :
2551 : : } else {
2552 : : __skb_frag_ref(fragfrom);
2553 : 0 : fragto->page = fragfrom->page;
2554 : 0 : fragto->page_offset = fragfrom->page_offset;
2555 : : skb_frag_size_set(fragto, todo);
2556 : :
2557 : 0 : fragfrom->page_offset += todo;
2558 : : skb_frag_size_sub(fragfrom, todo);
2559 : : todo = 0;
2560 : :
2561 : 0 : to++;
2562 : 0 : break;
2563 : : }
2564 : : }
2565 : :
2566 : : /* Ready to "commit" this state change to tgt */
2567 : 0 : skb_shinfo(tgt)->nr_frags = to;
2568 : :
2569 [ # # ]: 0 : if (merge >= 0) {
2570 : : fragfrom = &skb_shinfo(skb)->frags[0];
2571 : 0 : fragto = &skb_shinfo(tgt)->frags[merge];
2572 : :
2573 : : skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2574 : : __skb_frag_unref(fragfrom);
2575 : : }
2576 : :
2577 : : /* Reposition in the original skb */
2578 : : to = 0;
2579 [ # # ]: 0 : while (from < skb_shinfo(skb)->nr_frags)
2580 : 0 : skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2581 : 0 : skb_shinfo(skb)->nr_frags = to;
2582 : :
2583 [ # # ][ # # ]: 0 : BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2584 : :
2585 : : onlymerged:
2586 : : /* Most likely the tgt won't ever need its checksum anymore, skb on
2587 : : * the other hand might need it if it needs to be resent
2588 : : */
2589 : 0 : tgt->ip_summed = CHECKSUM_PARTIAL;
2590 : 0 : skb->ip_summed = CHECKSUM_PARTIAL;
2591 : :
2592 : : /* Yak, is it really working this way? Some helper please? */
2593 : 0 : skb->len -= shiftlen;
2594 : 0 : skb->data_len -= shiftlen;
2595 : 0 : skb->truesize -= shiftlen;
2596 : 0 : tgt->len += shiftlen;
2597 : 0 : tgt->data_len += shiftlen;
2598 : 0 : tgt->truesize += shiftlen;
2599 : :
2600 : 0 : return shiftlen;
2601 : : }
2602 : :
2603 : : /**
2604 : : * skb_prepare_seq_read - Prepare a sequential read of skb data
2605 : : * @skb: the buffer to read
2606 : : * @from: lower offset of data to be read
2607 : : * @to: upper offset of data to be read
2608 : : * @st: state variable
2609 : : *
2610 : : * Initializes the specified state variable. Must be called before
2611 : : * invoking skb_seq_read() for the first time.
2612 : : */
2613 : 0 : void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2614 : : unsigned int to, struct skb_seq_state *st)
2615 : : {
2616 : 0 : st->lower_offset = from;
2617 : 0 : st->upper_offset = to;
2618 : 0 : st->root_skb = st->cur_skb = skb;
2619 : 0 : st->frag_idx = st->stepped_offset = 0;
2620 : 0 : st->frag_data = NULL;
2621 : 0 : }
2622 : : EXPORT_SYMBOL(skb_prepare_seq_read);
2623 : :
2624 : : /**
2625 : : * skb_seq_read - Sequentially read skb data
2626 : : * @consumed: number of bytes consumed by the caller so far
2627 : : * @data: destination pointer for data to be returned
2628 : : * @st: state variable
2629 : : *
2630 : : * Reads a block of skb data at @consumed relative to the
2631 : : * lower offset specified to skb_prepare_seq_read(). Assigns
2632 : : * the head of the data block to @data and returns the length
2633 : : * of the block or 0 if the end of the skb data or the upper
2634 : : * offset has been reached.
2635 : : *
2636 : : * The caller is not required to consume all of the data
2637 : : * returned, i.e. @consumed is typically set to the number
2638 : : * of bytes already consumed and the next call to
2639 : : * skb_seq_read() will return the remaining part of the block.
2640 : : *
2641 : : * Note 1: The size of each block of data returned can be arbitrary,
2642 : : * this limitation is the cost for zerocopy seqeuental
2643 : : * reads of potentially non linear data.
2644 : : *
2645 : : * Note 2: Fragment lists within fragments are not implemented
2646 : : * at the moment, state->root_skb could be replaced with
2647 : : * a stack for this purpose.
2648 : : */
2649 : 0 : unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2650 : : struct skb_seq_state *st)
2651 : : {
2652 : 0 : unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2653 : 0 : skb_frag_t *frag;
2654 : :
2655 [ # # ]: 0 : if (unlikely(abs_offset >= st->upper_offset)) {
2656 [ # # ]: 0 : if (st->frag_data) {
2657 : 0 : kunmap_atomic(st->frag_data);
2658 : 0 : st->frag_data = NULL;
2659 : : }
2660 : : return 0;
2661 : : }
2662 : :
2663 : : next_skb:
2664 : 0 : block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2665 : :
2666 [ # # ][ # # ]: 0 : if (abs_offset < block_limit && !st->frag_data) {
2667 : 0 : *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2668 : 0 : return block_limit - abs_offset;
2669 : : }
2670 : :
2671 [ # # ][ # # ]: 0 : if (st->frag_idx == 0 && !st->frag_data)
2672 : 0 : st->stepped_offset += skb_headlen(st->cur_skb);
2673 : :
2674 [ # # ]: 0 : while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2675 : 0 : frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2676 : 0 : block_limit = skb_frag_size(frag) + st->stepped_offset;
2677 : :
2678 [ # # ]: 0 : if (abs_offset < block_limit) {
2679 [ # # ]: 0 : if (!st->frag_data)
2680 : 0 : st->frag_data = kmap_atomic(skb_frag_page(frag));
2681 : :
2682 : 0 : *data = (u8 *) st->frag_data + frag->page_offset +
2683 : 0 : (abs_offset - st->stepped_offset);
2684 : :
2685 : 0 : return block_limit - abs_offset;
2686 : : }
2687 : :
2688 [ # # ]: 0 : if (st->frag_data) {
2689 : 0 : kunmap_atomic(st->frag_data);
2690 : 0 : st->frag_data = NULL;
2691 : : }
2692 : :
2693 : 0 : st->frag_idx++;
2694 : 0 : st->stepped_offset += skb_frag_size(frag);
2695 : : }
2696 : :
2697 [ # # ]: 0 : if (st->frag_data) {
2698 : 0 : kunmap_atomic(st->frag_data);
2699 : 0 : st->frag_data = NULL;
2700 : : }
2701 : :
2702 [ # # ][ # # ]: 0 : if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2703 : 0 : st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2704 : 0 : st->frag_idx = 0;
2705 : 0 : goto next_skb;
2706 [ # # ]: 0 : } else if (st->cur_skb->next) {
2707 : 0 : st->cur_skb = st->cur_skb->next;
2708 : 0 : st->frag_idx = 0;
2709 : 0 : goto next_skb;
2710 : : }
2711 : :
2712 : : return 0;
2713 : : }
2714 : : EXPORT_SYMBOL(skb_seq_read);
2715 : :
2716 : : /**
2717 : : * skb_abort_seq_read - Abort a sequential read of skb data
2718 : : * @st: state variable
2719 : : *
2720 : : * Must be called if skb_seq_read() was not called until it
2721 : : * returned 0.
2722 : : */
2723 : 0 : void skb_abort_seq_read(struct skb_seq_state *st)
2724 : : {
2725 [ # # ][ # # ]: 0 : if (st->frag_data)
2726 : 0 : kunmap_atomic(st->frag_data);
2727 : 0 : }
2728 : : EXPORT_SYMBOL(skb_abort_seq_read);
2729 : :
2730 : : #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2731 : :
2732 : 0 : static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2733 : : struct ts_config *conf,
2734 : : struct ts_state *state)
2735 : : {
2736 : 0 : return skb_seq_read(offset, text, TS_SKB_CB(state));
2737 : : }
2738 : :
2739 : 0 : static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2740 : : {
2741 : : skb_abort_seq_read(TS_SKB_CB(state));
2742 : 0 : }
2743 : :
2744 : : /**
2745 : : * skb_find_text - Find a text pattern in skb data
2746 : : * @skb: the buffer to look in
2747 : : * @from: search offset
2748 : : * @to: search limit
2749 : : * @config: textsearch configuration
2750 : : * @state: uninitialized textsearch state variable
2751 : : *
2752 : : * Finds a pattern in the skb data according to the specified
2753 : : * textsearch configuration. Use textsearch_next() to retrieve
2754 : : * subsequent occurrences of the pattern. Returns the offset
2755 : : * to the first occurrence or UINT_MAX if no match was found.
2756 : : */
2757 : 0 : unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2758 : : unsigned int to, struct ts_config *config,
2759 : : struct ts_state *state)
2760 : : {
2761 : : unsigned int ret;
2762 : :
2763 : 0 : config->get_next_block = skb_ts_get_next_block;
2764 : 0 : config->finish = skb_ts_finish;
2765 : :
2766 : : skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2767 : :
2768 : : ret = textsearch_find(config, state);
2769 [ # # ]: 0 : return (ret <= to - from ? ret : UINT_MAX);
2770 : : }
2771 : : EXPORT_SYMBOL(skb_find_text);
2772 : :
2773 : : /**
2774 : : * skb_append_datato_frags - append the user data to a skb
2775 : : * @sk: sock structure
2776 : : * @skb: skb structure to be appened with user data.
2777 : : * @getfrag: call back function to be used for getting the user data
2778 : : * @from: pointer to user message iov
2779 : : * @length: length of the iov message
2780 : : *
2781 : : * Description: This procedure append the user data in the fragment part
2782 : : * of the skb if any page alloc fails user this procedure returns -ENOMEM
2783 : : */
2784 : 0 : int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2785 : : int (*getfrag)(void *from, char *to, int offset,
2786 : : int len, int odd, struct sk_buff *skb),
2787 : : void *from, int length)
2788 : : {
2789 : 0 : int frg_cnt = skb_shinfo(skb)->nr_frags;
2790 : : int copy;
2791 : : int offset = 0;
2792 : : int ret;
2793 : 0 : struct page_frag *pfrag = ¤t->task_frag;
2794 : :
2795 : : do {
2796 : : /* Return error if we don't have space for new frag */
2797 [ # # ]: 0 : if (frg_cnt >= MAX_SKB_FRAGS)
2798 : : return -EMSGSIZE;
2799 : :
2800 [ # # ]: 0 : if (!sk_page_frag_refill(sk, pfrag))
2801 : : return -ENOMEM;
2802 : :
2803 : : /* copy the user data to page */
2804 : 0 : copy = min_t(int, length, pfrag->size - pfrag->offset);
2805 : :
2806 : 0 : ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2807 : : offset, copy, 0, skb);
2808 [ # # ]: 0 : if (ret < 0)
2809 : : return -EFAULT;
2810 : :
2811 : : /* copy was successful so update the size parameters */
2812 : 0 : skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2813 : : copy);
2814 : 0 : frg_cnt++;
2815 : 0 : pfrag->offset += copy;
2816 : 0 : get_page(pfrag->page);
2817 : :
2818 : 0 : skb->truesize += copy;
2819 : 0 : atomic_add(copy, &sk->sk_wmem_alloc);
2820 : 0 : skb->len += copy;
2821 : 0 : skb->data_len += copy;
2822 : 0 : offset += copy;
2823 : 0 : length -= copy;
2824 : :
2825 [ # # ]: 0 : } while (length > 0);
2826 : :
2827 : : return 0;
2828 : : }
2829 : : EXPORT_SYMBOL(skb_append_datato_frags);
2830 : :
2831 : : /**
2832 : : * skb_pull_rcsum - pull skb and update receive checksum
2833 : : * @skb: buffer to update
2834 : : * @len: length of data pulled
2835 : : *
2836 : : * This function performs an skb_pull on the packet and updates
2837 : : * the CHECKSUM_COMPLETE checksum. It should be used on
2838 : : * receive path processing instead of skb_pull unless you know
2839 : : * that the checksum difference is zero (e.g., a valid IP header)
2840 : : * or you are setting ip_summed to CHECKSUM_NONE.
2841 : : */
2842 : 0 : unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2843 : : {
2844 [ # # ]: 0 : BUG_ON(len > skb->len);
2845 : 0 : skb->len -= len;
2846 [ # # ]: 0 : BUG_ON(skb->len < skb->data_len);
2847 : 0 : skb_postpull_rcsum(skb, skb->data, len);
2848 : 0 : return skb->data += len;
2849 : : }
2850 : : EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2851 : :
2852 : : /**
2853 : : * skb_segment - Perform protocol segmentation on skb.
2854 : : * @head_skb: buffer to segment
2855 : : * @features: features for the output path (see dev->features)
2856 : : *
2857 : : * This function performs segmentation on the given skb. It returns
2858 : : * a pointer to the first in a list of new skbs for the segments.
2859 : : * In case of error it returns ERR_PTR(err).
2860 : : */
2861 : 0 : struct sk_buff *skb_segment(struct sk_buff *head_skb,
2862 : : netdev_features_t features)
2863 : : {
2864 : : struct sk_buff *segs = NULL;
2865 : : struct sk_buff *tail = NULL;
2866 : 0 : struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2867 : 0 : skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2868 : 0 : unsigned int mss = skb_shinfo(head_skb)->gso_size;
2869 : 0 : unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2870 : : struct sk_buff *frag_skb = head_skb;
2871 : : unsigned int offset = doffset;
2872 : 0 : unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2873 : : unsigned int headroom;
2874 : : unsigned int len;
2875 : : __be16 proto;
2876 : : bool csum;
2877 : 0 : int sg = !!(features & NETIF_F_SG);
2878 : 0 : int nfrags = skb_shinfo(head_skb)->nr_frags;
2879 : : int err = -ENOMEM;
2880 : : int i = 0;
2881 : : int pos;
2882 : : int dummy;
2883 : :
2884 : 0 : proto = skb_network_protocol(head_skb, &dummy);
2885 [ # # ]: 0 : if (unlikely(!proto))
2886 : : return ERR_PTR(-EINVAL);
2887 : :
2888 : : csum = !!can_checksum_protocol(features, proto);
2889 : : __skb_push(head_skb, doffset);
2890 : : headroom = skb_headroom(head_skb);
2891 : 0 : pos = skb_headlen(head_skb);
2892 : :
2893 : : do {
2894 : 0 : struct sk_buff *nskb;
2895 : 0 : skb_frag_t *nskb_frag;
2896 : : int hsize;
2897 : : int size;
2898 : :
2899 : 0 : len = head_skb->len - offset;
2900 [ # # ]: 0 : if (len > mss)
2901 : : len = mss;
2902 : :
2903 : 0 : hsize = skb_headlen(head_skb) - offset;
2904 [ # # ]: 0 : if (hsize < 0)
2905 : : hsize = 0;
2906 [ # # ]: 0 : if (hsize > len || !sg)
2907 : 0 : hsize = len;
2908 : :
2909 [ # # ][ # # ]: 0 : if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
[ # # ]
2910 [ # # ]: 0 : (skb_headlen(list_skb) == len || sg)) {
2911 [ # # ]: 0 : BUG_ON(skb_headlen(list_skb) > len);
2912 : :
2913 : : i = 0;
2914 : 0 : nfrags = skb_shinfo(list_skb)->nr_frags;
2915 : 0 : frag = skb_shinfo(list_skb)->frags;
2916 : : frag_skb = list_skb;
2917 : 0 : pos += skb_headlen(list_skb);
2918 : :
2919 [ # # ]: 0 : while (pos < offset + len) {
2920 [ # # ]: 0 : BUG_ON(i >= nfrags);
2921 : :
2922 : 0 : size = skb_frag_size(frag);
2923 [ # # ]: 0 : if (pos + size > offset + len)
2924 : : break;
2925 : :
2926 : 0 : i++;
2927 : : pos += size;
2928 : 0 : frag++;
2929 : : }
2930 : :
2931 : 0 : nskb = skb_clone(list_skb, GFP_ATOMIC);
2932 : 0 : list_skb = list_skb->next;
2933 : :
2934 [ # # ]: 0 : if (unlikely(!nskb))
2935 : : goto err;
2936 : :
2937 [ # # ]: 0 : if (unlikely(pskb_trim(nskb, len))) {
2938 : 0 : kfree_skb(nskb);
2939 : 0 : goto err;
2940 : : }
2941 : :
2942 : : hsize = skb_end_offset(nskb);
2943 [ # # ]: 0 : if (skb_cow_head(nskb, doffset + headroom)) {
2944 : 0 : kfree_skb(nskb);
2945 : 0 : goto err;
2946 : : }
2947 : :
2948 : 0 : nskb->truesize += skb_end_offset(nskb) - hsize;
2949 : 0 : skb_release_head_state(nskb);
2950 : : __skb_push(nskb, doffset);
2951 : : } else {
2952 : 0 : nskb = __alloc_skb(hsize + doffset + headroom,
2953 : : GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
2954 : : NUMA_NO_NODE);
2955 : :
2956 [ # # ]: 0 : if (unlikely(!nskb))
2957 : : goto err;
2958 : :
2959 : : skb_reserve(nskb, headroom);
2960 : : __skb_put(nskb, doffset);
2961 : : }
2962 : :
2963 [ # # ]: 0 : if (segs)
2964 : 0 : tail->next = nskb;
2965 : : else
2966 : : segs = nskb;
2967 : : tail = nskb;
2968 : :
2969 : 0 : __copy_skb_header(nskb, head_skb);
2970 : 0 : nskb->mac_len = head_skb->mac_len;
2971 : :
2972 : 0 : skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
2973 : :
2974 : 0 : skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
2975 : 0 : nskb->data - tnl_hlen,
2976 : : doffset + tnl_hlen);
2977 : :
2978 [ # # ]: 0 : if (nskb->len == len + doffset)
2979 : : goto perform_csum_check;
2980 : :
2981 [ # # ]: 0 : if (!sg) {
2982 : 0 : nskb->ip_summed = CHECKSUM_NONE;
2983 : 0 : nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
2984 : 0 : skb_put(nskb, len),
2985 : : len, 0);
2986 : 0 : continue;
2987 : : }
2988 : :
2989 : 0 : nskb_frag = skb_shinfo(nskb)->frags;
2990 : :
2991 : : skb_copy_from_linear_data_offset(head_skb, offset,
2992 : 0 : skb_put(nskb, hsize), hsize);
2993 : :
2994 : 0 : skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
2995 : : SKBTX_SHARED_FRAG;
2996 : :
2997 [ # # ]: 0 : while (pos < offset + len) {
2998 [ # # ]: 0 : if (i >= nfrags) {
2999 [ # # ]: 0 : BUG_ON(skb_headlen(list_skb));
3000 : :
3001 : : i = 0;
3002 : 0 : nfrags = skb_shinfo(list_skb)->nr_frags;
3003 : 0 : frag = skb_shinfo(list_skb)->frags;
3004 : : frag_skb = list_skb;
3005 : :
3006 [ # # ]: 0 : BUG_ON(!nfrags);
3007 : :
3008 : 0 : list_skb = list_skb->next;
3009 : : }
3010 : :
3011 [ # # ]: 0 : if (unlikely(skb_shinfo(nskb)->nr_frags >=
3012 : : MAX_SKB_FRAGS)) {
3013 [ # # ]: 0 : net_warn_ratelimited(
3014 : : "skb_segment: too many frags: %u %u\n",
3015 : : pos, mss);
3016 : : goto err;
3017 : : }
3018 : :
3019 [ # # ]: 0 : if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3020 : : goto err;
3021 : :
3022 : 0 : *nskb_frag = *frag;
3023 : : __skb_frag_ref(nskb_frag);
3024 : 0 : size = skb_frag_size(nskb_frag);
3025 : :
3026 [ # # ]: 0 : if (pos < offset) {
3027 : 0 : nskb_frag->page_offset += offset - pos;
3028 : 0 : skb_frag_size_sub(nskb_frag, offset - pos);
3029 : : }
3030 : :
3031 : 0 : skb_shinfo(nskb)->nr_frags++;
3032 : :
3033 [ # # ]: 0 : if (pos + size <= offset + len) {
3034 : 0 : i++;
3035 : 0 : frag++;
3036 : : pos += size;
3037 : : } else {
3038 : 0 : skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3039 : : goto skip_fraglist;
3040 : : }
3041 : :
3042 : 0 : nskb_frag++;
3043 : : }
3044 : :
3045 : : skip_fraglist:
3046 : 0 : nskb->data_len = len - hsize;
3047 : 0 : nskb->len += nskb->data_len;
3048 : 0 : nskb->truesize += nskb->data_len;
3049 : :
3050 : : perform_csum_check:
3051 [ # # ]: 0 : if (!csum) {
3052 : 0 : nskb->csum = skb_checksum(nskb, doffset,
3053 : 0 : nskb->len - doffset, 0);
3054 : 0 : nskb->ip_summed = CHECKSUM_NONE;
3055 : : }
3056 [ # # ]: 0 : } while ((offset += len) < head_skb->len);
3057 : :
3058 : : return segs;
3059 : :
3060 : : err:
3061 : : kfree_skb_list(segs);
3062 : : return ERR_PTR(err);
3063 : : }
3064 : : EXPORT_SYMBOL_GPL(skb_segment);
3065 : :
3066 : 0 : int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3067 : : {
3068 : : struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3069 : : unsigned int offset = skb_gro_offset(skb);
3070 : : unsigned int headlen = skb_headlen(skb);
3071 : 0 : struct sk_buff *nskb, *lp, *p = *head;
3072 : : unsigned int len = skb_gro_len(skb);
3073 : : unsigned int delta_truesize;
3074 : : unsigned int headroom;
3075 : :
3076 [ # # ]: 0 : if (unlikely(p->len + len >= 65536))
3077 : : return -E2BIG;
3078 : :
3079 [ # # ]: 0 : lp = NAPI_GRO_CB(p)->last ?: p;
3080 : : pinfo = skb_shinfo(lp);
3081 : :
3082 [ # # ]: 0 : if (headlen <= offset) {
3083 : : skb_frag_t *frag;
3084 : : skb_frag_t *frag2;
3085 : 0 : int i = skbinfo->nr_frags;
3086 : 0 : int nr_frags = pinfo->nr_frags + i;
3087 : :
3088 [ # # ]: 0 : if (nr_frags > MAX_SKB_FRAGS)
3089 : : goto merge;
3090 : :
3091 : 0 : offset -= headlen;
3092 : 0 : pinfo->nr_frags = nr_frags;
3093 : 0 : skbinfo->nr_frags = 0;
3094 : :
3095 : 0 : frag = pinfo->frags + nr_frags;
3096 : 0 : frag2 = skbinfo->frags + i;
3097 : : do {
3098 : 0 : *--frag = *--frag2;
3099 [ # # ]: 0 : } while (--i);
3100 : :
3101 : 0 : frag->page_offset += offset;
3102 : : skb_frag_size_sub(frag, offset);
3103 : :
3104 : : /* all fragments truesize : remove (head size + sk_buff) */
3105 : 0 : delta_truesize = skb->truesize -
3106 : : SKB_TRUESIZE(skb_end_offset(skb));
3107 : :
3108 : 0 : skb->truesize -= skb->data_len;
3109 : 0 : skb->len -= skb->data_len;
3110 : 0 : skb->data_len = 0;
3111 : :
3112 : 0 : NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3113 : 0 : goto done;
3114 [ # # ]: 0 : } else if (skb->head_frag) {
3115 : 0 : int nr_frags = pinfo->nr_frags;
3116 : 0 : skb_frag_t *frag = pinfo->frags + nr_frags;
3117 : 0 : struct page *page = virt_to_head_page(skb->head);
3118 : 0 : unsigned int first_size = headlen - offset;
3119 : : unsigned int first_offset;
3120 : :
3121 [ # # ]: 0 : if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3122 : : goto merge;
3123 : :
3124 : 0 : first_offset = skb->data -
3125 : 0 : (unsigned char *)page_address(page) +
3126 : : offset;
3127 : :
3128 : 0 : pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3129 : :
3130 : 0 : frag->page.p = page;
3131 : 0 : frag->page_offset = first_offset;
3132 : : skb_frag_size_set(frag, first_size);
3133 : :
3134 : 0 : memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3135 : : /* We dont need to clear skbinfo->nr_frags here */
3136 : :
3137 : 0 : delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3138 : 0 : NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3139 : 0 : goto done;
3140 : : }
3141 [ # # ]: 0 : if (pinfo->frag_list)
3142 : : goto merge;
3143 [ # # ]: 0 : if (skb_gro_len(p) != pinfo->gso_size)
3144 : : return -E2BIG;
3145 : :
3146 : : headroom = skb_headroom(p);
3147 : 0 : nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3148 [ # # ]: 0 : if (unlikely(!nskb))
3149 : : return -ENOMEM;
3150 : :
3151 : 0 : __copy_skb_header(nskb, p);
3152 : 0 : nskb->mac_len = p->mac_len;
3153 : :
3154 : : skb_reserve(nskb, headroom);
3155 : : __skb_put(nskb, skb_gro_offset(p));
3156 : :
3157 : 0 : skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3158 : : skb_set_network_header(nskb, skb_network_offset(p));
3159 : : skb_set_transport_header(nskb, skb_transport_offset(p));
3160 : :
3161 : : __skb_pull(p, skb_gro_offset(p));
3162 : 0 : memcpy(skb_mac_header(nskb), skb_mac_header(p),
3163 : 0 : p->data - skb_mac_header(p));
3164 : :
3165 : 0 : skb_shinfo(nskb)->frag_list = p;
3166 : 0 : skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3167 : 0 : pinfo->gso_size = 0;
3168 : : skb_header_release(p);
3169 : 0 : NAPI_GRO_CB(nskb)->last = p;
3170 : :
3171 : 0 : nskb->data_len += p->len;
3172 : 0 : nskb->truesize += p->truesize;
3173 : 0 : nskb->len += p->len;
3174 : :
3175 : 0 : *head = nskb;
3176 : 0 : nskb->next = p->next;
3177 : 0 : p->next = NULL;
3178 : :
3179 : : p = nskb;
3180 : :
3181 : : merge:
3182 : 0 : delta_truesize = skb->truesize;
3183 [ # # ]: 0 : if (offset > headlen) {
3184 : 0 : unsigned int eat = offset - headlen;
3185 : :
3186 : 0 : skbinfo->frags[0].page_offset += eat;
3187 : : skb_frag_size_sub(&skbinfo->frags[0], eat);
3188 : 0 : skb->data_len -= eat;
3189 : 0 : skb->len -= eat;
3190 : : offset = headlen;
3191 : : }
3192 : :
3193 : : __skb_pull(skb, offset);
3194 : :
3195 [ # # ]: 0 : if (!NAPI_GRO_CB(p)->last)
3196 : 0 : skb_shinfo(p)->frag_list = skb;
3197 : : else
3198 : 0 : NAPI_GRO_CB(p)->last->next = skb;
3199 : 0 : NAPI_GRO_CB(p)->last = skb;
3200 : : skb_header_release(skb);
3201 : : lp = p;
3202 : :
3203 : : done:
3204 : 0 : NAPI_GRO_CB(p)->count++;
3205 : 0 : p->data_len += len;
3206 : 0 : p->truesize += delta_truesize;
3207 : 0 : p->len += len;
3208 [ # # ]: 0 : if (lp != p) {
3209 : 0 : lp->data_len += len;
3210 : 0 : lp->truesize += delta_truesize;
3211 : 0 : lp->len += len;
3212 : : }
3213 : 0 : NAPI_GRO_CB(skb)->same_flow = 1;
3214 : 0 : return 0;
3215 : : }
3216 : : EXPORT_SYMBOL_GPL(skb_gro_receive);
3217 : :
3218 : 0 : void __init skb_init(void)
3219 : : {
3220 : 0 : skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3221 : : sizeof(struct sk_buff),
3222 : : 0,
3223 : : SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3224 : : NULL);
3225 : 0 : skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3226 : : (2*sizeof(struct sk_buff)) +
3227 : : sizeof(atomic_t),
3228 : : 0,
3229 : : SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3230 : : NULL);
3231 : 0 : }
3232 : :
3233 : : /**
3234 : : * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3235 : : * @skb: Socket buffer containing the buffers to be mapped
3236 : : * @sg: The scatter-gather list to map into
3237 : : * @offset: The offset into the buffer's contents to start mapping
3238 : : * @len: Length of buffer space to be mapped
3239 : : *
3240 : : * Fill the specified scatter-gather list with mappings/pointers into a
3241 : : * region of the buffer space attached to a socket buffer.
3242 : : */
3243 : : static int
3244 : 0 : __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3245 : : {
3246 : 0 : int start = skb_headlen(skb);
3247 : 0 : int i, copy = start - offset;
3248 : : struct sk_buff *frag_iter;
3249 : : int elt = 0;
3250 : :
3251 [ # # ]: 0 : if (copy > 0) {
3252 [ # # ]: 0 : if (copy > len)
3253 : : copy = len;
3254 : 0 : sg_set_buf(sg, skb->data + offset, copy);
3255 : : elt++;
3256 [ # # ]: 0 : if ((len -= copy) == 0)
3257 : : return elt;
3258 : 0 : offset += copy;
3259 : : }
3260 : :
3261 [ # # ]: 0 : for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3262 : : int end;
3263 : :
3264 [ # # ]: 0 : WARN_ON(start > offset + len);
3265 : :
3266 : 0 : end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3267 [ # # ]: 0 : if ((copy = end - offset) > 0) {
3268 : 0 : skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3269 : :
3270 [ # # ]: 0 : if (copy > len)
3271 : : copy = len;
3272 : 0 : sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3273 : 0 : frag->page_offset+offset-start);
3274 : 0 : elt++;
3275 [ # # ]: 0 : if (!(len -= copy))
3276 : : return elt;
3277 : 0 : offset += copy;
3278 : : }
3279 : : start = end;
3280 : : }
3281 : :
3282 [ # # ]: 0 : skb_walk_frags(skb, frag_iter) {
3283 : : int end;
3284 : :
3285 [ # # ]: 0 : WARN_ON(start > offset + len);
3286 : :
3287 : 0 : end = start + frag_iter->len;
3288 [ # # ]: 0 : if ((copy = end - offset) > 0) {
3289 [ # # ]: 0 : if (copy > len)
3290 : : copy = len;
3291 : 0 : elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3292 : : copy);
3293 [ # # ]: 0 : if ((len -= copy) == 0)
3294 : : return elt;
3295 : 0 : offset += copy;
3296 : : }
3297 : : start = end;
3298 : : }
3299 [ # # ]: 0 : BUG_ON(len);
3300 : : return elt;
3301 : : }
3302 : :
3303 : 0 : int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3304 : : {
3305 : 0 : int nsg = __skb_to_sgvec(skb, sg, offset, len);
3306 : :
3307 : 0 : sg_mark_end(&sg[nsg - 1]);
3308 : :
3309 : 0 : return nsg;
3310 : : }
3311 : : EXPORT_SYMBOL_GPL(skb_to_sgvec);
3312 : :
3313 : : /**
3314 : : * skb_cow_data - Check that a socket buffer's data buffers are writable
3315 : : * @skb: The socket buffer to check.
3316 : : * @tailbits: Amount of trailing space to be added
3317 : : * @trailer: Returned pointer to the skb where the @tailbits space begins
3318 : : *
3319 : : * Make sure that the data buffers attached to a socket buffer are
3320 : : * writable. If they are not, private copies are made of the data buffers
3321 : : * and the socket buffer is set to use these instead.
3322 : : *
3323 : : * If @tailbits is given, make sure that there is space to write @tailbits
3324 : : * bytes of data beyond current end of socket buffer. @trailer will be
3325 : : * set to point to the skb in which this space begins.
3326 : : *
3327 : : * The number of scatterlist elements required to completely map the
3328 : : * COW'd and extended socket buffer will be returned.
3329 : : */
3330 : 0 : int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3331 : : {
3332 : : int copyflag;
3333 : : int elt;
3334 : 0 : struct sk_buff *skb1, **skb_p;
3335 : :
3336 : : /* If skb is cloned or its head is paged, reallocate
3337 : : * head pulling out all the pages (pages are considered not writable
3338 : : * at the moment even if they are anonymous).
3339 : : */
3340 [ # # ]: 0 : if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
[ # # # # ]
3341 : 0 : __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3342 : : return -ENOMEM;
3343 : :
3344 : : /* Easy case. Most of packets will go this way. */
3345 [ # # ]: 0 : if (!skb_has_frag_list(skb)) {
3346 : : /* A little of trouble, not enough of space for trailer.
3347 : : * This should not happen, when stack is tuned to generate
3348 : : * good frames. OK, on miss we reallocate and reserve even more
3349 : : * space, 128 bytes is fair. */
3350 : :
3351 [ # # # # ]: 0 : if (skb_tailroom(skb) < tailbits &&
3352 : 0 : pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3353 : : return -ENOMEM;
3354 : :
3355 : : /* Voila! */
3356 : 0 : *trailer = skb;
3357 : 0 : return 1;
3358 : : }
3359 : :
3360 : : /* Misery. We are in troubles, going to mincer fragments... */
3361 : :
3362 : : elt = 1;
3363 : 0 : skb_p = &skb_shinfo(skb)->frag_list;
3364 : : copyflag = 0;
3365 : :
3366 [ # # ]: 0 : while ((skb1 = *skb_p) != NULL) {
3367 : : int ntail = 0;
3368 : :
3369 : : /* The fragment is partially pulled by someone,
3370 : : * this can happen on input. Copy it and everything
3371 : : * after it. */
3372 : :
3373 [ # # ]: 0 : if (skb_shared(skb1))
3374 : : copyflag = 1;
3375 : :
3376 : : /* If the skb is the last, worry about trailer. */
3377 : :
3378 [ # # ][ # # ]: 0 : if (skb1->next == NULL && tailbits) {
3379 [ # # ][ # # ]: 0 : if (skb_shinfo(skb1)->nr_frags ||
3380 [ # # ]: 0 : skb_has_frag_list(skb1) ||
3381 : : skb_tailroom(skb1) < tailbits)
3382 : 0 : ntail = tailbits + 128;
3383 : : }
3384 : :
3385 [ # # ][ # # ]: 0 : if (copyflag ||
3386 [ # # ]: 0 : skb_cloned(skb1) ||
3387 [ # # ]: 0 : ntail ||
3388 [ # # ]: 0 : skb_shinfo(skb1)->nr_frags ||
3389 : : skb_has_frag_list(skb1)) {
3390 : : struct sk_buff *skb2;
3391 : :
3392 : : /* Fuck, we are miserable poor guys... */
3393 [ # # ]: 0 : if (ntail == 0)
3394 : 0 : skb2 = skb_copy(skb1, GFP_ATOMIC);
3395 : : else
3396 : 0 : skb2 = skb_copy_expand(skb1,
3397 : : skb_headroom(skb1),
3398 : : ntail,
3399 : : GFP_ATOMIC);
3400 [ # # ]: 0 : if (unlikely(skb2 == NULL))
3401 : : return -ENOMEM;
3402 : :
3403 [ # # ]: 0 : if (skb1->sk)
3404 : : skb_set_owner_w(skb2, skb1->sk);
3405 : :
3406 : : /* Looking around. Are we still alive?
3407 : : * OK, link new skb, drop old one */
3408 : :
3409 : 0 : skb2->next = skb1->next;
3410 : 0 : *skb_p = skb2;
3411 : 0 : kfree_skb(skb1);
3412 : : skb1 = skb2;
3413 : : }
3414 : 0 : elt++;
3415 : 0 : *trailer = skb1;
3416 : 0 : skb_p = &skb1->next;
3417 : : }
3418 : :
3419 : : return elt;
3420 : : }
3421 : : EXPORT_SYMBOL_GPL(skb_cow_data);
3422 : :
3423 : 0 : static void sock_rmem_free(struct sk_buff *skb)
3424 : : {
3425 : 0 : struct sock *sk = skb->sk;
3426 : :
3427 : 0 : atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3428 : 0 : }
3429 : :
3430 : : /*
3431 : : * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3432 : : */
3433 : 0 : int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3434 : : {
3435 : 0 : int len = skb->len;
3436 : :
3437 [ # # ]: 0 : if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3438 : 0 : (unsigned int)sk->sk_rcvbuf)
3439 : : return -ENOMEM;
3440 : :
3441 : : skb_orphan(skb);
3442 : 0 : skb->sk = sk;
3443 : 0 : skb->destructor = sock_rmem_free;
3444 : 0 : atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3445 : :
3446 : : /* before exiting rcu section, make sure dst is refcounted */
3447 : : skb_dst_force(skb);
3448 : :
3449 : 0 : skb_queue_tail(&sk->sk_error_queue, skb);
3450 [ # # ]: 0 : if (!sock_flag(sk, SOCK_DEAD))
3451 : 0 : sk->sk_data_ready(sk, len);
3452 : : return 0;
3453 : : }
3454 : : EXPORT_SYMBOL(sock_queue_err_skb);
3455 : :
3456 : 0 : void skb_tstamp_tx(struct sk_buff *orig_skb,
3457 : : struct skb_shared_hwtstamps *hwtstamps)
3458 : : {
3459 : 0 : struct sock *sk = orig_skb->sk;
3460 : : struct sock_exterr_skb *serr;
3461 : : struct sk_buff *skb;
3462 : : int err;
3463 : :
3464 [ # # ]: 0 : if (!sk)
3465 : : return;
3466 : :
3467 [ # # ]: 0 : if (hwtstamps) {
3468 : 0 : *skb_hwtstamps(orig_skb) =
3469 : : *hwtstamps;
3470 : : } else {
3471 : : /*
3472 : : * no hardware time stamps available,
3473 : : * so keep the shared tx_flags and only
3474 : : * store software time stamp
3475 : : */
3476 : 0 : orig_skb->tstamp = ktime_get_real();
3477 : : }
3478 : :
3479 : 0 : skb = skb_clone(orig_skb, GFP_ATOMIC);
3480 [ # # ]: 0 : if (!skb)
3481 : : return;
3482 : :
3483 : 0 : serr = SKB_EXT_ERR(skb);
3484 : 0 : memset(serr, 0, sizeof(*serr));
3485 : 0 : serr->ee.ee_errno = ENOMSG;
3486 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3487 : :
3488 : 0 : err = sock_queue_err_skb(sk, skb);
3489 : :
3490 [ # # ]: 0 : if (err)
3491 : 0 : kfree_skb(skb);
3492 : : }
3493 : : EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3494 : :
3495 : 0 : void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3496 : : {
3497 : 0 : struct sock *sk = skb->sk;
3498 : : struct sock_exterr_skb *serr;
3499 : : int err;
3500 : :
3501 : 0 : skb->wifi_acked_valid = 1;
3502 : 0 : skb->wifi_acked = acked;
3503 : :
3504 : 0 : serr = SKB_EXT_ERR(skb);
3505 : 0 : memset(serr, 0, sizeof(*serr));
3506 : 0 : serr->ee.ee_errno = ENOMSG;
3507 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3508 : :
3509 : 0 : err = sock_queue_err_skb(sk, skb);
3510 [ # # ]: 0 : if (err)
3511 : 0 : kfree_skb(skb);
3512 : 0 : }
3513 : : EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3514 : :
3515 : :
3516 : : /**
3517 : : * skb_partial_csum_set - set up and verify partial csum values for packet
3518 : : * @skb: the skb to set
3519 : : * @start: the number of bytes after skb->data to start checksumming.
3520 : : * @off: the offset from start to place the checksum.
3521 : : *
3522 : : * For untrusted partially-checksummed packets, we need to make sure the values
3523 : : * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3524 : : *
3525 : : * This function checks and sets those values and skb->ip_summed: if this
3526 : : * returns false you should drop the packet.
3527 : : */
3528 : 0 : bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3529 : : {
3530 [ # # ][ # # ]: 0 : if (unlikely(start > skb_headlen(skb)) ||
3531 : 0 : unlikely((int)start + off > skb_headlen(skb) - 2)) {
3532 [ # # ]: 0 : net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3533 : : start, off, skb_headlen(skb));
3534 : : return false;
3535 : : }
3536 : 0 : skb->ip_summed = CHECKSUM_PARTIAL;
3537 : 0 : skb->csum_start = skb_headroom(skb) + start;
3538 : 0 : skb->csum_offset = off;
3539 : : skb_set_transport_header(skb, start);
3540 : 0 : return true;
3541 : : }
3542 : : EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3543 : :
3544 : 0 : static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3545 : : unsigned int max)
3546 : : {
3547 [ # # ]: 0 : if (skb_headlen(skb) >= len)
3548 : : return 0;
3549 : :
3550 : : /* If we need to pullup then pullup to the max, so we
3551 : : * won't need to do it again.
3552 : : */
3553 [ # # ]: 0 : if (max > skb->len)
3554 : : max = skb->len;
3555 : :
3556 [ # # ]: 0 : if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3557 : : return -ENOMEM;
3558 : :
3559 [ # # ]: 0 : if (skb_headlen(skb) < len)
3560 : : return -EPROTO;
3561 : :
3562 : 0 : return 0;
3563 : : }
3564 : :
3565 : : /* This value should be large enough to cover a tagged ethernet header plus
3566 : : * maximally sized IP and TCP or UDP headers.
3567 : : */
3568 : : #define MAX_IP_HDR_LEN 128
3569 : :
3570 : 0 : static int skb_checksum_setup_ip(struct sk_buff *skb, bool recalculate)
3571 : : {
3572 : : unsigned int off;
3573 : : bool fragment;
3574 : : int err;
3575 : :
3576 : : fragment = false;
3577 : :
3578 : 0 : err = skb_maybe_pull_tail(skb,
3579 : : sizeof(struct iphdr),
3580 : : MAX_IP_HDR_LEN);
3581 [ # # ]: 0 : if (err < 0)
3582 : : goto out;
3583 : :
3584 [ # # ]: 0 : if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3585 : : fragment = true;
3586 : :
3587 : : off = ip_hdrlen(skb);
3588 : :
3589 : : err = -EPROTO;
3590 : :
3591 [ # # ]: 0 : if (fragment)
3592 : : goto out;
3593 : :
3594 [ # # # ]: 0 : switch (ip_hdr(skb)->protocol) {
3595 : : case IPPROTO_TCP:
3596 : 0 : err = skb_maybe_pull_tail(skb,
3597 : : off + sizeof(struct tcphdr),
3598 : : MAX_IP_HDR_LEN);
3599 [ # # ]: 0 : if (err < 0)
3600 : : goto out;
3601 : :
3602 [ # # ]: 0 : if (!skb_partial_csum_set(skb, off,
3603 : : offsetof(struct tcphdr, check))) {
3604 : : err = -EPROTO;
3605 : : goto out;
3606 : : }
3607 : :
3608 [ # # ]: 0 : if (recalculate)
3609 : 0 : tcp_hdr(skb)->check =
3610 : 0 : ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3611 : : ip_hdr(skb)->daddr,
3612 : 0 : skb->len - off,
3613 : : IPPROTO_TCP, 0);
3614 : : break;
3615 : : case IPPROTO_UDP:
3616 : 0 : err = skb_maybe_pull_tail(skb,
3617 : : off + sizeof(struct udphdr),
3618 : : MAX_IP_HDR_LEN);
3619 [ # # ]: 0 : if (err < 0)
3620 : : goto out;
3621 : :
3622 [ # # ]: 0 : if (!skb_partial_csum_set(skb, off,
3623 : : offsetof(struct udphdr, check))) {
3624 : : err = -EPROTO;
3625 : : goto out;
3626 : : }
3627 : :
3628 [ # # ]: 0 : if (recalculate)
3629 : 0 : udp_hdr(skb)->check =
3630 : 0 : ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3631 : : ip_hdr(skb)->daddr,
3632 : 0 : skb->len - off,
3633 : : IPPROTO_UDP, 0);
3634 : : break;
3635 : : default:
3636 : : goto out;
3637 : : }
3638 : :
3639 : : err = 0;
3640 : :
3641 : : out:
3642 : 0 : return err;
3643 : : }
3644 : :
3645 : : /* This value should be large enough to cover a tagged ethernet header plus
3646 : : * an IPv6 header, all options, and a maximal TCP or UDP header.
3647 : : */
3648 : : #define MAX_IPV6_HDR_LEN 256
3649 : :
3650 : : #define OPT_HDR(type, skb, off) \
3651 : : (type *)(skb_network_header(skb) + (off))
3652 : :
3653 : 0 : static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3654 : : {
3655 : : int err;
3656 : : u8 nexthdr;
3657 : : unsigned int off;
3658 : : unsigned int len;
3659 : : bool fragment;
3660 : : bool done;
3661 : :
3662 : : fragment = false;
3663 : : done = false;
3664 : :
3665 : : off = sizeof(struct ipv6hdr);
3666 : :
3667 : 0 : err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3668 [ # # ]: 0 : if (err < 0)
3669 : : goto out;
3670 : :
3671 : 0 : nexthdr = ipv6_hdr(skb)->nexthdr;
3672 : :
3673 : 0 : len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3674 [ # # ]: 0 : while (off <= len && !done) {
3675 [ # # # # ]: 0 : switch (nexthdr) {
3676 : : case IPPROTO_DSTOPTS:
3677 : : case IPPROTO_HOPOPTS:
3678 : : case IPPROTO_ROUTING: {
3679 : : struct ipv6_opt_hdr *hp;
3680 : :
3681 : 0 : err = skb_maybe_pull_tail(skb,
3682 : : off +
3683 : : sizeof(struct ipv6_opt_hdr),
3684 : : MAX_IPV6_HDR_LEN);
3685 [ # # ]: 0 : if (err < 0)
3686 : : goto out;
3687 : :
3688 : 0 : hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3689 : 0 : nexthdr = hp->nexthdr;
3690 : 0 : off += ipv6_optlen(hp);
3691 : 0 : break;
3692 : : }
3693 : : case IPPROTO_AH: {
3694 : : struct ip_auth_hdr *hp;
3695 : :
3696 : 0 : err = skb_maybe_pull_tail(skb,
3697 : : off +
3698 : : sizeof(struct ip_auth_hdr),
3699 : : MAX_IPV6_HDR_LEN);
3700 [ # # ]: 0 : if (err < 0)
3701 : : goto out;
3702 : :
3703 : 0 : hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3704 : 0 : nexthdr = hp->nexthdr;
3705 : 0 : off += ipv6_authlen(hp);
3706 : 0 : break;
3707 : : }
3708 : : case IPPROTO_FRAGMENT: {
3709 : : struct frag_hdr *hp;
3710 : :
3711 : 0 : err = skb_maybe_pull_tail(skb,
3712 : : off +
3713 : : sizeof(struct frag_hdr),
3714 : : MAX_IPV6_HDR_LEN);
3715 [ # # ]: 0 : if (err < 0)
3716 : : goto out;
3717 : :
3718 : 0 : hp = OPT_HDR(struct frag_hdr, skb, off);
3719 : :
3720 [ # # ]: 0 : if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3721 : : fragment = true;
3722 : :
3723 : 0 : nexthdr = hp->nexthdr;
3724 : : off += sizeof(struct frag_hdr);
3725 : 0 : break;
3726 : : }
3727 : : default:
3728 : : done = true;
3729 : : break;
3730 : : }
3731 : : }
3732 : :
3733 : : err = -EPROTO;
3734 : :
3735 [ # # ]: 0 : if (!done || fragment)
3736 : : goto out;
3737 : :
3738 [ # # # ]: 0 : switch (nexthdr) {
3739 : : case IPPROTO_TCP:
3740 : 0 : err = skb_maybe_pull_tail(skb,
3741 : : off + sizeof(struct tcphdr),
3742 : : MAX_IPV6_HDR_LEN);
3743 [ # # ]: 0 : if (err < 0)
3744 : : goto out;
3745 : :
3746 [ # # ]: 0 : if (!skb_partial_csum_set(skb, off,
3747 : : offsetof(struct tcphdr, check))) {
3748 : : err = -EPROTO;
3749 : : goto out;
3750 : : }
3751 : :
3752 [ # # ]: 0 : if (recalculate)
3753 : 0 : tcp_hdr(skb)->check =
3754 : 0 : ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3755 : 0 : &ipv6_hdr(skb)->daddr,
3756 : 0 : skb->len - off,
3757 : : IPPROTO_TCP, 0);
3758 : : break;
3759 : : case IPPROTO_UDP:
3760 : 0 : err = skb_maybe_pull_tail(skb,
3761 : : off + sizeof(struct udphdr),
3762 : : MAX_IPV6_HDR_LEN);
3763 [ # # ]: 0 : if (err < 0)
3764 : : goto out;
3765 : :
3766 [ # # ]: 0 : if (!skb_partial_csum_set(skb, off,
3767 : : offsetof(struct udphdr, check))) {
3768 : : err = -EPROTO;
3769 : : goto out;
3770 : : }
3771 : :
3772 [ # # ]: 0 : if (recalculate)
3773 : 0 : udp_hdr(skb)->check =
3774 : 0 : ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3775 : 0 : &ipv6_hdr(skb)->daddr,
3776 : 0 : skb->len - off,
3777 : : IPPROTO_UDP, 0);
3778 : : break;
3779 : : default:
3780 : : goto out;
3781 : : }
3782 : :
3783 : : err = 0;
3784 : :
3785 : : out:
3786 : 0 : return err;
3787 : : }
3788 : :
3789 : : /**
3790 : : * skb_checksum_setup - set up partial checksum offset
3791 : : * @skb: the skb to set up
3792 : : * @recalculate: if true the pseudo-header checksum will be recalculated
3793 : : */
3794 : 0 : int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3795 : : {
3796 : : int err;
3797 : :
3798 [ # # # ]: 0 : switch (skb->protocol) {
3799 : : case htons(ETH_P_IP):
3800 : 0 : err = skb_checksum_setup_ip(skb, recalculate);
3801 : 0 : break;
3802 : :
3803 : : case htons(ETH_P_IPV6):
3804 : 0 : err = skb_checksum_setup_ipv6(skb, recalculate);
3805 : 0 : break;
3806 : :
3807 : : default:
3808 : : err = -EPROTO;
3809 : : break;
3810 : : }
3811 : :
3812 : 0 : return err;
3813 : : }
3814 : : EXPORT_SYMBOL(skb_checksum_setup);
3815 : :
3816 : 0 : void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3817 : : {
3818 [ # # ]: 0 : net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3819 : : skb->dev->name);
3820 : 0 : }
3821 : : EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3822 : :
3823 : 0 : void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3824 : : {
3825 [ # # ]: 0 : if (head_stolen) {
3826 : 0 : skb_release_head_state(skb);
3827 : 0 : kmem_cache_free(skbuff_head_cache, skb);
3828 : : } else {
3829 : : __kfree_skb(skb);
3830 : : }
3831 : 0 : }
3832 : : EXPORT_SYMBOL(kfree_skb_partial);
3833 : :
3834 : : /**
3835 : : * skb_try_coalesce - try to merge skb to prior one
3836 : : * @to: prior buffer
3837 : : * @from: buffer to add
3838 : : * @fragstolen: pointer to boolean
3839 : : * @delta_truesize: how much more was allocated than was requested
3840 : : */
3841 : 0 : bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3842 : : bool *fragstolen, int *delta_truesize)
3843 : : {
3844 : 0 : int i, delta, len = from->len;
3845 : :
3846 : 0 : *fragstolen = false;
3847 : :
3848 [ # # ]: 0 : if (skb_cloned(to))
3849 : : return false;
3850 : :
3851 [ # # ]: 0 : if (len <= skb_tailroom(to)) {
3852 [ # # ]: 0 : BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3853 : 0 : *delta_truesize = 0;
3854 : 0 : return true;
3855 : : }
3856 : :
3857 [ # # ][ # # ]: 0 : if (skb_has_frag_list(to) || skb_has_frag_list(from))
3858 : : return false;
3859 : :
3860 [ # # ]: 0 : if (skb_headlen(from) != 0) {
3861 : : struct page *page;
3862 : : unsigned int offset;
3863 : :
3864 [ # # ]: 0 : if (skb_shinfo(to)->nr_frags +
3865 : 0 : skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3866 : : return false;
3867 : :
3868 [ # # ]: 0 : if (skb_head_is_locked(from))
3869 : : return false;
3870 : :
3871 : 0 : delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3872 : :
3873 : 0 : page = virt_to_head_page(from->head);
3874 : 0 : offset = from->data - (unsigned char *)page_address(page);
3875 : :
3876 : 0 : skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3877 : : page, offset, skb_headlen(from));
3878 : 0 : *fragstolen = true;
3879 : : } else {
3880 [ # # ]: 0 : if (skb_shinfo(to)->nr_frags +
3881 : 0 : skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3882 : : return false;
3883 : :
3884 : 0 : delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3885 : : }
3886 : :
3887 [ # # ][ # # ]: 0 : WARN_ON_ONCE(delta < len);
[ # # ]
3888 : :
3889 : 0 : memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3890 : 0 : skb_shinfo(from)->frags,
3891 : 0 : skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3892 : 0 : skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3893 : :
3894 [ # # ]: 0 : if (!skb_cloned(from))
3895 : 0 : skb_shinfo(from)->nr_frags = 0;
3896 : :
3897 : : /* if the skb is not cloned this does nothing
3898 : : * since we set nr_frags to 0.
3899 : : */
3900 [ # # ]: 0 : for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3901 : : skb_frag_ref(from, i);
3902 : :
3903 : 0 : to->truesize += delta;
3904 : 0 : to->len += len;
3905 : 0 : to->data_len += len;
3906 : :
3907 : 0 : *delta_truesize = delta;
3908 : 0 : return true;
3909 : : }
3910 : : EXPORT_SYMBOL(skb_try_coalesce);
3911 : :
3912 : : /**
3913 : : * skb_scrub_packet - scrub an skb
3914 : : *
3915 : : * @skb: buffer to clean
3916 : : * @xnet: packet is crossing netns
3917 : : *
3918 : : * skb_scrub_packet can be used after encapsulating or decapsulting a packet
3919 : : * into/from a tunnel. Some information have to be cleared during these
3920 : : * operations.
3921 : : * skb_scrub_packet can also be used to clean a skb before injecting it in
3922 : : * another namespace (@xnet == true). We have to clear all information in the
3923 : : * skb that could impact namespace isolation.
3924 : : */
3925 : 0 : void skb_scrub_packet(struct sk_buff *skb, bool xnet)
3926 : : {
3927 [ # # ]: 0 : if (xnet)
3928 : : skb_orphan(skb);
3929 : 0 : skb->tstamp.tv64 = 0;
3930 : 0 : skb->pkt_type = PACKET_HOST;
3931 : 0 : skb->skb_iif = 0;
3932 : 0 : skb->local_df = 0;
3933 : : skb_dst_drop(skb);
3934 : 0 : skb->mark = 0;
3935 : : secpath_reset(skb);
3936 : : nf_reset(skb);
3937 : : nf_reset_trace(skb);
3938 : 0 : }
3939 : : EXPORT_SYMBOL_GPL(skb_scrub_packet);
3940 : :
3941 : : /**
3942 : : * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3943 : : *
3944 : : * @skb: GSO skb
3945 : : *
3946 : : * skb_gso_transport_seglen is used to determine the real size of the
3947 : : * individual segments, including Layer4 headers (TCP/UDP).
3948 : : *
3949 : : * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3950 : : */
3951 : 0 : unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3952 : : {
3953 : : const struct skb_shared_info *shinfo = skb_shinfo(skb);
3954 : : unsigned int hdr_len;
3955 : :
3956 [ # # ]: 0 : if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3957 : : hdr_len = tcp_hdrlen(skb);
3958 : : else
3959 : : hdr_len = sizeof(struct udphdr);
3960 : 0 : return hdr_len + shinfo->gso_size;
3961 : : }
3962 : : EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
|