blob: 74d3c36f8419b9082579d0907fcb5e890568b625 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030052#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070063#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000064#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070065#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040066#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000072#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
74
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Eric Dumazetd7e88832012-04-30 08:10:34 +000081struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010083int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Jean Sacrenf05de732013-02-11 13:30:38 +000087 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000091 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Jean Sacrenf05de732013-02-11 13:30:38 +000098static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000099 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Joe Perchese005d192012-05-16 19:58:40 +0000101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000102 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 BUG();
106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jean Sacrenf05de732013-02-11 13:30:38 +0000110 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
161/**
David S. Millerd179cd12005-08-17 14:57:30 -0700162 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * @size: size to allocate
164 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800169 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
176 * %GFP_ATOMIC.
177 */
Al Virodd0fc662005-10-07 07:46:04 +0100178struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700179 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Christoph Lametere18b8902006-12-06 20:33:20 -0800181 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800182 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct sk_buff *skb;
184 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
189
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!skb)
196 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700197 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
203 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000204 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!data)
208 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
212 */
213 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700214 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300216 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300220 */
221 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700224 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300225 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 skb->head = data;
227 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700228 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700229 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000232
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800236 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000237 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238
Mel Gormanc93bdd02012-07-31 16:44:19 -0700239 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700240 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700242 fclones = container_of(skb, struct sk_buff_fclones, skb1);
243
244 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700245 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300246 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700247
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800248 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250out:
251 return skb;
252nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800253 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 skb = NULL;
255 goto out;
256}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800257EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700260 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000261 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700262 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000263 *
264 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100265 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700266 * @frag_size is 0, otherwise data should come from the page allocator
267 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000268 * The return is the new skb buffer.
269 * On a failure the return is %NULL, and @data is not freed.
270 * Notes :
271 * Before IO, driver allocates only data buffer where NIC put incoming frame
272 * Driver should add room at head (NET_SKB_PAD) and
273 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
274 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
275 * before giving packet to stack.
276 * RX rings only contains data buffers, not full skbs.
277 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700278struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000279{
280 struct skb_shared_info *shinfo;
281 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000282 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283
284 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
285 if (!skb)
286 return NULL;
287
Eric Dumazetd3836f22012-04-27 00:33:38 +0000288 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000289
290 memset(skb, 0, offsetof(struct sk_buff, tail));
291 skb->truesize = SKB_TRUESIZE(size);
Reshetova, Elena63354792017-06-30 13:07:58 +0300292 refcount_set(&skb->users, 1);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 skb->head = data;
294 skb->data = data;
295 skb_reset_tail_pointer(skb);
296 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800297 skb->mac_header = (typeof(skb->mac_header))~0U;
298 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299
300 /* make sure we initialize shinfo sequentially */
301 shinfo = skb_shinfo(skb);
302 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
303 atomic_set(&shinfo->dataref, 1);
304 kmemcheck_annotate_variable(shinfo->destructor_arg);
305
306 return skb;
307}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700308
309/* build_skb() is wrapper over __build_skb(), that specifically
310 * takes care of skb->head and skb->pfmemalloc
311 * This means that if @frag_size is not zero, then @data must be backed
312 * by a page fragment, not kmalloc() or vmalloc()
313 */
314struct sk_buff *build_skb(void *data, unsigned int frag_size)
315{
316 struct sk_buff *skb = __build_skb(data, frag_size);
317
318 if (skb && frag_size) {
319 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700320 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700321 skb->pfmemalloc = 1;
322 }
323 return skb;
324}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000325EXPORT_SYMBOL(build_skb);
326
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100327#define NAPI_SKB_CACHE_SIZE 64
328
329struct napi_alloc_cache {
330 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300331 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100332 void *skb_cache[NAPI_SKB_CACHE_SIZE];
333};
334
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700335static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100336static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800337
338static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
339{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700340 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800341 unsigned long flags;
342 void *data;
343
344 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700345 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800346 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000347 local_irq_restore(flags);
348 return data;
349}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700350
351/**
352 * netdev_alloc_frag - allocate a page fragment
353 * @fragsz: fragment size
354 *
355 * Allocates a frag from a page for receive buffer.
356 * Uses GFP_ATOMIC allocations.
357 */
358void *netdev_alloc_frag(unsigned int fragsz)
359{
360 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
361}
Eric Dumazet6f532612012-05-18 05:12:12 +0000362EXPORT_SYMBOL(netdev_alloc_frag);
363
Alexander Duyckffde7322014-12-09 19:40:42 -0800364static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
365{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100366 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700367
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800368 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800369}
370
371void *napi_alloc_frag(unsigned int fragsz)
372{
373 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
374}
375EXPORT_SYMBOL(napi_alloc_frag);
376
Eric Dumazet6f532612012-05-18 05:12:12 +0000377/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900380 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
382 *
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
387 *
388 * %NULL is returned if there is no free memory.
389 */
Alexander Duyck94519802015-05-06 21:11:40 -0700390struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
391 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800392{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700393 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700394 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800395 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700396 bool pfmemalloc;
397 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800398
Alexander Duyck94519802015-05-06 21:11:40 -0700399 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800400
Alexander Duyck94519802015-05-06 21:11:40 -0700401 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800402 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700403 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
404 if (!skb)
405 goto skb_fail;
406 goto skb_success;
407 }
Alexander Duyck94519802015-05-06 21:11:40 -0700408
409 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
410 len = SKB_DATA_ALIGN(len);
411
412 if (sk_memalloc_socks())
413 gfp_mask |= __GFP_MEMALLOC;
414
415 local_irq_save(flags);
416
417 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800418 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700419 pfmemalloc = nc->pfmemalloc;
420
421 local_irq_restore(flags);
422
423 if (unlikely(!data))
424 return NULL;
425
426 skb = __build_skb(data, len);
427 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700428 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700429 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700430 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431
Alexander Duyck94519802015-05-06 21:11:40 -0700432 /* use OR instead of assignment to avoid clearing of bits in mask */
433 if (pfmemalloc)
434 skb->pfmemalloc = 1;
435 skb->head_frag = 1;
436
Alexander Duycka080e7b2015-05-13 13:34:13 -0700437skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700438 skb_reserve(skb, NET_SKB_PAD);
439 skb->dev = dev;
440
Alexander Duycka080e7b2015-05-13 13:34:13 -0700441skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700442 return skb;
443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800444EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Alexander Duyckfd11a832014-12-09 19:40:49 -0800446/**
447 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
448 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900449 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800450 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
451 *
452 * Allocate a new sk_buff for use in NAPI receive. This buffer will
453 * attempt to allocate the head from a special reserved region used
454 * only for NAPI Rx allocation. By doing this we can save several
455 * CPU cycles by avoiding having to disable and re-enable IRQs.
456 *
457 * %NULL is returned if there is no free memory.
458 */
Alexander Duyck94519802015-05-06 21:11:40 -0700459struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
460 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800461{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100462 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800463 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700464 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800465
Alexander Duyck94519802015-05-06 21:11:40 -0700466 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467
Alexander Duyck94519802015-05-06 21:11:40 -0700468 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800469 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700470 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
471 if (!skb)
472 goto skb_fail;
473 goto skb_success;
474 }
Alexander Duyck94519802015-05-06 21:11:40 -0700475
476 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
477 len = SKB_DATA_ALIGN(len);
478
479 if (sk_memalloc_socks())
480 gfp_mask |= __GFP_MEMALLOC;
481
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800482 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700483 if (unlikely(!data))
484 return NULL;
485
486 skb = __build_skb(data, len);
487 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700488 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700489 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490 }
491
Alexander Duyck94519802015-05-06 21:11:40 -0700492 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100493 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700494 skb->pfmemalloc = 1;
495 skb->head_frag = 1;
496
Alexander Duycka080e7b2015-05-13 13:34:13 -0700497skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700498 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
499 skb->dev = napi->dev;
500
Alexander Duycka080e7b2015-05-13 13:34:13 -0700501skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800502 return skb;
503}
504EXPORT_SYMBOL(__napi_alloc_skb);
505
Peter Zijlstra654bed12008-10-07 14:22:33 -0700506void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000507 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700508{
509 skb_fill_page_desc(skb, i, page, off, size);
510 skb->len += size;
511 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000512 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700513}
514EXPORT_SYMBOL(skb_add_rx_frag);
515
Jason Wangf8e617e2013-11-01 14:07:47 +0800516void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
517 unsigned int truesize)
518{
519 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
520
521 skb_frag_size_add(frag, size);
522 skb->len += size;
523 skb->data_len += size;
524 skb->truesize += truesize;
525}
526EXPORT_SYMBOL(skb_coalesce_rx_frag);
527
Herbert Xu27b437c2006-07-13 19:26:39 -0700528static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700530 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700531 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Herbert Xu27b437c2006-07-13 19:26:39 -0700534static inline void skb_drop_fraglist(struct sk_buff *skb)
535{
536 skb_drop_list(&skb_shinfo(skb)->frag_list);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static void skb_clone_fraglist(struct sk_buff *skb)
540{
541 struct sk_buff *list;
542
David S. Millerfbb398a2009-06-09 00:18:59 -0700543 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 skb_get(list);
545}
546
Eric Dumazetd3836f22012-04-27 00:33:38 +0000547static void skb_free_head(struct sk_buff *skb)
548{
Alexander Duyck181edb22015-05-06 21:12:03 -0700549 unsigned char *head = skb->head;
550
Eric Dumazetd3836f22012-04-27 00:33:38 +0000551 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700552 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000553 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700554 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000555}
556
Adrian Bunk5bba1712006-06-29 13:02:35 -0700557static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Eric Dumazetff04a772014-09-23 18:39:30 -0700559 struct skb_shared_info *shinfo = skb_shinfo(skb);
560 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Dumazetff04a772014-09-23 18:39:30 -0700562 if (skb->cloned &&
563 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
564 &shinfo->dataref))
565 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000566
Eric Dumazetff04a772014-09-23 18:39:30 -0700567 for (i = 0; i < shinfo->nr_frags; i++)
568 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000569
Eric Dumazetff04a772014-09-23 18:39:30 -0700570 if (shinfo->frag_list)
571 kfree_skb_list(shinfo->frag_list);
572
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400573 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700574 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * Free an skbuff by memory without cleaning the state.
579 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800580static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700582 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700583
David S. Millerd179cd12005-08-17 14:57:30 -0700584 switch (skb->fclone) {
585 case SKB_FCLONE_UNAVAILABLE:
586 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800587 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700588
589 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700590 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800591
592 /* We usually free the clone (TX completion) before original skb
593 * This test would have no chance to be true for the clone,
594 * while here, branch prediction will be good.
595 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300596 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800597 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700598 break;
599
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800600 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700601 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700602 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700603 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300604 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800605 return;
606fastpath:
607 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Paolo Abeni0a463c72017-06-12 11:23:42 +0200610void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Eric Dumazetadf30902009-06-02 05:19:30 +0000612 skb_dst_drop(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200613 secpath_reset(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700614 if (skb->destructor) {
615 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 skb->destructor(skb);
617 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000618#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100619 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100620#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200621#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 nf_bridge_put(skb->nf_bridge);
623#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700624}
625
626/* Free everything but the sk_buff shell. */
627static void skb_release_all(struct sk_buff *skb)
628{
629 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200630 if (likely(skb->head))
631 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Herbert Xu2d4baff2007-11-26 23:11:19 +0800634/**
635 * __kfree_skb - private function
636 * @skb: buffer
637 *
638 * Free an sk_buff. Release anything attached to the buffer.
639 * Clean the state. This is an internal helper function. Users should
640 * always call kfree_skb
641 */
642
643void __kfree_skb(struct sk_buff *skb)
644{
645 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kfree_skbmem(skb);
647}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800648EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800651 * kfree_skb - free an sk_buff
652 * @skb: buffer to free
653 *
654 * Drop a reference to the buffer and free it if the usage count has
655 * hit zero.
656 */
657void kfree_skb(struct sk_buff *skb)
658{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200659 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800660 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200661
Neil Hormanead2ceb2009-03-11 09:49:55 +0000662 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800663 __kfree_skb(skb);
664}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800665EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800666
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700667void kfree_skb_list(struct sk_buff *segs)
668{
669 while (segs) {
670 struct sk_buff *next = segs->next;
671
672 kfree_skb(segs);
673 segs = next;
674 }
675}
676EXPORT_SYMBOL(kfree_skb_list);
677
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700678/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000679 * skb_tx_error - report an sk_buff xmit error
680 * @skb: buffer that triggered an error
681 *
682 * Report xmit error if a device callback is tracking this skb.
683 * skb must be freed afterwards.
684 */
685void skb_tx_error(struct sk_buff *skb)
686{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400687 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000688}
689EXPORT_SYMBOL(skb_tx_error);
690
691/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000692 * consume_skb - free an skbuff
693 * @skb: buffer to free
694 *
695 * Drop a ref to the buffer and free it if the usage count has hit zero
696 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
697 * is being dropped after a failure and notes that
698 */
699void consume_skb(struct sk_buff *skb)
700{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200701 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000702 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200703
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900704 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000705 __kfree_skb(skb);
706}
707EXPORT_SYMBOL(consume_skb);
708
Paolo Abeni0a463c72017-06-12 11:23:42 +0200709/**
710 * consume_stateless_skb - free an skbuff, assuming it is stateless
711 * @skb: buffer to free
712 *
713 * Works like consume_skb(), but this variant assumes that all the head
714 * states have been already dropped.
715 */
716void consume_stateless_skb(struct sk_buff *skb)
717{
718 if (!skb_unref(skb))
719 return;
720
721 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200722 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200723 kfree_skbmem(skb);
724}
725
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100726void __kfree_skb_flush(void)
727{
728 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
729
730 /* flush skb_cache if containing objects */
731 if (nc->skb_count) {
732 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
733 nc->skb_cache);
734 nc->skb_count = 0;
735 }
736}
737
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100738static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100739{
740 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
741
742 /* drop skb->head and call any destructors for packet */
743 skb_release_all(skb);
744
745 /* record skb to CPU local list */
746 nc->skb_cache[nc->skb_count++] = skb;
747
748#ifdef CONFIG_SLUB
749 /* SLUB writes into objects when freeing */
750 prefetchw(skb);
751#endif
752
753 /* flush skb_cache if it is filled */
754 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
755 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
756 nc->skb_cache);
757 nc->skb_count = 0;
758 }
759}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100760void __kfree_skb_defer(struct sk_buff *skb)
761{
762 _kfree_skb_defer(skb);
763}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100764
765void napi_consume_skb(struct sk_buff *skb, int budget)
766{
767 if (unlikely(!skb))
768 return;
769
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100770 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100771 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100772 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100773 return;
774 }
775
Paolo Abeni76088942017-06-14 11:48:48 +0200776 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100777 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200778
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100779 /* if reaching here SKB is ready to free */
780 trace_consume_skb(skb);
781
782 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700783 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100784 __kfree_skb(skb);
785 return;
786 }
787
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100788 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100789}
790EXPORT_SYMBOL(napi_consume_skb);
791
Eric Dumazetb1937222014-09-28 22:18:47 -0700792/* Make sure a field is enclosed inside headers_start/headers_end section */
793#define CHECK_SKB_FIELD(field) \
794 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
795 offsetof(struct sk_buff, headers_start)); \
796 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
797 offsetof(struct sk_buff, headers_end)); \
798
Herbert Xudec18812007-10-14 00:37:30 -0700799static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
800{
801 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700802 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700803 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700804 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000805 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700806#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700807 new->sp = secpath_get(old->sp);
808#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700809 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700810
Eric Dumazetb1937222014-09-28 22:18:47 -0700811 /* Note : this field could be in headers_start/headers_end section
812 * It is not yet because we do not want to have a 16 bit hole
813 */
814 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300815
Eric Dumazetb1937222014-09-28 22:18:47 -0700816 memcpy(&new->headers_start, &old->headers_start,
817 offsetof(struct sk_buff, headers_end) -
818 offsetof(struct sk_buff, headers_start));
819 CHECK_SKB_FIELD(protocol);
820 CHECK_SKB_FIELD(csum);
821 CHECK_SKB_FIELD(hash);
822 CHECK_SKB_FIELD(priority);
823 CHECK_SKB_FIELD(skb_iif);
824 CHECK_SKB_FIELD(vlan_proto);
825 CHECK_SKB_FIELD(vlan_tci);
826 CHECK_SKB_FIELD(transport_header);
827 CHECK_SKB_FIELD(network_header);
828 CHECK_SKB_FIELD(mac_header);
829 CHECK_SKB_FIELD(inner_protocol);
830 CHECK_SKB_FIELD(inner_transport_header);
831 CHECK_SKB_FIELD(inner_network_header);
832 CHECK_SKB_FIELD(inner_mac_header);
833 CHECK_SKB_FIELD(mark);
834#ifdef CONFIG_NETWORK_SECMARK
835 CHECK_SKB_FIELD(secmark);
836#endif
Cong Wange0d10952013-08-01 11:10:25 +0800837#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700838 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300839#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800840#ifdef CONFIG_XPS
841 CHECK_SKB_FIELD(sender_cpu);
842#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700843#ifdef CONFIG_NET_SCHED
844 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700845#endif
846
Herbert Xudec18812007-10-14 00:37:30 -0700847}
848
Herbert Xu82c49a32009-05-22 22:11:37 +0000849/*
850 * You should not add any new code to this function. Add it to
851 * __copy_skb_header above instead.
852 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700853static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#define C(x) n->x = skb->x
856
857 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700859 __copy_skb_header(n, skb);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 C(len);
862 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700863 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700864 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800865 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 C(tail);
869 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800870 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000871 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800872 C(data);
873 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300874 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 atomic_inc(&(skb_shinfo(skb)->dataref));
877 skb->cloned = 1;
878
879 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700880#undef C
881}
882
883/**
884 * skb_morph - morph one skb into another
885 * @dst: the skb to receive the contents
886 * @src: the skb to supply the contents
887 *
888 * This is identical to skb_clone except that the target skb is
889 * supplied by the user.
890 *
891 * The target skb is returned upon exit.
892 */
893struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
894{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800895 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700896 return __skb_clone(dst, src);
897}
898EXPORT_SYMBOL_GPL(skb_morph);
899
Willem de Bruijn52267792017-08-03 16:29:39 -0400900struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
901{
902 struct ubuf_info *uarg;
903 struct sk_buff *skb;
904
905 WARN_ON_ONCE(!in_task());
906
Willem de Bruijn76851d12017-08-03 16:29:40 -0400907 if (!sock_flag(sk, SOCK_ZEROCOPY))
908 return NULL;
909
Willem de Bruijn52267792017-08-03 16:29:39 -0400910 skb = sock_omalloc(sk, 0, GFP_KERNEL);
911 if (!skb)
912 return NULL;
913
914 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
915 uarg = (void *)skb->cb;
916
917 uarg->callback = sock_zerocopy_callback;
918 uarg->desc = atomic_inc_return(&sk->sk_zckey) - 1;
919 uarg->zerocopy = 1;
920 atomic_set(&uarg->refcnt, 0);
921 sock_hold(sk);
922
923 return uarg;
924}
925EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
926
927static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
928{
929 return container_of((void *)uarg, struct sk_buff, cb);
930}
931
932void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
933{
934 struct sk_buff *skb = skb_from_uarg(uarg);
935 struct sock_exterr_skb *serr;
936 struct sock *sk = skb->sk;
937 u16 id = uarg->desc;
938
939 if (sock_flag(sk, SOCK_DEAD))
940 goto release;
941
942 serr = SKB_EXT_ERR(skb);
943 memset(serr, 0, sizeof(*serr));
944 serr->ee.ee_errno = 0;
945 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
946 serr->ee.ee_data = id;
947 if (!success)
948 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
949
950 skb_queue_tail(&sk->sk_error_queue, skb);
951 skb = NULL;
952
953 sk->sk_error_report(sk);
954
955release:
956 consume_skb(skb);
957 sock_put(sk);
958}
959EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
960
961void sock_zerocopy_put(struct ubuf_info *uarg)
962{
963 if (uarg && atomic_dec_and_test(&uarg->refcnt)) {
964 if (uarg->callback)
965 uarg->callback(uarg, uarg->zerocopy);
966 else
967 consume_skb(skb_from_uarg(uarg));
968 }
969}
970EXPORT_SYMBOL_GPL(sock_zerocopy_put);
971
972void sock_zerocopy_put_abort(struct ubuf_info *uarg)
973{
974 if (uarg) {
975 struct sock *sk = skb_from_uarg(uarg)->sk;
976
977 atomic_dec(&sk->sk_zckey);
978
979 /* sock_zerocopy_put expects a ref. Most sockets take one per
980 * skb, which is zero on abort. tcp_sendmsg holds one extra, to
981 * avoid an skb send inside the main loop triggering uarg free.
982 */
983 if (sk->sk_type != SOCK_STREAM)
984 atomic_inc(&uarg->refcnt);
985
986 sock_zerocopy_put(uarg);
987 }
988}
989EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
990
991extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
992 struct iov_iter *from, size_t length);
993
994int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
995 struct msghdr *msg, int len,
996 struct ubuf_info *uarg)
997{
998 struct iov_iter orig_iter = msg->msg_iter;
999 int err, orig_len = skb->len;
1000
1001 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1002 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1003 /* Streams do not free skb on error. Reset to prev state. */
1004 msg->msg_iter = orig_iter;
1005 ___pskb_trim(skb, orig_len);
1006 return err;
1007 }
1008
1009 skb_zcopy_set(skb, uarg);
1010 return skb->len - orig_len;
1011}
1012EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1013
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001014static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001015 gfp_t gfp_mask)
1016{
1017 if (skb_zcopy(orig)) {
1018 if (skb_zcopy(nskb)) {
1019 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1020 if (!gfp_mask) {
1021 WARN_ON_ONCE(1);
1022 return -ENOMEM;
1023 }
1024 if (skb_uarg(nskb) == skb_uarg(orig))
1025 return 0;
1026 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1027 return -EIO;
1028 }
1029 skb_zcopy_set(nskb, skb_uarg(orig));
1030 }
1031 return 0;
1032}
1033
Ben Hutchings2c530402012-07-10 10:55:09 +00001034/**
1035 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001036 * @skb: the skb to modify
1037 * @gfp_mask: allocation priority
1038 *
1039 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1040 * It will copy all frags into kernel and drop the reference
1041 * to userspace pages.
1042 *
1043 * If this function is called from an interrupt gfp_mask() must be
1044 * %GFP_ATOMIC.
1045 *
1046 * Returns 0 on success or a negative error code on failure
1047 * to allocate kernel memory to copy to.
1048 */
1049int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001050{
Shirley Maa6686f22011-07-06 12:22:12 +00001051 int num_frags = skb_shinfo(skb)->nr_frags;
1052 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001053 int i, new_frags;
1054 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001055
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001056 if (!num_frags)
1057 return 0;
Shirley Maa6686f22011-07-06 12:22:12 +00001058
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001059 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1060 return -EINVAL;
1061
1062 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1063 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001064 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001065 if (!page) {
1066 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001067 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001068 put_page(head);
1069 head = next;
1070 }
1071 return -ENOMEM;
1072 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001073 set_page_private(page, (unsigned long)head);
1074 head = page;
1075 }
1076
1077 page = head;
1078 d_off = 0;
1079 for (i = 0; i < num_frags; i++) {
1080 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1081 u32 p_off, p_len, copied;
1082 struct page *p;
1083 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001084
1085 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1086 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001087 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001088 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001089
1090 while (done < p_len) {
1091 if (d_off == PAGE_SIZE) {
1092 d_off = 0;
1093 page = (struct page *)page_private(page);
1094 }
1095 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1096 memcpy(page_address(page) + d_off,
1097 vaddr + p_off + done, copy);
1098 done += copy;
1099 d_off += copy;
1100 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001101 kunmap_atomic(vaddr);
1102 }
Shirley Maa6686f22011-07-06 12:22:12 +00001103 }
1104
1105 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001106 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001107 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001108
Shirley Maa6686f22011-07-06 12:22:12 +00001109 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001110 for (i = 0; i < new_frags - 1; i++) {
1111 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001112 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001113 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001114 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1115 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001116
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001117 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001118 return 0;
1119}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001120EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001121
Herbert Xue0053ec2007-10-14 00:37:52 -07001122/**
1123 * skb_clone - duplicate an sk_buff
1124 * @skb: buffer to clone
1125 * @gfp_mask: allocation priority
1126 *
1127 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1128 * copies share the same packet data but not structure. The new
1129 * buffer has a reference count of 1. If the allocation fails the
1130 * function returns %NULL otherwise the new buffer is returned.
1131 *
1132 * If this function is called from an interrupt gfp_mask() must be
1133 * %GFP_ATOMIC.
1134 */
1135
1136struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1137{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001138 struct sk_buff_fclones *fclones = container_of(skb,
1139 struct sk_buff_fclones,
1140 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001141 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001142
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001143 if (skb_orphan_frags(skb, gfp_mask))
1144 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001145
Herbert Xue0053ec2007-10-14 00:37:52 -07001146 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001147 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001148 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001149 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001150 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001151 if (skb_pfmemalloc(skb))
1152 gfp_mask |= __GFP_MEMALLOC;
1153
Herbert Xue0053ec2007-10-14 00:37:52 -07001154 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1155 if (!n)
1156 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001157
1158 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001159 n->fclone = SKB_FCLONE_UNAVAILABLE;
1160 }
1161
1162 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001164EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001166static void skb_headers_offset_update(struct sk_buff *skb, int off)
1167{
Eric Dumazet030737b2013-10-19 11:42:54 -07001168 /* Only adjust this if it actually is csum_start rather than csum */
1169 if (skb->ip_summed == CHECKSUM_PARTIAL)
1170 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001171 /* {transport,network,mac}_header and tail are relative to skb->head */
1172 skb->transport_header += off;
1173 skb->network_header += off;
1174 if (skb_mac_header_was_set(skb))
1175 skb->mac_header += off;
1176 skb->inner_transport_header += off;
1177 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001178 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1182{
Herbert Xudec18812007-10-14 00:37:30 -07001183 __copy_skb_header(new, old);
1184
Herbert Xu79671682006-06-22 02:40:14 -07001185 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1186 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1187 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Mel Gormanc93bdd02012-07-31 16:44:19 -07001190static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1191{
1192 if (skb_pfmemalloc(skb))
1193 return SKB_ALLOC_RX;
1194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/**
1198 * skb_copy - create private copy of an sk_buff
1199 * @skb: buffer to copy
1200 * @gfp_mask: allocation priority
1201 *
1202 * Make a copy of both an &sk_buff and its data. This is used when the
1203 * caller wishes to modify the data and needs a private copy of the
1204 * data to alter. Returns %NULL on failure or the pointer to the buffer
1205 * on success. The returned buffer has a reference count of 1.
1206 *
1207 * As by-product this function converts non-linear &sk_buff to linear
1208 * one, so that &sk_buff becomes completely private and caller is allowed
1209 * to modify all the data of returned buffer. This means that this
1210 * function is not recommended for use in circumstances when only
1211 * header is going to be modified. Use pskb_copy() instead.
1212 */
1213
Al Virodd0fc662005-10-07 07:46:04 +01001214struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001216 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001217 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001218 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1219 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!n)
1222 return NULL;
1223
1224 /* Set the data pointer */
1225 skb_reserve(n, headerlen);
1226 /* Set the tail pointer and length */
1227 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1230 BUG();
1231
1232 copy_skb_header(n, skb);
1233 return n;
1234}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001235EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001238 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001240 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001242 * @fclone: if true allocate the copy of the skb from the fclone
1243 * cache instead of the head cache; it is recommended to set this
1244 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 *
1246 * Make a copy of both an &sk_buff and part of its data, located
1247 * in header. Fragmented data remain shared. This is used when
1248 * the caller wishes to modify only header of &sk_buff and needs
1249 * private copy of the header to alter. Returns %NULL on failure
1250 * or the pointer to the buffer on success.
1251 * The returned buffer has a reference count of 1.
1252 */
1253
Octavian Purdilabad93e92014-06-12 01:36:26 +03001254struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1255 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Eric Dumazet117632e2011-12-03 21:39:53 +00001257 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001258 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1259 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (!n)
1262 goto out;
1263
1264 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001265 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Set the tail pointer and length */
1267 skb_put(n, skb_headlen(skb));
1268 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001269 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Herbert Xu25f484a2006-11-07 14:57:15 -08001271 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 n->data_len = skb->data_len;
1273 n->len = skb->len;
1274
1275 if (skb_shinfo(skb)->nr_frags) {
1276 int i;
1277
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001278 if (skb_orphan_frags(skb, gfp_mask) ||
1279 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001280 kfree_skb(n);
1281 n = NULL;
1282 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1285 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001286 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 skb_shinfo(n)->nr_frags = i;
1289 }
1290
David S. Miller21dc3302010-08-23 00:13:46 -07001291 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1293 skb_clone_fraglist(n);
1294 }
1295
1296 copy_skb_header(n, skb);
1297out:
1298 return n;
1299}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001300EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/**
1303 * pskb_expand_head - reallocate header of &sk_buff
1304 * @skb: buffer to reallocate
1305 * @nhead: room to add at head
1306 * @ntail: room to add at tail
1307 * @gfp_mask: allocation priority
1308 *
Mathias Krausebc323832013-11-07 14:18:26 +01001309 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1310 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * reference count of 1. Returns zero in the case of success or error,
1312 * if expansion failed. In the last case, &sk_buff is not changed.
1313 *
1314 * All the pointers pointing into skb header may change and must be
1315 * reloaded after call to this function.
1316 */
1317
Victor Fusco86a76ca2005-07-08 14:57:47 -07001318int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001319 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Eric Dumazet158f3232017-01-27 07:11:27 -08001321 int i, osize = skb_end_offset(skb);
1322 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001324 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Herbert Xu4edd87a2008-10-01 07:09:38 -07001326 BUG_ON(nhead < 0);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (skb_shared(skb))
1329 BUG();
1330
1331 size = SKB_DATA_ALIGN(size);
1332
Mel Gormanc93bdd02012-07-31 16:44:19 -07001333 if (skb_pfmemalloc(skb))
1334 gfp_mask |= __GFP_MEMALLOC;
1335 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1336 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (!data)
1338 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001339 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001342 * optimized for the cases when header is void.
1343 */
1344 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1345
1346 memcpy((struct skb_shared_info *)(data + size),
1347 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001348 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alexander Duyck3e245912012-05-04 14:26:51 +00001350 /*
1351 * if shinfo is shared we must drop the old head gracefully, but if it
1352 * is not we can just drop the old head and let the existing refcount
1353 * be since all we did is relocate the values
1354 */
1355 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001356 if (skb_orphan_frags(skb, gfp_mask))
1357 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001358 if (skb_zcopy(skb))
1359 atomic_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001360 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001361 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Eric Dumazet1fd63042010-09-02 23:09:32 +00001363 if (skb_has_frag_list(skb))
1364 skb_clone_fraglist(skb);
1365
1366 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001367 } else {
1368 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 off = (data + nhead) - skb->head;
1371
1372 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001373 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001375#ifdef NET_SKBUFF_DATA_USES_OFFSET
1376 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001377 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001378#else
1379 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001380#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001381 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001382 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001384 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 skb->nohdr = 0;
1386 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001387
1388 /* It is not generally safe to change skb->truesize.
1389 * For the moment, we really care of rx path, or
1390 * when skb is orphaned (not attached to a socket).
1391 */
1392 if (!skb->sk || skb->destructor == sock_edemux)
1393 skb->truesize += size - osize;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return 0;
1396
Shirley Maa6686f22011-07-06 12:22:12 +00001397nofrags:
1398 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399nodata:
1400 return -ENOMEM;
1401}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001402EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404/* Make private copy of skb with writable head and some headroom */
1405
1406struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1407{
1408 struct sk_buff *skb2;
1409 int delta = headroom - skb_headroom(skb);
1410
1411 if (delta <= 0)
1412 skb2 = pskb_copy(skb, GFP_ATOMIC);
1413 else {
1414 skb2 = skb_clone(skb, GFP_ATOMIC);
1415 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1416 GFP_ATOMIC)) {
1417 kfree_skb(skb2);
1418 skb2 = NULL;
1419 }
1420 }
1421 return skb2;
1422}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001423EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425/**
1426 * skb_copy_expand - copy and expand sk_buff
1427 * @skb: buffer to copy
1428 * @newheadroom: new free bytes at head
1429 * @newtailroom: new free bytes at tail
1430 * @gfp_mask: allocation priority
1431 *
1432 * Make a copy of both an &sk_buff and its data and while doing so
1433 * allocate additional space.
1434 *
1435 * This is used when the caller wishes to modify the data and needs a
1436 * private copy of the data to alter as well as more space for new fields.
1437 * Returns %NULL on failure or the pointer to the buffer
1438 * on success. The returned buffer has a reference count of 1.
1439 *
1440 * You must pass %GFP_ATOMIC as the allocation priority if this function
1441 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001444 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001445 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 /*
1448 * Allocate the copy buffer
1449 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001450 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1451 gfp_mask, skb_alloc_rx_flag(skb),
1452 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001453 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int head_copy_len, head_copy_off;
1455
1456 if (!n)
1457 return NULL;
1458
1459 skb_reserve(n, newheadroom);
1460
1461 /* Set the tail pointer and length */
1462 skb_put(n, skb->len);
1463
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001464 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 head_copy_off = 0;
1466 if (newheadroom <= head_copy_len)
1467 head_copy_len = newheadroom;
1468 else
1469 head_copy_off = newheadroom - head_copy_len;
1470
1471 /* Copy the linear header and data. */
1472 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1473 skb->len + head_copy_len))
1474 BUG();
1475
1476 copy_skb_header(n, skb);
1477
Eric Dumazet030737b2013-10-19 11:42:54 -07001478 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return n;
1481}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001482EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484/**
1485 * skb_pad - zero pad the tail of an skb
1486 * @skb: buffer to pad
1487 * @pad: space to pad
1488 *
1489 * Ensure that a buffer is followed by a padding area that is zero
1490 * filled. Used by network drivers which may DMA or transfer data
1491 * beyond the buffer end onto the wire.
1492 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001493 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001495
Herbert Xu5b057c62006-06-23 02:06:41 -07001496int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Herbert Xu5b057c62006-06-23 02:06:41 -07001498 int err;
1499 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001502 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001506
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001507 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001508 if (likely(skb_cloned(skb) || ntail > 0)) {
1509 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1510 if (unlikely(err))
1511 goto free_skb;
1512 }
1513
1514 /* FIXME: The use of this function with non-linear skb's really needs
1515 * to be audited.
1516 */
1517 err = skb_linearize(skb);
1518 if (unlikely(err))
1519 goto free_skb;
1520
1521 memset(skb->data + skb->len, 0, pad);
1522 return 0;
1523
1524free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001526 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001527}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001528EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001529
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001530/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001531 * pskb_put - add data to the tail of a potentially fragmented buffer
1532 * @skb: start of the buffer to use
1533 * @tail: tail fragment of the buffer to use
1534 * @len: amount of data to add
1535 *
1536 * This function extends the used data area of the potentially
1537 * fragmented buffer. @tail must be the last fragment of @skb -- or
1538 * @skb itself. If this would exceed the total buffer size the kernel
1539 * will panic. A pointer to the first byte of the extra data is
1540 * returned.
1541 */
1542
Johannes Berg4df864c2017-06-16 14:29:21 +02001543void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001544{
1545 if (tail != skb) {
1546 skb->data_len += len;
1547 skb->len += len;
1548 }
1549 return skb_put(tail, len);
1550}
1551EXPORT_SYMBOL_GPL(pskb_put);
1552
1553/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001554 * skb_put - add data to a buffer
1555 * @skb: buffer to use
1556 * @len: amount of data to add
1557 *
1558 * This function extends the used data area of the buffer. If this would
1559 * exceed the total buffer size the kernel will panic. A pointer to the
1560 * first byte of the extra data is returned.
1561 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001562void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001563{
Johannes Berg4df864c2017-06-16 14:29:21 +02001564 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001565 SKB_LINEAR_ASSERT(skb);
1566 skb->tail += len;
1567 skb->len += len;
1568 if (unlikely(skb->tail > skb->end))
1569 skb_over_panic(skb, len, __builtin_return_address(0));
1570 return tmp;
1571}
1572EXPORT_SYMBOL(skb_put);
1573
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001574/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001575 * skb_push - add data to the start of a buffer
1576 * @skb: buffer to use
1577 * @len: amount of data to add
1578 *
1579 * This function extends the used data area of the buffer at the buffer
1580 * start. If this would exceed the total buffer headroom the kernel will
1581 * panic. A pointer to the first byte of the extra data is returned.
1582 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001583void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001584{
1585 skb->data -= len;
1586 skb->len += len;
1587 if (unlikely(skb->data<skb->head))
1588 skb_under_panic(skb, len, __builtin_return_address(0));
1589 return skb->data;
1590}
1591EXPORT_SYMBOL(skb_push);
1592
1593/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001594 * skb_pull - remove data from the start of a buffer
1595 * @skb: buffer to use
1596 * @len: amount of data to remove
1597 *
1598 * This function removes data from the start of a buffer, returning
1599 * the memory to the headroom. A pointer to the next data in the buffer
1600 * is returned. Once the data has been pulled future pushes will overwrite
1601 * the old data.
1602 */
Johannes Bergaf728682017-06-16 14:29:22 +02001603void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001604{
David S. Miller47d29642010-05-02 02:21:44 -07001605 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001606}
1607EXPORT_SYMBOL(skb_pull);
1608
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001609/**
1610 * skb_trim - remove end from a buffer
1611 * @skb: buffer to alter
1612 * @len: new length
1613 *
1614 * Cut the length of a buffer down by removing data from the tail. If
1615 * the buffer is already under the length specified it is not modified.
1616 * The skb must be linear.
1617 */
1618void skb_trim(struct sk_buff *skb, unsigned int len)
1619{
1620 if (skb->len > len)
1621 __skb_trim(skb, len);
1622}
1623EXPORT_SYMBOL(skb_trim);
1624
Herbert Xu3cc0e872006-06-09 16:13:38 -07001625/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
1627
Herbert Xu3cc0e872006-06-09 16:13:38 -07001628int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Herbert Xu27b437c2006-07-13 19:26:39 -07001630 struct sk_buff **fragp;
1631 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 int offset = skb_headlen(skb);
1633 int nfrags = skb_shinfo(skb)->nr_frags;
1634 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001635 int err;
1636
1637 if (skb_cloned(skb) &&
1638 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001641 i = 0;
1642 if (offset >= len)
1643 goto drop_pages;
1644
1645 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001646 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001647
1648 if (end < len) {
1649 offset = end;
1650 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001652
Eric Dumazet9e903e02011-10-18 21:00:24 +00001653 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001654
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001655drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001656 skb_shinfo(skb)->nr_frags = i;
1657
1658 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001659 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001660
David S. Miller21dc3302010-08-23 00:13:46 -07001661 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001662 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001663 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Herbert Xu27b437c2006-07-13 19:26:39 -07001666 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1667 fragp = &frag->next) {
1668 int end = offset + frag->len;
1669
1670 if (skb_shared(frag)) {
1671 struct sk_buff *nfrag;
1672
1673 nfrag = skb_clone(frag, GFP_ATOMIC);
1674 if (unlikely(!nfrag))
1675 return -ENOMEM;
1676
1677 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001678 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001679 frag = nfrag;
1680 *fragp = frag;
1681 }
1682
1683 if (end < len) {
1684 offset = end;
1685 continue;
1686 }
1687
1688 if (end > len &&
1689 unlikely((err = pskb_trim(frag, len - offset))))
1690 return err;
1691
1692 if (frag->next)
1693 skb_drop_list(&frag->next);
1694 break;
1695 }
1696
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001697done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001698 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 skb->data_len -= skb->len - len;
1700 skb->len = len;
1701 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001702 skb->len = len;
1703 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001704 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001707 if (!skb->sk || skb->destructor == sock_edemux)
1708 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 0;
1710}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001711EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713/**
1714 * __pskb_pull_tail - advance tail of skb header
1715 * @skb: buffer to reallocate
1716 * @delta: number of bytes to advance tail
1717 *
1718 * The function makes a sense only on a fragmented &sk_buff,
1719 * it expands header moving its tail forward and copying necessary
1720 * data from fragmented part.
1721 *
1722 * &sk_buff MUST have reference count of 1.
1723 *
1724 * Returns %NULL (and &sk_buff does not change) if pull failed
1725 * or value of new tail of skb in the case of success.
1726 *
1727 * All the pointers pointing into skb header may change and must be
1728 * reloaded after call to this function.
1729 */
1730
1731/* Moves tail of skb head forward, copying data from fragmented part,
1732 * when it is necessary.
1733 * 1. It may fail due to malloc failure.
1734 * 2. It may change skb pointers.
1735 *
1736 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1737 */
Johannes Bergaf728682017-06-16 14:29:22 +02001738void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 /* If skb has not enough free space at tail, get new one
1741 * plus 128 bytes for future expansions. If we have enough
1742 * room at tail, reallocate without expansion only if skb is cloned.
1743 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001744 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (eat > 0 || skb_cloned(skb)) {
1747 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1748 GFP_ATOMIC))
1749 return NULL;
1750 }
1751
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001752 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 BUG();
1754
1755 /* Optimization: no fragments, no reasons to preestimate
1756 * size of pulled pages. Superb.
1757 */
David S. Miller21dc3302010-08-23 00:13:46 -07001758 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto pull_pages;
1760
1761 /* Estimate size of pulled pages. */
1762 eat = delta;
1763 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001764 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1765
1766 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001768 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
1771 /* If we need update frag list, we are in troubles.
1772 * Certainly, it possible to add an offset to skb data,
1773 * but taking into account that pulling is expected to
1774 * be very rare operation, it is worth to fight against
1775 * further bloating skb head and crucify ourselves here instead.
1776 * Pure masohism, indeed. 8)8)
1777 */
1778 if (eat) {
1779 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1780 struct sk_buff *clone = NULL;
1781 struct sk_buff *insp = NULL;
1782
1783 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001784 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (list->len <= eat) {
1787 /* Eaten as whole. */
1788 eat -= list->len;
1789 list = list->next;
1790 insp = list;
1791 } else {
1792 /* Eaten partially. */
1793
1794 if (skb_shared(list)) {
1795 /* Sucks! We need to fork list. :-( */
1796 clone = skb_clone(list, GFP_ATOMIC);
1797 if (!clone)
1798 return NULL;
1799 insp = list->next;
1800 list = clone;
1801 } else {
1802 /* This may be pulled without
1803 * problems. */
1804 insp = list;
1805 }
1806 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001807 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return NULL;
1809 }
1810 break;
1811 }
1812 } while (eat);
1813
1814 /* Free pulled out fragments. */
1815 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1816 skb_shinfo(skb)->frag_list = list->next;
1817 kfree_skb(list);
1818 }
1819 /* And insert new clone at head. */
1820 if (clone) {
1821 clone->next = list;
1822 skb_shinfo(skb)->frag_list = clone;
1823 }
1824 }
1825 /* Success! Now we may commit changes to skb data. */
1826
1827pull_pages:
1828 eat = delta;
1829 k = 0;
1830 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001831 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1832
1833 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001834 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001835 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 } else {
1837 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1838 if (eat) {
1839 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001840 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001841 if (!i)
1842 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 eat = 0;
1844 }
1845 k++;
1846 }
1847 }
1848 skb_shinfo(skb)->nr_frags = k;
1849
linzhang3ccc6c62017-07-17 17:25:02 +08001850end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 skb->tail += delta;
1852 skb->data_len -= delta;
1853
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001854 if (!skb->data_len)
1855 skb_zcopy_clear(skb, false);
1856
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001857 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001859EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Eric Dumazet22019b12011-07-29 18:37:31 +00001861/**
1862 * skb_copy_bits - copy bits from skb to kernel buffer
1863 * @skb: source skb
1864 * @offset: offset in source
1865 * @to: destination buffer
1866 * @len: number of bytes to copy
1867 *
1868 * Copy the specified number of bytes from the source skb to the
1869 * destination buffer.
1870 *
1871 * CAUTION ! :
1872 * If its prototype is ever changed,
1873 * check arch/{*}/net/{*}.S files,
1874 * since it is called from BPF assembly code.
1875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1877{
David S. Miller1a028e52007-04-27 15:21:23 -07001878 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001879 struct sk_buff *frag_iter;
1880 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 if (offset > (int)skb->len - len)
1883 goto fault;
1884
1885 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001886 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (copy > len)
1888 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001889 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if ((len -= copy) == 0)
1891 return 0;
1892 offset += copy;
1893 to += copy;
1894 }
1895
1896 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001897 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001898 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001900 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001901
Eric Dumazet51c56b02012-04-05 11:35:15 +02001902 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04001904 u32 p_off, p_len, copied;
1905 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 u8 *vaddr;
1907
1908 if (copy > len)
1909 copy = len;
1910
Willem de Bruijnc613c202017-07-31 08:15:47 -04001911 skb_frag_foreach_page(f,
1912 f->page_offset + offset - start,
1913 copy, p, p_off, p_len, copied) {
1914 vaddr = kmap_atomic(p);
1915 memcpy(to + copied, vaddr + p_off, p_len);
1916 kunmap_atomic(vaddr);
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 if ((len -= copy) == 0)
1920 return 0;
1921 offset += copy;
1922 to += copy;
1923 }
David S. Miller1a028e52007-04-27 15:21:23 -07001924 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
David S. Millerfbb398a2009-06-09 00:18:59 -07001927 skb_walk_frags(skb, frag_iter) {
1928 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
David S. Millerfbb398a2009-06-09 00:18:59 -07001930 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
David S. Millerfbb398a2009-06-09 00:18:59 -07001932 end = start + frag_iter->len;
1933 if ((copy = end - offset) > 0) {
1934 if (copy > len)
1935 copy = len;
1936 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1937 goto fault;
1938 if ((len -= copy) == 0)
1939 return 0;
1940 offset += copy;
1941 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001943 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Shirley Maa6686f22011-07-06 12:22:12 +00001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!len)
1947 return 0;
1948
1949fault:
1950 return -EFAULT;
1951}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001952EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Jens Axboe9c55e012007-11-06 23:30:13 -08001954/*
1955 * Callback from splice_to_pipe(), if we need to release some pages
1956 * at the end of the spd in case we error'ed out in filling the pipe.
1957 */
1958static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1959{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001960 put_page(spd->pages[i]);
1961}
Jens Axboe9c55e012007-11-06 23:30:13 -08001962
David S. Millera108d5f2012-04-23 23:06:11 -04001963static struct page *linear_to_page(struct page *page, unsigned int *len,
1964 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001965 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001966{
Eric Dumazet5640f762012-09-23 23:04:42 +00001967 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001968
Eric Dumazet5640f762012-09-23 23:04:42 +00001969 if (!sk_page_frag_refill(sk, pfrag))
1970 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001971
Eric Dumazet5640f762012-09-23 23:04:42 +00001972 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001973
Eric Dumazet5640f762012-09-23 23:04:42 +00001974 memcpy(page_address(pfrag->page) + pfrag->offset,
1975 page_address(page) + *offset, *len);
1976 *offset = pfrag->offset;
1977 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001978
Eric Dumazet5640f762012-09-23 23:04:42 +00001979 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001980}
1981
Eric Dumazet41c73a02012-04-22 12:26:16 +00001982static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1983 struct page *page,
1984 unsigned int offset)
1985{
1986 return spd->nr_pages &&
1987 spd->pages[spd->nr_pages - 1] == page &&
1988 (spd->partial[spd->nr_pages - 1].offset +
1989 spd->partial[spd->nr_pages - 1].len == offset);
1990}
1991
Jens Axboe9c55e012007-11-06 23:30:13 -08001992/*
1993 * Fill page/offset/length into spd, if it can hold more pages.
1994 */
David S. Millera108d5f2012-04-23 23:06:11 -04001995static bool spd_fill_page(struct splice_pipe_desc *spd,
1996 struct pipe_inode_info *pipe, struct page *page,
1997 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001998 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001999 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002000{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002001 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002002 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002003
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002004 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002005 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002006 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002007 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002008 }
2009 if (spd_can_coalesce(spd, page, offset)) {
2010 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002011 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002012 }
2013 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002014 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002015 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002016 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002017 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002018
David S. Millera108d5f2012-04-23 23:06:11 -04002019 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002020}
2021
David S. Millera108d5f2012-04-23 23:06:11 -04002022static bool __splice_segment(struct page *page, unsigned int poff,
2023 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002024 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002025 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002026 struct sock *sk,
2027 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002028{
2029 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002030 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002031
2032 /* skip this segment if already processed */
2033 if (*off >= plen) {
2034 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002035 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002036 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002037
Octavian Purdila2870c432008-07-15 00:49:11 -07002038 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002039 poff += *off;
2040 plen -= *off;
2041 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002042
Eric Dumazet18aafc62013-01-11 14:46:37 +00002043 do {
2044 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002045
Eric Dumazet18aafc62013-01-11 14:46:37 +00002046 if (spd_fill_page(spd, pipe, page, &flen, poff,
2047 linear, sk))
2048 return true;
2049 poff += flen;
2050 plen -= flen;
2051 *len -= flen;
2052 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002053
David S. Millera108d5f2012-04-23 23:06:11 -04002054 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002055}
2056
2057/*
David S. Millera108d5f2012-04-23 23:06:11 -04002058 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002059 * pipe is full or if we already spliced the requested length.
2060 */
David S. Millera108d5f2012-04-23 23:06:11 -04002061static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2062 unsigned int *offset, unsigned int *len,
2063 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002064{
2065 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002066 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002067
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002068 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002069 * If skb->head_frag is set, this 'linear' part is backed by a
2070 * fragment, and if the head is not shared with any clones then
2071 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002072 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002073 if (__splice_segment(virt_to_page(skb->data),
2074 (unsigned long) skb->data & (PAGE_SIZE - 1),
2075 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002076 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002077 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002078 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002079 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002080
2081 /*
2082 * then map the fragments
2083 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002084 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2085 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2086
Ian Campbellea2ab692011-08-22 23:44:58 +00002087 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002088 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002089 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002090 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002091 }
2092
Tom Herbertfa9835e2016-03-07 14:11:04 -08002093 skb_walk_frags(skb, iter) {
2094 if (*offset >= iter->len) {
2095 *offset -= iter->len;
2096 continue;
2097 }
2098 /* __skb_splice_bits() only fails if the output has no room
2099 * left, so no point in going over the frag_list for the error
2100 * case.
2101 */
2102 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2103 return true;
2104 }
2105
David S. Millera108d5f2012-04-23 23:06:11 -04002106 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002107}
2108
2109/*
2110 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002111 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002112 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002113int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002114 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002115 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002116{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002117 struct partial_page partial[MAX_SKB_FRAGS];
2118 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002119 struct splice_pipe_desc spd = {
2120 .pages = pages,
2121 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002122 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002123 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002124 .spd_release = sock_spd_release,
2125 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002126 int ret = 0;
2127
Tom Herbertfa9835e2016-03-07 14:11:04 -08002128 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002129
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002130 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002131 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002132
Jens Axboe35f3d142010-05-20 10:43:18 +02002133 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002134}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002135EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002136
Tom Herbert20bf50d2017-07-28 16:22:42 -07002137/* Send skb data on a socket. Socket must be locked. */
2138int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2139 int len)
2140{
2141 unsigned int orig_len = len;
2142 struct sk_buff *head = skb;
2143 unsigned short fragidx;
2144 int slen, ret;
2145
2146do_frag_list:
2147
2148 /* Deal with head data */
2149 while (offset < skb_headlen(skb) && len) {
2150 struct kvec kv;
2151 struct msghdr msg;
2152
2153 slen = min_t(int, len, skb_headlen(skb) - offset);
2154 kv.iov_base = skb->data + offset;
2155 kv.iov_len = len;
2156 memset(&msg, 0, sizeof(msg));
2157
2158 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2159 if (ret <= 0)
2160 goto error;
2161
2162 offset += ret;
2163 len -= ret;
2164 }
2165
2166 /* All the data was skb head? */
2167 if (!len)
2168 goto out;
2169
2170 /* Make offset relative to start of frags */
2171 offset -= skb_headlen(skb);
2172
2173 /* Find where we are in frag list */
2174 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2175 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2176
2177 if (offset < frag->size)
2178 break;
2179
2180 offset -= frag->size;
2181 }
2182
2183 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2184 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2185
2186 slen = min_t(size_t, len, frag->size - offset);
2187
2188 while (slen) {
2189 ret = kernel_sendpage_locked(sk, frag->page.p,
2190 frag->page_offset + offset,
2191 slen, MSG_DONTWAIT);
2192 if (ret <= 0)
2193 goto error;
2194
2195 len -= ret;
2196 offset += ret;
2197 slen -= ret;
2198 }
2199
2200 offset = 0;
2201 }
2202
2203 if (len) {
2204 /* Process any frag lists */
2205
2206 if (skb == head) {
2207 if (skb_has_frag_list(skb)) {
2208 skb = skb_shinfo(skb)->frag_list;
2209 goto do_frag_list;
2210 }
2211 } else if (skb->next) {
2212 skb = skb->next;
2213 goto do_frag_list;
2214 }
2215 }
2216
2217out:
2218 return orig_len - len;
2219
2220error:
2221 return orig_len == len ? ret : orig_len - len;
2222}
2223EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2224
2225/* Send skb data on a socket. */
2226int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2227{
2228 int ret = 0;
2229
2230 lock_sock(sk);
2231 ret = skb_send_sock_locked(sk, skb, offset, len);
2232 release_sock(sk);
2233
2234 return ret;
2235}
2236EXPORT_SYMBOL_GPL(skb_send_sock);
2237
Herbert Xu357b40a2005-04-19 22:30:14 -07002238/**
2239 * skb_store_bits - store bits from kernel buffer to skb
2240 * @skb: destination buffer
2241 * @offset: offset in destination
2242 * @from: source buffer
2243 * @len: number of bytes to copy
2244 *
2245 * Copy the specified number of bytes from the source buffer to the
2246 * destination skb. This function handles all the messy bits of
2247 * traversing fragment lists and such.
2248 */
2249
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002250int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002251{
David S. Miller1a028e52007-04-27 15:21:23 -07002252 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002253 struct sk_buff *frag_iter;
2254 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002255
2256 if (offset > (int)skb->len - len)
2257 goto fault;
2258
David S. Miller1a028e52007-04-27 15:21:23 -07002259 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002260 if (copy > len)
2261 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002262 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002263 if ((len -= copy) == 0)
2264 return 0;
2265 offset += copy;
2266 from += copy;
2267 }
2268
2269 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2270 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002271 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002272
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002273 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002274
Eric Dumazet9e903e02011-10-18 21:00:24 +00002275 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002276 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002277 u32 p_off, p_len, copied;
2278 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002279 u8 *vaddr;
2280
2281 if (copy > len)
2282 copy = len;
2283
Willem de Bruijnc613c202017-07-31 08:15:47 -04002284 skb_frag_foreach_page(frag,
2285 frag->page_offset + offset - start,
2286 copy, p, p_off, p_len, copied) {
2287 vaddr = kmap_atomic(p);
2288 memcpy(vaddr + p_off, from + copied, p_len);
2289 kunmap_atomic(vaddr);
2290 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002291
2292 if ((len -= copy) == 0)
2293 return 0;
2294 offset += copy;
2295 from += copy;
2296 }
David S. Miller1a028e52007-04-27 15:21:23 -07002297 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002298 }
2299
David S. Millerfbb398a2009-06-09 00:18:59 -07002300 skb_walk_frags(skb, frag_iter) {
2301 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002302
David S. Millerfbb398a2009-06-09 00:18:59 -07002303 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002304
David S. Millerfbb398a2009-06-09 00:18:59 -07002305 end = start + frag_iter->len;
2306 if ((copy = end - offset) > 0) {
2307 if (copy > len)
2308 copy = len;
2309 if (skb_store_bits(frag_iter, offset - start,
2310 from, copy))
2311 goto fault;
2312 if ((len -= copy) == 0)
2313 return 0;
2314 offset += copy;
2315 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002316 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002317 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002318 }
2319 if (!len)
2320 return 0;
2321
2322fault:
2323 return -EFAULT;
2324}
Herbert Xu357b40a2005-04-19 22:30:14 -07002325EXPORT_SYMBOL(skb_store_bits);
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002328__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2329 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
David S. Miller1a028e52007-04-27 15:21:23 -07002331 int start = skb_headlen(skb);
2332 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002333 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int pos = 0;
2335
2336 /* Checksum header. */
2337 if (copy > 0) {
2338 if (copy > len)
2339 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002340 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if ((len -= copy) == 0)
2342 return csum;
2343 offset += copy;
2344 pos = copy;
2345 }
2346
2347 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002348 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002349 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002351 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002352
Eric Dumazet51c56b02012-04-05 11:35:15 +02002353 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002355 u32 p_off, p_len, copied;
2356 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002357 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 if (copy > len)
2361 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002362
2363 skb_frag_foreach_page(frag,
2364 frag->page_offset + offset - start,
2365 copy, p, p_off, p_len, copied) {
2366 vaddr = kmap_atomic(p);
2367 csum2 = ops->update(vaddr + p_off, p_len, 0);
2368 kunmap_atomic(vaddr);
2369 csum = ops->combine(csum, csum2, pos, p_len);
2370 pos += p_len;
2371 }
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (!(len -= copy))
2374 return csum;
2375 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
David S. Miller1a028e52007-04-27 15:21:23 -07002377 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
David S. Millerfbb398a2009-06-09 00:18:59 -07002380 skb_walk_frags(skb, frag_iter) {
2381 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
David S. Millerfbb398a2009-06-09 00:18:59 -07002383 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
David S. Millerfbb398a2009-06-09 00:18:59 -07002385 end = start + frag_iter->len;
2386 if ((copy = end - offset) > 0) {
2387 __wsum csum2;
2388 if (copy > len)
2389 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002390 csum2 = __skb_checksum(frag_iter, offset - start,
2391 copy, 0, ops);
2392 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002393 if ((len -= copy) == 0)
2394 return csum;
2395 offset += copy;
2396 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002398 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002400 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 return csum;
2403}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002404EXPORT_SYMBOL(__skb_checksum);
2405
2406__wsum skb_checksum(const struct sk_buff *skb, int offset,
2407 int len, __wsum csum)
2408{
2409 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002410 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002411 .combine = csum_block_add_ext,
2412 };
2413
2414 return __skb_checksum(skb, offset, len, csum, &ops);
2415}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002416EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418/* Both of above in one bottle. */
2419
Al Viro81d77662006-11-14 21:37:33 -08002420__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2421 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
David S. Miller1a028e52007-04-27 15:21:23 -07002423 int start = skb_headlen(skb);
2424 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002425 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int pos = 0;
2427
2428 /* Copy header. */
2429 if (copy > 0) {
2430 if (copy > len)
2431 copy = len;
2432 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2433 copy, csum);
2434 if ((len -= copy) == 0)
2435 return csum;
2436 offset += copy;
2437 to += copy;
2438 pos = copy;
2439 }
2440
2441 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002442 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002444 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002445
Eric Dumazet9e903e02011-10-18 21:00:24 +00002446 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002448 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2449 u32 p_off, p_len, copied;
2450 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002451 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (copy > len)
2455 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002456
2457 skb_frag_foreach_page(frag,
2458 frag->page_offset + offset - start,
2459 copy, p, p_off, p_len, copied) {
2460 vaddr = kmap_atomic(p);
2461 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2462 to + copied,
2463 p_len, 0);
2464 kunmap_atomic(vaddr);
2465 csum = csum_block_add(csum, csum2, pos);
2466 pos += p_len;
2467 }
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (!(len -= copy))
2470 return csum;
2471 offset += copy;
2472 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
David S. Miller1a028e52007-04-27 15:21:23 -07002474 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
David S. Millerfbb398a2009-06-09 00:18:59 -07002477 skb_walk_frags(skb, frag_iter) {
2478 __wsum csum2;
2479 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
David S. Millerfbb398a2009-06-09 00:18:59 -07002481 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
David S. Millerfbb398a2009-06-09 00:18:59 -07002483 end = start + frag_iter->len;
2484 if ((copy = end - offset) > 0) {
2485 if (copy > len)
2486 copy = len;
2487 csum2 = skb_copy_and_csum_bits(frag_iter,
2488 offset - start,
2489 to, copy, 0);
2490 csum = csum_block_add(csum, csum2, pos);
2491 if ((len -= copy) == 0)
2492 return csum;
2493 offset += copy;
2494 to += copy;
2495 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002497 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002499 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return csum;
2501}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002502EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Davide Caratti96178132017-05-18 15:44:37 +02002504static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2505{
2506 net_warn_ratelimited(
2507 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2508 __func__);
2509 return 0;
2510}
2511
2512static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2513 int offset, int len)
2514{
2515 net_warn_ratelimited(
2516 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2517 __func__);
2518 return 0;
2519}
2520
2521static const struct skb_checksum_ops default_crc32c_ops = {
2522 .update = warn_crc32c_csum_update,
2523 .combine = warn_crc32c_csum_combine,
2524};
2525
2526const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2527 &default_crc32c_ops;
2528EXPORT_SYMBOL(crc32c_csum_stub);
2529
Thomas Grafaf2806f2013-12-13 15:22:17 +01002530 /**
2531 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2532 * @from: source buffer
2533 *
2534 * Calculates the amount of linear headroom needed in the 'to' skb passed
2535 * into skb_zerocopy().
2536 */
2537unsigned int
2538skb_zerocopy_headlen(const struct sk_buff *from)
2539{
2540 unsigned int hlen = 0;
2541
2542 if (!from->head_frag ||
2543 skb_headlen(from) < L1_CACHE_BYTES ||
2544 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2545 hlen = skb_headlen(from);
2546
2547 if (skb_has_frag_list(from))
2548 hlen = from->len;
2549
2550 return hlen;
2551}
2552EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2553
2554/**
2555 * skb_zerocopy - Zero copy skb to skb
2556 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002557 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002558 * @len: number of bytes to copy from source buffer
2559 * @hlen: size of linear headroom in destination buffer
2560 *
2561 * Copies up to `len` bytes from `from` to `to` by creating references
2562 * to the frags in the source buffer.
2563 *
2564 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2565 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002566 *
2567 * Return value:
2568 * 0: everything is OK
2569 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2570 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002571 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002572int
2573skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002574{
2575 int i, j = 0;
2576 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002577 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002578 struct page *page;
2579 unsigned int offset;
2580
2581 BUG_ON(!from->head_frag && !hlen);
2582
2583 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002584 if (len <= skb_tailroom(to))
2585 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002586
2587 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002588 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2589 if (unlikely(ret))
2590 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002591 len -= hlen;
2592 } else {
2593 plen = min_t(int, skb_headlen(from), len);
2594 if (plen) {
2595 page = virt_to_head_page(from->head);
2596 offset = from->data - (unsigned char *)page_address(page);
2597 __skb_fill_page_desc(to, 0, page, offset, plen);
2598 get_page(page);
2599 j = 1;
2600 len -= plen;
2601 }
2602 }
2603
2604 to->truesize += len + plen;
2605 to->len += len + plen;
2606 to->data_len += len + plen;
2607
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002608 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2609 skb_tx_error(from);
2610 return -ENOMEM;
2611 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002612 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002613
Thomas Grafaf2806f2013-12-13 15:22:17 +01002614 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2615 if (!len)
2616 break;
2617 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2618 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2619 len -= skb_shinfo(to)->frags[j].size;
2620 skb_frag_ref(to, j);
2621 j++;
2622 }
2623 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002624
2625 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002626}
2627EXPORT_SYMBOL_GPL(skb_zerocopy);
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2630{
Al Virod3bc23e2006-11-14 21:24:49 -08002631 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 long csstart;
2633
Patrick McHardy84fa7932006-08-29 16:44:56 -07002634 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002635 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 else
2637 csstart = skb_headlen(skb);
2638
Kris Katterjohn09a62662006-01-08 22:24:28 -08002639 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002641 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 csum = 0;
2644 if (csstart != skb->len)
2645 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2646 skb->len - csstart, 0);
2647
Patrick McHardy84fa7932006-08-29 16:44:56 -07002648 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002649 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Al Virod3bc23e2006-11-14 21:24:49 -08002651 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002654EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656/**
2657 * skb_dequeue - remove from the head of the queue
2658 * @list: list to dequeue from
2659 *
2660 * Remove the head of the list. The list lock is taken so the function
2661 * may be used safely with other locking list functions. The head item is
2662 * returned or %NULL if the list is empty.
2663 */
2664
2665struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2666{
2667 unsigned long flags;
2668 struct sk_buff *result;
2669
2670 spin_lock_irqsave(&list->lock, flags);
2671 result = __skb_dequeue(list);
2672 spin_unlock_irqrestore(&list->lock, flags);
2673 return result;
2674}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002675EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677/**
2678 * skb_dequeue_tail - remove from the tail of the queue
2679 * @list: list to dequeue from
2680 *
2681 * Remove the tail of the list. The list lock is taken so the function
2682 * may be used safely with other locking list functions. The tail item is
2683 * returned or %NULL if the list is empty.
2684 */
2685struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2686{
2687 unsigned long flags;
2688 struct sk_buff *result;
2689
2690 spin_lock_irqsave(&list->lock, flags);
2691 result = __skb_dequeue_tail(list);
2692 spin_unlock_irqrestore(&list->lock, flags);
2693 return result;
2694}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002695EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697/**
2698 * skb_queue_purge - empty a list
2699 * @list: list to empty
2700 *
2701 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2702 * the list and one reference dropped. This function takes the list
2703 * lock and is atomic with respect to other list locking functions.
2704 */
2705void skb_queue_purge(struct sk_buff_head *list)
2706{
2707 struct sk_buff *skb;
2708 while ((skb = skb_dequeue(list)) != NULL)
2709 kfree_skb(skb);
2710}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002711EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002714 * skb_rbtree_purge - empty a skb rbtree
2715 * @root: root of the rbtree to empty
2716 *
2717 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2718 * the list and one reference dropped. This function does not take
2719 * any lock. Synchronization should be handled by the caller (e.g., TCP
2720 * out-of-order queue is protected by the socket lock).
2721 */
2722void skb_rbtree_purge(struct rb_root *root)
2723{
2724 struct sk_buff *skb, *next;
2725
2726 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2727 kfree_skb(skb);
2728
2729 *root = RB_ROOT;
2730}
2731
2732/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 * skb_queue_head - queue a buffer at the list head
2734 * @list: list to use
2735 * @newsk: buffer to queue
2736 *
2737 * Queue a buffer at the start of the list. This function takes the
2738 * list lock and can be used safely with other locking &sk_buff functions
2739 * safely.
2740 *
2741 * A buffer cannot be placed on two lists at the same time.
2742 */
2743void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2744{
2745 unsigned long flags;
2746
2747 spin_lock_irqsave(&list->lock, flags);
2748 __skb_queue_head(list, newsk);
2749 spin_unlock_irqrestore(&list->lock, flags);
2750}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002751EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753/**
2754 * skb_queue_tail - queue a buffer at the list tail
2755 * @list: list to use
2756 * @newsk: buffer to queue
2757 *
2758 * Queue a buffer at the tail of the list. This function takes the
2759 * list lock and can be used safely with other locking &sk_buff functions
2760 * safely.
2761 *
2762 * A buffer cannot be placed on two lists at the same time.
2763 */
2764void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2765{
2766 unsigned long flags;
2767
2768 spin_lock_irqsave(&list->lock, flags);
2769 __skb_queue_tail(list, newsk);
2770 spin_unlock_irqrestore(&list->lock, flags);
2771}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002772EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774/**
2775 * skb_unlink - remove a buffer from a list
2776 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002777 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 *
David S. Miller8728b832005-08-09 19:25:21 -07002779 * Remove a packet from a list. The list locks are taken and this
2780 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 *
David S. Miller8728b832005-08-09 19:25:21 -07002782 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
David S. Miller8728b832005-08-09 19:25:21 -07002784void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
David S. Miller8728b832005-08-09 19:25:21 -07002786 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
David S. Miller8728b832005-08-09 19:25:21 -07002788 spin_lock_irqsave(&list->lock, flags);
2789 __skb_unlink(skb, list);
2790 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002792EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794/**
2795 * skb_append - append a buffer
2796 * @old: buffer to insert after
2797 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002798 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 *
2800 * Place a packet after a given packet in a list. The list locks are taken
2801 * and this function is atomic with respect to other list locked calls.
2802 * A buffer cannot be placed on two lists at the same time.
2803 */
David S. Miller8728b832005-08-09 19:25:21 -07002804void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
2806 unsigned long flags;
2807
David S. Miller8728b832005-08-09 19:25:21 -07002808 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002809 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002810 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002812EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814/**
2815 * skb_insert - insert a buffer
2816 * @old: buffer to insert before
2817 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002818 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 *
David S. Miller8728b832005-08-09 19:25:21 -07002820 * Place a packet before a given packet in a list. The list locks are
2821 * taken and this function is atomic with respect to other list locked
2822 * calls.
2823 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 * A buffer cannot be placed on two lists at the same time.
2825 */
David S. Miller8728b832005-08-09 19:25:21 -07002826void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 unsigned long flags;
2829
David S. Miller8728b832005-08-09 19:25:21 -07002830 spin_lock_irqsave(&list->lock, flags);
2831 __skb_insert(newsk, old->prev, old, list);
2832 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002834EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836static inline void skb_split_inside_header(struct sk_buff *skb,
2837 struct sk_buff* skb1,
2838 const u32 len, const int pos)
2839{
2840 int i;
2841
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002842 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2843 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 /* And move data appendix as is. */
2845 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2846 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2847
2848 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2849 skb_shinfo(skb)->nr_frags = 0;
2850 skb1->data_len = skb->data_len;
2851 skb1->len += skb1->data_len;
2852 skb->data_len = 0;
2853 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002854 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
2857static inline void skb_split_no_header(struct sk_buff *skb,
2858 struct sk_buff* skb1,
2859 const u32 len, int pos)
2860{
2861 int i, k = 0;
2862 const int nfrags = skb_shinfo(skb)->nr_frags;
2863
2864 skb_shinfo(skb)->nr_frags = 0;
2865 skb1->len = skb1->data_len = skb->len - len;
2866 skb->len = len;
2867 skb->data_len = len - pos;
2868
2869 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002870 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 if (pos + size > len) {
2873 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2874
2875 if (pos < len) {
2876 /* Split frag.
2877 * We have two variants in this case:
2878 * 1. Move all the frag to the second
2879 * part, if it is possible. F.e.
2880 * this approach is mandatory for TUX,
2881 * where splitting is expensive.
2882 * 2. Split is accurately. We make this.
2883 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002884 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002886 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2887 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 skb_shinfo(skb)->nr_frags++;
2889 }
2890 k++;
2891 } else
2892 skb_shinfo(skb)->nr_frags++;
2893 pos += size;
2894 }
2895 skb_shinfo(skb1)->nr_frags = k;
2896}
2897
2898/**
2899 * skb_split - Split fragmented skb to two parts at length len.
2900 * @skb: the buffer to split
2901 * @skb1: the buffer to receive the second part
2902 * @len: new length for skb
2903 */
2904void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2905{
2906 int pos = skb_headlen(skb);
2907
Willem de Bruijnfff88032017-06-08 11:35:03 -04002908 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2909 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002910 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (len < pos) /* Split line is inside header. */
2912 skb_split_inside_header(skb, skb1, len, pos);
2913 else /* Second chunk has no header, nothing to copy. */
2914 skb_split_no_header(skb, skb1, len, pos);
2915}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002916EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002918/* Shifting from/to a cloned skb is a no-go.
2919 *
2920 * Caller cannot keep skb_shinfo related pointers past calling here!
2921 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002922static int skb_prepare_for_shift(struct sk_buff *skb)
2923{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002924 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002925}
2926
2927/**
2928 * skb_shift - Shifts paged data partially from skb to another
2929 * @tgt: buffer into which tail data gets added
2930 * @skb: buffer from which the paged data comes from
2931 * @shiftlen: shift up to this many bytes
2932 *
2933 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002934 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002935 * It's up to caller to free skb if everything was shifted.
2936 *
2937 * If @tgt runs out of frags, the whole operation is aborted.
2938 *
2939 * Skb cannot include anything else but paged data while tgt is allowed
2940 * to have non-paged data as well.
2941 *
2942 * TODO: full sized shift could be optimized but that would need
2943 * specialized skb free'er to handle frags without up-to-date nr_frags.
2944 */
2945int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2946{
2947 int from, to, merge, todo;
2948 struct skb_frag_struct *fragfrom, *fragto;
2949
2950 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002951
2952 if (skb_headlen(skb))
2953 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002954 if (skb_zcopy(tgt) || skb_zcopy(skb))
2955 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002956
2957 todo = shiftlen;
2958 from = 0;
2959 to = skb_shinfo(tgt)->nr_frags;
2960 fragfrom = &skb_shinfo(skb)->frags[from];
2961
2962 /* Actual merge is delayed until the point when we know we can
2963 * commit all, so that we don't have to undo partial changes
2964 */
2965 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002966 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2967 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002968 merge = -1;
2969 } else {
2970 merge = to - 1;
2971
Eric Dumazet9e903e02011-10-18 21:00:24 +00002972 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002973 if (todo < 0) {
2974 if (skb_prepare_for_shift(skb) ||
2975 skb_prepare_for_shift(tgt))
2976 return 0;
2977
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002978 /* All previous frag pointers might be stale! */
2979 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002980 fragto = &skb_shinfo(tgt)->frags[merge];
2981
Eric Dumazet9e903e02011-10-18 21:00:24 +00002982 skb_frag_size_add(fragto, shiftlen);
2983 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002984 fragfrom->page_offset += shiftlen;
2985
2986 goto onlymerged;
2987 }
2988
2989 from++;
2990 }
2991
2992 /* Skip full, not-fitting skb to avoid expensive operations */
2993 if ((shiftlen == skb->len) &&
2994 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2995 return 0;
2996
2997 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2998 return 0;
2999
3000 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3001 if (to == MAX_SKB_FRAGS)
3002 return 0;
3003
3004 fragfrom = &skb_shinfo(skb)->frags[from];
3005 fragto = &skb_shinfo(tgt)->frags[to];
3006
Eric Dumazet9e903e02011-10-18 21:00:24 +00003007 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003008 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003009 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003010 from++;
3011 to++;
3012
3013 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003014 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003015 fragto->page = fragfrom->page;
3016 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003017 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003018
3019 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003020 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003021 todo = 0;
3022
3023 to++;
3024 break;
3025 }
3026 }
3027
3028 /* Ready to "commit" this state change to tgt */
3029 skb_shinfo(tgt)->nr_frags = to;
3030
3031 if (merge >= 0) {
3032 fragfrom = &skb_shinfo(skb)->frags[0];
3033 fragto = &skb_shinfo(tgt)->frags[merge];
3034
Eric Dumazet9e903e02011-10-18 21:00:24 +00003035 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003036 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003037 }
3038
3039 /* Reposition in the original skb */
3040 to = 0;
3041 while (from < skb_shinfo(skb)->nr_frags)
3042 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3043 skb_shinfo(skb)->nr_frags = to;
3044
3045 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3046
3047onlymerged:
3048 /* Most likely the tgt won't ever need its checksum anymore, skb on
3049 * the other hand might need it if it needs to be resent
3050 */
3051 tgt->ip_summed = CHECKSUM_PARTIAL;
3052 skb->ip_summed = CHECKSUM_PARTIAL;
3053
3054 /* Yak, is it really working this way? Some helper please? */
3055 skb->len -= shiftlen;
3056 skb->data_len -= shiftlen;
3057 skb->truesize -= shiftlen;
3058 tgt->len += shiftlen;
3059 tgt->data_len += shiftlen;
3060 tgt->truesize += shiftlen;
3061
3062 return shiftlen;
3063}
3064
Thomas Graf677e90e2005-06-23 20:59:51 -07003065/**
3066 * skb_prepare_seq_read - Prepare a sequential read of skb data
3067 * @skb: the buffer to read
3068 * @from: lower offset of data to be read
3069 * @to: upper offset of data to be read
3070 * @st: state variable
3071 *
3072 * Initializes the specified state variable. Must be called before
3073 * invoking skb_seq_read() for the first time.
3074 */
3075void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3076 unsigned int to, struct skb_seq_state *st)
3077{
3078 st->lower_offset = from;
3079 st->upper_offset = to;
3080 st->root_skb = st->cur_skb = skb;
3081 st->frag_idx = st->stepped_offset = 0;
3082 st->frag_data = NULL;
3083}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003084EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003085
3086/**
3087 * skb_seq_read - Sequentially read skb data
3088 * @consumed: number of bytes consumed by the caller so far
3089 * @data: destination pointer for data to be returned
3090 * @st: state variable
3091 *
Mathias Krausebc323832013-11-07 14:18:26 +01003092 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003093 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003094 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003095 * of the block or 0 if the end of the skb data or the upper
3096 * offset has been reached.
3097 *
3098 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003099 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003100 * of bytes already consumed and the next call to
3101 * skb_seq_read() will return the remaining part of the block.
3102 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003103 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003104 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003105 * reads of potentially non linear data.
3106 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003107 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003108 * at the moment, state->root_skb could be replaced with
3109 * a stack for this purpose.
3110 */
3111unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3112 struct skb_seq_state *st)
3113{
3114 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3115 skb_frag_t *frag;
3116
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003117 if (unlikely(abs_offset >= st->upper_offset)) {
3118 if (st->frag_data) {
3119 kunmap_atomic(st->frag_data);
3120 st->frag_data = NULL;
3121 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003122 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003123 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003124
3125next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003126 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003127
Thomas Chenault995b3372009-05-18 21:43:27 -07003128 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003129 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003130 return block_limit - abs_offset;
3131 }
3132
3133 if (st->frag_idx == 0 && !st->frag_data)
3134 st->stepped_offset += skb_headlen(st->cur_skb);
3135
3136 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3137 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003138 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003139
3140 if (abs_offset < block_limit) {
3141 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003142 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003143
3144 *data = (u8 *) st->frag_data + frag->page_offset +
3145 (abs_offset - st->stepped_offset);
3146
3147 return block_limit - abs_offset;
3148 }
3149
3150 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003151 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003152 st->frag_data = NULL;
3153 }
3154
3155 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003156 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003157 }
3158
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003159 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003160 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003161 st->frag_data = NULL;
3162 }
3163
David S. Miller21dc3302010-08-23 00:13:46 -07003164 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003165 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003166 st->frag_idx = 0;
3167 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003168 } else if (st->cur_skb->next) {
3169 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003170 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003171 goto next_skb;
3172 }
3173
3174 return 0;
3175}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003176EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003177
3178/**
3179 * skb_abort_seq_read - Abort a sequential read of skb data
3180 * @st: state variable
3181 *
3182 * Must be called if skb_seq_read() was not called until it
3183 * returned 0.
3184 */
3185void skb_abort_seq_read(struct skb_seq_state *st)
3186{
3187 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003188 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003189}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003190EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003191
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003192#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3193
3194static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3195 struct ts_config *conf,
3196 struct ts_state *state)
3197{
3198 return skb_seq_read(offset, text, TS_SKB_CB(state));
3199}
3200
3201static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3202{
3203 skb_abort_seq_read(TS_SKB_CB(state));
3204}
3205
3206/**
3207 * skb_find_text - Find a text pattern in skb data
3208 * @skb: the buffer to look in
3209 * @from: search offset
3210 * @to: search limit
3211 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003212 *
3213 * Finds a pattern in the skb data according to the specified
3214 * textsearch configuration. Use textsearch_next() to retrieve
3215 * subsequent occurrences of the pattern. Returns the offset
3216 * to the first occurrence or UINT_MAX if no match was found.
3217 */
3218unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003219 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003220{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003221 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003222 unsigned int ret;
3223
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003224 config->get_next_block = skb_ts_get_next_block;
3225 config->finish = skb_ts_finish;
3226
Bojan Prtvar059a2442015-02-22 11:46:35 +01003227 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003228
Bojan Prtvar059a2442015-02-22 11:46:35 +01003229 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003230 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003231}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003232EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003233
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003234/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003235 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003236 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09003237 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003238 * @getfrag: call back function to be used for getting the user data
3239 * @from: pointer to user message iov
3240 * @length: length of the iov message
3241 *
3242 * Description: This procedure append the user data in the fragment part
3243 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3244 */
3245int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08003246 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003247 int len, int odd, struct sk_buff *skb),
3248 void *from, int length)
3249{
Eric Dumazetb2111722012-12-28 06:06:37 +00003250 int frg_cnt = skb_shinfo(skb)->nr_frags;
3251 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003252 int offset = 0;
3253 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00003254 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003255
3256 do {
3257 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003258 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00003259 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003260
Eric Dumazetb2111722012-12-28 06:06:37 +00003261 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003262 return -ENOMEM;
3263
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003264 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00003265 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003266
Eric Dumazetb2111722012-12-28 06:06:37 +00003267 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3268 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003269 if (ret < 0)
3270 return -EFAULT;
3271
3272 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003273 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3274 copy);
3275 frg_cnt++;
3276 pfrag->offset += copy;
3277 get_page(pfrag->page);
3278
3279 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003280 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003281 skb->len += copy;
3282 skb->data_len += copy;
3283 offset += copy;
3284 length -= copy;
3285
3286 } while (length > 0);
3287
3288 return 0;
3289}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003290EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003291
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003292int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3293 int offset, size_t size)
3294{
3295 int i = skb_shinfo(skb)->nr_frags;
3296
3297 if (skb_can_coalesce(skb, i, page, offset)) {
3298 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3299 } else if (i < MAX_SKB_FRAGS) {
3300 get_page(page);
3301 skb_fill_page_desc(skb, i, page, offset, size);
3302 } else {
3303 return -EMSGSIZE;
3304 }
3305
3306 return 0;
3307}
3308EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3309
Herbert Xucbb042f2006-03-20 22:43:56 -08003310/**
3311 * skb_pull_rcsum - pull skb and update receive checksum
3312 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003313 * @len: length of data pulled
3314 *
3315 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003316 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003317 * receive path processing instead of skb_pull unless you know
3318 * that the checksum difference is zero (e.g., a valid IP header)
3319 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003320 */
Johannes Bergaf728682017-06-16 14:29:22 +02003321void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003322{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003323 unsigned char *data = skb->data;
3324
Herbert Xucbb042f2006-03-20 22:43:56 -08003325 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003326 __skb_pull(skb, len);
3327 skb_postpull_rcsum(skb, data, len);
3328 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003329}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003330EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3331
Herbert Xuf4c50d92006-06-22 03:02:40 -07003332/**
3333 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003334 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003335 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003336 *
3337 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003338 * a pointer to the first in a list of new skbs for the segments.
3339 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003340 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003341struct sk_buff *skb_segment(struct sk_buff *head_skb,
3342 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003343{
3344 struct sk_buff *segs = NULL;
3345 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003346 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003347 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3348 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3349 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003350 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003351 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003352 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003353 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003354 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003355 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003356 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003357 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003358 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003359 int err = -ENOMEM;
3360 int i = 0;
3361 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003362 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003363
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003364 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003365 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003366 if (unlikely(!proto))
3367 return ERR_PTR(-EINVAL);
3368
Alexander Duyck36c98382016-05-02 09:38:18 -07003369 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003370 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003371
Steffen Klassert07b26c92016-09-19 12:58:47 +02003372 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3373 if (!(features & NETIF_F_GSO_PARTIAL)) {
3374 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003375 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003376
3377 if (!list_skb ||
3378 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3379 goto normal;
3380
Ilan Tayari43170c42017-04-19 21:26:07 +03003381 /* If we get here then all the required
3382 * GSO features except frag_list are supported.
3383 * Try to split the SKB to multiple GSO SKBs
3384 * with no frag_list.
3385 * Currently we can do that only when the buffers don't
3386 * have a linear part and all the buffers except
3387 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003388 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003389 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003390 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003391 if (frag_len != iter->len && iter->next)
3392 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003393 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003394 goto normal;
3395
3396 len -= iter->len;
3397 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003398
3399 if (len != frag_len)
3400 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003401 }
3402
3403 /* GSO partial only requires that we trim off any excess that
3404 * doesn't fit into an MSS sized block, so take care of that
3405 * now.
3406 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003407 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003408 if (partial_segs > 1)
3409 mss *= partial_segs;
3410 else
3411 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003412 }
3413
Steffen Klassert07b26c92016-09-19 12:58:47 +02003414normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003415 headroom = skb_headroom(head_skb);
3416 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003417
3418 do {
3419 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003420 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003421 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003422 int size;
3423
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003424 if (unlikely(mss == GSO_BY_FRAGS)) {
3425 len = list_skb->len;
3426 } else {
3427 len = head_skb->len - offset;
3428 if (len > mss)
3429 len = mss;
3430 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003431
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003432 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003433 if (hsize < 0)
3434 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003435 if (hsize > len || !sg)
3436 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003437
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003438 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3439 (skb_headlen(list_skb) == len || sg)) {
3440 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003441
Herbert Xu9d8506c2013-11-21 11:10:04 -08003442 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003443 nfrags = skb_shinfo(list_skb)->nr_frags;
3444 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003445 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003446 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003447
3448 while (pos < offset + len) {
3449 BUG_ON(i >= nfrags);
3450
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003451 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003452 if (pos + size > offset + len)
3453 break;
3454
3455 i++;
3456 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003457 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003458 }
3459
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003460 nskb = skb_clone(list_skb, GFP_ATOMIC);
3461 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003462
3463 if (unlikely(!nskb))
3464 goto err;
3465
Herbert Xu9d8506c2013-11-21 11:10:04 -08003466 if (unlikely(pskb_trim(nskb, len))) {
3467 kfree_skb(nskb);
3468 goto err;
3469 }
3470
Alexander Duyckec47ea82012-05-04 14:26:56 +00003471 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003472 if (skb_cow_head(nskb, doffset + headroom)) {
3473 kfree_skb(nskb);
3474 goto err;
3475 }
3476
Alexander Duyckec47ea82012-05-04 14:26:56 +00003477 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003478 skb_release_head_state(nskb);
3479 __skb_push(nskb, doffset);
3480 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003481 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003482 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003483 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003484
3485 if (unlikely(!nskb))
3486 goto err;
3487
3488 skb_reserve(nskb, headroom);
3489 __skb_put(nskb, doffset);
3490 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003491
3492 if (segs)
3493 tail->next = nskb;
3494 else
3495 segs = nskb;
3496 tail = nskb;
3497
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003498 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003499
Eric Dumazet030737b2013-10-19 11:42:54 -07003500 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003501 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003502
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003503 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003504 nskb->data - tnl_hlen,
3505 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003506
Herbert Xu9d8506c2013-11-21 11:10:04 -08003507 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003508 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003509
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003510 if (!sg) {
3511 if (!nskb->remcsum_offload)
3512 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003513 SKB_GSO_CB(nskb)->csum =
3514 skb_copy_and_csum_bits(head_skb, offset,
3515 skb_put(nskb, len),
3516 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003517 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003518 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003519 continue;
3520 }
3521
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003522 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003523
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003524 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003525 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003526
Willem de Bruijnfff88032017-06-08 11:35:03 -04003527 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3528 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003529 if (skb_zerocopy_clone(nskb, head_skb, GFP_ATOMIC))
3530 goto err;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003531
Herbert Xu9d8506c2013-11-21 11:10:04 -08003532 while (pos < offset + len) {
3533 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003534 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003535
3536 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003537 nfrags = skb_shinfo(list_skb)->nr_frags;
3538 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003539 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003540
3541 BUG_ON(!nfrags);
3542
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003543 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003544 }
3545
3546 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3547 MAX_SKB_FRAGS)) {
3548 net_warn_ratelimited(
3549 "skb_segment: too many frags: %u %u\n",
3550 pos, mss);
3551 goto err;
3552 }
3553
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003554 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3555 goto err;
3556
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003557 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003558 __skb_frag_ref(nskb_frag);
3559 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003560
3561 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003562 nskb_frag->page_offset += offset - pos;
3563 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003564 }
3565
Herbert Xu89319d382008-12-15 23:26:06 -08003566 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003567
3568 if (pos + size <= offset + len) {
3569 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003570 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003571 pos += size;
3572 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003573 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003574 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003575 }
3576
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003577 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003578 }
3579
Herbert Xu89319d382008-12-15 23:26:06 -08003580skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003581 nskb->data_len = len - hsize;
3582 nskb->len += nskb->data_len;
3583 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003584
Simon Horman1cdbcb72013-05-19 15:46:49 +00003585perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003586 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003587 if (skb_has_shared_frag(nskb)) {
3588 err = __skb_linearize(nskb);
3589 if (err)
3590 goto err;
3591 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003592 if (!nskb->remcsum_offload)
3593 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003594 SKB_GSO_CB(nskb)->csum =
3595 skb_checksum(nskb, doffset,
3596 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003597 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003598 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003599 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003600 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003601
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003602 /* Some callers want to get the end of the list.
3603 * Put it in segs->prev to avoid walking the list.
3604 * (see validate_xmit_skb_list() for example)
3605 */
3606 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003607
Alexander Duyck802ab552016-04-10 21:45:03 -04003608 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003609 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003610 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003611 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003612
3613 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003614 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003615 type &= ~SKB_GSO_DODGY;
3616
3617 /* Update GSO info and prepare to start updating headers on
3618 * our way back down the stack of protocols.
3619 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003620 for (iter = segs; iter; iter = iter->next) {
3621 skb_shinfo(iter)->gso_size = gso_size;
3622 skb_shinfo(iter)->gso_segs = partial_segs;
3623 skb_shinfo(iter)->gso_type = type;
3624 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3625 }
3626
3627 if (tail->len - doffset <= gso_size)
3628 skb_shinfo(tail)->gso_size = 0;
3629 else if (tail != segs)
3630 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003631 }
3632
Toshiaki Makita432c8562014-10-27 10:30:51 -07003633 /* Following permits correct backpressure, for protocols
3634 * using skb_set_owner_w().
3635 * Idea is to tranfert ownership from head_skb to last segment.
3636 */
3637 if (head_skb->destructor == sock_wfree) {
3638 swap(tail->truesize, head_skb->truesize);
3639 swap(tail->destructor, head_skb->destructor);
3640 swap(tail->sk, head_skb->sk);
3641 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003642 return segs;
3643
3644err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003645 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003646 return ERR_PTR(err);
3647}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003648EXPORT_SYMBOL_GPL(skb_segment);
3649
Herbert Xu71d93b32008-12-15 23:42:33 -08003650int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3651{
Eric Dumazet8a291112013-10-08 09:02:23 -07003652 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003653 unsigned int offset = skb_gro_offset(skb);
3654 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003655 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003656 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003657 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003658
Eric Dumazet8a291112013-10-08 09:02:23 -07003659 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003660 return -E2BIG;
3661
Eric Dumazet29e98242014-05-16 11:34:37 -07003662 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003663 pinfo = skb_shinfo(lp);
3664
3665 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003666 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003667 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003668 int i = skbinfo->nr_frags;
3669 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003670
Herbert Xu66e92fc2009-05-26 18:50:32 +00003671 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003672 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003673
Eric Dumazet8a291112013-10-08 09:02:23 -07003674 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003675 pinfo->nr_frags = nr_frags;
3676 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003677
Herbert Xu9aaa1562009-05-26 18:50:33 +00003678 frag = pinfo->frags + nr_frags;
3679 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003680 do {
3681 *--frag = *--frag2;
3682 } while (--i);
3683
3684 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003685 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003686
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003687 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003688 delta_truesize = skb->truesize -
3689 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003690
Herbert Xuf5572062009-01-14 20:40:03 -08003691 skb->truesize -= skb->data_len;
3692 skb->len -= skb->data_len;
3693 skb->data_len = 0;
3694
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003695 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003696 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003697 } else if (skb->head_frag) {
3698 int nr_frags = pinfo->nr_frags;
3699 skb_frag_t *frag = pinfo->frags + nr_frags;
3700 struct page *page = virt_to_head_page(skb->head);
3701 unsigned int first_size = headlen - offset;
3702 unsigned int first_offset;
3703
3704 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003705 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003706
3707 first_offset = skb->data -
3708 (unsigned char *)page_address(page) +
3709 offset;
3710
3711 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3712
3713 frag->page.p = page;
3714 frag->page_offset = first_offset;
3715 skb_frag_size_set(frag, first_size);
3716
3717 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3718 /* We dont need to clear skbinfo->nr_frags here */
3719
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003720 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003721 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3722 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003723 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003724
3725merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003726 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003727 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003728 unsigned int eat = offset - headlen;
3729
3730 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003731 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003732 skb->data_len -= eat;
3733 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003734 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003735 }
3736
Herbert Xu67147ba2009-05-26 18:50:22 +00003737 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003738
Eric Dumazet29e98242014-05-16 11:34:37 -07003739 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003740 skb_shinfo(p)->frag_list = skb;
3741 else
3742 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003743 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003744 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003745 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003746
Herbert Xu5d38a072009-01-04 16:13:40 -08003747done:
3748 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003749 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003750 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003751 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003752 if (lp != p) {
3753 lp->data_len += len;
3754 lp->truesize += delta_truesize;
3755 lp->len += len;
3756 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003757 NAPI_GRO_CB(skb)->same_flow = 1;
3758 return 0;
3759}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003760EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762void __init skb_init(void)
3763{
3764 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3765 sizeof(struct sk_buff),
3766 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003767 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003768 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003769 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003770 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003771 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003772 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003773 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
David S. Miller51c739d2007-10-30 21:29:29 -07003776static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003777__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3778 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003779{
David S. Miller1a028e52007-04-27 15:21:23 -07003780 int start = skb_headlen(skb);
3781 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003782 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003783 int elt = 0;
3784
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003785 if (unlikely(recursion_level >= 24))
3786 return -EMSGSIZE;
3787
David Howells716ea3a2007-04-02 20:19:53 -07003788 if (copy > 0) {
3789 if (copy > len)
3790 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003791 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003792 elt++;
3793 if ((len -= copy) == 0)
3794 return elt;
3795 offset += copy;
3796 }
3797
3798 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003799 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003800
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003801 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003802
Eric Dumazet9e903e02011-10-18 21:00:24 +00003803 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003804 if ((copy = end - offset) > 0) {
3805 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003806 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3807 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003808
3809 if (copy > len)
3810 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003811 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003812 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003813 elt++;
3814 if (!(len -= copy))
3815 return elt;
3816 offset += copy;
3817 }
David S. Miller1a028e52007-04-27 15:21:23 -07003818 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003819 }
3820
David S. Millerfbb398a2009-06-09 00:18:59 -07003821 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003822 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003823
David S. Millerfbb398a2009-06-09 00:18:59 -07003824 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003825
David S. Millerfbb398a2009-06-09 00:18:59 -07003826 end = start + frag_iter->len;
3827 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003828 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3829 return -EMSGSIZE;
3830
David S. Millerfbb398a2009-06-09 00:18:59 -07003831 if (copy > len)
3832 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003833 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3834 copy, recursion_level + 1);
3835 if (unlikely(ret < 0))
3836 return ret;
3837 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003838 if ((len -= copy) == 0)
3839 return elt;
3840 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003841 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003842 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003843 }
3844 BUG_ON(len);
3845 return elt;
3846}
3847
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003848/**
3849 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3850 * @skb: Socket buffer containing the buffers to be mapped
3851 * @sg: The scatter-gather list to map into
3852 * @offset: The offset into the buffer's contents to start mapping
3853 * @len: Length of buffer space to be mapped
3854 *
3855 * Fill the specified scatter-gather list with mappings/pointers into a
3856 * region of the buffer space attached to a socket buffer. Returns either
3857 * the number of scatterlist items used, or -EMSGSIZE if the contents
3858 * could not fit.
3859 */
3860int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3861{
3862 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3863
3864 if (nsg <= 0)
3865 return nsg;
3866
3867 sg_mark_end(&sg[nsg - 1]);
3868
3869 return nsg;
3870}
3871EXPORT_SYMBOL_GPL(skb_to_sgvec);
3872
Fan Du25a91d82014-01-18 09:54:23 +08003873/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3874 * sglist without mark the sg which contain last skb data as the end.
3875 * So the caller can mannipulate sg list as will when padding new data after
3876 * the first call without calling sg_unmark_end to expend sg list.
3877 *
3878 * Scenario to use skb_to_sgvec_nomark:
3879 * 1. sg_init_table
3880 * 2. skb_to_sgvec_nomark(payload1)
3881 * 3. skb_to_sgvec_nomark(payload2)
3882 *
3883 * This is equivalent to:
3884 * 1. sg_init_table
3885 * 2. skb_to_sgvec(payload1)
3886 * 3. sg_unmark_end
3887 * 4. skb_to_sgvec(payload2)
3888 *
3889 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3890 * is more preferable.
3891 */
3892int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3893 int offset, int len)
3894{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003895 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003896}
3897EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3898
David S. Miller51c739d2007-10-30 21:29:29 -07003899
David S. Miller51c739d2007-10-30 21:29:29 -07003900
David Howells716ea3a2007-04-02 20:19:53 -07003901/**
3902 * skb_cow_data - Check that a socket buffer's data buffers are writable
3903 * @skb: The socket buffer to check.
3904 * @tailbits: Amount of trailing space to be added
3905 * @trailer: Returned pointer to the skb where the @tailbits space begins
3906 *
3907 * Make sure that the data buffers attached to a socket buffer are
3908 * writable. If they are not, private copies are made of the data buffers
3909 * and the socket buffer is set to use these instead.
3910 *
3911 * If @tailbits is given, make sure that there is space to write @tailbits
3912 * bytes of data beyond current end of socket buffer. @trailer will be
3913 * set to point to the skb in which this space begins.
3914 *
3915 * The number of scatterlist elements required to completely map the
3916 * COW'd and extended socket buffer will be returned.
3917 */
3918int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3919{
3920 int copyflag;
3921 int elt;
3922 struct sk_buff *skb1, **skb_p;
3923
3924 /* If skb is cloned or its head is paged, reallocate
3925 * head pulling out all the pages (pages are considered not writable
3926 * at the moment even if they are anonymous).
3927 */
3928 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3929 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3930 return -ENOMEM;
3931
3932 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003933 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003934 /* A little of trouble, not enough of space for trailer.
3935 * This should not happen, when stack is tuned to generate
3936 * good frames. OK, on miss we reallocate and reserve even more
3937 * space, 128 bytes is fair. */
3938
3939 if (skb_tailroom(skb) < tailbits &&
3940 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3941 return -ENOMEM;
3942
3943 /* Voila! */
3944 *trailer = skb;
3945 return 1;
3946 }
3947
3948 /* Misery. We are in troubles, going to mincer fragments... */
3949
3950 elt = 1;
3951 skb_p = &skb_shinfo(skb)->frag_list;
3952 copyflag = 0;
3953
3954 while ((skb1 = *skb_p) != NULL) {
3955 int ntail = 0;
3956
3957 /* The fragment is partially pulled by someone,
3958 * this can happen on input. Copy it and everything
3959 * after it. */
3960
3961 if (skb_shared(skb1))
3962 copyflag = 1;
3963
3964 /* If the skb is the last, worry about trailer. */
3965
3966 if (skb1->next == NULL && tailbits) {
3967 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003968 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003969 skb_tailroom(skb1) < tailbits)
3970 ntail = tailbits + 128;
3971 }
3972
3973 if (copyflag ||
3974 skb_cloned(skb1) ||
3975 ntail ||
3976 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003977 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003978 struct sk_buff *skb2;
3979
3980 /* Fuck, we are miserable poor guys... */
3981 if (ntail == 0)
3982 skb2 = skb_copy(skb1, GFP_ATOMIC);
3983 else
3984 skb2 = skb_copy_expand(skb1,
3985 skb_headroom(skb1),
3986 ntail,
3987 GFP_ATOMIC);
3988 if (unlikely(skb2 == NULL))
3989 return -ENOMEM;
3990
3991 if (skb1->sk)
3992 skb_set_owner_w(skb2, skb1->sk);
3993
3994 /* Looking around. Are we still alive?
3995 * OK, link new skb, drop old one */
3996
3997 skb2->next = skb1->next;
3998 *skb_p = skb2;
3999 kfree_skb(skb1);
4000 skb1 = skb2;
4001 }
4002 elt++;
4003 *trailer = skb1;
4004 skb_p = &skb1->next;
4005 }
4006
4007 return elt;
4008}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004009EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004010
Eric Dumazetb1faf562010-05-31 23:44:05 -07004011static void sock_rmem_free(struct sk_buff *skb)
4012{
4013 struct sock *sk = skb->sk;
4014
4015 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4016}
4017
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004018static void skb_set_err_queue(struct sk_buff *skb)
4019{
4020 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4021 * So, it is safe to (mis)use it to mark skbs on the error queue.
4022 */
4023 skb->pkt_type = PACKET_OUTGOING;
4024 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4025}
4026
Eric Dumazetb1faf562010-05-31 23:44:05 -07004027/*
4028 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4029 */
4030int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4031{
4032 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004033 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004034 return -ENOMEM;
4035
4036 skb_orphan(skb);
4037 skb->sk = sk;
4038 skb->destructor = sock_rmem_free;
4039 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004040 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004041
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004042 /* before exiting rcu section, make sure dst is refcounted */
4043 skb_dst_force(skb);
4044
Eric Dumazetb1faf562010-05-31 23:44:05 -07004045 skb_queue_tail(&sk->sk_error_queue, skb);
4046 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04004047 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004048 return 0;
4049}
4050EXPORT_SYMBOL(sock_queue_err_skb);
4051
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004052static bool is_icmp_err_skb(const struct sk_buff *skb)
4053{
4054 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4055 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4056}
4057
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004058struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4059{
4060 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004061 struct sk_buff *skb, *skb_next = NULL;
4062 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004063 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004064
Eric Dumazet997d5c32015-02-18 05:47:55 -08004065 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004066 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004067 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004068 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004069 if (icmp_next)
4070 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4071 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004072 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004073
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004074 if (is_icmp_err_skb(skb) && !icmp_next)
4075 sk->sk_err = 0;
4076
4077 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004078 sk->sk_error_report(sk);
4079
4080 return skb;
4081}
4082EXPORT_SYMBOL(sock_dequeue_err_skb);
4083
Alexander Duyckcab41c42014-09-10 18:05:26 -04004084/**
4085 * skb_clone_sk - create clone of skb, and take reference to socket
4086 * @skb: the skb to clone
4087 *
4088 * This function creates a clone of a buffer that holds a reference on
4089 * sk_refcnt. Buffers created via this function are meant to be
4090 * returned using sock_queue_err_skb, or free via kfree_skb.
4091 *
4092 * When passing buffers allocated with this function to sock_queue_err_skb
4093 * it is necessary to wrap the call with sock_hold/sock_put in order to
4094 * prevent the socket from being released prior to being enqueued on
4095 * the sk_error_queue.
4096 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004097struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4098{
4099 struct sock *sk = skb->sk;
4100 struct sk_buff *clone;
4101
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004102 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004103 return NULL;
4104
4105 clone = skb_clone(skb, GFP_ATOMIC);
4106 if (!clone) {
4107 sock_put(sk);
4108 return NULL;
4109 }
4110
4111 clone->sk = sk;
4112 clone->destructor = sock_efree;
4113
4114 return clone;
4115}
4116EXPORT_SYMBOL(skb_clone_sk);
4117
Alexander Duyck37846ef2014-09-04 13:31:10 -04004118static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4119 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004120 int tstype,
4121 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004122{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004123 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004124 int err;
4125
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004126 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4127
Patrick Ohlyac45f602009-02-12 05:03:37 +00004128 serr = SKB_EXT_ERR(skb);
4129 memset(serr, 0, sizeof(*serr));
4130 serr->ee.ee_errno = ENOMSG;
4131 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004132 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004133 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004134 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004135 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004136 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004137 if (sk->sk_protocol == IPPROTO_TCP &&
4138 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004139 serr->ee.ee_data -= sk->sk_tskey;
4140 }
Eric Dumazet29030372010-05-29 00:20:48 -07004141
Patrick Ohlyac45f602009-02-12 05:03:37 +00004142 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004143
Patrick Ohlyac45f602009-02-12 05:03:37 +00004144 if (err)
4145 kfree_skb(skb);
4146}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004147
Willem de Bruijnb245be12015-01-30 13:29:32 -05004148static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4149{
4150 bool ret;
4151
4152 if (likely(sysctl_tstamp_allow_data || tsonly))
4153 return true;
4154
4155 read_lock_bh(&sk->sk_callback_lock);
4156 ret = sk->sk_socket && sk->sk_socket->file &&
4157 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4158 read_unlock_bh(&sk->sk_callback_lock);
4159 return ret;
4160}
4161
Alexander Duyck37846ef2014-09-04 13:31:10 -04004162void skb_complete_tx_timestamp(struct sk_buff *skb,
4163 struct skb_shared_hwtstamps *hwtstamps)
4164{
4165 struct sock *sk = skb->sk;
4166
Willem de Bruijnb245be12015-01-30 13:29:32 -05004167 if (!skb_may_tx_timestamp(sk, false))
4168 return;
4169
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004170 /* Take a reference to prevent skb_orphan() from freeing the socket,
4171 * but only if the socket refcount is not zero.
4172 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004173 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004174 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004175 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004176 sock_put(sk);
4177 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004178}
4179EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4180
4181void __skb_tstamp_tx(struct sk_buff *orig_skb,
4182 struct skb_shared_hwtstamps *hwtstamps,
4183 struct sock *sk, int tstype)
4184{
4185 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004186 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004187
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004188 if (!sk)
4189 return;
4190
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004191 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4192 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4193 return;
4194
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004195 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4196 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004197 return;
4198
Francis Yan1c885802016-11-27 23:07:18 -08004199 if (tsonly) {
4200#ifdef CONFIG_INET
4201 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4202 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004203 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004204 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004205 opt_stats = true;
4206 } else
Francis Yan1c885802016-11-27 23:07:18 -08004207#endif
4208 skb = alloc_skb(0, GFP_ATOMIC);
4209 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004210 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004211 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004212 if (!skb)
4213 return;
4214
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004215 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004216 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4217 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004218 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4219 }
4220
4221 if (hwtstamps)
4222 *skb_hwtstamps(skb) = *hwtstamps;
4223 else
4224 skb->tstamp = ktime_get_real();
4225
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004226 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004227}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004228EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4229
4230void skb_tstamp_tx(struct sk_buff *orig_skb,
4231 struct skb_shared_hwtstamps *hwtstamps)
4232{
4233 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4234 SCM_TSTAMP_SND);
4235}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004236EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4237
Johannes Berg6e3e9392011-11-09 10:15:42 +01004238void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4239{
4240 struct sock *sk = skb->sk;
4241 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004242 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004243
4244 skb->wifi_acked_valid = 1;
4245 skb->wifi_acked = acked;
4246
4247 serr = SKB_EXT_ERR(skb);
4248 memset(serr, 0, sizeof(*serr));
4249 serr->ee.ee_errno = ENOMSG;
4250 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4251
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004252 /* Take a reference to prevent skb_orphan() from freeing the socket,
4253 * but only if the socket refcount is not zero.
4254 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004255 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004256 err = sock_queue_err_skb(sk, skb);
4257 sock_put(sk);
4258 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004259 if (err)
4260 kfree_skb(skb);
4261}
4262EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4263
Rusty Russellf35d9d82008-02-04 23:49:54 -05004264/**
4265 * skb_partial_csum_set - set up and verify partial csum values for packet
4266 * @skb: the skb to set
4267 * @start: the number of bytes after skb->data to start checksumming.
4268 * @off: the offset from start to place the checksum.
4269 *
4270 * For untrusted partially-checksummed packets, we need to make sure the values
4271 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4272 *
4273 * This function checks and sets those values and skb->ip_summed: if this
4274 * returns false you should drop the packet.
4275 */
4276bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4277{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004278 if (unlikely(start > skb_headlen(skb)) ||
4279 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004280 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4281 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004282 return false;
4283 }
4284 skb->ip_summed = CHECKSUM_PARTIAL;
4285 skb->csum_start = skb_headroom(skb) + start;
4286 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004287 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004288 return true;
4289}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004290EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004291
Paul Durranted1f50c2014-01-09 10:02:46 +00004292static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4293 unsigned int max)
4294{
4295 if (skb_headlen(skb) >= len)
4296 return 0;
4297
4298 /* If we need to pullup then pullup to the max, so we
4299 * won't need to do it again.
4300 */
4301 if (max > skb->len)
4302 max = skb->len;
4303
4304 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4305 return -ENOMEM;
4306
4307 if (skb_headlen(skb) < len)
4308 return -EPROTO;
4309
4310 return 0;
4311}
4312
Jan Beulichf9708b42014-03-11 13:56:05 +00004313#define MAX_TCP_HDR_LEN (15 * 4)
4314
4315static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4316 typeof(IPPROTO_IP) proto,
4317 unsigned int off)
4318{
4319 switch (proto) {
4320 int err;
4321
4322 case IPPROTO_TCP:
4323 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4324 off + MAX_TCP_HDR_LEN);
4325 if (!err && !skb_partial_csum_set(skb, off,
4326 offsetof(struct tcphdr,
4327 check)))
4328 err = -EPROTO;
4329 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4330
4331 case IPPROTO_UDP:
4332 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4333 off + sizeof(struct udphdr));
4334 if (!err && !skb_partial_csum_set(skb, off,
4335 offsetof(struct udphdr,
4336 check)))
4337 err = -EPROTO;
4338 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4339 }
4340
4341 return ERR_PTR(-EPROTO);
4342}
4343
Paul Durranted1f50c2014-01-09 10:02:46 +00004344/* This value should be large enough to cover a tagged ethernet header plus
4345 * maximally sized IP and TCP or UDP headers.
4346 */
4347#define MAX_IP_HDR_LEN 128
4348
Jan Beulichf9708b42014-03-11 13:56:05 +00004349static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004350{
4351 unsigned int off;
4352 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004353 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004354 int err;
4355
4356 fragment = false;
4357
4358 err = skb_maybe_pull_tail(skb,
4359 sizeof(struct iphdr),
4360 MAX_IP_HDR_LEN);
4361 if (err < 0)
4362 goto out;
4363
4364 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4365 fragment = true;
4366
4367 off = ip_hdrlen(skb);
4368
4369 err = -EPROTO;
4370
4371 if (fragment)
4372 goto out;
4373
Jan Beulichf9708b42014-03-11 13:56:05 +00004374 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4375 if (IS_ERR(csum))
4376 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004377
Jan Beulichf9708b42014-03-11 13:56:05 +00004378 if (recalculate)
4379 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4380 ip_hdr(skb)->daddr,
4381 skb->len - off,
4382 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004383 err = 0;
4384
4385out:
4386 return err;
4387}
4388
4389/* This value should be large enough to cover a tagged ethernet header plus
4390 * an IPv6 header, all options, and a maximal TCP or UDP header.
4391 */
4392#define MAX_IPV6_HDR_LEN 256
4393
4394#define OPT_HDR(type, skb, off) \
4395 (type *)(skb_network_header(skb) + (off))
4396
4397static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4398{
4399 int err;
4400 u8 nexthdr;
4401 unsigned int off;
4402 unsigned int len;
4403 bool fragment;
4404 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004405 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004406
4407 fragment = false;
4408 done = false;
4409
4410 off = sizeof(struct ipv6hdr);
4411
4412 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4413 if (err < 0)
4414 goto out;
4415
4416 nexthdr = ipv6_hdr(skb)->nexthdr;
4417
4418 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4419 while (off <= len && !done) {
4420 switch (nexthdr) {
4421 case IPPROTO_DSTOPTS:
4422 case IPPROTO_HOPOPTS:
4423 case IPPROTO_ROUTING: {
4424 struct ipv6_opt_hdr *hp;
4425
4426 err = skb_maybe_pull_tail(skb,
4427 off +
4428 sizeof(struct ipv6_opt_hdr),
4429 MAX_IPV6_HDR_LEN);
4430 if (err < 0)
4431 goto out;
4432
4433 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4434 nexthdr = hp->nexthdr;
4435 off += ipv6_optlen(hp);
4436 break;
4437 }
4438 case IPPROTO_AH: {
4439 struct ip_auth_hdr *hp;
4440
4441 err = skb_maybe_pull_tail(skb,
4442 off +
4443 sizeof(struct ip_auth_hdr),
4444 MAX_IPV6_HDR_LEN);
4445 if (err < 0)
4446 goto out;
4447
4448 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4449 nexthdr = hp->nexthdr;
4450 off += ipv6_authlen(hp);
4451 break;
4452 }
4453 case IPPROTO_FRAGMENT: {
4454 struct frag_hdr *hp;
4455
4456 err = skb_maybe_pull_tail(skb,
4457 off +
4458 sizeof(struct frag_hdr),
4459 MAX_IPV6_HDR_LEN);
4460 if (err < 0)
4461 goto out;
4462
4463 hp = OPT_HDR(struct frag_hdr, skb, off);
4464
4465 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4466 fragment = true;
4467
4468 nexthdr = hp->nexthdr;
4469 off += sizeof(struct frag_hdr);
4470 break;
4471 }
4472 default:
4473 done = true;
4474 break;
4475 }
4476 }
4477
4478 err = -EPROTO;
4479
4480 if (!done || fragment)
4481 goto out;
4482
Jan Beulichf9708b42014-03-11 13:56:05 +00004483 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4484 if (IS_ERR(csum))
4485 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004486
Jan Beulichf9708b42014-03-11 13:56:05 +00004487 if (recalculate)
4488 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4489 &ipv6_hdr(skb)->daddr,
4490 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004491 err = 0;
4492
4493out:
4494 return err;
4495}
4496
4497/**
4498 * skb_checksum_setup - set up partial checksum offset
4499 * @skb: the skb to set up
4500 * @recalculate: if true the pseudo-header checksum will be recalculated
4501 */
4502int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4503{
4504 int err;
4505
4506 switch (skb->protocol) {
4507 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004508 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004509 break;
4510
4511 case htons(ETH_P_IPV6):
4512 err = skb_checksum_setup_ipv6(skb, recalculate);
4513 break;
4514
4515 default:
4516 err = -EPROTO;
4517 break;
4518 }
4519
4520 return err;
4521}
4522EXPORT_SYMBOL(skb_checksum_setup);
4523
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004524/**
4525 * skb_checksum_maybe_trim - maybe trims the given skb
4526 * @skb: the skb to check
4527 * @transport_len: the data length beyond the network header
4528 *
4529 * Checks whether the given skb has data beyond the given transport length.
4530 * If so, returns a cloned skb trimmed to this transport length.
4531 * Otherwise returns the provided skb. Returns NULL in error cases
4532 * (e.g. transport_len exceeds skb length or out-of-memory).
4533 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004534 * Caller needs to set the skb transport header and free any returned skb if it
4535 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004536 */
4537static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4538 unsigned int transport_len)
4539{
4540 struct sk_buff *skb_chk;
4541 unsigned int len = skb_transport_offset(skb) + transport_len;
4542 int ret;
4543
Linus Lüssinga5169932015-08-13 05:54:07 +02004544 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004545 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004546 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004547 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004548
4549 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004550 if (!skb_chk)
4551 return NULL;
4552
4553 ret = pskb_trim_rcsum(skb_chk, len);
4554 if (ret) {
4555 kfree_skb(skb_chk);
4556 return NULL;
4557 }
4558
4559 return skb_chk;
4560}
4561
4562/**
4563 * skb_checksum_trimmed - validate checksum of an skb
4564 * @skb: the skb to check
4565 * @transport_len: the data length beyond the network header
4566 * @skb_chkf: checksum function to use
4567 *
4568 * Applies the given checksum function skb_chkf to the provided skb.
4569 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4570 *
4571 * If the skb has data beyond the given transport length, then a
4572 * trimmed & cloned skb is checked and returned.
4573 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004574 * Caller needs to set the skb transport header and free any returned skb if it
4575 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004576 */
4577struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4578 unsigned int transport_len,
4579 __sum16(*skb_chkf)(struct sk_buff *skb))
4580{
4581 struct sk_buff *skb_chk;
4582 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004583 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004584
4585 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4586 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004587 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004588
Linus Lüssinga5169932015-08-13 05:54:07 +02004589 if (!pskb_may_pull(skb_chk, offset))
4590 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004591
Linus Lüssing9b368812016-02-24 04:21:42 +01004592 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004593 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004594 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004595
Linus Lüssinga5169932015-08-13 05:54:07 +02004596 if (ret)
4597 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004598
4599 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004600
4601err:
4602 if (skb_chk && skb_chk != skb)
4603 kfree_skb(skb_chk);
4604
4605 return NULL;
4606
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004607}
4608EXPORT_SYMBOL(skb_checksum_trimmed);
4609
Ben Hutchings4497b072008-06-19 16:22:28 -07004610void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4611{
Joe Perchese87cc472012-05-13 21:56:26 +00004612 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4613 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004614}
Ben Hutchings4497b072008-06-19 16:22:28 -07004615EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004616
4617void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4618{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004619 if (head_stolen) {
4620 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004621 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004622 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004623 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004624 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004625}
4626EXPORT_SYMBOL(kfree_skb_partial);
4627
4628/**
4629 * skb_try_coalesce - try to merge skb to prior one
4630 * @to: prior buffer
4631 * @from: buffer to add
4632 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004633 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004634 */
4635bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4636 bool *fragstolen, int *delta_truesize)
4637{
4638 int i, delta, len = from->len;
4639
4640 *fragstolen = false;
4641
4642 if (skb_cloned(to))
4643 return false;
4644
4645 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004646 if (len)
4647 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004648 *delta_truesize = 0;
4649 return true;
4650 }
4651
4652 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4653 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004654 if (skb_zcopy(to) || skb_zcopy(from))
4655 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004656
4657 if (skb_headlen(from) != 0) {
4658 struct page *page;
4659 unsigned int offset;
4660
4661 if (skb_shinfo(to)->nr_frags +
4662 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4663 return false;
4664
4665 if (skb_head_is_locked(from))
4666 return false;
4667
4668 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4669
4670 page = virt_to_head_page(from->head);
4671 offset = from->data - (unsigned char *)page_address(page);
4672
4673 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4674 page, offset, skb_headlen(from));
4675 *fragstolen = true;
4676 } else {
4677 if (skb_shinfo(to)->nr_frags +
4678 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4679 return false;
4680
Weiping Panf4b549a2012-09-28 20:15:30 +00004681 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004682 }
4683
4684 WARN_ON_ONCE(delta < len);
4685
4686 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4687 skb_shinfo(from)->frags,
4688 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4689 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4690
4691 if (!skb_cloned(from))
4692 skb_shinfo(from)->nr_frags = 0;
4693
Li RongQing8ea853f2012-09-18 16:53:21 +00004694 /* if the skb is not cloned this does nothing
4695 * since we set nr_frags to 0.
4696 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004697 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4698 skb_frag_ref(from, i);
4699
4700 to->truesize += delta;
4701 to->len += len;
4702 to->data_len += len;
4703
4704 *delta_truesize = delta;
4705 return true;
4706}
4707EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004708
4709/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004710 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004711 *
4712 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004713 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004714 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004715 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4716 * into/from a tunnel. Some information have to be cleared during these
4717 * operations.
4718 * skb_scrub_packet can also be used to clean a skb before injecting it in
4719 * another namespace (@xnet == true). We have to clear all information in the
4720 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004721 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004722void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004723{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004724 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004725 skb->pkt_type = PACKET_HOST;
4726 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004727 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004728 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004729 secpath_reset(skb);
4730 nf_reset(skb);
4731 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004732
4733 if (!xnet)
4734 return;
4735
4736 skb_orphan(skb);
4737 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004738}
4739EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004740
4741/**
4742 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4743 *
4744 * @skb: GSO skb
4745 *
4746 * skb_gso_transport_seglen is used to determine the real size of the
4747 * individual segments, including Layer4 headers (TCP/UDP).
4748 *
4749 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4750 */
4751unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4752{
4753 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004754 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004755
Florian Westphalf993bc22014-10-20 13:49:18 +02004756 if (skb->encapsulation) {
4757 thlen = skb_inner_transport_header(skb) -
4758 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004759
Florian Westphalf993bc22014-10-20 13:49:18 +02004760 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4761 thlen += inner_tcp_hdrlen(skb);
4762 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4763 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004764 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4765 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004766 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004767 /* UFO sets gso_size to the size of the fragmentation
4768 * payload, i.e. the size of the L4 (UDP) header is already
4769 * accounted for.
4770 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004771 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004772}
4773EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004774
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004775/**
4776 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4777 *
4778 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004779 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004780 *
4781 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4782 * once split.
4783 */
4784bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4785{
4786 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4787 const struct sk_buff *iter;
4788 unsigned int hlen;
4789
4790 hlen = skb_gso_network_seglen(skb);
4791
4792 if (shinfo->gso_size != GSO_BY_FRAGS)
4793 return hlen <= mtu;
4794
4795 /* Undo this so we can re-use header sizes */
4796 hlen -= GSO_BY_FRAGS;
4797
4798 skb_walk_frags(skb, iter) {
4799 if (hlen + skb_headlen(iter) > mtu)
4800 return false;
4801 }
4802
4803 return true;
4804}
4805EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4806
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004807static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4808{
4809 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4810 kfree_skb(skb);
4811 return NULL;
4812 }
4813
Vlad Yasevichf6548612015-12-14 17:44:10 -05004814 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004815 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004816 skb->mac_header += VLAN_HLEN;
4817 return skb;
4818}
4819
4820struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4821{
4822 struct vlan_hdr *vhdr;
4823 u16 vlan_tci;
4824
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004825 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004826 /* vlan_tci is already set-up so leave this for another time */
4827 return skb;
4828 }
4829
4830 skb = skb_share_check(skb, GFP_ATOMIC);
4831 if (unlikely(!skb))
4832 goto err_free;
4833
4834 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4835 goto err_free;
4836
4837 vhdr = (struct vlan_hdr *)skb->data;
4838 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4839 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4840
4841 skb_pull_rcsum(skb, VLAN_HLEN);
4842 vlan_set_encap_proto(skb, vhdr);
4843
4844 skb = skb_reorder_vlan_header(skb);
4845 if (unlikely(!skb))
4846 goto err_free;
4847
4848 skb_reset_network_header(skb);
4849 skb_reset_transport_header(skb);
4850 skb_reset_mac_len(skb);
4851
4852 return skb;
4853
4854err_free:
4855 kfree_skb(skb);
4856 return NULL;
4857}
4858EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004859
Jiri Pirkoe2195122014-11-19 14:05:01 +01004860int skb_ensure_writable(struct sk_buff *skb, int write_len)
4861{
4862 if (!pskb_may_pull(skb, write_len))
4863 return -ENOMEM;
4864
4865 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4866 return 0;
4867
4868 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4869}
4870EXPORT_SYMBOL(skb_ensure_writable);
4871
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004872/* remove VLAN header from packet and update csum accordingly.
4873 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4874 */
4875int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004876{
4877 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004878 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004879 int err;
4880
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004881 if (WARN_ONCE(offset,
4882 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4883 offset)) {
4884 return -EINVAL;
4885 }
4886
Jiri Pirko93515d52014-11-19 14:05:02 +01004887 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4888 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004889 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004890
4891 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4892
4893 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4894 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4895
4896 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4897 __skb_pull(skb, VLAN_HLEN);
4898
4899 vlan_set_encap_proto(skb, vhdr);
4900 skb->mac_header += VLAN_HLEN;
4901
4902 if (skb_network_offset(skb) < ETH_HLEN)
4903 skb_set_network_header(skb, ETH_HLEN);
4904
4905 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004906
4907 return err;
4908}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004909EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004910
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004911/* Pop a vlan tag either from hwaccel or from payload.
4912 * Expects skb->data at mac header.
4913 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004914int skb_vlan_pop(struct sk_buff *skb)
4915{
4916 u16 vlan_tci;
4917 __be16 vlan_proto;
4918 int err;
4919
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004920 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004921 skb->vlan_tci = 0;
4922 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004923 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004924 return 0;
4925
4926 err = __skb_vlan_pop(skb, &vlan_tci);
4927 if (err)
4928 return err;
4929 }
4930 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004931 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004932 return 0;
4933
4934 vlan_proto = skb->protocol;
4935 err = __skb_vlan_pop(skb, &vlan_tci);
4936 if (unlikely(err))
4937 return err;
4938
4939 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4940 return 0;
4941}
4942EXPORT_SYMBOL(skb_vlan_pop);
4943
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004944/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4945 * Expects skb->data at mac header.
4946 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004947int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4948{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004949 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004950 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004951 int err;
4952
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004953 if (WARN_ONCE(offset,
4954 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4955 offset)) {
4956 return -EINVAL;
4957 }
4958
Jiri Pirko93515d52014-11-19 14:05:02 +01004959 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004960 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004961 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004962 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004963
Jiri Pirko93515d52014-11-19 14:05:02 +01004964 skb->protocol = skb->vlan_proto;
4965 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004966
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004967 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004968 }
4969 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4970 return 0;
4971}
4972EXPORT_SYMBOL(skb_vlan_push);
4973
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004974/**
4975 * alloc_skb_with_frags - allocate skb with page frags
4976 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004977 * @header_len: size of linear part
4978 * @data_len: needed length in frags
4979 * @max_page_order: max page order desired.
4980 * @errcode: pointer to error code if any
4981 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004982 *
4983 * This can be used to allocate a paged skb, given a maximal order for frags.
4984 */
4985struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4986 unsigned long data_len,
4987 int max_page_order,
4988 int *errcode,
4989 gfp_t gfp_mask)
4990{
4991 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4992 unsigned long chunk;
4993 struct sk_buff *skb;
4994 struct page *page;
4995 gfp_t gfp_head;
4996 int i;
4997
4998 *errcode = -EMSGSIZE;
4999 /* Note this test could be relaxed, if we succeed to allocate
5000 * high order pages...
5001 */
5002 if (npages > MAX_SKB_FRAGS)
5003 return NULL;
5004
5005 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08005006 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07005007 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005008
5009 *errcode = -ENOBUFS;
5010 skb = alloc_skb(header_len, gfp_head);
5011 if (!skb)
5012 return NULL;
5013
5014 skb->truesize += npages << PAGE_SHIFT;
5015
5016 for (i = 0; npages > 0; i++) {
5017 int order = max_page_order;
5018
5019 while (order) {
5020 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005021 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005022 __GFP_COMP |
5023 __GFP_NOWARN |
5024 __GFP_NORETRY,
5025 order);
5026 if (page)
5027 goto fill_page;
5028 /* Do not retry other high order allocations */
5029 order = 1;
5030 max_page_order = 0;
5031 }
5032 order--;
5033 }
5034 page = alloc_page(gfp_mask);
5035 if (!page)
5036 goto failure;
5037fill_page:
5038 chunk = min_t(unsigned long, data_len,
5039 PAGE_SIZE << order);
5040 skb_fill_page_desc(skb, i, page, 0, chunk);
5041 data_len -= chunk;
5042 npages -= 1 << order;
5043 }
5044 return skb;
5045
5046failure:
5047 kfree_skb(skb);
5048 return NULL;
5049}
5050EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005051
5052/* carve out the first off bytes from skb when off < headlen */
5053static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5054 const int headlen, gfp_t gfp_mask)
5055{
5056 int i;
5057 int size = skb_end_offset(skb);
5058 int new_hlen = headlen - off;
5059 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005060
5061 size = SKB_DATA_ALIGN(size);
5062
5063 if (skb_pfmemalloc(skb))
5064 gfp_mask |= __GFP_MEMALLOC;
5065 data = kmalloc_reserve(size +
5066 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5067 gfp_mask, NUMA_NO_NODE, NULL);
5068 if (!data)
5069 return -ENOMEM;
5070
5071 size = SKB_WITH_OVERHEAD(ksize(data));
5072
5073 /* Copy real data, and all frags */
5074 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5075 skb->len -= off;
5076
5077 memcpy((struct skb_shared_info *)(data + size),
5078 skb_shinfo(skb),
5079 offsetof(struct skb_shared_info,
5080 frags[skb_shinfo(skb)->nr_frags]));
5081 if (skb_cloned(skb)) {
5082 /* drop the old head gracefully */
5083 if (skb_orphan_frags(skb, gfp_mask)) {
5084 kfree(data);
5085 return -ENOMEM;
5086 }
5087 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5088 skb_frag_ref(skb, i);
5089 if (skb_has_frag_list(skb))
5090 skb_clone_fraglist(skb);
5091 skb_release_data(skb);
5092 } else {
5093 /* we can reuse existing recount- all we did was
5094 * relocate values
5095 */
5096 skb_free_head(skb);
5097 }
5098
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005099 skb->head = data;
5100 skb->data = data;
5101 skb->head_frag = 0;
5102#ifdef NET_SKBUFF_DATA_USES_OFFSET
5103 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005104#else
5105 skb->end = skb->head + size;
5106#endif
5107 skb_set_tail_pointer(skb, skb_headlen(skb));
5108 skb_headers_offset_update(skb, 0);
5109 skb->cloned = 0;
5110 skb->hdr_len = 0;
5111 skb->nohdr = 0;
5112 atomic_set(&skb_shinfo(skb)->dataref, 1);
5113
5114 return 0;
5115}
5116
5117static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5118
5119/* carve out the first eat bytes from skb's frag_list. May recurse into
5120 * pskb_carve()
5121 */
5122static int pskb_carve_frag_list(struct sk_buff *skb,
5123 struct skb_shared_info *shinfo, int eat,
5124 gfp_t gfp_mask)
5125{
5126 struct sk_buff *list = shinfo->frag_list;
5127 struct sk_buff *clone = NULL;
5128 struct sk_buff *insp = NULL;
5129
5130 do {
5131 if (!list) {
5132 pr_err("Not enough bytes to eat. Want %d\n", eat);
5133 return -EFAULT;
5134 }
5135 if (list->len <= eat) {
5136 /* Eaten as whole. */
5137 eat -= list->len;
5138 list = list->next;
5139 insp = list;
5140 } else {
5141 /* Eaten partially. */
5142 if (skb_shared(list)) {
5143 clone = skb_clone(list, gfp_mask);
5144 if (!clone)
5145 return -ENOMEM;
5146 insp = list->next;
5147 list = clone;
5148 } else {
5149 /* This may be pulled without problems. */
5150 insp = list;
5151 }
5152 if (pskb_carve(list, eat, gfp_mask) < 0) {
5153 kfree_skb(clone);
5154 return -ENOMEM;
5155 }
5156 break;
5157 }
5158 } while (eat);
5159
5160 /* Free pulled out fragments. */
5161 while ((list = shinfo->frag_list) != insp) {
5162 shinfo->frag_list = list->next;
5163 kfree_skb(list);
5164 }
5165 /* And insert new clone at head. */
5166 if (clone) {
5167 clone->next = list;
5168 shinfo->frag_list = clone;
5169 }
5170 return 0;
5171}
5172
5173/* carve off first len bytes from skb. Split line (off) is in the
5174 * non-linear part of skb
5175 */
5176static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5177 int pos, gfp_t gfp_mask)
5178{
5179 int i, k = 0;
5180 int size = skb_end_offset(skb);
5181 u8 *data;
5182 const int nfrags = skb_shinfo(skb)->nr_frags;
5183 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005184
5185 size = SKB_DATA_ALIGN(size);
5186
5187 if (skb_pfmemalloc(skb))
5188 gfp_mask |= __GFP_MEMALLOC;
5189 data = kmalloc_reserve(size +
5190 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5191 gfp_mask, NUMA_NO_NODE, NULL);
5192 if (!data)
5193 return -ENOMEM;
5194
5195 size = SKB_WITH_OVERHEAD(ksize(data));
5196
5197 memcpy((struct skb_shared_info *)(data + size),
5198 skb_shinfo(skb), offsetof(struct skb_shared_info,
5199 frags[skb_shinfo(skb)->nr_frags]));
5200 if (skb_orphan_frags(skb, gfp_mask)) {
5201 kfree(data);
5202 return -ENOMEM;
5203 }
5204 shinfo = (struct skb_shared_info *)(data + size);
5205 for (i = 0; i < nfrags; i++) {
5206 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5207
5208 if (pos + fsize > off) {
5209 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5210
5211 if (pos < off) {
5212 /* Split frag.
5213 * We have two variants in this case:
5214 * 1. Move all the frag to the second
5215 * part, if it is possible. F.e.
5216 * this approach is mandatory for TUX,
5217 * where splitting is expensive.
5218 * 2. Split is accurately. We make this.
5219 */
5220 shinfo->frags[0].page_offset += off - pos;
5221 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5222 }
5223 skb_frag_ref(skb, i);
5224 k++;
5225 }
5226 pos += fsize;
5227 }
5228 shinfo->nr_frags = k;
5229 if (skb_has_frag_list(skb))
5230 skb_clone_fraglist(skb);
5231
5232 if (k == 0) {
5233 /* split line is in frag list */
5234 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5235 }
5236 skb_release_data(skb);
5237
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005238 skb->head = data;
5239 skb->head_frag = 0;
5240 skb->data = data;
5241#ifdef NET_SKBUFF_DATA_USES_OFFSET
5242 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005243#else
5244 skb->end = skb->head + size;
5245#endif
5246 skb_reset_tail_pointer(skb);
5247 skb_headers_offset_update(skb, 0);
5248 skb->cloned = 0;
5249 skb->hdr_len = 0;
5250 skb->nohdr = 0;
5251 skb->len -= off;
5252 skb->data_len = skb->len;
5253 atomic_set(&skb_shinfo(skb)->dataref, 1);
5254 return 0;
5255}
5256
5257/* remove len bytes from the beginning of the skb */
5258static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5259{
5260 int headlen = skb_headlen(skb);
5261
5262 if (len < headlen)
5263 return pskb_carve_inside_header(skb, len, headlen, gfp);
5264 else
5265 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5266}
5267
5268/* Extract to_copy bytes starting at off from skb, and return this in
5269 * a new skb
5270 */
5271struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5272 int to_copy, gfp_t gfp)
5273{
5274 struct sk_buff *clone = skb_clone(skb, gfp);
5275
5276 if (!clone)
5277 return NULL;
5278
5279 if (pskb_carve(clone, off, gfp) < 0 ||
5280 pskb_trim(clone, to_copy)) {
5281 kfree_skb(clone);
5282 return NULL;
5283 }
5284 return clone;
5285}
5286EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005287
5288/**
5289 * skb_condense - try to get rid of fragments/frag_list if possible
5290 * @skb: buffer
5291 *
5292 * Can be used to save memory before skb is added to a busy queue.
5293 * If packet has bytes in frags and enough tail room in skb->head,
5294 * pull all of them, so that we can free the frags right now and adjust
5295 * truesize.
5296 * Notes:
5297 * We do not reallocate skb->head thus can not fail.
5298 * Caller must re-evaluate skb->truesize if needed.
5299 */
5300void skb_condense(struct sk_buff *skb)
5301{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005302 if (skb->data_len) {
5303 if (skb->data_len > skb->end - skb->tail ||
5304 skb_cloned(skb))
5305 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005306
Eric Dumazet3174fed2016-12-09 08:02:05 -08005307 /* Nice, we can free page frag(s) right now */
5308 __pskb_pull_tail(skb, skb->data_len);
5309 }
5310 /* At this point, skb->truesize might be over estimated,
5311 * because skb had a fragment, and fragments do not tell
5312 * their truesize.
5313 * When we pulled its content into skb->head, fragment
5314 * was freed, but __pskb_pull_tail() could not possibly
5315 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005316 */
5317 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5318}