blob: a95877a8ac8badfcdee6e6b98ab7cd1c34cff89d [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{
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400935 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
Shirley Maa6686f22011-07-06 12:22:12 +0000936 int num_frags = skb_shinfo(skb)->nr_frags;
937 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400938 int i, new_frags;
939 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +0000940
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400941 if (!num_frags)
942 return 0;
Shirley Maa6686f22011-07-06 12:22:12 +0000943
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400944 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
945 return -EINVAL;
946
947 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
948 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +0000949 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000950 if (!page) {
951 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900952 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000953 put_page(head);
954 head = next;
955 }
956 return -ENOMEM;
957 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400958 set_page_private(page, (unsigned long)head);
959 head = page;
960 }
961
962 page = head;
963 d_off = 0;
964 for (i = 0; i < num_frags; i++) {
965 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
966 u32 p_off, p_len, copied;
967 struct page *p;
968 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -0400969
970 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
971 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400972 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -0400973 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400974
975 while (done < p_len) {
976 if (d_off == PAGE_SIZE) {
977 d_off = 0;
978 page = (struct page *)page_private(page);
979 }
980 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
981 memcpy(page_address(page) + d_off,
982 vaddr + p_off + done, copy);
983 done += copy;
984 d_off += copy;
985 }
Willem de Bruijnc613c202017-07-31 08:15:47 -0400986 kunmap_atomic(vaddr);
987 }
Shirley Maa6686f22011-07-06 12:22:12 +0000988 }
989
990 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000991 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000992 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000993
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000994 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000995
996 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -0400997 for (i = 0; i < new_frags - 1; i++) {
998 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900999 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001000 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001001 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1002 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001003
1004 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +00001005 return 0;
1006}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001007EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001008
Herbert Xue0053ec2007-10-14 00:37:52 -07001009/**
1010 * skb_clone - duplicate an sk_buff
1011 * @skb: buffer to clone
1012 * @gfp_mask: allocation priority
1013 *
1014 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1015 * copies share the same packet data but not structure. The new
1016 * buffer has a reference count of 1. If the allocation fails the
1017 * function returns %NULL otherwise the new buffer is returned.
1018 *
1019 * If this function is called from an interrupt gfp_mask() must be
1020 * %GFP_ATOMIC.
1021 */
1022
1023struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1024{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001025 struct sk_buff_fclones *fclones = container_of(skb,
1026 struct sk_buff_fclones,
1027 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001028 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001029
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001030 if (skb_orphan_frags(skb, gfp_mask))
1031 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001032
Herbert Xue0053ec2007-10-14 00:37:52 -07001033 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001034 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001035 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001036 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001037 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001038 if (skb_pfmemalloc(skb))
1039 gfp_mask |= __GFP_MEMALLOC;
1040
Herbert Xue0053ec2007-10-14 00:37:52 -07001041 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1042 if (!n)
1043 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001044
1045 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001046 n->fclone = SKB_FCLONE_UNAVAILABLE;
1047 }
1048
1049 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001051EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001053static void skb_headers_offset_update(struct sk_buff *skb, int off)
1054{
Eric Dumazet030737b2013-10-19 11:42:54 -07001055 /* Only adjust this if it actually is csum_start rather than csum */
1056 if (skb->ip_summed == CHECKSUM_PARTIAL)
1057 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001058 /* {transport,network,mac}_header and tail are relative to skb->head */
1059 skb->transport_header += off;
1060 skb->network_header += off;
1061 if (skb_mac_header_was_set(skb))
1062 skb->mac_header += off;
1063 skb->inner_transport_header += off;
1064 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001065 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1069{
Herbert Xudec18812007-10-14 00:37:30 -07001070 __copy_skb_header(new, old);
1071
Herbert Xu79671682006-06-22 02:40:14 -07001072 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1073 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1074 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Mel Gormanc93bdd02012-07-31 16:44:19 -07001077static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1078{
1079 if (skb_pfmemalloc(skb))
1080 return SKB_ALLOC_RX;
1081 return 0;
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/**
1085 * skb_copy - create private copy of an sk_buff
1086 * @skb: buffer to copy
1087 * @gfp_mask: allocation priority
1088 *
1089 * Make a copy of both an &sk_buff and its data. This is used when the
1090 * caller wishes to modify the data and needs a private copy of the
1091 * data to alter. Returns %NULL on failure or the pointer to the buffer
1092 * on success. The returned buffer has a reference count of 1.
1093 *
1094 * As by-product this function converts non-linear &sk_buff to linear
1095 * one, so that &sk_buff becomes completely private and caller is allowed
1096 * to modify all the data of returned buffer. This means that this
1097 * function is not recommended for use in circumstances when only
1098 * header is going to be modified. Use pskb_copy() instead.
1099 */
1100
Al Virodd0fc662005-10-07 07:46:04 +01001101struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001103 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001104 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001105 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1106 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!n)
1109 return NULL;
1110
1111 /* Set the data pointer */
1112 skb_reserve(n, headerlen);
1113 /* Set the tail pointer and length */
1114 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1117 BUG();
1118
1119 copy_skb_header(n, skb);
1120 return n;
1121}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001122EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001125 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001127 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001129 * @fclone: if true allocate the copy of the skb from the fclone
1130 * cache instead of the head cache; it is recommended to set this
1131 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *
1133 * Make a copy of both an &sk_buff and part of its data, located
1134 * in header. Fragmented data remain shared. This is used when
1135 * the caller wishes to modify only header of &sk_buff and needs
1136 * private copy of the header to alter. Returns %NULL on failure
1137 * or the pointer to the buffer on success.
1138 * The returned buffer has a reference count of 1.
1139 */
1140
Octavian Purdilabad93e92014-06-12 01:36:26 +03001141struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1142 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Eric Dumazet117632e2011-12-03 21:39:53 +00001144 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001145 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1146 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!n)
1149 goto out;
1150
1151 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001152 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Set the tail pointer and length */
1154 skb_put(n, skb_headlen(skb));
1155 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001156 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Herbert Xu25f484a2006-11-07 14:57:15 -08001158 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 n->data_len = skb->data_len;
1160 n->len = skb->len;
1161
1162 if (skb_shinfo(skb)->nr_frags) {
1163 int i;
1164
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001165 if (skb_orphan_frags(skb, gfp_mask)) {
1166 kfree_skb(n);
1167 n = NULL;
1168 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1171 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001172 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 skb_shinfo(n)->nr_frags = i;
1175 }
1176
David S. Miller21dc3302010-08-23 00:13:46 -07001177 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1179 skb_clone_fraglist(n);
1180 }
1181
1182 copy_skb_header(n, skb);
1183out:
1184 return n;
1185}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001186EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188/**
1189 * pskb_expand_head - reallocate header of &sk_buff
1190 * @skb: buffer to reallocate
1191 * @nhead: room to add at head
1192 * @ntail: room to add at tail
1193 * @gfp_mask: allocation priority
1194 *
Mathias Krausebc323832013-11-07 14:18:26 +01001195 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1196 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * reference count of 1. Returns zero in the case of success or error,
1198 * if expansion failed. In the last case, &sk_buff is not changed.
1199 *
1200 * All the pointers pointing into skb header may change and must be
1201 * reloaded after call to this function.
1202 */
1203
Victor Fusco86a76ca2005-07-08 14:57:47 -07001204int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001205 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Eric Dumazet158f3232017-01-27 07:11:27 -08001207 int i, osize = skb_end_offset(skb);
1208 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001210 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Herbert Xu4edd87a2008-10-01 07:09:38 -07001212 BUG_ON(nhead < 0);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (skb_shared(skb))
1215 BUG();
1216
1217 size = SKB_DATA_ALIGN(size);
1218
Mel Gormanc93bdd02012-07-31 16:44:19 -07001219 if (skb_pfmemalloc(skb))
1220 gfp_mask |= __GFP_MEMALLOC;
1221 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1222 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!data)
1224 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001225 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001228 * optimized for the cases when header is void.
1229 */
1230 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1231
1232 memcpy((struct skb_shared_info *)(data + size),
1233 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001234 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Alexander Duyck3e245912012-05-04 14:26:51 +00001236 /*
1237 * if shinfo is shared we must drop the old head gracefully, but if it
1238 * is not we can just drop the old head and let the existing refcount
1239 * be since all we did is relocate the values
1240 */
1241 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001242 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001243 if (skb_orphan_frags(skb, gfp_mask))
1244 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001245 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001246 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Eric Dumazet1fd63042010-09-02 23:09:32 +00001248 if (skb_has_frag_list(skb))
1249 skb_clone_fraglist(skb);
1250
1251 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001252 } else {
1253 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 off = (data + nhead) - skb->head;
1256
1257 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001258 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001260#ifdef NET_SKBUFF_DATA_USES_OFFSET
1261 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001262 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001263#else
1264 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001265#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001266 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001267 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001269 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 skb->nohdr = 0;
1271 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001272
1273 /* It is not generally safe to change skb->truesize.
1274 * For the moment, we really care of rx path, or
1275 * when skb is orphaned (not attached to a socket).
1276 */
1277 if (!skb->sk || skb->destructor == sock_edemux)
1278 skb->truesize += size - osize;
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281
Shirley Maa6686f22011-07-06 12:22:12 +00001282nofrags:
1283 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284nodata:
1285 return -ENOMEM;
1286}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001287EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/* Make private copy of skb with writable head and some headroom */
1290
1291struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1292{
1293 struct sk_buff *skb2;
1294 int delta = headroom - skb_headroom(skb);
1295
1296 if (delta <= 0)
1297 skb2 = pskb_copy(skb, GFP_ATOMIC);
1298 else {
1299 skb2 = skb_clone(skb, GFP_ATOMIC);
1300 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1301 GFP_ATOMIC)) {
1302 kfree_skb(skb2);
1303 skb2 = NULL;
1304 }
1305 }
1306 return skb2;
1307}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001308EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310/**
1311 * skb_copy_expand - copy and expand sk_buff
1312 * @skb: buffer to copy
1313 * @newheadroom: new free bytes at head
1314 * @newtailroom: new free bytes at tail
1315 * @gfp_mask: allocation priority
1316 *
1317 * Make a copy of both an &sk_buff and its data and while doing so
1318 * allocate additional space.
1319 *
1320 * This is used when the caller wishes to modify the data and needs a
1321 * private copy of the data to alter as well as more space for new fields.
1322 * Returns %NULL on failure or the pointer to the buffer
1323 * on success. The returned buffer has a reference count of 1.
1324 *
1325 * You must pass %GFP_ATOMIC as the allocation priority if this function
1326 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
1328struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001329 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001330 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 /*
1333 * Allocate the copy buffer
1334 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001335 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1336 gfp_mask, skb_alloc_rx_flag(skb),
1337 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001338 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int head_copy_len, head_copy_off;
1340
1341 if (!n)
1342 return NULL;
1343
1344 skb_reserve(n, newheadroom);
1345
1346 /* Set the tail pointer and length */
1347 skb_put(n, skb->len);
1348
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001349 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 head_copy_off = 0;
1351 if (newheadroom <= head_copy_len)
1352 head_copy_len = newheadroom;
1353 else
1354 head_copy_off = newheadroom - head_copy_len;
1355
1356 /* Copy the linear header and data. */
1357 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1358 skb->len + head_copy_len))
1359 BUG();
1360
1361 copy_skb_header(n, skb);
1362
Eric Dumazet030737b2013-10-19 11:42:54 -07001363 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return n;
1366}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001367EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369/**
1370 * skb_pad - zero pad the tail of an skb
1371 * @skb: buffer to pad
1372 * @pad: space to pad
1373 *
1374 * Ensure that a buffer is followed by a padding area that is zero
1375 * filled. Used by network drivers which may DMA or transfer data
1376 * beyond the buffer end onto the wire.
1377 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001378 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001380
Herbert Xu5b057c62006-06-23 02:06:41 -07001381int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Herbert Xu5b057c62006-06-23 02:06:41 -07001383 int err;
1384 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001387 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001391
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001392 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001393 if (likely(skb_cloned(skb) || ntail > 0)) {
1394 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1395 if (unlikely(err))
1396 goto free_skb;
1397 }
1398
1399 /* FIXME: The use of this function with non-linear skb's really needs
1400 * to be audited.
1401 */
1402 err = skb_linearize(skb);
1403 if (unlikely(err))
1404 goto free_skb;
1405
1406 memset(skb->data + skb->len, 0, pad);
1407 return 0;
1408
1409free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001411 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001412}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001413EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001414
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001415/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001416 * pskb_put - add data to the tail of a potentially fragmented buffer
1417 * @skb: start of the buffer to use
1418 * @tail: tail fragment of the buffer to use
1419 * @len: amount of data to add
1420 *
1421 * This function extends the used data area of the potentially
1422 * fragmented buffer. @tail must be the last fragment of @skb -- or
1423 * @skb itself. If this would exceed the total buffer size the kernel
1424 * will panic. A pointer to the first byte of the extra data is
1425 * returned.
1426 */
1427
Johannes Berg4df864c2017-06-16 14:29:21 +02001428void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001429{
1430 if (tail != skb) {
1431 skb->data_len += len;
1432 skb->len += len;
1433 }
1434 return skb_put(tail, len);
1435}
1436EXPORT_SYMBOL_GPL(pskb_put);
1437
1438/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001439 * skb_put - add data to a buffer
1440 * @skb: buffer to use
1441 * @len: amount of data to add
1442 *
1443 * This function extends the used data area of the buffer. If this would
1444 * exceed the total buffer size the kernel will panic. A pointer to the
1445 * first byte of the extra data is returned.
1446 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001447void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001448{
Johannes Berg4df864c2017-06-16 14:29:21 +02001449 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001450 SKB_LINEAR_ASSERT(skb);
1451 skb->tail += len;
1452 skb->len += len;
1453 if (unlikely(skb->tail > skb->end))
1454 skb_over_panic(skb, len, __builtin_return_address(0));
1455 return tmp;
1456}
1457EXPORT_SYMBOL(skb_put);
1458
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001459/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001460 * skb_push - add data to the start of a buffer
1461 * @skb: buffer to use
1462 * @len: amount of data to add
1463 *
1464 * This function extends the used data area of the buffer at the buffer
1465 * start. If this would exceed the total buffer headroom the kernel will
1466 * panic. A pointer to the first byte of the extra data is returned.
1467 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001468void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001469{
1470 skb->data -= len;
1471 skb->len += len;
1472 if (unlikely(skb->data<skb->head))
1473 skb_under_panic(skb, len, __builtin_return_address(0));
1474 return skb->data;
1475}
1476EXPORT_SYMBOL(skb_push);
1477
1478/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001479 * skb_pull - remove data from the start of a buffer
1480 * @skb: buffer to use
1481 * @len: amount of data to remove
1482 *
1483 * This function removes data from the start of a buffer, returning
1484 * the memory to the headroom. A pointer to the next data in the buffer
1485 * is returned. Once the data has been pulled future pushes will overwrite
1486 * the old data.
1487 */
Johannes Bergaf728682017-06-16 14:29:22 +02001488void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001489{
David S. Miller47d29642010-05-02 02:21:44 -07001490 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001491}
1492EXPORT_SYMBOL(skb_pull);
1493
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001494/**
1495 * skb_trim - remove end from a buffer
1496 * @skb: buffer to alter
1497 * @len: new length
1498 *
1499 * Cut the length of a buffer down by removing data from the tail. If
1500 * the buffer is already under the length specified it is not modified.
1501 * The skb must be linear.
1502 */
1503void skb_trim(struct sk_buff *skb, unsigned int len)
1504{
1505 if (skb->len > len)
1506 __skb_trim(skb, len);
1507}
1508EXPORT_SYMBOL(skb_trim);
1509
Herbert Xu3cc0e872006-06-09 16:13:38 -07001510/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
Herbert Xu3cc0e872006-06-09 16:13:38 -07001513int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Herbert Xu27b437c2006-07-13 19:26:39 -07001515 struct sk_buff **fragp;
1516 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int offset = skb_headlen(skb);
1518 int nfrags = skb_shinfo(skb)->nr_frags;
1519 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001520 int err;
1521
1522 if (skb_cloned(skb) &&
1523 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001526 i = 0;
1527 if (offset >= len)
1528 goto drop_pages;
1529
1530 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001531 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001532
1533 if (end < len) {
1534 offset = end;
1535 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001537
Eric Dumazet9e903e02011-10-18 21:00:24 +00001538 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001539
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001540drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001541 skb_shinfo(skb)->nr_frags = i;
1542
1543 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001544 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001545
David S. Miller21dc3302010-08-23 00:13:46 -07001546 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001547 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001548 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
Herbert Xu27b437c2006-07-13 19:26:39 -07001551 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1552 fragp = &frag->next) {
1553 int end = offset + frag->len;
1554
1555 if (skb_shared(frag)) {
1556 struct sk_buff *nfrag;
1557
1558 nfrag = skb_clone(frag, GFP_ATOMIC);
1559 if (unlikely(!nfrag))
1560 return -ENOMEM;
1561
1562 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001563 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001564 frag = nfrag;
1565 *fragp = frag;
1566 }
1567
1568 if (end < len) {
1569 offset = end;
1570 continue;
1571 }
1572
1573 if (end > len &&
1574 unlikely((err = pskb_trim(frag, len - offset))))
1575 return err;
1576
1577 if (frag->next)
1578 skb_drop_list(&frag->next);
1579 break;
1580 }
1581
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001582done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001583 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 skb->data_len -= skb->len - len;
1585 skb->len = len;
1586 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001587 skb->len = len;
1588 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001589 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001592 if (!skb->sk || skb->destructor == sock_edemux)
1593 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return 0;
1595}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001596EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598/**
1599 * __pskb_pull_tail - advance tail of skb header
1600 * @skb: buffer to reallocate
1601 * @delta: number of bytes to advance tail
1602 *
1603 * The function makes a sense only on a fragmented &sk_buff,
1604 * it expands header moving its tail forward and copying necessary
1605 * data from fragmented part.
1606 *
1607 * &sk_buff MUST have reference count of 1.
1608 *
1609 * Returns %NULL (and &sk_buff does not change) if pull failed
1610 * or value of new tail of skb in the case of success.
1611 *
1612 * All the pointers pointing into skb header may change and must be
1613 * reloaded after call to this function.
1614 */
1615
1616/* Moves tail of skb head forward, copying data from fragmented part,
1617 * when it is necessary.
1618 * 1. It may fail due to malloc failure.
1619 * 2. It may change skb pointers.
1620 *
1621 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1622 */
Johannes Bergaf728682017-06-16 14:29:22 +02001623void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 /* If skb has not enough free space at tail, get new one
1626 * plus 128 bytes for future expansions. If we have enough
1627 * room at tail, reallocate without expansion only if skb is cloned.
1628 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001629 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (eat > 0 || skb_cloned(skb)) {
1632 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1633 GFP_ATOMIC))
1634 return NULL;
1635 }
1636
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001637 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 BUG();
1639
1640 /* Optimization: no fragments, no reasons to preestimate
1641 * size of pulled pages. Superb.
1642 */
David S. Miller21dc3302010-08-23 00:13:46 -07001643 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto pull_pages;
1645
1646 /* Estimate size of pulled pages. */
1647 eat = delta;
1648 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001649 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1650
1651 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001653 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
1656 /* If we need update frag list, we are in troubles.
1657 * Certainly, it possible to add an offset to skb data,
1658 * but taking into account that pulling is expected to
1659 * be very rare operation, it is worth to fight against
1660 * further bloating skb head and crucify ourselves here instead.
1661 * Pure masohism, indeed. 8)8)
1662 */
1663 if (eat) {
1664 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1665 struct sk_buff *clone = NULL;
1666 struct sk_buff *insp = NULL;
1667
1668 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001669 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (list->len <= eat) {
1672 /* Eaten as whole. */
1673 eat -= list->len;
1674 list = list->next;
1675 insp = list;
1676 } else {
1677 /* Eaten partially. */
1678
1679 if (skb_shared(list)) {
1680 /* Sucks! We need to fork list. :-( */
1681 clone = skb_clone(list, GFP_ATOMIC);
1682 if (!clone)
1683 return NULL;
1684 insp = list->next;
1685 list = clone;
1686 } else {
1687 /* This may be pulled without
1688 * problems. */
1689 insp = list;
1690 }
1691 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001692 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return NULL;
1694 }
1695 break;
1696 }
1697 } while (eat);
1698
1699 /* Free pulled out fragments. */
1700 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1701 skb_shinfo(skb)->frag_list = list->next;
1702 kfree_skb(list);
1703 }
1704 /* And insert new clone at head. */
1705 if (clone) {
1706 clone->next = list;
1707 skb_shinfo(skb)->frag_list = clone;
1708 }
1709 }
1710 /* Success! Now we may commit changes to skb data. */
1711
1712pull_pages:
1713 eat = delta;
1714 k = 0;
1715 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001716 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1717
1718 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001719 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001720 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 } else {
1722 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1723 if (eat) {
1724 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001725 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001726 if (!i)
1727 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 eat = 0;
1729 }
1730 k++;
1731 }
1732 }
1733 skb_shinfo(skb)->nr_frags = k;
1734
linzhang3ccc6c62017-07-17 17:25:02 +08001735end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 skb->tail += delta;
1737 skb->data_len -= delta;
1738
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001739 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001741EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Eric Dumazet22019b12011-07-29 18:37:31 +00001743/**
1744 * skb_copy_bits - copy bits from skb to kernel buffer
1745 * @skb: source skb
1746 * @offset: offset in source
1747 * @to: destination buffer
1748 * @len: number of bytes to copy
1749 *
1750 * Copy the specified number of bytes from the source skb to the
1751 * destination buffer.
1752 *
1753 * CAUTION ! :
1754 * If its prototype is ever changed,
1755 * check arch/{*}/net/{*}.S files,
1756 * since it is called from BPF assembly code.
1757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1759{
David S. Miller1a028e52007-04-27 15:21:23 -07001760 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001761 struct sk_buff *frag_iter;
1762 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (offset > (int)skb->len - len)
1765 goto fault;
1766
1767 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001768 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (copy > len)
1770 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001771 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if ((len -= copy) == 0)
1773 return 0;
1774 offset += copy;
1775 to += copy;
1776 }
1777
1778 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001779 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001780 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001782 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001783
Eric Dumazet51c56b02012-04-05 11:35:15 +02001784 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04001786 u32 p_off, p_len, copied;
1787 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 u8 *vaddr;
1789
1790 if (copy > len)
1791 copy = len;
1792
Willem de Bruijnc613c202017-07-31 08:15:47 -04001793 skb_frag_foreach_page(f,
1794 f->page_offset + offset - start,
1795 copy, p, p_off, p_len, copied) {
1796 vaddr = kmap_atomic(p);
1797 memcpy(to + copied, vaddr + p_off, p_len);
1798 kunmap_atomic(vaddr);
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 if ((len -= copy) == 0)
1802 return 0;
1803 offset += copy;
1804 to += copy;
1805 }
David S. Miller1a028e52007-04-27 15:21:23 -07001806 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808
David S. Millerfbb398a2009-06-09 00:18:59 -07001809 skb_walk_frags(skb, frag_iter) {
1810 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
David S. Millerfbb398a2009-06-09 00:18:59 -07001812 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
David S. Millerfbb398a2009-06-09 00:18:59 -07001814 end = start + frag_iter->len;
1815 if ((copy = end - offset) > 0) {
1816 if (copy > len)
1817 copy = len;
1818 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1819 goto fault;
1820 if ((len -= copy) == 0)
1821 return 0;
1822 offset += copy;
1823 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001825 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Shirley Maa6686f22011-07-06 12:22:12 +00001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (!len)
1829 return 0;
1830
1831fault:
1832 return -EFAULT;
1833}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001834EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jens Axboe9c55e012007-11-06 23:30:13 -08001836/*
1837 * Callback from splice_to_pipe(), if we need to release some pages
1838 * at the end of the spd in case we error'ed out in filling the pipe.
1839 */
1840static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1841{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001842 put_page(spd->pages[i]);
1843}
Jens Axboe9c55e012007-11-06 23:30:13 -08001844
David S. Millera108d5f2012-04-23 23:06:11 -04001845static struct page *linear_to_page(struct page *page, unsigned int *len,
1846 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001847 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001848{
Eric Dumazet5640f762012-09-23 23:04:42 +00001849 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001850
Eric Dumazet5640f762012-09-23 23:04:42 +00001851 if (!sk_page_frag_refill(sk, pfrag))
1852 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001853
Eric Dumazet5640f762012-09-23 23:04:42 +00001854 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001855
Eric Dumazet5640f762012-09-23 23:04:42 +00001856 memcpy(page_address(pfrag->page) + pfrag->offset,
1857 page_address(page) + *offset, *len);
1858 *offset = pfrag->offset;
1859 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001860
Eric Dumazet5640f762012-09-23 23:04:42 +00001861 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001862}
1863
Eric Dumazet41c73a02012-04-22 12:26:16 +00001864static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1865 struct page *page,
1866 unsigned int offset)
1867{
1868 return spd->nr_pages &&
1869 spd->pages[spd->nr_pages - 1] == page &&
1870 (spd->partial[spd->nr_pages - 1].offset +
1871 spd->partial[spd->nr_pages - 1].len == offset);
1872}
1873
Jens Axboe9c55e012007-11-06 23:30:13 -08001874/*
1875 * Fill page/offset/length into spd, if it can hold more pages.
1876 */
David S. Millera108d5f2012-04-23 23:06:11 -04001877static bool spd_fill_page(struct splice_pipe_desc *spd,
1878 struct pipe_inode_info *pipe, struct page *page,
1879 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001880 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001881 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001882{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001883 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001884 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001885
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001886 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001887 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001888 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001889 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001890 }
1891 if (spd_can_coalesce(spd, page, offset)) {
1892 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001893 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001894 }
1895 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001896 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001897 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001898 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001899 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001900
David S. Millera108d5f2012-04-23 23:06:11 -04001901 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001902}
1903
David S. Millera108d5f2012-04-23 23:06:11 -04001904static bool __splice_segment(struct page *page, unsigned int poff,
1905 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001906 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001907 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001908 struct sock *sk,
1909 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001910{
1911 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001912 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001913
1914 /* skip this segment if already processed */
1915 if (*off >= plen) {
1916 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001917 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001918 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001919
Octavian Purdila2870c432008-07-15 00:49:11 -07001920 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001921 poff += *off;
1922 plen -= *off;
1923 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001924
Eric Dumazet18aafc62013-01-11 14:46:37 +00001925 do {
1926 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001927
Eric Dumazet18aafc62013-01-11 14:46:37 +00001928 if (spd_fill_page(spd, pipe, page, &flen, poff,
1929 linear, sk))
1930 return true;
1931 poff += flen;
1932 plen -= flen;
1933 *len -= flen;
1934 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001935
David S. Millera108d5f2012-04-23 23:06:11 -04001936 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001937}
1938
1939/*
David S. Millera108d5f2012-04-23 23:06:11 -04001940 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001941 * pipe is full or if we already spliced the requested length.
1942 */
David S. Millera108d5f2012-04-23 23:06:11 -04001943static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1944 unsigned int *offset, unsigned int *len,
1945 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001946{
1947 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001948 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001949
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001950 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001951 * If skb->head_frag is set, this 'linear' part is backed by a
1952 * fragment, and if the head is not shared with any clones then
1953 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001954 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001955 if (__splice_segment(virt_to_page(skb->data),
1956 (unsigned long) skb->data & (PAGE_SIZE - 1),
1957 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001958 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001959 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001960 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001961 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001962
1963 /*
1964 * then map the fragments
1965 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001966 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1967 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1968
Ian Campbellea2ab692011-08-22 23:44:58 +00001969 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001970 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001971 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001972 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001973 }
1974
Tom Herbertfa9835e2016-03-07 14:11:04 -08001975 skb_walk_frags(skb, iter) {
1976 if (*offset >= iter->len) {
1977 *offset -= iter->len;
1978 continue;
1979 }
1980 /* __skb_splice_bits() only fails if the output has no room
1981 * left, so no point in going over the frag_list for the error
1982 * case.
1983 */
1984 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1985 return true;
1986 }
1987
David S. Millera108d5f2012-04-23 23:06:11 -04001988 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001989}
1990
1991/*
1992 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001993 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001994 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001995int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001996 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001997 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001998{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001999 struct partial_page partial[MAX_SKB_FRAGS];
2000 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002001 struct splice_pipe_desc spd = {
2002 .pages = pages,
2003 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002004 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002005 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002006 .spd_release = sock_spd_release,
2007 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002008 int ret = 0;
2009
Tom Herbertfa9835e2016-03-07 14:11:04 -08002010 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002011
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002012 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002013 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002014
Jens Axboe35f3d142010-05-20 10:43:18 +02002015 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002016}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002017EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002018
Tom Herbert20bf50d2017-07-28 16:22:42 -07002019/* Send skb data on a socket. Socket must be locked. */
2020int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2021 int len)
2022{
2023 unsigned int orig_len = len;
2024 struct sk_buff *head = skb;
2025 unsigned short fragidx;
2026 int slen, ret;
2027
2028do_frag_list:
2029
2030 /* Deal with head data */
2031 while (offset < skb_headlen(skb) && len) {
2032 struct kvec kv;
2033 struct msghdr msg;
2034
2035 slen = min_t(int, len, skb_headlen(skb) - offset);
2036 kv.iov_base = skb->data + offset;
2037 kv.iov_len = len;
2038 memset(&msg, 0, sizeof(msg));
2039
2040 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2041 if (ret <= 0)
2042 goto error;
2043
2044 offset += ret;
2045 len -= ret;
2046 }
2047
2048 /* All the data was skb head? */
2049 if (!len)
2050 goto out;
2051
2052 /* Make offset relative to start of frags */
2053 offset -= skb_headlen(skb);
2054
2055 /* Find where we are in frag list */
2056 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2057 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2058
2059 if (offset < frag->size)
2060 break;
2061
2062 offset -= frag->size;
2063 }
2064
2065 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2066 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2067
2068 slen = min_t(size_t, len, frag->size - offset);
2069
2070 while (slen) {
2071 ret = kernel_sendpage_locked(sk, frag->page.p,
2072 frag->page_offset + offset,
2073 slen, MSG_DONTWAIT);
2074 if (ret <= 0)
2075 goto error;
2076
2077 len -= ret;
2078 offset += ret;
2079 slen -= ret;
2080 }
2081
2082 offset = 0;
2083 }
2084
2085 if (len) {
2086 /* Process any frag lists */
2087
2088 if (skb == head) {
2089 if (skb_has_frag_list(skb)) {
2090 skb = skb_shinfo(skb)->frag_list;
2091 goto do_frag_list;
2092 }
2093 } else if (skb->next) {
2094 skb = skb->next;
2095 goto do_frag_list;
2096 }
2097 }
2098
2099out:
2100 return orig_len - len;
2101
2102error:
2103 return orig_len == len ? ret : orig_len - len;
2104}
2105EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2106
2107/* Send skb data on a socket. */
2108int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2109{
2110 int ret = 0;
2111
2112 lock_sock(sk);
2113 ret = skb_send_sock_locked(sk, skb, offset, len);
2114 release_sock(sk);
2115
2116 return ret;
2117}
2118EXPORT_SYMBOL_GPL(skb_send_sock);
2119
Herbert Xu357b40a2005-04-19 22:30:14 -07002120/**
2121 * skb_store_bits - store bits from kernel buffer to skb
2122 * @skb: destination buffer
2123 * @offset: offset in destination
2124 * @from: source buffer
2125 * @len: number of bytes to copy
2126 *
2127 * Copy the specified number of bytes from the source buffer to the
2128 * destination skb. This function handles all the messy bits of
2129 * traversing fragment lists and such.
2130 */
2131
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002132int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002133{
David S. Miller1a028e52007-04-27 15:21:23 -07002134 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002135 struct sk_buff *frag_iter;
2136 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002137
2138 if (offset > (int)skb->len - len)
2139 goto fault;
2140
David S. Miller1a028e52007-04-27 15:21:23 -07002141 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002142 if (copy > len)
2143 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002144 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002145 if ((len -= copy) == 0)
2146 return 0;
2147 offset += copy;
2148 from += copy;
2149 }
2150
2151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2152 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002153 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002154
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002155 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002156
Eric Dumazet9e903e02011-10-18 21:00:24 +00002157 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002158 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002159 u32 p_off, p_len, copied;
2160 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002161 u8 *vaddr;
2162
2163 if (copy > len)
2164 copy = len;
2165
Willem de Bruijnc613c202017-07-31 08:15:47 -04002166 skb_frag_foreach_page(frag,
2167 frag->page_offset + offset - start,
2168 copy, p, p_off, p_len, copied) {
2169 vaddr = kmap_atomic(p);
2170 memcpy(vaddr + p_off, from + copied, p_len);
2171 kunmap_atomic(vaddr);
2172 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002173
2174 if ((len -= copy) == 0)
2175 return 0;
2176 offset += copy;
2177 from += copy;
2178 }
David S. Miller1a028e52007-04-27 15:21:23 -07002179 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002180 }
2181
David S. Millerfbb398a2009-06-09 00:18:59 -07002182 skb_walk_frags(skb, frag_iter) {
2183 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002184
David S. Millerfbb398a2009-06-09 00:18:59 -07002185 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002186
David S. Millerfbb398a2009-06-09 00:18:59 -07002187 end = start + frag_iter->len;
2188 if ((copy = end - offset) > 0) {
2189 if (copy > len)
2190 copy = len;
2191 if (skb_store_bits(frag_iter, offset - start,
2192 from, copy))
2193 goto fault;
2194 if ((len -= copy) == 0)
2195 return 0;
2196 offset += copy;
2197 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002198 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002199 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002200 }
2201 if (!len)
2202 return 0;
2203
2204fault:
2205 return -EFAULT;
2206}
Herbert Xu357b40a2005-04-19 22:30:14 -07002207EXPORT_SYMBOL(skb_store_bits);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002210__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2211 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
David S. Miller1a028e52007-04-27 15:21:23 -07002213 int start = skb_headlen(skb);
2214 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002215 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int pos = 0;
2217
2218 /* Checksum header. */
2219 if (copy > 0) {
2220 if (copy > len)
2221 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002222 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if ((len -= copy) == 0)
2224 return csum;
2225 offset += copy;
2226 pos = copy;
2227 }
2228
2229 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002230 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002231 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002233 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002234
Eric Dumazet51c56b02012-04-05 11:35:15 +02002235 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002237 u32 p_off, p_len, copied;
2238 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002239 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 if (copy > len)
2243 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002244
2245 skb_frag_foreach_page(frag,
2246 frag->page_offset + offset - start,
2247 copy, p, p_off, p_len, copied) {
2248 vaddr = kmap_atomic(p);
2249 csum2 = ops->update(vaddr + p_off, p_len, 0);
2250 kunmap_atomic(vaddr);
2251 csum = ops->combine(csum, csum2, pos, p_len);
2252 pos += p_len;
2253 }
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!(len -= copy))
2256 return csum;
2257 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
David S. Miller1a028e52007-04-27 15:21:23 -07002259 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
David S. Millerfbb398a2009-06-09 00:18:59 -07002262 skb_walk_frags(skb, frag_iter) {
2263 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
David S. Millerfbb398a2009-06-09 00:18:59 -07002265 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
David S. Millerfbb398a2009-06-09 00:18:59 -07002267 end = start + frag_iter->len;
2268 if ((copy = end - offset) > 0) {
2269 __wsum csum2;
2270 if (copy > len)
2271 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002272 csum2 = __skb_checksum(frag_iter, offset - start,
2273 copy, 0, ops);
2274 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002275 if ((len -= copy) == 0)
2276 return csum;
2277 offset += copy;
2278 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002280 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002282 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 return csum;
2285}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002286EXPORT_SYMBOL(__skb_checksum);
2287
2288__wsum skb_checksum(const struct sk_buff *skb, int offset,
2289 int len, __wsum csum)
2290{
2291 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002292 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002293 .combine = csum_block_add_ext,
2294 };
2295
2296 return __skb_checksum(skb, offset, len, csum, &ops);
2297}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002298EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300/* Both of above in one bottle. */
2301
Al Viro81d77662006-11-14 21:37:33 -08002302__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2303 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
David S. Miller1a028e52007-04-27 15:21:23 -07002305 int start = skb_headlen(skb);
2306 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002307 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int pos = 0;
2309
2310 /* Copy header. */
2311 if (copy > 0) {
2312 if (copy > len)
2313 copy = len;
2314 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2315 copy, csum);
2316 if ((len -= copy) == 0)
2317 return csum;
2318 offset += copy;
2319 to += copy;
2320 pos = copy;
2321 }
2322
2323 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002324 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002326 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002327
Eric Dumazet9e903e02011-10-18 21:00:24 +00002328 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002330 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2331 u32 p_off, p_len, copied;
2332 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002333 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 if (copy > len)
2337 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002338
2339 skb_frag_foreach_page(frag,
2340 frag->page_offset + offset - start,
2341 copy, p, p_off, p_len, copied) {
2342 vaddr = kmap_atomic(p);
2343 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2344 to + copied,
2345 p_len, 0);
2346 kunmap_atomic(vaddr);
2347 csum = csum_block_add(csum, csum2, pos);
2348 pos += p_len;
2349 }
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!(len -= copy))
2352 return csum;
2353 offset += copy;
2354 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
David S. Miller1a028e52007-04-27 15:21:23 -07002356 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358
David S. Millerfbb398a2009-06-09 00:18:59 -07002359 skb_walk_frags(skb, frag_iter) {
2360 __wsum csum2;
2361 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
David S. Millerfbb398a2009-06-09 00:18:59 -07002363 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
David S. Millerfbb398a2009-06-09 00:18:59 -07002365 end = start + frag_iter->len;
2366 if ((copy = end - offset) > 0) {
2367 if (copy > len)
2368 copy = len;
2369 csum2 = skb_copy_and_csum_bits(frag_iter,
2370 offset - start,
2371 to, copy, 0);
2372 csum = csum_block_add(csum, csum2, pos);
2373 if ((len -= copy) == 0)
2374 return csum;
2375 offset += copy;
2376 to += copy;
2377 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002379 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002381 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return csum;
2383}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002384EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Davide Caratti96178132017-05-18 15:44:37 +02002386static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2387{
2388 net_warn_ratelimited(
2389 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2390 __func__);
2391 return 0;
2392}
2393
2394static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2395 int offset, int len)
2396{
2397 net_warn_ratelimited(
2398 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2399 __func__);
2400 return 0;
2401}
2402
2403static const struct skb_checksum_ops default_crc32c_ops = {
2404 .update = warn_crc32c_csum_update,
2405 .combine = warn_crc32c_csum_combine,
2406};
2407
2408const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2409 &default_crc32c_ops;
2410EXPORT_SYMBOL(crc32c_csum_stub);
2411
Thomas Grafaf2806f2013-12-13 15:22:17 +01002412 /**
2413 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2414 * @from: source buffer
2415 *
2416 * Calculates the amount of linear headroom needed in the 'to' skb passed
2417 * into skb_zerocopy().
2418 */
2419unsigned int
2420skb_zerocopy_headlen(const struct sk_buff *from)
2421{
2422 unsigned int hlen = 0;
2423
2424 if (!from->head_frag ||
2425 skb_headlen(from) < L1_CACHE_BYTES ||
2426 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2427 hlen = skb_headlen(from);
2428
2429 if (skb_has_frag_list(from))
2430 hlen = from->len;
2431
2432 return hlen;
2433}
2434EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2435
2436/**
2437 * skb_zerocopy - Zero copy skb to skb
2438 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002439 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002440 * @len: number of bytes to copy from source buffer
2441 * @hlen: size of linear headroom in destination buffer
2442 *
2443 * Copies up to `len` bytes from `from` to `to` by creating references
2444 * to the frags in the source buffer.
2445 *
2446 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2447 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002448 *
2449 * Return value:
2450 * 0: everything is OK
2451 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2452 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002453 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002454int
2455skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002456{
2457 int i, j = 0;
2458 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002459 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002460 struct page *page;
2461 unsigned int offset;
2462
2463 BUG_ON(!from->head_frag && !hlen);
2464
2465 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002466 if (len <= skb_tailroom(to))
2467 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002468
2469 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002470 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2471 if (unlikely(ret))
2472 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002473 len -= hlen;
2474 } else {
2475 plen = min_t(int, skb_headlen(from), len);
2476 if (plen) {
2477 page = virt_to_head_page(from->head);
2478 offset = from->data - (unsigned char *)page_address(page);
2479 __skb_fill_page_desc(to, 0, page, offset, plen);
2480 get_page(page);
2481 j = 1;
2482 len -= plen;
2483 }
2484 }
2485
2486 to->truesize += len + plen;
2487 to->len += len + plen;
2488 to->data_len += len + plen;
2489
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002490 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2491 skb_tx_error(from);
2492 return -ENOMEM;
2493 }
2494
Thomas Grafaf2806f2013-12-13 15:22:17 +01002495 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2496 if (!len)
2497 break;
2498 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2499 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2500 len -= skb_shinfo(to)->frags[j].size;
2501 skb_frag_ref(to, j);
2502 j++;
2503 }
2504 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002505
2506 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002507}
2508EXPORT_SYMBOL_GPL(skb_zerocopy);
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2511{
Al Virod3bc23e2006-11-14 21:24:49 -08002512 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 long csstart;
2514
Patrick McHardy84fa7932006-08-29 16:44:56 -07002515 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002516 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 else
2518 csstart = skb_headlen(skb);
2519
Kris Katterjohn09a62662006-01-08 22:24:28 -08002520 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002522 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 csum = 0;
2525 if (csstart != skb->len)
2526 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2527 skb->len - csstart, 0);
2528
Patrick McHardy84fa7932006-08-29 16:44:56 -07002529 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002530 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Al Virod3bc23e2006-11-14 21:24:49 -08002532 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002535EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537/**
2538 * skb_dequeue - remove from the head of the queue
2539 * @list: list to dequeue from
2540 *
2541 * Remove the head of the list. The list lock is taken so the function
2542 * may be used safely with other locking list functions. The head item is
2543 * returned or %NULL if the list is empty.
2544 */
2545
2546struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2547{
2548 unsigned long flags;
2549 struct sk_buff *result;
2550
2551 spin_lock_irqsave(&list->lock, flags);
2552 result = __skb_dequeue(list);
2553 spin_unlock_irqrestore(&list->lock, flags);
2554 return result;
2555}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002556EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558/**
2559 * skb_dequeue_tail - remove from the tail of the queue
2560 * @list: list to dequeue from
2561 *
2562 * Remove the tail of the list. The list lock is taken so the function
2563 * may be used safely with other locking list functions. The tail item is
2564 * returned or %NULL if the list is empty.
2565 */
2566struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2567{
2568 unsigned long flags;
2569 struct sk_buff *result;
2570
2571 spin_lock_irqsave(&list->lock, flags);
2572 result = __skb_dequeue_tail(list);
2573 spin_unlock_irqrestore(&list->lock, flags);
2574 return result;
2575}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002576EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578/**
2579 * skb_queue_purge - empty a list
2580 * @list: list to empty
2581 *
2582 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2583 * the list and one reference dropped. This function takes the list
2584 * lock and is atomic with respect to other list locking functions.
2585 */
2586void skb_queue_purge(struct sk_buff_head *list)
2587{
2588 struct sk_buff *skb;
2589 while ((skb = skb_dequeue(list)) != NULL)
2590 kfree_skb(skb);
2591}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002592EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002595 * skb_rbtree_purge - empty a skb rbtree
2596 * @root: root of the rbtree to empty
2597 *
2598 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2599 * the list and one reference dropped. This function does not take
2600 * any lock. Synchronization should be handled by the caller (e.g., TCP
2601 * out-of-order queue is protected by the socket lock).
2602 */
2603void skb_rbtree_purge(struct rb_root *root)
2604{
2605 struct sk_buff *skb, *next;
2606
2607 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2608 kfree_skb(skb);
2609
2610 *root = RB_ROOT;
2611}
2612
2613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * skb_queue_head - queue a buffer at the list head
2615 * @list: list to use
2616 * @newsk: buffer to queue
2617 *
2618 * Queue a buffer at the start of the list. This function takes the
2619 * list lock and can be used safely with other locking &sk_buff functions
2620 * safely.
2621 *
2622 * A buffer cannot be placed on two lists at the same time.
2623 */
2624void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2625{
2626 unsigned long flags;
2627
2628 spin_lock_irqsave(&list->lock, flags);
2629 __skb_queue_head(list, newsk);
2630 spin_unlock_irqrestore(&list->lock, flags);
2631}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002632EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634/**
2635 * skb_queue_tail - queue a buffer at the list tail
2636 * @list: list to use
2637 * @newsk: buffer to queue
2638 *
2639 * Queue a buffer at the tail of the list. This function takes the
2640 * list lock and can be used safely with other locking &sk_buff functions
2641 * safely.
2642 *
2643 * A buffer cannot be placed on two lists at the same time.
2644 */
2645void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2646{
2647 unsigned long flags;
2648
2649 spin_lock_irqsave(&list->lock, flags);
2650 __skb_queue_tail(list, newsk);
2651 spin_unlock_irqrestore(&list->lock, flags);
2652}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002653EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/**
2656 * skb_unlink - remove a buffer from a list
2657 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002658 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 *
David S. Miller8728b832005-08-09 19:25:21 -07002660 * Remove a packet from a list. The list locks are taken and this
2661 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 *
David S. Miller8728b832005-08-09 19:25:21 -07002663 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 */
David S. Miller8728b832005-08-09 19:25:21 -07002665void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
David S. Miller8728b832005-08-09 19:25:21 -07002667 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
David S. Miller8728b832005-08-09 19:25:21 -07002669 spin_lock_irqsave(&list->lock, flags);
2670 __skb_unlink(skb, list);
2671 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002673EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675/**
2676 * skb_append - append a buffer
2677 * @old: buffer to insert after
2678 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002679 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 *
2681 * Place a packet after a given packet in a list. The list locks are taken
2682 * and this function is atomic with respect to other list locked calls.
2683 * A buffer cannot be placed on two lists at the same time.
2684 */
David S. Miller8728b832005-08-09 19:25:21 -07002685void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 unsigned long flags;
2688
David S. Miller8728b832005-08-09 19:25:21 -07002689 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002690 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002691 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002693EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695/**
2696 * skb_insert - insert a buffer
2697 * @old: buffer to insert before
2698 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002699 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 *
David S. Miller8728b832005-08-09 19:25:21 -07002701 * Place a packet before a given packet in a list. The list locks are
2702 * taken and this function is atomic with respect to other list locked
2703 * calls.
2704 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * A buffer cannot be placed on two lists at the same time.
2706 */
David S. Miller8728b832005-08-09 19:25:21 -07002707void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
2709 unsigned long flags;
2710
David S. Miller8728b832005-08-09 19:25:21 -07002711 spin_lock_irqsave(&list->lock, flags);
2712 __skb_insert(newsk, old->prev, old, list);
2713 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002715EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717static inline void skb_split_inside_header(struct sk_buff *skb,
2718 struct sk_buff* skb1,
2719 const u32 len, const int pos)
2720{
2721 int i;
2722
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002723 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2724 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* And move data appendix as is. */
2726 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2727 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2728
2729 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2730 skb_shinfo(skb)->nr_frags = 0;
2731 skb1->data_len = skb->data_len;
2732 skb1->len += skb1->data_len;
2733 skb->data_len = 0;
2734 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002735 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
2738static inline void skb_split_no_header(struct sk_buff *skb,
2739 struct sk_buff* skb1,
2740 const u32 len, int pos)
2741{
2742 int i, k = 0;
2743 const int nfrags = skb_shinfo(skb)->nr_frags;
2744
2745 skb_shinfo(skb)->nr_frags = 0;
2746 skb1->len = skb1->data_len = skb->len - len;
2747 skb->len = len;
2748 skb->data_len = len - pos;
2749
2750 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002751 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 if (pos + size > len) {
2754 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2755
2756 if (pos < len) {
2757 /* Split frag.
2758 * We have two variants in this case:
2759 * 1. Move all the frag to the second
2760 * part, if it is possible. F.e.
2761 * this approach is mandatory for TUX,
2762 * where splitting is expensive.
2763 * 2. Split is accurately. We make this.
2764 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002765 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002767 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2768 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 skb_shinfo(skb)->nr_frags++;
2770 }
2771 k++;
2772 } else
2773 skb_shinfo(skb)->nr_frags++;
2774 pos += size;
2775 }
2776 skb_shinfo(skb1)->nr_frags = k;
2777}
2778
2779/**
2780 * skb_split - Split fragmented skb to two parts at length len.
2781 * @skb: the buffer to split
2782 * @skb1: the buffer to receive the second part
2783 * @len: new length for skb
2784 */
2785void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2786{
2787 int pos = skb_headlen(skb);
2788
Willem de Bruijnfff88032017-06-08 11:35:03 -04002789 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2790 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (len < pos) /* Split line is inside header. */
2792 skb_split_inside_header(skb, skb1, len, pos);
2793 else /* Second chunk has no header, nothing to copy. */
2794 skb_split_no_header(skb, skb1, len, pos);
2795}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002796EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002798/* Shifting from/to a cloned skb is a no-go.
2799 *
2800 * Caller cannot keep skb_shinfo related pointers past calling here!
2801 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002802static int skb_prepare_for_shift(struct sk_buff *skb)
2803{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002804 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002805}
2806
2807/**
2808 * skb_shift - Shifts paged data partially from skb to another
2809 * @tgt: buffer into which tail data gets added
2810 * @skb: buffer from which the paged data comes from
2811 * @shiftlen: shift up to this many bytes
2812 *
2813 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002814 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002815 * It's up to caller to free skb if everything was shifted.
2816 *
2817 * If @tgt runs out of frags, the whole operation is aborted.
2818 *
2819 * Skb cannot include anything else but paged data while tgt is allowed
2820 * to have non-paged data as well.
2821 *
2822 * TODO: full sized shift could be optimized but that would need
2823 * specialized skb free'er to handle frags without up-to-date nr_frags.
2824 */
2825int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2826{
2827 int from, to, merge, todo;
2828 struct skb_frag_struct *fragfrom, *fragto;
2829
2830 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002831
2832 if (skb_headlen(skb))
2833 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002834
2835 todo = shiftlen;
2836 from = 0;
2837 to = skb_shinfo(tgt)->nr_frags;
2838 fragfrom = &skb_shinfo(skb)->frags[from];
2839
2840 /* Actual merge is delayed until the point when we know we can
2841 * commit all, so that we don't have to undo partial changes
2842 */
2843 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002844 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2845 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002846 merge = -1;
2847 } else {
2848 merge = to - 1;
2849
Eric Dumazet9e903e02011-10-18 21:00:24 +00002850 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002851 if (todo < 0) {
2852 if (skb_prepare_for_shift(skb) ||
2853 skb_prepare_for_shift(tgt))
2854 return 0;
2855
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002856 /* All previous frag pointers might be stale! */
2857 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002858 fragto = &skb_shinfo(tgt)->frags[merge];
2859
Eric Dumazet9e903e02011-10-18 21:00:24 +00002860 skb_frag_size_add(fragto, shiftlen);
2861 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002862 fragfrom->page_offset += shiftlen;
2863
2864 goto onlymerged;
2865 }
2866
2867 from++;
2868 }
2869
2870 /* Skip full, not-fitting skb to avoid expensive operations */
2871 if ((shiftlen == skb->len) &&
2872 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2873 return 0;
2874
2875 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2876 return 0;
2877
2878 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2879 if (to == MAX_SKB_FRAGS)
2880 return 0;
2881
2882 fragfrom = &skb_shinfo(skb)->frags[from];
2883 fragto = &skb_shinfo(tgt)->frags[to];
2884
Eric Dumazet9e903e02011-10-18 21:00:24 +00002885 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002886 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002887 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002888 from++;
2889 to++;
2890
2891 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002892 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002893 fragto->page = fragfrom->page;
2894 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002895 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002896
2897 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002898 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002899 todo = 0;
2900
2901 to++;
2902 break;
2903 }
2904 }
2905
2906 /* Ready to "commit" this state change to tgt */
2907 skb_shinfo(tgt)->nr_frags = to;
2908
2909 if (merge >= 0) {
2910 fragfrom = &skb_shinfo(skb)->frags[0];
2911 fragto = &skb_shinfo(tgt)->frags[merge];
2912
Eric Dumazet9e903e02011-10-18 21:00:24 +00002913 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002914 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002915 }
2916
2917 /* Reposition in the original skb */
2918 to = 0;
2919 while (from < skb_shinfo(skb)->nr_frags)
2920 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2921 skb_shinfo(skb)->nr_frags = to;
2922
2923 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2924
2925onlymerged:
2926 /* Most likely the tgt won't ever need its checksum anymore, skb on
2927 * the other hand might need it if it needs to be resent
2928 */
2929 tgt->ip_summed = CHECKSUM_PARTIAL;
2930 skb->ip_summed = CHECKSUM_PARTIAL;
2931
2932 /* Yak, is it really working this way? Some helper please? */
2933 skb->len -= shiftlen;
2934 skb->data_len -= shiftlen;
2935 skb->truesize -= shiftlen;
2936 tgt->len += shiftlen;
2937 tgt->data_len += shiftlen;
2938 tgt->truesize += shiftlen;
2939
2940 return shiftlen;
2941}
2942
Thomas Graf677e90e2005-06-23 20:59:51 -07002943/**
2944 * skb_prepare_seq_read - Prepare a sequential read of skb data
2945 * @skb: the buffer to read
2946 * @from: lower offset of data to be read
2947 * @to: upper offset of data to be read
2948 * @st: state variable
2949 *
2950 * Initializes the specified state variable. Must be called before
2951 * invoking skb_seq_read() for the first time.
2952 */
2953void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2954 unsigned int to, struct skb_seq_state *st)
2955{
2956 st->lower_offset = from;
2957 st->upper_offset = to;
2958 st->root_skb = st->cur_skb = skb;
2959 st->frag_idx = st->stepped_offset = 0;
2960 st->frag_data = NULL;
2961}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002962EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002963
2964/**
2965 * skb_seq_read - Sequentially read skb data
2966 * @consumed: number of bytes consumed by the caller so far
2967 * @data: destination pointer for data to be returned
2968 * @st: state variable
2969 *
Mathias Krausebc323832013-11-07 14:18:26 +01002970 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002971 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002972 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002973 * of the block or 0 if the end of the skb data or the upper
2974 * offset has been reached.
2975 *
2976 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002977 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002978 * of bytes already consumed and the next call to
2979 * skb_seq_read() will return the remaining part of the block.
2980 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002981 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002982 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002983 * reads of potentially non linear data.
2984 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002985 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002986 * at the moment, state->root_skb could be replaced with
2987 * a stack for this purpose.
2988 */
2989unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2990 struct skb_seq_state *st)
2991{
2992 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2993 skb_frag_t *frag;
2994
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002995 if (unlikely(abs_offset >= st->upper_offset)) {
2996 if (st->frag_data) {
2997 kunmap_atomic(st->frag_data);
2998 st->frag_data = NULL;
2999 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003000 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003001 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003002
3003next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003004 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003005
Thomas Chenault995b3372009-05-18 21:43:27 -07003006 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003007 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003008 return block_limit - abs_offset;
3009 }
3010
3011 if (st->frag_idx == 0 && !st->frag_data)
3012 st->stepped_offset += skb_headlen(st->cur_skb);
3013
3014 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3015 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003016 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003017
3018 if (abs_offset < block_limit) {
3019 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003020 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003021
3022 *data = (u8 *) st->frag_data + frag->page_offset +
3023 (abs_offset - st->stepped_offset);
3024
3025 return block_limit - abs_offset;
3026 }
3027
3028 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003029 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003030 st->frag_data = NULL;
3031 }
3032
3033 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003034 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003035 }
3036
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003037 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003038 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003039 st->frag_data = NULL;
3040 }
3041
David S. Miller21dc3302010-08-23 00:13:46 -07003042 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003043 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003044 st->frag_idx = 0;
3045 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003046 } else if (st->cur_skb->next) {
3047 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003048 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003049 goto next_skb;
3050 }
3051
3052 return 0;
3053}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003054EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003055
3056/**
3057 * skb_abort_seq_read - Abort a sequential read of skb data
3058 * @st: state variable
3059 *
3060 * Must be called if skb_seq_read() was not called until it
3061 * returned 0.
3062 */
3063void skb_abort_seq_read(struct skb_seq_state *st)
3064{
3065 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003066 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003067}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003068EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003069
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003070#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3071
3072static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3073 struct ts_config *conf,
3074 struct ts_state *state)
3075{
3076 return skb_seq_read(offset, text, TS_SKB_CB(state));
3077}
3078
3079static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3080{
3081 skb_abort_seq_read(TS_SKB_CB(state));
3082}
3083
3084/**
3085 * skb_find_text - Find a text pattern in skb data
3086 * @skb: the buffer to look in
3087 * @from: search offset
3088 * @to: search limit
3089 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003090 *
3091 * Finds a pattern in the skb data according to the specified
3092 * textsearch configuration. Use textsearch_next() to retrieve
3093 * subsequent occurrences of the pattern. Returns the offset
3094 * to the first occurrence or UINT_MAX if no match was found.
3095 */
3096unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003097 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003098{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003099 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003100 unsigned int ret;
3101
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003102 config->get_next_block = skb_ts_get_next_block;
3103 config->finish = skb_ts_finish;
3104
Bojan Prtvar059a2442015-02-22 11:46:35 +01003105 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003106
Bojan Prtvar059a2442015-02-22 11:46:35 +01003107 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003108 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003109}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003110EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003111
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003112/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003113 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003114 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09003115 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003116 * @getfrag: call back function to be used for getting the user data
3117 * @from: pointer to user message iov
3118 * @length: length of the iov message
3119 *
3120 * Description: This procedure append the user data in the fragment part
3121 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3122 */
3123int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08003124 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003125 int len, int odd, struct sk_buff *skb),
3126 void *from, int length)
3127{
Eric Dumazetb2111722012-12-28 06:06:37 +00003128 int frg_cnt = skb_shinfo(skb)->nr_frags;
3129 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003130 int offset = 0;
3131 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00003132 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003133
3134 do {
3135 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003136 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00003137 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003138
Eric Dumazetb2111722012-12-28 06:06:37 +00003139 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003140 return -ENOMEM;
3141
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003142 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00003143 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003144
Eric Dumazetb2111722012-12-28 06:06:37 +00003145 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3146 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003147 if (ret < 0)
3148 return -EFAULT;
3149
3150 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003151 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3152 copy);
3153 frg_cnt++;
3154 pfrag->offset += copy;
3155 get_page(pfrag->page);
3156
3157 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003158 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003159 skb->len += copy;
3160 skb->data_len += copy;
3161 offset += copy;
3162 length -= copy;
3163
3164 } while (length > 0);
3165
3166 return 0;
3167}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003168EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003169
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003170int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3171 int offset, size_t size)
3172{
3173 int i = skb_shinfo(skb)->nr_frags;
3174
3175 if (skb_can_coalesce(skb, i, page, offset)) {
3176 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3177 } else if (i < MAX_SKB_FRAGS) {
3178 get_page(page);
3179 skb_fill_page_desc(skb, i, page, offset, size);
3180 } else {
3181 return -EMSGSIZE;
3182 }
3183
3184 return 0;
3185}
3186EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3187
Herbert Xucbb042f2006-03-20 22:43:56 -08003188/**
3189 * skb_pull_rcsum - pull skb and update receive checksum
3190 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003191 * @len: length of data pulled
3192 *
3193 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003194 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003195 * receive path processing instead of skb_pull unless you know
3196 * that the checksum difference is zero (e.g., a valid IP header)
3197 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003198 */
Johannes Bergaf728682017-06-16 14:29:22 +02003199void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003200{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003201 unsigned char *data = skb->data;
3202
Herbert Xucbb042f2006-03-20 22:43:56 -08003203 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003204 __skb_pull(skb, len);
3205 skb_postpull_rcsum(skb, data, len);
3206 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003207}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003208EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3209
Herbert Xuf4c50d92006-06-22 03:02:40 -07003210/**
3211 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003212 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003213 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003214 *
3215 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003216 * a pointer to the first in a list of new skbs for the segments.
3217 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003218 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003219struct sk_buff *skb_segment(struct sk_buff *head_skb,
3220 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003221{
3222 struct sk_buff *segs = NULL;
3223 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003224 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003225 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3226 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3227 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003228 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003229 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003230 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003231 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003232 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003233 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003234 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003235 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003236 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003237 int err = -ENOMEM;
3238 int i = 0;
3239 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003240 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003241
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003242 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003243 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003244 if (unlikely(!proto))
3245 return ERR_PTR(-EINVAL);
3246
Alexander Duyck36c98382016-05-02 09:38:18 -07003247 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003248 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003249
Steffen Klassert07b26c92016-09-19 12:58:47 +02003250 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3251 if (!(features & NETIF_F_GSO_PARTIAL)) {
3252 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003253 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003254
3255 if (!list_skb ||
3256 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3257 goto normal;
3258
Ilan Tayari43170c42017-04-19 21:26:07 +03003259 /* If we get here then all the required
3260 * GSO features except frag_list are supported.
3261 * Try to split the SKB to multiple GSO SKBs
3262 * with no frag_list.
3263 * Currently we can do that only when the buffers don't
3264 * have a linear part and all the buffers except
3265 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003266 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003267 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003268 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003269 if (frag_len != iter->len && iter->next)
3270 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003271 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003272 goto normal;
3273
3274 len -= iter->len;
3275 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003276
3277 if (len != frag_len)
3278 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003279 }
3280
3281 /* GSO partial only requires that we trim off any excess that
3282 * doesn't fit into an MSS sized block, so take care of that
3283 * now.
3284 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003285 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003286 if (partial_segs > 1)
3287 mss *= partial_segs;
3288 else
3289 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003290 }
3291
Steffen Klassert07b26c92016-09-19 12:58:47 +02003292normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003293 headroom = skb_headroom(head_skb);
3294 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003295
3296 do {
3297 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003298 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003299 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003300 int size;
3301
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003302 if (unlikely(mss == GSO_BY_FRAGS)) {
3303 len = list_skb->len;
3304 } else {
3305 len = head_skb->len - offset;
3306 if (len > mss)
3307 len = mss;
3308 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003309
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003310 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003311 if (hsize < 0)
3312 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003313 if (hsize > len || !sg)
3314 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003315
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003316 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3317 (skb_headlen(list_skb) == len || sg)) {
3318 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003319
Herbert Xu9d8506c2013-11-21 11:10:04 -08003320 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003321 nfrags = skb_shinfo(list_skb)->nr_frags;
3322 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003323 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003324 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003325
3326 while (pos < offset + len) {
3327 BUG_ON(i >= nfrags);
3328
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003329 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003330 if (pos + size > offset + len)
3331 break;
3332
3333 i++;
3334 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003335 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003336 }
3337
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003338 nskb = skb_clone(list_skb, GFP_ATOMIC);
3339 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003340
3341 if (unlikely(!nskb))
3342 goto err;
3343
Herbert Xu9d8506c2013-11-21 11:10:04 -08003344 if (unlikely(pskb_trim(nskb, len))) {
3345 kfree_skb(nskb);
3346 goto err;
3347 }
3348
Alexander Duyckec47ea82012-05-04 14:26:56 +00003349 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003350 if (skb_cow_head(nskb, doffset + headroom)) {
3351 kfree_skb(nskb);
3352 goto err;
3353 }
3354
Alexander Duyckec47ea82012-05-04 14:26:56 +00003355 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003356 skb_release_head_state(nskb);
3357 __skb_push(nskb, doffset);
3358 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003359 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003360 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003361 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003362
3363 if (unlikely(!nskb))
3364 goto err;
3365
3366 skb_reserve(nskb, headroom);
3367 __skb_put(nskb, doffset);
3368 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003369
3370 if (segs)
3371 tail->next = nskb;
3372 else
3373 segs = nskb;
3374 tail = nskb;
3375
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003376 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003377
Eric Dumazet030737b2013-10-19 11:42:54 -07003378 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003379 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003380
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003381 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003382 nskb->data - tnl_hlen,
3383 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003384
Herbert Xu9d8506c2013-11-21 11:10:04 -08003385 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003386 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003387
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003388 if (!sg) {
3389 if (!nskb->remcsum_offload)
3390 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003391 SKB_GSO_CB(nskb)->csum =
3392 skb_copy_and_csum_bits(head_skb, offset,
3393 skb_put(nskb, len),
3394 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003395 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003396 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003397 continue;
3398 }
3399
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003400 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003401
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003402 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003403 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003404
Willem de Bruijnfff88032017-06-08 11:35:03 -04003405 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3406 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003407
Herbert Xu9d8506c2013-11-21 11:10:04 -08003408 while (pos < offset + len) {
3409 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003410 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003411
3412 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003413 nfrags = skb_shinfo(list_skb)->nr_frags;
3414 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003415 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003416
3417 BUG_ON(!nfrags);
3418
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003419 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003420 }
3421
3422 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3423 MAX_SKB_FRAGS)) {
3424 net_warn_ratelimited(
3425 "skb_segment: too many frags: %u %u\n",
3426 pos, mss);
3427 goto err;
3428 }
3429
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003430 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3431 goto err;
3432
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003433 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003434 __skb_frag_ref(nskb_frag);
3435 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003436
3437 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003438 nskb_frag->page_offset += offset - pos;
3439 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003440 }
3441
Herbert Xu89319d382008-12-15 23:26:06 -08003442 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003443
3444 if (pos + size <= offset + len) {
3445 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003446 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003447 pos += size;
3448 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003449 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003450 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003451 }
3452
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003453 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003454 }
3455
Herbert Xu89319d382008-12-15 23:26:06 -08003456skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003457 nskb->data_len = len - hsize;
3458 nskb->len += nskb->data_len;
3459 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003460
Simon Horman1cdbcb72013-05-19 15:46:49 +00003461perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003462 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003463 if (skb_has_shared_frag(nskb)) {
3464 err = __skb_linearize(nskb);
3465 if (err)
3466 goto err;
3467 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003468 if (!nskb->remcsum_offload)
3469 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003470 SKB_GSO_CB(nskb)->csum =
3471 skb_checksum(nskb, doffset,
3472 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003473 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003474 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003475 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003476 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003477
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003478 /* Some callers want to get the end of the list.
3479 * Put it in segs->prev to avoid walking the list.
3480 * (see validate_xmit_skb_list() for example)
3481 */
3482 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003483
Alexander Duyck802ab552016-04-10 21:45:03 -04003484 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003485 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003486 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003487 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003488
3489 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003490 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003491 type &= ~SKB_GSO_DODGY;
3492
3493 /* Update GSO info and prepare to start updating headers on
3494 * our way back down the stack of protocols.
3495 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003496 for (iter = segs; iter; iter = iter->next) {
3497 skb_shinfo(iter)->gso_size = gso_size;
3498 skb_shinfo(iter)->gso_segs = partial_segs;
3499 skb_shinfo(iter)->gso_type = type;
3500 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3501 }
3502
3503 if (tail->len - doffset <= gso_size)
3504 skb_shinfo(tail)->gso_size = 0;
3505 else if (tail != segs)
3506 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003507 }
3508
Toshiaki Makita432c8562014-10-27 10:30:51 -07003509 /* Following permits correct backpressure, for protocols
3510 * using skb_set_owner_w().
3511 * Idea is to tranfert ownership from head_skb to last segment.
3512 */
3513 if (head_skb->destructor == sock_wfree) {
3514 swap(tail->truesize, head_skb->truesize);
3515 swap(tail->destructor, head_skb->destructor);
3516 swap(tail->sk, head_skb->sk);
3517 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003518 return segs;
3519
3520err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003521 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003522 return ERR_PTR(err);
3523}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003524EXPORT_SYMBOL_GPL(skb_segment);
3525
Herbert Xu71d93b32008-12-15 23:42:33 -08003526int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3527{
Eric Dumazet8a291112013-10-08 09:02:23 -07003528 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003529 unsigned int offset = skb_gro_offset(skb);
3530 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003531 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003532 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003533 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003534
Eric Dumazet8a291112013-10-08 09:02:23 -07003535 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003536 return -E2BIG;
3537
Eric Dumazet29e98242014-05-16 11:34:37 -07003538 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003539 pinfo = skb_shinfo(lp);
3540
3541 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003542 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003543 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003544 int i = skbinfo->nr_frags;
3545 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003546
Herbert Xu66e92fc2009-05-26 18:50:32 +00003547 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003548 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003549
Eric Dumazet8a291112013-10-08 09:02:23 -07003550 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003551 pinfo->nr_frags = nr_frags;
3552 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003553
Herbert Xu9aaa1562009-05-26 18:50:33 +00003554 frag = pinfo->frags + nr_frags;
3555 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003556 do {
3557 *--frag = *--frag2;
3558 } while (--i);
3559
3560 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003561 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003562
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003563 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003564 delta_truesize = skb->truesize -
3565 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003566
Herbert Xuf5572062009-01-14 20:40:03 -08003567 skb->truesize -= skb->data_len;
3568 skb->len -= skb->data_len;
3569 skb->data_len = 0;
3570
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003571 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003572 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003573 } else if (skb->head_frag) {
3574 int nr_frags = pinfo->nr_frags;
3575 skb_frag_t *frag = pinfo->frags + nr_frags;
3576 struct page *page = virt_to_head_page(skb->head);
3577 unsigned int first_size = headlen - offset;
3578 unsigned int first_offset;
3579
3580 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003581 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003582
3583 first_offset = skb->data -
3584 (unsigned char *)page_address(page) +
3585 offset;
3586
3587 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3588
3589 frag->page.p = page;
3590 frag->page_offset = first_offset;
3591 skb_frag_size_set(frag, first_size);
3592
3593 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3594 /* We dont need to clear skbinfo->nr_frags here */
3595
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003596 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003597 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3598 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003599 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003600
3601merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003602 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003603 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003604 unsigned int eat = offset - headlen;
3605
3606 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003607 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003608 skb->data_len -= eat;
3609 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003610 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003611 }
3612
Herbert Xu67147ba2009-05-26 18:50:22 +00003613 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003614
Eric Dumazet29e98242014-05-16 11:34:37 -07003615 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003616 skb_shinfo(p)->frag_list = skb;
3617 else
3618 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003619 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003620 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003621 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003622
Herbert Xu5d38a072009-01-04 16:13:40 -08003623done:
3624 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003625 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003626 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003627 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003628 if (lp != p) {
3629 lp->data_len += len;
3630 lp->truesize += delta_truesize;
3631 lp->len += len;
3632 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003633 NAPI_GRO_CB(skb)->same_flow = 1;
3634 return 0;
3635}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003636EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638void __init skb_init(void)
3639{
3640 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3641 sizeof(struct sk_buff),
3642 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003643 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003644 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003645 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003646 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003647 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003648 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003649 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
David S. Miller51c739d2007-10-30 21:29:29 -07003652static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003653__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3654 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003655{
David S. Miller1a028e52007-04-27 15:21:23 -07003656 int start = skb_headlen(skb);
3657 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003658 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003659 int elt = 0;
3660
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003661 if (unlikely(recursion_level >= 24))
3662 return -EMSGSIZE;
3663
David Howells716ea3a2007-04-02 20:19:53 -07003664 if (copy > 0) {
3665 if (copy > len)
3666 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003667 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003668 elt++;
3669 if ((len -= copy) == 0)
3670 return elt;
3671 offset += copy;
3672 }
3673
3674 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003675 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003676
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003677 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003678
Eric Dumazet9e903e02011-10-18 21:00:24 +00003679 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003680 if ((copy = end - offset) > 0) {
3681 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003682 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3683 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003684
3685 if (copy > len)
3686 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003687 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003688 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003689 elt++;
3690 if (!(len -= copy))
3691 return elt;
3692 offset += copy;
3693 }
David S. Miller1a028e52007-04-27 15:21:23 -07003694 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003695 }
3696
David S. Millerfbb398a2009-06-09 00:18:59 -07003697 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003698 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003699
David S. Millerfbb398a2009-06-09 00:18:59 -07003700 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003701
David S. Millerfbb398a2009-06-09 00:18:59 -07003702 end = start + frag_iter->len;
3703 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003704 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3705 return -EMSGSIZE;
3706
David S. Millerfbb398a2009-06-09 00:18:59 -07003707 if (copy > len)
3708 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003709 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3710 copy, recursion_level + 1);
3711 if (unlikely(ret < 0))
3712 return ret;
3713 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003714 if ((len -= copy) == 0)
3715 return elt;
3716 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003717 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003718 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003719 }
3720 BUG_ON(len);
3721 return elt;
3722}
3723
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003724/**
3725 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3726 * @skb: Socket buffer containing the buffers to be mapped
3727 * @sg: The scatter-gather list to map into
3728 * @offset: The offset into the buffer's contents to start mapping
3729 * @len: Length of buffer space to be mapped
3730 *
3731 * Fill the specified scatter-gather list with mappings/pointers into a
3732 * region of the buffer space attached to a socket buffer. Returns either
3733 * the number of scatterlist items used, or -EMSGSIZE if the contents
3734 * could not fit.
3735 */
3736int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3737{
3738 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3739
3740 if (nsg <= 0)
3741 return nsg;
3742
3743 sg_mark_end(&sg[nsg - 1]);
3744
3745 return nsg;
3746}
3747EXPORT_SYMBOL_GPL(skb_to_sgvec);
3748
Fan Du25a91d82014-01-18 09:54:23 +08003749/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3750 * sglist without mark the sg which contain last skb data as the end.
3751 * So the caller can mannipulate sg list as will when padding new data after
3752 * the first call without calling sg_unmark_end to expend sg list.
3753 *
3754 * Scenario to use skb_to_sgvec_nomark:
3755 * 1. sg_init_table
3756 * 2. skb_to_sgvec_nomark(payload1)
3757 * 3. skb_to_sgvec_nomark(payload2)
3758 *
3759 * This is equivalent to:
3760 * 1. sg_init_table
3761 * 2. skb_to_sgvec(payload1)
3762 * 3. sg_unmark_end
3763 * 4. skb_to_sgvec(payload2)
3764 *
3765 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3766 * is more preferable.
3767 */
3768int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3769 int offset, int len)
3770{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003771 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003772}
3773EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3774
David S. Miller51c739d2007-10-30 21:29:29 -07003775
David S. Miller51c739d2007-10-30 21:29:29 -07003776
David Howells716ea3a2007-04-02 20:19:53 -07003777/**
3778 * skb_cow_data - Check that a socket buffer's data buffers are writable
3779 * @skb: The socket buffer to check.
3780 * @tailbits: Amount of trailing space to be added
3781 * @trailer: Returned pointer to the skb where the @tailbits space begins
3782 *
3783 * Make sure that the data buffers attached to a socket buffer are
3784 * writable. If they are not, private copies are made of the data buffers
3785 * and the socket buffer is set to use these instead.
3786 *
3787 * If @tailbits is given, make sure that there is space to write @tailbits
3788 * bytes of data beyond current end of socket buffer. @trailer will be
3789 * set to point to the skb in which this space begins.
3790 *
3791 * The number of scatterlist elements required to completely map the
3792 * COW'd and extended socket buffer will be returned.
3793 */
3794int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3795{
3796 int copyflag;
3797 int elt;
3798 struct sk_buff *skb1, **skb_p;
3799
3800 /* If skb is cloned or its head is paged, reallocate
3801 * head pulling out all the pages (pages are considered not writable
3802 * at the moment even if they are anonymous).
3803 */
3804 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3805 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3806 return -ENOMEM;
3807
3808 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003809 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003810 /* A little of trouble, not enough of space for trailer.
3811 * This should not happen, when stack is tuned to generate
3812 * good frames. OK, on miss we reallocate and reserve even more
3813 * space, 128 bytes is fair. */
3814
3815 if (skb_tailroom(skb) < tailbits &&
3816 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3817 return -ENOMEM;
3818
3819 /* Voila! */
3820 *trailer = skb;
3821 return 1;
3822 }
3823
3824 /* Misery. We are in troubles, going to mincer fragments... */
3825
3826 elt = 1;
3827 skb_p = &skb_shinfo(skb)->frag_list;
3828 copyflag = 0;
3829
3830 while ((skb1 = *skb_p) != NULL) {
3831 int ntail = 0;
3832
3833 /* The fragment is partially pulled by someone,
3834 * this can happen on input. Copy it and everything
3835 * after it. */
3836
3837 if (skb_shared(skb1))
3838 copyflag = 1;
3839
3840 /* If the skb is the last, worry about trailer. */
3841
3842 if (skb1->next == NULL && tailbits) {
3843 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003844 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003845 skb_tailroom(skb1) < tailbits)
3846 ntail = tailbits + 128;
3847 }
3848
3849 if (copyflag ||
3850 skb_cloned(skb1) ||
3851 ntail ||
3852 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003853 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003854 struct sk_buff *skb2;
3855
3856 /* Fuck, we are miserable poor guys... */
3857 if (ntail == 0)
3858 skb2 = skb_copy(skb1, GFP_ATOMIC);
3859 else
3860 skb2 = skb_copy_expand(skb1,
3861 skb_headroom(skb1),
3862 ntail,
3863 GFP_ATOMIC);
3864 if (unlikely(skb2 == NULL))
3865 return -ENOMEM;
3866
3867 if (skb1->sk)
3868 skb_set_owner_w(skb2, skb1->sk);
3869
3870 /* Looking around. Are we still alive?
3871 * OK, link new skb, drop old one */
3872
3873 skb2->next = skb1->next;
3874 *skb_p = skb2;
3875 kfree_skb(skb1);
3876 skb1 = skb2;
3877 }
3878 elt++;
3879 *trailer = skb1;
3880 skb_p = &skb1->next;
3881 }
3882
3883 return elt;
3884}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003885EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003886
Eric Dumazetb1faf562010-05-31 23:44:05 -07003887static void sock_rmem_free(struct sk_buff *skb)
3888{
3889 struct sock *sk = skb->sk;
3890
3891 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3892}
3893
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003894static void skb_set_err_queue(struct sk_buff *skb)
3895{
3896 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3897 * So, it is safe to (mis)use it to mark skbs on the error queue.
3898 */
3899 skb->pkt_type = PACKET_OUTGOING;
3900 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3901}
3902
Eric Dumazetb1faf562010-05-31 23:44:05 -07003903/*
3904 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3905 */
3906int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3907{
3908 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003909 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003910 return -ENOMEM;
3911
3912 skb_orphan(skb);
3913 skb->sk = sk;
3914 skb->destructor = sock_rmem_free;
3915 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003916 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003917
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003918 /* before exiting rcu section, make sure dst is refcounted */
3919 skb_dst_force(skb);
3920
Eric Dumazetb1faf562010-05-31 23:44:05 -07003921 skb_queue_tail(&sk->sk_error_queue, skb);
3922 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003923 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003924 return 0;
3925}
3926EXPORT_SYMBOL(sock_queue_err_skb);
3927
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003928static bool is_icmp_err_skb(const struct sk_buff *skb)
3929{
3930 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3931 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3932}
3933
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003934struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3935{
3936 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003937 struct sk_buff *skb, *skb_next = NULL;
3938 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003939 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003940
Eric Dumazet997d5c32015-02-18 05:47:55 -08003941 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003942 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003943 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003944 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003945 if (icmp_next)
3946 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3947 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08003948 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003949
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003950 if (is_icmp_err_skb(skb) && !icmp_next)
3951 sk->sk_err = 0;
3952
3953 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003954 sk->sk_error_report(sk);
3955
3956 return skb;
3957}
3958EXPORT_SYMBOL(sock_dequeue_err_skb);
3959
Alexander Duyckcab41c42014-09-10 18:05:26 -04003960/**
3961 * skb_clone_sk - create clone of skb, and take reference to socket
3962 * @skb: the skb to clone
3963 *
3964 * This function creates a clone of a buffer that holds a reference on
3965 * sk_refcnt. Buffers created via this function are meant to be
3966 * returned using sock_queue_err_skb, or free via kfree_skb.
3967 *
3968 * When passing buffers allocated with this function to sock_queue_err_skb
3969 * it is necessary to wrap the call with sock_hold/sock_put in order to
3970 * prevent the socket from being released prior to being enqueued on
3971 * the sk_error_queue.
3972 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003973struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3974{
3975 struct sock *sk = skb->sk;
3976 struct sk_buff *clone;
3977
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003978 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04003979 return NULL;
3980
3981 clone = skb_clone(skb, GFP_ATOMIC);
3982 if (!clone) {
3983 sock_put(sk);
3984 return NULL;
3985 }
3986
3987 clone->sk = sk;
3988 clone->destructor = sock_efree;
3989
3990 return clone;
3991}
3992EXPORT_SYMBOL(skb_clone_sk);
3993
Alexander Duyck37846ef2014-09-04 13:31:10 -04003994static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3995 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003996 int tstype,
3997 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003998{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003999 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004000 int err;
4001
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004002 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4003
Patrick Ohlyac45f602009-02-12 05:03:37 +00004004 serr = SKB_EXT_ERR(skb);
4005 memset(serr, 0, sizeof(*serr));
4006 serr->ee.ee_errno = ENOMSG;
4007 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004008 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004009 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004010 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004011 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004012 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004013 if (sk->sk_protocol == IPPROTO_TCP &&
4014 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004015 serr->ee.ee_data -= sk->sk_tskey;
4016 }
Eric Dumazet29030372010-05-29 00:20:48 -07004017
Patrick Ohlyac45f602009-02-12 05:03:37 +00004018 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004019
Patrick Ohlyac45f602009-02-12 05:03:37 +00004020 if (err)
4021 kfree_skb(skb);
4022}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004023
Willem de Bruijnb245be12015-01-30 13:29:32 -05004024static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4025{
4026 bool ret;
4027
4028 if (likely(sysctl_tstamp_allow_data || tsonly))
4029 return true;
4030
4031 read_lock_bh(&sk->sk_callback_lock);
4032 ret = sk->sk_socket && sk->sk_socket->file &&
4033 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4034 read_unlock_bh(&sk->sk_callback_lock);
4035 return ret;
4036}
4037
Alexander Duyck37846ef2014-09-04 13:31:10 -04004038void skb_complete_tx_timestamp(struct sk_buff *skb,
4039 struct skb_shared_hwtstamps *hwtstamps)
4040{
4041 struct sock *sk = skb->sk;
4042
Willem de Bruijnb245be12015-01-30 13:29:32 -05004043 if (!skb_may_tx_timestamp(sk, false))
4044 return;
4045
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004046 /* Take a reference to prevent skb_orphan() from freeing the socket,
4047 * but only if the socket refcount is not zero.
4048 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004049 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004050 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004051 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004052 sock_put(sk);
4053 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004054}
4055EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4056
4057void __skb_tstamp_tx(struct sk_buff *orig_skb,
4058 struct skb_shared_hwtstamps *hwtstamps,
4059 struct sock *sk, int tstype)
4060{
4061 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004062 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004063
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004064 if (!sk)
4065 return;
4066
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004067 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4068 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4069 return;
4070
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004071 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4072 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004073 return;
4074
Francis Yan1c885802016-11-27 23:07:18 -08004075 if (tsonly) {
4076#ifdef CONFIG_INET
4077 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4078 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004079 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004080 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004081 opt_stats = true;
4082 } else
Francis Yan1c885802016-11-27 23:07:18 -08004083#endif
4084 skb = alloc_skb(0, GFP_ATOMIC);
4085 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004086 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004087 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004088 if (!skb)
4089 return;
4090
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004091 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004092 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4093 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004094 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4095 }
4096
4097 if (hwtstamps)
4098 *skb_hwtstamps(skb) = *hwtstamps;
4099 else
4100 skb->tstamp = ktime_get_real();
4101
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004102 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004103}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004104EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4105
4106void skb_tstamp_tx(struct sk_buff *orig_skb,
4107 struct skb_shared_hwtstamps *hwtstamps)
4108{
4109 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4110 SCM_TSTAMP_SND);
4111}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004112EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4113
Johannes Berg6e3e9392011-11-09 10:15:42 +01004114void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4115{
4116 struct sock *sk = skb->sk;
4117 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004118 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004119
4120 skb->wifi_acked_valid = 1;
4121 skb->wifi_acked = acked;
4122
4123 serr = SKB_EXT_ERR(skb);
4124 memset(serr, 0, sizeof(*serr));
4125 serr->ee.ee_errno = ENOMSG;
4126 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4127
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004128 /* Take a reference to prevent skb_orphan() from freeing the socket,
4129 * but only if the socket refcount is not zero.
4130 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004131 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004132 err = sock_queue_err_skb(sk, skb);
4133 sock_put(sk);
4134 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004135 if (err)
4136 kfree_skb(skb);
4137}
4138EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4139
Rusty Russellf35d9d82008-02-04 23:49:54 -05004140/**
4141 * skb_partial_csum_set - set up and verify partial csum values for packet
4142 * @skb: the skb to set
4143 * @start: the number of bytes after skb->data to start checksumming.
4144 * @off: the offset from start to place the checksum.
4145 *
4146 * For untrusted partially-checksummed packets, we need to make sure the values
4147 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4148 *
4149 * This function checks and sets those values and skb->ip_summed: if this
4150 * returns false you should drop the packet.
4151 */
4152bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4153{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004154 if (unlikely(start > skb_headlen(skb)) ||
4155 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004156 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4157 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004158 return false;
4159 }
4160 skb->ip_summed = CHECKSUM_PARTIAL;
4161 skb->csum_start = skb_headroom(skb) + start;
4162 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004163 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004164 return true;
4165}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004166EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004167
Paul Durranted1f50c2014-01-09 10:02:46 +00004168static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4169 unsigned int max)
4170{
4171 if (skb_headlen(skb) >= len)
4172 return 0;
4173
4174 /* If we need to pullup then pullup to the max, so we
4175 * won't need to do it again.
4176 */
4177 if (max > skb->len)
4178 max = skb->len;
4179
4180 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4181 return -ENOMEM;
4182
4183 if (skb_headlen(skb) < len)
4184 return -EPROTO;
4185
4186 return 0;
4187}
4188
Jan Beulichf9708b42014-03-11 13:56:05 +00004189#define MAX_TCP_HDR_LEN (15 * 4)
4190
4191static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4192 typeof(IPPROTO_IP) proto,
4193 unsigned int off)
4194{
4195 switch (proto) {
4196 int err;
4197
4198 case IPPROTO_TCP:
4199 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4200 off + MAX_TCP_HDR_LEN);
4201 if (!err && !skb_partial_csum_set(skb, off,
4202 offsetof(struct tcphdr,
4203 check)))
4204 err = -EPROTO;
4205 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4206
4207 case IPPROTO_UDP:
4208 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4209 off + sizeof(struct udphdr));
4210 if (!err && !skb_partial_csum_set(skb, off,
4211 offsetof(struct udphdr,
4212 check)))
4213 err = -EPROTO;
4214 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4215 }
4216
4217 return ERR_PTR(-EPROTO);
4218}
4219
Paul Durranted1f50c2014-01-09 10:02:46 +00004220/* This value should be large enough to cover a tagged ethernet header plus
4221 * maximally sized IP and TCP or UDP headers.
4222 */
4223#define MAX_IP_HDR_LEN 128
4224
Jan Beulichf9708b42014-03-11 13:56:05 +00004225static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004226{
4227 unsigned int off;
4228 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004229 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004230 int err;
4231
4232 fragment = false;
4233
4234 err = skb_maybe_pull_tail(skb,
4235 sizeof(struct iphdr),
4236 MAX_IP_HDR_LEN);
4237 if (err < 0)
4238 goto out;
4239
4240 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4241 fragment = true;
4242
4243 off = ip_hdrlen(skb);
4244
4245 err = -EPROTO;
4246
4247 if (fragment)
4248 goto out;
4249
Jan Beulichf9708b42014-03-11 13:56:05 +00004250 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4251 if (IS_ERR(csum))
4252 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004253
Jan Beulichf9708b42014-03-11 13:56:05 +00004254 if (recalculate)
4255 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4256 ip_hdr(skb)->daddr,
4257 skb->len - off,
4258 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004259 err = 0;
4260
4261out:
4262 return err;
4263}
4264
4265/* This value should be large enough to cover a tagged ethernet header plus
4266 * an IPv6 header, all options, and a maximal TCP or UDP header.
4267 */
4268#define MAX_IPV6_HDR_LEN 256
4269
4270#define OPT_HDR(type, skb, off) \
4271 (type *)(skb_network_header(skb) + (off))
4272
4273static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4274{
4275 int err;
4276 u8 nexthdr;
4277 unsigned int off;
4278 unsigned int len;
4279 bool fragment;
4280 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004281 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004282
4283 fragment = false;
4284 done = false;
4285
4286 off = sizeof(struct ipv6hdr);
4287
4288 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4289 if (err < 0)
4290 goto out;
4291
4292 nexthdr = ipv6_hdr(skb)->nexthdr;
4293
4294 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4295 while (off <= len && !done) {
4296 switch (nexthdr) {
4297 case IPPROTO_DSTOPTS:
4298 case IPPROTO_HOPOPTS:
4299 case IPPROTO_ROUTING: {
4300 struct ipv6_opt_hdr *hp;
4301
4302 err = skb_maybe_pull_tail(skb,
4303 off +
4304 sizeof(struct ipv6_opt_hdr),
4305 MAX_IPV6_HDR_LEN);
4306 if (err < 0)
4307 goto out;
4308
4309 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4310 nexthdr = hp->nexthdr;
4311 off += ipv6_optlen(hp);
4312 break;
4313 }
4314 case IPPROTO_AH: {
4315 struct ip_auth_hdr *hp;
4316
4317 err = skb_maybe_pull_tail(skb,
4318 off +
4319 sizeof(struct ip_auth_hdr),
4320 MAX_IPV6_HDR_LEN);
4321 if (err < 0)
4322 goto out;
4323
4324 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4325 nexthdr = hp->nexthdr;
4326 off += ipv6_authlen(hp);
4327 break;
4328 }
4329 case IPPROTO_FRAGMENT: {
4330 struct frag_hdr *hp;
4331
4332 err = skb_maybe_pull_tail(skb,
4333 off +
4334 sizeof(struct frag_hdr),
4335 MAX_IPV6_HDR_LEN);
4336 if (err < 0)
4337 goto out;
4338
4339 hp = OPT_HDR(struct frag_hdr, skb, off);
4340
4341 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4342 fragment = true;
4343
4344 nexthdr = hp->nexthdr;
4345 off += sizeof(struct frag_hdr);
4346 break;
4347 }
4348 default:
4349 done = true;
4350 break;
4351 }
4352 }
4353
4354 err = -EPROTO;
4355
4356 if (!done || fragment)
4357 goto out;
4358
Jan Beulichf9708b42014-03-11 13:56:05 +00004359 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4360 if (IS_ERR(csum))
4361 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004362
Jan Beulichf9708b42014-03-11 13:56:05 +00004363 if (recalculate)
4364 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4365 &ipv6_hdr(skb)->daddr,
4366 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004367 err = 0;
4368
4369out:
4370 return err;
4371}
4372
4373/**
4374 * skb_checksum_setup - set up partial checksum offset
4375 * @skb: the skb to set up
4376 * @recalculate: if true the pseudo-header checksum will be recalculated
4377 */
4378int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4379{
4380 int err;
4381
4382 switch (skb->protocol) {
4383 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004384 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004385 break;
4386
4387 case htons(ETH_P_IPV6):
4388 err = skb_checksum_setup_ipv6(skb, recalculate);
4389 break;
4390
4391 default:
4392 err = -EPROTO;
4393 break;
4394 }
4395
4396 return err;
4397}
4398EXPORT_SYMBOL(skb_checksum_setup);
4399
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004400/**
4401 * skb_checksum_maybe_trim - maybe trims the given skb
4402 * @skb: the skb to check
4403 * @transport_len: the data length beyond the network header
4404 *
4405 * Checks whether the given skb has data beyond the given transport length.
4406 * If so, returns a cloned skb trimmed to this transport length.
4407 * Otherwise returns the provided skb. Returns NULL in error cases
4408 * (e.g. transport_len exceeds skb length or out-of-memory).
4409 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004410 * Caller needs to set the skb transport header and free any returned skb if it
4411 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004412 */
4413static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4414 unsigned int transport_len)
4415{
4416 struct sk_buff *skb_chk;
4417 unsigned int len = skb_transport_offset(skb) + transport_len;
4418 int ret;
4419
Linus Lüssinga5169932015-08-13 05:54:07 +02004420 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004421 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004422 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004423 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004424
4425 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004426 if (!skb_chk)
4427 return NULL;
4428
4429 ret = pskb_trim_rcsum(skb_chk, len);
4430 if (ret) {
4431 kfree_skb(skb_chk);
4432 return NULL;
4433 }
4434
4435 return skb_chk;
4436}
4437
4438/**
4439 * skb_checksum_trimmed - validate checksum of an skb
4440 * @skb: the skb to check
4441 * @transport_len: the data length beyond the network header
4442 * @skb_chkf: checksum function to use
4443 *
4444 * Applies the given checksum function skb_chkf to the provided skb.
4445 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4446 *
4447 * If the skb has data beyond the given transport length, then a
4448 * trimmed & cloned skb is checked and returned.
4449 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004450 * Caller needs to set the skb transport header and free any returned skb if it
4451 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004452 */
4453struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4454 unsigned int transport_len,
4455 __sum16(*skb_chkf)(struct sk_buff *skb))
4456{
4457 struct sk_buff *skb_chk;
4458 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004459 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004460
4461 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4462 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004463 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004464
Linus Lüssinga5169932015-08-13 05:54:07 +02004465 if (!pskb_may_pull(skb_chk, offset))
4466 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004467
Linus Lüssing9b368812016-02-24 04:21:42 +01004468 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004469 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004470 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004471
Linus Lüssinga5169932015-08-13 05:54:07 +02004472 if (ret)
4473 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004474
4475 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004476
4477err:
4478 if (skb_chk && skb_chk != skb)
4479 kfree_skb(skb_chk);
4480
4481 return NULL;
4482
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004483}
4484EXPORT_SYMBOL(skb_checksum_trimmed);
4485
Ben Hutchings4497b072008-06-19 16:22:28 -07004486void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4487{
Joe Perchese87cc472012-05-13 21:56:26 +00004488 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4489 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004490}
Ben Hutchings4497b072008-06-19 16:22:28 -07004491EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004492
4493void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4494{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004495 if (head_stolen) {
4496 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004497 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004498 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004499 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004500 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004501}
4502EXPORT_SYMBOL(kfree_skb_partial);
4503
4504/**
4505 * skb_try_coalesce - try to merge skb to prior one
4506 * @to: prior buffer
4507 * @from: buffer to add
4508 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004509 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004510 */
4511bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4512 bool *fragstolen, int *delta_truesize)
4513{
4514 int i, delta, len = from->len;
4515
4516 *fragstolen = false;
4517
4518 if (skb_cloned(to))
4519 return false;
4520
4521 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004522 if (len)
4523 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004524 *delta_truesize = 0;
4525 return true;
4526 }
4527
4528 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4529 return false;
4530
4531 if (skb_headlen(from) != 0) {
4532 struct page *page;
4533 unsigned int offset;
4534
4535 if (skb_shinfo(to)->nr_frags +
4536 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4537 return false;
4538
4539 if (skb_head_is_locked(from))
4540 return false;
4541
4542 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4543
4544 page = virt_to_head_page(from->head);
4545 offset = from->data - (unsigned char *)page_address(page);
4546
4547 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4548 page, offset, skb_headlen(from));
4549 *fragstolen = true;
4550 } else {
4551 if (skb_shinfo(to)->nr_frags +
4552 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4553 return false;
4554
Weiping Panf4b549a2012-09-28 20:15:30 +00004555 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004556 }
4557
4558 WARN_ON_ONCE(delta < len);
4559
4560 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4561 skb_shinfo(from)->frags,
4562 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4563 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4564
4565 if (!skb_cloned(from))
4566 skb_shinfo(from)->nr_frags = 0;
4567
Li RongQing8ea853f2012-09-18 16:53:21 +00004568 /* if the skb is not cloned this does nothing
4569 * since we set nr_frags to 0.
4570 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004571 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4572 skb_frag_ref(from, i);
4573
4574 to->truesize += delta;
4575 to->len += len;
4576 to->data_len += len;
4577
4578 *delta_truesize = delta;
4579 return true;
4580}
4581EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004582
4583/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004584 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004585 *
4586 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004587 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004588 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004589 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4590 * into/from a tunnel. Some information have to be cleared during these
4591 * operations.
4592 * skb_scrub_packet can also be used to clean a skb before injecting it in
4593 * another namespace (@xnet == true). We have to clear all information in the
4594 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004595 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004596void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004597{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004598 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004599 skb->pkt_type = PACKET_HOST;
4600 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004601 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004602 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004603 secpath_reset(skb);
4604 nf_reset(skb);
4605 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004606
4607 if (!xnet)
4608 return;
4609
4610 skb_orphan(skb);
4611 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004612}
4613EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004614
4615/**
4616 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4617 *
4618 * @skb: GSO skb
4619 *
4620 * skb_gso_transport_seglen is used to determine the real size of the
4621 * individual segments, including Layer4 headers (TCP/UDP).
4622 *
4623 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4624 */
4625unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4626{
4627 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004628 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004629
Florian Westphalf993bc22014-10-20 13:49:18 +02004630 if (skb->encapsulation) {
4631 thlen = skb_inner_transport_header(skb) -
4632 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004633
Florian Westphalf993bc22014-10-20 13:49:18 +02004634 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4635 thlen += inner_tcp_hdrlen(skb);
4636 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4637 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004638 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4639 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004640 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004641 /* UFO sets gso_size to the size of the fragmentation
4642 * payload, i.e. the size of the L4 (UDP) header is already
4643 * accounted for.
4644 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004645 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004646}
4647EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004648
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004649/**
4650 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4651 *
4652 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004653 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004654 *
4655 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4656 * once split.
4657 */
4658bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4659{
4660 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4661 const struct sk_buff *iter;
4662 unsigned int hlen;
4663
4664 hlen = skb_gso_network_seglen(skb);
4665
4666 if (shinfo->gso_size != GSO_BY_FRAGS)
4667 return hlen <= mtu;
4668
4669 /* Undo this so we can re-use header sizes */
4670 hlen -= GSO_BY_FRAGS;
4671
4672 skb_walk_frags(skb, iter) {
4673 if (hlen + skb_headlen(iter) > mtu)
4674 return false;
4675 }
4676
4677 return true;
4678}
4679EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4680
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004681static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4682{
4683 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4684 kfree_skb(skb);
4685 return NULL;
4686 }
4687
Vlad Yasevichf6548612015-12-14 17:44:10 -05004688 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004689 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004690 skb->mac_header += VLAN_HLEN;
4691 return skb;
4692}
4693
4694struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4695{
4696 struct vlan_hdr *vhdr;
4697 u16 vlan_tci;
4698
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004699 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004700 /* vlan_tci is already set-up so leave this for another time */
4701 return skb;
4702 }
4703
4704 skb = skb_share_check(skb, GFP_ATOMIC);
4705 if (unlikely(!skb))
4706 goto err_free;
4707
4708 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4709 goto err_free;
4710
4711 vhdr = (struct vlan_hdr *)skb->data;
4712 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4713 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4714
4715 skb_pull_rcsum(skb, VLAN_HLEN);
4716 vlan_set_encap_proto(skb, vhdr);
4717
4718 skb = skb_reorder_vlan_header(skb);
4719 if (unlikely(!skb))
4720 goto err_free;
4721
4722 skb_reset_network_header(skb);
4723 skb_reset_transport_header(skb);
4724 skb_reset_mac_len(skb);
4725
4726 return skb;
4727
4728err_free:
4729 kfree_skb(skb);
4730 return NULL;
4731}
4732EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004733
Jiri Pirkoe2195122014-11-19 14:05:01 +01004734int skb_ensure_writable(struct sk_buff *skb, int write_len)
4735{
4736 if (!pskb_may_pull(skb, write_len))
4737 return -ENOMEM;
4738
4739 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4740 return 0;
4741
4742 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4743}
4744EXPORT_SYMBOL(skb_ensure_writable);
4745
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004746/* remove VLAN header from packet and update csum accordingly.
4747 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4748 */
4749int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004750{
4751 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004752 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004753 int err;
4754
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004755 if (WARN_ONCE(offset,
4756 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4757 offset)) {
4758 return -EINVAL;
4759 }
4760
Jiri Pirko93515d52014-11-19 14:05:02 +01004761 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4762 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004763 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004764
4765 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4766
4767 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4768 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4769
4770 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4771 __skb_pull(skb, VLAN_HLEN);
4772
4773 vlan_set_encap_proto(skb, vhdr);
4774 skb->mac_header += VLAN_HLEN;
4775
4776 if (skb_network_offset(skb) < ETH_HLEN)
4777 skb_set_network_header(skb, ETH_HLEN);
4778
4779 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004780
4781 return err;
4782}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004783EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004784
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004785/* Pop a vlan tag either from hwaccel or from payload.
4786 * Expects skb->data at mac header.
4787 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004788int skb_vlan_pop(struct sk_buff *skb)
4789{
4790 u16 vlan_tci;
4791 __be16 vlan_proto;
4792 int err;
4793
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004794 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004795 skb->vlan_tci = 0;
4796 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004797 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004798 return 0;
4799
4800 err = __skb_vlan_pop(skb, &vlan_tci);
4801 if (err)
4802 return err;
4803 }
4804 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004805 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004806 return 0;
4807
4808 vlan_proto = skb->protocol;
4809 err = __skb_vlan_pop(skb, &vlan_tci);
4810 if (unlikely(err))
4811 return err;
4812
4813 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4814 return 0;
4815}
4816EXPORT_SYMBOL(skb_vlan_pop);
4817
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004818/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4819 * Expects skb->data at mac header.
4820 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004821int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4822{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004823 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004824 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004825 int err;
4826
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004827 if (WARN_ONCE(offset,
4828 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4829 offset)) {
4830 return -EINVAL;
4831 }
4832
Jiri Pirko93515d52014-11-19 14:05:02 +01004833 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004834 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004835 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004836 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004837
Jiri Pirko93515d52014-11-19 14:05:02 +01004838 skb->protocol = skb->vlan_proto;
4839 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004840
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004841 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004842 }
4843 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4844 return 0;
4845}
4846EXPORT_SYMBOL(skb_vlan_push);
4847
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004848/**
4849 * alloc_skb_with_frags - allocate skb with page frags
4850 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004851 * @header_len: size of linear part
4852 * @data_len: needed length in frags
4853 * @max_page_order: max page order desired.
4854 * @errcode: pointer to error code if any
4855 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004856 *
4857 * This can be used to allocate a paged skb, given a maximal order for frags.
4858 */
4859struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4860 unsigned long data_len,
4861 int max_page_order,
4862 int *errcode,
4863 gfp_t gfp_mask)
4864{
4865 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4866 unsigned long chunk;
4867 struct sk_buff *skb;
4868 struct page *page;
4869 gfp_t gfp_head;
4870 int i;
4871
4872 *errcode = -EMSGSIZE;
4873 /* Note this test could be relaxed, if we succeed to allocate
4874 * high order pages...
4875 */
4876 if (npages > MAX_SKB_FRAGS)
4877 return NULL;
4878
4879 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004880 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07004881 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004882
4883 *errcode = -ENOBUFS;
4884 skb = alloc_skb(header_len, gfp_head);
4885 if (!skb)
4886 return NULL;
4887
4888 skb->truesize += npages << PAGE_SHIFT;
4889
4890 for (i = 0; npages > 0; i++) {
4891 int order = max_page_order;
4892
4893 while (order) {
4894 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004895 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004896 __GFP_COMP |
4897 __GFP_NOWARN |
4898 __GFP_NORETRY,
4899 order);
4900 if (page)
4901 goto fill_page;
4902 /* Do not retry other high order allocations */
4903 order = 1;
4904 max_page_order = 0;
4905 }
4906 order--;
4907 }
4908 page = alloc_page(gfp_mask);
4909 if (!page)
4910 goto failure;
4911fill_page:
4912 chunk = min_t(unsigned long, data_len,
4913 PAGE_SIZE << order);
4914 skb_fill_page_desc(skb, i, page, 0, chunk);
4915 data_len -= chunk;
4916 npages -= 1 << order;
4917 }
4918 return skb;
4919
4920failure:
4921 kfree_skb(skb);
4922 return NULL;
4923}
4924EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004925
4926/* carve out the first off bytes from skb when off < headlen */
4927static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4928 const int headlen, gfp_t gfp_mask)
4929{
4930 int i;
4931 int size = skb_end_offset(skb);
4932 int new_hlen = headlen - off;
4933 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004934
4935 size = SKB_DATA_ALIGN(size);
4936
4937 if (skb_pfmemalloc(skb))
4938 gfp_mask |= __GFP_MEMALLOC;
4939 data = kmalloc_reserve(size +
4940 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4941 gfp_mask, NUMA_NO_NODE, NULL);
4942 if (!data)
4943 return -ENOMEM;
4944
4945 size = SKB_WITH_OVERHEAD(ksize(data));
4946
4947 /* Copy real data, and all frags */
4948 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4949 skb->len -= off;
4950
4951 memcpy((struct skb_shared_info *)(data + size),
4952 skb_shinfo(skb),
4953 offsetof(struct skb_shared_info,
4954 frags[skb_shinfo(skb)->nr_frags]));
4955 if (skb_cloned(skb)) {
4956 /* drop the old head gracefully */
4957 if (skb_orphan_frags(skb, gfp_mask)) {
4958 kfree(data);
4959 return -ENOMEM;
4960 }
4961 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4962 skb_frag_ref(skb, i);
4963 if (skb_has_frag_list(skb))
4964 skb_clone_fraglist(skb);
4965 skb_release_data(skb);
4966 } else {
4967 /* we can reuse existing recount- all we did was
4968 * relocate values
4969 */
4970 skb_free_head(skb);
4971 }
4972
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004973 skb->head = data;
4974 skb->data = data;
4975 skb->head_frag = 0;
4976#ifdef NET_SKBUFF_DATA_USES_OFFSET
4977 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004978#else
4979 skb->end = skb->head + size;
4980#endif
4981 skb_set_tail_pointer(skb, skb_headlen(skb));
4982 skb_headers_offset_update(skb, 0);
4983 skb->cloned = 0;
4984 skb->hdr_len = 0;
4985 skb->nohdr = 0;
4986 atomic_set(&skb_shinfo(skb)->dataref, 1);
4987
4988 return 0;
4989}
4990
4991static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4992
4993/* carve out the first eat bytes from skb's frag_list. May recurse into
4994 * pskb_carve()
4995 */
4996static int pskb_carve_frag_list(struct sk_buff *skb,
4997 struct skb_shared_info *shinfo, int eat,
4998 gfp_t gfp_mask)
4999{
5000 struct sk_buff *list = shinfo->frag_list;
5001 struct sk_buff *clone = NULL;
5002 struct sk_buff *insp = NULL;
5003
5004 do {
5005 if (!list) {
5006 pr_err("Not enough bytes to eat. Want %d\n", eat);
5007 return -EFAULT;
5008 }
5009 if (list->len <= eat) {
5010 /* Eaten as whole. */
5011 eat -= list->len;
5012 list = list->next;
5013 insp = list;
5014 } else {
5015 /* Eaten partially. */
5016 if (skb_shared(list)) {
5017 clone = skb_clone(list, gfp_mask);
5018 if (!clone)
5019 return -ENOMEM;
5020 insp = list->next;
5021 list = clone;
5022 } else {
5023 /* This may be pulled without problems. */
5024 insp = list;
5025 }
5026 if (pskb_carve(list, eat, gfp_mask) < 0) {
5027 kfree_skb(clone);
5028 return -ENOMEM;
5029 }
5030 break;
5031 }
5032 } while (eat);
5033
5034 /* Free pulled out fragments. */
5035 while ((list = shinfo->frag_list) != insp) {
5036 shinfo->frag_list = list->next;
5037 kfree_skb(list);
5038 }
5039 /* And insert new clone at head. */
5040 if (clone) {
5041 clone->next = list;
5042 shinfo->frag_list = clone;
5043 }
5044 return 0;
5045}
5046
5047/* carve off first len bytes from skb. Split line (off) is in the
5048 * non-linear part of skb
5049 */
5050static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5051 int pos, gfp_t gfp_mask)
5052{
5053 int i, k = 0;
5054 int size = skb_end_offset(skb);
5055 u8 *data;
5056 const int nfrags = skb_shinfo(skb)->nr_frags;
5057 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005058
5059 size = SKB_DATA_ALIGN(size);
5060
5061 if (skb_pfmemalloc(skb))
5062 gfp_mask |= __GFP_MEMALLOC;
5063 data = kmalloc_reserve(size +
5064 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5065 gfp_mask, NUMA_NO_NODE, NULL);
5066 if (!data)
5067 return -ENOMEM;
5068
5069 size = SKB_WITH_OVERHEAD(ksize(data));
5070
5071 memcpy((struct skb_shared_info *)(data + size),
5072 skb_shinfo(skb), offsetof(struct skb_shared_info,
5073 frags[skb_shinfo(skb)->nr_frags]));
5074 if (skb_orphan_frags(skb, gfp_mask)) {
5075 kfree(data);
5076 return -ENOMEM;
5077 }
5078 shinfo = (struct skb_shared_info *)(data + size);
5079 for (i = 0; i < nfrags; i++) {
5080 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5081
5082 if (pos + fsize > off) {
5083 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5084
5085 if (pos < off) {
5086 /* Split frag.
5087 * We have two variants in this case:
5088 * 1. Move all the frag to the second
5089 * part, if it is possible. F.e.
5090 * this approach is mandatory for TUX,
5091 * where splitting is expensive.
5092 * 2. Split is accurately. We make this.
5093 */
5094 shinfo->frags[0].page_offset += off - pos;
5095 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5096 }
5097 skb_frag_ref(skb, i);
5098 k++;
5099 }
5100 pos += fsize;
5101 }
5102 shinfo->nr_frags = k;
5103 if (skb_has_frag_list(skb))
5104 skb_clone_fraglist(skb);
5105
5106 if (k == 0) {
5107 /* split line is in frag list */
5108 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5109 }
5110 skb_release_data(skb);
5111
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005112 skb->head = data;
5113 skb->head_frag = 0;
5114 skb->data = data;
5115#ifdef NET_SKBUFF_DATA_USES_OFFSET
5116 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005117#else
5118 skb->end = skb->head + size;
5119#endif
5120 skb_reset_tail_pointer(skb);
5121 skb_headers_offset_update(skb, 0);
5122 skb->cloned = 0;
5123 skb->hdr_len = 0;
5124 skb->nohdr = 0;
5125 skb->len -= off;
5126 skb->data_len = skb->len;
5127 atomic_set(&skb_shinfo(skb)->dataref, 1);
5128 return 0;
5129}
5130
5131/* remove len bytes from the beginning of the skb */
5132static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5133{
5134 int headlen = skb_headlen(skb);
5135
5136 if (len < headlen)
5137 return pskb_carve_inside_header(skb, len, headlen, gfp);
5138 else
5139 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5140}
5141
5142/* Extract to_copy bytes starting at off from skb, and return this in
5143 * a new skb
5144 */
5145struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5146 int to_copy, gfp_t gfp)
5147{
5148 struct sk_buff *clone = skb_clone(skb, gfp);
5149
5150 if (!clone)
5151 return NULL;
5152
5153 if (pskb_carve(clone, off, gfp) < 0 ||
5154 pskb_trim(clone, to_copy)) {
5155 kfree_skb(clone);
5156 return NULL;
5157 }
5158 return clone;
5159}
5160EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005161
5162/**
5163 * skb_condense - try to get rid of fragments/frag_list if possible
5164 * @skb: buffer
5165 *
5166 * Can be used to save memory before skb is added to a busy queue.
5167 * If packet has bytes in frags and enough tail room in skb->head,
5168 * pull all of them, so that we can free the frags right now and adjust
5169 * truesize.
5170 * Notes:
5171 * We do not reallocate skb->head thus can not fail.
5172 * Caller must re-evaluate skb->truesize if needed.
5173 */
5174void skb_condense(struct sk_buff *skb)
5175{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005176 if (skb->data_len) {
5177 if (skb->data_len > skb->end - skb->tail ||
5178 skb_cloned(skb))
5179 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005180
Eric Dumazet3174fed2016-12-09 08:02:05 -08005181 /* Nice, we can free page frag(s) right now */
5182 __pskb_pull_tail(skb, skb->data_len);
5183 }
5184 /* At this point, skb->truesize might be over estimated,
5185 * because skb had a fragment, and fragments do not tell
5186 * their truesize.
5187 * When we pulled its content into skb->head, fragment
5188 * was freed, but __pskb_pull_tail() could not possibly
5189 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005190 */
5191 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5192}