blob: 6bc19c80c210f7a2522676cad68355480a43a624 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030052#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070063#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000064#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070065#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040066#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000072#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
74
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Eric Dumazetd7e88832012-04-30 08:10:34 +000081struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Hans Westgaard Ry5f74f82e2016-02-03 09:26:57 +010083int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Jean Sacrenf05de732013-02-11 13:30:38 +000087 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000091 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Jean Sacrenf05de732013-02-11 13:30:38 +000098static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000099 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Joe Perchese005d192012-05-16 19:58:40 +0000101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000102 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 BUG();
106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jean Sacrenf05de732013-02-11 13:30:38 +0000110 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
161/**
David S. Millerd179cd12005-08-17 14:57:30 -0700162 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * @size: size to allocate
164 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800169 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
176 * %GFP_ATOMIC.
177 */
Al Virodd0fc662005-10-07 07:46:04 +0100178struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700179 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Christoph Lametere18b8902006-12-06 20:33:20 -0800181 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800182 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct sk_buff *skb;
184 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
189
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!skb)
196 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700197 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
203 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000204 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!data)
208 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
212 */
213 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700214 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300216 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300220 */
221 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700224 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300225 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 skb->head = data;
227 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700228 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700229 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000232
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800236 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000237 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238
Mel Gormanc93bdd02012-07-31 16:44:19 -0700239 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700240 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700242 fclones = container_of(skb, struct sk_buff_fclones, skb1);
243
244 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700245 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300246 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700247
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800248 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250out:
251 return skb;
252nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800253 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 skb = NULL;
255 goto out;
256}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800257EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700260 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000261 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700262 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000263 *
264 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100265 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700266 * @frag_size is 0, otherwise data should come from the page allocator
267 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000268 * The return is the new skb buffer.
269 * On a failure the return is %NULL, and @data is not freed.
270 * Notes :
271 * Before IO, driver allocates only data buffer where NIC put incoming frame
272 * Driver should add room at head (NET_SKB_PAD) and
273 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
274 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
275 * before giving packet to stack.
276 * RX rings only contains data buffers, not full skbs.
277 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700278struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000279{
280 struct skb_shared_info *shinfo;
281 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000282 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283
284 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
285 if (!skb)
286 return NULL;
287
Eric Dumazetd3836f22012-04-27 00:33:38 +0000288 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000289
290 memset(skb, 0, offsetof(struct sk_buff, tail));
291 skb->truesize = SKB_TRUESIZE(size);
Reshetova, Elena63354792017-06-30 13:07:58 +0300292 refcount_set(&skb->users, 1);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 skb->head = data;
294 skb->data = data;
295 skb_reset_tail_pointer(skb);
296 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800297 skb->mac_header = (typeof(skb->mac_header))~0U;
298 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299
300 /* make sure we initialize shinfo sequentially */
301 shinfo = skb_shinfo(skb);
302 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
303 atomic_set(&shinfo->dataref, 1);
304 kmemcheck_annotate_variable(shinfo->destructor_arg);
305
306 return skb;
307}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700308
309/* build_skb() is wrapper over __build_skb(), that specifically
310 * takes care of skb->head and skb->pfmemalloc
311 * This means that if @frag_size is not zero, then @data must be backed
312 * by a page fragment, not kmalloc() or vmalloc()
313 */
314struct sk_buff *build_skb(void *data, unsigned int frag_size)
315{
316 struct sk_buff *skb = __build_skb(data, frag_size);
317
318 if (skb && frag_size) {
319 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700320 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700321 skb->pfmemalloc = 1;
322 }
323 return skb;
324}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000325EXPORT_SYMBOL(build_skb);
326
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100327#define NAPI_SKB_CACHE_SIZE 64
328
329struct napi_alloc_cache {
330 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300331 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100332 void *skb_cache[NAPI_SKB_CACHE_SIZE];
333};
334
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700335static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100336static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800337
338static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
339{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700340 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800341 unsigned long flags;
342 void *data;
343
344 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700345 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800346 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000347 local_irq_restore(flags);
348 return data;
349}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700350
351/**
352 * netdev_alloc_frag - allocate a page fragment
353 * @fragsz: fragment size
354 *
355 * Allocates a frag from a page for receive buffer.
356 * Uses GFP_ATOMIC allocations.
357 */
358void *netdev_alloc_frag(unsigned int fragsz)
359{
360 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
361}
Eric Dumazet6f532612012-05-18 05:12:12 +0000362EXPORT_SYMBOL(netdev_alloc_frag);
363
Alexander Duyckffde7322014-12-09 19:40:42 -0800364static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
365{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100366 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700367
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800368 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800369}
370
371void *napi_alloc_frag(unsigned int fragsz)
372{
373 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
374}
375EXPORT_SYMBOL(napi_alloc_frag);
376
Eric Dumazet6f532612012-05-18 05:12:12 +0000377/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900380 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
382 *
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
387 *
388 * %NULL is returned if there is no free memory.
389 */
Alexander Duyck94519802015-05-06 21:11:40 -0700390struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
391 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800392{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700393 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700394 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800395 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700396 bool pfmemalloc;
397 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800398
Alexander Duyck94519802015-05-06 21:11:40 -0700399 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800400
Alexander Duyck94519802015-05-06 21:11:40 -0700401 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800402 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700403 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
404 if (!skb)
405 goto skb_fail;
406 goto skb_success;
407 }
Alexander Duyck94519802015-05-06 21:11:40 -0700408
409 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
410 len = SKB_DATA_ALIGN(len);
411
412 if (sk_memalloc_socks())
413 gfp_mask |= __GFP_MEMALLOC;
414
415 local_irq_save(flags);
416
417 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800418 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700419 pfmemalloc = nc->pfmemalloc;
420
421 local_irq_restore(flags);
422
423 if (unlikely(!data))
424 return NULL;
425
426 skb = __build_skb(data, len);
427 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700428 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700429 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700430 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431
Alexander Duyck94519802015-05-06 21:11:40 -0700432 /* use OR instead of assignment to avoid clearing of bits in mask */
433 if (pfmemalloc)
434 skb->pfmemalloc = 1;
435 skb->head_frag = 1;
436
Alexander Duycka080e7b2015-05-13 13:34:13 -0700437skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700438 skb_reserve(skb, NET_SKB_PAD);
439 skb->dev = dev;
440
Alexander Duycka080e7b2015-05-13 13:34:13 -0700441skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700442 return skb;
443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800444EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Alexander Duyckfd11a832014-12-09 19:40:49 -0800446/**
447 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
448 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900449 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800450 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
451 *
452 * Allocate a new sk_buff for use in NAPI receive. This buffer will
453 * attempt to allocate the head from a special reserved region used
454 * only for NAPI Rx allocation. By doing this we can save several
455 * CPU cycles by avoiding having to disable and re-enable IRQs.
456 *
457 * %NULL is returned if there is no free memory.
458 */
Alexander Duyck94519802015-05-06 21:11:40 -0700459struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
460 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800461{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100462 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800463 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700464 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800465
Alexander Duyck94519802015-05-06 21:11:40 -0700466 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467
Alexander Duyck94519802015-05-06 21:11:40 -0700468 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800469 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700470 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
471 if (!skb)
472 goto skb_fail;
473 goto skb_success;
474 }
Alexander Duyck94519802015-05-06 21:11:40 -0700475
476 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
477 len = SKB_DATA_ALIGN(len);
478
479 if (sk_memalloc_socks())
480 gfp_mask |= __GFP_MEMALLOC;
481
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800482 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700483 if (unlikely(!data))
484 return NULL;
485
486 skb = __build_skb(data, len);
487 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700488 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700489 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490 }
491
Alexander Duyck94519802015-05-06 21:11:40 -0700492 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100493 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700494 skb->pfmemalloc = 1;
495 skb->head_frag = 1;
496
Alexander Duycka080e7b2015-05-13 13:34:13 -0700497skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700498 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
499 skb->dev = napi->dev;
500
Alexander Duycka080e7b2015-05-13 13:34:13 -0700501skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800502 return skb;
503}
504EXPORT_SYMBOL(__napi_alloc_skb);
505
Peter Zijlstra654bed12008-10-07 14:22:33 -0700506void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000507 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700508{
509 skb_fill_page_desc(skb, i, page, off, size);
510 skb->len += size;
511 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000512 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700513}
514EXPORT_SYMBOL(skb_add_rx_frag);
515
Jason Wangf8e617e2013-11-01 14:07:47 +0800516void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
517 unsigned int truesize)
518{
519 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
520
521 skb_frag_size_add(frag, size);
522 skb->len += size;
523 skb->data_len += size;
524 skb->truesize += truesize;
525}
526EXPORT_SYMBOL(skb_coalesce_rx_frag);
527
Herbert Xu27b437c2006-07-13 19:26:39 -0700528static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700530 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700531 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Herbert Xu27b437c2006-07-13 19:26:39 -0700534static inline void skb_drop_fraglist(struct sk_buff *skb)
535{
536 skb_drop_list(&skb_shinfo(skb)->frag_list);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static void skb_clone_fraglist(struct sk_buff *skb)
540{
541 struct sk_buff *list;
542
David S. Millerfbb398a2009-06-09 00:18:59 -0700543 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 skb_get(list);
545}
546
Eric Dumazetd3836f22012-04-27 00:33:38 +0000547static void skb_free_head(struct sk_buff *skb)
548{
Alexander Duyck181edb22015-05-06 21:12:03 -0700549 unsigned char *head = skb->head;
550
Eric Dumazetd3836f22012-04-27 00:33:38 +0000551 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700552 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000553 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700554 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000555}
556
Adrian Bunk5bba1712006-06-29 13:02:35 -0700557static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Eric Dumazetff04a772014-09-23 18:39:30 -0700559 struct skb_shared_info *shinfo = skb_shinfo(skb);
560 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Dumazetff04a772014-09-23 18:39:30 -0700562 if (skb->cloned &&
563 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
564 &shinfo->dataref))
565 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000566
Eric Dumazetff04a772014-09-23 18:39:30 -0700567 for (i = 0; i < shinfo->nr_frags; i++)
568 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000569
Eric Dumazetff04a772014-09-23 18:39:30 -0700570 /*
571 * If skb buf is from userspace, we need to notify the caller
572 * the lower device DMA has done;
573 */
574 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
575 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Eric Dumazetff04a772014-09-23 18:39:30 -0700577 uarg = shinfo->destructor_arg;
578 if (uarg->callback)
579 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700581
582 if (shinfo->frag_list)
583 kfree_skb_list(shinfo->frag_list);
584
585 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
589 * Free an skbuff by memory without cleaning the state.
590 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800591static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700593 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700594
David S. Millerd179cd12005-08-17 14:57:30 -0700595 switch (skb->fclone) {
596 case SKB_FCLONE_UNAVAILABLE:
597 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800598 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700599
600 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700601 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800602
603 /* We usually free the clone (TX completion) before original skb
604 * This test would have no chance to be true for the clone,
605 * while here, branch prediction will be good.
606 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300607 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800608 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700609 break;
610
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800611 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700612 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700613 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700614 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300615 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800616 return;
617fastpath:
618 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Paolo Abeni0a463c72017-06-12 11:23:42 +0200621void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Eric Dumazetadf30902009-06-02 05:19:30 +0000623 skb_dst_drop(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200624 secpath_reset(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700625 if (skb->destructor) {
626 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 skb->destructor(skb);
628 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000629#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100630 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100631#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200632#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nf_bridge_put(skb->nf_bridge);
634#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700635}
636
637/* Free everything but the sk_buff shell. */
638static void skb_release_all(struct sk_buff *skb)
639{
640 skb_release_head_state(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200641 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Herbert Xu2d4baff2007-11-26 23:11:19 +0800644/**
645 * __kfree_skb - private function
646 * @skb: buffer
647 *
648 * Free an sk_buff. Release anything attached to the buffer.
649 * Clean the state. This is an internal helper function. Users should
650 * always call kfree_skb
651 */
652
653void __kfree_skb(struct sk_buff *skb)
654{
655 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 kfree_skbmem(skb);
657}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800658EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800661 * kfree_skb - free an sk_buff
662 * @skb: buffer to free
663 *
664 * Drop a reference to the buffer and free it if the usage count has
665 * hit zero.
666 */
667void kfree_skb(struct sk_buff *skb)
668{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200669 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800670 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200671
Neil Hormanead2ceb2009-03-11 09:49:55 +0000672 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800673 __kfree_skb(skb);
674}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800675EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800676
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700677void kfree_skb_list(struct sk_buff *segs)
678{
679 while (segs) {
680 struct sk_buff *next = segs->next;
681
682 kfree_skb(segs);
683 segs = next;
684 }
685}
686EXPORT_SYMBOL(kfree_skb_list);
687
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700688/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000689 * skb_tx_error - report an sk_buff xmit error
690 * @skb: buffer that triggered an error
691 *
692 * Report xmit error if a device callback is tracking this skb.
693 * skb must be freed afterwards.
694 */
695void skb_tx_error(struct sk_buff *skb)
696{
697 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
698 struct ubuf_info *uarg;
699
700 uarg = skb_shinfo(skb)->destructor_arg;
701 if (uarg->callback)
702 uarg->callback(uarg, false);
703 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
704 }
705}
706EXPORT_SYMBOL(skb_tx_error);
707
708/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000709 * consume_skb - free an skbuff
710 * @skb: buffer to free
711 *
712 * Drop a ref to the buffer and free it if the usage count has hit zero
713 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
714 * is being dropped after a failure and notes that
715 */
716void consume_skb(struct sk_buff *skb)
717{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200718 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000719 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200720
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900721 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000722 __kfree_skb(skb);
723}
724EXPORT_SYMBOL(consume_skb);
725
Paolo Abeni0a463c72017-06-12 11:23:42 +0200726/**
727 * consume_stateless_skb - free an skbuff, assuming it is stateless
728 * @skb: buffer to free
729 *
730 * Works like consume_skb(), but this variant assumes that all the head
731 * states have been already dropped.
732 */
733void consume_stateless_skb(struct sk_buff *skb)
734{
735 if (!skb_unref(skb))
736 return;
737
738 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200739 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200740 kfree_skbmem(skb);
741}
742
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100743void __kfree_skb_flush(void)
744{
745 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
746
747 /* flush skb_cache if containing objects */
748 if (nc->skb_count) {
749 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
750 nc->skb_cache);
751 nc->skb_count = 0;
752 }
753}
754
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100755static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100756{
757 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
758
759 /* drop skb->head and call any destructors for packet */
760 skb_release_all(skb);
761
762 /* record skb to CPU local list */
763 nc->skb_cache[nc->skb_count++] = skb;
764
765#ifdef CONFIG_SLUB
766 /* SLUB writes into objects when freeing */
767 prefetchw(skb);
768#endif
769
770 /* flush skb_cache if it is filled */
771 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
772 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
773 nc->skb_cache);
774 nc->skb_count = 0;
775 }
776}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100777void __kfree_skb_defer(struct sk_buff *skb)
778{
779 _kfree_skb_defer(skb);
780}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100781
782void napi_consume_skb(struct sk_buff *skb, int budget)
783{
784 if (unlikely(!skb))
785 return;
786
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100787 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100788 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100789 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100790 return;
791 }
792
Paolo Abeni76088942017-06-14 11:48:48 +0200793 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100794 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200795
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100796 /* if reaching here SKB is ready to free */
797 trace_consume_skb(skb);
798
799 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700800 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100801 __kfree_skb(skb);
802 return;
803 }
804
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100805 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100806}
807EXPORT_SYMBOL(napi_consume_skb);
808
Eric Dumazetb1937222014-09-28 22:18:47 -0700809/* Make sure a field is enclosed inside headers_start/headers_end section */
810#define CHECK_SKB_FIELD(field) \
811 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
812 offsetof(struct sk_buff, headers_start)); \
813 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
814 offsetof(struct sk_buff, headers_end)); \
815
Herbert Xudec18812007-10-14 00:37:30 -0700816static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
817{
818 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700819 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700820 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700821 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000822 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700823#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700824 new->sp = secpath_get(old->sp);
825#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700826 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700827
Eric Dumazetb1937222014-09-28 22:18:47 -0700828 /* Note : this field could be in headers_start/headers_end section
829 * It is not yet because we do not want to have a 16 bit hole
830 */
831 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300832
Eric Dumazetb1937222014-09-28 22:18:47 -0700833 memcpy(&new->headers_start, &old->headers_start,
834 offsetof(struct sk_buff, headers_end) -
835 offsetof(struct sk_buff, headers_start));
836 CHECK_SKB_FIELD(protocol);
837 CHECK_SKB_FIELD(csum);
838 CHECK_SKB_FIELD(hash);
839 CHECK_SKB_FIELD(priority);
840 CHECK_SKB_FIELD(skb_iif);
841 CHECK_SKB_FIELD(vlan_proto);
842 CHECK_SKB_FIELD(vlan_tci);
843 CHECK_SKB_FIELD(transport_header);
844 CHECK_SKB_FIELD(network_header);
845 CHECK_SKB_FIELD(mac_header);
846 CHECK_SKB_FIELD(inner_protocol);
847 CHECK_SKB_FIELD(inner_transport_header);
848 CHECK_SKB_FIELD(inner_network_header);
849 CHECK_SKB_FIELD(inner_mac_header);
850 CHECK_SKB_FIELD(mark);
851#ifdef CONFIG_NETWORK_SECMARK
852 CHECK_SKB_FIELD(secmark);
853#endif
Cong Wange0d10952013-08-01 11:10:25 +0800854#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700855 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300856#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800857#ifdef CONFIG_XPS
858 CHECK_SKB_FIELD(sender_cpu);
859#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700860#ifdef CONFIG_NET_SCHED
861 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700862#endif
863
Herbert Xudec18812007-10-14 00:37:30 -0700864}
865
Herbert Xu82c49a32009-05-22 22:11:37 +0000866/*
867 * You should not add any new code to this function. Add it to
868 * __copy_skb_header above instead.
869 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700870static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#define C(x) n->x = skb->x
873
874 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700876 __copy_skb_header(n, skb);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 C(len);
879 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700880 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700881 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800882 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 C(tail);
886 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800887 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000888 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800889 C(data);
890 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300891 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 atomic_inc(&(skb_shinfo(skb)->dataref));
894 skb->cloned = 1;
895
896 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700897#undef C
898}
899
900/**
901 * skb_morph - morph one skb into another
902 * @dst: the skb to receive the contents
903 * @src: the skb to supply the contents
904 *
905 * This is identical to skb_clone except that the target skb is
906 * supplied by the user.
907 *
908 * The target skb is returned upon exit.
909 */
910struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
911{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800912 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700913 return __skb_clone(dst, src);
914}
915EXPORT_SYMBOL_GPL(skb_morph);
916
Ben Hutchings2c530402012-07-10 10:55:09 +0000917/**
918 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000919 * @skb: the skb to modify
920 * @gfp_mask: allocation priority
921 *
922 * This must be called on SKBTX_DEV_ZEROCOPY skb.
923 * It will copy all frags into kernel and drop the reference
924 * to userspace pages.
925 *
926 * If this function is called from an interrupt gfp_mask() must be
927 * %GFP_ATOMIC.
928 *
929 * Returns 0 on success or a negative error code on failure
930 * to allocate kernel memory to copy to.
931 */
932int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000933{
934 int i;
935 int num_frags = skb_shinfo(skb)->nr_frags;
936 struct page *page, *head = NULL;
937 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
938
939 for (i = 0; i < num_frags; i++) {
940 u8 *vaddr;
941 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
942
Krishna Kumar02756ed2012-07-17 02:05:29 +0000943 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000944 if (!page) {
945 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900946 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000947 put_page(head);
948 head = next;
949 }
950 return -ENOMEM;
951 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200952 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000953 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000954 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200955 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900956 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000957 head = page;
958 }
959
960 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000961 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000962 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000963
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000964 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000965
966 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000967 for (i = num_frags - 1; i >= 0; i--) {
968 __skb_fill_page_desc(skb, i, head, 0,
969 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900970 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000971 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000972
973 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000974 return 0;
975}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000976EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000977
Herbert Xue0053ec2007-10-14 00:37:52 -0700978/**
979 * skb_clone - duplicate an sk_buff
980 * @skb: buffer to clone
981 * @gfp_mask: allocation priority
982 *
983 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
984 * copies share the same packet data but not structure. The new
985 * buffer has a reference count of 1. If the allocation fails the
986 * function returns %NULL otherwise the new buffer is returned.
987 *
988 * If this function is called from an interrupt gfp_mask() must be
989 * %GFP_ATOMIC.
990 */
991
992struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
993{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700994 struct sk_buff_fclones *fclones = container_of(skb,
995 struct sk_buff_fclones,
996 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800997 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700998
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000999 if (skb_orphan_frags(skb, gfp_mask))
1000 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001001
Herbert Xue0053ec2007-10-14 00:37:52 -07001002 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001003 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001004 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001005 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001006 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001007 if (skb_pfmemalloc(skb))
1008 gfp_mask |= __GFP_MEMALLOC;
1009
Herbert Xue0053ec2007-10-14 00:37:52 -07001010 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1011 if (!n)
1012 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001013
1014 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001015 n->fclone = SKB_FCLONE_UNAVAILABLE;
1016 }
1017
1018 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001020EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001022static void skb_headers_offset_update(struct sk_buff *skb, int off)
1023{
Eric Dumazet030737b2013-10-19 11:42:54 -07001024 /* Only adjust this if it actually is csum_start rather than csum */
1025 if (skb->ip_summed == CHECKSUM_PARTIAL)
1026 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001027 /* {transport,network,mac}_header and tail are relative to skb->head */
1028 skb->transport_header += off;
1029 skb->network_header += off;
1030 if (skb_mac_header_was_set(skb))
1031 skb->mac_header += off;
1032 skb->inner_transport_header += off;
1033 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001034 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1038{
Herbert Xudec18812007-10-14 00:37:30 -07001039 __copy_skb_header(new, old);
1040
Herbert Xu79671682006-06-22 02:40:14 -07001041 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1042 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1043 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Mel Gormanc93bdd02012-07-31 16:44:19 -07001046static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1047{
1048 if (skb_pfmemalloc(skb))
1049 return SKB_ALLOC_RX;
1050 return 0;
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/**
1054 * skb_copy - create private copy of an sk_buff
1055 * @skb: buffer to copy
1056 * @gfp_mask: allocation priority
1057 *
1058 * Make a copy of both an &sk_buff and its data. This is used when the
1059 * caller wishes to modify the data and needs a private copy of the
1060 * data to alter. Returns %NULL on failure or the pointer to the buffer
1061 * on success. The returned buffer has a reference count of 1.
1062 *
1063 * As by-product this function converts non-linear &sk_buff to linear
1064 * one, so that &sk_buff becomes completely private and caller is allowed
1065 * to modify all the data of returned buffer. This means that this
1066 * function is not recommended for use in circumstances when only
1067 * header is going to be modified. Use pskb_copy() instead.
1068 */
1069
Al Virodd0fc662005-10-07 07:46:04 +01001070struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001072 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001073 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001074 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1075 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!n)
1078 return NULL;
1079
1080 /* Set the data pointer */
1081 skb_reserve(n, headerlen);
1082 /* Set the tail pointer and length */
1083 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1086 BUG();
1087
1088 copy_skb_header(n, skb);
1089 return n;
1090}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001091EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001094 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001096 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001098 * @fclone: if true allocate the copy of the skb from the fclone
1099 * cache instead of the head cache; it is recommended to set this
1100 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *
1102 * Make a copy of both an &sk_buff and part of its data, located
1103 * in header. Fragmented data remain shared. This is used when
1104 * the caller wishes to modify only header of &sk_buff and needs
1105 * private copy of the header to alter. Returns %NULL on failure
1106 * or the pointer to the buffer on success.
1107 * The returned buffer has a reference count of 1.
1108 */
1109
Octavian Purdilabad93e92014-06-12 01:36:26 +03001110struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1111 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Eric Dumazet117632e2011-12-03 21:39:53 +00001113 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001114 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1115 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!n)
1118 goto out;
1119
1120 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001121 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Set the tail pointer and length */
1123 skb_put(n, skb_headlen(skb));
1124 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001125 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Herbert Xu25f484a2006-11-07 14:57:15 -08001127 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 n->data_len = skb->data_len;
1129 n->len = skb->len;
1130
1131 if (skb_shinfo(skb)->nr_frags) {
1132 int i;
1133
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001134 if (skb_orphan_frags(skb, gfp_mask)) {
1135 kfree_skb(n);
1136 n = NULL;
1137 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1140 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001141 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 skb_shinfo(n)->nr_frags = i;
1144 }
1145
David S. Miller21dc3302010-08-23 00:13:46 -07001146 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1148 skb_clone_fraglist(n);
1149 }
1150
1151 copy_skb_header(n, skb);
1152out:
1153 return n;
1154}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001155EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157/**
1158 * pskb_expand_head - reallocate header of &sk_buff
1159 * @skb: buffer to reallocate
1160 * @nhead: room to add at head
1161 * @ntail: room to add at tail
1162 * @gfp_mask: allocation priority
1163 *
Mathias Krausebc323832013-11-07 14:18:26 +01001164 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1165 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * reference count of 1. Returns zero in the case of success or error,
1167 * if expansion failed. In the last case, &sk_buff is not changed.
1168 *
1169 * All the pointers pointing into skb header may change and must be
1170 * reloaded after call to this function.
1171 */
1172
Victor Fusco86a76ca2005-07-08 14:57:47 -07001173int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001174 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Eric Dumazet158f3232017-01-27 07:11:27 -08001176 int i, osize = skb_end_offset(skb);
1177 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001179 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Herbert Xu4edd87a2008-10-01 07:09:38 -07001181 BUG_ON(nhead < 0);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (skb_shared(skb))
1184 BUG();
1185
1186 size = SKB_DATA_ALIGN(size);
1187
Mel Gormanc93bdd02012-07-31 16:44:19 -07001188 if (skb_pfmemalloc(skb))
1189 gfp_mask |= __GFP_MEMALLOC;
1190 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1191 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!data)
1193 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001194 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001197 * optimized for the cases when header is void.
1198 */
1199 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1200
1201 memcpy((struct skb_shared_info *)(data + size),
1202 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001203 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Alexander Duyck3e245912012-05-04 14:26:51 +00001205 /*
1206 * if shinfo is shared we must drop the old head gracefully, but if it
1207 * is not we can just drop the old head and let the existing refcount
1208 * be since all we did is relocate the values
1209 */
1210 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001211 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001212 if (skb_orphan_frags(skb, gfp_mask))
1213 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001214 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001215 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Eric Dumazet1fd63042010-09-02 23:09:32 +00001217 if (skb_has_frag_list(skb))
1218 skb_clone_fraglist(skb);
1219
1220 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001221 } else {
1222 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 off = (data + nhead) - skb->head;
1225
1226 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001227 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001229#ifdef NET_SKBUFF_DATA_USES_OFFSET
1230 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001231 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001232#else
1233 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001234#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001235 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001236 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001238 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 skb->nohdr = 0;
1240 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001241
1242 /* It is not generally safe to change skb->truesize.
1243 * For the moment, we really care of rx path, or
1244 * when skb is orphaned (not attached to a socket).
1245 */
1246 if (!skb->sk || skb->destructor == sock_edemux)
1247 skb->truesize += size - osize;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 0;
1250
Shirley Maa6686f22011-07-06 12:22:12 +00001251nofrags:
1252 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253nodata:
1254 return -ENOMEM;
1255}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001256EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258/* Make private copy of skb with writable head and some headroom */
1259
1260struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1261{
1262 struct sk_buff *skb2;
1263 int delta = headroom - skb_headroom(skb);
1264
1265 if (delta <= 0)
1266 skb2 = pskb_copy(skb, GFP_ATOMIC);
1267 else {
1268 skb2 = skb_clone(skb, GFP_ATOMIC);
1269 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1270 GFP_ATOMIC)) {
1271 kfree_skb(skb2);
1272 skb2 = NULL;
1273 }
1274 }
1275 return skb2;
1276}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001277EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279/**
1280 * skb_copy_expand - copy and expand sk_buff
1281 * @skb: buffer to copy
1282 * @newheadroom: new free bytes at head
1283 * @newtailroom: new free bytes at tail
1284 * @gfp_mask: allocation priority
1285 *
1286 * Make a copy of both an &sk_buff and its data and while doing so
1287 * allocate additional space.
1288 *
1289 * This is used when the caller wishes to modify the data and needs a
1290 * private copy of the data to alter as well as more space for new fields.
1291 * Returns %NULL on failure or the pointer to the buffer
1292 * on success. The returned buffer has a reference count of 1.
1293 *
1294 * You must pass %GFP_ATOMIC as the allocation priority if this function
1295 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001298 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001299 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 /*
1302 * Allocate the copy buffer
1303 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001304 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1305 gfp_mask, skb_alloc_rx_flag(skb),
1306 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001307 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int head_copy_len, head_copy_off;
1309
1310 if (!n)
1311 return NULL;
1312
1313 skb_reserve(n, newheadroom);
1314
1315 /* Set the tail pointer and length */
1316 skb_put(n, skb->len);
1317
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001318 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 head_copy_off = 0;
1320 if (newheadroom <= head_copy_len)
1321 head_copy_len = newheadroom;
1322 else
1323 head_copy_off = newheadroom - head_copy_len;
1324
1325 /* Copy the linear header and data. */
1326 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1327 skb->len + head_copy_len))
1328 BUG();
1329
1330 copy_skb_header(n, skb);
1331
Eric Dumazet030737b2013-10-19 11:42:54 -07001332 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return n;
1335}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001336EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/**
1339 * skb_pad - zero pad the tail of an skb
1340 * @skb: buffer to pad
1341 * @pad: space to pad
1342 *
1343 * Ensure that a buffer is followed by a padding area that is zero
1344 * filled. Used by network drivers which may DMA or transfer data
1345 * beyond the buffer end onto the wire.
1346 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001347 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001349
Herbert Xu5b057c62006-06-23 02:06:41 -07001350int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Herbert Xu5b057c62006-06-23 02:06:41 -07001352 int err;
1353 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001356 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001360
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001361 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001362 if (likely(skb_cloned(skb) || ntail > 0)) {
1363 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1364 if (unlikely(err))
1365 goto free_skb;
1366 }
1367
1368 /* FIXME: The use of this function with non-linear skb's really needs
1369 * to be audited.
1370 */
1371 err = skb_linearize(skb);
1372 if (unlikely(err))
1373 goto free_skb;
1374
1375 memset(skb->data + skb->len, 0, pad);
1376 return 0;
1377
1378free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001380 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001381}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001382EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001383
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001384/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001385 * pskb_put - add data to the tail of a potentially fragmented buffer
1386 * @skb: start of the buffer to use
1387 * @tail: tail fragment of the buffer to use
1388 * @len: amount of data to add
1389 *
1390 * This function extends the used data area of the potentially
1391 * fragmented buffer. @tail must be the last fragment of @skb -- or
1392 * @skb itself. If this would exceed the total buffer size the kernel
1393 * will panic. A pointer to the first byte of the extra data is
1394 * returned.
1395 */
1396
Johannes Berg4df864c2017-06-16 14:29:21 +02001397void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001398{
1399 if (tail != skb) {
1400 skb->data_len += len;
1401 skb->len += len;
1402 }
1403 return skb_put(tail, len);
1404}
1405EXPORT_SYMBOL_GPL(pskb_put);
1406
1407/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001408 * skb_put - add data to a buffer
1409 * @skb: buffer to use
1410 * @len: amount of data to add
1411 *
1412 * This function extends the used data area of the buffer. If this would
1413 * exceed the total buffer size the kernel will panic. A pointer to the
1414 * first byte of the extra data is returned.
1415 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001416void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001417{
Johannes Berg4df864c2017-06-16 14:29:21 +02001418 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001419 SKB_LINEAR_ASSERT(skb);
1420 skb->tail += len;
1421 skb->len += len;
1422 if (unlikely(skb->tail > skb->end))
1423 skb_over_panic(skb, len, __builtin_return_address(0));
1424 return tmp;
1425}
1426EXPORT_SYMBOL(skb_put);
1427
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001428/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001429 * skb_push - add data to the start of a buffer
1430 * @skb: buffer to use
1431 * @len: amount of data to add
1432 *
1433 * This function extends the used data area of the buffer at the buffer
1434 * start. If this would exceed the total buffer headroom the kernel will
1435 * panic. A pointer to the first byte of the extra data is returned.
1436 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001437void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001438{
1439 skb->data -= len;
1440 skb->len += len;
1441 if (unlikely(skb->data<skb->head))
1442 skb_under_panic(skb, len, __builtin_return_address(0));
1443 return skb->data;
1444}
1445EXPORT_SYMBOL(skb_push);
1446
1447/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001448 * skb_pull - remove data from the start of a buffer
1449 * @skb: buffer to use
1450 * @len: amount of data to remove
1451 *
1452 * This function removes data from the start of a buffer, returning
1453 * the memory to the headroom. A pointer to the next data in the buffer
1454 * is returned. Once the data has been pulled future pushes will overwrite
1455 * the old data.
1456 */
Johannes Bergaf728682017-06-16 14:29:22 +02001457void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001458{
David S. Miller47d29642010-05-02 02:21:44 -07001459 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001460}
1461EXPORT_SYMBOL(skb_pull);
1462
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001463/**
1464 * skb_trim - remove end from a buffer
1465 * @skb: buffer to alter
1466 * @len: new length
1467 *
1468 * Cut the length of a buffer down by removing data from the tail. If
1469 * the buffer is already under the length specified it is not modified.
1470 * The skb must be linear.
1471 */
1472void skb_trim(struct sk_buff *skb, unsigned int len)
1473{
1474 if (skb->len > len)
1475 __skb_trim(skb, len);
1476}
1477EXPORT_SYMBOL(skb_trim);
1478
Herbert Xu3cc0e872006-06-09 16:13:38 -07001479/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481
Herbert Xu3cc0e872006-06-09 16:13:38 -07001482int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Herbert Xu27b437c2006-07-13 19:26:39 -07001484 struct sk_buff **fragp;
1485 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int offset = skb_headlen(skb);
1487 int nfrags = skb_shinfo(skb)->nr_frags;
1488 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001489 int err;
1490
1491 if (skb_cloned(skb) &&
1492 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001495 i = 0;
1496 if (offset >= len)
1497 goto drop_pages;
1498
1499 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001500 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001501
1502 if (end < len) {
1503 offset = end;
1504 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001506
Eric Dumazet9e903e02011-10-18 21:00:24 +00001507 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001508
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001509drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001510 skb_shinfo(skb)->nr_frags = i;
1511
1512 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001513 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001514
David S. Miller21dc3302010-08-23 00:13:46 -07001515 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001516 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001517 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
Herbert Xu27b437c2006-07-13 19:26:39 -07001520 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1521 fragp = &frag->next) {
1522 int end = offset + frag->len;
1523
1524 if (skb_shared(frag)) {
1525 struct sk_buff *nfrag;
1526
1527 nfrag = skb_clone(frag, GFP_ATOMIC);
1528 if (unlikely(!nfrag))
1529 return -ENOMEM;
1530
1531 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001532 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001533 frag = nfrag;
1534 *fragp = frag;
1535 }
1536
1537 if (end < len) {
1538 offset = end;
1539 continue;
1540 }
1541
1542 if (end > len &&
1543 unlikely((err = pskb_trim(frag, len - offset))))
1544 return err;
1545
1546 if (frag->next)
1547 skb_drop_list(&frag->next);
1548 break;
1549 }
1550
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001551done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001552 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 skb->data_len -= skb->len - len;
1554 skb->len = len;
1555 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001556 skb->len = len;
1557 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001558 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001561 if (!skb->sk || skb->destructor == sock_edemux)
1562 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001565EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567/**
1568 * __pskb_pull_tail - advance tail of skb header
1569 * @skb: buffer to reallocate
1570 * @delta: number of bytes to advance tail
1571 *
1572 * The function makes a sense only on a fragmented &sk_buff,
1573 * it expands header moving its tail forward and copying necessary
1574 * data from fragmented part.
1575 *
1576 * &sk_buff MUST have reference count of 1.
1577 *
1578 * Returns %NULL (and &sk_buff does not change) if pull failed
1579 * or value of new tail of skb in the case of success.
1580 *
1581 * All the pointers pointing into skb header may change and must be
1582 * reloaded after call to this function.
1583 */
1584
1585/* Moves tail of skb head forward, copying data from fragmented part,
1586 * when it is necessary.
1587 * 1. It may fail due to malloc failure.
1588 * 2. It may change skb pointers.
1589 *
1590 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1591 */
Johannes Bergaf728682017-06-16 14:29:22 +02001592void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 /* If skb has not enough free space at tail, get new one
1595 * plus 128 bytes for future expansions. If we have enough
1596 * room at tail, reallocate without expansion only if skb is cloned.
1597 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001598 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (eat > 0 || skb_cloned(skb)) {
1601 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1602 GFP_ATOMIC))
1603 return NULL;
1604 }
1605
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001606 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 BUG();
1608
1609 /* Optimization: no fragments, no reasons to preestimate
1610 * size of pulled pages. Superb.
1611 */
David S. Miller21dc3302010-08-23 00:13:46 -07001612 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto pull_pages;
1614
1615 /* Estimate size of pulled pages. */
1616 eat = delta;
1617 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001618 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1619
1620 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001622 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 /* If we need update frag list, we are in troubles.
1626 * Certainly, it possible to add an offset to skb data,
1627 * but taking into account that pulling is expected to
1628 * be very rare operation, it is worth to fight against
1629 * further bloating skb head and crucify ourselves here instead.
1630 * Pure masohism, indeed. 8)8)
1631 */
1632 if (eat) {
1633 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1634 struct sk_buff *clone = NULL;
1635 struct sk_buff *insp = NULL;
1636
1637 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001638 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (list->len <= eat) {
1641 /* Eaten as whole. */
1642 eat -= list->len;
1643 list = list->next;
1644 insp = list;
1645 } else {
1646 /* Eaten partially. */
1647
1648 if (skb_shared(list)) {
1649 /* Sucks! We need to fork list. :-( */
1650 clone = skb_clone(list, GFP_ATOMIC);
1651 if (!clone)
1652 return NULL;
1653 insp = list->next;
1654 list = clone;
1655 } else {
1656 /* This may be pulled without
1657 * problems. */
1658 insp = list;
1659 }
1660 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001661 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return NULL;
1663 }
1664 break;
1665 }
1666 } while (eat);
1667
1668 /* Free pulled out fragments. */
1669 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1670 skb_shinfo(skb)->frag_list = list->next;
1671 kfree_skb(list);
1672 }
1673 /* And insert new clone at head. */
1674 if (clone) {
1675 clone->next = list;
1676 skb_shinfo(skb)->frag_list = clone;
1677 }
1678 }
1679 /* Success! Now we may commit changes to skb data. */
1680
1681pull_pages:
1682 eat = delta;
1683 k = 0;
1684 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001685 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1686
1687 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001688 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001689 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 } else {
1691 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1692 if (eat) {
1693 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001694 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001695 if (!i)
1696 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 eat = 0;
1698 }
1699 k++;
1700 }
1701 }
1702 skb_shinfo(skb)->nr_frags = k;
1703
linzhang3ccc6c62017-07-17 17:25:02 +08001704end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 skb->tail += delta;
1706 skb->data_len -= delta;
1707
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001708 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001710EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Eric Dumazet22019b12011-07-29 18:37:31 +00001712/**
1713 * skb_copy_bits - copy bits from skb to kernel buffer
1714 * @skb: source skb
1715 * @offset: offset in source
1716 * @to: destination buffer
1717 * @len: number of bytes to copy
1718 *
1719 * Copy the specified number of bytes from the source skb to the
1720 * destination buffer.
1721 *
1722 * CAUTION ! :
1723 * If its prototype is ever changed,
1724 * check arch/{*}/net/{*}.S files,
1725 * since it is called from BPF assembly code.
1726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1728{
David S. Miller1a028e52007-04-27 15:21:23 -07001729 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001730 struct sk_buff *frag_iter;
1731 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (offset > (int)skb->len - len)
1734 goto fault;
1735
1736 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001737 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (copy > len)
1739 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001740 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if ((len -= copy) == 0)
1742 return 0;
1743 offset += copy;
1744 to += copy;
1745 }
1746
1747 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001748 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001749 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001751 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001752
Eric Dumazet51c56b02012-04-05 11:35:15 +02001753 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if ((copy = end - offset) > 0) {
1755 u8 *vaddr;
1756
1757 if (copy > len)
1758 copy = len;
1759
Eric Dumazet51c56b02012-04-05 11:35:15 +02001760 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001762 vaddr + f->page_offset + offset - start,
1763 copy);
1764 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if ((len -= copy) == 0)
1767 return 0;
1768 offset += copy;
1769 to += copy;
1770 }
David S. Miller1a028e52007-04-27 15:21:23 -07001771 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
David S. Millerfbb398a2009-06-09 00:18:59 -07001774 skb_walk_frags(skb, frag_iter) {
1775 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
David S. Millerfbb398a2009-06-09 00:18:59 -07001777 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
David S. Millerfbb398a2009-06-09 00:18:59 -07001779 end = start + frag_iter->len;
1780 if ((copy = end - offset) > 0) {
1781 if (copy > len)
1782 copy = len;
1783 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1784 goto fault;
1785 if ((len -= copy) == 0)
1786 return 0;
1787 offset += copy;
1788 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001790 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Shirley Maa6686f22011-07-06 12:22:12 +00001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (!len)
1794 return 0;
1795
1796fault:
1797 return -EFAULT;
1798}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001799EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jens Axboe9c55e012007-11-06 23:30:13 -08001801/*
1802 * Callback from splice_to_pipe(), if we need to release some pages
1803 * at the end of the spd in case we error'ed out in filling the pipe.
1804 */
1805static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1806{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001807 put_page(spd->pages[i]);
1808}
Jens Axboe9c55e012007-11-06 23:30:13 -08001809
David S. Millera108d5f2012-04-23 23:06:11 -04001810static struct page *linear_to_page(struct page *page, unsigned int *len,
1811 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001812 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001813{
Eric Dumazet5640f762012-09-23 23:04:42 +00001814 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001815
Eric Dumazet5640f762012-09-23 23:04:42 +00001816 if (!sk_page_frag_refill(sk, pfrag))
1817 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001818
Eric Dumazet5640f762012-09-23 23:04:42 +00001819 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001820
Eric Dumazet5640f762012-09-23 23:04:42 +00001821 memcpy(page_address(pfrag->page) + pfrag->offset,
1822 page_address(page) + *offset, *len);
1823 *offset = pfrag->offset;
1824 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001825
Eric Dumazet5640f762012-09-23 23:04:42 +00001826 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001827}
1828
Eric Dumazet41c73a02012-04-22 12:26:16 +00001829static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1830 struct page *page,
1831 unsigned int offset)
1832{
1833 return spd->nr_pages &&
1834 spd->pages[spd->nr_pages - 1] == page &&
1835 (spd->partial[spd->nr_pages - 1].offset +
1836 spd->partial[spd->nr_pages - 1].len == offset);
1837}
1838
Jens Axboe9c55e012007-11-06 23:30:13 -08001839/*
1840 * Fill page/offset/length into spd, if it can hold more pages.
1841 */
David S. Millera108d5f2012-04-23 23:06:11 -04001842static bool spd_fill_page(struct splice_pipe_desc *spd,
1843 struct pipe_inode_info *pipe, struct page *page,
1844 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001845 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001846 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001847{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001848 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001849 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001850
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001851 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001852 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001853 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001854 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001855 }
1856 if (spd_can_coalesce(spd, page, offset)) {
1857 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001858 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001859 }
1860 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001861 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001862 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001864 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001865
David S. Millera108d5f2012-04-23 23:06:11 -04001866 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001867}
1868
David S. Millera108d5f2012-04-23 23:06:11 -04001869static bool __splice_segment(struct page *page, unsigned int poff,
1870 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001871 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001872 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001873 struct sock *sk,
1874 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001875{
1876 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001877 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001878
1879 /* skip this segment if already processed */
1880 if (*off >= plen) {
1881 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001882 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001883 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001884
Octavian Purdila2870c432008-07-15 00:49:11 -07001885 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001886 poff += *off;
1887 plen -= *off;
1888 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001889
Eric Dumazet18aafc62013-01-11 14:46:37 +00001890 do {
1891 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001892
Eric Dumazet18aafc62013-01-11 14:46:37 +00001893 if (spd_fill_page(spd, pipe, page, &flen, poff,
1894 linear, sk))
1895 return true;
1896 poff += flen;
1897 plen -= flen;
1898 *len -= flen;
1899 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001900
David S. Millera108d5f2012-04-23 23:06:11 -04001901 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001902}
1903
1904/*
David S. Millera108d5f2012-04-23 23:06:11 -04001905 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001906 * pipe is full or if we already spliced the requested length.
1907 */
David S. Millera108d5f2012-04-23 23:06:11 -04001908static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1909 unsigned int *offset, unsigned int *len,
1910 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001911{
1912 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08001913 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07001914
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001915 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001916 * If skb->head_frag is set, this 'linear' part is backed by a
1917 * fragment, and if the head is not shared with any clones then
1918 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001919 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001920 if (__splice_segment(virt_to_page(skb->data),
1921 (unsigned long) skb->data & (PAGE_SIZE - 1),
1922 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001923 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001924 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001925 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001926 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001927
1928 /*
1929 * then map the fragments
1930 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001931 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1932 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1933
Ian Campbellea2ab692011-08-22 23:44:58 +00001934 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001935 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001936 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001937 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001938 }
1939
Tom Herbertfa9835e2016-03-07 14:11:04 -08001940 skb_walk_frags(skb, iter) {
1941 if (*offset >= iter->len) {
1942 *offset -= iter->len;
1943 continue;
1944 }
1945 /* __skb_splice_bits() only fails if the output has no room
1946 * left, so no point in going over the frag_list for the error
1947 * case.
1948 */
1949 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1950 return true;
1951 }
1952
David S. Millera108d5f2012-04-23 23:06:11 -04001953 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001954}
1955
1956/*
1957 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08001958 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08001959 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001960int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001961 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04001962 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08001963{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001964 struct partial_page partial[MAX_SKB_FRAGS];
1965 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001966 struct splice_pipe_desc spd = {
1967 .pages = pages,
1968 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001969 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001970 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001971 .spd_release = sock_spd_release,
1972 };
Jens Axboe35f3d142010-05-20 10:43:18 +02001973 int ret = 0;
1974
Tom Herbertfa9835e2016-03-07 14:11:04 -08001975 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001976
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001977 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04001978 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001979
Jens Axboe35f3d142010-05-20 10:43:18 +02001980 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001981}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001982EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001983
Herbert Xu357b40a2005-04-19 22:30:14 -07001984/**
1985 * skb_store_bits - store bits from kernel buffer to skb
1986 * @skb: destination buffer
1987 * @offset: offset in destination
1988 * @from: source buffer
1989 * @len: number of bytes to copy
1990 *
1991 * Copy the specified number of bytes from the source buffer to the
1992 * destination skb. This function handles all the messy bits of
1993 * traversing fragment lists and such.
1994 */
1995
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001996int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001997{
David S. Miller1a028e52007-04-27 15:21:23 -07001998 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001999 struct sk_buff *frag_iter;
2000 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002001
2002 if (offset > (int)skb->len - len)
2003 goto fault;
2004
David S. Miller1a028e52007-04-27 15:21:23 -07002005 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002006 if (copy > len)
2007 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002008 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002009 if ((len -= copy) == 0)
2010 return 0;
2011 offset += copy;
2012 from += copy;
2013 }
2014
2015 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2016 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002017 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002018
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002019 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002020
Eric Dumazet9e903e02011-10-18 21:00:24 +00002021 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002022 if ((copy = end - offset) > 0) {
2023 u8 *vaddr;
2024
2025 if (copy > len)
2026 copy = len;
2027
Eric Dumazet51c56b02012-04-05 11:35:15 +02002028 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002029 memcpy(vaddr + frag->page_offset + offset - start,
2030 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002031 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002032
2033 if ((len -= copy) == 0)
2034 return 0;
2035 offset += copy;
2036 from += copy;
2037 }
David S. Miller1a028e52007-04-27 15:21:23 -07002038 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002039 }
2040
David S. Millerfbb398a2009-06-09 00:18:59 -07002041 skb_walk_frags(skb, frag_iter) {
2042 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002043
David S. Millerfbb398a2009-06-09 00:18:59 -07002044 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002045
David S. Millerfbb398a2009-06-09 00:18:59 -07002046 end = start + frag_iter->len;
2047 if ((copy = end - offset) > 0) {
2048 if (copy > len)
2049 copy = len;
2050 if (skb_store_bits(frag_iter, offset - start,
2051 from, copy))
2052 goto fault;
2053 if ((len -= copy) == 0)
2054 return 0;
2055 offset += copy;
2056 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002057 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002058 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002059 }
2060 if (!len)
2061 return 0;
2062
2063fault:
2064 return -EFAULT;
2065}
Herbert Xu357b40a2005-04-19 22:30:14 -07002066EXPORT_SYMBOL(skb_store_bits);
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002069__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2070 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
David S. Miller1a028e52007-04-27 15:21:23 -07002072 int start = skb_headlen(skb);
2073 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002074 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int pos = 0;
2076
2077 /* Checksum header. */
2078 if (copy > 0) {
2079 if (copy > len)
2080 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002081 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if ((len -= copy) == 0)
2083 return csum;
2084 offset += copy;
2085 pos = copy;
2086 }
2087
2088 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002089 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002090 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002092 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002093
Eric Dumazet51c56b02012-04-05 11:35:15 +02002094 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002096 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if (copy > len)
2100 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002101 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002102 csum2 = ops->update(vaddr + frag->page_offset +
2103 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002104 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002105 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!(len -= copy))
2107 return csum;
2108 offset += copy;
2109 pos += copy;
2110 }
David S. Miller1a028e52007-04-27 15:21:23 -07002111 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113
David S. Millerfbb398a2009-06-09 00:18:59 -07002114 skb_walk_frags(skb, frag_iter) {
2115 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
David S. Millerfbb398a2009-06-09 00:18:59 -07002117 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
David S. Millerfbb398a2009-06-09 00:18:59 -07002119 end = start + frag_iter->len;
2120 if ((copy = end - offset) > 0) {
2121 __wsum csum2;
2122 if (copy > len)
2123 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002124 csum2 = __skb_checksum(frag_iter, offset - start,
2125 copy, 0, ops);
2126 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002127 if ((len -= copy) == 0)
2128 return csum;
2129 offset += copy;
2130 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002132 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002134 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 return csum;
2137}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002138EXPORT_SYMBOL(__skb_checksum);
2139
2140__wsum skb_checksum(const struct sk_buff *skb, int offset,
2141 int len, __wsum csum)
2142{
2143 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002144 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002145 .combine = csum_block_add_ext,
2146 };
2147
2148 return __skb_checksum(skb, offset, len, csum, &ops);
2149}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002150EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152/* Both of above in one bottle. */
2153
Al Viro81d776622006-11-14 21:37:33 -08002154__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2155 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
David S. Miller1a028e52007-04-27 15:21:23 -07002157 int start = skb_headlen(skb);
2158 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002159 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int pos = 0;
2161
2162 /* Copy header. */
2163 if (copy > 0) {
2164 if (copy > len)
2165 copy = len;
2166 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2167 copy, csum);
2168 if ((len -= copy) == 0)
2169 return csum;
2170 offset += copy;
2171 to += copy;
2172 pos = copy;
2173 }
2174
2175 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002176 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002178 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002179
Eric Dumazet9e903e02011-10-18 21:00:24 +00002180 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002182 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 u8 *vaddr;
2184 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2185
2186 if (copy > len)
2187 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002188 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002190 frag->page_offset +
2191 offset - start, to,
2192 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002193 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 csum = csum_block_add(csum, csum2, pos);
2195 if (!(len -= copy))
2196 return csum;
2197 offset += copy;
2198 to += copy;
2199 pos += copy;
2200 }
David S. Miller1a028e52007-04-27 15:21:23 -07002201 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203
David S. Millerfbb398a2009-06-09 00:18:59 -07002204 skb_walk_frags(skb, frag_iter) {
2205 __wsum csum2;
2206 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
David S. Millerfbb398a2009-06-09 00:18:59 -07002208 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
David S. Millerfbb398a2009-06-09 00:18:59 -07002210 end = start + frag_iter->len;
2211 if ((copy = end - offset) > 0) {
2212 if (copy > len)
2213 copy = len;
2214 csum2 = skb_copy_and_csum_bits(frag_iter,
2215 offset - start,
2216 to, copy, 0);
2217 csum = csum_block_add(csum, csum2, pos);
2218 if ((len -= copy) == 0)
2219 return csum;
2220 offset += copy;
2221 to += copy;
2222 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002224 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002226 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return csum;
2228}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002229EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Davide Caratti96178132017-05-18 15:44:37 +02002231static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2232{
2233 net_warn_ratelimited(
2234 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2235 __func__);
2236 return 0;
2237}
2238
2239static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2240 int offset, int len)
2241{
2242 net_warn_ratelimited(
2243 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2244 __func__);
2245 return 0;
2246}
2247
2248static const struct skb_checksum_ops default_crc32c_ops = {
2249 .update = warn_crc32c_csum_update,
2250 .combine = warn_crc32c_csum_combine,
2251};
2252
2253const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2254 &default_crc32c_ops;
2255EXPORT_SYMBOL(crc32c_csum_stub);
2256
Thomas Grafaf2806f2013-12-13 15:22:17 +01002257 /**
2258 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2259 * @from: source buffer
2260 *
2261 * Calculates the amount of linear headroom needed in the 'to' skb passed
2262 * into skb_zerocopy().
2263 */
2264unsigned int
2265skb_zerocopy_headlen(const struct sk_buff *from)
2266{
2267 unsigned int hlen = 0;
2268
2269 if (!from->head_frag ||
2270 skb_headlen(from) < L1_CACHE_BYTES ||
2271 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2272 hlen = skb_headlen(from);
2273
2274 if (skb_has_frag_list(from))
2275 hlen = from->len;
2276
2277 return hlen;
2278}
2279EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2280
2281/**
2282 * skb_zerocopy - Zero copy skb to skb
2283 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002284 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002285 * @len: number of bytes to copy from source buffer
2286 * @hlen: size of linear headroom in destination buffer
2287 *
2288 * Copies up to `len` bytes from `from` to `to` by creating references
2289 * to the frags in the source buffer.
2290 *
2291 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2292 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002293 *
2294 * Return value:
2295 * 0: everything is OK
2296 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2297 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002298 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002299int
2300skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002301{
2302 int i, j = 0;
2303 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002304 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002305 struct page *page;
2306 unsigned int offset;
2307
2308 BUG_ON(!from->head_frag && !hlen);
2309
2310 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002311 if (len <= skb_tailroom(to))
2312 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002313
2314 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002315 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2316 if (unlikely(ret))
2317 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002318 len -= hlen;
2319 } else {
2320 plen = min_t(int, skb_headlen(from), len);
2321 if (plen) {
2322 page = virt_to_head_page(from->head);
2323 offset = from->data - (unsigned char *)page_address(page);
2324 __skb_fill_page_desc(to, 0, page, offset, plen);
2325 get_page(page);
2326 j = 1;
2327 len -= plen;
2328 }
2329 }
2330
2331 to->truesize += len + plen;
2332 to->len += len + plen;
2333 to->data_len += len + plen;
2334
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002335 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2336 skb_tx_error(from);
2337 return -ENOMEM;
2338 }
2339
Thomas Grafaf2806f2013-12-13 15:22:17 +01002340 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2341 if (!len)
2342 break;
2343 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2344 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2345 len -= skb_shinfo(to)->frags[j].size;
2346 skb_frag_ref(to, j);
2347 j++;
2348 }
2349 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002350
2351 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002352}
2353EXPORT_SYMBOL_GPL(skb_zerocopy);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2356{
Al Virod3bc23e2006-11-14 21:24:49 -08002357 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 long csstart;
2359
Patrick McHardy84fa7932006-08-29 16:44:56 -07002360 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002361 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 else
2363 csstart = skb_headlen(skb);
2364
Kris Katterjohn09a62662006-01-08 22:24:28 -08002365 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002367 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 csum = 0;
2370 if (csstart != skb->len)
2371 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2372 skb->len - csstart, 0);
2373
Patrick McHardy84fa7932006-08-29 16:44:56 -07002374 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002375 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Al Virod3bc23e2006-11-14 21:24:49 -08002377 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002380EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382/**
2383 * skb_dequeue - remove from the head of the queue
2384 * @list: list to dequeue from
2385 *
2386 * Remove the head of the list. The list lock is taken so the function
2387 * may be used safely with other locking list functions. The head item is
2388 * returned or %NULL if the list is empty.
2389 */
2390
2391struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2392{
2393 unsigned long flags;
2394 struct sk_buff *result;
2395
2396 spin_lock_irqsave(&list->lock, flags);
2397 result = __skb_dequeue(list);
2398 spin_unlock_irqrestore(&list->lock, flags);
2399 return result;
2400}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002401EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403/**
2404 * skb_dequeue_tail - remove from the tail of the queue
2405 * @list: list to dequeue from
2406 *
2407 * Remove the tail of the list. The list lock is taken so the function
2408 * may be used safely with other locking list functions. The tail item is
2409 * returned or %NULL if the list is empty.
2410 */
2411struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2412{
2413 unsigned long flags;
2414 struct sk_buff *result;
2415
2416 spin_lock_irqsave(&list->lock, flags);
2417 result = __skb_dequeue_tail(list);
2418 spin_unlock_irqrestore(&list->lock, flags);
2419 return result;
2420}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002421EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423/**
2424 * skb_queue_purge - empty a list
2425 * @list: list to empty
2426 *
2427 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2428 * the list and one reference dropped. This function takes the list
2429 * lock and is atomic with respect to other list locking functions.
2430 */
2431void skb_queue_purge(struct sk_buff_head *list)
2432{
2433 struct sk_buff *skb;
2434 while ((skb = skb_dequeue(list)) != NULL)
2435 kfree_skb(skb);
2436}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002437EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002440 * skb_rbtree_purge - empty a skb rbtree
2441 * @root: root of the rbtree to empty
2442 *
2443 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2444 * the list and one reference dropped. This function does not take
2445 * any lock. Synchronization should be handled by the caller (e.g., TCP
2446 * out-of-order queue is protected by the socket lock).
2447 */
2448void skb_rbtree_purge(struct rb_root *root)
2449{
2450 struct sk_buff *skb, *next;
2451
2452 rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
2453 kfree_skb(skb);
2454
2455 *root = RB_ROOT;
2456}
2457
2458/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * skb_queue_head - queue a buffer at the list head
2460 * @list: list to use
2461 * @newsk: buffer to queue
2462 *
2463 * Queue a buffer at the start of the list. This function takes the
2464 * list lock and can be used safely with other locking &sk_buff functions
2465 * safely.
2466 *
2467 * A buffer cannot be placed on two lists at the same time.
2468 */
2469void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2470{
2471 unsigned long flags;
2472
2473 spin_lock_irqsave(&list->lock, flags);
2474 __skb_queue_head(list, newsk);
2475 spin_unlock_irqrestore(&list->lock, flags);
2476}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002477EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479/**
2480 * skb_queue_tail - queue a buffer at the list tail
2481 * @list: list to use
2482 * @newsk: buffer to queue
2483 *
2484 * Queue a buffer at the tail of the list. This function takes the
2485 * list lock and can be used safely with other locking &sk_buff functions
2486 * safely.
2487 *
2488 * A buffer cannot be placed on two lists at the same time.
2489 */
2490void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2491{
2492 unsigned long flags;
2493
2494 spin_lock_irqsave(&list->lock, flags);
2495 __skb_queue_tail(list, newsk);
2496 spin_unlock_irqrestore(&list->lock, flags);
2497}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002498EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/**
2501 * skb_unlink - remove a buffer from a list
2502 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002503 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 *
David S. Miller8728b832005-08-09 19:25:21 -07002505 * Remove a packet from a list. The list locks are taken and this
2506 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 *
David S. Miller8728b832005-08-09 19:25:21 -07002508 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
David S. Miller8728b832005-08-09 19:25:21 -07002510void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
David S. Miller8728b832005-08-09 19:25:21 -07002512 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
David S. Miller8728b832005-08-09 19:25:21 -07002514 spin_lock_irqsave(&list->lock, flags);
2515 __skb_unlink(skb, list);
2516 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002518EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/**
2521 * skb_append - append a buffer
2522 * @old: buffer to insert after
2523 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002524 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 *
2526 * Place a packet after a given packet in a list. The list locks are taken
2527 * and this function is atomic with respect to other list locked calls.
2528 * A buffer cannot be placed on two lists at the same time.
2529 */
David S. Miller8728b832005-08-09 19:25:21 -07002530void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 unsigned long flags;
2533
David S. Miller8728b832005-08-09 19:25:21 -07002534 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002535 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002536 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002538EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540/**
2541 * skb_insert - insert a buffer
2542 * @old: buffer to insert before
2543 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002544 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 *
David S. Miller8728b832005-08-09 19:25:21 -07002546 * Place a packet before a given packet in a list. The list locks are
2547 * taken and this function is atomic with respect to other list locked
2548 * calls.
2549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 * A buffer cannot be placed on two lists at the same time.
2551 */
David S. Miller8728b832005-08-09 19:25:21 -07002552void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 unsigned long flags;
2555
David S. Miller8728b832005-08-09 19:25:21 -07002556 spin_lock_irqsave(&list->lock, flags);
2557 __skb_insert(newsk, old->prev, old, list);
2558 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002560EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562static inline void skb_split_inside_header(struct sk_buff *skb,
2563 struct sk_buff* skb1,
2564 const u32 len, const int pos)
2565{
2566 int i;
2567
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002568 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2569 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /* And move data appendix as is. */
2571 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2572 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2573
2574 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2575 skb_shinfo(skb)->nr_frags = 0;
2576 skb1->data_len = skb->data_len;
2577 skb1->len += skb1->data_len;
2578 skb->data_len = 0;
2579 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002580 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
2583static inline void skb_split_no_header(struct sk_buff *skb,
2584 struct sk_buff* skb1,
2585 const u32 len, int pos)
2586{
2587 int i, k = 0;
2588 const int nfrags = skb_shinfo(skb)->nr_frags;
2589
2590 skb_shinfo(skb)->nr_frags = 0;
2591 skb1->len = skb1->data_len = skb->len - len;
2592 skb->len = len;
2593 skb->data_len = len - pos;
2594
2595 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002596 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 if (pos + size > len) {
2599 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2600
2601 if (pos < len) {
2602 /* Split frag.
2603 * We have two variants in this case:
2604 * 1. Move all the frag to the second
2605 * part, if it is possible. F.e.
2606 * this approach is mandatory for TUX,
2607 * where splitting is expensive.
2608 * 2. Split is accurately. We make this.
2609 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002610 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002612 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2613 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 skb_shinfo(skb)->nr_frags++;
2615 }
2616 k++;
2617 } else
2618 skb_shinfo(skb)->nr_frags++;
2619 pos += size;
2620 }
2621 skb_shinfo(skb1)->nr_frags = k;
2622}
2623
2624/**
2625 * skb_split - Split fragmented skb to two parts at length len.
2626 * @skb: the buffer to split
2627 * @skb1: the buffer to receive the second part
2628 * @len: new length for skb
2629 */
2630void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2631{
2632 int pos = skb_headlen(skb);
2633
Willem de Bruijnfff88032017-06-08 11:35:03 -04002634 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2635 SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (len < pos) /* Split line is inside header. */
2637 skb_split_inside_header(skb, skb1, len, pos);
2638 else /* Second chunk has no header, nothing to copy. */
2639 skb_split_no_header(skb, skb1, len, pos);
2640}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002641EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002643/* Shifting from/to a cloned skb is a no-go.
2644 *
2645 * Caller cannot keep skb_shinfo related pointers past calling here!
2646 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002647static int skb_prepare_for_shift(struct sk_buff *skb)
2648{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002649 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002650}
2651
2652/**
2653 * skb_shift - Shifts paged data partially from skb to another
2654 * @tgt: buffer into which tail data gets added
2655 * @skb: buffer from which the paged data comes from
2656 * @shiftlen: shift up to this many bytes
2657 *
2658 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002659 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002660 * It's up to caller to free skb if everything was shifted.
2661 *
2662 * If @tgt runs out of frags, the whole operation is aborted.
2663 *
2664 * Skb cannot include anything else but paged data while tgt is allowed
2665 * to have non-paged data as well.
2666 *
2667 * TODO: full sized shift could be optimized but that would need
2668 * specialized skb free'er to handle frags without up-to-date nr_frags.
2669 */
2670int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2671{
2672 int from, to, merge, todo;
2673 struct skb_frag_struct *fragfrom, *fragto;
2674
2675 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08002676
2677 if (skb_headlen(skb))
2678 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002679
2680 todo = shiftlen;
2681 from = 0;
2682 to = skb_shinfo(tgt)->nr_frags;
2683 fragfrom = &skb_shinfo(skb)->frags[from];
2684
2685 /* Actual merge is delayed until the point when we know we can
2686 * commit all, so that we don't have to undo partial changes
2687 */
2688 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002689 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2690 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002691 merge = -1;
2692 } else {
2693 merge = to - 1;
2694
Eric Dumazet9e903e02011-10-18 21:00:24 +00002695 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002696 if (todo < 0) {
2697 if (skb_prepare_for_shift(skb) ||
2698 skb_prepare_for_shift(tgt))
2699 return 0;
2700
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002701 /* All previous frag pointers might be stale! */
2702 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002703 fragto = &skb_shinfo(tgt)->frags[merge];
2704
Eric Dumazet9e903e02011-10-18 21:00:24 +00002705 skb_frag_size_add(fragto, shiftlen);
2706 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002707 fragfrom->page_offset += shiftlen;
2708
2709 goto onlymerged;
2710 }
2711
2712 from++;
2713 }
2714
2715 /* Skip full, not-fitting skb to avoid expensive operations */
2716 if ((shiftlen == skb->len) &&
2717 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2718 return 0;
2719
2720 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2721 return 0;
2722
2723 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2724 if (to == MAX_SKB_FRAGS)
2725 return 0;
2726
2727 fragfrom = &skb_shinfo(skb)->frags[from];
2728 fragto = &skb_shinfo(tgt)->frags[to];
2729
Eric Dumazet9e903e02011-10-18 21:00:24 +00002730 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002731 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002732 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002733 from++;
2734 to++;
2735
2736 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002737 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002738 fragto->page = fragfrom->page;
2739 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002740 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002741
2742 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002743 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002744 todo = 0;
2745
2746 to++;
2747 break;
2748 }
2749 }
2750
2751 /* Ready to "commit" this state change to tgt */
2752 skb_shinfo(tgt)->nr_frags = to;
2753
2754 if (merge >= 0) {
2755 fragfrom = &skb_shinfo(skb)->frags[0];
2756 fragto = &skb_shinfo(tgt)->frags[merge];
2757
Eric Dumazet9e903e02011-10-18 21:00:24 +00002758 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002759 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002760 }
2761
2762 /* Reposition in the original skb */
2763 to = 0;
2764 while (from < skb_shinfo(skb)->nr_frags)
2765 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2766 skb_shinfo(skb)->nr_frags = to;
2767
2768 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2769
2770onlymerged:
2771 /* Most likely the tgt won't ever need its checksum anymore, skb on
2772 * the other hand might need it if it needs to be resent
2773 */
2774 tgt->ip_summed = CHECKSUM_PARTIAL;
2775 skb->ip_summed = CHECKSUM_PARTIAL;
2776
2777 /* Yak, is it really working this way? Some helper please? */
2778 skb->len -= shiftlen;
2779 skb->data_len -= shiftlen;
2780 skb->truesize -= shiftlen;
2781 tgt->len += shiftlen;
2782 tgt->data_len += shiftlen;
2783 tgt->truesize += shiftlen;
2784
2785 return shiftlen;
2786}
2787
Thomas Graf677e90e2005-06-23 20:59:51 -07002788/**
2789 * skb_prepare_seq_read - Prepare a sequential read of skb data
2790 * @skb: the buffer to read
2791 * @from: lower offset of data to be read
2792 * @to: upper offset of data to be read
2793 * @st: state variable
2794 *
2795 * Initializes the specified state variable. Must be called before
2796 * invoking skb_seq_read() for the first time.
2797 */
2798void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2799 unsigned int to, struct skb_seq_state *st)
2800{
2801 st->lower_offset = from;
2802 st->upper_offset = to;
2803 st->root_skb = st->cur_skb = skb;
2804 st->frag_idx = st->stepped_offset = 0;
2805 st->frag_data = NULL;
2806}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002807EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002808
2809/**
2810 * skb_seq_read - Sequentially read skb data
2811 * @consumed: number of bytes consumed by the caller so far
2812 * @data: destination pointer for data to be returned
2813 * @st: state variable
2814 *
Mathias Krausebc323832013-11-07 14:18:26 +01002815 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002816 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002817 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002818 * of the block or 0 if the end of the skb data or the upper
2819 * offset has been reached.
2820 *
2821 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002822 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002823 * of bytes already consumed and the next call to
2824 * skb_seq_read() will return the remaining part of the block.
2825 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002826 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002827 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002828 * reads of potentially non linear data.
2829 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002830 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002831 * at the moment, state->root_skb could be replaced with
2832 * a stack for this purpose.
2833 */
2834unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2835 struct skb_seq_state *st)
2836{
2837 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2838 skb_frag_t *frag;
2839
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002840 if (unlikely(abs_offset >= st->upper_offset)) {
2841 if (st->frag_data) {
2842 kunmap_atomic(st->frag_data);
2843 st->frag_data = NULL;
2844 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002845 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002846 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002847
2848next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002849 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002850
Thomas Chenault995b3372009-05-18 21:43:27 -07002851 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002852 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002853 return block_limit - abs_offset;
2854 }
2855
2856 if (st->frag_idx == 0 && !st->frag_data)
2857 st->stepped_offset += skb_headlen(st->cur_skb);
2858
2859 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2860 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002861 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002862
2863 if (abs_offset < block_limit) {
2864 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002865 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002866
2867 *data = (u8 *) st->frag_data + frag->page_offset +
2868 (abs_offset - st->stepped_offset);
2869
2870 return block_limit - abs_offset;
2871 }
2872
2873 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002874 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002875 st->frag_data = NULL;
2876 }
2877
2878 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002879 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002880 }
2881
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002882 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002883 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002884 st->frag_data = NULL;
2885 }
2886
David S. Miller21dc3302010-08-23 00:13:46 -07002887 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002888 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002889 st->frag_idx = 0;
2890 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002891 } else if (st->cur_skb->next) {
2892 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002893 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002894 goto next_skb;
2895 }
2896
2897 return 0;
2898}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002899EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002900
2901/**
2902 * skb_abort_seq_read - Abort a sequential read of skb data
2903 * @st: state variable
2904 *
2905 * Must be called if skb_seq_read() was not called until it
2906 * returned 0.
2907 */
2908void skb_abort_seq_read(struct skb_seq_state *st)
2909{
2910 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002911 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002912}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002913EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002914
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002915#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2916
2917static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2918 struct ts_config *conf,
2919 struct ts_state *state)
2920{
2921 return skb_seq_read(offset, text, TS_SKB_CB(state));
2922}
2923
2924static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2925{
2926 skb_abort_seq_read(TS_SKB_CB(state));
2927}
2928
2929/**
2930 * skb_find_text - Find a text pattern in skb data
2931 * @skb: the buffer to look in
2932 * @from: search offset
2933 * @to: search limit
2934 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002935 *
2936 * Finds a pattern in the skb data according to the specified
2937 * textsearch configuration. Use textsearch_next() to retrieve
2938 * subsequent occurrences of the pattern. Returns the offset
2939 * to the first occurrence or UINT_MAX if no match was found.
2940 */
2941unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002942 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002943{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002944 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002945 unsigned int ret;
2946
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002947 config->get_next_block = skb_ts_get_next_block;
2948 config->finish = skb_ts_finish;
2949
Bojan Prtvar059a2442015-02-22 11:46:35 +01002950 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002951
Bojan Prtvar059a2442015-02-22 11:46:35 +01002952 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002953 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002954}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002955EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002956
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002957/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002958 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002959 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002960 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002961 * @getfrag: call back function to be used for getting the user data
2962 * @from: pointer to user message iov
2963 * @length: length of the iov message
2964 *
2965 * Description: This procedure append the user data in the fragment part
2966 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2967 */
2968int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002969 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002970 int len, int odd, struct sk_buff *skb),
2971 void *from, int length)
2972{
Eric Dumazetb2111722012-12-28 06:06:37 +00002973 int frg_cnt = skb_shinfo(skb)->nr_frags;
2974 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002975 int offset = 0;
2976 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002977 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002978
2979 do {
2980 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002981 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002982 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002983
Eric Dumazetb2111722012-12-28 06:06:37 +00002984 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002985 return -ENOMEM;
2986
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002987 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002988 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002989
Eric Dumazetb2111722012-12-28 06:06:37 +00002990 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2991 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002992 if (ret < 0)
2993 return -EFAULT;
2994
2995 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002996 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2997 copy);
2998 frg_cnt++;
2999 pfrag->offset += copy;
3000 get_page(pfrag->page);
3001
3002 skb->truesize += copy;
Reshetova, Elena14afee42017-06-30 13:08:00 +03003003 refcount_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003004 skb->len += copy;
3005 skb->data_len += copy;
3006 offset += copy;
3007 length -= copy;
3008
3009 } while (length > 0);
3010
3011 return 0;
3012}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003013EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003014
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003015int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3016 int offset, size_t size)
3017{
3018 int i = skb_shinfo(skb)->nr_frags;
3019
3020 if (skb_can_coalesce(skb, i, page, offset)) {
3021 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3022 } else if (i < MAX_SKB_FRAGS) {
3023 get_page(page);
3024 skb_fill_page_desc(skb, i, page, offset, size);
3025 } else {
3026 return -EMSGSIZE;
3027 }
3028
3029 return 0;
3030}
3031EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3032
Herbert Xucbb042f92006-03-20 22:43:56 -08003033/**
3034 * skb_pull_rcsum - pull skb and update receive checksum
3035 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08003036 * @len: length of data pulled
3037 *
3038 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003039 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003040 * receive path processing instead of skb_pull unless you know
3041 * that the checksum difference is zero (e.g., a valid IP header)
3042 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08003043 */
Johannes Bergaf728682017-06-16 14:29:22 +02003044void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f92006-03-20 22:43:56 -08003045{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003046 unsigned char *data = skb->data;
3047
Herbert Xucbb042f92006-03-20 22:43:56 -08003048 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003049 __skb_pull(skb, len);
3050 skb_postpull_rcsum(skb, data, len);
3051 return skb->data;
Herbert Xucbb042f92006-03-20 22:43:56 -08003052}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003053EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3054
Herbert Xuf4c50d92006-06-22 03:02:40 -07003055/**
3056 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003057 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003058 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003059 *
3060 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003061 * a pointer to the first in a list of new skbs for the segments.
3062 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003063 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003064struct sk_buff *skb_segment(struct sk_buff *head_skb,
3065 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003066{
3067 struct sk_buff *segs = NULL;
3068 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003069 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003070 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3071 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3072 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003073 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003074 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003075 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003076 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003077 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003078 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003079 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003080 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003081 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003082 int err = -ENOMEM;
3083 int i = 0;
3084 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003085 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003086
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003087 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003088 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003089 if (unlikely(!proto))
3090 return ERR_PTR(-EINVAL);
3091
Alexander Duyck36c98382016-05-02 09:38:18 -07003092 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003093 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003094
Steffen Klassert07b26c92016-09-19 12:58:47 +02003095 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3096 if (!(features & NETIF_F_GSO_PARTIAL)) {
3097 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003098 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003099
3100 if (!list_skb ||
3101 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3102 goto normal;
3103
Ilan Tayari43170c42017-04-19 21:26:07 +03003104 /* If we get here then all the required
3105 * GSO features except frag_list are supported.
3106 * Try to split the SKB to multiple GSO SKBs
3107 * with no frag_list.
3108 * Currently we can do that only when the buffers don't
3109 * have a linear part and all the buffers except
3110 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003111 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003112 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003113 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003114 if (frag_len != iter->len && iter->next)
3115 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003116 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003117 goto normal;
3118
3119 len -= iter->len;
3120 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003121
3122 if (len != frag_len)
3123 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003124 }
3125
3126 /* GSO partial only requires that we trim off any excess that
3127 * doesn't fit into an MSS sized block, so take care of that
3128 * now.
3129 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003130 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003131 if (partial_segs > 1)
3132 mss *= partial_segs;
3133 else
3134 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003135 }
3136
Steffen Klassert07b26c92016-09-19 12:58:47 +02003137normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003138 headroom = skb_headroom(head_skb);
3139 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003140
3141 do {
3142 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003143 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003144 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003145 int size;
3146
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003147 if (unlikely(mss == GSO_BY_FRAGS)) {
3148 len = list_skb->len;
3149 } else {
3150 len = head_skb->len - offset;
3151 if (len > mss)
3152 len = mss;
3153 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003154
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003155 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003156 if (hsize < 0)
3157 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003158 if (hsize > len || !sg)
3159 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003160
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003161 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3162 (skb_headlen(list_skb) == len || sg)) {
3163 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003164
Herbert Xu9d8506c2013-11-21 11:10:04 -08003165 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003166 nfrags = skb_shinfo(list_skb)->nr_frags;
3167 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003168 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003169 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003170
3171 while (pos < offset + len) {
3172 BUG_ON(i >= nfrags);
3173
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003174 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003175 if (pos + size > offset + len)
3176 break;
3177
3178 i++;
3179 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003180 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003181 }
3182
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003183 nskb = skb_clone(list_skb, GFP_ATOMIC);
3184 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003185
3186 if (unlikely(!nskb))
3187 goto err;
3188
Herbert Xu9d8506c2013-11-21 11:10:04 -08003189 if (unlikely(pskb_trim(nskb, len))) {
3190 kfree_skb(nskb);
3191 goto err;
3192 }
3193
Alexander Duyckec47ea82012-05-04 14:26:56 +00003194 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003195 if (skb_cow_head(nskb, doffset + headroom)) {
3196 kfree_skb(nskb);
3197 goto err;
3198 }
3199
Alexander Duyckec47ea82012-05-04 14:26:56 +00003200 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003201 skb_release_head_state(nskb);
3202 __skb_push(nskb, doffset);
3203 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003204 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003205 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003206 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003207
3208 if (unlikely(!nskb))
3209 goto err;
3210
3211 skb_reserve(nskb, headroom);
3212 __skb_put(nskb, doffset);
3213 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003214
3215 if (segs)
3216 tail->next = nskb;
3217 else
3218 segs = nskb;
3219 tail = nskb;
3220
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003221 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003222
Eric Dumazet030737b2013-10-19 11:42:54 -07003223 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003224 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003225
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003226 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003227 nskb->data - tnl_hlen,
3228 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003229
Herbert Xu9d8506c2013-11-21 11:10:04 -08003230 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003231 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003232
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003233 if (!sg) {
3234 if (!nskb->remcsum_offload)
3235 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003236 SKB_GSO_CB(nskb)->csum =
3237 skb_copy_and_csum_bits(head_skb, offset,
3238 skb_put(nskb, len),
3239 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003240 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003241 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003242 continue;
3243 }
3244
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003245 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003246
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003247 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003248 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003249
Willem de Bruijnfff88032017-06-08 11:35:03 -04003250 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3251 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003252
Herbert Xu9d8506c2013-11-21 11:10:04 -08003253 while (pos < offset + len) {
3254 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003255 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003256
3257 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003258 nfrags = skb_shinfo(list_skb)->nr_frags;
3259 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003260 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003261
3262 BUG_ON(!nfrags);
3263
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003264 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003265 }
3266
3267 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3268 MAX_SKB_FRAGS)) {
3269 net_warn_ratelimited(
3270 "skb_segment: too many frags: %u %u\n",
3271 pos, mss);
3272 goto err;
3273 }
3274
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003275 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3276 goto err;
3277
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003278 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003279 __skb_frag_ref(nskb_frag);
3280 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003281
3282 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003283 nskb_frag->page_offset += offset - pos;
3284 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003285 }
3286
Herbert Xu89319d382008-12-15 23:26:06 -08003287 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003288
3289 if (pos + size <= offset + len) {
3290 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003291 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003292 pos += size;
3293 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003294 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003295 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003296 }
3297
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003298 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003299 }
3300
Herbert Xu89319d382008-12-15 23:26:06 -08003301skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003302 nskb->data_len = len - hsize;
3303 nskb->len += nskb->data_len;
3304 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003305
Simon Horman1cdbcb72013-05-19 15:46:49 +00003306perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003307 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003308 if (skb_has_shared_frag(nskb)) {
3309 err = __skb_linearize(nskb);
3310 if (err)
3311 goto err;
3312 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003313 if (!nskb->remcsum_offload)
3314 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003315 SKB_GSO_CB(nskb)->csum =
3316 skb_checksum(nskb, doffset,
3317 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003318 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003319 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003320 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003321 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003322
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003323 /* Some callers want to get the end of the list.
3324 * Put it in segs->prev to avoid walking the list.
3325 * (see validate_xmit_skb_list() for example)
3326 */
3327 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003328
Alexander Duyck802ab552016-04-10 21:45:03 -04003329 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003330 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003331 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003332 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003333
3334 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003335 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003336 type &= ~SKB_GSO_DODGY;
3337
3338 /* Update GSO info and prepare to start updating headers on
3339 * our way back down the stack of protocols.
3340 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003341 for (iter = segs; iter; iter = iter->next) {
3342 skb_shinfo(iter)->gso_size = gso_size;
3343 skb_shinfo(iter)->gso_segs = partial_segs;
3344 skb_shinfo(iter)->gso_type = type;
3345 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3346 }
3347
3348 if (tail->len - doffset <= gso_size)
3349 skb_shinfo(tail)->gso_size = 0;
3350 else if (tail != segs)
3351 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003352 }
3353
Toshiaki Makita432c8562014-10-27 10:30:51 -07003354 /* Following permits correct backpressure, for protocols
3355 * using skb_set_owner_w().
3356 * Idea is to tranfert ownership from head_skb to last segment.
3357 */
3358 if (head_skb->destructor == sock_wfree) {
3359 swap(tail->truesize, head_skb->truesize);
3360 swap(tail->destructor, head_skb->destructor);
3361 swap(tail->sk, head_skb->sk);
3362 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003363 return segs;
3364
3365err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003366 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003367 return ERR_PTR(err);
3368}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003369EXPORT_SYMBOL_GPL(skb_segment);
3370
Herbert Xu71d93b32008-12-15 23:42:33 -08003371int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3372{
Eric Dumazet8a291112013-10-08 09:02:23 -07003373 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003374 unsigned int offset = skb_gro_offset(skb);
3375 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003376 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003377 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003378 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003379
Eric Dumazet8a291112013-10-08 09:02:23 -07003380 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003381 return -E2BIG;
3382
Eric Dumazet29e98242014-05-16 11:34:37 -07003383 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003384 pinfo = skb_shinfo(lp);
3385
3386 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003387 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003388 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003389 int i = skbinfo->nr_frags;
3390 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003391
Herbert Xu66e92fc2009-05-26 18:50:32 +00003392 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003393 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003394
Eric Dumazet8a291112013-10-08 09:02:23 -07003395 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003396 pinfo->nr_frags = nr_frags;
3397 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003398
Herbert Xu9aaa1562009-05-26 18:50:33 +00003399 frag = pinfo->frags + nr_frags;
3400 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003401 do {
3402 *--frag = *--frag2;
3403 } while (--i);
3404
3405 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003406 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003407
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003408 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003409 delta_truesize = skb->truesize -
3410 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003411
Herbert Xuf5572062009-01-14 20:40:03 -08003412 skb->truesize -= skb->data_len;
3413 skb->len -= skb->data_len;
3414 skb->data_len = 0;
3415
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003416 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003417 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003418 } else if (skb->head_frag) {
3419 int nr_frags = pinfo->nr_frags;
3420 skb_frag_t *frag = pinfo->frags + nr_frags;
3421 struct page *page = virt_to_head_page(skb->head);
3422 unsigned int first_size = headlen - offset;
3423 unsigned int first_offset;
3424
3425 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003426 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003427
3428 first_offset = skb->data -
3429 (unsigned char *)page_address(page) +
3430 offset;
3431
3432 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3433
3434 frag->page.p = page;
3435 frag->page_offset = first_offset;
3436 skb_frag_size_set(frag, first_size);
3437
3438 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3439 /* We dont need to clear skbinfo->nr_frags here */
3440
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003441 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003442 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3443 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003444 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003445
3446merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003447 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003448 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003449 unsigned int eat = offset - headlen;
3450
3451 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003452 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003453 skb->data_len -= eat;
3454 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003455 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003456 }
3457
Herbert Xu67147ba2009-05-26 18:50:22 +00003458 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003459
Eric Dumazet29e98242014-05-16 11:34:37 -07003460 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003461 skb_shinfo(p)->frag_list = skb;
3462 else
3463 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003464 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003465 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003466 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003467
Herbert Xu5d38a072009-01-04 16:13:40 -08003468done:
3469 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003470 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003471 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003472 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003473 if (lp != p) {
3474 lp->data_len += len;
3475 lp->truesize += delta_truesize;
3476 lp->len += len;
3477 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003478 NAPI_GRO_CB(skb)->same_flow = 1;
3479 return 0;
3480}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003481EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483void __init skb_init(void)
3484{
3485 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3486 sizeof(struct sk_buff),
3487 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003488 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003489 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003490 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003491 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003492 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003493 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003494 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
David S. Miller51c739d2007-10-30 21:29:29 -07003497static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003498__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3499 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003500{
David S. Miller1a028e52007-04-27 15:21:23 -07003501 int start = skb_headlen(skb);
3502 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003503 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003504 int elt = 0;
3505
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003506 if (unlikely(recursion_level >= 24))
3507 return -EMSGSIZE;
3508
David Howells716ea3a2007-04-02 20:19:53 -07003509 if (copy > 0) {
3510 if (copy > len)
3511 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003512 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003513 elt++;
3514 if ((len -= copy) == 0)
3515 return elt;
3516 offset += copy;
3517 }
3518
3519 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003520 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003521
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003522 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003523
Eric Dumazet9e903e02011-10-18 21:00:24 +00003524 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003525 if ((copy = end - offset) > 0) {
3526 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003527 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3528 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003529
3530 if (copy > len)
3531 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003532 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003533 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003534 elt++;
3535 if (!(len -= copy))
3536 return elt;
3537 offset += copy;
3538 }
David S. Miller1a028e52007-04-27 15:21:23 -07003539 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003540 }
3541
David S. Millerfbb398a2009-06-09 00:18:59 -07003542 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003543 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07003544
David S. Millerfbb398a2009-06-09 00:18:59 -07003545 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003546
David S. Millerfbb398a2009-06-09 00:18:59 -07003547 end = start + frag_iter->len;
3548 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003549 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3550 return -EMSGSIZE;
3551
David S. Millerfbb398a2009-06-09 00:18:59 -07003552 if (copy > len)
3553 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003554 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3555 copy, recursion_level + 1);
3556 if (unlikely(ret < 0))
3557 return ret;
3558 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07003559 if ((len -= copy) == 0)
3560 return elt;
3561 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003562 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003563 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003564 }
3565 BUG_ON(len);
3566 return elt;
3567}
3568
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003569/**
3570 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3571 * @skb: Socket buffer containing the buffers to be mapped
3572 * @sg: The scatter-gather list to map into
3573 * @offset: The offset into the buffer's contents to start mapping
3574 * @len: Length of buffer space to be mapped
3575 *
3576 * Fill the specified scatter-gather list with mappings/pointers into a
3577 * region of the buffer space attached to a socket buffer. Returns either
3578 * the number of scatterlist items used, or -EMSGSIZE if the contents
3579 * could not fit.
3580 */
3581int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3582{
3583 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3584
3585 if (nsg <= 0)
3586 return nsg;
3587
3588 sg_mark_end(&sg[nsg - 1]);
3589
3590 return nsg;
3591}
3592EXPORT_SYMBOL_GPL(skb_to_sgvec);
3593
Fan Du25a91d82014-01-18 09:54:23 +08003594/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3595 * sglist without mark the sg which contain last skb data as the end.
3596 * So the caller can mannipulate sg list as will when padding new data after
3597 * the first call without calling sg_unmark_end to expend sg list.
3598 *
3599 * Scenario to use skb_to_sgvec_nomark:
3600 * 1. sg_init_table
3601 * 2. skb_to_sgvec_nomark(payload1)
3602 * 3. skb_to_sgvec_nomark(payload2)
3603 *
3604 * This is equivalent to:
3605 * 1. sg_init_table
3606 * 2. skb_to_sgvec(payload1)
3607 * 3. sg_unmark_end
3608 * 4. skb_to_sgvec(payload2)
3609 *
3610 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3611 * is more preferable.
3612 */
3613int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3614 int offset, int len)
3615{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003616 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08003617}
3618EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3619
David S. Miller51c739d2007-10-30 21:29:29 -07003620
David S. Miller51c739d2007-10-30 21:29:29 -07003621
David Howells716ea3a2007-04-02 20:19:53 -07003622/**
3623 * skb_cow_data - Check that a socket buffer's data buffers are writable
3624 * @skb: The socket buffer to check.
3625 * @tailbits: Amount of trailing space to be added
3626 * @trailer: Returned pointer to the skb where the @tailbits space begins
3627 *
3628 * Make sure that the data buffers attached to a socket buffer are
3629 * writable. If they are not, private copies are made of the data buffers
3630 * and the socket buffer is set to use these instead.
3631 *
3632 * If @tailbits is given, make sure that there is space to write @tailbits
3633 * bytes of data beyond current end of socket buffer. @trailer will be
3634 * set to point to the skb in which this space begins.
3635 *
3636 * The number of scatterlist elements required to completely map the
3637 * COW'd and extended socket buffer will be returned.
3638 */
3639int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3640{
3641 int copyflag;
3642 int elt;
3643 struct sk_buff *skb1, **skb_p;
3644
3645 /* If skb is cloned or its head is paged, reallocate
3646 * head pulling out all the pages (pages are considered not writable
3647 * at the moment even if they are anonymous).
3648 */
3649 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3650 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3651 return -ENOMEM;
3652
3653 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003654 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003655 /* A little of trouble, not enough of space for trailer.
3656 * This should not happen, when stack is tuned to generate
3657 * good frames. OK, on miss we reallocate and reserve even more
3658 * space, 128 bytes is fair. */
3659
3660 if (skb_tailroom(skb) < tailbits &&
3661 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3662 return -ENOMEM;
3663
3664 /* Voila! */
3665 *trailer = skb;
3666 return 1;
3667 }
3668
3669 /* Misery. We are in troubles, going to mincer fragments... */
3670
3671 elt = 1;
3672 skb_p = &skb_shinfo(skb)->frag_list;
3673 copyflag = 0;
3674
3675 while ((skb1 = *skb_p) != NULL) {
3676 int ntail = 0;
3677
3678 /* The fragment is partially pulled by someone,
3679 * this can happen on input. Copy it and everything
3680 * after it. */
3681
3682 if (skb_shared(skb1))
3683 copyflag = 1;
3684
3685 /* If the skb is the last, worry about trailer. */
3686
3687 if (skb1->next == NULL && tailbits) {
3688 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003689 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003690 skb_tailroom(skb1) < tailbits)
3691 ntail = tailbits + 128;
3692 }
3693
3694 if (copyflag ||
3695 skb_cloned(skb1) ||
3696 ntail ||
3697 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003698 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003699 struct sk_buff *skb2;
3700
3701 /* Fuck, we are miserable poor guys... */
3702 if (ntail == 0)
3703 skb2 = skb_copy(skb1, GFP_ATOMIC);
3704 else
3705 skb2 = skb_copy_expand(skb1,
3706 skb_headroom(skb1),
3707 ntail,
3708 GFP_ATOMIC);
3709 if (unlikely(skb2 == NULL))
3710 return -ENOMEM;
3711
3712 if (skb1->sk)
3713 skb_set_owner_w(skb2, skb1->sk);
3714
3715 /* Looking around. Are we still alive?
3716 * OK, link new skb, drop old one */
3717
3718 skb2->next = skb1->next;
3719 *skb_p = skb2;
3720 kfree_skb(skb1);
3721 skb1 = skb2;
3722 }
3723 elt++;
3724 *trailer = skb1;
3725 skb_p = &skb1->next;
3726 }
3727
3728 return elt;
3729}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003730EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003731
Eric Dumazetb1faf562010-05-31 23:44:05 -07003732static void sock_rmem_free(struct sk_buff *skb)
3733{
3734 struct sock *sk = skb->sk;
3735
3736 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3737}
3738
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003739static void skb_set_err_queue(struct sk_buff *skb)
3740{
3741 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
3742 * So, it is safe to (mis)use it to mark skbs on the error queue.
3743 */
3744 skb->pkt_type = PACKET_OUTGOING;
3745 BUILD_BUG_ON(PACKET_OUTGOING == 0);
3746}
3747
Eric Dumazetb1faf562010-05-31 23:44:05 -07003748/*
3749 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3750 */
3751int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3752{
3753 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003754 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003755 return -ENOMEM;
3756
3757 skb_orphan(skb);
3758 skb->sk = sk;
3759 skb->destructor = sock_rmem_free;
3760 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04003761 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003762
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003763 /* before exiting rcu section, make sure dst is refcounted */
3764 skb_dst_force(skb);
3765
Eric Dumazetb1faf562010-05-31 23:44:05 -07003766 skb_queue_tail(&sk->sk_error_queue, skb);
3767 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003768 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003769 return 0;
3770}
3771EXPORT_SYMBOL(sock_queue_err_skb);
3772
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003773static bool is_icmp_err_skb(const struct sk_buff *skb)
3774{
3775 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
3776 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
3777}
3778
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003779struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3780{
3781 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003782 struct sk_buff *skb, *skb_next = NULL;
3783 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003784 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003785
Eric Dumazet997d5c32015-02-18 05:47:55 -08003786 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003787 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b257932017-06-02 12:38:22 -04003788 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003789 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b257932017-06-02 12:38:22 -04003790 if (icmp_next)
3791 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
3792 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08003793 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003794
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05003795 if (is_icmp_err_skb(skb) && !icmp_next)
3796 sk->sk_err = 0;
3797
3798 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003799 sk->sk_error_report(sk);
3800
3801 return skb;
3802}
3803EXPORT_SYMBOL(sock_dequeue_err_skb);
3804
Alexander Duyckcab41c42014-09-10 18:05:26 -04003805/**
3806 * skb_clone_sk - create clone of skb, and take reference to socket
3807 * @skb: the skb to clone
3808 *
3809 * This function creates a clone of a buffer that holds a reference on
3810 * sk_refcnt. Buffers created via this function are meant to be
3811 * returned using sock_queue_err_skb, or free via kfree_skb.
3812 *
3813 * When passing buffers allocated with this function to sock_queue_err_skb
3814 * it is necessary to wrap the call with sock_hold/sock_put in order to
3815 * prevent the socket from being released prior to being enqueued on
3816 * the sk_error_queue.
3817 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003818struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3819{
3820 struct sock *sk = skb->sk;
3821 struct sk_buff *clone;
3822
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003823 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04003824 return NULL;
3825
3826 clone = skb_clone(skb, GFP_ATOMIC);
3827 if (!clone) {
3828 sock_put(sk);
3829 return NULL;
3830 }
3831
3832 clone->sk = sk;
3833 clone->destructor = sock_efree;
3834
3835 return clone;
3836}
3837EXPORT_SYMBOL(skb_clone_sk);
3838
Alexander Duyck37846ef2014-09-04 13:31:10 -04003839static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3840 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003841 int tstype,
3842 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003843{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003844 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003845 int err;
3846
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003847 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
3848
Patrick Ohlyac45f602009-02-12 05:03:37 +00003849 serr = SKB_EXT_ERR(skb);
3850 memset(serr, 0, sizeof(*serr));
3851 serr->ee.ee_errno = ENOMSG;
3852 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003853 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003854 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04003855 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003856 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003857 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003858 if (sk->sk_protocol == IPPROTO_TCP &&
3859 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003860 serr->ee.ee_data -= sk->sk_tskey;
3861 }
Eric Dumazet29030372010-05-29 00:20:48 -07003862
Patrick Ohlyac45f602009-02-12 05:03:37 +00003863 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003864
Patrick Ohlyac45f602009-02-12 05:03:37 +00003865 if (err)
3866 kfree_skb(skb);
3867}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003868
Willem de Bruijnb245be12015-01-30 13:29:32 -05003869static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3870{
3871 bool ret;
3872
3873 if (likely(sysctl_tstamp_allow_data || tsonly))
3874 return true;
3875
3876 read_lock_bh(&sk->sk_callback_lock);
3877 ret = sk->sk_socket && sk->sk_socket->file &&
3878 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3879 read_unlock_bh(&sk->sk_callback_lock);
3880 return ret;
3881}
3882
Alexander Duyck37846ef2014-09-04 13:31:10 -04003883void skb_complete_tx_timestamp(struct sk_buff *skb,
3884 struct skb_shared_hwtstamps *hwtstamps)
3885{
3886 struct sock *sk = skb->sk;
3887
Willem de Bruijnb245be12015-01-30 13:29:32 -05003888 if (!skb_may_tx_timestamp(sk, false))
3889 return;
3890
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003891 /* Take a reference to prevent skb_orphan() from freeing the socket,
3892 * but only if the socket refcount is not zero.
3893 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003894 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003895 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003896 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08003897 sock_put(sk);
3898 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003899}
3900EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3901
3902void __skb_tstamp_tx(struct sk_buff *orig_skb,
3903 struct skb_shared_hwtstamps *hwtstamps,
3904 struct sock *sk, int tstype)
3905{
3906 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003907 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003908
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003909 if (!sk)
3910 return;
3911
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02003912 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
3913 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
3914 return;
3915
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003916 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3917 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003918 return;
3919
Francis Yan1c885802016-11-27 23:07:18 -08003920 if (tsonly) {
3921#ifdef CONFIG_INET
3922 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
3923 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003924 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08003925 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003926 opt_stats = true;
3927 } else
Francis Yan1c885802016-11-27 23:07:18 -08003928#endif
3929 skb = alloc_skb(0, GFP_ATOMIC);
3930 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003931 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08003932 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04003933 if (!skb)
3934 return;
3935
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003936 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04003937 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3938 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003939 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3940 }
3941
3942 if (hwtstamps)
3943 *skb_hwtstamps(skb) = *hwtstamps;
3944 else
3945 skb->tstamp = ktime_get_real();
3946
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04003947 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003948}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003949EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3950
3951void skb_tstamp_tx(struct sk_buff *orig_skb,
3952 struct skb_shared_hwtstamps *hwtstamps)
3953{
3954 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3955 SCM_TSTAMP_SND);
3956}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003957EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3958
Johannes Berg6e3e9392011-11-09 10:15:42 +01003959void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3960{
3961 struct sock *sk = skb->sk;
3962 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003963 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01003964
3965 skb->wifi_acked_valid = 1;
3966 skb->wifi_acked = acked;
3967
3968 serr = SKB_EXT_ERR(skb);
3969 memset(serr, 0, sizeof(*serr));
3970 serr->ee.ee_errno = ENOMSG;
3971 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3972
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003973 /* Take a reference to prevent skb_orphan() from freeing the socket,
3974 * but only if the socket refcount is not zero.
3975 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003976 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08003977 err = sock_queue_err_skb(sk, skb);
3978 sock_put(sk);
3979 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01003980 if (err)
3981 kfree_skb(skb);
3982}
3983EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3984
Rusty Russellf35d9d82008-02-04 23:49:54 -05003985/**
3986 * skb_partial_csum_set - set up and verify partial csum values for packet
3987 * @skb: the skb to set
3988 * @start: the number of bytes after skb->data to start checksumming.
3989 * @off: the offset from start to place the checksum.
3990 *
3991 * For untrusted partially-checksummed packets, we need to make sure the values
3992 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3993 *
3994 * This function checks and sets those values and skb->ip_summed: if this
3995 * returns false you should drop the packet.
3996 */
3997bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3998{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003999 if (unlikely(start > skb_headlen(skb)) ||
4000 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00004001 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4002 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004003 return false;
4004 }
4005 skb->ip_summed = CHECKSUM_PARTIAL;
4006 skb->csum_start = skb_headroom(skb) + start;
4007 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004008 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004009 return true;
4010}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004011EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004012
Paul Durranted1f50c2014-01-09 10:02:46 +00004013static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4014 unsigned int max)
4015{
4016 if (skb_headlen(skb) >= len)
4017 return 0;
4018
4019 /* If we need to pullup then pullup to the max, so we
4020 * won't need to do it again.
4021 */
4022 if (max > skb->len)
4023 max = skb->len;
4024
4025 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4026 return -ENOMEM;
4027
4028 if (skb_headlen(skb) < len)
4029 return -EPROTO;
4030
4031 return 0;
4032}
4033
Jan Beulichf9708b42014-03-11 13:56:05 +00004034#define MAX_TCP_HDR_LEN (15 * 4)
4035
4036static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4037 typeof(IPPROTO_IP) proto,
4038 unsigned int off)
4039{
4040 switch (proto) {
4041 int err;
4042
4043 case IPPROTO_TCP:
4044 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4045 off + MAX_TCP_HDR_LEN);
4046 if (!err && !skb_partial_csum_set(skb, off,
4047 offsetof(struct tcphdr,
4048 check)))
4049 err = -EPROTO;
4050 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4051
4052 case IPPROTO_UDP:
4053 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4054 off + sizeof(struct udphdr));
4055 if (!err && !skb_partial_csum_set(skb, off,
4056 offsetof(struct udphdr,
4057 check)))
4058 err = -EPROTO;
4059 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4060 }
4061
4062 return ERR_PTR(-EPROTO);
4063}
4064
Paul Durranted1f50c2014-01-09 10:02:46 +00004065/* This value should be large enough to cover a tagged ethernet header plus
4066 * maximally sized IP and TCP or UDP headers.
4067 */
4068#define MAX_IP_HDR_LEN 128
4069
Jan Beulichf9708b42014-03-11 13:56:05 +00004070static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004071{
4072 unsigned int off;
4073 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004074 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004075 int err;
4076
4077 fragment = false;
4078
4079 err = skb_maybe_pull_tail(skb,
4080 sizeof(struct iphdr),
4081 MAX_IP_HDR_LEN);
4082 if (err < 0)
4083 goto out;
4084
4085 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4086 fragment = true;
4087
4088 off = ip_hdrlen(skb);
4089
4090 err = -EPROTO;
4091
4092 if (fragment)
4093 goto out;
4094
Jan Beulichf9708b42014-03-11 13:56:05 +00004095 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4096 if (IS_ERR(csum))
4097 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004098
Jan Beulichf9708b42014-03-11 13:56:05 +00004099 if (recalculate)
4100 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4101 ip_hdr(skb)->daddr,
4102 skb->len - off,
4103 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004104 err = 0;
4105
4106out:
4107 return err;
4108}
4109
4110/* This value should be large enough to cover a tagged ethernet header plus
4111 * an IPv6 header, all options, and a maximal TCP or UDP header.
4112 */
4113#define MAX_IPV6_HDR_LEN 256
4114
4115#define OPT_HDR(type, skb, off) \
4116 (type *)(skb_network_header(skb) + (off))
4117
4118static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4119{
4120 int err;
4121 u8 nexthdr;
4122 unsigned int off;
4123 unsigned int len;
4124 bool fragment;
4125 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004126 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004127
4128 fragment = false;
4129 done = false;
4130
4131 off = sizeof(struct ipv6hdr);
4132
4133 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4134 if (err < 0)
4135 goto out;
4136
4137 nexthdr = ipv6_hdr(skb)->nexthdr;
4138
4139 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4140 while (off <= len && !done) {
4141 switch (nexthdr) {
4142 case IPPROTO_DSTOPTS:
4143 case IPPROTO_HOPOPTS:
4144 case IPPROTO_ROUTING: {
4145 struct ipv6_opt_hdr *hp;
4146
4147 err = skb_maybe_pull_tail(skb,
4148 off +
4149 sizeof(struct ipv6_opt_hdr),
4150 MAX_IPV6_HDR_LEN);
4151 if (err < 0)
4152 goto out;
4153
4154 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4155 nexthdr = hp->nexthdr;
4156 off += ipv6_optlen(hp);
4157 break;
4158 }
4159 case IPPROTO_AH: {
4160 struct ip_auth_hdr *hp;
4161
4162 err = skb_maybe_pull_tail(skb,
4163 off +
4164 sizeof(struct ip_auth_hdr),
4165 MAX_IPV6_HDR_LEN);
4166 if (err < 0)
4167 goto out;
4168
4169 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4170 nexthdr = hp->nexthdr;
4171 off += ipv6_authlen(hp);
4172 break;
4173 }
4174 case IPPROTO_FRAGMENT: {
4175 struct frag_hdr *hp;
4176
4177 err = skb_maybe_pull_tail(skb,
4178 off +
4179 sizeof(struct frag_hdr),
4180 MAX_IPV6_HDR_LEN);
4181 if (err < 0)
4182 goto out;
4183
4184 hp = OPT_HDR(struct frag_hdr, skb, off);
4185
4186 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4187 fragment = true;
4188
4189 nexthdr = hp->nexthdr;
4190 off += sizeof(struct frag_hdr);
4191 break;
4192 }
4193 default:
4194 done = true;
4195 break;
4196 }
4197 }
4198
4199 err = -EPROTO;
4200
4201 if (!done || fragment)
4202 goto out;
4203
Jan Beulichf9708b42014-03-11 13:56:05 +00004204 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4205 if (IS_ERR(csum))
4206 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004207
Jan Beulichf9708b42014-03-11 13:56:05 +00004208 if (recalculate)
4209 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4210 &ipv6_hdr(skb)->daddr,
4211 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004212 err = 0;
4213
4214out:
4215 return err;
4216}
4217
4218/**
4219 * skb_checksum_setup - set up partial checksum offset
4220 * @skb: the skb to set up
4221 * @recalculate: if true the pseudo-header checksum will be recalculated
4222 */
4223int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4224{
4225 int err;
4226
4227 switch (skb->protocol) {
4228 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004229 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004230 break;
4231
4232 case htons(ETH_P_IPV6):
4233 err = skb_checksum_setup_ipv6(skb, recalculate);
4234 break;
4235
4236 default:
4237 err = -EPROTO;
4238 break;
4239 }
4240
4241 return err;
4242}
4243EXPORT_SYMBOL(skb_checksum_setup);
4244
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004245/**
4246 * skb_checksum_maybe_trim - maybe trims the given skb
4247 * @skb: the skb to check
4248 * @transport_len: the data length beyond the network header
4249 *
4250 * Checks whether the given skb has data beyond the given transport length.
4251 * If so, returns a cloned skb trimmed to this transport length.
4252 * Otherwise returns the provided skb. Returns NULL in error cases
4253 * (e.g. transport_len exceeds skb length or out-of-memory).
4254 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004255 * Caller needs to set the skb transport header and free any returned skb if it
4256 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004257 */
4258static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4259 unsigned int transport_len)
4260{
4261 struct sk_buff *skb_chk;
4262 unsigned int len = skb_transport_offset(skb) + transport_len;
4263 int ret;
4264
Linus Lüssinga5169932015-08-13 05:54:07 +02004265 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004266 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004267 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004268 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004269
4270 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004271 if (!skb_chk)
4272 return NULL;
4273
4274 ret = pskb_trim_rcsum(skb_chk, len);
4275 if (ret) {
4276 kfree_skb(skb_chk);
4277 return NULL;
4278 }
4279
4280 return skb_chk;
4281}
4282
4283/**
4284 * skb_checksum_trimmed - validate checksum of an skb
4285 * @skb: the skb to check
4286 * @transport_len: the data length beyond the network header
4287 * @skb_chkf: checksum function to use
4288 *
4289 * Applies the given checksum function skb_chkf to the provided skb.
4290 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4291 *
4292 * If the skb has data beyond the given transport length, then a
4293 * trimmed & cloned skb is checked and returned.
4294 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004295 * Caller needs to set the skb transport header and free any returned skb if it
4296 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004297 */
4298struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4299 unsigned int transport_len,
4300 __sum16(*skb_chkf)(struct sk_buff *skb))
4301{
4302 struct sk_buff *skb_chk;
4303 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004304 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004305
4306 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4307 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004308 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004309
Linus Lüssinga5169932015-08-13 05:54:07 +02004310 if (!pskb_may_pull(skb_chk, offset))
4311 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004312
Linus Lüssing9b368812016-02-24 04:21:42 +01004313 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004314 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004315 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004316
Linus Lüssinga5169932015-08-13 05:54:07 +02004317 if (ret)
4318 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004319
4320 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004321
4322err:
4323 if (skb_chk && skb_chk != skb)
4324 kfree_skb(skb_chk);
4325
4326 return NULL;
4327
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004328}
4329EXPORT_SYMBOL(skb_checksum_trimmed);
4330
Ben Hutchings4497b072008-06-19 16:22:28 -07004331void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4332{
Joe Perchese87cc472012-05-13 21:56:26 +00004333 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4334 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004335}
Ben Hutchings4497b072008-06-19 16:22:28 -07004336EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004337
4338void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4339{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004340 if (head_stolen) {
4341 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004342 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004343 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004344 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004345 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004346}
4347EXPORT_SYMBOL(kfree_skb_partial);
4348
4349/**
4350 * skb_try_coalesce - try to merge skb to prior one
4351 * @to: prior buffer
4352 * @from: buffer to add
4353 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004354 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004355 */
4356bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4357 bool *fragstolen, int *delta_truesize)
4358{
4359 int i, delta, len = from->len;
4360
4361 *fragstolen = false;
4362
4363 if (skb_cloned(to))
4364 return false;
4365
4366 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004367 if (len)
4368 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004369 *delta_truesize = 0;
4370 return true;
4371 }
4372
4373 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4374 return false;
4375
4376 if (skb_headlen(from) != 0) {
4377 struct page *page;
4378 unsigned int offset;
4379
4380 if (skb_shinfo(to)->nr_frags +
4381 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4382 return false;
4383
4384 if (skb_head_is_locked(from))
4385 return false;
4386
4387 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4388
4389 page = virt_to_head_page(from->head);
4390 offset = from->data - (unsigned char *)page_address(page);
4391
4392 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4393 page, offset, skb_headlen(from));
4394 *fragstolen = true;
4395 } else {
4396 if (skb_shinfo(to)->nr_frags +
4397 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4398 return false;
4399
Weiping Panf4b549a2012-09-28 20:15:30 +00004400 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004401 }
4402
4403 WARN_ON_ONCE(delta < len);
4404
4405 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4406 skb_shinfo(from)->frags,
4407 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4408 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4409
4410 if (!skb_cloned(from))
4411 skb_shinfo(from)->nr_frags = 0;
4412
Li RongQing8ea853f2012-09-18 16:53:21 +00004413 /* if the skb is not cloned this does nothing
4414 * since we set nr_frags to 0.
4415 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004416 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4417 skb_frag_ref(from, i);
4418
4419 to->truesize += delta;
4420 to->len += len;
4421 to->data_len += len;
4422
4423 *delta_truesize = delta;
4424 return true;
4425}
4426EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004427
4428/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004429 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004430 *
4431 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004432 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004433 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004434 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4435 * into/from a tunnel. Some information have to be cleared during these
4436 * operations.
4437 * skb_scrub_packet can also be used to clean a skb before injecting it in
4438 * another namespace (@xnet == true). We have to clear all information in the
4439 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004440 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004441void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004442{
Thomas Gleixner2456e852016-12-25 11:38:40 +01004443 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004444 skb->pkt_type = PACKET_HOST;
4445 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004446 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004447 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004448 secpath_reset(skb);
4449 nf_reset(skb);
4450 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004451
4452 if (!xnet)
4453 return;
4454
4455 skb_orphan(skb);
4456 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004457}
4458EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004459
4460/**
4461 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4462 *
4463 * @skb: GSO skb
4464 *
4465 * skb_gso_transport_seglen is used to determine the real size of the
4466 * individual segments, including Layer4 headers (TCP/UDP).
4467 *
4468 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4469 */
4470unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4471{
4472 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004473 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004474
Florian Westphalf993bc22014-10-20 13:49:18 +02004475 if (skb->encapsulation) {
4476 thlen = skb_inner_transport_header(skb) -
4477 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004478
Florian Westphalf993bc22014-10-20 13:49:18 +02004479 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4480 thlen += inner_tcp_hdrlen(skb);
4481 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4482 thlen = tcp_hdrlen(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004483 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4484 thlen = sizeof(struct sctphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004485 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004486 /* UFO sets gso_size to the size of the fragmentation
4487 * payload, i.e. the size of the L4 (UDP) header is already
4488 * accounted for.
4489 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004490 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004491}
4492EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004493
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004494/**
4495 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4496 *
4497 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07004498 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03004499 *
4500 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4501 * once split.
4502 */
4503bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4504{
4505 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4506 const struct sk_buff *iter;
4507 unsigned int hlen;
4508
4509 hlen = skb_gso_network_seglen(skb);
4510
4511 if (shinfo->gso_size != GSO_BY_FRAGS)
4512 return hlen <= mtu;
4513
4514 /* Undo this so we can re-use header sizes */
4515 hlen -= GSO_BY_FRAGS;
4516
4517 skb_walk_frags(skb, iter) {
4518 if (hlen + skb_headlen(iter) > mtu)
4519 return false;
4520 }
4521
4522 return true;
4523}
4524EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4525
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004526static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4527{
4528 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4529 kfree_skb(skb);
4530 return NULL;
4531 }
4532
Vlad Yasevichf6548612015-12-14 17:44:10 -05004533 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004534 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004535 skb->mac_header += VLAN_HLEN;
4536 return skb;
4537}
4538
4539struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4540{
4541 struct vlan_hdr *vhdr;
4542 u16 vlan_tci;
4543
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004544 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004545 /* vlan_tci is already set-up so leave this for another time */
4546 return skb;
4547 }
4548
4549 skb = skb_share_check(skb, GFP_ATOMIC);
4550 if (unlikely(!skb))
4551 goto err_free;
4552
4553 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4554 goto err_free;
4555
4556 vhdr = (struct vlan_hdr *)skb->data;
4557 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4558 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4559
4560 skb_pull_rcsum(skb, VLAN_HLEN);
4561 vlan_set_encap_proto(skb, vhdr);
4562
4563 skb = skb_reorder_vlan_header(skb);
4564 if (unlikely(!skb))
4565 goto err_free;
4566
4567 skb_reset_network_header(skb);
4568 skb_reset_transport_header(skb);
4569 skb_reset_mac_len(skb);
4570
4571 return skb;
4572
4573err_free:
4574 kfree_skb(skb);
4575 return NULL;
4576}
4577EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004578
Jiri Pirkoe2195122014-11-19 14:05:01 +01004579int skb_ensure_writable(struct sk_buff *skb, int write_len)
4580{
4581 if (!pskb_may_pull(skb, write_len))
4582 return -ENOMEM;
4583
4584 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4585 return 0;
4586
4587 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4588}
4589EXPORT_SYMBOL(skb_ensure_writable);
4590
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004591/* remove VLAN header from packet and update csum accordingly.
4592 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4593 */
4594int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01004595{
4596 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004597 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004598 int err;
4599
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004600 if (WARN_ONCE(offset,
4601 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4602 offset)) {
4603 return -EINVAL;
4604 }
4605
Jiri Pirko93515d52014-11-19 14:05:02 +01004606 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4607 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004608 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01004609
4610 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4611
4612 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4613 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4614
4615 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4616 __skb_pull(skb, VLAN_HLEN);
4617
4618 vlan_set_encap_proto(skb, vhdr);
4619 skb->mac_header += VLAN_HLEN;
4620
4621 if (skb_network_offset(skb) < ETH_HLEN)
4622 skb_set_network_header(skb, ETH_HLEN);
4623
4624 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004625
4626 return err;
4627}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03004628EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01004629
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004630/* Pop a vlan tag either from hwaccel or from payload.
4631 * Expects skb->data at mac header.
4632 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004633int skb_vlan_pop(struct sk_buff *skb)
4634{
4635 u16 vlan_tci;
4636 __be16 vlan_proto;
4637 int err;
4638
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004639 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004640 skb->vlan_tci = 0;
4641 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004642 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004643 return 0;
4644
4645 err = __skb_vlan_pop(skb, &vlan_tci);
4646 if (err)
4647 return err;
4648 }
4649 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03004650 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01004651 return 0;
4652
4653 vlan_proto = skb->protocol;
4654 err = __skb_vlan_pop(skb, &vlan_tci);
4655 if (unlikely(err))
4656 return err;
4657
4658 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4659 return 0;
4660}
4661EXPORT_SYMBOL(skb_vlan_pop);
4662
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004663/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4664 * Expects skb->data at mac header.
4665 */
Jiri Pirko93515d52014-11-19 14:05:02 +01004666int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4667{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004668 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004669 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01004670 int err;
4671
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004672 if (WARN_ONCE(offset,
4673 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4674 offset)) {
4675 return -EINVAL;
4676 }
4677
Jiri Pirko93515d52014-11-19 14:05:02 +01004678 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004679 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03004680 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01004681 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02004682
Jiri Pirko93515d52014-11-19 14:05:02 +01004683 skb->protocol = skb->vlan_proto;
4684 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01004685
Daniel Borkmann6b83d282016-02-20 00:29:30 +01004686 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01004687 }
4688 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4689 return 0;
4690}
4691EXPORT_SYMBOL(skb_vlan_push);
4692
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004693/**
4694 * alloc_skb_with_frags - allocate skb with page frags
4695 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004696 * @header_len: size of linear part
4697 * @data_len: needed length in frags
4698 * @max_page_order: max page order desired.
4699 * @errcode: pointer to error code if any
4700 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004701 *
4702 * This can be used to allocate a paged skb, given a maximal order for frags.
4703 */
4704struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4705 unsigned long data_len,
4706 int max_page_order,
4707 int *errcode,
4708 gfp_t gfp_mask)
4709{
4710 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4711 unsigned long chunk;
4712 struct sk_buff *skb;
4713 struct page *page;
4714 gfp_t gfp_head;
4715 int i;
4716
4717 *errcode = -EMSGSIZE;
4718 /* Note this test could be relaxed, if we succeed to allocate
4719 * high order pages...
4720 */
4721 if (npages > MAX_SKB_FRAGS)
4722 return NULL;
4723
4724 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004725 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004726 gfp_head |= __GFP_REPEAT;
4727
4728 *errcode = -ENOBUFS;
4729 skb = alloc_skb(header_len, gfp_head);
4730 if (!skb)
4731 return NULL;
4732
4733 skb->truesize += npages << PAGE_SHIFT;
4734
4735 for (i = 0; npages > 0; i++) {
4736 int order = max_page_order;
4737
4738 while (order) {
4739 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004740 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004741 __GFP_COMP |
4742 __GFP_NOWARN |
4743 __GFP_NORETRY,
4744 order);
4745 if (page)
4746 goto fill_page;
4747 /* Do not retry other high order allocations */
4748 order = 1;
4749 max_page_order = 0;
4750 }
4751 order--;
4752 }
4753 page = alloc_page(gfp_mask);
4754 if (!page)
4755 goto failure;
4756fill_page:
4757 chunk = min_t(unsigned long, data_len,
4758 PAGE_SIZE << order);
4759 skb_fill_page_desc(skb, i, page, 0, chunk);
4760 data_len -= chunk;
4761 npages -= 1 << order;
4762 }
4763 return skb;
4764
4765failure:
4766 kfree_skb(skb);
4767 return NULL;
4768}
4769EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004770
4771/* carve out the first off bytes from skb when off < headlen */
4772static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4773 const int headlen, gfp_t gfp_mask)
4774{
4775 int i;
4776 int size = skb_end_offset(skb);
4777 int new_hlen = headlen - off;
4778 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004779
4780 size = SKB_DATA_ALIGN(size);
4781
4782 if (skb_pfmemalloc(skb))
4783 gfp_mask |= __GFP_MEMALLOC;
4784 data = kmalloc_reserve(size +
4785 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4786 gfp_mask, NUMA_NO_NODE, NULL);
4787 if (!data)
4788 return -ENOMEM;
4789
4790 size = SKB_WITH_OVERHEAD(ksize(data));
4791
4792 /* Copy real data, and all frags */
4793 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4794 skb->len -= off;
4795
4796 memcpy((struct skb_shared_info *)(data + size),
4797 skb_shinfo(skb),
4798 offsetof(struct skb_shared_info,
4799 frags[skb_shinfo(skb)->nr_frags]));
4800 if (skb_cloned(skb)) {
4801 /* drop the old head gracefully */
4802 if (skb_orphan_frags(skb, gfp_mask)) {
4803 kfree(data);
4804 return -ENOMEM;
4805 }
4806 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4807 skb_frag_ref(skb, i);
4808 if (skb_has_frag_list(skb))
4809 skb_clone_fraglist(skb);
4810 skb_release_data(skb);
4811 } else {
4812 /* we can reuse existing recount- all we did was
4813 * relocate values
4814 */
4815 skb_free_head(skb);
4816 }
4817
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004818 skb->head = data;
4819 skb->data = data;
4820 skb->head_frag = 0;
4821#ifdef NET_SKBUFF_DATA_USES_OFFSET
4822 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004823#else
4824 skb->end = skb->head + size;
4825#endif
4826 skb_set_tail_pointer(skb, skb_headlen(skb));
4827 skb_headers_offset_update(skb, 0);
4828 skb->cloned = 0;
4829 skb->hdr_len = 0;
4830 skb->nohdr = 0;
4831 atomic_set(&skb_shinfo(skb)->dataref, 1);
4832
4833 return 0;
4834}
4835
4836static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4837
4838/* carve out the first eat bytes from skb's frag_list. May recurse into
4839 * pskb_carve()
4840 */
4841static int pskb_carve_frag_list(struct sk_buff *skb,
4842 struct skb_shared_info *shinfo, int eat,
4843 gfp_t gfp_mask)
4844{
4845 struct sk_buff *list = shinfo->frag_list;
4846 struct sk_buff *clone = NULL;
4847 struct sk_buff *insp = NULL;
4848
4849 do {
4850 if (!list) {
4851 pr_err("Not enough bytes to eat. Want %d\n", eat);
4852 return -EFAULT;
4853 }
4854 if (list->len <= eat) {
4855 /* Eaten as whole. */
4856 eat -= list->len;
4857 list = list->next;
4858 insp = list;
4859 } else {
4860 /* Eaten partially. */
4861 if (skb_shared(list)) {
4862 clone = skb_clone(list, gfp_mask);
4863 if (!clone)
4864 return -ENOMEM;
4865 insp = list->next;
4866 list = clone;
4867 } else {
4868 /* This may be pulled without problems. */
4869 insp = list;
4870 }
4871 if (pskb_carve(list, eat, gfp_mask) < 0) {
4872 kfree_skb(clone);
4873 return -ENOMEM;
4874 }
4875 break;
4876 }
4877 } while (eat);
4878
4879 /* Free pulled out fragments. */
4880 while ((list = shinfo->frag_list) != insp) {
4881 shinfo->frag_list = list->next;
4882 kfree_skb(list);
4883 }
4884 /* And insert new clone at head. */
4885 if (clone) {
4886 clone->next = list;
4887 shinfo->frag_list = clone;
4888 }
4889 return 0;
4890}
4891
4892/* carve off first len bytes from skb. Split line (off) is in the
4893 * non-linear part of skb
4894 */
4895static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4896 int pos, gfp_t gfp_mask)
4897{
4898 int i, k = 0;
4899 int size = skb_end_offset(skb);
4900 u8 *data;
4901 const int nfrags = skb_shinfo(skb)->nr_frags;
4902 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004903
4904 size = SKB_DATA_ALIGN(size);
4905
4906 if (skb_pfmemalloc(skb))
4907 gfp_mask |= __GFP_MEMALLOC;
4908 data = kmalloc_reserve(size +
4909 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4910 gfp_mask, NUMA_NO_NODE, NULL);
4911 if (!data)
4912 return -ENOMEM;
4913
4914 size = SKB_WITH_OVERHEAD(ksize(data));
4915
4916 memcpy((struct skb_shared_info *)(data + size),
4917 skb_shinfo(skb), offsetof(struct skb_shared_info,
4918 frags[skb_shinfo(skb)->nr_frags]));
4919 if (skb_orphan_frags(skb, gfp_mask)) {
4920 kfree(data);
4921 return -ENOMEM;
4922 }
4923 shinfo = (struct skb_shared_info *)(data + size);
4924 for (i = 0; i < nfrags; i++) {
4925 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4926
4927 if (pos + fsize > off) {
4928 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
4929
4930 if (pos < off) {
4931 /* Split frag.
4932 * We have two variants in this case:
4933 * 1. Move all the frag to the second
4934 * part, if it is possible. F.e.
4935 * this approach is mandatory for TUX,
4936 * where splitting is expensive.
4937 * 2. Split is accurately. We make this.
4938 */
4939 shinfo->frags[0].page_offset += off - pos;
4940 skb_frag_size_sub(&shinfo->frags[0], off - pos);
4941 }
4942 skb_frag_ref(skb, i);
4943 k++;
4944 }
4945 pos += fsize;
4946 }
4947 shinfo->nr_frags = k;
4948 if (skb_has_frag_list(skb))
4949 skb_clone_fraglist(skb);
4950
4951 if (k == 0) {
4952 /* split line is in frag list */
4953 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
4954 }
4955 skb_release_data(skb);
4956
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004957 skb->head = data;
4958 skb->head_frag = 0;
4959 skb->data = data;
4960#ifdef NET_SKBUFF_DATA_USES_OFFSET
4961 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07004962#else
4963 skb->end = skb->head + size;
4964#endif
4965 skb_reset_tail_pointer(skb);
4966 skb_headers_offset_update(skb, 0);
4967 skb->cloned = 0;
4968 skb->hdr_len = 0;
4969 skb->nohdr = 0;
4970 skb->len -= off;
4971 skb->data_len = skb->len;
4972 atomic_set(&skb_shinfo(skb)->dataref, 1);
4973 return 0;
4974}
4975
4976/* remove len bytes from the beginning of the skb */
4977static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
4978{
4979 int headlen = skb_headlen(skb);
4980
4981 if (len < headlen)
4982 return pskb_carve_inside_header(skb, len, headlen, gfp);
4983 else
4984 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
4985}
4986
4987/* Extract to_copy bytes starting at off from skb, and return this in
4988 * a new skb
4989 */
4990struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
4991 int to_copy, gfp_t gfp)
4992{
4993 struct sk_buff *clone = skb_clone(skb, gfp);
4994
4995 if (!clone)
4996 return NULL;
4997
4998 if (pskb_carve(clone, off, gfp) < 0 ||
4999 pskb_trim(clone, to_copy)) {
5000 kfree_skb(clone);
5001 return NULL;
5002 }
5003 return clone;
5004}
5005EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005006
5007/**
5008 * skb_condense - try to get rid of fragments/frag_list if possible
5009 * @skb: buffer
5010 *
5011 * Can be used to save memory before skb is added to a busy queue.
5012 * If packet has bytes in frags and enough tail room in skb->head,
5013 * pull all of them, so that we can free the frags right now and adjust
5014 * truesize.
5015 * Notes:
5016 * We do not reallocate skb->head thus can not fail.
5017 * Caller must re-evaluate skb->truesize if needed.
5018 */
5019void skb_condense(struct sk_buff *skb)
5020{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005021 if (skb->data_len) {
5022 if (skb->data_len > skb->end - skb->tail ||
5023 skb_cloned(skb))
5024 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005025
Eric Dumazet3174fed2016-12-09 08:02:05 -08005026 /* Nice, we can free page frag(s) right now */
5027 __pskb_pull_tail(skb, skb->data_len);
5028 }
5029 /* At this point, skb->truesize might be over estimated,
5030 * because skb had a fragment, and fragments do not tell
5031 * their truesize.
5032 * When we pulled its content into skb->head, fragment
5033 * was freed, but __pskb_pull_tail() could not possibly
5034 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005035 */
5036 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5037}