blob: c27da51d14e4903937b34efe839999cfca2e049e [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
Ben Hutchings2c530402012-07-10 10:55:09 +0000918/**
919 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000920 * @skb: the skb to modify
921 * @gfp_mask: allocation priority
922 *
923 * This must be called on SKBTX_DEV_ZEROCOPY skb.
924 * It will copy all frags into kernel and drop the reference
925 * to userspace pages.
926 *
927 * If this function is called from an interrupt gfp_mask() must be
928 * %GFP_ATOMIC.
929 *
930 * Returns 0 on success or a negative error code on failure
931 * to allocate kernel memory to copy to.
932 */
933int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000934{
935 int i;
936 int num_frags = skb_shinfo(skb)->nr_frags;
937 struct page *page, *head = NULL;
938 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
939
940 for (i = 0; i < num_frags; i++) {
941 u8 *vaddr;
942 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
943
Krishna Kumar02756ed2012-07-17 02:05:29 +0000944 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000945 if (!page) {
946 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900947 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000948 put_page(head);
949 head = next;
950 }
951 return -ENOMEM;
952 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200953 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000954 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000955 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200956 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900957 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000958 head = page;
959 }
960
961 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000962 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000963 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000964
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000965 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000966
967 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000968 for (i = num_frags - 1; i >= 0; i--) {
969 __skb_fill_page_desc(skb, i, head, 0,
970 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900971 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000972 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000973
974 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000975 return 0;
976}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000977EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000978
Herbert Xue0053ec2007-10-14 00:37:52 -0700979/**
980 * skb_clone - duplicate an sk_buff
981 * @skb: buffer to clone
982 * @gfp_mask: allocation priority
983 *
984 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
985 * copies share the same packet data but not structure. The new
986 * buffer has a reference count of 1. If the allocation fails the
987 * function returns %NULL otherwise the new buffer is returned.
988 *
989 * If this function is called from an interrupt gfp_mask() must be
990 * %GFP_ATOMIC.
991 */
992
993struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
994{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700995 struct sk_buff_fclones *fclones = container_of(skb,
996 struct sk_buff_fclones,
997 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800998 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700999
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001000 if (skb_orphan_frags(skb, gfp_mask))
1001 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001002
Herbert Xue0053ec2007-10-14 00:37:52 -07001003 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001004 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001005 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001006 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001007 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001008 if (skb_pfmemalloc(skb))
1009 gfp_mask |= __GFP_MEMALLOC;
1010
Herbert Xue0053ec2007-10-14 00:37:52 -07001011 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1012 if (!n)
1013 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001014
1015 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001016 n->fclone = SKB_FCLONE_UNAVAILABLE;
1017 }
1018
1019 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001021EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001023static void skb_headers_offset_update(struct sk_buff *skb, int off)
1024{
Eric Dumazet030737b2013-10-19 11:42:54 -07001025 /* Only adjust this if it actually is csum_start rather than csum */
1026 if (skb->ip_summed == CHECKSUM_PARTIAL)
1027 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001028 /* {transport,network,mac}_header and tail are relative to skb->head */
1029 skb->transport_header += off;
1030 skb->network_header += off;
1031 if (skb_mac_header_was_set(skb))
1032 skb->mac_header += off;
1033 skb->inner_transport_header += off;
1034 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001035 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1039{
Herbert Xudec18812007-10-14 00:37:30 -07001040 __copy_skb_header(new, old);
1041
Herbert Xu79671682006-06-22 02:40:14 -07001042 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1043 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1044 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Mel Gormanc93bdd02012-07-31 16:44:19 -07001047static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1048{
1049 if (skb_pfmemalloc(skb))
1050 return SKB_ALLOC_RX;
1051 return 0;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
1055 * skb_copy - create private copy of an sk_buff
1056 * @skb: buffer to copy
1057 * @gfp_mask: allocation priority
1058 *
1059 * Make a copy of both an &sk_buff and its data. This is used when the
1060 * caller wishes to modify the data and needs a private copy of the
1061 * data to alter. Returns %NULL on failure or the pointer to the buffer
1062 * on success. The returned buffer has a reference count of 1.
1063 *
1064 * As by-product this function converts non-linear &sk_buff to linear
1065 * one, so that &sk_buff becomes completely private and caller is allowed
1066 * to modify all the data of returned buffer. This means that this
1067 * function is not recommended for use in circumstances when only
1068 * header is going to be modified. Use pskb_copy() instead.
1069 */
1070
Al Virodd0fc662005-10-07 07:46:04 +01001071struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001073 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001074 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001075 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1076 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!n)
1079 return NULL;
1080
1081 /* Set the data pointer */
1082 skb_reserve(n, headerlen);
1083 /* Set the tail pointer and length */
1084 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1087 BUG();
1088
1089 copy_skb_header(n, skb);
1090 return n;
1091}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001092EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001095 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001097 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001099 * @fclone: if true allocate the copy of the skb from the fclone
1100 * cache instead of the head cache; it is recommended to set this
1101 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 *
1103 * Make a copy of both an &sk_buff and part of its data, located
1104 * in header. Fragmented data remain shared. This is used when
1105 * the caller wishes to modify only header of &sk_buff and needs
1106 * private copy of the header to alter. Returns %NULL on failure
1107 * or the pointer to the buffer on success.
1108 * The returned buffer has a reference count of 1.
1109 */
1110
Octavian Purdilabad93e92014-06-12 01:36:26 +03001111struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1112 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Eric Dumazet117632e2011-12-03 21:39:53 +00001114 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001115 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1116 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!n)
1119 goto out;
1120
1121 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001122 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Set the tail pointer and length */
1124 skb_put(n, skb_headlen(skb));
1125 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001126 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Herbert Xu25f484a2006-11-07 14:57:15 -08001128 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 n->data_len = skb->data_len;
1130 n->len = skb->len;
1131
1132 if (skb_shinfo(skb)->nr_frags) {
1133 int i;
1134
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001135 if (skb_orphan_frags(skb, gfp_mask)) {
1136 kfree_skb(n);
1137 n = NULL;
1138 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1141 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001142 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 skb_shinfo(n)->nr_frags = i;
1145 }
1146
David S. Miller21dc3302010-08-23 00:13:46 -07001147 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1149 skb_clone_fraglist(n);
1150 }
1151
1152 copy_skb_header(n, skb);
1153out:
1154 return n;
1155}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001156EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158/**
1159 * pskb_expand_head - reallocate header of &sk_buff
1160 * @skb: buffer to reallocate
1161 * @nhead: room to add at head
1162 * @ntail: room to add at tail
1163 * @gfp_mask: allocation priority
1164 *
Mathias Krausebc323832013-11-07 14:18:26 +01001165 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1166 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * reference count of 1. Returns zero in the case of success or error,
1168 * if expansion failed. In the last case, &sk_buff is not changed.
1169 *
1170 * All the pointers pointing into skb header may change and must be
1171 * reloaded after call to this function.
1172 */
1173
Victor Fusco86a76ca2005-07-08 14:57:47 -07001174int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001175 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Eric Dumazet158f3232017-01-27 07:11:27 -08001177 int i, osize = skb_end_offset(skb);
1178 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001180 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Herbert Xu4edd87a2008-10-01 07:09:38 -07001182 BUG_ON(nhead < 0);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (skb_shared(skb))
1185 BUG();
1186
1187 size = SKB_DATA_ALIGN(size);
1188
Mel Gormanc93bdd02012-07-31 16:44:19 -07001189 if (skb_pfmemalloc(skb))
1190 gfp_mask |= __GFP_MEMALLOC;
1191 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1192 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!data)
1194 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001195 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001198 * optimized for the cases when header is void.
1199 */
1200 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1201
1202 memcpy((struct skb_shared_info *)(data + size),
1203 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001204 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Alexander Duyck3e245912012-05-04 14:26:51 +00001206 /*
1207 * if shinfo is shared we must drop the old head gracefully, but if it
1208 * is not we can just drop the old head and let the existing refcount
1209 * be since all we did is relocate the values
1210 */
1211 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001212 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001213 if (skb_orphan_frags(skb, gfp_mask))
1214 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001215 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001216 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric Dumazet1fd63042010-09-02 23:09:32 +00001218 if (skb_has_frag_list(skb))
1219 skb_clone_fraglist(skb);
1220
1221 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001222 } else {
1223 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 off = (data + nhead) - skb->head;
1226
1227 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001228 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001230#ifdef NET_SKBUFF_DATA_USES_OFFSET
1231 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001232 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001233#else
1234 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001235#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001236 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001237 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001239 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 skb->nohdr = 0;
1241 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001242
1243 /* It is not generally safe to change skb->truesize.
1244 * For the moment, we really care of rx path, or
1245 * when skb is orphaned (not attached to a socket).
1246 */
1247 if (!skb->sk || skb->destructor == sock_edemux)
1248 skb->truesize += size - osize;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251
Shirley Maa6686f22011-07-06 12:22:12 +00001252nofrags:
1253 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254nodata:
1255 return -ENOMEM;
1256}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001257EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259/* Make private copy of skb with writable head and some headroom */
1260
1261struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1262{
1263 struct sk_buff *skb2;
1264 int delta = headroom - skb_headroom(skb);
1265
1266 if (delta <= 0)
1267 skb2 = pskb_copy(skb, GFP_ATOMIC);
1268 else {
1269 skb2 = skb_clone(skb, GFP_ATOMIC);
1270 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1271 GFP_ATOMIC)) {
1272 kfree_skb(skb2);
1273 skb2 = NULL;
1274 }
1275 }
1276 return skb2;
1277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001278EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280/**
1281 * skb_copy_expand - copy and expand sk_buff
1282 * @skb: buffer to copy
1283 * @newheadroom: new free bytes at head
1284 * @newtailroom: new free bytes at tail
1285 * @gfp_mask: allocation priority
1286 *
1287 * Make a copy of both an &sk_buff and its data and while doing so
1288 * allocate additional space.
1289 *
1290 * This is used when the caller wishes to modify the data and needs a
1291 * private copy of the data to alter as well as more space for new fields.
1292 * Returns %NULL on failure or the pointer to the buffer
1293 * on success. The returned buffer has a reference count of 1.
1294 *
1295 * You must pass %GFP_ATOMIC as the allocation priority if this function
1296 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001299 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001300 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 /*
1303 * Allocate the copy buffer
1304 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001305 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1306 gfp_mask, skb_alloc_rx_flag(skb),
1307 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001308 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int head_copy_len, head_copy_off;
1310
1311 if (!n)
1312 return NULL;
1313
1314 skb_reserve(n, newheadroom);
1315
1316 /* Set the tail pointer and length */
1317 skb_put(n, skb->len);
1318
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001319 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 head_copy_off = 0;
1321 if (newheadroom <= head_copy_len)
1322 head_copy_len = newheadroom;
1323 else
1324 head_copy_off = newheadroom - head_copy_len;
1325
1326 /* Copy the linear header and data. */
1327 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1328 skb->len + head_copy_len))
1329 BUG();
1330
1331 copy_skb_header(n, skb);
1332
Eric Dumazet030737b2013-10-19 11:42:54 -07001333 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return n;
1336}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001337EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/**
1340 * skb_pad - zero pad the tail of an skb
1341 * @skb: buffer to pad
1342 * @pad: space to pad
1343 *
1344 * Ensure that a buffer is followed by a padding area that is zero
1345 * filled. Used by network drivers which may DMA or transfer data
1346 * beyond the buffer end onto the wire.
1347 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001348 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001350
Herbert Xu5b057c62006-06-23 02:06:41 -07001351int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Herbert Xu5b057c62006-06-23 02:06:41 -07001353 int err;
1354 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001357 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001361
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001362 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001363 if (likely(skb_cloned(skb) || ntail > 0)) {
1364 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1365 if (unlikely(err))
1366 goto free_skb;
1367 }
1368
1369 /* FIXME: The use of this function with non-linear skb's really needs
1370 * to be audited.
1371 */
1372 err = skb_linearize(skb);
1373 if (unlikely(err))
1374 goto free_skb;
1375
1376 memset(skb->data + skb->len, 0, pad);
1377 return 0;
1378
1379free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001381 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001382}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001383EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001384
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001385/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001386 * pskb_put - add data to the tail of a potentially fragmented buffer
1387 * @skb: start of the buffer to use
1388 * @tail: tail fragment of the buffer to use
1389 * @len: amount of data to add
1390 *
1391 * This function extends the used data area of the potentially
1392 * fragmented buffer. @tail must be the last fragment of @skb -- or
1393 * @skb itself. If this would exceed the total buffer size the kernel
1394 * will panic. A pointer to the first byte of the extra data is
1395 * returned.
1396 */
1397
Johannes Berg4df864c2017-06-16 14:29:21 +02001398void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001399{
1400 if (tail != skb) {
1401 skb->data_len += len;
1402 skb->len += len;
1403 }
1404 return skb_put(tail, len);
1405}
1406EXPORT_SYMBOL_GPL(pskb_put);
1407
1408/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001409 * skb_put - add data to a buffer
1410 * @skb: buffer to use
1411 * @len: amount of data to add
1412 *
1413 * This function extends the used data area of the buffer. If this would
1414 * exceed the total buffer size the kernel will panic. A pointer to the
1415 * first byte of the extra data is returned.
1416 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001417void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001418{
Johannes Berg4df864c2017-06-16 14:29:21 +02001419 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001420 SKB_LINEAR_ASSERT(skb);
1421 skb->tail += len;
1422 skb->len += len;
1423 if (unlikely(skb->tail > skb->end))
1424 skb_over_panic(skb, len, __builtin_return_address(0));
1425 return tmp;
1426}
1427EXPORT_SYMBOL(skb_put);
1428
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001429/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001430 * skb_push - add data to the start of a buffer
1431 * @skb: buffer to use
1432 * @len: amount of data to add
1433 *
1434 * This function extends the used data area of the buffer at the buffer
1435 * start. If this would exceed the total buffer headroom the kernel will
1436 * panic. A pointer to the first byte of the extra data is returned.
1437 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001438void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001439{
1440 skb->data -= len;
1441 skb->len += len;
1442 if (unlikely(skb->data<skb->head))
1443 skb_under_panic(skb, len, __builtin_return_address(0));
1444 return skb->data;
1445}
1446EXPORT_SYMBOL(skb_push);
1447
1448/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001449 * skb_pull - remove data from the start of a buffer
1450 * @skb: buffer to use
1451 * @len: amount of data to remove
1452 *
1453 * This function removes data from the start of a buffer, returning
1454 * the memory to the headroom. A pointer to the next data in the buffer
1455 * is returned. Once the data has been pulled future pushes will overwrite
1456 * the old data.
1457 */
Johannes Bergaf728682017-06-16 14:29:22 +02001458void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001459{
David S. Miller47d29642010-05-02 02:21:44 -07001460 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001461}
1462EXPORT_SYMBOL(skb_pull);
1463
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001464/**
1465 * skb_trim - remove end from a buffer
1466 * @skb: buffer to alter
1467 * @len: new length
1468 *
1469 * Cut the length of a buffer down by removing data from the tail. If
1470 * the buffer is already under the length specified it is not modified.
1471 * The skb must be linear.
1472 */
1473void skb_trim(struct sk_buff *skb, unsigned int len)
1474{
1475 if (skb->len > len)
1476 __skb_trim(skb, len);
1477}
1478EXPORT_SYMBOL(skb_trim);
1479
Herbert Xu3cc0e872006-06-09 16:13:38 -07001480/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
1482
Herbert Xu3cc0e872006-06-09 16:13:38 -07001483int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Herbert Xu27b437c2006-07-13 19:26:39 -07001485 struct sk_buff **fragp;
1486 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int offset = skb_headlen(skb);
1488 int nfrags = skb_shinfo(skb)->nr_frags;
1489 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001490 int err;
1491
1492 if (skb_cloned(skb) &&
1493 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001496 i = 0;
1497 if (offset >= len)
1498 goto drop_pages;
1499
1500 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001501 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001502
1503 if (end < len) {
1504 offset = end;
1505 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001507
Eric Dumazet9e903e02011-10-18 21:00:24 +00001508 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001509
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001510drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001511 skb_shinfo(skb)->nr_frags = i;
1512
1513 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001514 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001515
David S. Miller21dc3302010-08-23 00:13:46 -07001516 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001517 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001518 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
Herbert Xu27b437c2006-07-13 19:26:39 -07001521 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1522 fragp = &frag->next) {
1523 int end = offset + frag->len;
1524
1525 if (skb_shared(frag)) {
1526 struct sk_buff *nfrag;
1527
1528 nfrag = skb_clone(frag, GFP_ATOMIC);
1529 if (unlikely(!nfrag))
1530 return -ENOMEM;
1531
1532 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001533 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001534 frag = nfrag;
1535 *fragp = frag;
1536 }
1537
1538 if (end < len) {
1539 offset = end;
1540 continue;
1541 }
1542
1543 if (end > len &&
1544 unlikely((err = pskb_trim(frag, len - offset))))
1545 return err;
1546
1547 if (frag->next)
1548 skb_drop_list(&frag->next);
1549 break;
1550 }
1551
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001552done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001553 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 skb->data_len -= skb->len - len;
1555 skb->len = len;
1556 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001557 skb->len = len;
1558 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001559 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001562 if (!skb->sk || skb->destructor == sock_edemux)
1563 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001566EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/**
1569 * __pskb_pull_tail - advance tail of skb header
1570 * @skb: buffer to reallocate
1571 * @delta: number of bytes to advance tail
1572 *
1573 * The function makes a sense only on a fragmented &sk_buff,
1574 * it expands header moving its tail forward and copying necessary
1575 * data from fragmented part.
1576 *
1577 * &sk_buff MUST have reference count of 1.
1578 *
1579 * Returns %NULL (and &sk_buff does not change) if pull failed
1580 * or value of new tail of skb in the case of success.
1581 *
1582 * All the pointers pointing into skb header may change and must be
1583 * reloaded after call to this function.
1584 */
1585
1586/* Moves tail of skb head forward, copying data from fragmented part,
1587 * when it is necessary.
1588 * 1. It may fail due to malloc failure.
1589 * 2. It may change skb pointers.
1590 *
1591 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1592 */
Johannes Bergaf728682017-06-16 14:29:22 +02001593void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 /* If skb has not enough free space at tail, get new one
1596 * plus 128 bytes for future expansions. If we have enough
1597 * room at tail, reallocate without expansion only if skb is cloned.
1598 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001599 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (eat > 0 || skb_cloned(skb)) {
1602 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1603 GFP_ATOMIC))
1604 return NULL;
1605 }
1606
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001607 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 BUG();
1609
1610 /* Optimization: no fragments, no reasons to preestimate
1611 * size of pulled pages. Superb.
1612 */
David S. Miller21dc3302010-08-23 00:13:46 -07001613 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto pull_pages;
1615
1616 /* Estimate size of pulled pages. */
1617 eat = delta;
1618 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001619 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1620
1621 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001623 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /* If we need update frag list, we are in troubles.
1627 * Certainly, it possible to add an offset to skb data,
1628 * but taking into account that pulling is expected to
1629 * be very rare operation, it is worth to fight against
1630 * further bloating skb head and crucify ourselves here instead.
1631 * Pure masohism, indeed. 8)8)
1632 */
1633 if (eat) {
1634 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1635 struct sk_buff *clone = NULL;
1636 struct sk_buff *insp = NULL;
1637
1638 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001639 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (list->len <= eat) {
1642 /* Eaten as whole. */
1643 eat -= list->len;
1644 list = list->next;
1645 insp = list;
1646 } else {
1647 /* Eaten partially. */
1648
1649 if (skb_shared(list)) {
1650 /* Sucks! We need to fork list. :-( */
1651 clone = skb_clone(list, GFP_ATOMIC);
1652 if (!clone)
1653 return NULL;
1654 insp = list->next;
1655 list = clone;
1656 } else {
1657 /* This may be pulled without
1658 * problems. */
1659 insp = list;
1660 }
1661 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001662 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return NULL;
1664 }
1665 break;
1666 }
1667 } while (eat);
1668
1669 /* Free pulled out fragments. */
1670 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1671 skb_shinfo(skb)->frag_list = list->next;
1672 kfree_skb(list);
1673 }
1674 /* And insert new clone at head. */
1675 if (clone) {
1676 clone->next = list;
1677 skb_shinfo(skb)->frag_list = clone;
1678 }
1679 }
1680 /* Success! Now we may commit changes to skb data. */
1681
1682pull_pages:
1683 eat = delta;
1684 k = 0;
1685 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001686 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1687
1688 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001689 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001690 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else {
1692 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1693 if (eat) {
1694 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001695 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001696 if (!i)
1697 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 eat = 0;
1699 }
1700 k++;
1701 }
1702 }
1703 skb_shinfo(skb)->nr_frags = k;
1704
linzhang3ccc6c62017-07-17 17:25:02 +08001705end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 skb->tail += delta;
1707 skb->data_len -= delta;
1708
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001709 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001711EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Eric Dumazet22019b12011-07-29 18:37:31 +00001713/**
1714 * skb_copy_bits - copy bits from skb to kernel buffer
1715 * @skb: source skb
1716 * @offset: offset in source
1717 * @to: destination buffer
1718 * @len: number of bytes to copy
1719 *
1720 * Copy the specified number of bytes from the source skb to the
1721 * destination buffer.
1722 *
1723 * CAUTION ! :
1724 * If its prototype is ever changed,
1725 * check arch/{*}/net/{*}.S files,
1726 * since it is called from BPF assembly code.
1727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1729{
David S. Miller1a028e52007-04-27 15:21:23 -07001730 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001731 struct sk_buff *frag_iter;
1732 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 if (offset > (int)skb->len - len)
1735 goto fault;
1736
1737 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001738 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (copy > len)
1740 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001741 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if ((len -= copy) == 0)
1743 return 0;
1744 offset += copy;
1745 to += copy;
1746 }
1747
1748 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001749 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001750 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001752 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001753
Eric Dumazet51c56b02012-04-05 11:35:15 +02001754 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if ((copy = end - offset) > 0) {
1756 u8 *vaddr;
1757
1758 if (copy > len)
1759 copy = len;
1760
Eric Dumazet51c56b02012-04-05 11:35:15 +02001761 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001763 vaddr + f->page_offset + offset - start,
1764 copy);
1765 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if ((len -= copy) == 0)
1768 return 0;
1769 offset += copy;
1770 to += copy;
1771 }
David S. Miller1a028e52007-04-27 15:21:23 -07001772 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
David S. Millerfbb398a2009-06-09 00:18:59 -07001775 skb_walk_frags(skb, frag_iter) {
1776 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David S. Millerfbb398a2009-06-09 00:18:59 -07001778 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
David S. Millerfbb398a2009-06-09 00:18:59 -07001780 end = start + frag_iter->len;
1781 if ((copy = end - offset) > 0) {
1782 if (copy > len)
1783 copy = len;
1784 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1785 goto fault;
1786 if ((len -= copy) == 0)
1787 return 0;
1788 offset += copy;
1789 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001791 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Shirley Maa6686f22011-07-06 12:22:12 +00001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (!len)
1795 return 0;
1796
1797fault:
1798 return -EFAULT;
1799}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001800EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Jens Axboe9c55e012007-11-06 23:30:13 -08001802/*
1803 * Callback from splice_to_pipe(), if we need to release some pages
1804 * at the end of the spd in case we error'ed out in filling the pipe.
1805 */
1806static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1807{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001808 put_page(spd->pages[i]);
1809}
Jens Axboe9c55e012007-11-06 23:30:13 -08001810
David S. Millera108d5f2012-04-23 23:06:11 -04001811static struct page *linear_to_page(struct page *page, unsigned int *len,
1812 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001813 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001814{
Eric Dumazet5640f762012-09-23 23:04:42 +00001815 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001816
Eric Dumazet5640f762012-09-23 23:04:42 +00001817 if (!sk_page_frag_refill(sk, pfrag))
1818 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001819
Eric Dumazet5640f762012-09-23 23:04:42 +00001820 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001821
Eric Dumazet5640f762012-09-23 23:04:42 +00001822 memcpy(page_address(pfrag->page) + pfrag->offset,
1823 page_address(page) + *offset, *len);
1824 *offset = pfrag->offset;
1825 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001826
Eric Dumazet5640f762012-09-23 23:04:42 +00001827 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001828}
1829
Eric Dumazet41c73a02012-04-22 12:26:16 +00001830static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1831 struct page *page,
1832 unsigned int offset)
1833{
1834 return spd->nr_pages &&
1835 spd->pages[spd->nr_pages - 1] == page &&
1836 (spd->partial[spd->nr_pages - 1].offset +
1837 spd->partial[spd->nr_pages - 1].len == offset);
1838}
1839
Jens Axboe9c55e012007-11-06 23:30:13 -08001840/*
1841 * Fill page/offset/length into spd, if it can hold more pages.
1842 */
David S. Millera108d5f2012-04-23 23:06:11 -04001843static bool spd_fill_page(struct splice_pipe_desc *spd,
1844 struct pipe_inode_info *pipe, struct page *page,
1845 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001846 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001847 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001848{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001849 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001850 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001851
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001852 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001853 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001854 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001855 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001856 }
1857 if (spd_can_coalesce(spd, page, offset)) {
1858 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001859 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001860 }
1861 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001862 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001863 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001864 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001865 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001866
David S. Millera108d5f2012-04-23 23:06:11 -04001867 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001868}
1869
David S. Millera108d5f2012-04-23 23:06:11 -04001870static bool __splice_segment(struct page *page, unsigned int poff,
1871 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001872 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001873 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001874 struct sock *sk,
1875 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001876{
1877 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001878 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001879
1880 /* skip this segment if already processed */
1881 if (*off >= plen) {
1882 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001883 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001884 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001885
Octavian Purdila2870c432008-07-15 00:49:11 -07001886 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001887 poff += *off;
1888 plen -= *off;
1889 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001890
Eric Dumazet18aafc62013-01-11 14:46:37 +00001891 do {
1892 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001893
Eric Dumazet18aafc62013-01-11 14:46:37 +00001894 if (spd_fill_page(spd, pipe, page, &flen, poff,
1895 linear, sk))
1896 return true;
1897 poff += flen;
1898 plen -= flen;
1899 *len -= flen;
1900 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001901
David S. Millera108d5f2012-04-23 23:06:11 -04001902 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001903}
1904
1905/*
David S. Millera108d5f2012-04-23 23:06:11 -04001906 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001907 * pipe is full or if we already spliced the requested length.
1908 */
David S. Millera108d5f2012-04-23 23:06:11 -04001909static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1910 unsigned int *offset, unsigned int *len,
1911 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001912{
1913 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001914 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001915
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001916 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001917 * If skb->head_frag is set, this 'linear' part is backed by a
1918 * fragment, and if the head is not shared with any clones then
1919 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001920 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001921 if (__splice_segment(virt_to_page(skb->data),
1922 (unsigned long) skb->data & (PAGE_SIZE - 1),
1923 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001924 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001925 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001926 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001927 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001928
1929 /*
1930 * then map the fragments
1931 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001932 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1933 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1934
Ian Campbellea2ab692011-08-22 23:44:58 +00001935 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001936 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001937 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001938 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 }
1940
Tom Herbertfa9835e2016-03-07 14:11:04 -08001941 skb_walk_frags(skb, iter) {
1942 if (*offset >= iter->len) {
1943 *offset -= iter->len;
1944 continue;
1945 }
1946 /* __skb_splice_bits() only fails if the output has no room
1947 * left, so no point in going over the frag_list for the error
1948 * case.
1949 */
1950 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1951 return true;
1952 }
1953
David S. Millera108d5f2012-04-23 23:06:11 -04001954 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001955}
1956
1957/*
1958 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001959 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001960 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001961int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001962 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001963 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001964{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001965 struct partial_page partial[MAX_SKB_FRAGS];
1966 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001967 struct splice_pipe_desc spd = {
1968 .pages = pages,
1969 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001970 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001971 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001972 .spd_release = sock_spd_release,
1973 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001974 int ret = 0;
1975
Tom Herbertfa9835e2016-03-07 14:11:04 -08001976 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001977
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001978 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04001979 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001980
Jens Axboe35f3d142010-05-20 10:43:18 +02001981 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001982}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001983EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001984
Herbert Xu357b40a2005-04-19 22:30:14 -07001985/**
1986 * skb_store_bits - store bits from kernel buffer to skb
1987 * @skb: destination buffer
1988 * @offset: offset in destination
1989 * @from: source buffer
1990 * @len: number of bytes to copy
1991 *
1992 * Copy the specified number of bytes from the source buffer to the
1993 * destination skb. This function handles all the messy bits of
1994 * traversing fragment lists and such.
1995 */
1996
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001997int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001998{
David S. Miller1a028e52007-04-27 15:21:23 -07001999 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002000 struct sk_buff *frag_iter;
2001 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002002
2003 if (offset > (int)skb->len - len)
2004 goto fault;
2005
David S. Miller1a028e52007-04-27 15:21:23 -07002006 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002007 if (copy > len)
2008 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002009 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002010 if ((len -= copy) == 0)
2011 return 0;
2012 offset += copy;
2013 from += copy;
2014 }
2015
2016 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2017 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002018 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002019
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002020 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002021
Eric Dumazet9e903e02011-10-18 21:00:24 +00002022 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002023 if ((copy = end - offset) > 0) {
2024 u8 *vaddr;
2025
2026 if (copy > len)
2027 copy = len;
2028
Eric Dumazet51c56b02012-04-05 11:35:15 +02002029 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002030 memcpy(vaddr + frag->page_offset + offset - start,
2031 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002032 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002033
2034 if ((len -= copy) == 0)
2035 return 0;
2036 offset += copy;
2037 from += copy;
2038 }
David S. Miller1a028e52007-04-27 15:21:23 -07002039 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002040 }
2041
David S. Millerfbb398a2009-06-09 00:18:59 -07002042 skb_walk_frags(skb, frag_iter) {
2043 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002044
David S. Millerfbb398a2009-06-09 00:18:59 -07002045 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002046
David S. Millerfbb398a2009-06-09 00:18:59 -07002047 end = start + frag_iter->len;
2048 if ((copy = end - offset) > 0) {
2049 if (copy > len)
2050 copy = len;
2051 if (skb_store_bits(frag_iter, offset - start,
2052 from, copy))
2053 goto fault;
2054 if ((len -= copy) == 0)
2055 return 0;
2056 offset += copy;
2057 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002058 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002059 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002060 }
2061 if (!len)
2062 return 0;
2063
2064fault:
2065 return -EFAULT;
2066}
Herbert Xu357b40a2005-04-19 22:30:14 -07002067EXPORT_SYMBOL(skb_store_bits);
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002070__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2071 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
David S. Miller1a028e52007-04-27 15:21:23 -07002073 int start = skb_headlen(skb);
2074 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002075 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int pos = 0;
2077
2078 /* Checksum header. */
2079 if (copy > 0) {
2080 if (copy > len)
2081 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002082 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if ((len -= copy) == 0)
2084 return csum;
2085 offset += copy;
2086 pos = copy;
2087 }
2088
2089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002090 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002091 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002093 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002094
Eric Dumazet51c56b02012-04-05 11:35:15 +02002095 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002097 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 if (copy > len)
2101 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002102 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002103 csum2 = ops->update(vaddr + frag->page_offset +
2104 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002105 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002106 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!(len -= copy))
2108 return csum;
2109 offset += copy;
2110 pos += copy;
2111 }
David S. Miller1a028e52007-04-27 15:21:23 -07002112 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114
David S. Millerfbb398a2009-06-09 00:18:59 -07002115 skb_walk_frags(skb, frag_iter) {
2116 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David S. Millerfbb398a2009-06-09 00:18:59 -07002118 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
David S. Millerfbb398a2009-06-09 00:18:59 -07002120 end = start + frag_iter->len;
2121 if ((copy = end - offset) > 0) {
2122 __wsum csum2;
2123 if (copy > len)
2124 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002125 csum2 = __skb_checksum(frag_iter, offset - start,
2126 copy, 0, ops);
2127 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002128 if ((len -= copy) == 0)
2129 return csum;
2130 offset += copy;
2131 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002133 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002135 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 return csum;
2138}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002139EXPORT_SYMBOL(__skb_checksum);
2140
2141__wsum skb_checksum(const struct sk_buff *skb, int offset,
2142 int len, __wsum csum)
2143{
2144 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002145 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002146 .combine = csum_block_add_ext,
2147 };
2148
2149 return __skb_checksum(skb, offset, len, csum, &ops);
2150}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002151EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153/* Both of above in one bottle. */
2154
Al Viro81d776622006-11-14 21:37:33 -08002155__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2156 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
David S. Miller1a028e52007-04-27 15:21:23 -07002158 int start = skb_headlen(skb);
2159 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002160 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int pos = 0;
2162
2163 /* Copy header. */
2164 if (copy > 0) {
2165 if (copy > len)
2166 copy = len;
2167 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2168 copy, csum);
2169 if ((len -= copy) == 0)
2170 return csum;
2171 offset += copy;
2172 to += copy;
2173 pos = copy;
2174 }
2175
2176 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002177 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002179 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002180
Eric Dumazet9e903e02011-10-18 21:00:24 +00002181 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002183 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 u8 *vaddr;
2185 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2186
2187 if (copy > len)
2188 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002189 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002191 frag->page_offset +
2192 offset - start, to,
2193 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002194 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 csum = csum_block_add(csum, csum2, pos);
2196 if (!(len -= copy))
2197 return csum;
2198 offset += copy;
2199 to += copy;
2200 pos += copy;
2201 }
David S. Miller1a028e52007-04-27 15:21:23 -07002202 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204
David S. Millerfbb398a2009-06-09 00:18:59 -07002205 skb_walk_frags(skb, frag_iter) {
2206 __wsum csum2;
2207 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
David S. Millerfbb398a2009-06-09 00:18:59 -07002209 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
David S. Millerfbb398a2009-06-09 00:18:59 -07002211 end = start + frag_iter->len;
2212 if ((copy = end - offset) > 0) {
2213 if (copy > len)
2214 copy = len;
2215 csum2 = skb_copy_and_csum_bits(frag_iter,
2216 offset - start,
2217 to, copy, 0);
2218 csum = csum_block_add(csum, csum2, pos);
2219 if ((len -= copy) == 0)
2220 return csum;
2221 offset += copy;
2222 to += copy;
2223 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002225 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002227 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return csum;
2229}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002230EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Davide Caratti96178132017-05-18 15:44:37 +02002232static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2233{
2234 net_warn_ratelimited(
2235 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2236 __func__);
2237 return 0;
2238}
2239
2240static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2241 int offset, int len)
2242{
2243 net_warn_ratelimited(
2244 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2245 __func__);
2246 return 0;
2247}
2248
2249static const struct skb_checksum_ops default_crc32c_ops = {
2250 .update = warn_crc32c_csum_update,
2251 .combine = warn_crc32c_csum_combine,
2252};
2253
2254const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2255 &default_crc32c_ops;
2256EXPORT_SYMBOL(crc32c_csum_stub);
2257
Thomas Grafaf2806f2013-12-13 15:22:17 +01002258 /**
2259 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2260 * @from: source buffer
2261 *
2262 * Calculates the amount of linear headroom needed in the 'to' skb passed
2263 * into skb_zerocopy().
2264 */
2265unsigned int
2266skb_zerocopy_headlen(const struct sk_buff *from)
2267{
2268 unsigned int hlen = 0;
2269
2270 if (!from->head_frag ||
2271 skb_headlen(from) < L1_CACHE_BYTES ||
2272 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2273 hlen = skb_headlen(from);
2274
2275 if (skb_has_frag_list(from))
2276 hlen = from->len;
2277
2278 return hlen;
2279}
2280EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2281
2282/**
2283 * skb_zerocopy - Zero copy skb to skb
2284 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002285 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002286 * @len: number of bytes to copy from source buffer
2287 * @hlen: size of linear headroom in destination buffer
2288 *
2289 * Copies up to `len` bytes from `from` to `to` by creating references
2290 * to the frags in the source buffer.
2291 *
2292 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2293 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002294 *
2295 * Return value:
2296 * 0: everything is OK
2297 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2298 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002299 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002300int
2301skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002302{
2303 int i, j = 0;
2304 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002305 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002306 struct page *page;
2307 unsigned int offset;
2308
2309 BUG_ON(!from->head_frag && !hlen);
2310
2311 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002312 if (len <= skb_tailroom(to))
2313 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002314
2315 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002316 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2317 if (unlikely(ret))
2318 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002319 len -= hlen;
2320 } else {
2321 plen = min_t(int, skb_headlen(from), len);
2322 if (plen) {
2323 page = virt_to_head_page(from->head);
2324 offset = from->data - (unsigned char *)page_address(page);
2325 __skb_fill_page_desc(to, 0, page, offset, plen);
2326 get_page(page);
2327 j = 1;
2328 len -= plen;
2329 }
2330 }
2331
2332 to->truesize += len + plen;
2333 to->len += len + plen;
2334 to->data_len += len + plen;
2335
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002336 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2337 skb_tx_error(from);
2338 return -ENOMEM;
2339 }
2340
Thomas Grafaf2806f2013-12-13 15:22:17 +01002341 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2342 if (!len)
2343 break;
2344 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2345 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2346 len -= skb_shinfo(to)->frags[j].size;
2347 skb_frag_ref(to, j);
2348 j++;
2349 }
2350 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002351
2352 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002353}
2354EXPORT_SYMBOL_GPL(skb_zerocopy);
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2357{
Al Virod3bc23e2006-11-14 21:24:49 -08002358 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 long csstart;
2360
Patrick McHardy84fa7932006-08-29 16:44:56 -07002361 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002362 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 else
2364 csstart = skb_headlen(skb);
2365
Kris Katterjohn09a62662006-01-08 22:24:28 -08002366 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002368 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 csum = 0;
2371 if (csstart != skb->len)
2372 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2373 skb->len - csstart, 0);
2374
Patrick McHardy84fa7932006-08-29 16:44:56 -07002375 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002376 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Al Virod3bc23e2006-11-14 21:24:49 -08002378 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002381EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383/**
2384 * skb_dequeue - remove from the head of the queue
2385 * @list: list to dequeue from
2386 *
2387 * Remove the head of the list. The list lock is taken so the function
2388 * may be used safely with other locking list functions. The head item is
2389 * returned or %NULL if the list is empty.
2390 */
2391
2392struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2393{
2394 unsigned long flags;
2395 struct sk_buff *result;
2396
2397 spin_lock_irqsave(&list->lock, flags);
2398 result = __skb_dequeue(list);
2399 spin_unlock_irqrestore(&list->lock, flags);
2400 return result;
2401}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002402EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404/**
2405 * skb_dequeue_tail - remove from the tail of the queue
2406 * @list: list to dequeue from
2407 *
2408 * Remove the tail of the list. The list lock is taken so the function
2409 * may be used safely with other locking list functions. The tail item is
2410 * returned or %NULL if the list is empty.
2411 */
2412struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2413{
2414 unsigned long flags;
2415 struct sk_buff *result;
2416
2417 spin_lock_irqsave(&list->lock, flags);
2418 result = __skb_dequeue_tail(list);
2419 spin_unlock_irqrestore(&list->lock, flags);
2420 return result;
2421}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002422EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424/**
2425 * skb_queue_purge - empty a list
2426 * @list: list to empty
2427 *
2428 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2429 * the list and one reference dropped. This function takes the list
2430 * lock and is atomic with respect to other list locking functions.
2431 */
2432void skb_queue_purge(struct sk_buff_head *list)
2433{
2434 struct sk_buff *skb;
2435 while ((skb = skb_dequeue(list)) != NULL)
2436 kfree_skb(skb);
2437}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002438EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002441 * skb_rbtree_purge - empty a skb rbtree
2442 * @root: root of the rbtree to empty
2443 *
2444 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2445 * the list and one reference dropped. This function does not take
2446 * any lock. Synchronization should be handled by the caller (e.g., TCP
2447 * out-of-order queue is protected by the socket lock).
2448 */
2449void skb_rbtree_purge(struct rb_root *root)
2450{
2451 struct sk_buff *skb, *next;
2452
2453 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2454 kfree_skb(skb);
2455
2456 *root = RB_ROOT;
2457}
2458
2459/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 * skb_queue_head - queue a buffer at the list head
2461 * @list: list to use
2462 * @newsk: buffer to queue
2463 *
2464 * Queue a buffer at the start of the list. This function takes the
2465 * list lock and can be used safely with other locking &sk_buff functions
2466 * safely.
2467 *
2468 * A buffer cannot be placed on two lists at the same time.
2469 */
2470void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2471{
2472 unsigned long flags;
2473
2474 spin_lock_irqsave(&list->lock, flags);
2475 __skb_queue_head(list, newsk);
2476 spin_unlock_irqrestore(&list->lock, flags);
2477}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002478EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480/**
2481 * skb_queue_tail - queue a buffer at the list tail
2482 * @list: list to use
2483 * @newsk: buffer to queue
2484 *
2485 * Queue a buffer at the tail of the list. This function takes the
2486 * list lock and can be used safely with other locking &sk_buff functions
2487 * safely.
2488 *
2489 * A buffer cannot be placed on two lists at the same time.
2490 */
2491void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2492{
2493 unsigned long flags;
2494
2495 spin_lock_irqsave(&list->lock, flags);
2496 __skb_queue_tail(list, newsk);
2497 spin_unlock_irqrestore(&list->lock, flags);
2498}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002499EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/**
2502 * skb_unlink - remove a buffer from a list
2503 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002504 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 *
David S. Miller8728b832005-08-09 19:25:21 -07002506 * Remove a packet from a list. The list locks are taken and this
2507 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 *
David S. Miller8728b832005-08-09 19:25:21 -07002509 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 */
David S. Miller8728b832005-08-09 19:25:21 -07002511void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
David S. Miller8728b832005-08-09 19:25:21 -07002513 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
David S. Miller8728b832005-08-09 19:25:21 -07002515 spin_lock_irqsave(&list->lock, flags);
2516 __skb_unlink(skb, list);
2517 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002519EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521/**
2522 * skb_append - append a buffer
2523 * @old: buffer to insert after
2524 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002525 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 *
2527 * Place a packet after a given packet in a list. The list locks are taken
2528 * and this function is atomic with respect to other list locked calls.
2529 * A buffer cannot be placed on two lists at the same time.
2530 */
David S. Miller8728b832005-08-09 19:25:21 -07002531void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
2533 unsigned long flags;
2534
David S. Miller8728b832005-08-09 19:25:21 -07002535 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002536 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002537 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002539EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541/**
2542 * skb_insert - insert a buffer
2543 * @old: buffer to insert before
2544 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002545 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 *
David S. Miller8728b832005-08-09 19:25:21 -07002547 * Place a packet before a given packet in a list. The list locks are
2548 * taken and this function is atomic with respect to other list locked
2549 * calls.
2550 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * A buffer cannot be placed on two lists at the same time.
2552 */
David S. Miller8728b832005-08-09 19:25:21 -07002553void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 unsigned long flags;
2556
David S. Miller8728b832005-08-09 19:25:21 -07002557 spin_lock_irqsave(&list->lock, flags);
2558 __skb_insert(newsk, old->prev, old, list);
2559 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002561EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static inline void skb_split_inside_header(struct sk_buff *skb,
2564 struct sk_buff* skb1,
2565 const u32 len, const int pos)
2566{
2567 int i;
2568
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002569 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2570 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /* And move data appendix as is. */
2572 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2573 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2574
2575 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2576 skb_shinfo(skb)->nr_frags = 0;
2577 skb1->data_len = skb->data_len;
2578 skb1->len += skb1->data_len;
2579 skb->data_len = 0;
2580 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002581 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584static inline void skb_split_no_header(struct sk_buff *skb,
2585 struct sk_buff* skb1,
2586 const u32 len, int pos)
2587{
2588 int i, k = 0;
2589 const int nfrags = skb_shinfo(skb)->nr_frags;
2590
2591 skb_shinfo(skb)->nr_frags = 0;
2592 skb1->len = skb1->data_len = skb->len - len;
2593 skb->len = len;
2594 skb->data_len = len - pos;
2595
2596 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002597 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 if (pos + size > len) {
2600 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2601
2602 if (pos < len) {
2603 /* Split frag.
2604 * We have two variants in this case:
2605 * 1. Move all the frag to the second
2606 * part, if it is possible. F.e.
2607 * this approach is mandatory for TUX,
2608 * where splitting is expensive.
2609 * 2. Split is accurately. We make this.
2610 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002611 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002613 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2614 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 skb_shinfo(skb)->nr_frags++;
2616 }
2617 k++;
2618 } else
2619 skb_shinfo(skb)->nr_frags++;
2620 pos += size;
2621 }
2622 skb_shinfo(skb1)->nr_frags = k;
2623}
2624
2625/**
2626 * skb_split - Split fragmented skb to two parts at length len.
2627 * @skb: the buffer to split
2628 * @skb1: the buffer to receive the second part
2629 * @len: new length for skb
2630 */
2631void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2632{
2633 int pos = skb_headlen(skb);
2634
Willem de Bruijnfff88032017-06-08 11:35:03 -04002635 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2636 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (len < pos) /* Split line is inside header. */
2638 skb_split_inside_header(skb, skb1, len, pos);
2639 else /* Second chunk has no header, nothing to copy. */
2640 skb_split_no_header(skb, skb1, len, pos);
2641}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002642EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002644/* Shifting from/to a cloned skb is a no-go.
2645 *
2646 * Caller cannot keep skb_shinfo related pointers past calling here!
2647 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002648static int skb_prepare_for_shift(struct sk_buff *skb)
2649{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002650 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002651}
2652
2653/**
2654 * skb_shift - Shifts paged data partially from skb to another
2655 * @tgt: buffer into which tail data gets added
2656 * @skb: buffer from which the paged data comes from
2657 * @shiftlen: shift up to this many bytes
2658 *
2659 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002660 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002661 * It's up to caller to free skb if everything was shifted.
2662 *
2663 * If @tgt runs out of frags, the whole operation is aborted.
2664 *
2665 * Skb cannot include anything else but paged data while tgt is allowed
2666 * to have non-paged data as well.
2667 *
2668 * TODO: full sized shift could be optimized but that would need
2669 * specialized skb free'er to handle frags without up-to-date nr_frags.
2670 */
2671int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2672{
2673 int from, to, merge, todo;
2674 struct skb_frag_struct *fragfrom, *fragto;
2675
2676 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002677
2678 if (skb_headlen(skb))
2679 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002680
2681 todo = shiftlen;
2682 from = 0;
2683 to = skb_shinfo(tgt)->nr_frags;
2684 fragfrom = &skb_shinfo(skb)->frags[from];
2685
2686 /* Actual merge is delayed until the point when we know we can
2687 * commit all, so that we don't have to undo partial changes
2688 */
2689 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002690 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2691 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002692 merge = -1;
2693 } else {
2694 merge = to - 1;
2695
Eric Dumazet9e903e02011-10-18 21:00:24 +00002696 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002697 if (todo < 0) {
2698 if (skb_prepare_for_shift(skb) ||
2699 skb_prepare_for_shift(tgt))
2700 return 0;
2701
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002702 /* All previous frag pointers might be stale! */
2703 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002704 fragto = &skb_shinfo(tgt)->frags[merge];
2705
Eric Dumazet9e903e02011-10-18 21:00:24 +00002706 skb_frag_size_add(fragto, shiftlen);
2707 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002708 fragfrom->page_offset += shiftlen;
2709
2710 goto onlymerged;
2711 }
2712
2713 from++;
2714 }
2715
2716 /* Skip full, not-fitting skb to avoid expensive operations */
2717 if ((shiftlen == skb->len) &&
2718 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2719 return 0;
2720
2721 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2722 return 0;
2723
2724 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2725 if (to == MAX_SKB_FRAGS)
2726 return 0;
2727
2728 fragfrom = &skb_shinfo(skb)->frags[from];
2729 fragto = &skb_shinfo(tgt)->frags[to];
2730
Eric Dumazet9e903e02011-10-18 21:00:24 +00002731 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002732 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002733 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002734 from++;
2735 to++;
2736
2737 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002738 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002739 fragto->page = fragfrom->page;
2740 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002741 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002742
2743 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002744 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002745 todo = 0;
2746
2747 to++;
2748 break;
2749 }
2750 }
2751
2752 /* Ready to "commit" this state change to tgt */
2753 skb_shinfo(tgt)->nr_frags = to;
2754
2755 if (merge >= 0) {
2756 fragfrom = &skb_shinfo(skb)->frags[0];
2757 fragto = &skb_shinfo(tgt)->frags[merge];
2758
Eric Dumazet9e903e02011-10-18 21:00:24 +00002759 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002760 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002761 }
2762
2763 /* Reposition in the original skb */
2764 to = 0;
2765 while (from < skb_shinfo(skb)->nr_frags)
2766 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2767 skb_shinfo(skb)->nr_frags = to;
2768
2769 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2770
2771onlymerged:
2772 /* Most likely the tgt won't ever need its checksum anymore, skb on
2773 * the other hand might need it if it needs to be resent
2774 */
2775 tgt->ip_summed = CHECKSUM_PARTIAL;
2776 skb->ip_summed = CHECKSUM_PARTIAL;
2777
2778 /* Yak, is it really working this way? Some helper please? */
2779 skb->len -= shiftlen;
2780 skb->data_len -= shiftlen;
2781 skb->truesize -= shiftlen;
2782 tgt->len += shiftlen;
2783 tgt->data_len += shiftlen;
2784 tgt->truesize += shiftlen;
2785
2786 return shiftlen;
2787}
2788
Thomas Graf677e90e2005-06-23 20:59:51 -07002789/**
2790 * skb_prepare_seq_read - Prepare a sequential read of skb data
2791 * @skb: the buffer to read
2792 * @from: lower offset of data to be read
2793 * @to: upper offset of data to be read
2794 * @st: state variable
2795 *
2796 * Initializes the specified state variable. Must be called before
2797 * invoking skb_seq_read() for the first time.
2798 */
2799void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2800 unsigned int to, struct skb_seq_state *st)
2801{
2802 st->lower_offset = from;
2803 st->upper_offset = to;
2804 st->root_skb = st->cur_skb = skb;
2805 st->frag_idx = st->stepped_offset = 0;
2806 st->frag_data = NULL;
2807}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002808EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002809
2810/**
2811 * skb_seq_read - Sequentially read skb data
2812 * @consumed: number of bytes consumed by the caller so far
2813 * @data: destination pointer for data to be returned
2814 * @st: state variable
2815 *
Mathias Krausebc323832013-11-07 14:18:26 +01002816 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002817 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002818 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002819 * of the block or 0 if the end of the skb data or the upper
2820 * offset has been reached.
2821 *
2822 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002823 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002824 * of bytes already consumed and the next call to
2825 * skb_seq_read() will return the remaining part of the block.
2826 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002827 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002828 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002829 * reads of potentially non linear data.
2830 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002831 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002832 * at the moment, state->root_skb could be replaced with
2833 * a stack for this purpose.
2834 */
2835unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2836 struct skb_seq_state *st)
2837{
2838 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2839 skb_frag_t *frag;
2840
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002841 if (unlikely(abs_offset >= st->upper_offset)) {
2842 if (st->frag_data) {
2843 kunmap_atomic(st->frag_data);
2844 st->frag_data = NULL;
2845 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002846 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002847 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002848
2849next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002850 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002851
Thomas Chenault995b3372009-05-18 21:43:27 -07002852 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002853 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002854 return block_limit - abs_offset;
2855 }
2856
2857 if (st->frag_idx == 0 && !st->frag_data)
2858 st->stepped_offset += skb_headlen(st->cur_skb);
2859
2860 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2861 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002862 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002863
2864 if (abs_offset < block_limit) {
2865 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002866 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002867
2868 *data = (u8 *) st->frag_data + frag->page_offset +
2869 (abs_offset - st->stepped_offset);
2870
2871 return block_limit - abs_offset;
2872 }
2873
2874 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002875 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002876 st->frag_data = NULL;
2877 }
2878
2879 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002880 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002881 }
2882
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002883 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002884 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002885 st->frag_data = NULL;
2886 }
2887
David S. Miller21dc3302010-08-23 00:13:46 -07002888 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002889 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002890 st->frag_idx = 0;
2891 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002892 } else if (st->cur_skb->next) {
2893 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002894 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002895 goto next_skb;
2896 }
2897
2898 return 0;
2899}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002900EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002901
2902/**
2903 * skb_abort_seq_read - Abort a sequential read of skb data
2904 * @st: state variable
2905 *
2906 * Must be called if skb_seq_read() was not called until it
2907 * returned 0.
2908 */
2909void skb_abort_seq_read(struct skb_seq_state *st)
2910{
2911 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002912 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002913}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002914EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002915
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002916#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2917
2918static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2919 struct ts_config *conf,
2920 struct ts_state *state)
2921{
2922 return skb_seq_read(offset, text, TS_SKB_CB(state));
2923}
2924
2925static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2926{
2927 skb_abort_seq_read(TS_SKB_CB(state));
2928}
2929
2930/**
2931 * skb_find_text - Find a text pattern in skb data
2932 * @skb: the buffer to look in
2933 * @from: search offset
2934 * @to: search limit
2935 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002936 *
2937 * Finds a pattern in the skb data according to the specified
2938 * textsearch configuration. Use textsearch_next() to retrieve
2939 * subsequent occurrences of the pattern. Returns the offset
2940 * to the first occurrence or UINT_MAX if no match was found.
2941 */
2942unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002943 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002944{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002945 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002946 unsigned int ret;
2947
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002948 config->get_next_block = skb_ts_get_next_block;
2949 config->finish = skb_ts_finish;
2950
Bojan Prtvar059a2442015-02-22 11:46:35 +01002951 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002952
Bojan Prtvar059a2442015-02-22 11:46:35 +01002953 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002954 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002955}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002956EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002957
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002958/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002959 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002960 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002961 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002962 * @getfrag: call back function to be used for getting the user data
2963 * @from: pointer to user message iov
2964 * @length: length of the iov message
2965 *
2966 * Description: This procedure append the user data in the fragment part
2967 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2968 */
2969int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002970 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002971 int len, int odd, struct sk_buff *skb),
2972 void *from, int length)
2973{
Eric Dumazetb2111722012-12-28 06:06:37 +00002974 int frg_cnt = skb_shinfo(skb)->nr_frags;
2975 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002976 int offset = 0;
2977 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002978 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002979
2980 do {
2981 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002982 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002983 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002984
Eric Dumazetb2111722012-12-28 06:06:37 +00002985 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002986 return -ENOMEM;
2987
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002988 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002989 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002990
Eric Dumazetb2111722012-12-28 06:06:37 +00002991 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2992 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002993 if (ret < 0)
2994 return -EFAULT;
2995
2996 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002997 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2998 copy);
2999 frg_cnt++;
3000 pfrag->offset += copy;
3001 get_page(pfrag->page);
3002
3003 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003004 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003005 skb->len += copy;
3006 skb->data_len += copy;
3007 offset += copy;
3008 length -= copy;
3009
3010 } while (length > 0);
3011
3012 return 0;
3013}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003014EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003015
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003016int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3017 int offset, size_t size)
3018{
3019 int i = skb_shinfo(skb)->nr_frags;
3020
3021 if (skb_can_coalesce(skb, i, page, offset)) {
3022 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3023 } else if (i < MAX_SKB_FRAGS) {
3024 get_page(page);
3025 skb_fill_page_desc(skb, i, page, offset, size);
3026 } else {
3027 return -EMSGSIZE;
3028 }
3029
3030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3033
Herbert Xucbb042f92006-03-20 22:43:56 -08003034/**
3035 * skb_pull_rcsum - pull skb and update receive checksum
3036 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08003037 * @len: length of data pulled
3038 *
3039 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003040 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003041 * receive path processing instead of skb_pull unless you know
3042 * that the checksum difference is zero (e.g., a valid IP header)
3043 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08003044 */
Johannes Bergaf728682017-06-16 14:29:22 +02003045void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f92006-03-20 22:43:56 -08003046{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003047 unsigned char *data = skb->data;
3048
Herbert Xucbb042f92006-03-20 22:43:56 -08003049 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003050 __skb_pull(skb, len);
3051 skb_postpull_rcsum(skb, data, len);
3052 return skb->data;
Herbert Xucbb042f92006-03-20 22:43:56 -08003053}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003054EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3055
Herbert Xuf4c50d92006-06-22 03:02:40 -07003056/**
3057 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003058 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003059 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003060 *
3061 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003062 * a pointer to the first in a list of new skbs for the segments.
3063 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003064 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003065struct sk_buff *skb_segment(struct sk_buff *head_skb,
3066 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003067{
3068 struct sk_buff *segs = NULL;
3069 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003070 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003071 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3072 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3073 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003074 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003075 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003076 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003077 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003078 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003079 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003080 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003081 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003082 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003083 int err = -ENOMEM;
3084 int i = 0;
3085 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003086 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003087
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003088 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003089 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003090 if (unlikely(!proto))
3091 return ERR_PTR(-EINVAL);
3092
Alexander Duyck36c98382016-05-02 09:38:18 -07003093 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003094 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003095
Steffen Klassert07b26c92016-09-19 12:58:47 +02003096 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3097 if (!(features & NETIF_F_GSO_PARTIAL)) {
3098 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003099 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003100
3101 if (!list_skb ||
3102 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3103 goto normal;
3104
Ilan Tayari43170c42017-04-19 21:26:07 +03003105 /* If we get here then all the required
3106 * GSO features except frag_list are supported.
3107 * Try to split the SKB to multiple GSO SKBs
3108 * with no frag_list.
3109 * Currently we can do that only when the buffers don't
3110 * have a linear part and all the buffers except
3111 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003112 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003113 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003114 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003115 if (frag_len != iter->len && iter->next)
3116 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003117 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003118 goto normal;
3119
3120 len -= iter->len;
3121 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003122
3123 if (len != frag_len)
3124 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003125 }
3126
3127 /* GSO partial only requires that we trim off any excess that
3128 * doesn't fit into an MSS sized block, so take care of that
3129 * now.
3130 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003131 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003132 if (partial_segs > 1)
3133 mss *= partial_segs;
3134 else
3135 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003136 }
3137
Steffen Klassert07b26c92016-09-19 12:58:47 +02003138normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003139 headroom = skb_headroom(head_skb);
3140 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003141
3142 do {
3143 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003144 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003145 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003146 int size;
3147
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003148 if (unlikely(mss == GSO_BY_FRAGS)) {
3149 len = list_skb->len;
3150 } else {
3151 len = head_skb->len - offset;
3152 if (len > mss)
3153 len = mss;
3154 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003155
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003156 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003157 if (hsize < 0)
3158 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003159 if (hsize > len || !sg)
3160 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003161
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003162 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3163 (skb_headlen(list_skb) == len || sg)) {
3164 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003165
Herbert Xu9d8506c2013-11-21 11:10:04 -08003166 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003167 nfrags = skb_shinfo(list_skb)->nr_frags;
3168 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003169 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003170 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003171
3172 while (pos < offset + len) {
3173 BUG_ON(i >= nfrags);
3174
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003175 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003176 if (pos + size > offset + len)
3177 break;
3178
3179 i++;
3180 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003181 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003182 }
3183
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003184 nskb = skb_clone(list_skb, GFP_ATOMIC);
3185 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003186
3187 if (unlikely(!nskb))
3188 goto err;
3189
Herbert Xu9d8506c2013-11-21 11:10:04 -08003190 if (unlikely(pskb_trim(nskb, len))) {
3191 kfree_skb(nskb);
3192 goto err;
3193 }
3194
Alexander Duyckec47ea82012-05-04 14:26:56 +00003195 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003196 if (skb_cow_head(nskb, doffset + headroom)) {
3197 kfree_skb(nskb);
3198 goto err;
3199 }
3200
Alexander Duyckec47ea82012-05-04 14:26:56 +00003201 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003202 skb_release_head_state(nskb);
3203 __skb_push(nskb, doffset);
3204 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003205 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003206 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003207 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003208
3209 if (unlikely(!nskb))
3210 goto err;
3211
3212 skb_reserve(nskb, headroom);
3213 __skb_put(nskb, doffset);
3214 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003215
3216 if (segs)
3217 tail->next = nskb;
3218 else
3219 segs = nskb;
3220 tail = nskb;
3221
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003222 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003223
Eric Dumazet030737b2013-10-19 11:42:54 -07003224 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003225 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003226
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003227 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003228 nskb->data - tnl_hlen,
3229 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003230
Herbert Xu9d8506c2013-11-21 11:10:04 -08003231 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003232 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003233
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003234 if (!sg) {
3235 if (!nskb->remcsum_offload)
3236 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003237 SKB_GSO_CB(nskb)->csum =
3238 skb_copy_and_csum_bits(head_skb, offset,
3239 skb_put(nskb, len),
3240 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003241 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003242 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003243 continue;
3244 }
3245
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003246 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003247
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003248 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003249 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003250
Willem de Bruijnfff88032017-06-08 11:35:03 -04003251 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3252 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003253
Herbert Xu9d8506c2013-11-21 11:10:04 -08003254 while (pos < offset + len) {
3255 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003256 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003257
3258 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003259 nfrags = skb_shinfo(list_skb)->nr_frags;
3260 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003261 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003262
3263 BUG_ON(!nfrags);
3264
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003265 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003266 }
3267
3268 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3269 MAX_SKB_FRAGS)) {
3270 net_warn_ratelimited(
3271 "skb_segment: too many frags: %u %u\n",
3272 pos, mss);
3273 goto err;
3274 }
3275
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003276 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3277 goto err;
3278
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003279 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003280 __skb_frag_ref(nskb_frag);
3281 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003282
3283 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003284 nskb_frag->page_offset += offset - pos;
3285 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003286 }
3287
Herbert Xu89319d382008-12-15 23:26:06 -08003288 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003289
3290 if (pos + size <= offset + len) {
3291 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003292 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003293 pos += size;
3294 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003295 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003296 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003297 }
3298
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003299 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003300 }
3301
Herbert Xu89319d382008-12-15 23:26:06 -08003302skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003303 nskb->data_len = len - hsize;
3304 nskb->len += nskb->data_len;
3305 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003306
Simon Horman1cdbcb72013-05-19 15:46:49 +00003307perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003308 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003309 if (skb_has_shared_frag(nskb)) {
3310 err = __skb_linearize(nskb);
3311 if (err)
3312 goto err;
3313 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003314 if (!nskb->remcsum_offload)
3315 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003316 SKB_GSO_CB(nskb)->csum =
3317 skb_checksum(nskb, doffset,
3318 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003319 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003320 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003321 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003322 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003323
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003324 /* Some callers want to get the end of the list.
3325 * Put it in segs->prev to avoid walking the list.
3326 * (see validate_xmit_skb_list() for example)
3327 */
3328 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003329
Alexander Duyck802ab552016-04-10 21:45:03 -04003330 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003331 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003332 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003333 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003334
3335 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003336 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003337 type &= ~SKB_GSO_DODGY;
3338
3339 /* Update GSO info and prepare to start updating headers on
3340 * our way back down the stack of protocols.
3341 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003342 for (iter = segs; iter; iter = iter->next) {
3343 skb_shinfo(iter)->gso_size = gso_size;
3344 skb_shinfo(iter)->gso_segs = partial_segs;
3345 skb_shinfo(iter)->gso_type = type;
3346 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3347 }
3348
3349 if (tail->len - doffset <= gso_size)
3350 skb_shinfo(tail)->gso_size = 0;
3351 else if (tail != segs)
3352 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003353 }
3354
Toshiaki Makita432c8562014-10-27 10:30:51 -07003355 /* Following permits correct backpressure, for protocols
3356 * using skb_set_owner_w().
3357 * Idea is to tranfert ownership from head_skb to last segment.
3358 */
3359 if (head_skb->destructor == sock_wfree) {
3360 swap(tail->truesize, head_skb->truesize);
3361 swap(tail->destructor, head_skb->destructor);
3362 swap(tail->sk, head_skb->sk);
3363 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003364 return segs;
3365
3366err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003367 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003368 return ERR_PTR(err);
3369}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003370EXPORT_SYMBOL_GPL(skb_segment);
3371
Herbert Xu71d93b32008-12-15 23:42:33 -08003372int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3373{
Eric Dumazet8a291112013-10-08 09:02:23 -07003374 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003375 unsigned int offset = skb_gro_offset(skb);
3376 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003377 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003378 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003379 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003380
Eric Dumazet8a291112013-10-08 09:02:23 -07003381 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003382 return -E2BIG;
3383
Eric Dumazet29e98242014-05-16 11:34:37 -07003384 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003385 pinfo = skb_shinfo(lp);
3386
3387 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003388 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003389 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003390 int i = skbinfo->nr_frags;
3391 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003392
Herbert Xu66e92fc2009-05-26 18:50:32 +00003393 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003394 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003395
Eric Dumazet8a291112013-10-08 09:02:23 -07003396 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003397 pinfo->nr_frags = nr_frags;
3398 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003399
Herbert Xu9aaa1562009-05-26 18:50:33 +00003400 frag = pinfo->frags + nr_frags;
3401 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003402 do {
3403 *--frag = *--frag2;
3404 } while (--i);
3405
3406 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003407 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003408
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003409 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003410 delta_truesize = skb->truesize -
3411 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003412
Herbert Xuf5572062009-01-14 20:40:03 -08003413 skb->truesize -= skb->data_len;
3414 skb->len -= skb->data_len;
3415 skb->data_len = 0;
3416
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003417 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003418 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003419 } else if (skb->head_frag) {
3420 int nr_frags = pinfo->nr_frags;
3421 skb_frag_t *frag = pinfo->frags + nr_frags;
3422 struct page *page = virt_to_head_page(skb->head);
3423 unsigned int first_size = headlen - offset;
3424 unsigned int first_offset;
3425
3426 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003427 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003428
3429 first_offset = skb->data -
3430 (unsigned char *)page_address(page) +
3431 offset;
3432
3433 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3434
3435 frag->page.p = page;
3436 frag->page_offset = first_offset;
3437 skb_frag_size_set(frag, first_size);
3438
3439 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3440 /* We dont need to clear skbinfo->nr_frags here */
3441
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003442 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003443 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3444 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003445 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003446
3447merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003448 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003449 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003450 unsigned int eat = offset - headlen;
3451
3452 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003453 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003454 skb->data_len -= eat;
3455 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003456 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003457 }
3458
Herbert Xu67147ba2009-05-26 18:50:22 +00003459 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003460
Eric Dumazet29e98242014-05-16 11:34:37 -07003461 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003462 skb_shinfo(p)->frag_list = skb;
3463 else
3464 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003465 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003466 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003467 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003468
Herbert Xu5d38a072009-01-04 16:13:40 -08003469done:
3470 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003471 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003472 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003473 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003474 if (lp != p) {
3475 lp->data_len += len;
3476 lp->truesize += delta_truesize;
3477 lp->len += len;
3478 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003479 NAPI_GRO_CB(skb)->same_flow = 1;
3480 return 0;
3481}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003482EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484void __init skb_init(void)
3485{
3486 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3487 sizeof(struct sk_buff),
3488 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003489 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003490 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003491 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003492 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003493 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003494 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003495 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
David S. Miller51c739d2007-10-30 21:29:29 -07003498static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003499__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3500 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003501{
David S. Miller1a028e52007-04-27 15:21:23 -07003502 int start = skb_headlen(skb);
3503 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003504 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003505 int elt = 0;
3506
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003507 if (unlikely(recursion_level >= 24))
3508 return -EMSGSIZE;
3509
David Howells716ea3a2007-04-02 20:19:53 -07003510 if (copy > 0) {
3511 if (copy > len)
3512 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003513 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003514 elt++;
3515 if ((len -= copy) == 0)
3516 return elt;
3517 offset += copy;
3518 }
3519
3520 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003521 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003522
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003523 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003524
Eric Dumazet9e903e02011-10-18 21:00:24 +00003525 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003526 if ((copy = end - offset) > 0) {
3527 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003528 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3529 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003530
3531 if (copy > len)
3532 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003533 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003534 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003535 elt++;
3536 if (!(len -= copy))
3537 return elt;
3538 offset += copy;
3539 }
David S. Miller1a028e52007-04-27 15:21:23 -07003540 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003541 }
3542
David S. Millerfbb398a2009-06-09 00:18:59 -07003543 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003544 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003545
David S. Millerfbb398a2009-06-09 00:18:59 -07003546 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003547
David S. Millerfbb398a2009-06-09 00:18:59 -07003548 end = start + frag_iter->len;
3549 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003550 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3551 return -EMSGSIZE;
3552
David S. Millerfbb398a2009-06-09 00:18:59 -07003553 if (copy > len)
3554 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003555 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3556 copy, recursion_level + 1);
3557 if (unlikely(ret < 0))
3558 return ret;
3559 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003560 if ((len -= copy) == 0)
3561 return elt;
3562 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003563 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003564 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003565 }
3566 BUG_ON(len);
3567 return elt;
3568}
3569
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003570/**
3571 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3572 * @skb: Socket buffer containing the buffers to be mapped
3573 * @sg: The scatter-gather list to map into
3574 * @offset: The offset into the buffer's contents to start mapping
3575 * @len: Length of buffer space to be mapped
3576 *
3577 * Fill the specified scatter-gather list with mappings/pointers into a
3578 * region of the buffer space attached to a socket buffer. Returns either
3579 * the number of scatterlist items used, or -EMSGSIZE if the contents
3580 * could not fit.
3581 */
3582int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3583{
3584 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3585
3586 if (nsg <= 0)
3587 return nsg;
3588
3589 sg_mark_end(&sg[nsg - 1]);
3590
3591 return nsg;
3592}
3593EXPORT_SYMBOL_GPL(skb_to_sgvec);
3594
Fan Du25a91d82014-01-18 09:54:23 +08003595/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3596 * sglist without mark the sg which contain last skb data as the end.
3597 * So the caller can mannipulate sg list as will when padding new data after
3598 * the first call without calling sg_unmark_end to expend sg list.
3599 *
3600 * Scenario to use skb_to_sgvec_nomark:
3601 * 1. sg_init_table
3602 * 2. skb_to_sgvec_nomark(payload1)
3603 * 3. skb_to_sgvec_nomark(payload2)
3604 *
3605 * This is equivalent to:
3606 * 1. sg_init_table
3607 * 2. skb_to_sgvec(payload1)
3608 * 3. sg_unmark_end
3609 * 4. skb_to_sgvec(payload2)
3610 *
3611 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3612 * is more preferable.
3613 */
3614int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3615 int offset, int len)
3616{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003617 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003618}
3619EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3620
David S. Miller51c739d2007-10-30 21:29:29 -07003621
David S. Miller51c739d2007-10-30 21:29:29 -07003622
David Howells716ea3a2007-04-02 20:19:53 -07003623/**
3624 * skb_cow_data - Check that a socket buffer's data buffers are writable
3625 * @skb: The socket buffer to check.
3626 * @tailbits: Amount of trailing space to be added
3627 * @trailer: Returned pointer to the skb where the @tailbits space begins
3628 *
3629 * Make sure that the data buffers attached to a socket buffer are
3630 * writable. If they are not, private copies are made of the data buffers
3631 * and the socket buffer is set to use these instead.
3632 *
3633 * If @tailbits is given, make sure that there is space to write @tailbits
3634 * bytes of data beyond current end of socket buffer. @trailer will be
3635 * set to point to the skb in which this space begins.
3636 *
3637 * The number of scatterlist elements required to completely map the
3638 * COW'd and extended socket buffer will be returned.
3639 */
3640int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3641{
3642 int copyflag;
3643 int elt;
3644 struct sk_buff *skb1, **skb_p;
3645
3646 /* If skb is cloned or its head is paged, reallocate
3647 * head pulling out all the pages (pages are considered not writable
3648 * at the moment even if they are anonymous).
3649 */
3650 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3651 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3652 return -ENOMEM;
3653
3654 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003655 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003656 /* A little of trouble, not enough of space for trailer.
3657 * This should not happen, when stack is tuned to generate
3658 * good frames. OK, on miss we reallocate and reserve even more
3659 * space, 128 bytes is fair. */
3660
3661 if (skb_tailroom(skb) < tailbits &&
3662 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3663 return -ENOMEM;
3664
3665 /* Voila! */
3666 *trailer = skb;
3667 return 1;
3668 }
3669
3670 /* Misery. We are in troubles, going to mincer fragments... */
3671
3672 elt = 1;
3673 skb_p = &skb_shinfo(skb)->frag_list;
3674 copyflag = 0;
3675
3676 while ((skb1 = *skb_p) != NULL) {
3677 int ntail = 0;
3678
3679 /* The fragment is partially pulled by someone,
3680 * this can happen on input. Copy it and everything
3681 * after it. */
3682
3683 if (skb_shared(skb1))
3684 copyflag = 1;
3685
3686 /* If the skb is the last, worry about trailer. */
3687
3688 if (skb1->next == NULL && tailbits) {
3689 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003690 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003691 skb_tailroom(skb1) < tailbits)
3692 ntail = tailbits + 128;
3693 }
3694
3695 if (copyflag ||
3696 skb_cloned(skb1) ||
3697 ntail ||
3698 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003699 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003700 struct sk_buff *skb2;
3701
3702 /* Fuck, we are miserable poor guys... */
3703 if (ntail == 0)
3704 skb2 = skb_copy(skb1, GFP_ATOMIC);
3705 else
3706 skb2 = skb_copy_expand(skb1,
3707 skb_headroom(skb1),
3708 ntail,
3709 GFP_ATOMIC);
3710 if (unlikely(skb2 == NULL))
3711 return -ENOMEM;
3712
3713 if (skb1->sk)
3714 skb_set_owner_w(skb2, skb1->sk);
3715
3716 /* Looking around. Are we still alive?
3717 * OK, link new skb, drop old one */
3718
3719 skb2->next = skb1->next;
3720 *skb_p = skb2;
3721 kfree_skb(skb1);
3722 skb1 = skb2;
3723 }
3724 elt++;
3725 *trailer = skb1;
3726 skb_p = &skb1->next;
3727 }
3728
3729 return elt;
3730}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003731EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003732
Eric Dumazetb1faf562010-05-31 23:44:05 -07003733static void sock_rmem_free(struct sk_buff *skb)
3734{
3735 struct sock *sk = skb->sk;
3736
3737 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3738}
3739
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003740static void skb_set_err_queue(struct sk_buff *skb)
3741{
3742 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3743 * So, it is safe to (mis)use it to mark skbs on the error queue.
3744 */
3745 skb->pkt_type = PACKET_OUTGOING;
3746 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3747}
3748
Eric Dumazetb1faf562010-05-31 23:44:05 -07003749/*
3750 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3751 */
3752int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3753{
3754 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003755 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003756 return -ENOMEM;
3757
3758 skb_orphan(skb);
3759 skb->sk = sk;
3760 skb->destructor = sock_rmem_free;
3761 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003762 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003763
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003764 /* before exiting rcu section, make sure dst is refcounted */
3765 skb_dst_force(skb);
3766
Eric Dumazetb1faf562010-05-31 23:44:05 -07003767 skb_queue_tail(&sk->sk_error_queue, skb);
3768 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003769 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003770 return 0;
3771}
3772EXPORT_SYMBOL(sock_queue_err_skb);
3773
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003774static bool is_icmp_err_skb(const struct sk_buff *skb)
3775{
3776 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3777 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3778}
3779
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003780struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3781{
3782 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003783 struct sk_buff *skb, *skb_next = NULL;
3784 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003785 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003786
Eric Dumazet997d5c32015-02-18 05:47:55 -08003787 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003788 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b257932017-06-02 12:38:22 -04003789 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003790 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b257932017-06-02 12:38:22 -04003791 if (icmp_next)
3792 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3793 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08003794 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003795
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003796 if (is_icmp_err_skb(skb) && !icmp_next)
3797 sk->sk_err = 0;
3798
3799 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003800 sk->sk_error_report(sk);
3801
3802 return skb;
3803}
3804EXPORT_SYMBOL(sock_dequeue_err_skb);
3805
Alexander Duyckcab41c42014-09-10 18:05:26 -04003806/**
3807 * skb_clone_sk - create clone of skb, and take reference to socket
3808 * @skb: the skb to clone
3809 *
3810 * This function creates a clone of a buffer that holds a reference on
3811 * sk_refcnt. Buffers created via this function are meant to be
3812 * returned using sock_queue_err_skb, or free via kfree_skb.
3813 *
3814 * When passing buffers allocated with this function to sock_queue_err_skb
3815 * it is necessary to wrap the call with sock_hold/sock_put in order to
3816 * prevent the socket from being released prior to being enqueued on
3817 * the sk_error_queue.
3818 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003819struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3820{
3821 struct sock *sk = skb->sk;
3822 struct sk_buff *clone;
3823
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003824 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04003825 return NULL;
3826
3827 clone = skb_clone(skb, GFP_ATOMIC);
3828 if (!clone) {
3829 sock_put(sk);
3830 return NULL;
3831 }
3832
3833 clone->sk = sk;
3834 clone->destructor = sock_efree;
3835
3836 return clone;
3837}
3838EXPORT_SYMBOL(skb_clone_sk);
3839
Alexander Duyck37846ef2014-09-04 13:31:10 -04003840static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3841 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003842 int tstype,
3843 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003844{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003845 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003846 int err;
3847
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003848 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
3849
Patrick Ohlyac45f602009-02-12 05:03:37 +00003850 serr = SKB_EXT_ERR(skb);
3851 memset(serr, 0, sizeof(*serr));
3852 serr->ee.ee_errno = ENOMSG;
3853 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003854 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003855 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04003856 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003857 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003858 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003859 if (sk->sk_protocol == IPPROTO_TCP &&
3860 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003861 serr->ee.ee_data -= sk->sk_tskey;
3862 }
Eric Dumazet29030372010-05-29 00:20:48 -07003863
Patrick Ohlyac45f602009-02-12 05:03:37 +00003864 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003865
Patrick Ohlyac45f602009-02-12 05:03:37 +00003866 if (err)
3867 kfree_skb(skb);
3868}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003869
Willem de Bruijnb245be12015-01-30 13:29:32 -05003870static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3871{
3872 bool ret;
3873
3874 if (likely(sysctl_tstamp_allow_data || tsonly))
3875 return true;
3876
3877 read_lock_bh(&sk->sk_callback_lock);
3878 ret = sk->sk_socket && sk->sk_socket->file &&
3879 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3880 read_unlock_bh(&sk->sk_callback_lock);
3881 return ret;
3882}
3883
Alexander Duyck37846ef2014-09-04 13:31:10 -04003884void skb_complete_tx_timestamp(struct sk_buff *skb,
3885 struct skb_shared_hwtstamps *hwtstamps)
3886{
3887 struct sock *sk = skb->sk;
3888
Willem de Bruijnb245be12015-01-30 13:29:32 -05003889 if (!skb_may_tx_timestamp(sk, false))
3890 return;
3891
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003892 /* Take a reference to prevent skb_orphan() from freeing the socket,
3893 * but only if the socket refcount is not zero.
3894 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003895 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003896 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003897 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003898 sock_put(sk);
3899 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003900}
3901EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3902
3903void __skb_tstamp_tx(struct sk_buff *orig_skb,
3904 struct skb_shared_hwtstamps *hwtstamps,
3905 struct sock *sk, int tstype)
3906{
3907 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003908 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003909
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003910 if (!sk)
3911 return;
3912
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02003913 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
3914 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
3915 return;
3916
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003917 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3918 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003919 return;
3920
Francis Yan1c885802016-11-27 23:07:18 -08003921 if (tsonly) {
3922#ifdef CONFIG_INET
3923 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
3924 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003925 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08003926 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003927 opt_stats = true;
3928 } else
Francis Yan1c885802016-11-27 23:07:18 -08003929#endif
3930 skb = alloc_skb(0, GFP_ATOMIC);
3931 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003932 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08003933 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003934 if (!skb)
3935 return;
3936
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003937 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04003938 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3939 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003940 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3941 }
3942
3943 if (hwtstamps)
3944 *skb_hwtstamps(skb) = *hwtstamps;
3945 else
3946 skb->tstamp = ktime_get_real();
3947
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003948 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003949}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003950EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3951
3952void skb_tstamp_tx(struct sk_buff *orig_skb,
3953 struct skb_shared_hwtstamps *hwtstamps)
3954{
3955 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3956 SCM_TSTAMP_SND);
3957}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003958EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3959
Johannes Berg6e3e9392011-11-09 10:15:42 +01003960void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3961{
3962 struct sock *sk = skb->sk;
3963 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003964 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01003965
3966 skb->wifi_acked_valid = 1;
3967 skb->wifi_acked = acked;
3968
3969 serr = SKB_EXT_ERR(skb);
3970 memset(serr, 0, sizeof(*serr));
3971 serr->ee.ee_errno = ENOMSG;
3972 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3973
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003974 /* Take a reference to prevent skb_orphan() from freeing the socket,
3975 * but only if the socket refcount is not zero.
3976 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003977 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003978 err = sock_queue_err_skb(sk, skb);
3979 sock_put(sk);
3980 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01003981 if (err)
3982 kfree_skb(skb);
3983}
3984EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3985
Rusty Russellf35d9d82008-02-04 23:49:54 -05003986/**
3987 * skb_partial_csum_set - set up and verify partial csum values for packet
3988 * @skb: the skb to set
3989 * @start: the number of bytes after skb->data to start checksumming.
3990 * @off: the offset from start to place the checksum.
3991 *
3992 * For untrusted partially-checksummed packets, we need to make sure the values
3993 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3994 *
3995 * This function checks and sets those values and skb->ip_summed: if this
3996 * returns false you should drop the packet.
3997 */
3998bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3999{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004000 if (unlikely(start > skb_headlen(skb)) ||
4001 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004002 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4003 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004004 return false;
4005 }
4006 skb->ip_summed = CHECKSUM_PARTIAL;
4007 skb->csum_start = skb_headroom(skb) + start;
4008 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004009 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004010 return true;
4011}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004012EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004013
Paul Durranted1f50c2014-01-09 10:02:46 +00004014static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4015 unsigned int max)
4016{
4017 if (skb_headlen(skb) >= len)
4018 return 0;
4019
4020 /* If we need to pullup then pullup to the max, so we
4021 * won't need to do it again.
4022 */
4023 if (max > skb->len)
4024 max = skb->len;
4025
4026 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4027 return -ENOMEM;
4028
4029 if (skb_headlen(skb) < len)
4030 return -EPROTO;
4031
4032 return 0;
4033}
4034
Jan Beulichf9708b42014-03-11 13:56:05 +00004035#define MAX_TCP_HDR_LEN (15 * 4)
4036
4037static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4038 typeof(IPPROTO_IP) proto,
4039 unsigned int off)
4040{
4041 switch (proto) {
4042 int err;
4043
4044 case IPPROTO_TCP:
4045 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4046 off + MAX_TCP_HDR_LEN);
4047 if (!err && !skb_partial_csum_set(skb, off,
4048 offsetof(struct tcphdr,
4049 check)))
4050 err = -EPROTO;
4051 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4052
4053 case IPPROTO_UDP:
4054 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4055 off + sizeof(struct udphdr));
4056 if (!err && !skb_partial_csum_set(skb, off,
4057 offsetof(struct udphdr,
4058 check)))
4059 err = -EPROTO;
4060 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4061 }
4062
4063 return ERR_PTR(-EPROTO);
4064}
4065
Paul Durranted1f50c2014-01-09 10:02:46 +00004066/* This value should be large enough to cover a tagged ethernet header plus
4067 * maximally sized IP and TCP or UDP headers.
4068 */
4069#define MAX_IP_HDR_LEN 128
4070
Jan Beulichf9708b42014-03-11 13:56:05 +00004071static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004072{
4073 unsigned int off;
4074 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004075 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004076 int err;
4077
4078 fragment = false;
4079
4080 err = skb_maybe_pull_tail(skb,
4081 sizeof(struct iphdr),
4082 MAX_IP_HDR_LEN);
4083 if (err < 0)
4084 goto out;
4085
4086 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4087 fragment = true;
4088
4089 off = ip_hdrlen(skb);
4090
4091 err = -EPROTO;
4092
4093 if (fragment)
4094 goto out;
4095
Jan Beulichf9708b42014-03-11 13:56:05 +00004096 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4097 if (IS_ERR(csum))
4098 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004099
Jan Beulichf9708b42014-03-11 13:56:05 +00004100 if (recalculate)
4101 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4102 ip_hdr(skb)->daddr,
4103 skb->len - off,
4104 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004105 err = 0;
4106
4107out:
4108 return err;
4109}
4110
4111/* This value should be large enough to cover a tagged ethernet header plus
4112 * an IPv6 header, all options, and a maximal TCP or UDP header.
4113 */
4114#define MAX_IPV6_HDR_LEN 256
4115
4116#define OPT_HDR(type, skb, off) \
4117 (type *)(skb_network_header(skb) + (off))
4118
4119static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4120{
4121 int err;
4122 u8 nexthdr;
4123 unsigned int off;
4124 unsigned int len;
4125 bool fragment;
4126 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004127 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004128
4129 fragment = false;
4130 done = false;
4131
4132 off = sizeof(struct ipv6hdr);
4133
4134 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4135 if (err < 0)
4136 goto out;
4137
4138 nexthdr = ipv6_hdr(skb)->nexthdr;
4139
4140 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4141 while (off <= len && !done) {
4142 switch (nexthdr) {
4143 case IPPROTO_DSTOPTS:
4144 case IPPROTO_HOPOPTS:
4145 case IPPROTO_ROUTING: {
4146 struct ipv6_opt_hdr *hp;
4147
4148 err = skb_maybe_pull_tail(skb,
4149 off +
4150 sizeof(struct ipv6_opt_hdr),
4151 MAX_IPV6_HDR_LEN);
4152 if (err < 0)
4153 goto out;
4154
4155 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4156 nexthdr = hp->nexthdr;
4157 off += ipv6_optlen(hp);
4158 break;
4159 }
4160 case IPPROTO_AH: {
4161 struct ip_auth_hdr *hp;
4162
4163 err = skb_maybe_pull_tail(skb,
4164 off +
4165 sizeof(struct ip_auth_hdr),
4166 MAX_IPV6_HDR_LEN);
4167 if (err < 0)
4168 goto out;
4169
4170 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4171 nexthdr = hp->nexthdr;
4172 off += ipv6_authlen(hp);
4173 break;
4174 }
4175 case IPPROTO_FRAGMENT: {
4176 struct frag_hdr *hp;
4177
4178 err = skb_maybe_pull_tail(skb,
4179 off +
4180 sizeof(struct frag_hdr),
4181 MAX_IPV6_HDR_LEN);
4182 if (err < 0)
4183 goto out;
4184
4185 hp = OPT_HDR(struct frag_hdr, skb, off);
4186
4187 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4188 fragment = true;
4189
4190 nexthdr = hp->nexthdr;
4191 off += sizeof(struct frag_hdr);
4192 break;
4193 }
4194 default:
4195 done = true;
4196 break;
4197 }
4198 }
4199
4200 err = -EPROTO;
4201
4202 if (!done || fragment)
4203 goto out;
4204
Jan Beulichf9708b42014-03-11 13:56:05 +00004205 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4206 if (IS_ERR(csum))
4207 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004208
Jan Beulichf9708b42014-03-11 13:56:05 +00004209 if (recalculate)
4210 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4211 &ipv6_hdr(skb)->daddr,
4212 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004213 err = 0;
4214
4215out:
4216 return err;
4217}
4218
4219/**
4220 * skb_checksum_setup - set up partial checksum offset
4221 * @skb: the skb to set up
4222 * @recalculate: if true the pseudo-header checksum will be recalculated
4223 */
4224int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4225{
4226 int err;
4227
4228 switch (skb->protocol) {
4229 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004230 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004231 break;
4232
4233 case htons(ETH_P_IPV6):
4234 err = skb_checksum_setup_ipv6(skb, recalculate);
4235 break;
4236
4237 default:
4238 err = -EPROTO;
4239 break;
4240 }
4241
4242 return err;
4243}
4244EXPORT_SYMBOL(skb_checksum_setup);
4245
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004246/**
4247 * skb_checksum_maybe_trim - maybe trims the given skb
4248 * @skb: the skb to check
4249 * @transport_len: the data length beyond the network header
4250 *
4251 * Checks whether the given skb has data beyond the given transport length.
4252 * If so, returns a cloned skb trimmed to this transport length.
4253 * Otherwise returns the provided skb. Returns NULL in error cases
4254 * (e.g. transport_len exceeds skb length or out-of-memory).
4255 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004256 * Caller needs to set the skb transport header and free any returned skb if it
4257 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004258 */
4259static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4260 unsigned int transport_len)
4261{
4262 struct sk_buff *skb_chk;
4263 unsigned int len = skb_transport_offset(skb) + transport_len;
4264 int ret;
4265
Linus Lüssinga5169932015-08-13 05:54:07 +02004266 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004267 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004268 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004269 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004270
4271 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004272 if (!skb_chk)
4273 return NULL;
4274
4275 ret = pskb_trim_rcsum(skb_chk, len);
4276 if (ret) {
4277 kfree_skb(skb_chk);
4278 return NULL;
4279 }
4280
4281 return skb_chk;
4282}
4283
4284/**
4285 * skb_checksum_trimmed - validate checksum of an skb
4286 * @skb: the skb to check
4287 * @transport_len: the data length beyond the network header
4288 * @skb_chkf: checksum function to use
4289 *
4290 * Applies the given checksum function skb_chkf to the provided skb.
4291 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4292 *
4293 * If the skb has data beyond the given transport length, then a
4294 * trimmed & cloned skb is checked and returned.
4295 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004296 * Caller needs to set the skb transport header and free any returned skb if it
4297 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004298 */
4299struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4300 unsigned int transport_len,
4301 __sum16(*skb_chkf)(struct sk_buff *skb))
4302{
4303 struct sk_buff *skb_chk;
4304 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004305 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004306
4307 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4308 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004309 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004310
Linus Lüssinga5169932015-08-13 05:54:07 +02004311 if (!pskb_may_pull(skb_chk, offset))
4312 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004313
Linus Lüssing9b368812016-02-24 04:21:42 +01004314 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004315 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004316 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004317
Linus Lüssinga5169932015-08-13 05:54:07 +02004318 if (ret)
4319 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004320
4321 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004322
4323err:
4324 if (skb_chk && skb_chk != skb)
4325 kfree_skb(skb_chk);
4326
4327 return NULL;
4328
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004329}
4330EXPORT_SYMBOL(skb_checksum_trimmed);
4331
Ben Hutchings4497b072008-06-19 16:22:28 -07004332void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4333{
Joe Perchese87cc472012-05-13 21:56:26 +00004334 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4335 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004336}
Ben Hutchings4497b072008-06-19 16:22:28 -07004337EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004338
4339void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4340{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004341 if (head_stolen) {
4342 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004343 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004344 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004345 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004346 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004347}
4348EXPORT_SYMBOL(kfree_skb_partial);
4349
4350/**
4351 * skb_try_coalesce - try to merge skb to prior one
4352 * @to: prior buffer
4353 * @from: buffer to add
4354 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004355 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004356 */
4357bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4358 bool *fragstolen, int *delta_truesize)
4359{
4360 int i, delta, len = from->len;
4361
4362 *fragstolen = false;
4363
4364 if (skb_cloned(to))
4365 return false;
4366
4367 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004368 if (len)
4369 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004370 *delta_truesize = 0;
4371 return true;
4372 }
4373
4374 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4375 return false;
4376
4377 if (skb_headlen(from) != 0) {
4378 struct page *page;
4379 unsigned int offset;
4380
4381 if (skb_shinfo(to)->nr_frags +
4382 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4383 return false;
4384
4385 if (skb_head_is_locked(from))
4386 return false;
4387
4388 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4389
4390 page = virt_to_head_page(from->head);
4391 offset = from->data - (unsigned char *)page_address(page);
4392
4393 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4394 page, offset, skb_headlen(from));
4395 *fragstolen = true;
4396 } else {
4397 if (skb_shinfo(to)->nr_frags +
4398 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4399 return false;
4400
Weiping Panf4b549a2012-09-28 20:15:30 +00004401 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004402 }
4403
4404 WARN_ON_ONCE(delta < len);
4405
4406 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4407 skb_shinfo(from)->frags,
4408 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4409 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4410
4411 if (!skb_cloned(from))
4412 skb_shinfo(from)->nr_frags = 0;
4413
Li RongQing8ea853f2012-09-18 16:53:21 +00004414 /* if the skb is not cloned this does nothing
4415 * since we set nr_frags to 0.
4416 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004417 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4418 skb_frag_ref(from, i);
4419
4420 to->truesize += delta;
4421 to->len += len;
4422 to->data_len += len;
4423
4424 *delta_truesize = delta;
4425 return true;
4426}
4427EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004428
4429/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004430 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004431 *
4432 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004433 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004434 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004435 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4436 * into/from a tunnel. Some information have to be cleared during these
4437 * operations.
4438 * skb_scrub_packet can also be used to clean a skb before injecting it in
4439 * another namespace (@xnet == true). We have to clear all information in the
4440 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004441 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004442void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004443{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004444 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004445 skb->pkt_type = PACKET_HOST;
4446 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004447 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004448 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004449 secpath_reset(skb);
4450 nf_reset(skb);
4451 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004452
4453 if (!xnet)
4454 return;
4455
4456 skb_orphan(skb);
4457 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004458}
4459EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004460
4461/**
4462 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4463 *
4464 * @skb: GSO skb
4465 *
4466 * skb_gso_transport_seglen is used to determine the real size of the
4467 * individual segments, including Layer4 headers (TCP/UDP).
4468 *
4469 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4470 */
4471unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4472{
4473 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004474 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004475
Florian Westphalf993bc22014-10-20 13:49:18 +02004476 if (skb->encapsulation) {
4477 thlen = skb_inner_transport_header(skb) -
4478 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004479
Florian Westphalf993bc22014-10-20 13:49:18 +02004480 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4481 thlen += inner_tcp_hdrlen(skb);
4482 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4483 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004484 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4485 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004486 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004487 /* UFO sets gso_size to the size of the fragmentation
4488 * payload, i.e. the size of the L4 (UDP) header is already
4489 * accounted for.
4490 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004491 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004492}
4493EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004494
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004495/**
4496 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4497 *
4498 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004499 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004500 *
4501 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4502 * once split.
4503 */
4504bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4505{
4506 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4507 const struct sk_buff *iter;
4508 unsigned int hlen;
4509
4510 hlen = skb_gso_network_seglen(skb);
4511
4512 if (shinfo->gso_size != GSO_BY_FRAGS)
4513 return hlen <= mtu;
4514
4515 /* Undo this so we can re-use header sizes */
4516 hlen -= GSO_BY_FRAGS;
4517
4518 skb_walk_frags(skb, iter) {
4519 if (hlen + skb_headlen(iter) > mtu)
4520 return false;
4521 }
4522
4523 return true;
4524}
4525EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4526
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004527static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4528{
4529 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4530 kfree_skb(skb);
4531 return NULL;
4532 }
4533
Vlad Yasevichf6548612015-12-14 17:44:10 -05004534 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004535 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004536 skb->mac_header += VLAN_HLEN;
4537 return skb;
4538}
4539
4540struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4541{
4542 struct vlan_hdr *vhdr;
4543 u16 vlan_tci;
4544
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004545 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004546 /* vlan_tci is already set-up so leave this for another time */
4547 return skb;
4548 }
4549
4550 skb = skb_share_check(skb, GFP_ATOMIC);
4551 if (unlikely(!skb))
4552 goto err_free;
4553
4554 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4555 goto err_free;
4556
4557 vhdr = (struct vlan_hdr *)skb->data;
4558 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4559 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4560
4561 skb_pull_rcsum(skb, VLAN_HLEN);
4562 vlan_set_encap_proto(skb, vhdr);
4563
4564 skb = skb_reorder_vlan_header(skb);
4565 if (unlikely(!skb))
4566 goto err_free;
4567
4568 skb_reset_network_header(skb);
4569 skb_reset_transport_header(skb);
4570 skb_reset_mac_len(skb);
4571
4572 return skb;
4573
4574err_free:
4575 kfree_skb(skb);
4576 return NULL;
4577}
4578EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004579
Jiri Pirkoe2195122014-11-19 14:05:01 +01004580int skb_ensure_writable(struct sk_buff *skb, int write_len)
4581{
4582 if (!pskb_may_pull(skb, write_len))
4583 return -ENOMEM;
4584
4585 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4586 return 0;
4587
4588 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4589}
4590EXPORT_SYMBOL(skb_ensure_writable);
4591
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004592/* remove VLAN header from packet and update csum accordingly.
4593 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4594 */
4595int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004596{
4597 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004598 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004599 int err;
4600
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004601 if (WARN_ONCE(offset,
4602 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4603 offset)) {
4604 return -EINVAL;
4605 }
4606
Jiri Pirko93515d52014-11-19 14:05:02 +01004607 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4608 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004609 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004610
4611 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4612
4613 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4614 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4615
4616 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4617 __skb_pull(skb, VLAN_HLEN);
4618
4619 vlan_set_encap_proto(skb, vhdr);
4620 skb->mac_header += VLAN_HLEN;
4621
4622 if (skb_network_offset(skb) < ETH_HLEN)
4623 skb_set_network_header(skb, ETH_HLEN);
4624
4625 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004626
4627 return err;
4628}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004629EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004630
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004631/* Pop a vlan tag either from hwaccel or from payload.
4632 * Expects skb->data at mac header.
4633 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004634int skb_vlan_pop(struct sk_buff *skb)
4635{
4636 u16 vlan_tci;
4637 __be16 vlan_proto;
4638 int err;
4639
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004640 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004641 skb->vlan_tci = 0;
4642 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004643 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004644 return 0;
4645
4646 err = __skb_vlan_pop(skb, &vlan_tci);
4647 if (err)
4648 return err;
4649 }
4650 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004651 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004652 return 0;
4653
4654 vlan_proto = skb->protocol;
4655 err = __skb_vlan_pop(skb, &vlan_tci);
4656 if (unlikely(err))
4657 return err;
4658
4659 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4660 return 0;
4661}
4662EXPORT_SYMBOL(skb_vlan_pop);
4663
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004664/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4665 * Expects skb->data at mac header.
4666 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004667int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4668{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004669 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004670 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004671 int err;
4672
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004673 if (WARN_ONCE(offset,
4674 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4675 offset)) {
4676 return -EINVAL;
4677 }
4678
Jiri Pirko93515d52014-11-19 14:05:02 +01004679 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004680 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004681 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004682 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004683
Jiri Pirko93515d52014-11-19 14:05:02 +01004684 skb->protocol = skb->vlan_proto;
4685 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004686
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004687 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004688 }
4689 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4690 return 0;
4691}
4692EXPORT_SYMBOL(skb_vlan_push);
4693
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004694/**
4695 * alloc_skb_with_frags - allocate skb with page frags
4696 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004697 * @header_len: size of linear part
4698 * @data_len: needed length in frags
4699 * @max_page_order: max page order desired.
4700 * @errcode: pointer to error code if any
4701 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004702 *
4703 * This can be used to allocate a paged skb, given a maximal order for frags.
4704 */
4705struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4706 unsigned long data_len,
4707 int max_page_order,
4708 int *errcode,
4709 gfp_t gfp_mask)
4710{
4711 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4712 unsigned long chunk;
4713 struct sk_buff *skb;
4714 struct page *page;
4715 gfp_t gfp_head;
4716 int i;
4717
4718 *errcode = -EMSGSIZE;
4719 /* Note this test could be relaxed, if we succeed to allocate
4720 * high order pages...
4721 */
4722 if (npages > MAX_SKB_FRAGS)
4723 return NULL;
4724
4725 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004726 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07004727 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004728
4729 *errcode = -ENOBUFS;
4730 skb = alloc_skb(header_len, gfp_head);
4731 if (!skb)
4732 return NULL;
4733
4734 skb->truesize += npages << PAGE_SHIFT;
4735
4736 for (i = 0; npages > 0; i++) {
4737 int order = max_page_order;
4738
4739 while (order) {
4740 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004741 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004742 __GFP_COMP |
4743 __GFP_NOWARN |
4744 __GFP_NORETRY,
4745 order);
4746 if (page)
4747 goto fill_page;
4748 /* Do not retry other high order allocations */
4749 order = 1;
4750 max_page_order = 0;
4751 }
4752 order--;
4753 }
4754 page = alloc_page(gfp_mask);
4755 if (!page)
4756 goto failure;
4757fill_page:
4758 chunk = min_t(unsigned long, data_len,
4759 PAGE_SIZE << order);
4760 skb_fill_page_desc(skb, i, page, 0, chunk);
4761 data_len -= chunk;
4762 npages -= 1 << order;
4763 }
4764 return skb;
4765
4766failure:
4767 kfree_skb(skb);
4768 return NULL;
4769}
4770EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004771
4772/* carve out the first off bytes from skb when off < headlen */
4773static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4774 const int headlen, gfp_t gfp_mask)
4775{
4776 int i;
4777 int size = skb_end_offset(skb);
4778 int new_hlen = headlen - off;
4779 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004780
4781 size = SKB_DATA_ALIGN(size);
4782
4783 if (skb_pfmemalloc(skb))
4784 gfp_mask |= __GFP_MEMALLOC;
4785 data = kmalloc_reserve(size +
4786 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4787 gfp_mask, NUMA_NO_NODE, NULL);
4788 if (!data)
4789 return -ENOMEM;
4790
4791 size = SKB_WITH_OVERHEAD(ksize(data));
4792
4793 /* Copy real data, and all frags */
4794 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4795 skb->len -= off;
4796
4797 memcpy((struct skb_shared_info *)(data + size),
4798 skb_shinfo(skb),
4799 offsetof(struct skb_shared_info,
4800 frags[skb_shinfo(skb)->nr_frags]));
4801 if (skb_cloned(skb)) {
4802 /* drop the old head gracefully */
4803 if (skb_orphan_frags(skb, gfp_mask)) {
4804 kfree(data);
4805 return -ENOMEM;
4806 }
4807 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4808 skb_frag_ref(skb, i);
4809 if (skb_has_frag_list(skb))
4810 skb_clone_fraglist(skb);
4811 skb_release_data(skb);
4812 } else {
4813 /* we can reuse existing recount- all we did was
4814 * relocate values
4815 */
4816 skb_free_head(skb);
4817 }
4818
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004819 skb->head = data;
4820 skb->data = data;
4821 skb->head_frag = 0;
4822#ifdef NET_SKBUFF_DATA_USES_OFFSET
4823 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004824#else
4825 skb->end = skb->head + size;
4826#endif
4827 skb_set_tail_pointer(skb, skb_headlen(skb));
4828 skb_headers_offset_update(skb, 0);
4829 skb->cloned = 0;
4830 skb->hdr_len = 0;
4831 skb->nohdr = 0;
4832 atomic_set(&skb_shinfo(skb)->dataref, 1);
4833
4834 return 0;
4835}
4836
4837static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4838
4839/* carve out the first eat bytes from skb's frag_list. May recurse into
4840 * pskb_carve()
4841 */
4842static int pskb_carve_frag_list(struct sk_buff *skb,
4843 struct skb_shared_info *shinfo, int eat,
4844 gfp_t gfp_mask)
4845{
4846 struct sk_buff *list = shinfo->frag_list;
4847 struct sk_buff *clone = NULL;
4848 struct sk_buff *insp = NULL;
4849
4850 do {
4851 if (!list) {
4852 pr_err("Not enough bytes to eat. Want %d\n", eat);
4853 return -EFAULT;
4854 }
4855 if (list->len <= eat) {
4856 /* Eaten as whole. */
4857 eat -= list->len;
4858 list = list->next;
4859 insp = list;
4860 } else {
4861 /* Eaten partially. */
4862 if (skb_shared(list)) {
4863 clone = skb_clone(list, gfp_mask);
4864 if (!clone)
4865 return -ENOMEM;
4866 insp = list->next;
4867 list = clone;
4868 } else {
4869 /* This may be pulled without problems. */
4870 insp = list;
4871 }
4872 if (pskb_carve(list, eat, gfp_mask) < 0) {
4873 kfree_skb(clone);
4874 return -ENOMEM;
4875 }
4876 break;
4877 }
4878 } while (eat);
4879
4880 /* Free pulled out fragments. */
4881 while ((list = shinfo->frag_list) != insp) {
4882 shinfo->frag_list = list->next;
4883 kfree_skb(list);
4884 }
4885 /* And insert new clone at head. */
4886 if (clone) {
4887 clone->next = list;
4888 shinfo->frag_list = clone;
4889 }
4890 return 0;
4891}
4892
4893/* carve off first len bytes from skb. Split line (off) is in the
4894 * non-linear part of skb
4895 */
4896static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4897 int pos, gfp_t gfp_mask)
4898{
4899 int i, k = 0;
4900 int size = skb_end_offset(skb);
4901 u8 *data;
4902 const int nfrags = skb_shinfo(skb)->nr_frags;
4903 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004904
4905 size = SKB_DATA_ALIGN(size);
4906
4907 if (skb_pfmemalloc(skb))
4908 gfp_mask |= __GFP_MEMALLOC;
4909 data = kmalloc_reserve(size +
4910 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4911 gfp_mask, NUMA_NO_NODE, NULL);
4912 if (!data)
4913 return -ENOMEM;
4914
4915 size = SKB_WITH_OVERHEAD(ksize(data));
4916
4917 memcpy((struct skb_shared_info *)(data + size),
4918 skb_shinfo(skb), offsetof(struct skb_shared_info,
4919 frags[skb_shinfo(skb)->nr_frags]));
4920 if (skb_orphan_frags(skb, gfp_mask)) {
4921 kfree(data);
4922 return -ENOMEM;
4923 }
4924 shinfo = (struct skb_shared_info *)(data + size);
4925 for (i = 0; i < nfrags; i++) {
4926 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4927
4928 if (pos + fsize > off) {
4929 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4930
4931 if (pos < off) {
4932 /* Split frag.
4933 * We have two variants in this case:
4934 * 1. Move all the frag to the second
4935 * part, if it is possible. F.e.
4936 * this approach is mandatory for TUX,
4937 * where splitting is expensive.
4938 * 2. Split is accurately. We make this.
4939 */
4940 shinfo->frags[0].page_offset += off - pos;
4941 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4942 }
4943 skb_frag_ref(skb, i);
4944 k++;
4945 }
4946 pos += fsize;
4947 }
4948 shinfo->nr_frags = k;
4949 if (skb_has_frag_list(skb))
4950 skb_clone_fraglist(skb);
4951
4952 if (k == 0) {
4953 /* split line is in frag list */
4954 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4955 }
4956 skb_release_data(skb);
4957
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004958 skb->head = data;
4959 skb->head_frag = 0;
4960 skb->data = data;
4961#ifdef NET_SKBUFF_DATA_USES_OFFSET
4962 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004963#else
4964 skb->end = skb->head + size;
4965#endif
4966 skb_reset_tail_pointer(skb);
4967 skb_headers_offset_update(skb, 0);
4968 skb->cloned = 0;
4969 skb->hdr_len = 0;
4970 skb->nohdr = 0;
4971 skb->len -= off;
4972 skb->data_len = skb->len;
4973 atomic_set(&skb_shinfo(skb)->dataref, 1);
4974 return 0;
4975}
4976
4977/* remove len bytes from the beginning of the skb */
4978static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4979{
4980 int headlen = skb_headlen(skb);
4981
4982 if (len < headlen)
4983 return pskb_carve_inside_header(skb, len, headlen, gfp);
4984 else
4985 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4986}
4987
4988/* Extract to_copy bytes starting at off from skb, and return this in
4989 * a new skb
4990 */
4991struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
4992 int to_copy, gfp_t gfp)
4993{
4994 struct sk_buff *clone = skb_clone(skb, gfp);
4995
4996 if (!clone)
4997 return NULL;
4998
4999 if (pskb_carve(clone, off, gfp) < 0 ||
5000 pskb_trim(clone, to_copy)) {
5001 kfree_skb(clone);
5002 return NULL;
5003 }
5004 return clone;
5005}
5006EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005007
5008/**
5009 * skb_condense - try to get rid of fragments/frag_list if possible
5010 * @skb: buffer
5011 *
5012 * Can be used to save memory before skb is added to a busy queue.
5013 * If packet has bytes in frags and enough tail room in skb->head,
5014 * pull all of them, so that we can free the frags right now and adjust
5015 * truesize.
5016 * Notes:
5017 * We do not reallocate skb->head thus can not fail.
5018 * Caller must re-evaluate skb->truesize if needed.
5019 */
5020void skb_condense(struct sk_buff *skb)
5021{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005022 if (skb->data_len) {
5023 if (skb->data_len > skb->end - skb->tail ||
5024 skb_cloned(skb))
5025 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005026
Eric Dumazet3174fed2016-12-09 08:02:05 -08005027 /* Nice, we can free page frag(s) right now */
5028 __pskb_pull_tail(skb, skb->data_len);
5029 }
5030 /* At this point, skb->truesize might be over estimated,
5031 * because skb had a fragment, and fragments do not tell
5032 * their truesize.
5033 * When we pulled its content into skb->head, fragment
5034 * was freed, but __pskb_pull_tail() could not possibly
5035 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005036 */
5037 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5038}