blob: 29e34bc6a17ce913bdd0315258b3c1836b995386 [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 Ry5f74f82e2016-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 Dumazet6ffe75eb2014-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 /*
571 * If skb buf is from userspace, we need to notify the caller
572 * the lower device DMA has done;
573 */
574 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
575 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Eric Dumazetff04a772014-09-23 18:39:30 -0700577 uarg = shinfo->destructor_arg;
578 if (uarg->callback)
579 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700581
582 if (shinfo->frag_list)
583 kfree_skb_list(shinfo->frag_list);
584
585 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
589 * Free an skbuff by memory without cleaning the state.
590 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800591static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700593 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700594
David S. Millerd179cd12005-08-17 14:57:30 -0700595 switch (skb->fclone) {
596 case SKB_FCLONE_UNAVAILABLE:
597 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800598 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700599
600 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700601 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800602
603 /* We usually free the clone (TX completion) before original skb
604 * This test would have no chance to be true for the clone,
605 * while here, branch prediction will be good.
606 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300607 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800608 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700609 break;
610
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800611 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700612 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700613 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700614 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300615 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800616 return;
617fastpath:
618 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Paolo Abeni0a463c72017-06-12 11:23:42 +0200621void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Eric Dumazetadf30902009-06-02 05:19:30 +0000623 skb_dst_drop(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200624 secpath_reset(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700625 if (skb->destructor) {
626 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 skb->destructor(skb);
628 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000629#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100630 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100631#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200632#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nf_bridge_put(skb->nf_bridge);
634#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700635}
636
637/* Free everything but the sk_buff shell. */
638static void skb_release_all(struct sk_buff *skb)
639{
640 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200641 if (likely(skb->head))
642 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Herbert Xu2d4baff2007-11-26 23:11:19 +0800645/**
646 * __kfree_skb - private function
647 * @skb: buffer
648 *
649 * Free an sk_buff. Release anything attached to the buffer.
650 * Clean the state. This is an internal helper function. Users should
651 * always call kfree_skb
652 */
653
654void __kfree_skb(struct sk_buff *skb)
655{
656 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 kfree_skbmem(skb);
658}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800659EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800662 * kfree_skb - free an sk_buff
663 * @skb: buffer to free
664 *
665 * Drop a reference to the buffer and free it if the usage count has
666 * hit zero.
667 */
668void kfree_skb(struct sk_buff *skb)
669{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200670 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800671 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200672
Neil Hormanead2ceb2009-03-11 09:49:55 +0000673 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800674 __kfree_skb(skb);
675}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800676EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800677
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700678void kfree_skb_list(struct sk_buff *segs)
679{
680 while (segs) {
681 struct sk_buff *next = segs->next;
682
683 kfree_skb(segs);
684 segs = next;
685 }
686}
687EXPORT_SYMBOL(kfree_skb_list);
688
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700689/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000690 * skb_tx_error - report an sk_buff xmit error
691 * @skb: buffer that triggered an error
692 *
693 * Report xmit error if a device callback is tracking this skb.
694 * skb must be freed afterwards.
695 */
696void skb_tx_error(struct sk_buff *skb)
697{
698 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
699 struct ubuf_info *uarg;
700
701 uarg = skb_shinfo(skb)->destructor_arg;
702 if (uarg->callback)
703 uarg->callback(uarg, false);
704 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
705 }
706}
707EXPORT_SYMBOL(skb_tx_error);
708
709/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000710 * consume_skb - free an skbuff
711 * @skb: buffer to free
712 *
713 * Drop a ref to the buffer and free it if the usage count has hit zero
714 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
715 * is being dropped after a failure and notes that
716 */
717void consume_skb(struct sk_buff *skb)
718{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200719 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000720 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200721
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900722 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000723 __kfree_skb(skb);
724}
725EXPORT_SYMBOL(consume_skb);
726
Paolo Abeni0a463c72017-06-12 11:23:42 +0200727/**
728 * consume_stateless_skb - free an skbuff, assuming it is stateless
729 * @skb: buffer to free
730 *
731 * Works like consume_skb(), but this variant assumes that all the head
732 * states have been already dropped.
733 */
734void consume_stateless_skb(struct sk_buff *skb)
735{
736 if (!skb_unref(skb))
737 return;
738
739 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200740 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200741 kfree_skbmem(skb);
742}
743
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100744void __kfree_skb_flush(void)
745{
746 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
747
748 /* flush skb_cache if containing objects */
749 if (nc->skb_count) {
750 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
751 nc->skb_cache);
752 nc->skb_count = 0;
753 }
754}
755
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100756static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100757{
758 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
759
760 /* drop skb->head and call any destructors for packet */
761 skb_release_all(skb);
762
763 /* record skb to CPU local list */
764 nc->skb_cache[nc->skb_count++] = skb;
765
766#ifdef CONFIG_SLUB
767 /* SLUB writes into objects when freeing */
768 prefetchw(skb);
769#endif
770
771 /* flush skb_cache if it is filled */
772 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
773 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
774 nc->skb_cache);
775 nc->skb_count = 0;
776 }
777}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100778void __kfree_skb_defer(struct sk_buff *skb)
779{
780 _kfree_skb_defer(skb);
781}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100782
783void napi_consume_skb(struct sk_buff *skb, int budget)
784{
785 if (unlikely(!skb))
786 return;
787
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100788 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100789 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100790 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100791 return;
792 }
793
Paolo Abeni76088942017-06-14 11:48:48 +0200794 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100795 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200796
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100797 /* if reaching here SKB is ready to free */
798 trace_consume_skb(skb);
799
800 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700801 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100802 __kfree_skb(skb);
803 return;
804 }
805
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100806 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100807}
808EXPORT_SYMBOL(napi_consume_skb);
809
Eric Dumazetb1937222014-09-28 22:18:47 -0700810/* Make sure a field is enclosed inside headers_start/headers_end section */
811#define CHECK_SKB_FIELD(field) \
812 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
813 offsetof(struct sk_buff, headers_start)); \
814 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
815 offsetof(struct sk_buff, headers_end)); \
816
Herbert Xudec18812007-10-14 00:37:30 -0700817static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
818{
819 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700820 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700821 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700822 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000823 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700824#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700825 new->sp = secpath_get(old->sp);
826#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700827 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700828
Eric Dumazetb1937222014-09-28 22:18:47 -0700829 /* Note : this field could be in headers_start/headers_end section
830 * It is not yet because we do not want to have a 16 bit hole
831 */
832 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300833
Eric Dumazetb1937222014-09-28 22:18:47 -0700834 memcpy(&new->headers_start, &old->headers_start,
835 offsetof(struct sk_buff, headers_end) -
836 offsetof(struct sk_buff, headers_start));
837 CHECK_SKB_FIELD(protocol);
838 CHECK_SKB_FIELD(csum);
839 CHECK_SKB_FIELD(hash);
840 CHECK_SKB_FIELD(priority);
841 CHECK_SKB_FIELD(skb_iif);
842 CHECK_SKB_FIELD(vlan_proto);
843 CHECK_SKB_FIELD(vlan_tci);
844 CHECK_SKB_FIELD(transport_header);
845 CHECK_SKB_FIELD(network_header);
846 CHECK_SKB_FIELD(mac_header);
847 CHECK_SKB_FIELD(inner_protocol);
848 CHECK_SKB_FIELD(inner_transport_header);
849 CHECK_SKB_FIELD(inner_network_header);
850 CHECK_SKB_FIELD(inner_mac_header);
851 CHECK_SKB_FIELD(mark);
852#ifdef CONFIG_NETWORK_SECMARK
853 CHECK_SKB_FIELD(secmark);
854#endif
Cong Wange0d10952013-08-01 11:10:25 +0800855#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700856 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300857#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800858#ifdef CONFIG_XPS
859 CHECK_SKB_FIELD(sender_cpu);
860#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700861#ifdef CONFIG_NET_SCHED
862 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700863#endif
864
Herbert Xudec18812007-10-14 00:37:30 -0700865}
866
Herbert Xu82c49a32009-05-22 22:11:37 +0000867/*
868 * You should not add any new code to this function. Add it to
869 * __copy_skb_header above instead.
870 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700871static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#define C(x) n->x = skb->x
874
875 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700877 __copy_skb_header(n, skb);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 C(len);
880 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700881 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700882 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800883 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 C(tail);
887 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800888 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000889 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800890 C(data);
891 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300892 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 atomic_inc(&(skb_shinfo(skb)->dataref));
895 skb->cloned = 1;
896
897 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700898#undef C
899}
900
901/**
902 * skb_morph - morph one skb into another
903 * @dst: the skb to receive the contents
904 * @src: the skb to supply the contents
905 *
906 * This is identical to skb_clone except that the target skb is
907 * supplied by the user.
908 *
909 * The target skb is returned upon exit.
910 */
911struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
912{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800913 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700914 return __skb_clone(dst, src);
915}
916EXPORT_SYMBOL_GPL(skb_morph);
917
Willem de Bruijn52267792017-08-03 16:29:39 -0400918struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
919{
920 struct ubuf_info *uarg;
921 struct sk_buff *skb;
922
923 WARN_ON_ONCE(!in_task());
924
Willem de Bruijn76851d12017-08-03 16:29:40 -0400925 if (!sock_flag(sk, SOCK_ZEROCOPY))
926 return NULL;
927
Willem de Bruijn52267792017-08-03 16:29:39 -0400928 skb = sock_omalloc(sk, 0, GFP_KERNEL);
929 if (!skb)
930 return NULL;
931
932 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
933 uarg = (void *)skb->cb;
934
935 uarg->callback = sock_zerocopy_callback;
936 uarg->desc = atomic_inc_return(&sk->sk_zckey) - 1;
937 uarg->zerocopy = 1;
938 atomic_set(&uarg->refcnt, 0);
939 sock_hold(sk);
940
941 return uarg;
942}
943EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
944
945static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
946{
947 return container_of((void *)uarg, struct sk_buff, cb);
948}
949
950void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
951{
952 struct sk_buff *skb = skb_from_uarg(uarg);
953 struct sock_exterr_skb *serr;
954 struct sock *sk = skb->sk;
955 u16 id = uarg->desc;
956
957 if (sock_flag(sk, SOCK_DEAD))
958 goto release;
959
960 serr = SKB_EXT_ERR(skb);
961 memset(serr, 0, sizeof(*serr));
962 serr->ee.ee_errno = 0;
963 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
964 serr->ee.ee_data = id;
965 if (!success)
966 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
967
968 skb_queue_tail(&sk->sk_error_queue, skb);
969 skb = NULL;
970
971 sk->sk_error_report(sk);
972
973release:
974 consume_skb(skb);
975 sock_put(sk);
976}
977EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
978
979void sock_zerocopy_put(struct ubuf_info *uarg)
980{
981 if (uarg && atomic_dec_and_test(&uarg->refcnt)) {
982 if (uarg->callback)
983 uarg->callback(uarg, uarg->zerocopy);
984 else
985 consume_skb(skb_from_uarg(uarg));
986 }
987}
988EXPORT_SYMBOL_GPL(sock_zerocopy_put);
989
990void sock_zerocopy_put_abort(struct ubuf_info *uarg)
991{
992 if (uarg) {
993 struct sock *sk = skb_from_uarg(uarg)->sk;
994
995 atomic_dec(&sk->sk_zckey);
996
997 /* sock_zerocopy_put expects a ref. Most sockets take one per
998 * skb, which is zero on abort. tcp_sendmsg holds one extra, to
999 * avoid an skb send inside the main loop triggering uarg free.
1000 */
1001 if (sk->sk_type != SOCK_STREAM)
1002 atomic_inc(&uarg->refcnt);
1003
1004 sock_zerocopy_put(uarg);
1005 }
1006}
1007EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1008
1009extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1010 struct iov_iter *from, size_t length);
1011
1012int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1013 struct msghdr *msg, int len,
1014 struct ubuf_info *uarg)
1015{
1016 struct iov_iter orig_iter = msg->msg_iter;
1017 int err, orig_len = skb->len;
1018
1019 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1020 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1021 /* Streams do not free skb on error. Reset to prev state. */
1022 msg->msg_iter = orig_iter;
1023 ___pskb_trim(skb, orig_len);
1024 return err;
1025 }
1026
1027 skb_zcopy_set(skb, uarg);
1028 return skb->len - orig_len;
1029}
1030EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1031
1032/* unused only until next patch in the series; will remove attribute */
1033static int __attribute__((unused))
1034 skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1035 gfp_t gfp_mask)
1036{
1037 if (skb_zcopy(orig)) {
1038 if (skb_zcopy(nskb)) {
1039 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1040 if (!gfp_mask) {
1041 WARN_ON_ONCE(1);
1042 return -ENOMEM;
1043 }
1044 if (skb_uarg(nskb) == skb_uarg(orig))
1045 return 0;
1046 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1047 return -EIO;
1048 }
1049 skb_zcopy_set(nskb, skb_uarg(orig));
1050 }
1051 return 0;
1052}
1053
Ben Hutchings2c530402012-07-10 10:55:09 +00001054/**
1055 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001056 * @skb: the skb to modify
1057 * @gfp_mask: allocation priority
1058 *
1059 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1060 * It will copy all frags into kernel and drop the reference
1061 * to userspace pages.
1062 *
1063 * If this function is called from an interrupt gfp_mask() must be
1064 * %GFP_ATOMIC.
1065 *
1066 * Returns 0 on success or a negative error code on failure
1067 * to allocate kernel memory to copy to.
1068 */
1069int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001070{
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001071 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
Shirley Maa6686f22011-07-06 12:22:12 +00001072 int num_frags = skb_shinfo(skb)->nr_frags;
1073 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001074 int i, new_frags;
1075 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001076
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001077 if (!num_frags)
1078 return 0;
Shirley Maa6686f22011-07-06 12:22:12 +00001079
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001080 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1081 return -EINVAL;
1082
1083 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1084 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001085 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001086 if (!page) {
1087 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001088 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001089 put_page(head);
1090 head = next;
1091 }
1092 return -ENOMEM;
1093 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001094 set_page_private(page, (unsigned long)head);
1095 head = page;
1096 }
1097
1098 page = head;
1099 d_off = 0;
1100 for (i = 0; i < num_frags; i++) {
1101 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1102 u32 p_off, p_len, copied;
1103 struct page *p;
1104 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001105
1106 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1107 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001108 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001109 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001110
1111 while (done < p_len) {
1112 if (d_off == PAGE_SIZE) {
1113 d_off = 0;
1114 page = (struct page *)page_private(page);
1115 }
1116 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1117 memcpy(page_address(page) + d_off,
1118 vaddr + p_off + done, copy);
1119 done += copy;
1120 d_off += copy;
1121 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001122 kunmap_atomic(vaddr);
1123 }
Shirley Maa6686f22011-07-06 12:22:12 +00001124 }
1125
1126 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001127 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001128 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001129
Michael S. Tsirkine19d6762012-11-01 09:16:22 +00001130 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001131
1132 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001133 for (i = 0; i < new_frags - 1; i++) {
1134 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001135 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001136 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001137 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1138 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001139
1140 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +00001141 return 0;
1142}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001143EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001144
Herbert Xue0053ec2007-10-14 00:37:52 -07001145/**
1146 * skb_clone - duplicate an sk_buff
1147 * @skb: buffer to clone
1148 * @gfp_mask: allocation priority
1149 *
1150 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1151 * copies share the same packet data but not structure. The new
1152 * buffer has a reference count of 1. If the allocation fails the
1153 * function returns %NULL otherwise the new buffer is returned.
1154 *
1155 * If this function is called from an interrupt gfp_mask() must be
1156 * %GFP_ATOMIC.
1157 */
1158
1159struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1160{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001161 struct sk_buff_fclones *fclones = container_of(skb,
1162 struct sk_buff_fclones,
1163 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001164 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001165
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001166 if (skb_orphan_frags(skb, gfp_mask))
1167 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001168
Herbert Xue0053ec2007-10-14 00:37:52 -07001169 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001170 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001171 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001172 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001173 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001174 if (skb_pfmemalloc(skb))
1175 gfp_mask |= __GFP_MEMALLOC;
1176
Herbert Xue0053ec2007-10-14 00:37:52 -07001177 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1178 if (!n)
1179 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001180
1181 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001182 n->fclone = SKB_FCLONE_UNAVAILABLE;
1183 }
1184
1185 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001187EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001189static void skb_headers_offset_update(struct sk_buff *skb, int off)
1190{
Eric Dumazet030737b2013-10-19 11:42:54 -07001191 /* Only adjust this if it actually is csum_start rather than csum */
1192 if (skb->ip_summed == CHECKSUM_PARTIAL)
1193 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001194 /* {transport,network,mac}_header and tail are relative to skb->head */
1195 skb->transport_header += off;
1196 skb->network_header += off;
1197 if (skb_mac_header_was_set(skb))
1198 skb->mac_header += off;
1199 skb->inner_transport_header += off;
1200 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001201 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1205{
Herbert Xudec18812007-10-14 00:37:30 -07001206 __copy_skb_header(new, old);
1207
Herbert Xu79671682006-06-22 02:40:14 -07001208 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1209 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1210 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Mel Gormanc93bdd02012-07-31 16:44:19 -07001213static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1214{
1215 if (skb_pfmemalloc(skb))
1216 return SKB_ALLOC_RX;
1217 return 0;
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/**
1221 * skb_copy - create private copy of an sk_buff
1222 * @skb: buffer to copy
1223 * @gfp_mask: allocation priority
1224 *
1225 * Make a copy of both an &sk_buff and its data. This is used when the
1226 * caller wishes to modify the data and needs a private copy of the
1227 * data to alter. Returns %NULL on failure or the pointer to the buffer
1228 * on success. The returned buffer has a reference count of 1.
1229 *
1230 * As by-product this function converts non-linear &sk_buff to linear
1231 * one, so that &sk_buff becomes completely private and caller is allowed
1232 * to modify all the data of returned buffer. This means that this
1233 * function is not recommended for use in circumstances when only
1234 * header is going to be modified. Use pskb_copy() instead.
1235 */
1236
Al Virodd0fc662005-10-07 07:46:04 +01001237struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001239 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001240 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001241 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1242 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!n)
1245 return NULL;
1246
1247 /* Set the data pointer */
1248 skb_reserve(n, headerlen);
1249 /* Set the tail pointer and length */
1250 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1253 BUG();
1254
1255 copy_skb_header(n, skb);
1256 return n;
1257}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001258EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001261 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001263 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001265 * @fclone: if true allocate the copy of the skb from the fclone
1266 * cache instead of the head cache; it is recommended to set this
1267 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 *
1269 * Make a copy of both an &sk_buff and part of its data, located
1270 * in header. Fragmented data remain shared. This is used when
1271 * the caller wishes to modify only header of &sk_buff and needs
1272 * private copy of the header to alter. Returns %NULL on failure
1273 * or the pointer to the buffer on success.
1274 * The returned buffer has a reference count of 1.
1275 */
1276
Octavian Purdilabad93e92014-06-12 01:36:26 +03001277struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1278 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Eric Dumazet117632e2011-12-03 21:39:53 +00001280 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001281 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1282 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!n)
1285 goto out;
1286
1287 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001288 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* Set the tail pointer and length */
1290 skb_put(n, skb_headlen(skb));
1291 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001292 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Herbert Xu25f484a2006-11-07 14:57:15 -08001294 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 n->data_len = skb->data_len;
1296 n->len = skb->len;
1297
1298 if (skb_shinfo(skb)->nr_frags) {
1299 int i;
1300
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001301 if (skb_orphan_frags(skb, gfp_mask)) {
1302 kfree_skb(n);
1303 n = NULL;
1304 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1307 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001308 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 skb_shinfo(n)->nr_frags = i;
1311 }
1312
David S. Miller21dc3302010-08-23 00:13:46 -07001313 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1315 skb_clone_fraglist(n);
1316 }
1317
1318 copy_skb_header(n, skb);
1319out:
1320 return n;
1321}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001322EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324/**
1325 * pskb_expand_head - reallocate header of &sk_buff
1326 * @skb: buffer to reallocate
1327 * @nhead: room to add at head
1328 * @ntail: room to add at tail
1329 * @gfp_mask: allocation priority
1330 *
Mathias Krausebc323832013-11-07 14:18:26 +01001331 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1332 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * reference count of 1. Returns zero in the case of success or error,
1334 * if expansion failed. In the last case, &sk_buff is not changed.
1335 *
1336 * All the pointers pointing into skb header may change and must be
1337 * reloaded after call to this function.
1338 */
1339
Victor Fusco86a76ca2005-07-08 14:57:47 -07001340int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001341 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Eric Dumazet158f3232017-01-27 07:11:27 -08001343 int i, osize = skb_end_offset(skb);
1344 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001346 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Herbert Xu4edd87a2008-10-01 07:09:38 -07001348 BUG_ON(nhead < 0);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (skb_shared(skb))
1351 BUG();
1352
1353 size = SKB_DATA_ALIGN(size);
1354
Mel Gormanc93bdd02012-07-31 16:44:19 -07001355 if (skb_pfmemalloc(skb))
1356 gfp_mask |= __GFP_MEMALLOC;
1357 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1358 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!data)
1360 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001361 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001364 * optimized for the cases when header is void.
1365 */
1366 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1367
1368 memcpy((struct skb_shared_info *)(data + size),
1369 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001370 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Alexander Duyck3e245912012-05-04 14:26:51 +00001372 /*
1373 * if shinfo is shared we must drop the old head gracefully, but if it
1374 * is not we can just drop the old head and let the existing refcount
1375 * be since all we did is relocate the values
1376 */
1377 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001378 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001379 if (skb_orphan_frags(skb, gfp_mask))
1380 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001381 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001382 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Eric Dumazet1fd63042010-09-02 23:09:32 +00001384 if (skb_has_frag_list(skb))
1385 skb_clone_fraglist(skb);
1386
1387 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001388 } else {
1389 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 off = (data + nhead) - skb->head;
1392
1393 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001394 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001396#ifdef NET_SKBUFF_DATA_USES_OFFSET
1397 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001398 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001399#else
1400 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001401#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001402 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001403 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001405 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 skb->nohdr = 0;
1407 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001408
1409 /* It is not generally safe to change skb->truesize.
1410 * For the moment, we really care of rx path, or
1411 * when skb is orphaned (not attached to a socket).
1412 */
1413 if (!skb->sk || skb->destructor == sock_edemux)
1414 skb->truesize += size - osize;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
1417
Shirley Maa6686f22011-07-06 12:22:12 +00001418nofrags:
1419 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420nodata:
1421 return -ENOMEM;
1422}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001423EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425/* Make private copy of skb with writable head and some headroom */
1426
1427struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1428{
1429 struct sk_buff *skb2;
1430 int delta = headroom - skb_headroom(skb);
1431
1432 if (delta <= 0)
1433 skb2 = pskb_copy(skb, GFP_ATOMIC);
1434 else {
1435 skb2 = skb_clone(skb, GFP_ATOMIC);
1436 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1437 GFP_ATOMIC)) {
1438 kfree_skb(skb2);
1439 skb2 = NULL;
1440 }
1441 }
1442 return skb2;
1443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001444EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/**
1447 * skb_copy_expand - copy and expand sk_buff
1448 * @skb: buffer to copy
1449 * @newheadroom: new free bytes at head
1450 * @newtailroom: new free bytes at tail
1451 * @gfp_mask: allocation priority
1452 *
1453 * Make a copy of both an &sk_buff and its data and while doing so
1454 * allocate additional space.
1455 *
1456 * This is used when the caller wishes to modify the data and needs a
1457 * private copy of the data to alter as well as more space for new fields.
1458 * Returns %NULL on failure or the pointer to the buffer
1459 * on success. The returned buffer has a reference count of 1.
1460 *
1461 * You must pass %GFP_ATOMIC as the allocation priority if this function
1462 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001465 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001466 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 /*
1469 * Allocate the copy buffer
1470 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001471 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1472 gfp_mask, skb_alloc_rx_flag(skb),
1473 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001474 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 int head_copy_len, head_copy_off;
1476
1477 if (!n)
1478 return NULL;
1479
1480 skb_reserve(n, newheadroom);
1481
1482 /* Set the tail pointer and length */
1483 skb_put(n, skb->len);
1484
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001485 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 head_copy_off = 0;
1487 if (newheadroom <= head_copy_len)
1488 head_copy_len = newheadroom;
1489 else
1490 head_copy_off = newheadroom - head_copy_len;
1491
1492 /* Copy the linear header and data. */
1493 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1494 skb->len + head_copy_len))
1495 BUG();
1496
1497 copy_skb_header(n, skb);
1498
Eric Dumazet030737b2013-10-19 11:42:54 -07001499 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return n;
1502}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001503EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505/**
1506 * skb_pad - zero pad the tail of an skb
1507 * @skb: buffer to pad
1508 * @pad: space to pad
1509 *
1510 * Ensure that a buffer is followed by a padding area that is zero
1511 * filled. Used by network drivers which may DMA or transfer data
1512 * beyond the buffer end onto the wire.
1513 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001514 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001516
Herbert Xu5b057c62006-06-23 02:06:41 -07001517int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Herbert Xu5b057c62006-06-23 02:06:41 -07001519 int err;
1520 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001523 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001527
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001528 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001529 if (likely(skb_cloned(skb) || ntail > 0)) {
1530 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1531 if (unlikely(err))
1532 goto free_skb;
1533 }
1534
1535 /* FIXME: The use of this function with non-linear skb's really needs
1536 * to be audited.
1537 */
1538 err = skb_linearize(skb);
1539 if (unlikely(err))
1540 goto free_skb;
1541
1542 memset(skb->data + skb->len, 0, pad);
1543 return 0;
1544
1545free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001547 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001548}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001549EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001550
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001551/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001552 * pskb_put - add data to the tail of a potentially fragmented buffer
1553 * @skb: start of the buffer to use
1554 * @tail: tail fragment of the buffer to use
1555 * @len: amount of data to add
1556 *
1557 * This function extends the used data area of the potentially
1558 * fragmented buffer. @tail must be the last fragment of @skb -- or
1559 * @skb itself. If this would exceed the total buffer size the kernel
1560 * will panic. A pointer to the first byte of the extra data is
1561 * returned.
1562 */
1563
Johannes Berg4df864c2017-06-16 14:29:21 +02001564void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001565{
1566 if (tail != skb) {
1567 skb->data_len += len;
1568 skb->len += len;
1569 }
1570 return skb_put(tail, len);
1571}
1572EXPORT_SYMBOL_GPL(pskb_put);
1573
1574/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001575 * skb_put - add data to 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. If this would
1580 * exceed the total buffer size the kernel will panic. A pointer to the
1581 * first byte of the extra data is returned.
1582 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001583void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001584{
Johannes Berg4df864c2017-06-16 14:29:21 +02001585 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001586 SKB_LINEAR_ASSERT(skb);
1587 skb->tail += len;
1588 skb->len += len;
1589 if (unlikely(skb->tail > skb->end))
1590 skb_over_panic(skb, len, __builtin_return_address(0));
1591 return tmp;
1592}
1593EXPORT_SYMBOL(skb_put);
1594
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001595/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001596 * skb_push - add data to the start of a buffer
1597 * @skb: buffer to use
1598 * @len: amount of data to add
1599 *
1600 * This function extends the used data area of the buffer at the buffer
1601 * start. If this would exceed the total buffer headroom the kernel will
1602 * panic. A pointer to the first byte of the extra data is returned.
1603 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001604void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001605{
1606 skb->data -= len;
1607 skb->len += len;
1608 if (unlikely(skb->data<skb->head))
1609 skb_under_panic(skb, len, __builtin_return_address(0));
1610 return skb->data;
1611}
1612EXPORT_SYMBOL(skb_push);
1613
1614/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001615 * skb_pull - remove data from the start of a buffer
1616 * @skb: buffer to use
1617 * @len: amount of data to remove
1618 *
1619 * This function removes data from the start of a buffer, returning
1620 * the memory to the headroom. A pointer to the next data in the buffer
1621 * is returned. Once the data has been pulled future pushes will overwrite
1622 * the old data.
1623 */
Johannes Bergaf728682017-06-16 14:29:22 +02001624void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001625{
David S. Miller47d29642010-05-02 02:21:44 -07001626 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001627}
1628EXPORT_SYMBOL(skb_pull);
1629
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001630/**
1631 * skb_trim - remove end from a buffer
1632 * @skb: buffer to alter
1633 * @len: new length
1634 *
1635 * Cut the length of a buffer down by removing data from the tail. If
1636 * the buffer is already under the length specified it is not modified.
1637 * The skb must be linear.
1638 */
1639void skb_trim(struct sk_buff *skb, unsigned int len)
1640{
1641 if (skb->len > len)
1642 __skb_trim(skb, len);
1643}
1644EXPORT_SYMBOL(skb_trim);
1645
Herbert Xu3cc0e872006-06-09 16:13:38 -07001646/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
1648
Herbert Xu3cc0e872006-06-09 16:13:38 -07001649int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Herbert Xu27b437c2006-07-13 19:26:39 -07001651 struct sk_buff **fragp;
1652 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int offset = skb_headlen(skb);
1654 int nfrags = skb_shinfo(skb)->nr_frags;
1655 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001656 int err;
1657
1658 if (skb_cloned(skb) &&
1659 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001662 i = 0;
1663 if (offset >= len)
1664 goto drop_pages;
1665
1666 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001667 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001668
1669 if (end < len) {
1670 offset = end;
1671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001673
Eric Dumazet9e903e02011-10-18 21:00:24 +00001674 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001675
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001676drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001677 skb_shinfo(skb)->nr_frags = i;
1678
1679 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001680 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001681
David S. Miller21dc3302010-08-23 00:13:46 -07001682 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001683 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001684 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
Herbert Xu27b437c2006-07-13 19:26:39 -07001687 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1688 fragp = &frag->next) {
1689 int end = offset + frag->len;
1690
1691 if (skb_shared(frag)) {
1692 struct sk_buff *nfrag;
1693
1694 nfrag = skb_clone(frag, GFP_ATOMIC);
1695 if (unlikely(!nfrag))
1696 return -ENOMEM;
1697
1698 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001699 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001700 frag = nfrag;
1701 *fragp = frag;
1702 }
1703
1704 if (end < len) {
1705 offset = end;
1706 continue;
1707 }
1708
1709 if (end > len &&
1710 unlikely((err = pskb_trim(frag, len - offset))))
1711 return err;
1712
1713 if (frag->next)
1714 skb_drop_list(&frag->next);
1715 break;
1716 }
1717
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001718done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001719 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 skb->data_len -= skb->len - len;
1721 skb->len = len;
1722 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001723 skb->len = len;
1724 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001725 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001728 if (!skb->sk || skb->destructor == sock_edemux)
1729 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
1731}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001732EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734/**
1735 * __pskb_pull_tail - advance tail of skb header
1736 * @skb: buffer to reallocate
1737 * @delta: number of bytes to advance tail
1738 *
1739 * The function makes a sense only on a fragmented &sk_buff,
1740 * it expands header moving its tail forward and copying necessary
1741 * data from fragmented part.
1742 *
1743 * &sk_buff MUST have reference count of 1.
1744 *
1745 * Returns %NULL (and &sk_buff does not change) if pull failed
1746 * or value of new tail of skb in the case of success.
1747 *
1748 * All the pointers pointing into skb header may change and must be
1749 * reloaded after call to this function.
1750 */
1751
1752/* Moves tail of skb head forward, copying data from fragmented part,
1753 * when it is necessary.
1754 * 1. It may fail due to malloc failure.
1755 * 2. It may change skb pointers.
1756 *
1757 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1758 */
Johannes Bergaf728682017-06-16 14:29:22 +02001759void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 /* If skb has not enough free space at tail, get new one
1762 * plus 128 bytes for future expansions. If we have enough
1763 * room at tail, reallocate without expansion only if skb is cloned.
1764 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001765 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (eat > 0 || skb_cloned(skb)) {
1768 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1769 GFP_ATOMIC))
1770 return NULL;
1771 }
1772
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001773 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 BUG();
1775
1776 /* Optimization: no fragments, no reasons to preestimate
1777 * size of pulled pages. Superb.
1778 */
David S. Miller21dc3302010-08-23 00:13:46 -07001779 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto pull_pages;
1781
1782 /* Estimate size of pulled pages. */
1783 eat = delta;
1784 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001785 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1786
1787 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001789 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
1792 /* If we need update frag list, we are in troubles.
1793 * Certainly, it possible to add an offset to skb data,
1794 * but taking into account that pulling is expected to
1795 * be very rare operation, it is worth to fight against
1796 * further bloating skb head and crucify ourselves here instead.
1797 * Pure masohism, indeed. 8)8)
1798 */
1799 if (eat) {
1800 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1801 struct sk_buff *clone = NULL;
1802 struct sk_buff *insp = NULL;
1803
1804 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001805 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 if (list->len <= eat) {
1808 /* Eaten as whole. */
1809 eat -= list->len;
1810 list = list->next;
1811 insp = list;
1812 } else {
1813 /* Eaten partially. */
1814
1815 if (skb_shared(list)) {
1816 /* Sucks! We need to fork list. :-( */
1817 clone = skb_clone(list, GFP_ATOMIC);
1818 if (!clone)
1819 return NULL;
1820 insp = list->next;
1821 list = clone;
1822 } else {
1823 /* This may be pulled without
1824 * problems. */
1825 insp = list;
1826 }
1827 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001828 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return NULL;
1830 }
1831 break;
1832 }
1833 } while (eat);
1834
1835 /* Free pulled out fragments. */
1836 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1837 skb_shinfo(skb)->frag_list = list->next;
1838 kfree_skb(list);
1839 }
1840 /* And insert new clone at head. */
1841 if (clone) {
1842 clone->next = list;
1843 skb_shinfo(skb)->frag_list = clone;
1844 }
1845 }
1846 /* Success! Now we may commit changes to skb data. */
1847
1848pull_pages:
1849 eat = delta;
1850 k = 0;
1851 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001852 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1853
1854 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001855 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001856 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 } else {
1858 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1859 if (eat) {
1860 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001861 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001862 if (!i)
1863 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 eat = 0;
1865 }
1866 k++;
1867 }
1868 }
1869 skb_shinfo(skb)->nr_frags = k;
1870
linzhang3ccc6c62017-07-17 17:25:02 +08001871end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 skb->tail += delta;
1873 skb->data_len -= delta;
1874
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001875 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001877EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric Dumazet22019b12011-07-29 18:37:31 +00001879/**
1880 * skb_copy_bits - copy bits from skb to kernel buffer
1881 * @skb: source skb
1882 * @offset: offset in source
1883 * @to: destination buffer
1884 * @len: number of bytes to copy
1885 *
1886 * Copy the specified number of bytes from the source skb to the
1887 * destination buffer.
1888 *
1889 * CAUTION ! :
1890 * If its prototype is ever changed,
1891 * check arch/{*}/net/{*}.S files,
1892 * since it is called from BPF assembly code.
1893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1895{
David S. Miller1a028e52007-04-27 15:21:23 -07001896 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001897 struct sk_buff *frag_iter;
1898 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (offset > (int)skb->len - len)
1901 goto fault;
1902
1903 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001904 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (copy > len)
1906 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001907 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if ((len -= copy) == 0)
1909 return 0;
1910 offset += copy;
1911 to += copy;
1912 }
1913
1914 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001915 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001916 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001918 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001919
Eric Dumazet51c56b02012-04-05 11:35:15 +02001920 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04001922 u32 p_off, p_len, copied;
1923 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 u8 *vaddr;
1925
1926 if (copy > len)
1927 copy = len;
1928
Willem de Bruijnc613c202017-07-31 08:15:47 -04001929 skb_frag_foreach_page(f,
1930 f->page_offset + offset - start,
1931 copy, p, p_off, p_len, copied) {
1932 vaddr = kmap_atomic(p);
1933 memcpy(to + copied, vaddr + p_off, p_len);
1934 kunmap_atomic(vaddr);
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if ((len -= copy) == 0)
1938 return 0;
1939 offset += copy;
1940 to += copy;
1941 }
David S. Miller1a028e52007-04-27 15:21:23 -07001942 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
David S. Millerfbb398a2009-06-09 00:18:59 -07001945 skb_walk_frags(skb, frag_iter) {
1946 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
David S. Millerfbb398a2009-06-09 00:18:59 -07001948 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
David S. Millerfbb398a2009-06-09 00:18:59 -07001950 end = start + frag_iter->len;
1951 if ((copy = end - offset) > 0) {
1952 if (copy > len)
1953 copy = len;
1954 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1955 goto fault;
1956 if ((len -= copy) == 0)
1957 return 0;
1958 offset += copy;
1959 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001961 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Shirley Maa6686f22011-07-06 12:22:12 +00001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (!len)
1965 return 0;
1966
1967fault:
1968 return -EFAULT;
1969}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001970EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Jens Axboe9c55e012007-11-06 23:30:13 -08001972/*
1973 * Callback from splice_to_pipe(), if we need to release some pages
1974 * at the end of the spd in case we error'ed out in filling the pipe.
1975 */
1976static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1977{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001978 put_page(spd->pages[i]);
1979}
Jens Axboe9c55e012007-11-06 23:30:13 -08001980
David S. Millera108d5f2012-04-23 23:06:11 -04001981static struct page *linear_to_page(struct page *page, unsigned int *len,
1982 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001983 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001984{
Eric Dumazet5640f762012-09-23 23:04:42 +00001985 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001986
Eric Dumazet5640f762012-09-23 23:04:42 +00001987 if (!sk_page_frag_refill(sk, pfrag))
1988 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001989
Eric Dumazet5640f762012-09-23 23:04:42 +00001990 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001991
Eric Dumazet5640f762012-09-23 23:04:42 +00001992 memcpy(page_address(pfrag->page) + pfrag->offset,
1993 page_address(page) + *offset, *len);
1994 *offset = pfrag->offset;
1995 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001996
Eric Dumazet5640f762012-09-23 23:04:42 +00001997 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001998}
1999
Eric Dumazet41c73a02012-04-22 12:26:16 +00002000static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2001 struct page *page,
2002 unsigned int offset)
2003{
2004 return spd->nr_pages &&
2005 spd->pages[spd->nr_pages - 1] == page &&
2006 (spd->partial[spd->nr_pages - 1].offset +
2007 spd->partial[spd->nr_pages - 1].len == offset);
2008}
2009
Jens Axboe9c55e012007-11-06 23:30:13 -08002010/*
2011 * Fill page/offset/length into spd, if it can hold more pages.
2012 */
David S. Millera108d5f2012-04-23 23:06:11 -04002013static bool spd_fill_page(struct splice_pipe_desc *spd,
2014 struct pipe_inode_info *pipe, struct page *page,
2015 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002016 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002017 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002018{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002019 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002020 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002021
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002022 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002023 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002024 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002025 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002026 }
2027 if (spd_can_coalesce(spd, page, offset)) {
2028 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002029 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002030 }
2031 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002032 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002033 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002034 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002035 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002036
David S. Millera108d5f2012-04-23 23:06:11 -04002037 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002038}
2039
David S. Millera108d5f2012-04-23 23:06:11 -04002040static bool __splice_segment(struct page *page, unsigned int poff,
2041 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002042 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002043 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002044 struct sock *sk,
2045 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002046{
2047 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002048 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002049
2050 /* skip this segment if already processed */
2051 if (*off >= plen) {
2052 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002053 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002054 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002055
Octavian Purdila2870c432008-07-15 00:49:11 -07002056 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002057 poff += *off;
2058 plen -= *off;
2059 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002060
Eric Dumazet18aafc62013-01-11 14:46:37 +00002061 do {
2062 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002063
Eric Dumazet18aafc62013-01-11 14:46:37 +00002064 if (spd_fill_page(spd, pipe, page, &flen, poff,
2065 linear, sk))
2066 return true;
2067 poff += flen;
2068 plen -= flen;
2069 *len -= flen;
2070 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002071
David S. Millera108d5f2012-04-23 23:06:11 -04002072 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002073}
2074
2075/*
David S. Millera108d5f2012-04-23 23:06:11 -04002076 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002077 * pipe is full or if we already spliced the requested length.
2078 */
David S. Millera108d5f2012-04-23 23:06:11 -04002079static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2080 unsigned int *offset, unsigned int *len,
2081 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002082{
2083 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002084 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002085
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002086 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002087 * If skb->head_frag is set, this 'linear' part is backed by a
2088 * fragment, and if the head is not shared with any clones then
2089 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002090 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002091 if (__splice_segment(virt_to_page(skb->data),
2092 (unsigned long) skb->data & (PAGE_SIZE - 1),
2093 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002094 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002095 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002096 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002097 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002098
2099 /*
2100 * then map the fragments
2101 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002102 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2103 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2104
Ian Campbellea2ab692011-08-22 23:44:58 +00002105 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002106 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002107 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002108 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002109 }
2110
Tom Herbertfa9835e2016-03-07 14:11:04 -08002111 skb_walk_frags(skb, iter) {
2112 if (*offset >= iter->len) {
2113 *offset -= iter->len;
2114 continue;
2115 }
2116 /* __skb_splice_bits() only fails if the output has no room
2117 * left, so no point in going over the frag_list for the error
2118 * case.
2119 */
2120 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2121 return true;
2122 }
2123
David S. Millera108d5f2012-04-23 23:06:11 -04002124 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002125}
2126
2127/*
2128 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002129 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002130 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002131int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002132 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002133 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002134{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002135 struct partial_page partial[MAX_SKB_FRAGS];
2136 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002137 struct splice_pipe_desc spd = {
2138 .pages = pages,
2139 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002140 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002141 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002142 .spd_release = sock_spd_release,
2143 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002144 int ret = 0;
2145
Tom Herbertfa9835e2016-03-07 14:11:04 -08002146 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002147
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002148 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002149 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002150
Jens Axboe35f3d142010-05-20 10:43:18 +02002151 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002152}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002153EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002154
Tom Herbert20bf50d2017-07-28 16:22:42 -07002155/* Send skb data on a socket. Socket must be locked. */
2156int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2157 int len)
2158{
2159 unsigned int orig_len = len;
2160 struct sk_buff *head = skb;
2161 unsigned short fragidx;
2162 int slen, ret;
2163
2164do_frag_list:
2165
2166 /* Deal with head data */
2167 while (offset < skb_headlen(skb) && len) {
2168 struct kvec kv;
2169 struct msghdr msg;
2170
2171 slen = min_t(int, len, skb_headlen(skb) - offset);
2172 kv.iov_base = skb->data + offset;
2173 kv.iov_len = len;
2174 memset(&msg, 0, sizeof(msg));
2175
2176 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2177 if (ret <= 0)
2178 goto error;
2179
2180 offset += ret;
2181 len -= ret;
2182 }
2183
2184 /* All the data was skb head? */
2185 if (!len)
2186 goto out;
2187
2188 /* Make offset relative to start of frags */
2189 offset -= skb_headlen(skb);
2190
2191 /* Find where we are in frag list */
2192 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2193 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2194
2195 if (offset < frag->size)
2196 break;
2197
2198 offset -= frag->size;
2199 }
2200
2201 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2202 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2203
2204 slen = min_t(size_t, len, frag->size - offset);
2205
2206 while (slen) {
2207 ret = kernel_sendpage_locked(sk, frag->page.p,
2208 frag->page_offset + offset,
2209 slen, MSG_DONTWAIT);
2210 if (ret <= 0)
2211 goto error;
2212
2213 len -= ret;
2214 offset += ret;
2215 slen -= ret;
2216 }
2217
2218 offset = 0;
2219 }
2220
2221 if (len) {
2222 /* Process any frag lists */
2223
2224 if (skb == head) {
2225 if (skb_has_frag_list(skb)) {
2226 skb = skb_shinfo(skb)->frag_list;
2227 goto do_frag_list;
2228 }
2229 } else if (skb->next) {
2230 skb = skb->next;
2231 goto do_frag_list;
2232 }
2233 }
2234
2235out:
2236 return orig_len - len;
2237
2238error:
2239 return orig_len == len ? ret : orig_len - len;
2240}
2241EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2242
2243/* Send skb data on a socket. */
2244int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2245{
2246 int ret = 0;
2247
2248 lock_sock(sk);
2249 ret = skb_send_sock_locked(sk, skb, offset, len);
2250 release_sock(sk);
2251
2252 return ret;
2253}
2254EXPORT_SYMBOL_GPL(skb_send_sock);
2255
Herbert Xu357b40a2005-04-19 22:30:14 -07002256/**
2257 * skb_store_bits - store bits from kernel buffer to skb
2258 * @skb: destination buffer
2259 * @offset: offset in destination
2260 * @from: source buffer
2261 * @len: number of bytes to copy
2262 *
2263 * Copy the specified number of bytes from the source buffer to the
2264 * destination skb. This function handles all the messy bits of
2265 * traversing fragment lists and such.
2266 */
2267
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002268int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002269{
David S. Miller1a028e52007-04-27 15:21:23 -07002270 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002271 struct sk_buff *frag_iter;
2272 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002273
2274 if (offset > (int)skb->len - len)
2275 goto fault;
2276
David S. Miller1a028e52007-04-27 15:21:23 -07002277 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002278 if (copy > len)
2279 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002280 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002281 if ((len -= copy) == 0)
2282 return 0;
2283 offset += copy;
2284 from += copy;
2285 }
2286
2287 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2288 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002289 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002290
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002291 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002292
Eric Dumazet9e903e02011-10-18 21:00:24 +00002293 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002294 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002295 u32 p_off, p_len, copied;
2296 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002297 u8 *vaddr;
2298
2299 if (copy > len)
2300 copy = len;
2301
Willem de Bruijnc613c202017-07-31 08:15:47 -04002302 skb_frag_foreach_page(frag,
2303 frag->page_offset + offset - start,
2304 copy, p, p_off, p_len, copied) {
2305 vaddr = kmap_atomic(p);
2306 memcpy(vaddr + p_off, from + copied, p_len);
2307 kunmap_atomic(vaddr);
2308 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002309
2310 if ((len -= copy) == 0)
2311 return 0;
2312 offset += copy;
2313 from += copy;
2314 }
David S. Miller1a028e52007-04-27 15:21:23 -07002315 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002316 }
2317
David S. Millerfbb398a2009-06-09 00:18:59 -07002318 skb_walk_frags(skb, frag_iter) {
2319 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002320
David S. Millerfbb398a2009-06-09 00:18:59 -07002321 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002322
David S. Millerfbb398a2009-06-09 00:18:59 -07002323 end = start + frag_iter->len;
2324 if ((copy = end - offset) > 0) {
2325 if (copy > len)
2326 copy = len;
2327 if (skb_store_bits(frag_iter, offset - start,
2328 from, copy))
2329 goto fault;
2330 if ((len -= copy) == 0)
2331 return 0;
2332 offset += copy;
2333 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002334 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002335 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002336 }
2337 if (!len)
2338 return 0;
2339
2340fault:
2341 return -EFAULT;
2342}
Herbert Xu357b40a2005-04-19 22:30:14 -07002343EXPORT_SYMBOL(skb_store_bits);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002346__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2347 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
David S. Miller1a028e52007-04-27 15:21:23 -07002349 int start = skb_headlen(skb);
2350 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002351 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 int pos = 0;
2353
2354 /* Checksum header. */
2355 if (copy > 0) {
2356 if (copy > len)
2357 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002358 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if ((len -= copy) == 0)
2360 return csum;
2361 offset += copy;
2362 pos = copy;
2363 }
2364
2365 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002366 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002367 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002369 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002370
Eric Dumazet51c56b02012-04-05 11:35:15 +02002371 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002373 u32 p_off, p_len, copied;
2374 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002375 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 if (copy > len)
2379 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002380
2381 skb_frag_foreach_page(frag,
2382 frag->page_offset + offset - start,
2383 copy, p, p_off, p_len, copied) {
2384 vaddr = kmap_atomic(p);
2385 csum2 = ops->update(vaddr + p_off, p_len, 0);
2386 kunmap_atomic(vaddr);
2387 csum = ops->combine(csum, csum2, pos, p_len);
2388 pos += p_len;
2389 }
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (!(len -= copy))
2392 return csum;
2393 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
David S. Miller1a028e52007-04-27 15:21:23 -07002395 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
David S. Millerfbb398a2009-06-09 00:18:59 -07002398 skb_walk_frags(skb, frag_iter) {
2399 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
David S. Millerfbb398a2009-06-09 00:18:59 -07002401 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
David S. Millerfbb398a2009-06-09 00:18:59 -07002403 end = start + frag_iter->len;
2404 if ((copy = end - offset) > 0) {
2405 __wsum csum2;
2406 if (copy > len)
2407 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002408 csum2 = __skb_checksum(frag_iter, offset - start,
2409 copy, 0, ops);
2410 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002411 if ((len -= copy) == 0)
2412 return csum;
2413 offset += copy;
2414 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002416 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002418 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 return csum;
2421}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002422EXPORT_SYMBOL(__skb_checksum);
2423
2424__wsum skb_checksum(const struct sk_buff *skb, int offset,
2425 int len, __wsum csum)
2426{
2427 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002428 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002429 .combine = csum_block_add_ext,
2430 };
2431
2432 return __skb_checksum(skb, offset, len, csum, &ops);
2433}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002434EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436/* Both of above in one bottle. */
2437
Al Viro81d77662006-11-14 21:37:33 -08002438__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2439 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
David S. Miller1a028e52007-04-27 15:21:23 -07002441 int start = skb_headlen(skb);
2442 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002443 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int pos = 0;
2445
2446 /* Copy header. */
2447 if (copy > 0) {
2448 if (copy > len)
2449 copy = len;
2450 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2451 copy, csum);
2452 if ((len -= copy) == 0)
2453 return csum;
2454 offset += copy;
2455 to += copy;
2456 pos = copy;
2457 }
2458
2459 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002460 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002462 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002463
Eric Dumazet9e903e02011-10-18 21:00:24 +00002464 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002466 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2467 u32 p_off, p_len, copied;
2468 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002469 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 if (copy > len)
2473 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002474
2475 skb_frag_foreach_page(frag,
2476 frag->page_offset + offset - start,
2477 copy, p, p_off, p_len, copied) {
2478 vaddr = kmap_atomic(p);
2479 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2480 to + copied,
2481 p_len, 0);
2482 kunmap_atomic(vaddr);
2483 csum = csum_block_add(csum, csum2, pos);
2484 pos += p_len;
2485 }
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (!(len -= copy))
2488 return csum;
2489 offset += copy;
2490 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
David S. Miller1a028e52007-04-27 15:21:23 -07002492 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
David S. Millerfbb398a2009-06-09 00:18:59 -07002495 skb_walk_frags(skb, frag_iter) {
2496 __wsum csum2;
2497 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
David S. Millerfbb398a2009-06-09 00:18:59 -07002499 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
David S. Millerfbb398a2009-06-09 00:18:59 -07002501 end = start + frag_iter->len;
2502 if ((copy = end - offset) > 0) {
2503 if (copy > len)
2504 copy = len;
2505 csum2 = skb_copy_and_csum_bits(frag_iter,
2506 offset - start,
2507 to, copy, 0);
2508 csum = csum_block_add(csum, csum2, pos);
2509 if ((len -= copy) == 0)
2510 return csum;
2511 offset += copy;
2512 to += copy;
2513 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002515 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002517 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return csum;
2519}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002520EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Davide Caratti96178132017-05-18 15:44:37 +02002522static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2523{
2524 net_warn_ratelimited(
2525 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2526 __func__);
2527 return 0;
2528}
2529
2530static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2531 int offset, int len)
2532{
2533 net_warn_ratelimited(
2534 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2535 __func__);
2536 return 0;
2537}
2538
2539static const struct skb_checksum_ops default_crc32c_ops = {
2540 .update = warn_crc32c_csum_update,
2541 .combine = warn_crc32c_csum_combine,
2542};
2543
2544const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2545 &default_crc32c_ops;
2546EXPORT_SYMBOL(crc32c_csum_stub);
2547
Thomas Grafaf2806f2013-12-13 15:22:17 +01002548 /**
2549 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2550 * @from: source buffer
2551 *
2552 * Calculates the amount of linear headroom needed in the 'to' skb passed
2553 * into skb_zerocopy().
2554 */
2555unsigned int
2556skb_zerocopy_headlen(const struct sk_buff *from)
2557{
2558 unsigned int hlen = 0;
2559
2560 if (!from->head_frag ||
2561 skb_headlen(from) < L1_CACHE_BYTES ||
2562 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2563 hlen = skb_headlen(from);
2564
2565 if (skb_has_frag_list(from))
2566 hlen = from->len;
2567
2568 return hlen;
2569}
2570EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2571
2572/**
2573 * skb_zerocopy - Zero copy skb to skb
2574 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002575 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002576 * @len: number of bytes to copy from source buffer
2577 * @hlen: size of linear headroom in destination buffer
2578 *
2579 * Copies up to `len` bytes from `from` to `to` by creating references
2580 * to the frags in the source buffer.
2581 *
2582 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2583 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002584 *
2585 * Return value:
2586 * 0: everything is OK
2587 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2588 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002589 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002590int
2591skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002592{
2593 int i, j = 0;
2594 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002595 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002596 struct page *page;
2597 unsigned int offset;
2598
2599 BUG_ON(!from->head_frag && !hlen);
2600
2601 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002602 if (len <= skb_tailroom(to))
2603 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002604
2605 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002606 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2607 if (unlikely(ret))
2608 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002609 len -= hlen;
2610 } else {
2611 plen = min_t(int, skb_headlen(from), len);
2612 if (plen) {
2613 page = virt_to_head_page(from->head);
2614 offset = from->data - (unsigned char *)page_address(page);
2615 __skb_fill_page_desc(to, 0, page, offset, plen);
2616 get_page(page);
2617 j = 1;
2618 len -= plen;
2619 }
2620 }
2621
2622 to->truesize += len + plen;
2623 to->len += len + plen;
2624 to->data_len += len + plen;
2625
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002626 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2627 skb_tx_error(from);
2628 return -ENOMEM;
2629 }
2630
Thomas Grafaf2806f2013-12-13 15:22:17 +01002631 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2632 if (!len)
2633 break;
2634 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2635 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2636 len -= skb_shinfo(to)->frags[j].size;
2637 skb_frag_ref(to, j);
2638 j++;
2639 }
2640 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002641
2642 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002643}
2644EXPORT_SYMBOL_GPL(skb_zerocopy);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2647{
Al Virod3bc23e2006-11-14 21:24:49 -08002648 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 long csstart;
2650
Patrick McHardy84fa7932006-08-29 16:44:56 -07002651 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002652 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 else
2654 csstart = skb_headlen(skb);
2655
Kris Katterjohn09a62662006-01-08 22:24:28 -08002656 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002658 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 csum = 0;
2661 if (csstart != skb->len)
2662 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2663 skb->len - csstart, 0);
2664
Patrick McHardy84fa7932006-08-29 16:44:56 -07002665 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002666 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Al Virod3bc23e2006-11-14 21:24:49 -08002668 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002671EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673/**
2674 * skb_dequeue - remove from the head of the queue
2675 * @list: list to dequeue from
2676 *
2677 * Remove the head of the list. The list lock is taken so the function
2678 * may be used safely with other locking list functions. The head item is
2679 * returned or %NULL if the list is empty.
2680 */
2681
2682struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2683{
2684 unsigned long flags;
2685 struct sk_buff *result;
2686
2687 spin_lock_irqsave(&list->lock, flags);
2688 result = __skb_dequeue(list);
2689 spin_unlock_irqrestore(&list->lock, flags);
2690 return result;
2691}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002692EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694/**
2695 * skb_dequeue_tail - remove from the tail of the queue
2696 * @list: list to dequeue from
2697 *
2698 * Remove the tail of the list. The list lock is taken so the function
2699 * may be used safely with other locking list functions. The tail item is
2700 * returned or %NULL if the list is empty.
2701 */
2702struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2703{
2704 unsigned long flags;
2705 struct sk_buff *result;
2706
2707 spin_lock_irqsave(&list->lock, flags);
2708 result = __skb_dequeue_tail(list);
2709 spin_unlock_irqrestore(&list->lock, flags);
2710 return result;
2711}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002712EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714/**
2715 * skb_queue_purge - empty a list
2716 * @list: list to empty
2717 *
2718 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2719 * the list and one reference dropped. This function takes the list
2720 * lock and is atomic with respect to other list locking functions.
2721 */
2722void skb_queue_purge(struct sk_buff_head *list)
2723{
2724 struct sk_buff *skb;
2725 while ((skb = skb_dequeue(list)) != NULL)
2726 kfree_skb(skb);
2727}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002728EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002731 * skb_rbtree_purge - empty a skb rbtree
2732 * @root: root of the rbtree to empty
2733 *
2734 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2735 * the list and one reference dropped. This function does not take
2736 * any lock. Synchronization should be handled by the caller (e.g., TCP
2737 * out-of-order queue is protected by the socket lock).
2738 */
2739void skb_rbtree_purge(struct rb_root *root)
2740{
2741 struct sk_buff *skb, *next;
2742
2743 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2744 kfree_skb(skb);
2745
2746 *root = RB_ROOT;
2747}
2748
2749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 * skb_queue_head - queue a buffer at the list head
2751 * @list: list to use
2752 * @newsk: buffer to queue
2753 *
2754 * Queue a buffer at the start of the list. This function takes the
2755 * list lock and can be used safely with other locking &sk_buff functions
2756 * safely.
2757 *
2758 * A buffer cannot be placed on two lists at the same time.
2759 */
2760void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2761{
2762 unsigned long flags;
2763
2764 spin_lock_irqsave(&list->lock, flags);
2765 __skb_queue_head(list, newsk);
2766 spin_unlock_irqrestore(&list->lock, flags);
2767}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002768EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770/**
2771 * skb_queue_tail - queue a buffer at the list tail
2772 * @list: list to use
2773 * @newsk: buffer to queue
2774 *
2775 * Queue a buffer at the tail of the list. This function takes the
2776 * list lock and can be used safely with other locking &sk_buff functions
2777 * safely.
2778 *
2779 * A buffer cannot be placed on two lists at the same time.
2780 */
2781void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2782{
2783 unsigned long flags;
2784
2785 spin_lock_irqsave(&list->lock, flags);
2786 __skb_queue_tail(list, newsk);
2787 spin_unlock_irqrestore(&list->lock, flags);
2788}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002789EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791/**
2792 * skb_unlink - remove a buffer from a list
2793 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002794 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 *
David S. Miller8728b832005-08-09 19:25:21 -07002796 * Remove a packet from a list. The list locks are taken and this
2797 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 *
David S. Miller8728b832005-08-09 19:25:21 -07002799 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 */
David S. Miller8728b832005-08-09 19:25:21 -07002801void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
David S. Miller8728b832005-08-09 19:25:21 -07002803 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
David S. Miller8728b832005-08-09 19:25:21 -07002805 spin_lock_irqsave(&list->lock, flags);
2806 __skb_unlink(skb, list);
2807 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002809EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/**
2812 * skb_append - append a buffer
2813 * @old: buffer to insert after
2814 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002815 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 *
2817 * Place a packet after a given packet in a list. The list locks are taken
2818 * and this function is atomic with respect to other list locked calls.
2819 * A buffer cannot be placed on two lists at the same time.
2820 */
David S. Miller8728b832005-08-09 19:25:21 -07002821void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 unsigned long flags;
2824
David S. Miller8728b832005-08-09 19:25:21 -07002825 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002826 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002827 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002829EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831/**
2832 * skb_insert - insert a buffer
2833 * @old: buffer to insert before
2834 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002835 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 *
David S. Miller8728b832005-08-09 19:25:21 -07002837 * Place a packet before a given packet in a list. The list locks are
2838 * taken and this function is atomic with respect to other list locked
2839 * calls.
2840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * A buffer cannot be placed on two lists at the same time.
2842 */
David S. Miller8728b832005-08-09 19:25:21 -07002843void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 unsigned long flags;
2846
David S. Miller8728b832005-08-09 19:25:21 -07002847 spin_lock_irqsave(&list->lock, flags);
2848 __skb_insert(newsk, old->prev, old, list);
2849 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002851EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853static inline void skb_split_inside_header(struct sk_buff *skb,
2854 struct sk_buff* skb1,
2855 const u32 len, const int pos)
2856{
2857 int i;
2858
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002859 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2860 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 /* And move data appendix as is. */
2862 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2863 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2864
2865 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2866 skb_shinfo(skb)->nr_frags = 0;
2867 skb1->data_len = skb->data_len;
2868 skb1->len += skb1->data_len;
2869 skb->data_len = 0;
2870 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002871 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
2874static inline void skb_split_no_header(struct sk_buff *skb,
2875 struct sk_buff* skb1,
2876 const u32 len, int pos)
2877{
2878 int i, k = 0;
2879 const int nfrags = skb_shinfo(skb)->nr_frags;
2880
2881 skb_shinfo(skb)->nr_frags = 0;
2882 skb1->len = skb1->data_len = skb->len - len;
2883 skb->len = len;
2884 skb->data_len = len - pos;
2885
2886 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002887 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 if (pos + size > len) {
2890 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2891
2892 if (pos < len) {
2893 /* Split frag.
2894 * We have two variants in this case:
2895 * 1. Move all the frag to the second
2896 * part, if it is possible. F.e.
2897 * this approach is mandatory for TUX,
2898 * where splitting is expensive.
2899 * 2. Split is accurately. We make this.
2900 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002901 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002903 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2904 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 skb_shinfo(skb)->nr_frags++;
2906 }
2907 k++;
2908 } else
2909 skb_shinfo(skb)->nr_frags++;
2910 pos += size;
2911 }
2912 skb_shinfo(skb1)->nr_frags = k;
2913}
2914
2915/**
2916 * skb_split - Split fragmented skb to two parts at length len.
2917 * @skb: the buffer to split
2918 * @skb1: the buffer to receive the second part
2919 * @len: new length for skb
2920 */
2921void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2922{
2923 int pos = skb_headlen(skb);
2924
Willem de Bruijnfff88032017-06-08 11:35:03 -04002925 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2926 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (len < pos) /* Split line is inside header. */
2928 skb_split_inside_header(skb, skb1, len, pos);
2929 else /* Second chunk has no header, nothing to copy. */
2930 skb_split_no_header(skb, skb1, len, pos);
2931}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002932EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002934/* Shifting from/to a cloned skb is a no-go.
2935 *
2936 * Caller cannot keep skb_shinfo related pointers past calling here!
2937 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002938static int skb_prepare_for_shift(struct sk_buff *skb)
2939{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002940 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002941}
2942
2943/**
2944 * skb_shift - Shifts paged data partially from skb to another
2945 * @tgt: buffer into which tail data gets added
2946 * @skb: buffer from which the paged data comes from
2947 * @shiftlen: shift up to this many bytes
2948 *
2949 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002950 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002951 * It's up to caller to free skb if everything was shifted.
2952 *
2953 * If @tgt runs out of frags, the whole operation is aborted.
2954 *
2955 * Skb cannot include anything else but paged data while tgt is allowed
2956 * to have non-paged data as well.
2957 *
2958 * TODO: full sized shift could be optimized but that would need
2959 * specialized skb free'er to handle frags without up-to-date nr_frags.
2960 */
2961int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2962{
2963 int from, to, merge, todo;
2964 struct skb_frag_struct *fragfrom, *fragto;
2965
2966 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002967
2968 if (skb_headlen(skb))
2969 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002970
2971 todo = shiftlen;
2972 from = 0;
2973 to = skb_shinfo(tgt)->nr_frags;
2974 fragfrom = &skb_shinfo(skb)->frags[from];
2975
2976 /* Actual merge is delayed until the point when we know we can
2977 * commit all, so that we don't have to undo partial changes
2978 */
2979 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002980 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2981 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002982 merge = -1;
2983 } else {
2984 merge = to - 1;
2985
Eric Dumazet9e903e02011-10-18 21:00:24 +00002986 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002987 if (todo < 0) {
2988 if (skb_prepare_for_shift(skb) ||
2989 skb_prepare_for_shift(tgt))
2990 return 0;
2991
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002992 /* All previous frag pointers might be stale! */
2993 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002994 fragto = &skb_shinfo(tgt)->frags[merge];
2995
Eric Dumazet9e903e02011-10-18 21:00:24 +00002996 skb_frag_size_add(fragto, shiftlen);
2997 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002998 fragfrom->page_offset += shiftlen;
2999
3000 goto onlymerged;
3001 }
3002
3003 from++;
3004 }
3005
3006 /* Skip full, not-fitting skb to avoid expensive operations */
3007 if ((shiftlen == skb->len) &&
3008 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3009 return 0;
3010
3011 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3012 return 0;
3013
3014 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3015 if (to == MAX_SKB_FRAGS)
3016 return 0;
3017
3018 fragfrom = &skb_shinfo(skb)->frags[from];
3019 fragto = &skb_shinfo(tgt)->frags[to];
3020
Eric Dumazet9e903e02011-10-18 21:00:24 +00003021 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003022 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003023 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003024 from++;
3025 to++;
3026
3027 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003028 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003029 fragto->page = fragfrom->page;
3030 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003031 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003032
3033 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003034 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003035 todo = 0;
3036
3037 to++;
3038 break;
3039 }
3040 }
3041
3042 /* Ready to "commit" this state change to tgt */
3043 skb_shinfo(tgt)->nr_frags = to;
3044
3045 if (merge >= 0) {
3046 fragfrom = &skb_shinfo(skb)->frags[0];
3047 fragto = &skb_shinfo(tgt)->frags[merge];
3048
Eric Dumazet9e903e02011-10-18 21:00:24 +00003049 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003050 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003051 }
3052
3053 /* Reposition in the original skb */
3054 to = 0;
3055 while (from < skb_shinfo(skb)->nr_frags)
3056 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3057 skb_shinfo(skb)->nr_frags = to;
3058
3059 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3060
3061onlymerged:
3062 /* Most likely the tgt won't ever need its checksum anymore, skb on
3063 * the other hand might need it if it needs to be resent
3064 */
3065 tgt->ip_summed = CHECKSUM_PARTIAL;
3066 skb->ip_summed = CHECKSUM_PARTIAL;
3067
3068 /* Yak, is it really working this way? Some helper please? */
3069 skb->len -= shiftlen;
3070 skb->data_len -= shiftlen;
3071 skb->truesize -= shiftlen;
3072 tgt->len += shiftlen;
3073 tgt->data_len += shiftlen;
3074 tgt->truesize += shiftlen;
3075
3076 return shiftlen;
3077}
3078
Thomas Graf677e90e2005-06-23 20:59:51 -07003079/**
3080 * skb_prepare_seq_read - Prepare a sequential read of skb data
3081 * @skb: the buffer to read
3082 * @from: lower offset of data to be read
3083 * @to: upper offset of data to be read
3084 * @st: state variable
3085 *
3086 * Initializes the specified state variable. Must be called before
3087 * invoking skb_seq_read() for the first time.
3088 */
3089void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3090 unsigned int to, struct skb_seq_state *st)
3091{
3092 st->lower_offset = from;
3093 st->upper_offset = to;
3094 st->root_skb = st->cur_skb = skb;
3095 st->frag_idx = st->stepped_offset = 0;
3096 st->frag_data = NULL;
3097}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003098EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003099
3100/**
3101 * skb_seq_read - Sequentially read skb data
3102 * @consumed: number of bytes consumed by the caller so far
3103 * @data: destination pointer for data to be returned
3104 * @st: state variable
3105 *
Mathias Krausebc323832013-11-07 14:18:26 +01003106 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003107 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003108 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003109 * of the block or 0 if the end of the skb data or the upper
3110 * offset has been reached.
3111 *
3112 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003113 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003114 * of bytes already consumed and the next call to
3115 * skb_seq_read() will return the remaining part of the block.
3116 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003117 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003118 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003119 * reads of potentially non linear data.
3120 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003121 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003122 * at the moment, state->root_skb could be replaced with
3123 * a stack for this purpose.
3124 */
3125unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3126 struct skb_seq_state *st)
3127{
3128 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3129 skb_frag_t *frag;
3130
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003131 if (unlikely(abs_offset >= st->upper_offset)) {
3132 if (st->frag_data) {
3133 kunmap_atomic(st->frag_data);
3134 st->frag_data = NULL;
3135 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003136 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003137 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003138
3139next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003140 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003141
Thomas Chenault995b3372009-05-18 21:43:27 -07003142 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003143 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003144 return block_limit - abs_offset;
3145 }
3146
3147 if (st->frag_idx == 0 && !st->frag_data)
3148 st->stepped_offset += skb_headlen(st->cur_skb);
3149
3150 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3151 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003152 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003153
3154 if (abs_offset < block_limit) {
3155 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003156 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003157
3158 *data = (u8 *) st->frag_data + frag->page_offset +
3159 (abs_offset - st->stepped_offset);
3160
3161 return block_limit - abs_offset;
3162 }
3163
3164 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003165 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003166 st->frag_data = NULL;
3167 }
3168
3169 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003170 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003171 }
3172
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003173 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003174 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003175 st->frag_data = NULL;
3176 }
3177
David S. Miller21dc3302010-08-23 00:13:46 -07003178 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003179 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003180 st->frag_idx = 0;
3181 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003182 } else if (st->cur_skb->next) {
3183 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003184 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003185 goto next_skb;
3186 }
3187
3188 return 0;
3189}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003190EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003191
3192/**
3193 * skb_abort_seq_read - Abort a sequential read of skb data
3194 * @st: state variable
3195 *
3196 * Must be called if skb_seq_read() was not called until it
3197 * returned 0.
3198 */
3199void skb_abort_seq_read(struct skb_seq_state *st)
3200{
3201 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003202 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003203}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003204EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003205
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003206#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3207
3208static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3209 struct ts_config *conf,
3210 struct ts_state *state)
3211{
3212 return skb_seq_read(offset, text, TS_SKB_CB(state));
3213}
3214
3215static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3216{
3217 skb_abort_seq_read(TS_SKB_CB(state));
3218}
3219
3220/**
3221 * skb_find_text - Find a text pattern in skb data
3222 * @skb: the buffer to look in
3223 * @from: search offset
3224 * @to: search limit
3225 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003226 *
3227 * Finds a pattern in the skb data according to the specified
3228 * textsearch configuration. Use textsearch_next() to retrieve
3229 * subsequent occurrences of the pattern. Returns the offset
3230 * to the first occurrence or UINT_MAX if no match was found.
3231 */
3232unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003233 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003234{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003235 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003236 unsigned int ret;
3237
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003238 config->get_next_block = skb_ts_get_next_block;
3239 config->finish = skb_ts_finish;
3240
Bojan Prtvar059a2442015-02-22 11:46:35 +01003241 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003242
Bojan Prtvar059a2442015-02-22 11:46:35 +01003243 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003244 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003245}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003246EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003247
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003248/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003249 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003250 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09003251 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003252 * @getfrag: call back function to be used for getting the user data
3253 * @from: pointer to user message iov
3254 * @length: length of the iov message
3255 *
3256 * Description: This procedure append the user data in the fragment part
3257 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3258 */
3259int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08003260 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003261 int len, int odd, struct sk_buff *skb),
3262 void *from, int length)
3263{
Eric Dumazetb2111722012-12-28 06:06:37 +00003264 int frg_cnt = skb_shinfo(skb)->nr_frags;
3265 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003266 int offset = 0;
3267 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00003268 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003269
3270 do {
3271 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003272 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00003273 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003274
Eric Dumazetb2111722012-12-28 06:06:37 +00003275 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003276 return -ENOMEM;
3277
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003278 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00003279 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003280
Eric Dumazetb2111722012-12-28 06:06:37 +00003281 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3282 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003283 if (ret < 0)
3284 return -EFAULT;
3285
3286 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003287 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3288 copy);
3289 frg_cnt++;
3290 pfrag->offset += copy;
3291 get_page(pfrag->page);
3292
3293 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003294 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003295 skb->len += copy;
3296 skb->data_len += copy;
3297 offset += copy;
3298 length -= copy;
3299
3300 } while (length > 0);
3301
3302 return 0;
3303}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003304EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003305
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003306int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3307 int offset, size_t size)
3308{
3309 int i = skb_shinfo(skb)->nr_frags;
3310
3311 if (skb_can_coalesce(skb, i, page, offset)) {
3312 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3313 } else if (i < MAX_SKB_FRAGS) {
3314 get_page(page);
3315 skb_fill_page_desc(skb, i, page, offset, size);
3316 } else {
3317 return -EMSGSIZE;
3318 }
3319
3320 return 0;
3321}
3322EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3323
Herbert Xucbb042f2006-03-20 22:43:56 -08003324/**
3325 * skb_pull_rcsum - pull skb and update receive checksum
3326 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003327 * @len: length of data pulled
3328 *
3329 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003330 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003331 * receive path processing instead of skb_pull unless you know
3332 * that the checksum difference is zero (e.g., a valid IP header)
3333 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003334 */
Johannes Bergaf728682017-06-16 14:29:22 +02003335void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003336{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003337 unsigned char *data = skb->data;
3338
Herbert Xucbb042f2006-03-20 22:43:56 -08003339 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003340 __skb_pull(skb, len);
3341 skb_postpull_rcsum(skb, data, len);
3342 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003343}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003344EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3345
Herbert Xuf4c50d92006-06-22 03:02:40 -07003346/**
3347 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003348 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003349 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003350 *
3351 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003352 * a pointer to the first in a list of new skbs for the segments.
3353 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003354 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003355struct sk_buff *skb_segment(struct sk_buff *head_skb,
3356 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003357{
3358 struct sk_buff *segs = NULL;
3359 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003360 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003361 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3362 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3363 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003364 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003365 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003366 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003367 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003368 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003369 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003370 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003371 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003372 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003373 int err = -ENOMEM;
3374 int i = 0;
3375 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003376 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003377
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003378 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003379 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003380 if (unlikely(!proto))
3381 return ERR_PTR(-EINVAL);
3382
Alexander Duyck36c98382016-05-02 09:38:18 -07003383 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003384 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003385
Steffen Klassert07b26c92016-09-19 12:58:47 +02003386 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3387 if (!(features & NETIF_F_GSO_PARTIAL)) {
3388 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003389 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003390
3391 if (!list_skb ||
3392 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3393 goto normal;
3394
Ilan Tayari43170c42017-04-19 21:26:07 +03003395 /* If we get here then all the required
3396 * GSO features except frag_list are supported.
3397 * Try to split the SKB to multiple GSO SKBs
3398 * with no frag_list.
3399 * Currently we can do that only when the buffers don't
3400 * have a linear part and all the buffers except
3401 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003402 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003403 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003404 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003405 if (frag_len != iter->len && iter->next)
3406 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003407 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003408 goto normal;
3409
3410 len -= iter->len;
3411 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003412
3413 if (len != frag_len)
3414 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003415 }
3416
3417 /* GSO partial only requires that we trim off any excess that
3418 * doesn't fit into an MSS sized block, so take care of that
3419 * now.
3420 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003421 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003422 if (partial_segs > 1)
3423 mss *= partial_segs;
3424 else
3425 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003426 }
3427
Steffen Klassert07b26c92016-09-19 12:58:47 +02003428normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003429 headroom = skb_headroom(head_skb);
3430 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003431
3432 do {
3433 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003434 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003435 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003436 int size;
3437
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003438 if (unlikely(mss == GSO_BY_FRAGS)) {
3439 len = list_skb->len;
3440 } else {
3441 len = head_skb->len - offset;
3442 if (len > mss)
3443 len = mss;
3444 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003445
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003446 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003447 if (hsize < 0)
3448 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003449 if (hsize > len || !sg)
3450 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003451
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003452 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3453 (skb_headlen(list_skb) == len || sg)) {
3454 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003455
Herbert Xu9d8506c2013-11-21 11:10:04 -08003456 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003457 nfrags = skb_shinfo(list_skb)->nr_frags;
3458 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003459 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003460 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003461
3462 while (pos < offset + len) {
3463 BUG_ON(i >= nfrags);
3464
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003465 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003466 if (pos + size > offset + len)
3467 break;
3468
3469 i++;
3470 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003471 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003472 }
3473
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003474 nskb = skb_clone(list_skb, GFP_ATOMIC);
3475 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003476
3477 if (unlikely(!nskb))
3478 goto err;
3479
Herbert Xu9d8506c2013-11-21 11:10:04 -08003480 if (unlikely(pskb_trim(nskb, len))) {
3481 kfree_skb(nskb);
3482 goto err;
3483 }
3484
Alexander Duyckec47ea82012-05-04 14:26:56 +00003485 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003486 if (skb_cow_head(nskb, doffset + headroom)) {
3487 kfree_skb(nskb);
3488 goto err;
3489 }
3490
Alexander Duyckec47ea82012-05-04 14:26:56 +00003491 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003492 skb_release_head_state(nskb);
3493 __skb_push(nskb, doffset);
3494 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003495 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003496 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003497 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003498
3499 if (unlikely(!nskb))
3500 goto err;
3501
3502 skb_reserve(nskb, headroom);
3503 __skb_put(nskb, doffset);
3504 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003505
3506 if (segs)
3507 tail->next = nskb;
3508 else
3509 segs = nskb;
3510 tail = nskb;
3511
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003512 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003513
Eric Dumazet030737b2013-10-19 11:42:54 -07003514 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003515 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003516
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003517 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003518 nskb->data - tnl_hlen,
3519 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003520
Herbert Xu9d8506c2013-11-21 11:10:04 -08003521 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003522 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003523
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003524 if (!sg) {
3525 if (!nskb->remcsum_offload)
3526 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003527 SKB_GSO_CB(nskb)->csum =
3528 skb_copy_and_csum_bits(head_skb, offset,
3529 skb_put(nskb, len),
3530 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003531 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003532 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003533 continue;
3534 }
3535
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003536 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003537
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003538 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003539 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003540
Willem de Bruijnfff88032017-06-08 11:35:03 -04003541 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3542 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003543
Herbert Xu9d8506c2013-11-21 11:10:04 -08003544 while (pos < offset + len) {
3545 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003546 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003547
3548 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003549 nfrags = skb_shinfo(list_skb)->nr_frags;
3550 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003551 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003552
3553 BUG_ON(!nfrags);
3554
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003555 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003556 }
3557
3558 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3559 MAX_SKB_FRAGS)) {
3560 net_warn_ratelimited(
3561 "skb_segment: too many frags: %u %u\n",
3562 pos, mss);
3563 goto err;
3564 }
3565
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003566 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3567 goto err;
3568
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003569 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003570 __skb_frag_ref(nskb_frag);
3571 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003572
3573 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003574 nskb_frag->page_offset += offset - pos;
3575 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003576 }
3577
Herbert Xu89319d382008-12-15 23:26:06 -08003578 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003579
3580 if (pos + size <= offset + len) {
3581 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003582 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003583 pos += size;
3584 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003585 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003586 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003587 }
3588
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003589 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003590 }
3591
Herbert Xu89319d382008-12-15 23:26:06 -08003592skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003593 nskb->data_len = len - hsize;
3594 nskb->len += nskb->data_len;
3595 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003596
Simon Horman1cdbcb72013-05-19 15:46:49 +00003597perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003598 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003599 if (skb_has_shared_frag(nskb)) {
3600 err = __skb_linearize(nskb);
3601 if (err)
3602 goto err;
3603 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003604 if (!nskb->remcsum_offload)
3605 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003606 SKB_GSO_CB(nskb)->csum =
3607 skb_checksum(nskb, doffset,
3608 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003609 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003610 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003611 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003612 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003613
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003614 /* Some callers want to get the end of the list.
3615 * Put it in segs->prev to avoid walking the list.
3616 * (see validate_xmit_skb_list() for example)
3617 */
3618 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003619
Alexander Duyck802ab552016-04-10 21:45:03 -04003620 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003621 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003622 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003623 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003624
3625 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003626 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003627 type &= ~SKB_GSO_DODGY;
3628
3629 /* Update GSO info and prepare to start updating headers on
3630 * our way back down the stack of protocols.
3631 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003632 for (iter = segs; iter; iter = iter->next) {
3633 skb_shinfo(iter)->gso_size = gso_size;
3634 skb_shinfo(iter)->gso_segs = partial_segs;
3635 skb_shinfo(iter)->gso_type = type;
3636 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3637 }
3638
3639 if (tail->len - doffset <= gso_size)
3640 skb_shinfo(tail)->gso_size = 0;
3641 else if (tail != segs)
3642 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003643 }
3644
Toshiaki Makita432c8562014-10-27 10:30:51 -07003645 /* Following permits correct backpressure, for protocols
3646 * using skb_set_owner_w().
3647 * Idea is to tranfert ownership from head_skb to last segment.
3648 */
3649 if (head_skb->destructor == sock_wfree) {
3650 swap(tail->truesize, head_skb->truesize);
3651 swap(tail->destructor, head_skb->destructor);
3652 swap(tail->sk, head_skb->sk);
3653 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003654 return segs;
3655
3656err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003657 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003658 return ERR_PTR(err);
3659}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003660EXPORT_SYMBOL_GPL(skb_segment);
3661
Herbert Xu71d93b32008-12-15 23:42:33 -08003662int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3663{
Eric Dumazet8a291112013-10-08 09:02:23 -07003664 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003665 unsigned int offset = skb_gro_offset(skb);
3666 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003667 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003668 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003669 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003670
Eric Dumazet8a291112013-10-08 09:02:23 -07003671 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003672 return -E2BIG;
3673
Eric Dumazet29e98242014-05-16 11:34:37 -07003674 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003675 pinfo = skb_shinfo(lp);
3676
3677 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003678 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003679 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003680 int i = skbinfo->nr_frags;
3681 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003682
Herbert Xu66e92fc2009-05-26 18:50:32 +00003683 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003684 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003685
Eric Dumazet8a291112013-10-08 09:02:23 -07003686 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003687 pinfo->nr_frags = nr_frags;
3688 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003689
Herbert Xu9aaa1562009-05-26 18:50:33 +00003690 frag = pinfo->frags + nr_frags;
3691 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003692 do {
3693 *--frag = *--frag2;
3694 } while (--i);
3695
3696 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003697 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003698
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003699 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003700 delta_truesize = skb->truesize -
3701 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003702
Herbert Xuf5572062009-01-14 20:40:03 -08003703 skb->truesize -= skb->data_len;
3704 skb->len -= skb->data_len;
3705 skb->data_len = 0;
3706
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003707 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003708 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003709 } else if (skb->head_frag) {
3710 int nr_frags = pinfo->nr_frags;
3711 skb_frag_t *frag = pinfo->frags + nr_frags;
3712 struct page *page = virt_to_head_page(skb->head);
3713 unsigned int first_size = headlen - offset;
3714 unsigned int first_offset;
3715
3716 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003717 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003718
3719 first_offset = skb->data -
3720 (unsigned char *)page_address(page) +
3721 offset;
3722
3723 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3724
3725 frag->page.p = page;
3726 frag->page_offset = first_offset;
3727 skb_frag_size_set(frag, first_size);
3728
3729 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3730 /* We dont need to clear skbinfo->nr_frags here */
3731
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003732 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003733 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3734 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003735 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003736
3737merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003738 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003739 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003740 unsigned int eat = offset - headlen;
3741
3742 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003743 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003744 skb->data_len -= eat;
3745 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003746 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003747 }
3748
Herbert Xu67147ba2009-05-26 18:50:22 +00003749 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003750
Eric Dumazet29e98242014-05-16 11:34:37 -07003751 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003752 skb_shinfo(p)->frag_list = skb;
3753 else
3754 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003755 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003756 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003757 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003758
Herbert Xu5d38a072009-01-04 16:13:40 -08003759done:
3760 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003761 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003762 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003763 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003764 if (lp != p) {
3765 lp->data_len += len;
3766 lp->truesize += delta_truesize;
3767 lp->len += len;
3768 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003769 NAPI_GRO_CB(skb)->same_flow = 1;
3770 return 0;
3771}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003772EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774void __init skb_init(void)
3775{
3776 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3777 sizeof(struct sk_buff),
3778 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003779 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003780 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003781 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003782 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003783 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003784 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003785 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
David S. Miller51c739d2007-10-30 21:29:29 -07003788static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003789__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3790 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003791{
David S. Miller1a028e52007-04-27 15:21:23 -07003792 int start = skb_headlen(skb);
3793 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003794 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003795 int elt = 0;
3796
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003797 if (unlikely(recursion_level >= 24))
3798 return -EMSGSIZE;
3799
David Howells716ea3a2007-04-02 20:19:53 -07003800 if (copy > 0) {
3801 if (copy > len)
3802 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003803 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003804 elt++;
3805 if ((len -= copy) == 0)
3806 return elt;
3807 offset += copy;
3808 }
3809
3810 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003811 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003812
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003813 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003814
Eric Dumazet9e903e02011-10-18 21:00:24 +00003815 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003816 if ((copy = end - offset) > 0) {
3817 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003818 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3819 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003820
3821 if (copy > len)
3822 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003823 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003824 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003825 elt++;
3826 if (!(len -= copy))
3827 return elt;
3828 offset += copy;
3829 }
David S. Miller1a028e52007-04-27 15:21:23 -07003830 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003831 }
3832
David S. Millerfbb398a2009-06-09 00:18:59 -07003833 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003834 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003835
David S. Millerfbb398a2009-06-09 00:18:59 -07003836 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003837
David S. Millerfbb398a2009-06-09 00:18:59 -07003838 end = start + frag_iter->len;
3839 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003840 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3841 return -EMSGSIZE;
3842
David S. Millerfbb398a2009-06-09 00:18:59 -07003843 if (copy > len)
3844 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003845 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3846 copy, recursion_level + 1);
3847 if (unlikely(ret < 0))
3848 return ret;
3849 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003850 if ((len -= copy) == 0)
3851 return elt;
3852 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003853 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003854 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003855 }
3856 BUG_ON(len);
3857 return elt;
3858}
3859
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003860/**
3861 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3862 * @skb: Socket buffer containing the buffers to be mapped
3863 * @sg: The scatter-gather list to map into
3864 * @offset: The offset into the buffer's contents to start mapping
3865 * @len: Length of buffer space to be mapped
3866 *
3867 * Fill the specified scatter-gather list with mappings/pointers into a
3868 * region of the buffer space attached to a socket buffer. Returns either
3869 * the number of scatterlist items used, or -EMSGSIZE if the contents
3870 * could not fit.
3871 */
3872int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3873{
3874 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3875
3876 if (nsg <= 0)
3877 return nsg;
3878
3879 sg_mark_end(&sg[nsg - 1]);
3880
3881 return nsg;
3882}
3883EXPORT_SYMBOL_GPL(skb_to_sgvec);
3884
Fan Du25a91d82014-01-18 09:54:23 +08003885/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3886 * sglist without mark the sg which contain last skb data as the end.
3887 * So the caller can mannipulate sg list as will when padding new data after
3888 * the first call without calling sg_unmark_end to expend sg list.
3889 *
3890 * Scenario to use skb_to_sgvec_nomark:
3891 * 1. sg_init_table
3892 * 2. skb_to_sgvec_nomark(payload1)
3893 * 3. skb_to_sgvec_nomark(payload2)
3894 *
3895 * This is equivalent to:
3896 * 1. sg_init_table
3897 * 2. skb_to_sgvec(payload1)
3898 * 3. sg_unmark_end
3899 * 4. skb_to_sgvec(payload2)
3900 *
3901 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3902 * is more preferable.
3903 */
3904int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3905 int offset, int len)
3906{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003907 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003908}
3909EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3910
David S. Miller51c739d2007-10-30 21:29:29 -07003911
David S. Miller51c739d2007-10-30 21:29:29 -07003912
David Howells716ea3a2007-04-02 20:19:53 -07003913/**
3914 * skb_cow_data - Check that a socket buffer's data buffers are writable
3915 * @skb: The socket buffer to check.
3916 * @tailbits: Amount of trailing space to be added
3917 * @trailer: Returned pointer to the skb where the @tailbits space begins
3918 *
3919 * Make sure that the data buffers attached to a socket buffer are
3920 * writable. If they are not, private copies are made of the data buffers
3921 * and the socket buffer is set to use these instead.
3922 *
3923 * If @tailbits is given, make sure that there is space to write @tailbits
3924 * bytes of data beyond current end of socket buffer. @trailer will be
3925 * set to point to the skb in which this space begins.
3926 *
3927 * The number of scatterlist elements required to completely map the
3928 * COW'd and extended socket buffer will be returned.
3929 */
3930int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3931{
3932 int copyflag;
3933 int elt;
3934 struct sk_buff *skb1, **skb_p;
3935
3936 /* If skb is cloned or its head is paged, reallocate
3937 * head pulling out all the pages (pages are considered not writable
3938 * at the moment even if they are anonymous).
3939 */
3940 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3941 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3942 return -ENOMEM;
3943
3944 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003945 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003946 /* A little of trouble, not enough of space for trailer.
3947 * This should not happen, when stack is tuned to generate
3948 * good frames. OK, on miss we reallocate and reserve even more
3949 * space, 128 bytes is fair. */
3950
3951 if (skb_tailroom(skb) < tailbits &&
3952 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3953 return -ENOMEM;
3954
3955 /* Voila! */
3956 *trailer = skb;
3957 return 1;
3958 }
3959
3960 /* Misery. We are in troubles, going to mincer fragments... */
3961
3962 elt = 1;
3963 skb_p = &skb_shinfo(skb)->frag_list;
3964 copyflag = 0;
3965
3966 while ((skb1 = *skb_p) != NULL) {
3967 int ntail = 0;
3968
3969 /* The fragment is partially pulled by someone,
3970 * this can happen on input. Copy it and everything
3971 * after it. */
3972
3973 if (skb_shared(skb1))
3974 copyflag = 1;
3975
3976 /* If the skb is the last, worry about trailer. */
3977
3978 if (skb1->next == NULL && tailbits) {
3979 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003980 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003981 skb_tailroom(skb1) < tailbits)
3982 ntail = tailbits + 128;
3983 }
3984
3985 if (copyflag ||
3986 skb_cloned(skb1) ||
3987 ntail ||
3988 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003989 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003990 struct sk_buff *skb2;
3991
3992 /* Fuck, we are miserable poor guys... */
3993 if (ntail == 0)
3994 skb2 = skb_copy(skb1, GFP_ATOMIC);
3995 else
3996 skb2 = skb_copy_expand(skb1,
3997 skb_headroom(skb1),
3998 ntail,
3999 GFP_ATOMIC);
4000 if (unlikely(skb2 == NULL))
4001 return -ENOMEM;
4002
4003 if (skb1->sk)
4004 skb_set_owner_w(skb2, skb1->sk);
4005
4006 /* Looking around. Are we still alive?
4007 * OK, link new skb, drop old one */
4008
4009 skb2->next = skb1->next;
4010 *skb_p = skb2;
4011 kfree_skb(skb1);
4012 skb1 = skb2;
4013 }
4014 elt++;
4015 *trailer = skb1;
4016 skb_p = &skb1->next;
4017 }
4018
4019 return elt;
4020}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004021EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004022
Eric Dumazetb1faf562010-05-31 23:44:05 -07004023static void sock_rmem_free(struct sk_buff *skb)
4024{
4025 struct sock *sk = skb->sk;
4026
4027 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4028}
4029
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004030static void skb_set_err_queue(struct sk_buff *skb)
4031{
4032 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4033 * So, it is safe to (mis)use it to mark skbs on the error queue.
4034 */
4035 skb->pkt_type = PACKET_OUTGOING;
4036 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4037}
4038
Eric Dumazetb1faf562010-05-31 23:44:05 -07004039/*
4040 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4041 */
4042int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4043{
4044 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004045 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004046 return -ENOMEM;
4047
4048 skb_orphan(skb);
4049 skb->sk = sk;
4050 skb->destructor = sock_rmem_free;
4051 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004052 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004053
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004054 /* before exiting rcu section, make sure dst is refcounted */
4055 skb_dst_force(skb);
4056
Eric Dumazetb1faf562010-05-31 23:44:05 -07004057 skb_queue_tail(&sk->sk_error_queue, skb);
4058 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04004059 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004060 return 0;
4061}
4062EXPORT_SYMBOL(sock_queue_err_skb);
4063
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004064static bool is_icmp_err_skb(const struct sk_buff *skb)
4065{
4066 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4067 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4068}
4069
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004070struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4071{
4072 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004073 struct sk_buff *skb, *skb_next = NULL;
4074 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004075 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004076
Eric Dumazet997d5c32015-02-18 05:47:55 -08004077 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004078 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004079 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004080 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004081 if (icmp_next)
4082 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4083 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004084 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004085
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004086 if (is_icmp_err_skb(skb) && !icmp_next)
4087 sk->sk_err = 0;
4088
4089 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004090 sk->sk_error_report(sk);
4091
4092 return skb;
4093}
4094EXPORT_SYMBOL(sock_dequeue_err_skb);
4095
Alexander Duyckcab41c42014-09-10 18:05:26 -04004096/**
4097 * skb_clone_sk - create clone of skb, and take reference to socket
4098 * @skb: the skb to clone
4099 *
4100 * This function creates a clone of a buffer that holds a reference on
4101 * sk_refcnt. Buffers created via this function are meant to be
4102 * returned using sock_queue_err_skb, or free via kfree_skb.
4103 *
4104 * When passing buffers allocated with this function to sock_queue_err_skb
4105 * it is necessary to wrap the call with sock_hold/sock_put in order to
4106 * prevent the socket from being released prior to being enqueued on
4107 * the sk_error_queue.
4108 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004109struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4110{
4111 struct sock *sk = skb->sk;
4112 struct sk_buff *clone;
4113
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004114 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004115 return NULL;
4116
4117 clone = skb_clone(skb, GFP_ATOMIC);
4118 if (!clone) {
4119 sock_put(sk);
4120 return NULL;
4121 }
4122
4123 clone->sk = sk;
4124 clone->destructor = sock_efree;
4125
4126 return clone;
4127}
4128EXPORT_SYMBOL(skb_clone_sk);
4129
Alexander Duyck37846ef2014-09-04 13:31:10 -04004130static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4131 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004132 int tstype,
4133 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004134{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004135 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004136 int err;
4137
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004138 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4139
Patrick Ohlyac45f602009-02-12 05:03:37 +00004140 serr = SKB_EXT_ERR(skb);
4141 memset(serr, 0, sizeof(*serr));
4142 serr->ee.ee_errno = ENOMSG;
4143 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004144 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004145 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004146 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004147 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004148 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004149 if (sk->sk_protocol == IPPROTO_TCP &&
4150 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004151 serr->ee.ee_data -= sk->sk_tskey;
4152 }
Eric Dumazet29030372010-05-29 00:20:48 -07004153
Patrick Ohlyac45f602009-02-12 05:03:37 +00004154 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004155
Patrick Ohlyac45f602009-02-12 05:03:37 +00004156 if (err)
4157 kfree_skb(skb);
4158}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004159
Willem de Bruijnb245be12015-01-30 13:29:32 -05004160static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4161{
4162 bool ret;
4163
4164 if (likely(sysctl_tstamp_allow_data || tsonly))
4165 return true;
4166
4167 read_lock_bh(&sk->sk_callback_lock);
4168 ret = sk->sk_socket && sk->sk_socket->file &&
4169 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4170 read_unlock_bh(&sk->sk_callback_lock);
4171 return ret;
4172}
4173
Alexander Duyck37846ef2014-09-04 13:31:10 -04004174void skb_complete_tx_timestamp(struct sk_buff *skb,
4175 struct skb_shared_hwtstamps *hwtstamps)
4176{
4177 struct sock *sk = skb->sk;
4178
Willem de Bruijnb245be12015-01-30 13:29:32 -05004179 if (!skb_may_tx_timestamp(sk, false))
4180 return;
4181
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004182 /* Take a reference to prevent skb_orphan() from freeing the socket,
4183 * but only if the socket refcount is not zero.
4184 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004185 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004186 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004187 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004188 sock_put(sk);
4189 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004190}
4191EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4192
4193void __skb_tstamp_tx(struct sk_buff *orig_skb,
4194 struct skb_shared_hwtstamps *hwtstamps,
4195 struct sock *sk, int tstype)
4196{
4197 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004198 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004199
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004200 if (!sk)
4201 return;
4202
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004203 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4204 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4205 return;
4206
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004207 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4208 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004209 return;
4210
Francis Yan1c885802016-11-27 23:07:18 -08004211 if (tsonly) {
4212#ifdef CONFIG_INET
4213 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4214 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004215 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004216 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004217 opt_stats = true;
4218 } else
Francis Yan1c885802016-11-27 23:07:18 -08004219#endif
4220 skb = alloc_skb(0, GFP_ATOMIC);
4221 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004222 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004223 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004224 if (!skb)
4225 return;
4226
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004227 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004228 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4229 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004230 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4231 }
4232
4233 if (hwtstamps)
4234 *skb_hwtstamps(skb) = *hwtstamps;
4235 else
4236 skb->tstamp = ktime_get_real();
4237
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004238 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004239}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004240EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4241
4242void skb_tstamp_tx(struct sk_buff *orig_skb,
4243 struct skb_shared_hwtstamps *hwtstamps)
4244{
4245 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4246 SCM_TSTAMP_SND);
4247}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004248EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4249
Johannes Berg6e3e9392011-11-09 10:15:42 +01004250void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4251{
4252 struct sock *sk = skb->sk;
4253 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004254 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004255
4256 skb->wifi_acked_valid = 1;
4257 skb->wifi_acked = acked;
4258
4259 serr = SKB_EXT_ERR(skb);
4260 memset(serr, 0, sizeof(*serr));
4261 serr->ee.ee_errno = ENOMSG;
4262 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4263
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004264 /* Take a reference to prevent skb_orphan() from freeing the socket,
4265 * but only if the socket refcount is not zero.
4266 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004267 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004268 err = sock_queue_err_skb(sk, skb);
4269 sock_put(sk);
4270 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004271 if (err)
4272 kfree_skb(skb);
4273}
4274EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4275
Rusty Russellf35d9d82008-02-04 23:49:54 -05004276/**
4277 * skb_partial_csum_set - set up and verify partial csum values for packet
4278 * @skb: the skb to set
4279 * @start: the number of bytes after skb->data to start checksumming.
4280 * @off: the offset from start to place the checksum.
4281 *
4282 * For untrusted partially-checksummed packets, we need to make sure the values
4283 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4284 *
4285 * This function checks and sets those values and skb->ip_summed: if this
4286 * returns false you should drop the packet.
4287 */
4288bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4289{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004290 if (unlikely(start > skb_headlen(skb)) ||
4291 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004292 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4293 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004294 return false;
4295 }
4296 skb->ip_summed = CHECKSUM_PARTIAL;
4297 skb->csum_start = skb_headroom(skb) + start;
4298 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004299 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004300 return true;
4301}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004302EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004303
Paul Durranted1f50c2014-01-09 10:02:46 +00004304static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4305 unsigned int max)
4306{
4307 if (skb_headlen(skb) >= len)
4308 return 0;
4309
4310 /* If we need to pullup then pullup to the max, so we
4311 * won't need to do it again.
4312 */
4313 if (max > skb->len)
4314 max = skb->len;
4315
4316 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4317 return -ENOMEM;
4318
4319 if (skb_headlen(skb) < len)
4320 return -EPROTO;
4321
4322 return 0;
4323}
4324
Jan Beulichf9708b42014-03-11 13:56:05 +00004325#define MAX_TCP_HDR_LEN (15 * 4)
4326
4327static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4328 typeof(IPPROTO_IP) proto,
4329 unsigned int off)
4330{
4331 switch (proto) {
4332 int err;
4333
4334 case IPPROTO_TCP:
4335 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4336 off + MAX_TCP_HDR_LEN);
4337 if (!err && !skb_partial_csum_set(skb, off,
4338 offsetof(struct tcphdr,
4339 check)))
4340 err = -EPROTO;
4341 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4342
4343 case IPPROTO_UDP:
4344 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4345 off + sizeof(struct udphdr));
4346 if (!err && !skb_partial_csum_set(skb, off,
4347 offsetof(struct udphdr,
4348 check)))
4349 err = -EPROTO;
4350 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4351 }
4352
4353 return ERR_PTR(-EPROTO);
4354}
4355
Paul Durranted1f50c2014-01-09 10:02:46 +00004356/* This value should be large enough to cover a tagged ethernet header plus
4357 * maximally sized IP and TCP or UDP headers.
4358 */
4359#define MAX_IP_HDR_LEN 128
4360
Jan Beulichf9708b42014-03-11 13:56:05 +00004361static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004362{
4363 unsigned int off;
4364 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004365 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004366 int err;
4367
4368 fragment = false;
4369
4370 err = skb_maybe_pull_tail(skb,
4371 sizeof(struct iphdr),
4372 MAX_IP_HDR_LEN);
4373 if (err < 0)
4374 goto out;
4375
4376 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4377 fragment = true;
4378
4379 off = ip_hdrlen(skb);
4380
4381 err = -EPROTO;
4382
4383 if (fragment)
4384 goto out;
4385
Jan Beulichf9708b42014-03-11 13:56:05 +00004386 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4387 if (IS_ERR(csum))
4388 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004389
Jan Beulichf9708b42014-03-11 13:56:05 +00004390 if (recalculate)
4391 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4392 ip_hdr(skb)->daddr,
4393 skb->len - off,
4394 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004395 err = 0;
4396
4397out:
4398 return err;
4399}
4400
4401/* This value should be large enough to cover a tagged ethernet header plus
4402 * an IPv6 header, all options, and a maximal TCP or UDP header.
4403 */
4404#define MAX_IPV6_HDR_LEN 256
4405
4406#define OPT_HDR(type, skb, off) \
4407 (type *)(skb_network_header(skb) + (off))
4408
4409static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4410{
4411 int err;
4412 u8 nexthdr;
4413 unsigned int off;
4414 unsigned int len;
4415 bool fragment;
4416 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004417 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004418
4419 fragment = false;
4420 done = false;
4421
4422 off = sizeof(struct ipv6hdr);
4423
4424 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4425 if (err < 0)
4426 goto out;
4427
4428 nexthdr = ipv6_hdr(skb)->nexthdr;
4429
4430 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4431 while (off <= len && !done) {
4432 switch (nexthdr) {
4433 case IPPROTO_DSTOPTS:
4434 case IPPROTO_HOPOPTS:
4435 case IPPROTO_ROUTING: {
4436 struct ipv6_opt_hdr *hp;
4437
4438 err = skb_maybe_pull_tail(skb,
4439 off +
4440 sizeof(struct ipv6_opt_hdr),
4441 MAX_IPV6_HDR_LEN);
4442 if (err < 0)
4443 goto out;
4444
4445 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4446 nexthdr = hp->nexthdr;
4447 off += ipv6_optlen(hp);
4448 break;
4449 }
4450 case IPPROTO_AH: {
4451 struct ip_auth_hdr *hp;
4452
4453 err = skb_maybe_pull_tail(skb,
4454 off +
4455 sizeof(struct ip_auth_hdr),
4456 MAX_IPV6_HDR_LEN);
4457 if (err < 0)
4458 goto out;
4459
4460 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4461 nexthdr = hp->nexthdr;
4462 off += ipv6_authlen(hp);
4463 break;
4464 }
4465 case IPPROTO_FRAGMENT: {
4466 struct frag_hdr *hp;
4467
4468 err = skb_maybe_pull_tail(skb,
4469 off +
4470 sizeof(struct frag_hdr),
4471 MAX_IPV6_HDR_LEN);
4472 if (err < 0)
4473 goto out;
4474
4475 hp = OPT_HDR(struct frag_hdr, skb, off);
4476
4477 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4478 fragment = true;
4479
4480 nexthdr = hp->nexthdr;
4481 off += sizeof(struct frag_hdr);
4482 break;
4483 }
4484 default:
4485 done = true;
4486 break;
4487 }
4488 }
4489
4490 err = -EPROTO;
4491
4492 if (!done || fragment)
4493 goto out;
4494
Jan Beulichf9708b42014-03-11 13:56:05 +00004495 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4496 if (IS_ERR(csum))
4497 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004498
Jan Beulichf9708b42014-03-11 13:56:05 +00004499 if (recalculate)
4500 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4501 &ipv6_hdr(skb)->daddr,
4502 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004503 err = 0;
4504
4505out:
4506 return err;
4507}
4508
4509/**
4510 * skb_checksum_setup - set up partial checksum offset
4511 * @skb: the skb to set up
4512 * @recalculate: if true the pseudo-header checksum will be recalculated
4513 */
4514int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4515{
4516 int err;
4517
4518 switch (skb->protocol) {
4519 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004520 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004521 break;
4522
4523 case htons(ETH_P_IPV6):
4524 err = skb_checksum_setup_ipv6(skb, recalculate);
4525 break;
4526
4527 default:
4528 err = -EPROTO;
4529 break;
4530 }
4531
4532 return err;
4533}
4534EXPORT_SYMBOL(skb_checksum_setup);
4535
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004536/**
4537 * skb_checksum_maybe_trim - maybe trims the given skb
4538 * @skb: the skb to check
4539 * @transport_len: the data length beyond the network header
4540 *
4541 * Checks whether the given skb has data beyond the given transport length.
4542 * If so, returns a cloned skb trimmed to this transport length.
4543 * Otherwise returns the provided skb. Returns NULL in error cases
4544 * (e.g. transport_len exceeds skb length or out-of-memory).
4545 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004546 * Caller needs to set the skb transport header and free any returned skb if it
4547 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004548 */
4549static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4550 unsigned int transport_len)
4551{
4552 struct sk_buff *skb_chk;
4553 unsigned int len = skb_transport_offset(skb) + transport_len;
4554 int ret;
4555
Linus Lüssinga5169932015-08-13 05:54:07 +02004556 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004557 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004558 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004559 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004560
4561 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004562 if (!skb_chk)
4563 return NULL;
4564
4565 ret = pskb_trim_rcsum(skb_chk, len);
4566 if (ret) {
4567 kfree_skb(skb_chk);
4568 return NULL;
4569 }
4570
4571 return skb_chk;
4572}
4573
4574/**
4575 * skb_checksum_trimmed - validate checksum of an skb
4576 * @skb: the skb to check
4577 * @transport_len: the data length beyond the network header
4578 * @skb_chkf: checksum function to use
4579 *
4580 * Applies the given checksum function skb_chkf to the provided skb.
4581 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4582 *
4583 * If the skb has data beyond the given transport length, then a
4584 * trimmed & cloned skb is checked and returned.
4585 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004586 * Caller needs to set the skb transport header and free any returned skb if it
4587 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004588 */
4589struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4590 unsigned int transport_len,
4591 __sum16(*skb_chkf)(struct sk_buff *skb))
4592{
4593 struct sk_buff *skb_chk;
4594 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004595 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004596
4597 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4598 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004599 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004600
Linus Lüssinga5169932015-08-13 05:54:07 +02004601 if (!pskb_may_pull(skb_chk, offset))
4602 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004603
Linus Lüssing9b368812016-02-24 04:21:42 +01004604 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004605 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004606 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004607
Linus Lüssinga5169932015-08-13 05:54:07 +02004608 if (ret)
4609 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004610
4611 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004612
4613err:
4614 if (skb_chk && skb_chk != skb)
4615 kfree_skb(skb_chk);
4616
4617 return NULL;
4618
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004619}
4620EXPORT_SYMBOL(skb_checksum_trimmed);
4621
Ben Hutchings4497b072008-06-19 16:22:28 -07004622void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4623{
Joe Perchese87cc472012-05-13 21:56:26 +00004624 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4625 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004626}
Ben Hutchings4497b072008-06-19 16:22:28 -07004627EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004628
4629void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4630{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004631 if (head_stolen) {
4632 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004633 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004634 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004635 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004636 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004637}
4638EXPORT_SYMBOL(kfree_skb_partial);
4639
4640/**
4641 * skb_try_coalesce - try to merge skb to prior one
4642 * @to: prior buffer
4643 * @from: buffer to add
4644 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004645 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004646 */
4647bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4648 bool *fragstolen, int *delta_truesize)
4649{
4650 int i, delta, len = from->len;
4651
4652 *fragstolen = false;
4653
4654 if (skb_cloned(to))
4655 return false;
4656
4657 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004658 if (len)
4659 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004660 *delta_truesize = 0;
4661 return true;
4662 }
4663
4664 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4665 return false;
4666
4667 if (skb_headlen(from) != 0) {
4668 struct page *page;
4669 unsigned int offset;
4670
4671 if (skb_shinfo(to)->nr_frags +
4672 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4673 return false;
4674
4675 if (skb_head_is_locked(from))
4676 return false;
4677
4678 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4679
4680 page = virt_to_head_page(from->head);
4681 offset = from->data - (unsigned char *)page_address(page);
4682
4683 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4684 page, offset, skb_headlen(from));
4685 *fragstolen = true;
4686 } else {
4687 if (skb_shinfo(to)->nr_frags +
4688 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4689 return false;
4690
Weiping Panf4b549a2012-09-28 20:15:30 +00004691 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004692 }
4693
4694 WARN_ON_ONCE(delta < len);
4695
4696 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4697 skb_shinfo(from)->frags,
4698 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4699 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4700
4701 if (!skb_cloned(from))
4702 skb_shinfo(from)->nr_frags = 0;
4703
Li RongQing8ea853f2012-09-18 16:53:21 +00004704 /* if the skb is not cloned this does nothing
4705 * since we set nr_frags to 0.
4706 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004707 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4708 skb_frag_ref(from, i);
4709
4710 to->truesize += delta;
4711 to->len += len;
4712 to->data_len += len;
4713
4714 *delta_truesize = delta;
4715 return true;
4716}
4717EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004718
4719/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004720 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004721 *
4722 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004723 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004724 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004725 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4726 * into/from a tunnel. Some information have to be cleared during these
4727 * operations.
4728 * skb_scrub_packet can also be used to clean a skb before injecting it in
4729 * another namespace (@xnet == true). We have to clear all information in the
4730 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004731 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004732void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004733{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004734 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004735 skb->pkt_type = PACKET_HOST;
4736 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004737 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004738 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004739 secpath_reset(skb);
4740 nf_reset(skb);
4741 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004742
4743 if (!xnet)
4744 return;
4745
4746 skb_orphan(skb);
4747 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004748}
4749EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004750
4751/**
4752 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4753 *
4754 * @skb: GSO skb
4755 *
4756 * skb_gso_transport_seglen is used to determine the real size of the
4757 * individual segments, including Layer4 headers (TCP/UDP).
4758 *
4759 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4760 */
4761unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4762{
4763 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004764 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004765
Florian Westphalf993bc22014-10-20 13:49:18 +02004766 if (skb->encapsulation) {
4767 thlen = skb_inner_transport_header(skb) -
4768 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004769
Florian Westphalf993bc22014-10-20 13:49:18 +02004770 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4771 thlen += inner_tcp_hdrlen(skb);
4772 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4773 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004774 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4775 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004776 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004777 /* UFO sets gso_size to the size of the fragmentation
4778 * payload, i.e. the size of the L4 (UDP) header is already
4779 * accounted for.
4780 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004781 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004782}
4783EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004784
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004785/**
4786 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4787 *
4788 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004789 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004790 *
4791 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4792 * once split.
4793 */
4794bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4795{
4796 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4797 const struct sk_buff *iter;
4798 unsigned int hlen;
4799
4800 hlen = skb_gso_network_seglen(skb);
4801
4802 if (shinfo->gso_size != GSO_BY_FRAGS)
4803 return hlen <= mtu;
4804
4805 /* Undo this so we can re-use header sizes */
4806 hlen -= GSO_BY_FRAGS;
4807
4808 skb_walk_frags(skb, iter) {
4809 if (hlen + skb_headlen(iter) > mtu)
4810 return false;
4811 }
4812
4813 return true;
4814}
4815EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4816
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004817static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4818{
4819 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4820 kfree_skb(skb);
4821 return NULL;
4822 }
4823
Vlad Yasevichf6548612015-12-14 17:44:10 -05004824 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004825 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004826 skb->mac_header += VLAN_HLEN;
4827 return skb;
4828}
4829
4830struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4831{
4832 struct vlan_hdr *vhdr;
4833 u16 vlan_tci;
4834
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004835 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004836 /* vlan_tci is already set-up so leave this for another time */
4837 return skb;
4838 }
4839
4840 skb = skb_share_check(skb, GFP_ATOMIC);
4841 if (unlikely(!skb))
4842 goto err_free;
4843
4844 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4845 goto err_free;
4846
4847 vhdr = (struct vlan_hdr *)skb->data;
4848 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4849 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4850
4851 skb_pull_rcsum(skb, VLAN_HLEN);
4852 vlan_set_encap_proto(skb, vhdr);
4853
4854 skb = skb_reorder_vlan_header(skb);
4855 if (unlikely(!skb))
4856 goto err_free;
4857
4858 skb_reset_network_header(skb);
4859 skb_reset_transport_header(skb);
4860 skb_reset_mac_len(skb);
4861
4862 return skb;
4863
4864err_free:
4865 kfree_skb(skb);
4866 return NULL;
4867}
4868EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004869
Jiri Pirkoe2195122014-11-19 14:05:01 +01004870int skb_ensure_writable(struct sk_buff *skb, int write_len)
4871{
4872 if (!pskb_may_pull(skb, write_len))
4873 return -ENOMEM;
4874
4875 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4876 return 0;
4877
4878 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4879}
4880EXPORT_SYMBOL(skb_ensure_writable);
4881
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004882/* remove VLAN header from packet and update csum accordingly.
4883 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4884 */
4885int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004886{
4887 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004888 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004889 int err;
4890
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004891 if (WARN_ONCE(offset,
4892 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4893 offset)) {
4894 return -EINVAL;
4895 }
4896
Jiri Pirko93515d52014-11-19 14:05:02 +01004897 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4898 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004899 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004900
4901 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4902
4903 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4904 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4905
4906 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4907 __skb_pull(skb, VLAN_HLEN);
4908
4909 vlan_set_encap_proto(skb, vhdr);
4910 skb->mac_header += VLAN_HLEN;
4911
4912 if (skb_network_offset(skb) < ETH_HLEN)
4913 skb_set_network_header(skb, ETH_HLEN);
4914
4915 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004916
4917 return err;
4918}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004919EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004920
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004921/* Pop a vlan tag either from hwaccel or from payload.
4922 * Expects skb->data at mac header.
4923 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004924int skb_vlan_pop(struct sk_buff *skb)
4925{
4926 u16 vlan_tci;
4927 __be16 vlan_proto;
4928 int err;
4929
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004930 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004931 skb->vlan_tci = 0;
4932 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004933 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004934 return 0;
4935
4936 err = __skb_vlan_pop(skb, &vlan_tci);
4937 if (err)
4938 return err;
4939 }
4940 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004941 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004942 return 0;
4943
4944 vlan_proto = skb->protocol;
4945 err = __skb_vlan_pop(skb, &vlan_tci);
4946 if (unlikely(err))
4947 return err;
4948
4949 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4950 return 0;
4951}
4952EXPORT_SYMBOL(skb_vlan_pop);
4953
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004954/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4955 * Expects skb->data at mac header.
4956 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004957int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4958{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004959 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004960 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004961 int err;
4962
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004963 if (WARN_ONCE(offset,
4964 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4965 offset)) {
4966 return -EINVAL;
4967 }
4968
Jiri Pirko93515d52014-11-19 14:05:02 +01004969 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004970 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004971 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004972 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004973
Jiri Pirko93515d52014-11-19 14:05:02 +01004974 skb->protocol = skb->vlan_proto;
4975 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004976
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004977 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004978 }
4979 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4980 return 0;
4981}
4982EXPORT_SYMBOL(skb_vlan_push);
4983
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004984/**
4985 * alloc_skb_with_frags - allocate skb with page frags
4986 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004987 * @header_len: size of linear part
4988 * @data_len: needed length in frags
4989 * @max_page_order: max page order desired.
4990 * @errcode: pointer to error code if any
4991 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004992 *
4993 * This can be used to allocate a paged skb, given a maximal order for frags.
4994 */
4995struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4996 unsigned long data_len,
4997 int max_page_order,
4998 int *errcode,
4999 gfp_t gfp_mask)
5000{
5001 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5002 unsigned long chunk;
5003 struct sk_buff *skb;
5004 struct page *page;
5005 gfp_t gfp_head;
5006 int i;
5007
5008 *errcode = -EMSGSIZE;
5009 /* Note this test could be relaxed, if we succeed to allocate
5010 * high order pages...
5011 */
5012 if (npages > MAX_SKB_FRAGS)
5013 return NULL;
5014
5015 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08005016 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07005017 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005018
5019 *errcode = -ENOBUFS;
5020 skb = alloc_skb(header_len, gfp_head);
5021 if (!skb)
5022 return NULL;
5023
5024 skb->truesize += npages << PAGE_SHIFT;
5025
5026 for (i = 0; npages > 0; i++) {
5027 int order = max_page_order;
5028
5029 while (order) {
5030 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005031 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005032 __GFP_COMP |
5033 __GFP_NOWARN |
5034 __GFP_NORETRY,
5035 order);
5036 if (page)
5037 goto fill_page;
5038 /* Do not retry other high order allocations */
5039 order = 1;
5040 max_page_order = 0;
5041 }
5042 order--;
5043 }
5044 page = alloc_page(gfp_mask);
5045 if (!page)
5046 goto failure;
5047fill_page:
5048 chunk = min_t(unsigned long, data_len,
5049 PAGE_SIZE << order);
5050 skb_fill_page_desc(skb, i, page, 0, chunk);
5051 data_len -= chunk;
5052 npages -= 1 << order;
5053 }
5054 return skb;
5055
5056failure:
5057 kfree_skb(skb);
5058 return NULL;
5059}
5060EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005061
5062/* carve out the first off bytes from skb when off < headlen */
5063static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5064 const int headlen, gfp_t gfp_mask)
5065{
5066 int i;
5067 int size = skb_end_offset(skb);
5068 int new_hlen = headlen - off;
5069 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005070
5071 size = SKB_DATA_ALIGN(size);
5072
5073 if (skb_pfmemalloc(skb))
5074 gfp_mask |= __GFP_MEMALLOC;
5075 data = kmalloc_reserve(size +
5076 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5077 gfp_mask, NUMA_NO_NODE, NULL);
5078 if (!data)
5079 return -ENOMEM;
5080
5081 size = SKB_WITH_OVERHEAD(ksize(data));
5082
5083 /* Copy real data, and all frags */
5084 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5085 skb->len -= off;
5086
5087 memcpy((struct skb_shared_info *)(data + size),
5088 skb_shinfo(skb),
5089 offsetof(struct skb_shared_info,
5090 frags[skb_shinfo(skb)->nr_frags]));
5091 if (skb_cloned(skb)) {
5092 /* drop the old head gracefully */
5093 if (skb_orphan_frags(skb, gfp_mask)) {
5094 kfree(data);
5095 return -ENOMEM;
5096 }
5097 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5098 skb_frag_ref(skb, i);
5099 if (skb_has_frag_list(skb))
5100 skb_clone_fraglist(skb);
5101 skb_release_data(skb);
5102 } else {
5103 /* we can reuse existing recount- all we did was
5104 * relocate values
5105 */
5106 skb_free_head(skb);
5107 }
5108
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005109 skb->head = data;
5110 skb->data = data;
5111 skb->head_frag = 0;
5112#ifdef NET_SKBUFF_DATA_USES_OFFSET
5113 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005114#else
5115 skb->end = skb->head + size;
5116#endif
5117 skb_set_tail_pointer(skb, skb_headlen(skb));
5118 skb_headers_offset_update(skb, 0);
5119 skb->cloned = 0;
5120 skb->hdr_len = 0;
5121 skb->nohdr = 0;
5122 atomic_set(&skb_shinfo(skb)->dataref, 1);
5123
5124 return 0;
5125}
5126
5127static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5128
5129/* carve out the first eat bytes from skb's frag_list. May recurse into
5130 * pskb_carve()
5131 */
5132static int pskb_carve_frag_list(struct sk_buff *skb,
5133 struct skb_shared_info *shinfo, int eat,
5134 gfp_t gfp_mask)
5135{
5136 struct sk_buff *list = shinfo->frag_list;
5137 struct sk_buff *clone = NULL;
5138 struct sk_buff *insp = NULL;
5139
5140 do {
5141 if (!list) {
5142 pr_err("Not enough bytes to eat. Want %d\n", eat);
5143 return -EFAULT;
5144 }
5145 if (list->len <= eat) {
5146 /* Eaten as whole. */
5147 eat -= list->len;
5148 list = list->next;
5149 insp = list;
5150 } else {
5151 /* Eaten partially. */
5152 if (skb_shared(list)) {
5153 clone = skb_clone(list, gfp_mask);
5154 if (!clone)
5155 return -ENOMEM;
5156 insp = list->next;
5157 list = clone;
5158 } else {
5159 /* This may be pulled without problems. */
5160 insp = list;
5161 }
5162 if (pskb_carve(list, eat, gfp_mask) < 0) {
5163 kfree_skb(clone);
5164 return -ENOMEM;
5165 }
5166 break;
5167 }
5168 } while (eat);
5169
5170 /* Free pulled out fragments. */
5171 while ((list = shinfo->frag_list) != insp) {
5172 shinfo->frag_list = list->next;
5173 kfree_skb(list);
5174 }
5175 /* And insert new clone at head. */
5176 if (clone) {
5177 clone->next = list;
5178 shinfo->frag_list = clone;
5179 }
5180 return 0;
5181}
5182
5183/* carve off first len bytes from skb. Split line (off) is in the
5184 * non-linear part of skb
5185 */
5186static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5187 int pos, gfp_t gfp_mask)
5188{
5189 int i, k = 0;
5190 int size = skb_end_offset(skb);
5191 u8 *data;
5192 const int nfrags = skb_shinfo(skb)->nr_frags;
5193 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005194
5195 size = SKB_DATA_ALIGN(size);
5196
5197 if (skb_pfmemalloc(skb))
5198 gfp_mask |= __GFP_MEMALLOC;
5199 data = kmalloc_reserve(size +
5200 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5201 gfp_mask, NUMA_NO_NODE, NULL);
5202 if (!data)
5203 return -ENOMEM;
5204
5205 size = SKB_WITH_OVERHEAD(ksize(data));
5206
5207 memcpy((struct skb_shared_info *)(data + size),
5208 skb_shinfo(skb), offsetof(struct skb_shared_info,
5209 frags[skb_shinfo(skb)->nr_frags]));
5210 if (skb_orphan_frags(skb, gfp_mask)) {
5211 kfree(data);
5212 return -ENOMEM;
5213 }
5214 shinfo = (struct skb_shared_info *)(data + size);
5215 for (i = 0; i < nfrags; i++) {
5216 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5217
5218 if (pos + fsize > off) {
5219 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5220
5221 if (pos < off) {
5222 /* Split frag.
5223 * We have two variants in this case:
5224 * 1. Move all the frag to the second
5225 * part, if it is possible. F.e.
5226 * this approach is mandatory for TUX,
5227 * where splitting is expensive.
5228 * 2. Split is accurately. We make this.
5229 */
5230 shinfo->frags[0].page_offset += off - pos;
5231 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5232 }
5233 skb_frag_ref(skb, i);
5234 k++;
5235 }
5236 pos += fsize;
5237 }
5238 shinfo->nr_frags = k;
5239 if (skb_has_frag_list(skb))
5240 skb_clone_fraglist(skb);
5241
5242 if (k == 0) {
5243 /* split line is in frag list */
5244 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5245 }
5246 skb_release_data(skb);
5247
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005248 skb->head = data;
5249 skb->head_frag = 0;
5250 skb->data = data;
5251#ifdef NET_SKBUFF_DATA_USES_OFFSET
5252 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005253#else
5254 skb->end = skb->head + size;
5255#endif
5256 skb_reset_tail_pointer(skb);
5257 skb_headers_offset_update(skb, 0);
5258 skb->cloned = 0;
5259 skb->hdr_len = 0;
5260 skb->nohdr = 0;
5261 skb->len -= off;
5262 skb->data_len = skb->len;
5263 atomic_set(&skb_shinfo(skb)->dataref, 1);
5264 return 0;
5265}
5266
5267/* remove len bytes from the beginning of the skb */
5268static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5269{
5270 int headlen = skb_headlen(skb);
5271
5272 if (len < headlen)
5273 return pskb_carve_inside_header(skb, len, headlen, gfp);
5274 else
5275 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5276}
5277
5278/* Extract to_copy bytes starting at off from skb, and return this in
5279 * a new skb
5280 */
5281struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5282 int to_copy, gfp_t gfp)
5283{
5284 struct sk_buff *clone = skb_clone(skb, gfp);
5285
5286 if (!clone)
5287 return NULL;
5288
5289 if (pskb_carve(clone, off, gfp) < 0 ||
5290 pskb_trim(clone, to_copy)) {
5291 kfree_skb(clone);
5292 return NULL;
5293 }
5294 return clone;
5295}
5296EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005297
5298/**
5299 * skb_condense - try to get rid of fragments/frag_list if possible
5300 * @skb: buffer
5301 *
5302 * Can be used to save memory before skb is added to a busy queue.
5303 * If packet has bytes in frags and enough tail room in skb->head,
5304 * pull all of them, so that we can free the frags right now and adjust
5305 * truesize.
5306 * Notes:
5307 * We do not reallocate skb->head thus can not fail.
5308 * Caller must re-evaluate skb->truesize if needed.
5309 */
5310void skb_condense(struct sk_buff *skb)
5311{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005312 if (skb->data_len) {
5313 if (skb->data_len > skb->end - skb->tail ||
5314 skb_cloned(skb))
5315 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005316
Eric Dumazet3174fed2016-12-09 08:02:05 -08005317 /* Nice, we can free page frag(s) right now */
5318 __pskb_pull_tail(skb, skb->data_len);
5319 }
5320 /* At this point, skb->truesize might be over estimated,
5321 * because skb had a fragment, and fragments do not tell
5322 * their truesize.
5323 * When we pulled its content into skb->head, fragment
5324 * was freed, but __pskb_pull_tail() could not possibly
5325 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005326 */
5327 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5328}