blob: d2dfad33e6864b45f06ce8de319f7133222ecd3c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010049#include <linux/tcp.h>
50#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030051#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080058#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070062#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000063#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070064#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040065#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <net/protocol.h>
68#include <net/dst.h>
69#include <net/sock.h>
70#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000071#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <net/xfrm.h>
73
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080074#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040075#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020076#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050077#include <linux/capability.h>
78#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079
Alexey Dobriyan08009a72018-02-24 21:20:33 +030080struct kmem_cache *skbuff_head_cache __ro_after_init;
81static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
Florian Westphaldf5042f2018-12-18 17:15:16 +010082#ifdef CONFIG_SKB_EXTENSIONS
83static struct kmem_cache *skbuff_ext_cache __ro_after_init;
84#endif
Hans Westgaard Ry5f74f82e2016-02-03 09:26:57 +010085int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
86EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/**
Jean Sacrenf05de732013-02-11 13:30:38 +000089 * skb_panic - private function for out-of-line support
90 * @skb: buffer
91 * @sz: size
92 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000093 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Jean Sacrenf05de732013-02-11 13:30:38 +000095 * Out-of-line support for skb_put() and skb_push().
96 * Called via the wrapper skb_over_panic() or skb_under_panic().
97 * Keep out of line to prevent kernel bloat.
98 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Jean Sacrenf05de732013-02-11 13:30:38 +0000100static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +0000101 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Joe Perchese005d192012-05-16 19:58:40 +0000103 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 +0000104 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000105 (unsigned long)skb->tail, (unsigned long)skb->end,
106 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 BUG();
108}
109
Jean Sacrenf05de732013-02-11 13:30:38 +0000110static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Jean Sacrenf05de732013-02-11 13:30:38 +0000112 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jean Sacrenf05de732013-02-11 13:30:38 +0000115static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
116{
117 skb_panic(skb, sz, addr, __func__);
118}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700119
120/*
121 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
122 * the caller if emergency pfmemalloc reserves are being used. If it is and
123 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
124 * may be used. Otherwise, the packet data may be discarded until enough
125 * memory is free
126 */
127#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
128 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000129
130static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
131 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700132{
133 void *obj;
134 bool ret_pfmemalloc = false;
135
136 /*
137 * Try a regular allocation, when that fails and we're not entitled
138 * to the reserves, fail.
139 */
140 obj = kmalloc_node_track_caller(size,
141 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
142 node);
143 if (obj || !(gfp_pfmemalloc_allowed(flags)))
144 goto out;
145
146 /* Try again but now we are using pfmemalloc reserves */
147 ret_pfmemalloc = true;
148 obj = kmalloc_node_track_caller(size, flags, node);
149
150out:
151 if (pfmemalloc)
152 *pfmemalloc = ret_pfmemalloc;
153
154 return obj;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Allocate a new skbuff. We do this ourselves so we can fill in a few
158 * 'private' fields and also do memory statistics to find all the
159 * [BEEP] leaks.
160 *
161 */
162
163/**
David S. Millerd179cd12005-08-17 14:57:30 -0700164 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * @size: size to allocate
166 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700167 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
168 * instead of head cache and allocate a cloned (child) skb.
169 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
170 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800171 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000174 * tail room of at least size bytes. The object has a reference count
175 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Buffers may only be allocated from interrupts using a @gfp_mask of
178 * %GFP_ATOMIC.
179 */
Al Virodd0fc662005-10-07 07:46:04 +0100180struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700181 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Christoph Lametere18b8902006-12-06 20:33:20 -0800183 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800184 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct sk_buff *skb;
186 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Mel Gormanc93bdd02012-07-31 16:44:19 -0700189 cache = (flags & SKB_ALLOC_FCLONE)
190 ? skbuff_fclone_cache : skbuff_head_cache;
191
192 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
193 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800196 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!skb)
198 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700199 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000201 /* We do our best to align skb_shared_info on a separate cache
202 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
203 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
204 * Both skb->head and skb_shared_info are cache line aligned.
205 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000206 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000207 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700208 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (!data)
210 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000211 /* kmalloc(size) might give us more room than requested.
212 * Put skb_shared_info exactly at the end of allocated zone,
213 * to allow max possible filling before reallocation.
214 */
215 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700216 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300218 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700219 * Only clear those fields we need to clear, not those that we will
220 * actually initialise below. Hence, don't put any more fields after
221 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300222 */
223 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000224 /* Account for allocated memory : skb + skb->head */
225 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700226 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300227 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 skb->head = data;
229 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700230 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700231 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800232 skb->mac_header = (typeof(skb->mac_header))~0U;
233 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000234
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800235 /* make sure we initialize shinfo sequentially */
236 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700237 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238 atomic_set(&shinfo->dataref, 1);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800239
Mel Gormanc93bdd02012-07-31 16:44:19 -0700240 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700241 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700243 fclones = container_of(skb, struct sk_buff_fclones, skb1);
244
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);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000304
305 return skb;
306}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700307
308/* build_skb() is wrapper over __build_skb(), that specifically
309 * takes care of skb->head and skb->pfmemalloc
310 * This means that if @frag_size is not zero, then @data must be backed
311 * by a page fragment, not kmalloc() or vmalloc()
312 */
313struct sk_buff *build_skb(void *data, unsigned int frag_size)
314{
315 struct sk_buff *skb = __build_skb(data, frag_size);
316
317 if (skb && frag_size) {
318 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700319 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700320 skb->pfmemalloc = 1;
321 }
322 return skb;
323}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000324EXPORT_SYMBOL(build_skb);
325
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100326#define NAPI_SKB_CACHE_SIZE 64
327
328struct napi_alloc_cache {
329 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300330 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100331 void *skb_cache[NAPI_SKB_CACHE_SIZE];
332};
333
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700334static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100335static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800336
337static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
338{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700339 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800340 unsigned long flags;
341 void *data;
342
343 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700344 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800345 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000346 local_irq_restore(flags);
347 return data;
348}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700349
350/**
351 * netdev_alloc_frag - allocate a page fragment
352 * @fragsz: fragment size
353 *
354 * Allocates a frag from a page for receive buffer.
355 * Uses GFP_ATOMIC allocations.
356 */
357void *netdev_alloc_frag(unsigned int fragsz)
358{
Mel Gorman453f85d2017-11-15 17:38:03 -0800359 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700360}
Eric Dumazet6f532612012-05-18 05:12:12 +0000361EXPORT_SYMBOL(netdev_alloc_frag);
362
Alexander Duyckffde7322014-12-09 19:40:42 -0800363static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100365 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700366
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800367 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800368}
369
370void *napi_alloc_frag(unsigned int fragsz)
371{
Mel Gorman453f85d2017-11-15 17:38:03 -0800372 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
Alexander Duyckffde7322014-12-09 19:40:42 -0800373}
374EXPORT_SYMBOL(napi_alloc_frag);
375
Eric Dumazet6f532612012-05-18 05:12:12 +0000376/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800377 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
378 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900379 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800380 * @gfp_mask: get_free_pages mask, passed to alloc_skb
381 *
382 * Allocate a new &sk_buff and assign it a usage count of one. The
383 * buffer has NET_SKB_PAD headroom built in. Users should allocate
384 * the headroom they think they need without accounting for the
385 * built in space. The built in space is used for optimisations.
386 *
387 * %NULL is returned if there is no free memory.
388 */
Alexander Duyck94519802015-05-06 21:11:40 -0700389struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
390 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800391{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700392 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700393 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800394 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700395 bool pfmemalloc;
396 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800397
Alexander Duyck94519802015-05-06 21:11:40 -0700398 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800399
Alexander Duyck94519802015-05-06 21:11:40 -0700400 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800401 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700402 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
403 if (!skb)
404 goto skb_fail;
405 goto skb_success;
406 }
Alexander Duyck94519802015-05-06 21:11:40 -0700407
408 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
409 len = SKB_DATA_ALIGN(len);
410
411 if (sk_memalloc_socks())
412 gfp_mask |= __GFP_MEMALLOC;
413
414 local_irq_save(flags);
415
416 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800417 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700418 pfmemalloc = nc->pfmemalloc;
419
420 local_irq_restore(flags);
421
422 if (unlikely(!data))
423 return NULL;
424
425 skb = __build_skb(data, len);
426 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700427 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700428 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700429 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800430
Alexander Duyck94519802015-05-06 21:11:40 -0700431 /* use OR instead of assignment to avoid clearing of bits in mask */
432 if (pfmemalloc)
433 skb->pfmemalloc = 1;
434 skb->head_frag = 1;
435
Alexander Duycka080e7b2015-05-13 13:34:13 -0700436skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700437 skb_reserve(skb, NET_SKB_PAD);
438 skb->dev = dev;
439
Alexander Duycka080e7b2015-05-13 13:34:13 -0700440skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700441 return skb;
442}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800443EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alexander Duyckfd11a832014-12-09 19:40:49 -0800445/**
446 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
447 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900448 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800449 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
450 *
451 * Allocate a new sk_buff for use in NAPI receive. This buffer will
452 * attempt to allocate the head from a special reserved region used
453 * only for NAPI Rx allocation. By doing this we can save several
454 * CPU cycles by avoiding having to disable and re-enable IRQs.
455 *
456 * %NULL is returned if there is no free memory.
457 */
Alexander Duyck94519802015-05-06 21:11:40 -0700458struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
459 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800460{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100461 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800462 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700463 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800464
Alexander Duyck94519802015-05-06 21:11:40 -0700465 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800466
Alexander Duyck94519802015-05-06 21:11:40 -0700467 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800468 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700469 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
470 if (!skb)
471 goto skb_fail;
472 goto skb_success;
473 }
Alexander Duyck94519802015-05-06 21:11:40 -0700474
475 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
476 len = SKB_DATA_ALIGN(len);
477
478 if (sk_memalloc_socks())
479 gfp_mask |= __GFP_MEMALLOC;
480
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800481 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700482 if (unlikely(!data))
483 return NULL;
484
485 skb = __build_skb(data, len);
486 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700487 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700488 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800489 }
490
Alexander Duyck94519802015-05-06 21:11:40 -0700491 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100492 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700493 skb->pfmemalloc = 1;
494 skb->head_frag = 1;
495
Alexander Duycka080e7b2015-05-13 13:34:13 -0700496skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700497 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
498 skb->dev = napi->dev;
499
Alexander Duycka080e7b2015-05-13 13:34:13 -0700500skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800501 return skb;
502}
503EXPORT_SYMBOL(__napi_alloc_skb);
504
Peter Zijlstra654bed12008-10-07 14:22:33 -0700505void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000506 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700507{
508 skb_fill_page_desc(skb, i, page, off, size);
509 skb->len += size;
510 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000511 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700512}
513EXPORT_SYMBOL(skb_add_rx_frag);
514
Jason Wangf8e617e2013-11-01 14:07:47 +0800515void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
516 unsigned int truesize)
517{
518 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
519
520 skb_frag_size_add(frag, size);
521 skb->len += size;
522 skb->data_len += size;
523 skb->truesize += truesize;
524}
525EXPORT_SYMBOL(skb_coalesce_rx_frag);
526
Herbert Xu27b437c2006-07-13 19:26:39 -0700527static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700529 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700530 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Herbert Xu27b437c2006-07-13 19:26:39 -0700533static inline void skb_drop_fraglist(struct sk_buff *skb)
534{
535 skb_drop_list(&skb_shinfo(skb)->frag_list);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static void skb_clone_fraglist(struct sk_buff *skb)
539{
540 struct sk_buff *list;
541
David S. Millerfbb398a2009-06-09 00:18:59 -0700542 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 skb_get(list);
544}
545
Eric Dumazetd3836f22012-04-27 00:33:38 +0000546static void skb_free_head(struct sk_buff *skb)
547{
Alexander Duyck181edb22015-05-06 21:12:03 -0700548 unsigned char *head = skb->head;
549
Eric Dumazetd3836f22012-04-27 00:33:38 +0000550 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700551 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000552 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700553 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000554}
555
Adrian Bunk5bba1712006-06-29 13:02:35 -0700556static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Eric Dumazetff04a772014-09-23 18:39:30 -0700558 struct skb_shared_info *shinfo = skb_shinfo(skb);
559 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Eric Dumazetff04a772014-09-23 18:39:30 -0700561 if (skb->cloned &&
562 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
563 &shinfo->dataref))
564 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000565
Eric Dumazetff04a772014-09-23 18:39:30 -0700566 for (i = 0; i < shinfo->nr_frags; i++)
567 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000568
Eric Dumazetff04a772014-09-23 18:39:30 -0700569 if (shinfo->frag_list)
570 kfree_skb_list(shinfo->frag_list);
571
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400572 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700573 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
577 * Free an skbuff by memory without cleaning the state.
578 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800579static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700581 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700582
David S. Millerd179cd12005-08-17 14:57:30 -0700583 switch (skb->fclone) {
584 case SKB_FCLONE_UNAVAILABLE:
585 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800586 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700587
588 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700589 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800590
591 /* We usually free the clone (TX completion) before original skb
592 * This test would have no chance to be true for the clone,
593 * while here, branch prediction will be good.
594 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300595 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800596 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700597 break;
598
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800599 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700600 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700601 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700602 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300603 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800604 return;
605fastpath:
606 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Paolo Abeni0a463c72017-06-12 11:23:42 +0200609void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Eric Dumazetadf30902009-06-02 05:19:30 +0000611 skb_dst_drop(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200612 secpath_reset(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700613 if (skb->destructor) {
614 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 skb->destructor(skb);
616 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000617#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100618 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100619#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200620#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 nf_bridge_put(skb->nf_bridge);
622#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100623 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700624}
625
626/* Free everything but the sk_buff shell. */
627static void skb_release_all(struct sk_buff *skb)
628{
629 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200630 if (likely(skb->head))
631 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Herbert Xu2d4baff2007-11-26 23:11:19 +0800634/**
635 * __kfree_skb - private function
636 * @skb: buffer
637 *
638 * Free an sk_buff. Release anything attached to the buffer.
639 * Clean the state. This is an internal helper function. Users should
640 * always call kfree_skb
641 */
642
643void __kfree_skb(struct sk_buff *skb)
644{
645 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kfree_skbmem(skb);
647}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800648EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800651 * kfree_skb - free an sk_buff
652 * @skb: buffer to free
653 *
654 * Drop a reference to the buffer and free it if the usage count has
655 * hit zero.
656 */
657void kfree_skb(struct sk_buff *skb)
658{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200659 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800660 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200661
Neil Hormanead2ceb2009-03-11 09:49:55 +0000662 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800663 __kfree_skb(skb);
664}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800665EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800666
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700667void kfree_skb_list(struct sk_buff *segs)
668{
669 while (segs) {
670 struct sk_buff *next = segs->next;
671
672 kfree_skb(segs);
673 segs = next;
674 }
675}
676EXPORT_SYMBOL(kfree_skb_list);
677
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700678/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000679 * skb_tx_error - report an sk_buff xmit error
680 * @skb: buffer that triggered an error
681 *
682 * Report xmit error if a device callback is tracking this skb.
683 * skb must be freed afterwards.
684 */
685void skb_tx_error(struct sk_buff *skb)
686{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400687 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000688}
689EXPORT_SYMBOL(skb_tx_error);
690
691/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000692 * consume_skb - free an skbuff
693 * @skb: buffer to free
694 *
695 * Drop a ref to the buffer and free it if the usage count has hit zero
696 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
697 * is being dropped after a failure and notes that
698 */
699void consume_skb(struct sk_buff *skb)
700{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200701 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000702 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200703
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900704 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000705 __kfree_skb(skb);
706}
707EXPORT_SYMBOL(consume_skb);
708
Paolo Abeni0a463c72017-06-12 11:23:42 +0200709/**
710 * consume_stateless_skb - free an skbuff, assuming it is stateless
711 * @skb: buffer to free
712 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200713 * Alike consume_skb(), but this variant assumes that this is the last
714 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200715 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200716void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200717{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200718 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200719 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200720 kfree_skbmem(skb);
721}
722
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100723void __kfree_skb_flush(void)
724{
725 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
726
727 /* flush skb_cache if containing objects */
728 if (nc->skb_count) {
729 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
730 nc->skb_cache);
731 nc->skb_count = 0;
732 }
733}
734
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100735static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100736{
737 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
738
739 /* drop skb->head and call any destructors for packet */
740 skb_release_all(skb);
741
742 /* record skb to CPU local list */
743 nc->skb_cache[nc->skb_count++] = skb;
744
745#ifdef CONFIG_SLUB
746 /* SLUB writes into objects when freeing */
747 prefetchw(skb);
748#endif
749
750 /* flush skb_cache if it is filled */
751 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
752 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
753 nc->skb_cache);
754 nc->skb_count = 0;
755 }
756}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100757void __kfree_skb_defer(struct sk_buff *skb)
758{
759 _kfree_skb_defer(skb);
760}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100761
762void napi_consume_skb(struct sk_buff *skb, int budget)
763{
764 if (unlikely(!skb))
765 return;
766
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100767 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100768 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100769 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100770 return;
771 }
772
Paolo Abeni76088942017-06-14 11:48:48 +0200773 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100774 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200775
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100776 /* if reaching here SKB is ready to free */
777 trace_consume_skb(skb);
778
779 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700780 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100781 __kfree_skb(skb);
782 return;
783 }
784
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100785 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100786}
787EXPORT_SYMBOL(napi_consume_skb);
788
Eric Dumazetb1937222014-09-28 22:18:47 -0700789/* Make sure a field is enclosed inside headers_start/headers_end section */
790#define CHECK_SKB_FIELD(field) \
791 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
792 offsetof(struct sk_buff, headers_start)); \
793 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
794 offsetof(struct sk_buff, headers_end)); \
795
Herbert Xudec18812007-10-14 00:37:30 -0700796static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
797{
798 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700799 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700800 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700801 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000802 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100803 __skb_ext_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700804#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700805 new->sp = secpath_get(old->sp);
806#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700807 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700808
Eric Dumazetb1937222014-09-28 22:18:47 -0700809 /* Note : this field could be in headers_start/headers_end section
810 * It is not yet because we do not want to have a 16 bit hole
811 */
812 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300813
Eric Dumazetb1937222014-09-28 22:18:47 -0700814 memcpy(&new->headers_start, &old->headers_start,
815 offsetof(struct sk_buff, headers_end) -
816 offsetof(struct sk_buff, headers_start));
817 CHECK_SKB_FIELD(protocol);
818 CHECK_SKB_FIELD(csum);
819 CHECK_SKB_FIELD(hash);
820 CHECK_SKB_FIELD(priority);
821 CHECK_SKB_FIELD(skb_iif);
822 CHECK_SKB_FIELD(vlan_proto);
823 CHECK_SKB_FIELD(vlan_tci);
824 CHECK_SKB_FIELD(transport_header);
825 CHECK_SKB_FIELD(network_header);
826 CHECK_SKB_FIELD(mac_header);
827 CHECK_SKB_FIELD(inner_protocol);
828 CHECK_SKB_FIELD(inner_transport_header);
829 CHECK_SKB_FIELD(inner_network_header);
830 CHECK_SKB_FIELD(inner_mac_header);
831 CHECK_SKB_FIELD(mark);
832#ifdef CONFIG_NETWORK_SECMARK
833 CHECK_SKB_FIELD(secmark);
834#endif
Cong Wange0d10952013-08-01 11:10:25 +0800835#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700836 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300837#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800838#ifdef CONFIG_XPS
839 CHECK_SKB_FIELD(sender_cpu);
840#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700841#ifdef CONFIG_NET_SCHED
842 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700843#endif
844
Herbert Xudec18812007-10-14 00:37:30 -0700845}
846
Herbert Xu82c49a32009-05-22 22:11:37 +0000847/*
848 * You should not add any new code to this function. Add it to
849 * __copy_skb_header above instead.
850 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700851static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#define C(x) n->x = skb->x
854
855 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700857 __copy_skb_header(n, skb);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 C(len);
860 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700861 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700862 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800863 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -0700865 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +0200866 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 C(tail);
869 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800870 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000871 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800872 C(data);
873 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300874 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 atomic_inc(&(skb_shinfo(skb)->dataref));
877 skb->cloned = 1;
878
879 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700880#undef C
881}
882
883/**
884 * skb_morph - morph one skb into another
885 * @dst: the skb to receive the contents
886 * @src: the skb to supply the contents
887 *
888 * This is identical to skb_clone except that the target skb is
889 * supplied by the user.
890 *
891 * The target skb is returned upon exit.
892 */
893struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
894{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800895 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700896 return __skb_clone(dst, src);
897}
898EXPORT_SYMBOL_GPL(skb_morph);
899
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800900int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400901{
902 unsigned long max_pg, num_pg, new_pg, old_pg;
903 struct user_struct *user;
904
905 if (capable(CAP_IPC_LOCK) || !size)
906 return 0;
907
908 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
909 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
910 user = mmp->user ? : current_user();
911
912 do {
913 old_pg = atomic_long_read(&user->locked_vm);
914 new_pg = old_pg + num_pg;
915 if (new_pg > max_pg)
916 return -ENOBUFS;
917 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
918 old_pg);
919
920 if (!mmp->user) {
921 mmp->user = get_uid(user);
922 mmp->num_pg = num_pg;
923 } else {
924 mmp->num_pg += num_pg;
925 }
926
927 return 0;
928}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800929EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400930
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800931void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400932{
933 if (mmp->user) {
934 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
935 free_uid(mmp->user);
936 }
937}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800938EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400939
Willem de Bruijn52267792017-08-03 16:29:39 -0400940struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
941{
942 struct ubuf_info *uarg;
943 struct sk_buff *skb;
944
945 WARN_ON_ONCE(!in_task());
946
947 skb = sock_omalloc(sk, 0, GFP_KERNEL);
948 if (!skb)
949 return NULL;
950
951 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
952 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400953 uarg->mmp.user = NULL;
954
955 if (mm_account_pinned_pages(&uarg->mmp, size)) {
956 kfree_skb(skb);
957 return NULL;
958 }
Willem de Bruijn52267792017-08-03 16:29:39 -0400959
960 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -0400961 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
962 uarg->len = 1;
963 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -0400964 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -0700965 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -0400966 sock_hold(sk);
967
968 return uarg;
969}
970EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
971
972static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
973{
974 return container_of((void *)uarg, struct sk_buff, cb);
975}
976
Willem de Bruijn4ab6c992017-08-03 16:29:42 -0400977struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
978 struct ubuf_info *uarg)
979{
980 if (uarg) {
981 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
982 u32 bytelen, next;
983
984 /* realloc only when socket is locked (TCP, UDP cork),
985 * so uarg->len and sk_zckey access is serialized
986 */
987 if (!sock_owned_by_user(sk)) {
988 WARN_ON_ONCE(1);
989 return NULL;
990 }
991
992 bytelen = uarg->bytelen + size;
993 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
994 /* TCP can create new skb to attach new uarg */
995 if (sk->sk_type == SOCK_STREAM)
996 goto new_alloc;
997 return NULL;
998 }
999
1000 next = (u32)atomic_read(&sk->sk_zckey);
1001 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001002 if (mm_account_pinned_pages(&uarg->mmp, size))
1003 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001004 uarg->len++;
1005 uarg->bytelen = bytelen;
1006 atomic_set(&sk->sk_zckey, ++next);
Eric Dumazetdb5bce32017-08-31 16:48:21 -07001007 sock_zerocopy_get(uarg);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001008 return uarg;
1009 }
1010 }
1011
1012new_alloc:
1013 return sock_zerocopy_alloc(sk, size);
1014}
1015EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1016
1017static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1018{
1019 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1020 u32 old_lo, old_hi;
1021 u64 sum_len;
1022
1023 old_lo = serr->ee.ee_info;
1024 old_hi = serr->ee.ee_data;
1025 sum_len = old_hi - old_lo + 1ULL + len;
1026
1027 if (sum_len >= (1ULL << 32))
1028 return false;
1029
1030 if (lo != old_hi + 1)
1031 return false;
1032
1033 serr->ee.ee_data += len;
1034 return true;
1035}
1036
Willem de Bruijn52267792017-08-03 16:29:39 -04001037void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1038{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001039 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001040 struct sock_exterr_skb *serr;
1041 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001042 struct sk_buff_head *q;
1043 unsigned long flags;
1044 u32 lo, hi;
1045 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001046
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001047 mm_unaccount_pinned_pages(&uarg->mmp);
1048
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001049 /* if !len, there was only 1 call, and it was aborted
1050 * so do not queue a completion notification
1051 */
1052 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001053 goto release;
1054
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001055 len = uarg->len;
1056 lo = uarg->id;
1057 hi = uarg->id + len - 1;
1058
Willem de Bruijn52267792017-08-03 16:29:39 -04001059 serr = SKB_EXT_ERR(skb);
1060 memset(serr, 0, sizeof(*serr));
1061 serr->ee.ee_errno = 0;
1062 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001063 serr->ee.ee_data = hi;
1064 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001065 if (!success)
1066 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1067
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001068 q = &sk->sk_error_queue;
1069 spin_lock_irqsave(&q->lock, flags);
1070 tail = skb_peek_tail(q);
1071 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1072 !skb_zerocopy_notify_extend(tail, lo, len)) {
1073 __skb_queue_tail(q, skb);
1074 skb = NULL;
1075 }
1076 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001077
1078 sk->sk_error_report(sk);
1079
1080release:
1081 consume_skb(skb);
1082 sock_put(sk);
1083}
1084EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1085
1086void sock_zerocopy_put(struct ubuf_info *uarg)
1087{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001088 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001089 if (uarg->callback)
1090 uarg->callback(uarg, uarg->zerocopy);
1091 else
1092 consume_skb(skb_from_uarg(uarg));
1093 }
1094}
1095EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1096
Willem de Bruijn52900d22018-11-30 15:32:40 -05001097void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001098{
1099 if (uarg) {
1100 struct sock *sk = skb_from_uarg(uarg)->sk;
1101
1102 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001103 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001104
Willem de Bruijn52900d22018-11-30 15:32:40 -05001105 if (have_uref)
1106 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001107 }
1108}
1109EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1110
1111extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1112 struct iov_iter *from, size_t length);
1113
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001114int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1115{
1116 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1117}
1118EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1119
Willem de Bruijn52267792017-08-03 16:29:39 -04001120int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1121 struct msghdr *msg, int len,
1122 struct ubuf_info *uarg)
1123{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001124 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001125 struct iov_iter orig_iter = msg->msg_iter;
1126 int err, orig_len = skb->len;
1127
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001128 /* An skb can only point to one uarg. This edge case happens when
1129 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1130 */
1131 if (orig_uarg && uarg != orig_uarg)
1132 return -EEXIST;
1133
Willem de Bruijn52267792017-08-03 16:29:39 -04001134 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1135 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001136 struct sock *save_sk = skb->sk;
1137
Willem de Bruijn52267792017-08-03 16:29:39 -04001138 /* Streams do not free skb on error. Reset to prev state. */
1139 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001140 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001141 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001142 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001143 return err;
1144 }
1145
Willem de Bruijn52900d22018-11-30 15:32:40 -05001146 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001147 return skb->len - orig_len;
1148}
1149EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1150
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001151static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001152 gfp_t gfp_mask)
1153{
1154 if (skb_zcopy(orig)) {
1155 if (skb_zcopy(nskb)) {
1156 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1157 if (!gfp_mask) {
1158 WARN_ON_ONCE(1);
1159 return -ENOMEM;
1160 }
1161 if (skb_uarg(nskb) == skb_uarg(orig))
1162 return 0;
1163 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1164 return -EIO;
1165 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001166 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001167 }
1168 return 0;
1169}
1170
Ben Hutchings2c530402012-07-10 10:55:09 +00001171/**
1172 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001173 * @skb: the skb to modify
1174 * @gfp_mask: allocation priority
1175 *
1176 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1177 * It will copy all frags into kernel and drop the reference
1178 * to userspace pages.
1179 *
1180 * If this function is called from an interrupt gfp_mask() must be
1181 * %GFP_ATOMIC.
1182 *
1183 * Returns 0 on success or a negative error code on failure
1184 * to allocate kernel memory to copy to.
1185 */
1186int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001187{
Shirley Maa6686f22011-07-06 12:22:12 +00001188 int num_frags = skb_shinfo(skb)->nr_frags;
1189 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001190 int i, new_frags;
1191 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001192
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001193 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1194 return -EINVAL;
1195
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001196 if (!num_frags)
1197 goto release;
1198
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001199 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1200 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001201 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001202 if (!page) {
1203 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001204 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001205 put_page(head);
1206 head = next;
1207 }
1208 return -ENOMEM;
1209 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001210 set_page_private(page, (unsigned long)head);
1211 head = page;
1212 }
1213
1214 page = head;
1215 d_off = 0;
1216 for (i = 0; i < num_frags; i++) {
1217 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1218 u32 p_off, p_len, copied;
1219 struct page *p;
1220 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001221
1222 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1223 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001224 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001225 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001226
1227 while (done < p_len) {
1228 if (d_off == PAGE_SIZE) {
1229 d_off = 0;
1230 page = (struct page *)page_private(page);
1231 }
1232 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1233 memcpy(page_address(page) + d_off,
1234 vaddr + p_off + done, copy);
1235 done += copy;
1236 d_off += copy;
1237 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001238 kunmap_atomic(vaddr);
1239 }
Shirley Maa6686f22011-07-06 12:22:12 +00001240 }
1241
1242 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001243 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001244 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001245
Shirley Maa6686f22011-07-06 12:22:12 +00001246 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001247 for (i = 0; i < new_frags - 1; i++) {
1248 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001249 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001250 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001251 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1252 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001253
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001254release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001255 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001256 return 0;
1257}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001258EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001259
Herbert Xue0053ec2007-10-14 00:37:52 -07001260/**
1261 * skb_clone - duplicate an sk_buff
1262 * @skb: buffer to clone
1263 * @gfp_mask: allocation priority
1264 *
1265 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1266 * copies share the same packet data but not structure. The new
1267 * buffer has a reference count of 1. If the allocation fails the
1268 * function returns %NULL otherwise the new buffer is returned.
1269 *
1270 * If this function is called from an interrupt gfp_mask() must be
1271 * %GFP_ATOMIC.
1272 */
1273
1274struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1275{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001276 struct sk_buff_fclones *fclones = container_of(skb,
1277 struct sk_buff_fclones,
1278 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001279 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001280
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001281 if (skb_orphan_frags(skb, gfp_mask))
1282 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001283
Herbert Xue0053ec2007-10-14 00:37:52 -07001284 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001285 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001286 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001287 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001288 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001289 if (skb_pfmemalloc(skb))
1290 gfp_mask |= __GFP_MEMALLOC;
1291
Herbert Xue0053ec2007-10-14 00:37:52 -07001292 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1293 if (!n)
1294 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001295
Herbert Xue0053ec2007-10-14 00:37:52 -07001296 n->fclone = SKB_FCLONE_UNAVAILABLE;
1297 }
1298
1299 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001301EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001303void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001304{
Eric Dumazet030737b2013-10-19 11:42:54 -07001305 /* Only adjust this if it actually is csum_start rather than csum */
1306 if (skb->ip_summed == CHECKSUM_PARTIAL)
1307 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001308 /* {transport,network,mac}_header and tail are relative to skb->head */
1309 skb->transport_header += off;
1310 skb->network_header += off;
1311 if (skb_mac_header_was_set(skb))
1312 skb->mac_header += off;
1313 skb->inner_transport_header += off;
1314 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001315 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001316}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001317EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001318
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001319void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Herbert Xudec18812007-10-14 00:37:30 -07001321 __copy_skb_header(new, old);
1322
Herbert Xu79671682006-06-22 02:40:14 -07001323 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1324 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1325 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001327EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Mel Gormanc93bdd02012-07-31 16:44:19 -07001329static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1330{
1331 if (skb_pfmemalloc(skb))
1332 return SKB_ALLOC_RX;
1333 return 0;
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/**
1337 * skb_copy - create private copy of an sk_buff
1338 * @skb: buffer to copy
1339 * @gfp_mask: allocation priority
1340 *
1341 * Make a copy of both an &sk_buff and its data. This is used when the
1342 * caller wishes to modify the data and needs a private copy of the
1343 * data to alter. Returns %NULL on failure or the pointer to the buffer
1344 * on success. The returned buffer has a reference count of 1.
1345 *
1346 * As by-product this function converts non-linear &sk_buff to linear
1347 * one, so that &sk_buff becomes completely private and caller is allowed
1348 * to modify all the data of returned buffer. This means that this
1349 * function is not recommended for use in circumstances when only
1350 * header is going to be modified. Use pskb_copy() instead.
1351 */
1352
Al Virodd0fc662005-10-07 07:46:04 +01001353struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001355 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001356 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001357 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1358 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!n)
1361 return NULL;
1362
1363 /* Set the data pointer */
1364 skb_reserve(n, headerlen);
1365 /* Set the tail pointer and length */
1366 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Tim Hansen9f77fad2017-10-09 11:37:59 -04001368 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001370 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return n;
1372}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001373EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001376 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001378 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001380 * @fclone: if true allocate the copy of the skb from the fclone
1381 * cache instead of the head cache; it is recommended to set this
1382 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
1384 * Make a copy of both an &sk_buff and part of its data, located
1385 * in header. Fragmented data remain shared. This is used when
1386 * the caller wishes to modify only header of &sk_buff and needs
1387 * private copy of the header to alter. Returns %NULL on failure
1388 * or the pointer to the buffer on success.
1389 * The returned buffer has a reference count of 1.
1390 */
1391
Octavian Purdilabad93e92014-06-12 01:36:26 +03001392struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1393 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Eric Dumazet117632e2011-12-03 21:39:53 +00001395 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001396 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1397 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!n)
1400 goto out;
1401
1402 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001403 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Set the tail pointer and length */
1405 skb_put(n, skb_headlen(skb));
1406 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001407 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Herbert Xu25f484a2006-11-07 14:57:15 -08001409 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 n->data_len = skb->data_len;
1411 n->len = skb->len;
1412
1413 if (skb_shinfo(skb)->nr_frags) {
1414 int i;
1415
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001416 if (skb_orphan_frags(skb, gfp_mask) ||
1417 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001418 kfree_skb(n);
1419 n = NULL;
1420 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1423 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001424 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426 skb_shinfo(n)->nr_frags = i;
1427 }
1428
David S. Miller21dc3302010-08-23 00:13:46 -07001429 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1431 skb_clone_fraglist(n);
1432 }
1433
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001434 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435out:
1436 return n;
1437}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001438EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/**
1441 * pskb_expand_head - reallocate header of &sk_buff
1442 * @skb: buffer to reallocate
1443 * @nhead: room to add at head
1444 * @ntail: room to add at tail
1445 * @gfp_mask: allocation priority
1446 *
Mathias Krausebc323832013-11-07 14:18:26 +01001447 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1448 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 * reference count of 1. Returns zero in the case of success or error,
1450 * if expansion failed. In the last case, &sk_buff is not changed.
1451 *
1452 * All the pointers pointing into skb header may change and must be
1453 * reloaded after call to this function.
1454 */
1455
Victor Fusco86a76ca2005-07-08 14:57:47 -07001456int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001457 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Eric Dumazet158f3232017-01-27 07:11:27 -08001459 int i, osize = skb_end_offset(skb);
1460 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001462 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Herbert Xu4edd87a2008-10-01 07:09:38 -07001464 BUG_ON(nhead < 0);
1465
Tim Hansen9f77fad2017-10-09 11:37:59 -04001466 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 size = SKB_DATA_ALIGN(size);
1469
Mel Gormanc93bdd02012-07-31 16:44:19 -07001470 if (skb_pfmemalloc(skb))
1471 gfp_mask |= __GFP_MEMALLOC;
1472 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1473 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!data)
1475 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001476 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001479 * optimized for the cases when header is void.
1480 */
1481 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1482
1483 memcpy((struct skb_shared_info *)(data + size),
1484 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001485 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Alexander Duyck3e245912012-05-04 14:26:51 +00001487 /*
1488 * if shinfo is shared we must drop the old head gracefully, but if it
1489 * is not we can just drop the old head and let the existing refcount
1490 * be since all we did is relocate the values
1491 */
1492 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001493 if (skb_orphan_frags(skb, gfp_mask))
1494 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001495 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001496 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001497 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001498 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Eric Dumazet1fd63042010-09-02 23:09:32 +00001500 if (skb_has_frag_list(skb))
1501 skb_clone_fraglist(skb);
1502
1503 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001504 } else {
1505 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 off = (data + nhead) - skb->head;
1508
1509 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001510 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001512#ifdef NET_SKBUFF_DATA_USES_OFFSET
1513 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001514 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001515#else
1516 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001517#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001518 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001519 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001521 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 skb->nohdr = 0;
1523 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001524
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001525 skb_metadata_clear(skb);
1526
Eric Dumazet158f3232017-01-27 07:11:27 -08001527 /* It is not generally safe to change skb->truesize.
1528 * For the moment, we really care of rx path, or
1529 * when skb is orphaned (not attached to a socket).
1530 */
1531 if (!skb->sk || skb->destructor == sock_edemux)
1532 skb->truesize += size - osize;
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535
Shirley Maa6686f22011-07-06 12:22:12 +00001536nofrags:
1537 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538nodata:
1539 return -ENOMEM;
1540}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001541EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543/* Make private copy of skb with writable head and some headroom */
1544
1545struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1546{
1547 struct sk_buff *skb2;
1548 int delta = headroom - skb_headroom(skb);
1549
1550 if (delta <= 0)
1551 skb2 = pskb_copy(skb, GFP_ATOMIC);
1552 else {
1553 skb2 = skb_clone(skb, GFP_ATOMIC);
1554 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1555 GFP_ATOMIC)) {
1556 kfree_skb(skb2);
1557 skb2 = NULL;
1558 }
1559 }
1560 return skb2;
1561}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001562EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564/**
1565 * skb_copy_expand - copy and expand sk_buff
1566 * @skb: buffer to copy
1567 * @newheadroom: new free bytes at head
1568 * @newtailroom: new free bytes at tail
1569 * @gfp_mask: allocation priority
1570 *
1571 * Make a copy of both an &sk_buff and its data and while doing so
1572 * allocate additional space.
1573 *
1574 * This is used when the caller wishes to modify the data and needs a
1575 * private copy of the data to alter as well as more space for new fields.
1576 * Returns %NULL on failure or the pointer to the buffer
1577 * on success. The returned buffer has a reference count of 1.
1578 *
1579 * You must pass %GFP_ATOMIC as the allocation priority if this function
1580 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 */
1582struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001583 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001584 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 /*
1587 * Allocate the copy buffer
1588 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001589 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1590 gfp_mask, skb_alloc_rx_flag(skb),
1591 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001592 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int head_copy_len, head_copy_off;
1594
1595 if (!n)
1596 return NULL;
1597
1598 skb_reserve(n, newheadroom);
1599
1600 /* Set the tail pointer and length */
1601 skb_put(n, skb->len);
1602
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001603 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 head_copy_off = 0;
1605 if (newheadroom <= head_copy_len)
1606 head_copy_len = newheadroom;
1607 else
1608 head_copy_off = newheadroom - head_copy_len;
1609
1610 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001611 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1612 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001614 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Eric Dumazet030737b2013-10-19 11:42:54 -07001616 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return n;
1619}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001620EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001623 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * @skb: buffer to pad
1625 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001626 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 *
1628 * Ensure that a buffer is followed by a padding area that is zero
1629 * filled. Used by network drivers which may DMA or transfer data
1630 * beyond the buffer end onto the wire.
1631 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001632 * May return error in out of memory cases. The skb is freed on error
1633 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001635
Florian Fainellicd0a1372017-08-22 15:12:14 -07001636int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Herbert Xu5b057c62006-06-23 02:06:41 -07001638 int err;
1639 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001642 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001646
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001647 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001648 if (likely(skb_cloned(skb) || ntail > 0)) {
1649 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1650 if (unlikely(err))
1651 goto free_skb;
1652 }
1653
1654 /* FIXME: The use of this function with non-linear skb's really needs
1655 * to be audited.
1656 */
1657 err = skb_linearize(skb);
1658 if (unlikely(err))
1659 goto free_skb;
1660
1661 memset(skb->data + skb->len, 0, pad);
1662 return 0;
1663
1664free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001665 if (free_on_error)
1666 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001667 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001668}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001669EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001670
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001671/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001672 * pskb_put - add data to the tail of a potentially fragmented buffer
1673 * @skb: start of the buffer to use
1674 * @tail: tail fragment of the buffer to use
1675 * @len: amount of data to add
1676 *
1677 * This function extends the used data area of the potentially
1678 * fragmented buffer. @tail must be the last fragment of @skb -- or
1679 * @skb itself. If this would exceed the total buffer size the kernel
1680 * will panic. A pointer to the first byte of the extra data is
1681 * returned.
1682 */
1683
Johannes Berg4df864c2017-06-16 14:29:21 +02001684void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001685{
1686 if (tail != skb) {
1687 skb->data_len += len;
1688 skb->len += len;
1689 }
1690 return skb_put(tail, len);
1691}
1692EXPORT_SYMBOL_GPL(pskb_put);
1693
1694/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001695 * skb_put - add data to a buffer
1696 * @skb: buffer to use
1697 * @len: amount of data to add
1698 *
1699 * This function extends the used data area of the buffer. If this would
1700 * exceed the total buffer size the kernel will panic. A pointer to the
1701 * first byte of the extra data is returned.
1702 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001703void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001704{
Johannes Berg4df864c2017-06-16 14:29:21 +02001705 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001706 SKB_LINEAR_ASSERT(skb);
1707 skb->tail += len;
1708 skb->len += len;
1709 if (unlikely(skb->tail > skb->end))
1710 skb_over_panic(skb, len, __builtin_return_address(0));
1711 return tmp;
1712}
1713EXPORT_SYMBOL(skb_put);
1714
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001715/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001716 * skb_push - add data to the start of a buffer
1717 * @skb: buffer to use
1718 * @len: amount of data to add
1719 *
1720 * This function extends the used data area of the buffer at the buffer
1721 * start. If this would exceed the total buffer headroom the kernel will
1722 * panic. A pointer to the first byte of the extra data is returned.
1723 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001724void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001725{
1726 skb->data -= len;
1727 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301728 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001729 skb_under_panic(skb, len, __builtin_return_address(0));
1730 return skb->data;
1731}
1732EXPORT_SYMBOL(skb_push);
1733
1734/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001735 * skb_pull - remove data from the start of a buffer
1736 * @skb: buffer to use
1737 * @len: amount of data to remove
1738 *
1739 * This function removes data from the start of a buffer, returning
1740 * the memory to the headroom. A pointer to the next data in the buffer
1741 * is returned. Once the data has been pulled future pushes will overwrite
1742 * the old data.
1743 */
Johannes Bergaf728682017-06-16 14:29:22 +02001744void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001745{
David S. Miller47d29642010-05-02 02:21:44 -07001746 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001747}
1748EXPORT_SYMBOL(skb_pull);
1749
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001750/**
1751 * skb_trim - remove end from a buffer
1752 * @skb: buffer to alter
1753 * @len: new length
1754 *
1755 * Cut the length of a buffer down by removing data from the tail. If
1756 * the buffer is already under the length specified it is not modified.
1757 * The skb must be linear.
1758 */
1759void skb_trim(struct sk_buff *skb, unsigned int len)
1760{
1761 if (skb->len > len)
1762 __skb_trim(skb, len);
1763}
1764EXPORT_SYMBOL(skb_trim);
1765
Herbert Xu3cc0e872006-06-09 16:13:38 -07001766/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
1768
Herbert Xu3cc0e872006-06-09 16:13:38 -07001769int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Herbert Xu27b437c2006-07-13 19:26:39 -07001771 struct sk_buff **fragp;
1772 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 int offset = skb_headlen(skb);
1774 int nfrags = skb_shinfo(skb)->nr_frags;
1775 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001776 int err;
1777
1778 if (skb_cloned(skb) &&
1779 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001782 i = 0;
1783 if (offset >= len)
1784 goto drop_pages;
1785
1786 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001787 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001788
1789 if (end < len) {
1790 offset = end;
1791 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001793
Eric Dumazet9e903e02011-10-18 21:00:24 +00001794 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001795
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001796drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001797 skb_shinfo(skb)->nr_frags = i;
1798
1799 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001800 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001801
David S. Miller21dc3302010-08-23 00:13:46 -07001802 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001803 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001804 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
Herbert Xu27b437c2006-07-13 19:26:39 -07001807 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1808 fragp = &frag->next) {
1809 int end = offset + frag->len;
1810
1811 if (skb_shared(frag)) {
1812 struct sk_buff *nfrag;
1813
1814 nfrag = skb_clone(frag, GFP_ATOMIC);
1815 if (unlikely(!nfrag))
1816 return -ENOMEM;
1817
1818 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001819 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001820 frag = nfrag;
1821 *fragp = frag;
1822 }
1823
1824 if (end < len) {
1825 offset = end;
1826 continue;
1827 }
1828
1829 if (end > len &&
1830 unlikely((err = pskb_trim(frag, len - offset))))
1831 return err;
1832
1833 if (frag->next)
1834 skb_drop_list(&frag->next);
1835 break;
1836 }
1837
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001838done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001839 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 skb->data_len -= skb->len - len;
1841 skb->len = len;
1842 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001843 skb->len = len;
1844 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001845 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001848 if (!skb->sk || skb->destructor == sock_edemux)
1849 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 0;
1851}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001852EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Eric Dumazet88078d92018-04-18 11:43:15 -07001854/* Note : use pskb_trim_rcsum() instead of calling this directly
1855 */
1856int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1857{
1858 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1859 int delta = skb->len - len;
1860
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07001861 skb->csum = csum_block_sub(skb->csum,
1862 skb_checksum(skb, len, delta, 0),
1863 len);
Eric Dumazet88078d92018-04-18 11:43:15 -07001864 }
1865 return __pskb_trim(skb, len);
1866}
1867EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/**
1870 * __pskb_pull_tail - advance tail of skb header
1871 * @skb: buffer to reallocate
1872 * @delta: number of bytes to advance tail
1873 *
1874 * The function makes a sense only on a fragmented &sk_buff,
1875 * it expands header moving its tail forward and copying necessary
1876 * data from fragmented part.
1877 *
1878 * &sk_buff MUST have reference count of 1.
1879 *
1880 * Returns %NULL (and &sk_buff does not change) if pull failed
1881 * or value of new tail of skb in the case of success.
1882 *
1883 * All the pointers pointing into skb header may change and must be
1884 * reloaded after call to this function.
1885 */
1886
1887/* Moves tail of skb head forward, copying data from fragmented part,
1888 * when it is necessary.
1889 * 1. It may fail due to malloc failure.
1890 * 2. It may change skb pointers.
1891 *
1892 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1893 */
Johannes Bergaf728682017-06-16 14:29:22 +02001894void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 /* If skb has not enough free space at tail, get new one
1897 * plus 128 bytes for future expansions. If we have enough
1898 * room at tail, reallocate without expansion only if skb is cloned.
1899 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001900 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 if (eat > 0 || skb_cloned(skb)) {
1903 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1904 GFP_ATOMIC))
1905 return NULL;
1906 }
1907
Tim Hansen9f77fad2017-10-09 11:37:59 -04001908 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1909 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* Optimization: no fragments, no reasons to preestimate
1912 * size of pulled pages. Superb.
1913 */
David S. Miller21dc3302010-08-23 00:13:46 -07001914 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto pull_pages;
1916
1917 /* Estimate size of pulled pages. */
1918 eat = delta;
1919 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001920 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1921
1922 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001924 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
1927 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02001928 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * but taking into account that pulling is expected to
1930 * be very rare operation, it is worth to fight against
1931 * further bloating skb head and crucify ourselves here instead.
1932 * Pure masohism, indeed. 8)8)
1933 */
1934 if (eat) {
1935 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1936 struct sk_buff *clone = NULL;
1937 struct sk_buff *insp = NULL;
1938
1939 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (list->len <= eat) {
1941 /* Eaten as whole. */
1942 eat -= list->len;
1943 list = list->next;
1944 insp = list;
1945 } else {
1946 /* Eaten partially. */
1947
1948 if (skb_shared(list)) {
1949 /* Sucks! We need to fork list. :-( */
1950 clone = skb_clone(list, GFP_ATOMIC);
1951 if (!clone)
1952 return NULL;
1953 insp = list->next;
1954 list = clone;
1955 } else {
1956 /* This may be pulled without
1957 * problems. */
1958 insp = list;
1959 }
1960 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001961 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return NULL;
1963 }
1964 break;
1965 }
1966 } while (eat);
1967
1968 /* Free pulled out fragments. */
1969 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1970 skb_shinfo(skb)->frag_list = list->next;
1971 kfree_skb(list);
1972 }
1973 /* And insert new clone at head. */
1974 if (clone) {
1975 clone->next = list;
1976 skb_shinfo(skb)->frag_list = clone;
1977 }
1978 }
1979 /* Success! Now we may commit changes to skb data. */
1980
1981pull_pages:
1982 eat = delta;
1983 k = 0;
1984 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001985 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1986
1987 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001988 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001989 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else {
1991 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1992 if (eat) {
1993 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001994 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001995 if (!i)
1996 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 eat = 0;
1998 }
1999 k++;
2000 }
2001 }
2002 skb_shinfo(skb)->nr_frags = k;
2003
linzhang3ccc6c62017-07-17 17:25:02 +08002004end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 skb->tail += delta;
2006 skb->data_len -= delta;
2007
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002008 if (!skb->data_len)
2009 skb_zcopy_clear(skb, false);
2010
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002011 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002013EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Eric Dumazet22019b12011-07-29 18:37:31 +00002015/**
2016 * skb_copy_bits - copy bits from skb to kernel buffer
2017 * @skb: source skb
2018 * @offset: offset in source
2019 * @to: destination buffer
2020 * @len: number of bytes to copy
2021 *
2022 * Copy the specified number of bytes from the source skb to the
2023 * destination buffer.
2024 *
2025 * CAUTION ! :
2026 * If its prototype is ever changed,
2027 * check arch/{*}/net/{*}.S files,
2028 * since it is called from BPF assembly code.
2029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2031{
David S. Miller1a028e52007-04-27 15:21:23 -07002032 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002033 struct sk_buff *frag_iter;
2034 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 if (offset > (int)skb->len - len)
2037 goto fault;
2038
2039 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002040 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (copy > len)
2042 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002043 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if ((len -= copy) == 0)
2045 return 0;
2046 offset += copy;
2047 to += copy;
2048 }
2049
2050 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002051 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002052 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002054 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002055
Eric Dumazet51c56b02012-04-05 11:35:15 +02002056 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002058 u32 p_off, p_len, copied;
2059 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 u8 *vaddr;
2061
2062 if (copy > len)
2063 copy = len;
2064
Willem de Bruijnc613c202017-07-31 08:15:47 -04002065 skb_frag_foreach_page(f,
2066 f->page_offset + offset - start,
2067 copy, p, p_off, p_len, copied) {
2068 vaddr = kmap_atomic(p);
2069 memcpy(to + copied, vaddr + p_off, p_len);
2070 kunmap_atomic(vaddr);
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if ((len -= copy) == 0)
2074 return 0;
2075 offset += copy;
2076 to += copy;
2077 }
David S. Miller1a028e52007-04-27 15:21:23 -07002078 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080
David S. Millerfbb398a2009-06-09 00:18:59 -07002081 skb_walk_frags(skb, frag_iter) {
2082 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
David S. Millerfbb398a2009-06-09 00:18:59 -07002084 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
David S. Millerfbb398a2009-06-09 00:18:59 -07002086 end = start + frag_iter->len;
2087 if ((copy = end - offset) > 0) {
2088 if (copy > len)
2089 copy = len;
2090 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2091 goto fault;
2092 if ((len -= copy) == 0)
2093 return 0;
2094 offset += copy;
2095 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002097 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Shirley Maa6686f22011-07-06 12:22:12 +00002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (!len)
2101 return 0;
2102
2103fault:
2104 return -EFAULT;
2105}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002106EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Jens Axboe9c55e012007-11-06 23:30:13 -08002108/*
2109 * Callback from splice_to_pipe(), if we need to release some pages
2110 * at the end of the spd in case we error'ed out in filling the pipe.
2111 */
2112static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2113{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002114 put_page(spd->pages[i]);
2115}
Jens Axboe9c55e012007-11-06 23:30:13 -08002116
David S. Millera108d5f2012-04-23 23:06:11 -04002117static struct page *linear_to_page(struct page *page, unsigned int *len,
2118 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002119 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002120{
Eric Dumazet5640f762012-09-23 23:04:42 +00002121 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002122
Eric Dumazet5640f762012-09-23 23:04:42 +00002123 if (!sk_page_frag_refill(sk, pfrag))
2124 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002125
Eric Dumazet5640f762012-09-23 23:04:42 +00002126 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002127
Eric Dumazet5640f762012-09-23 23:04:42 +00002128 memcpy(page_address(pfrag->page) + pfrag->offset,
2129 page_address(page) + *offset, *len);
2130 *offset = pfrag->offset;
2131 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002132
Eric Dumazet5640f762012-09-23 23:04:42 +00002133 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002134}
2135
Eric Dumazet41c73a02012-04-22 12:26:16 +00002136static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2137 struct page *page,
2138 unsigned int offset)
2139{
2140 return spd->nr_pages &&
2141 spd->pages[spd->nr_pages - 1] == page &&
2142 (spd->partial[spd->nr_pages - 1].offset +
2143 spd->partial[spd->nr_pages - 1].len == offset);
2144}
2145
Jens Axboe9c55e012007-11-06 23:30:13 -08002146/*
2147 * Fill page/offset/length into spd, if it can hold more pages.
2148 */
David S. Millera108d5f2012-04-23 23:06:11 -04002149static bool spd_fill_page(struct splice_pipe_desc *spd,
2150 struct pipe_inode_info *pipe, struct page *page,
2151 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002152 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002153 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002154{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002155 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002156 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002157
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002158 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002159 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002160 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002161 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002162 }
2163 if (spd_can_coalesce(spd, page, offset)) {
2164 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002165 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002166 }
2167 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002168 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002169 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002170 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002171 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002172
David S. Millera108d5f2012-04-23 23:06:11 -04002173 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002174}
2175
David S. Millera108d5f2012-04-23 23:06:11 -04002176static bool __splice_segment(struct page *page, unsigned int poff,
2177 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002178 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002179 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002180 struct sock *sk,
2181 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002182{
2183 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002184 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002185
2186 /* skip this segment if already processed */
2187 if (*off >= plen) {
2188 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002189 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002190 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002191
Octavian Purdila2870c432008-07-15 00:49:11 -07002192 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002193 poff += *off;
2194 plen -= *off;
2195 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002196
Eric Dumazet18aafc62013-01-11 14:46:37 +00002197 do {
2198 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002199
Eric Dumazet18aafc62013-01-11 14:46:37 +00002200 if (spd_fill_page(spd, pipe, page, &flen, poff,
2201 linear, sk))
2202 return true;
2203 poff += flen;
2204 plen -= flen;
2205 *len -= flen;
2206 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002207
David S. Millera108d5f2012-04-23 23:06:11 -04002208 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002209}
2210
2211/*
David S. Millera108d5f2012-04-23 23:06:11 -04002212 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002213 * pipe is full or if we already spliced the requested length.
2214 */
David S. Millera108d5f2012-04-23 23:06:11 -04002215static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2216 unsigned int *offset, unsigned int *len,
2217 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002218{
2219 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002220 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002221
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002222 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002223 * If skb->head_frag is set, this 'linear' part is backed by a
2224 * fragment, and if the head is not shared with any clones then
2225 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002226 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002227 if (__splice_segment(virt_to_page(skb->data),
2228 (unsigned long) skb->data & (PAGE_SIZE - 1),
2229 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002230 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002231 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002232 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002233 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002234
2235 /*
2236 * then map the fragments
2237 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002238 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2239 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2240
Ian Campbellea2ab692011-08-22 23:44:58 +00002241 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002242 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002243 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002244 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002245 }
2246
Tom Herbertfa9835e2016-03-07 14:11:04 -08002247 skb_walk_frags(skb, iter) {
2248 if (*offset >= iter->len) {
2249 *offset -= iter->len;
2250 continue;
2251 }
2252 /* __skb_splice_bits() only fails if the output has no room
2253 * left, so no point in going over the frag_list for the error
2254 * case.
2255 */
2256 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2257 return true;
2258 }
2259
David S. Millera108d5f2012-04-23 23:06:11 -04002260 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002261}
2262
2263/*
2264 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002265 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002266 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002267int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002268 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002269 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002270{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002271 struct partial_page partial[MAX_SKB_FRAGS];
2272 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002273 struct splice_pipe_desc spd = {
2274 .pages = pages,
2275 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002276 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002277 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002278 .spd_release = sock_spd_release,
2279 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002280 int ret = 0;
2281
Tom Herbertfa9835e2016-03-07 14:11:04 -08002282 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002283
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002284 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002285 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002286
Jens Axboe35f3d142010-05-20 10:43:18 +02002287 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002288}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002289EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002290
Tom Herbert20bf50d2017-07-28 16:22:42 -07002291/* Send skb data on a socket. Socket must be locked. */
2292int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2293 int len)
2294{
2295 unsigned int orig_len = len;
2296 struct sk_buff *head = skb;
2297 unsigned short fragidx;
2298 int slen, ret;
2299
2300do_frag_list:
2301
2302 /* Deal with head data */
2303 while (offset < skb_headlen(skb) && len) {
2304 struct kvec kv;
2305 struct msghdr msg;
2306
2307 slen = min_t(int, len, skb_headlen(skb) - offset);
2308 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002309 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002310 memset(&msg, 0, sizeof(msg));
2311
2312 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2313 if (ret <= 0)
2314 goto error;
2315
2316 offset += ret;
2317 len -= ret;
2318 }
2319
2320 /* All the data was skb head? */
2321 if (!len)
2322 goto out;
2323
2324 /* Make offset relative to start of frags */
2325 offset -= skb_headlen(skb);
2326
2327 /* Find where we are in frag list */
2328 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2329 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2330
2331 if (offset < frag->size)
2332 break;
2333
2334 offset -= frag->size;
2335 }
2336
2337 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2338 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2339
2340 slen = min_t(size_t, len, frag->size - offset);
2341
2342 while (slen) {
2343 ret = kernel_sendpage_locked(sk, frag->page.p,
2344 frag->page_offset + offset,
2345 slen, MSG_DONTWAIT);
2346 if (ret <= 0)
2347 goto error;
2348
2349 len -= ret;
2350 offset += ret;
2351 slen -= ret;
2352 }
2353
2354 offset = 0;
2355 }
2356
2357 if (len) {
2358 /* Process any frag lists */
2359
2360 if (skb == head) {
2361 if (skb_has_frag_list(skb)) {
2362 skb = skb_shinfo(skb)->frag_list;
2363 goto do_frag_list;
2364 }
2365 } else if (skb->next) {
2366 skb = skb->next;
2367 goto do_frag_list;
2368 }
2369 }
2370
2371out:
2372 return orig_len - len;
2373
2374error:
2375 return orig_len == len ? ret : orig_len - len;
2376}
2377EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2378
Herbert Xu357b40a2005-04-19 22:30:14 -07002379/**
2380 * skb_store_bits - store bits from kernel buffer to skb
2381 * @skb: destination buffer
2382 * @offset: offset in destination
2383 * @from: source buffer
2384 * @len: number of bytes to copy
2385 *
2386 * Copy the specified number of bytes from the source buffer to the
2387 * destination skb. This function handles all the messy bits of
2388 * traversing fragment lists and such.
2389 */
2390
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002391int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002392{
David S. Miller1a028e52007-04-27 15:21:23 -07002393 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002394 struct sk_buff *frag_iter;
2395 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002396
2397 if (offset > (int)skb->len - len)
2398 goto fault;
2399
David S. Miller1a028e52007-04-27 15:21:23 -07002400 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002401 if (copy > len)
2402 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002403 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002404 if ((len -= copy) == 0)
2405 return 0;
2406 offset += copy;
2407 from += copy;
2408 }
2409
2410 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2411 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002412 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002413
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002414 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002415
Eric Dumazet9e903e02011-10-18 21:00:24 +00002416 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002417 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002418 u32 p_off, p_len, copied;
2419 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002420 u8 *vaddr;
2421
2422 if (copy > len)
2423 copy = len;
2424
Willem de Bruijnc613c202017-07-31 08:15:47 -04002425 skb_frag_foreach_page(frag,
2426 frag->page_offset + offset - start,
2427 copy, p, p_off, p_len, copied) {
2428 vaddr = kmap_atomic(p);
2429 memcpy(vaddr + p_off, from + copied, p_len);
2430 kunmap_atomic(vaddr);
2431 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002432
2433 if ((len -= copy) == 0)
2434 return 0;
2435 offset += copy;
2436 from += copy;
2437 }
David S. Miller1a028e52007-04-27 15:21:23 -07002438 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002439 }
2440
David S. Millerfbb398a2009-06-09 00:18:59 -07002441 skb_walk_frags(skb, frag_iter) {
2442 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002443
David S. Millerfbb398a2009-06-09 00:18:59 -07002444 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002445
David S. Millerfbb398a2009-06-09 00:18:59 -07002446 end = start + frag_iter->len;
2447 if ((copy = end - offset) > 0) {
2448 if (copy > len)
2449 copy = len;
2450 if (skb_store_bits(frag_iter, offset - start,
2451 from, copy))
2452 goto fault;
2453 if ((len -= copy) == 0)
2454 return 0;
2455 offset += copy;
2456 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002457 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002458 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002459 }
2460 if (!len)
2461 return 0;
2462
2463fault:
2464 return -EFAULT;
2465}
Herbert Xu357b40a2005-04-19 22:30:14 -07002466EXPORT_SYMBOL(skb_store_bits);
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002469__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2470 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
David S. Miller1a028e52007-04-27 15:21:23 -07002472 int start = skb_headlen(skb);
2473 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002474 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 int pos = 0;
2476
2477 /* Checksum header. */
2478 if (copy > 0) {
2479 if (copy > len)
2480 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002481 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if ((len -= copy) == 0)
2483 return csum;
2484 offset += copy;
2485 pos = copy;
2486 }
2487
2488 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002489 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002490 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002492 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002493
Eric Dumazet51c56b02012-04-05 11:35:15 +02002494 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002496 u32 p_off, p_len, copied;
2497 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002498 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 if (copy > len)
2502 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002503
2504 skb_frag_foreach_page(frag,
2505 frag->page_offset + offset - start,
2506 copy, p, p_off, p_len, copied) {
2507 vaddr = kmap_atomic(p);
2508 csum2 = ops->update(vaddr + p_off, p_len, 0);
2509 kunmap_atomic(vaddr);
2510 csum = ops->combine(csum, csum2, pos, p_len);
2511 pos += p_len;
2512 }
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (!(len -= copy))
2515 return csum;
2516 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
David S. Miller1a028e52007-04-27 15:21:23 -07002518 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520
David S. Millerfbb398a2009-06-09 00:18:59 -07002521 skb_walk_frags(skb, frag_iter) {
2522 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
David S. Millerfbb398a2009-06-09 00:18:59 -07002524 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
David S. Millerfbb398a2009-06-09 00:18:59 -07002526 end = start + frag_iter->len;
2527 if ((copy = end - offset) > 0) {
2528 __wsum csum2;
2529 if (copy > len)
2530 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002531 csum2 = __skb_checksum(frag_iter, offset - start,
2532 copy, 0, ops);
2533 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002534 if ((len -= copy) == 0)
2535 return csum;
2536 offset += copy;
2537 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002539 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002541 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 return csum;
2544}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002545EXPORT_SYMBOL(__skb_checksum);
2546
2547__wsum skb_checksum(const struct sk_buff *skb, int offset,
2548 int len, __wsum csum)
2549{
2550 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002551 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002552 .combine = csum_block_add_ext,
2553 };
2554
2555 return __skb_checksum(skb, offset, len, csum, &ops);
2556}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002557EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559/* Both of above in one bottle. */
2560
Al Viro81d77662006-11-14 21:37:33 -08002561__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2562 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
David S. Miller1a028e52007-04-27 15:21:23 -07002564 int start = skb_headlen(skb);
2565 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002566 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 int pos = 0;
2568
2569 /* Copy header. */
2570 if (copy > 0) {
2571 if (copy > len)
2572 copy = len;
2573 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2574 copy, csum);
2575 if ((len -= copy) == 0)
2576 return csum;
2577 offset += copy;
2578 to += copy;
2579 pos = copy;
2580 }
2581
2582 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002583 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002585 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002586
Eric Dumazet9e903e02011-10-18 21:00:24 +00002587 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002589 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2590 u32 p_off, p_len, copied;
2591 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002592 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 if (copy > len)
2596 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002597
2598 skb_frag_foreach_page(frag,
2599 frag->page_offset + offset - start,
2600 copy, p, p_off, p_len, copied) {
2601 vaddr = kmap_atomic(p);
2602 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2603 to + copied,
2604 p_len, 0);
2605 kunmap_atomic(vaddr);
2606 csum = csum_block_add(csum, csum2, pos);
2607 pos += p_len;
2608 }
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (!(len -= copy))
2611 return csum;
2612 offset += copy;
2613 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
David S. Miller1a028e52007-04-27 15:21:23 -07002615 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617
David S. Millerfbb398a2009-06-09 00:18:59 -07002618 skb_walk_frags(skb, frag_iter) {
2619 __wsum csum2;
2620 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
David S. Millerfbb398a2009-06-09 00:18:59 -07002622 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
David S. Millerfbb398a2009-06-09 00:18:59 -07002624 end = start + frag_iter->len;
2625 if ((copy = end - offset) > 0) {
2626 if (copy > len)
2627 copy = len;
2628 csum2 = skb_copy_and_csum_bits(frag_iter,
2629 offset - start,
2630 to, copy, 0);
2631 csum = csum_block_add(csum, csum2, pos);
2632 if ((len -= copy) == 0)
2633 return csum;
2634 offset += copy;
2635 to += copy;
2636 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002638 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002640 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return csum;
2642}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002643EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Cong Wang49f8e832018-11-08 14:05:42 -08002645__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2646{
2647 __sum16 sum;
2648
2649 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002650 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002651 if (likely(!sum)) {
2652 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2653 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002654 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002655 }
2656 if (!skb_shared(skb))
2657 skb->csum_valid = !sum;
2658 return sum;
2659}
2660EXPORT_SYMBOL(__skb_checksum_complete_head);
2661
Cong Wang14641932018-11-26 09:31:26 -08002662/* This function assumes skb->csum already holds pseudo header's checksum,
2663 * which has been changed from the hardware checksum, for example, by
2664 * __skb_checksum_validate_complete(). And, the original skb->csum must
2665 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2666 *
2667 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2668 * zero. The new checksum is stored back into skb->csum unless the skb is
2669 * shared.
2670 */
Cong Wang49f8e832018-11-08 14:05:42 -08002671__sum16 __skb_checksum_complete(struct sk_buff *skb)
2672{
2673 __wsum csum;
2674 __sum16 sum;
2675
2676 csum = skb_checksum(skb, 0, skb->len, 0);
2677
Cong Wang49f8e832018-11-08 14:05:42 -08002678 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002679 /* This check is inverted, because we already knew the hardware
2680 * checksum is invalid before calling this function. So, if the
2681 * re-computed checksum is valid instead, then we have a mismatch
2682 * between the original skb->csum and skb_checksum(). This means either
2683 * the original hardware checksum is incorrect or we screw up skb->csum
2684 * when moving skb->data around.
2685 */
Cong Wang49f8e832018-11-08 14:05:42 -08002686 if (likely(!sum)) {
2687 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2688 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002689 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002690 }
2691
2692 if (!skb_shared(skb)) {
2693 /* Save full packet checksum */
2694 skb->csum = csum;
2695 skb->ip_summed = CHECKSUM_COMPLETE;
2696 skb->csum_complete_sw = 1;
2697 skb->csum_valid = !sum;
2698 }
2699
2700 return sum;
2701}
2702EXPORT_SYMBOL(__skb_checksum_complete);
2703
Davide Caratti96178132017-05-18 15:44:37 +02002704static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2705{
2706 net_warn_ratelimited(
2707 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2708 __func__);
2709 return 0;
2710}
2711
2712static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2713 int offset, int len)
2714{
2715 net_warn_ratelimited(
2716 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2717 __func__);
2718 return 0;
2719}
2720
2721static const struct skb_checksum_ops default_crc32c_ops = {
2722 .update = warn_crc32c_csum_update,
2723 .combine = warn_crc32c_csum_combine,
2724};
2725
2726const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2727 &default_crc32c_ops;
2728EXPORT_SYMBOL(crc32c_csum_stub);
2729
Thomas Grafaf2806f2013-12-13 15:22:17 +01002730 /**
2731 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2732 * @from: source buffer
2733 *
2734 * Calculates the amount of linear headroom needed in the 'to' skb passed
2735 * into skb_zerocopy().
2736 */
2737unsigned int
2738skb_zerocopy_headlen(const struct sk_buff *from)
2739{
2740 unsigned int hlen = 0;
2741
2742 if (!from->head_frag ||
2743 skb_headlen(from) < L1_CACHE_BYTES ||
2744 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2745 hlen = skb_headlen(from);
2746
2747 if (skb_has_frag_list(from))
2748 hlen = from->len;
2749
2750 return hlen;
2751}
2752EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2753
2754/**
2755 * skb_zerocopy - Zero copy skb to skb
2756 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002757 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002758 * @len: number of bytes to copy from source buffer
2759 * @hlen: size of linear headroom in destination buffer
2760 *
2761 * Copies up to `len` bytes from `from` to `to` by creating references
2762 * to the frags in the source buffer.
2763 *
2764 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2765 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002766 *
2767 * Return value:
2768 * 0: everything is OK
2769 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2770 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002771 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002772int
2773skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002774{
2775 int i, j = 0;
2776 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002777 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002778 struct page *page;
2779 unsigned int offset;
2780
2781 BUG_ON(!from->head_frag && !hlen);
2782
2783 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002784 if (len <= skb_tailroom(to))
2785 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002786
2787 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002788 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2789 if (unlikely(ret))
2790 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002791 len -= hlen;
2792 } else {
2793 plen = min_t(int, skb_headlen(from), len);
2794 if (plen) {
2795 page = virt_to_head_page(from->head);
2796 offset = from->data - (unsigned char *)page_address(page);
2797 __skb_fill_page_desc(to, 0, page, offset, plen);
2798 get_page(page);
2799 j = 1;
2800 len -= plen;
2801 }
2802 }
2803
2804 to->truesize += len + plen;
2805 to->len += len + plen;
2806 to->data_len += len + plen;
2807
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002808 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2809 skb_tx_error(from);
2810 return -ENOMEM;
2811 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002812 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002813
Thomas Grafaf2806f2013-12-13 15:22:17 +01002814 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2815 if (!len)
2816 break;
2817 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2818 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2819 len -= skb_shinfo(to)->frags[j].size;
2820 skb_frag_ref(to, j);
2821 j++;
2822 }
2823 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002824
2825 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002826}
2827EXPORT_SYMBOL_GPL(skb_zerocopy);
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2830{
Al Virod3bc23e2006-11-14 21:24:49 -08002831 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 long csstart;
2833
Patrick McHardy84fa7932006-08-29 16:44:56 -07002834 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002835 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 else
2837 csstart = skb_headlen(skb);
2838
Kris Katterjohn09a62662006-01-08 22:24:28 -08002839 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002841 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 csum = 0;
2844 if (csstart != skb->len)
2845 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2846 skb->len - csstart, 0);
2847
Patrick McHardy84fa7932006-08-29 16:44:56 -07002848 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002849 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Al Virod3bc23e2006-11-14 21:24:49 -08002851 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002854EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856/**
2857 * skb_dequeue - remove from the head of the queue
2858 * @list: list to dequeue from
2859 *
2860 * Remove the head of the list. The list lock is taken so the function
2861 * may be used safely with other locking list functions. The head item is
2862 * returned or %NULL if the list is empty.
2863 */
2864
2865struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2866{
2867 unsigned long flags;
2868 struct sk_buff *result;
2869
2870 spin_lock_irqsave(&list->lock, flags);
2871 result = __skb_dequeue(list);
2872 spin_unlock_irqrestore(&list->lock, flags);
2873 return result;
2874}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002875EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877/**
2878 * skb_dequeue_tail - remove from the tail of the queue
2879 * @list: list to dequeue from
2880 *
2881 * Remove the tail of the list. The list lock is taken so the function
2882 * may be used safely with other locking list functions. The tail item is
2883 * returned or %NULL if the list is empty.
2884 */
2885struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2886{
2887 unsigned long flags;
2888 struct sk_buff *result;
2889
2890 spin_lock_irqsave(&list->lock, flags);
2891 result = __skb_dequeue_tail(list);
2892 spin_unlock_irqrestore(&list->lock, flags);
2893 return result;
2894}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002895EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897/**
2898 * skb_queue_purge - empty a list
2899 * @list: list to empty
2900 *
2901 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2902 * the list and one reference dropped. This function takes the list
2903 * lock and is atomic with respect to other list locking functions.
2904 */
2905void skb_queue_purge(struct sk_buff_head *list)
2906{
2907 struct sk_buff *skb;
2908 while ((skb = skb_dequeue(list)) != NULL)
2909 kfree_skb(skb);
2910}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002911EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002914 * skb_rbtree_purge - empty a skb rbtree
2915 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00002916 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002917 *
2918 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2919 * the list and one reference dropped. This function does not take
2920 * any lock. Synchronization should be handled by the caller (e.g., TCP
2921 * out-of-order queue is protected by the socket lock).
2922 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00002923unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002924{
Eric Dumazet7c905842017-09-23 12:39:12 -07002925 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002926 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002927
Eric Dumazet7c905842017-09-23 12:39:12 -07002928 while (p) {
2929 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2930
2931 p = rb_next(p);
2932 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002933 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002934 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07002935 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00002936 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002937}
2938
2939/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 * skb_queue_head - queue a buffer at the list head
2941 * @list: list to use
2942 * @newsk: buffer to queue
2943 *
2944 * Queue a buffer at the start of the list. This function takes the
2945 * list lock and can be used safely with other locking &sk_buff functions
2946 * safely.
2947 *
2948 * A buffer cannot be placed on two lists at the same time.
2949 */
2950void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2951{
2952 unsigned long flags;
2953
2954 spin_lock_irqsave(&list->lock, flags);
2955 __skb_queue_head(list, newsk);
2956 spin_unlock_irqrestore(&list->lock, flags);
2957}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002958EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960/**
2961 * skb_queue_tail - queue a buffer at the list tail
2962 * @list: list to use
2963 * @newsk: buffer to queue
2964 *
2965 * Queue a buffer at the tail of the list. This function takes the
2966 * list lock and can be used safely with other locking &sk_buff functions
2967 * safely.
2968 *
2969 * A buffer cannot be placed on two lists at the same time.
2970 */
2971void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2972{
2973 unsigned long flags;
2974
2975 spin_lock_irqsave(&list->lock, flags);
2976 __skb_queue_tail(list, newsk);
2977 spin_unlock_irqrestore(&list->lock, flags);
2978}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002979EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981/**
2982 * skb_unlink - remove a buffer from a list
2983 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002984 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 *
David S. Miller8728b832005-08-09 19:25:21 -07002986 * Remove a packet from a list. The list locks are taken and this
2987 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 *
David S. Miller8728b832005-08-09 19:25:21 -07002989 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 */
David S. Miller8728b832005-08-09 19:25:21 -07002991void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
David S. Miller8728b832005-08-09 19:25:21 -07002993 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
David S. Miller8728b832005-08-09 19:25:21 -07002995 spin_lock_irqsave(&list->lock, flags);
2996 __skb_unlink(skb, list);
2997 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002999EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001/**
3002 * skb_append - append a buffer
3003 * @old: buffer to insert after
3004 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003005 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 *
3007 * Place a packet after a given packet in a list. The list locks are taken
3008 * and this function is atomic with respect to other list locked calls.
3009 * A buffer cannot be placed on two lists at the same time.
3010 */
David S. Miller8728b832005-08-09 19:25:21 -07003011void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 unsigned long flags;
3014
David S. Miller8728b832005-08-09 19:25:21 -07003015 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003016 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003017 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003019EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021static inline void skb_split_inside_header(struct sk_buff *skb,
3022 struct sk_buff* skb1,
3023 const u32 len, const int pos)
3024{
3025 int i;
3026
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003027 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3028 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* And move data appendix as is. */
3030 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3031 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3032
3033 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3034 skb_shinfo(skb)->nr_frags = 0;
3035 skb1->data_len = skb->data_len;
3036 skb1->len += skb1->data_len;
3037 skb->data_len = 0;
3038 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003039 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042static inline void skb_split_no_header(struct sk_buff *skb,
3043 struct sk_buff* skb1,
3044 const u32 len, int pos)
3045{
3046 int i, k = 0;
3047 const int nfrags = skb_shinfo(skb)->nr_frags;
3048
3049 skb_shinfo(skb)->nr_frags = 0;
3050 skb1->len = skb1->data_len = skb->len - len;
3051 skb->len = len;
3052 skb->data_len = len - pos;
3053
3054 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003055 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 if (pos + size > len) {
3058 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3059
3060 if (pos < len) {
3061 /* Split frag.
3062 * We have two variants in this case:
3063 * 1. Move all the frag to the second
3064 * part, if it is possible. F.e.
3065 * this approach is mandatory for TUX,
3066 * where splitting is expensive.
3067 * 2. Split is accurately. We make this.
3068 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003069 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003071 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3072 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 skb_shinfo(skb)->nr_frags++;
3074 }
3075 k++;
3076 } else
3077 skb_shinfo(skb)->nr_frags++;
3078 pos += size;
3079 }
3080 skb_shinfo(skb1)->nr_frags = k;
3081}
3082
3083/**
3084 * skb_split - Split fragmented skb to two parts at length len.
3085 * @skb: the buffer to split
3086 * @skb1: the buffer to receive the second part
3087 * @len: new length for skb
3088 */
3089void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3090{
3091 int pos = skb_headlen(skb);
3092
Willem de Bruijnfff88032017-06-08 11:35:03 -04003093 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3094 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003095 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (len < pos) /* Split line is inside header. */
3097 skb_split_inside_header(skb, skb1, len, pos);
3098 else /* Second chunk has no header, nothing to copy. */
3099 skb_split_no_header(skb, skb1, len, pos);
3100}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003101EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003103/* Shifting from/to a cloned skb is a no-go.
3104 *
3105 * Caller cannot keep skb_shinfo related pointers past calling here!
3106 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003107static int skb_prepare_for_shift(struct sk_buff *skb)
3108{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08003109 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003110}
3111
3112/**
3113 * skb_shift - Shifts paged data partially from skb to another
3114 * @tgt: buffer into which tail data gets added
3115 * @skb: buffer from which the paged data comes from
3116 * @shiftlen: shift up to this many bytes
3117 *
3118 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003119 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003120 * It's up to caller to free skb if everything was shifted.
3121 *
3122 * If @tgt runs out of frags, the whole operation is aborted.
3123 *
3124 * Skb cannot include anything else but paged data while tgt is allowed
3125 * to have non-paged data as well.
3126 *
3127 * TODO: full sized shift could be optimized but that would need
3128 * specialized skb free'er to handle frags without up-to-date nr_frags.
3129 */
3130int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3131{
3132 int from, to, merge, todo;
3133 struct skb_frag_struct *fragfrom, *fragto;
3134
3135 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003136
3137 if (skb_headlen(skb))
3138 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003139 if (skb_zcopy(tgt) || skb_zcopy(skb))
3140 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003141
3142 todo = shiftlen;
3143 from = 0;
3144 to = skb_shinfo(tgt)->nr_frags;
3145 fragfrom = &skb_shinfo(skb)->frags[from];
3146
3147 /* Actual merge is delayed until the point when we know we can
3148 * commit all, so that we don't have to undo partial changes
3149 */
3150 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003151 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3152 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003153 merge = -1;
3154 } else {
3155 merge = to - 1;
3156
Eric Dumazet9e903e02011-10-18 21:00:24 +00003157 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003158 if (todo < 0) {
3159 if (skb_prepare_for_shift(skb) ||
3160 skb_prepare_for_shift(tgt))
3161 return 0;
3162
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003163 /* All previous frag pointers might be stale! */
3164 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003165 fragto = &skb_shinfo(tgt)->frags[merge];
3166
Eric Dumazet9e903e02011-10-18 21:00:24 +00003167 skb_frag_size_add(fragto, shiftlen);
3168 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003169 fragfrom->page_offset += shiftlen;
3170
3171 goto onlymerged;
3172 }
3173
3174 from++;
3175 }
3176
3177 /* Skip full, not-fitting skb to avoid expensive operations */
3178 if ((shiftlen == skb->len) &&
3179 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3180 return 0;
3181
3182 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3183 return 0;
3184
3185 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3186 if (to == MAX_SKB_FRAGS)
3187 return 0;
3188
3189 fragfrom = &skb_shinfo(skb)->frags[from];
3190 fragto = &skb_shinfo(tgt)->frags[to];
3191
Eric Dumazet9e903e02011-10-18 21:00:24 +00003192 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003193 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003194 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003195 from++;
3196 to++;
3197
3198 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003199 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003200 fragto->page = fragfrom->page;
3201 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003202 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003203
3204 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003205 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003206 todo = 0;
3207
3208 to++;
3209 break;
3210 }
3211 }
3212
3213 /* Ready to "commit" this state change to tgt */
3214 skb_shinfo(tgt)->nr_frags = to;
3215
3216 if (merge >= 0) {
3217 fragfrom = &skb_shinfo(skb)->frags[0];
3218 fragto = &skb_shinfo(tgt)->frags[merge];
3219
Eric Dumazet9e903e02011-10-18 21:00:24 +00003220 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003221 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003222 }
3223
3224 /* Reposition in the original skb */
3225 to = 0;
3226 while (from < skb_shinfo(skb)->nr_frags)
3227 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3228 skb_shinfo(skb)->nr_frags = to;
3229
3230 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3231
3232onlymerged:
3233 /* Most likely the tgt won't ever need its checksum anymore, skb on
3234 * the other hand might need it if it needs to be resent
3235 */
3236 tgt->ip_summed = CHECKSUM_PARTIAL;
3237 skb->ip_summed = CHECKSUM_PARTIAL;
3238
3239 /* Yak, is it really working this way? Some helper please? */
3240 skb->len -= shiftlen;
3241 skb->data_len -= shiftlen;
3242 skb->truesize -= shiftlen;
3243 tgt->len += shiftlen;
3244 tgt->data_len += shiftlen;
3245 tgt->truesize += shiftlen;
3246
3247 return shiftlen;
3248}
3249
Thomas Graf677e90e2005-06-23 20:59:51 -07003250/**
3251 * skb_prepare_seq_read - Prepare a sequential read of skb data
3252 * @skb: the buffer to read
3253 * @from: lower offset of data to be read
3254 * @to: upper offset of data to be read
3255 * @st: state variable
3256 *
3257 * Initializes the specified state variable. Must be called before
3258 * invoking skb_seq_read() for the first time.
3259 */
3260void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3261 unsigned int to, struct skb_seq_state *st)
3262{
3263 st->lower_offset = from;
3264 st->upper_offset = to;
3265 st->root_skb = st->cur_skb = skb;
3266 st->frag_idx = st->stepped_offset = 0;
3267 st->frag_data = NULL;
3268}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003269EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003270
3271/**
3272 * skb_seq_read - Sequentially read skb data
3273 * @consumed: number of bytes consumed by the caller so far
3274 * @data: destination pointer for data to be returned
3275 * @st: state variable
3276 *
Mathias Krausebc323832013-11-07 14:18:26 +01003277 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003278 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003279 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003280 * of the block or 0 if the end of the skb data or the upper
3281 * offset has been reached.
3282 *
3283 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003284 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003285 * of bytes already consumed and the next call to
3286 * skb_seq_read() will return the remaining part of the block.
3287 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003288 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003289 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003290 * reads of potentially non linear data.
3291 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003292 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003293 * at the moment, state->root_skb could be replaced with
3294 * a stack for this purpose.
3295 */
3296unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3297 struct skb_seq_state *st)
3298{
3299 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3300 skb_frag_t *frag;
3301
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003302 if (unlikely(abs_offset >= st->upper_offset)) {
3303 if (st->frag_data) {
3304 kunmap_atomic(st->frag_data);
3305 st->frag_data = NULL;
3306 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003307 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003308 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003309
3310next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003311 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003312
Thomas Chenault995b3372009-05-18 21:43:27 -07003313 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003314 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003315 return block_limit - abs_offset;
3316 }
3317
3318 if (st->frag_idx == 0 && !st->frag_data)
3319 st->stepped_offset += skb_headlen(st->cur_skb);
3320
3321 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3322 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003323 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003324
3325 if (abs_offset < block_limit) {
3326 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003327 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003328
3329 *data = (u8 *) st->frag_data + frag->page_offset +
3330 (abs_offset - st->stepped_offset);
3331
3332 return block_limit - abs_offset;
3333 }
3334
3335 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003336 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003337 st->frag_data = NULL;
3338 }
3339
3340 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003341 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003342 }
3343
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003344 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003345 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003346 st->frag_data = NULL;
3347 }
3348
David S. Miller21dc3302010-08-23 00:13:46 -07003349 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003350 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003351 st->frag_idx = 0;
3352 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003353 } else if (st->cur_skb->next) {
3354 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003355 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003356 goto next_skb;
3357 }
3358
3359 return 0;
3360}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003361EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003362
3363/**
3364 * skb_abort_seq_read - Abort a sequential read of skb data
3365 * @st: state variable
3366 *
3367 * Must be called if skb_seq_read() was not called until it
3368 * returned 0.
3369 */
3370void skb_abort_seq_read(struct skb_seq_state *st)
3371{
3372 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003373 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003374}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003375EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003376
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003377#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3378
3379static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3380 struct ts_config *conf,
3381 struct ts_state *state)
3382{
3383 return skb_seq_read(offset, text, TS_SKB_CB(state));
3384}
3385
3386static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3387{
3388 skb_abort_seq_read(TS_SKB_CB(state));
3389}
3390
3391/**
3392 * skb_find_text - Find a text pattern in skb data
3393 * @skb: the buffer to look in
3394 * @from: search offset
3395 * @to: search limit
3396 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003397 *
3398 * Finds a pattern in the skb data according to the specified
3399 * textsearch configuration. Use textsearch_next() to retrieve
3400 * subsequent occurrences of the pattern. Returns the offset
3401 * to the first occurrence or UINT_MAX if no match was found.
3402 */
3403unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003404 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003405{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003406 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003407 unsigned int ret;
3408
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003409 config->get_next_block = skb_ts_get_next_block;
3410 config->finish = skb_ts_finish;
3411
Bojan Prtvar059a2442015-02-22 11:46:35 +01003412 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003413
Bojan Prtvar059a2442015-02-22 11:46:35 +01003414 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003415 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003416}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003417EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003418
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003419int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3420 int offset, size_t size)
3421{
3422 int i = skb_shinfo(skb)->nr_frags;
3423
3424 if (skb_can_coalesce(skb, i, page, offset)) {
3425 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3426 } else if (i < MAX_SKB_FRAGS) {
3427 get_page(page);
3428 skb_fill_page_desc(skb, i, page, offset, size);
3429 } else {
3430 return -EMSGSIZE;
3431 }
3432
3433 return 0;
3434}
3435EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3436
Herbert Xucbb042f2006-03-20 22:43:56 -08003437/**
3438 * skb_pull_rcsum - pull skb and update receive checksum
3439 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003440 * @len: length of data pulled
3441 *
3442 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003443 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003444 * receive path processing instead of skb_pull unless you know
3445 * that the checksum difference is zero (e.g., a valid IP header)
3446 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003447 */
Johannes Bergaf728682017-06-16 14:29:22 +02003448void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003449{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003450 unsigned char *data = skb->data;
3451
Herbert Xucbb042f2006-03-20 22:43:56 -08003452 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003453 __skb_pull(skb, len);
3454 skb_postpull_rcsum(skb, data, len);
3455 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003456}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003457EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3458
Yonghong Song13acc942018-03-21 16:31:03 -07003459static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3460{
3461 skb_frag_t head_frag;
3462 struct page *page;
3463
3464 page = virt_to_head_page(frag_skb->head);
3465 head_frag.page.p = page;
3466 head_frag.page_offset = frag_skb->data -
3467 (unsigned char *)page_address(page);
3468 head_frag.size = skb_headlen(frag_skb);
3469 return head_frag;
3470}
3471
Herbert Xuf4c50d92006-06-22 03:02:40 -07003472/**
3473 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003474 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003475 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003476 *
3477 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003478 * a pointer to the first in a list of new skbs for the segments.
3479 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003480 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003481struct sk_buff *skb_segment(struct sk_buff *head_skb,
3482 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003483{
3484 struct sk_buff *segs = NULL;
3485 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003486 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003487 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3488 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3489 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003490 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003491 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003492 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003493 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003494 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003495 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003496 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003497 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003498 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003499 int err = -ENOMEM;
3500 int i = 0;
3501 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003502 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003503
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003504 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003505 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003506 if (unlikely(!proto))
3507 return ERR_PTR(-EINVAL);
3508
Alexander Duyck36c98382016-05-02 09:38:18 -07003509 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003510 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003511
Steffen Klassert07b26c92016-09-19 12:58:47 +02003512 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3513 if (!(features & NETIF_F_GSO_PARTIAL)) {
3514 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003515 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003516
3517 if (!list_skb ||
3518 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3519 goto normal;
3520
Ilan Tayari43170c42017-04-19 21:26:07 +03003521 /* If we get here then all the required
3522 * GSO features except frag_list are supported.
3523 * Try to split the SKB to multiple GSO SKBs
3524 * with no frag_list.
3525 * Currently we can do that only when the buffers don't
3526 * have a linear part and all the buffers except
3527 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003528 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003529 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003530 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003531 if (frag_len != iter->len && iter->next)
3532 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003533 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003534 goto normal;
3535
3536 len -= iter->len;
3537 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003538
3539 if (len != frag_len)
3540 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003541 }
3542
3543 /* GSO partial only requires that we trim off any excess that
3544 * doesn't fit into an MSS sized block, so take care of that
3545 * now.
3546 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003547 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003548 if (partial_segs > 1)
3549 mss *= partial_segs;
3550 else
3551 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003552 }
3553
Steffen Klassert07b26c92016-09-19 12:58:47 +02003554normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003555 headroom = skb_headroom(head_skb);
3556 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003557
3558 do {
3559 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003560 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003561 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003562 int size;
3563
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003564 if (unlikely(mss == GSO_BY_FRAGS)) {
3565 len = list_skb->len;
3566 } else {
3567 len = head_skb->len - offset;
3568 if (len > mss)
3569 len = mss;
3570 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003571
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003572 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003573 if (hsize < 0)
3574 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003575 if (hsize > len || !sg)
3576 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003577
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003578 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3579 (skb_headlen(list_skb) == len || sg)) {
3580 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003581
Herbert Xu9d8506c2013-11-21 11:10:04 -08003582 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003583 nfrags = skb_shinfo(list_skb)->nr_frags;
3584 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003585 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003586 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003587
3588 while (pos < offset + len) {
3589 BUG_ON(i >= nfrags);
3590
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003591 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003592 if (pos + size > offset + len)
3593 break;
3594
3595 i++;
3596 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003597 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003598 }
3599
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003600 nskb = skb_clone(list_skb, GFP_ATOMIC);
3601 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003602
3603 if (unlikely(!nskb))
3604 goto err;
3605
Herbert Xu9d8506c2013-11-21 11:10:04 -08003606 if (unlikely(pskb_trim(nskb, len))) {
3607 kfree_skb(nskb);
3608 goto err;
3609 }
3610
Alexander Duyckec47ea82012-05-04 14:26:56 +00003611 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003612 if (skb_cow_head(nskb, doffset + headroom)) {
3613 kfree_skb(nskb);
3614 goto err;
3615 }
3616
Alexander Duyckec47ea82012-05-04 14:26:56 +00003617 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003618 skb_release_head_state(nskb);
3619 __skb_push(nskb, doffset);
3620 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003621 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003622 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003623 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003624
3625 if (unlikely(!nskb))
3626 goto err;
3627
3628 skb_reserve(nskb, headroom);
3629 __skb_put(nskb, doffset);
3630 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003631
3632 if (segs)
3633 tail->next = nskb;
3634 else
3635 segs = nskb;
3636 tail = nskb;
3637
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003638 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003639
Eric Dumazet030737b2013-10-19 11:42:54 -07003640 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003641 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003642
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003643 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003644 nskb->data - tnl_hlen,
3645 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003646
Herbert Xu9d8506c2013-11-21 11:10:04 -08003647 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003648 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003649
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003650 if (!sg) {
3651 if (!nskb->remcsum_offload)
3652 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003653 SKB_GSO_CB(nskb)->csum =
3654 skb_copy_and_csum_bits(head_skb, offset,
3655 skb_put(nskb, len),
3656 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003657 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003658 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003659 continue;
3660 }
3661
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003662 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003663
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003664 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003665 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003666
Willem de Bruijnfff88032017-06-08 11:35:03 -04003667 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3668 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003669
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003670 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3671 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3672 goto err;
3673
Herbert Xu9d8506c2013-11-21 11:10:04 -08003674 while (pos < offset + len) {
3675 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08003676 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003677 nfrags = skb_shinfo(list_skb)->nr_frags;
3678 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003679 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07003680 if (!skb_headlen(list_skb)) {
3681 BUG_ON(!nfrags);
3682 } else {
3683 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003684
Yonghong Song13acc942018-03-21 16:31:03 -07003685 /* to make room for head_frag. */
3686 i--;
3687 frag--;
3688 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003689 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3690 skb_zerocopy_clone(nskb, frag_skb,
3691 GFP_ATOMIC))
3692 goto err;
3693
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003694 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003695 }
3696
3697 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3698 MAX_SKB_FRAGS)) {
3699 net_warn_ratelimited(
3700 "skb_segment: too many frags: %u %u\n",
3701 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07003702 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003703 goto err;
3704 }
3705
Yonghong Song13acc942018-03-21 16:31:03 -07003706 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003707 __skb_frag_ref(nskb_frag);
3708 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003709
3710 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003711 nskb_frag->page_offset += offset - pos;
3712 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003713 }
3714
Herbert Xu89319d382008-12-15 23:26:06 -08003715 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003716
3717 if (pos + size <= offset + len) {
3718 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003719 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003720 pos += size;
3721 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003722 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003723 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003724 }
3725
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003726 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003727 }
3728
Herbert Xu89319d382008-12-15 23:26:06 -08003729skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003730 nskb->data_len = len - hsize;
3731 nskb->len += nskb->data_len;
3732 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003733
Simon Horman1cdbcb72013-05-19 15:46:49 +00003734perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003735 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07003736 if (skb_has_shared_frag(nskb) &&
3737 __skb_linearize(nskb))
3738 goto err;
3739
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003740 if (!nskb->remcsum_offload)
3741 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003742 SKB_GSO_CB(nskb)->csum =
3743 skb_checksum(nskb, doffset,
3744 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003745 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003746 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003747 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003748 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003749
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003750 /* Some callers want to get the end of the list.
3751 * Put it in segs->prev to avoid walking the list.
3752 * (see validate_xmit_skb_list() for example)
3753 */
3754 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003755
Alexander Duyck802ab552016-04-10 21:45:03 -04003756 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003757 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003758 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003759 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003760
3761 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003762 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003763 type &= ~SKB_GSO_DODGY;
3764
3765 /* Update GSO info and prepare to start updating headers on
3766 * our way back down the stack of protocols.
3767 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003768 for (iter = segs; iter; iter = iter->next) {
3769 skb_shinfo(iter)->gso_size = gso_size;
3770 skb_shinfo(iter)->gso_segs = partial_segs;
3771 skb_shinfo(iter)->gso_type = type;
3772 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3773 }
3774
3775 if (tail->len - doffset <= gso_size)
3776 skb_shinfo(tail)->gso_size = 0;
3777 else if (tail != segs)
3778 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003779 }
3780
Toshiaki Makita432c8562014-10-27 10:30:51 -07003781 /* Following permits correct backpressure, for protocols
3782 * using skb_set_owner_w().
3783 * Idea is to tranfert ownership from head_skb to last segment.
3784 */
3785 if (head_skb->destructor == sock_wfree) {
3786 swap(tail->truesize, head_skb->truesize);
3787 swap(tail->destructor, head_skb->destructor);
3788 swap(tail->sk, head_skb->sk);
3789 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003790 return segs;
3791
3792err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003793 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003794 return ERR_PTR(err);
3795}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003796EXPORT_SYMBOL_GPL(skb_segment);
3797
David Millerd4546c22018-06-24 14:13:49 +09003798int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08003799{
Eric Dumazet8a291112013-10-08 09:02:23 -07003800 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003801 unsigned int offset = skb_gro_offset(skb);
3802 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003803 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003804 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09003805 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08003806
Eric Dumazet8a291112013-10-08 09:02:23 -07003807 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003808 return -E2BIG;
3809
Eric Dumazet29e98242014-05-16 11:34:37 -07003810 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003811 pinfo = skb_shinfo(lp);
3812
3813 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003814 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003815 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003816 int i = skbinfo->nr_frags;
3817 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003818
Herbert Xu66e92fc2009-05-26 18:50:32 +00003819 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003820 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003821
Eric Dumazet8a291112013-10-08 09:02:23 -07003822 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003823 pinfo->nr_frags = nr_frags;
3824 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003825
Herbert Xu9aaa1562009-05-26 18:50:33 +00003826 frag = pinfo->frags + nr_frags;
3827 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003828 do {
3829 *--frag = *--frag2;
3830 } while (--i);
3831
3832 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003833 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003834
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003835 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003836 delta_truesize = skb->truesize -
3837 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003838
Herbert Xuf5572062009-01-14 20:40:03 -08003839 skb->truesize -= skb->data_len;
3840 skb->len -= skb->data_len;
3841 skb->data_len = 0;
3842
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003843 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003844 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003845 } else if (skb->head_frag) {
3846 int nr_frags = pinfo->nr_frags;
3847 skb_frag_t *frag = pinfo->frags + nr_frags;
3848 struct page *page = virt_to_head_page(skb->head);
3849 unsigned int first_size = headlen - offset;
3850 unsigned int first_offset;
3851
3852 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003853 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003854
3855 first_offset = skb->data -
3856 (unsigned char *)page_address(page) +
3857 offset;
3858
3859 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3860
3861 frag->page.p = page;
3862 frag->page_offset = first_offset;
3863 skb_frag_size_set(frag, first_size);
3864
3865 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3866 /* We dont need to clear skbinfo->nr_frags here */
3867
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003868 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003869 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3870 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003871 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003872
3873merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003874 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003875 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003876 unsigned int eat = offset - headlen;
3877
3878 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003879 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003880 skb->data_len -= eat;
3881 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003882 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003883 }
3884
Herbert Xu67147ba2009-05-26 18:50:22 +00003885 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003886
Eric Dumazet29e98242014-05-16 11:34:37 -07003887 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003888 skb_shinfo(p)->frag_list = skb;
3889 else
3890 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003891 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003892 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003893 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003894
Herbert Xu5d38a072009-01-04 16:13:40 -08003895done:
3896 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003897 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003898 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003899 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003900 if (lp != p) {
3901 lp->data_len += len;
3902 lp->truesize += delta_truesize;
3903 lp->len += len;
3904 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003905 NAPI_GRO_CB(skb)->same_flow = 1;
3906 return 0;
3907}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003908EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003909
Florian Westphaldf5042f2018-12-18 17:15:16 +01003910#ifdef CONFIG_SKB_EXTENSIONS
3911#define SKB_EXT_ALIGN_VALUE 8
3912#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3913
3914static const u8 skb_ext_type_len[] = {
3915#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3916 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3917#endif
3918};
3919
3920static __always_inline unsigned int skb_ext_total_length(void)
3921{
3922 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3923#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3924 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3925#endif
3926 0;
3927}
3928
3929static void skb_extensions_init(void)
3930{
3931 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3932 BUILD_BUG_ON(skb_ext_total_length() > 255);
3933
3934 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3935 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3936 0,
3937 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3938 NULL);
3939}
3940#else
3941static void skb_extensions_init(void) {}
3942#endif
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944void __init skb_init(void)
3945{
Kees Cook79a8a642018-02-07 17:44:38 -08003946 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 sizeof(struct sk_buff),
3948 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003949 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08003950 offsetof(struct sk_buff, cb),
3951 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09003952 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003953 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003954 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003955 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003956 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003957 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01003958 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
David S. Miller51c739d2007-10-30 21:29:29 -07003961static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003962__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3963 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003964{
David S. Miller1a028e52007-04-27 15:21:23 -07003965 int start = skb_headlen(skb);
3966 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003967 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003968 int elt = 0;
3969
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003970 if (unlikely(recursion_level >= 24))
3971 return -EMSGSIZE;
3972
David Howells716ea3a2007-04-02 20:19:53 -07003973 if (copy > 0) {
3974 if (copy > len)
3975 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003976 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003977 elt++;
3978 if ((len -= copy) == 0)
3979 return elt;
3980 offset += copy;
3981 }
3982
3983 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003984 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003985
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003986 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003987
Eric Dumazet9e903e02011-10-18 21:00:24 +00003988 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003989 if ((copy = end - offset) > 0) {
3990 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003991 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3992 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003993
3994 if (copy > len)
3995 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003996 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003997 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003998 elt++;
3999 if (!(len -= copy))
4000 return elt;
4001 offset += copy;
4002 }
David S. Miller1a028e52007-04-27 15:21:23 -07004003 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004004 }
4005
David S. Millerfbb398a2009-06-09 00:18:59 -07004006 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004007 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004008
David S. Millerfbb398a2009-06-09 00:18:59 -07004009 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004010
David S. Millerfbb398a2009-06-09 00:18:59 -07004011 end = start + frag_iter->len;
4012 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004013 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4014 return -EMSGSIZE;
4015
David S. Millerfbb398a2009-06-09 00:18:59 -07004016 if (copy > len)
4017 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004018 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4019 copy, recursion_level + 1);
4020 if (unlikely(ret < 0))
4021 return ret;
4022 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004023 if ((len -= copy) == 0)
4024 return elt;
4025 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004026 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004027 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004028 }
4029 BUG_ON(len);
4030 return elt;
4031}
4032
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004033/**
4034 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4035 * @skb: Socket buffer containing the buffers to be mapped
4036 * @sg: The scatter-gather list to map into
4037 * @offset: The offset into the buffer's contents to start mapping
4038 * @len: Length of buffer space to be mapped
4039 *
4040 * Fill the specified scatter-gather list with mappings/pointers into a
4041 * region of the buffer space attached to a socket buffer. Returns either
4042 * the number of scatterlist items used, or -EMSGSIZE if the contents
4043 * could not fit.
4044 */
4045int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4046{
4047 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4048
4049 if (nsg <= 0)
4050 return nsg;
4051
4052 sg_mark_end(&sg[nsg - 1]);
4053
4054 return nsg;
4055}
4056EXPORT_SYMBOL_GPL(skb_to_sgvec);
4057
Fan Du25a91d82014-01-18 09:54:23 +08004058/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4059 * sglist without mark the sg which contain last skb data as the end.
4060 * So the caller can mannipulate sg list as will when padding new data after
4061 * the first call without calling sg_unmark_end to expend sg list.
4062 *
4063 * Scenario to use skb_to_sgvec_nomark:
4064 * 1. sg_init_table
4065 * 2. skb_to_sgvec_nomark(payload1)
4066 * 3. skb_to_sgvec_nomark(payload2)
4067 *
4068 * This is equivalent to:
4069 * 1. sg_init_table
4070 * 2. skb_to_sgvec(payload1)
4071 * 3. sg_unmark_end
4072 * 4. skb_to_sgvec(payload2)
4073 *
4074 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4075 * is more preferable.
4076 */
4077int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4078 int offset, int len)
4079{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004080 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004081}
4082EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4083
David S. Miller51c739d2007-10-30 21:29:29 -07004084
David S. Miller51c739d2007-10-30 21:29:29 -07004085
David Howells716ea3a2007-04-02 20:19:53 -07004086/**
4087 * skb_cow_data - Check that a socket buffer's data buffers are writable
4088 * @skb: The socket buffer to check.
4089 * @tailbits: Amount of trailing space to be added
4090 * @trailer: Returned pointer to the skb where the @tailbits space begins
4091 *
4092 * Make sure that the data buffers attached to a socket buffer are
4093 * writable. If they are not, private copies are made of the data buffers
4094 * and the socket buffer is set to use these instead.
4095 *
4096 * If @tailbits is given, make sure that there is space to write @tailbits
4097 * bytes of data beyond current end of socket buffer. @trailer will be
4098 * set to point to the skb in which this space begins.
4099 *
4100 * The number of scatterlist elements required to completely map the
4101 * COW'd and extended socket buffer will be returned.
4102 */
4103int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4104{
4105 int copyflag;
4106 int elt;
4107 struct sk_buff *skb1, **skb_p;
4108
4109 /* If skb is cloned or its head is paged, reallocate
4110 * head pulling out all the pages (pages are considered not writable
4111 * at the moment even if they are anonymous).
4112 */
4113 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4114 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4115 return -ENOMEM;
4116
4117 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004118 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004119 /* A little of trouble, not enough of space for trailer.
4120 * This should not happen, when stack is tuned to generate
4121 * good frames. OK, on miss we reallocate and reserve even more
4122 * space, 128 bytes is fair. */
4123
4124 if (skb_tailroom(skb) < tailbits &&
4125 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4126 return -ENOMEM;
4127
4128 /* Voila! */
4129 *trailer = skb;
4130 return 1;
4131 }
4132
4133 /* Misery. We are in troubles, going to mincer fragments... */
4134
4135 elt = 1;
4136 skb_p = &skb_shinfo(skb)->frag_list;
4137 copyflag = 0;
4138
4139 while ((skb1 = *skb_p) != NULL) {
4140 int ntail = 0;
4141
4142 /* The fragment is partially pulled by someone,
4143 * this can happen on input. Copy it and everything
4144 * after it. */
4145
4146 if (skb_shared(skb1))
4147 copyflag = 1;
4148
4149 /* If the skb is the last, worry about trailer. */
4150
4151 if (skb1->next == NULL && tailbits) {
4152 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004153 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004154 skb_tailroom(skb1) < tailbits)
4155 ntail = tailbits + 128;
4156 }
4157
4158 if (copyflag ||
4159 skb_cloned(skb1) ||
4160 ntail ||
4161 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004162 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004163 struct sk_buff *skb2;
4164
4165 /* Fuck, we are miserable poor guys... */
4166 if (ntail == 0)
4167 skb2 = skb_copy(skb1, GFP_ATOMIC);
4168 else
4169 skb2 = skb_copy_expand(skb1,
4170 skb_headroom(skb1),
4171 ntail,
4172 GFP_ATOMIC);
4173 if (unlikely(skb2 == NULL))
4174 return -ENOMEM;
4175
4176 if (skb1->sk)
4177 skb_set_owner_w(skb2, skb1->sk);
4178
4179 /* Looking around. Are we still alive?
4180 * OK, link new skb, drop old one */
4181
4182 skb2->next = skb1->next;
4183 *skb_p = skb2;
4184 kfree_skb(skb1);
4185 skb1 = skb2;
4186 }
4187 elt++;
4188 *trailer = skb1;
4189 skb_p = &skb1->next;
4190 }
4191
4192 return elt;
4193}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004194EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004195
Eric Dumazetb1faf562010-05-31 23:44:05 -07004196static void sock_rmem_free(struct sk_buff *skb)
4197{
4198 struct sock *sk = skb->sk;
4199
4200 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4201}
4202
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004203static void skb_set_err_queue(struct sk_buff *skb)
4204{
4205 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4206 * So, it is safe to (mis)use it to mark skbs on the error queue.
4207 */
4208 skb->pkt_type = PACKET_OUTGOING;
4209 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4210}
4211
Eric Dumazetb1faf562010-05-31 23:44:05 -07004212/*
4213 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4214 */
4215int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4216{
4217 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004218 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004219 return -ENOMEM;
4220
4221 skb_orphan(skb);
4222 skb->sk = sk;
4223 skb->destructor = sock_rmem_free;
4224 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004225 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004226
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004227 /* before exiting rcu section, make sure dst is refcounted */
4228 skb_dst_force(skb);
4229
Eric Dumazetb1faf562010-05-31 23:44:05 -07004230 skb_queue_tail(&sk->sk_error_queue, skb);
4231 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004232 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004233 return 0;
4234}
4235EXPORT_SYMBOL(sock_queue_err_skb);
4236
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004237static bool is_icmp_err_skb(const struct sk_buff *skb)
4238{
4239 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4240 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4241}
4242
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004243struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4244{
4245 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004246 struct sk_buff *skb, *skb_next = NULL;
4247 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004248 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004249
Eric Dumazet997d5c32015-02-18 05:47:55 -08004250 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004251 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004252 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004253 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004254 if (icmp_next)
4255 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4256 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004257 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004258
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004259 if (is_icmp_err_skb(skb) && !icmp_next)
4260 sk->sk_err = 0;
4261
4262 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004263 sk->sk_error_report(sk);
4264
4265 return skb;
4266}
4267EXPORT_SYMBOL(sock_dequeue_err_skb);
4268
Alexander Duyckcab41c42014-09-10 18:05:26 -04004269/**
4270 * skb_clone_sk - create clone of skb, and take reference to socket
4271 * @skb: the skb to clone
4272 *
4273 * This function creates a clone of a buffer that holds a reference on
4274 * sk_refcnt. Buffers created via this function are meant to be
4275 * returned using sock_queue_err_skb, or free via kfree_skb.
4276 *
4277 * When passing buffers allocated with this function to sock_queue_err_skb
4278 * it is necessary to wrap the call with sock_hold/sock_put in order to
4279 * prevent the socket from being released prior to being enqueued on
4280 * the sk_error_queue.
4281 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004282struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4283{
4284 struct sock *sk = skb->sk;
4285 struct sk_buff *clone;
4286
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004287 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004288 return NULL;
4289
4290 clone = skb_clone(skb, GFP_ATOMIC);
4291 if (!clone) {
4292 sock_put(sk);
4293 return NULL;
4294 }
4295
4296 clone->sk = sk;
4297 clone->destructor = sock_efree;
4298
4299 return clone;
4300}
4301EXPORT_SYMBOL(skb_clone_sk);
4302
Alexander Duyck37846ef2014-09-04 13:31:10 -04004303static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4304 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004305 int tstype,
4306 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004307{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004308 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004309 int err;
4310
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004311 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4312
Patrick Ohlyac45f602009-02-12 05:03:37 +00004313 serr = SKB_EXT_ERR(skb);
4314 memset(serr, 0, sizeof(*serr));
4315 serr->ee.ee_errno = ENOMSG;
4316 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004317 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004318 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004319 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004320 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004321 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004322 if (sk->sk_protocol == IPPROTO_TCP &&
4323 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004324 serr->ee.ee_data -= sk->sk_tskey;
4325 }
Eric Dumazet29030372010-05-29 00:20:48 -07004326
Patrick Ohlyac45f602009-02-12 05:03:37 +00004327 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004328
Patrick Ohlyac45f602009-02-12 05:03:37 +00004329 if (err)
4330 kfree_skb(skb);
4331}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004332
Willem de Bruijnb245be12015-01-30 13:29:32 -05004333static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4334{
4335 bool ret;
4336
4337 if (likely(sysctl_tstamp_allow_data || tsonly))
4338 return true;
4339
4340 read_lock_bh(&sk->sk_callback_lock);
4341 ret = sk->sk_socket && sk->sk_socket->file &&
4342 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4343 read_unlock_bh(&sk->sk_callback_lock);
4344 return ret;
4345}
4346
Alexander Duyck37846ef2014-09-04 13:31:10 -04004347void skb_complete_tx_timestamp(struct sk_buff *skb,
4348 struct skb_shared_hwtstamps *hwtstamps)
4349{
4350 struct sock *sk = skb->sk;
4351
Willem de Bruijnb245be12015-01-30 13:29:32 -05004352 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004353 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004354
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004355 /* Take a reference to prevent skb_orphan() from freeing the socket,
4356 * but only if the socket refcount is not zero.
4357 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004358 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004359 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004360 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004361 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004362 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004363 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004364
4365err:
4366 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004367}
4368EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4369
4370void __skb_tstamp_tx(struct sk_buff *orig_skb,
4371 struct skb_shared_hwtstamps *hwtstamps,
4372 struct sock *sk, int tstype)
4373{
4374 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004375 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004376
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004377 if (!sk)
4378 return;
4379
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004380 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4381 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4382 return;
4383
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004384 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4385 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004386 return;
4387
Francis Yan1c885802016-11-27 23:07:18 -08004388 if (tsonly) {
4389#ifdef CONFIG_INET
4390 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4391 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004392 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004393 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004394 opt_stats = true;
4395 } else
Francis Yan1c885802016-11-27 23:07:18 -08004396#endif
4397 skb = alloc_skb(0, GFP_ATOMIC);
4398 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004399 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004400 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004401 if (!skb)
4402 return;
4403
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004404 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004405 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4406 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004407 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4408 }
4409
4410 if (hwtstamps)
4411 *skb_hwtstamps(skb) = *hwtstamps;
4412 else
4413 skb->tstamp = ktime_get_real();
4414
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004415 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004416}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004417EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4418
4419void skb_tstamp_tx(struct sk_buff *orig_skb,
4420 struct skb_shared_hwtstamps *hwtstamps)
4421{
4422 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4423 SCM_TSTAMP_SND);
4424}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004425EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4426
Johannes Berg6e3e9392011-11-09 10:15:42 +01004427void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4428{
4429 struct sock *sk = skb->sk;
4430 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004431 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004432
4433 skb->wifi_acked_valid = 1;
4434 skb->wifi_acked = acked;
4435
4436 serr = SKB_EXT_ERR(skb);
4437 memset(serr, 0, sizeof(*serr));
4438 serr->ee.ee_errno = ENOMSG;
4439 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4440
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004441 /* Take a reference to prevent skb_orphan() from freeing the socket,
4442 * but only if the socket refcount is not zero.
4443 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004444 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004445 err = sock_queue_err_skb(sk, skb);
4446 sock_put(sk);
4447 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004448 if (err)
4449 kfree_skb(skb);
4450}
4451EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4452
Rusty Russellf35d9d82008-02-04 23:49:54 -05004453/**
4454 * skb_partial_csum_set - set up and verify partial csum values for packet
4455 * @skb: the skb to set
4456 * @start: the number of bytes after skb->data to start checksumming.
4457 * @off: the offset from start to place the checksum.
4458 *
4459 * For untrusted partially-checksummed packets, we need to make sure the values
4460 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4461 *
4462 * This function checks and sets those values and skb->ip_summed: if this
4463 * returns false you should drop the packet.
4464 */
4465bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4466{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004467 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4468 u32 csum_start = skb_headroom(skb) + (u32)start;
4469
4470 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4471 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4472 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004473 return false;
4474 }
4475 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004476 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004477 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004478 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004479 return true;
4480}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004481EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004482
Paul Durranted1f50c2014-01-09 10:02:46 +00004483static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4484 unsigned int max)
4485{
4486 if (skb_headlen(skb) >= len)
4487 return 0;
4488
4489 /* If we need to pullup then pullup to the max, so we
4490 * won't need to do it again.
4491 */
4492 if (max > skb->len)
4493 max = skb->len;
4494
4495 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4496 return -ENOMEM;
4497
4498 if (skb_headlen(skb) < len)
4499 return -EPROTO;
4500
4501 return 0;
4502}
4503
Jan Beulichf9708b42014-03-11 13:56:05 +00004504#define MAX_TCP_HDR_LEN (15 * 4)
4505
4506static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4507 typeof(IPPROTO_IP) proto,
4508 unsigned int off)
4509{
4510 switch (proto) {
4511 int err;
4512
4513 case IPPROTO_TCP:
4514 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4515 off + MAX_TCP_HDR_LEN);
4516 if (!err && !skb_partial_csum_set(skb, off,
4517 offsetof(struct tcphdr,
4518 check)))
4519 err = -EPROTO;
4520 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4521
4522 case IPPROTO_UDP:
4523 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4524 off + sizeof(struct udphdr));
4525 if (!err && !skb_partial_csum_set(skb, off,
4526 offsetof(struct udphdr,
4527 check)))
4528 err = -EPROTO;
4529 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4530 }
4531
4532 return ERR_PTR(-EPROTO);
4533}
4534
Paul Durranted1f50c2014-01-09 10:02:46 +00004535/* This value should be large enough to cover a tagged ethernet header plus
4536 * maximally sized IP and TCP or UDP headers.
4537 */
4538#define MAX_IP_HDR_LEN 128
4539
Jan Beulichf9708b42014-03-11 13:56:05 +00004540static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004541{
4542 unsigned int off;
4543 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004544 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004545 int err;
4546
4547 fragment = false;
4548
4549 err = skb_maybe_pull_tail(skb,
4550 sizeof(struct iphdr),
4551 MAX_IP_HDR_LEN);
4552 if (err < 0)
4553 goto out;
4554
4555 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4556 fragment = true;
4557
4558 off = ip_hdrlen(skb);
4559
4560 err = -EPROTO;
4561
4562 if (fragment)
4563 goto out;
4564
Jan Beulichf9708b42014-03-11 13:56:05 +00004565 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4566 if (IS_ERR(csum))
4567 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004568
Jan Beulichf9708b42014-03-11 13:56:05 +00004569 if (recalculate)
4570 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4571 ip_hdr(skb)->daddr,
4572 skb->len - off,
4573 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004574 err = 0;
4575
4576out:
4577 return err;
4578}
4579
4580/* This value should be large enough to cover a tagged ethernet header plus
4581 * an IPv6 header, all options, and a maximal TCP or UDP header.
4582 */
4583#define MAX_IPV6_HDR_LEN 256
4584
4585#define OPT_HDR(type, skb, off) \
4586 (type *)(skb_network_header(skb) + (off))
4587
4588static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4589{
4590 int err;
4591 u8 nexthdr;
4592 unsigned int off;
4593 unsigned int len;
4594 bool fragment;
4595 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004596 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004597
4598 fragment = false;
4599 done = false;
4600
4601 off = sizeof(struct ipv6hdr);
4602
4603 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4604 if (err < 0)
4605 goto out;
4606
4607 nexthdr = ipv6_hdr(skb)->nexthdr;
4608
4609 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4610 while (off <= len && !done) {
4611 switch (nexthdr) {
4612 case IPPROTO_DSTOPTS:
4613 case IPPROTO_HOPOPTS:
4614 case IPPROTO_ROUTING: {
4615 struct ipv6_opt_hdr *hp;
4616
4617 err = skb_maybe_pull_tail(skb,
4618 off +
4619 sizeof(struct ipv6_opt_hdr),
4620 MAX_IPV6_HDR_LEN);
4621 if (err < 0)
4622 goto out;
4623
4624 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4625 nexthdr = hp->nexthdr;
4626 off += ipv6_optlen(hp);
4627 break;
4628 }
4629 case IPPROTO_AH: {
4630 struct ip_auth_hdr *hp;
4631
4632 err = skb_maybe_pull_tail(skb,
4633 off +
4634 sizeof(struct ip_auth_hdr),
4635 MAX_IPV6_HDR_LEN);
4636 if (err < 0)
4637 goto out;
4638
4639 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4640 nexthdr = hp->nexthdr;
4641 off += ipv6_authlen(hp);
4642 break;
4643 }
4644 case IPPROTO_FRAGMENT: {
4645 struct frag_hdr *hp;
4646
4647 err = skb_maybe_pull_tail(skb,
4648 off +
4649 sizeof(struct frag_hdr),
4650 MAX_IPV6_HDR_LEN);
4651 if (err < 0)
4652 goto out;
4653
4654 hp = OPT_HDR(struct frag_hdr, skb, off);
4655
4656 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4657 fragment = true;
4658
4659 nexthdr = hp->nexthdr;
4660 off += sizeof(struct frag_hdr);
4661 break;
4662 }
4663 default:
4664 done = true;
4665 break;
4666 }
4667 }
4668
4669 err = -EPROTO;
4670
4671 if (!done || fragment)
4672 goto out;
4673
Jan Beulichf9708b42014-03-11 13:56:05 +00004674 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4675 if (IS_ERR(csum))
4676 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004677
Jan Beulichf9708b42014-03-11 13:56:05 +00004678 if (recalculate)
4679 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4680 &ipv6_hdr(skb)->daddr,
4681 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004682 err = 0;
4683
4684out:
4685 return err;
4686}
4687
4688/**
4689 * skb_checksum_setup - set up partial checksum offset
4690 * @skb: the skb to set up
4691 * @recalculate: if true the pseudo-header checksum will be recalculated
4692 */
4693int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4694{
4695 int err;
4696
4697 switch (skb->protocol) {
4698 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004699 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004700 break;
4701
4702 case htons(ETH_P_IPV6):
4703 err = skb_checksum_setup_ipv6(skb, recalculate);
4704 break;
4705
4706 default:
4707 err = -EPROTO;
4708 break;
4709 }
4710
4711 return err;
4712}
4713EXPORT_SYMBOL(skb_checksum_setup);
4714
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004715/**
4716 * skb_checksum_maybe_trim - maybe trims the given skb
4717 * @skb: the skb to check
4718 * @transport_len: the data length beyond the network header
4719 *
4720 * Checks whether the given skb has data beyond the given transport length.
4721 * If so, returns a cloned skb trimmed to this transport length.
4722 * Otherwise returns the provided skb. Returns NULL in error cases
4723 * (e.g. transport_len exceeds skb length or out-of-memory).
4724 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004725 * Caller needs to set the skb transport header and free any returned skb if it
4726 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004727 */
4728static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4729 unsigned int transport_len)
4730{
4731 struct sk_buff *skb_chk;
4732 unsigned int len = skb_transport_offset(skb) + transport_len;
4733 int ret;
4734
Linus Lüssinga5169932015-08-13 05:54:07 +02004735 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004736 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004737 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004738 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004739
4740 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004741 if (!skb_chk)
4742 return NULL;
4743
4744 ret = pskb_trim_rcsum(skb_chk, len);
4745 if (ret) {
4746 kfree_skb(skb_chk);
4747 return NULL;
4748 }
4749
4750 return skb_chk;
4751}
4752
4753/**
4754 * skb_checksum_trimmed - validate checksum of an skb
4755 * @skb: the skb to check
4756 * @transport_len: the data length beyond the network header
4757 * @skb_chkf: checksum function to use
4758 *
4759 * Applies the given checksum function skb_chkf to the provided skb.
4760 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4761 *
4762 * If the skb has data beyond the given transport length, then a
4763 * trimmed & cloned skb is checked and returned.
4764 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004765 * Caller needs to set the skb transport header and free any returned skb if it
4766 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004767 */
4768struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4769 unsigned int transport_len,
4770 __sum16(*skb_chkf)(struct sk_buff *skb))
4771{
4772 struct sk_buff *skb_chk;
4773 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004774 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004775
4776 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4777 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004778 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004779
Linus Lüssinga5169932015-08-13 05:54:07 +02004780 if (!pskb_may_pull(skb_chk, offset))
4781 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004782
Linus Lüssing9b368812016-02-24 04:21:42 +01004783 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004784 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004785 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004786
Linus Lüssinga5169932015-08-13 05:54:07 +02004787 if (ret)
4788 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004789
4790 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004791
4792err:
4793 if (skb_chk && skb_chk != skb)
4794 kfree_skb(skb_chk);
4795
4796 return NULL;
4797
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004798}
4799EXPORT_SYMBOL(skb_checksum_trimmed);
4800
Ben Hutchings4497b072008-06-19 16:22:28 -07004801void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4802{
Joe Perchese87cc472012-05-13 21:56:26 +00004803 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4804 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004805}
Ben Hutchings4497b072008-06-19 16:22:28 -07004806EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004807
4808void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4809{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004810 if (head_stolen) {
4811 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004812 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004813 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004814 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004815 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004816}
4817EXPORT_SYMBOL(kfree_skb_partial);
4818
4819/**
4820 * skb_try_coalesce - try to merge skb to prior one
4821 * @to: prior buffer
4822 * @from: buffer to add
4823 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004824 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004825 */
4826bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4827 bool *fragstolen, int *delta_truesize)
4828{
Eric Dumazetc818fa92017-10-04 10:48:35 -07004829 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004830 int i, delta, len = from->len;
4831
4832 *fragstolen = false;
4833
4834 if (skb_cloned(to))
4835 return false;
4836
4837 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004838 if (len)
4839 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004840 *delta_truesize = 0;
4841 return true;
4842 }
4843
Eric Dumazetc818fa92017-10-04 10:48:35 -07004844 to_shinfo = skb_shinfo(to);
4845 from_shinfo = skb_shinfo(from);
4846 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004847 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004848 if (skb_zcopy(to) || skb_zcopy(from))
4849 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004850
4851 if (skb_headlen(from) != 0) {
4852 struct page *page;
4853 unsigned int offset;
4854
Eric Dumazetc818fa92017-10-04 10:48:35 -07004855 if (to_shinfo->nr_frags +
4856 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004857 return false;
4858
4859 if (skb_head_is_locked(from))
4860 return false;
4861
4862 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4863
4864 page = virt_to_head_page(from->head);
4865 offset = from->data - (unsigned char *)page_address(page);
4866
Eric Dumazetc818fa92017-10-04 10:48:35 -07004867 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004868 page, offset, skb_headlen(from));
4869 *fragstolen = true;
4870 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07004871 if (to_shinfo->nr_frags +
4872 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004873 return false;
4874
Weiping Panf4b549a2012-09-28 20:15:30 +00004875 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004876 }
4877
4878 WARN_ON_ONCE(delta < len);
4879
Eric Dumazetc818fa92017-10-04 10:48:35 -07004880 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4881 from_shinfo->frags,
4882 from_shinfo->nr_frags * sizeof(skb_frag_t));
4883 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004884
4885 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07004886 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004887
Li RongQing8ea853f2012-09-18 16:53:21 +00004888 /* if the skb is not cloned this does nothing
4889 * since we set nr_frags to 0.
4890 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07004891 for (i = 0; i < from_shinfo->nr_frags; i++)
4892 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004893
4894 to->truesize += delta;
4895 to->len += len;
4896 to->data_len += len;
4897
4898 *delta_truesize = delta;
4899 return true;
4900}
4901EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004902
4903/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004904 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004905 *
4906 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004907 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004908 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004909 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4910 * into/from a tunnel. Some information have to be cleared during these
4911 * operations.
4912 * skb_scrub_packet can also be used to clean a skb before injecting it in
4913 * another namespace (@xnet == true). We have to clear all information in the
4914 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004915 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004916void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004917{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004918 skb->pkt_type = PACKET_HOST;
4919 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004920 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004921 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004922 secpath_reset(skb);
4923 nf_reset(skb);
4924 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004925
Petr Machata6f9a5062018-11-19 16:11:07 +00004926#ifdef CONFIG_NET_SWITCHDEV
4927 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00004928 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00004929#endif
4930
Herbert Xu213dd742015-04-16 09:03:27 +08004931 if (!xnet)
4932 return;
4933
Ye Yin2b5ec1a2017-10-26 16:57:05 +08004934 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004935 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07004936 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004937}
4938EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004939
4940/**
4941 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4942 *
4943 * @skb: GSO skb
4944 *
4945 * skb_gso_transport_seglen is used to determine the real size of the
4946 * individual segments, including Layer4 headers (TCP/UDP).
4947 *
4948 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4949 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11004950static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01004951{
4952 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004953 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004954
Florian Westphalf993bc22014-10-20 13:49:18 +02004955 if (skb->encapsulation) {
4956 thlen = skb_inner_transport_header(skb) -
4957 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004958
Florian Westphalf993bc22014-10-20 13:49:18 +02004959 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4960 thlen += inner_tcp_hdrlen(skb);
4961 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4962 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11004963 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004964 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04004965 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
4966 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004967 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004968 /* UFO sets gso_size to the size of the fragmentation
4969 * payload, i.e. the size of the L4 (UDP) header is already
4970 * accounted for.
4971 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004972 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004973}
Daniel Axtensa4a77712018-03-01 17:13:40 +11004974
4975/**
4976 * skb_gso_network_seglen - Return length of individual segments of a gso packet
4977 *
4978 * @skb: GSO skb
4979 *
4980 * skb_gso_network_seglen is used to determine the real size of the
4981 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
4982 *
4983 * The MAC/L2 header is not accounted for.
4984 */
4985static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
4986{
4987 unsigned int hdr_len = skb_transport_header(skb) -
4988 skb_network_header(skb);
4989
4990 return hdr_len + skb_gso_transport_seglen(skb);
4991}
4992
4993/**
4994 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
4995 *
4996 * @skb: GSO skb
4997 *
4998 * skb_gso_mac_seglen is used to determine the real size of the
4999 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5000 * headers (TCP/UDP).
5001 */
5002static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5003{
5004 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5005
5006 return hdr_len + skb_gso_transport_seglen(skb);
5007}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005008
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005009/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005010 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5011 *
5012 * There are a couple of instances where we have a GSO skb, and we
5013 * want to determine what size it would be after it is segmented.
5014 *
5015 * We might want to check:
5016 * - L3+L4+payload size (e.g. IP forwarding)
5017 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5018 *
5019 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5020 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005021 * @skb: GSO skb
5022 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005023 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5024 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5025 *
5026 * @max_len: The maximum permissible length.
5027 *
5028 * Returns true if the segmented length <= max length.
5029 */
5030static inline bool skb_gso_size_check(const struct sk_buff *skb,
5031 unsigned int seg_len,
5032 unsigned int max_len) {
5033 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5034 const struct sk_buff *iter;
5035
5036 if (shinfo->gso_size != GSO_BY_FRAGS)
5037 return seg_len <= max_len;
5038
5039 /* Undo this so we can re-use header sizes */
5040 seg_len -= GSO_BY_FRAGS;
5041
5042 skb_walk_frags(skb, iter) {
5043 if (seg_len + skb_headlen(iter) > max_len)
5044 return false;
5045 }
5046
5047 return true;
5048}
5049
5050/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005051 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005052 *
5053 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005054 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005055 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005056 * skb_gso_validate_network_len validates if a given skb will fit a
5057 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5058 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005059 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005060bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005061{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005062 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005063}
Daniel Axtens779b7932018-03-01 17:13:37 +11005064EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005065
Daniel Axtens2b16f042018-01-31 14:15:33 +11005066/**
5067 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5068 *
5069 * @skb: GSO skb
5070 * @len: length to validate against
5071 *
5072 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5073 * length once split, including L2, L3 and L4 headers and the payload.
5074 */
5075bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5076{
5077 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5078}
5079EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5080
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005081static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5082{
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005083 int mac_len;
5084
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005085 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5086 kfree_skb(skb);
5087 return NULL;
5088 }
5089
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005090 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005091 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5092 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5093 mac_len - VLAN_HLEN - ETH_TLEN);
5094 }
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005095 skb->mac_header += VLAN_HLEN;
5096 return skb;
5097}
5098
5099struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5100{
5101 struct vlan_hdr *vhdr;
5102 u16 vlan_tci;
5103
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005104 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005105 /* vlan_tci is already set-up so leave this for another time */
5106 return skb;
5107 }
5108
5109 skb = skb_share_check(skb, GFP_ATOMIC);
5110 if (unlikely(!skb))
5111 goto err_free;
5112
5113 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5114 goto err_free;
5115
5116 vhdr = (struct vlan_hdr *)skb->data;
5117 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5118 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5119
5120 skb_pull_rcsum(skb, VLAN_HLEN);
5121 vlan_set_encap_proto(skb, vhdr);
5122
5123 skb = skb_reorder_vlan_header(skb);
5124 if (unlikely(!skb))
5125 goto err_free;
5126
5127 skb_reset_network_header(skb);
5128 skb_reset_transport_header(skb);
5129 skb_reset_mac_len(skb);
5130
5131 return skb;
5132
5133err_free:
5134 kfree_skb(skb);
5135 return NULL;
5136}
5137EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005138
Jiri Pirkoe2195122014-11-19 14:05:01 +01005139int skb_ensure_writable(struct sk_buff *skb, int write_len)
5140{
5141 if (!pskb_may_pull(skb, write_len))
5142 return -ENOMEM;
5143
5144 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5145 return 0;
5146
5147 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5148}
5149EXPORT_SYMBOL(skb_ensure_writable);
5150
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005151/* remove VLAN header from packet and update csum accordingly.
5152 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5153 */
5154int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005155{
5156 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005157 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005158 int err;
5159
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005160 if (WARN_ONCE(offset,
5161 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5162 offset)) {
5163 return -EINVAL;
5164 }
5165
Jiri Pirko93515d52014-11-19 14:05:02 +01005166 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5167 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005168 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005169
5170 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5171
5172 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5173 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5174
5175 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5176 __skb_pull(skb, VLAN_HLEN);
5177
5178 vlan_set_encap_proto(skb, vhdr);
5179 skb->mac_header += VLAN_HLEN;
5180
5181 if (skb_network_offset(skb) < ETH_HLEN)
5182 skb_set_network_header(skb, ETH_HLEN);
5183
5184 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005185
5186 return err;
5187}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005188EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005189
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005190/* Pop a vlan tag either from hwaccel or from payload.
5191 * Expects skb->data at mac header.
5192 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005193int skb_vlan_pop(struct sk_buff *skb)
5194{
5195 u16 vlan_tci;
5196 __be16 vlan_proto;
5197 int err;
5198
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005199 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005200 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005201 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005202 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005203 return 0;
5204
5205 err = __skb_vlan_pop(skb, &vlan_tci);
5206 if (err)
5207 return err;
5208 }
5209 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005210 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005211 return 0;
5212
5213 vlan_proto = skb->protocol;
5214 err = __skb_vlan_pop(skb, &vlan_tci);
5215 if (unlikely(err))
5216 return err;
5217
5218 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5219 return 0;
5220}
5221EXPORT_SYMBOL(skb_vlan_pop);
5222
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005223/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5224 * Expects skb->data at mac header.
5225 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005226int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5227{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005228 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005229 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005230 int err;
5231
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005232 if (WARN_ONCE(offset,
5233 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5234 offset)) {
5235 return -EINVAL;
5236 }
5237
Jiri Pirko93515d52014-11-19 14:05:02 +01005238 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005239 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005240 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005241 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005242
Jiri Pirko93515d52014-11-19 14:05:02 +01005243 skb->protocol = skb->vlan_proto;
5244 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005245
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005246 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005247 }
5248 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5249 return 0;
5250}
5251EXPORT_SYMBOL(skb_vlan_push);
5252
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005253/**
5254 * alloc_skb_with_frags - allocate skb with page frags
5255 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005256 * @header_len: size of linear part
5257 * @data_len: needed length in frags
5258 * @max_page_order: max page order desired.
5259 * @errcode: pointer to error code if any
5260 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005261 *
5262 * This can be used to allocate a paged skb, given a maximal order for frags.
5263 */
5264struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5265 unsigned long data_len,
5266 int max_page_order,
5267 int *errcode,
5268 gfp_t gfp_mask)
5269{
5270 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5271 unsigned long chunk;
5272 struct sk_buff *skb;
5273 struct page *page;
5274 gfp_t gfp_head;
5275 int i;
5276
5277 *errcode = -EMSGSIZE;
5278 /* Note this test could be relaxed, if we succeed to allocate
5279 * high order pages...
5280 */
5281 if (npages > MAX_SKB_FRAGS)
5282 return NULL;
5283
5284 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08005285 if (gfp_head & __GFP_DIRECT_RECLAIM)
Michal Hockodcda9b02017-07-12 14:36:45 -07005286 gfp_head |= __GFP_RETRY_MAYFAIL;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005287
5288 *errcode = -ENOBUFS;
5289 skb = alloc_skb(header_len, gfp_head);
5290 if (!skb)
5291 return NULL;
5292
5293 skb->truesize += npages << PAGE_SHIFT;
5294
5295 for (i = 0; npages > 0; i++) {
5296 int order = max_page_order;
5297
5298 while (order) {
5299 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005300 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005301 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005302 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005303 order);
5304 if (page)
5305 goto fill_page;
5306 /* Do not retry other high order allocations */
5307 order = 1;
5308 max_page_order = 0;
5309 }
5310 order--;
5311 }
5312 page = alloc_page(gfp_mask);
5313 if (!page)
5314 goto failure;
5315fill_page:
5316 chunk = min_t(unsigned long, data_len,
5317 PAGE_SIZE << order);
5318 skb_fill_page_desc(skb, i, page, 0, chunk);
5319 data_len -= chunk;
5320 npages -= 1 << order;
5321 }
5322 return skb;
5323
5324failure:
5325 kfree_skb(skb);
5326 return NULL;
5327}
5328EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005329
5330/* carve out the first off bytes from skb when off < headlen */
5331static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5332 const int headlen, gfp_t gfp_mask)
5333{
5334 int i;
5335 int size = skb_end_offset(skb);
5336 int new_hlen = headlen - off;
5337 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005338
5339 size = SKB_DATA_ALIGN(size);
5340
5341 if (skb_pfmemalloc(skb))
5342 gfp_mask |= __GFP_MEMALLOC;
5343 data = kmalloc_reserve(size +
5344 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5345 gfp_mask, NUMA_NO_NODE, NULL);
5346 if (!data)
5347 return -ENOMEM;
5348
5349 size = SKB_WITH_OVERHEAD(ksize(data));
5350
5351 /* Copy real data, and all frags */
5352 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5353 skb->len -= off;
5354
5355 memcpy((struct skb_shared_info *)(data + size),
5356 skb_shinfo(skb),
5357 offsetof(struct skb_shared_info,
5358 frags[skb_shinfo(skb)->nr_frags]));
5359 if (skb_cloned(skb)) {
5360 /* drop the old head gracefully */
5361 if (skb_orphan_frags(skb, gfp_mask)) {
5362 kfree(data);
5363 return -ENOMEM;
5364 }
5365 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5366 skb_frag_ref(skb, i);
5367 if (skb_has_frag_list(skb))
5368 skb_clone_fraglist(skb);
5369 skb_release_data(skb);
5370 } else {
5371 /* we can reuse existing recount- all we did was
5372 * relocate values
5373 */
5374 skb_free_head(skb);
5375 }
5376
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005377 skb->head = data;
5378 skb->data = data;
5379 skb->head_frag = 0;
5380#ifdef NET_SKBUFF_DATA_USES_OFFSET
5381 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005382#else
5383 skb->end = skb->head + size;
5384#endif
5385 skb_set_tail_pointer(skb, skb_headlen(skb));
5386 skb_headers_offset_update(skb, 0);
5387 skb->cloned = 0;
5388 skb->hdr_len = 0;
5389 skb->nohdr = 0;
5390 atomic_set(&skb_shinfo(skb)->dataref, 1);
5391
5392 return 0;
5393}
5394
5395static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5396
5397/* carve out the first eat bytes from skb's frag_list. May recurse into
5398 * pskb_carve()
5399 */
5400static int pskb_carve_frag_list(struct sk_buff *skb,
5401 struct skb_shared_info *shinfo, int eat,
5402 gfp_t gfp_mask)
5403{
5404 struct sk_buff *list = shinfo->frag_list;
5405 struct sk_buff *clone = NULL;
5406 struct sk_buff *insp = NULL;
5407
5408 do {
5409 if (!list) {
5410 pr_err("Not enough bytes to eat. Want %d\n", eat);
5411 return -EFAULT;
5412 }
5413 if (list->len <= eat) {
5414 /* Eaten as whole. */
5415 eat -= list->len;
5416 list = list->next;
5417 insp = list;
5418 } else {
5419 /* Eaten partially. */
5420 if (skb_shared(list)) {
5421 clone = skb_clone(list, gfp_mask);
5422 if (!clone)
5423 return -ENOMEM;
5424 insp = list->next;
5425 list = clone;
5426 } else {
5427 /* This may be pulled without problems. */
5428 insp = list;
5429 }
5430 if (pskb_carve(list, eat, gfp_mask) < 0) {
5431 kfree_skb(clone);
5432 return -ENOMEM;
5433 }
5434 break;
5435 }
5436 } while (eat);
5437
5438 /* Free pulled out fragments. */
5439 while ((list = shinfo->frag_list) != insp) {
5440 shinfo->frag_list = list->next;
5441 kfree_skb(list);
5442 }
5443 /* And insert new clone at head. */
5444 if (clone) {
5445 clone->next = list;
5446 shinfo->frag_list = clone;
5447 }
5448 return 0;
5449}
5450
5451/* carve off first len bytes from skb. Split line (off) is in the
5452 * non-linear part of skb
5453 */
5454static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5455 int pos, gfp_t gfp_mask)
5456{
5457 int i, k = 0;
5458 int size = skb_end_offset(skb);
5459 u8 *data;
5460 const int nfrags = skb_shinfo(skb)->nr_frags;
5461 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005462
5463 size = SKB_DATA_ALIGN(size);
5464
5465 if (skb_pfmemalloc(skb))
5466 gfp_mask |= __GFP_MEMALLOC;
5467 data = kmalloc_reserve(size +
5468 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5469 gfp_mask, NUMA_NO_NODE, NULL);
5470 if (!data)
5471 return -ENOMEM;
5472
5473 size = SKB_WITH_OVERHEAD(ksize(data));
5474
5475 memcpy((struct skb_shared_info *)(data + size),
5476 skb_shinfo(skb), offsetof(struct skb_shared_info,
5477 frags[skb_shinfo(skb)->nr_frags]));
5478 if (skb_orphan_frags(skb, gfp_mask)) {
5479 kfree(data);
5480 return -ENOMEM;
5481 }
5482 shinfo = (struct skb_shared_info *)(data + size);
5483 for (i = 0; i < nfrags; i++) {
5484 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5485
5486 if (pos + fsize > off) {
5487 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5488
5489 if (pos < off) {
5490 /* Split frag.
5491 * We have two variants in this case:
5492 * 1. Move all the frag to the second
5493 * part, if it is possible. F.e.
5494 * this approach is mandatory for TUX,
5495 * where splitting is expensive.
5496 * 2. Split is accurately. We make this.
5497 */
5498 shinfo->frags[0].page_offset += off - pos;
5499 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5500 }
5501 skb_frag_ref(skb, i);
5502 k++;
5503 }
5504 pos += fsize;
5505 }
5506 shinfo->nr_frags = k;
5507 if (skb_has_frag_list(skb))
5508 skb_clone_fraglist(skb);
5509
5510 if (k == 0) {
5511 /* split line is in frag list */
5512 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5513 }
5514 skb_release_data(skb);
5515
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005516 skb->head = data;
5517 skb->head_frag = 0;
5518 skb->data = data;
5519#ifdef NET_SKBUFF_DATA_USES_OFFSET
5520 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005521#else
5522 skb->end = skb->head + size;
5523#endif
5524 skb_reset_tail_pointer(skb);
5525 skb_headers_offset_update(skb, 0);
5526 skb->cloned = 0;
5527 skb->hdr_len = 0;
5528 skb->nohdr = 0;
5529 skb->len -= off;
5530 skb->data_len = skb->len;
5531 atomic_set(&skb_shinfo(skb)->dataref, 1);
5532 return 0;
5533}
5534
5535/* remove len bytes from the beginning of the skb */
5536static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5537{
5538 int headlen = skb_headlen(skb);
5539
5540 if (len < headlen)
5541 return pskb_carve_inside_header(skb, len, headlen, gfp);
5542 else
5543 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5544}
5545
5546/* Extract to_copy bytes starting at off from skb, and return this in
5547 * a new skb
5548 */
5549struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5550 int to_copy, gfp_t gfp)
5551{
5552 struct sk_buff *clone = skb_clone(skb, gfp);
5553
5554 if (!clone)
5555 return NULL;
5556
5557 if (pskb_carve(clone, off, gfp) < 0 ||
5558 pskb_trim(clone, to_copy)) {
5559 kfree_skb(clone);
5560 return NULL;
5561 }
5562 return clone;
5563}
5564EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005565
5566/**
5567 * skb_condense - try to get rid of fragments/frag_list if possible
5568 * @skb: buffer
5569 *
5570 * Can be used to save memory before skb is added to a busy queue.
5571 * If packet has bytes in frags and enough tail room in skb->head,
5572 * pull all of them, so that we can free the frags right now and adjust
5573 * truesize.
5574 * Notes:
5575 * We do not reallocate skb->head thus can not fail.
5576 * Caller must re-evaluate skb->truesize if needed.
5577 */
5578void skb_condense(struct sk_buff *skb)
5579{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005580 if (skb->data_len) {
5581 if (skb->data_len > skb->end - skb->tail ||
5582 skb_cloned(skb))
5583 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005584
Eric Dumazet3174fed2016-12-09 08:02:05 -08005585 /* Nice, we can free page frag(s) right now */
5586 __pskb_pull_tail(skb, skb->data_len);
5587 }
5588 /* At this point, skb->truesize might be over estimated,
5589 * because skb had a fragment, and fragments do not tell
5590 * their truesize.
5591 * When we pulled its content into skb->head, fragment
5592 * was freed, but __pskb_pull_tail() could not possibly
5593 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005594 */
5595 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5596}
Florian Westphaldf5042f2018-12-18 17:15:16 +01005597
5598#ifdef CONFIG_SKB_EXTENSIONS
5599static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5600{
5601 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5602}
5603
5604static struct skb_ext *skb_ext_alloc(void)
5605{
5606 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5607
5608 if (new) {
5609 memset(new->offset, 0, sizeof(new->offset));
5610 refcount_set(&new->refcnt, 1);
5611 }
5612
5613 return new;
5614}
5615
5616static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old)
5617{
5618 struct skb_ext *new;
5619
5620 if (refcount_read(&old->refcnt) == 1)
5621 return old;
5622
5623 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5624 if (!new)
5625 return NULL;
5626
5627 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5628 refcount_set(&new->refcnt, 1);
5629
5630 __skb_ext_put(old);
5631 return new;
5632}
5633
5634/**
5635 * skb_ext_add - allocate space for given extension, COW if needed
5636 * @skb: buffer
5637 * @id: extension to allocate space for
5638 *
5639 * Allocates enough space for the given extension.
5640 * If the extension is already present, a pointer to that extension
5641 * is returned.
5642 *
5643 * If the skb was cloned, COW applies and the returned memory can be
5644 * modified without changing the extension space of clones buffers.
5645 *
5646 * Returns pointer to the extension or NULL on allocation failure.
5647 */
5648void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5649{
5650 struct skb_ext *new, *old = NULL;
5651 unsigned int newlen, newoff;
5652
5653 if (skb->active_extensions) {
5654 old = skb->extensions;
5655
5656 new = skb_ext_maybe_cow(old);
5657 if (!new)
5658 return NULL;
5659
5660 if (__skb_ext_exist(old, id)) {
5661 if (old != new)
5662 skb->extensions = new;
5663 goto set_active;
5664 }
5665
5666 newoff = old->chunks;
5667 } else {
5668 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5669
5670 new = skb_ext_alloc();
5671 if (!new)
5672 return NULL;
5673 }
5674
5675 newlen = newoff + skb_ext_type_len[id];
5676 new->chunks = newlen;
5677 new->offset[id] = newoff;
5678 skb->extensions = new;
5679set_active:
5680 skb->active_extensions |= 1 << id;
5681 return skb_ext_get_ptr(new, id);
5682}
5683EXPORT_SYMBOL(skb_ext_add);
5684
5685void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5686{
5687 struct skb_ext *ext = skb->extensions;
5688
5689 skb->active_extensions &= ~(1 << id);
5690 if (skb->active_extensions == 0) {
5691 skb->extensions = NULL;
5692 __skb_ext_put(ext);
5693 }
5694}
5695EXPORT_SYMBOL(__skb_ext_del);
5696
5697void __skb_ext_put(struct skb_ext *ext)
5698{
5699 /* If this is last clone, nothing can increment
5700 * it after check passes. Avoids one atomic op.
5701 */
5702 if (refcount_read(&ext->refcnt) == 1)
5703 goto free_now;
5704
5705 if (!refcount_dec_and_test(&ext->refcnt))
5706 return;
5707free_now:
5708 kmem_cache_free(skbuff_ext_cache, ext);
5709}
5710EXPORT_SYMBOL(__skb_ext_put);
5711#endif /* CONFIG_SKB_EXTENSIONS */