blob: b50a5e3ac4e478c9828d92424ab4548cc5d79ba7 [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>
Matteo Croce2544af02019-05-29 17:13:48 +020079#include <linux/indirect_call_wrapper.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040080
Bart Van Assche7b7ed882019-03-25 09:17:23 -070081#include "datagram.h"
82
Alexey Dobriyan08009a72018-02-24 21:20:33 +030083struct kmem_cache *skbuff_head_cache __ro_after_init;
84static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
Florian Westphaldf5042f2018-12-18 17:15:16 +010085#ifdef CONFIG_SKB_EXTENSIONS
86static struct kmem_cache *skbuff_ext_cache __ro_after_init;
87#endif
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010088int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
89EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/**
Jean Sacrenf05de732013-02-11 13:30:38 +000092 * skb_panic - private function for out-of-line support
93 * @skb: buffer
94 * @sz: size
95 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000096 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Jean Sacrenf05de732013-02-11 13:30:38 +000098 * Out-of-line support for skb_put() and skb_push().
99 * Called via the wrapper skb_over_panic() or skb_under_panic().
100 * Keep out of line to prevent kernel bloat.
101 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Jean Sacrenf05de732013-02-11 13:30:38 +0000103static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +0000104 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Joe Perchese005d192012-05-16 19:58:40 +0000106 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 +0000107 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000108 (unsigned long)skb->tail, (unsigned long)skb->end,
109 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 BUG();
111}
112
Jean Sacrenf05de732013-02-11 13:30:38 +0000113static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Jean Sacrenf05de732013-02-11 13:30:38 +0000115 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Jean Sacrenf05de732013-02-11 13:30:38 +0000118static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
119{
120 skb_panic(skb, sz, addr, __func__);
121}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700122
123/*
124 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
125 * the caller if emergency pfmemalloc reserves are being used. If it is and
126 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
127 * may be used. Otherwise, the packet data may be discarded until enough
128 * memory is free
129 */
130#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
131 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000132
133static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
134 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700135{
136 void *obj;
137 bool ret_pfmemalloc = false;
138
139 /*
140 * Try a regular allocation, when that fails and we're not entitled
141 * to the reserves, fail.
142 */
143 obj = kmalloc_node_track_caller(size,
144 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
145 node);
146 if (obj || !(gfp_pfmemalloc_allowed(flags)))
147 goto out;
148
149 /* Try again but now we are using pfmemalloc reserves */
150 ret_pfmemalloc = true;
151 obj = kmalloc_node_track_caller(size, flags, node);
152
153out:
154 if (pfmemalloc)
155 *pfmemalloc = ret_pfmemalloc;
156
157 return obj;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Allocate a new skbuff. We do this ourselves so we can fill in a few
161 * 'private' fields and also do memory statistics to find all the
162 * [BEEP] leaks.
163 *
164 */
165
166/**
David S. Millerd179cd12005-08-17 14:57:30 -0700167 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @size: size to allocate
169 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700170 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
171 * instead of head cache and allocate a cloned (child) skb.
172 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
173 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800174 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000177 * tail room of at least size bytes. The object has a reference count
178 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
180 * Buffers may only be allocated from interrupts using a @gfp_mask of
181 * %GFP_ATOMIC.
182 */
Al Virodd0fc662005-10-07 07:46:04 +0100183struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700184 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Christoph Lametere18b8902006-12-06 20:33:20 -0800186 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800187 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct sk_buff *skb;
189 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700190 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Mel Gormanc93bdd02012-07-31 16:44:19 -0700192 cache = (flags & SKB_ALLOC_FCLONE)
193 ? skbuff_fclone_cache : skbuff_head_cache;
194
195 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
196 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800199 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!skb)
201 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700202 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000204 /* We do our best to align skb_shared_info on a separate cache
205 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
206 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
207 * Both skb->head and skb_shared_info are cache line aligned.
208 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000209 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000210 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700211 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!data)
213 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000214 /* kmalloc(size) might give us more room than requested.
215 * Put skb_shared_info exactly at the end of allocated zone,
216 * to allow max possible filling before reallocation.
217 */
218 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700219 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300221 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700222 * Only clear those fields we need to clear, not those that we will
223 * actually initialise below. Hence, don't put any more fields after
224 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300225 */
226 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 /* Account for allocated memory : skb + skb->head */
228 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700229 skb->pfmemalloc = pfmemalloc;
Reshetova, Elena63354792017-06-30 13:07:58 +0300230 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 skb->head = data;
232 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700233 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700234 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800235 skb->mac_header = (typeof(skb->mac_header))~0U;
236 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000237
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800238 /* make sure we initialize shinfo sequentially */
239 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700240 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800241 atomic_set(&shinfo->dataref, 1);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800242
Mel Gormanc93bdd02012-07-31 16:44:19 -0700243 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700244 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700246 fclones = container_of(skb, struct sk_buff_fclones, skb1);
247
David S. Millerd179cd12005-08-17 14:57:30 -0700248 skb->fclone = SKB_FCLONE_ORIG;
Reshetova, Elena26385952017-06-30 13:07:59 +0300249 refcount_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700250
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800251 fclones->skb2.fclone = SKB_FCLONE_CLONE;
David S. Millerd179cd12005-08-17 14:57:30 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253out:
254 return skb;
255nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800256 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 skb = NULL;
258 goto out;
259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800260EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200262/* Caller must provide SKB that is memset cleared */
263static struct sk_buff *__build_skb_around(struct sk_buff *skb,
264 void *data, unsigned int frag_size)
265{
266 struct skb_shared_info *shinfo;
267 unsigned int size = frag_size ? : ksize(data);
268
269 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
270
271 /* Assumes caller memset cleared SKB */
272 skb->truesize = SKB_TRUESIZE(size);
273 refcount_set(&skb->users, 1);
274 skb->head = data;
275 skb->data = data;
276 skb_reset_tail_pointer(skb);
277 skb->end = skb->tail + size;
278 skb->mac_header = (typeof(skb->mac_header))~0U;
279 skb->transport_header = (typeof(skb->transport_header))~0U;
280
281 /* make sure we initialize shinfo sequentially */
282 shinfo = skb_shinfo(skb);
283 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
284 atomic_set(&shinfo->dataref, 1);
285
286 return skb;
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700290 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000291 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700292 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 *
294 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100295 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700296 * @frag_size is 0, otherwise data should come from the page allocator
297 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000298 * The return is the new skb buffer.
299 * On a failure the return is %NULL, and @data is not freed.
300 * Notes :
301 * Before IO, driver allocates only data buffer where NIC put incoming frame
302 * Driver should add room at head (NET_SKB_PAD) and
303 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
304 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
305 * before giving packet to stack.
306 * RX rings only contains data buffers, not full skbs.
307 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700308struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309{
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000310 struct sk_buff *skb;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000311
312 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200313 if (unlikely(!skb))
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314 return NULL;
315
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000316 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000317
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200318 return __build_skb_around(skb, data, frag_size);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000319}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700320
321/* build_skb() is wrapper over __build_skb(), that specifically
322 * takes care of skb->head and skb->pfmemalloc
323 * This means that if @frag_size is not zero, then @data must be backed
324 * by a page fragment, not kmalloc() or vmalloc()
325 */
326struct sk_buff *build_skb(void *data, unsigned int frag_size)
327{
328 struct sk_buff *skb = __build_skb(data, frag_size);
329
330 if (skb && frag_size) {
331 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700332 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700333 skb->pfmemalloc = 1;
334 }
335 return skb;
336}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000337EXPORT_SYMBOL(build_skb);
338
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200339/**
340 * build_skb_around - build a network buffer around provided skb
341 * @skb: sk_buff provide by caller, must be memset cleared
342 * @data: data buffer provided by caller
343 * @frag_size: size of data, or 0 if head was kmalloced
344 */
345struct sk_buff *build_skb_around(struct sk_buff *skb,
346 void *data, unsigned int frag_size)
347{
348 if (unlikely(!skb))
349 return NULL;
350
351 skb = __build_skb_around(skb, data, frag_size);
352
353 if (skb && frag_size) {
354 skb->head_frag = 1;
355 if (page_is_pfmemalloc(virt_to_head_page(data)))
356 skb->pfmemalloc = 1;
357 }
358 return skb;
359}
360EXPORT_SYMBOL(build_skb_around);
361
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100362#define NAPI_SKB_CACHE_SIZE 64
363
364struct napi_alloc_cache {
365 struct page_frag_cache page;
Alexey Dobriyane0d79242016-11-19 03:47:56 +0300366 unsigned int skb_count;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100367 void *skb_cache[NAPI_SKB_CACHE_SIZE];
368};
369
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700370static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100371static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800372
373static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
374{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700375 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800376 unsigned long flags;
377 void *data;
378
379 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700380 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800381 data = page_frag_alloc(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000382 local_irq_restore(flags);
383 return data;
384}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700385
386/**
387 * netdev_alloc_frag - allocate a page fragment
388 * @fragsz: fragment size
389 *
390 * Allocates a frag from a page for receive buffer.
391 * Uses GFP_ATOMIC allocations.
392 */
393void *netdev_alloc_frag(unsigned int fragsz)
394{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800395 fragsz = SKB_DATA_ALIGN(fragsz);
396
Mel Gorman453f85d2017-11-15 17:38:03 -0800397 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700398}
Eric Dumazet6f532612012-05-18 05:12:12 +0000399EXPORT_SYMBOL(netdev_alloc_frag);
400
Alexander Duyckffde7322014-12-09 19:40:42 -0800401static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
402{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100403 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700404
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800405 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800406}
407
408void *napi_alloc_frag(unsigned int fragsz)
409{
Alexander Duyck3bed3cc2019-02-15 14:44:18 -0800410 fragsz = SKB_DATA_ALIGN(fragsz);
411
Mel Gorman453f85d2017-11-15 17:38:03 -0800412 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
Alexander Duyckffde7322014-12-09 19:40:42 -0800413}
414EXPORT_SYMBOL(napi_alloc_frag);
415
Eric Dumazet6f532612012-05-18 05:12:12 +0000416/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800417 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
418 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900419 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800420 * @gfp_mask: get_free_pages mask, passed to alloc_skb
421 *
422 * Allocate a new &sk_buff and assign it a usage count of one. The
423 * buffer has NET_SKB_PAD headroom built in. Users should allocate
424 * the headroom they think they need without accounting for the
425 * built in space. The built in space is used for optimisations.
426 *
427 * %NULL is returned if there is no free memory.
428 */
Alexander Duyck94519802015-05-06 21:11:40 -0700429struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
430 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800431{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700432 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700433 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800434 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700435 bool pfmemalloc;
436 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800437
Alexander Duyck94519802015-05-06 21:11:40 -0700438 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800439
Alexander Duyck94519802015-05-06 21:11:40 -0700440 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800441 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700442 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
443 if (!skb)
444 goto skb_fail;
445 goto skb_success;
446 }
Alexander Duyck94519802015-05-06 21:11:40 -0700447
448 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
449 len = SKB_DATA_ALIGN(len);
450
451 if (sk_memalloc_socks())
452 gfp_mask |= __GFP_MEMALLOC;
453
454 local_irq_save(flags);
455
456 nc = this_cpu_ptr(&netdev_alloc_cache);
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800457 data = page_frag_alloc(nc, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700458 pfmemalloc = nc->pfmemalloc;
459
460 local_irq_restore(flags);
461
462 if (unlikely(!data))
463 return NULL;
464
465 skb = __build_skb(data, len);
466 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700467 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700468 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700469 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800470
Alexander Duyck94519802015-05-06 21:11:40 -0700471 /* use OR instead of assignment to avoid clearing of bits in mask */
472 if (pfmemalloc)
473 skb->pfmemalloc = 1;
474 skb->head_frag = 1;
475
Alexander Duycka080e7b2015-05-13 13:34:13 -0700476skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700477 skb_reserve(skb, NET_SKB_PAD);
478 skb->dev = dev;
479
Alexander Duycka080e7b2015-05-13 13:34:13 -0700480skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700481 return skb;
482}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800483EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alexander Duyckfd11a832014-12-09 19:40:49 -0800485/**
486 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
487 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900488 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800489 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
490 *
491 * Allocate a new sk_buff for use in NAPI receive. This buffer will
492 * attempt to allocate the head from a special reserved region used
493 * only for NAPI Rx allocation. By doing this we can save several
494 * CPU cycles by avoiding having to disable and re-enable IRQs.
495 *
496 * %NULL is returned if there is no free memory.
497 */
Alexander Duyck94519802015-05-06 21:11:40 -0700498struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
499 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800500{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100501 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800502 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700503 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800504
Alexander Duyck94519802015-05-06 21:11:40 -0700505 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800506
Alexander Duyck94519802015-05-06 21:11:40 -0700507 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800508 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700509 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
510 if (!skb)
511 goto skb_fail;
512 goto skb_success;
513 }
Alexander Duyck94519802015-05-06 21:11:40 -0700514
515 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
516 len = SKB_DATA_ALIGN(len);
517
518 if (sk_memalloc_socks())
519 gfp_mask |= __GFP_MEMALLOC;
520
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800521 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700522 if (unlikely(!data))
523 return NULL;
524
525 skb = __build_skb(data, len);
526 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700527 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700528 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800529 }
530
Alexander Duyck94519802015-05-06 21:11:40 -0700531 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100532 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700533 skb->pfmemalloc = 1;
534 skb->head_frag = 1;
535
Alexander Duycka080e7b2015-05-13 13:34:13 -0700536skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700537 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
538 skb->dev = napi->dev;
539
Alexander Duycka080e7b2015-05-13 13:34:13 -0700540skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800541 return skb;
542}
543EXPORT_SYMBOL(__napi_alloc_skb);
544
Peter Zijlstra654bed12008-10-07 14:22:33 -0700545void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000546 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700547{
548 skb_fill_page_desc(skb, i, page, off, size);
549 skb->len += size;
550 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000551 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700552}
553EXPORT_SYMBOL(skb_add_rx_frag);
554
Jason Wangf8e617e2013-11-01 14:07:47 +0800555void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
556 unsigned int truesize)
557{
558 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
559
560 skb_frag_size_add(frag, size);
561 skb->len += size;
562 skb->data_len += size;
563 skb->truesize += truesize;
564}
565EXPORT_SYMBOL(skb_coalesce_rx_frag);
566
Herbert Xu27b437c2006-07-13 19:26:39 -0700567static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700569 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700570 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Herbert Xu27b437c2006-07-13 19:26:39 -0700573static inline void skb_drop_fraglist(struct sk_buff *skb)
574{
575 skb_drop_list(&skb_shinfo(skb)->frag_list);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static void skb_clone_fraglist(struct sk_buff *skb)
579{
580 struct sk_buff *list;
581
David S. Millerfbb398a2009-06-09 00:18:59 -0700582 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 skb_get(list);
584}
585
Eric Dumazetd3836f22012-04-27 00:33:38 +0000586static void skb_free_head(struct sk_buff *skb)
587{
Alexander Duyck181edb22015-05-06 21:12:03 -0700588 unsigned char *head = skb->head;
589
Eric Dumazetd3836f22012-04-27 00:33:38 +0000590 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700591 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000592 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700593 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000594}
595
Adrian Bunk5bba1712006-06-29 13:02:35 -0700596static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Eric Dumazetff04a772014-09-23 18:39:30 -0700598 struct skb_shared_info *shinfo = skb_shinfo(skb);
599 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Eric Dumazetff04a772014-09-23 18:39:30 -0700601 if (skb->cloned &&
602 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
603 &shinfo->dataref))
604 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000605
Eric Dumazetff04a772014-09-23 18:39:30 -0700606 for (i = 0; i < shinfo->nr_frags; i++)
607 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000608
Eric Dumazetff04a772014-09-23 18:39:30 -0700609 if (shinfo->frag_list)
610 kfree_skb_list(shinfo->frag_list);
611
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400612 skb_zcopy_clear(skb, true);
Eric Dumazetff04a772014-09-23 18:39:30 -0700613 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * Free an skbuff by memory without cleaning the state.
618 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800619static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700621 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700622
David S. Millerd179cd12005-08-17 14:57:30 -0700623 switch (skb->fclone) {
624 case SKB_FCLONE_UNAVAILABLE:
625 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800626 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700627
628 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700629 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800630
631 /* We usually free the clone (TX completion) before original skb
632 * This test would have no chance to be true for the clone,
633 * while here, branch prediction will be good.
634 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300635 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800636 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700637 break;
638
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800639 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700640 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700641 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700642 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300643 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800644 return;
645fastpath:
646 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Paolo Abeni0a463c72017-06-12 11:23:42 +0200649void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Eric Dumazetadf30902009-06-02 05:19:30 +0000651 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700652 if (skb->destructor) {
653 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 skb->destructor(skb);
655 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000656#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100657 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100658#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100659 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700660}
661
662/* Free everything but the sk_buff shell. */
663static void skb_release_all(struct sk_buff *skb)
664{
665 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200666 if (likely(skb->head))
667 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Herbert Xu2d4baff2007-11-26 23:11:19 +0800670/**
671 * __kfree_skb - private function
672 * @skb: buffer
673 *
674 * Free an sk_buff. Release anything attached to the buffer.
675 * Clean the state. This is an internal helper function. Users should
676 * always call kfree_skb
677 */
678
679void __kfree_skb(struct sk_buff *skb)
680{
681 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 kfree_skbmem(skb);
683}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800684EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800687 * kfree_skb - free an sk_buff
688 * @skb: buffer to free
689 *
690 * Drop a reference to the buffer and free it if the usage count has
691 * hit zero.
692 */
693void kfree_skb(struct sk_buff *skb)
694{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200695 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800696 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200697
Neil Hormanead2ceb2009-03-11 09:49:55 +0000698 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800699 __kfree_skb(skb);
700}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800701EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800702
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700703void kfree_skb_list(struct sk_buff *segs)
704{
705 while (segs) {
706 struct sk_buff *next = segs->next;
707
708 kfree_skb(segs);
709 segs = next;
710 }
711}
712EXPORT_SYMBOL(kfree_skb_list);
713
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700714/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000715 * skb_tx_error - report an sk_buff xmit error
716 * @skb: buffer that triggered an error
717 *
718 * Report xmit error if a device callback is tracking this skb.
719 * skb must be freed afterwards.
720 */
721void skb_tx_error(struct sk_buff *skb)
722{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400723 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000724}
725EXPORT_SYMBOL(skb_tx_error);
726
727/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000728 * consume_skb - free an skbuff
729 * @skb: buffer to free
730 *
731 * Drop a ref to the buffer and free it if the usage count has hit zero
732 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
733 * is being dropped after a failure and notes that
734 */
735void consume_skb(struct sk_buff *skb)
736{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200737 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000738 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200739
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900740 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000741 __kfree_skb(skb);
742}
743EXPORT_SYMBOL(consume_skb);
744
Paolo Abeni0a463c72017-06-12 11:23:42 +0200745/**
746 * consume_stateless_skb - free an skbuff, assuming it is stateless
747 * @skb: buffer to free
748 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200749 * Alike consume_skb(), but this variant assumes that this is the last
750 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200751 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200752void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200753{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200754 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200755 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200756 kfree_skbmem(skb);
757}
758
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100759void __kfree_skb_flush(void)
760{
761 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
762
763 /* flush skb_cache if containing objects */
764 if (nc->skb_count) {
765 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
766 nc->skb_cache);
767 nc->skb_count = 0;
768 }
769}
770
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100771static inline void _kfree_skb_defer(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100772{
773 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
774
775 /* drop skb->head and call any destructors for packet */
776 skb_release_all(skb);
777
778 /* record skb to CPU local list */
779 nc->skb_cache[nc->skb_count++] = skb;
780
781#ifdef CONFIG_SLUB
782 /* SLUB writes into objects when freeing */
783 prefetchw(skb);
784#endif
785
786 /* flush skb_cache if it is filled */
787 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
788 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
789 nc->skb_cache);
790 nc->skb_count = 0;
791 }
792}
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100793void __kfree_skb_defer(struct sk_buff *skb)
794{
795 _kfree_skb_defer(skb);
796}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100797
798void napi_consume_skb(struct sk_buff *skb, int budget)
799{
800 if (unlikely(!skb))
801 return;
802
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100803 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100804 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100805 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100806 return;
807 }
808
Paolo Abeni76088942017-06-14 11:48:48 +0200809 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100810 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200811
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100812 /* if reaching here SKB is ready to free */
813 trace_consume_skb(skb);
814
815 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700816 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100817 __kfree_skb(skb);
818 return;
819 }
820
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100821 _kfree_skb_defer(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100822}
823EXPORT_SYMBOL(napi_consume_skb);
824
Eric Dumazetb1937222014-09-28 22:18:47 -0700825/* Make sure a field is enclosed inside headers_start/headers_end section */
826#define CHECK_SKB_FIELD(field) \
827 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
828 offsetof(struct sk_buff, headers_start)); \
829 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
830 offsetof(struct sk_buff, headers_end)); \
831
Herbert Xudec18812007-10-14 00:37:30 -0700832static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
833{
834 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700835 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700836 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700837 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000838 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100839 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -0700840 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700841
Eric Dumazetb1937222014-09-28 22:18:47 -0700842 /* Note : this field could be in headers_start/headers_end section
843 * It is not yet because we do not want to have a 16 bit hole
844 */
845 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300846
Eric Dumazetb1937222014-09-28 22:18:47 -0700847 memcpy(&new->headers_start, &old->headers_start,
848 offsetof(struct sk_buff, headers_end) -
849 offsetof(struct sk_buff, headers_start));
850 CHECK_SKB_FIELD(protocol);
851 CHECK_SKB_FIELD(csum);
852 CHECK_SKB_FIELD(hash);
853 CHECK_SKB_FIELD(priority);
854 CHECK_SKB_FIELD(skb_iif);
855 CHECK_SKB_FIELD(vlan_proto);
856 CHECK_SKB_FIELD(vlan_tci);
857 CHECK_SKB_FIELD(transport_header);
858 CHECK_SKB_FIELD(network_header);
859 CHECK_SKB_FIELD(mac_header);
860 CHECK_SKB_FIELD(inner_protocol);
861 CHECK_SKB_FIELD(inner_transport_header);
862 CHECK_SKB_FIELD(inner_network_header);
863 CHECK_SKB_FIELD(inner_mac_header);
864 CHECK_SKB_FIELD(mark);
865#ifdef CONFIG_NETWORK_SECMARK
866 CHECK_SKB_FIELD(secmark);
867#endif
Cong Wange0d10952013-08-01 11:10:25 +0800868#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700869 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300870#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800871#ifdef CONFIG_XPS
872 CHECK_SKB_FIELD(sender_cpu);
873#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700874#ifdef CONFIG_NET_SCHED
875 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -0700876#endif
877
Herbert Xudec18812007-10-14 00:37:30 -0700878}
879
Herbert Xu82c49a32009-05-22 22:11:37 +0000880/*
881 * You should not add any new code to this function. Add it to
882 * __copy_skb_header above instead.
883 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700884static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#define C(x) n->x = skb->x
887
888 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700890 __copy_skb_header(n, skb);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 C(len);
893 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700894 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700895 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800896 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -0700898 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +0200899 C(pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 C(tail);
902 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800903 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000904 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800905 C(data);
906 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +0300907 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 atomic_inc(&(skb_shinfo(skb)->dataref));
910 skb->cloned = 1;
911
912 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700913#undef C
914}
915
916/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -0700917 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
918 * @first: first sk_buff of the msg
919 */
920struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
921{
922 struct sk_buff *n;
923
924 n = alloc_skb(0, GFP_ATOMIC);
925 if (!n)
926 return NULL;
927
928 n->len = first->len;
929 n->data_len = first->len;
930 n->truesize = first->truesize;
931
932 skb_shinfo(n)->frag_list = first;
933
934 __copy_skb_header(n, first);
935 n->destructor = NULL;
936
937 return n;
938}
939EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
940
941/**
Herbert Xue0053ec2007-10-14 00:37:52 -0700942 * skb_morph - morph one skb into another
943 * @dst: the skb to receive the contents
944 * @src: the skb to supply the contents
945 *
946 * This is identical to skb_clone except that the target skb is
947 * supplied by the user.
948 *
949 * The target skb is returned upon exit.
950 */
951struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
952{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800953 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700954 return __skb_clone(dst, src);
955}
956EXPORT_SYMBOL_GPL(skb_morph);
957
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800958int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400959{
960 unsigned long max_pg, num_pg, new_pg, old_pg;
961 struct user_struct *user;
962
963 if (capable(CAP_IPC_LOCK) || !size)
964 return 0;
965
966 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
967 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
968 user = mmp->user ? : current_user();
969
970 do {
971 old_pg = atomic_long_read(&user->locked_vm);
972 new_pg = old_pg + num_pg;
973 if (new_pg > max_pg)
974 return -ENOBUFS;
975 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
976 old_pg);
977
978 if (!mmp->user) {
979 mmp->user = get_uid(user);
980 mmp->num_pg = num_pg;
981 } else {
982 mmp->num_pg += num_pg;
983 }
984
985 return 0;
986}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800987EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400988
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800989void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400990{
991 if (mmp->user) {
992 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
993 free_uid(mmp->user);
994 }
995}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -0800996EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -0400997
Willem de Bruijn52267792017-08-03 16:29:39 -0400998struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
999{
1000 struct ubuf_info *uarg;
1001 struct sk_buff *skb;
1002
1003 WARN_ON_ONCE(!in_task());
1004
1005 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1006 if (!skb)
1007 return NULL;
1008
1009 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1010 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001011 uarg->mmp.user = NULL;
1012
1013 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1014 kfree_skb(skb);
1015 return NULL;
1016 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001017
1018 uarg->callback = sock_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001019 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1020 uarg->len = 1;
1021 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001022 uarg->zerocopy = 1;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001023 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001024 sock_hold(sk);
1025
1026 return uarg;
1027}
1028EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
1029
1030static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1031{
1032 return container_of((void *)uarg, struct sk_buff, cb);
1033}
1034
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001035struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
1036 struct ubuf_info *uarg)
1037{
1038 if (uarg) {
1039 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1040 u32 bytelen, next;
1041
1042 /* realloc only when socket is locked (TCP, UDP cork),
1043 * so uarg->len and sk_zckey access is serialized
1044 */
1045 if (!sock_owned_by_user(sk)) {
1046 WARN_ON_ONCE(1);
1047 return NULL;
1048 }
1049
1050 bytelen = uarg->bytelen + size;
1051 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1052 /* TCP can create new skb to attach new uarg */
1053 if (sk->sk_type == SOCK_STREAM)
1054 goto new_alloc;
1055 return NULL;
1056 }
1057
1058 next = (u32)atomic_read(&sk->sk_zckey);
1059 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001060 if (mm_account_pinned_pages(&uarg->mmp, size))
1061 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001062 uarg->len++;
1063 uarg->bytelen = bytelen;
1064 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001065
1066 /* no extra ref when appending to datagram (MSG_MORE) */
1067 if (sk->sk_type == SOCK_STREAM)
1068 sock_zerocopy_get(uarg);
1069
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001070 return uarg;
1071 }
1072 }
1073
1074new_alloc:
1075 return sock_zerocopy_alloc(sk, size);
1076}
1077EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
1078
1079static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1080{
1081 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1082 u32 old_lo, old_hi;
1083 u64 sum_len;
1084
1085 old_lo = serr->ee.ee_info;
1086 old_hi = serr->ee.ee_data;
1087 sum_len = old_hi - old_lo + 1ULL + len;
1088
1089 if (sum_len >= (1ULL << 32))
1090 return false;
1091
1092 if (lo != old_hi + 1)
1093 return false;
1094
1095 serr->ee.ee_data += len;
1096 return true;
1097}
1098
Willem de Bruijn52267792017-08-03 16:29:39 -04001099void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
1100{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001101 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001102 struct sock_exterr_skb *serr;
1103 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001104 struct sk_buff_head *q;
1105 unsigned long flags;
1106 u32 lo, hi;
1107 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001108
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001109 mm_unaccount_pinned_pages(&uarg->mmp);
1110
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001111 /* if !len, there was only 1 call, and it was aborted
1112 * so do not queue a completion notification
1113 */
1114 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001115 goto release;
1116
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001117 len = uarg->len;
1118 lo = uarg->id;
1119 hi = uarg->id + len - 1;
1120
Willem de Bruijn52267792017-08-03 16:29:39 -04001121 serr = SKB_EXT_ERR(skb);
1122 memset(serr, 0, sizeof(*serr));
1123 serr->ee.ee_errno = 0;
1124 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001125 serr->ee.ee_data = hi;
1126 serr->ee.ee_info = lo;
Willem de Bruijn52267792017-08-03 16:29:39 -04001127 if (!success)
1128 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1129
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001130 q = &sk->sk_error_queue;
1131 spin_lock_irqsave(&q->lock, flags);
1132 tail = skb_peek_tail(q);
1133 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1134 !skb_zerocopy_notify_extend(tail, lo, len)) {
1135 __skb_queue_tail(q, skb);
1136 skb = NULL;
1137 }
1138 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001139
1140 sk->sk_error_report(sk);
1141
1142release:
1143 consume_skb(skb);
1144 sock_put(sk);
1145}
1146EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
1147
1148void sock_zerocopy_put(struct ubuf_info *uarg)
1149{
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001150 if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
Willem de Bruijn52267792017-08-03 16:29:39 -04001151 if (uarg->callback)
1152 uarg->callback(uarg, uarg->zerocopy);
1153 else
1154 consume_skb(skb_from_uarg(uarg));
1155 }
1156}
1157EXPORT_SYMBOL_GPL(sock_zerocopy_put);
1158
Willem de Bruijn52900d22018-11-30 15:32:40 -05001159void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001160{
1161 if (uarg) {
1162 struct sock *sk = skb_from_uarg(uarg)->sk;
1163
1164 atomic_dec(&sk->sk_zckey);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001165 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001166
Willem de Bruijn52900d22018-11-30 15:32:40 -05001167 if (have_uref)
1168 sock_zerocopy_put(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001169 }
1170}
1171EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
1172
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001173int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1174{
1175 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1176}
1177EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1178
Willem de Bruijn52267792017-08-03 16:29:39 -04001179int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1180 struct msghdr *msg, int len,
1181 struct ubuf_info *uarg)
1182{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001183 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001184 struct iov_iter orig_iter = msg->msg_iter;
1185 int err, orig_len = skb->len;
1186
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001187 /* An skb can only point to one uarg. This edge case happens when
1188 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1189 */
1190 if (orig_uarg && uarg != orig_uarg)
1191 return -EEXIST;
1192
Willem de Bruijn52267792017-08-03 16:29:39 -04001193 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1194 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001195 struct sock *save_sk = skb->sk;
1196
Willem de Bruijn52267792017-08-03 16:29:39 -04001197 /* Streams do not free skb on error. Reset to prev state. */
1198 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001199 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001200 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001201 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001202 return err;
1203 }
1204
Willem de Bruijn52900d22018-11-30 15:32:40 -05001205 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001206 return skb->len - orig_len;
1207}
1208EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1209
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001210static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001211 gfp_t gfp_mask)
1212{
1213 if (skb_zcopy(orig)) {
1214 if (skb_zcopy(nskb)) {
1215 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1216 if (!gfp_mask) {
1217 WARN_ON_ONCE(1);
1218 return -ENOMEM;
1219 }
1220 if (skb_uarg(nskb) == skb_uarg(orig))
1221 return 0;
1222 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1223 return -EIO;
1224 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001225 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001226 }
1227 return 0;
1228}
1229
Ben Hutchings2c530402012-07-10 10:55:09 +00001230/**
1231 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001232 * @skb: the skb to modify
1233 * @gfp_mask: allocation priority
1234 *
1235 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1236 * It will copy all frags into kernel and drop the reference
1237 * to userspace pages.
1238 *
1239 * If this function is called from an interrupt gfp_mask() must be
1240 * %GFP_ATOMIC.
1241 *
1242 * Returns 0 on success or a negative error code on failure
1243 * to allocate kernel memory to copy to.
1244 */
1245int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001246{
Shirley Maa6686f22011-07-06 12:22:12 +00001247 int num_frags = skb_shinfo(skb)->nr_frags;
1248 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001249 int i, new_frags;
1250 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001251
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001252 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1253 return -EINVAL;
1254
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001255 if (!num_frags)
1256 goto release;
1257
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001258 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1259 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001260 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001261 if (!page) {
1262 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001263 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001264 put_page(head);
1265 head = next;
1266 }
1267 return -ENOMEM;
1268 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001269 set_page_private(page, (unsigned long)head);
1270 head = page;
1271 }
1272
1273 page = head;
1274 d_off = 0;
1275 for (i = 0; i < num_frags; i++) {
1276 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1277 u32 p_off, p_len, copied;
1278 struct page *p;
1279 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001280
1281 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
1282 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001283 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001284 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001285
1286 while (done < p_len) {
1287 if (d_off == PAGE_SIZE) {
1288 d_off = 0;
1289 page = (struct page *)page_private(page);
1290 }
1291 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1292 memcpy(page_address(page) + d_off,
1293 vaddr + p_off + done, copy);
1294 done += copy;
1295 d_off += copy;
1296 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001297 kunmap_atomic(vaddr);
1298 }
Shirley Maa6686f22011-07-06 12:22:12 +00001299 }
1300
1301 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001302 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001303 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001304
Shirley Maa6686f22011-07-06 12:22:12 +00001305 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001306 for (i = 0; i < new_frags - 1; i++) {
1307 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001308 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001309 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001310 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1311 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001312
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001313release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001314 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001315 return 0;
1316}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001317EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001318
Herbert Xue0053ec2007-10-14 00:37:52 -07001319/**
1320 * skb_clone - duplicate an sk_buff
1321 * @skb: buffer to clone
1322 * @gfp_mask: allocation priority
1323 *
1324 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1325 * copies share the same packet data but not structure. The new
1326 * buffer has a reference count of 1. If the allocation fails the
1327 * function returns %NULL otherwise the new buffer is returned.
1328 *
1329 * If this function is called from an interrupt gfp_mask() must be
1330 * %GFP_ATOMIC.
1331 */
1332
1333struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1334{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001335 struct sk_buff_fclones *fclones = container_of(skb,
1336 struct sk_buff_fclones,
1337 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001338 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001339
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001340 if (skb_orphan_frags(skb, gfp_mask))
1341 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001342
Herbert Xue0053ec2007-10-14 00:37:52 -07001343 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001344 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001345 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001346 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001347 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001348 if (skb_pfmemalloc(skb))
1349 gfp_mask |= __GFP_MEMALLOC;
1350
Herbert Xue0053ec2007-10-14 00:37:52 -07001351 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1352 if (!n)
1353 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001354
Herbert Xue0053ec2007-10-14 00:37:52 -07001355 n->fclone = SKB_FCLONE_UNAVAILABLE;
1356 }
1357
1358 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001360EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001362void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001363{
Eric Dumazet030737b2013-10-19 11:42:54 -07001364 /* Only adjust this if it actually is csum_start rather than csum */
1365 if (skb->ip_summed == CHECKSUM_PARTIAL)
1366 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001367 /* {transport,network,mac}_header and tail are relative to skb->head */
1368 skb->transport_header += off;
1369 skb->network_header += off;
1370 if (skb_mac_header_was_set(skb))
1371 skb->mac_header += off;
1372 skb->inner_transport_header += off;
1373 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001374 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001375}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001376EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001377
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001378void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Herbert Xudec18812007-10-14 00:37:30 -07001380 __copy_skb_header(new, old);
1381
Herbert Xu79671682006-06-22 02:40:14 -07001382 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1383 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1384 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001386EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Mel Gormanc93bdd02012-07-31 16:44:19 -07001388static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1389{
1390 if (skb_pfmemalloc(skb))
1391 return SKB_ALLOC_RX;
1392 return 0;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/**
1396 * skb_copy - create private copy of an sk_buff
1397 * @skb: buffer to copy
1398 * @gfp_mask: allocation priority
1399 *
1400 * Make a copy of both an &sk_buff and its data. This is used when the
1401 * caller wishes to modify the data and needs a private copy of the
1402 * data to alter. Returns %NULL on failure or the pointer to the buffer
1403 * on success. The returned buffer has a reference count of 1.
1404 *
1405 * As by-product this function converts non-linear &sk_buff to linear
1406 * one, so that &sk_buff becomes completely private and caller is allowed
1407 * to modify all the data of returned buffer. This means that this
1408 * function is not recommended for use in circumstances when only
1409 * header is going to be modified. Use pskb_copy() instead.
1410 */
1411
Al Virodd0fc662005-10-07 07:46:04 +01001412struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001414 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001415 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001416 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1417 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!n)
1420 return NULL;
1421
1422 /* Set the data pointer */
1423 skb_reserve(n, headerlen);
1424 /* Set the tail pointer and length */
1425 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Tim Hansen9f77fad2017-10-09 11:37:59 -04001427 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001429 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return n;
1431}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001432EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001435 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001437 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001439 * @fclone: if true allocate the copy of the skb from the fclone
1440 * cache instead of the head cache; it is recommended to set this
1441 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 *
1443 * Make a copy of both an &sk_buff and part of its data, located
1444 * in header. Fragmented data remain shared. This is used when
1445 * the caller wishes to modify only header of &sk_buff and needs
1446 * private copy of the header to alter. Returns %NULL on failure
1447 * or the pointer to the buffer on success.
1448 * The returned buffer has a reference count of 1.
1449 */
1450
Octavian Purdilabad93e92014-06-12 01:36:26 +03001451struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1452 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Eric Dumazet117632e2011-12-03 21:39:53 +00001454 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001455 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1456 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (!n)
1459 goto out;
1460
1461 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001462 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Set the tail pointer and length */
1464 skb_put(n, skb_headlen(skb));
1465 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001466 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Herbert Xu25f484a2006-11-07 14:57:15 -08001468 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 n->data_len = skb->data_len;
1470 n->len = skb->len;
1471
1472 if (skb_shinfo(skb)->nr_frags) {
1473 int i;
1474
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001475 if (skb_orphan_frags(skb, gfp_mask) ||
1476 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001477 kfree_skb(n);
1478 n = NULL;
1479 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1482 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001483 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 skb_shinfo(n)->nr_frags = i;
1486 }
1487
David S. Miller21dc3302010-08-23 00:13:46 -07001488 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1490 skb_clone_fraglist(n);
1491 }
1492
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001493 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494out:
1495 return n;
1496}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001497EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499/**
1500 * pskb_expand_head - reallocate header of &sk_buff
1501 * @skb: buffer to reallocate
1502 * @nhead: room to add at head
1503 * @ntail: room to add at tail
1504 * @gfp_mask: allocation priority
1505 *
Mathias Krausebc323832013-11-07 14:18:26 +01001506 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1507 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 * reference count of 1. Returns zero in the case of success or error,
1509 * if expansion failed. In the last case, &sk_buff is not changed.
1510 *
1511 * All the pointers pointing into skb header may change and must be
1512 * reloaded after call to this function.
1513 */
1514
Victor Fusco86a76ca2005-07-08 14:57:47 -07001515int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001516 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Eric Dumazet158f3232017-01-27 07:11:27 -08001518 int i, osize = skb_end_offset(skb);
1519 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001521 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Herbert Xu4edd87a2008-10-01 07:09:38 -07001523 BUG_ON(nhead < 0);
1524
Tim Hansen9f77fad2017-10-09 11:37:59 -04001525 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 size = SKB_DATA_ALIGN(size);
1528
Mel Gormanc93bdd02012-07-31 16:44:19 -07001529 if (skb_pfmemalloc(skb))
1530 gfp_mask |= __GFP_MEMALLOC;
1531 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1532 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (!data)
1534 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001535 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001538 * optimized for the cases when header is void.
1539 */
1540 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1541
1542 memcpy((struct skb_shared_info *)(data + size),
1543 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001544 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Alexander Duyck3e245912012-05-04 14:26:51 +00001546 /*
1547 * if shinfo is shared we must drop the old head gracefully, but if it
1548 * is not we can just drop the old head and let the existing refcount
1549 * be since all we did is relocate the values
1550 */
1551 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001552 if (skb_orphan_frags(skb, gfp_mask))
1553 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001554 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001555 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001556 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001557 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Eric Dumazet1fd63042010-09-02 23:09:32 +00001559 if (skb_has_frag_list(skb))
1560 skb_clone_fraglist(skb);
1561
1562 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001563 } else {
1564 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 off = (data + nhead) - skb->head;
1567
1568 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001569 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001571#ifdef NET_SKBUFF_DATA_USES_OFFSET
1572 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001573 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001574#else
1575 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001576#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001577 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001578 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001580 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 skb->nohdr = 0;
1582 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001583
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001584 skb_metadata_clear(skb);
1585
Eric Dumazet158f3232017-01-27 07:11:27 -08001586 /* It is not generally safe to change skb->truesize.
1587 * For the moment, we really care of rx path, or
1588 * when skb is orphaned (not attached to a socket).
1589 */
1590 if (!skb->sk || skb->destructor == sock_edemux)
1591 skb->truesize += size - osize;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594
Shirley Maa6686f22011-07-06 12:22:12 +00001595nofrags:
1596 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597nodata:
1598 return -ENOMEM;
1599}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001600EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602/* Make private copy of skb with writable head and some headroom */
1603
1604struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1605{
1606 struct sk_buff *skb2;
1607 int delta = headroom - skb_headroom(skb);
1608
1609 if (delta <= 0)
1610 skb2 = pskb_copy(skb, GFP_ATOMIC);
1611 else {
1612 skb2 = skb_clone(skb, GFP_ATOMIC);
1613 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1614 GFP_ATOMIC)) {
1615 kfree_skb(skb2);
1616 skb2 = NULL;
1617 }
1618 }
1619 return skb2;
1620}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001621EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623/**
1624 * skb_copy_expand - copy and expand sk_buff
1625 * @skb: buffer to copy
1626 * @newheadroom: new free bytes at head
1627 * @newtailroom: new free bytes at tail
1628 * @gfp_mask: allocation priority
1629 *
1630 * Make a copy of both an &sk_buff and its data and while doing so
1631 * allocate additional space.
1632 *
1633 * This is used when the caller wishes to modify the data and needs a
1634 * private copy of the data to alter as well as more space for new fields.
1635 * Returns %NULL on failure or the pointer to the buffer
1636 * on success. The returned buffer has a reference count of 1.
1637 *
1638 * You must pass %GFP_ATOMIC as the allocation priority if this function
1639 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
1641struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001642 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001643 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 /*
1646 * Allocate the copy buffer
1647 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001648 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1649 gfp_mask, skb_alloc_rx_flag(skb),
1650 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001651 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int head_copy_len, head_copy_off;
1653
1654 if (!n)
1655 return NULL;
1656
1657 skb_reserve(n, newheadroom);
1658
1659 /* Set the tail pointer and length */
1660 skb_put(n, skb->len);
1661
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001662 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 head_copy_off = 0;
1664 if (newheadroom <= head_copy_len)
1665 head_copy_len = newheadroom;
1666 else
1667 head_copy_off = newheadroom - head_copy_len;
1668
1669 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001670 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1671 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001673 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Eric Dumazet030737b2013-10-19 11:42:54 -07001675 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return n;
1678}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001679EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001682 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 * @skb: buffer to pad
1684 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001685 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
1687 * Ensure that a buffer is followed by a padding area that is zero
1688 * filled. Used by network drivers which may DMA or transfer data
1689 * beyond the buffer end onto the wire.
1690 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001691 * May return error in out of memory cases. The skb is freed on error
1692 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001694
Florian Fainellicd0a1372017-08-22 15:12:14 -07001695int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Herbert Xu5b057c62006-06-23 02:06:41 -07001697 int err;
1698 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001701 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001705
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001706 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001707 if (likely(skb_cloned(skb) || ntail > 0)) {
1708 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1709 if (unlikely(err))
1710 goto free_skb;
1711 }
1712
1713 /* FIXME: The use of this function with non-linear skb's really needs
1714 * to be audited.
1715 */
1716 err = skb_linearize(skb);
1717 if (unlikely(err))
1718 goto free_skb;
1719
1720 memset(skb->data + skb->len, 0, pad);
1721 return 0;
1722
1723free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001724 if (free_on_error)
1725 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001726 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001727}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001728EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001729
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001730/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001731 * pskb_put - add data to the tail of a potentially fragmented buffer
1732 * @skb: start of the buffer to use
1733 * @tail: tail fragment of the buffer to use
1734 * @len: amount of data to add
1735 *
1736 * This function extends the used data area of the potentially
1737 * fragmented buffer. @tail must be the last fragment of @skb -- or
1738 * @skb itself. If this would exceed the total buffer size the kernel
1739 * will panic. A pointer to the first byte of the extra data is
1740 * returned.
1741 */
1742
Johannes Berg4df864c2017-06-16 14:29:21 +02001743void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001744{
1745 if (tail != skb) {
1746 skb->data_len += len;
1747 skb->len += len;
1748 }
1749 return skb_put(tail, len);
1750}
1751EXPORT_SYMBOL_GPL(pskb_put);
1752
1753/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001754 * skb_put - add data to a buffer
1755 * @skb: buffer to use
1756 * @len: amount of data to add
1757 *
1758 * This function extends the used data area of the buffer. If this would
1759 * exceed the total buffer size the kernel will panic. A pointer to the
1760 * first byte of the extra data is returned.
1761 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001762void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001763{
Johannes Berg4df864c2017-06-16 14:29:21 +02001764 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001765 SKB_LINEAR_ASSERT(skb);
1766 skb->tail += len;
1767 skb->len += len;
1768 if (unlikely(skb->tail > skb->end))
1769 skb_over_panic(skb, len, __builtin_return_address(0));
1770 return tmp;
1771}
1772EXPORT_SYMBOL(skb_put);
1773
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001774/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001775 * skb_push - add data to the start of a buffer
1776 * @skb: buffer to use
1777 * @len: amount of data to add
1778 *
1779 * This function extends the used data area of the buffer at the buffer
1780 * start. If this would exceed the total buffer headroom the kernel will
1781 * panic. A pointer to the first byte of the extra data is returned.
1782 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001783void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001784{
1785 skb->data -= len;
1786 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301787 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001788 skb_under_panic(skb, len, __builtin_return_address(0));
1789 return skb->data;
1790}
1791EXPORT_SYMBOL(skb_push);
1792
1793/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001794 * skb_pull - remove data from the start of a buffer
1795 * @skb: buffer to use
1796 * @len: amount of data to remove
1797 *
1798 * This function removes data from the start of a buffer, returning
1799 * the memory to the headroom. A pointer to the next data in the buffer
1800 * is returned. Once the data has been pulled future pushes will overwrite
1801 * the old data.
1802 */
Johannes Bergaf728682017-06-16 14:29:22 +02001803void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001804{
David S. Miller47d29642010-05-02 02:21:44 -07001805 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001806}
1807EXPORT_SYMBOL(skb_pull);
1808
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001809/**
1810 * skb_trim - remove end from a buffer
1811 * @skb: buffer to alter
1812 * @len: new length
1813 *
1814 * Cut the length of a buffer down by removing data from the tail. If
1815 * the buffer is already under the length specified it is not modified.
1816 * The skb must be linear.
1817 */
1818void skb_trim(struct sk_buff *skb, unsigned int len)
1819{
1820 if (skb->len > len)
1821 __skb_trim(skb, len);
1822}
1823EXPORT_SYMBOL(skb_trim);
1824
Herbert Xu3cc0e872006-06-09 16:13:38 -07001825/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
1827
Herbert Xu3cc0e872006-06-09 16:13:38 -07001828int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Herbert Xu27b437c2006-07-13 19:26:39 -07001830 struct sk_buff **fragp;
1831 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 int offset = skb_headlen(skb);
1833 int nfrags = skb_shinfo(skb)->nr_frags;
1834 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001835 int err;
1836
1837 if (skb_cloned(skb) &&
1838 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001841 i = 0;
1842 if (offset >= len)
1843 goto drop_pages;
1844
1845 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001846 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001847
1848 if (end < len) {
1849 offset = end;
1850 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001852
Eric Dumazet9e903e02011-10-18 21:00:24 +00001853 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001854
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001855drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001856 skb_shinfo(skb)->nr_frags = i;
1857
1858 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001859 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001860
David S. Miller21dc3302010-08-23 00:13:46 -07001861 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001862 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001863 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
Herbert Xu27b437c2006-07-13 19:26:39 -07001866 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1867 fragp = &frag->next) {
1868 int end = offset + frag->len;
1869
1870 if (skb_shared(frag)) {
1871 struct sk_buff *nfrag;
1872
1873 nfrag = skb_clone(frag, GFP_ATOMIC);
1874 if (unlikely(!nfrag))
1875 return -ENOMEM;
1876
1877 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001878 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001879 frag = nfrag;
1880 *fragp = frag;
1881 }
1882
1883 if (end < len) {
1884 offset = end;
1885 continue;
1886 }
1887
1888 if (end > len &&
1889 unlikely((err = pskb_trim(frag, len - offset))))
1890 return err;
1891
1892 if (frag->next)
1893 skb_drop_list(&frag->next);
1894 break;
1895 }
1896
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001897done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001898 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 skb->data_len -= skb->len - len;
1900 skb->len = len;
1901 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001902 skb->len = len;
1903 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001904 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Eric Dumazetc21b48c2017-04-26 09:07:46 -07001907 if (!skb->sk || skb->destructor == sock_edemux)
1908 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001911EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Eric Dumazet88078d92018-04-18 11:43:15 -07001913/* Note : use pskb_trim_rcsum() instead of calling this directly
1914 */
1915int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1916{
1917 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1918 int delta = skb->len - len;
1919
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07001920 skb->csum = csum_block_sub(skb->csum,
1921 skb_checksum(skb, len, delta, 0),
1922 len);
Eric Dumazet88078d92018-04-18 11:43:15 -07001923 }
1924 return __pskb_trim(skb, len);
1925}
1926EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928/**
1929 * __pskb_pull_tail - advance tail of skb header
1930 * @skb: buffer to reallocate
1931 * @delta: number of bytes to advance tail
1932 *
1933 * The function makes a sense only on a fragmented &sk_buff,
1934 * it expands header moving its tail forward and copying necessary
1935 * data from fragmented part.
1936 *
1937 * &sk_buff MUST have reference count of 1.
1938 *
1939 * Returns %NULL (and &sk_buff does not change) if pull failed
1940 * or value of new tail of skb in the case of success.
1941 *
1942 * All the pointers pointing into skb header may change and must be
1943 * reloaded after call to this function.
1944 */
1945
1946/* Moves tail of skb head forward, copying data from fragmented part,
1947 * when it is necessary.
1948 * 1. It may fail due to malloc failure.
1949 * 2. It may change skb pointers.
1950 *
1951 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1952 */
Johannes Bergaf728682017-06-16 14:29:22 +02001953void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 /* If skb has not enough free space at tail, get new one
1956 * plus 128 bytes for future expansions. If we have enough
1957 * room at tail, reallocate without expansion only if skb is cloned.
1958 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001959 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 if (eat > 0 || skb_cloned(skb)) {
1962 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1963 GFP_ATOMIC))
1964 return NULL;
1965 }
1966
Tim Hansen9f77fad2017-10-09 11:37:59 -04001967 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
1968 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* Optimization: no fragments, no reasons to preestimate
1971 * size of pulled pages. Superb.
1972 */
David S. Miller21dc3302010-08-23 00:13:46 -07001973 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto pull_pages;
1975
1976 /* Estimate size of pulled pages. */
1977 eat = delta;
1978 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001979 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1980
1981 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001983 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
1986 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02001987 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * but taking into account that pulling is expected to
1989 * be very rare operation, it is worth to fight against
1990 * further bloating skb head and crucify ourselves here instead.
1991 * Pure masohism, indeed. 8)8)
1992 */
1993 if (eat) {
1994 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1995 struct sk_buff *clone = NULL;
1996 struct sk_buff *insp = NULL;
1997
1998 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (list->len <= eat) {
2000 /* Eaten as whole. */
2001 eat -= list->len;
2002 list = list->next;
2003 insp = list;
2004 } else {
2005 /* Eaten partially. */
2006
2007 if (skb_shared(list)) {
2008 /* Sucks! We need to fork list. :-( */
2009 clone = skb_clone(list, GFP_ATOMIC);
2010 if (!clone)
2011 return NULL;
2012 insp = list->next;
2013 list = clone;
2014 } else {
2015 /* This may be pulled without
2016 * problems. */
2017 insp = list;
2018 }
2019 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002020 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return NULL;
2022 }
2023 break;
2024 }
2025 } while (eat);
2026
2027 /* Free pulled out fragments. */
2028 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2029 skb_shinfo(skb)->frag_list = list->next;
2030 kfree_skb(list);
2031 }
2032 /* And insert new clone at head. */
2033 if (clone) {
2034 clone->next = list;
2035 skb_shinfo(skb)->frag_list = clone;
2036 }
2037 }
2038 /* Success! Now we may commit changes to skb data. */
2039
2040pull_pages:
2041 eat = delta;
2042 k = 0;
2043 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002044 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2045
2046 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002047 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002048 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } else {
2050 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
2051 if (eat) {
2052 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002053 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002054 if (!i)
2055 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 eat = 0;
2057 }
2058 k++;
2059 }
2060 }
2061 skb_shinfo(skb)->nr_frags = k;
2062
linzhang3ccc6c62017-07-17 17:25:02 +08002063end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 skb->tail += delta;
2065 skb->data_len -= delta;
2066
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002067 if (!skb->data_len)
2068 skb_zcopy_clear(skb, false);
2069
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002070 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002072EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Eric Dumazet22019b12011-07-29 18:37:31 +00002074/**
2075 * skb_copy_bits - copy bits from skb to kernel buffer
2076 * @skb: source skb
2077 * @offset: offset in source
2078 * @to: destination buffer
2079 * @len: number of bytes to copy
2080 *
2081 * Copy the specified number of bytes from the source skb to the
2082 * destination buffer.
2083 *
2084 * CAUTION ! :
2085 * If its prototype is ever changed,
2086 * check arch/{*}/net/{*}.S files,
2087 * since it is called from BPF assembly code.
2088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2090{
David S. Miller1a028e52007-04-27 15:21:23 -07002091 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002092 struct sk_buff *frag_iter;
2093 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 if (offset > (int)skb->len - len)
2096 goto fault;
2097
2098 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002099 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (copy > len)
2101 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002102 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if ((len -= copy) == 0)
2104 return 0;
2105 offset += copy;
2106 to += copy;
2107 }
2108
2109 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002110 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002111 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002113 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002114
Eric Dumazet51c56b02012-04-05 11:35:15 +02002115 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002117 u32 p_off, p_len, copied;
2118 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 u8 *vaddr;
2120
2121 if (copy > len)
2122 copy = len;
2123
Willem de Bruijnc613c202017-07-31 08:15:47 -04002124 skb_frag_foreach_page(f,
2125 f->page_offset + offset - start,
2126 copy, p, p_off, p_len, copied) {
2127 vaddr = kmap_atomic(p);
2128 memcpy(to + copied, vaddr + p_off, p_len);
2129 kunmap_atomic(vaddr);
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 if ((len -= copy) == 0)
2133 return 0;
2134 offset += copy;
2135 to += copy;
2136 }
David S. Miller1a028e52007-04-27 15:21:23 -07002137 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139
David S. Millerfbb398a2009-06-09 00:18:59 -07002140 skb_walk_frags(skb, frag_iter) {
2141 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
David S. Millerfbb398a2009-06-09 00:18:59 -07002143 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
David S. Millerfbb398a2009-06-09 00:18:59 -07002145 end = start + frag_iter->len;
2146 if ((copy = end - offset) > 0) {
2147 if (copy > len)
2148 copy = len;
2149 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2150 goto fault;
2151 if ((len -= copy) == 0)
2152 return 0;
2153 offset += copy;
2154 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002156 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Shirley Maa6686f22011-07-06 12:22:12 +00002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (!len)
2160 return 0;
2161
2162fault:
2163 return -EFAULT;
2164}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002165EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jens Axboe9c55e012007-11-06 23:30:13 -08002167/*
2168 * Callback from splice_to_pipe(), if we need to release some pages
2169 * at the end of the spd in case we error'ed out in filling the pipe.
2170 */
2171static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2172{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002173 put_page(spd->pages[i]);
2174}
Jens Axboe9c55e012007-11-06 23:30:13 -08002175
David S. Millera108d5f2012-04-23 23:06:11 -04002176static struct page *linear_to_page(struct page *page, unsigned int *len,
2177 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002178 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002179{
Eric Dumazet5640f762012-09-23 23:04:42 +00002180 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002181
Eric Dumazet5640f762012-09-23 23:04:42 +00002182 if (!sk_page_frag_refill(sk, pfrag))
2183 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002184
Eric Dumazet5640f762012-09-23 23:04:42 +00002185 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002186
Eric Dumazet5640f762012-09-23 23:04:42 +00002187 memcpy(page_address(pfrag->page) + pfrag->offset,
2188 page_address(page) + *offset, *len);
2189 *offset = pfrag->offset;
2190 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002191
Eric Dumazet5640f762012-09-23 23:04:42 +00002192 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002193}
2194
Eric Dumazet41c73a02012-04-22 12:26:16 +00002195static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2196 struct page *page,
2197 unsigned int offset)
2198{
2199 return spd->nr_pages &&
2200 spd->pages[spd->nr_pages - 1] == page &&
2201 (spd->partial[spd->nr_pages - 1].offset +
2202 spd->partial[spd->nr_pages - 1].len == offset);
2203}
2204
Jens Axboe9c55e012007-11-06 23:30:13 -08002205/*
2206 * Fill page/offset/length into spd, if it can hold more pages.
2207 */
David S. Millera108d5f2012-04-23 23:06:11 -04002208static bool spd_fill_page(struct splice_pipe_desc *spd,
2209 struct pipe_inode_info *pipe, struct page *page,
2210 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002211 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002212 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002213{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002214 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002215 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002216
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002217 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002218 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002219 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002220 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002221 }
2222 if (spd_can_coalesce(spd, page, offset)) {
2223 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002224 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002225 }
2226 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002227 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002228 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002229 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002230 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002231
David S. Millera108d5f2012-04-23 23:06:11 -04002232 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002233}
2234
David S. Millera108d5f2012-04-23 23:06:11 -04002235static bool __splice_segment(struct page *page, unsigned int poff,
2236 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002237 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002238 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002239 struct sock *sk,
2240 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002241{
2242 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002243 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002244
2245 /* skip this segment if already processed */
2246 if (*off >= plen) {
2247 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002248 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002249 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002250
Octavian Purdila2870c432008-07-15 00:49:11 -07002251 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002252 poff += *off;
2253 plen -= *off;
2254 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002255
Eric Dumazet18aafc62013-01-11 14:46:37 +00002256 do {
2257 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002258
Eric Dumazet18aafc62013-01-11 14:46:37 +00002259 if (spd_fill_page(spd, pipe, page, &flen, poff,
2260 linear, sk))
2261 return true;
2262 poff += flen;
2263 plen -= flen;
2264 *len -= flen;
2265 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002266
David S. Millera108d5f2012-04-23 23:06:11 -04002267 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002268}
2269
2270/*
David S. Millera108d5f2012-04-23 23:06:11 -04002271 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002272 * pipe is full or if we already spliced the requested length.
2273 */
David S. Millera108d5f2012-04-23 23:06:11 -04002274static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2275 unsigned int *offset, unsigned int *len,
2276 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002277{
2278 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002279 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002280
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002281 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002282 * If skb->head_frag is set, this 'linear' part is backed by a
2283 * fragment, and if the head is not shared with any clones then
2284 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002285 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002286 if (__splice_segment(virt_to_page(skb->data),
2287 (unsigned long) skb->data & (PAGE_SIZE - 1),
2288 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002289 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002290 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002291 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002292 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002293
2294 /*
2295 * then map the fragments
2296 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002297 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2298 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2299
Ian Campbellea2ab692011-08-22 23:44:58 +00002300 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00002301 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002302 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002303 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002304 }
2305
Tom Herbertfa9835e2016-03-07 14:11:04 -08002306 skb_walk_frags(skb, iter) {
2307 if (*offset >= iter->len) {
2308 *offset -= iter->len;
2309 continue;
2310 }
2311 /* __skb_splice_bits() only fails if the output has no room
2312 * left, so no point in going over the frag_list for the error
2313 * case.
2314 */
2315 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2316 return true;
2317 }
2318
David S. Millera108d5f2012-04-23 23:06:11 -04002319 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002320}
2321
2322/*
2323 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002324 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002325 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002326int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002327 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002328 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002329{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002330 struct partial_page partial[MAX_SKB_FRAGS];
2331 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002332 struct splice_pipe_desc spd = {
2333 .pages = pages,
2334 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002335 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002336 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002337 .spd_release = sock_spd_release,
2338 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002339 int ret = 0;
2340
Tom Herbertfa9835e2016-03-07 14:11:04 -08002341 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002342
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002343 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002344 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002345
Jens Axboe35f3d142010-05-20 10:43:18 +02002346 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002347}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002348EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002349
Tom Herbert20bf50d2017-07-28 16:22:42 -07002350/* Send skb data on a socket. Socket must be locked. */
2351int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2352 int len)
2353{
2354 unsigned int orig_len = len;
2355 struct sk_buff *head = skb;
2356 unsigned short fragidx;
2357 int slen, ret;
2358
2359do_frag_list:
2360
2361 /* Deal with head data */
2362 while (offset < skb_headlen(skb) && len) {
2363 struct kvec kv;
2364 struct msghdr msg;
2365
2366 slen = min_t(int, len, skb_headlen(skb) - offset);
2367 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002368 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002369 memset(&msg, 0, sizeof(msg));
2370
2371 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
2372 if (ret <= 0)
2373 goto error;
2374
2375 offset += ret;
2376 len -= ret;
2377 }
2378
2379 /* All the data was skb head? */
2380 if (!len)
2381 goto out;
2382
2383 /* Make offset relative to start of frags */
2384 offset -= skb_headlen(skb);
2385
2386 /* Find where we are in frag list */
2387 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2388 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2389
2390 if (offset < frag->size)
2391 break;
2392
2393 offset -= frag->size;
2394 }
2395
2396 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2397 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2398
2399 slen = min_t(size_t, len, frag->size - offset);
2400
2401 while (slen) {
2402 ret = kernel_sendpage_locked(sk, frag->page.p,
2403 frag->page_offset + offset,
2404 slen, MSG_DONTWAIT);
2405 if (ret <= 0)
2406 goto error;
2407
2408 len -= ret;
2409 offset += ret;
2410 slen -= ret;
2411 }
2412
2413 offset = 0;
2414 }
2415
2416 if (len) {
2417 /* Process any frag lists */
2418
2419 if (skb == head) {
2420 if (skb_has_frag_list(skb)) {
2421 skb = skb_shinfo(skb)->frag_list;
2422 goto do_frag_list;
2423 }
2424 } else if (skb->next) {
2425 skb = skb->next;
2426 goto do_frag_list;
2427 }
2428 }
2429
2430out:
2431 return orig_len - len;
2432
2433error:
2434 return orig_len == len ? ret : orig_len - len;
2435}
2436EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2437
Herbert Xu357b40a2005-04-19 22:30:14 -07002438/**
2439 * skb_store_bits - store bits from kernel buffer to skb
2440 * @skb: destination buffer
2441 * @offset: offset in destination
2442 * @from: source buffer
2443 * @len: number of bytes to copy
2444 *
2445 * Copy the specified number of bytes from the source buffer to the
2446 * destination skb. This function handles all the messy bits of
2447 * traversing fragment lists and such.
2448 */
2449
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002450int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002451{
David S. Miller1a028e52007-04-27 15:21:23 -07002452 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002453 struct sk_buff *frag_iter;
2454 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002455
2456 if (offset > (int)skb->len - len)
2457 goto fault;
2458
David S. Miller1a028e52007-04-27 15:21:23 -07002459 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002460 if (copy > len)
2461 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002462 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002463 if ((len -= copy) == 0)
2464 return 0;
2465 offset += copy;
2466 from += copy;
2467 }
2468
2469 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2470 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002471 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002472
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002473 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002474
Eric Dumazet9e903e02011-10-18 21:00:24 +00002475 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002476 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002477 u32 p_off, p_len, copied;
2478 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002479 u8 *vaddr;
2480
2481 if (copy > len)
2482 copy = len;
2483
Willem de Bruijnc613c202017-07-31 08:15:47 -04002484 skb_frag_foreach_page(frag,
2485 frag->page_offset + offset - start,
2486 copy, p, p_off, p_len, copied) {
2487 vaddr = kmap_atomic(p);
2488 memcpy(vaddr + p_off, from + copied, p_len);
2489 kunmap_atomic(vaddr);
2490 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002491
2492 if ((len -= copy) == 0)
2493 return 0;
2494 offset += copy;
2495 from += copy;
2496 }
David S. Miller1a028e52007-04-27 15:21:23 -07002497 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002498 }
2499
David S. Millerfbb398a2009-06-09 00:18:59 -07002500 skb_walk_frags(skb, frag_iter) {
2501 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002502
David S. Millerfbb398a2009-06-09 00:18:59 -07002503 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002504
David S. Millerfbb398a2009-06-09 00:18:59 -07002505 end = start + frag_iter->len;
2506 if ((copy = end - offset) > 0) {
2507 if (copy > len)
2508 copy = len;
2509 if (skb_store_bits(frag_iter, offset - start,
2510 from, copy))
2511 goto fault;
2512 if ((len -= copy) == 0)
2513 return 0;
2514 offset += copy;
2515 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002516 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002517 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002518 }
2519 if (!len)
2520 return 0;
2521
2522fault:
2523 return -EFAULT;
2524}
Herbert Xu357b40a2005-04-19 22:30:14 -07002525EXPORT_SYMBOL(skb_store_bits);
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002528__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2529 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
David S. Miller1a028e52007-04-27 15:21:23 -07002531 int start = skb_headlen(skb);
2532 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002533 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int pos = 0;
2535
2536 /* Checksum header. */
2537 if (copy > 0) {
2538 if (copy > len)
2539 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002540 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2541 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if ((len -= copy) == 0)
2543 return csum;
2544 offset += copy;
2545 pos = copy;
2546 }
2547
2548 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002549 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002550 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002552 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002553
Eric Dumazet51c56b02012-04-05 11:35:15 +02002554 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002556 u32 p_off, p_len, copied;
2557 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002558 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 if (copy > len)
2562 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002563
2564 skb_frag_foreach_page(frag,
2565 frag->page_offset + offset - start,
2566 copy, p, p_off, p_len, copied) {
2567 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002568 csum2 = INDIRECT_CALL_1(ops->update,
2569 csum_partial_ext,
2570 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002571 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002572 csum = INDIRECT_CALL_1(ops->combine,
2573 csum_block_add_ext, csum,
2574 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002575 pos += p_len;
2576 }
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (!(len -= copy))
2579 return csum;
2580 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
David S. Miller1a028e52007-04-27 15:21:23 -07002582 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
2584
David S. Millerfbb398a2009-06-09 00:18:59 -07002585 skb_walk_frags(skb, frag_iter) {
2586 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
David S. Millerfbb398a2009-06-09 00:18:59 -07002588 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
David S. Millerfbb398a2009-06-09 00:18:59 -07002590 end = start + frag_iter->len;
2591 if ((copy = end - offset) > 0) {
2592 __wsum csum2;
2593 if (copy > len)
2594 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002595 csum2 = __skb_checksum(frag_iter, offset - start,
2596 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002597 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2598 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002599 if ((len -= copy) == 0)
2600 return csum;
2601 offset += copy;
2602 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002604 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002606 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 return csum;
2609}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002610EXPORT_SYMBOL(__skb_checksum);
2611
2612__wsum skb_checksum(const struct sk_buff *skb, int offset,
2613 int len, __wsum csum)
2614{
2615 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002616 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002617 .combine = csum_block_add_ext,
2618 };
2619
2620 return __skb_checksum(skb, offset, len, csum, &ops);
2621}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002622EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624/* Both of above in one bottle. */
2625
Al Viro81d77662006-11-14 21:37:33 -08002626__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2627 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
David S. Miller1a028e52007-04-27 15:21:23 -07002629 int start = skb_headlen(skb);
2630 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002631 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int pos = 0;
2633
2634 /* Copy header. */
2635 if (copy > 0) {
2636 if (copy > len)
2637 copy = len;
2638 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2639 copy, csum);
2640 if ((len -= copy) == 0)
2641 return csum;
2642 offset += copy;
2643 to += copy;
2644 pos = copy;
2645 }
2646
2647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002648 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002650 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002651
Eric Dumazet9e903e02011-10-18 21:00:24 +00002652 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002654 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2655 u32 p_off, p_len, copied;
2656 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002657 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 if (copy > len)
2661 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002662
2663 skb_frag_foreach_page(frag,
2664 frag->page_offset + offset - start,
2665 copy, p, p_off, p_len, copied) {
2666 vaddr = kmap_atomic(p);
2667 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2668 to + copied,
2669 p_len, 0);
2670 kunmap_atomic(vaddr);
2671 csum = csum_block_add(csum, csum2, pos);
2672 pos += p_len;
2673 }
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (!(len -= copy))
2676 return csum;
2677 offset += copy;
2678 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
David S. Miller1a028e52007-04-27 15:21:23 -07002680 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682
David S. Millerfbb398a2009-06-09 00:18:59 -07002683 skb_walk_frags(skb, frag_iter) {
2684 __wsum csum2;
2685 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
David S. Millerfbb398a2009-06-09 00:18:59 -07002687 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
David S. Millerfbb398a2009-06-09 00:18:59 -07002689 end = start + frag_iter->len;
2690 if ((copy = end - offset) > 0) {
2691 if (copy > len)
2692 copy = len;
2693 csum2 = skb_copy_and_csum_bits(frag_iter,
2694 offset - start,
2695 to, copy, 0);
2696 csum = csum_block_add(csum, csum2, pos);
2697 if ((len -= copy) == 0)
2698 return csum;
2699 offset += copy;
2700 to += copy;
2701 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002703 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002705 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return csum;
2707}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002708EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Cong Wang49f8e832018-11-08 14:05:42 -08002710__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2711{
2712 __sum16 sum;
2713
2714 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002715 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002716 if (likely(!sum)) {
2717 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2718 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002719 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002720 }
2721 if (!skb_shared(skb))
2722 skb->csum_valid = !sum;
2723 return sum;
2724}
2725EXPORT_SYMBOL(__skb_checksum_complete_head);
2726
Cong Wang14641932018-11-26 09:31:26 -08002727/* This function assumes skb->csum already holds pseudo header's checksum,
2728 * which has been changed from the hardware checksum, for example, by
2729 * __skb_checksum_validate_complete(). And, the original skb->csum must
2730 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2731 *
2732 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2733 * zero. The new checksum is stored back into skb->csum unless the skb is
2734 * shared.
2735 */
Cong Wang49f8e832018-11-08 14:05:42 -08002736__sum16 __skb_checksum_complete(struct sk_buff *skb)
2737{
2738 __wsum csum;
2739 __sum16 sum;
2740
2741 csum = skb_checksum(skb, 0, skb->len, 0);
2742
Cong Wang49f8e832018-11-08 14:05:42 -08002743 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002744 /* This check is inverted, because we already knew the hardware
2745 * checksum is invalid before calling this function. So, if the
2746 * re-computed checksum is valid instead, then we have a mismatch
2747 * between the original skb->csum and skb_checksum(). This means either
2748 * the original hardware checksum is incorrect or we screw up skb->csum
2749 * when moving skb->data around.
2750 */
Cong Wang49f8e832018-11-08 14:05:42 -08002751 if (likely(!sum)) {
2752 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2753 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002754 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002755 }
2756
2757 if (!skb_shared(skb)) {
2758 /* Save full packet checksum */
2759 skb->csum = csum;
2760 skb->ip_summed = CHECKSUM_COMPLETE;
2761 skb->csum_complete_sw = 1;
2762 skb->csum_valid = !sum;
2763 }
2764
2765 return sum;
2766}
2767EXPORT_SYMBOL(__skb_checksum_complete);
2768
Davide Caratti96178132017-05-18 15:44:37 +02002769static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2770{
2771 net_warn_ratelimited(
2772 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2773 __func__);
2774 return 0;
2775}
2776
2777static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2778 int offset, int len)
2779{
2780 net_warn_ratelimited(
2781 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2782 __func__);
2783 return 0;
2784}
2785
2786static const struct skb_checksum_ops default_crc32c_ops = {
2787 .update = warn_crc32c_csum_update,
2788 .combine = warn_crc32c_csum_combine,
2789};
2790
2791const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2792 &default_crc32c_ops;
2793EXPORT_SYMBOL(crc32c_csum_stub);
2794
Thomas Grafaf2806f2013-12-13 15:22:17 +01002795 /**
2796 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2797 * @from: source buffer
2798 *
2799 * Calculates the amount of linear headroom needed in the 'to' skb passed
2800 * into skb_zerocopy().
2801 */
2802unsigned int
2803skb_zerocopy_headlen(const struct sk_buff *from)
2804{
2805 unsigned int hlen = 0;
2806
2807 if (!from->head_frag ||
2808 skb_headlen(from) < L1_CACHE_BYTES ||
2809 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2810 hlen = skb_headlen(from);
2811
2812 if (skb_has_frag_list(from))
2813 hlen = from->len;
2814
2815 return hlen;
2816}
2817EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2818
2819/**
2820 * skb_zerocopy - Zero copy skb to skb
2821 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002822 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002823 * @len: number of bytes to copy from source buffer
2824 * @hlen: size of linear headroom in destination buffer
2825 *
2826 * Copies up to `len` bytes from `from` to `to` by creating references
2827 * to the frags in the source buffer.
2828 *
2829 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2830 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002831 *
2832 * Return value:
2833 * 0: everything is OK
2834 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2835 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002836 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002837int
2838skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002839{
2840 int i, j = 0;
2841 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002842 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002843 struct page *page;
2844 unsigned int offset;
2845
2846 BUG_ON(!from->head_frag && !hlen);
2847
2848 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002849 if (len <= skb_tailroom(to))
2850 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002851
2852 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002853 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2854 if (unlikely(ret))
2855 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002856 len -= hlen;
2857 } else {
2858 plen = min_t(int, skb_headlen(from), len);
2859 if (plen) {
2860 page = virt_to_head_page(from->head);
2861 offset = from->data - (unsigned char *)page_address(page);
2862 __skb_fill_page_desc(to, 0, page, offset, plen);
2863 get_page(page);
2864 j = 1;
2865 len -= plen;
2866 }
2867 }
2868
2869 to->truesize += len + plen;
2870 to->len += len + plen;
2871 to->data_len += len + plen;
2872
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002873 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2874 skb_tx_error(from);
2875 return -ENOMEM;
2876 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002877 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002878
Thomas Grafaf2806f2013-12-13 15:22:17 +01002879 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2880 if (!len)
2881 break;
2882 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2883 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2884 len -= skb_shinfo(to)->frags[j].size;
2885 skb_frag_ref(to, j);
2886 j++;
2887 }
2888 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002889
2890 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002891}
2892EXPORT_SYMBOL_GPL(skb_zerocopy);
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2895{
Al Virod3bc23e2006-11-14 21:24:49 -08002896 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 long csstart;
2898
Patrick McHardy84fa7932006-08-29 16:44:56 -07002899 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002900 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 else
2902 csstart = skb_headlen(skb);
2903
Kris Katterjohn09a62662006-01-08 22:24:28 -08002904 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002906 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 csum = 0;
2909 if (csstart != skb->len)
2910 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2911 skb->len - csstart, 0);
2912
Patrick McHardy84fa7932006-08-29 16:44:56 -07002913 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002914 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Al Virod3bc23e2006-11-14 21:24:49 -08002916 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002919EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921/**
2922 * skb_dequeue - remove from the head of the queue
2923 * @list: list to dequeue from
2924 *
2925 * Remove the head of the list. The list lock is taken so the function
2926 * may be used safely with other locking list functions. The head item is
2927 * returned or %NULL if the list is empty.
2928 */
2929
2930struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2931{
2932 unsigned long flags;
2933 struct sk_buff *result;
2934
2935 spin_lock_irqsave(&list->lock, flags);
2936 result = __skb_dequeue(list);
2937 spin_unlock_irqrestore(&list->lock, flags);
2938 return result;
2939}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002940EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942/**
2943 * skb_dequeue_tail - remove from the tail of the queue
2944 * @list: list to dequeue from
2945 *
2946 * Remove the tail of the list. The list lock is taken so the function
2947 * may be used safely with other locking list functions. The tail item is
2948 * returned or %NULL if the list is empty.
2949 */
2950struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2951{
2952 unsigned long flags;
2953 struct sk_buff *result;
2954
2955 spin_lock_irqsave(&list->lock, flags);
2956 result = __skb_dequeue_tail(list);
2957 spin_unlock_irqrestore(&list->lock, flags);
2958 return result;
2959}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002960EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962/**
2963 * skb_queue_purge - empty a list
2964 * @list: list to empty
2965 *
2966 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2967 * the list and one reference dropped. This function takes the list
2968 * lock and is atomic with respect to other list locking functions.
2969 */
2970void skb_queue_purge(struct sk_buff_head *list)
2971{
2972 struct sk_buff *skb;
2973 while ((skb = skb_dequeue(list)) != NULL)
2974 kfree_skb(skb);
2975}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002976EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002979 * skb_rbtree_purge - empty a skb rbtree
2980 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00002981 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002982 *
2983 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2984 * the list and one reference dropped. This function does not take
2985 * any lock. Synchronization should be handled by the caller (e.g., TCP
2986 * out-of-order queue is protected by the socket lock).
2987 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00002988unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002989{
Eric Dumazet7c905842017-09-23 12:39:12 -07002990 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002991 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002992
Eric Dumazet7c905842017-09-23 12:39:12 -07002993 while (p) {
2994 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2995
2996 p = rb_next(p);
2997 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00002998 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07002999 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003000 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003001 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003002}
3003
3004/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 * skb_queue_head - queue a buffer at the list head
3006 * @list: list to use
3007 * @newsk: buffer to queue
3008 *
3009 * Queue a buffer at the start of the list. This function takes the
3010 * list lock and can be used safely with other locking &sk_buff functions
3011 * safely.
3012 *
3013 * A buffer cannot be placed on two lists at the same time.
3014 */
3015void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3016{
3017 unsigned long flags;
3018
3019 spin_lock_irqsave(&list->lock, flags);
3020 __skb_queue_head(list, newsk);
3021 spin_unlock_irqrestore(&list->lock, flags);
3022}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003023EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025/**
3026 * skb_queue_tail - queue a buffer at the list tail
3027 * @list: list to use
3028 * @newsk: buffer to queue
3029 *
3030 * Queue a buffer at the tail of the list. This function takes the
3031 * list lock and can be used safely with other locking &sk_buff functions
3032 * safely.
3033 *
3034 * A buffer cannot be placed on two lists at the same time.
3035 */
3036void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3037{
3038 unsigned long flags;
3039
3040 spin_lock_irqsave(&list->lock, flags);
3041 __skb_queue_tail(list, newsk);
3042 spin_unlock_irqrestore(&list->lock, flags);
3043}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003044EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046/**
3047 * skb_unlink - remove a buffer from a list
3048 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003049 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 *
David S. Miller8728b832005-08-09 19:25:21 -07003051 * Remove a packet from a list. The list locks are taken and this
3052 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 *
David S. Miller8728b832005-08-09 19:25:21 -07003054 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 */
David S. Miller8728b832005-08-09 19:25:21 -07003056void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
David S. Miller8728b832005-08-09 19:25:21 -07003058 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
David S. Miller8728b832005-08-09 19:25:21 -07003060 spin_lock_irqsave(&list->lock, flags);
3061 __skb_unlink(skb, list);
3062 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003064EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066/**
3067 * skb_append - append a buffer
3068 * @old: buffer to insert after
3069 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003070 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 *
3072 * Place a packet after a given packet in a list. The list locks are taken
3073 * and this function is atomic with respect to other list locked calls.
3074 * A buffer cannot be placed on two lists at the same time.
3075 */
David S. Miller8728b832005-08-09 19:25:21 -07003076void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 unsigned long flags;
3079
David S. Miller8728b832005-08-09 19:25:21 -07003080 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003081 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003082 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003084EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086static inline void skb_split_inside_header(struct sk_buff *skb,
3087 struct sk_buff* skb1,
3088 const u32 len, const int pos)
3089{
3090 int i;
3091
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003092 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3093 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 /* And move data appendix as is. */
3095 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3096 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3097
3098 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3099 skb_shinfo(skb)->nr_frags = 0;
3100 skb1->data_len = skb->data_len;
3101 skb1->len += skb1->data_len;
3102 skb->data_len = 0;
3103 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003104 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
3107static inline void skb_split_no_header(struct sk_buff *skb,
3108 struct sk_buff* skb1,
3109 const u32 len, int pos)
3110{
3111 int i, k = 0;
3112 const int nfrags = skb_shinfo(skb)->nr_frags;
3113
3114 skb_shinfo(skb)->nr_frags = 0;
3115 skb1->len = skb1->data_len = skb->len - len;
3116 skb->len = len;
3117 skb->data_len = len - pos;
3118
3119 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003120 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 if (pos + size > len) {
3123 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3124
3125 if (pos < len) {
3126 /* Split frag.
3127 * We have two variants in this case:
3128 * 1. Move all the frag to the second
3129 * part, if it is possible. F.e.
3130 * this approach is mandatory for TUX,
3131 * where splitting is expensive.
3132 * 2. Split is accurately. We make this.
3133 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003134 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003136 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3137 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 skb_shinfo(skb)->nr_frags++;
3139 }
3140 k++;
3141 } else
3142 skb_shinfo(skb)->nr_frags++;
3143 pos += size;
3144 }
3145 skb_shinfo(skb1)->nr_frags = k;
3146}
3147
3148/**
3149 * skb_split - Split fragmented skb to two parts at length len.
3150 * @skb: the buffer to split
3151 * @skb1: the buffer to receive the second part
3152 * @len: new length for skb
3153 */
3154void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3155{
3156 int pos = skb_headlen(skb);
3157
Willem de Bruijnfff88032017-06-08 11:35:03 -04003158 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
3159 SKBTX_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003160 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (len < pos) /* Split line is inside header. */
3162 skb_split_inside_header(skb, skb1, len, pos);
3163 else /* Second chunk has no header, nothing to copy. */
3164 skb_split_no_header(skb, skb1, len, pos);
3165}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003166EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003168/* Shifting from/to a cloned skb is a no-go.
3169 *
3170 * Caller cannot keep skb_shinfo related pointers past calling here!
3171 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003172static int skb_prepare_for_shift(struct sk_buff *skb)
3173{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08003174 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003175}
3176
3177/**
3178 * skb_shift - Shifts paged data partially from skb to another
3179 * @tgt: buffer into which tail data gets added
3180 * @skb: buffer from which the paged data comes from
3181 * @shiftlen: shift up to this many bytes
3182 *
3183 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003184 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003185 * It's up to caller to free skb if everything was shifted.
3186 *
3187 * If @tgt runs out of frags, the whole operation is aborted.
3188 *
3189 * Skb cannot include anything else but paged data while tgt is allowed
3190 * to have non-paged data as well.
3191 *
3192 * TODO: full sized shift could be optimized but that would need
3193 * specialized skb free'er to handle frags without up-to-date nr_frags.
3194 */
3195int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3196{
3197 int from, to, merge, todo;
3198 struct skb_frag_struct *fragfrom, *fragto;
3199
3200 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003201
3202 if (skb_headlen(skb))
3203 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003204 if (skb_zcopy(tgt) || skb_zcopy(skb))
3205 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003206
3207 todo = shiftlen;
3208 from = 0;
3209 to = skb_shinfo(tgt)->nr_frags;
3210 fragfrom = &skb_shinfo(skb)->frags[from];
3211
3212 /* Actual merge is delayed until the point when we know we can
3213 * commit all, so that we don't have to undo partial changes
3214 */
3215 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003216 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3217 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003218 merge = -1;
3219 } else {
3220 merge = to - 1;
3221
Eric Dumazet9e903e02011-10-18 21:00:24 +00003222 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003223 if (todo < 0) {
3224 if (skb_prepare_for_shift(skb) ||
3225 skb_prepare_for_shift(tgt))
3226 return 0;
3227
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003228 /* All previous frag pointers might be stale! */
3229 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003230 fragto = &skb_shinfo(tgt)->frags[merge];
3231
Eric Dumazet9e903e02011-10-18 21:00:24 +00003232 skb_frag_size_add(fragto, shiftlen);
3233 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003234 fragfrom->page_offset += shiftlen;
3235
3236 goto onlymerged;
3237 }
3238
3239 from++;
3240 }
3241
3242 /* Skip full, not-fitting skb to avoid expensive operations */
3243 if ((shiftlen == skb->len) &&
3244 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3245 return 0;
3246
3247 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3248 return 0;
3249
3250 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3251 if (to == MAX_SKB_FRAGS)
3252 return 0;
3253
3254 fragfrom = &skb_shinfo(skb)->frags[from];
3255 fragto = &skb_shinfo(tgt)->frags[to];
3256
Eric Dumazet9e903e02011-10-18 21:00:24 +00003257 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003258 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003259 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003260 from++;
3261 to++;
3262
3263 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003264 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003265 fragto->page = fragfrom->page;
3266 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003267 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003268
3269 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003270 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003271 todo = 0;
3272
3273 to++;
3274 break;
3275 }
3276 }
3277
3278 /* Ready to "commit" this state change to tgt */
3279 skb_shinfo(tgt)->nr_frags = to;
3280
3281 if (merge >= 0) {
3282 fragfrom = &skb_shinfo(skb)->frags[0];
3283 fragto = &skb_shinfo(tgt)->frags[merge];
3284
Eric Dumazet9e903e02011-10-18 21:00:24 +00003285 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00003286 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003287 }
3288
3289 /* Reposition in the original skb */
3290 to = 0;
3291 while (from < skb_shinfo(skb)->nr_frags)
3292 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3293 skb_shinfo(skb)->nr_frags = to;
3294
3295 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3296
3297onlymerged:
3298 /* Most likely the tgt won't ever need its checksum anymore, skb on
3299 * the other hand might need it if it needs to be resent
3300 */
3301 tgt->ip_summed = CHECKSUM_PARTIAL;
3302 skb->ip_summed = CHECKSUM_PARTIAL;
3303
3304 /* Yak, is it really working this way? Some helper please? */
3305 skb->len -= shiftlen;
3306 skb->data_len -= shiftlen;
3307 skb->truesize -= shiftlen;
3308 tgt->len += shiftlen;
3309 tgt->data_len += shiftlen;
3310 tgt->truesize += shiftlen;
3311
3312 return shiftlen;
3313}
3314
Thomas Graf677e90e2005-06-23 20:59:51 -07003315/**
3316 * skb_prepare_seq_read - Prepare a sequential read of skb data
3317 * @skb: the buffer to read
3318 * @from: lower offset of data to be read
3319 * @to: upper offset of data to be read
3320 * @st: state variable
3321 *
3322 * Initializes the specified state variable. Must be called before
3323 * invoking skb_seq_read() for the first time.
3324 */
3325void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3326 unsigned int to, struct skb_seq_state *st)
3327{
3328 st->lower_offset = from;
3329 st->upper_offset = to;
3330 st->root_skb = st->cur_skb = skb;
3331 st->frag_idx = st->stepped_offset = 0;
3332 st->frag_data = NULL;
3333}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003334EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003335
3336/**
3337 * skb_seq_read - Sequentially read skb data
3338 * @consumed: number of bytes consumed by the caller so far
3339 * @data: destination pointer for data to be returned
3340 * @st: state variable
3341 *
Mathias Krausebc323832013-11-07 14:18:26 +01003342 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003343 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003344 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003345 * of the block or 0 if the end of the skb data or the upper
3346 * offset has been reached.
3347 *
3348 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003349 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003350 * of bytes already consumed and the next call to
3351 * skb_seq_read() will return the remaining part of the block.
3352 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003353 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003354 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003355 * reads of potentially non linear data.
3356 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003357 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003358 * at the moment, state->root_skb could be replaced with
3359 * a stack for this purpose.
3360 */
3361unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3362 struct skb_seq_state *st)
3363{
3364 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3365 skb_frag_t *frag;
3366
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003367 if (unlikely(abs_offset >= st->upper_offset)) {
3368 if (st->frag_data) {
3369 kunmap_atomic(st->frag_data);
3370 st->frag_data = NULL;
3371 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003372 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003373 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003374
3375next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003376 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003377
Thomas Chenault995b3372009-05-18 21:43:27 -07003378 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003379 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003380 return block_limit - abs_offset;
3381 }
3382
3383 if (st->frag_idx == 0 && !st->frag_data)
3384 st->stepped_offset += skb_headlen(st->cur_skb);
3385
3386 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3387 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00003388 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003389
3390 if (abs_offset < block_limit) {
3391 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003392 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07003393
3394 *data = (u8 *) st->frag_data + frag->page_offset +
3395 (abs_offset - st->stepped_offset);
3396
3397 return block_limit - abs_offset;
3398 }
3399
3400 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003401 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003402 st->frag_data = NULL;
3403 }
3404
3405 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003406 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07003407 }
3408
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003409 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003410 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003411 st->frag_data = NULL;
3412 }
3413
David S. Miller21dc3302010-08-23 00:13:46 -07003414 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003415 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003416 st->frag_idx = 0;
3417 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003418 } else if (st->cur_skb->next) {
3419 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003420 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003421 goto next_skb;
3422 }
3423
3424 return 0;
3425}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003426EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003427
3428/**
3429 * skb_abort_seq_read - Abort a sequential read of skb data
3430 * @st: state variable
3431 *
3432 * Must be called if skb_seq_read() was not called until it
3433 * returned 0.
3434 */
3435void skb_abort_seq_read(struct skb_seq_state *st)
3436{
3437 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003438 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003439}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003440EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003441
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003442#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3443
3444static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3445 struct ts_config *conf,
3446 struct ts_state *state)
3447{
3448 return skb_seq_read(offset, text, TS_SKB_CB(state));
3449}
3450
3451static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3452{
3453 skb_abort_seq_read(TS_SKB_CB(state));
3454}
3455
3456/**
3457 * skb_find_text - Find a text pattern in skb data
3458 * @skb: the buffer to look in
3459 * @from: search offset
3460 * @to: search limit
3461 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003462 *
3463 * Finds a pattern in the skb data according to the specified
3464 * textsearch configuration. Use textsearch_next() to retrieve
3465 * subsequent occurrences of the pattern. Returns the offset
3466 * to the first occurrence or UINT_MAX if no match was found.
3467 */
3468unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003469 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003470{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003471 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003472 unsigned int ret;
3473
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003474 config->get_next_block = skb_ts_get_next_block;
3475 config->finish = skb_ts_finish;
3476
Bojan Prtvar059a2442015-02-22 11:46:35 +01003477 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003478
Bojan Prtvar059a2442015-02-22 11:46:35 +01003479 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003480 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003481}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003482EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003483
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003484int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3485 int offset, size_t size)
3486{
3487 int i = skb_shinfo(skb)->nr_frags;
3488
3489 if (skb_can_coalesce(skb, i, page, offset)) {
3490 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3491 } else if (i < MAX_SKB_FRAGS) {
3492 get_page(page);
3493 skb_fill_page_desc(skb, i, page, offset, size);
3494 } else {
3495 return -EMSGSIZE;
3496 }
3497
3498 return 0;
3499}
3500EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3501
Herbert Xucbb042f2006-03-20 22:43:56 -08003502/**
3503 * skb_pull_rcsum - pull skb and update receive checksum
3504 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003505 * @len: length of data pulled
3506 *
3507 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003508 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003509 * receive path processing instead of skb_pull unless you know
3510 * that the checksum difference is zero (e.g., a valid IP header)
3511 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003512 */
Johannes Bergaf728682017-06-16 14:29:22 +02003513void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003514{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003515 unsigned char *data = skb->data;
3516
Herbert Xucbb042f2006-03-20 22:43:56 -08003517 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003518 __skb_pull(skb, len);
3519 skb_postpull_rcsum(skb, data, len);
3520 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003521}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003522EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3523
Yonghong Song13acc942018-03-21 16:31:03 -07003524static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3525{
3526 skb_frag_t head_frag;
3527 struct page *page;
3528
3529 page = virt_to_head_page(frag_skb->head);
3530 head_frag.page.p = page;
3531 head_frag.page_offset = frag_skb->data -
3532 (unsigned char *)page_address(page);
3533 head_frag.size = skb_headlen(frag_skb);
3534 return head_frag;
3535}
3536
Herbert Xuf4c50d92006-06-22 03:02:40 -07003537/**
3538 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003539 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003540 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003541 *
3542 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003543 * a pointer to the first in a list of new skbs for the segments.
3544 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003545 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003546struct sk_buff *skb_segment(struct sk_buff *head_skb,
3547 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003548{
3549 struct sk_buff *segs = NULL;
3550 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003551 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003552 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3553 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3554 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003555 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003556 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003557 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003558 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003559 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003560 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003561 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003562 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003563 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003564 int err = -ENOMEM;
3565 int i = 0;
3566 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003567 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003568
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003569 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003570 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003571 if (unlikely(!proto))
3572 return ERR_PTR(-EINVAL);
3573
Alexander Duyck36c98382016-05-02 09:38:18 -07003574 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003575 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003576
Steffen Klassert07b26c92016-09-19 12:58:47 +02003577 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3578 if (!(features & NETIF_F_GSO_PARTIAL)) {
3579 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003580 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003581
3582 if (!list_skb ||
3583 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3584 goto normal;
3585
Ilan Tayari43170c42017-04-19 21:26:07 +03003586 /* If we get here then all the required
3587 * GSO features except frag_list are supported.
3588 * Try to split the SKB to multiple GSO SKBs
3589 * with no frag_list.
3590 * Currently we can do that only when the buffers don't
3591 * have a linear part and all the buffers except
3592 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003593 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003594 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003595 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003596 if (frag_len != iter->len && iter->next)
3597 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003598 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003599 goto normal;
3600
3601 len -= iter->len;
3602 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003603
3604 if (len != frag_len)
3605 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003606 }
3607
3608 /* GSO partial only requires that we trim off any excess that
3609 * doesn't fit into an MSS sized block, so take care of that
3610 * now.
3611 */
Alexander Duyck802ab552016-04-10 21:45:03 -04003612 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07003613 if (partial_segs > 1)
3614 mss *= partial_segs;
3615 else
3616 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04003617 }
3618
Steffen Klassert07b26c92016-09-19 12:58:47 +02003619normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003620 headroom = skb_headroom(head_skb);
3621 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003622
3623 do {
3624 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003625 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003626 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003627 int size;
3628
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03003629 if (unlikely(mss == GSO_BY_FRAGS)) {
3630 len = list_skb->len;
3631 } else {
3632 len = head_skb->len - offset;
3633 if (len > mss)
3634 len = mss;
3635 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003636
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003637 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003638 if (hsize < 0)
3639 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003640 if (hsize > len || !sg)
3641 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003642
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003643 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3644 (skb_headlen(list_skb) == len || sg)) {
3645 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003646
Herbert Xu9d8506c2013-11-21 11:10:04 -08003647 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003648 nfrags = skb_shinfo(list_skb)->nr_frags;
3649 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003650 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003651 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003652
3653 while (pos < offset + len) {
3654 BUG_ON(i >= nfrags);
3655
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003656 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003657 if (pos + size > offset + len)
3658 break;
3659
3660 i++;
3661 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003662 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003663 }
3664
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003665 nskb = skb_clone(list_skb, GFP_ATOMIC);
3666 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003667
3668 if (unlikely(!nskb))
3669 goto err;
3670
Herbert Xu9d8506c2013-11-21 11:10:04 -08003671 if (unlikely(pskb_trim(nskb, len))) {
3672 kfree_skb(nskb);
3673 goto err;
3674 }
3675
Alexander Duyckec47ea82012-05-04 14:26:56 +00003676 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003677 if (skb_cow_head(nskb, doffset + headroom)) {
3678 kfree_skb(nskb);
3679 goto err;
3680 }
3681
Alexander Duyckec47ea82012-05-04 14:26:56 +00003682 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003683 skb_release_head_state(nskb);
3684 __skb_push(nskb, doffset);
3685 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003686 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003687 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003688 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003689
3690 if (unlikely(!nskb))
3691 goto err;
3692
3693 skb_reserve(nskb, headroom);
3694 __skb_put(nskb, doffset);
3695 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003696
3697 if (segs)
3698 tail->next = nskb;
3699 else
3700 segs = nskb;
3701 tail = nskb;
3702
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003703 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003704
Eric Dumazet030737b2013-10-19 11:42:54 -07003705 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003706 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003707
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003708 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003709 nskb->data - tnl_hlen,
3710 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003711
Herbert Xu9d8506c2013-11-21 11:10:04 -08003712 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003713 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003714
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003715 if (!sg) {
3716 if (!nskb->remcsum_offload)
3717 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003718 SKB_GSO_CB(nskb)->csum =
3719 skb_copy_and_csum_bits(head_skb, offset,
3720 skb_put(nskb, len),
3721 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003722 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003723 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003724 continue;
3725 }
3726
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003727 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003728
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003729 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003730 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003731
Willem de Bruijnfff88032017-06-08 11:35:03 -04003732 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3733 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003734
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003735 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3736 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
3737 goto err;
3738
Herbert Xu9d8506c2013-11-21 11:10:04 -08003739 while (pos < offset + len) {
3740 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08003741 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003742 nfrags = skb_shinfo(list_skb)->nr_frags;
3743 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003744 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07003745 if (!skb_headlen(list_skb)) {
3746 BUG_ON(!nfrags);
3747 } else {
3748 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003749
Yonghong Song13acc942018-03-21 16:31:03 -07003750 /* to make room for head_frag. */
3751 i--;
3752 frag--;
3753 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05003754 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
3755 skb_zerocopy_clone(nskb, frag_skb,
3756 GFP_ATOMIC))
3757 goto err;
3758
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003759 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003760 }
3761
3762 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3763 MAX_SKB_FRAGS)) {
3764 net_warn_ratelimited(
3765 "skb_segment: too many frags: %u %u\n",
3766 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07003767 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003768 goto err;
3769 }
3770
Yonghong Song13acc942018-03-21 16:31:03 -07003771 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003772 __skb_frag_ref(nskb_frag);
3773 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003774
3775 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003776 nskb_frag->page_offset += offset - pos;
3777 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003778 }
3779
Herbert Xu89319d382008-12-15 23:26:06 -08003780 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003781
3782 if (pos + size <= offset + len) {
3783 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003784 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003785 pos += size;
3786 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003787 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003788 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003789 }
3790
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003791 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003792 }
3793
Herbert Xu89319d382008-12-15 23:26:06 -08003794skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003795 nskb->data_len = len - hsize;
3796 nskb->len += nskb->data_len;
3797 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003798
Simon Horman1cdbcb72013-05-19 15:46:49 +00003799perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003800 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07003801 if (skb_has_shared_frag(nskb) &&
3802 __skb_linearize(nskb))
3803 goto err;
3804
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003805 if (!nskb->remcsum_offload)
3806 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003807 SKB_GSO_CB(nskb)->csum =
3808 skb_checksum(nskb, doffset,
3809 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003810 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003811 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003812 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003813 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003814
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003815 /* Some callers want to get the end of the list.
3816 * Put it in segs->prev to avoid walking the list.
3817 * (see validate_xmit_skb_list() for example)
3818 */
3819 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003820
Alexander Duyck802ab552016-04-10 21:45:03 -04003821 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02003822 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04003823 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003824 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04003825
3826 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003827 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04003828 type &= ~SKB_GSO_DODGY;
3829
3830 /* Update GSO info and prepare to start updating headers on
3831 * our way back down the stack of protocols.
3832 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02003833 for (iter = segs; iter; iter = iter->next) {
3834 skb_shinfo(iter)->gso_size = gso_size;
3835 skb_shinfo(iter)->gso_segs = partial_segs;
3836 skb_shinfo(iter)->gso_type = type;
3837 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3838 }
3839
3840 if (tail->len - doffset <= gso_size)
3841 skb_shinfo(tail)->gso_size = 0;
3842 else if (tail != segs)
3843 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04003844 }
3845
Toshiaki Makita432c8562014-10-27 10:30:51 -07003846 /* Following permits correct backpressure, for protocols
3847 * using skb_set_owner_w().
3848 * Idea is to tranfert ownership from head_skb to last segment.
3849 */
3850 if (head_skb->destructor == sock_wfree) {
3851 swap(tail->truesize, head_skb->truesize);
3852 swap(tail->destructor, head_skb->destructor);
3853 swap(tail->sk, head_skb->sk);
3854 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003855 return segs;
3856
3857err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003858 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003859 return ERR_PTR(err);
3860}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003861EXPORT_SYMBOL_GPL(skb_segment);
3862
David Millerd4546c22018-06-24 14:13:49 +09003863int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08003864{
Eric Dumazet8a291112013-10-08 09:02:23 -07003865 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003866 unsigned int offset = skb_gro_offset(skb);
3867 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003868 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003869 unsigned int delta_truesize;
David Millerd4546c22018-06-24 14:13:49 +09003870 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08003871
Steffen Klassert0ab03f32019-04-02 08:16:03 +02003872 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08003873 return -E2BIG;
3874
Eric Dumazet29e98242014-05-16 11:34:37 -07003875 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003876 pinfo = skb_shinfo(lp);
3877
3878 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003879 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003880 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003881 int i = skbinfo->nr_frags;
3882 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003883
Herbert Xu66e92fc2009-05-26 18:50:32 +00003884 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003885 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003886
Eric Dumazet8a291112013-10-08 09:02:23 -07003887 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003888 pinfo->nr_frags = nr_frags;
3889 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003890
Herbert Xu9aaa1562009-05-26 18:50:33 +00003891 frag = pinfo->frags + nr_frags;
3892 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003893 do {
3894 *--frag = *--frag2;
3895 } while (--i);
3896
3897 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003898 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003899
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003900 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003901 delta_truesize = skb->truesize -
3902 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003903
Herbert Xuf5572062009-01-14 20:40:03 -08003904 skb->truesize -= skb->data_len;
3905 skb->len -= skb->data_len;
3906 skb->data_len = 0;
3907
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003908 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003909 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003910 } else if (skb->head_frag) {
3911 int nr_frags = pinfo->nr_frags;
3912 skb_frag_t *frag = pinfo->frags + nr_frags;
3913 struct page *page = virt_to_head_page(skb->head);
3914 unsigned int first_size = headlen - offset;
3915 unsigned int first_offset;
3916
3917 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003918 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003919
3920 first_offset = skb->data -
3921 (unsigned char *)page_address(page) +
3922 offset;
3923
3924 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3925
3926 frag->page.p = page;
3927 frag->page_offset = first_offset;
3928 skb_frag_size_set(frag, first_size);
3929
3930 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3931 /* We dont need to clear skbinfo->nr_frags here */
3932
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003933 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003934 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3935 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003936 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003937
3938merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003939 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003940 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003941 unsigned int eat = offset - headlen;
3942
3943 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003944 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003945 skb->data_len -= eat;
3946 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003947 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003948 }
3949
Herbert Xu67147ba2009-05-26 18:50:22 +00003950 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003951
Eric Dumazet29e98242014-05-16 11:34:37 -07003952 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003953 skb_shinfo(p)->frag_list = skb;
3954 else
3955 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003956 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003957 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003958 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003959
Herbert Xu5d38a072009-01-04 16:13:40 -08003960done:
3961 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003962 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003963 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003964 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003965 if (lp != p) {
3966 lp->data_len += len;
3967 lp->truesize += delta_truesize;
3968 lp->len += len;
3969 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003970 NAPI_GRO_CB(skb)->same_flow = 1;
3971 return 0;
3972}
Marcelo Ricardo Leitner57c056502016-06-02 15:05:39 -03003973EXPORT_SYMBOL_GPL(skb_gro_receive);
Herbert Xu71d93b32008-12-15 23:42:33 -08003974
Florian Westphaldf5042f2018-12-18 17:15:16 +01003975#ifdef CONFIG_SKB_EXTENSIONS
3976#define SKB_EXT_ALIGN_VALUE 8
3977#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3978
3979static const u8 skb_ext_type_len[] = {
3980#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3981 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
3982#endif
Florian Westphal41650792018-12-18 17:15:27 +01003983#ifdef CONFIG_XFRM
3984 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
3985#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01003986};
3987
3988static __always_inline unsigned int skb_ext_total_length(void)
3989{
3990 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
3991#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3992 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
3993#endif
Florian Westphal41650792018-12-18 17:15:27 +01003994#ifdef CONFIG_XFRM
3995 skb_ext_type_len[SKB_EXT_SEC_PATH] +
3996#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01003997 0;
3998}
3999
4000static void skb_extensions_init(void)
4001{
4002 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4003 BUILD_BUG_ON(skb_ext_total_length() > 255);
4004
4005 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4006 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4007 0,
4008 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4009 NULL);
4010}
4011#else
4012static void skb_extensions_init(void) {}
4013#endif
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015void __init skb_init(void)
4016{
Kees Cook79a8a642018-02-07 17:44:38 -08004017 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 sizeof(struct sk_buff),
4019 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004020 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004021 offsetof(struct sk_buff, cb),
4022 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004023 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004024 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004025 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004026 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004027 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004028 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004029 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
David S. Miller51c739d2007-10-30 21:29:29 -07004032static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004033__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4034 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004035{
David S. Miller1a028e52007-04-27 15:21:23 -07004036 int start = skb_headlen(skb);
4037 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004038 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004039 int elt = 0;
4040
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004041 if (unlikely(recursion_level >= 24))
4042 return -EMSGSIZE;
4043
David Howells716ea3a2007-04-02 20:19:53 -07004044 if (copy > 0) {
4045 if (copy > len)
4046 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004047 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004048 elt++;
4049 if ((len -= copy) == 0)
4050 return elt;
4051 offset += copy;
4052 }
4053
4054 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004055 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004056
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004057 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004058
Eric Dumazet9e903e02011-10-18 21:00:24 +00004059 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004060 if ((copy = end - offset) > 0) {
4061 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004062 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4063 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004064
4065 if (copy > len)
4066 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004067 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02004068 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07004069 elt++;
4070 if (!(len -= copy))
4071 return elt;
4072 offset += copy;
4073 }
David S. Miller1a028e52007-04-27 15:21:23 -07004074 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004075 }
4076
David S. Millerfbb398a2009-06-09 00:18:59 -07004077 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004078 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004079
David S. Millerfbb398a2009-06-09 00:18:59 -07004080 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004081
David S. Millerfbb398a2009-06-09 00:18:59 -07004082 end = start + frag_iter->len;
4083 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004084 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4085 return -EMSGSIZE;
4086
David S. Millerfbb398a2009-06-09 00:18:59 -07004087 if (copy > len)
4088 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004089 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4090 copy, recursion_level + 1);
4091 if (unlikely(ret < 0))
4092 return ret;
4093 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004094 if ((len -= copy) == 0)
4095 return elt;
4096 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004097 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004098 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004099 }
4100 BUG_ON(len);
4101 return elt;
4102}
4103
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004104/**
4105 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4106 * @skb: Socket buffer containing the buffers to be mapped
4107 * @sg: The scatter-gather list to map into
4108 * @offset: The offset into the buffer's contents to start mapping
4109 * @len: Length of buffer space to be mapped
4110 *
4111 * Fill the specified scatter-gather list with mappings/pointers into a
4112 * region of the buffer space attached to a socket buffer. Returns either
4113 * the number of scatterlist items used, or -EMSGSIZE if the contents
4114 * could not fit.
4115 */
4116int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4117{
4118 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4119
4120 if (nsg <= 0)
4121 return nsg;
4122
4123 sg_mark_end(&sg[nsg - 1]);
4124
4125 return nsg;
4126}
4127EXPORT_SYMBOL_GPL(skb_to_sgvec);
4128
Fan Du25a91d82014-01-18 09:54:23 +08004129/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4130 * sglist without mark the sg which contain last skb data as the end.
4131 * So the caller can mannipulate sg list as will when padding new data after
4132 * the first call without calling sg_unmark_end to expend sg list.
4133 *
4134 * Scenario to use skb_to_sgvec_nomark:
4135 * 1. sg_init_table
4136 * 2. skb_to_sgvec_nomark(payload1)
4137 * 3. skb_to_sgvec_nomark(payload2)
4138 *
4139 * This is equivalent to:
4140 * 1. sg_init_table
4141 * 2. skb_to_sgvec(payload1)
4142 * 3. sg_unmark_end
4143 * 4. skb_to_sgvec(payload2)
4144 *
4145 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4146 * is more preferable.
4147 */
4148int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4149 int offset, int len)
4150{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004151 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004152}
4153EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4154
David S. Miller51c739d2007-10-30 21:29:29 -07004155
David S. Miller51c739d2007-10-30 21:29:29 -07004156
David Howells716ea3a2007-04-02 20:19:53 -07004157/**
4158 * skb_cow_data - Check that a socket buffer's data buffers are writable
4159 * @skb: The socket buffer to check.
4160 * @tailbits: Amount of trailing space to be added
4161 * @trailer: Returned pointer to the skb where the @tailbits space begins
4162 *
4163 * Make sure that the data buffers attached to a socket buffer are
4164 * writable. If they are not, private copies are made of the data buffers
4165 * and the socket buffer is set to use these instead.
4166 *
4167 * If @tailbits is given, make sure that there is space to write @tailbits
4168 * bytes of data beyond current end of socket buffer. @trailer will be
4169 * set to point to the skb in which this space begins.
4170 *
4171 * The number of scatterlist elements required to completely map the
4172 * COW'd and extended socket buffer will be returned.
4173 */
4174int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4175{
4176 int copyflag;
4177 int elt;
4178 struct sk_buff *skb1, **skb_p;
4179
4180 /* If skb is cloned or its head is paged, reallocate
4181 * head pulling out all the pages (pages are considered not writable
4182 * at the moment even if they are anonymous).
4183 */
4184 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4185 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
4186 return -ENOMEM;
4187
4188 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004189 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004190 /* A little of trouble, not enough of space for trailer.
4191 * This should not happen, when stack is tuned to generate
4192 * good frames. OK, on miss we reallocate and reserve even more
4193 * space, 128 bytes is fair. */
4194
4195 if (skb_tailroom(skb) < tailbits &&
4196 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4197 return -ENOMEM;
4198
4199 /* Voila! */
4200 *trailer = skb;
4201 return 1;
4202 }
4203
4204 /* Misery. We are in troubles, going to mincer fragments... */
4205
4206 elt = 1;
4207 skb_p = &skb_shinfo(skb)->frag_list;
4208 copyflag = 0;
4209
4210 while ((skb1 = *skb_p) != NULL) {
4211 int ntail = 0;
4212
4213 /* The fragment is partially pulled by someone,
4214 * this can happen on input. Copy it and everything
4215 * after it. */
4216
4217 if (skb_shared(skb1))
4218 copyflag = 1;
4219
4220 /* If the skb is the last, worry about trailer. */
4221
4222 if (skb1->next == NULL && tailbits) {
4223 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004224 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004225 skb_tailroom(skb1) < tailbits)
4226 ntail = tailbits + 128;
4227 }
4228
4229 if (copyflag ||
4230 skb_cloned(skb1) ||
4231 ntail ||
4232 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004233 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004234 struct sk_buff *skb2;
4235
4236 /* Fuck, we are miserable poor guys... */
4237 if (ntail == 0)
4238 skb2 = skb_copy(skb1, GFP_ATOMIC);
4239 else
4240 skb2 = skb_copy_expand(skb1,
4241 skb_headroom(skb1),
4242 ntail,
4243 GFP_ATOMIC);
4244 if (unlikely(skb2 == NULL))
4245 return -ENOMEM;
4246
4247 if (skb1->sk)
4248 skb_set_owner_w(skb2, skb1->sk);
4249
4250 /* Looking around. Are we still alive?
4251 * OK, link new skb, drop old one */
4252
4253 skb2->next = skb1->next;
4254 *skb_p = skb2;
4255 kfree_skb(skb1);
4256 skb1 = skb2;
4257 }
4258 elt++;
4259 *trailer = skb1;
4260 skb_p = &skb1->next;
4261 }
4262
4263 return elt;
4264}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004265EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004266
Eric Dumazetb1faf562010-05-31 23:44:05 -07004267static void sock_rmem_free(struct sk_buff *skb)
4268{
4269 struct sock *sk = skb->sk;
4270
4271 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4272}
4273
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004274static void skb_set_err_queue(struct sk_buff *skb)
4275{
4276 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4277 * So, it is safe to (mis)use it to mark skbs on the error queue.
4278 */
4279 skb->pkt_type = PACKET_OUTGOING;
4280 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4281}
4282
Eric Dumazetb1faf562010-05-31 23:44:05 -07004283/*
4284 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4285 */
4286int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4287{
4288 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00004289 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07004290 return -ENOMEM;
4291
4292 skb_orphan(skb);
4293 skb->sk = sk;
4294 skb->destructor = sock_rmem_free;
4295 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004296 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004297
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004298 /* before exiting rcu section, make sure dst is refcounted */
4299 skb_dst_force(skb);
4300
Eric Dumazetb1faf562010-05-31 23:44:05 -07004301 skb_queue_tail(&sk->sk_error_queue, skb);
4302 if (!sock_flag(sk, SOCK_DEAD))
Vinicius Costa Gomes6e5d58f2018-03-14 13:32:09 -07004303 sk->sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004304 return 0;
4305}
4306EXPORT_SYMBOL(sock_queue_err_skb);
4307
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004308static bool is_icmp_err_skb(const struct sk_buff *skb)
4309{
4310 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4311 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4312}
4313
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004314struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4315{
4316 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004317 struct sk_buff *skb, *skb_next = NULL;
4318 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004319 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004320
Eric Dumazet997d5c32015-02-18 05:47:55 -08004321 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004322 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004323 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004324 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004325 if (icmp_next)
4326 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
4327 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004328 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004329
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004330 if (is_icmp_err_skb(skb) && !icmp_next)
4331 sk->sk_err = 0;
4332
4333 if (skb_next)
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004334 sk->sk_error_report(sk);
4335
4336 return skb;
4337}
4338EXPORT_SYMBOL(sock_dequeue_err_skb);
4339
Alexander Duyckcab41c42014-09-10 18:05:26 -04004340/**
4341 * skb_clone_sk - create clone of skb, and take reference to socket
4342 * @skb: the skb to clone
4343 *
4344 * This function creates a clone of a buffer that holds a reference on
4345 * sk_refcnt. Buffers created via this function are meant to be
4346 * returned using sock_queue_err_skb, or free via kfree_skb.
4347 *
4348 * When passing buffers allocated with this function to sock_queue_err_skb
4349 * it is necessary to wrap the call with sock_hold/sock_put in order to
4350 * prevent the socket from being released prior to being enqueued on
4351 * the sk_error_queue.
4352 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004353struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4354{
4355 struct sock *sk = skb->sk;
4356 struct sk_buff *clone;
4357
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004358 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004359 return NULL;
4360
4361 clone = skb_clone(skb, GFP_ATOMIC);
4362 if (!clone) {
4363 sock_put(sk);
4364 return NULL;
4365 }
4366
4367 clone->sk = sk;
4368 clone->destructor = sock_efree;
4369
4370 return clone;
4371}
4372EXPORT_SYMBOL(skb_clone_sk);
4373
Alexander Duyck37846ef2014-09-04 13:31:10 -04004374static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4375 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004376 int tstype,
4377 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004378{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004379 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004380 int err;
4381
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004382 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4383
Patrick Ohlyac45f602009-02-12 05:03:37 +00004384 serr = SKB_EXT_ERR(skb);
4385 memset(serr, 0, sizeof(*serr));
4386 serr->ee.ee_errno = ENOMSG;
4387 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004388 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004389 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004390 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004391 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004392 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004393 if (sk->sk_protocol == IPPROTO_TCP &&
4394 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004395 serr->ee.ee_data -= sk->sk_tskey;
4396 }
Eric Dumazet29030372010-05-29 00:20:48 -07004397
Patrick Ohlyac45f602009-02-12 05:03:37 +00004398 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004399
Patrick Ohlyac45f602009-02-12 05:03:37 +00004400 if (err)
4401 kfree_skb(skb);
4402}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004403
Willem de Bruijnb245be12015-01-30 13:29:32 -05004404static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4405{
4406 bool ret;
4407
4408 if (likely(sysctl_tstamp_allow_data || tsonly))
4409 return true;
4410
4411 read_lock_bh(&sk->sk_callback_lock);
4412 ret = sk->sk_socket && sk->sk_socket->file &&
4413 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4414 read_unlock_bh(&sk->sk_callback_lock);
4415 return ret;
4416}
4417
Alexander Duyck37846ef2014-09-04 13:31:10 -04004418void skb_complete_tx_timestamp(struct sk_buff *skb,
4419 struct skb_shared_hwtstamps *hwtstamps)
4420{
4421 struct sock *sk = skb->sk;
4422
Willem de Bruijnb245be12015-01-30 13:29:32 -05004423 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004424 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004425
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004426 /* Take a reference to prevent skb_orphan() from freeing the socket,
4427 * but only if the socket refcount is not zero.
4428 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004429 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004430 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004431 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004432 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004433 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004434 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004435
4436err:
4437 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004438}
4439EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4440
4441void __skb_tstamp_tx(struct sk_buff *orig_skb,
4442 struct skb_shared_hwtstamps *hwtstamps,
4443 struct sock *sk, int tstype)
4444{
4445 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004446 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004447
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004448 if (!sk)
4449 return;
4450
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004451 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4452 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4453 return;
4454
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004455 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4456 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004457 return;
4458
Francis Yan1c885802016-11-27 23:07:18 -08004459 if (tsonly) {
4460#ifdef CONFIG_INET
4461 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4462 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004463 sk->sk_type == SOCK_STREAM) {
Francis Yan1c885802016-11-27 23:07:18 -08004464 skb = tcp_get_timestamping_opt_stats(sk);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004465 opt_stats = true;
4466 } else
Francis Yan1c885802016-11-27 23:07:18 -08004467#endif
4468 skb = alloc_skb(0, GFP_ATOMIC);
4469 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004470 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004471 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004472 if (!skb)
4473 return;
4474
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004475 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004476 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4477 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004478 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4479 }
4480
4481 if (hwtstamps)
4482 *skb_hwtstamps(skb) = *hwtstamps;
4483 else
4484 skb->tstamp = ktime_get_real();
4485
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004486 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004487}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004488EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4489
4490void skb_tstamp_tx(struct sk_buff *orig_skb,
4491 struct skb_shared_hwtstamps *hwtstamps)
4492{
4493 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
4494 SCM_TSTAMP_SND);
4495}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004496EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4497
Johannes Berg6e3e9392011-11-09 10:15:42 +01004498void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4499{
4500 struct sock *sk = skb->sk;
4501 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004502 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004503
4504 skb->wifi_acked_valid = 1;
4505 skb->wifi_acked = acked;
4506
4507 serr = SKB_EXT_ERR(skb);
4508 memset(serr, 0, sizeof(*serr));
4509 serr->ee.ee_errno = ENOMSG;
4510 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4511
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004512 /* Take a reference to prevent skb_orphan() from freeing the socket,
4513 * but only if the socket refcount is not zero.
4514 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004515 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004516 err = sock_queue_err_skb(sk, skb);
4517 sock_put(sk);
4518 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004519 if (err)
4520 kfree_skb(skb);
4521}
4522EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4523
Rusty Russellf35d9d82008-02-04 23:49:54 -05004524/**
4525 * skb_partial_csum_set - set up and verify partial csum values for packet
4526 * @skb: the skb to set
4527 * @start: the number of bytes after skb->data to start checksumming.
4528 * @off: the offset from start to place the checksum.
4529 *
4530 * For untrusted partially-checksummed packets, we need to make sure the values
4531 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4532 *
4533 * This function checks and sets those values and skb->ip_summed: if this
4534 * returns false you should drop the packet.
4535 */
4536bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4537{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004538 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4539 u32 csum_start = skb_headroom(skb) + (u32)start;
4540
4541 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4542 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4543 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004544 return false;
4545 }
4546 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004547 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004548 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004549 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004550 return true;
4551}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004552EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004553
Paul Durranted1f50c2014-01-09 10:02:46 +00004554static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4555 unsigned int max)
4556{
4557 if (skb_headlen(skb) >= len)
4558 return 0;
4559
4560 /* If we need to pullup then pullup to the max, so we
4561 * won't need to do it again.
4562 */
4563 if (max > skb->len)
4564 max = skb->len;
4565
4566 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4567 return -ENOMEM;
4568
4569 if (skb_headlen(skb) < len)
4570 return -EPROTO;
4571
4572 return 0;
4573}
4574
Jan Beulichf9708b42014-03-11 13:56:05 +00004575#define MAX_TCP_HDR_LEN (15 * 4)
4576
4577static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4578 typeof(IPPROTO_IP) proto,
4579 unsigned int off)
4580{
4581 switch (proto) {
4582 int err;
4583
4584 case IPPROTO_TCP:
4585 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4586 off + MAX_TCP_HDR_LEN);
4587 if (!err && !skb_partial_csum_set(skb, off,
4588 offsetof(struct tcphdr,
4589 check)))
4590 err = -EPROTO;
4591 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4592
4593 case IPPROTO_UDP:
4594 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4595 off + sizeof(struct udphdr));
4596 if (!err && !skb_partial_csum_set(skb, off,
4597 offsetof(struct udphdr,
4598 check)))
4599 err = -EPROTO;
4600 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4601 }
4602
4603 return ERR_PTR(-EPROTO);
4604}
4605
Paul Durranted1f50c2014-01-09 10:02:46 +00004606/* This value should be large enough to cover a tagged ethernet header plus
4607 * maximally sized IP and TCP or UDP headers.
4608 */
4609#define MAX_IP_HDR_LEN 128
4610
Jan Beulichf9708b42014-03-11 13:56:05 +00004611static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00004612{
4613 unsigned int off;
4614 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00004615 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004616 int err;
4617
4618 fragment = false;
4619
4620 err = skb_maybe_pull_tail(skb,
4621 sizeof(struct iphdr),
4622 MAX_IP_HDR_LEN);
4623 if (err < 0)
4624 goto out;
4625
4626 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4627 fragment = true;
4628
4629 off = ip_hdrlen(skb);
4630
4631 err = -EPROTO;
4632
4633 if (fragment)
4634 goto out;
4635
Jan Beulichf9708b42014-03-11 13:56:05 +00004636 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4637 if (IS_ERR(csum))
4638 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004639
Jan Beulichf9708b42014-03-11 13:56:05 +00004640 if (recalculate)
4641 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4642 ip_hdr(skb)->daddr,
4643 skb->len - off,
4644 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004645 err = 0;
4646
4647out:
4648 return err;
4649}
4650
4651/* This value should be large enough to cover a tagged ethernet header plus
4652 * an IPv6 header, all options, and a maximal TCP or UDP header.
4653 */
4654#define MAX_IPV6_HDR_LEN 256
4655
4656#define OPT_HDR(type, skb, off) \
4657 (type *)(skb_network_header(skb) + (off))
4658
4659static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4660{
4661 int err;
4662 u8 nexthdr;
4663 unsigned int off;
4664 unsigned int len;
4665 bool fragment;
4666 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00004667 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00004668
4669 fragment = false;
4670 done = false;
4671
4672 off = sizeof(struct ipv6hdr);
4673
4674 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4675 if (err < 0)
4676 goto out;
4677
4678 nexthdr = ipv6_hdr(skb)->nexthdr;
4679
4680 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4681 while (off <= len && !done) {
4682 switch (nexthdr) {
4683 case IPPROTO_DSTOPTS:
4684 case IPPROTO_HOPOPTS:
4685 case IPPROTO_ROUTING: {
4686 struct ipv6_opt_hdr *hp;
4687
4688 err = skb_maybe_pull_tail(skb,
4689 off +
4690 sizeof(struct ipv6_opt_hdr),
4691 MAX_IPV6_HDR_LEN);
4692 if (err < 0)
4693 goto out;
4694
4695 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4696 nexthdr = hp->nexthdr;
4697 off += ipv6_optlen(hp);
4698 break;
4699 }
4700 case IPPROTO_AH: {
4701 struct ip_auth_hdr *hp;
4702
4703 err = skb_maybe_pull_tail(skb,
4704 off +
4705 sizeof(struct ip_auth_hdr),
4706 MAX_IPV6_HDR_LEN);
4707 if (err < 0)
4708 goto out;
4709
4710 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4711 nexthdr = hp->nexthdr;
4712 off += ipv6_authlen(hp);
4713 break;
4714 }
4715 case IPPROTO_FRAGMENT: {
4716 struct frag_hdr *hp;
4717
4718 err = skb_maybe_pull_tail(skb,
4719 off +
4720 sizeof(struct frag_hdr),
4721 MAX_IPV6_HDR_LEN);
4722 if (err < 0)
4723 goto out;
4724
4725 hp = OPT_HDR(struct frag_hdr, skb, off);
4726
4727 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4728 fragment = true;
4729
4730 nexthdr = hp->nexthdr;
4731 off += sizeof(struct frag_hdr);
4732 break;
4733 }
4734 default:
4735 done = true;
4736 break;
4737 }
4738 }
4739
4740 err = -EPROTO;
4741
4742 if (!done || fragment)
4743 goto out;
4744
Jan Beulichf9708b42014-03-11 13:56:05 +00004745 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4746 if (IS_ERR(csum))
4747 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004748
Jan Beulichf9708b42014-03-11 13:56:05 +00004749 if (recalculate)
4750 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4751 &ipv6_hdr(skb)->daddr,
4752 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004753 err = 0;
4754
4755out:
4756 return err;
4757}
4758
4759/**
4760 * skb_checksum_setup - set up partial checksum offset
4761 * @skb: the skb to set up
4762 * @recalculate: if true the pseudo-header checksum will be recalculated
4763 */
4764int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4765{
4766 int err;
4767
4768 switch (skb->protocol) {
4769 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004770 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004771 break;
4772
4773 case htons(ETH_P_IPV6):
4774 err = skb_checksum_setup_ipv6(skb, recalculate);
4775 break;
4776
4777 default:
4778 err = -EPROTO;
4779 break;
4780 }
4781
4782 return err;
4783}
4784EXPORT_SYMBOL(skb_checksum_setup);
4785
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004786/**
4787 * skb_checksum_maybe_trim - maybe trims the given skb
4788 * @skb: the skb to check
4789 * @transport_len: the data length beyond the network header
4790 *
4791 * Checks whether the given skb has data beyond the given transport length.
4792 * If so, returns a cloned skb trimmed to this transport length.
4793 * Otherwise returns the provided skb. Returns NULL in error cases
4794 * (e.g. transport_len exceeds skb length or out-of-memory).
4795 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004796 * Caller needs to set the skb transport header and free any returned skb if it
4797 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004798 */
4799static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4800 unsigned int transport_len)
4801{
4802 struct sk_buff *skb_chk;
4803 unsigned int len = skb_transport_offset(skb) + transport_len;
4804 int ret;
4805
Linus Lüssinga5169932015-08-13 05:54:07 +02004806 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004807 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004808 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004809 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004810
4811 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004812 if (!skb_chk)
4813 return NULL;
4814
4815 ret = pskb_trim_rcsum(skb_chk, len);
4816 if (ret) {
4817 kfree_skb(skb_chk);
4818 return NULL;
4819 }
4820
4821 return skb_chk;
4822}
4823
4824/**
4825 * skb_checksum_trimmed - validate checksum of an skb
4826 * @skb: the skb to check
4827 * @transport_len: the data length beyond the network header
4828 * @skb_chkf: checksum function to use
4829 *
4830 * Applies the given checksum function skb_chkf to the provided skb.
4831 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4832 *
4833 * If the skb has data beyond the given transport length, then a
4834 * trimmed & cloned skb is checked and returned.
4835 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004836 * Caller needs to set the skb transport header and free any returned skb if it
4837 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004838 */
4839struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4840 unsigned int transport_len,
4841 __sum16(*skb_chkf)(struct sk_buff *skb))
4842{
4843 struct sk_buff *skb_chk;
4844 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004845 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004846
4847 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4848 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004849 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004850
Linus Lüssinga5169932015-08-13 05:54:07 +02004851 if (!pskb_may_pull(skb_chk, offset))
4852 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004853
Linus Lüssing9b368812016-02-24 04:21:42 +01004854 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004855 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004856 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004857
Linus Lüssinga5169932015-08-13 05:54:07 +02004858 if (ret)
4859 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004860
4861 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004862
4863err:
4864 if (skb_chk && skb_chk != skb)
4865 kfree_skb(skb_chk);
4866
4867 return NULL;
4868
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004869}
4870EXPORT_SYMBOL(skb_checksum_trimmed);
4871
Ben Hutchings4497b072008-06-19 16:22:28 -07004872void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4873{
Joe Perchese87cc472012-05-13 21:56:26 +00004874 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4875 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004876}
Ben Hutchings4497b072008-06-19 16:22:28 -07004877EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004878
4879void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4880{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004881 if (head_stolen) {
4882 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004883 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004884 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004885 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004886 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004887}
4888EXPORT_SYMBOL(kfree_skb_partial);
4889
4890/**
4891 * skb_try_coalesce - try to merge skb to prior one
4892 * @to: prior buffer
4893 * @from: buffer to add
4894 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004895 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004896 */
4897bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4898 bool *fragstolen, int *delta_truesize)
4899{
Eric Dumazetc818fa92017-10-04 10:48:35 -07004900 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004901 int i, delta, len = from->len;
4902
4903 *fragstolen = false;
4904
4905 if (skb_cloned(to))
4906 return false;
4907
4908 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004909 if (len)
4910 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004911 *delta_truesize = 0;
4912 return true;
4913 }
4914
Eric Dumazetc818fa92017-10-04 10:48:35 -07004915 to_shinfo = skb_shinfo(to);
4916 from_shinfo = skb_shinfo(from);
4917 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004918 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04004919 if (skb_zcopy(to) || skb_zcopy(from))
4920 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004921
4922 if (skb_headlen(from) != 0) {
4923 struct page *page;
4924 unsigned int offset;
4925
Eric Dumazetc818fa92017-10-04 10:48:35 -07004926 if (to_shinfo->nr_frags +
4927 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004928 return false;
4929
4930 if (skb_head_is_locked(from))
4931 return false;
4932
4933 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4934
4935 page = virt_to_head_page(from->head);
4936 offset = from->data - (unsigned char *)page_address(page);
4937
Eric Dumazetc818fa92017-10-04 10:48:35 -07004938 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004939 page, offset, skb_headlen(from));
4940 *fragstolen = true;
4941 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07004942 if (to_shinfo->nr_frags +
4943 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004944 return false;
4945
Weiping Panf4b549a2012-09-28 20:15:30 +00004946 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004947 }
4948
4949 WARN_ON_ONCE(delta < len);
4950
Eric Dumazetc818fa92017-10-04 10:48:35 -07004951 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
4952 from_shinfo->frags,
4953 from_shinfo->nr_frags * sizeof(skb_frag_t));
4954 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004955
4956 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07004957 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004958
Li RongQing8ea853f2012-09-18 16:53:21 +00004959 /* if the skb is not cloned this does nothing
4960 * since we set nr_frags to 0.
4961 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07004962 for (i = 0; i < from_shinfo->nr_frags; i++)
4963 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004964
4965 to->truesize += delta;
4966 to->len += len;
4967 to->data_len += len;
4968
4969 *delta_truesize = delta;
4970 return true;
4971}
4972EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004973
4974/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004975 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004976 *
4977 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004978 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004979 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004980 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4981 * into/from a tunnel. Some information have to be cleared during these
4982 * operations.
4983 * skb_scrub_packet can also be used to clean a skb before injecting it in
4984 * another namespace (@xnet == true). We have to clear all information in the
4985 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004986 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004987void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004988{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004989 skb->pkt_type = PACKET_HOST;
4990 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004991 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004992 skb_dst_drop(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004993 secpath_reset(skb);
4994 nf_reset(skb);
4995 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004996
Petr Machata6f9a5062018-11-19 16:11:07 +00004997#ifdef CONFIG_NET_SWITCHDEV
4998 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00004999 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005000#endif
5001
Herbert Xu213dd742015-04-16 09:03:27 +08005002 if (!xnet)
5003 return;
5004
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005005 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005006 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005007 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005008}
5009EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005010
5011/**
5012 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5013 *
5014 * @skb: GSO skb
5015 *
5016 * skb_gso_transport_seglen is used to determine the real size of the
5017 * individual segments, including Layer4 headers (TCP/UDP).
5018 *
5019 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5020 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005021static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005022{
5023 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005024 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005025
Florian Westphalf993bc22014-10-20 13:49:18 +02005026 if (skb->encapsulation) {
5027 thlen = skb_inner_transport_header(skb) -
5028 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005029
Florian Westphalf993bc22014-10-20 13:49:18 +02005030 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5031 thlen += inner_tcp_hdrlen(skb);
5032 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5033 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005034 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005035 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005036 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5037 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005038 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005039 /* UFO sets gso_size to the size of the fragmentation
5040 * payload, i.e. the size of the L4 (UDP) header is already
5041 * accounted for.
5042 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005043 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005044}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005045
5046/**
5047 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5048 *
5049 * @skb: GSO skb
5050 *
5051 * skb_gso_network_seglen is used to determine the real size of the
5052 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5053 *
5054 * The MAC/L2 header is not accounted for.
5055 */
5056static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5057{
5058 unsigned int hdr_len = skb_transport_header(skb) -
5059 skb_network_header(skb);
5060
5061 return hdr_len + skb_gso_transport_seglen(skb);
5062}
5063
5064/**
5065 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5066 *
5067 * @skb: GSO skb
5068 *
5069 * skb_gso_mac_seglen is used to determine the real size of the
5070 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5071 * headers (TCP/UDP).
5072 */
5073static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5074{
5075 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5076
5077 return hdr_len + skb_gso_transport_seglen(skb);
5078}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005079
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005080/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005081 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5082 *
5083 * There are a couple of instances where we have a GSO skb, and we
5084 * want to determine what size it would be after it is segmented.
5085 *
5086 * We might want to check:
5087 * - L3+L4+payload size (e.g. IP forwarding)
5088 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5089 *
5090 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5091 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005092 * @skb: GSO skb
5093 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005094 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5095 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5096 *
5097 * @max_len: The maximum permissible length.
5098 *
5099 * Returns true if the segmented length <= max length.
5100 */
5101static inline bool skb_gso_size_check(const struct sk_buff *skb,
5102 unsigned int seg_len,
5103 unsigned int max_len) {
5104 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5105 const struct sk_buff *iter;
5106
5107 if (shinfo->gso_size != GSO_BY_FRAGS)
5108 return seg_len <= max_len;
5109
5110 /* Undo this so we can re-use header sizes */
5111 seg_len -= GSO_BY_FRAGS;
5112
5113 skb_walk_frags(skb, iter) {
5114 if (seg_len + skb_headlen(iter) > max_len)
5115 return false;
5116 }
5117
5118 return true;
5119}
5120
5121/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005122 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005123 *
5124 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005125 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005126 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005127 * skb_gso_validate_network_len validates if a given skb will fit a
5128 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5129 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005130 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005131bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005132{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005133 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005134}
Daniel Axtens779b7932018-03-01 17:13:37 +11005135EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005136
Daniel Axtens2b16f042018-01-31 14:15:33 +11005137/**
5138 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5139 *
5140 * @skb: GSO skb
5141 * @len: length to validate against
5142 *
5143 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5144 * length once split, including L2, L3 and L4 headers and the payload.
5145 */
5146bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5147{
5148 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5149}
5150EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5151
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005152static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5153{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005154 int mac_len, meta_len;
5155 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005156
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005157 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5158 kfree_skb(skb);
5159 return NULL;
5160 }
5161
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005162 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005163 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5164 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5165 mac_len - VLAN_HLEN - ETH_TLEN);
5166 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005167
5168 meta_len = skb_metadata_len(skb);
5169 if (meta_len) {
5170 meta = skb_metadata_end(skb) - meta_len;
5171 memmove(meta + VLAN_HLEN, meta, meta_len);
5172 }
5173
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005174 skb->mac_header += VLAN_HLEN;
5175 return skb;
5176}
5177
5178struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5179{
5180 struct vlan_hdr *vhdr;
5181 u16 vlan_tci;
5182
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005183 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005184 /* vlan_tci is already set-up so leave this for another time */
5185 return skb;
5186 }
5187
5188 skb = skb_share_check(skb, GFP_ATOMIC);
5189 if (unlikely(!skb))
5190 goto err_free;
5191
5192 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
5193 goto err_free;
5194
5195 vhdr = (struct vlan_hdr *)skb->data;
5196 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5197 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5198
5199 skb_pull_rcsum(skb, VLAN_HLEN);
5200 vlan_set_encap_proto(skb, vhdr);
5201
5202 skb = skb_reorder_vlan_header(skb);
5203 if (unlikely(!skb))
5204 goto err_free;
5205
5206 skb_reset_network_header(skb);
5207 skb_reset_transport_header(skb);
5208 skb_reset_mac_len(skb);
5209
5210 return skb;
5211
5212err_free:
5213 kfree_skb(skb);
5214 return NULL;
5215}
5216EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005217
Jiri Pirkoe2195122014-11-19 14:05:01 +01005218int skb_ensure_writable(struct sk_buff *skb, int write_len)
5219{
5220 if (!pskb_may_pull(skb, write_len))
5221 return -ENOMEM;
5222
5223 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5224 return 0;
5225
5226 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5227}
5228EXPORT_SYMBOL(skb_ensure_writable);
5229
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005230/* remove VLAN header from packet and update csum accordingly.
5231 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5232 */
5233int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005234{
5235 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005236 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005237 int err;
5238
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005239 if (WARN_ONCE(offset,
5240 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5241 offset)) {
5242 return -EINVAL;
5243 }
5244
Jiri Pirko93515d52014-11-19 14:05:02 +01005245 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5246 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005247 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005248
5249 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5250
5251 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5252 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5253
5254 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5255 __skb_pull(skb, VLAN_HLEN);
5256
5257 vlan_set_encap_proto(skb, vhdr);
5258 skb->mac_header += VLAN_HLEN;
5259
5260 if (skb_network_offset(skb) < ETH_HLEN)
5261 skb_set_network_header(skb, ETH_HLEN);
5262
5263 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005264
5265 return err;
5266}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005267EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005268
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005269/* Pop a vlan tag either from hwaccel or from payload.
5270 * Expects skb->data at mac header.
5271 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005272int skb_vlan_pop(struct sk_buff *skb)
5273{
5274 u16 vlan_tci;
5275 __be16 vlan_proto;
5276 int err;
5277
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005278 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005279 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005280 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005281 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005282 return 0;
5283
5284 err = __skb_vlan_pop(skb, &vlan_tci);
5285 if (err)
5286 return err;
5287 }
5288 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005289 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005290 return 0;
5291
5292 vlan_proto = skb->protocol;
5293 err = __skb_vlan_pop(skb, &vlan_tci);
5294 if (unlikely(err))
5295 return err;
5296
5297 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5298 return 0;
5299}
5300EXPORT_SYMBOL(skb_vlan_pop);
5301
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005302/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5303 * Expects skb->data at mac header.
5304 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005305int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5306{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005307 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005308 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005309 int err;
5310
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005311 if (WARN_ONCE(offset,
5312 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5313 offset)) {
5314 return -EINVAL;
5315 }
5316
Jiri Pirko93515d52014-11-19 14:05:02 +01005317 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005318 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005319 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005320 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005321
Jiri Pirko93515d52014-11-19 14:05:02 +01005322 skb->protocol = skb->vlan_proto;
5323 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005324
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005325 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005326 }
5327 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5328 return 0;
5329}
5330EXPORT_SYMBOL(skb_vlan_push);
5331
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005332/**
5333 * alloc_skb_with_frags - allocate skb with page frags
5334 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09005335 * @header_len: size of linear part
5336 * @data_len: needed length in frags
5337 * @max_page_order: max page order desired.
5338 * @errcode: pointer to error code if any
5339 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005340 *
5341 * This can be used to allocate a paged skb, given a maximal order for frags.
5342 */
5343struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5344 unsigned long data_len,
5345 int max_page_order,
5346 int *errcode,
5347 gfp_t gfp_mask)
5348{
5349 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5350 unsigned long chunk;
5351 struct sk_buff *skb;
5352 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005353 int i;
5354
5355 *errcode = -EMSGSIZE;
5356 /* Note this test could be relaxed, if we succeed to allocate
5357 * high order pages...
5358 */
5359 if (npages > MAX_SKB_FRAGS)
5360 return NULL;
5361
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005362 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08005363 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005364 if (!skb)
5365 return NULL;
5366
5367 skb->truesize += npages << PAGE_SHIFT;
5368
5369 for (i = 0; npages > 0; i++) {
5370 int order = max_page_order;
5371
5372 while (order) {
5373 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08005374 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005375 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02005376 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005377 order);
5378 if (page)
5379 goto fill_page;
5380 /* Do not retry other high order allocations */
5381 order = 1;
5382 max_page_order = 0;
5383 }
5384 order--;
5385 }
5386 page = alloc_page(gfp_mask);
5387 if (!page)
5388 goto failure;
5389fill_page:
5390 chunk = min_t(unsigned long, data_len,
5391 PAGE_SIZE << order);
5392 skb_fill_page_desc(skb, i, page, 0, chunk);
5393 data_len -= chunk;
5394 npages -= 1 << order;
5395 }
5396 return skb;
5397
5398failure:
5399 kfree_skb(skb);
5400 return NULL;
5401}
5402EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005403
5404/* carve out the first off bytes from skb when off < headlen */
5405static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
5406 const int headlen, gfp_t gfp_mask)
5407{
5408 int i;
5409 int size = skb_end_offset(skb);
5410 int new_hlen = headlen - off;
5411 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005412
5413 size = SKB_DATA_ALIGN(size);
5414
5415 if (skb_pfmemalloc(skb))
5416 gfp_mask |= __GFP_MEMALLOC;
5417 data = kmalloc_reserve(size +
5418 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5419 gfp_mask, NUMA_NO_NODE, NULL);
5420 if (!data)
5421 return -ENOMEM;
5422
5423 size = SKB_WITH_OVERHEAD(ksize(data));
5424
5425 /* Copy real data, and all frags */
5426 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
5427 skb->len -= off;
5428
5429 memcpy((struct skb_shared_info *)(data + size),
5430 skb_shinfo(skb),
5431 offsetof(struct skb_shared_info,
5432 frags[skb_shinfo(skb)->nr_frags]));
5433 if (skb_cloned(skb)) {
5434 /* drop the old head gracefully */
5435 if (skb_orphan_frags(skb, gfp_mask)) {
5436 kfree(data);
5437 return -ENOMEM;
5438 }
5439 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
5440 skb_frag_ref(skb, i);
5441 if (skb_has_frag_list(skb))
5442 skb_clone_fraglist(skb);
5443 skb_release_data(skb);
5444 } else {
5445 /* we can reuse existing recount- all we did was
5446 * relocate values
5447 */
5448 skb_free_head(skb);
5449 }
5450
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005451 skb->head = data;
5452 skb->data = data;
5453 skb->head_frag = 0;
5454#ifdef NET_SKBUFF_DATA_USES_OFFSET
5455 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005456#else
5457 skb->end = skb->head + size;
5458#endif
5459 skb_set_tail_pointer(skb, skb_headlen(skb));
5460 skb_headers_offset_update(skb, 0);
5461 skb->cloned = 0;
5462 skb->hdr_len = 0;
5463 skb->nohdr = 0;
5464 atomic_set(&skb_shinfo(skb)->dataref, 1);
5465
5466 return 0;
5467}
5468
5469static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
5470
5471/* carve out the first eat bytes from skb's frag_list. May recurse into
5472 * pskb_carve()
5473 */
5474static int pskb_carve_frag_list(struct sk_buff *skb,
5475 struct skb_shared_info *shinfo, int eat,
5476 gfp_t gfp_mask)
5477{
5478 struct sk_buff *list = shinfo->frag_list;
5479 struct sk_buff *clone = NULL;
5480 struct sk_buff *insp = NULL;
5481
5482 do {
5483 if (!list) {
5484 pr_err("Not enough bytes to eat. Want %d\n", eat);
5485 return -EFAULT;
5486 }
5487 if (list->len <= eat) {
5488 /* Eaten as whole. */
5489 eat -= list->len;
5490 list = list->next;
5491 insp = list;
5492 } else {
5493 /* Eaten partially. */
5494 if (skb_shared(list)) {
5495 clone = skb_clone(list, gfp_mask);
5496 if (!clone)
5497 return -ENOMEM;
5498 insp = list->next;
5499 list = clone;
5500 } else {
5501 /* This may be pulled without problems. */
5502 insp = list;
5503 }
5504 if (pskb_carve(list, eat, gfp_mask) < 0) {
5505 kfree_skb(clone);
5506 return -ENOMEM;
5507 }
5508 break;
5509 }
5510 } while (eat);
5511
5512 /* Free pulled out fragments. */
5513 while ((list = shinfo->frag_list) != insp) {
5514 shinfo->frag_list = list->next;
5515 kfree_skb(list);
5516 }
5517 /* And insert new clone at head. */
5518 if (clone) {
5519 clone->next = list;
5520 shinfo->frag_list = clone;
5521 }
5522 return 0;
5523}
5524
5525/* carve off first len bytes from skb. Split line (off) is in the
5526 * non-linear part of skb
5527 */
5528static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
5529 int pos, gfp_t gfp_mask)
5530{
5531 int i, k = 0;
5532 int size = skb_end_offset(skb);
5533 u8 *data;
5534 const int nfrags = skb_shinfo(skb)->nr_frags;
5535 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005536
5537 size = SKB_DATA_ALIGN(size);
5538
5539 if (skb_pfmemalloc(skb))
5540 gfp_mask |= __GFP_MEMALLOC;
5541 data = kmalloc_reserve(size +
5542 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
5543 gfp_mask, NUMA_NO_NODE, NULL);
5544 if (!data)
5545 return -ENOMEM;
5546
5547 size = SKB_WITH_OVERHEAD(ksize(data));
5548
5549 memcpy((struct skb_shared_info *)(data + size),
5550 skb_shinfo(skb), offsetof(struct skb_shared_info,
5551 frags[skb_shinfo(skb)->nr_frags]));
5552 if (skb_orphan_frags(skb, gfp_mask)) {
5553 kfree(data);
5554 return -ENOMEM;
5555 }
5556 shinfo = (struct skb_shared_info *)(data + size);
5557 for (i = 0; i < nfrags; i++) {
5558 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
5559
5560 if (pos + fsize > off) {
5561 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5562
5563 if (pos < off) {
5564 /* Split frag.
5565 * We have two variants in this case:
5566 * 1. Move all the frag to the second
5567 * part, if it is possible. F.e.
5568 * this approach is mandatory for TUX,
5569 * where splitting is expensive.
5570 * 2. Split is accurately. We make this.
5571 */
5572 shinfo->frags[0].page_offset += off - pos;
5573 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5574 }
5575 skb_frag_ref(skb, i);
5576 k++;
5577 }
5578 pos += fsize;
5579 }
5580 shinfo->nr_frags = k;
5581 if (skb_has_frag_list(skb))
5582 skb_clone_fraglist(skb);
5583
5584 if (k == 0) {
5585 /* split line is in frag list */
5586 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
5587 }
5588 skb_release_data(skb);
5589
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005590 skb->head = data;
5591 skb->head_frag = 0;
5592 skb->data = data;
5593#ifdef NET_SKBUFF_DATA_USES_OFFSET
5594 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07005595#else
5596 skb->end = skb->head + size;
5597#endif
5598 skb_reset_tail_pointer(skb);
5599 skb_headers_offset_update(skb, 0);
5600 skb->cloned = 0;
5601 skb->hdr_len = 0;
5602 skb->nohdr = 0;
5603 skb->len -= off;
5604 skb->data_len = skb->len;
5605 atomic_set(&skb_shinfo(skb)->dataref, 1);
5606 return 0;
5607}
5608
5609/* remove len bytes from the beginning of the skb */
5610static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5611{
5612 int headlen = skb_headlen(skb);
5613
5614 if (len < headlen)
5615 return pskb_carve_inside_header(skb, len, headlen, gfp);
5616 else
5617 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5618}
5619
5620/* Extract to_copy bytes starting at off from skb, and return this in
5621 * a new skb
5622 */
5623struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5624 int to_copy, gfp_t gfp)
5625{
5626 struct sk_buff *clone = skb_clone(skb, gfp);
5627
5628 if (!clone)
5629 return NULL;
5630
5631 if (pskb_carve(clone, off, gfp) < 0 ||
5632 pskb_trim(clone, to_copy)) {
5633 kfree_skb(clone);
5634 return NULL;
5635 }
5636 return clone;
5637}
5638EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005639
5640/**
5641 * skb_condense - try to get rid of fragments/frag_list if possible
5642 * @skb: buffer
5643 *
5644 * Can be used to save memory before skb is added to a busy queue.
5645 * If packet has bytes in frags and enough tail room in skb->head,
5646 * pull all of them, so that we can free the frags right now and adjust
5647 * truesize.
5648 * Notes:
5649 * We do not reallocate skb->head thus can not fail.
5650 * Caller must re-evaluate skb->truesize if needed.
5651 */
5652void skb_condense(struct sk_buff *skb)
5653{
Eric Dumazet3174fed2016-12-09 08:02:05 -08005654 if (skb->data_len) {
5655 if (skb->data_len > skb->end - skb->tail ||
5656 skb_cloned(skb))
5657 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005658
Eric Dumazet3174fed2016-12-09 08:02:05 -08005659 /* Nice, we can free page frag(s) right now */
5660 __pskb_pull_tail(skb, skb->data_len);
5661 }
5662 /* At this point, skb->truesize might be over estimated,
5663 * because skb had a fragment, and fragments do not tell
5664 * their truesize.
5665 * When we pulled its content into skb->head, fragment
5666 * was freed, but __pskb_pull_tail() could not possibly
5667 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08005668 */
5669 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
5670}
Florian Westphaldf5042f2018-12-18 17:15:16 +01005671
5672#ifdef CONFIG_SKB_EXTENSIONS
5673static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
5674{
5675 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
5676}
5677
5678static struct skb_ext *skb_ext_alloc(void)
5679{
5680 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5681
5682 if (new) {
5683 memset(new->offset, 0, sizeof(new->offset));
5684 refcount_set(&new->refcnt, 1);
5685 }
5686
5687 return new;
5688}
5689
Florian Westphal41650792018-12-18 17:15:27 +01005690static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
5691 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01005692{
5693 struct skb_ext *new;
5694
5695 if (refcount_read(&old->refcnt) == 1)
5696 return old;
5697
5698 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
5699 if (!new)
5700 return NULL;
5701
5702 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
5703 refcount_set(&new->refcnt, 1);
5704
Florian Westphal41650792018-12-18 17:15:27 +01005705#ifdef CONFIG_XFRM
5706 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
5707 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
5708 unsigned int i;
5709
5710 for (i = 0; i < sp->len; i++)
5711 xfrm_state_hold(sp->xvec[i]);
5712 }
5713#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005714 __skb_ext_put(old);
5715 return new;
5716}
5717
5718/**
5719 * skb_ext_add - allocate space for given extension, COW if needed
5720 * @skb: buffer
5721 * @id: extension to allocate space for
5722 *
5723 * Allocates enough space for the given extension.
5724 * If the extension is already present, a pointer to that extension
5725 * is returned.
5726 *
5727 * If the skb was cloned, COW applies and the returned memory can be
5728 * modified without changing the extension space of clones buffers.
5729 *
5730 * Returns pointer to the extension or NULL on allocation failure.
5731 */
5732void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
5733{
5734 struct skb_ext *new, *old = NULL;
5735 unsigned int newlen, newoff;
5736
5737 if (skb->active_extensions) {
5738 old = skb->extensions;
5739
Florian Westphal41650792018-12-18 17:15:27 +01005740 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01005741 if (!new)
5742 return NULL;
5743
Paolo Abeni682ec852018-12-21 19:03:15 +01005744 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01005745 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005746
Paolo Abenie94e50b2018-12-21 19:03:13 +01005747 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005748 } else {
5749 newoff = SKB_EXT_CHUNKSIZEOF(*new);
5750
5751 new = skb_ext_alloc();
5752 if (!new)
5753 return NULL;
5754 }
5755
5756 newlen = newoff + skb_ext_type_len[id];
5757 new->chunks = newlen;
5758 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005759set_active:
Paolo Abeni682ec852018-12-21 19:03:15 +01005760 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01005761 skb->active_extensions |= 1 << id;
5762 return skb_ext_get_ptr(new, id);
5763}
5764EXPORT_SYMBOL(skb_ext_add);
5765
Florian Westphal41650792018-12-18 17:15:27 +01005766#ifdef CONFIG_XFRM
5767static void skb_ext_put_sp(struct sec_path *sp)
5768{
5769 unsigned int i;
5770
5771 for (i = 0; i < sp->len; i++)
5772 xfrm_state_put(sp->xvec[i]);
5773}
5774#endif
5775
Florian Westphaldf5042f2018-12-18 17:15:16 +01005776void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
5777{
5778 struct skb_ext *ext = skb->extensions;
5779
5780 skb->active_extensions &= ~(1 << id);
5781 if (skb->active_extensions == 0) {
5782 skb->extensions = NULL;
5783 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01005784#ifdef CONFIG_XFRM
5785 } else if (id == SKB_EXT_SEC_PATH &&
5786 refcount_read(&ext->refcnt) == 1) {
5787 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
5788
5789 skb_ext_put_sp(sp);
5790 sp->len = 0;
5791#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01005792 }
5793}
5794EXPORT_SYMBOL(__skb_ext_del);
5795
5796void __skb_ext_put(struct skb_ext *ext)
5797{
5798 /* If this is last clone, nothing can increment
5799 * it after check passes. Avoids one atomic op.
5800 */
5801 if (refcount_read(&ext->refcnt) == 1)
5802 goto free_now;
5803
5804 if (!refcount_dec_and_test(&ext->refcnt))
5805 return;
5806free_now:
Florian Westphal41650792018-12-18 17:15:27 +01005807#ifdef CONFIG_XFRM
5808 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
5809 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
5810#endif
5811
Florian Westphaldf5042f2018-12-18 17:15:16 +01005812 kmem_cache_free(skbuff_ext_cache, ext);
5813}
5814EXPORT_SYMBOL(__skb_ext_put);
5815#endif /* CONFIG_SKB_EXTENSIONS */