blob: 2415d9cb9b89fefb30a7932a70c3497aeb67c80e [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{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800359 fragsz = SKB_DATA_ALIGN(fragsz);
360
Mel Gorman453f85d2017-11-15 17:38:03 -0800361 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700362}
Eric Dumazet6f532612012-05-18 05:12:12 +0000363EXPORT_SYMBOL(netdev_alloc_frag);
364
Alexander Duyckffde7322014-12-09 19:40:42 -0800365static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
366{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100367 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700368
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800369 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800370}
371
372void *napi_alloc_frag(unsigned int fragsz)
373{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800374 fragsz = SKB_DATA_ALIGN(fragsz);
375
Mel Gorman453f85d2017-11-15 17:38:03 -0800376 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
Alexander Duyckffde7322014-12-09 19:40:42 -0800377}
378EXPORT_SYMBOL(napi_alloc_frag);
379
Eric Dumazet6f532612012-05-18 05:12:12 +0000380/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800381 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
382 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900383 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800384 * @gfp_mask: get_free_pages mask, passed to alloc_skb
385 *
386 * Allocate a new &sk_buff and assign it a usage count of one. The
387 * buffer has NET_SKB_PAD headroom built in. Users should allocate
388 * the headroom they think they need without accounting for the
389 * built in space. The built in space is used for optimisations.
390 *
391 * %NULL is returned if there is no free memory.
392 */
Alexander Duyck94519802015-05-06 21:11:40 -0700393struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
394 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800395{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700396 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700397 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800398 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700399 bool pfmemalloc;
400 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800401
Alexander Duyck94519802015-05-06 21:11:40 -0700402 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800403
Alexander Duyck94519802015-05-06 21:11:40 -0700404 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800405 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700406 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
407 if (!skb)
408 goto skb_fail;
409 goto skb_success;
410 }
Alexander Duyck94519802015-05-06 21:11:40 -0700411
412 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
413 len = SKB_DATA_ALIGN(len);
414
415 if (sk_memalloc_socks())
416 gfp_mask |= __GFP_MEMALLOC;
417
418 local_irq_save(flags);
419
420 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800421 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700422 pfmemalloc = nc->pfmemalloc;
423
424 local_irq_restore(flags);
425
426 if (unlikely(!data))
427 return NULL;
428
429 skb = __build_skb(data, len);
430 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700431 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700432 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700433 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800434
Alexander Duyck94519802015-05-06 21:11:40 -0700435 /* use OR instead of assignment to avoid clearing of bits in mask */
436 if (pfmemalloc)
437 skb->pfmemalloc = 1;
438 skb->head_frag = 1;
439
Alexander Duycka080e7b2015-05-13 13:34:13 -0700440skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700441 skb_reserve(skb, NET_SKB_PAD);
442 skb->dev = dev;
443
Alexander Duycka080e7b2015-05-13 13:34:13 -0700444skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700445 return skb;
446}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800447EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alexander Duyckfd11a832014-12-09 19:40:49 -0800449/**
450 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
451 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900452 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800453 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
454 *
455 * Allocate a new sk_buff for use in NAPI receive. This buffer will
456 * attempt to allocate the head from a special reserved region used
457 * only for NAPI Rx allocation. By doing this we can save several
458 * CPU cycles by avoiding having to disable and re-enable IRQs.
459 *
460 * %NULL is returned if there is no free memory.
461 */
Alexander Duyck94519802015-05-06 21:11:40 -0700462struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
463 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800464{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100465 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800466 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700467 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800468
Alexander Duyck94519802015-05-06 21:11:40 -0700469 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800470
Alexander Duyck94519802015-05-06 21:11:40 -0700471 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800472 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700473 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
474 if (!skb)
475 goto skb_fail;
476 goto skb_success;
477 }
Alexander Duyck94519802015-05-06 21:11:40 -0700478
479 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
480 len = SKB_DATA_ALIGN(len);
481
482 if (sk_memalloc_socks())
483 gfp_mask |= __GFP_MEMALLOC;
484
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800485 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700486 if (unlikely(!data))
487 return NULL;
488
489 skb = __build_skb(data, len);
490 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700491 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700492 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800493 }
494
Alexander Duyck94519802015-05-06 21:11:40 -0700495 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100496 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700497 skb->pfmemalloc = 1;
498 skb->head_frag = 1;
499
Alexander Duycka080e7b2015-05-13 13:34:13 -0700500skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700501 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
502 skb->dev = napi->dev;
503
Alexander Duycka080e7b2015-05-13 13:34:13 -0700504skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800505 return skb;
506}
507EXPORT_SYMBOL(__napi_alloc_skb);
508
Peter Zijlstra654bed12008-10-07 14:22:33 -0700509void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000510 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700511{
512 skb_fill_page_desc(skb, i, page, off, size);
513 skb->len += size;
514 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000515 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700516}
517EXPORT_SYMBOL(skb_add_rx_frag);
518
Jason Wangf8e617e2013-11-01 14:07:47 +0800519void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
520 unsigned int truesize)
521{
522 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
523
524 skb_frag_size_add(frag, size);
525 skb->len += size;
526 skb->data_len += size;
527 skb->truesize += truesize;
528}
529EXPORT_SYMBOL(skb_coalesce_rx_frag);
530
Herbert Xu27b437c2006-07-13 19:26:39 -0700531static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700533 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700534 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Herbert Xu27b437c2006-07-13 19:26:39 -0700537static inline void skb_drop_fraglist(struct sk_buff *skb)
538{
539 skb_drop_list(&skb_shinfo(skb)->frag_list);
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void skb_clone_fraglist(struct sk_buff *skb)
543{
544 struct sk_buff *list;
545
David S. Millerfbb398a2009-06-09 00:18:59 -0700546 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 skb_get(list);
548}
549
Eric Dumazetd3836f22012-04-27 00:33:38 +0000550static void skb_free_head(struct sk_buff *skb)
551{
Alexander Duyck181edb22015-05-06 21:12:03 -0700552 unsigned char *head = skb->head;
553
Eric Dumazetd3836f22012-04-27 00:33:38 +0000554 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700555 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000556 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700557 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000558}
559
Adrian Bunk5bba1712006-06-29 13:02:35 -0700560static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Eric Dumazetff04a772014-09-23 18:39:30 -0700562 struct skb_shared_info *shinfo = skb_shinfo(skb);
563 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Eric Dumazetff04a772014-09-23 18:39:30 -0700565 if (skb->cloned &&
566 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
567 &shinfo->dataref))
568 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000569
Eric Dumazetff04a772014-09-23 18:39:30 -0700570 for (i = 0; i < shinfo->nr_frags; i++)
571 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000572
Eric Dumazetff04a772014-09-23 18:39:30 -0700573 if (shinfo->frag_list)
574 kfree_skb_list(shinfo->frag_list);
575
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400576 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700577 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
581 * Free an skbuff by memory without cleaning the state.
582 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800583static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700585 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700586
David S. Millerd179cd12005-08-17 14:57:30 -0700587 switch (skb->fclone) {
588 case SKB_FCLONE_UNAVAILABLE:
589 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800590 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700591
592 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700593 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800594
595 /* We usually free the clone (TX completion) before original skb
596 * This test would have no chance to be true for the clone,
597 * while here, branch prediction will be good.
598 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300599 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800600 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700601 break;
602
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800603 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700604 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700605 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700606 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300607 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800608 return;
609fastpath:
610 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Paolo Abeni0a463c72017-06-12 11:23:42 +0200613void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Eric Dumazetadf30902009-06-02 05:19:30 +0000615 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700616 if (skb->destructor) {
617 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 skb->destructor(skb);
619 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000620#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100621 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100622#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);
Eric Dumazetb1937222014-09-28 22:18:47 -0700804 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700805
Eric Dumazetb1937222014-09-28 22:18:47 -0700806 /* Note : this field could be in headers_start/headers_end section
807 * It is not yet because we do not want to have a 16 bit hole
808 */
809 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300810
Eric Dumazetb1937222014-09-28 22:18:47 -0700811 memcpy(&new->headers_start, &old->headers_start,
812 offsetof(struct sk_buff, headers_end) -
813 offsetof(struct sk_buff, headers_start));
814 CHECK_SKB_FIELD(protocol);
815 CHECK_SKB_FIELD(csum);
816 CHECK_SKB_FIELD(hash);
817 CHECK_SKB_FIELD(priority);
818 CHECK_SKB_FIELD(skb_iif);
819 CHECK_SKB_FIELD(vlan_proto);
820 CHECK_SKB_FIELD(vlan_tci);
821 CHECK_SKB_FIELD(transport_header);
822 CHECK_SKB_FIELD(network_header);
823 CHECK_SKB_FIELD(mac_header);
824 CHECK_SKB_FIELD(inner_protocol);
825 CHECK_SKB_FIELD(inner_transport_header);
826 CHECK_SKB_FIELD(inner_network_header);
827 CHECK_SKB_FIELD(inner_mac_header);
828 CHECK_SKB_FIELD(mark);
829#ifdef CONFIG_NETWORK_SECMARK
830 CHECK_SKB_FIELD(secmark);
831#endif
Cong Wange0d10952013-08-01 11:10:25 +0800832#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700833 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300834#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800835#ifdef CONFIG_XPS
836 CHECK_SKB_FIELD(sender_cpu);
837#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700838#ifdef CONFIG_NET_SCHED
839 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700840#endif
841
Herbert Xudec18812007-10-14 00:37:30 -0700842}
843
Herbert Xu82c49a32009-05-22 22:11:37 +0000844/*
845 * You should not add any new code to this function. Add it to
846 * __copy_skb_header above instead.
847 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700848static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#define C(x) n->x = skb->x
851
852 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700854 __copy_skb_header(n, skb);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 C(len);
857 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700858 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700859 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800860 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -0700862 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +0200863 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 C(tail);
866 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800867 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000868 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800869 C(data);
870 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300871 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 atomic_inc(&(skb_shinfo(skb)->dataref));
874 skb->cloned = 1;
875
876 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700877#undef C
878}
879
880/**
881 * skb_morph - morph one skb into another
882 * @dst: the skb to receive the contents
883 * @src: the skb to supply the contents
884 *
885 * This is identical to skb_clone except that the target skb is
886 * supplied by the user.
887 *
888 * The target skb is returned upon exit.
889 */
890struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
891{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800892 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700893 return __skb_clone(dst, src);
894}
895EXPORT_SYMBOL_GPL(skb_morph);
896
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800897int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400898{
899 unsigned long max_pg, num_pg, new_pg, old_pg;
900 struct user_struct *user;
901
902 if (capable(CAP_IPC_LOCK) || !size)
903 return 0;
904
905 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
906 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
907 user = mmp->user ? : current_user();
908
909 do {
910 old_pg = atomic_long_read(&user->locked_vm);
911 new_pg = old_pg + num_pg;
912 if (new_pg > max_pg)
913 return -ENOBUFS;
914 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
915 old_pg);
916
917 if (!mmp->user) {
918 mmp->user = get_uid(user);
919 mmp->num_pg = num_pg;
920 } else {
921 mmp->num_pg += num_pg;
922 }
923
924 return 0;
925}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800926EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400927
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800928void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400929{
930 if (mmp->user) {
931 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
932 free_uid(mmp->user);
933 }
934}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800935EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400936
Willem de Bruijn52267792017-08-03 16:29:39 -0400937struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
938{
939 struct ubuf_info *uarg;
940 struct sk_buff *skb;
941
942 WARN_ON_ONCE(!in_task());
943
944 skb = sock_omalloc(sk, 0, GFP_KERNEL);
945 if (!skb)
946 return NULL;
947
948 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
949 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400950 uarg->mmp.user = NULL;
951
952 if (mm_account_pinned_pages(&uarg->mmp, size)) {
953 kfree_skb(skb);
954 return NULL;
955 }
Willem de Bruijn52267792017-08-03 16:29:39 -0400956
957 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -0400958 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
959 uarg->len = 1;
960 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -0400961 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -0700962 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -0400963 sock_hold(sk);
964
965 return uarg;
966}
967EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
968
969static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
970{
971 return container_of((void *)uarg, struct sk_buff, cb);
972}
973
Willem de Bruijn4ab6c992017-08-03 16:29:42 -0400974struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
975 struct ubuf_info *uarg)
976{
977 if (uarg) {
978 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
979 u32 bytelen, next;
980
981 /* realloc only when socket is locked (TCP, UDP cork),
982 * so uarg->len and sk_zckey access is serialized
983 */
984 if (!sock_owned_by_user(sk)) {
985 WARN_ON_ONCE(1);
986 return NULL;
987 }
988
989 bytelen = uarg->bytelen + size;
990 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
991 /* TCP can create new skb to attach new uarg */
992 if (sk->sk_type == SOCK_STREAM)
993 goto new_alloc;
994 return NULL;
995 }
996
997 next = (u32)atomic_read(&sk->sk_zckey);
998 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400999 if (mm_account_pinned_pages(&uarg->mmp, size))
1000 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001001 uarg->len++;
1002 uarg->bytelen = bytelen;
1003 atomic_set(&sk->sk_zckey, ++next);
Eric Dumazetdb5bce32017-08-31 16:48:21 -07001004 sock_zerocopy_get(uarg);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001005 return uarg;
1006 }
1007 }
1008
1009new_alloc:
1010 return sock_zerocopy_alloc(sk, size);
1011}
1012EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1013
1014static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1015{
1016 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1017 u32 old_lo, old_hi;
1018 u64 sum_len;
1019
1020 old_lo = serr->ee.ee_info;
1021 old_hi = serr->ee.ee_data;
1022 sum_len = old_hi - old_lo + 1ULL + len;
1023
1024 if (sum_len >= (1ULL << 32))
1025 return false;
1026
1027 if (lo != old_hi + 1)
1028 return false;
1029
1030 serr->ee.ee_data += len;
1031 return true;
1032}
1033
Willem de Bruijn52267792017-08-03 16:29:39 -04001034void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1035{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001036 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001037 struct sock_exterr_skb *serr;
1038 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001039 struct sk_buff_head *q;
1040 unsigned long flags;
1041 u32 lo, hi;
1042 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001043
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001044 mm_unaccount_pinned_pages(&uarg->mmp);
1045
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001046 /* if !len, there was only 1 call, and it was aborted
1047 * so do not queue a completion notification
1048 */
1049 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001050 goto release;
1051
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001052 len = uarg->len;
1053 lo = uarg->id;
1054 hi = uarg->id + len - 1;
1055
Willem de Bruijn52267792017-08-03 16:29:39 -04001056 serr = SKB_EXT_ERR(skb);
1057 memset(serr, 0, sizeof(*serr));
1058 serr->ee.ee_errno = 0;
1059 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001060 serr->ee.ee_data = hi;
1061 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001062 if (!success)
1063 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1064
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001065 q = &sk->sk_error_queue;
1066 spin_lock_irqsave(&q->lock, flags);
1067 tail = skb_peek_tail(q);
1068 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1069 !skb_zerocopy_notify_extend(tail, lo, len)) {
1070 __skb_queue_tail(q, skb);
1071 skb = NULL;
1072 }
1073 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001074
1075 sk->sk_error_report(sk);
1076
1077release:
1078 consume_skb(skb);
1079 sock_put(sk);
1080}
1081EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1082
1083void sock_zerocopy_put(struct ubuf_info *uarg)
1084{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001085 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001086 if (uarg->callback)
1087 uarg->callback(uarg, uarg->zerocopy);
1088 else
1089 consume_skb(skb_from_uarg(uarg));
1090 }
1091}
1092EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1093
Willem de Bruijn52900d22018-11-30 15:32:40 -05001094void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001095{
1096 if (uarg) {
1097 struct sock *sk = skb_from_uarg(uarg)->sk;
1098
1099 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001100 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001101
Willem de Bruijn52900d22018-11-30 15:32:40 -05001102 if (have_uref)
1103 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001104 }
1105}
1106EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1107
1108extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1109 struct iov_iter *from, size_t length);
1110
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001111int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1112{
1113 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1114}
1115EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1116
Willem de Bruijn52267792017-08-03 16:29:39 -04001117int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1118 struct msghdr *msg, int len,
1119 struct ubuf_info *uarg)
1120{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001121 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001122 struct iov_iter orig_iter = msg->msg_iter;
1123 int err, orig_len = skb->len;
1124
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001125 /* An skb can only point to one uarg. This edge case happens when
1126 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1127 */
1128 if (orig_uarg && uarg != orig_uarg)
1129 return -EEXIST;
1130
Willem de Bruijn52267792017-08-03 16:29:39 -04001131 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1132 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001133 struct sock *save_sk = skb->sk;
1134
Willem de Bruijn52267792017-08-03 16:29:39 -04001135 /* Streams do not free skb on error. Reset to prev state. */
1136 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001137 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001138 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001139 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001140 return err;
1141 }
1142
Willem de Bruijn52900d22018-11-30 15:32:40 -05001143 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001144 return skb->len - orig_len;
1145}
1146EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1147
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001148static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001149 gfp_t gfp_mask)
1150{
1151 if (skb_zcopy(orig)) {
1152 if (skb_zcopy(nskb)) {
1153 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1154 if (!gfp_mask) {
1155 WARN_ON_ONCE(1);
1156 return -ENOMEM;
1157 }
1158 if (skb_uarg(nskb) == skb_uarg(orig))
1159 return 0;
1160 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1161 return -EIO;
1162 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001163 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001164 }
1165 return 0;
1166}
1167
Ben Hutchings2c530402012-07-10 10:55:09 +00001168/**
1169 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001170 * @skb: the skb to modify
1171 * @gfp_mask: allocation priority
1172 *
1173 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1174 * It will copy all frags into kernel and drop the reference
1175 * to userspace pages.
1176 *
1177 * If this function is called from an interrupt gfp_mask() must be
1178 * %GFP_ATOMIC.
1179 *
1180 * Returns 0 on success or a negative error code on failure
1181 * to allocate kernel memory to copy to.
1182 */
1183int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001184{
Shirley Maa6686f22011-07-06 12:22:12 +00001185 int num_frags = skb_shinfo(skb)->nr_frags;
1186 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001187 int i, new_frags;
1188 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001189
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001190 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1191 return -EINVAL;
1192
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001193 if (!num_frags)
1194 goto release;
1195
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001196 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1197 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001198 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001199 if (!page) {
1200 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001201 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001202 put_page(head);
1203 head = next;
1204 }
1205 return -ENOMEM;
1206 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001207 set_page_private(page, (unsigned long)head);
1208 head = page;
1209 }
1210
1211 page = head;
1212 d_off = 0;
1213 for (i = 0; i < num_frags; i++) {
1214 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1215 u32 p_off, p_len, copied;
1216 struct page *p;
1217 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001218
1219 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1220 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001221 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001222 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001223
1224 while (done < p_len) {
1225 if (d_off == PAGE_SIZE) {
1226 d_off = 0;
1227 page = (struct page *)page_private(page);
1228 }
1229 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1230 memcpy(page_address(page) + d_off,
1231 vaddr + p_off + done, copy);
1232 done += copy;
1233 d_off += copy;
1234 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001235 kunmap_atomic(vaddr);
1236 }
Shirley Maa6686f22011-07-06 12:22:12 +00001237 }
1238
1239 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001240 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001241 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001242
Shirley Maa6686f22011-07-06 12:22:12 +00001243 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001244 for (i = 0; i < new_frags - 1; i++) {
1245 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001246 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001247 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001248 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1249 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001250
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001251release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001252 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001253 return 0;
1254}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001255EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001256
Herbert Xue0053ec2007-10-14 00:37:52 -07001257/**
1258 * skb_clone - duplicate an sk_buff
1259 * @skb: buffer to clone
1260 * @gfp_mask: allocation priority
1261 *
1262 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1263 * copies share the same packet data but not structure. The new
1264 * buffer has a reference count of 1. If the allocation fails the
1265 * function returns %NULL otherwise the new buffer is returned.
1266 *
1267 * If this function is called from an interrupt gfp_mask() must be
1268 * %GFP_ATOMIC.
1269 */
1270
1271struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1272{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001273 struct sk_buff_fclones *fclones = container_of(skb,
1274 struct sk_buff_fclones,
1275 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001276 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001277
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001278 if (skb_orphan_frags(skb, gfp_mask))
1279 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001280
Herbert Xue0053ec2007-10-14 00:37:52 -07001281 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001282 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001283 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001284 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001285 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001286 if (skb_pfmemalloc(skb))
1287 gfp_mask |= __GFP_MEMALLOC;
1288
Herbert Xue0053ec2007-10-14 00:37:52 -07001289 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1290 if (!n)
1291 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001292
Herbert Xue0053ec2007-10-14 00:37:52 -07001293 n->fclone = SKB_FCLONE_UNAVAILABLE;
1294 }
1295
1296 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001298EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001300void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001301{
Eric Dumazet030737b2013-10-19 11:42:54 -07001302 /* Only adjust this if it actually is csum_start rather than csum */
1303 if (skb->ip_summed == CHECKSUM_PARTIAL)
1304 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001305 /* {transport,network,mac}_header and tail are relative to skb->head */
1306 skb->transport_header += off;
1307 skb->network_header += off;
1308 if (skb_mac_header_was_set(skb))
1309 skb->mac_header += off;
1310 skb->inner_transport_header += off;
1311 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001312 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001313}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001314EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001315
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001316void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Herbert Xudec18812007-10-14 00:37:30 -07001318 __copy_skb_header(new, old);
1319
Herbert Xu79671682006-06-22 02:40:14 -07001320 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1321 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1322 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001324EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Mel Gormanc93bdd02012-07-31 16:44:19 -07001326static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1327{
1328 if (skb_pfmemalloc(skb))
1329 return SKB_ALLOC_RX;
1330 return 0;
1331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/**
1334 * skb_copy - create private copy of an sk_buff
1335 * @skb: buffer to copy
1336 * @gfp_mask: allocation priority
1337 *
1338 * Make a copy of both an &sk_buff and its data. This is used when the
1339 * caller wishes to modify the data and needs a private copy of the
1340 * data to alter. Returns %NULL on failure or the pointer to the buffer
1341 * on success. The returned buffer has a reference count of 1.
1342 *
1343 * As by-product this function converts non-linear &sk_buff to linear
1344 * one, so that &sk_buff becomes completely private and caller is allowed
1345 * to modify all the data of returned buffer. This means that this
1346 * function is not recommended for use in circumstances when only
1347 * header is going to be modified. Use pskb_copy() instead.
1348 */
1349
Al Virodd0fc662005-10-07 07:46:04 +01001350struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001352 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001353 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001354 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1355 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!n)
1358 return NULL;
1359
1360 /* Set the data pointer */
1361 skb_reserve(n, headerlen);
1362 /* Set the tail pointer and length */
1363 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Tim Hansen9f77fad2017-10-09 11:37:59 -04001365 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001367 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return n;
1369}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001370EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001373 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001375 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001377 * @fclone: if true allocate the copy of the skb from the fclone
1378 * cache instead of the head cache; it is recommended to set this
1379 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 *
1381 * Make a copy of both an &sk_buff and part of its data, located
1382 * in header. Fragmented data remain shared. This is used when
1383 * the caller wishes to modify only header of &sk_buff and needs
1384 * private copy of the header to alter. Returns %NULL on failure
1385 * or the pointer to the buffer on success.
1386 * The returned buffer has a reference count of 1.
1387 */
1388
Octavian Purdilabad93e92014-06-12 01:36:26 +03001389struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1390 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Eric Dumazet117632e2011-12-03 21:39:53 +00001392 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001393 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1394 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (!n)
1397 goto out;
1398
1399 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001400 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Set the tail pointer and length */
1402 skb_put(n, skb_headlen(skb));
1403 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001404 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Herbert Xu25f484a2006-11-07 14:57:15 -08001406 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 n->data_len = skb->data_len;
1408 n->len = skb->len;
1409
1410 if (skb_shinfo(skb)->nr_frags) {
1411 int i;
1412
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001413 if (skb_orphan_frags(skb, gfp_mask) ||
1414 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001415 kfree_skb(n);
1416 n = NULL;
1417 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1420 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001421 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423 skb_shinfo(n)->nr_frags = i;
1424 }
1425
David S. Miller21dc3302010-08-23 00:13:46 -07001426 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1428 skb_clone_fraglist(n);
1429 }
1430
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001431 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432out:
1433 return n;
1434}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001435EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437/**
1438 * pskb_expand_head - reallocate header of &sk_buff
1439 * @skb: buffer to reallocate
1440 * @nhead: room to add at head
1441 * @ntail: room to add at tail
1442 * @gfp_mask: allocation priority
1443 *
Mathias Krausebc323832013-11-07 14:18:26 +01001444 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1445 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * reference count of 1. Returns zero in the case of success or error,
1447 * if expansion failed. In the last case, &sk_buff is not changed.
1448 *
1449 * All the pointers pointing into skb header may change and must be
1450 * reloaded after call to this function.
1451 */
1452
Victor Fusco86a76ca2005-07-08 14:57:47 -07001453int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001454 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Eric Dumazet158f3232017-01-27 07:11:27 -08001456 int i, osize = skb_end_offset(skb);
1457 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001459 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Herbert Xu4edd87a2008-10-01 07:09:38 -07001461 BUG_ON(nhead < 0);
1462
Tim Hansen9f77fad2017-10-09 11:37:59 -04001463 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 size = SKB_DATA_ALIGN(size);
1466
Mel Gormanc93bdd02012-07-31 16:44:19 -07001467 if (skb_pfmemalloc(skb))
1468 gfp_mask |= __GFP_MEMALLOC;
1469 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1470 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (!data)
1472 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001473 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001476 * optimized for the cases when header is void.
1477 */
1478 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1479
1480 memcpy((struct skb_shared_info *)(data + size),
1481 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001482 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Alexander Duyck3e245912012-05-04 14:26:51 +00001484 /*
1485 * if shinfo is shared we must drop the old head gracefully, but if it
1486 * is not we can just drop the old head and let the existing refcount
1487 * be since all we did is relocate the values
1488 */
1489 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001490 if (skb_orphan_frags(skb, gfp_mask))
1491 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001492 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001493 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001494 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001495 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Eric Dumazet1fd63042010-09-02 23:09:32 +00001497 if (skb_has_frag_list(skb))
1498 skb_clone_fraglist(skb);
1499
1500 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001501 } else {
1502 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 off = (data + nhead) - skb->head;
1505
1506 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001507 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001509#ifdef NET_SKBUFF_DATA_USES_OFFSET
1510 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001511 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001512#else
1513 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001514#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001515 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001516 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001518 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 skb->nohdr = 0;
1520 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001521
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001522 skb_metadata_clear(skb);
1523
Eric Dumazet158f3232017-01-27 07:11:27 -08001524 /* It is not generally safe to change skb->truesize.
1525 * For the moment, we really care of rx path, or
1526 * when skb is orphaned (not attached to a socket).
1527 */
1528 if (!skb->sk || skb->destructor == sock_edemux)
1529 skb->truesize += size - osize;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return 0;
1532
Shirley Maa6686f22011-07-06 12:22:12 +00001533nofrags:
1534 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535nodata:
1536 return -ENOMEM;
1537}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001538EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/* Make private copy of skb with writable head and some headroom */
1541
1542struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1543{
1544 struct sk_buff *skb2;
1545 int delta = headroom - skb_headroom(skb);
1546
1547 if (delta <= 0)
1548 skb2 = pskb_copy(skb, GFP_ATOMIC);
1549 else {
1550 skb2 = skb_clone(skb, GFP_ATOMIC);
1551 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1552 GFP_ATOMIC)) {
1553 kfree_skb(skb2);
1554 skb2 = NULL;
1555 }
1556 }
1557 return skb2;
1558}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001559EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561/**
1562 * skb_copy_expand - copy and expand sk_buff
1563 * @skb: buffer to copy
1564 * @newheadroom: new free bytes at head
1565 * @newtailroom: new free bytes at tail
1566 * @gfp_mask: allocation priority
1567 *
1568 * Make a copy of both an &sk_buff and its data and while doing so
1569 * allocate additional space.
1570 *
1571 * This is used when the caller wishes to modify the data and needs a
1572 * private copy of the data to alter as well as more space for new fields.
1573 * Returns %NULL on failure or the pointer to the buffer
1574 * on success. The returned buffer has a reference count of 1.
1575 *
1576 * You must pass %GFP_ATOMIC as the allocation priority if this function
1577 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
1579struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001580 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001581 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 /*
1584 * Allocate the copy buffer
1585 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001586 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1587 gfp_mask, skb_alloc_rx_flag(skb),
1588 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001589 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int head_copy_len, head_copy_off;
1591
1592 if (!n)
1593 return NULL;
1594
1595 skb_reserve(n, newheadroom);
1596
1597 /* Set the tail pointer and length */
1598 skb_put(n, skb->len);
1599
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001600 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 head_copy_off = 0;
1602 if (newheadroom <= head_copy_len)
1603 head_copy_len = newheadroom;
1604 else
1605 head_copy_off = newheadroom - head_copy_len;
1606
1607 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001608 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1609 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001611 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Eric Dumazet030737b2013-10-19 11:42:54 -07001613 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return n;
1616}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001617EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001620 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * @skb: buffer to pad
1622 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001623 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 *
1625 * Ensure that a buffer is followed by a padding area that is zero
1626 * filled. Used by network drivers which may DMA or transfer data
1627 * beyond the buffer end onto the wire.
1628 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001629 * May return error in out of memory cases. The skb is freed on error
1630 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001632
Florian Fainellicd0a1372017-08-22 15:12:14 -07001633int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Herbert Xu5b057c62006-06-23 02:06:41 -07001635 int err;
1636 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001639 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001643
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001644 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001645 if (likely(skb_cloned(skb) || ntail > 0)) {
1646 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1647 if (unlikely(err))
1648 goto free_skb;
1649 }
1650
1651 /* FIXME: The use of this function with non-linear skb's really needs
1652 * to be audited.
1653 */
1654 err = skb_linearize(skb);
1655 if (unlikely(err))
1656 goto free_skb;
1657
1658 memset(skb->data + skb->len, 0, pad);
1659 return 0;
1660
1661free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001662 if (free_on_error)
1663 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001664 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001665}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001666EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001667
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001668/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001669 * pskb_put - add data to the tail of a potentially fragmented buffer
1670 * @skb: start of the buffer to use
1671 * @tail: tail fragment of the buffer to use
1672 * @len: amount of data to add
1673 *
1674 * This function extends the used data area of the potentially
1675 * fragmented buffer. @tail must be the last fragment of @skb -- or
1676 * @skb itself. If this would exceed the total buffer size the kernel
1677 * will panic. A pointer to the first byte of the extra data is
1678 * returned.
1679 */
1680
Johannes Berg4df864c2017-06-16 14:29:21 +02001681void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001682{
1683 if (tail != skb) {
1684 skb->data_len += len;
1685 skb->len += len;
1686 }
1687 return skb_put(tail, len);
1688}
1689EXPORT_SYMBOL_GPL(pskb_put);
1690
1691/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001692 * skb_put - add data to a buffer
1693 * @skb: buffer to use
1694 * @len: amount of data to add
1695 *
1696 * This function extends the used data area of the buffer. If this would
1697 * exceed the total buffer size the kernel will panic. A pointer to the
1698 * first byte of the extra data is returned.
1699 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001700void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001701{
Johannes Berg4df864c2017-06-16 14:29:21 +02001702 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001703 SKB_LINEAR_ASSERT(skb);
1704 skb->tail += len;
1705 skb->len += len;
1706 if (unlikely(skb->tail > skb->end))
1707 skb_over_panic(skb, len, __builtin_return_address(0));
1708 return tmp;
1709}
1710EXPORT_SYMBOL(skb_put);
1711
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001712/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001713 * skb_push - add data to the start of a buffer
1714 * @skb: buffer to use
1715 * @len: amount of data to add
1716 *
1717 * This function extends the used data area of the buffer at the buffer
1718 * start. If this would exceed the total buffer headroom the kernel will
1719 * panic. A pointer to the first byte of the extra data is returned.
1720 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001721void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001722{
1723 skb->data -= len;
1724 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301725 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001726 skb_under_panic(skb, len, __builtin_return_address(0));
1727 return skb->data;
1728}
1729EXPORT_SYMBOL(skb_push);
1730
1731/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001732 * skb_pull - remove data from the start of a buffer
1733 * @skb: buffer to use
1734 * @len: amount of data to remove
1735 *
1736 * This function removes data from the start of a buffer, returning
1737 * the memory to the headroom. A pointer to the next data in the buffer
1738 * is returned. Once the data has been pulled future pushes will overwrite
1739 * the old data.
1740 */
Johannes Bergaf728682017-06-16 14:29:22 +02001741void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001742{
David S. Miller47d29642010-05-02 02:21:44 -07001743 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001744}
1745EXPORT_SYMBOL(skb_pull);
1746
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001747/**
1748 * skb_trim - remove end from a buffer
1749 * @skb: buffer to alter
1750 * @len: new length
1751 *
1752 * Cut the length of a buffer down by removing data from the tail. If
1753 * the buffer is already under the length specified it is not modified.
1754 * The skb must be linear.
1755 */
1756void skb_trim(struct sk_buff *skb, unsigned int len)
1757{
1758 if (skb->len > len)
1759 __skb_trim(skb, len);
1760}
1761EXPORT_SYMBOL(skb_trim);
1762
Herbert Xu3cc0e872006-06-09 16:13:38 -07001763/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
1765
Herbert Xu3cc0e872006-06-09 16:13:38 -07001766int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Herbert Xu27b437c2006-07-13 19:26:39 -07001768 struct sk_buff **fragp;
1769 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 int offset = skb_headlen(skb);
1771 int nfrags = skb_shinfo(skb)->nr_frags;
1772 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001773 int err;
1774
1775 if (skb_cloned(skb) &&
1776 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001779 i = 0;
1780 if (offset >= len)
1781 goto drop_pages;
1782
1783 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001784 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001785
1786 if (end < len) {
1787 offset = end;
1788 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001790
Eric Dumazet9e903e02011-10-18 21:00:24 +00001791 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001792
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001793drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001794 skb_shinfo(skb)->nr_frags = i;
1795
1796 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001797 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001798
David S. Miller21dc3302010-08-23 00:13:46 -07001799 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001800 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001801 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803
Herbert Xu27b437c2006-07-13 19:26:39 -07001804 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1805 fragp = &frag->next) {
1806 int end = offset + frag->len;
1807
1808 if (skb_shared(frag)) {
1809 struct sk_buff *nfrag;
1810
1811 nfrag = skb_clone(frag, GFP_ATOMIC);
1812 if (unlikely(!nfrag))
1813 return -ENOMEM;
1814
1815 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001816 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001817 frag = nfrag;
1818 *fragp = frag;
1819 }
1820
1821 if (end < len) {
1822 offset = end;
1823 continue;
1824 }
1825
1826 if (end > len &&
1827 unlikely((err = pskb_trim(frag, len - offset))))
1828 return err;
1829
1830 if (frag->next)
1831 skb_drop_list(&frag->next);
1832 break;
1833 }
1834
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001835done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001836 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 skb->data_len -= skb->len - len;
1838 skb->len = len;
1839 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001840 skb->len = len;
1841 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001842 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001845 if (!skb->sk || skb->destructor == sock_edemux)
1846 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return 0;
1848}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001849EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Eric Dumazet88078d92018-04-18 11:43:15 -07001851/* Note : use pskb_trim_rcsum() instead of calling this directly
1852 */
1853int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1854{
1855 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1856 int delta = skb->len - len;
1857
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07001858 skb->csum = csum_block_sub(skb->csum,
1859 skb_checksum(skb, len, delta, 0),
1860 len);
Eric Dumazet88078d92018-04-18 11:43:15 -07001861 }
1862 return __pskb_trim(skb, len);
1863}
1864EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866/**
1867 * __pskb_pull_tail - advance tail of skb header
1868 * @skb: buffer to reallocate
1869 * @delta: number of bytes to advance tail
1870 *
1871 * The function makes a sense only on a fragmented &sk_buff,
1872 * it expands header moving its tail forward and copying necessary
1873 * data from fragmented part.
1874 *
1875 * &sk_buff MUST have reference count of 1.
1876 *
1877 * Returns %NULL (and &sk_buff does not change) if pull failed
1878 * or value of new tail of skb in the case of success.
1879 *
1880 * All the pointers pointing into skb header may change and must be
1881 * reloaded after call to this function.
1882 */
1883
1884/* Moves tail of skb head forward, copying data from fragmented part,
1885 * when it is necessary.
1886 * 1. It may fail due to malloc failure.
1887 * 2. It may change skb pointers.
1888 *
1889 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1890 */
Johannes Bergaf728682017-06-16 14:29:22 +02001891void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 /* If skb has not enough free space at tail, get new one
1894 * plus 128 bytes for future expansions. If we have enough
1895 * room at tail, reallocate without expansion only if skb is cloned.
1896 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001897 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if (eat > 0 || skb_cloned(skb)) {
1900 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1901 GFP_ATOMIC))
1902 return NULL;
1903 }
1904
Tim Hansen9f77fad2017-10-09 11:37:59 -04001905 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1906 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* Optimization: no fragments, no reasons to preestimate
1909 * size of pulled pages. Superb.
1910 */
David S. Miller21dc3302010-08-23 00:13:46 -07001911 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto pull_pages;
1913
1914 /* Estimate size of pulled pages. */
1915 eat = delta;
1916 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001917 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1918
1919 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001921 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
1924 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02001925 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * but taking into account that pulling is expected to
1927 * be very rare operation, it is worth to fight against
1928 * further bloating skb head and crucify ourselves here instead.
1929 * Pure masohism, indeed. 8)8)
1930 */
1931 if (eat) {
1932 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1933 struct sk_buff *clone = NULL;
1934 struct sk_buff *insp = NULL;
1935
1936 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (list->len <= eat) {
1938 /* Eaten as whole. */
1939 eat -= list->len;
1940 list = list->next;
1941 insp = list;
1942 } else {
1943 /* Eaten partially. */
1944
1945 if (skb_shared(list)) {
1946 /* Sucks! We need to fork list. :-( */
1947 clone = skb_clone(list, GFP_ATOMIC);
1948 if (!clone)
1949 return NULL;
1950 insp = list->next;
1951 list = clone;
1952 } else {
1953 /* This may be pulled without
1954 * problems. */
1955 insp = list;
1956 }
1957 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001958 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return NULL;
1960 }
1961 break;
1962 }
1963 } while (eat);
1964
1965 /* Free pulled out fragments. */
1966 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1967 skb_shinfo(skb)->frag_list = list->next;
1968 kfree_skb(list);
1969 }
1970 /* And insert new clone at head. */
1971 if (clone) {
1972 clone->next = list;
1973 skb_shinfo(skb)->frag_list = clone;
1974 }
1975 }
1976 /* Success! Now we may commit changes to skb data. */
1977
1978pull_pages:
1979 eat = delta;
1980 k = 0;
1981 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001982 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1983
1984 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001985 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001986 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 } else {
1988 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1989 if (eat) {
1990 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001991 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08001992 if (!i)
1993 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 eat = 0;
1995 }
1996 k++;
1997 }
1998 }
1999 skb_shinfo(skb)->nr_frags = k;
2000
linzhang3ccc6c62017-07-17 17:25:02 +08002001end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 skb->tail += delta;
2003 skb->data_len -= delta;
2004
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002005 if (!skb->data_len)
2006 skb_zcopy_clear(skb, false);
2007
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002008 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002010EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Eric Dumazet22019b12011-07-29 18:37:31 +00002012/**
2013 * skb_copy_bits - copy bits from skb to kernel buffer
2014 * @skb: source skb
2015 * @offset: offset in source
2016 * @to: destination buffer
2017 * @len: number of bytes to copy
2018 *
2019 * Copy the specified number of bytes from the source skb to the
2020 * destination buffer.
2021 *
2022 * CAUTION ! :
2023 * If its prototype is ever changed,
2024 * check arch/{*}/net/{*}.S files,
2025 * since it is called from BPF assembly code.
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2028{
David S. Miller1a028e52007-04-27 15:21:23 -07002029 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002030 struct sk_buff *frag_iter;
2031 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 if (offset > (int)skb->len - len)
2034 goto fault;
2035
2036 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002037 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (copy > len)
2039 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002040 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if ((len -= copy) == 0)
2042 return 0;
2043 offset += copy;
2044 to += copy;
2045 }
2046
2047 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002048 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002049 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002051 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002052
Eric Dumazet51c56b02012-04-05 11:35:15 +02002053 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002055 u32 p_off, p_len, copied;
2056 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 u8 *vaddr;
2058
2059 if (copy > len)
2060 copy = len;
2061
Willem de Bruijnc613c202017-07-31 08:15:47 -04002062 skb_frag_foreach_page(f,
2063 f->page_offset + offset - start,
2064 copy, p, p_off, p_len, copied) {
2065 vaddr = kmap_atomic(p);
2066 memcpy(to + copied, vaddr + p_off, p_len);
2067 kunmap_atomic(vaddr);
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if ((len -= copy) == 0)
2071 return 0;
2072 offset += copy;
2073 to += copy;
2074 }
David S. Miller1a028e52007-04-27 15:21:23 -07002075 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077
David S. Millerfbb398a2009-06-09 00:18:59 -07002078 skb_walk_frags(skb, frag_iter) {
2079 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
David S. Millerfbb398a2009-06-09 00:18:59 -07002081 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
David S. Millerfbb398a2009-06-09 00:18:59 -07002083 end = start + frag_iter->len;
2084 if ((copy = end - offset) > 0) {
2085 if (copy > len)
2086 copy = len;
2087 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2088 goto fault;
2089 if ((len -= copy) == 0)
2090 return 0;
2091 offset += copy;
2092 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002094 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Shirley Maa6686f22011-07-06 12:22:12 +00002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (!len)
2098 return 0;
2099
2100fault:
2101 return -EFAULT;
2102}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002103EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Jens Axboe9c55e012007-11-06 23:30:13 -08002105/*
2106 * Callback from splice_to_pipe(), if we need to release some pages
2107 * at the end of the spd in case we error'ed out in filling the pipe.
2108 */
2109static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2110{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002111 put_page(spd->pages[i]);
2112}
Jens Axboe9c55e012007-11-06 23:30:13 -08002113
David S. Millera108d5f2012-04-23 23:06:11 -04002114static struct page *linear_to_page(struct page *page, unsigned int *len,
2115 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002116 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002117{
Eric Dumazet5640f762012-09-23 23:04:42 +00002118 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002119
Eric Dumazet5640f762012-09-23 23:04:42 +00002120 if (!sk_page_frag_refill(sk, pfrag))
2121 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002122
Eric Dumazet5640f762012-09-23 23:04:42 +00002123 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002124
Eric Dumazet5640f762012-09-23 23:04:42 +00002125 memcpy(page_address(pfrag->page) + pfrag->offset,
2126 page_address(page) + *offset, *len);
2127 *offset = pfrag->offset;
2128 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002129
Eric Dumazet5640f762012-09-23 23:04:42 +00002130 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002131}
2132
Eric Dumazet41c73a02012-04-22 12:26:16 +00002133static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2134 struct page *page,
2135 unsigned int offset)
2136{
2137 return spd->nr_pages &&
2138 spd->pages[spd->nr_pages - 1] == page &&
2139 (spd->partial[spd->nr_pages - 1].offset +
2140 spd->partial[spd->nr_pages - 1].len == offset);
2141}
2142
Jens Axboe9c55e012007-11-06 23:30:13 -08002143/*
2144 * Fill page/offset/length into spd, if it can hold more pages.
2145 */
David S. Millera108d5f2012-04-23 23:06:11 -04002146static bool spd_fill_page(struct splice_pipe_desc *spd,
2147 struct pipe_inode_info *pipe, struct page *page,
2148 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002149 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002150 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002151{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002152 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002153 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002154
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002155 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002156 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002157 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002158 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002159 }
2160 if (spd_can_coalesce(spd, page, offset)) {
2161 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002162 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002163 }
2164 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002165 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002166 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002167 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002168 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002169
David S. Millera108d5f2012-04-23 23:06:11 -04002170 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002171}
2172
David S. Millera108d5f2012-04-23 23:06:11 -04002173static bool __splice_segment(struct page *page, unsigned int poff,
2174 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002175 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002176 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002177 struct sock *sk,
2178 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002179{
2180 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002181 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002182
2183 /* skip this segment if already processed */
2184 if (*off >= plen) {
2185 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002186 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002187 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002188
Octavian Purdila2870c432008-07-15 00:49:11 -07002189 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002190 poff += *off;
2191 plen -= *off;
2192 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002193
Eric Dumazet18aafc62013-01-11 14:46:37 +00002194 do {
2195 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002196
Eric Dumazet18aafc62013-01-11 14:46:37 +00002197 if (spd_fill_page(spd, pipe, page, &flen, poff,
2198 linear, sk))
2199 return true;
2200 poff += flen;
2201 plen -= flen;
2202 *len -= flen;
2203 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002204
David S. Millera108d5f2012-04-23 23:06:11 -04002205 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002206}
2207
2208/*
David S. Millera108d5f2012-04-23 23:06:11 -04002209 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002210 * pipe is full or if we already spliced the requested length.
2211 */
David S. Millera108d5f2012-04-23 23:06:11 -04002212static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2213 unsigned int *offset, unsigned int *len,
2214 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002215{
2216 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002217 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002218
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002219 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002220 * If skb->head_frag is set, this 'linear' part is backed by a
2221 * fragment, and if the head is not shared with any clones then
2222 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002223 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002224 if (__splice_segment(virt_to_page(skb->data),
2225 (unsigned long) skb->data & (PAGE_SIZE - 1),
2226 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002227 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002228 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002229 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002230 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002231
2232 /*
2233 * then map the fragments
2234 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002235 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2236 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2237
Ian Campbellea2ab692011-08-22 23:44:58 +00002238 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002239 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002240 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002241 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002242 }
2243
Tom Herbertfa9835e2016-03-07 14:11:04 -08002244 skb_walk_frags(skb, iter) {
2245 if (*offset >= iter->len) {
2246 *offset -= iter->len;
2247 continue;
2248 }
2249 /* __skb_splice_bits() only fails if the output has no room
2250 * left, so no point in going over the frag_list for the error
2251 * case.
2252 */
2253 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2254 return true;
2255 }
2256
David S. Millera108d5f2012-04-23 23:06:11 -04002257 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002258}
2259
2260/*
2261 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002262 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002263 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002264int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002265 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002266 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002267{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002268 struct partial_page partial[MAX_SKB_FRAGS];
2269 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002270 struct splice_pipe_desc spd = {
2271 .pages = pages,
2272 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002273 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002274 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002275 .spd_release = sock_spd_release,
2276 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002277 int ret = 0;
2278
Tom Herbertfa9835e2016-03-07 14:11:04 -08002279 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002280
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002281 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002282 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002283
Jens Axboe35f3d142010-05-20 10:43:18 +02002284 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002285}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002286EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002287
Tom Herbert20bf50d2017-07-28 16:22:42 -07002288/* Send skb data on a socket. Socket must be locked. */
2289int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2290 int len)
2291{
2292 unsigned int orig_len = len;
2293 struct sk_buff *head = skb;
2294 unsigned short fragidx;
2295 int slen, ret;
2296
2297do_frag_list:
2298
2299 /* Deal with head data */
2300 while (offset < skb_headlen(skb) && len) {
2301 struct kvec kv;
2302 struct msghdr msg;
2303
2304 slen = min_t(int, len, skb_headlen(skb) - offset);
2305 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002306 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002307 memset(&msg, 0, sizeof(msg));
2308
2309 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2310 if (ret <= 0)
2311 goto error;
2312
2313 offset += ret;
2314 len -= ret;
2315 }
2316
2317 /* All the data was skb head? */
2318 if (!len)
2319 goto out;
2320
2321 /* Make offset relative to start of frags */
2322 offset -= skb_headlen(skb);
2323
2324 /* Find where we are in frag list */
2325 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2326 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2327
2328 if (offset < frag->size)
2329 break;
2330
2331 offset -= frag->size;
2332 }
2333
2334 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2335 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2336
2337 slen = min_t(size_t, len, frag->size - offset);
2338
2339 while (slen) {
2340 ret = kernel_sendpage_locked(sk, frag->page.p,
2341 frag->page_offset + offset,
2342 slen, MSG_DONTWAIT);
2343 if (ret <= 0)
2344 goto error;
2345
2346 len -= ret;
2347 offset += ret;
2348 slen -= ret;
2349 }
2350
2351 offset = 0;
2352 }
2353
2354 if (len) {
2355 /* Process any frag lists */
2356
2357 if (skb == head) {
2358 if (skb_has_frag_list(skb)) {
2359 skb = skb_shinfo(skb)->frag_list;
2360 goto do_frag_list;
2361 }
2362 } else if (skb->next) {
2363 skb = skb->next;
2364 goto do_frag_list;
2365 }
2366 }
2367
2368out:
2369 return orig_len - len;
2370
2371error:
2372 return orig_len == len ? ret : orig_len - len;
2373}
2374EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2375
Herbert Xu357b40a2005-04-19 22:30:14 -07002376/**
2377 * skb_store_bits - store bits from kernel buffer to skb
2378 * @skb: destination buffer
2379 * @offset: offset in destination
2380 * @from: source buffer
2381 * @len: number of bytes to copy
2382 *
2383 * Copy the specified number of bytes from the source buffer to the
2384 * destination skb. This function handles all the messy bits of
2385 * traversing fragment lists and such.
2386 */
2387
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002388int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002389{
David S. Miller1a028e52007-04-27 15:21:23 -07002390 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002391 struct sk_buff *frag_iter;
2392 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002393
2394 if (offset > (int)skb->len - len)
2395 goto fault;
2396
David S. Miller1a028e52007-04-27 15:21:23 -07002397 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002398 if (copy > len)
2399 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002400 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002401 if ((len -= copy) == 0)
2402 return 0;
2403 offset += copy;
2404 from += copy;
2405 }
2406
2407 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2408 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002409 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002410
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002411 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002412
Eric Dumazet9e903e02011-10-18 21:00:24 +00002413 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002414 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002415 u32 p_off, p_len, copied;
2416 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002417 u8 *vaddr;
2418
2419 if (copy > len)
2420 copy = len;
2421
Willem de Bruijnc613c202017-07-31 08:15:47 -04002422 skb_frag_foreach_page(frag,
2423 frag->page_offset + offset - start,
2424 copy, p, p_off, p_len, copied) {
2425 vaddr = kmap_atomic(p);
2426 memcpy(vaddr + p_off, from + copied, p_len);
2427 kunmap_atomic(vaddr);
2428 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002429
2430 if ((len -= copy) == 0)
2431 return 0;
2432 offset += copy;
2433 from += copy;
2434 }
David S. Miller1a028e52007-04-27 15:21:23 -07002435 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002436 }
2437
David S. Millerfbb398a2009-06-09 00:18:59 -07002438 skb_walk_frags(skb, frag_iter) {
2439 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002440
David S. Millerfbb398a2009-06-09 00:18:59 -07002441 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002442
David S. Millerfbb398a2009-06-09 00:18:59 -07002443 end = start + frag_iter->len;
2444 if ((copy = end - offset) > 0) {
2445 if (copy > len)
2446 copy = len;
2447 if (skb_store_bits(frag_iter, offset - start,
2448 from, copy))
2449 goto fault;
2450 if ((len -= copy) == 0)
2451 return 0;
2452 offset += copy;
2453 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002454 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002455 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002456 }
2457 if (!len)
2458 return 0;
2459
2460fault:
2461 return -EFAULT;
2462}
Herbert Xu357b40a2005-04-19 22:30:14 -07002463EXPORT_SYMBOL(skb_store_bits);
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002466__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2467 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
David S. Miller1a028e52007-04-27 15:21:23 -07002469 int start = skb_headlen(skb);
2470 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002471 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 int pos = 0;
2473
2474 /* Checksum header. */
2475 if (copy > 0) {
2476 if (copy > len)
2477 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002478 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if ((len -= copy) == 0)
2480 return csum;
2481 offset += copy;
2482 pos = copy;
2483 }
2484
2485 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002486 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002487 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002489 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002490
Eric Dumazet51c56b02012-04-05 11:35:15 +02002491 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002493 u32 p_off, p_len, copied;
2494 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002495 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 if (copy > len)
2499 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002500
2501 skb_frag_foreach_page(frag,
2502 frag->page_offset + offset - start,
2503 copy, p, p_off, p_len, copied) {
2504 vaddr = kmap_atomic(p);
2505 csum2 = ops->update(vaddr + p_off, p_len, 0);
2506 kunmap_atomic(vaddr);
2507 csum = ops->combine(csum, csum2, pos, p_len);
2508 pos += p_len;
2509 }
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (!(len -= copy))
2512 return csum;
2513 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
David S. Miller1a028e52007-04-27 15:21:23 -07002515 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
David S. Millerfbb398a2009-06-09 00:18:59 -07002518 skb_walk_frags(skb, frag_iter) {
2519 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
David S. Millerfbb398a2009-06-09 00:18:59 -07002521 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
David S. Millerfbb398a2009-06-09 00:18:59 -07002523 end = start + frag_iter->len;
2524 if ((copy = end - offset) > 0) {
2525 __wsum csum2;
2526 if (copy > len)
2527 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002528 csum2 = __skb_checksum(frag_iter, offset - start,
2529 copy, 0, ops);
2530 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002531 if ((len -= copy) == 0)
2532 return csum;
2533 offset += copy;
2534 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002536 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002538 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 return csum;
2541}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002542EXPORT_SYMBOL(__skb_checksum);
2543
2544__wsum skb_checksum(const struct sk_buff *skb, int offset,
2545 int len, __wsum csum)
2546{
2547 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002548 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002549 .combine = csum_block_add_ext,
2550 };
2551
2552 return __skb_checksum(skb, offset, len, csum, &ops);
2553}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002554EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556/* Both of above in one bottle. */
2557
Al Viro81d77662006-11-14 21:37:33 -08002558__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2559 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
David S. Miller1a028e52007-04-27 15:21:23 -07002561 int start = skb_headlen(skb);
2562 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002563 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 int pos = 0;
2565
2566 /* Copy header. */
2567 if (copy > 0) {
2568 if (copy > len)
2569 copy = len;
2570 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2571 copy, csum);
2572 if ((len -= copy) == 0)
2573 return csum;
2574 offset += copy;
2575 to += copy;
2576 pos = copy;
2577 }
2578
2579 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002580 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002582 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002583
Eric Dumazet9e903e02011-10-18 21:00:24 +00002584 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002586 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2587 u32 p_off, p_len, copied;
2588 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002589 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (copy > len)
2593 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002594
2595 skb_frag_foreach_page(frag,
2596 frag->page_offset + offset - start,
2597 copy, p, p_off, p_len, copied) {
2598 vaddr = kmap_atomic(p);
2599 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2600 to + copied,
2601 p_len, 0);
2602 kunmap_atomic(vaddr);
2603 csum = csum_block_add(csum, csum2, pos);
2604 pos += p_len;
2605 }
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!(len -= copy))
2608 return csum;
2609 offset += copy;
2610 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
David S. Miller1a028e52007-04-27 15:21:23 -07002612 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614
David S. Millerfbb398a2009-06-09 00:18:59 -07002615 skb_walk_frags(skb, frag_iter) {
2616 __wsum csum2;
2617 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
David S. Millerfbb398a2009-06-09 00:18:59 -07002619 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
David S. Millerfbb398a2009-06-09 00:18:59 -07002621 end = start + frag_iter->len;
2622 if ((copy = end - offset) > 0) {
2623 if (copy > len)
2624 copy = len;
2625 csum2 = skb_copy_and_csum_bits(frag_iter,
2626 offset - start,
2627 to, copy, 0);
2628 csum = csum_block_add(csum, csum2, pos);
2629 if ((len -= copy) == 0)
2630 return csum;
2631 offset += copy;
2632 to += copy;
2633 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002635 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002637 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return csum;
2639}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002640EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Cong Wang49f8e832018-11-08 14:05:42 -08002642__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2643{
2644 __sum16 sum;
2645
2646 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002647 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002648 if (likely(!sum)) {
2649 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2650 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002651 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002652 }
2653 if (!skb_shared(skb))
2654 skb->csum_valid = !sum;
2655 return sum;
2656}
2657EXPORT_SYMBOL(__skb_checksum_complete_head);
2658
Cong Wang14641932018-11-26 09:31:26 -08002659/* This function assumes skb->csum already holds pseudo header's checksum,
2660 * which has been changed from the hardware checksum, for example, by
2661 * __skb_checksum_validate_complete(). And, the original skb->csum must
2662 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2663 *
2664 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2665 * zero. The new checksum is stored back into skb->csum unless the skb is
2666 * shared.
2667 */
Cong Wang49f8e832018-11-08 14:05:42 -08002668__sum16 __skb_checksum_complete(struct sk_buff *skb)
2669{
2670 __wsum csum;
2671 __sum16 sum;
2672
2673 csum = skb_checksum(skb, 0, skb->len, 0);
2674
Cong Wang49f8e832018-11-08 14:05:42 -08002675 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002676 /* This check is inverted, because we already knew the hardware
2677 * checksum is invalid before calling this function. So, if the
2678 * re-computed checksum is valid instead, then we have a mismatch
2679 * between the original skb->csum and skb_checksum(). This means either
2680 * the original hardware checksum is incorrect or we screw up skb->csum
2681 * when moving skb->data around.
2682 */
Cong Wang49f8e832018-11-08 14:05:42 -08002683 if (likely(!sum)) {
2684 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2685 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002686 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002687 }
2688
2689 if (!skb_shared(skb)) {
2690 /* Save full packet checksum */
2691 skb->csum = csum;
2692 skb->ip_summed = CHECKSUM_COMPLETE;
2693 skb->csum_complete_sw = 1;
2694 skb->csum_valid = !sum;
2695 }
2696
2697 return sum;
2698}
2699EXPORT_SYMBOL(__skb_checksum_complete);
2700
Davide Caratti96178132017-05-18 15:44:37 +02002701static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2702{
2703 net_warn_ratelimited(
2704 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2705 __func__);
2706 return 0;
2707}
2708
2709static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2710 int offset, int len)
2711{
2712 net_warn_ratelimited(
2713 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2714 __func__);
2715 return 0;
2716}
2717
2718static const struct skb_checksum_ops default_crc32c_ops = {
2719 .update = warn_crc32c_csum_update,
2720 .combine = warn_crc32c_csum_combine,
2721};
2722
2723const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2724 &default_crc32c_ops;
2725EXPORT_SYMBOL(crc32c_csum_stub);
2726
Thomas Grafaf2806f2013-12-13 15:22:17 +01002727 /**
2728 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2729 * @from: source buffer
2730 *
2731 * Calculates the amount of linear headroom needed in the 'to' skb passed
2732 * into skb_zerocopy().
2733 */
2734unsigned int
2735skb_zerocopy_headlen(const struct sk_buff *from)
2736{
2737 unsigned int hlen = 0;
2738
2739 if (!from->head_frag ||
2740 skb_headlen(from) < L1_CACHE_BYTES ||
2741 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2742 hlen = skb_headlen(from);
2743
2744 if (skb_has_frag_list(from))
2745 hlen = from->len;
2746
2747 return hlen;
2748}
2749EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2750
2751/**
2752 * skb_zerocopy - Zero copy skb to skb
2753 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002754 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002755 * @len: number of bytes to copy from source buffer
2756 * @hlen: size of linear headroom in destination buffer
2757 *
2758 * Copies up to `len` bytes from `from` to `to` by creating references
2759 * to the frags in the source buffer.
2760 *
2761 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2762 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002763 *
2764 * Return value:
2765 * 0: everything is OK
2766 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2767 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002768 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002769int
2770skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002771{
2772 int i, j = 0;
2773 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002774 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002775 struct page *page;
2776 unsigned int offset;
2777
2778 BUG_ON(!from->head_frag && !hlen);
2779
2780 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002781 if (len <= skb_tailroom(to))
2782 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002783
2784 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002785 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2786 if (unlikely(ret))
2787 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002788 len -= hlen;
2789 } else {
2790 plen = min_t(int, skb_headlen(from), len);
2791 if (plen) {
2792 page = virt_to_head_page(from->head);
2793 offset = from->data - (unsigned char *)page_address(page);
2794 __skb_fill_page_desc(to, 0, page, offset, plen);
2795 get_page(page);
2796 j = 1;
2797 len -= plen;
2798 }
2799 }
2800
2801 to->truesize += len + plen;
2802 to->len += len + plen;
2803 to->data_len += len + plen;
2804
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002805 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2806 skb_tx_error(from);
2807 return -ENOMEM;
2808 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002809 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002810
Thomas Grafaf2806f2013-12-13 15:22:17 +01002811 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2812 if (!len)
2813 break;
2814 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2815 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2816 len -= skb_shinfo(to)->frags[j].size;
2817 skb_frag_ref(to, j);
2818 j++;
2819 }
2820 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002821
2822 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002823}
2824EXPORT_SYMBOL_GPL(skb_zerocopy);
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2827{
Al Virod3bc23e2006-11-14 21:24:49 -08002828 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 long csstart;
2830
Patrick McHardy84fa7932006-08-29 16:44:56 -07002831 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002832 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 else
2834 csstart = skb_headlen(skb);
2835
Kris Katterjohn09a62662006-01-08 22:24:28 -08002836 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002838 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 csum = 0;
2841 if (csstart != skb->len)
2842 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2843 skb->len - csstart, 0);
2844
Patrick McHardy84fa7932006-08-29 16:44:56 -07002845 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002846 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Al Virod3bc23e2006-11-14 21:24:49 -08002848 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002851EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853/**
2854 * skb_dequeue - remove from the head of the queue
2855 * @list: list to dequeue from
2856 *
2857 * Remove the head of the list. The list lock is taken so the function
2858 * may be used safely with other locking list functions. The head item is
2859 * returned or %NULL if the list is empty.
2860 */
2861
2862struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2863{
2864 unsigned long flags;
2865 struct sk_buff *result;
2866
2867 spin_lock_irqsave(&list->lock, flags);
2868 result = __skb_dequeue(list);
2869 spin_unlock_irqrestore(&list->lock, flags);
2870 return result;
2871}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002872EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874/**
2875 * skb_dequeue_tail - remove from the tail of the queue
2876 * @list: list to dequeue from
2877 *
2878 * Remove the tail of the list. The list lock is taken so the function
2879 * may be used safely with other locking list functions. The tail item is
2880 * returned or %NULL if the list is empty.
2881 */
2882struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2883{
2884 unsigned long flags;
2885 struct sk_buff *result;
2886
2887 spin_lock_irqsave(&list->lock, flags);
2888 result = __skb_dequeue_tail(list);
2889 spin_unlock_irqrestore(&list->lock, flags);
2890 return result;
2891}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002892EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894/**
2895 * skb_queue_purge - empty a list
2896 * @list: list to empty
2897 *
2898 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2899 * the list and one reference dropped. This function takes the list
2900 * lock and is atomic with respect to other list locking functions.
2901 */
2902void skb_queue_purge(struct sk_buff_head *list)
2903{
2904 struct sk_buff *skb;
2905 while ((skb = skb_dequeue(list)) != NULL)
2906 kfree_skb(skb);
2907}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002908EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002911 * skb_rbtree_purge - empty a skb rbtree
2912 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00002913 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002914 *
2915 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2916 * the list and one reference dropped. This function does not take
2917 * any lock. Synchronization should be handled by the caller (e.g., TCP
2918 * out-of-order queue is protected by the socket lock).
2919 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00002920unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002921{
Eric Dumazet7c905842017-09-23 12:39:12 -07002922 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002923 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002924
Eric Dumazet7c905842017-09-23 12:39:12 -07002925 while (p) {
2926 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2927
2928 p = rb_next(p);
2929 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002930 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002931 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07002932 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00002933 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002934}
2935
2936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 * skb_queue_head - queue a buffer at the list head
2938 * @list: list to use
2939 * @newsk: buffer to queue
2940 *
2941 * Queue a buffer at the start of the list. This function takes the
2942 * list lock and can be used safely with other locking &sk_buff functions
2943 * safely.
2944 *
2945 * A buffer cannot be placed on two lists at the same time.
2946 */
2947void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2948{
2949 unsigned long flags;
2950
2951 spin_lock_irqsave(&list->lock, flags);
2952 __skb_queue_head(list, newsk);
2953 spin_unlock_irqrestore(&list->lock, flags);
2954}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002955EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957/**
2958 * skb_queue_tail - queue a buffer at the list tail
2959 * @list: list to use
2960 * @newsk: buffer to queue
2961 *
2962 * Queue a buffer at the tail of the list. This function takes the
2963 * list lock and can be used safely with other locking &sk_buff functions
2964 * safely.
2965 *
2966 * A buffer cannot be placed on two lists at the same time.
2967 */
2968void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2969{
2970 unsigned long flags;
2971
2972 spin_lock_irqsave(&list->lock, flags);
2973 __skb_queue_tail(list, newsk);
2974 spin_unlock_irqrestore(&list->lock, flags);
2975}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002976EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978/**
2979 * skb_unlink - remove a buffer from a list
2980 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002981 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 *
David S. Miller8728b832005-08-09 19:25:21 -07002983 * Remove a packet from a list. The list locks are taken and this
2984 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 *
David S. Miller8728b832005-08-09 19:25:21 -07002986 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 */
David S. Miller8728b832005-08-09 19:25:21 -07002988void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
David S. Miller8728b832005-08-09 19:25:21 -07002990 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
David S. Miller8728b832005-08-09 19:25:21 -07002992 spin_lock_irqsave(&list->lock, flags);
2993 __skb_unlink(skb, list);
2994 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002996EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998/**
2999 * skb_append - append a buffer
3000 * @old: buffer to insert after
3001 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003002 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 *
3004 * Place a packet after a given packet in a list. The list locks are taken
3005 * and this function is atomic with respect to other list locked calls.
3006 * A buffer cannot be placed on two lists at the same time.
3007 */
David S. Miller8728b832005-08-09 19:25:21 -07003008void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 unsigned long flags;
3011
David S. Miller8728b832005-08-09 19:25:21 -07003012 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003013 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003014 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003016EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018static inline void skb_split_inside_header(struct sk_buff *skb,
3019 struct sk_buff* skb1,
3020 const u32 len, const int pos)
3021{
3022 int i;
3023
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003024 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3025 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 /* And move data appendix as is. */
3027 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3028 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3029
3030 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3031 skb_shinfo(skb)->nr_frags = 0;
3032 skb1->data_len = skb->data_len;
3033 skb1->len += skb1->data_len;
3034 skb->data_len = 0;
3035 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003036 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039static inline void skb_split_no_header(struct sk_buff *skb,
3040 struct sk_buff* skb1,
3041 const u32 len, int pos)
3042{
3043 int i, k = 0;
3044 const int nfrags = skb_shinfo(skb)->nr_frags;
3045
3046 skb_shinfo(skb)->nr_frags = 0;
3047 skb1->len = skb1->data_len = skb->len - len;
3048 skb->len = len;
3049 skb->data_len = len - pos;
3050
3051 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003052 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054 if (pos + size > len) {
3055 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3056
3057 if (pos < len) {
3058 /* Split frag.
3059 * We have two variants in this case:
3060 * 1. Move all the frag to the second
3061 * part, if it is possible. F.e.
3062 * this approach is mandatory for TUX,
3063 * where splitting is expensive.
3064 * 2. Split is accurately. We make this.
3065 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003066 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003068 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3069 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 skb_shinfo(skb)->nr_frags++;
3071 }
3072 k++;
3073 } else
3074 skb_shinfo(skb)->nr_frags++;
3075 pos += size;
3076 }
3077 skb_shinfo(skb1)->nr_frags = k;
3078}
3079
3080/**
3081 * skb_split - Split fragmented skb to two parts at length len.
3082 * @skb: the buffer to split
3083 * @skb1: the buffer to receive the second part
3084 * @len: new length for skb
3085 */
3086void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3087{
3088 int pos = skb_headlen(skb);
3089
Willem de Bruijnfff88032017-06-08 11:35:03 -04003090 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3091 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003092 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (len < pos) /* Split line is inside header. */
3094 skb_split_inside_header(skb, skb1, len, pos);
3095 else /* Second chunk has no header, nothing to copy. */
3096 skb_split_no_header(skb, skb1, len, pos);
3097}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003098EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003100/* Shifting from/to a cloned skb is a no-go.
3101 *
3102 * Caller cannot keep skb_shinfo related pointers past calling here!
3103 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003104static int skb_prepare_for_shift(struct sk_buff *skb)
3105{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08003106 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003107}
3108
3109/**
3110 * skb_shift - Shifts paged data partially from skb to another
3111 * @tgt: buffer into which tail data gets added
3112 * @skb: buffer from which the paged data comes from
3113 * @shiftlen: shift up to this many bytes
3114 *
3115 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003116 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003117 * It's up to caller to free skb if everything was shifted.
3118 *
3119 * If @tgt runs out of frags, the whole operation is aborted.
3120 *
3121 * Skb cannot include anything else but paged data while tgt is allowed
3122 * to have non-paged data as well.
3123 *
3124 * TODO: full sized shift could be optimized but that would need
3125 * specialized skb free'er to handle frags without up-to-date nr_frags.
3126 */
3127int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3128{
3129 int from, to, merge, todo;
3130 struct skb_frag_struct *fragfrom, *fragto;
3131
3132 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003133
3134 if (skb_headlen(skb))
3135 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003136 if (skb_zcopy(tgt) || skb_zcopy(skb))
3137 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003138
3139 todo = shiftlen;
3140 from = 0;
3141 to = skb_shinfo(tgt)->nr_frags;
3142 fragfrom = &skb_shinfo(skb)->frags[from];
3143
3144 /* Actual merge is delayed until the point when we know we can
3145 * commit all, so that we don't have to undo partial changes
3146 */
3147 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003148 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3149 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003150 merge = -1;
3151 } else {
3152 merge = to - 1;
3153
Eric Dumazet9e903e02011-10-18 21:00:24 +00003154 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003155 if (todo < 0) {
3156 if (skb_prepare_for_shift(skb) ||
3157 skb_prepare_for_shift(tgt))
3158 return 0;
3159
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003160 /* All previous frag pointers might be stale! */
3161 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003162 fragto = &skb_shinfo(tgt)->frags[merge];
3163
Eric Dumazet9e903e02011-10-18 21:00:24 +00003164 skb_frag_size_add(fragto, shiftlen);
3165 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003166 fragfrom->page_offset += shiftlen;
3167
3168 goto onlymerged;
3169 }
3170
3171 from++;
3172 }
3173
3174 /* Skip full, not-fitting skb to avoid expensive operations */
3175 if ((shiftlen == skb->len) &&
3176 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3177 return 0;
3178
3179 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3180 return 0;
3181
3182 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3183 if (to == MAX_SKB_FRAGS)
3184 return 0;
3185
3186 fragfrom = &skb_shinfo(skb)->frags[from];
3187 fragto = &skb_shinfo(tgt)->frags[to];
3188
Eric Dumazet9e903e02011-10-18 21:00:24 +00003189 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003190 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003191 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003192 from++;
3193 to++;
3194
3195 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003196 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003197 fragto->page = fragfrom->page;
3198 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003199 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003200
3201 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003202 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003203 todo = 0;
3204
3205 to++;
3206 break;
3207 }
3208 }
3209
3210 /* Ready to "commit" this state change to tgt */
3211 skb_shinfo(tgt)->nr_frags = to;
3212
3213 if (merge >= 0) {
3214 fragfrom = &skb_shinfo(skb)->frags[0];
3215 fragto = &skb_shinfo(tgt)->frags[merge];
3216
Eric Dumazet9e903e02011-10-18 21:00:24 +00003217 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003218 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003219 }
3220
3221 /* Reposition in the original skb */
3222 to = 0;
3223 while (from < skb_shinfo(skb)->nr_frags)
3224 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3225 skb_shinfo(skb)->nr_frags = to;
3226
3227 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3228
3229onlymerged:
3230 /* Most likely the tgt won't ever need its checksum anymore, skb on
3231 * the other hand might need it if it needs to be resent
3232 */
3233 tgt->ip_summed = CHECKSUM_PARTIAL;
3234 skb->ip_summed = CHECKSUM_PARTIAL;
3235
3236 /* Yak, is it really working this way? Some helper please? */
3237 skb->len -= shiftlen;
3238 skb->data_len -= shiftlen;
3239 skb->truesize -= shiftlen;
3240 tgt->len += shiftlen;
3241 tgt->data_len += shiftlen;
3242 tgt->truesize += shiftlen;
3243
3244 return shiftlen;
3245}
3246
Thomas Graf677e90e2005-06-23 20:59:51 -07003247/**
3248 * skb_prepare_seq_read - Prepare a sequential read of skb data
3249 * @skb: the buffer to read
3250 * @from: lower offset of data to be read
3251 * @to: upper offset of data to be read
3252 * @st: state variable
3253 *
3254 * Initializes the specified state variable. Must be called before
3255 * invoking skb_seq_read() for the first time.
3256 */
3257void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3258 unsigned int to, struct skb_seq_state *st)
3259{
3260 st->lower_offset = from;
3261 st->upper_offset = to;
3262 st->root_skb = st->cur_skb = skb;
3263 st->frag_idx = st->stepped_offset = 0;
3264 st->frag_data = NULL;
3265}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003266EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003267
3268/**
3269 * skb_seq_read - Sequentially read skb data
3270 * @consumed: number of bytes consumed by the caller so far
3271 * @data: destination pointer for data to be returned
3272 * @st: state variable
3273 *
Mathias Krausebc323832013-11-07 14:18:26 +01003274 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003275 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003276 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003277 * of the block or 0 if the end of the skb data or the upper
3278 * offset has been reached.
3279 *
3280 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003281 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003282 * of bytes already consumed and the next call to
3283 * skb_seq_read() will return the remaining part of the block.
3284 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003285 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003286 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003287 * reads of potentially non linear data.
3288 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003289 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003290 * at the moment, state->root_skb could be replaced with
3291 * a stack for this purpose.
3292 */
3293unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3294 struct skb_seq_state *st)
3295{
3296 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3297 skb_frag_t *frag;
3298
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003299 if (unlikely(abs_offset >= st->upper_offset)) {
3300 if (st->frag_data) {
3301 kunmap_atomic(st->frag_data);
3302 st->frag_data = NULL;
3303 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003304 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003305 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003306
3307next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003308 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003309
Thomas Chenault995b3372009-05-18 21:43:27 -07003310 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003311 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003312 return block_limit - abs_offset;
3313 }
3314
3315 if (st->frag_idx == 0 && !st->frag_data)
3316 st->stepped_offset += skb_headlen(st->cur_skb);
3317
3318 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3319 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003320 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003321
3322 if (abs_offset < block_limit) {
3323 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003324 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003325
3326 *data = (u8 *) st->frag_data + frag->page_offset +
3327 (abs_offset - st->stepped_offset);
3328
3329 return block_limit - abs_offset;
3330 }
3331
3332 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003333 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003334 st->frag_data = NULL;
3335 }
3336
3337 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003338 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003339 }
3340
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003341 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003342 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003343 st->frag_data = NULL;
3344 }
3345
David S. Miller21dc3302010-08-23 00:13:46 -07003346 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003347 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003348 st->frag_idx = 0;
3349 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003350 } else if (st->cur_skb->next) {
3351 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003352 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003353 goto next_skb;
3354 }
3355
3356 return 0;
3357}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003358EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003359
3360/**
3361 * skb_abort_seq_read - Abort a sequential read of skb data
3362 * @st: state variable
3363 *
3364 * Must be called if skb_seq_read() was not called until it
3365 * returned 0.
3366 */
3367void skb_abort_seq_read(struct skb_seq_state *st)
3368{
3369 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003370 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003371}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003372EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003373
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003374#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3375
3376static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3377 struct ts_config *conf,
3378 struct ts_state *state)
3379{
3380 return skb_seq_read(offset, text, TS_SKB_CB(state));
3381}
3382
3383static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3384{
3385 skb_abort_seq_read(TS_SKB_CB(state));
3386}
3387
3388/**
3389 * skb_find_text - Find a text pattern in skb data
3390 * @skb: the buffer to look in
3391 * @from: search offset
3392 * @to: search limit
3393 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003394 *
3395 * Finds a pattern in the skb data according to the specified
3396 * textsearch configuration. Use textsearch_next() to retrieve
3397 * subsequent occurrences of the pattern. Returns the offset
3398 * to the first occurrence or UINT_MAX if no match was found.
3399 */
3400unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003401 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003402{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003403 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003404 unsigned int ret;
3405
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003406 config->get_next_block = skb_ts_get_next_block;
3407 config->finish = skb_ts_finish;
3408
Bojan Prtvar059a2442015-02-22 11:46:35 +01003409 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003410
Bojan Prtvar059a2442015-02-22 11:46:35 +01003411 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003412 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003413}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003414EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003415
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003416int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3417 int offset, size_t size)
3418{
3419 int i = skb_shinfo(skb)->nr_frags;
3420
3421 if (skb_can_coalesce(skb, i, page, offset)) {
3422 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3423 } else if (i < MAX_SKB_FRAGS) {
3424 get_page(page);
3425 skb_fill_page_desc(skb, i, page, offset, size);
3426 } else {
3427 return -EMSGSIZE;
3428 }
3429
3430 return 0;
3431}
3432EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3433
Herbert Xucbb042f2006-03-20 22:43:56 -08003434/**
3435 * skb_pull_rcsum - pull skb and update receive checksum
3436 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003437 * @len: length of data pulled
3438 *
3439 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003440 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003441 * receive path processing instead of skb_pull unless you know
3442 * that the checksum difference is zero (e.g., a valid IP header)
3443 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003444 */
Johannes Bergaf728682017-06-16 14:29:22 +02003445void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003446{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003447 unsigned char *data = skb->data;
3448
Herbert Xucbb042f2006-03-20 22:43:56 -08003449 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003450 __skb_pull(skb, len);
3451 skb_postpull_rcsum(skb, data, len);
3452 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003453}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003454EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3455
Yonghong Song13acc942018-03-21 16:31:03 -07003456static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3457{
3458 skb_frag_t head_frag;
3459 struct page *page;
3460
3461 page = virt_to_head_page(frag_skb->head);
3462 head_frag.page.p = page;
3463 head_frag.page_offset = frag_skb->data -
3464 (unsigned char *)page_address(page);
3465 head_frag.size = skb_headlen(frag_skb);
3466 return head_frag;
3467}
3468
Herbert Xuf4c50d92006-06-22 03:02:40 -07003469/**
3470 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003471 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003472 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003473 *
3474 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003475 * a pointer to the first in a list of new skbs for the segments.
3476 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003477 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003478struct sk_buff *skb_segment(struct sk_buff *head_skb,
3479 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003480{
3481 struct sk_buff *segs = NULL;
3482 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003483 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003484 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3485 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3486 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003487 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003488 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003489 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003490 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003491 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003492 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003493 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003494 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003495 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003496 int err = -ENOMEM;
3497 int i = 0;
3498 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003499 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003500
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003501 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003502 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003503 if (unlikely(!proto))
3504 return ERR_PTR(-EINVAL);
3505
Alexander Duyck36c98382016-05-02 09:38:18 -07003506 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003507 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003508
Steffen Klassert07b26c92016-09-19 12:58:47 +02003509 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3510 if (!(features & NETIF_F_GSO_PARTIAL)) {
3511 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003512 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003513
3514 if (!list_skb ||
3515 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3516 goto normal;
3517
Ilan Tayari43170c42017-04-19 21:26:07 +03003518 /* If we get here then all the required
3519 * GSO features except frag_list are supported.
3520 * Try to split the SKB to multiple GSO SKBs
3521 * with no frag_list.
3522 * Currently we can do that only when the buffers don't
3523 * have a linear part and all the buffers except
3524 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003525 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003526 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003527 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003528 if (frag_len != iter->len && iter->next)
3529 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003530 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003531 goto normal;
3532
3533 len -= iter->len;
3534 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003535
3536 if (len != frag_len)
3537 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003538 }
3539
3540 /* GSO partial only requires that we trim off any excess that
3541 * doesn't fit into an MSS sized block, so take care of that
3542 * now.
3543 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003544 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003545 if (partial_segs > 1)
3546 mss *= partial_segs;
3547 else
3548 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003549 }
3550
Steffen Klassert07b26c92016-09-19 12:58:47 +02003551normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003552 headroom = skb_headroom(head_skb);
3553 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003554
3555 do {
3556 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003557 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003558 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003559 int size;
3560
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003561 if (unlikely(mss == GSO_BY_FRAGS)) {
3562 len = list_skb->len;
3563 } else {
3564 len = head_skb->len - offset;
3565 if (len > mss)
3566 len = mss;
3567 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003568
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003569 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003570 if (hsize < 0)
3571 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003572 if (hsize > len || !sg)
3573 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003574
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003575 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3576 (skb_headlen(list_skb) == len || sg)) {
3577 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003578
Herbert Xu9d8506c2013-11-21 11:10:04 -08003579 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003580 nfrags = skb_shinfo(list_skb)->nr_frags;
3581 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003582 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003583 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003584
3585 while (pos < offset + len) {
3586 BUG_ON(i >= nfrags);
3587
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003588 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003589 if (pos + size > offset + len)
3590 break;
3591
3592 i++;
3593 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003594 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003595 }
3596
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003597 nskb = skb_clone(list_skb, GFP_ATOMIC);
3598 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003599
3600 if (unlikely(!nskb))
3601 goto err;
3602
Herbert Xu9d8506c2013-11-21 11:10:04 -08003603 if (unlikely(pskb_trim(nskb, len))) {
3604 kfree_skb(nskb);
3605 goto err;
3606 }
3607
Alexander Duyckec47ea82012-05-04 14:26:56 +00003608 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003609 if (skb_cow_head(nskb, doffset + headroom)) {
3610 kfree_skb(nskb);
3611 goto err;
3612 }
3613
Alexander Duyckec47ea82012-05-04 14:26:56 +00003614 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003615 skb_release_head_state(nskb);
3616 __skb_push(nskb, doffset);
3617 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003618 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003619 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003620 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003621
3622 if (unlikely(!nskb))
3623 goto err;
3624
3625 skb_reserve(nskb, headroom);
3626 __skb_put(nskb, doffset);
3627 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003628
3629 if (segs)
3630 tail->next = nskb;
3631 else
3632 segs = nskb;
3633 tail = nskb;
3634
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003635 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003636
Eric Dumazet030737b2013-10-19 11:42:54 -07003637 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003638 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003639
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003640 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003641 nskb->data - tnl_hlen,
3642 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003643
Herbert Xu9d8506c2013-11-21 11:10:04 -08003644 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003645 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003646
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003647 if (!sg) {
3648 if (!nskb->remcsum_offload)
3649 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003650 SKB_GSO_CB(nskb)->csum =
3651 skb_copy_and_csum_bits(head_skb, offset,
3652 skb_put(nskb, len),
3653 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003654 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003655 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003656 continue;
3657 }
3658
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003659 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003660
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003661 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003662 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003663
Willem de Bruijnfff88032017-06-08 11:35:03 -04003664 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3665 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003666
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003667 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3668 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3669 goto err;
3670
Herbert Xu9d8506c2013-11-21 11:10:04 -08003671 while (pos < offset + len) {
3672 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08003673 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003674 nfrags = skb_shinfo(list_skb)->nr_frags;
3675 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003676 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07003677 if (!skb_headlen(list_skb)) {
3678 BUG_ON(!nfrags);
3679 } else {
3680 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003681
Yonghong Song13acc942018-03-21 16:31:03 -07003682 /* to make room for head_frag. */
3683 i--;
3684 frag--;
3685 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003686 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3687 skb_zerocopy_clone(nskb, frag_skb,
3688 GFP_ATOMIC))
3689 goto err;
3690
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003691 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003692 }
3693
3694 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3695 MAX_SKB_FRAGS)) {
3696 net_warn_ratelimited(
3697 "skb_segment: too many frags: %u %u\n",
3698 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07003699 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003700 goto err;
3701 }
3702
Yonghong Song13acc942018-03-21 16:31:03 -07003703 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003704 __skb_frag_ref(nskb_frag);
3705 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003706
3707 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003708 nskb_frag->page_offset += offset - pos;
3709 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003710 }
3711
Herbert Xu89319d382008-12-15 23:26:06 -08003712 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003713
3714 if (pos + size <= offset + len) {
3715 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003716 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003717 pos += size;
3718 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003719 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003720 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003721 }
3722
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003723 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003724 }
3725
Herbert Xu89319d382008-12-15 23:26:06 -08003726skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003727 nskb->data_len = len - hsize;
3728 nskb->len += nskb->data_len;
3729 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003730
Simon Horman1cdbcb72013-05-19 15:46:49 +00003731perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003732 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07003733 if (skb_has_shared_frag(nskb) &&
3734 __skb_linearize(nskb))
3735 goto err;
3736
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003737 if (!nskb->remcsum_offload)
3738 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003739 SKB_GSO_CB(nskb)->csum =
3740 skb_checksum(nskb, doffset,
3741 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003742 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003743 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003744 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003745 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003746
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003747 /* Some callers want to get the end of the list.
3748 * Put it in segs->prev to avoid walking the list.
3749 * (see validate_xmit_skb_list() for example)
3750 */
3751 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003752
Alexander Duyck802ab552016-04-10 21:45:03 -04003753 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003754 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003755 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003756 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003757
3758 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003759 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003760 type &= ~SKB_GSO_DODGY;
3761
3762 /* Update GSO info and prepare to start updating headers on
3763 * our way back down the stack of protocols.
3764 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003765 for (iter = segs; iter; iter = iter->next) {
3766 skb_shinfo(iter)->gso_size = gso_size;
3767 skb_shinfo(iter)->gso_segs = partial_segs;
3768 skb_shinfo(iter)->gso_type = type;
3769 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3770 }
3771
3772 if (tail->len - doffset <= gso_size)
3773 skb_shinfo(tail)->gso_size = 0;
3774 else if (tail != segs)
3775 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003776 }
3777
Toshiaki Makita432c8562014-10-27 10:30:51 -07003778 /* Following permits correct backpressure, for protocols
3779 * using skb_set_owner_w().
3780 * Idea is to tranfert ownership from head_skb to last segment.
3781 */
3782 if (head_skb->destructor == sock_wfree) {
3783 swap(tail->truesize, head_skb->truesize);
3784 swap(tail->destructor, head_skb->destructor);
3785 swap(tail->sk, head_skb->sk);
3786 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003787 return segs;
3788
3789err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003790 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003791 return ERR_PTR(err);
3792}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003793EXPORT_SYMBOL_GPL(skb_segment);
3794
David Millerd4546c22018-06-24 14:13:49 +09003795int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08003796{
Eric Dumazet8a291112013-10-08 09:02:23 -07003797 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003798 unsigned int offset = skb_gro_offset(skb);
3799 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003800 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003801 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09003802 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08003803
Eric Dumazet8a291112013-10-08 09:02:23 -07003804 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003805 return -E2BIG;
3806
Eric Dumazet29e98242014-05-16 11:34:37 -07003807 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003808 pinfo = skb_shinfo(lp);
3809
3810 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003811 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003812 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003813 int i = skbinfo->nr_frags;
3814 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003815
Herbert Xu66e92fc2009-05-26 18:50:32 +00003816 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003817 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003818
Eric Dumazet8a291112013-10-08 09:02:23 -07003819 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003820 pinfo->nr_frags = nr_frags;
3821 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003822
Herbert Xu9aaa1562009-05-26 18:50:33 +00003823 frag = pinfo->frags + nr_frags;
3824 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003825 do {
3826 *--frag = *--frag2;
3827 } while (--i);
3828
3829 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003830 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003831
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003832 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003833 delta_truesize = skb->truesize -
3834 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003835
Herbert Xuf5572062009-01-14 20:40:03 -08003836 skb->truesize -= skb->data_len;
3837 skb->len -= skb->data_len;
3838 skb->data_len = 0;
3839
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003840 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003841 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003842 } else if (skb->head_frag) {
3843 int nr_frags = pinfo->nr_frags;
3844 skb_frag_t *frag = pinfo->frags + nr_frags;
3845 struct page *page = virt_to_head_page(skb->head);
3846 unsigned int first_size = headlen - offset;
3847 unsigned int first_offset;
3848
3849 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003850 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003851
3852 first_offset = skb->data -
3853 (unsigned char *)page_address(page) +
3854 offset;
3855
3856 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3857
3858 frag->page.p = page;
3859 frag->page_offset = first_offset;
3860 skb_frag_size_set(frag, first_size);
3861
3862 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3863 /* We dont need to clear skbinfo->nr_frags here */
3864
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003865 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003866 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3867 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003868 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003869
3870merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003871 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003872 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003873 unsigned int eat = offset - headlen;
3874
3875 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003876 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003877 skb->data_len -= eat;
3878 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003879 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003880 }
3881
Herbert Xu67147ba2009-05-26 18:50:22 +00003882 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003883
Eric Dumazet29e98242014-05-16 11:34:37 -07003884 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003885 skb_shinfo(p)->frag_list = skb;
3886 else
3887 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003888 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003889 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003890 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003891
Herbert Xu5d38a072009-01-04 16:13:40 -08003892done:
3893 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003894 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003895 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003896 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003897 if (lp != p) {
3898 lp->data_len += len;
3899 lp->truesize += delta_truesize;
3900 lp->len += len;
3901 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003902 NAPI_GRO_CB(skb)->same_flow = 1;
3903 return 0;
3904}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003905EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003906
Florian Westphaldf5042f2018-12-18 17:15:16 +01003907#ifdef CONFIG_SKB_EXTENSIONS
3908#define SKB_EXT_ALIGN_VALUE 8
3909#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3910
3911static const u8 skb_ext_type_len[] = {
3912#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3913 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3914#endif
Florian Westphal41650792018-12-18 17:15:27 +01003915#ifdef CONFIG_XFRM
3916 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3917#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01003918};
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
Florian Westphal41650792018-12-18 17:15:27 +01003926#ifdef CONFIG_XFRM
3927 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3928#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01003929 0;
3930}
3931
3932static void skb_extensions_init(void)
3933{
3934 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
3935 BUILD_BUG_ON(skb_ext_total_length() > 255);
3936
3937 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
3938 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
3939 0,
3940 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3941 NULL);
3942}
3943#else
3944static void skb_extensions_init(void) {}
3945#endif
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947void __init skb_init(void)
3948{
Kees Cook79a8a642018-02-07 17:44:38 -08003949 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 sizeof(struct sk_buff),
3951 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003952 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08003953 offsetof(struct sk_buff, cb),
3954 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09003955 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003956 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003957 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003958 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003959 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003960 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01003961 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962}
3963
David S. Miller51c739d2007-10-30 21:29:29 -07003964static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003965__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3966 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07003967{
David S. Miller1a028e52007-04-27 15:21:23 -07003968 int start = skb_headlen(skb);
3969 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003970 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003971 int elt = 0;
3972
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003973 if (unlikely(recursion_level >= 24))
3974 return -EMSGSIZE;
3975
David Howells716ea3a2007-04-02 20:19:53 -07003976 if (copy > 0) {
3977 if (copy > len)
3978 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003979 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003980 elt++;
3981 if ((len -= copy) == 0)
3982 return elt;
3983 offset += copy;
3984 }
3985
3986 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003987 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003988
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003989 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003990
Eric Dumazet9e903e02011-10-18 21:00:24 +00003991 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003992 if ((copy = end - offset) > 0) {
3993 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02003994 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3995 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07003996
3997 if (copy > len)
3998 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003999 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02004000 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07004001 elt++;
4002 if (!(len -= copy))
4003 return elt;
4004 offset += copy;
4005 }
David S. Miller1a028e52007-04-27 15:21:23 -07004006 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004007 }
4008
David S. Millerfbb398a2009-06-09 00:18:59 -07004009 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004010 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004011
David S. Millerfbb398a2009-06-09 00:18:59 -07004012 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004013
David S. Millerfbb398a2009-06-09 00:18:59 -07004014 end = start + frag_iter->len;
4015 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004016 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4017 return -EMSGSIZE;
4018
David S. Millerfbb398a2009-06-09 00:18:59 -07004019 if (copy > len)
4020 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004021 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4022 copy, recursion_level + 1);
4023 if (unlikely(ret < 0))
4024 return ret;
4025 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004026 if ((len -= copy) == 0)
4027 return elt;
4028 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004029 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004030 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004031 }
4032 BUG_ON(len);
4033 return elt;
4034}
4035
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004036/**
4037 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4038 * @skb: Socket buffer containing the buffers to be mapped
4039 * @sg: The scatter-gather list to map into
4040 * @offset: The offset into the buffer's contents to start mapping
4041 * @len: Length of buffer space to be mapped
4042 *
4043 * Fill the specified scatter-gather list with mappings/pointers into a
4044 * region of the buffer space attached to a socket buffer. Returns either
4045 * the number of scatterlist items used, or -EMSGSIZE if the contents
4046 * could not fit.
4047 */
4048int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4049{
4050 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4051
4052 if (nsg <= 0)
4053 return nsg;
4054
4055 sg_mark_end(&sg[nsg - 1]);
4056
4057 return nsg;
4058}
4059EXPORT_SYMBOL_GPL(skb_to_sgvec);
4060
Fan Du25a91d82014-01-18 09:54:23 +08004061/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4062 * sglist without mark the sg which contain last skb data as the end.
4063 * So the caller can mannipulate sg list as will when padding new data after
4064 * the first call without calling sg_unmark_end to expend sg list.
4065 *
4066 * Scenario to use skb_to_sgvec_nomark:
4067 * 1. sg_init_table
4068 * 2. skb_to_sgvec_nomark(payload1)
4069 * 3. skb_to_sgvec_nomark(payload2)
4070 *
4071 * This is equivalent to:
4072 * 1. sg_init_table
4073 * 2. skb_to_sgvec(payload1)
4074 * 3. sg_unmark_end
4075 * 4. skb_to_sgvec(payload2)
4076 *
4077 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4078 * is more preferable.
4079 */
4080int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4081 int offset, int len)
4082{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004083 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004084}
4085EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4086
David S. Miller51c739d2007-10-30 21:29:29 -07004087
David S. Miller51c739d2007-10-30 21:29:29 -07004088
David Howells716ea3a2007-04-02 20:19:53 -07004089/**
4090 * skb_cow_data - Check that a socket buffer's data buffers are writable
4091 * @skb: The socket buffer to check.
4092 * @tailbits: Amount of trailing space to be added
4093 * @trailer: Returned pointer to the skb where the @tailbits space begins
4094 *
4095 * Make sure that the data buffers attached to a socket buffer are
4096 * writable. If they are not, private copies are made of the data buffers
4097 * and the socket buffer is set to use these instead.
4098 *
4099 * If @tailbits is given, make sure that there is space to write @tailbits
4100 * bytes of data beyond current end of socket buffer. @trailer will be
4101 * set to point to the skb in which this space begins.
4102 *
4103 * The number of scatterlist elements required to completely map the
4104 * COW'd and extended socket buffer will be returned.
4105 */
4106int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4107{
4108 int copyflag;
4109 int elt;
4110 struct sk_buff *skb1, **skb_p;
4111
4112 /* If skb is cloned or its head is paged, reallocate
4113 * head pulling out all the pages (pages are considered not writable
4114 * at the moment even if they are anonymous).
4115 */
4116 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4117 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4118 return -ENOMEM;
4119
4120 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004121 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004122 /* A little of trouble, not enough of space for trailer.
4123 * This should not happen, when stack is tuned to generate
4124 * good frames. OK, on miss we reallocate and reserve even more
4125 * space, 128 bytes is fair. */
4126
4127 if (skb_tailroom(skb) < tailbits &&
4128 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4129 return -ENOMEM;
4130
4131 /* Voila! */
4132 *trailer = skb;
4133 return 1;
4134 }
4135
4136 /* Misery. We are in troubles, going to mincer fragments... */
4137
4138 elt = 1;
4139 skb_p = &skb_shinfo(skb)->frag_list;
4140 copyflag = 0;
4141
4142 while ((skb1 = *skb_p) != NULL) {
4143 int ntail = 0;
4144
4145 /* The fragment is partially pulled by someone,
4146 * this can happen on input. Copy it and everything
4147 * after it. */
4148
4149 if (skb_shared(skb1))
4150 copyflag = 1;
4151
4152 /* If the skb is the last, worry about trailer. */
4153
4154 if (skb1->next == NULL && tailbits) {
4155 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004156 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004157 skb_tailroom(skb1) < tailbits)
4158 ntail = tailbits + 128;
4159 }
4160
4161 if (copyflag ||
4162 skb_cloned(skb1) ||
4163 ntail ||
4164 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004165 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004166 struct sk_buff *skb2;
4167
4168 /* Fuck, we are miserable poor guys... */
4169 if (ntail == 0)
4170 skb2 = skb_copy(skb1, GFP_ATOMIC);
4171 else
4172 skb2 = skb_copy_expand(skb1,
4173 skb_headroom(skb1),
4174 ntail,
4175 GFP_ATOMIC);
4176 if (unlikely(skb2 == NULL))
4177 return -ENOMEM;
4178
4179 if (skb1->sk)
4180 skb_set_owner_w(skb2, skb1->sk);
4181
4182 /* Looking around. Are we still alive?
4183 * OK, link new skb, drop old one */
4184
4185 skb2->next = skb1->next;
4186 *skb_p = skb2;
4187 kfree_skb(skb1);
4188 skb1 = skb2;
4189 }
4190 elt++;
4191 *trailer = skb1;
4192 skb_p = &skb1->next;
4193 }
4194
4195 return elt;
4196}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004197EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004198
Eric Dumazetb1faf562010-05-31 23:44:05 -07004199static void sock_rmem_free(struct sk_buff *skb)
4200{
4201 struct sock *sk = skb->sk;
4202
4203 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4204}
4205
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004206static void skb_set_err_queue(struct sk_buff *skb)
4207{
4208 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4209 * So, it is safe to (mis)use it to mark skbs on the error queue.
4210 */
4211 skb->pkt_type = PACKET_OUTGOING;
4212 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4213}
4214
Eric Dumazetb1faf562010-05-31 23:44:05 -07004215/*
4216 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4217 */
4218int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4219{
4220 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004221 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004222 return -ENOMEM;
4223
4224 skb_orphan(skb);
4225 skb->sk = sk;
4226 skb->destructor = sock_rmem_free;
4227 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004228 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004229
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004230 /* before exiting rcu section, make sure dst is refcounted */
4231 skb_dst_force(skb);
4232
Eric Dumazetb1faf562010-05-31 23:44:05 -07004233 skb_queue_tail(&sk->sk_error_queue, skb);
4234 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004235 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004236 return 0;
4237}
4238EXPORT_SYMBOL(sock_queue_err_skb);
4239
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004240static bool is_icmp_err_skb(const struct sk_buff *skb)
4241{
4242 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4243 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4244}
4245
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004246struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4247{
4248 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004249 struct sk_buff *skb, *skb_next = NULL;
4250 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004251 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004252
Eric Dumazet997d5c32015-02-18 05:47:55 -08004253 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004254 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004255 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004256 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004257 if (icmp_next)
4258 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4259 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004260 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004261
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004262 if (is_icmp_err_skb(skb) && !icmp_next)
4263 sk->sk_err = 0;
4264
4265 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004266 sk->sk_error_report(sk);
4267
4268 return skb;
4269}
4270EXPORT_SYMBOL(sock_dequeue_err_skb);
4271
Alexander Duyckcab41c42014-09-10 18:05:26 -04004272/**
4273 * skb_clone_sk - create clone of skb, and take reference to socket
4274 * @skb: the skb to clone
4275 *
4276 * This function creates a clone of a buffer that holds a reference on
4277 * sk_refcnt. Buffers created via this function are meant to be
4278 * returned using sock_queue_err_skb, or free via kfree_skb.
4279 *
4280 * When passing buffers allocated with this function to sock_queue_err_skb
4281 * it is necessary to wrap the call with sock_hold/sock_put in order to
4282 * prevent the socket from being released prior to being enqueued on
4283 * the sk_error_queue.
4284 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004285struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4286{
4287 struct sock *sk = skb->sk;
4288 struct sk_buff *clone;
4289
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004290 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004291 return NULL;
4292
4293 clone = skb_clone(skb, GFP_ATOMIC);
4294 if (!clone) {
4295 sock_put(sk);
4296 return NULL;
4297 }
4298
4299 clone->sk = sk;
4300 clone->destructor = sock_efree;
4301
4302 return clone;
4303}
4304EXPORT_SYMBOL(skb_clone_sk);
4305
Alexander Duyck37846ef2014-09-04 13:31:10 -04004306static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4307 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004308 int tstype,
4309 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004310{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004311 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004312 int err;
4313
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004314 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4315
Patrick Ohlyac45f602009-02-12 05:03:37 +00004316 serr = SKB_EXT_ERR(skb);
4317 memset(serr, 0, sizeof(*serr));
4318 serr->ee.ee_errno = ENOMSG;
4319 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004320 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004321 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004322 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004323 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004324 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004325 if (sk->sk_protocol == IPPROTO_TCP &&
4326 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004327 serr->ee.ee_data -= sk->sk_tskey;
4328 }
Eric Dumazet29030372010-05-29 00:20:48 -07004329
Patrick Ohlyac45f602009-02-12 05:03:37 +00004330 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004331
Patrick Ohlyac45f602009-02-12 05:03:37 +00004332 if (err)
4333 kfree_skb(skb);
4334}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004335
Willem de Bruijnb245be12015-01-30 13:29:32 -05004336static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4337{
4338 bool ret;
4339
4340 if (likely(sysctl_tstamp_allow_data || tsonly))
4341 return true;
4342
4343 read_lock_bh(&sk->sk_callback_lock);
4344 ret = sk->sk_socket && sk->sk_socket->file &&
4345 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4346 read_unlock_bh(&sk->sk_callback_lock);
4347 return ret;
4348}
4349
Alexander Duyck37846ef2014-09-04 13:31:10 -04004350void skb_complete_tx_timestamp(struct sk_buff *skb,
4351 struct skb_shared_hwtstamps *hwtstamps)
4352{
4353 struct sock *sk = skb->sk;
4354
Willem de Bruijnb245be12015-01-30 13:29:32 -05004355 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004356 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004357
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004358 /* Take a reference to prevent skb_orphan() from freeing the socket,
4359 * but only if the socket refcount is not zero.
4360 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004361 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004362 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004363 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004364 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004365 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004366 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004367
4368err:
4369 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004370}
4371EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4372
4373void __skb_tstamp_tx(struct sk_buff *orig_skb,
4374 struct skb_shared_hwtstamps *hwtstamps,
4375 struct sock *sk, int tstype)
4376{
4377 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004378 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004379
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004380 if (!sk)
4381 return;
4382
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004383 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4384 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4385 return;
4386
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004387 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4388 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004389 return;
4390
Francis Yan1c885802016-11-27 23:07:18 -08004391 if (tsonly) {
4392#ifdef CONFIG_INET
4393 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4394 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004395 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004396 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004397 opt_stats = true;
4398 } else
Francis Yan1c885802016-11-27 23:07:18 -08004399#endif
4400 skb = alloc_skb(0, GFP_ATOMIC);
4401 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004402 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004403 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004404 if (!skb)
4405 return;
4406
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004407 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004408 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4409 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004410 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4411 }
4412
4413 if (hwtstamps)
4414 *skb_hwtstamps(skb) = *hwtstamps;
4415 else
4416 skb->tstamp = ktime_get_real();
4417
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004418 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004419}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004420EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4421
4422void skb_tstamp_tx(struct sk_buff *orig_skb,
4423 struct skb_shared_hwtstamps *hwtstamps)
4424{
4425 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4426 SCM_TSTAMP_SND);
4427}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004428EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4429
Johannes Berg6e3e9392011-11-09 10:15:42 +01004430void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4431{
4432 struct sock *sk = skb->sk;
4433 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004434 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004435
4436 skb->wifi_acked_valid = 1;
4437 skb->wifi_acked = acked;
4438
4439 serr = SKB_EXT_ERR(skb);
4440 memset(serr, 0, sizeof(*serr));
4441 serr->ee.ee_errno = ENOMSG;
4442 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4443
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004444 /* Take a reference to prevent skb_orphan() from freeing the socket,
4445 * but only if the socket refcount is not zero.
4446 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004447 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004448 err = sock_queue_err_skb(sk, skb);
4449 sock_put(sk);
4450 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004451 if (err)
4452 kfree_skb(skb);
4453}
4454EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4455
Rusty Russellf35d9d82008-02-04 23:49:54 -05004456/**
4457 * skb_partial_csum_set - set up and verify partial csum values for packet
4458 * @skb: the skb to set
4459 * @start: the number of bytes after skb->data to start checksumming.
4460 * @off: the offset from start to place the checksum.
4461 *
4462 * For untrusted partially-checksummed packets, we need to make sure the values
4463 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4464 *
4465 * This function checks and sets those values and skb->ip_summed: if this
4466 * returns false you should drop the packet.
4467 */
4468bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4469{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004470 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4471 u32 csum_start = skb_headroom(skb) + (u32)start;
4472
4473 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4474 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4475 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004476 return false;
4477 }
4478 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004479 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004480 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004481 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004482 return true;
4483}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004484EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004485
Paul Durranted1f50c2014-01-09 10:02:46 +00004486static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4487 unsigned int max)
4488{
4489 if (skb_headlen(skb) >= len)
4490 return 0;
4491
4492 /* If we need to pullup then pullup to the max, so we
4493 * won't need to do it again.
4494 */
4495 if (max > skb->len)
4496 max = skb->len;
4497
4498 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4499 return -ENOMEM;
4500
4501 if (skb_headlen(skb) < len)
4502 return -EPROTO;
4503
4504 return 0;
4505}
4506
Jan Beulichf9708b42014-03-11 13:56:05 +00004507#define MAX_TCP_HDR_LEN (15 * 4)
4508
4509static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4510 typeof(IPPROTO_IP) proto,
4511 unsigned int off)
4512{
4513 switch (proto) {
4514 int err;
4515
4516 case IPPROTO_TCP:
4517 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4518 off + MAX_TCP_HDR_LEN);
4519 if (!err && !skb_partial_csum_set(skb, off,
4520 offsetof(struct tcphdr,
4521 check)))
4522 err = -EPROTO;
4523 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4524
4525 case IPPROTO_UDP:
4526 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4527 off + sizeof(struct udphdr));
4528 if (!err && !skb_partial_csum_set(skb, off,
4529 offsetof(struct udphdr,
4530 check)))
4531 err = -EPROTO;
4532 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4533 }
4534
4535 return ERR_PTR(-EPROTO);
4536}
4537
Paul Durranted1f50c2014-01-09 10:02:46 +00004538/* This value should be large enough to cover a tagged ethernet header plus
4539 * maximally sized IP and TCP or UDP headers.
4540 */
4541#define MAX_IP_HDR_LEN 128
4542
Jan Beulichf9708b42014-03-11 13:56:05 +00004543static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004544{
4545 unsigned int off;
4546 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004547 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004548 int err;
4549
4550 fragment = false;
4551
4552 err = skb_maybe_pull_tail(skb,
4553 sizeof(struct iphdr),
4554 MAX_IP_HDR_LEN);
4555 if (err < 0)
4556 goto out;
4557
4558 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4559 fragment = true;
4560
4561 off = ip_hdrlen(skb);
4562
4563 err = -EPROTO;
4564
4565 if (fragment)
4566 goto out;
4567
Jan Beulichf9708b42014-03-11 13:56:05 +00004568 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4569 if (IS_ERR(csum))
4570 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004571
Jan Beulichf9708b42014-03-11 13:56:05 +00004572 if (recalculate)
4573 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4574 ip_hdr(skb)->daddr,
4575 skb->len - off,
4576 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004577 err = 0;
4578
4579out:
4580 return err;
4581}
4582
4583/* This value should be large enough to cover a tagged ethernet header plus
4584 * an IPv6 header, all options, and a maximal TCP or UDP header.
4585 */
4586#define MAX_IPV6_HDR_LEN 256
4587
4588#define OPT_HDR(type, skb, off) \
4589 (type *)(skb_network_header(skb) + (off))
4590
4591static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4592{
4593 int err;
4594 u8 nexthdr;
4595 unsigned int off;
4596 unsigned int len;
4597 bool fragment;
4598 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004599 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004600
4601 fragment = false;
4602 done = false;
4603
4604 off = sizeof(struct ipv6hdr);
4605
4606 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4607 if (err < 0)
4608 goto out;
4609
4610 nexthdr = ipv6_hdr(skb)->nexthdr;
4611
4612 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4613 while (off <= len && !done) {
4614 switch (nexthdr) {
4615 case IPPROTO_DSTOPTS:
4616 case IPPROTO_HOPOPTS:
4617 case IPPROTO_ROUTING: {
4618 struct ipv6_opt_hdr *hp;
4619
4620 err = skb_maybe_pull_tail(skb,
4621 off +
4622 sizeof(struct ipv6_opt_hdr),
4623 MAX_IPV6_HDR_LEN);
4624 if (err < 0)
4625 goto out;
4626
4627 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4628 nexthdr = hp->nexthdr;
4629 off += ipv6_optlen(hp);
4630 break;
4631 }
4632 case IPPROTO_AH: {
4633 struct ip_auth_hdr *hp;
4634
4635 err = skb_maybe_pull_tail(skb,
4636 off +
4637 sizeof(struct ip_auth_hdr),
4638 MAX_IPV6_HDR_LEN);
4639 if (err < 0)
4640 goto out;
4641
4642 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4643 nexthdr = hp->nexthdr;
4644 off += ipv6_authlen(hp);
4645 break;
4646 }
4647 case IPPROTO_FRAGMENT: {
4648 struct frag_hdr *hp;
4649
4650 err = skb_maybe_pull_tail(skb,
4651 off +
4652 sizeof(struct frag_hdr),
4653 MAX_IPV6_HDR_LEN);
4654 if (err < 0)
4655 goto out;
4656
4657 hp = OPT_HDR(struct frag_hdr, skb, off);
4658
4659 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4660 fragment = true;
4661
4662 nexthdr = hp->nexthdr;
4663 off += sizeof(struct frag_hdr);
4664 break;
4665 }
4666 default:
4667 done = true;
4668 break;
4669 }
4670 }
4671
4672 err = -EPROTO;
4673
4674 if (!done || fragment)
4675 goto out;
4676
Jan Beulichf9708b42014-03-11 13:56:05 +00004677 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4678 if (IS_ERR(csum))
4679 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004680
Jan Beulichf9708b42014-03-11 13:56:05 +00004681 if (recalculate)
4682 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4683 &ipv6_hdr(skb)->daddr,
4684 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004685 err = 0;
4686
4687out:
4688 return err;
4689}
4690
4691/**
4692 * skb_checksum_setup - set up partial checksum offset
4693 * @skb: the skb to set up
4694 * @recalculate: if true the pseudo-header checksum will be recalculated
4695 */
4696int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4697{
4698 int err;
4699
4700 switch (skb->protocol) {
4701 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004702 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004703 break;
4704
4705 case htons(ETH_P_IPV6):
4706 err = skb_checksum_setup_ipv6(skb, recalculate);
4707 break;
4708
4709 default:
4710 err = -EPROTO;
4711 break;
4712 }
4713
4714 return err;
4715}
4716EXPORT_SYMBOL(skb_checksum_setup);
4717
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004718/**
4719 * skb_checksum_maybe_trim - maybe trims the given skb
4720 * @skb: the skb to check
4721 * @transport_len: the data length beyond the network header
4722 *
4723 * Checks whether the given skb has data beyond the given transport length.
4724 * If so, returns a cloned skb trimmed to this transport length.
4725 * Otherwise returns the provided skb. Returns NULL in error cases
4726 * (e.g. transport_len exceeds skb length or out-of-memory).
4727 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004728 * Caller needs to set the skb transport header and free any returned skb if it
4729 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004730 */
4731static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4732 unsigned int transport_len)
4733{
4734 struct sk_buff *skb_chk;
4735 unsigned int len = skb_transport_offset(skb) + transport_len;
4736 int ret;
4737
Linus Lüssinga5169932015-08-13 05:54:07 +02004738 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004739 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004740 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004741 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004742
4743 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004744 if (!skb_chk)
4745 return NULL;
4746
4747 ret = pskb_trim_rcsum(skb_chk, len);
4748 if (ret) {
4749 kfree_skb(skb_chk);
4750 return NULL;
4751 }
4752
4753 return skb_chk;
4754}
4755
4756/**
4757 * skb_checksum_trimmed - validate checksum of an skb
4758 * @skb: the skb to check
4759 * @transport_len: the data length beyond the network header
4760 * @skb_chkf: checksum function to use
4761 *
4762 * Applies the given checksum function skb_chkf to the provided skb.
4763 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4764 *
4765 * If the skb has data beyond the given transport length, then a
4766 * trimmed & cloned skb is checked and returned.
4767 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004768 * Caller needs to set the skb transport header and free any returned skb if it
4769 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004770 */
4771struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4772 unsigned int transport_len,
4773 __sum16(*skb_chkf)(struct sk_buff *skb))
4774{
4775 struct sk_buff *skb_chk;
4776 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004777 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004778
4779 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4780 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004781 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004782
Linus Lüssinga5169932015-08-13 05:54:07 +02004783 if (!pskb_may_pull(skb_chk, offset))
4784 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004785
Linus Lüssing9b368812016-02-24 04:21:42 +01004786 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004787 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004788 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004789
Linus Lüssinga5169932015-08-13 05:54:07 +02004790 if (ret)
4791 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004792
4793 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004794
4795err:
4796 if (skb_chk && skb_chk != skb)
4797 kfree_skb(skb_chk);
4798
4799 return NULL;
4800
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004801}
4802EXPORT_SYMBOL(skb_checksum_trimmed);
4803
Ben Hutchings4497b072008-06-19 16:22:28 -07004804void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4805{
Joe Perchese87cc472012-05-13 21:56:26 +00004806 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4807 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004808}
Ben Hutchings4497b072008-06-19 16:22:28 -07004809EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004810
4811void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4812{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004813 if (head_stolen) {
4814 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004815 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004816 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004817 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004818 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004819}
4820EXPORT_SYMBOL(kfree_skb_partial);
4821
4822/**
4823 * skb_try_coalesce - try to merge skb to prior one
4824 * @to: prior buffer
4825 * @from: buffer to add
4826 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004827 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004828 */
4829bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4830 bool *fragstolen, int *delta_truesize)
4831{
Eric Dumazetc818fa92017-10-04 10:48:35 -07004832 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004833 int i, delta, len = from->len;
4834
4835 *fragstolen = false;
4836
4837 if (skb_cloned(to))
4838 return false;
4839
4840 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004841 if (len)
4842 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004843 *delta_truesize = 0;
4844 return true;
4845 }
4846
Eric Dumazetc818fa92017-10-04 10:48:35 -07004847 to_shinfo = skb_shinfo(to);
4848 from_shinfo = skb_shinfo(from);
4849 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004850 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004851 if (skb_zcopy(to) || skb_zcopy(from))
4852 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004853
4854 if (skb_headlen(from) != 0) {
4855 struct page *page;
4856 unsigned int offset;
4857
Eric Dumazetc818fa92017-10-04 10:48:35 -07004858 if (to_shinfo->nr_frags +
4859 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004860 return false;
4861
4862 if (skb_head_is_locked(from))
4863 return false;
4864
4865 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4866
4867 page = virt_to_head_page(from->head);
4868 offset = from->data - (unsigned char *)page_address(page);
4869
Eric Dumazetc818fa92017-10-04 10:48:35 -07004870 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004871 page, offset, skb_headlen(from));
4872 *fragstolen = true;
4873 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07004874 if (to_shinfo->nr_frags +
4875 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004876 return false;
4877
Weiping Panf4b549a2012-09-28 20:15:30 +00004878 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004879 }
4880
4881 WARN_ON_ONCE(delta < len);
4882
Eric Dumazetc818fa92017-10-04 10:48:35 -07004883 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4884 from_shinfo->frags,
4885 from_shinfo->nr_frags * sizeof(skb_frag_t));
4886 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004887
4888 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07004889 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004890
Li RongQing8ea853f2012-09-18 16:53:21 +00004891 /* if the skb is not cloned this does nothing
4892 * since we set nr_frags to 0.
4893 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07004894 for (i = 0; i < from_shinfo->nr_frags; i++)
4895 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004896
4897 to->truesize += delta;
4898 to->len += len;
4899 to->data_len += len;
4900
4901 *delta_truesize = delta;
4902 return true;
4903}
4904EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004905
4906/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004907 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004908 *
4909 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004910 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004911 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004912 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4913 * into/from a tunnel. Some information have to be cleared during these
4914 * operations.
4915 * skb_scrub_packet can also be used to clean a skb before injecting it in
4916 * another namespace (@xnet == true). We have to clear all information in the
4917 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004918 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004919void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004920{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004921 skb->pkt_type = PACKET_HOST;
4922 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004923 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004924 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004925 secpath_reset(skb);
4926 nf_reset(skb);
4927 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004928
Petr Machata6f9a5062018-11-19 16:11:07 +00004929#ifdef CONFIG_NET_SWITCHDEV
4930 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00004931 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00004932#endif
4933
Herbert Xu213dd742015-04-16 09:03:27 +08004934 if (!xnet)
4935 return;
4936
Ye Yin2b5ec1a2017-10-26 16:57:05 +08004937 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004938 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07004939 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004940}
4941EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004942
4943/**
4944 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4945 *
4946 * @skb: GSO skb
4947 *
4948 * skb_gso_transport_seglen is used to determine the real size of the
4949 * individual segments, including Layer4 headers (TCP/UDP).
4950 *
4951 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4952 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11004953static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01004954{
4955 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004956 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004957
Florian Westphalf993bc22014-10-20 13:49:18 +02004958 if (skb->encapsulation) {
4959 thlen = skb_inner_transport_header(skb) -
4960 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004961
Florian Westphalf993bc22014-10-20 13:49:18 +02004962 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4963 thlen += inner_tcp_hdrlen(skb);
4964 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4965 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11004966 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004967 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04004968 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
4969 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02004970 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004971 /* UFO sets gso_size to the size of the fragmentation
4972 * payload, i.e. the size of the L4 (UDP) header is already
4973 * accounted for.
4974 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004975 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004976}
Daniel Axtensa4a77712018-03-01 17:13:40 +11004977
4978/**
4979 * skb_gso_network_seglen - Return length of individual segments of a gso packet
4980 *
4981 * @skb: GSO skb
4982 *
4983 * skb_gso_network_seglen is used to determine the real size of the
4984 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
4985 *
4986 * The MAC/L2 header is not accounted for.
4987 */
4988static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
4989{
4990 unsigned int hdr_len = skb_transport_header(skb) -
4991 skb_network_header(skb);
4992
4993 return hdr_len + skb_gso_transport_seglen(skb);
4994}
4995
4996/**
4997 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
4998 *
4999 * @skb: GSO skb
5000 *
5001 * skb_gso_mac_seglen is used to determine the real size of the
5002 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5003 * headers (TCP/UDP).
5004 */
5005static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5006{
5007 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5008
5009 return hdr_len + skb_gso_transport_seglen(skb);
5010}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005011
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005012/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005013 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5014 *
5015 * There are a couple of instances where we have a GSO skb, and we
5016 * want to determine what size it would be after it is segmented.
5017 *
5018 * We might want to check:
5019 * - L3+L4+payload size (e.g. IP forwarding)
5020 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5021 *
5022 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5023 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005024 * @skb: GSO skb
5025 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005026 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5027 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5028 *
5029 * @max_len: The maximum permissible length.
5030 *
5031 * Returns true if the segmented length <= max length.
5032 */
5033static inline bool skb_gso_size_check(const struct sk_buff *skb,
5034 unsigned int seg_len,
5035 unsigned int max_len) {
5036 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5037 const struct sk_buff *iter;
5038
5039 if (shinfo->gso_size != GSO_BY_FRAGS)
5040 return seg_len <= max_len;
5041
5042 /* Undo this so we can re-use header sizes */
5043 seg_len -= GSO_BY_FRAGS;
5044
5045 skb_walk_frags(skb, iter) {
5046 if (seg_len + skb_headlen(iter) > max_len)
5047 return false;
5048 }
5049
5050 return true;
5051}
5052
5053/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005054 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005055 *
5056 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005057 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005058 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005059 * skb_gso_validate_network_len validates if a given skb will fit a
5060 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5061 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005062 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005063bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005064{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005065 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005066}
Daniel Axtens779b7932018-03-01 17:13:37 +11005067EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005068
Daniel Axtens2b16f042018-01-31 14:15:33 +11005069/**
5070 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5071 *
5072 * @skb: GSO skb
5073 * @len: length to validate against
5074 *
5075 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5076 * length once split, including L2, L3 and L4 headers and the payload.
5077 */
5078bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5079{
5080 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5081}
5082EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5083
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005084static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5085{
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005086 int mac_len;
5087
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005088 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5089 kfree_skb(skb);
5090 return NULL;
5091 }
5092
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005093 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005094 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5095 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5096 mac_len - VLAN_HLEN - ETH_TLEN);
5097 }
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005098 skb->mac_header += VLAN_HLEN;
5099 return skb;
5100}
5101
5102struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5103{
5104 struct vlan_hdr *vhdr;
5105 u16 vlan_tci;
5106
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005107 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005108 /* vlan_tci is already set-up so leave this for another time */
5109 return skb;
5110 }
5111
5112 skb = skb_share_check(skb, GFP_ATOMIC);
5113 if (unlikely(!skb))
5114 goto err_free;
5115
5116 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5117 goto err_free;
5118
5119 vhdr = (struct vlan_hdr *)skb->data;
5120 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5121 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5122
5123 skb_pull_rcsum(skb, VLAN_HLEN);
5124 vlan_set_encap_proto(skb, vhdr);
5125
5126 skb = skb_reorder_vlan_header(skb);
5127 if (unlikely(!skb))
5128 goto err_free;
5129
5130 skb_reset_network_header(skb);
5131 skb_reset_transport_header(skb);
5132 skb_reset_mac_len(skb);
5133
5134 return skb;
5135
5136err_free:
5137 kfree_skb(skb);
5138 return NULL;
5139}
5140EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005141
Jiri Pirkoe2195122014-11-19 14:05:01 +01005142int skb_ensure_writable(struct sk_buff *skb, int write_len)
5143{
5144 if (!pskb_may_pull(skb, write_len))
5145 return -ENOMEM;
5146
5147 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5148 return 0;
5149
5150 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5151}
5152EXPORT_SYMBOL(skb_ensure_writable);
5153
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005154/* remove VLAN header from packet and update csum accordingly.
5155 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5156 */
5157int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005158{
5159 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005160 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005161 int err;
5162
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005163 if (WARN_ONCE(offset,
5164 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5165 offset)) {
5166 return -EINVAL;
5167 }
5168
Jiri Pirko93515d52014-11-19 14:05:02 +01005169 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5170 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005171 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005172
5173 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5174
5175 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5176 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5177
5178 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5179 __skb_pull(skb, VLAN_HLEN);
5180
5181 vlan_set_encap_proto(skb, vhdr);
5182 skb->mac_header += VLAN_HLEN;
5183
5184 if (skb_network_offset(skb) < ETH_HLEN)
5185 skb_set_network_header(skb, ETH_HLEN);
5186
5187 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005188
5189 return err;
5190}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005191EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005192
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005193/* Pop a vlan tag either from hwaccel or from payload.
5194 * Expects skb->data at mac header.
5195 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005196int skb_vlan_pop(struct sk_buff *skb)
5197{
5198 u16 vlan_tci;
5199 __be16 vlan_proto;
5200 int err;
5201
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005202 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005203 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005204 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005205 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005206 return 0;
5207
5208 err = __skb_vlan_pop(skb, &vlan_tci);
5209 if (err)
5210 return err;
5211 }
5212 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005213 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005214 return 0;
5215
5216 vlan_proto = skb->protocol;
5217 err = __skb_vlan_pop(skb, &vlan_tci);
5218 if (unlikely(err))
5219 return err;
5220
5221 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5222 return 0;
5223}
5224EXPORT_SYMBOL(skb_vlan_pop);
5225
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005226/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5227 * Expects skb->data at mac header.
5228 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005229int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5230{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005231 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005232 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005233 int err;
5234
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005235 if (WARN_ONCE(offset,
5236 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5237 offset)) {
5238 return -EINVAL;
5239 }
5240
Jiri Pirko93515d52014-11-19 14:05:02 +01005241 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005242 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005243 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005244 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005245
Jiri Pirko93515d52014-11-19 14:05:02 +01005246 skb->protocol = skb->vlan_proto;
5247 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005248
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005249 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005250 }
5251 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5252 return 0;
5253}
5254EXPORT_SYMBOL(skb_vlan_push);
5255
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005256/**
5257 * alloc_skb_with_frags - allocate skb with page frags
5258 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005259 * @header_len: size of linear part
5260 * @data_len: needed length in frags
5261 * @max_page_order: max page order desired.
5262 * @errcode: pointer to error code if any
5263 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005264 *
5265 * This can be used to allocate a paged skb, given a maximal order for frags.
5266 */
5267struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5268 unsigned long data_len,
5269 int max_page_order,
5270 int *errcode,
5271 gfp_t gfp_mask)
5272{
5273 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5274 unsigned long chunk;
5275 struct sk_buff *skb;
5276 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005277 int i;
5278
5279 *errcode = -EMSGSIZE;
5280 /* Note this test could be relaxed, if we succeed to allocate
5281 * high order pages...
5282 */
5283 if (npages > MAX_SKB_FRAGS)
5284 return NULL;
5285
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005286 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005287 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005288 if (!skb)
5289 return NULL;
5290
5291 skb->truesize += npages << PAGE_SHIFT;
5292
5293 for (i = 0; npages > 0; i++) {
5294 int order = max_page_order;
5295
5296 while (order) {
5297 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005298 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005299 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005300 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005301 order);
5302 if (page)
5303 goto fill_page;
5304 /* Do not retry other high order allocations */
5305 order = 1;
5306 max_page_order = 0;
5307 }
5308 order--;
5309 }
5310 page = alloc_page(gfp_mask);
5311 if (!page)
5312 goto failure;
5313fill_page:
5314 chunk = min_t(unsigned long, data_len,
5315 PAGE_SIZE << order);
5316 skb_fill_page_desc(skb, i, page, 0, chunk);
5317 data_len -= chunk;
5318 npages -= 1 << order;
5319 }
5320 return skb;
5321
5322failure:
5323 kfree_skb(skb);
5324 return NULL;
5325}
5326EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005327
5328/* carve out the first off bytes from skb when off < headlen */
5329static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5330 const int headlen, gfp_t gfp_mask)
5331{
5332 int i;
5333 int size = skb_end_offset(skb);
5334 int new_hlen = headlen - off;
5335 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005336
5337 size = SKB_DATA_ALIGN(size);
5338
5339 if (skb_pfmemalloc(skb))
5340 gfp_mask |= __GFP_MEMALLOC;
5341 data = kmalloc_reserve(size +
5342 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5343 gfp_mask, NUMA_NO_NODE, NULL);
5344 if (!data)
5345 return -ENOMEM;
5346
5347 size = SKB_WITH_OVERHEAD(ksize(data));
5348
5349 /* Copy real data, and all frags */
5350 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5351 skb->len -= off;
5352
5353 memcpy((struct skb_shared_info *)(data + size),
5354 skb_shinfo(skb),
5355 offsetof(struct skb_shared_info,
5356 frags[skb_shinfo(skb)->nr_frags]));
5357 if (skb_cloned(skb)) {
5358 /* drop the old head gracefully */
5359 if (skb_orphan_frags(skb, gfp_mask)) {
5360 kfree(data);
5361 return -ENOMEM;
5362 }
5363 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5364 skb_frag_ref(skb, i);
5365 if (skb_has_frag_list(skb))
5366 skb_clone_fraglist(skb);
5367 skb_release_data(skb);
5368 } else {
5369 /* we can reuse existing recount- all we did was
5370 * relocate values
5371 */
5372 skb_free_head(skb);
5373 }
5374
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005375 skb->head = data;
5376 skb->data = data;
5377 skb->head_frag = 0;
5378#ifdef NET_SKBUFF_DATA_USES_OFFSET
5379 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005380#else
5381 skb->end = skb->head + size;
5382#endif
5383 skb_set_tail_pointer(skb, skb_headlen(skb));
5384 skb_headers_offset_update(skb, 0);
5385 skb->cloned = 0;
5386 skb->hdr_len = 0;
5387 skb->nohdr = 0;
5388 atomic_set(&skb_shinfo(skb)->dataref, 1);
5389
5390 return 0;
5391}
5392
5393static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5394
5395/* carve out the first eat bytes from skb's frag_list. May recurse into
5396 * pskb_carve()
5397 */
5398static int pskb_carve_frag_list(struct sk_buff *skb,
5399 struct skb_shared_info *shinfo, int eat,
5400 gfp_t gfp_mask)
5401{
5402 struct sk_buff *list = shinfo->frag_list;
5403 struct sk_buff *clone = NULL;
5404 struct sk_buff *insp = NULL;
5405
5406 do {
5407 if (!list) {
5408 pr_err("Not enough bytes to eat. Want %d\n", eat);
5409 return -EFAULT;
5410 }
5411 if (list->len <= eat) {
5412 /* Eaten as whole. */
5413 eat -= list->len;
5414 list = list->next;
5415 insp = list;
5416 } else {
5417 /* Eaten partially. */
5418 if (skb_shared(list)) {
5419 clone = skb_clone(list, gfp_mask);
5420 if (!clone)
5421 return -ENOMEM;
5422 insp = list->next;
5423 list = clone;
5424 } else {
5425 /* This may be pulled without problems. */
5426 insp = list;
5427 }
5428 if (pskb_carve(list, eat, gfp_mask) < 0) {
5429 kfree_skb(clone);
5430 return -ENOMEM;
5431 }
5432 break;
5433 }
5434 } while (eat);
5435
5436 /* Free pulled out fragments. */
5437 while ((list = shinfo->frag_list) != insp) {
5438 shinfo->frag_list = list->next;
5439 kfree_skb(list);
5440 }
5441 /* And insert new clone at head. */
5442 if (clone) {
5443 clone->next = list;
5444 shinfo->frag_list = clone;
5445 }
5446 return 0;
5447}
5448
5449/* carve off first len bytes from skb. Split line (off) is in the
5450 * non-linear part of skb
5451 */
5452static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5453 int pos, gfp_t gfp_mask)
5454{
5455 int i, k = 0;
5456 int size = skb_end_offset(skb);
5457 u8 *data;
5458 const int nfrags = skb_shinfo(skb)->nr_frags;
5459 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005460
5461 size = SKB_DATA_ALIGN(size);
5462
5463 if (skb_pfmemalloc(skb))
5464 gfp_mask |= __GFP_MEMALLOC;
5465 data = kmalloc_reserve(size +
5466 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5467 gfp_mask, NUMA_NO_NODE, NULL);
5468 if (!data)
5469 return -ENOMEM;
5470
5471 size = SKB_WITH_OVERHEAD(ksize(data));
5472
5473 memcpy((struct skb_shared_info *)(data + size),
5474 skb_shinfo(skb), offsetof(struct skb_shared_info,
5475 frags[skb_shinfo(skb)->nr_frags]));
5476 if (skb_orphan_frags(skb, gfp_mask)) {
5477 kfree(data);
5478 return -ENOMEM;
5479 }
5480 shinfo = (struct skb_shared_info *)(data + size);
5481 for (i = 0; i < nfrags; i++) {
5482 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5483
5484 if (pos + fsize > off) {
5485 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5486
5487 if (pos < off) {
5488 /* Split frag.
5489 * We have two variants in this case:
5490 * 1. Move all the frag to the second
5491 * part, if it is possible. F.e.
5492 * this approach is mandatory for TUX,
5493 * where splitting is expensive.
5494 * 2. Split is accurately. We make this.
5495 */
5496 shinfo->frags[0].page_offset += off - pos;
5497 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5498 }
5499 skb_frag_ref(skb, i);
5500 k++;
5501 }
5502 pos += fsize;
5503 }
5504 shinfo->nr_frags = k;
5505 if (skb_has_frag_list(skb))
5506 skb_clone_fraglist(skb);
5507
5508 if (k == 0) {
5509 /* split line is in frag list */
5510 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5511 }
5512 skb_release_data(skb);
5513
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005514 skb->head = data;
5515 skb->head_frag = 0;
5516 skb->data = data;
5517#ifdef NET_SKBUFF_DATA_USES_OFFSET
5518 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005519#else
5520 skb->end = skb->head + size;
5521#endif
5522 skb_reset_tail_pointer(skb);
5523 skb_headers_offset_update(skb, 0);
5524 skb->cloned = 0;
5525 skb->hdr_len = 0;
5526 skb->nohdr = 0;
5527 skb->len -= off;
5528 skb->data_len = skb->len;
5529 atomic_set(&skb_shinfo(skb)->dataref, 1);
5530 return 0;
5531}
5532
5533/* remove len bytes from the beginning of the skb */
5534static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5535{
5536 int headlen = skb_headlen(skb);
5537
5538 if (len < headlen)
5539 return pskb_carve_inside_header(skb, len, headlen, gfp);
5540 else
5541 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5542}
5543
5544/* Extract to_copy bytes starting at off from skb, and return this in
5545 * a new skb
5546 */
5547struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5548 int to_copy, gfp_t gfp)
5549{
5550 struct sk_buff *clone = skb_clone(skb, gfp);
5551
5552 if (!clone)
5553 return NULL;
5554
5555 if (pskb_carve(clone, off, gfp) < 0 ||
5556 pskb_trim(clone, to_copy)) {
5557 kfree_skb(clone);
5558 return NULL;
5559 }
5560 return clone;
5561}
5562EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005563
5564/**
5565 * skb_condense - try to get rid of fragments/frag_list if possible
5566 * @skb: buffer
5567 *
5568 * Can be used to save memory before skb is added to a busy queue.
5569 * If packet has bytes in frags and enough tail room in skb->head,
5570 * pull all of them, so that we can free the frags right now and adjust
5571 * truesize.
5572 * Notes:
5573 * We do not reallocate skb->head thus can not fail.
5574 * Caller must re-evaluate skb->truesize if needed.
5575 */
5576void skb_condense(struct sk_buff *skb)
5577{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005578 if (skb->data_len) {
5579 if (skb->data_len > skb->end - skb->tail ||
5580 skb_cloned(skb))
5581 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005582
Eric Dumazet3174fed2016-12-09 08:02:05 -08005583 /* Nice, we can free page frag(s) right now */
5584 __pskb_pull_tail(skb, skb->data_len);
5585 }
5586 /* At this point, skb->truesize might be over estimated,
5587 * because skb had a fragment, and fragments do not tell
5588 * their truesize.
5589 * When we pulled its content into skb->head, fragment
5590 * was freed, but __pskb_pull_tail() could not possibly
5591 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005592 */
5593 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5594}
Florian Westphaldf5042f2018-12-18 17:15:16 +01005595
5596#ifdef CONFIG_SKB_EXTENSIONS
5597static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5598{
5599 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5600}
5601
5602static struct skb_ext *skb_ext_alloc(void)
5603{
5604 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5605
5606 if (new) {
5607 memset(new->offset, 0, sizeof(new->offset));
5608 refcount_set(&new->refcnt, 1);
5609 }
5610
5611 return new;
5612}
5613
Florian Westphal41650792018-12-18 17:15:27 +01005614static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5615 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01005616{
5617 struct skb_ext *new;
5618
5619 if (refcount_read(&old->refcnt) == 1)
5620 return old;
5621
5622 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5623 if (!new)
5624 return NULL;
5625
5626 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5627 refcount_set(&new->refcnt, 1);
5628
Florian Westphal41650792018-12-18 17:15:27 +01005629#ifdef CONFIG_XFRM
5630 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5631 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5632 unsigned int i;
5633
5634 for (i = 0; i < sp->len; i++)
5635 xfrm_state_hold(sp->xvec[i]);
5636 }
5637#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005638 __skb_ext_put(old);
5639 return new;
5640}
5641
5642/**
5643 * skb_ext_add - allocate space for given extension, COW if needed
5644 * @skb: buffer
5645 * @id: extension to allocate space for
5646 *
5647 * Allocates enough space for the given extension.
5648 * If the extension is already present, a pointer to that extension
5649 * is returned.
5650 *
5651 * If the skb was cloned, COW applies and the returned memory can be
5652 * modified without changing the extension space of clones buffers.
5653 *
5654 * Returns pointer to the extension or NULL on allocation failure.
5655 */
5656void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5657{
5658 struct skb_ext *new, *old = NULL;
5659 unsigned int newlen, newoff;
5660
5661 if (skb->active_extensions) {
5662 old = skb->extensions;
5663
Florian Westphal41650792018-12-18 17:15:27 +01005664 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01005665 if (!new)
5666 return NULL;
5667
Paolo Abeni682ec852018-12-21 19:03:15 +01005668 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01005669 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005670
Paolo Abenie94e50b2018-12-21 19:03:13 +01005671 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005672 } else {
5673 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5674
5675 new = skb_ext_alloc();
5676 if (!new)
5677 return NULL;
5678 }
5679
5680 newlen = newoff + skb_ext_type_len[id];
5681 new->chunks = newlen;
5682 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005683set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01005684 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005685 skb->active_extensions |= 1 << id;
5686 return skb_ext_get_ptr(new, id);
5687}
5688EXPORT_SYMBOL(skb_ext_add);
5689
Florian Westphal41650792018-12-18 17:15:27 +01005690#ifdef CONFIG_XFRM
5691static void skb_ext_put_sp(struct sec_path *sp)
5692{
5693 unsigned int i;
5694
5695 for (i = 0; i < sp->len; i++)
5696 xfrm_state_put(sp->xvec[i]);
5697}
5698#endif
5699
Florian Westphaldf5042f2018-12-18 17:15:16 +01005700void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5701{
5702 struct skb_ext *ext = skb->extensions;
5703
5704 skb->active_extensions &= ~(1 << id);
5705 if (skb->active_extensions == 0) {
5706 skb->extensions = NULL;
5707 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01005708#ifdef CONFIG_XFRM
5709 } else if (id == SKB_EXT_SEC_PATH &&
5710 refcount_read(&ext->refcnt) == 1) {
5711 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5712
5713 skb_ext_put_sp(sp);
5714 sp->len = 0;
5715#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005716 }
5717}
5718EXPORT_SYMBOL(__skb_ext_del);
5719
5720void __skb_ext_put(struct skb_ext *ext)
5721{
5722 /* If this is last clone, nothing can increment
5723 * it after check passes. Avoids one atomic op.
5724 */
5725 if (refcount_read(&ext->refcnt) == 1)
5726 goto free_now;
5727
5728 if (!refcount_dec_and_test(&ext->refcnt))
5729 return;
5730free_now:
Florian Westphal41650792018-12-18 17:15:27 +01005731#ifdef CONFIG_XFRM
5732 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5733 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5734#endif
5735
Florian Westphaldf5042f2018-12-18 17:15:16 +01005736 kmem_cache_free(skbuff_ext_cache, ext);
5737}
5738EXPORT_SYMBOL(__skb_ext_put);
5739#endif /* CONFIG_SKB_EXTENSIONS */