blob: 9c0e015ff3fe2753f8ca20f6533b7aa6b93caca3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030052#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070063#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000064#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070065#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040066#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000072#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
74
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Eric Dumazetd7e88832012-04-30 08:10:34 +000081struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010083int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Jean Sacrenf05de732013-02-11 13:30:38 +000087 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000091 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Jean Sacrenf05de732013-02-11 13:30:38 +000098static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000099 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Joe Perchese005d192012-05-16 19:58:40 +0000101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000102 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 BUG();
106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jean Sacrenf05de732013-02-11 13:30:38 +0000110 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
161/**
David S. Millerd179cd12005-08-17 14:57:30 -0700162 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * @size: size to allocate
164 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800169 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
176 * %GFP_ATOMIC.
177 */
Al Virodd0fc662005-10-07 07:46:04 +0100178struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700179 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Christoph Lametere18b8902006-12-06 20:33:20 -0800181 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800182 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct sk_buff *skb;
184 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
189
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!skb)
196 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700197 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
203 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000204 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!data)
208 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
212 */
213 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700214 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300216 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300220 */
221 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700224 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300225 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 skb->head = data;
227 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700228 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700229 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000232
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800236 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000237 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238
Mel Gormanc93bdd02012-07-31 16:44:19 -0700239 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700240 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700242 fclones = container_of(skb, struct sk_buff_fclones, skb1);
243
244 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700245 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300246 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700247
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800248 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250out:
251 return skb;
252nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800253 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 skb = NULL;
255 goto out;
256}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800257EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700260 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000261 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700262 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000263 *
264 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100265 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700266 * @frag_size is 0, otherwise data should come from the page allocator
267 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000268 * The return is the new skb buffer.
269 * On a failure the return is %NULL, and @data is not freed.
270 * Notes :
271 * Before IO, driver allocates only data buffer where NIC put incoming frame
272 * Driver should add room at head (NET_SKB_PAD) and
273 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
274 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
275 * before giving packet to stack.
276 * RX rings only contains data buffers, not full skbs.
277 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700278struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000279{
280 struct skb_shared_info *shinfo;
281 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000282 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283
284 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
285 if (!skb)
286 return NULL;
287
Eric Dumazetd3836f22012-04-27 00:33:38 +0000288 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000289
290 memset(skb, 0, offsetof(struct sk_buff, tail));
291 skb->truesize = SKB_TRUESIZE(size);
Reshetova, Elena63354792017-06-30 13:07:58 +0300292 refcount_set(&skb->users, 1);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 skb->head = data;
294 skb->data = data;
295 skb_reset_tail_pointer(skb);
296 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800297 skb->mac_header = (typeof(skb->mac_header))~0U;
298 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299
300 /* make sure we initialize shinfo sequentially */
301 shinfo = skb_shinfo(skb);
302 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
303 atomic_set(&shinfo->dataref, 1);
304 kmemcheck_annotate_variable(shinfo->destructor_arg);
305
306 return skb;
307}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700308
309/* build_skb() is wrapper over __build_skb(), that specifically
310 * takes care of skb->head and skb->pfmemalloc
311 * This means that if @frag_size is not zero, then @data must be backed
312 * by a page fragment, not kmalloc() or vmalloc()
313 */
314struct sk_buff *build_skb(void *data, unsigned int frag_size)
315{
316 struct sk_buff *skb = __build_skb(data, frag_size);
317
318 if (skb && frag_size) {
319 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700320 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700321 skb->pfmemalloc = 1;
322 }
323 return skb;
324}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000325EXPORT_SYMBOL(build_skb);
326
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100327#define NAPI_SKB_CACHE_SIZE 64
328
329struct napi_alloc_cache {
330 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300331 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100332 void *skb_cache[NAPI_SKB_CACHE_SIZE];
333};
334
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700335static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100336static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800337
338static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
339{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700340 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800341 unsigned long flags;
342 void *data;
343
344 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700345 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800346 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000347 local_irq_restore(flags);
348 return data;
349}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700350
351/**
352 * netdev_alloc_frag - allocate a page fragment
353 * @fragsz: fragment size
354 *
355 * Allocates a frag from a page for receive buffer.
356 * Uses GFP_ATOMIC allocations.
357 */
358void *netdev_alloc_frag(unsigned int fragsz)
359{
360 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
361}
Eric Dumazet6f532612012-05-18 05:12:12 +0000362EXPORT_SYMBOL(netdev_alloc_frag);
363
Alexander Duyckffde7322014-12-09 19:40:42 -0800364static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
365{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100366 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700367
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800368 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800369}
370
371void *napi_alloc_frag(unsigned int fragsz)
372{
373 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
374}
375EXPORT_SYMBOL(napi_alloc_frag);
376
Eric Dumazet6f532612012-05-18 05:12:12 +0000377/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900380 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
382 *
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
387 *
388 * %NULL is returned if there is no free memory.
389 */
Alexander Duyck94519802015-05-06 21:11:40 -0700390struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
391 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800392{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700393 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700394 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800395 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700396 bool pfmemalloc;
397 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800398
Alexander Duyck94519802015-05-06 21:11:40 -0700399 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800400
Alexander Duyck94519802015-05-06 21:11:40 -0700401 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800402 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700403 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
404 if (!skb)
405 goto skb_fail;
406 goto skb_success;
407 }
Alexander Duyck94519802015-05-06 21:11:40 -0700408
409 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
410 len = SKB_DATA_ALIGN(len);
411
412 if (sk_memalloc_socks())
413 gfp_mask |= __GFP_MEMALLOC;
414
415 local_irq_save(flags);
416
417 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800418 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700419 pfmemalloc = nc->pfmemalloc;
420
421 local_irq_restore(flags);
422
423 if (unlikely(!data))
424 return NULL;
425
426 skb = __build_skb(data, len);
427 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700428 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700429 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700430 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431
Alexander Duyck94519802015-05-06 21:11:40 -0700432 /* use OR instead of assignment to avoid clearing of bits in mask */
433 if (pfmemalloc)
434 skb->pfmemalloc = 1;
435 skb->head_frag = 1;
436
Alexander Duycka080e7b2015-05-13 13:34:13 -0700437skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700438 skb_reserve(skb, NET_SKB_PAD);
439 skb->dev = dev;
440
Alexander Duycka080e7b2015-05-13 13:34:13 -0700441skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700442 return skb;
443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800444EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Alexander Duyckfd11a832014-12-09 19:40:49 -0800446/**
447 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
448 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900449 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800450 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
451 *
452 * Allocate a new sk_buff for use in NAPI receive. This buffer will
453 * attempt to allocate the head from a special reserved region used
454 * only for NAPI Rx allocation. By doing this we can save several
455 * CPU cycles by avoiding having to disable and re-enable IRQs.
456 *
457 * %NULL is returned if there is no free memory.
458 */
Alexander Duyck94519802015-05-06 21:11:40 -0700459struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
460 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800461{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100462 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800463 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700464 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800465
Alexander Duyck94519802015-05-06 21:11:40 -0700466 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467
Alexander Duyck94519802015-05-06 21:11:40 -0700468 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800469 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700470 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
471 if (!skb)
472 goto skb_fail;
473 goto skb_success;
474 }
Alexander Duyck94519802015-05-06 21:11:40 -0700475
476 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
477 len = SKB_DATA_ALIGN(len);
478
479 if (sk_memalloc_socks())
480 gfp_mask |= __GFP_MEMALLOC;
481
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800482 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700483 if (unlikely(!data))
484 return NULL;
485
486 skb = __build_skb(data, len);
487 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700488 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700489 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490 }
491
Alexander Duyck94519802015-05-06 21:11:40 -0700492 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100493 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700494 skb->pfmemalloc = 1;
495 skb->head_frag = 1;
496
Alexander Duycka080e7b2015-05-13 13:34:13 -0700497skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700498 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
499 skb->dev = napi->dev;
500
Alexander Duycka080e7b2015-05-13 13:34:13 -0700501skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800502 return skb;
503}
504EXPORT_SYMBOL(__napi_alloc_skb);
505
Peter Zijlstra654bed12008-10-07 14:22:33 -0700506void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000507 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700508{
509 skb_fill_page_desc(skb, i, page, off, size);
510 skb->len += size;
511 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000512 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700513}
514EXPORT_SYMBOL(skb_add_rx_frag);
515
Jason Wangf8e617e2013-11-01 14:07:47 +0800516void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
517 unsigned int truesize)
518{
519 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
520
521 skb_frag_size_add(frag, size);
522 skb->len += size;
523 skb->data_len += size;
524 skb->truesize += truesize;
525}
526EXPORT_SYMBOL(skb_coalesce_rx_frag);
527
Herbert Xu27b437c2006-07-13 19:26:39 -0700528static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700530 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700531 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Herbert Xu27b437c2006-07-13 19:26:39 -0700534static inline void skb_drop_fraglist(struct sk_buff *skb)
535{
536 skb_drop_list(&skb_shinfo(skb)->frag_list);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static void skb_clone_fraglist(struct sk_buff *skb)
540{
541 struct sk_buff *list;
542
David S. Millerfbb398a2009-06-09 00:18:59 -0700543 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 skb_get(list);
545}
546
Eric Dumazetd3836f22012-04-27 00:33:38 +0000547static void skb_free_head(struct sk_buff *skb)
548{
Alexander Duyck181edb22015-05-06 21:12:03 -0700549 unsigned char *head = skb->head;
550
Eric Dumazetd3836f22012-04-27 00:33:38 +0000551 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700552 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000553 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700554 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000555}
556
Adrian Bunk5bba1712006-06-29 13:02:35 -0700557static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Eric Dumazetff04a772014-09-23 18:39:30 -0700559 struct skb_shared_info *shinfo = skb_shinfo(skb);
560 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Dumazetff04a772014-09-23 18:39:30 -0700562 if (skb->cloned &&
563 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
564 &shinfo->dataref))
565 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000566
Eric Dumazetff04a772014-09-23 18:39:30 -0700567 for (i = 0; i < shinfo->nr_frags; i++)
568 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000569
Eric Dumazetff04a772014-09-23 18:39:30 -0700570 /*
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 Dumazet6ffe75e2014-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 Dumazet6ffe75e2014-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 Dumazet6ffe75e2014-12-03 17:04:39 -0800608 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700609 break;
610
Eric Dumazet6ffe75e2014-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 Dumazet6ffe75e2014-12-03 17:04:39 -0800616 return;
617fastpath:
618 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Paolo Abeni0a463c72017-06-12 11:23:42 +0200621void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Eric Dumazetadf30902009-06-02 05:19:30 +0000623 skb_dst_drop(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200624 secpath_reset(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700625 if (skb->destructor) {
626 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 skb->destructor(skb);
628 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000629#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100630 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100631#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200632#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nf_bridge_put(skb->nf_bridge);
634#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700635}
636
637/* Free everything but the sk_buff shell. */
638static void skb_release_all(struct sk_buff *skb)
639{
640 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200641 if (likely(skb->head))
642 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Herbert Xu2d4baff2007-11-26 23:11:19 +0800645/**
646 * __kfree_skb - private function
647 * @skb: buffer
648 *
649 * Free an sk_buff. Release anything attached to the buffer.
650 * Clean the state. This is an internal helper function. Users should
651 * always call kfree_skb
652 */
653
654void __kfree_skb(struct sk_buff *skb)
655{
656 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 kfree_skbmem(skb);
658}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800659EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800662 * kfree_skb - free an sk_buff
663 * @skb: buffer to free
664 *
665 * Drop a reference to the buffer and free it if the usage count has
666 * hit zero.
667 */
668void kfree_skb(struct sk_buff *skb)
669{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200670 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800671 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200672
Neil Hormanead2ceb2009-03-11 09:49:55 +0000673 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800674 __kfree_skb(skb);
675}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800676EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800677
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700678void kfree_skb_list(struct sk_buff *segs)
679{
680 while (segs) {
681 struct sk_buff *next = segs->next;
682
683 kfree_skb(segs);
684 segs = next;
685 }
686}
687EXPORT_SYMBOL(kfree_skb_list);
688
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700689/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000690 * skb_tx_error - report an sk_buff xmit error
691 * @skb: buffer that triggered an error
692 *
693 * Report xmit error if a device callback is tracking this skb.
694 * skb must be freed afterwards.
695 */
696void skb_tx_error(struct sk_buff *skb)
697{
698 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
699 struct ubuf_info *uarg;
700
701 uarg = skb_shinfo(skb)->destructor_arg;
702 if (uarg->callback)
703 uarg->callback(uarg, false);
704 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
705 }
706}
707EXPORT_SYMBOL(skb_tx_error);
708
709/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000710 * consume_skb - free an skbuff
711 * @skb: buffer to free
712 *
713 * Drop a ref to the buffer and free it if the usage count has hit zero
714 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
715 * is being dropped after a failure and notes that
716 */
717void consume_skb(struct sk_buff *skb)
718{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200719 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000720 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200721
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900722 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000723 __kfree_skb(skb);
724}
725EXPORT_SYMBOL(consume_skb);
726
Paolo Abeni0a463c72017-06-12 11:23:42 +0200727/**
728 * consume_stateless_skb - free an skbuff, assuming it is stateless
729 * @skb: buffer to free
730 *
731 * Works like consume_skb(), but this variant assumes that all the head
732 * states have been already dropped.
733 */
734void consume_stateless_skb(struct sk_buff *skb)
735{
736 if (!skb_unref(skb))
737 return;
738
739 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200740 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200741 kfree_skbmem(skb);
742}
743
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100744void __kfree_skb_flush(void)
745{
746 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
747
748 /* flush skb_cache if containing objects */
749 if (nc->skb_count) {
750 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
751 nc->skb_cache);
752 nc->skb_count = 0;
753 }
754}
755
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100756static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100757{
758 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
759
760 /* drop skb->head and call any destructors for packet */
761 skb_release_all(skb);
762
763 /* record skb to CPU local list */
764 nc->skb_cache[nc->skb_count++] = skb;
765
766#ifdef CONFIG_SLUB
767 /* SLUB writes into objects when freeing */
768 prefetchw(skb);
769#endif
770
771 /* flush skb_cache if it is filled */
772 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
773 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
774 nc->skb_cache);
775 nc->skb_count = 0;
776 }
777}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100778void __kfree_skb_defer(struct sk_buff *skb)
779{
780 _kfree_skb_defer(skb);
781}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100782
783void napi_consume_skb(struct sk_buff *skb, int budget)
784{
785 if (unlikely(!skb))
786 return;
787
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100788 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100789 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100790 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100791 return;
792 }
793
Paolo Abeni76088942017-06-14 11:48:48 +0200794 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100795 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200796
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100797 /* if reaching here SKB is ready to free */
798 trace_consume_skb(skb);
799
800 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700801 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100802 __kfree_skb(skb);
803 return;
804 }
805
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100806 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100807}
808EXPORT_SYMBOL(napi_consume_skb);
809
Eric Dumazetb1937222014-09-28 22:18:47 -0700810/* Make sure a field is enclosed inside headers_start/headers_end section */
811#define CHECK_SKB_FIELD(field) \
812 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
813 offsetof(struct sk_buff, headers_start)); \
814 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
815 offsetof(struct sk_buff, headers_end)); \
816
Herbert Xudec18812007-10-14 00:37:30 -0700817static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
818{
819 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700820 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700821 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700822 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000823 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700824#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700825 new->sp = secpath_get(old->sp);
826#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700827 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700828
Eric Dumazetb1937222014-09-28 22:18:47 -0700829 /* Note : this field could be in headers_start/headers_end section
830 * It is not yet because we do not want to have a 16 bit hole
831 */
832 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300833
Eric Dumazetb1937222014-09-28 22:18:47 -0700834 memcpy(&new->headers_start, &old->headers_start,
835 offsetof(struct sk_buff, headers_end) -
836 offsetof(struct sk_buff, headers_start));
837 CHECK_SKB_FIELD(protocol);
838 CHECK_SKB_FIELD(csum);
839 CHECK_SKB_FIELD(hash);
840 CHECK_SKB_FIELD(priority);
841 CHECK_SKB_FIELD(skb_iif);
842 CHECK_SKB_FIELD(vlan_proto);
843 CHECK_SKB_FIELD(vlan_tci);
844 CHECK_SKB_FIELD(transport_header);
845 CHECK_SKB_FIELD(network_header);
846 CHECK_SKB_FIELD(mac_header);
847 CHECK_SKB_FIELD(inner_protocol);
848 CHECK_SKB_FIELD(inner_transport_header);
849 CHECK_SKB_FIELD(inner_network_header);
850 CHECK_SKB_FIELD(inner_mac_header);
851 CHECK_SKB_FIELD(mark);
852#ifdef CONFIG_NETWORK_SECMARK
853 CHECK_SKB_FIELD(secmark);
854#endif
Cong Wange0d10952013-08-01 11:10:25 +0800855#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700856 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300857#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800858#ifdef CONFIG_XPS
859 CHECK_SKB_FIELD(sender_cpu);
860#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700861#ifdef CONFIG_NET_SCHED
862 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700863#endif
864
Herbert Xudec18812007-10-14 00:37:30 -0700865}
866
Herbert Xu82c49a32009-05-22 22:11:37 +0000867/*
868 * You should not add any new code to this function. Add it to
869 * __copy_skb_header above instead.
870 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700871static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#define C(x) n->x = skb->x
874
875 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700877 __copy_skb_header(n, skb);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 C(len);
880 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700881 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700882 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800883 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 C(tail);
887 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800888 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000889 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800890 C(data);
891 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300892 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 atomic_inc(&(skb_shinfo(skb)->dataref));
895 skb->cloned = 1;
896
897 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700898#undef C
899}
900
901/**
902 * skb_morph - morph one skb into another
903 * @dst: the skb to receive the contents
904 * @src: the skb to supply the contents
905 *
906 * This is identical to skb_clone except that the target skb is
907 * supplied by the user.
908 *
909 * The target skb is returned upon exit.
910 */
911struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
912{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800913 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700914 return __skb_clone(dst, src);
915}
916EXPORT_SYMBOL_GPL(skb_morph);
917
Ben Hutchings2c530402012-07-10 10:55:09 +0000918/**
919 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000920 * @skb: the skb to modify
921 * @gfp_mask: allocation priority
922 *
923 * This must be called on SKBTX_DEV_ZEROCOPY skb.
924 * It will copy all frags into kernel and drop the reference
925 * to userspace pages.
926 *
927 * If this function is called from an interrupt gfp_mask() must be
928 * %GFP_ATOMIC.
929 *
930 * Returns 0 on success or a negative error code on failure
931 * to allocate kernel memory to copy to.
932 */
933int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000934{
935 int i;
936 int num_frags = skb_shinfo(skb)->nr_frags;
937 struct page *page, *head = NULL;
938 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
939
940 for (i = 0; i < num_frags; i++) {
941 u8 *vaddr;
942 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
943
Krishna Kumar02756ed2012-07-17 02:05:29 +0000944 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000945 if (!page) {
946 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900947 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000948 put_page(head);
949 head = next;
950 }
951 return -ENOMEM;
952 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200953 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000954 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000955 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200956 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900957 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000958 head = page;
959 }
960
961 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000962 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000963 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000964
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000965 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000966
967 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000968 for (i = num_frags - 1; i >= 0; i--) {
969 __skb_fill_page_desc(skb, i, head, 0,
970 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900971 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000972 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000973
974 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000975 return 0;
976}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000977EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000978
Herbert Xue0053ec2007-10-14 00:37:52 -0700979/**
980 * skb_clone - duplicate an sk_buff
981 * @skb: buffer to clone
982 * @gfp_mask: allocation priority
983 *
984 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
985 * copies share the same packet data but not structure. The new
986 * buffer has a reference count of 1. If the allocation fails the
987 * function returns %NULL otherwise the new buffer is returned.
988 *
989 * If this function is called from an interrupt gfp_mask() must be
990 * %GFP_ATOMIC.
991 */
992
993struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
994{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700995 struct sk_buff_fclones *fclones = container_of(skb,
996 struct sk_buff_fclones,
997 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800998 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700999
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001000 if (skb_orphan_frags(skb, gfp_mask))
1001 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001002
Herbert Xue0053ec2007-10-14 00:37:52 -07001003 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001004 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001005 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001006 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001007 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001008 if (skb_pfmemalloc(skb))
1009 gfp_mask |= __GFP_MEMALLOC;
1010
Herbert Xue0053ec2007-10-14 00:37:52 -07001011 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1012 if (!n)
1013 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001014
1015 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001016 n->fclone = SKB_FCLONE_UNAVAILABLE;
1017 }
1018
1019 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001021EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001023static void skb_headers_offset_update(struct sk_buff *skb, int off)
1024{
Eric Dumazet030737b2013-10-19 11:42:54 -07001025 /* Only adjust this if it actually is csum_start rather than csum */
1026 if (skb->ip_summed == CHECKSUM_PARTIAL)
1027 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001028 /* {transport,network,mac}_header and tail are relative to skb->head */
1029 skb->transport_header += off;
1030 skb->network_header += off;
1031 if (skb_mac_header_was_set(skb))
1032 skb->mac_header += off;
1033 skb->inner_transport_header += off;
1034 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001035 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1039{
Herbert Xudec18812007-10-14 00:37:30 -07001040 __copy_skb_header(new, old);
1041
Herbert Xu79671682006-06-22 02:40:14 -07001042 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1043 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1044 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Mel Gormanc93bdd02012-07-31 16:44:19 -07001047static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1048{
1049 if (skb_pfmemalloc(skb))
1050 return SKB_ALLOC_RX;
1051 return 0;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
1055 * skb_copy - create private copy of an sk_buff
1056 * @skb: buffer to copy
1057 * @gfp_mask: allocation priority
1058 *
1059 * Make a copy of both an &sk_buff and its data. This is used when the
1060 * caller wishes to modify the data and needs a private copy of the
1061 * data to alter. Returns %NULL on failure or the pointer to the buffer
1062 * on success. The returned buffer has a reference count of 1.
1063 *
1064 * As by-product this function converts non-linear &sk_buff to linear
1065 * one, so that &sk_buff becomes completely private and caller is allowed
1066 * to modify all the data of returned buffer. This means that this
1067 * function is not recommended for use in circumstances when only
1068 * header is going to be modified. Use pskb_copy() instead.
1069 */
1070
Al Virodd0fc662005-10-07 07:46:04 +01001071struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001073 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001074 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001075 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1076 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!n)
1079 return NULL;
1080
1081 /* Set the data pointer */
1082 skb_reserve(n, headerlen);
1083 /* Set the tail pointer and length */
1084 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1087 BUG();
1088
1089 copy_skb_header(n, skb);
1090 return n;
1091}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001092EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001095 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001097 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001099 * @fclone: if true allocate the copy of the skb from the fclone
1100 * cache instead of the head cache; it is recommended to set this
1101 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 *
1103 * Make a copy of both an &sk_buff and part of its data, located
1104 * in header. Fragmented data remain shared. This is used when
1105 * the caller wishes to modify only header of &sk_buff and needs
1106 * private copy of the header to alter. Returns %NULL on failure
1107 * or the pointer to the buffer on success.
1108 * The returned buffer has a reference count of 1.
1109 */
1110
Octavian Purdilabad93e92014-06-12 01:36:26 +03001111struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1112 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Eric Dumazet117632e2011-12-03 21:39:53 +00001114 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001115 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1116 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!n)
1119 goto out;
1120
1121 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001122 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Set the tail pointer and length */
1124 skb_put(n, skb_headlen(skb));
1125 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001126 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Herbert Xu25f484a2006-11-07 14:57:15 -08001128 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 n->data_len = skb->data_len;
1130 n->len = skb->len;
1131
1132 if (skb_shinfo(skb)->nr_frags) {
1133 int i;
1134
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001135 if (skb_orphan_frags(skb, gfp_mask)) {
1136 kfree_skb(n);
1137 n = NULL;
1138 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1141 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001142 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 skb_shinfo(n)->nr_frags = i;
1145 }
1146
David S. Miller21dc3302010-08-23 00:13:46 -07001147 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1149 skb_clone_fraglist(n);
1150 }
1151
1152 copy_skb_header(n, skb);
1153out:
1154 return n;
1155}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001156EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158/**
1159 * pskb_expand_head - reallocate header of &sk_buff
1160 * @skb: buffer to reallocate
1161 * @nhead: room to add at head
1162 * @ntail: room to add at tail
1163 * @gfp_mask: allocation priority
1164 *
Mathias Krausebc323832013-11-07 14:18:26 +01001165 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1166 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * reference count of 1. Returns zero in the case of success or error,
1168 * if expansion failed. In the last case, &sk_buff is not changed.
1169 *
1170 * All the pointers pointing into skb header may change and must be
1171 * reloaded after call to this function.
1172 */
1173
Victor Fusco86a76ca2005-07-08 14:57:47 -07001174int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001175 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Eric Dumazet158f3232017-01-27 07:11:27 -08001177 int i, osize = skb_end_offset(skb);
1178 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001180 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Herbert Xu4edd87a2008-10-01 07:09:38 -07001182 BUG_ON(nhead < 0);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (skb_shared(skb))
1185 BUG();
1186
1187 size = SKB_DATA_ALIGN(size);
1188
Mel Gormanc93bdd02012-07-31 16:44:19 -07001189 if (skb_pfmemalloc(skb))
1190 gfp_mask |= __GFP_MEMALLOC;
1191 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1192 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!data)
1194 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001195 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001198 * optimized for the cases when header is void.
1199 */
1200 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1201
1202 memcpy((struct skb_shared_info *)(data + size),
1203 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001204 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Alexander Duyck3e245912012-05-04 14:26:51 +00001206 /*
1207 * if shinfo is shared we must drop the old head gracefully, but if it
1208 * is not we can just drop the old head and let the existing refcount
1209 * be since all we did is relocate the values
1210 */
1211 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001212 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001213 if (skb_orphan_frags(skb, gfp_mask))
1214 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001215 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001216 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric Dumazet1fd63042010-09-02 23:09:32 +00001218 if (skb_has_frag_list(skb))
1219 skb_clone_fraglist(skb);
1220
1221 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001222 } else {
1223 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 off = (data + nhead) - skb->head;
1226
1227 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001228 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001230#ifdef NET_SKBUFF_DATA_USES_OFFSET
1231 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001232 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001233#else
1234 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001235#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001236 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001237 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001239 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 skb->nohdr = 0;
1241 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001242
1243 /* It is not generally safe to change skb->truesize.
1244 * For the moment, we really care of rx path, or
1245 * when skb is orphaned (not attached to a socket).
1246 */
1247 if (!skb->sk || skb->destructor == sock_edemux)
1248 skb->truesize += size - osize;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251
Shirley Maa6686f22011-07-06 12:22:12 +00001252nofrags:
1253 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254nodata:
1255 return -ENOMEM;
1256}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001257EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259/* Make private copy of skb with writable head and some headroom */
1260
1261struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1262{
1263 struct sk_buff *skb2;
1264 int delta = headroom - skb_headroom(skb);
1265
1266 if (delta <= 0)
1267 skb2 = pskb_copy(skb, GFP_ATOMIC);
1268 else {
1269 skb2 = skb_clone(skb, GFP_ATOMIC);
1270 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1271 GFP_ATOMIC)) {
1272 kfree_skb(skb2);
1273 skb2 = NULL;
1274 }
1275 }
1276 return skb2;
1277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001278EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280/**
1281 * skb_copy_expand - copy and expand sk_buff
1282 * @skb: buffer to copy
1283 * @newheadroom: new free bytes at head
1284 * @newtailroom: new free bytes at tail
1285 * @gfp_mask: allocation priority
1286 *
1287 * Make a copy of both an &sk_buff and its data and while doing so
1288 * allocate additional space.
1289 *
1290 * This is used when the caller wishes to modify the data and needs a
1291 * private copy of the data to alter as well as more space for new fields.
1292 * Returns %NULL on failure or the pointer to the buffer
1293 * on success. The returned buffer has a reference count of 1.
1294 *
1295 * You must pass %GFP_ATOMIC as the allocation priority if this function
1296 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001299 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001300 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 /*
1303 * Allocate the copy buffer
1304 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001305 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1306 gfp_mask, skb_alloc_rx_flag(skb),
1307 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001308 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int head_copy_len, head_copy_off;
1310
1311 if (!n)
1312 return NULL;
1313
1314 skb_reserve(n, newheadroom);
1315
1316 /* Set the tail pointer and length */
1317 skb_put(n, skb->len);
1318
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001319 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 head_copy_off = 0;
1321 if (newheadroom <= head_copy_len)
1322 head_copy_len = newheadroom;
1323 else
1324 head_copy_off = newheadroom - head_copy_len;
1325
1326 /* Copy the linear header and data. */
1327 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1328 skb->len + head_copy_len))
1329 BUG();
1330
1331 copy_skb_header(n, skb);
1332
Eric Dumazet030737b2013-10-19 11:42:54 -07001333 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return n;
1336}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001337EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/**
1340 * skb_pad - zero pad the tail of an skb
1341 * @skb: buffer to pad
1342 * @pad: space to pad
1343 *
1344 * Ensure that a buffer is followed by a padding area that is zero
1345 * filled. Used by network drivers which may DMA or transfer data
1346 * beyond the buffer end onto the wire.
1347 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001348 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001350
Herbert Xu5b057c62006-06-23 02:06:41 -07001351int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Herbert Xu5b057c62006-06-23 02:06:41 -07001353 int err;
1354 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001357 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001361
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001362 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001363 if (likely(skb_cloned(skb) || ntail > 0)) {
1364 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1365 if (unlikely(err))
1366 goto free_skb;
1367 }
1368
1369 /* FIXME: The use of this function with non-linear skb's really needs
1370 * to be audited.
1371 */
1372 err = skb_linearize(skb);
1373 if (unlikely(err))
1374 goto free_skb;
1375
1376 memset(skb->data + skb->len, 0, pad);
1377 return 0;
1378
1379free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001381 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001382}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001383EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001384
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001385/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001386 * pskb_put - add data to the tail of a potentially fragmented buffer
1387 * @skb: start of the buffer to use
1388 * @tail: tail fragment of the buffer to use
1389 * @len: amount of data to add
1390 *
1391 * This function extends the used data area of the potentially
1392 * fragmented buffer. @tail must be the last fragment of @skb -- or
1393 * @skb itself. If this would exceed the total buffer size the kernel
1394 * will panic. A pointer to the first byte of the extra data is
1395 * returned.
1396 */
1397
Johannes Berg4df864c2017-06-16 14:29:21 +02001398void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001399{
1400 if (tail != skb) {
1401 skb->data_len += len;
1402 skb->len += len;
1403 }
1404 return skb_put(tail, len);
1405}
1406EXPORT_SYMBOL_GPL(pskb_put);
1407
1408/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001409 * skb_put - add data to a buffer
1410 * @skb: buffer to use
1411 * @len: amount of data to add
1412 *
1413 * This function extends the used data area of the buffer. If this would
1414 * exceed the total buffer size the kernel will panic. A pointer to the
1415 * first byte of the extra data is returned.
1416 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001417void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001418{
Johannes Berg4df864c2017-06-16 14:29:21 +02001419 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001420 SKB_LINEAR_ASSERT(skb);
1421 skb->tail += len;
1422 skb->len += len;
1423 if (unlikely(skb->tail > skb->end))
1424 skb_over_panic(skb, len, __builtin_return_address(0));
1425 return tmp;
1426}
1427EXPORT_SYMBOL(skb_put);
1428
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001429/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001430 * skb_push - add data to the start of a buffer
1431 * @skb: buffer to use
1432 * @len: amount of data to add
1433 *
1434 * This function extends the used data area of the buffer at the buffer
1435 * start. If this would exceed the total buffer headroom the kernel will
1436 * panic. A pointer to the first byte of the extra data is returned.
1437 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001438void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001439{
1440 skb->data -= len;
1441 skb->len += len;
1442 if (unlikely(skb->data<skb->head))
1443 skb_under_panic(skb, len, __builtin_return_address(0));
1444 return skb->data;
1445}
1446EXPORT_SYMBOL(skb_push);
1447
1448/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001449 * skb_pull - remove data from the start of a buffer
1450 * @skb: buffer to use
1451 * @len: amount of data to remove
1452 *
1453 * This function removes data from the start of a buffer, returning
1454 * the memory to the headroom. A pointer to the next data in the buffer
1455 * is returned. Once the data has been pulled future pushes will overwrite
1456 * the old data.
1457 */
Johannes Bergaf728682017-06-16 14:29:22 +02001458void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001459{
David S. Miller47d29642010-05-02 02:21:44 -07001460 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001461}
1462EXPORT_SYMBOL(skb_pull);
1463
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001464/**
1465 * skb_trim - remove end from a buffer
1466 * @skb: buffer to alter
1467 * @len: new length
1468 *
1469 * Cut the length of a buffer down by removing data from the tail. If
1470 * the buffer is already under the length specified it is not modified.
1471 * The skb must be linear.
1472 */
1473void skb_trim(struct sk_buff *skb, unsigned int len)
1474{
1475 if (skb->len > len)
1476 __skb_trim(skb, len);
1477}
1478EXPORT_SYMBOL(skb_trim);
1479
Herbert Xu3cc0e872006-06-09 16:13:38 -07001480/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 */
1482
Herbert Xu3cc0e872006-06-09 16:13:38 -07001483int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Herbert Xu27b437c2006-07-13 19:26:39 -07001485 struct sk_buff **fragp;
1486 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int offset = skb_headlen(skb);
1488 int nfrags = skb_shinfo(skb)->nr_frags;
1489 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001490 int err;
1491
1492 if (skb_cloned(skb) &&
1493 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001496 i = 0;
1497 if (offset >= len)
1498 goto drop_pages;
1499
1500 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001501 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001502
1503 if (end < len) {
1504 offset = end;
1505 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001507
Eric Dumazet9e903e02011-10-18 21:00:24 +00001508 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001509
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001510drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001511 skb_shinfo(skb)->nr_frags = i;
1512
1513 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001514 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001515
David S. Miller21dc3302010-08-23 00:13:46 -07001516 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001517 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001518 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
Herbert Xu27b437c2006-07-13 19:26:39 -07001521 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1522 fragp = &frag->next) {
1523 int end = offset + frag->len;
1524
1525 if (skb_shared(frag)) {
1526 struct sk_buff *nfrag;
1527
1528 nfrag = skb_clone(frag, GFP_ATOMIC);
1529 if (unlikely(!nfrag))
1530 return -ENOMEM;
1531
1532 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001533 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001534 frag = nfrag;
1535 *fragp = frag;
1536 }
1537
1538 if (end < len) {
1539 offset = end;
1540 continue;
1541 }
1542
1543 if (end > len &&
1544 unlikely((err = pskb_trim(frag, len - offset))))
1545 return err;
1546
1547 if (frag->next)
1548 skb_drop_list(&frag->next);
1549 break;
1550 }
1551
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001552done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001553 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 skb->data_len -= skb->len - len;
1555 skb->len = len;
1556 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001557 skb->len = len;
1558 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001559 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001562 if (!skb->sk || skb->destructor == sock_edemux)
1563 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001566EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/**
1569 * __pskb_pull_tail - advance tail of skb header
1570 * @skb: buffer to reallocate
1571 * @delta: number of bytes to advance tail
1572 *
1573 * The function makes a sense only on a fragmented &sk_buff,
1574 * it expands header moving its tail forward and copying necessary
1575 * data from fragmented part.
1576 *
1577 * &sk_buff MUST have reference count of 1.
1578 *
1579 * Returns %NULL (and &sk_buff does not change) if pull failed
1580 * or value of new tail of skb in the case of success.
1581 *
1582 * All the pointers pointing into skb header may change and must be
1583 * reloaded after call to this function.
1584 */
1585
1586/* Moves tail of skb head forward, copying data from fragmented part,
1587 * when it is necessary.
1588 * 1. It may fail due to malloc failure.
1589 * 2. It may change skb pointers.
1590 *
1591 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1592 */
Johannes Bergaf728682017-06-16 14:29:22 +02001593void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 /* If skb has not enough free space at tail, get new one
1596 * plus 128 bytes for future expansions. If we have enough
1597 * room at tail, reallocate without expansion only if skb is cloned.
1598 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001599 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (eat > 0 || skb_cloned(skb)) {
1602 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1603 GFP_ATOMIC))
1604 return NULL;
1605 }
1606
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001607 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 BUG();
1609
1610 /* Optimization: no fragments, no reasons to preestimate
1611 * size of pulled pages. Superb.
1612 */
David S. Miller21dc3302010-08-23 00:13:46 -07001613 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto pull_pages;
1615
1616 /* Estimate size of pulled pages. */
1617 eat = delta;
1618 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001619 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1620
1621 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001623 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /* If we need update frag list, we are in troubles.
1627 * Certainly, it possible to add an offset to skb data,
1628 * but taking into account that pulling is expected to
1629 * be very rare operation, it is worth to fight against
1630 * further bloating skb head and crucify ourselves here instead.
1631 * Pure masohism, indeed. 8)8)
1632 */
1633 if (eat) {
1634 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1635 struct sk_buff *clone = NULL;
1636 struct sk_buff *insp = NULL;
1637
1638 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001639 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (list->len <= eat) {
1642 /* Eaten as whole. */
1643 eat -= list->len;
1644 list = list->next;
1645 insp = list;
1646 } else {
1647 /* Eaten partially. */
1648
1649 if (skb_shared(list)) {
1650 /* Sucks! We need to fork list. :-( */
1651 clone = skb_clone(list, GFP_ATOMIC);
1652 if (!clone)
1653 return NULL;
1654 insp = list->next;
1655 list = clone;
1656 } else {
1657 /* This may be pulled without
1658 * problems. */
1659 insp = list;
1660 }
1661 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001662 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return NULL;
1664 }
1665 break;
1666 }
1667 } while (eat);
1668
1669 /* Free pulled out fragments. */
1670 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1671 skb_shinfo(skb)->frag_list = list->next;
1672 kfree_skb(list);
1673 }
1674 /* And insert new clone at head. */
1675 if (clone) {
1676 clone->next = list;
1677 skb_shinfo(skb)->frag_list = clone;
1678 }
1679 }
1680 /* Success! Now we may commit changes to skb data. */
1681
1682pull_pages:
1683 eat = delta;
1684 k = 0;
1685 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001686 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1687
1688 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001689 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001690 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else {
1692 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1693 if (eat) {
1694 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001695 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001696 if (!i)
1697 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 eat = 0;
1699 }
1700 k++;
1701 }
1702 }
1703 skb_shinfo(skb)->nr_frags = k;
1704
linzhang3ccc6c62017-07-17 17:25:02 +08001705end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 skb->tail += delta;
1707 skb->data_len -= delta;
1708
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001709 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001711EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Eric Dumazet22019b12011-07-29 18:37:31 +00001713/**
1714 * skb_copy_bits - copy bits from skb to kernel buffer
1715 * @skb: source skb
1716 * @offset: offset in source
1717 * @to: destination buffer
1718 * @len: number of bytes to copy
1719 *
1720 * Copy the specified number of bytes from the source skb to the
1721 * destination buffer.
1722 *
1723 * CAUTION ! :
1724 * If its prototype is ever changed,
1725 * check arch/{*}/net/{*}.S files,
1726 * since it is called from BPF assembly code.
1727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1729{
David S. Miller1a028e52007-04-27 15:21:23 -07001730 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001731 struct sk_buff *frag_iter;
1732 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 if (offset > (int)skb->len - len)
1735 goto fault;
1736
1737 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001738 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (copy > len)
1740 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001741 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if ((len -= copy) == 0)
1743 return 0;
1744 offset += copy;
1745 to += copy;
1746 }
1747
1748 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001749 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001750 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001752 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001753
Eric Dumazet51c56b02012-04-05 11:35:15 +02001754 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if ((copy = end - offset) > 0) {
1756 u8 *vaddr;
1757
1758 if (copy > len)
1759 copy = len;
1760
Eric Dumazet51c56b02012-04-05 11:35:15 +02001761 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001763 vaddr + f->page_offset + offset - start,
1764 copy);
1765 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if ((len -= copy) == 0)
1768 return 0;
1769 offset += copy;
1770 to += copy;
1771 }
David S. Miller1a028e52007-04-27 15:21:23 -07001772 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
David S. Millerfbb398a2009-06-09 00:18:59 -07001775 skb_walk_frags(skb, frag_iter) {
1776 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David S. Millerfbb398a2009-06-09 00:18:59 -07001778 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
David S. Millerfbb398a2009-06-09 00:18:59 -07001780 end = start + frag_iter->len;
1781 if ((copy = end - offset) > 0) {
1782 if (copy > len)
1783 copy = len;
1784 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1785 goto fault;
1786 if ((len -= copy) == 0)
1787 return 0;
1788 offset += copy;
1789 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001791 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Shirley Maa6686f22011-07-06 12:22:12 +00001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (!len)
1795 return 0;
1796
1797fault:
1798 return -EFAULT;
1799}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001800EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Jens Axboe9c55e012007-11-06 23:30:13 -08001802/*
1803 * Callback from splice_to_pipe(), if we need to release some pages
1804 * at the end of the spd in case we error'ed out in filling the pipe.
1805 */
1806static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1807{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001808 put_page(spd->pages[i]);
1809}
Jens Axboe9c55e012007-11-06 23:30:13 -08001810
David S. Millera108d5f2012-04-23 23:06:11 -04001811static struct page *linear_to_page(struct page *page, unsigned int *len,
1812 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001813 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001814{
Eric Dumazet5640f762012-09-23 23:04:42 +00001815 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001816
Eric Dumazet5640f762012-09-23 23:04:42 +00001817 if (!sk_page_frag_refill(sk, pfrag))
1818 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001819
Eric Dumazet5640f762012-09-23 23:04:42 +00001820 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001821
Eric Dumazet5640f762012-09-23 23:04:42 +00001822 memcpy(page_address(pfrag->page) + pfrag->offset,
1823 page_address(page) + *offset, *len);
1824 *offset = pfrag->offset;
1825 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001826
Eric Dumazet5640f762012-09-23 23:04:42 +00001827 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001828}
1829
Eric Dumazet41c73a02012-04-22 12:26:16 +00001830static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1831 struct page *page,
1832 unsigned int offset)
1833{
1834 return spd->nr_pages &&
1835 spd->pages[spd->nr_pages - 1] == page &&
1836 (spd->partial[spd->nr_pages - 1].offset +
1837 spd->partial[spd->nr_pages - 1].len == offset);
1838}
1839
Jens Axboe9c55e012007-11-06 23:30:13 -08001840/*
1841 * Fill page/offset/length into spd, if it can hold more pages.
1842 */
David S. Millera108d5f2012-04-23 23:06:11 -04001843static bool spd_fill_page(struct splice_pipe_desc *spd,
1844 struct pipe_inode_info *pipe, struct page *page,
1845 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001846 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001847 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001848{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001849 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001850 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001851
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001852 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001853 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001854 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001855 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001856 }
1857 if (spd_can_coalesce(spd, page, offset)) {
1858 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001859 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001860 }
1861 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001862 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001863 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001864 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001865 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001866
David S. Millera108d5f2012-04-23 23:06:11 -04001867 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001868}
1869
David S. Millera108d5f2012-04-23 23:06:11 -04001870static bool __splice_segment(struct page *page, unsigned int poff,
1871 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001872 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001873 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001874 struct sock *sk,
1875 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001876{
1877 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001878 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001879
1880 /* skip this segment if already processed */
1881 if (*off >= plen) {
1882 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001883 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001884 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001885
Octavian Purdila2870c432008-07-15 00:49:11 -07001886 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001887 poff += *off;
1888 plen -= *off;
1889 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001890
Eric Dumazet18aafc62013-01-11 14:46:37 +00001891 do {
1892 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001893
Eric Dumazet18aafc62013-01-11 14:46:37 +00001894 if (spd_fill_page(spd, pipe, page, &flen, poff,
1895 linear, sk))
1896 return true;
1897 poff += flen;
1898 plen -= flen;
1899 *len -= flen;
1900 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001901
David S. Millera108d5f2012-04-23 23:06:11 -04001902 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001903}
1904
1905/*
David S. Millera108d5f2012-04-23 23:06:11 -04001906 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001907 * pipe is full or if we already spliced the requested length.
1908 */
David S. Millera108d5f2012-04-23 23:06:11 -04001909static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1910 unsigned int *offset, unsigned int *len,
1911 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001912{
1913 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001914 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001915
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001916 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001917 * If skb->head_frag is set, this 'linear' part is backed by a
1918 * fragment, and if the head is not shared with any clones then
1919 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001920 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001921 if (__splice_segment(virt_to_page(skb->data),
1922 (unsigned long) skb->data & (PAGE_SIZE - 1),
1923 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001924 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001925 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001926 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001927 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001928
1929 /*
1930 * then map the fragments
1931 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001932 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1933 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1934
Ian Campbellea2ab692011-08-22 23:44:58 +00001935 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001936 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001937 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001938 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 }
1940
Tom Herbertfa9835e2016-03-07 14:11:04 -08001941 skb_walk_frags(skb, iter) {
1942 if (*offset >= iter->len) {
1943 *offset -= iter->len;
1944 continue;
1945 }
1946 /* __skb_splice_bits() only fails if the output has no room
1947 * left, so no point in going over the frag_list for the error
1948 * case.
1949 */
1950 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1951 return true;
1952 }
1953
David S. Millera108d5f2012-04-23 23:06:11 -04001954 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001955}
1956
1957/*
1958 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001959 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001960 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001961int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001962 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001963 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001964{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001965 struct partial_page partial[MAX_SKB_FRAGS];
1966 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001967 struct splice_pipe_desc spd = {
1968 .pages = pages,
1969 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001970 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001971 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001972 .spd_release = sock_spd_release,
1973 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001974 int ret = 0;
1975
Tom Herbertfa9835e2016-03-07 14:11:04 -08001976 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001977
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001978 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04001979 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001980
Jens Axboe35f3d142010-05-20 10:43:18 +02001981 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001982}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001983EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001984
Tom Herbert20bf50d2017-07-28 16:22:42 -07001985/* Send skb data on a socket. Socket must be locked. */
1986int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
1987 int len)
1988{
1989 unsigned int orig_len = len;
1990 struct sk_buff *head = skb;
1991 unsigned short fragidx;
1992 int slen, ret;
1993
1994do_frag_list:
1995
1996 /* Deal with head data */
1997 while (offset < skb_headlen(skb) && len) {
1998 struct kvec kv;
1999 struct msghdr msg;
2000
2001 slen = min_t(int, len, skb_headlen(skb) - offset);
2002 kv.iov_base = skb->data + offset;
2003 kv.iov_len = len;
2004 memset(&msg, 0, sizeof(msg));
2005
2006 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2007 if (ret <= 0)
2008 goto error;
2009
2010 offset += ret;
2011 len -= ret;
2012 }
2013
2014 /* All the data was skb head? */
2015 if (!len)
2016 goto out;
2017
2018 /* Make offset relative to start of frags */
2019 offset -= skb_headlen(skb);
2020
2021 /* Find where we are in frag list */
2022 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2023 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2024
2025 if (offset < frag->size)
2026 break;
2027
2028 offset -= frag->size;
2029 }
2030
2031 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2032 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2033
2034 slen = min_t(size_t, len, frag->size - offset);
2035
2036 while (slen) {
2037 ret = kernel_sendpage_locked(sk, frag->page.p,
2038 frag->page_offset + offset,
2039 slen, MSG_DONTWAIT);
2040 if (ret <= 0)
2041 goto error;
2042
2043 len -= ret;
2044 offset += ret;
2045 slen -= ret;
2046 }
2047
2048 offset = 0;
2049 }
2050
2051 if (len) {
2052 /* Process any frag lists */
2053
2054 if (skb == head) {
2055 if (skb_has_frag_list(skb)) {
2056 skb = skb_shinfo(skb)->frag_list;
2057 goto do_frag_list;
2058 }
2059 } else if (skb->next) {
2060 skb = skb->next;
2061 goto do_frag_list;
2062 }
2063 }
2064
2065out:
2066 return orig_len - len;
2067
2068error:
2069 return orig_len == len ? ret : orig_len - len;
2070}
2071EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2072
2073/* Send skb data on a socket. */
2074int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2075{
2076 int ret = 0;
2077
2078 lock_sock(sk);
2079 ret = skb_send_sock_locked(sk, skb, offset, len);
2080 release_sock(sk);
2081
2082 return ret;
2083}
2084EXPORT_SYMBOL_GPL(skb_send_sock);
2085
Herbert Xu357b40a2005-04-19 22:30:14 -07002086/**
2087 * skb_store_bits - store bits from kernel buffer to skb
2088 * @skb: destination buffer
2089 * @offset: offset in destination
2090 * @from: source buffer
2091 * @len: number of bytes to copy
2092 *
2093 * Copy the specified number of bytes from the source buffer to the
2094 * destination skb. This function handles all the messy bits of
2095 * traversing fragment lists and such.
2096 */
2097
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002098int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002099{
David S. Miller1a028e52007-04-27 15:21:23 -07002100 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002101 struct sk_buff *frag_iter;
2102 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002103
2104 if (offset > (int)skb->len - len)
2105 goto fault;
2106
David S. Miller1a028e52007-04-27 15:21:23 -07002107 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002108 if (copy > len)
2109 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002110 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002111 if ((len -= copy) == 0)
2112 return 0;
2113 offset += copy;
2114 from += copy;
2115 }
2116
2117 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2118 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002119 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002120
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002121 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002122
Eric Dumazet9e903e02011-10-18 21:00:24 +00002123 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002124 if ((copy = end - offset) > 0) {
2125 u8 *vaddr;
2126
2127 if (copy > len)
2128 copy = len;
2129
Eric Dumazet51c56b02012-04-05 11:35:15 +02002130 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002131 memcpy(vaddr + frag->page_offset + offset - start,
2132 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002133 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002134
2135 if ((len -= copy) == 0)
2136 return 0;
2137 offset += copy;
2138 from += copy;
2139 }
David S. Miller1a028e52007-04-27 15:21:23 -07002140 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002141 }
2142
David S. Millerfbb398a2009-06-09 00:18:59 -07002143 skb_walk_frags(skb, frag_iter) {
2144 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002145
David S. Millerfbb398a2009-06-09 00:18:59 -07002146 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002147
David S. Millerfbb398a2009-06-09 00:18:59 -07002148 end = start + frag_iter->len;
2149 if ((copy = end - offset) > 0) {
2150 if (copy > len)
2151 copy = len;
2152 if (skb_store_bits(frag_iter, offset - start,
2153 from, copy))
2154 goto fault;
2155 if ((len -= copy) == 0)
2156 return 0;
2157 offset += copy;
2158 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002159 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002160 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002161 }
2162 if (!len)
2163 return 0;
2164
2165fault:
2166 return -EFAULT;
2167}
Herbert Xu357b40a2005-04-19 22:30:14 -07002168EXPORT_SYMBOL(skb_store_bits);
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002171__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2172 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
David S. Miller1a028e52007-04-27 15:21:23 -07002174 int start = skb_headlen(skb);
2175 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002176 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 int pos = 0;
2178
2179 /* Checksum header. */
2180 if (copy > 0) {
2181 if (copy > len)
2182 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002183 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if ((len -= copy) == 0)
2185 return csum;
2186 offset += copy;
2187 pos = copy;
2188 }
2189
2190 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002191 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002192 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002194 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002195
Eric Dumazet51c56b02012-04-05 11:35:15 +02002196 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002198 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 if (copy > len)
2202 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002203 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002204 csum2 = ops->update(vaddr + frag->page_offset +
2205 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002206 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002207 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!(len -= copy))
2209 return csum;
2210 offset += copy;
2211 pos += copy;
2212 }
David S. Miller1a028e52007-04-27 15:21:23 -07002213 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
David S. Millerfbb398a2009-06-09 00:18:59 -07002216 skb_walk_frags(skb, frag_iter) {
2217 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
David S. Millerfbb398a2009-06-09 00:18:59 -07002219 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
David S. Millerfbb398a2009-06-09 00:18:59 -07002221 end = start + frag_iter->len;
2222 if ((copy = end - offset) > 0) {
2223 __wsum csum2;
2224 if (copy > len)
2225 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002226 csum2 = __skb_checksum(frag_iter, offset - start,
2227 copy, 0, ops);
2228 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002229 if ((len -= copy) == 0)
2230 return csum;
2231 offset += copy;
2232 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002234 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002236 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 return csum;
2239}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002240EXPORT_SYMBOL(__skb_checksum);
2241
2242__wsum skb_checksum(const struct sk_buff *skb, int offset,
2243 int len, __wsum csum)
2244{
2245 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002246 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002247 .combine = csum_block_add_ext,
2248 };
2249
2250 return __skb_checksum(skb, offset, len, csum, &ops);
2251}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002252EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254/* Both of above in one bottle. */
2255
Al Viro81d77662006-11-14 21:37:33 -08002256__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2257 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
David S. Miller1a028e52007-04-27 15:21:23 -07002259 int start = skb_headlen(skb);
2260 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002261 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int pos = 0;
2263
2264 /* Copy header. */
2265 if (copy > 0) {
2266 if (copy > len)
2267 copy = len;
2268 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2269 copy, csum);
2270 if ((len -= copy) == 0)
2271 return csum;
2272 offset += copy;
2273 to += copy;
2274 pos = copy;
2275 }
2276
2277 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002278 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002280 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002281
Eric Dumazet9e903e02011-10-18 21:00:24 +00002282 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002284 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 u8 *vaddr;
2286 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2287
2288 if (copy > len)
2289 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002290 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002292 frag->page_offset +
2293 offset - start, to,
2294 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002295 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 csum = csum_block_add(csum, csum2, pos);
2297 if (!(len -= copy))
2298 return csum;
2299 offset += copy;
2300 to += copy;
2301 pos += copy;
2302 }
David S. Miller1a028e52007-04-27 15:21:23 -07002303 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
David S. Millerfbb398a2009-06-09 00:18:59 -07002306 skb_walk_frags(skb, frag_iter) {
2307 __wsum csum2;
2308 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
David S. Millerfbb398a2009-06-09 00:18:59 -07002310 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
David S. Millerfbb398a2009-06-09 00:18:59 -07002312 end = start + frag_iter->len;
2313 if ((copy = end - offset) > 0) {
2314 if (copy > len)
2315 copy = len;
2316 csum2 = skb_copy_and_csum_bits(frag_iter,
2317 offset - start,
2318 to, copy, 0);
2319 csum = csum_block_add(csum, csum2, pos);
2320 if ((len -= copy) == 0)
2321 return csum;
2322 offset += copy;
2323 to += copy;
2324 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002326 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002328 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return csum;
2330}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002331EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Davide Caratti96178132017-05-18 15:44:37 +02002333static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2334{
2335 net_warn_ratelimited(
2336 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2337 __func__);
2338 return 0;
2339}
2340
2341static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2342 int offset, int len)
2343{
2344 net_warn_ratelimited(
2345 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2346 __func__);
2347 return 0;
2348}
2349
2350static const struct skb_checksum_ops default_crc32c_ops = {
2351 .update = warn_crc32c_csum_update,
2352 .combine = warn_crc32c_csum_combine,
2353};
2354
2355const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2356 &default_crc32c_ops;
2357EXPORT_SYMBOL(crc32c_csum_stub);
2358
Thomas Grafaf2806f2013-12-13 15:22:17 +01002359 /**
2360 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2361 * @from: source buffer
2362 *
2363 * Calculates the amount of linear headroom needed in the 'to' skb passed
2364 * into skb_zerocopy().
2365 */
2366unsigned int
2367skb_zerocopy_headlen(const struct sk_buff *from)
2368{
2369 unsigned int hlen = 0;
2370
2371 if (!from->head_frag ||
2372 skb_headlen(from) < L1_CACHE_BYTES ||
2373 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2374 hlen = skb_headlen(from);
2375
2376 if (skb_has_frag_list(from))
2377 hlen = from->len;
2378
2379 return hlen;
2380}
2381EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2382
2383/**
2384 * skb_zerocopy - Zero copy skb to skb
2385 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002386 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002387 * @len: number of bytes to copy from source buffer
2388 * @hlen: size of linear headroom in destination buffer
2389 *
2390 * Copies up to `len` bytes from `from` to `to` by creating references
2391 * to the frags in the source buffer.
2392 *
2393 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2394 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002395 *
2396 * Return value:
2397 * 0: everything is OK
2398 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2399 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002400 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002401int
2402skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002403{
2404 int i, j = 0;
2405 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002406 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002407 struct page *page;
2408 unsigned int offset;
2409
2410 BUG_ON(!from->head_frag && !hlen);
2411
2412 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002413 if (len <= skb_tailroom(to))
2414 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002415
2416 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002417 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2418 if (unlikely(ret))
2419 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002420 len -= hlen;
2421 } else {
2422 plen = min_t(int, skb_headlen(from), len);
2423 if (plen) {
2424 page = virt_to_head_page(from->head);
2425 offset = from->data - (unsigned char *)page_address(page);
2426 __skb_fill_page_desc(to, 0, page, offset, plen);
2427 get_page(page);
2428 j = 1;
2429 len -= plen;
2430 }
2431 }
2432
2433 to->truesize += len + plen;
2434 to->len += len + plen;
2435 to->data_len += len + plen;
2436
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002437 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2438 skb_tx_error(from);
2439 return -ENOMEM;
2440 }
2441
Thomas Grafaf2806f2013-12-13 15:22:17 +01002442 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2443 if (!len)
2444 break;
2445 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2446 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2447 len -= skb_shinfo(to)->frags[j].size;
2448 skb_frag_ref(to, j);
2449 j++;
2450 }
2451 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002452
2453 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002454}
2455EXPORT_SYMBOL_GPL(skb_zerocopy);
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2458{
Al Virod3bc23e2006-11-14 21:24:49 -08002459 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 long csstart;
2461
Patrick McHardy84fa7932006-08-29 16:44:56 -07002462 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002463 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 else
2465 csstart = skb_headlen(skb);
2466
Kris Katterjohn09a62662006-01-08 22:24:28 -08002467 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002469 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 csum = 0;
2472 if (csstart != skb->len)
2473 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2474 skb->len - csstart, 0);
2475
Patrick McHardy84fa7932006-08-29 16:44:56 -07002476 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002477 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Al Virod3bc23e2006-11-14 21:24:49 -08002479 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002482EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484/**
2485 * skb_dequeue - remove from the head of the queue
2486 * @list: list to dequeue from
2487 *
2488 * Remove the head of the list. The list lock is taken so the function
2489 * may be used safely with other locking list functions. The head item is
2490 * returned or %NULL if the list is empty.
2491 */
2492
2493struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2494{
2495 unsigned long flags;
2496 struct sk_buff *result;
2497
2498 spin_lock_irqsave(&list->lock, flags);
2499 result = __skb_dequeue(list);
2500 spin_unlock_irqrestore(&list->lock, flags);
2501 return result;
2502}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002503EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505/**
2506 * skb_dequeue_tail - remove from the tail of the queue
2507 * @list: list to dequeue from
2508 *
2509 * Remove the tail of the list. The list lock is taken so the function
2510 * may be used safely with other locking list functions. The tail item is
2511 * returned or %NULL if the list is empty.
2512 */
2513struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2514{
2515 unsigned long flags;
2516 struct sk_buff *result;
2517
2518 spin_lock_irqsave(&list->lock, flags);
2519 result = __skb_dequeue_tail(list);
2520 spin_unlock_irqrestore(&list->lock, flags);
2521 return result;
2522}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002523EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525/**
2526 * skb_queue_purge - empty a list
2527 * @list: list to empty
2528 *
2529 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2530 * the list and one reference dropped. This function takes the list
2531 * lock and is atomic with respect to other list locking functions.
2532 */
2533void skb_queue_purge(struct sk_buff_head *list)
2534{
2535 struct sk_buff *skb;
2536 while ((skb = skb_dequeue(list)) != NULL)
2537 kfree_skb(skb);
2538}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002539EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002542 * skb_rbtree_purge - empty a skb rbtree
2543 * @root: root of the rbtree to empty
2544 *
2545 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2546 * the list and one reference dropped. This function does not take
2547 * any lock. Synchronization should be handled by the caller (e.g., TCP
2548 * out-of-order queue is protected by the socket lock).
2549 */
2550void skb_rbtree_purge(struct rb_root *root)
2551{
2552 struct sk_buff *skb, *next;
2553
2554 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2555 kfree_skb(skb);
2556
2557 *root = RB_ROOT;
2558}
2559
2560/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 * skb_queue_head - queue a buffer at the list head
2562 * @list: list to use
2563 * @newsk: buffer to queue
2564 *
2565 * Queue a buffer at the start of the list. This function takes the
2566 * list lock and can be used safely with other locking &sk_buff functions
2567 * safely.
2568 *
2569 * A buffer cannot be placed on two lists at the same time.
2570 */
2571void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2572{
2573 unsigned long flags;
2574
2575 spin_lock_irqsave(&list->lock, flags);
2576 __skb_queue_head(list, newsk);
2577 spin_unlock_irqrestore(&list->lock, flags);
2578}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002579EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581/**
2582 * skb_queue_tail - queue a buffer at the list tail
2583 * @list: list to use
2584 * @newsk: buffer to queue
2585 *
2586 * Queue a buffer at the tail of the list. This function takes the
2587 * list lock and can be used safely with other locking &sk_buff functions
2588 * safely.
2589 *
2590 * A buffer cannot be placed on two lists at the same time.
2591 */
2592void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2593{
2594 unsigned long flags;
2595
2596 spin_lock_irqsave(&list->lock, flags);
2597 __skb_queue_tail(list, newsk);
2598 spin_unlock_irqrestore(&list->lock, flags);
2599}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002600EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/**
2603 * skb_unlink - remove a buffer from a list
2604 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002605 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *
David S. Miller8728b832005-08-09 19:25:21 -07002607 * Remove a packet from a list. The list locks are taken and this
2608 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 *
David S. Miller8728b832005-08-09 19:25:21 -07002610 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 */
David S. Miller8728b832005-08-09 19:25:21 -07002612void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
David S. Miller8728b832005-08-09 19:25:21 -07002614 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
David S. Miller8728b832005-08-09 19:25:21 -07002616 spin_lock_irqsave(&list->lock, flags);
2617 __skb_unlink(skb, list);
2618 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002620EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622/**
2623 * skb_append - append a buffer
2624 * @old: buffer to insert after
2625 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002626 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
2628 * Place a packet after a given packet in a list. The list locks are taken
2629 * and this function is atomic with respect to other list locked calls.
2630 * A buffer cannot be placed on two lists at the same time.
2631 */
David S. Miller8728b832005-08-09 19:25:21 -07002632void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 unsigned long flags;
2635
David S. Miller8728b832005-08-09 19:25:21 -07002636 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002637 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002638 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002640EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642/**
2643 * skb_insert - insert a buffer
2644 * @old: buffer to insert before
2645 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002646 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 *
David S. Miller8728b832005-08-09 19:25:21 -07002648 * Place a packet before a given packet in a list. The list locks are
2649 * taken and this function is atomic with respect to other list locked
2650 * calls.
2651 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * A buffer cannot be placed on two lists at the same time.
2653 */
David S. Miller8728b832005-08-09 19:25:21 -07002654void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 unsigned long flags;
2657
David S. Miller8728b832005-08-09 19:25:21 -07002658 spin_lock_irqsave(&list->lock, flags);
2659 __skb_insert(newsk, old->prev, old, list);
2660 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002662EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664static inline void skb_split_inside_header(struct sk_buff *skb,
2665 struct sk_buff* skb1,
2666 const u32 len, const int pos)
2667{
2668 int i;
2669
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002670 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2671 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /* And move data appendix as is. */
2673 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2674 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2675
2676 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2677 skb_shinfo(skb)->nr_frags = 0;
2678 skb1->data_len = skb->data_len;
2679 skb1->len += skb1->data_len;
2680 skb->data_len = 0;
2681 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002682 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
2685static inline void skb_split_no_header(struct sk_buff *skb,
2686 struct sk_buff* skb1,
2687 const u32 len, int pos)
2688{
2689 int i, k = 0;
2690 const int nfrags = skb_shinfo(skb)->nr_frags;
2691
2692 skb_shinfo(skb)->nr_frags = 0;
2693 skb1->len = skb1->data_len = skb->len - len;
2694 skb->len = len;
2695 skb->data_len = len - pos;
2696
2697 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002698 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 if (pos + size > len) {
2701 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2702
2703 if (pos < len) {
2704 /* Split frag.
2705 * We have two variants in this case:
2706 * 1. Move all the frag to the second
2707 * part, if it is possible. F.e.
2708 * this approach is mandatory for TUX,
2709 * where splitting is expensive.
2710 * 2. Split is accurately. We make this.
2711 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002712 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002714 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2715 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 skb_shinfo(skb)->nr_frags++;
2717 }
2718 k++;
2719 } else
2720 skb_shinfo(skb)->nr_frags++;
2721 pos += size;
2722 }
2723 skb_shinfo(skb1)->nr_frags = k;
2724}
2725
2726/**
2727 * skb_split - Split fragmented skb to two parts at length len.
2728 * @skb: the buffer to split
2729 * @skb1: the buffer to receive the second part
2730 * @len: new length for skb
2731 */
2732void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2733{
2734 int pos = skb_headlen(skb);
2735
Willem de Bruijnfff88032017-06-08 11:35:03 -04002736 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2737 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (len < pos) /* Split line is inside header. */
2739 skb_split_inside_header(skb, skb1, len, pos);
2740 else /* Second chunk has no header, nothing to copy. */
2741 skb_split_no_header(skb, skb1, len, pos);
2742}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002743EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002745/* Shifting from/to a cloned skb is a no-go.
2746 *
2747 * Caller cannot keep skb_shinfo related pointers past calling here!
2748 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002749static int skb_prepare_for_shift(struct sk_buff *skb)
2750{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002751 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002752}
2753
2754/**
2755 * skb_shift - Shifts paged data partially from skb to another
2756 * @tgt: buffer into which tail data gets added
2757 * @skb: buffer from which the paged data comes from
2758 * @shiftlen: shift up to this many bytes
2759 *
2760 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002761 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002762 * It's up to caller to free skb if everything was shifted.
2763 *
2764 * If @tgt runs out of frags, the whole operation is aborted.
2765 *
2766 * Skb cannot include anything else but paged data while tgt is allowed
2767 * to have non-paged data as well.
2768 *
2769 * TODO: full sized shift could be optimized but that would need
2770 * specialized skb free'er to handle frags without up-to-date nr_frags.
2771 */
2772int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2773{
2774 int from, to, merge, todo;
2775 struct skb_frag_struct *fragfrom, *fragto;
2776
2777 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002778
2779 if (skb_headlen(skb))
2780 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002781
2782 todo = shiftlen;
2783 from = 0;
2784 to = skb_shinfo(tgt)->nr_frags;
2785 fragfrom = &skb_shinfo(skb)->frags[from];
2786
2787 /* Actual merge is delayed until the point when we know we can
2788 * commit all, so that we don't have to undo partial changes
2789 */
2790 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002791 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2792 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002793 merge = -1;
2794 } else {
2795 merge = to - 1;
2796
Eric Dumazet9e903e02011-10-18 21:00:24 +00002797 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002798 if (todo < 0) {
2799 if (skb_prepare_for_shift(skb) ||
2800 skb_prepare_for_shift(tgt))
2801 return 0;
2802
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002803 /* All previous frag pointers might be stale! */
2804 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002805 fragto = &skb_shinfo(tgt)->frags[merge];
2806
Eric Dumazet9e903e02011-10-18 21:00:24 +00002807 skb_frag_size_add(fragto, shiftlen);
2808 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002809 fragfrom->page_offset += shiftlen;
2810
2811 goto onlymerged;
2812 }
2813
2814 from++;
2815 }
2816
2817 /* Skip full, not-fitting skb to avoid expensive operations */
2818 if ((shiftlen == skb->len) &&
2819 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2820 return 0;
2821
2822 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2823 return 0;
2824
2825 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2826 if (to == MAX_SKB_FRAGS)
2827 return 0;
2828
2829 fragfrom = &skb_shinfo(skb)->frags[from];
2830 fragto = &skb_shinfo(tgt)->frags[to];
2831
Eric Dumazet9e903e02011-10-18 21:00:24 +00002832 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002833 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002834 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002835 from++;
2836 to++;
2837
2838 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002839 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002840 fragto->page = fragfrom->page;
2841 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002842 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002843
2844 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002845 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002846 todo = 0;
2847
2848 to++;
2849 break;
2850 }
2851 }
2852
2853 /* Ready to "commit" this state change to tgt */
2854 skb_shinfo(tgt)->nr_frags = to;
2855
2856 if (merge >= 0) {
2857 fragfrom = &skb_shinfo(skb)->frags[0];
2858 fragto = &skb_shinfo(tgt)->frags[merge];
2859
Eric Dumazet9e903e02011-10-18 21:00:24 +00002860 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002861 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002862 }
2863
2864 /* Reposition in the original skb */
2865 to = 0;
2866 while (from < skb_shinfo(skb)->nr_frags)
2867 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2868 skb_shinfo(skb)->nr_frags = to;
2869
2870 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2871
2872onlymerged:
2873 /* Most likely the tgt won't ever need its checksum anymore, skb on
2874 * the other hand might need it if it needs to be resent
2875 */
2876 tgt->ip_summed = CHECKSUM_PARTIAL;
2877 skb->ip_summed = CHECKSUM_PARTIAL;
2878
2879 /* Yak, is it really working this way? Some helper please? */
2880 skb->len -= shiftlen;
2881 skb->data_len -= shiftlen;
2882 skb->truesize -= shiftlen;
2883 tgt->len += shiftlen;
2884 tgt->data_len += shiftlen;
2885 tgt->truesize += shiftlen;
2886
2887 return shiftlen;
2888}
2889
Thomas Graf677e90e2005-06-23 20:59:51 -07002890/**
2891 * skb_prepare_seq_read - Prepare a sequential read of skb data
2892 * @skb: the buffer to read
2893 * @from: lower offset of data to be read
2894 * @to: upper offset of data to be read
2895 * @st: state variable
2896 *
2897 * Initializes the specified state variable. Must be called before
2898 * invoking skb_seq_read() for the first time.
2899 */
2900void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2901 unsigned int to, struct skb_seq_state *st)
2902{
2903 st->lower_offset = from;
2904 st->upper_offset = to;
2905 st->root_skb = st->cur_skb = skb;
2906 st->frag_idx = st->stepped_offset = 0;
2907 st->frag_data = NULL;
2908}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002909EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002910
2911/**
2912 * skb_seq_read - Sequentially read skb data
2913 * @consumed: number of bytes consumed by the caller so far
2914 * @data: destination pointer for data to be returned
2915 * @st: state variable
2916 *
Mathias Krausebc323832013-11-07 14:18:26 +01002917 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002918 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002919 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002920 * of the block or 0 if the end of the skb data or the upper
2921 * offset has been reached.
2922 *
2923 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002924 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002925 * of bytes already consumed and the next call to
2926 * skb_seq_read() will return the remaining part of the block.
2927 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002928 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002929 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002930 * reads of potentially non linear data.
2931 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002932 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002933 * at the moment, state->root_skb could be replaced with
2934 * a stack for this purpose.
2935 */
2936unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2937 struct skb_seq_state *st)
2938{
2939 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2940 skb_frag_t *frag;
2941
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002942 if (unlikely(abs_offset >= st->upper_offset)) {
2943 if (st->frag_data) {
2944 kunmap_atomic(st->frag_data);
2945 st->frag_data = NULL;
2946 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002947 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002948 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002949
2950next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002951 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002952
Thomas Chenault995b3372009-05-18 21:43:27 -07002953 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002954 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002955 return block_limit - abs_offset;
2956 }
2957
2958 if (st->frag_idx == 0 && !st->frag_data)
2959 st->stepped_offset += skb_headlen(st->cur_skb);
2960
2961 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2962 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002963 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002964
2965 if (abs_offset < block_limit) {
2966 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002967 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002968
2969 *data = (u8 *) st->frag_data + frag->page_offset +
2970 (abs_offset - st->stepped_offset);
2971
2972 return block_limit - abs_offset;
2973 }
2974
2975 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002976 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002977 st->frag_data = NULL;
2978 }
2979
2980 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002981 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002982 }
2983
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002984 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002985 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002986 st->frag_data = NULL;
2987 }
2988
David S. Miller21dc3302010-08-23 00:13:46 -07002989 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002990 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002991 st->frag_idx = 0;
2992 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002993 } else if (st->cur_skb->next) {
2994 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002995 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002996 goto next_skb;
2997 }
2998
2999 return 0;
3000}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003001EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003002
3003/**
3004 * skb_abort_seq_read - Abort a sequential read of skb data
3005 * @st: state variable
3006 *
3007 * Must be called if skb_seq_read() was not called until it
3008 * returned 0.
3009 */
3010void skb_abort_seq_read(struct skb_seq_state *st)
3011{
3012 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003013 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003014}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003015EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003016
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003017#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3018
3019static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3020 struct ts_config *conf,
3021 struct ts_state *state)
3022{
3023 return skb_seq_read(offset, text, TS_SKB_CB(state));
3024}
3025
3026static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3027{
3028 skb_abort_seq_read(TS_SKB_CB(state));
3029}
3030
3031/**
3032 * skb_find_text - Find a text pattern in skb data
3033 * @skb: the buffer to look in
3034 * @from: search offset
3035 * @to: search limit
3036 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003037 *
3038 * Finds a pattern in the skb data according to the specified
3039 * textsearch configuration. Use textsearch_next() to retrieve
3040 * subsequent occurrences of the pattern. Returns the offset
3041 * to the first occurrence or UINT_MAX if no match was found.
3042 */
3043unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003044 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003045{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003046 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003047 unsigned int ret;
3048
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003049 config->get_next_block = skb_ts_get_next_block;
3050 config->finish = skb_ts_finish;
3051
Bojan Prtvar059a2442015-02-22 11:46:35 +01003052 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003053
Bojan Prtvar059a2442015-02-22 11:46:35 +01003054 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003055 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003056}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003057EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003058
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003059/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003060 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003061 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09003062 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003063 * @getfrag: call back function to be used for getting the user data
3064 * @from: pointer to user message iov
3065 * @length: length of the iov message
3066 *
3067 * Description: This procedure append the user data in the fragment part
3068 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3069 */
3070int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08003071 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003072 int len, int odd, struct sk_buff *skb),
3073 void *from, int length)
3074{
Eric Dumazetb2111722012-12-28 06:06:37 +00003075 int frg_cnt = skb_shinfo(skb)->nr_frags;
3076 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003077 int offset = 0;
3078 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00003079 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003080
3081 do {
3082 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003083 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00003084 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003085
Eric Dumazetb2111722012-12-28 06:06:37 +00003086 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003087 return -ENOMEM;
3088
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003089 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00003090 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003091
Eric Dumazetb2111722012-12-28 06:06:37 +00003092 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3093 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003094 if (ret < 0)
3095 return -EFAULT;
3096
3097 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00003098 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3099 copy);
3100 frg_cnt++;
3101 pfrag->offset += copy;
3102 get_page(pfrag->page);
3103
3104 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003105 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003106 skb->len += copy;
3107 skb->data_len += copy;
3108 offset += copy;
3109 length -= copy;
3110
3111 } while (length > 0);
3112
3113 return 0;
3114}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003115EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003116
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003117int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3118 int offset, size_t size)
3119{
3120 int i = skb_shinfo(skb)->nr_frags;
3121
3122 if (skb_can_coalesce(skb, i, page, offset)) {
3123 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3124 } else if (i < MAX_SKB_FRAGS) {
3125 get_page(page);
3126 skb_fill_page_desc(skb, i, page, offset, size);
3127 } else {
3128 return -EMSGSIZE;
3129 }
3130
3131 return 0;
3132}
3133EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3134
Herbert Xucbb042f2006-03-20 22:43:56 -08003135/**
3136 * skb_pull_rcsum - pull skb and update receive checksum
3137 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003138 * @len: length of data pulled
3139 *
3140 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003141 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003142 * receive path processing instead of skb_pull unless you know
3143 * that the checksum difference is zero (e.g., a valid IP header)
3144 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003145 */
Johannes Bergaf728682017-06-16 14:29:22 +02003146void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003147{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003148 unsigned char *data = skb->data;
3149
Herbert Xucbb042f2006-03-20 22:43:56 -08003150 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003151 __skb_pull(skb, len);
3152 skb_postpull_rcsum(skb, data, len);
3153 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003154}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003155EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3156
Herbert Xuf4c50d92006-06-22 03:02:40 -07003157/**
3158 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003159 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003160 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003161 *
3162 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003163 * a pointer to the first in a list of new skbs for the segments.
3164 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003165 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003166struct sk_buff *skb_segment(struct sk_buff *head_skb,
3167 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003168{
3169 struct sk_buff *segs = NULL;
3170 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003171 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003172 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3173 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3174 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003175 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003176 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003177 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003178 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003179 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003180 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003181 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003182 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003183 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003184 int err = -ENOMEM;
3185 int i = 0;
3186 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003187 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003188
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003189 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003190 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003191 if (unlikely(!proto))
3192 return ERR_PTR(-EINVAL);
3193
Alexander Duyck36c98382016-05-02 09:38:18 -07003194 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003195 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003196
Steffen Klassert07b26c92016-09-19 12:58:47 +02003197 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3198 if (!(features & NETIF_F_GSO_PARTIAL)) {
3199 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003200 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003201
3202 if (!list_skb ||
3203 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3204 goto normal;
3205
Ilan Tayari43170c42017-04-19 21:26:07 +03003206 /* If we get here then all the required
3207 * GSO features except frag_list are supported.
3208 * Try to split the SKB to multiple GSO SKBs
3209 * with no frag_list.
3210 * Currently we can do that only when the buffers don't
3211 * have a linear part and all the buffers except
3212 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003213 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003214 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003215 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003216 if (frag_len != iter->len && iter->next)
3217 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003218 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003219 goto normal;
3220
3221 len -= iter->len;
3222 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003223
3224 if (len != frag_len)
3225 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003226 }
3227
3228 /* GSO partial only requires that we trim off any excess that
3229 * doesn't fit into an MSS sized block, so take care of that
3230 * now.
3231 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003232 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003233 if (partial_segs > 1)
3234 mss *= partial_segs;
3235 else
3236 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003237 }
3238
Steffen Klassert07b26c92016-09-19 12:58:47 +02003239normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003240 headroom = skb_headroom(head_skb);
3241 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003242
3243 do {
3244 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003245 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003246 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003247 int size;
3248
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003249 if (unlikely(mss == GSO_BY_FRAGS)) {
3250 len = list_skb->len;
3251 } else {
3252 len = head_skb->len - offset;
3253 if (len > mss)
3254 len = mss;
3255 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003256
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003257 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003258 if (hsize < 0)
3259 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003260 if (hsize > len || !sg)
3261 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003262
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003263 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3264 (skb_headlen(list_skb) == len || sg)) {
3265 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003266
Herbert Xu9d8506c2013-11-21 11:10:04 -08003267 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003268 nfrags = skb_shinfo(list_skb)->nr_frags;
3269 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003270 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003271 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003272
3273 while (pos < offset + len) {
3274 BUG_ON(i >= nfrags);
3275
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003276 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003277 if (pos + size > offset + len)
3278 break;
3279
3280 i++;
3281 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003282 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003283 }
3284
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003285 nskb = skb_clone(list_skb, GFP_ATOMIC);
3286 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003287
3288 if (unlikely(!nskb))
3289 goto err;
3290
Herbert Xu9d8506c2013-11-21 11:10:04 -08003291 if (unlikely(pskb_trim(nskb, len))) {
3292 kfree_skb(nskb);
3293 goto err;
3294 }
3295
Alexander Duyckec47ea82012-05-04 14:26:56 +00003296 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003297 if (skb_cow_head(nskb, doffset + headroom)) {
3298 kfree_skb(nskb);
3299 goto err;
3300 }
3301
Alexander Duyckec47ea82012-05-04 14:26:56 +00003302 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003303 skb_release_head_state(nskb);
3304 __skb_push(nskb, doffset);
3305 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003306 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003307 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003308 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003309
3310 if (unlikely(!nskb))
3311 goto err;
3312
3313 skb_reserve(nskb, headroom);
3314 __skb_put(nskb, doffset);
3315 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003316
3317 if (segs)
3318 tail->next = nskb;
3319 else
3320 segs = nskb;
3321 tail = nskb;
3322
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003323 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003324
Eric Dumazet030737b2013-10-19 11:42:54 -07003325 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003326 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003327
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003328 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003329 nskb->data - tnl_hlen,
3330 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003331
Herbert Xu9d8506c2013-11-21 11:10:04 -08003332 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003333 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003334
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003335 if (!sg) {
3336 if (!nskb->remcsum_offload)
3337 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003338 SKB_GSO_CB(nskb)->csum =
3339 skb_copy_and_csum_bits(head_skb, offset,
3340 skb_put(nskb, len),
3341 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003342 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003343 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003344 continue;
3345 }
3346
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003347 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003348
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003349 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003350 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003351
Willem de Bruijnfff88032017-06-08 11:35:03 -04003352 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3353 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003354
Herbert Xu9d8506c2013-11-21 11:10:04 -08003355 while (pos < offset + len) {
3356 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003357 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003358
3359 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003360 nfrags = skb_shinfo(list_skb)->nr_frags;
3361 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003362 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003363
3364 BUG_ON(!nfrags);
3365
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003366 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003367 }
3368
3369 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3370 MAX_SKB_FRAGS)) {
3371 net_warn_ratelimited(
3372 "skb_segment: too many frags: %u %u\n",
3373 pos, mss);
3374 goto err;
3375 }
3376
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003377 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3378 goto err;
3379
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003380 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003381 __skb_frag_ref(nskb_frag);
3382 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003383
3384 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003385 nskb_frag->page_offset += offset - pos;
3386 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003387 }
3388
Herbert Xu89319d382008-12-15 23:26:06 -08003389 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003390
3391 if (pos + size <= offset + len) {
3392 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003393 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003394 pos += size;
3395 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003396 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003397 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003398 }
3399
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003400 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003401 }
3402
Herbert Xu89319d382008-12-15 23:26:06 -08003403skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003404 nskb->data_len = len - hsize;
3405 nskb->len += nskb->data_len;
3406 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003407
Simon Horman1cdbcb72013-05-19 15:46:49 +00003408perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003409 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003410 if (skb_has_shared_frag(nskb)) {
3411 err = __skb_linearize(nskb);
3412 if (err)
3413 goto err;
3414 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003415 if (!nskb->remcsum_offload)
3416 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003417 SKB_GSO_CB(nskb)->csum =
3418 skb_checksum(nskb, doffset,
3419 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003420 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003421 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003422 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003423 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003424
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003425 /* Some callers want to get the end of the list.
3426 * Put it in segs->prev to avoid walking the list.
3427 * (see validate_xmit_skb_list() for example)
3428 */
3429 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003430
Alexander Duyck802ab552016-04-10 21:45:03 -04003431 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003432 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003433 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003434 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003435
3436 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003437 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003438 type &= ~SKB_GSO_DODGY;
3439
3440 /* Update GSO info and prepare to start updating headers on
3441 * our way back down the stack of protocols.
3442 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003443 for (iter = segs; iter; iter = iter->next) {
3444 skb_shinfo(iter)->gso_size = gso_size;
3445 skb_shinfo(iter)->gso_segs = partial_segs;
3446 skb_shinfo(iter)->gso_type = type;
3447 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3448 }
3449
3450 if (tail->len - doffset <= gso_size)
3451 skb_shinfo(tail)->gso_size = 0;
3452 else if (tail != segs)
3453 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003454 }
3455
Toshiaki Makita432c8562014-10-27 10:30:51 -07003456 /* Following permits correct backpressure, for protocols
3457 * using skb_set_owner_w().
3458 * Idea is to tranfert ownership from head_skb to last segment.
3459 */
3460 if (head_skb->destructor == sock_wfree) {
3461 swap(tail->truesize, head_skb->truesize);
3462 swap(tail->destructor, head_skb->destructor);
3463 swap(tail->sk, head_skb->sk);
3464 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003465 return segs;
3466
3467err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003468 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003469 return ERR_PTR(err);
3470}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003471EXPORT_SYMBOL_GPL(skb_segment);
3472
Herbert Xu71d93b32008-12-15 23:42:33 -08003473int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3474{
Eric Dumazet8a291112013-10-08 09:02:23 -07003475 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003476 unsigned int offset = skb_gro_offset(skb);
3477 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003478 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003479 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003480 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003481
Eric Dumazet8a291112013-10-08 09:02:23 -07003482 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003483 return -E2BIG;
3484
Eric Dumazet29e98242014-05-16 11:34:37 -07003485 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003486 pinfo = skb_shinfo(lp);
3487
3488 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003489 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003490 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003491 int i = skbinfo->nr_frags;
3492 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003493
Herbert Xu66e92fc2009-05-26 18:50:32 +00003494 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003495 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003496
Eric Dumazet8a291112013-10-08 09:02:23 -07003497 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003498 pinfo->nr_frags = nr_frags;
3499 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003500
Herbert Xu9aaa1562009-05-26 18:50:33 +00003501 frag = pinfo->frags + nr_frags;
3502 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003503 do {
3504 *--frag = *--frag2;
3505 } while (--i);
3506
3507 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003508 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003509
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003510 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003511 delta_truesize = skb->truesize -
3512 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003513
Herbert Xuf5572062009-01-14 20:40:03 -08003514 skb->truesize -= skb->data_len;
3515 skb->len -= skb->data_len;
3516 skb->data_len = 0;
3517
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003518 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003519 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003520 } else if (skb->head_frag) {
3521 int nr_frags = pinfo->nr_frags;
3522 skb_frag_t *frag = pinfo->frags + nr_frags;
3523 struct page *page = virt_to_head_page(skb->head);
3524 unsigned int first_size = headlen - offset;
3525 unsigned int first_offset;
3526
3527 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003528 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003529
3530 first_offset = skb->data -
3531 (unsigned char *)page_address(page) +
3532 offset;
3533
3534 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3535
3536 frag->page.p = page;
3537 frag->page_offset = first_offset;
3538 skb_frag_size_set(frag, first_size);
3539
3540 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3541 /* We dont need to clear skbinfo->nr_frags here */
3542
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003543 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003544 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3545 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003546 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003547
3548merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003549 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003550 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003551 unsigned int eat = offset - headlen;
3552
3553 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003554 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003555 skb->data_len -= eat;
3556 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003557 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003558 }
3559
Herbert Xu67147ba2009-05-26 18:50:22 +00003560 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003561
Eric Dumazet29e98242014-05-16 11:34:37 -07003562 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003563 skb_shinfo(p)->frag_list = skb;
3564 else
3565 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003566 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003567 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003568 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003569
Herbert Xu5d38a072009-01-04 16:13:40 -08003570done:
3571 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003572 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003573 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003574 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003575 if (lp != p) {
3576 lp->data_len += len;
3577 lp->truesize += delta_truesize;
3578 lp->len += len;
3579 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003580 NAPI_GRO_CB(skb)->same_flow = 1;
3581 return 0;
3582}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003583EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585void __init skb_init(void)
3586{
3587 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3588 sizeof(struct sk_buff),
3589 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003590 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003591 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003592 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003593 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003594 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003595 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003596 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
David S. Miller51c739d2007-10-30 21:29:29 -07003599static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003600__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3601 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003602{
David S. Miller1a028e52007-04-27 15:21:23 -07003603 int start = skb_headlen(skb);
3604 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003605 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003606 int elt = 0;
3607
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003608 if (unlikely(recursion_level >= 24))
3609 return -EMSGSIZE;
3610
David Howells716ea3a2007-04-02 20:19:53 -07003611 if (copy > 0) {
3612 if (copy > len)
3613 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003614 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003615 elt++;
3616 if ((len -= copy) == 0)
3617 return elt;
3618 offset += copy;
3619 }
3620
3621 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003622 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003623
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003624 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003625
Eric Dumazet9e903e02011-10-18 21:00:24 +00003626 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003627 if ((copy = end - offset) > 0) {
3628 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003629 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3630 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003631
3632 if (copy > len)
3633 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003634 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003635 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003636 elt++;
3637 if (!(len -= copy))
3638 return elt;
3639 offset += copy;
3640 }
David S. Miller1a028e52007-04-27 15:21:23 -07003641 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003642 }
3643
David S. Millerfbb398a2009-06-09 00:18:59 -07003644 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003645 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003646
David S. Millerfbb398a2009-06-09 00:18:59 -07003647 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003648
David S. Millerfbb398a2009-06-09 00:18:59 -07003649 end = start + frag_iter->len;
3650 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003651 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3652 return -EMSGSIZE;
3653
David S. Millerfbb398a2009-06-09 00:18:59 -07003654 if (copy > len)
3655 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003656 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3657 copy, recursion_level + 1);
3658 if (unlikely(ret < 0))
3659 return ret;
3660 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003661 if ((len -= copy) == 0)
3662 return elt;
3663 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003664 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003665 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003666 }
3667 BUG_ON(len);
3668 return elt;
3669}
3670
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003671/**
3672 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3673 * @skb: Socket buffer containing the buffers to be mapped
3674 * @sg: The scatter-gather list to map into
3675 * @offset: The offset into the buffer's contents to start mapping
3676 * @len: Length of buffer space to be mapped
3677 *
3678 * Fill the specified scatter-gather list with mappings/pointers into a
3679 * region of the buffer space attached to a socket buffer. Returns either
3680 * the number of scatterlist items used, or -EMSGSIZE if the contents
3681 * could not fit.
3682 */
3683int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3684{
3685 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3686
3687 if (nsg <= 0)
3688 return nsg;
3689
3690 sg_mark_end(&sg[nsg - 1]);
3691
3692 return nsg;
3693}
3694EXPORT_SYMBOL_GPL(skb_to_sgvec);
3695
Fan Du25a91d82014-01-18 09:54:23 +08003696/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3697 * sglist without mark the sg which contain last skb data as the end.
3698 * So the caller can mannipulate sg list as will when padding new data after
3699 * the first call without calling sg_unmark_end to expend sg list.
3700 *
3701 * Scenario to use skb_to_sgvec_nomark:
3702 * 1. sg_init_table
3703 * 2. skb_to_sgvec_nomark(payload1)
3704 * 3. skb_to_sgvec_nomark(payload2)
3705 *
3706 * This is equivalent to:
3707 * 1. sg_init_table
3708 * 2. skb_to_sgvec(payload1)
3709 * 3. sg_unmark_end
3710 * 4. skb_to_sgvec(payload2)
3711 *
3712 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3713 * is more preferable.
3714 */
3715int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3716 int offset, int len)
3717{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003718 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003719}
3720EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3721
David S. Miller51c739d2007-10-30 21:29:29 -07003722
David S. Miller51c739d2007-10-30 21:29:29 -07003723
David Howells716ea3a2007-04-02 20:19:53 -07003724/**
3725 * skb_cow_data - Check that a socket buffer's data buffers are writable
3726 * @skb: The socket buffer to check.
3727 * @tailbits: Amount of trailing space to be added
3728 * @trailer: Returned pointer to the skb where the @tailbits space begins
3729 *
3730 * Make sure that the data buffers attached to a socket buffer are
3731 * writable. If they are not, private copies are made of the data buffers
3732 * and the socket buffer is set to use these instead.
3733 *
3734 * If @tailbits is given, make sure that there is space to write @tailbits
3735 * bytes of data beyond current end of socket buffer. @trailer will be
3736 * set to point to the skb in which this space begins.
3737 *
3738 * The number of scatterlist elements required to completely map the
3739 * COW'd and extended socket buffer will be returned.
3740 */
3741int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3742{
3743 int copyflag;
3744 int elt;
3745 struct sk_buff *skb1, **skb_p;
3746
3747 /* If skb is cloned or its head is paged, reallocate
3748 * head pulling out all the pages (pages are considered not writable
3749 * at the moment even if they are anonymous).
3750 */
3751 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3752 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3753 return -ENOMEM;
3754
3755 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003756 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003757 /* A little of trouble, not enough of space for trailer.
3758 * This should not happen, when stack is tuned to generate
3759 * good frames. OK, on miss we reallocate and reserve even more
3760 * space, 128 bytes is fair. */
3761
3762 if (skb_tailroom(skb) < tailbits &&
3763 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3764 return -ENOMEM;
3765
3766 /* Voila! */
3767 *trailer = skb;
3768 return 1;
3769 }
3770
3771 /* Misery. We are in troubles, going to mincer fragments... */
3772
3773 elt = 1;
3774 skb_p = &skb_shinfo(skb)->frag_list;
3775 copyflag = 0;
3776
3777 while ((skb1 = *skb_p) != NULL) {
3778 int ntail = 0;
3779
3780 /* The fragment is partially pulled by someone,
3781 * this can happen on input. Copy it and everything
3782 * after it. */
3783
3784 if (skb_shared(skb1))
3785 copyflag = 1;
3786
3787 /* If the skb is the last, worry about trailer. */
3788
3789 if (skb1->next == NULL && tailbits) {
3790 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003791 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003792 skb_tailroom(skb1) < tailbits)
3793 ntail = tailbits + 128;
3794 }
3795
3796 if (copyflag ||
3797 skb_cloned(skb1) ||
3798 ntail ||
3799 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003800 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003801 struct sk_buff *skb2;
3802
3803 /* Fuck, we are miserable poor guys... */
3804 if (ntail == 0)
3805 skb2 = skb_copy(skb1, GFP_ATOMIC);
3806 else
3807 skb2 = skb_copy_expand(skb1,
3808 skb_headroom(skb1),
3809 ntail,
3810 GFP_ATOMIC);
3811 if (unlikely(skb2 == NULL))
3812 return -ENOMEM;
3813
3814 if (skb1->sk)
3815 skb_set_owner_w(skb2, skb1->sk);
3816
3817 /* Looking around. Are we still alive?
3818 * OK, link new skb, drop old one */
3819
3820 skb2->next = skb1->next;
3821 *skb_p = skb2;
3822 kfree_skb(skb1);
3823 skb1 = skb2;
3824 }
3825 elt++;
3826 *trailer = skb1;
3827 skb_p = &skb1->next;
3828 }
3829
3830 return elt;
3831}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003832EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003833
Eric Dumazetb1faf562010-05-31 23:44:05 -07003834static void sock_rmem_free(struct sk_buff *skb)
3835{
3836 struct sock *sk = skb->sk;
3837
3838 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3839}
3840
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003841static void skb_set_err_queue(struct sk_buff *skb)
3842{
3843 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3844 * So, it is safe to (mis)use it to mark skbs on the error queue.
3845 */
3846 skb->pkt_type = PACKET_OUTGOING;
3847 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3848}
3849
Eric Dumazetb1faf562010-05-31 23:44:05 -07003850/*
3851 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3852 */
3853int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3854{
3855 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003856 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003857 return -ENOMEM;
3858
3859 skb_orphan(skb);
3860 skb->sk = sk;
3861 skb->destructor = sock_rmem_free;
3862 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003863 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003864
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003865 /* before exiting rcu section, make sure dst is refcounted */
3866 skb_dst_force(skb);
3867
Eric Dumazetb1faf562010-05-31 23:44:05 -07003868 skb_queue_tail(&sk->sk_error_queue, skb);
3869 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003870 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003871 return 0;
3872}
3873EXPORT_SYMBOL(sock_queue_err_skb);
3874
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003875static bool is_icmp_err_skb(const struct sk_buff *skb)
3876{
3877 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3878 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3879}
3880
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003881struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3882{
3883 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003884 struct sk_buff *skb, *skb_next = NULL;
3885 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003886 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003887
Eric Dumazet997d5c32015-02-18 05:47:55 -08003888 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003889 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003890 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003891 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04003892 if (icmp_next)
3893 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3894 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08003895 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003896
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003897 if (is_icmp_err_skb(skb) && !icmp_next)
3898 sk->sk_err = 0;
3899
3900 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003901 sk->sk_error_report(sk);
3902
3903 return skb;
3904}
3905EXPORT_SYMBOL(sock_dequeue_err_skb);
3906
Alexander Duyckcab41c42014-09-10 18:05:26 -04003907/**
3908 * skb_clone_sk - create clone of skb, and take reference to socket
3909 * @skb: the skb to clone
3910 *
3911 * This function creates a clone of a buffer that holds a reference on
3912 * sk_refcnt. Buffers created via this function are meant to be
3913 * returned using sock_queue_err_skb, or free via kfree_skb.
3914 *
3915 * When passing buffers allocated with this function to sock_queue_err_skb
3916 * it is necessary to wrap the call with sock_hold/sock_put in order to
3917 * prevent the socket from being released prior to being enqueued on
3918 * the sk_error_queue.
3919 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003920struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3921{
3922 struct sock *sk = skb->sk;
3923 struct sk_buff *clone;
3924
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003925 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04003926 return NULL;
3927
3928 clone = skb_clone(skb, GFP_ATOMIC);
3929 if (!clone) {
3930 sock_put(sk);
3931 return NULL;
3932 }
3933
3934 clone->sk = sk;
3935 clone->destructor = sock_efree;
3936
3937 return clone;
3938}
3939EXPORT_SYMBOL(skb_clone_sk);
3940
Alexander Duyck37846ef2014-09-04 13:31:10 -04003941static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3942 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003943 int tstype,
3944 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003945{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003946 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003947 int err;
3948
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003949 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
3950
Patrick Ohlyac45f602009-02-12 05:03:37 +00003951 serr = SKB_EXT_ERR(skb);
3952 memset(serr, 0, sizeof(*serr));
3953 serr->ee.ee_errno = ENOMSG;
3954 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003955 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003956 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04003957 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003958 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003959 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003960 if (sk->sk_protocol == IPPROTO_TCP &&
3961 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003962 serr->ee.ee_data -= sk->sk_tskey;
3963 }
Eric Dumazet29030372010-05-29 00:20:48 -07003964
Patrick Ohlyac45f602009-02-12 05:03:37 +00003965 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003966
Patrick Ohlyac45f602009-02-12 05:03:37 +00003967 if (err)
3968 kfree_skb(skb);
3969}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003970
Willem de Bruijnb245be12015-01-30 13:29:32 -05003971static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3972{
3973 bool ret;
3974
3975 if (likely(sysctl_tstamp_allow_data || tsonly))
3976 return true;
3977
3978 read_lock_bh(&sk->sk_callback_lock);
3979 ret = sk->sk_socket && sk->sk_socket->file &&
3980 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3981 read_unlock_bh(&sk->sk_callback_lock);
3982 return ret;
3983}
3984
Alexander Duyck37846ef2014-09-04 13:31:10 -04003985void skb_complete_tx_timestamp(struct sk_buff *skb,
3986 struct skb_shared_hwtstamps *hwtstamps)
3987{
3988 struct sock *sk = skb->sk;
3989
Willem de Bruijnb245be12015-01-30 13:29:32 -05003990 if (!skb_may_tx_timestamp(sk, false))
3991 return;
3992
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003993 /* Take a reference to prevent skb_orphan() from freeing the socket,
3994 * but only if the socket refcount is not zero.
3995 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003996 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003997 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003998 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003999 sock_put(sk);
4000 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004001}
4002EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4003
4004void __skb_tstamp_tx(struct sk_buff *orig_skb,
4005 struct skb_shared_hwtstamps *hwtstamps,
4006 struct sock *sk, int tstype)
4007{
4008 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004009 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004010
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004011 if (!sk)
4012 return;
4013
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004014 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4015 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4016 return;
4017
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004018 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4019 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004020 return;
4021
Francis Yan1c885802016-11-27 23:07:18 -08004022 if (tsonly) {
4023#ifdef CONFIG_INET
4024 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4025 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004026 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004027 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004028 opt_stats = true;
4029 } else
Francis Yan1c885802016-11-27 23:07:18 -08004030#endif
4031 skb = alloc_skb(0, GFP_ATOMIC);
4032 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004033 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004034 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004035 if (!skb)
4036 return;
4037
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004038 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004039 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4040 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004041 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4042 }
4043
4044 if (hwtstamps)
4045 *skb_hwtstamps(skb) = *hwtstamps;
4046 else
4047 skb->tstamp = ktime_get_real();
4048
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004049 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004050}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004051EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4052
4053void skb_tstamp_tx(struct sk_buff *orig_skb,
4054 struct skb_shared_hwtstamps *hwtstamps)
4055{
4056 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4057 SCM_TSTAMP_SND);
4058}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004059EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4060
Johannes Berg6e3e9392011-11-09 10:15:42 +01004061void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4062{
4063 struct sock *sk = skb->sk;
4064 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004065 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004066
4067 skb->wifi_acked_valid = 1;
4068 skb->wifi_acked = acked;
4069
4070 serr = SKB_EXT_ERR(skb);
4071 memset(serr, 0, sizeof(*serr));
4072 serr->ee.ee_errno = ENOMSG;
4073 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4074
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004075 /* Take a reference to prevent skb_orphan() from freeing the socket,
4076 * but only if the socket refcount is not zero.
4077 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004078 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004079 err = sock_queue_err_skb(sk, skb);
4080 sock_put(sk);
4081 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004082 if (err)
4083 kfree_skb(skb);
4084}
4085EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4086
Rusty Russellf35d9d82008-02-04 23:49:54 -05004087/**
4088 * skb_partial_csum_set - set up and verify partial csum values for packet
4089 * @skb: the skb to set
4090 * @start: the number of bytes after skb->data to start checksumming.
4091 * @off: the offset from start to place the checksum.
4092 *
4093 * For untrusted partially-checksummed packets, we need to make sure the values
4094 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4095 *
4096 * This function checks and sets those values and skb->ip_summed: if this
4097 * returns false you should drop the packet.
4098 */
4099bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4100{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00004101 if (unlikely(start > skb_headlen(skb)) ||
4102 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004103 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4104 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004105 return false;
4106 }
4107 skb->ip_summed = CHECKSUM_PARTIAL;
4108 skb->csum_start = skb_headroom(skb) + start;
4109 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004110 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004111 return true;
4112}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004113EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004114
Paul Durranted1f50c2014-01-09 10:02:46 +00004115static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4116 unsigned int max)
4117{
4118 if (skb_headlen(skb) >= len)
4119 return 0;
4120
4121 /* If we need to pullup then pullup to the max, so we
4122 * won't need to do it again.
4123 */
4124 if (max > skb->len)
4125 max = skb->len;
4126
4127 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4128 return -ENOMEM;
4129
4130 if (skb_headlen(skb) < len)
4131 return -EPROTO;
4132
4133 return 0;
4134}
4135
Jan Beulichf9708b42014-03-11 13:56:05 +00004136#define MAX_TCP_HDR_LEN (15 * 4)
4137
4138static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4139 typeof(IPPROTO_IP) proto,
4140 unsigned int off)
4141{
4142 switch (proto) {
4143 int err;
4144
4145 case IPPROTO_TCP:
4146 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4147 off + MAX_TCP_HDR_LEN);
4148 if (!err && !skb_partial_csum_set(skb, off,
4149 offsetof(struct tcphdr,
4150 check)))
4151 err = -EPROTO;
4152 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4153
4154 case IPPROTO_UDP:
4155 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4156 off + sizeof(struct udphdr));
4157 if (!err && !skb_partial_csum_set(skb, off,
4158 offsetof(struct udphdr,
4159 check)))
4160 err = -EPROTO;
4161 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4162 }
4163
4164 return ERR_PTR(-EPROTO);
4165}
4166
Paul Durranted1f50c2014-01-09 10:02:46 +00004167/* This value should be large enough to cover a tagged ethernet header plus
4168 * maximally sized IP and TCP or UDP headers.
4169 */
4170#define MAX_IP_HDR_LEN 128
4171
Jan Beulichf9708b42014-03-11 13:56:05 +00004172static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004173{
4174 unsigned int off;
4175 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004176 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004177 int err;
4178
4179 fragment = false;
4180
4181 err = skb_maybe_pull_tail(skb,
4182 sizeof(struct iphdr),
4183 MAX_IP_HDR_LEN);
4184 if (err < 0)
4185 goto out;
4186
4187 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4188 fragment = true;
4189
4190 off = ip_hdrlen(skb);
4191
4192 err = -EPROTO;
4193
4194 if (fragment)
4195 goto out;
4196
Jan Beulichf9708b42014-03-11 13:56:05 +00004197 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4198 if (IS_ERR(csum))
4199 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004200
Jan Beulichf9708b42014-03-11 13:56:05 +00004201 if (recalculate)
4202 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4203 ip_hdr(skb)->daddr,
4204 skb->len - off,
4205 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004206 err = 0;
4207
4208out:
4209 return err;
4210}
4211
4212/* This value should be large enough to cover a tagged ethernet header plus
4213 * an IPv6 header, all options, and a maximal TCP or UDP header.
4214 */
4215#define MAX_IPV6_HDR_LEN 256
4216
4217#define OPT_HDR(type, skb, off) \
4218 (type *)(skb_network_header(skb) + (off))
4219
4220static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4221{
4222 int err;
4223 u8 nexthdr;
4224 unsigned int off;
4225 unsigned int len;
4226 bool fragment;
4227 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004228 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004229
4230 fragment = false;
4231 done = false;
4232
4233 off = sizeof(struct ipv6hdr);
4234
4235 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4236 if (err < 0)
4237 goto out;
4238
4239 nexthdr = ipv6_hdr(skb)->nexthdr;
4240
4241 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4242 while (off <= len && !done) {
4243 switch (nexthdr) {
4244 case IPPROTO_DSTOPTS:
4245 case IPPROTO_HOPOPTS:
4246 case IPPROTO_ROUTING: {
4247 struct ipv6_opt_hdr *hp;
4248
4249 err = skb_maybe_pull_tail(skb,
4250 off +
4251 sizeof(struct ipv6_opt_hdr),
4252 MAX_IPV6_HDR_LEN);
4253 if (err < 0)
4254 goto out;
4255
4256 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4257 nexthdr = hp->nexthdr;
4258 off += ipv6_optlen(hp);
4259 break;
4260 }
4261 case IPPROTO_AH: {
4262 struct ip_auth_hdr *hp;
4263
4264 err = skb_maybe_pull_tail(skb,
4265 off +
4266 sizeof(struct ip_auth_hdr),
4267 MAX_IPV6_HDR_LEN);
4268 if (err < 0)
4269 goto out;
4270
4271 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4272 nexthdr = hp->nexthdr;
4273 off += ipv6_authlen(hp);
4274 break;
4275 }
4276 case IPPROTO_FRAGMENT: {
4277 struct frag_hdr *hp;
4278
4279 err = skb_maybe_pull_tail(skb,
4280 off +
4281 sizeof(struct frag_hdr),
4282 MAX_IPV6_HDR_LEN);
4283 if (err < 0)
4284 goto out;
4285
4286 hp = OPT_HDR(struct frag_hdr, skb, off);
4287
4288 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4289 fragment = true;
4290
4291 nexthdr = hp->nexthdr;
4292 off += sizeof(struct frag_hdr);
4293 break;
4294 }
4295 default:
4296 done = true;
4297 break;
4298 }
4299 }
4300
4301 err = -EPROTO;
4302
4303 if (!done || fragment)
4304 goto out;
4305
Jan Beulichf9708b42014-03-11 13:56:05 +00004306 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4307 if (IS_ERR(csum))
4308 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004309
Jan Beulichf9708b42014-03-11 13:56:05 +00004310 if (recalculate)
4311 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4312 &ipv6_hdr(skb)->daddr,
4313 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004314 err = 0;
4315
4316out:
4317 return err;
4318}
4319
4320/**
4321 * skb_checksum_setup - set up partial checksum offset
4322 * @skb: the skb to set up
4323 * @recalculate: if true the pseudo-header checksum will be recalculated
4324 */
4325int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4326{
4327 int err;
4328
4329 switch (skb->protocol) {
4330 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004331 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004332 break;
4333
4334 case htons(ETH_P_IPV6):
4335 err = skb_checksum_setup_ipv6(skb, recalculate);
4336 break;
4337
4338 default:
4339 err = -EPROTO;
4340 break;
4341 }
4342
4343 return err;
4344}
4345EXPORT_SYMBOL(skb_checksum_setup);
4346
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004347/**
4348 * skb_checksum_maybe_trim - maybe trims the given skb
4349 * @skb: the skb to check
4350 * @transport_len: the data length beyond the network header
4351 *
4352 * Checks whether the given skb has data beyond the given transport length.
4353 * If so, returns a cloned skb trimmed to this transport length.
4354 * Otherwise returns the provided skb. Returns NULL in error cases
4355 * (e.g. transport_len exceeds skb length or out-of-memory).
4356 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004357 * Caller needs to set the skb transport header and free any returned skb if it
4358 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004359 */
4360static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4361 unsigned int transport_len)
4362{
4363 struct sk_buff *skb_chk;
4364 unsigned int len = skb_transport_offset(skb) + transport_len;
4365 int ret;
4366
Linus Lüssinga5169932015-08-13 05:54:07 +02004367 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004368 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004369 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004370 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004371
4372 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004373 if (!skb_chk)
4374 return NULL;
4375
4376 ret = pskb_trim_rcsum(skb_chk, len);
4377 if (ret) {
4378 kfree_skb(skb_chk);
4379 return NULL;
4380 }
4381
4382 return skb_chk;
4383}
4384
4385/**
4386 * skb_checksum_trimmed - validate checksum of an skb
4387 * @skb: the skb to check
4388 * @transport_len: the data length beyond the network header
4389 * @skb_chkf: checksum function to use
4390 *
4391 * Applies the given checksum function skb_chkf to the provided skb.
4392 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4393 *
4394 * If the skb has data beyond the given transport length, then a
4395 * trimmed & cloned skb is checked and returned.
4396 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004397 * Caller needs to set the skb transport header and free any returned skb if it
4398 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004399 */
4400struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4401 unsigned int transport_len,
4402 __sum16(*skb_chkf)(struct sk_buff *skb))
4403{
4404 struct sk_buff *skb_chk;
4405 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004406 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004407
4408 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4409 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004410 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004411
Linus Lüssinga5169932015-08-13 05:54:07 +02004412 if (!pskb_may_pull(skb_chk, offset))
4413 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004414
Linus Lüssing9b368812016-02-24 04:21:42 +01004415 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004416 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004417 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004418
Linus Lüssinga5169932015-08-13 05:54:07 +02004419 if (ret)
4420 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004421
4422 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004423
4424err:
4425 if (skb_chk && skb_chk != skb)
4426 kfree_skb(skb_chk);
4427
4428 return NULL;
4429
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004430}
4431EXPORT_SYMBOL(skb_checksum_trimmed);
4432
Ben Hutchings4497b072008-06-19 16:22:28 -07004433void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4434{
Joe Perchese87cc472012-05-13 21:56:26 +00004435 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4436 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004437}
Ben Hutchings4497b072008-06-19 16:22:28 -07004438EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004439
4440void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4441{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004442 if (head_stolen) {
4443 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004444 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004445 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004446 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004447 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004448}
4449EXPORT_SYMBOL(kfree_skb_partial);
4450
4451/**
4452 * skb_try_coalesce - try to merge skb to prior one
4453 * @to: prior buffer
4454 * @from: buffer to add
4455 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004456 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004457 */
4458bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4459 bool *fragstolen, int *delta_truesize)
4460{
4461 int i, delta, len = from->len;
4462
4463 *fragstolen = false;
4464
4465 if (skb_cloned(to))
4466 return false;
4467
4468 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004469 if (len)
4470 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004471 *delta_truesize = 0;
4472 return true;
4473 }
4474
4475 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4476 return false;
4477
4478 if (skb_headlen(from) != 0) {
4479 struct page *page;
4480 unsigned int offset;
4481
4482 if (skb_shinfo(to)->nr_frags +
4483 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4484 return false;
4485
4486 if (skb_head_is_locked(from))
4487 return false;
4488
4489 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4490
4491 page = virt_to_head_page(from->head);
4492 offset = from->data - (unsigned char *)page_address(page);
4493
4494 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4495 page, offset, skb_headlen(from));
4496 *fragstolen = true;
4497 } else {
4498 if (skb_shinfo(to)->nr_frags +
4499 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4500 return false;
4501
Weiping Panf4b549a2012-09-28 20:15:30 +00004502 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004503 }
4504
4505 WARN_ON_ONCE(delta < len);
4506
4507 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4508 skb_shinfo(from)->frags,
4509 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4510 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4511
4512 if (!skb_cloned(from))
4513 skb_shinfo(from)->nr_frags = 0;
4514
Li RongQing8ea853f2012-09-18 16:53:21 +00004515 /* if the skb is not cloned this does nothing
4516 * since we set nr_frags to 0.
4517 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004518 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4519 skb_frag_ref(from, i);
4520
4521 to->truesize += delta;
4522 to->len += len;
4523 to->data_len += len;
4524
4525 *delta_truesize = delta;
4526 return true;
4527}
4528EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004529
4530/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004531 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004532 *
4533 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004534 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004535 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004536 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4537 * into/from a tunnel. Some information have to be cleared during these
4538 * operations.
4539 * skb_scrub_packet can also be used to clean a skb before injecting it in
4540 * another namespace (@xnet == true). We have to clear all information in the
4541 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004542 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004543void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004544{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004545 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004546 skb->pkt_type = PACKET_HOST;
4547 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004548 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004549 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004550 secpath_reset(skb);
4551 nf_reset(skb);
4552 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004553
4554 if (!xnet)
4555 return;
4556
4557 skb_orphan(skb);
4558 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004559}
4560EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004561
4562/**
4563 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4564 *
4565 * @skb: GSO skb
4566 *
4567 * skb_gso_transport_seglen is used to determine the real size of the
4568 * individual segments, including Layer4 headers (TCP/UDP).
4569 *
4570 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4571 */
4572unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4573{
4574 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004575 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004576
Florian Westphalf993bc22014-10-20 13:49:18 +02004577 if (skb->encapsulation) {
4578 thlen = skb_inner_transport_header(skb) -
4579 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004580
Florian Westphalf993bc22014-10-20 13:49:18 +02004581 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4582 thlen += inner_tcp_hdrlen(skb);
4583 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4584 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004585 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4586 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004587 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004588 /* UFO sets gso_size to the size of the fragmentation
4589 * payload, i.e. the size of the L4 (UDP) header is already
4590 * accounted for.
4591 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004592 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004593}
4594EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004595
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004596/**
4597 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4598 *
4599 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004600 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004601 *
4602 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4603 * once split.
4604 */
4605bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4606{
4607 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4608 const struct sk_buff *iter;
4609 unsigned int hlen;
4610
4611 hlen = skb_gso_network_seglen(skb);
4612
4613 if (shinfo->gso_size != GSO_BY_FRAGS)
4614 return hlen <= mtu;
4615
4616 /* Undo this so we can re-use header sizes */
4617 hlen -= GSO_BY_FRAGS;
4618
4619 skb_walk_frags(skb, iter) {
4620 if (hlen + skb_headlen(iter) > mtu)
4621 return false;
4622 }
4623
4624 return true;
4625}
4626EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4627
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004628static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4629{
4630 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4631 kfree_skb(skb);
4632 return NULL;
4633 }
4634
Vlad Yasevichf6548612015-12-14 17:44:10 -05004635 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004636 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004637 skb->mac_header += VLAN_HLEN;
4638 return skb;
4639}
4640
4641struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4642{
4643 struct vlan_hdr *vhdr;
4644 u16 vlan_tci;
4645
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004646 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004647 /* vlan_tci is already set-up so leave this for another time */
4648 return skb;
4649 }
4650
4651 skb = skb_share_check(skb, GFP_ATOMIC);
4652 if (unlikely(!skb))
4653 goto err_free;
4654
4655 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4656 goto err_free;
4657
4658 vhdr = (struct vlan_hdr *)skb->data;
4659 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4660 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4661
4662 skb_pull_rcsum(skb, VLAN_HLEN);
4663 vlan_set_encap_proto(skb, vhdr);
4664
4665 skb = skb_reorder_vlan_header(skb);
4666 if (unlikely(!skb))
4667 goto err_free;
4668
4669 skb_reset_network_header(skb);
4670 skb_reset_transport_header(skb);
4671 skb_reset_mac_len(skb);
4672
4673 return skb;
4674
4675err_free:
4676 kfree_skb(skb);
4677 return NULL;
4678}
4679EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004680
Jiri Pirkoe2195122014-11-19 14:05:01 +01004681int skb_ensure_writable(struct sk_buff *skb, int write_len)
4682{
4683 if (!pskb_may_pull(skb, write_len))
4684 return -ENOMEM;
4685
4686 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4687 return 0;
4688
4689 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4690}
4691EXPORT_SYMBOL(skb_ensure_writable);
4692
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004693/* remove VLAN header from packet and update csum accordingly.
4694 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4695 */
4696int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004697{
4698 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004699 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004700 int err;
4701
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004702 if (WARN_ONCE(offset,
4703 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4704 offset)) {
4705 return -EINVAL;
4706 }
4707
Jiri Pirko93515d52014-11-19 14:05:02 +01004708 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4709 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004710 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004711
4712 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4713
4714 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4715 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4716
4717 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4718 __skb_pull(skb, VLAN_HLEN);
4719
4720 vlan_set_encap_proto(skb, vhdr);
4721 skb->mac_header += VLAN_HLEN;
4722
4723 if (skb_network_offset(skb) < ETH_HLEN)
4724 skb_set_network_header(skb, ETH_HLEN);
4725
4726 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004727
4728 return err;
4729}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004730EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004731
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004732/* Pop a vlan tag either from hwaccel or from payload.
4733 * Expects skb->data at mac header.
4734 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004735int skb_vlan_pop(struct sk_buff *skb)
4736{
4737 u16 vlan_tci;
4738 __be16 vlan_proto;
4739 int err;
4740
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004741 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004742 skb->vlan_tci = 0;
4743 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004744 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004745 return 0;
4746
4747 err = __skb_vlan_pop(skb, &vlan_tci);
4748 if (err)
4749 return err;
4750 }
4751 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004752 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004753 return 0;
4754
4755 vlan_proto = skb->protocol;
4756 err = __skb_vlan_pop(skb, &vlan_tci);
4757 if (unlikely(err))
4758 return err;
4759
4760 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4761 return 0;
4762}
4763EXPORT_SYMBOL(skb_vlan_pop);
4764
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004765/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4766 * Expects skb->data at mac header.
4767 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004768int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4769{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004770 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004771 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004772 int err;
4773
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004774 if (WARN_ONCE(offset,
4775 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4776 offset)) {
4777 return -EINVAL;
4778 }
4779
Jiri Pirko93515d52014-11-19 14:05:02 +01004780 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004781 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004782 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004783 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004784
Jiri Pirko93515d52014-11-19 14:05:02 +01004785 skb->protocol = skb->vlan_proto;
4786 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004787
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004788 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004789 }
4790 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4791 return 0;
4792}
4793EXPORT_SYMBOL(skb_vlan_push);
4794
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004795/**
4796 * alloc_skb_with_frags - allocate skb with page frags
4797 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004798 * @header_len: size of linear part
4799 * @data_len: needed length in frags
4800 * @max_page_order: max page order desired.
4801 * @errcode: pointer to error code if any
4802 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004803 *
4804 * This can be used to allocate a paged skb, given a maximal order for frags.
4805 */
4806struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4807 unsigned long data_len,
4808 int max_page_order,
4809 int *errcode,
4810 gfp_t gfp_mask)
4811{
4812 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4813 unsigned long chunk;
4814 struct sk_buff *skb;
4815 struct page *page;
4816 gfp_t gfp_head;
4817 int i;
4818
4819 *errcode = -EMSGSIZE;
4820 /* Note this test could be relaxed, if we succeed to allocate
4821 * high order pages...
4822 */
4823 if (npages > MAX_SKB_FRAGS)
4824 return NULL;
4825
4826 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004827 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07004828 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004829
4830 *errcode = -ENOBUFS;
4831 skb = alloc_skb(header_len, gfp_head);
4832 if (!skb)
4833 return NULL;
4834
4835 skb->truesize += npages << PAGE_SHIFT;
4836
4837 for (i = 0; npages > 0; i++) {
4838 int order = max_page_order;
4839
4840 while (order) {
4841 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004842 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004843 __GFP_COMP |
4844 __GFP_NOWARN |
4845 __GFP_NORETRY,
4846 order);
4847 if (page)
4848 goto fill_page;
4849 /* Do not retry other high order allocations */
4850 order = 1;
4851 max_page_order = 0;
4852 }
4853 order--;
4854 }
4855 page = alloc_page(gfp_mask);
4856 if (!page)
4857 goto failure;
4858fill_page:
4859 chunk = min_t(unsigned long, data_len,
4860 PAGE_SIZE << order);
4861 skb_fill_page_desc(skb, i, page, 0, chunk);
4862 data_len -= chunk;
4863 npages -= 1 << order;
4864 }
4865 return skb;
4866
4867failure:
4868 kfree_skb(skb);
4869 return NULL;
4870}
4871EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004872
4873/* carve out the first off bytes from skb when off < headlen */
4874static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4875 const int headlen, gfp_t gfp_mask)
4876{
4877 int i;
4878 int size = skb_end_offset(skb);
4879 int new_hlen = headlen - off;
4880 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004881
4882 size = SKB_DATA_ALIGN(size);
4883
4884 if (skb_pfmemalloc(skb))
4885 gfp_mask |= __GFP_MEMALLOC;
4886 data = kmalloc_reserve(size +
4887 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4888 gfp_mask, NUMA_NO_NODE, NULL);
4889 if (!data)
4890 return -ENOMEM;
4891
4892 size = SKB_WITH_OVERHEAD(ksize(data));
4893
4894 /* Copy real data, and all frags */
4895 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4896 skb->len -= off;
4897
4898 memcpy((struct skb_shared_info *)(data + size),
4899 skb_shinfo(skb),
4900 offsetof(struct skb_shared_info,
4901 frags[skb_shinfo(skb)->nr_frags]));
4902 if (skb_cloned(skb)) {
4903 /* drop the old head gracefully */
4904 if (skb_orphan_frags(skb, gfp_mask)) {
4905 kfree(data);
4906 return -ENOMEM;
4907 }
4908 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4909 skb_frag_ref(skb, i);
4910 if (skb_has_frag_list(skb))
4911 skb_clone_fraglist(skb);
4912 skb_release_data(skb);
4913 } else {
4914 /* we can reuse existing recount- all we did was
4915 * relocate values
4916 */
4917 skb_free_head(skb);
4918 }
4919
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004920 skb->head = data;
4921 skb->data = data;
4922 skb->head_frag = 0;
4923#ifdef NET_SKBUFF_DATA_USES_OFFSET
4924 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004925#else
4926 skb->end = skb->head + size;
4927#endif
4928 skb_set_tail_pointer(skb, skb_headlen(skb));
4929 skb_headers_offset_update(skb, 0);
4930 skb->cloned = 0;
4931 skb->hdr_len = 0;
4932 skb->nohdr = 0;
4933 atomic_set(&skb_shinfo(skb)->dataref, 1);
4934
4935 return 0;
4936}
4937
4938static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4939
4940/* carve out the first eat bytes from skb's frag_list. May recurse into
4941 * pskb_carve()
4942 */
4943static int pskb_carve_frag_list(struct sk_buff *skb,
4944 struct skb_shared_info *shinfo, int eat,
4945 gfp_t gfp_mask)
4946{
4947 struct sk_buff *list = shinfo->frag_list;
4948 struct sk_buff *clone = NULL;
4949 struct sk_buff *insp = NULL;
4950
4951 do {
4952 if (!list) {
4953 pr_err("Not enough bytes to eat. Want %d\n", eat);
4954 return -EFAULT;
4955 }
4956 if (list->len <= eat) {
4957 /* Eaten as whole. */
4958 eat -= list->len;
4959 list = list->next;
4960 insp = list;
4961 } else {
4962 /* Eaten partially. */
4963 if (skb_shared(list)) {
4964 clone = skb_clone(list, gfp_mask);
4965 if (!clone)
4966 return -ENOMEM;
4967 insp = list->next;
4968 list = clone;
4969 } else {
4970 /* This may be pulled without problems. */
4971 insp = list;
4972 }
4973 if (pskb_carve(list, eat, gfp_mask) < 0) {
4974 kfree_skb(clone);
4975 return -ENOMEM;
4976 }
4977 break;
4978 }
4979 } while (eat);
4980
4981 /* Free pulled out fragments. */
4982 while ((list = shinfo->frag_list) != insp) {
4983 shinfo->frag_list = list->next;
4984 kfree_skb(list);
4985 }
4986 /* And insert new clone at head. */
4987 if (clone) {
4988 clone->next = list;
4989 shinfo->frag_list = clone;
4990 }
4991 return 0;
4992}
4993
4994/* carve off first len bytes from skb. Split line (off) is in the
4995 * non-linear part of skb
4996 */
4997static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4998 int pos, gfp_t gfp_mask)
4999{
5000 int i, k = 0;
5001 int size = skb_end_offset(skb);
5002 u8 *data;
5003 const int nfrags = skb_shinfo(skb)->nr_frags;
5004 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005005
5006 size = SKB_DATA_ALIGN(size);
5007
5008 if (skb_pfmemalloc(skb))
5009 gfp_mask |= __GFP_MEMALLOC;
5010 data = kmalloc_reserve(size +
5011 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5012 gfp_mask, NUMA_NO_NODE, NULL);
5013 if (!data)
5014 return -ENOMEM;
5015
5016 size = SKB_WITH_OVERHEAD(ksize(data));
5017
5018 memcpy((struct skb_shared_info *)(data + size),
5019 skb_shinfo(skb), offsetof(struct skb_shared_info,
5020 frags[skb_shinfo(skb)->nr_frags]));
5021 if (skb_orphan_frags(skb, gfp_mask)) {
5022 kfree(data);
5023 return -ENOMEM;
5024 }
5025 shinfo = (struct skb_shared_info *)(data + size);
5026 for (i = 0; i < nfrags; i++) {
5027 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5028
5029 if (pos + fsize > off) {
5030 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5031
5032 if (pos < off) {
5033 /* Split frag.
5034 * We have two variants in this case:
5035 * 1. Move all the frag to the second
5036 * part, if it is possible. F.e.
5037 * this approach is mandatory for TUX,
5038 * where splitting is expensive.
5039 * 2. Split is accurately. We make this.
5040 */
5041 shinfo->frags[0].page_offset += off - pos;
5042 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5043 }
5044 skb_frag_ref(skb, i);
5045 k++;
5046 }
5047 pos += fsize;
5048 }
5049 shinfo->nr_frags = k;
5050 if (skb_has_frag_list(skb))
5051 skb_clone_fraglist(skb);
5052
5053 if (k == 0) {
5054 /* split line is in frag list */
5055 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5056 }
5057 skb_release_data(skb);
5058
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005059 skb->head = data;
5060 skb->head_frag = 0;
5061 skb->data = data;
5062#ifdef NET_SKBUFF_DATA_USES_OFFSET
5063 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005064#else
5065 skb->end = skb->head + size;
5066#endif
5067 skb_reset_tail_pointer(skb);
5068 skb_headers_offset_update(skb, 0);
5069 skb->cloned = 0;
5070 skb->hdr_len = 0;
5071 skb->nohdr = 0;
5072 skb->len -= off;
5073 skb->data_len = skb->len;
5074 atomic_set(&skb_shinfo(skb)->dataref, 1);
5075 return 0;
5076}
5077
5078/* remove len bytes from the beginning of the skb */
5079static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5080{
5081 int headlen = skb_headlen(skb);
5082
5083 if (len < headlen)
5084 return pskb_carve_inside_header(skb, len, headlen, gfp);
5085 else
5086 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5087}
5088
5089/* Extract to_copy bytes starting at off from skb, and return this in
5090 * a new skb
5091 */
5092struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5093 int to_copy, gfp_t gfp)
5094{
5095 struct sk_buff *clone = skb_clone(skb, gfp);
5096
5097 if (!clone)
5098 return NULL;
5099
5100 if (pskb_carve(clone, off, gfp) < 0 ||
5101 pskb_trim(clone, to_copy)) {
5102 kfree_skb(clone);
5103 return NULL;
5104 }
5105 return clone;
5106}
5107EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005108
5109/**
5110 * skb_condense - try to get rid of fragments/frag_list if possible
5111 * @skb: buffer
5112 *
5113 * Can be used to save memory before skb is added to a busy queue.
5114 * If packet has bytes in frags and enough tail room in skb->head,
5115 * pull all of them, so that we can free the frags right now and adjust
5116 * truesize.
5117 * Notes:
5118 * We do not reallocate skb->head thus can not fail.
5119 * Caller must re-evaluate skb->truesize if needed.
5120 */
5121void skb_condense(struct sk_buff *skb)
5122{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005123 if (skb->data_len) {
5124 if (skb->data_len > skb->end - skb->tail ||
5125 skb_cloned(skb))
5126 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005127
Eric Dumazet3174fed2016-12-09 08:02:05 -08005128 /* Nice, we can free page frag(s) right now */
5129 __pskb_pull_tail(skb, skb->data_len);
5130 }
5131 /* At this point, skb->truesize might be over estimated,
5132 * because skb had a fragment, and fragments do not tell
5133 * their truesize.
5134 * When we pulled its content into skb->head, fragment
5135 * was freed, but __pskb_pull_tail() could not possibly
5136 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005137 */
5138 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5139}